2003-10-01 Nathan Myers <ncm@cantrip.org>
[official-gcc.git] / libjava / include / jvm.h
blobb28f61a0e970c635fc6e66fdb270441e14bbe4ca
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 // This function is used to determine the hash code of an object.
339 inline jint
340 _Jv_HashCode (jobject obj)
342 // This was chosen to yield relatively well distributed results on
343 // both 32- and 64-bit architectures. Note 0x7fffffff is prime.
344 // FIXME: we assume sizeof(long) == sizeof(void *).
345 return (jint) ((unsigned long) obj % 0x7fffffff);
348 // Return a raw pointer to the elements of an array given the array
349 // and its element type. You might think we could just pick a single
350 // array type and use elements() on it, but we can't because we must
351 // account for alignment of the element type. When ARRAY is null, we
352 // obtain the number of bytes taken by the base part of the array.
353 inline char *
354 _Jv_GetArrayElementFromElementType (jobject array,
355 jclass element_type)
357 char *elts;
358 if (element_type == JvPrimClass (byte))
359 elts = (char *) elements ((jbyteArray) array);
360 else if (element_type == JvPrimClass (short))
361 elts = (char *) elements ((jshortArray) array);
362 else if (element_type == JvPrimClass (int))
363 elts = (char *) elements ((jintArray) array);
364 else if (element_type == JvPrimClass (long))
365 elts = (char *) elements ((jlongArray) array);
366 else if (element_type == JvPrimClass (boolean))
367 elts = (char *) elements ((jbooleanArray) array);
368 else if (element_type == JvPrimClass (char))
369 elts = (char *) elements ((jcharArray) array);
370 else if (element_type == JvPrimClass (float))
371 elts = (char *) elements ((jfloatArray) array);
372 else if (element_type == JvPrimClass (double))
373 elts = (char *) elements ((jdoubleArray) array);
374 else
375 elts = (char *) elements ((jobjectArray) array);
376 return elts;
379 extern "C" void _Jv_ThrowBadArrayIndex (jint bad_index)
380 __attribute__((noreturn));
381 extern "C" void _Jv_ThrowNullPointerException (void)
382 __attribute__((noreturn));
383 extern "C" jobject _Jv_NewArray (jint type, jint size)
384 __attribute__((__malloc__));
385 extern "C" jobject _Jv_NewMultiArray (jclass klass, jint dims, ...)
386 __attribute__((__malloc__));
387 extern "C" void *_Jv_CheckCast (jclass klass, jobject obj);
388 extern "C" void *_Jv_LookupInterfaceMethod (jclass klass, Utf8Const *name,
389 Utf8Const *signature);
390 extern "C" void *_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface,
391 int meth_idx);
392 extern "C" void _Jv_CheckArrayStore (jobject array, jobject obj);
393 extern "C" void _Jv_RegisterClass (jclass klass);
394 extern "C" void _Jv_RegisterClasses (jclass *classes);
395 extern "C" void _Jv_RegisterResource (void *vptr);
396 extern void _Jv_UnregisterClass (_Jv_Utf8Const*, java::lang::ClassLoader*);
397 extern void _Jv_ResolveField (_Jv_Field *, java::lang::ClassLoader*);
399 extern jclass _Jv_FindClass (_Jv_Utf8Const *name,
400 java::lang::ClassLoader *loader);
401 extern jclass _Jv_FindClassFromSignature (char *,
402 java::lang::ClassLoader *loader);
403 extern void _Jv_GetTypesFromSignature (jmethodID method,
404 jclass declaringClass,
405 JArray<jclass> **arg_types_out,
406 jclass *return_type_out);
408 extern jboolean _Jv_CheckAccess (jclass self_klass, jclass other_klass,
409 jint flags);
411 extern jobject _Jv_CallAnyMethodA (jobject obj, jclass return_type,
412 jmethodID meth, jboolean is_constructor,
413 JArray<jclass> *parameter_types,
414 jobjectArray args);
416 union jvalue;
417 extern void _Jv_CallAnyMethodA (jobject obj,
418 jclass return_type,
419 jmethodID meth,
420 jboolean is_constructor,
421 JArray<jclass> *parameter_types,
422 jvalue *args,
423 jvalue *result,
424 jboolean is_jni_call = true);
426 extern jobject _Jv_NewMultiArray (jclass, jint ndims, jint* dims)
427 __attribute__((__malloc__));
429 /* Checked divide subroutines. */
430 extern "C"
432 jint _Jv_divI (jint, jint);
433 jint _Jv_remI (jint, jint);
434 jlong _Jv_divJ (jlong, jlong);
435 jlong _Jv_remJ (jlong, jlong);
438 /* Get the number of arguments (cf. argc) or 0 if our argument
439 list was never initialized. */
440 extern int _Jv_GetNbArgs (void);
442 /* Get the specified argument (cf. argv[index]) or "" if either
443 our argument list was never initialized or the specified index
444 is out of bounds. */
445 extern const char * _Jv_GetSafeArg (int index);
447 /* Sets our argument list. Can be used by programs with non-standard
448 entry points. */
449 extern void _Jv_SetArgs (int argc, const char **argv);
451 /* Get the name of the running executable. */
452 extern const char *_Jv_ThisExecutable (void);
454 /* Return a pointer to a symbol in executable or loaded library. */
455 void *_Jv_FindSymbolInExecutable (const char *);
457 /* Initialize JNI. */
458 extern void _Jv_JNI_Init (void);
460 /* Get or set the per-thread JNIEnv used by the invocation API. */
461 _Jv_JNIEnv *_Jv_GetCurrentJNIEnv ();
462 void _Jv_SetCurrentJNIEnv (_Jv_JNIEnv *);
464 struct _Jv_JavaVM;
465 _Jv_JavaVM *_Jv_GetJavaVM ();
467 // Some verification functions from defineclass.cc.
468 bool _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig);
469 bool _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig);
470 bool _Jv_VerifyClassName (unsigned char* ptr, _Jv_ushort length);
471 bool _Jv_VerifyClassName (_Jv_Utf8Const *name);
472 bool _Jv_VerifyIdentifier (_Jv_Utf8Const *);
473 bool _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2);
475 struct _Jv_core_chain
477 int name_length;
478 const char *name;
479 int data_length;
480 const void *data;
482 struct _Jv_core_chain *next;
485 // This is called when new core data is loaded.
486 extern void (*_Jv_RegisterCoreHook) (_Jv_core_chain *);
488 _Jv_core_chain *_Jv_FindCore (_Jv_core_chain *node, jstring name);
489 void _Jv_FreeCoreChain (_Jv_core_chain *chain);
491 #ifdef ENABLE_JVMPI
493 #include "jvmpi.h"
495 extern void (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (JVMPI_Event *event);
496 extern void (*_Jv_JVMPI_Notify_THREAD_START) (JVMPI_Event *event);
497 extern void (*_Jv_JVMPI_Notify_THREAD_END) (JVMPI_Event *event);
498 #endif
500 #endif /* __JAVA_JVM_H__ */