1 // jvm.h - Header file for private implementation information. -*- c++ -*-
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
11 #ifndef __JAVA_JVM_H__
12 #define __JAVA_JVM_H__
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__
20 #include <gcj/javaprims.h>
22 #include <java-assert.h>
23 #include <java-threads.h>
24 // Must include java-gc.h before Object.h for the implementation.
27 #include <java/lang/Object.h>
29 // Include cni.h before field.h to enable all definitions. FIXME.
31 #include <gcj/field.h>
33 /* Macro for possible unused arguments. */
34 #define MAYBE_UNUSED __attribute__((__unused__))
36 /* Structure of the virtual table. */
40 typedef struct { void *pc
, *gp
; } vtable_elt
;
42 typedef void *vtable_elt
;
47 // This must be last, as derived classes "extend" this by
48 // adding new data members.
52 void *get_method(int i
) { return &method
[i
]; }
53 void set_method(int i
, void *fptr
) { method
[i
] = *(vtable_elt
*)fptr
; }
56 // We know that get_finalizer is only used for checking whether
57 // this object needs to have a finalizer registered. So it is
58 // safe to simply return just the PC component of the vtable
60 return ((vtable_elt
*)(get_method(0)))->pc
;
63 void *get_method(int i
) { return method
[i
]; }
64 void set_method(int i
, void *fptr
) { method
[i
] = fptr
; }
65 void *get_finalizer() { return get_method(0); }
68 static size_t vtable_elt_size() { return sizeof(vtable_elt
); }
70 // Given a method index, return byte offset from the vtable pointer.
71 static jint
idx_to_offset (int index
)
73 return (2 * sizeof (void *)) + (index
* vtable_elt_size ());
75 static _Jv_VTable
*new_vtable (int count
);
78 // Number of virtual methods on object. FIXME: it sucks that we have
79 // to keep this up to date by hand.
80 #define NUM_OBJECT_METHODS 5
85 jint i
; // Also stores smaller integral types.
87 jint ia
[1]; // Half of _Jv_word2.
90 #if SIZEOF_VOID_P == 8
91 // We can safely put a long or a double in here without increasing
92 // the size of _Jv_Word; we take advantage of this in the interpreter.
99 struct _Jv_Field
*field
;
100 struct _Jv_Utf8Const
*utf8
;
101 struct _Jv_ResolvedMethod
*rmethod
;
119 jdouble double_value
;
120 jobject object_value
;
123 // An instance of this type is used to represent a single frame in a
124 // backtrace. If the interpreter has been built, we also include
125 // information about the interpreted method.
126 struct _Jv_frame_info
131 // Actually a _Jv_InterpMethod, but we don't want to include
132 // java-interp.h everywhere.
134 #endif // INTERPRETER
137 /* Extract a character from a Java-style Utf8 string.
138 * PTR points to the current character.
139 * LIMIT points to the end of the Utf8 string.
140 * PTR is incremented to point after the character thta gets returns.
141 * On an error, -1 is returned. */
142 #define UTF8_GET(PTR, LIMIT) \
143 ((PTR) >= (LIMIT) ? -1 \
144 : *(PTR) < 128 ? *(PTR)++ \
145 : (*(PTR)&0xE0) == 0xC0 && ((PTR)+=2)<=(LIMIT) && ((PTR)[-1]&0xC0) == 0x80 \
146 ? (((PTR)[-2] & 0x1F) << 6) + ((PTR)[-1] & 0x3F) \
147 : (*(PTR) & 0xF0) == 0xE0 && ((PTR) += 3) <= (LIMIT) \
148 && ((PTR)[-2] & 0xC0) == 0x80 && ((PTR)[-1] & 0xC0) == 0x80 \
149 ? (((PTR)[-3]&0x0F) << 12) + (((PTR)[-2]&0x3F) << 6) + ((PTR)[-1]&0x3F) \
152 extern int _Jv_strLengthUtf8(char* str
, int len
);
154 typedef struct _Jv_Utf8Const Utf8Const
;
155 _Jv_Utf8Const
*_Jv_makeUtf8Const (char *s
, int len
);
156 _Jv_Utf8Const
*_Jv_makeUtf8Const (jstring string
);
157 extern jboolean
_Jv_equalUtf8Consts (const _Jv_Utf8Const
*, const _Jv_Utf8Const
*);
158 extern jboolean
_Jv_equal (_Jv_Utf8Const
*, jstring
, jint
);
159 extern jboolean
_Jv_equaln (_Jv_Utf8Const
*, jstring
, jint
);
161 /* Helper class which converts a jstring to a temporary char*.
162 Uses the supplied buffer, if non-null. Otherwise, allocates
163 the buffer on the heap. Use the JV_TEMP_UTF_STRING macro,
164 which follows, to automatically allocate a stack buffer if
165 the string is small enough. */
166 class _Jv_TempUTFString
169 _Jv_TempUTFString(jstring jstr
, char* buf
=0);
170 ~_Jv_TempUTFString();
173 operator const char*() const
177 const char* buf() const
191 inline _Jv_TempUTFString::_Jv_TempUTFString (jstring jstr
, char* buf
)
192 : buf_(0), heapAllocated_(false)
195 jsize len
= JvGetStringUTFLength (jstr
);
200 buf_
= (char*) _Jv_Malloc (len
+1);
201 heapAllocated_
= true;
204 JvGetStringUTFRegion (jstr
, 0, jstr
->length(), buf_
);
208 inline _Jv_TempUTFString::~_Jv_TempUTFString ()
214 /* Macro which uses _Jv_TempUTFString. Allocates a stack-based
215 buffer if the string and its null terminator are <= 256
216 characters in length. Otherwise, a heap-based buffer is
217 used. The parameters to this macro are the variable name
218 which is an instance of _Jv_TempUTFString (above) and a
223 jstring jstr = getAJString();
224 JV_TEMP_UTF_STRING(utfstr, jstr);
225 printf("The string is: %s\n", utfstr.buf());
228 #define JV_TEMP_UTF_STRING(utfstr, jstr) \
229 jstring utfstr##thejstr = (jstr); \
230 jsize utfstr##_len = utfstr##thejstr ? JvGetStringUTFLength (utfstr##thejstr) + 1 : 0; \
231 char utfstr##_buf[utfstr##_len <= 256 ? utfstr##_len : 0]; \
232 _Jv_TempUTFString utfstr(utfstr##thejstr, sizeof(utfstr##_buf)==0 ? 0 : utfstr##_buf)
236 /* Some constants used during lookup of special class methods. */
237 extern _Jv_Utf8Const
*void_signature
; /* "()V" */
238 extern _Jv_Utf8Const
*clinit_name
; /* "<clinit>" */
239 extern _Jv_Utf8Const
*init_name
; /* "<init>" */
240 extern _Jv_Utf8Const
*finit_name
; /* "finit$", */
242 /* Set to true by _Jv_CreateJavaVM. */
243 extern bool runtimeInitialized
;
245 /* Print out class names as they are initialized. */
246 extern bool verbose_class_flag
;
249 // This class handles all aspects of class preparation and linking.
253 static _Jv_Field
*find_field_helper(jclass
, _Jv_Utf8Const
*, jclass
*);
254 static _Jv_Field
*find_field(jclass
, jclass
, _Jv_Utf8Const
*,
256 static void prepare_constant_time_tables(jclass
);
257 static jshort
get_interfaces(jclass
, _Jv_ifaces
*);
258 static void link_symbol_table(jclass
);
259 static void link_exception_table(jclass
);
260 static void layout_interface_methods(jclass
);
261 static void layout_vtable_methods(jclass
);
262 static void set_vtable_entries(jclass
, _Jv_VTable
*);
263 static void make_vtable(jclass
);
264 static void ensure_fields_laid_out(jclass
);
265 static void ensure_class_linked(jclass
);
266 static void ensure_supers_installed(jclass
);
267 static void add_miranda_methods(jclass
, jclass
);
268 static void ensure_method_table_complete(jclass
);
269 static void verify_class(jclass
);
270 static jshort
find_iindex(jclass
*, jshort
*, jshort
);
271 static jshort
indexof(void *, void **, jshort
);
272 static int get_alignment_from_class(jclass
);
273 static void generate_itable(jclass
, _Jv_ifaces
*, jshort
*);
274 static jshort
append_partial_itable(jclass
, jclass
, void **, jshort
);
275 static _Jv_Method
*search_method_in_class (jclass
, jclass
,
281 static void print_class_loaded (jclass
);
282 static void resolve_class_ref (jclass
, jclass
*);
283 static void wait_for_state(jclass
, int);
284 static _Jv_word
resolve_pool_entry (jclass
, int);
285 static void resolve_field (_Jv_Field
*, java::lang::ClassLoader
*);
286 static void verify_type_assertions (jclass
);
289 /* Type of pointer used as finalizer. */
290 typedef void _Jv_FinalizerFunc (jobject
);
292 /* Allocate space for a new Java object. */
293 void *_Jv_AllocObj (jsize size
, jclass cl
) __attribute__((__malloc__
));
294 /* Allocate space for a potentially uninitialized pointer-free object.
295 Interesting only with JV_HASH_SYNCHRONIZATION. */
296 void *_Jv_AllocPtrFreeObj (jsize size
, jclass cl
) __attribute__((__malloc__
));
297 /* Allocate space for an array of Java objects. */
298 void *_Jv_AllocArray (jsize size
, jclass cl
) __attribute__((__malloc__
));
299 /* Allocate space that is known to be pointer-free. */
300 void *_Jv_AllocBytes (jsize size
) __attribute__((__malloc__
));
301 /* Allocate space for a new non-Java object, which does not have the usual
302 Java object header but may contain pointers to other GC'ed objects. */
303 void *_Jv_AllocRawObj (jsize size
) __attribute__((__malloc__
));
304 /* Explicitly throw an out-of-memory exception. */
305 void _Jv_ThrowNoMemory() __attribute__((__noreturn__
));
306 /* Allocate an object with a single pointer. The first word is reserved
307 for the GC, and the second word is the traced pointer. */
308 void *_Jv_AllocTraceOne (jsize size
/* incl. reserved slot */);
309 /* Ditto, but for two traced pointers. */
310 void *_Jv_AllocTraceTwo (jsize size
/* incl. reserved slot */);
311 /* Initialize the GC. */
312 void _Jv_InitGC (void);
313 /* Register a finalizer. */
314 void _Jv_RegisterFinalizer (void *object
, _Jv_FinalizerFunc
*method
);
315 /* Compute the GC descriptor for a class */
316 void * _Jv_BuildGCDescr(jclass
);
318 /* Allocate some unscanned, unmoveable memory. Return NULL if out of
320 void *_Jv_MallocUnchecked (jsize size
) __attribute__((__malloc__
));
322 /* Initialize finalizers. The argument is a function to be called
323 when a finalizer is ready to be run. */
324 void _Jv_GCInitializeFinalizers (void (*notifier
) (void));
325 /* Run finalizers for objects ready to be finalized.. */
326 void _Jv_RunFinalizers (void);
327 /* Run all finalizers. Should be called only before exit. */
328 void _Jv_RunAllFinalizers (void);
330 void _Jv_RunGC (void);
331 /* Disable and enable GC. */
332 void _Jv_DisableGC (void);
333 void _Jv_EnableGC (void);
334 /* Register a disappearing link. This is a field F which should be
335 cleared when *F is found to be inaccessible. This is used in the
336 implementation of java.lang.ref.Reference. */
337 void _Jv_GCRegisterDisappearingLink (jobject
*objp
);
338 /* Return true if OBJECT should be reclaimed. This is used to
339 implement soft references. */
340 jboolean
_Jv_GCCanReclaimSoftReference (jobject obj
);
342 /* Register a finalizer for a String object. This is only used by
343 the intern() implementation. */
344 void _Jv_RegisterStringFinalizer (jobject str
);
345 /* This is called to actually finalize a possibly-intern()d String. */
346 void _Jv_FinalizeString (jobject str
);
348 /* Return approximation of total size of heap. */
349 long _Jv_GCTotalMemory (void);
350 /* Return approximation of total free memory. */
351 long _Jv_GCFreeMemory (void);
353 /* Set initial heap size. If SIZE==0, ignore. Should be run before
354 _Jv_InitGC. Not required to have any actual effect. */
355 void _Jv_GCSetInitialHeapSize (size_t size
);
357 /* Set maximum heap size. If SIZE==0, unbounded. Should be run
358 before _Jv_InitGC. Not required to have any actual effect. */
359 void _Jv_GCSetMaximumHeapSize (size_t size
);
361 /* External interface to setting the heap size. Parses ARG (a number
362 which can optionally have "k" or "m" appended and calls
363 _Jv_GCSetInitialHeapSize. */
364 void _Jv_SetInitialHeapSize (const char *arg
);
366 /* External interface to setting the maximum heap size. Parses ARG (a
367 number which can optionally have "k" or "m" appended and calls
368 _Jv_GCSetMaximumHeapSize. */
369 void _Jv_SetMaximumHeapSize (const char *arg
);
371 extern "C" void JvRunMain (jclass klass
, int argc
, const char **argv
);
372 void _Jv_RunMain (jclass klass
, const char *name
, int argc
, const char **argv
,
375 // Delayed until after _Jv_AllocBytes is declared.
377 // Note that we allocate this as unscanned memory -- the vtables
378 // are handled specially by the GC.
381 _Jv_VTable::new_vtable (int count
)
383 size_t size
= sizeof(_Jv_VTable
) + (count
- 1) * vtable_elt_size ();
384 return (_Jv_VTable
*) _Jv_AllocBytes (size
);
387 // Determine if METH gets an entry in a VTable.
388 static inline jboolean
_Jv_isVirtualMethod (_Jv_Method
*meth
)
390 using namespace java::lang::reflect
;
391 return (((meth
->accflags
& (Modifier::STATIC
| Modifier::PRIVATE
)) == 0)
392 && meth
->name
->first() != '<');
395 // This function is used to determine the hash code of an object.
397 _Jv_HashCode (jobject obj
)
399 // This was chosen to yield relatively well distributed results on
400 // both 32- and 64-bit architectures. Note 0x7fffffff is prime.
401 // FIXME: we assume sizeof(long) == sizeof(void *).
402 return (jint
) ((unsigned long) obj
% 0x7fffffff);
405 // Return a raw pointer to the elements of an array given the array
406 // and its element type. You might think we could just pick a single
407 // array type and use elements() on it, but we can't because we must
408 // account for alignment of the element type. When ARRAY is null, we
409 // obtain the number of bytes taken by the base part of the array.
411 _Jv_GetArrayElementFromElementType (jobject array
,
415 if (element_type
== JvPrimClass (byte
))
416 elts
= (char *) elements ((jbyteArray
) array
);
417 else if (element_type
== JvPrimClass (short))
418 elts
= (char *) elements ((jshortArray
) array
);
419 else if (element_type
== JvPrimClass (int))
420 elts
= (char *) elements ((jintArray
) array
);
421 else if (element_type
== JvPrimClass (long))
422 elts
= (char *) elements ((jlongArray
) array
);
423 else if (element_type
== JvPrimClass (boolean
))
424 elts
= (char *) elements ((jbooleanArray
) array
);
425 else if (element_type
== JvPrimClass (char))
426 elts
= (char *) elements ((jcharArray
) array
);
427 else if (element_type
== JvPrimClass (float))
428 elts
= (char *) elements ((jfloatArray
) array
);
429 else if (element_type
== JvPrimClass (double))
430 elts
= (char *) elements ((jdoubleArray
) array
);
432 elts
= (char *) elements ((jobjectArray
) array
);
436 extern "C" void _Jv_ThrowBadArrayIndex (jint bad_index
)
437 __attribute__((noreturn
));
438 extern "C" void _Jv_ThrowNullPointerException (void)
439 __attribute__((noreturn
));
440 extern "C" jobject
_Jv_NewArray (jint type
, jint size
)
441 __attribute__((__malloc__
));
442 extern "C" jobject
_Jv_NewMultiArray (jclass klass
, jint dims
, ...)
443 __attribute__((__malloc__
));
444 extern "C" void *_Jv_CheckCast (jclass klass
, jobject obj
);
445 extern "C" void *_Jv_LookupInterfaceMethod (jclass klass
, Utf8Const
*name
,
446 Utf8Const
*signature
);
447 extern "C" void *_Jv_LookupInterfaceMethodIdx (jclass klass
, jclass iface
,
449 extern "C" void _Jv_CheckArrayStore (jobject array
, jobject obj
);
450 extern "C" void _Jv_RegisterClass (jclass klass
);
451 extern "C" void _Jv_RegisterClasses (const jclass
*classes
);
452 extern "C" void _Jv_RegisterClasses_Counted (const jclass
*classes
,
454 extern "C" void _Jv_RegisterResource (void *vptr
);
455 extern void _Jv_UnregisterClass (_Jv_Utf8Const
*, java::lang::ClassLoader
*);
457 extern jclass
_Jv_FindClass (_Jv_Utf8Const
*name
,
458 java::lang::ClassLoader
*loader
);
459 extern jclass
_Jv_FindClassFromSignature (char *,
460 java::lang::ClassLoader
*loader
);
461 extern void _Jv_GetTypesFromSignature (jmethodID method
,
462 jclass declaringClass
,
463 JArray
<jclass
> **arg_types_out
,
464 jclass
*return_type_out
);
466 extern jboolean
_Jv_CheckAccess (jclass self_klass
, jclass other_klass
,
469 extern jobject
_Jv_CallAnyMethodA (jobject obj
, jclass return_type
,
470 jmethodID meth
, jboolean is_constructor
,
471 JArray
<jclass
> *parameter_types
,
473 jclass iface
= NULL
);
476 extern void _Jv_CallAnyMethodA (jobject obj
,
479 jboolean is_constructor
,
480 jboolean is_virtual_call
,
481 JArray
<jclass
> *parameter_types
,
484 jboolean is_jni_call
= true,
485 jclass iface
= NULL
);
487 extern jobject
_Jv_NewMultiArray (jclass
, jint ndims
, jint
* dims
)
488 __attribute__((__malloc__
));
490 /* Checked divide subroutines. */
493 jint
_Jv_divI (jint
, jint
);
494 jint
_Jv_remI (jint
, jint
);
495 jlong
_Jv_divJ (jlong
, jlong
);
496 jlong
_Jv_remJ (jlong
, jlong
);
499 /* Get the number of arguments (cf. argc) or 0 if our argument
500 list was never initialized. */
501 extern int _Jv_GetNbArgs (void);
503 /* Get the specified argument (cf. argv[index]) or "" if either
504 our argument list was never initialized or the specified index
506 extern const char * _Jv_GetSafeArg (int index
);
508 /* Sets our argument list. Can be used by programs with non-standard
510 extern void _Jv_SetArgs (int argc
, const char **argv
);
512 /* Get the name of the running executable. */
513 extern const char *_Jv_ThisExecutable (void);
515 /* Return a pointer to a symbol in executable or loaded library. */
516 void *_Jv_FindSymbolInExecutable (const char *);
518 /* Initialize JNI. */
519 extern void _Jv_JNI_Init (void);
521 /* Get or set the per-thread JNIEnv used by the invocation API. */
522 _Jv_JNIEnv
*_Jv_GetCurrentJNIEnv ();
523 void _Jv_SetCurrentJNIEnv (_Jv_JNIEnv
*);
526 void _Jv_FreeJNIEnv (_Jv_JNIEnv
*);
529 _Jv_JavaVM
*_Jv_GetJavaVM ();
531 // Some verification functions from defineclass.cc.
532 bool _Jv_VerifyFieldSignature (_Jv_Utf8Const
*sig
);
533 bool _Jv_VerifyMethodSignature (_Jv_Utf8Const
*sig
);
534 bool _Jv_VerifyClassName (unsigned char* ptr
, _Jv_ushort length
);
535 bool _Jv_VerifyClassName (_Jv_Utf8Const
*name
);
536 bool _Jv_VerifyIdentifier (_Jv_Utf8Const
*);
537 bool _Jv_ClassNameSamePackage (_Jv_Utf8Const
*name1
, _Jv_Utf8Const
*name2
);
539 struct _Jv_core_chain
546 struct _Jv_core_chain
*next
;
549 // This is called when new core data is loaded.
550 extern void (*_Jv_RegisterCoreHook
) (_Jv_core_chain
*);
552 _Jv_core_chain
*_Jv_FindCore (_Jv_core_chain
*node
, jstring name
);
553 void _Jv_FreeCoreChain (_Jv_core_chain
*chain
);
559 extern void (*_Jv_JVMPI_Notify_OBJECT_ALLOC
) (JVMPI_Event
*event
);
560 extern void (*_Jv_JVMPI_Notify_THREAD_START
) (JVMPI_Event
*event
);
561 extern void (*_Jv_JVMPI_Notify_THREAD_END
) (JVMPI_Event
*event
);
564 /* FIXME: this should really be defined in some more generic place */
565 #define ROUND(V, A) (((((unsigned) (V))-1) | ((A)-1))+1)
567 extern void _Jv_RegisterBootstrapPackages ();
570 // This is used to find ABI versions we recognize.
571 #define GCJ_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 10)
572 #define GCJ_BINARYCOMPAT_ADDITION 5
575 _Jv_CheckABIVersion (unsigned long value
)
577 // For this release, recognize just our defined C++ ABI and our
578 // defined BC ABI. (In the future we may recognize past BC ABIs as
580 return (value
== GCJ_VERSION
581 || value
== (GCJ_VERSION
+ GCJ_BINARYCOMPAT_ADDITION
));
584 #endif /* __JAVA_JVM_H__ */