2004-09-15 Aaron W. LaFramboise <aaronavay62@aaronwl.com>
[official-gcc.git] / libjava / include / jvm.h
blob554e3459a479bfb8fa5f4d618954f29cf5ac24f4
1 // jvm.h - Header file for private implementation information. -*- c++ -*-
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 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
9 details. */
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.
25 #include <java-gc.h>
27 #include <java/lang/Object.h>
29 // Include cni.h before field.h to enable all definitions. FIXME.
30 #include <gcj/cni.h>
31 #include <gcj/field.h>
33 /* Macro for possible unused arguments. */
34 #define MAYBE_UNUSED __attribute__((__unused__))
36 /* Structure of the virtual table. */
37 struct _Jv_VTable
39 #ifdef __ia64__
40 typedef struct { void *pc, *gp; } vtable_elt;
41 #else
42 typedef void *vtable_elt;
43 #endif
44 jclass clas;
45 void *gc_descr;
47 // This must be last, as derived classes "extend" this by
48 // adding new data members.
49 vtable_elt method[1];
51 #ifdef __ia64__
52 void *get_method(int i) { return &method[i]; }
53 void set_method(int i, void *fptr) { method[i] = *(vtable_elt *)fptr; }
54 void *get_finalizer()
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
59 // slot.
60 return ((vtable_elt *)(get_method(0)))->pc;
62 #else
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); }
66 #endif
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
82 union _Jv_word
84 jobject o;
85 jint i; // Also stores smaller integral types.
86 jfloat f;
87 jint ia[1]; // Half of _Jv_word2.
88 void* p;
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.
93 jlong l;
94 jdouble d;
95 #endif
97 jclass clazz;
98 jstring string;
99 struct _Jv_Field *field;
100 struct _Jv_Utf8Const *utf8;
101 struct _Jv_ResolvedMethod *rmethod;
104 union _Jv_word2
106 jint ia[2];
107 jlong l;
108 jdouble d;
111 union _Jv_value
113 jbyte byte_value;
114 jshort short_value;
115 jchar char_value;
116 jint int_value;
117 jlong long_value;
118 jfloat float_value;
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
128 // PC value.
129 void *addr;
130 #ifdef INTERPRETER
131 // Actually a _Jv_InterpMethod, but we don't want to include
132 // java-interp.h everywhere.
133 void *interp;
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) \
150 : ((PTR)++, -1))
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
168 public:
169 _Jv_TempUTFString(jstring jstr, char* buf=0);
170 ~_Jv_TempUTFString();
172 // Accessors
173 operator const char*() const
175 return buf_;
177 const char* buf() const
179 return buf_;
181 char* buf()
183 return buf_;
186 private:
187 char* buf_;
188 bool heapAllocated_;
191 inline _Jv_TempUTFString::_Jv_TempUTFString (jstring jstr, char* buf)
192 : buf_(0), heapAllocated_(false)
194 if (!jstr) return;
195 jsize len = JvGetStringUTFLength (jstr);
196 if (buf)
197 buf_ = buf;
198 else
200 buf_ = (char*) _Jv_Malloc (len+1);
201 heapAllocated_ = true;
204 JvGetStringUTFRegion (jstr, 0, jstr->length(), buf_);
205 buf_[len] = '\0';
208 inline _Jv_TempUTFString::~_Jv_TempUTFString ()
210 if (heapAllocated_)
211 _Jv_Free (buf_);
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
219 jstring.
221 Sample Usage:
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)
234 // FIXME: remove this define.
235 #define StringClass java::lang::String::class$
237 namespace gcj
239 /* Some constants used during lookup of special class methods. */
240 extern _Jv_Utf8Const *void_signature; /* "()V" */
241 extern _Jv_Utf8Const *clinit_name; /* "<clinit>" */
242 extern _Jv_Utf8Const *init_name; /* "<init>" */
243 extern _Jv_Utf8Const *finit_name; /* "finit$", */
245 /* Set to true by _Jv_CreateJavaVM. */
246 extern bool runtimeInitialized;
248 /* Print out class names as they are initialized. */
249 extern bool verbose_class_flag;
252 /* Type of pointer used as finalizer. */
253 typedef void _Jv_FinalizerFunc (jobject);
255 /* Allocate space for a new Java object. */
256 void *_Jv_AllocObj (jsize size, jclass cl) __attribute__((__malloc__));
257 /* Allocate space for a potentially uninitialized pointer-free object.
258 Interesting only with JV_HASH_SYNCHRONIZATION. */
259 void *_Jv_AllocPtrFreeObj (jsize size, jclass cl) __attribute__((__malloc__));
260 /* Allocate space for an array of Java objects. */
261 void *_Jv_AllocArray (jsize size, jclass cl) __attribute__((__malloc__));
262 /* Allocate space that is known to be pointer-free. */
263 void *_Jv_AllocBytes (jsize size) __attribute__((__malloc__));
264 /* Allocate space for a new non-Java object, which does not have the usual
265 Java object header but may contain pointers to other GC'ed objects. */
266 void *_Jv_AllocRawObj (jsize size) __attribute__((__malloc__));
267 /* Explicitly throw an out-of-memory exception. */
268 void _Jv_ThrowNoMemory() __attribute__((__noreturn__));
269 /* Allocate an object with a single pointer. The first word is reserved
270 for the GC, and the second word is the traced pointer. */
271 void *_Jv_AllocTraceOne (jsize size /* incl. reserved slot */);
272 /* Ditto, but for two traced pointers. */
273 void *_Jv_AllocTraceTwo (jsize size /* incl. reserved slot */);
274 /* Initialize the GC. */
275 void _Jv_InitGC (void);
276 /* Register a finalizer. */
277 void _Jv_RegisterFinalizer (void *object, _Jv_FinalizerFunc *method);
278 /* Compute the GC descriptor for a class */
279 void * _Jv_BuildGCDescr(jclass);
281 /* Allocate some unscanned, unmoveable memory. Return NULL if out of
282 memory. */
283 void *_Jv_MallocUnchecked (jsize size) __attribute__((__malloc__));
285 /* Initialize finalizers. The argument is a function to be called
286 when a finalizer is ready to be run. */
287 void _Jv_GCInitializeFinalizers (void (*notifier) (void));
288 /* Run finalizers for objects ready to be finalized.. */
289 void _Jv_RunFinalizers (void);
290 /* Run all finalizers. Should be called only before exit. */
291 void _Jv_RunAllFinalizers (void);
292 /* Perform a GC. */
293 void _Jv_RunGC (void);
294 /* Disable and enable GC. */
295 void _Jv_DisableGC (void);
296 void _Jv_EnableGC (void);
297 /* Register a disappearing link. This is a field F which should be
298 cleared when *F is found to be inaccessible. This is used in the
299 implementation of java.lang.ref.Reference. */
300 void _Jv_GCRegisterDisappearingLink (jobject *objp);
301 /* Return true if OBJECT should be reclaimed. This is used to
302 implement soft references. */
303 jboolean _Jv_GCCanReclaimSoftReference (jobject obj);
305 /* Register a finalizer for a String object. This is only used by
306 the intern() implementation. */
307 void _Jv_RegisterStringFinalizer (jobject str);
308 /* This is called to actually finalize a possibly-intern()d String. */
309 void _Jv_FinalizeString (jobject str);
311 /* Return approximation of total size of heap. */
312 long _Jv_GCTotalMemory (void);
313 /* Return approximation of total free memory. */
314 long _Jv_GCFreeMemory (void);
316 /* Set initial heap size. If SIZE==0, ignore. Should be run before
317 _Jv_InitGC. Not required to have any actual effect. */
318 void _Jv_GCSetInitialHeapSize (size_t size);
320 /* Set maximum heap size. If SIZE==0, unbounded. Should be run
321 before _Jv_InitGC. Not required to have any actual effect. */
322 void _Jv_GCSetMaximumHeapSize (size_t size);
324 /* External interface to setting the heap size. Parses ARG (a number
325 which can optionally have "k" or "m" appended and calls
326 _Jv_GCSetInitialHeapSize. */
327 void _Jv_SetInitialHeapSize (const char *arg);
329 /* External interface to setting the maximum heap size. Parses ARG (a
330 number which can optionally have "k" or "m" appended and calls
331 _Jv_GCSetMaximumHeapSize. */
332 void _Jv_SetMaximumHeapSize (const char *arg);
334 extern "C" void JvRunMain (jclass klass, int argc, const char **argv);
335 void _Jv_RunMain (jclass klass, const char *name, int argc, const char **argv,
336 bool is_jar);
338 // Delayed until after _Jv_AllocBytes is declared.
340 // Note that we allocate this as unscanned memory -- the vtables
341 // are handled specially by the GC.
343 inline _Jv_VTable *
344 _Jv_VTable::new_vtable (int count)
346 size_t size = sizeof(_Jv_VTable) + (count - 1) * vtable_elt_size ();
347 return (_Jv_VTable *) _Jv_AllocBytes (size);
350 // Determine if METH gets an entry in a VTable.
351 static inline jboolean _Jv_isVirtualMethod (_Jv_Method *meth)
353 using namespace java::lang::reflect;
354 return (((meth->accflags & (Modifier::STATIC | Modifier::PRIVATE)) == 0)
355 && meth->name->first() != '<');
358 // This function is used to determine the hash code of an object.
359 inline jint
360 _Jv_HashCode (jobject obj)
362 // This was chosen to yield relatively well distributed results on
363 // both 32- and 64-bit architectures. Note 0x7fffffff is prime.
364 // FIXME: we assume sizeof(long) == sizeof(void *).
365 return (jint) ((unsigned long) obj % 0x7fffffff);
368 // Return a raw pointer to the elements of an array given the array
369 // and its element type. You might think we could just pick a single
370 // array type and use elements() on it, but we can't because we must
371 // account for alignment of the element type. When ARRAY is null, we
372 // obtain the number of bytes taken by the base part of the array.
373 inline char *
374 _Jv_GetArrayElementFromElementType (jobject array,
375 jclass element_type)
377 char *elts;
378 if (element_type == JvPrimClass (byte))
379 elts = (char *) elements ((jbyteArray) array);
380 else if (element_type == JvPrimClass (short))
381 elts = (char *) elements ((jshortArray) array);
382 else if (element_type == JvPrimClass (int))
383 elts = (char *) elements ((jintArray) array);
384 else if (element_type == JvPrimClass (long))
385 elts = (char *) elements ((jlongArray) array);
386 else if (element_type == JvPrimClass (boolean))
387 elts = (char *) elements ((jbooleanArray) array);
388 else if (element_type == JvPrimClass (char))
389 elts = (char *) elements ((jcharArray) array);
390 else if (element_type == JvPrimClass (float))
391 elts = (char *) elements ((jfloatArray) array);
392 else if (element_type == JvPrimClass (double))
393 elts = (char *) elements ((jdoubleArray) array);
394 else
395 elts = (char *) elements ((jobjectArray) array);
396 return elts;
399 extern "C" void _Jv_ThrowBadArrayIndex (jint bad_index)
400 __attribute__((noreturn));
401 extern "C" void _Jv_ThrowNullPointerException (void)
402 __attribute__((noreturn));
403 extern "C" jobject _Jv_NewArray (jint type, jint size)
404 __attribute__((__malloc__));
405 extern "C" jobject _Jv_NewMultiArray (jclass klass, jint dims, ...)
406 __attribute__((__malloc__));
407 extern "C" void *_Jv_CheckCast (jclass klass, jobject obj);
408 extern "C" void *_Jv_LookupInterfaceMethod (jclass klass, Utf8Const *name,
409 Utf8Const *signature);
410 extern "C" void *_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface,
411 int meth_idx);
412 extern "C" void _Jv_CheckArrayStore (jobject array, jobject obj);
413 extern "C" void _Jv_RegisterClass (jclass klass);
414 extern "C" void _Jv_RegisterClasses (jclass *classes);
415 extern "C" void _Jv_RegisterResource (void *vptr);
416 extern void _Jv_UnregisterClass (_Jv_Utf8Const*, java::lang::ClassLoader*);
417 extern void _Jv_ResolveField (_Jv_Field *, java::lang::ClassLoader*);
419 extern jclass _Jv_FindClass (_Jv_Utf8Const *name,
420 java::lang::ClassLoader *loader);
421 extern jclass _Jv_FindClassFromSignature (char *,
422 java::lang::ClassLoader *loader);
423 extern void _Jv_GetTypesFromSignature (jmethodID method,
424 jclass declaringClass,
425 JArray<jclass> **arg_types_out,
426 jclass *return_type_out);
428 extern jboolean _Jv_CheckAccess (jclass self_klass, jclass other_klass,
429 jint flags);
431 extern jobject _Jv_CallAnyMethodA (jobject obj, jclass return_type,
432 jmethodID meth, jboolean is_constructor,
433 JArray<jclass> *parameter_types,
434 jobjectArray args,
435 jclass iface = NULL);
437 union jvalue;
438 extern void _Jv_CallAnyMethodA (jobject obj,
439 jclass return_type,
440 jmethodID meth,
441 jboolean is_constructor,
442 jboolean is_virtual_call,
443 JArray<jclass> *parameter_types,
444 jvalue *args,
445 jvalue *result,
446 jboolean is_jni_call = true,
447 jclass iface = NULL);
449 extern jobject _Jv_NewMultiArray (jclass, jint ndims, jint* dims)
450 __attribute__((__malloc__));
452 /* Checked divide subroutines. */
453 extern "C"
455 jint _Jv_divI (jint, jint);
456 jint _Jv_remI (jint, jint);
457 jlong _Jv_divJ (jlong, jlong);
458 jlong _Jv_remJ (jlong, jlong);
461 /* Get the number of arguments (cf. argc) or 0 if our argument
462 list was never initialized. */
463 extern int _Jv_GetNbArgs (void);
465 /* Get the specified argument (cf. argv[index]) or "" if either
466 our argument list was never initialized or the specified index
467 is out of bounds. */
468 extern const char * _Jv_GetSafeArg (int index);
470 /* Sets our argument list. Can be used by programs with non-standard
471 entry points. */
472 extern void _Jv_SetArgs (int argc, const char **argv);
474 /* Get the name of the running executable. */
475 extern const char *_Jv_ThisExecutable (void);
477 /* Return a pointer to a symbol in executable or loaded library. */
478 void *_Jv_FindSymbolInExecutable (const char *);
480 /* Initialize JNI. */
481 extern void _Jv_JNI_Init (void);
483 /* Get or set the per-thread JNIEnv used by the invocation API. */
484 _Jv_JNIEnv *_Jv_GetCurrentJNIEnv ();
485 void _Jv_SetCurrentJNIEnv (_Jv_JNIEnv *);
487 struct _Jv_JavaVM;
488 _Jv_JavaVM *_Jv_GetJavaVM ();
490 // Some verification functions from defineclass.cc.
491 bool _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig);
492 bool _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig);
493 bool _Jv_VerifyClassName (unsigned char* ptr, _Jv_ushort length);
494 bool _Jv_VerifyClassName (_Jv_Utf8Const *name);
495 bool _Jv_VerifyIdentifier (_Jv_Utf8Const *);
496 bool _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2);
498 struct _Jv_core_chain
500 int name_length;
501 const char *name;
502 int data_length;
503 const void *data;
505 struct _Jv_core_chain *next;
508 // This is called when new core data is loaded.
509 extern void (*_Jv_RegisterCoreHook) (_Jv_core_chain *);
511 _Jv_core_chain *_Jv_FindCore (_Jv_core_chain *node, jstring name);
512 void _Jv_FreeCoreChain (_Jv_core_chain *chain);
514 #ifdef ENABLE_JVMPI
516 #include "jvmpi.h"
518 extern void (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (JVMPI_Event *event);
519 extern void (*_Jv_JVMPI_Notify_THREAD_START) (JVMPI_Event *event);
520 extern void (*_Jv_JVMPI_Notify_THREAD_END) (JVMPI_Event *event);
521 #endif
523 #endif /* __JAVA_JVM_H__ */