2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / include / jvm.h
blobe1a5c33bbc6ffbdc29ac1402babeb8c4f075f0a4
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 /* Structure of the virtual table. */
34 struct _Jv_VTable
36 #ifdef __ia64__
37 typedef struct { void *pc, *gp; } vtable_elt;
38 #else
39 typedef void *vtable_elt;
40 #endif
41 jclass clas;
42 void *gc_descr;
44 // This must be last, as derived classes "extend" this by
45 // adding new data members.
46 vtable_elt method[1];
48 #ifdef __ia64__
49 void *get_method(int i) { return &method[i]; }
50 void set_method(int i, void *fptr) { method[i] = *(vtable_elt *)fptr; }
51 void *get_finalizer()
53 // We know that get_finalizer is only used for checking whether
54 // this object needs to have a finalizer registered. So it is
55 // safe to simply return just the PC component of the vtable
56 // slot.
57 return ((vtable_elt *)(get_method(0)))->pc;
59 #else
60 void *get_method(int i) { return method[i]; }
61 void set_method(int i, void *fptr) { method[i] = fptr; }
62 void *get_finalizer() { return get_method(0); }
63 #endif
65 static size_t vtable_elt_size() { return sizeof(vtable_elt); }
67 // Given a method index, return byte offset from the vtable pointer.
68 static jint idx_to_offset (int index)
70 return (2 * sizeof (void *)) + (index * vtable_elt_size ());
72 static _Jv_VTable *new_vtable (int count);
75 // Number of virtual methods on object. FIXME: it sucks that we have
76 // to keep this up to date by hand.
77 #define NUM_OBJECT_METHODS 5
79 // This structure is the type of an array's vtable.
80 struct _Jv_ArrayVTable : public _Jv_VTable
82 vtable_elt extra_method[NUM_OBJECT_METHODS - 1];
85 union _Jv_word
87 jobject o;
88 jint i; // Also stores smaller integral types.
89 jfloat f;
90 jint ia[1]; // Half of _Jv_word2.
91 void* p;
93 #if SIZEOF_VOID_P == 8
94 // We can safely put a long or a double in here without increasing
95 // the size of _Jv_Word; we take advantage of this in the interpreter.
96 jlong l;
97 jdouble d;
98 #endif
100 jclass clazz;
101 jstring string;
102 struct _Jv_Field *field;
103 struct _Jv_Utf8Const *utf8;
104 struct _Jv_ResolvedMethod *rmethod;
107 union _Jv_word2
109 jint ia[2];
110 jlong l;
111 jdouble d;
114 // An instance of this type is used to represent a single frame in a
115 // backtrace. If the interpreter has been built, we also include
116 // information about the interpreted method.
117 struct _Jv_frame_info
119 // PC value.
120 void *addr;
121 #ifdef INTERPRETER
122 // Actually a _Jv_InterpMethod, but we don't want to include
123 // java-interp.h everywhere.
124 void *interp;
125 #endif // INTERPRETER
128 /* Extract a character from a Java-style Utf8 string.
129 * PTR points to the current character.
130 * LIMIT points to the end of the Utf8 string.
131 * PTR is incremented to point after the character thta gets returns.
132 * On an error, -1 is returned. */
133 #define UTF8_GET(PTR, LIMIT) \
134 ((PTR) >= (LIMIT) ? -1 \
135 : *(PTR) < 128 ? *(PTR)++ \
136 : (*(PTR)&0xE0) == 0xC0 && ((PTR)+=2)<=(LIMIT) && ((PTR)[-1]&0xC0) == 0x80 \
137 ? (((PTR)[-2] & 0x1F) << 6) + ((PTR)[-1] & 0x3F) \
138 : (*(PTR) & 0xF0) == 0xE0 && ((PTR) += 3) <= (LIMIT) \
139 && ((PTR)[-2] & 0xC0) == 0x80 && ((PTR)[-1] & 0xC0) == 0x80 \
140 ? (((PTR)[-3]&0x0F) << 12) + (((PTR)[-2]&0x3F) << 6) + ((PTR)[-1]&0x3F) \
141 : ((PTR)++, -1))
143 extern int _Jv_strLengthUtf8(char* str, int len);
145 typedef struct _Jv_Utf8Const Utf8Const;
146 _Jv_Utf8Const *_Jv_makeUtf8Const (char *s, int len);
147 _Jv_Utf8Const *_Jv_makeUtf8Const (jstring string);
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);
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
159 public:
160 _Jv_TempUTFString(jstring jstr, char* buf=0);
161 ~_Jv_TempUTFString();
163 // Accessors
164 operator const char*() const
166 return buf_;
168 const char* buf() const
170 return buf_;
172 char* buf()
174 return buf_;
177 private:
178 char* buf_;
179 bool heapAllocated_;
182 inline _Jv_TempUTFString::_Jv_TempUTFString (jstring jstr, char* buf)
183 : buf_(0), heapAllocated_(false)
185 if (!jstr) return;
186 jsize len = JvGetStringUTFLength (jstr);
187 if (buf)
188 buf_ = buf;
189 else
191 buf_ = (char*) _Jv_Malloc (len+1);
192 heapAllocated_ = true;
195 JvGetStringUTFRegion (jstr, 0, jstr->length(), buf_);
196 buf_[len] = '\0';
199 inline _Jv_TempUTFString::~_Jv_TempUTFString ()
201 if (heapAllocated_)
202 _Jv_Free (buf_);
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.
212 Sample Usage:
214 jstring jstr = getAJString();
215 JV_TEMP_UTF_STRING(utfstr, jstr);
216 printf("The string is: %s\n", utfstr.buf());
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)
225 // FIXME: remove this define.
226 #define StringClass java::lang::String::class$
228 namespace gcj
230 /* Some constants used during lookup of special class methods. */
231 extern _Jv_Utf8Const *void_signature; /* "()V" */
232 extern _Jv_Utf8Const *clinit_name; /* "<clinit>" */
233 extern _Jv_Utf8Const *init_name; /* "<init>" */
234 extern _Jv_Utf8Const *finit_name; /* "finit$", */
236 /* Set to true by _Jv_CreateJavaVM. */
237 extern bool runtimeInitialized;
240 /* Type of pointer used as finalizer. */
241 typedef void _Jv_FinalizerFunc (jobject);
243 /* Allocate space for a new Java object. */
244 void *_Jv_AllocObj (jsize size, jclass cl) __attribute__((__malloc__));
245 /* Allocate space for a potentially uninitialized pointer-free object.
246 Interesting only with JV_HASH_SYNCHRONIZATION. */
247 void *_Jv_AllocPtrFreeObj (jsize size, jclass cl) __attribute__((__malloc__));
248 /* Allocate space for an array of Java objects. */
249 void *_Jv_AllocArray (jsize size, jclass cl) __attribute__((__malloc__));
250 /* Allocate space that is known to be pointer-free. */
251 void *_Jv_AllocBytes (jsize size) __attribute__((__malloc__));
252 /* Allocate space for a new non-Java object, which does not have the usual
253 Java object header but may contain pointers to other GC'ed objects. */
254 void *_Jv_AllocRawObj (jsize size) __attribute__((__malloc__));
255 /* Explicitly throw an out-of-memory exception. */
256 void _Jv_ThrowNoMemory() __attribute__((__noreturn__));
257 /* Allocate an object with a single pointer. The first word is reserved
258 for the GC, and the second word is the traced pointer. */
259 void *_Jv_AllocTraceOne (jsize size /* incl. reserved slot */);
260 /* Ditto, but for two traced pointers. */
261 void *_Jv_AllocTraceTwo (jsize size /* incl. reserved slot */);
262 /* Initialize the GC. */
263 void _Jv_InitGC (void);
264 /* Register a finalizer. */
265 void _Jv_RegisterFinalizer (void *object, _Jv_FinalizerFunc *method);
266 /* Compute the GC descriptor for a class */
267 void * _Jv_BuildGCDescr(jclass);
269 /* Allocate some unscanned, unmoveable memory. Return NULL if out of
270 memory. */
271 void *_Jv_MallocUnchecked (jsize size) __attribute__((__malloc__));
273 /* Initialize finalizers. The argument is a function to be called
274 when a finalizer is ready to be run. */
275 void _Jv_GCInitializeFinalizers (void (*notifier) (void));
276 /* Run finalizers for objects ready to be finalized.. */
277 void _Jv_RunFinalizers (void);
278 /* Run all finalizers. Should be called only before exit. */
279 void _Jv_RunAllFinalizers (void);
280 /* Perform a GC. */
281 void _Jv_RunGC (void);
282 /* Disable and enable GC. */
283 void _Jv_DisableGC (void);
284 void _Jv_EnableGC (void);
285 /* Register a disappearing link. This is a field F which should be
286 cleared when *F is found to be inaccessible. This is used in the
287 implementation of java.lang.ref.Reference. */
288 void _Jv_GCRegisterDisappearingLink (jobject *objp);
289 /* Return true if OBJECT should be reclaimed. This is used to
290 implement soft references. */
291 jboolean _Jv_GCCanReclaimSoftReference (jobject obj);
293 /* Register a finalizer for a String object. This is only used by
294 the intern() implementation. */
295 void _Jv_RegisterStringFinalizer (jobject str);
296 /* This is called to actually finalize a possibly-intern()d String. */
297 void _Jv_FinalizeString (jobject str);
299 /* Return approximation of total size of heap. */
300 long _Jv_GCTotalMemory (void);
301 /* Return approximation of total free memory. */
302 long _Jv_GCFreeMemory (void);
304 /* Set initial heap size. If SIZE==0, ignore. Should be run before
305 _Jv_InitGC. Not required to have any actual effect. */
306 void _Jv_GCSetInitialHeapSize (size_t size);
308 /* Set maximum heap size. If SIZE==0, unbounded. Should be run
309 before _Jv_InitGC. Not required to have any actual effect. */
310 void _Jv_GCSetMaximumHeapSize (size_t size);
312 /* External interface to setting the heap size. Parses ARG (a number
313 which can optionally have "k" or "m" appended and calls
314 _Jv_GCSetInitialHeapSize. */
315 void _Jv_SetInitialHeapSize (const char *arg);
317 /* External interface to setting the maximum heap size. Parses ARG (a
318 number which can optionally have "k" or "m" appended and calls
319 _Jv_GCSetMaximumHeapSize. */
320 void _Jv_SetMaximumHeapSize (const char *arg);
322 extern "C" void JvRunMain (jclass klass, int argc, const char **argv);
323 void _Jv_RunMain (jclass klass, const char *name, int argc, const char **argv,
324 bool is_jar);
326 // Delayed until after _Jv_AllocBytes is declared.
328 // Note that we allocate this as unscanned memory -- the vtables
329 // are handled specially by the GC.
331 inline _Jv_VTable *
332 _Jv_VTable::new_vtable (int count)
334 size_t size = sizeof(_Jv_VTable) + (count - 1) * vtable_elt_size ();
335 return (_Jv_VTable *) _Jv_AllocBytes (size);
338 // Determine if METH gets an entry in a VTable.
339 static inline jboolean _Jv_isVirtualMethod (_Jv_Method *meth)
341 using namespace java::lang::reflect;
342 return (((meth->accflags & (Modifier::STATIC | Modifier::PRIVATE)) == 0)
343 && meth->name->data[0] != '<');
346 // This function is used to determine the hash code of an object.
347 inline jint
348 _Jv_HashCode (jobject obj)
350 // This was chosen to yield relatively well distributed results on
351 // both 32- and 64-bit architectures. Note 0x7fffffff is prime.
352 // FIXME: we assume sizeof(long) == sizeof(void *).
353 return (jint) ((unsigned long) obj % 0x7fffffff);
356 // Return a raw pointer to the elements of an array given the array
357 // and its element type. You might think we could just pick a single
358 // array type and use elements() on it, but we can't because we must
359 // account for alignment of the element type. When ARRAY is null, we
360 // obtain the number of bytes taken by the base part of the array.
361 inline char *
362 _Jv_GetArrayElementFromElementType (jobject array,
363 jclass element_type)
365 char *elts;
366 if (element_type == JvPrimClass (byte))
367 elts = (char *) elements ((jbyteArray) array);
368 else if (element_type == JvPrimClass (short))
369 elts = (char *) elements ((jshortArray) array);
370 else if (element_type == JvPrimClass (int))
371 elts = (char *) elements ((jintArray) array);
372 else if (element_type == JvPrimClass (long))
373 elts = (char *) elements ((jlongArray) array);
374 else if (element_type == JvPrimClass (boolean))
375 elts = (char *) elements ((jbooleanArray) array);
376 else if (element_type == JvPrimClass (char))
377 elts = (char *) elements ((jcharArray) array);
378 else if (element_type == JvPrimClass (float))
379 elts = (char *) elements ((jfloatArray) array);
380 else if (element_type == JvPrimClass (double))
381 elts = (char *) elements ((jdoubleArray) array);
382 else
383 elts = (char *) elements ((jobjectArray) array);
384 return elts;
387 extern "C" void _Jv_ThrowBadArrayIndex (jint bad_index)
388 __attribute__((noreturn));
389 extern "C" void _Jv_ThrowNullPointerException (void)
390 __attribute__((noreturn));
391 extern "C" jobject _Jv_NewArray (jint type, jint size)
392 __attribute__((__malloc__));
393 extern "C" jobject _Jv_NewMultiArray (jclass klass, jint dims, ...)
394 __attribute__((__malloc__));
395 extern "C" void *_Jv_CheckCast (jclass klass, jobject obj);
396 extern "C" void *_Jv_LookupInterfaceMethod (jclass klass, Utf8Const *name,
397 Utf8Const *signature);
398 extern "C" void *_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface,
399 int meth_idx);
400 extern "C" void _Jv_CheckArrayStore (jobject array, jobject obj);
401 extern "C" void _Jv_RegisterClass (jclass klass);
402 extern "C" void _Jv_RegisterClasses (jclass *classes);
403 extern "C" void _Jv_RegisterResource (void *vptr);
404 extern void _Jv_UnregisterClass (_Jv_Utf8Const*, java::lang::ClassLoader*);
405 extern void _Jv_ResolveField (_Jv_Field *, java::lang::ClassLoader*);
407 extern jclass _Jv_FindClass (_Jv_Utf8Const *name,
408 java::lang::ClassLoader *loader);
409 extern jclass _Jv_FindClassFromSignature (char *,
410 java::lang::ClassLoader *loader);
411 extern void _Jv_GetTypesFromSignature (jmethodID method,
412 jclass declaringClass,
413 JArray<jclass> **arg_types_out,
414 jclass *return_type_out);
416 extern jboolean _Jv_CheckAccess (jclass self_klass, jclass other_klass,
417 jint flags);
419 extern jobject _Jv_CallAnyMethodA (jobject obj, jclass return_type,
420 jmethodID meth, jboolean is_constructor,
421 JArray<jclass> *parameter_types,
422 jobjectArray args);
424 union jvalue;
425 extern void _Jv_CallAnyMethodA (jobject obj,
426 jclass return_type,
427 jmethodID meth,
428 jboolean is_constructor,
429 jboolean is_virtual_call,
430 JArray<jclass> *parameter_types,
431 jvalue *args,
432 jvalue *result,
433 jboolean is_jni_call = true);
435 extern jobject _Jv_NewMultiArray (jclass, jint ndims, jint* dims)
436 __attribute__((__malloc__));
438 /* Checked divide subroutines. */
439 extern "C"
441 jint _Jv_divI (jint, jint);
442 jint _Jv_remI (jint, jint);
443 jlong _Jv_divJ (jlong, jlong);
444 jlong _Jv_remJ (jlong, jlong);
447 /* Get the number of arguments (cf. argc) or 0 if our argument
448 list was never initialized. */
449 extern int _Jv_GetNbArgs (void);
451 /* Get the specified argument (cf. argv[index]) or "" if either
452 our argument list was never initialized or the specified index
453 is out of bounds. */
454 extern const char * _Jv_GetSafeArg (int index);
456 /* Sets our argument list. Can be used by programs with non-standard
457 entry points. */
458 extern void _Jv_SetArgs (int argc, const char **argv);
460 /* Get the name of the running executable. */
461 extern const char *_Jv_ThisExecutable (void);
463 /* Return a pointer to a symbol in executable or loaded library. */
464 void *_Jv_FindSymbolInExecutable (const char *);
466 /* Initialize JNI. */
467 extern void _Jv_JNI_Init (void);
469 /* Get or set the per-thread JNIEnv used by the invocation API. */
470 _Jv_JNIEnv *_Jv_GetCurrentJNIEnv ();
471 void _Jv_SetCurrentJNIEnv (_Jv_JNIEnv *);
473 struct _Jv_JavaVM;
474 _Jv_JavaVM *_Jv_GetJavaVM ();
476 // Some verification functions from defineclass.cc.
477 bool _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig);
478 bool _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig);
479 bool _Jv_VerifyClassName (unsigned char* ptr, _Jv_ushort length);
480 bool _Jv_VerifyClassName (_Jv_Utf8Const *name);
481 bool _Jv_VerifyIdentifier (_Jv_Utf8Const *);
482 bool _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2);
484 struct _Jv_core_chain
486 int name_length;
487 const char *name;
488 int data_length;
489 const void *data;
491 struct _Jv_core_chain *next;
494 // This is called when new core data is loaded.
495 extern void (*_Jv_RegisterCoreHook) (_Jv_core_chain *);
497 _Jv_core_chain *_Jv_FindCore (_Jv_core_chain *node, jstring name);
498 void _Jv_FreeCoreChain (_Jv_core_chain *chain);
500 #ifdef ENABLE_JVMPI
502 #include "jvmpi.h"
504 extern void (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (JVMPI_Event *event);
505 extern void (*_Jv_JVMPI_Notify_THREAD_START) (JVMPI_Event *event);
506 extern void (*_Jv_JVMPI_Notify_THREAD_END) (JVMPI_Event *event);
507 #endif
509 #endif /* __JAVA_JVM_H__ */