Missed these two in previous commit.
[official-gcc.git] / libjava / include / jvm.h
blob398f854e7836186c7ea0053391367e2b59ddb693
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 // This structure is the type of an array's vtable.
83 struct _Jv_ArrayVTable : public _Jv_VTable
85 vtable_elt extra_method[NUM_OBJECT_METHODS - 1];
88 union _Jv_word
90 jobject o;
91 jint i; // Also stores smaller integral types.
92 jfloat f;
93 jint ia[1]; // Half of _Jv_word2.
94 void* p;
96 #if SIZEOF_VOID_P == 8
97 // We can safely put a long or a double in here without increasing
98 // the size of _Jv_Word; we take advantage of this in the interpreter.
99 jlong l;
100 jdouble d;
101 #endif
103 jclass clazz;
104 jstring string;
105 struct _Jv_Field *field;
106 struct _Jv_Utf8Const *utf8;
107 struct _Jv_ResolvedMethod *rmethod;
110 union _Jv_word2
112 jint ia[2];
113 jlong l;
114 jdouble d;
117 // An instance of this type is used to represent a single frame in a
118 // backtrace. If the interpreter has been built, we also include
119 // information about the interpreted method.
120 struct _Jv_frame_info
122 // PC value.
123 void *addr;
124 #ifdef INTERPRETER
125 // Actually a _Jv_InterpMethod, but we don't want to include
126 // java-interp.h everywhere.
127 void *interp;
128 #endif // INTERPRETER
131 /* Extract a character from a Java-style Utf8 string.
132 * PTR points to the current character.
133 * LIMIT points to the end of the Utf8 string.
134 * PTR is incremented to point after the character thta gets returns.
135 * On an error, -1 is returned. */
136 #define UTF8_GET(PTR, LIMIT) \
137 ((PTR) >= (LIMIT) ? -1 \
138 : *(PTR) < 128 ? *(PTR)++ \
139 : (*(PTR)&0xE0) == 0xC0 && ((PTR)+=2)<=(LIMIT) && ((PTR)[-1]&0xC0) == 0x80 \
140 ? (((PTR)[-2] & 0x1F) << 6) + ((PTR)[-1] & 0x3F) \
141 : (*(PTR) & 0xF0) == 0xE0 && ((PTR) += 3) <= (LIMIT) \
142 && ((PTR)[-2] & 0xC0) == 0x80 && ((PTR)[-1] & 0xC0) == 0x80 \
143 ? (((PTR)[-3]&0x0F) << 12) + (((PTR)[-2]&0x3F) << 6) + ((PTR)[-1]&0x3F) \
144 : ((PTR)++, -1))
146 extern int _Jv_strLengthUtf8(char* str, int len);
148 typedef struct _Jv_Utf8Const Utf8Const;
149 _Jv_Utf8Const *_Jv_makeUtf8Const (char *s, int len);
150 _Jv_Utf8Const *_Jv_makeUtf8Const (jstring string);
151 extern jboolean _Jv_equalUtf8Consts (const _Jv_Utf8Const *, const _Jv_Utf8Const *);
152 extern jboolean _Jv_equal (_Jv_Utf8Const *, jstring, jint);
153 extern jboolean _Jv_equaln (_Jv_Utf8Const *, jstring, jint);
155 /* Helper class which converts a jstring to a temporary char*.
156 Uses the supplied buffer, if non-null. Otherwise, allocates
157 the buffer on the heap. Use the JV_TEMP_UTF_STRING macro,
158 which follows, to automatically allocate a stack buffer if
159 the string is small enough. */
160 class _Jv_TempUTFString
162 public:
163 _Jv_TempUTFString(jstring jstr, char* buf=0);
164 ~_Jv_TempUTFString();
166 // Accessors
167 operator const char*() const
169 return buf_;
171 const char* buf() const
173 return buf_;
175 char* buf()
177 return buf_;
180 private:
181 char* buf_;
182 bool heapAllocated_;
185 inline _Jv_TempUTFString::_Jv_TempUTFString (jstring jstr, char* buf)
186 : buf_(0), heapAllocated_(false)
188 if (!jstr) return;
189 jsize len = JvGetStringUTFLength (jstr);
190 if (buf)
191 buf_ = buf;
192 else
194 buf_ = (char*) _Jv_Malloc (len+1);
195 heapAllocated_ = true;
198 JvGetStringUTFRegion (jstr, 0, jstr->length(), buf_);
199 buf_[len] = '\0';
202 inline _Jv_TempUTFString::~_Jv_TempUTFString ()
204 if (heapAllocated_)
205 _Jv_Free (buf_);
208 /* Macro which uses _Jv_TempUTFString. Allocates a stack-based
209 buffer if the string and its null terminator are <= 256
210 characters in length. Otherwise, a heap-based buffer is
211 used. The parameters to this macro are the variable name
212 which is an instance of _Jv_TempUTFString (above) and a
213 jstring.
215 Sample Usage:
217 jstring jstr = getAJString();
218 JV_TEMP_UTF_STRING(utfstr, jstr);
219 printf("The string is: %s\n", utfstr.buf());
222 #define JV_TEMP_UTF_STRING(utfstr, jstr) \
223 jstring utfstr##thejstr = (jstr); \
224 jsize utfstr##_len = utfstr##thejstr ? JvGetStringUTFLength (utfstr##thejstr) + 1 : 0; \
225 char utfstr##_buf[utfstr##_len <= 256 ? utfstr##_len : 0]; \
226 _Jv_TempUTFString utfstr(utfstr##thejstr, sizeof(utfstr##_buf)==0 ? 0 : utfstr##_buf)
228 // FIXME: remove this define.
229 #define StringClass java::lang::String::class$
231 namespace gcj
233 /* Some constants used during lookup of special class methods. */
234 extern _Jv_Utf8Const *void_signature; /* "()V" */
235 extern _Jv_Utf8Const *clinit_name; /* "<clinit>" */
236 extern _Jv_Utf8Const *init_name; /* "<init>" */
237 extern _Jv_Utf8Const *finit_name; /* "finit$", */
239 /* Set to true by _Jv_CreateJavaVM. */
240 extern bool runtimeInitialized;
243 /* Type of pointer used as finalizer. */
244 typedef void _Jv_FinalizerFunc (jobject);
246 /* Allocate space for a new Java object. */
247 void *_Jv_AllocObj (jsize size, jclass cl) __attribute__((__malloc__));
248 /* Allocate space for a potentially uninitialized pointer-free object.
249 Interesting only with JV_HASH_SYNCHRONIZATION. */
250 void *_Jv_AllocPtrFreeObj (jsize size, jclass cl) __attribute__((__malloc__));
251 /* Allocate space for an array of Java objects. */
252 void *_Jv_AllocArray (jsize size, jclass cl) __attribute__((__malloc__));
253 /* Allocate space that is known to be pointer-free. */
254 void *_Jv_AllocBytes (jsize size) __attribute__((__malloc__));
255 /* Allocate space for a new non-Java object, which does not have the usual
256 Java object header but may contain pointers to other GC'ed objects. */
257 void *_Jv_AllocRawObj (jsize size) __attribute__((__malloc__));
258 /* Explicitly throw an out-of-memory exception. */
259 void _Jv_ThrowNoMemory() __attribute__((__noreturn__));
260 /* Allocate an object with a single pointer. The first word is reserved
261 for the GC, and the second word is the traced pointer. */
262 void *_Jv_AllocTraceOne (jsize size /* incl. reserved slot */);
263 /* Ditto, but for two traced pointers. */
264 void *_Jv_AllocTraceTwo (jsize size /* incl. reserved slot */);
265 /* Initialize the GC. */
266 void _Jv_InitGC (void);
267 /* Register a finalizer. */
268 void _Jv_RegisterFinalizer (void *object, _Jv_FinalizerFunc *method);
269 /* Compute the GC descriptor for a class */
270 void * _Jv_BuildGCDescr(jclass);
272 /* Allocate some unscanned, unmoveable memory. Return NULL if out of
273 memory. */
274 void *_Jv_MallocUnchecked (jsize size) __attribute__((__malloc__));
276 /* Initialize finalizers. The argument is a function to be called
277 when a finalizer is ready to be run. */
278 void _Jv_GCInitializeFinalizers (void (*notifier) (void));
279 /* Run finalizers for objects ready to be finalized.. */
280 void _Jv_RunFinalizers (void);
281 /* Run all finalizers. Should be called only before exit. */
282 void _Jv_RunAllFinalizers (void);
283 /* Perform a GC. */
284 void _Jv_RunGC (void);
285 /* Disable and enable GC. */
286 void _Jv_DisableGC (void);
287 void _Jv_EnableGC (void);
288 /* Register a disappearing link. This is a field F which should be
289 cleared when *F is found to be inaccessible. This is used in the
290 implementation of java.lang.ref.Reference. */
291 void _Jv_GCRegisterDisappearingLink (jobject *objp);
292 /* Return true if OBJECT should be reclaimed. This is used to
293 implement soft references. */
294 jboolean _Jv_GCCanReclaimSoftReference (jobject obj);
296 /* Register a finalizer for a String object. This is only used by
297 the intern() implementation. */
298 void _Jv_RegisterStringFinalizer (jobject str);
299 /* This is called to actually finalize a possibly-intern()d String. */
300 void _Jv_FinalizeString (jobject str);
302 /* Return approximation of total size of heap. */
303 long _Jv_GCTotalMemory (void);
304 /* Return approximation of total free memory. */
305 long _Jv_GCFreeMemory (void);
307 /* Set initial heap size. If SIZE==0, ignore. Should be run before
308 _Jv_InitGC. Not required to have any actual effect. */
309 void _Jv_GCSetInitialHeapSize (size_t size);
311 /* Set maximum heap size. If SIZE==0, unbounded. Should be run
312 before _Jv_InitGC. Not required to have any actual effect. */
313 void _Jv_GCSetMaximumHeapSize (size_t size);
315 /* External interface to setting the heap size. Parses ARG (a number
316 which can optionally have "k" or "m" appended and calls
317 _Jv_GCSetInitialHeapSize. */
318 void _Jv_SetInitialHeapSize (const char *arg);
320 /* External interface to setting the maximum heap size. Parses ARG (a
321 number which can optionally have "k" or "m" appended and calls
322 _Jv_GCSetMaximumHeapSize. */
323 void _Jv_SetMaximumHeapSize (const char *arg);
325 extern "C" void JvRunMain (jclass klass, int argc, const char **argv);
326 void _Jv_RunMain (jclass klass, const char *name, int argc, const char **argv,
327 bool is_jar);
329 // Delayed until after _Jv_AllocBytes is declared.
331 // Note that we allocate this as unscanned memory -- the vtables
332 // are handled specially by the GC.
334 inline _Jv_VTable *
335 _Jv_VTable::new_vtable (int count)
337 size_t size = sizeof(_Jv_VTable) + (count - 1) * vtable_elt_size ();
338 return (_Jv_VTable *) _Jv_AllocBytes (size);
341 // Determine if METH gets an entry in a VTable.
342 static inline jboolean _Jv_isVirtualMethod (_Jv_Method *meth)
344 using namespace java::lang::reflect;
345 return (((meth->accflags & (Modifier::STATIC | Modifier::PRIVATE)) == 0)
346 && meth->name->data[0] != '<');
349 // This function is used to determine the hash code of an object.
350 inline jint
351 _Jv_HashCode (jobject obj)
353 // This was chosen to yield relatively well distributed results on
354 // both 32- and 64-bit architectures. Note 0x7fffffff is prime.
355 // FIXME: we assume sizeof(long) == sizeof(void *).
356 return (jint) ((unsigned long) obj % 0x7fffffff);
359 // Return a raw pointer to the elements of an array given the array
360 // and its element type. You might think we could just pick a single
361 // array type and use elements() on it, but we can't because we must
362 // account for alignment of the element type. When ARRAY is null, we
363 // obtain the number of bytes taken by the base part of the array.
364 inline char *
365 _Jv_GetArrayElementFromElementType (jobject array,
366 jclass element_type)
368 char *elts;
369 if (element_type == JvPrimClass (byte))
370 elts = (char *) elements ((jbyteArray) array);
371 else if (element_type == JvPrimClass (short))
372 elts = (char *) elements ((jshortArray) array);
373 else if (element_type == JvPrimClass (int))
374 elts = (char *) elements ((jintArray) array);
375 else if (element_type == JvPrimClass (long))
376 elts = (char *) elements ((jlongArray) array);
377 else if (element_type == JvPrimClass (boolean))
378 elts = (char *) elements ((jbooleanArray) array);
379 else if (element_type == JvPrimClass (char))
380 elts = (char *) elements ((jcharArray) array);
381 else if (element_type == JvPrimClass (float))
382 elts = (char *) elements ((jfloatArray) array);
383 else if (element_type == JvPrimClass (double))
384 elts = (char *) elements ((jdoubleArray) array);
385 else
386 elts = (char *) elements ((jobjectArray) array);
387 return elts;
390 extern "C" void _Jv_ThrowBadArrayIndex (jint bad_index)
391 __attribute__((noreturn));
392 extern "C" void _Jv_ThrowNullPointerException (void)
393 __attribute__((noreturn));
394 extern "C" jobject _Jv_NewArray (jint type, jint size)
395 __attribute__((__malloc__));
396 extern "C" jobject _Jv_NewMultiArray (jclass klass, jint dims, ...)
397 __attribute__((__malloc__));
398 extern "C" void *_Jv_CheckCast (jclass klass, jobject obj);
399 extern "C" void *_Jv_LookupInterfaceMethod (jclass klass, Utf8Const *name,
400 Utf8Const *signature);
401 extern "C" void *_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface,
402 int meth_idx);
403 extern "C" void _Jv_CheckArrayStore (jobject array, jobject obj);
404 extern "C" void _Jv_RegisterClass (jclass klass);
405 extern "C" void _Jv_RegisterClasses (jclass *classes);
406 extern "C" void _Jv_RegisterResource (void *vptr);
407 extern void _Jv_UnregisterClass (_Jv_Utf8Const*, java::lang::ClassLoader*);
408 extern void _Jv_ResolveField (_Jv_Field *, java::lang::ClassLoader*);
410 extern jclass _Jv_FindClass (_Jv_Utf8Const *name,
411 java::lang::ClassLoader *loader);
412 extern jclass _Jv_FindClassFromSignature (char *,
413 java::lang::ClassLoader *loader);
414 extern void _Jv_GetTypesFromSignature (jmethodID method,
415 jclass declaringClass,
416 JArray<jclass> **arg_types_out,
417 jclass *return_type_out);
419 extern jboolean _Jv_CheckAccess (jclass self_klass, jclass other_klass,
420 jint flags);
422 extern jobject _Jv_CallAnyMethodA (jobject obj, jclass return_type,
423 jmethodID meth, jboolean is_constructor,
424 JArray<jclass> *parameter_types,
425 jobjectArray args,
426 jclass iface = NULL);
428 union jvalue;
429 extern void _Jv_CallAnyMethodA (jobject obj,
430 jclass return_type,
431 jmethodID meth,
432 jboolean is_constructor,
433 jboolean is_virtual_call,
434 JArray<jclass> *parameter_types,
435 jvalue *args,
436 jvalue *result,
437 jboolean is_jni_call = true,
438 jclass iface = NULL);
440 extern jobject _Jv_NewMultiArray (jclass, jint ndims, jint* dims)
441 __attribute__((__malloc__));
443 /* Checked divide subroutines. */
444 extern "C"
446 jint _Jv_divI (jint, jint);
447 jint _Jv_remI (jint, jint);
448 jlong _Jv_divJ (jlong, jlong);
449 jlong _Jv_remJ (jlong, jlong);
452 /* Get the number of arguments (cf. argc) or 0 if our argument
453 list was never initialized. */
454 extern int _Jv_GetNbArgs (void);
456 /* Get the specified argument (cf. argv[index]) or "" if either
457 our argument list was never initialized or the specified index
458 is out of bounds. */
459 extern const char * _Jv_GetSafeArg (int index);
461 /* Sets our argument list. Can be used by programs with non-standard
462 entry points. */
463 extern void _Jv_SetArgs (int argc, const char **argv);
465 /* Get the name of the running executable. */
466 extern const char *_Jv_ThisExecutable (void);
468 /* Return a pointer to a symbol in executable or loaded library. */
469 void *_Jv_FindSymbolInExecutable (const char *);
471 /* Initialize JNI. */
472 extern void _Jv_JNI_Init (void);
474 /* Get or set the per-thread JNIEnv used by the invocation API. */
475 _Jv_JNIEnv *_Jv_GetCurrentJNIEnv ();
476 void _Jv_SetCurrentJNIEnv (_Jv_JNIEnv *);
478 struct _Jv_JavaVM;
479 _Jv_JavaVM *_Jv_GetJavaVM ();
481 // Some verification functions from defineclass.cc.
482 bool _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig);
483 bool _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig);
484 bool _Jv_VerifyClassName (unsigned char* ptr, _Jv_ushort length);
485 bool _Jv_VerifyClassName (_Jv_Utf8Const *name);
486 bool _Jv_VerifyIdentifier (_Jv_Utf8Const *);
487 bool _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2);
489 struct _Jv_core_chain
491 int name_length;
492 const char *name;
493 int data_length;
494 const void *data;
496 struct _Jv_core_chain *next;
499 // This is called when new core data is loaded.
500 extern void (*_Jv_RegisterCoreHook) (_Jv_core_chain *);
502 _Jv_core_chain *_Jv_FindCore (_Jv_core_chain *node, jstring name);
503 void _Jv_FreeCoreChain (_Jv_core_chain *chain);
505 #ifdef ENABLE_JVMPI
507 #include "jvmpi.h"
509 extern void (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (JVMPI_Event *event);
510 extern void (*_Jv_JVMPI_Notify_THREAD_START) (JVMPI_Event *event);
511 extern void (*_Jv_JVMPI_Notify_THREAD_END) (JVMPI_Event *event);
512 #endif
514 #endif /* __JAVA_JVM_H__ */