OSDN Git Service

2008-01-04 Paolo Carlini <pcarlini@suse.de>
[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  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 prepare_constant_time_tables(jclass);
269   static jshort get_interfaces(jclass, _Jv_ifaces *);
270   static void link_symbol_table(jclass);
271   static void link_exception_table(jclass);
272   static void layout_interface_methods(jclass);
273   static void set_vtable_entries(jclass, _Jv_VTable *);
274   static void make_vtable(jclass);
275   static void ensure_fields_laid_out(jclass);
276   static void ensure_class_linked(jclass);
277   static void ensure_supers_installed(jclass);
278   static void add_miranda_methods(jclass, jclass);
279   static void ensure_method_table_complete(jclass);
280   static void verify_class(jclass);
281   static jshort find_iindex(jclass *, jshort *, jshort);
282   static jshort indexof(void *, void **, jshort);
283   static int get_alignment_from_class(jclass);
284   static void generate_itable(jclass, _Jv_ifaces *, jshort *);
285   static jshort append_partial_itable(jclass, jclass, void **, jshort);
286   static _Jv_Method *search_method_in_superclasses (jclass cls, jclass klass, 
287                                                     _Jv_Utf8Const *method_name,
288                                                     _Jv_Utf8Const *method_signature,
289                                                     jclass *found_class,
290                                                     bool check_perms = true);
291   static void *create_error_method(_Jv_Utf8Const *, jclass);
292
293   /* The least significant bit of the signature pointer in a symbol
294      table is set to 1 by the compiler if the reference is "special",
295      i.e. if it is an access to a private field or method.  Extract
296      that bit, clearing it in the address and setting the LSB of
297      SPECIAL accordingly.  */
298   static void maybe_adjust_signature (_Jv_Utf8Const *&s, uaddr &special)
299   {
300     union {
301       _Jv_Utf8Const *signature;
302       uaddr signature_bits;
303     };
304     signature = s;
305     special = signature_bits & 1;
306     signature_bits -= special;
307     s = signature;
308   }  
309
310 public:
311
312   static bool has_field_p (jclass, _Jv_Utf8Const *);
313   static void print_class_loaded (jclass);
314   static void resolve_class_ref (jclass, jclass *);
315   static void wait_for_state(jclass, int);
316   static _Jv_Method *resolve_method_entry (jclass, jclass &,
317                                            int, int,
318                                            bool, bool);
319   static _Jv_word resolve_pool_entry (jclass, int, bool =false);
320   static void resolve_field (_Jv_Field *, java::lang::ClassLoader *);
321   static void verify_type_assertions (jclass);
322   static _Jv_Method *search_method_in_class (jclass, jclass,
323                                              _Jv_Utf8Const *,
324                                              _Jv_Utf8Const *,
325                                              bool check_perms = true);
326   static void layout_vtable_methods(jclass);
327 };
328
329 /* Type of pointer used as finalizer.  */
330 typedef void _Jv_FinalizerFunc (jobject);
331
332 /* Allocate space for a new Java object.  */
333 void *_Jv_AllocObj (jsize size, jclass cl) __attribute__((__malloc__));
334 /* Allocate space for a potentially uninitialized pointer-free object.
335    Interesting only with JV_HASH_SYNCHRONIZATION.  */
336 void *_Jv_AllocPtrFreeObj (jsize size, jclass cl) __attribute__((__malloc__));
337 /* Allocate space for an array of Java objects.  */
338 void *_Jv_AllocArray (jsize size, jclass cl) __attribute__((__malloc__));
339 /* Allocate space that is known to be pointer-free.  */
340 void *_Jv_AllocBytes (jsize size) __attribute__((__malloc__));
341 /* Allocate space for a new non-Java object, which does not have the usual 
342    Java object header but may contain pointers to other GC'ed objects.  */
343 void *_Jv_AllocRawObj (jsize size) __attribute__((__malloc__));
344 /* Allocate a double-indirect pointer to a _Jv_ClosureList such that
345    the _Jv_ClosureList gets automatically finalized when it is no
346    longer reachable, not even by other finalizable objects.  */
347 _Jv_ClosureList **_Jv_ClosureListFinalizer (void) __attribute__((__malloc__));
348 /* Explicitly throw an out-of-memory exception. */
349 void _Jv_ThrowNoMemory() __attribute__((__noreturn__));
350 /* Allocate an object with a single pointer.  The first word is reserved
351    for the GC, and the second word is the traced pointer.  */
352 void *_Jv_AllocTraceOne (jsize size /* incl. reserved slot */);
353 /* Ditto, but for two traced pointers.                     */
354 void *_Jv_AllocTraceTwo (jsize size /* incl. reserved slot */);
355 /* Initialize the GC.  */
356 void _Jv_InitGC (void);
357 /* Register a finalizer.  */
358 void _Jv_RegisterFinalizer (void *object, _Jv_FinalizerFunc *method);
359 /* Compute the GC descriptor for a class */
360 void * _Jv_BuildGCDescr(jclass);
361
362 /* Allocate some unscanned, unmoveable memory.  Return NULL if out of
363    memory.  */
364 void *_Jv_MallocUnchecked (jsize size) __attribute__((__malloc__));
365
366 /* Initialize finalizers.  The argument is a function to be called
367    when a finalizer is ready to be run.  */
368 void _Jv_GCInitializeFinalizers (void (*notifier) (void));
369 /* Run finalizers for objects ready to be finalized..  */
370 void _Jv_RunFinalizers (void);
371 /* Run all finalizers.  Should be called only before exit.  */
372 void _Jv_RunAllFinalizers (void);
373 /* Perform a GC.  */
374 void _Jv_RunGC (void);
375 /* Disable and enable GC.  */
376 void _Jv_DisableGC (void);
377 void _Jv_EnableGC (void);
378 /* Register a disappearing link.  This is a field F which should be
379    cleared when *F is found to be inaccessible.  This is used in the
380    implementation of java.lang.ref.Reference.  */
381 void _Jv_GCRegisterDisappearingLink (jobject *objp);
382 /* Return true if OBJECT should be reclaimed.  This is used to
383    implement soft references.  */
384 jboolean _Jv_GCCanReclaimSoftReference (jobject obj);
385
386 /* Register a finalizer for a String object.  This is only used by
387    the intern() implementation.  */
388 void _Jv_RegisterStringFinalizer (jobject str);
389 /* This is called to actually finalize a possibly-intern()d String.  */
390 void _Jv_FinalizeString (jobject str);
391
392 /* Return approximation of total size of heap.  */
393 long _Jv_GCTotalMemory (void);
394 /* Return approximation of total free memory.  */
395 long _Jv_GCFreeMemory (void);
396
397 /* Set initial heap size.  If SIZE==0, ignore.  Should be run before
398    _Jv_InitGC.  Not required to have any actual effect.  */
399 void _Jv_GCSetInitialHeapSize (size_t size);
400
401 /* Set maximum heap size.  If SIZE==0, unbounded.  Should be run
402    before _Jv_InitGC.  Not required to have any actual effect.  */
403 void _Jv_GCSetMaximumHeapSize (size_t size);
404
405 /* External interface to setting the heap size.  Parses ARG (a number
406    which can optionally have "k" or "m" appended and calls
407    _Jv_GCSetInitialHeapSize.  */
408 void _Jv_SetInitialHeapSize (const char *arg);
409
410 /* External interface to setting the maximum heap size.  Parses ARG (a
411    number which can optionally have "k" or "m" appended and calls
412    _Jv_GCSetMaximumHeapSize.  */
413 void _Jv_SetMaximumHeapSize (const char *arg);
414
415 /* External interface for setting the GC_free_space_divisor.  Calls
416    GC_set_free_space_divisor and returns the old value.  */
417 int _Jv_SetGCFreeSpaceDivisor (int div);
418
419 /* Free the method cache, if one was allocated.  This is only called
420    during thread deregistration.  */
421 void _Jv_FreeMethodCache ();
422
423 /* Set the stack size for threads.  Parses ARG, a number which can 
424    optionally have "k" or "m" appended.  */
425 void _Jv_SetStackSize (const char *arg);
426
427 extern "C" void JvRunMain (jclass klass, int argc, const char **argv);
428 extern "C" void JvRunMainName (const char *name, int argc, const char **argv);
429
430 void _Jv_RunMain (jclass klass, const char *name, int argc, const char **argv, 
431                   bool is_jar);
432
433 void _Jv_RunMain (struct _Jv_VMInitArgs *vm_args, jclass klass,
434                   const char *name, int argc, const char **argv, bool is_jar);
435
436 // Delayed until after _Jv_AllocRawObj is declared.
437 inline _Jv_VTable *
438 _Jv_VTable::new_vtable (int count)
439 {
440   size_t size = sizeof(_Jv_VTable) + (count - 1) * vtable_elt_size ();
441   return (_Jv_VTable *) _Jv_AllocRawObj (size);
442 }
443
444 // Determine if METH gets an entry in a VTable.
445 static inline jboolean _Jv_isVirtualMethod (_Jv_Method *meth)
446 {
447   using namespace java::lang::reflect;
448   return (((meth->accflags & (Modifier::STATIC | Modifier::PRIVATE)) == 0)
449           && meth->name->first() != '<');
450 }
451
452 // This function is used to determine the hash code of an object.
453 inline jint
454 _Jv_HashCode (jobject obj)
455 {
456   // This was chosen to yield relatively well distributed results on
457   // both 32- and 64-bit architectures.  Note 0x7fffffff is prime.
458   // FIXME: we assume sizeof(long) == sizeof(void *).
459   return (jint) ((unsigned long) obj % 0x7fffffff);
460 }
461
462 // Return a raw pointer to the elements of an array given the array
463 // and its element type.  You might think we could just pick a single
464 // array type and use elements() on it, but we can't because we must
465 // account for alignment of the element type.  When ARRAY is null, we
466 // obtain the number of bytes taken by the base part of the array.
467 inline char *
468 _Jv_GetArrayElementFromElementType (jobject array,
469                                     jclass element_type)
470 {
471   char *elts;
472   if (element_type == JvPrimClass (byte))
473     elts = (char *) elements ((jbyteArray) array);
474   else if (element_type == JvPrimClass (short))
475     elts = (char *) elements ((jshortArray) array);
476   else if (element_type == JvPrimClass (int))
477     elts = (char *) elements ((jintArray) array);
478   else if (element_type == JvPrimClass (long))
479     elts = (char *) elements ((jlongArray) array);
480   else if (element_type == JvPrimClass (boolean))
481     elts = (char *) elements ((jbooleanArray) array);
482   else if (element_type == JvPrimClass (char))
483     elts = (char *) elements ((jcharArray) array);
484   else if (element_type == JvPrimClass (float))
485     elts = (char *) elements ((jfloatArray) array);
486   else if (element_type == JvPrimClass (double))
487     elts = (char *) elements ((jdoubleArray) array);
488   else
489     elts = (char *) elements ((jobjectArray) array);
490   return elts;
491 }
492
493 extern "C" void _Jv_ThrowBadArrayIndex (jint bad_index)
494   __attribute__((noreturn));
495 extern "C" void _Jv_ThrowNullPointerException (void)
496   __attribute__((noreturn));
497 extern "C" void _Jv_ThrowNoSuchMethodError (void)
498   __attribute__((noreturn));
499 extern "C" void _Jv_ThrowNoSuchFieldError (int)
500   __attribute__((noreturn));
501 extern "C" jobject _Jv_NewArray (jint type, jint size)
502   __attribute__((__malloc__));
503 extern "C" jobject _Jv_NewMultiArray (jclass klass, jint dims, ...)
504   __attribute__((__malloc__));
505 extern "C" void *_Jv_CheckCast (jclass klass, jobject obj);
506 extern "C" void *_Jv_LookupInterfaceMethod (jclass klass, Utf8Const *name,
507                                            Utf8Const *signature);
508 extern "C" void *_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface, 
509                                                int meth_idx);
510 extern "C" void _Jv_CheckArrayStore (jobject array, jobject obj);
511 extern "C" void _Jv_RegisterClass (jclass klass);
512 extern "C" void _Jv_RegisterClasses (const jclass *classes);
513 extern "C" void _Jv_RegisterClasses_Counted (const jclass *classes,
514                                              size_t count);
515 extern "C" void _Jv_RegisterResource (void *vptr);
516 extern void _Jv_UnregisterClass (_Jv_Utf8Const*, java::lang::ClassLoader*);
517
518 extern "C" jobject _Jv_UnwrapJNIweakReference (jobject);
519
520 extern jclass _Jv_FindClass (_Jv_Utf8Const *name,
521                              java::lang::ClassLoader *loader);
522
523 extern jclass _Jv_FindClassNoException (_Jv_Utf8Const *name,
524                              java::lang::ClassLoader *loader);
525
526 extern jclass _Jv_FindClassFromSignature (char *,
527                                           java::lang::ClassLoader *loader,
528                                           char ** = NULL);
529
530 extern jclass _Jv_FindClassFromSignatureNoException (char *,
531                                           java::lang::ClassLoader *loader,
532                                           char ** = NULL);
533
534 extern void _Jv_GetTypesFromSignature (jmethodID method,
535                                        jclass declaringClass,
536                                        JArray<jclass> **arg_types_out,
537                                        jclass *return_type_out);
538
539 extern jboolean _Jv_CheckAccess (jclass self_klass, jclass other_klass,
540                                  jint flags);
541
542 extern jobject _Jv_CallAnyMethodA (jobject obj, jclass return_type,
543                                    jmethodID meth, jboolean is_constructor,
544                                    JArray<jclass> *parameter_types,
545                                    jobjectArray args,
546                                    jclass iface = NULL);
547
548 union jvalue;
549 extern void _Jv_CallAnyMethodA (jobject obj,
550                                 jclass return_type,
551                                 jmethodID meth,
552                                 jboolean is_constructor,
553                                 jboolean is_virtual_call,
554                                 JArray<jclass> *parameter_types,
555                                 const jvalue *args,
556                                 jvalue *result,
557                                 jboolean is_jni_call = true,
558                                 jclass iface = NULL);
559
560 extern jobject _Jv_NewMultiArray (jclass, jint ndims, jint* dims)
561   __attribute__((__malloc__));
562
563 extern "C" void _Jv_ThrowAbstractMethodError () __attribute__((__noreturn__));
564
565 /* Checked divide subroutines. */
566 extern "C"
567 {
568   jint _Jv_divI (jint, jint);
569   jint _Jv_remI (jint, jint);
570   jlong _Jv_divJ (jlong, jlong);
571   jlong _Jv_remJ (jlong, jlong);
572 }
573
574 /* Get the number of arguments (cf. argc) or 0 if our argument
575    list was never initialized.  */
576 extern int _Jv_GetNbArgs (void);
577
578 /* Get the specified argument (cf. argv[index]) or "" if either
579    our argument list was never initialized or the specified index
580    is out of bounds.  */
581 extern const char * _Jv_GetSafeArg (int index);
582
583 /* Sets our argument list. Can be used by programs with non-standard
584    entry points.  */
585 extern void _Jv_SetArgs (int argc, const char **argv);
586
587 /* Get the name of the running executable.  */
588 extern const char *_Jv_ThisExecutable (void);
589
590 /* Return a pointer to a symbol in executable or loaded library.  */
591 void *_Jv_FindSymbolInExecutable (const char *);
592
593 /* Initialize JNI.  */
594 extern void _Jv_JNI_Init (void);
595
596 /* Get or set the per-thread JNIEnv used by the invocation API.  */
597 _Jv_JNIEnv *_Jv_GetCurrentJNIEnv ();
598 void _Jv_SetCurrentJNIEnv (_Jv_JNIEnv *);
599
600 /* Free a JNIEnv. */
601 void _Jv_FreeJNIEnv (_Jv_JNIEnv *);
602
603 extern "C" void _Jv_JNI_PopSystemFrame (_Jv_JNIEnv *);
604 _Jv_JNIEnv *_Jv_GetJNIEnvNewFrameWithLoader (::java::lang::ClassLoader *);
605
606 struct _Jv_JavaVM;
607 _Jv_JavaVM *_Jv_GetJavaVM (); 
608
609 /* Get a JVMTI environment */
610 struct _Jv_JVMTIEnv;
611 _Jv_JVMTIEnv *_Jv_GetJVMTIEnv (void);
612
613 /* Initialize JVMTI */
614 extern void _Jv_JVMTI_Init (void);
615
616 // Some verification functions from defineclass.cc.
617 bool _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig);
618 bool _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig);
619 bool _Jv_VerifyClassName (unsigned char* ptr, _Jv_ushort length);
620 bool _Jv_VerifyClassName (_Jv_Utf8Const *name);
621 bool _Jv_VerifyIdentifier (_Jv_Utf8Const *);
622 bool _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2);
623
624 struct _Jv_core_chain
625 {
626   int name_length;
627   const char *name;
628   int data_length;
629   const void *data;
630
631   struct _Jv_core_chain *next;
632 };
633
634 // This is called when new core data is loaded.
635 extern void (*_Jv_RegisterCoreHook) (_Jv_core_chain *);
636
637 _Jv_core_chain *_Jv_FindCore (_Jv_core_chain *node, jstring name);
638 void _Jv_FreeCoreChain (_Jv_core_chain *chain);
639
640 #ifdef ENABLE_JVMPI
641
642 #include "jvmpi.h"
643
644 extern void (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (JVMPI_Event *event);
645 extern void (*_Jv_JVMPI_Notify_THREAD_START) (JVMPI_Event *event);
646 extern void (*_Jv_JVMPI_Notify_THREAD_END) (JVMPI_Event *event);
647 #endif
648
649 /* FIXME: this should really be defined in some more generic place */
650 #define ROUND(V, A) (((((unsigned) (V))-1) | ((A)-1))+1)
651
652 extern void _Jv_RegisterBootstrapPackages ();
653
654 #define FLAG_BINARYCOMPAT_ABI (1<<31)  /* Class is built with the BC-ABI. */
655
656 #define FLAG_BOOTSTRAP_LOADER (1<<30)  /* Used when defining a class that 
657                                           should be loaded by the bootstrap
658                                           loader.  */
659
660 // These are used to find ABI versions we recognize.
661 #define GCJ_CXX_ABI_VERSION (__GNUC__ * 100000 + __GNUC_MINOR__ * 1000)
662
663 // This is the old-style BC version ID used by GCJ 4.0.0.
664 #define OLD_GCJ_40_BC_ABI_VERSION (4 * 10000 + 0 * 10 + 5)
665
666 // New style version IDs used by GCJ 4.0.1 and later.
667 #define GCJ_40_BC_ABI_VERSION (4 * 100000 + 0 * 1000)
668
669 void _Jv_CheckABIVersion (unsigned long value);
670
671
672 inline bool
673 _Jv_ClassForBootstrapLoader (unsigned long value)
674 {
675   return (value & FLAG_BOOTSTRAP_LOADER);
676 }
677
678 // It makes the source cleaner if we simply always define this
679 // function.  If the interpreter is not built, it will never return
680 // 'true'.
681 extern inline jboolean
682 _Jv_IsInterpretedClass (jclass c)
683 {
684   return (c->accflags & java::lang::reflect::Modifier::INTERPRETED) != 0;
685 }
686
687 // Return true if the class was compiled with the BC ABI.
688 extern inline jboolean
689 _Jv_IsBinaryCompatibilityABI (jclass c)
690 {
691   // There isn't really a better test for the ABI type at this point,
692   // that will work once the class has been registered.
693   return c->otable_syms || c->atable_syms || c->itable_syms;
694 }
695
696 // Returns whether the given class does not really exists (ie. we have no
697 // bytecode) but still allows us to do some very conservative actions.
698 // E.g. throwing a NoClassDefFoundError with the name of the missing
699 // class.
700 extern inline jboolean
701 _Jv_IsPhantomClass (jclass c)
702 {
703   return c->state == JV_STATE_PHANTOM;
704 }
705
706 // A helper function defined in prims.cc.
707 char* _Jv_PrependVersionedLibdir (char* libpath);
708
709
710 // An enum for use with JvSetThreadState.  We use a C++ enum rather
711 // than the Java enum to avoid problems with class initialization
712 // during VM bootstrap.
713 typedef enum
714 {
715   JV_BLOCKED,
716   JV_NEW,
717   JV_RUNNABLE,
718   JV_TERMINATED,
719   JV_TIMED_WAITING,
720   JV_WAITING
721 } JvThreadState;
722
723 // Temporarily set the thread's state.
724 class JvSetThreadState
725 {
726 private:
727   ::java::lang::Thread *thread;
728   jint saved;
729
730 public:
731
732   // Note that 'cthread' could be NULL -- during VM startup there may
733   // not be a Thread available.
734   JvSetThreadState(::java::lang::Thread *cthread, JvThreadState nstate)
735     : thread (cthread),
736       saved (cthread ? cthread->state : (jint)JV_NEW)
737   {
738     if (thread)
739       thread->state = nstate;
740   }
741
742   ~JvSetThreadState()
743   {
744     if (thread)
745       thread->state = saved;
746   }
747 };
748
749 // This structure is used to represent all the data the native side
750 // needs.  An object of this type is assigned to the `data' member of
751 // the Thread class.
752 struct natThread
753 {
754   // A thread is either alive, dead, or being sent a signal; if it is
755   // being sent a signal, it is also alive.  Thus, if you want to know
756   // if a thread is alive, it is sufficient to test alive_status !=
757   // THREAD_DEAD.
758   volatile obj_addr_t alive_flag;
759
760   // These are used to interrupt sleep and join calls.  We can share a
761   // condition variable here since it only ever gets notified when the thread
762   // exits.
763   _Jv_Mutex_t join_mutex;
764   _Jv_ConditionVariable_t join_cond;
765
766   // These are used by Unsafe.park() and Unsafe.unpark().
767   ParkHelper park_helper;
768
769   // This is private data for the thread system layer.
770   _Jv_Thread_t *thread;
771
772   // Each thread has its own JNI object.
773   _Jv_JNIEnv *jni_env;
774 };
775
776 #endif /* __JAVA_JVM_H__ */