OSDN Git Service

* include/jvm.h (struct _Jv_frame_info): New structure.
[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, 2002  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 // 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
118 {
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
126 };
127
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))
142
143 extern int _Jv_strLengthUtf8(char* str, int len);
144
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 (_Jv_Utf8Const *, _Jv_Utf8Const *);
149 extern jboolean _Jv_equal (_Jv_Utf8Const *, jstring, jint);
150 extern jboolean _Jv_equaln (_Jv_Utf8Const *, jstring, jint);
151
152 // FIXME: remove this define.
153 #define StringClass java::lang::String::class$
154
155 namespace gcj
156 {
157   /* Some constants used during lookup of special class methods.  */
158   extern _Jv_Utf8Const *void_signature; /* "()V" */
159   extern _Jv_Utf8Const *clinit_name;    /* "<clinit>" */
160   extern _Jv_Utf8Const *init_name;      /* "<init>" */
161   extern _Jv_Utf8Const *finit_name;     /* "finit$", */
162   
163   /* Set to true by _Jv_CreateJavaVM. */
164   extern bool runtimeInitialized;
165 };
166
167 /* Type of pointer used as finalizer.  */
168 typedef void _Jv_FinalizerFunc (jobject);
169
170 /* Allocate space for a new Java object.  */
171 void *_Jv_AllocObj (jsize size, jclass cl) __attribute__((__malloc__));
172 /* Allocate space for a potentially uninitialized pointer-free object.
173    Interesting only with JV_HASH_SYNCHRONIZATION.  */
174 void *_Jv_AllocPtrFreeObj (jsize size, jclass cl) __attribute__((__malloc__));
175 /* Allocate space for an array of Java objects.  */
176 void *_Jv_AllocArray (jsize size, jclass cl) __attribute__((__malloc__));
177 /* Allocate space that is known to be pointer-free.  */
178 void *_Jv_AllocBytes (jsize size) __attribute__((__malloc__));
179 /* Allocate space for a new non-Java object, which does not have the usual 
180    Java object header but may contain pointers to other GC'ed objects.  */
181 void *_Jv_AllocRawObj (jsize size) __attribute__((__malloc__));
182 /* Explicitly throw an out-of-memory exception. */
183 void _Jv_ThrowNoMemory() __attribute__((__noreturn__));
184 /* Allocate an object with a single pointer.  The first word is reserved
185    for the GC, and the second word is the traced pointer.  */
186 void *_Jv_AllocTraceOne (jsize size /* incl. reserved slot */);
187 /* Ditto, but for two traced pointers.                     */
188 void *_Jv_AllocTraceTwo (jsize size /* incl. reserved slot */);
189 /* Initialize the GC.  */
190 void _Jv_InitGC (void);
191 /* Register a finalizer.  */
192 void _Jv_RegisterFinalizer (void *object, _Jv_FinalizerFunc *method);
193 /* Compute the GC descriptor for a class */
194 void * _Jv_BuildGCDescr(jclass);
195
196 /* Allocate some unscanned, unmoveable memory.  Return NULL if out of
197    memory.  */
198 void *_Jv_MallocUnchecked (jsize size) __attribute__((__malloc__));
199
200 /* Initialize finalizers.  The argument is a function to be called
201    when a finalizer is ready to be run.  */
202 void _Jv_GCInitializeFinalizers (void (*notifier) (void));
203 /* Run finalizers for objects ready to be finalized..  */
204 void _Jv_RunFinalizers (void);
205 /* Run all finalizers.  Should be called only before exit.  */
206 void _Jv_RunAllFinalizers (void);
207 /* Perform a GC.  */
208 void _Jv_RunGC (void);
209 /* Disable and enable GC.  */
210 void _Jv_DisableGC (void);
211 void _Jv_EnableGC (void);
212 /* Register a disappearing link.  This is a field F which should be
213    cleared when *F is found to be inaccessible.  This is used in the
214    implementation of java.lang.ref.Reference.  */
215 void _Jv_GCRegisterDisappearingLink (jobject *objp);
216 /* Return true if OBJECT should be reclaimed.  This is used to
217    implement soft references.  */
218 jboolean _Jv_GCCanReclaimSoftReference (jobject obj);
219
220 /* Return approximation of total size of heap.  */
221 long _Jv_GCTotalMemory (void);
222 /* Return approximation of total free memory.  */
223 long _Jv_GCFreeMemory (void);
224
225 /* Set initial heap size.  If SIZE==0, ignore.  Should be run before
226    _Jv_InitGC.  Not required to have any actual effect.  */
227 void _Jv_GCSetInitialHeapSize (size_t size);
228
229 /* Set maximum heap size.  If SIZE==0, unbounded.  Should be run
230    before _Jv_InitGC.  Not required to have any actual effect.  */
231 void _Jv_GCSetMaximumHeapSize (size_t size);
232
233 /* External interface to setting the heap size.  Parses ARG (a number
234    which can optionally have "k" or "m" appended and calls
235    _Jv_GCSetInitialHeapSize.  */
236 void _Jv_SetInitialHeapSize (const char *arg);
237
238 /* External interface to setting the maximum heap size.  Parses ARG (a
239    number which can optionally have "k" or "m" appended and calls
240    _Jv_GCSetMaximumHeapSize.  */
241 void _Jv_SetMaximumHeapSize (const char *arg);
242
243 extern "C" void JvRunMain (jclass klass, int argc, const char **argv);
244 void _Jv_RunMain (jclass klass, const char *name, int argc, const char **argv, 
245                   bool is_jar);
246
247 // Delayed until after _Jv_AllocBytes is declared.
248 //
249 // Note that we allocate this as unscanned memory -- the vtables
250 // are handled specially by the GC.
251
252 inline _Jv_VTable *
253 _Jv_VTable::new_vtable (int count)
254 {
255   size_t size = sizeof(_Jv_VTable) + (count - 1) * vtable_elt_size ();
256   return (_Jv_VTable *) _Jv_AllocBytes (size);
257 }
258
259 // This function is used to determine the hash code of an object.
260 inline jint
261 _Jv_HashCode (jobject obj)
262 {
263   // This was chosen to yield relatively well distributed results on
264   // both 32- and 64-bit architectures.  Note 0x7fffffff is prime.
265   // FIXME: we assume sizeof(long) == sizeof(void *).
266   return (jint) ((unsigned long) obj % 0x7fffffff);
267 }
268
269 // Return a raw pointer to the elements of an array given the array
270 // and its element type.  You might think we could just pick a single
271 // array type and use elements() on it, but we can't because we must
272 // account for alignment of the element type.  When ARRAY is null, we
273 // obtain the number of bytes taken by the base part of the array.
274 inline char *
275 _Jv_GetArrayElementFromElementType (jobject array,
276                                     jclass element_type)
277 {
278   char *elts;
279   if (element_type == JvPrimClass (byte))
280     elts = (char *) elements ((jbyteArray) array);
281   else if (element_type == JvPrimClass (short))
282     elts = (char *) elements ((jshortArray) array);
283   else if (element_type == JvPrimClass (int))
284     elts = (char *) elements ((jintArray) array);
285   else if (element_type == JvPrimClass (long))
286     elts = (char *) elements ((jlongArray) array);
287   else if (element_type == JvPrimClass (boolean))
288     elts = (char *) elements ((jbooleanArray) array);
289   else if (element_type == JvPrimClass (char))
290     elts = (char *) elements ((jcharArray) array);
291   else if (element_type == JvPrimClass (float))
292     elts = (char *) elements ((jfloatArray) array);
293   else if (element_type == JvPrimClass (double))
294     elts = (char *) elements ((jdoubleArray) array);
295   else
296     elts = (char *) elements ((jobjectArray) array);
297   return elts;
298 }
299
300 extern "C" void _Jv_ThrowBadArrayIndex (jint bad_index)
301   __attribute__((noreturn));
302 extern "C" void _Jv_ThrowNullPointerException (void)
303   __attribute__((noreturn));
304 extern "C" jobject _Jv_NewArray (jint type, jint size)
305   __attribute__((__malloc__));
306 extern "C" jobject _Jv_NewMultiArray (jclass klass, jint dims, ...)
307   __attribute__((__malloc__));
308 extern "C" void *_Jv_CheckCast (jclass klass, jobject obj);
309 extern "C" void *_Jv_LookupInterfaceMethod (jclass klass, Utf8Const *name,
310                                            Utf8Const *signature);
311 extern "C" void *_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface, 
312                                                int meth_idx);
313 extern "C" void _Jv_CheckArrayStore (jobject array, jobject obj);
314 extern "C" void _Jv_RegisterClass (jclass klass);
315 extern "C" void _Jv_RegisterClasses (jclass *classes);
316 extern "C" void _Jv_RegisterResource (void *vptr);
317 extern void _Jv_UnregisterClass (_Jv_Utf8Const*, java::lang::ClassLoader*);
318 extern void _Jv_ResolveField (_Jv_Field *, java::lang::ClassLoader*);
319
320 extern jclass _Jv_FindClass (_Jv_Utf8Const *name,
321                              java::lang::ClassLoader *loader);
322 extern jclass _Jv_FindClassFromSignature (char *,
323                                           java::lang::ClassLoader *loader);
324 extern void _Jv_GetTypesFromSignature (jmethodID method,
325                                        jclass declaringClass,
326                                        JArray<jclass> **arg_types_out,
327                                        jclass *return_type_out);
328
329 extern jobject _Jv_CallAnyMethodA (jobject obj, jclass return_type,
330                                    jmethodID meth, jboolean is_constructor,
331                                    JArray<jclass> *parameter_types,
332                                    jobjectArray args);
333
334 union jvalue;
335 extern jthrowable _Jv_CallAnyMethodA (jobject obj,
336                                       jclass return_type,
337                                       jmethodID meth,
338                                       jboolean is_constructor,
339                                       JArray<jclass> *parameter_types,
340                                       jvalue *args,
341                                       jvalue *result);
342
343 extern jobject _Jv_NewMultiArray (jclass, jint ndims, jint* dims)
344   __attribute__((__malloc__));
345
346 /* Checked divide subroutines. */
347 extern "C"
348 {
349   jint _Jv_divI (jint, jint);
350   jint _Jv_remI (jint, jint);
351   jlong _Jv_divJ (jlong, jlong);
352   jlong _Jv_remJ (jlong, jlong);
353 }
354
355 /* get/set the name of the running executable. */
356 extern char *_Jv_ThisExecutable (void);
357 extern void _Jv_ThisExecutable (const char *);
358
359 /* Return a pointer to a symbol in executable or loaded library.  */
360 void *_Jv_FindSymbolInExecutable (const char *);
361
362 /* Initialize JNI.  */
363 extern void _Jv_JNI_Init (void);
364
365 /* Get or set the per-thread JNIEnv used by the invocation API.  */
366 _Jv_JNIEnv *_Jv_GetCurrentJNIEnv ();
367 void _Jv_SetCurrentJNIEnv (_Jv_JNIEnv *);
368
369 struct _Jv_JavaVM;
370 _Jv_JavaVM *_Jv_GetJavaVM (); 
371
372 // Some verification functions from defineclass.cc.
373 bool _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig);
374 bool _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig);
375 bool _Jv_VerifyClassName (unsigned char* ptr, _Jv_ushort length);
376 bool _Jv_VerifyClassName (_Jv_Utf8Const *name);
377 bool _Jv_VerifyIdentifier (_Jv_Utf8Const *);
378 bool _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2);
379
380 #ifdef ENABLE_JVMPI
381
382 #include "jvmpi.h"
383
384 extern void (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (JVMPI_Event *event);
385 extern void (*_Jv_JVMPI_Notify_THREAD_START) (JVMPI_Event *event);
386 extern void (*_Jv_JVMPI_Notify_THREAD_END) (JVMPI_Event *event);
387 #endif
388
389 #endif /* __JAVA_JVM_H__ */