OSDN Git Service

2002-03-18 Andrew Haley <aph@cambridge.redhat.com>
[pf3gnuchains/gcc-fork.git] / libjava / include / jvm.h
1 // jvm.h - Header file for private implementation information. -*- c++ -*-
2
3 /* Copyright (C) 1998, 1999, 2000, 2001  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 #ifndef __JAVA_JVM_H__
12 #define __JAVA_JVM_H__
13
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__
19
20 #include <gcj/javaprims.h>
21
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>
26
27 #include <java/lang/Object.h>
28
29 // Include cni.h before field.h to enable all definitions.  FIXME.
30 #include <gcj/cni.h>
31 #include <gcj/field.h>
32
33 /* Structure of the virtual table.  */
34 struct _Jv_VTable
35 {
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;
43
44   // This must be last, as derived classes "extend" this by
45   // adding new data members.
46   vtable_elt method[1];
47
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()
52   {
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;
58   }
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
64
65   static size_t vtable_elt_size() { return sizeof(vtable_elt); }
66
67   // Given a method index, return byte offset from the vtable pointer.
68   static jint idx_to_offset (int index)
69   {
70     return (2 * sizeof (void *)) + (index * vtable_elt_size ());
71   }
72   static _Jv_VTable *new_vtable (int count);
73 };
74
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
78
79 // This structure is the type of an array's vtable.
80 struct _Jv_ArrayVTable : public _Jv_VTable
81 {
82   vtable_elt extra_method[NUM_OBJECT_METHODS - 1];
83 };
84
85 union _Jv_word
86 {
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;
92
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
99
100   jclass                     clazz;
101   jstring                    string;
102   struct _Jv_Field          *field;
103   struct _Jv_Utf8Const      *utf8;
104   struct _Jv_ResolvedMethod *rmethod;
105 };
106
107 union _Jv_word2
108 {
109   jint ia[2];
110   jlong l;
111   jdouble d;
112 };                              
113
114 /* Extract a character from a Java-style Utf8 string.
115  * PTR points to the current character.
116  * LIMIT points to the end of the Utf8 string.
117  * PTR is incremented to point after the character thta gets returns.
118  * On an error, -1 is returned. */
119 #define UTF8_GET(PTR, LIMIT) \
120   ((PTR) >= (LIMIT) ? -1 \
121    : *(PTR) < 128 ? *(PTR)++ \
122    : (*(PTR)&0xE0) == 0xC0 && ((PTR)+=2)<=(LIMIT) && ((PTR)[-1]&0xC0) == 0x80 \
123    ? (((PTR)[-2] & 0x1F) << 6) + ((PTR)[-1] & 0x3F) \
124    : (*(PTR) & 0xF0) == 0xE0 && ((PTR) += 3) <= (LIMIT) \
125    && ((PTR)[-2] & 0xC0) == 0x80 && ((PTR)[-1] & 0xC0) == 0x80 \
126    ? (((PTR)[-3]&0x0F) << 12) + (((PTR)[-2]&0x3F) << 6) + ((PTR)[-1]&0x3F) \
127    : ((PTR)++, -1))
128
129 extern int _Jv_strLengthUtf8(char* str, int len);
130
131 typedef struct _Jv_Utf8Const Utf8Const;
132 _Jv_Utf8Const *_Jv_makeUtf8Const (char *s, int len);
133 _Jv_Utf8Const *_Jv_makeUtf8Const (jstring string);
134 extern jboolean _Jv_equalUtf8Consts (_Jv_Utf8Const *, _Jv_Utf8Const *);
135 extern jboolean _Jv_equal (_Jv_Utf8Const *, jstring, jint);
136 extern jboolean _Jv_equaln (_Jv_Utf8Const *, jstring, jint);
137
138 // FIXME: remove this define.
139 #define StringClass java::lang::String::class$
140
141 namespace gcj
142 {
143   /* Some constants used during lookup of special class methods.  */
144   extern _Jv_Utf8Const *void_signature; /* "()V" */
145   extern _Jv_Utf8Const *clinit_name;    /* "<clinit>" */
146   extern _Jv_Utf8Const *init_name;      /* "<init>" */
147   extern _Jv_Utf8Const *finit_name;     /* "finit$", */
148   
149   /* Set to true by _Jv_CreateJavaVM. */
150   extern bool runtimeInitialized;
151 };
152
153 /* Type of pointer used as finalizer.  */
154 typedef void _Jv_FinalizerFunc (jobject);
155
156 /* Allocate space for a new Java object.  */
157 void *_Jv_AllocObj (jsize size, jclass cl) __attribute__((__malloc__));
158 /* Allocate space for a potentially uninitialized pointer-free object.
159    Interesting only with JV_HASH_SYNCHRONIZATION.  */
160 void *_Jv_AllocPtrFreeObj (jsize size, jclass cl) __attribute__((__malloc__));
161 /* Allocate space for an array of Java objects.  */
162 void *_Jv_AllocArray (jsize size, jclass cl) __attribute__((__malloc__));
163 /* Allocate space that is known to be pointer-free.  */
164 void *_Jv_AllocBytes (jsize size) __attribute__((__malloc__));
165 /* Allocate space for a new non-Java object, which does not have the usual 
166    Java object header but may contain pointers to other GC'ed objects.  */
167 void *_Jv_AllocRawObj (jsize size) __attribute__((__malloc__));
168 /* Explicitly throw an out-of-memory exception. */
169 void _Jv_ThrowNoMemory() __attribute__((__noreturn__));
170 /* Allocate an object with a single pointer.  The first word is reserved
171    for the GC, and the second word is the traced pointer.  */
172 void *_Jv_AllocTraceOne (jsize size /* incl. reserved slot */);
173 /* Ditto, but for two traced pointers.                     */
174 void *_Jv_AllocTraceTwo (jsize size /* incl. reserved slot */);
175 /* Initialize the GC.  */
176 void _Jv_InitGC (void);
177 /* Register a finalizer.  */
178 void _Jv_RegisterFinalizer (void *object, _Jv_FinalizerFunc *method);
179 /* Compute the GC descriptor for a class */
180 void * _Jv_BuildGCDescr(jclass);
181
182 /* Allocate some unscanned, unmoveable memory.  Return NULL if out of
183    memory.  */
184 void *_Jv_MallocUnchecked (jsize size) __attribute__((__malloc__));
185
186 /* Initialize finalizers.  The argument is a function to be called
187    when a finalizer is ready to be run.  */
188 void _Jv_GCInitializeFinalizers (void (*notifier) (void));
189 /* Run finalizers for objects ready to be finalized..  */
190 void _Jv_RunFinalizers (void);
191 /* Run all finalizers.  Should be called only before exit.  */
192 void _Jv_RunAllFinalizers (void);
193 /* Perform a GC.  */
194 void _Jv_RunGC (void);
195 /* Disable and enable GC.  */
196 void _Jv_DisableGC (void);
197 void _Jv_EnableGC (void);
198 /* Register a disappearing link.  This is a field F which should be
199    cleared when *F is found to be inaccessible.  This is used in the
200    implementation of java.lang.ref.Reference.  */
201 void _Jv_GCRegisterDisappearingLink (jobject *objp);
202 /* Return true if OBJECT should be reclaimed.  This is used to
203    implement soft references.  */
204 jboolean _Jv_GCCanReclaimSoftReference (jobject obj);
205
206 /* Return approximation of total size of heap.  */
207 long _Jv_GCTotalMemory (void);
208 /* Return approximation of total free memory.  */
209 long _Jv_GCFreeMemory (void);
210
211 /* Set initial heap size.  If SIZE==0, ignore.  Should be run before
212    _Jv_InitGC.  Not required to have any actual effect.  */
213 void _Jv_GCSetInitialHeapSize (size_t size);
214
215 /* Set maximum heap size.  If SIZE==0, unbounded.  Should be run
216    before _Jv_InitGC.  Not required to have any actual effect.  */
217 void _Jv_GCSetMaximumHeapSize (size_t size);
218
219 /* External interface to setting the heap size.  Parses ARG (a number
220    which can optionally have "k" or "m" appended and calls
221    _Jv_GCSetInitialHeapSize.  */
222 void _Jv_SetInitialHeapSize (const char *arg);
223
224 /* External interface to setting the maximum heap size.  Parses ARG (a
225    number which can optionally have "k" or "m" appended and calls
226    _Jv_GCSetMaximumHeapSize.  */
227 void _Jv_SetMaximumHeapSize (const char *arg);
228
229 extern "C" void JvRunMain (jclass klass, int argc, const char **argv);
230 void _Jv_RunMain (jclass klass, const char *name, int argc, const char **argv, 
231                   bool is_jar);
232
233 // Delayed until after _Jv_AllocBytes is declared.
234 //
235 // Note that we allocate this as unscanned memory -- the vtables
236 // are handled specially by the GC.
237
238 inline _Jv_VTable *
239 _Jv_VTable::new_vtable (int count)
240 {
241   size_t size = sizeof(_Jv_VTable) + (count - 1) * vtable_elt_size ();
242   return (_Jv_VTable *) _Jv_AllocBytes (size);
243 }
244
245 // This function is used to determine the hash code of an object.
246 inline jint
247 _Jv_HashCode (jobject obj)
248 {
249   // This was chosen to yield relatively well distributed results on
250   // both 32- and 64-bit architectures.  Note 0x7fffffff is prime.
251   // FIXME: we assume sizeof(long) == sizeof(void *).
252   return (jint) ((unsigned long) obj % 0x7fffffff);
253 }
254
255 // Return a raw pointer to the elements of an array given the array
256 // and its element type.  You might think we could just pick a single
257 // array type and use elements() on it, but we can't because we must
258 // account for alignment of the element type.  When ARRAY is null, we
259 // obtain the number of bytes taken by the base part of the array.
260 inline char *
261 _Jv_GetArrayElementFromElementType (jobject array,
262                                     jclass element_type)
263 {
264   char *elts;
265   if (element_type == JvPrimClass (byte))
266     elts = (char *) elements ((jbyteArray) array);
267   else if (element_type == JvPrimClass (short))
268     elts = (char *) elements ((jshortArray) array);
269   else if (element_type == JvPrimClass (int))
270     elts = (char *) elements ((jintArray) array);
271   else if (element_type == JvPrimClass (long))
272     elts = (char *) elements ((jlongArray) array);
273   else if (element_type == JvPrimClass (boolean))
274     elts = (char *) elements ((jbooleanArray) array);
275   else if (element_type == JvPrimClass (char))
276     elts = (char *) elements ((jcharArray) array);
277   else if (element_type == JvPrimClass (float))
278     elts = (char *) elements ((jfloatArray) array);
279   else if (element_type == JvPrimClass (double))
280     elts = (char *) elements ((jdoubleArray) array);
281   else
282     elts = (char *) elements ((jobjectArray) array);
283   return elts;
284 }
285
286 extern "C" void _Jv_ThrowBadArrayIndex (jint bad_index);
287 extern "C" void _Jv_ThrowNullPointerException (void);
288 extern "C" jobject _Jv_NewArray (jint type, jint size)
289   __attribute__((__malloc__));
290 extern "C" jobject _Jv_NewMultiArray (jclass klass, jint dims, ...)
291   __attribute__((__malloc__));
292 extern "C" void *_Jv_CheckCast (jclass klass, jobject obj);
293 extern "C" void *_Jv_LookupInterfaceMethod (jclass klass, Utf8Const *name,
294                                            Utf8Const *signature);
295 extern "C" void *_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface, 
296                                                int meth_idx);
297 extern "C" void _Jv_CheckArrayStore (jobject array, jobject obj);
298 extern "C" void _Jv_RegisterClass (jclass klass);
299 extern "C" void _Jv_RegisterClasses (jclass *classes);
300 extern "C" void _Jv_RegisterResource (void *vptr);
301 extern void _Jv_UnregisterClass (_Jv_Utf8Const*, java::lang::ClassLoader*);
302 extern void _Jv_ResolveField (_Jv_Field *, java::lang::ClassLoader*);
303
304 extern jclass _Jv_FindClass (_Jv_Utf8Const *name,
305                              java::lang::ClassLoader *loader);
306 extern jclass _Jv_FindClassFromSignature (char *,
307                                           java::lang::ClassLoader *loader);
308 extern void _Jv_GetTypesFromSignature (jmethodID method,
309                                        jclass declaringClass,
310                                        JArray<jclass> **arg_types_out,
311                                        jclass *return_type_out);
312
313 extern jobject _Jv_CallAnyMethodA (jobject obj, jclass return_type,
314                                    jmethodID meth, jboolean is_constructor,
315                                    JArray<jclass> *parameter_types,
316                                    jobjectArray args);
317
318 union jvalue;
319 extern jthrowable _Jv_CallAnyMethodA (jobject obj,
320                                       jclass return_type,
321                                       jmethodID meth,
322                                       jboolean is_constructor,
323                                       JArray<jclass> *parameter_types,
324                                       jvalue *args,
325                                       jvalue *result);
326
327 extern jobject _Jv_NewMultiArray (jclass, jint ndims, jint* dims)
328   __attribute__((__malloc__));
329
330 /* Checked divide subroutines. */
331 extern "C"
332 {
333   jint _Jv_divI (jint, jint);
334   jint _Jv_remI (jint, jint);
335   jlong _Jv_divJ (jlong, jlong);
336   jlong _Jv_remJ (jlong, jlong);
337 }
338
339 /* get/set the name of the running executable. */
340 extern char *_Jv_ThisExecutable (void);
341 extern void _Jv_ThisExecutable (const char *);
342
343 /* Return a pointer to a symbol in executable or loaded library.  */
344 void *_Jv_FindSymbolInExecutable (const char *);
345
346 /* Initialize JNI.  */
347 extern void _Jv_JNI_Init (void);
348
349 /* Get or set the per-thread JNIEnv used by the invocation API.  */
350 _Jv_JNIEnv *_Jv_GetCurrentJNIEnv ();
351 void _Jv_SetCurrentJNIEnv (_Jv_JNIEnv *);
352
353 struct _Jv_JavaVM;
354 _Jv_JavaVM *_Jv_GetJavaVM (); 
355
356 // Some verification functions from defineclass.cc.
357 bool _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig);
358 bool _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig);
359 bool _Jv_VerifyClassName (unsigned char* ptr, _Jv_ushort length);
360 bool _Jv_VerifyClassName (_Jv_Utf8Const *name);
361 bool _Jv_VerifyIdentifier (_Jv_Utf8Const *);
362 bool _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2);
363
364 #ifdef ENABLE_JVMPI
365
366 #include "jvmpi.h"
367
368 extern void (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (JVMPI_Event *event);
369 extern void (*_Jv_JVMPI_Notify_THREAD_START) (JVMPI_Event *event);
370 extern void (*_Jv_JVMPI_Notify_THREAD_END) (JVMPI_Event *event);
371 #endif
372
373 #endif /* __JAVA_JVM_H__ */