OSDN Git Service

2008-08-22 Andrew Haley <aph@redhat.com>
[pf3gnuchains/gcc-fork.git] / libjava / include / jvm.h
1 // jvm.h - Header file for private implementation information. -*- c++ -*-
2
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 #ifndef __JAVA_JVM_H__
12 #define __JAVA_JVM_H__
13
14 // Define this before including jni.h.
15 // jni.h is included by jvmpi.h, which might be included.  We define
16 // this unconditionally because it is convenient and it lets other
17 // files include jni.h without difficulty.
18 #define __GCJ_JNI_IMPL__
19
20 #include <gcj/javaprims.h>
21
22 #include <java-assert.h>
23 #include <java-threads.h>
24 // Must include java-gc.h before Object.h for the implementation.
25 #include <java-gc.h>
26
27 #include <java/lang/Object.h>
28
29 // Include cni.h before field.h to enable all definitions.  FIXME.
30 #include <gcj/cni.h>
31 #include <gcj/field.h>
32
33 #include <java/lang/Thread.h>
34
35 #include <sysdep/locks.h>
36
37 /* Macro for possible unused arguments.  */
38 #define MAYBE_UNUSED __attribute__((__unused__))
39
40 /* Structure of the virtual table.  */
41 struct _Jv_VTable
42 {
43 #ifdef __ia64__
44   typedef struct { void *pc, *gp; } vtable_elt;
45 #else
46   typedef void *vtable_elt;
47 #endif
48   jclass clas;
49   void *gc_descr;
50
51   // This must be last, as derived classes "extend" this by
52   // adding new data members.
53   vtable_elt method[1];
54
55 #ifdef __ia64__
56   void *get_method(int i) { return &method[i]; }
57   void set_method(int i, void *fptr) { method[i] = *(vtable_elt *)fptr; }
58   void *get_finalizer()
59   {
60     // We know that get_finalizer is only used for checking whether
61     // this object needs to have a finalizer registered.  So it is
62     // safe to simply return just the PC component of the vtable
63     // slot.
64     return ((vtable_elt *)(get_method(0)))->pc;
65   }
66 #else
67   void *get_method(int i) { return method[i]; }
68   void set_method(int i, void *fptr) { method[i] = fptr; }
69   void *get_finalizer() { return get_method(0); }
70 #endif
71
72   static size_t vtable_elt_size() { return sizeof(vtable_elt); }
73
74   // Given a method index, return byte offset from the vtable pointer.
75   static jint idx_to_offset (int index)
76   {
77     return (2 * sizeof (void *)) + (index * vtable_elt_size ());
78   }
79
80   static _Jv_VTable *new_vtable (int count);
81 };
82
83 union _Jv_word
84 {
85   jobject o;
86   jint i;                       // Also stores smaller integral types.
87   jfloat f;
88   jint ia[1];                   // Half of _Jv_word2.
89   void* p;
90
91 #if SIZEOF_VOID_P == 8
92   // We can safely put a long or a double in here without increasing
93   // the size of _Jv_Word; we take advantage of this in the interpreter.
94   jlong l;
95   jdouble d;
96 #endif
97
98   jclass                     clazz;
99   jstring                    string;
100   struct _Jv_Field          *field;
101   struct _Jv_Utf8Const      *utf8;
102   struct _Jv_ResolvedMethod *rmethod;
103 };
104
105 union _Jv_word2
106 {
107   jint ia[2];
108   jlong l;
109   jdouble d;
110 };                              
111
112 union _Jv_value
113 {
114   jbyte byte_value;
115   jshort short_value;
116   jchar char_value;
117   jint int_value;
118   jlong long_value;
119   jfloat float_value;
120   jdouble double_value;
121   jobject object_value;
122 };
123
124 /* Extract a character from a Java-style Utf8 string.
125  * PTR points to the current character.
126  * LIMIT points to the end of the Utf8 string.
127  * PTR is incremented to point after the character thta gets returns.
128  * On an error, -1 is returned. */
129 #define UTF8_GET(PTR, LIMIT) \
130   ((PTR) >= (LIMIT) ? -1 \
131    : *(PTR) < 128 ? *(PTR)++ \
132    : (*(PTR)&0xE0) == 0xC0 && ((PTR)+=2)<=(LIMIT) && ((PTR)[-1]&0xC0) == 0x80 \
133    ? (((PTR)[-2] & 0x1F) << 6) + ((PTR)[-1] & 0x3F) \
134    : (*(PTR) & 0xF0) == 0xE0 && ((PTR) += 3) <= (LIMIT) \
135    && ((PTR)[-2] & 0xC0) == 0x80 && ((PTR)[-1] & 0xC0) == 0x80 \
136    ? (((PTR)[-3]&0x0F) << 12) + (((PTR)[-2]&0x3F) << 6) + ((PTR)[-1]&0x3F) \
137    : ((PTR)++, -1))
138
139 extern int _Jv_strLengthUtf8(const char* str, int len);
140
141 typedef struct _Jv_Utf8Const Utf8Const;
142 _Jv_Utf8Const *_Jv_makeUtf8Const (const char *s, int len);
143 _Jv_Utf8Const *_Jv_makeUtf8Const (jstring string);
144 static inline _Jv_Utf8Const *_Jv_makeUtf8Const (const char *s)
145 {
146   return _Jv_makeUtf8Const (s, strlen (s));
147 }
148 extern jboolean _Jv_equalUtf8Consts (const _Jv_Utf8Const *, const _Jv_Utf8Const *);
149 extern jboolean _Jv_equal (_Jv_Utf8Const *, jstring, jint);
150 extern jboolean _Jv_equaln (_Jv_Utf8Const *, jstring, jint);
151
152 /* Helper class which converts a jstring to a temporary char*.
153    Uses the supplied buffer, if non-null. Otherwise, allocates
154    the buffer on the heap. Use the JV_TEMP_UTF_STRING macro,
155    which follows, to automatically allocate a stack buffer if
156    the string is small enough. */
157 class _Jv_TempUTFString
158 {
159 public:
160   _Jv_TempUTFString(jstring jstr, char* buf=0);
161   ~_Jv_TempUTFString();
162
163 // Accessors
164   operator const char*() const
165   {
166     return buf_;
167   }
168   const char* buf() const
169   {
170     return buf_;
171   }
172   char* buf()
173   {
174     return buf_;
175   }
176
177 private:
178   char* buf_;
179   bool heapAllocated_;
180 };
181
182 inline _Jv_TempUTFString::_Jv_TempUTFString (jstring jstr, char* buf)
183   : buf_(0), heapAllocated_(false)
184 {
185   if (!jstr) return;
186   jsize len = JvGetStringUTFLength (jstr);
187   if (buf)
188     buf_ = buf;
189   else
190     {
191       buf_ = (char*) _Jv_Malloc (len+1);
192       heapAllocated_ = true;
193     }
194
195   JvGetStringUTFRegion (jstr, 0, jstr->length(), buf_);
196   buf_[len] = '\0';
197 }
198
199 inline _Jv_TempUTFString::~_Jv_TempUTFString ()
200 {
201   if (heapAllocated_)
202     _Jv_Free (buf_);
203 }
204
205 /* Macro which uses _Jv_TempUTFString. Allocates a stack-based
206    buffer if the string and its null terminator are <= 256
207    characters in length. Otherwise, a heap-based buffer is
208    used. The parameters to this macro are the variable name
209    which is an instance of _Jv_TempUTFString (above) and a
210    jstring.
211    
212    Sample Usage:
213    
214    jstring jstr = getAJString();
215    JV_TEMP_UTF_STRING(utfstr, jstr);
216    printf("The string is: %s\n", utfstr.buf());
217    
218  */
219 #define JV_TEMP_UTF_STRING(utfstr, jstr) \
220   jstring utfstr##thejstr = (jstr); \
221   jsize utfstr##_len = utfstr##thejstr ? JvGetStringUTFLength (utfstr##thejstr) + 1 : 0; \
222   char utfstr##_buf[utfstr##_len <= 256 ? utfstr##_len : 0]; \
223   _Jv_TempUTFString utfstr(utfstr##thejstr, sizeof(utfstr##_buf)==0 ? 0 : utfstr##_buf)
224
225 namespace gcj
226 {
227   /* Some constants used during lookup of special class methods.  */
228   extern _Jv_Utf8Const *void_signature; /* "()V" */
229   extern _Jv_Utf8Const *clinit_name;    /* "<clinit>" */
230   extern _Jv_Utf8Const *init_name;      /* "<init>" */
231   extern _Jv_Utf8Const *finit_name;     /* "finit$", */
232   
233   /* Set to true by _Jv_CreateJavaVM. */
234   extern bool runtimeInitialized;
235
236   /* Print out class names as they are initialized. */
237   extern bool verbose_class_flag;
238   
239   /* When true, enable the bytecode verifier and BC-ABI verification. */
240   extern bool verifyClasses;
241
242   /* Thread stack size specified by the -Xss runtime argument. */
243   extern size_t stack_size;
244
245   /* The start time */
246   extern jlong startTime;
247   
248   /* The VM arguments */
249   extern JArray<jstring>* vmArgs;
250
251   // Currently loaded classes
252   extern jint loadedClasses;
253
254   // Unloaded classes
255   extern jlong unloadedClasses;
256 }
257
258 // This class handles all aspects of class preparation and linking.
259 class _Jv_Linker
260 {
261 private:
262   typedef unsigned int uaddr __attribute__ ((mode (pointer)));
263
264   static _Jv_Field *find_field_helper(jclass, _Jv_Utf8Const *, _Jv_Utf8Const *,
265                                       jclass, jclass *);
266   static _Jv_Field *find_field(jclass, jclass, jclass *, _Jv_Utf8Const *,
267                                _Jv_Utf8Const *);
268   static void check_loading_constraints (_Jv_Method *, jclass, jclass);
269   static void prepare_constant_time_tables(jclass);
270   static jshort get_interfaces(jclass, _Jv_ifaces *);
271   static void link_symbol_table(jclass);
272   static void link_exception_table(jclass);
273   static void layout_interface_methods(jclass);
274   static void set_vtable_entries(jclass, _Jv_VTable *);
275   static void make_vtable(jclass);
276   static void ensure_fields_laid_out(jclass);
277   static void ensure_class_linked(jclass);
278   static void ensure_supers_installed(jclass);
279   static void add_miranda_methods(jclass, jclass);
280   static void ensure_method_table_complete(jclass);
281   static void verify_class(jclass);
282   static jshort find_iindex(jclass *, jshort *, jshort);
283   static jshort indexof(void *, void **, jshort);
284   static int get_alignment_from_class(jclass);
285   static void generate_itable(jclass, _Jv_ifaces *, jshort *);
286   static jshort append_partial_itable(jclass, jclass, void **, jshort);
287   static _Jv_Method *search_method_in_superclasses (jclass cls, jclass klass, 
288                                                     _Jv_Utf8Const *method_name,
289                                                     _Jv_Utf8Const *method_signature,
290                                                     jclass *found_class,
291                                                     bool check_perms = true);
292   static void *create_error_method(_Jv_Utf8Const *, jclass);
293
294   /* The least significant bit of the signature pointer in a symbol
295      table is set to 1 by the compiler if the reference is "special",
296      i.e. if it is an access to a private field or method.  Extract
297      that bit, clearing it in the address and setting the LSB of
298      SPECIAL accordingly.  */
299   static void maybe_adjust_signature (_Jv_Utf8Const *&s, uaddr &special)
300   {
301     union {
302       _Jv_Utf8Const *signature;
303       uaddr signature_bits;
304     };
305     signature = s;
306     special = signature_bits & 1;
307     signature_bits -= special;
308     s = signature;
309   }  
310
311   static _Jv_Mutex_t resolve_mutex;
312   static void init (void) __attribute__((constructor));
313
314 public:
315
316   static bool has_field_p (jclass, _Jv_Utf8Const *);
317   static void print_class_loaded (jclass);
318   static void resolve_class_ref (jclass, jclass *);
319   static void wait_for_state(jclass, int);
320   static _Jv_Method *resolve_method_entry (jclass, jclass &,
321                                            int, int,
322                                            bool, bool);
323   static _Jv_word resolve_pool_entry (jclass, int, bool =false);
324   static void resolve_field (_Jv_Field *, java::lang::ClassLoader *);
325   static void verify_type_assertions (jclass);
326   static _Jv_Method *search_method_in_class (jclass, jclass,
327                                              _Jv_Utf8Const *,
328                                              _Jv_Utf8Const *,
329                                              bool check_perms = true);
330   static void layout_vtable_methods(jclass);
331
332   static jbyte read_cpool_entry (_Jv_word *data,
333                                  const _Jv_Constants *const pool,
334                                  int index)
335   {
336     _Jv_MutexLock (&resolve_mutex);
337     jbyte tags = pool->tags[index];
338     *data = pool->data[index];
339     _Jv_MutexUnlock (&resolve_mutex);
340     return tags;
341   }
342
343   static void write_cpool_entry (_Jv_word data, jbyte tags,
344                                  _Jv_Constants *pool,
345                                  int index)
346   {
347     _Jv_MutexLock (&resolve_mutex);
348     pool->data[index] = data;
349     pool->tags[index] = tags;
350     _Jv_MutexUnlock (&resolve_mutex);
351   }
352 };
353
354 /* Type of pointer used as finalizer.  */
355 typedef void _Jv_FinalizerFunc (jobject);
356
357 /* Allocate space for a new Java object.  */
358 void *_Jv_AllocObj (jsize size, jclass cl) __attribute__((__malloc__));
359 /* Allocate space for a potentially uninitialized pointer-free object.
360    Interesting only with JV_HASH_SYNCHRONIZATION.  */
361 void *_Jv_AllocPtrFreeObj (jsize size, jclass cl) __attribute__((__malloc__));
362 /* Allocate space for an array of Java objects.  */
363 void *_Jv_AllocArray (jsize size, jclass cl) __attribute__((__malloc__));
364 /* Allocate space that is known to be pointer-free.  */
365 void *_Jv_AllocBytes (jsize size) __attribute__((__malloc__));
366 /* Allocate space for a new non-Java object, which does not have the usual 
367    Java object header but may contain pointers to other GC'ed objects.  */
368 void *_Jv_AllocRawObj (jsize size) __attribute__((__malloc__));
369 /* Allocate a double-indirect pointer to a _Jv_ClosureList such that
370    the _Jv_ClosureList gets automatically finalized when it is no
371    longer reachable, not even by other finalizable objects.  */
372 _Jv_ClosureList **_Jv_ClosureListFinalizer (void) __attribute__((__malloc__));
373 /* Explicitly throw an out-of-memory exception. */
374 void _Jv_ThrowNoMemory() __attribute__((__noreturn__));
375 /* Allocate an object with a single pointer.  The first word is reserved
376    for the GC, and the second word is the traced pointer.  */
377 void *_Jv_AllocTraceOne (jsize size /* incl. reserved slot */);
378 /* Ditto, but for two traced pointers.                     */
379 void *_Jv_AllocTraceTwo (jsize size /* incl. reserved slot */);
380 /* Initialize the GC.  */
381 void _Jv_InitGC (void);
382 /* Register a finalizer.  */
383 void _Jv_RegisterFinalizer (void *object, _Jv_FinalizerFunc *method);
384 /* Compute the GC descriptor for a class */
385 void * _Jv_BuildGCDescr(jclass);
386
387 /* Allocate some unscanned, unmoveable memory.  Return NULL if out of
388    memory.  */
389 void *_Jv_MallocUnchecked (jsize size) __attribute__((__malloc__));
390
391 /* Initialize finalizers.  The argument is a function to be called
392    when a finalizer is ready to be run.  */
393 void _Jv_GCInitializeFinalizers (void (*notifier) (void));
394 /* Run finalizers for objects ready to be finalized..  */
395 void _Jv_RunFinalizers (void);
396 /* Run all finalizers.  Should be called only before exit.  */
397 void _Jv_RunAllFinalizers (void);
398 /* Perform a GC.  */
399 void _Jv_RunGC (void);
400 /* Disable and enable GC.  */
401 void _Jv_DisableGC (void);
402 void _Jv_EnableGC (void);
403 /* Register a disappearing link.  This is a field F which should be
404    cleared when *F is found to be inaccessible.  This is used in the
405    implementation of java.lang.ref.Reference.  */
406 void _Jv_GCRegisterDisappearingLink (jobject *objp);
407 /* Return true if OBJECT should be reclaimed.  This is used to
408    implement soft references.  */
409 jboolean _Jv_GCCanReclaimSoftReference (jobject obj);
410
411 /* Register a finalizer for a String object.  This is only used by
412    the intern() implementation.  */
413 void _Jv_RegisterStringFinalizer (jobject str);
414 /* This is called to actually finalize a possibly-intern()d String.  */
415 void _Jv_FinalizeString (jobject str);
416
417 /* Return approximation of total size of heap.  */
418 long _Jv_GCTotalMemory (void);
419 /* Return approximation of total free memory.  */
420 long _Jv_GCFreeMemory (void);
421
422 /* Set initial heap size.  If SIZE==0, ignore.  Should be run before
423    _Jv_InitGC.  Not required to have any actual effect.  */
424 void _Jv_GCSetInitialHeapSize (size_t size);
425
426 /* Set maximum heap size.  If SIZE==0, unbounded.  Should be run
427    before _Jv_InitGC.  Not required to have any actual effect.  */
428 void _Jv_GCSetMaximumHeapSize (size_t size);
429
430 /* External interface to setting the heap size.  Parses ARG (a number
431    which can optionally have "k" or "m" appended and calls
432    _Jv_GCSetInitialHeapSize.  */
433 void _Jv_SetInitialHeapSize (const char *arg);
434
435 /* External interface to setting the maximum heap size.  Parses ARG (a
436    number which can optionally have "k" or "m" appended and calls
437    _Jv_GCSetMaximumHeapSize.  */
438 void _Jv_SetMaximumHeapSize (const char *arg);
439
440 /* External interface for setting the GC_free_space_divisor.  Calls
441    GC_set_free_space_divisor and returns the old value.  */
442 int _Jv_SetGCFreeSpaceDivisor (int div);
443
444 /* Free the method cache, if one was allocated.  This is only called
445    during thread deregistration.  */
446 void _Jv_FreeMethodCache ();
447
448 /* Set the stack size for threads.  Parses ARG, a number which can 
449    optionally have "k" or "m" appended.  */
450 void _Jv_SetStackSize (const char *arg);
451
452 extern "C" void JvRunMain (jclass klass, int argc, const char **argv);
453 extern "C" void JvRunMainName (const char *name, int argc, const char **argv);
454
455 void _Jv_RunMain (jclass klass, const char *name, int argc, const char **argv, 
456                   bool is_jar);
457
458 void _Jv_RunMain (struct _Jv_VMInitArgs *vm_args, jclass klass,
459                   const char *name, int argc, const char **argv, bool is_jar);
460
461 // Delayed until after _Jv_AllocRawObj is declared.
462 inline _Jv_VTable *
463 _Jv_VTable::new_vtable (int count)
464 {
465   size_t size = sizeof(_Jv_VTable) + (count - 1) * vtable_elt_size ();
466   return (_Jv_VTable *) _Jv_AllocRawObj (size);
467 }
468
469 // Determine if METH gets an entry in a VTable.
470 static inline jboolean _Jv_isVirtualMethod (_Jv_Method *meth)
471 {
472   using namespace java::lang::reflect;
473   return (((meth->accflags & (Modifier::STATIC | Modifier::PRIVATE)) == 0)
474           && meth->name->first() != '<');
475 }
476
477 // This function is used to determine the hash code of an object.
478 inline jint
479 _Jv_HashCode (jobject obj)
480 {
481   // This was chosen to yield relatively well distributed results on
482   // both 32- and 64-bit architectures.  Note 0x7fffffff is prime.
483   // FIXME: we assume sizeof(long) == sizeof(void *).
484   return (jint) ((unsigned long) obj % 0x7fffffff);
485 }
486
487 // Return a raw pointer to the elements of an array given the array
488 // and its element type.  You might think we could just pick a single
489 // array type and use elements() on it, but we can't because we must
490 // account for alignment of the element type.  When ARRAY is null, we
491 // obtain the number of bytes taken by the base part of the array.
492 inline char *
493 _Jv_GetArrayElementFromElementType (jobject array,
494                                     jclass element_type)
495 {
496   char *elts;
497   if (element_type == JvPrimClass (byte))
498     elts = (char *) elements ((jbyteArray) array);
499   else if (element_type == JvPrimClass (short))
500     elts = (char *) elements ((jshortArray) array);
501   else if (element_type == JvPrimClass (int))
502     elts = (char *) elements ((jintArray) array);
503   else if (element_type == JvPrimClass (long))
504     elts = (char *) elements ((jlongArray) array);
505   else if (element_type == JvPrimClass (boolean))
506     elts = (char *) elements ((jbooleanArray) array);
507   else if (element_type == JvPrimClass (char))
508     elts = (char *) elements ((jcharArray) array);
509   else if (element_type == JvPrimClass (float))
510     elts = (char *) elements ((jfloatArray) array);
511   else if (element_type == JvPrimClass (double))
512     elts = (char *) elements ((jdoubleArray) array);
513   else
514     elts = (char *) elements ((jobjectArray) array);
515   return elts;
516 }
517
518 extern "C" void _Jv_ThrowBadArrayIndex (jint bad_index)
519   __attribute__((noreturn));
520 extern "C" void _Jv_ThrowNullPointerException (void)
521   __attribute__((noreturn));
522 extern "C" void _Jv_ThrowNoSuchMethodError (void)
523   __attribute__((noreturn));
524 extern "C" void _Jv_ThrowNoSuchFieldError (int)
525   __attribute__((noreturn));
526 extern "C" jobject _Jv_NewArray (jint type, jint size)
527   __attribute__((__malloc__));
528 extern "C" jobject _Jv_NewMultiArray (jclass klass, jint dims, ...)
529   __attribute__((__malloc__));
530 extern "C" void *_Jv_CheckCast (jclass klass, jobject obj);
531 extern "C" void *_Jv_LookupInterfaceMethod (jclass klass, Utf8Const *name,
532                                            Utf8Const *signature);
533 extern "C" void *_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface, 
534                                                int meth_idx);
535 extern "C" void _Jv_CheckArrayStore (jobject array, jobject obj);
536 extern "C" void _Jv_RegisterClass (jclass klass);
537 extern "C" void _Jv_RegisterClasses (const jclass *classes);
538 extern "C" void _Jv_RegisterClasses_Counted (const jclass *classes,
539                                              size_t count);
540 extern "C" void _Jv_RegisterResource (void *vptr);
541 extern void _Jv_UnregisterClass (_Jv_Utf8Const*, java::lang::ClassLoader*);
542
543 extern "C" jobject _Jv_UnwrapJNIweakReference (jobject);
544
545 extern jclass _Jv_FindClass (_Jv_Utf8Const *name,
546                              java::lang::ClassLoader *loader);
547
548 extern jclass _Jv_FindClassNoException (_Jv_Utf8Const *name,
549                              java::lang::ClassLoader *loader);
550
551 extern jclass _Jv_FindClassFromSignature (char *,
552                                           java::lang::ClassLoader *loader,
553                                           char ** = NULL);
554
555 extern jclass _Jv_FindClassFromSignatureNoException (char *,
556                                           java::lang::ClassLoader *loader,
557                                           char ** = NULL);
558
559 extern void _Jv_GetTypesFromSignature (jmethodID method,
560                                        jclass declaringClass,
561                                        JArray<jclass> **arg_types_out,
562                                        jclass *return_type_out);
563
564 extern jboolean _Jv_CheckAccess (jclass self_klass, jclass other_klass,
565                                  jint flags);
566
567 extern jobject _Jv_CallAnyMethodA (jobject obj, jclass return_type,
568                                    jmethodID meth, jboolean is_constructor,
569                                    JArray<jclass> *parameter_types,
570                                    jobjectArray args,
571                                    jclass iface = NULL);
572
573 union jvalue;
574 extern void _Jv_CallAnyMethodA (jobject obj,
575                                 jclass return_type,
576                                 jmethodID meth,
577                                 jboolean is_constructor,
578                                 jboolean is_virtual_call,
579                                 JArray<jclass> *parameter_types,
580                                 const jvalue *args,
581                                 jvalue *result,
582                                 jboolean is_jni_call = true,
583                                 jclass iface = NULL);
584
585 extern void _Jv_CheckOrCreateLoadingConstraint (jclass,
586                                                 java::lang::ClassLoader *);
587
588 extern jobject _Jv_NewMultiArray (jclass, jint ndims, jint* dims)
589   __attribute__((__malloc__));
590
591 extern "C" void _Jv_ThrowAbstractMethodError () __attribute__((__noreturn__));
592
593 /* Checked divide subroutines. */
594 extern "C"
595 {
596   jint _Jv_divI (jint, jint);
597   jint _Jv_remI (jint, jint);
598   jlong _Jv_divJ (jlong, jlong);
599   jlong _Jv_remJ (jlong, jlong);
600 }
601
602 /* Get the number of arguments (cf. argc) or 0 if our argument
603    list was never initialized.  */
604 extern int _Jv_GetNbArgs (void);
605
606 /* Get the specified argument (cf. argv[index]) or "" if either
607    our argument list was never initialized or the specified index
608    is out of bounds.  */
609 extern const char * _Jv_GetSafeArg (int index);
610
611 /* Sets our argument list. Can be used by programs with non-standard
612    entry points.  */
613 extern void _Jv_SetArgs (int argc, const char **argv);
614
615 /* Get the name of the running executable.  */
616 extern const char *_Jv_ThisExecutable (void);
617
618 /* Return a pointer to a symbol in executable or loaded library.  */
619 void *_Jv_FindSymbolInExecutable (const char *);
620
621 /* Initialize JNI.  */
622 extern void _Jv_JNI_Init (void);
623
624 /* Get or set the per-thread JNIEnv used by the invocation API.  */
625 _Jv_JNIEnv *_Jv_GetCurrentJNIEnv ();
626 void _Jv_SetCurrentJNIEnv (_Jv_JNIEnv *);
627
628 /* Free a JNIEnv. */
629 void _Jv_FreeJNIEnv (_Jv_JNIEnv *);
630
631 extern "C" void _Jv_JNI_PopSystemFrame (_Jv_JNIEnv *);
632 _Jv_JNIEnv *_Jv_GetJNIEnvNewFrameWithLoader (::java::lang::ClassLoader *);
633
634 struct _Jv_JavaVM;
635 _Jv_JavaVM *_Jv_GetJavaVM (); 
636
637 /* Get a JVMTI environment */
638 struct _Jv_JVMTIEnv;
639 _Jv_JVMTIEnv *_Jv_GetJVMTIEnv (void);
640
641 /* Initialize JVMTI */
642 extern void _Jv_JVMTI_Init (void);
643
644 // Some verification functions from defineclass.cc.
645 bool _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig);
646 bool _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig);
647 bool _Jv_VerifyClassName (unsigned char* ptr, _Jv_ushort length);
648 bool _Jv_VerifyClassName (_Jv_Utf8Const *name);
649 bool _Jv_VerifyIdentifier (_Jv_Utf8Const *);
650 bool _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2);
651
652 struct _Jv_core_chain
653 {
654   int name_length;
655   const char *name;
656   int data_length;
657   const void *data;
658
659   struct _Jv_core_chain *next;
660 };
661
662 // This is called when new core data is loaded.
663 extern void (*_Jv_RegisterCoreHook) (_Jv_core_chain *);
664
665 _Jv_core_chain *_Jv_FindCore (_Jv_core_chain *node, jstring name);
666 void _Jv_FreeCoreChain (_Jv_core_chain *chain);
667
668 #ifdef ENABLE_JVMPI
669
670 #include "jvmpi.h"
671
672 extern void (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (JVMPI_Event *event);
673 extern void (*_Jv_JVMPI_Notify_THREAD_START) (JVMPI_Event *event);
674 extern void (*_Jv_JVMPI_Notify_THREAD_END) (JVMPI_Event *event);
675 #endif
676
677 /* FIXME: this should really be defined in some more generic place */
678 #define ROUND(V, A) (((((unsigned) (V))-1) | ((A)-1))+1)
679
680 extern void _Jv_RegisterBootstrapPackages ();
681
682 #define FLAG_BINARYCOMPAT_ABI (1<<31)  /* Class is built with the BC-ABI. */
683
684 #define FLAG_BOOTSTRAP_LOADER (1<<30)  /* Used when defining a class that 
685                                           should be loaded by the bootstrap
686                                           loader.  */
687
688 // These are used to find ABI versions we recognize.
689 #define GCJ_CXX_ABI_VERSION (__GNUC__ * 100000 + __GNUC_MINOR__ * 1000)
690
691 // This is the old-style BC version ID used by GCJ 4.0.0.
692 #define OLD_GCJ_40_BC_ABI_VERSION (4 * 10000 + 0 * 10 + 5)
693
694 // New style version IDs used by GCJ 4.0.1 and later.
695 #define GCJ_40_BC_ABI_VERSION (4 * 100000 + 0 * 1000)
696
697 void _Jv_CheckABIVersion (unsigned long value);
698
699
700 inline bool
701 _Jv_ClassForBootstrapLoader (unsigned long value)
702 {
703   return (value & FLAG_BOOTSTRAP_LOADER);
704 }
705
706 // It makes the source cleaner if we simply always define this
707 // function.  If the interpreter is not built, it will never return
708 // 'true'.
709 extern inline jboolean
710 _Jv_IsInterpretedClass (jclass c)
711 {
712   return (c->accflags & java::lang::reflect::Modifier::INTERPRETED) != 0;
713 }
714
715 // Return true if the class was compiled with the BC ABI.
716 extern inline jboolean
717 _Jv_IsBinaryCompatibilityABI (jclass c)
718 {
719   // There isn't really a better test for the ABI type at this point,
720   // that will work once the class has been registered.
721   return c->otable_syms || c->atable_syms || c->itable_syms;
722 }
723
724 // Returns whether the given class does not really exists (ie. we have no
725 // bytecode) but still allows us to do some very conservative actions.
726 // E.g. throwing a NoClassDefFoundError with the name of the missing
727 // class.
728 extern inline jboolean
729 _Jv_IsPhantomClass (jclass c)
730 {
731   return c->state == JV_STATE_PHANTOM;
732 }
733
734 // A helper function defined in prims.cc.
735 char* _Jv_PrependVersionedLibdir (char* libpath);
736
737
738 // An enum for use with JvSetThreadState.  We use a C++ enum rather
739 // than the Java enum to avoid problems with class initialization
740 // during VM bootstrap.
741 typedef enum
742 {
743   JV_BLOCKED,
744   JV_NEW,
745   JV_RUNNABLE,
746   JV_TERMINATED,
747   JV_TIMED_WAITING,
748   JV_WAITING
749 } JvThreadState;
750
751 // Temporarily set the thread's state.
752 class JvSetThreadState
753 {
754 private:
755   ::java::lang::Thread *thread;
756   jint saved;
757
758 public:
759
760   // Note that 'cthread' could be NULL -- during VM startup there may
761   // not be a Thread available.
762   JvSetThreadState(::java::lang::Thread *cthread, JvThreadState nstate)
763     : thread (cthread),
764       saved (cthread ? cthread->state : (jint)JV_NEW)
765   {
766     if (thread)
767       thread->state = nstate;
768   }
769
770   ~JvSetThreadState()
771   {
772     if (thread)
773       thread->state = saved;
774   }
775 };
776
777 // This structure is used to represent all the data the native side
778 // needs.  An object of this type is assigned to the `data' member of
779 // the Thread class.
780 struct natThread
781 {
782   // A thread is either alive, dead, or being sent a signal; if it is
783   // being sent a signal, it is also alive.  Thus, if you want to know
784   // if a thread is alive, it is sufficient to test alive_status !=
785   // THREAD_DEAD.
786   volatile obj_addr_t alive_flag;
787
788   // These are used to interrupt sleep and join calls.  We can share a
789   // condition variable here since it only ever gets notified when the thread
790   // exits.
791   _Jv_Mutex_t join_mutex;
792   _Jv_ConditionVariable_t join_cond;
793
794   // These are used by Unsafe.park() and Unsafe.unpark().
795   ParkHelper park_helper;
796
797   // This is private data for the thread system layer.
798   _Jv_Thread_t *thread;
799
800   // Each thread has its own JNI object.
801   _Jv_JNIEnv *jni_env;
802 };
803
804 #endif /* __JAVA_JVM_H__ */