1 /* global.h ********************************************************************
3 Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
5 See file COPYRIGHT for information on usage and disclaimer of warranties
7 Contains global definitions which are used in the whole program, includes
8 some files and contains global used macros.
10 Authors: Reinhard Grafl EMAIL: cacao@complang.tuwien.ac.at
11 Andreas Krall (andi) EMAIL: cacao@complang.tuwien.ac.at
12 Changes: Mark Probst (schani) EMAIL: cacao@complang.tuwien.ac.at
13 Philipp Tomsich (phil) EMAIL: cacao@complang.tuwien.ac.at
15 Last Change: $Id: global.h 118 1999-01-20 14:58:16Z andi $
17 *******************************************************************************/
24 #define NEW_GC /* if enabled, includes the new gc. -- phil. */
26 #define STATISTICS /* if enabled collects program statistics */
29 * JIT_MARKER_SUPPORT is the define used to toggle Just-in-time generated
30 * marker functions on and off.
32 * SIZE_FROM_CLASSINFO toggles between the bitmap_based and the new method
33 * of determining the sizes of objects on the heap.
35 #undef JIT_MARKER_SUPPORT /* phil */
36 #define SIZE_FROM_CLASSINFO
38 /* standard includes **********************************************************/
45 #include "toolbox/memory.h"
46 #include "toolbox/chain.h"
47 #include "toolbox/list.h"
48 #include "toolbox/loging.h"
50 /* system dependent types *****************************************************/
55 /* additional data types ******************************************************/
57 typedef void *voidptr; /* generic pointer */
59 typedef int bool; /* boolean data type */
64 typedef void (*functionptr) (); /* generic function pointer */
67 #define MAX_ALIGN 8 /* most generic alignment for JavaVM values */
70 /* shutdown function **********************************************************/
72 void cacao_shutdown(s4 status);
75 /* basic data types ***********************************************************/
77 #define TYPE_INT 0 /* the JavaVM types must numbered in the */
78 #define TYPE_LONG 1 /* same order as the ICMD_Ixxx to ICMD_Axxx */
79 #define TYPE_FLOAT 2 /* instructions (LOAD and STORE) */
80 #define TYPE_DOUBLE 3 /* integer, long, float, double, address */
81 #define TYPE_ADDRESS 4 /* all other types can be numbered arbitrarly */
86 /* Java class file constants **************************************************/
88 #define MAGIC 0xcafebabe
89 #define MINOR_VERSION 3
90 #define MAJOR_VERSION 45
92 #define CONSTANT_Class 7
93 #define CONSTANT_Fieldref 9
94 #define CONSTANT_Methodref 10
95 #define CONSTANT_InterfaceMethodref 11
96 #define CONSTANT_String 8
97 #define CONSTANT_Integer 3
98 #define CONSTANT_Float 4
99 #define CONSTANT_Long 5
100 #define CONSTANT_Double 6
101 #define CONSTANT_NameAndType 12
102 #define CONSTANT_Utf8 1
104 #define CONSTANT_Arraydescriptor 13
105 #define CONSTANT_UNUSED 0
107 #define ACC_PUBLIC 0x0001
108 #define ACC_PRIVATE 0x0002
109 #define ACC_PROTECTED 0x0004
110 #define ACC_STATIC 0x0008
111 #define ACC_FINAL 0x0010
112 #define ACC_SYNCHRONIZED 0x0020
113 #define ACC_VOLATILE 0x0040
114 #define ACC_TRANSIENT 0x0080
115 #define ACC_NATIVE 0x0100
116 #define ACC_INTERFACE 0x0200
117 #define ACC_ABSTRACT 0x0400
120 /* resolve typedef cycles *****************************************************/
122 typedef struct unicode unicode;
123 typedef struct java_objectheader java_objectheader;
124 typedef struct classinfo classinfo;
125 typedef struct vftbl vftbl;
126 typedef u1* methodptr;
129 /* constant pool entries *******************************************************
131 All constant pool entries need a data structure which contain the entrys
132 value. In some cases this structure exist already, in the remaining cases
133 this structure must be generated:
135 kind structure generated?
136 ----------------------------------------------------------------------
137 CONSTANT_Class classinfo no
138 CONSTANT_Fieldref constant_FMIref yes
139 CONSTANT_Methodref constant_FMIref yes
140 CONSTANT_InterfaceMethodref constant_FMIref yes
141 CONSTANT_String unicode no
142 CONSTANT_Integer constant_integer yes
143 CONSTANT_Float constant_float yes
144 CONSTANT_Long constant_long yes
145 CONSTANT_Double constant_double yes
146 CONSTANT_NameAndType constant_nameandtype yes
147 CONSTANT_Utf8 unicode no
148 CONSTANT_Arraydescriptor constant_arraydescriptor yes
151 *******************************************************************************/
153 /* data structures of Unicode symbol *******************************************
155 All Unicode symbols are stored in one global (hash) table, every symbol
156 exists only once. Equal symbols have identical pointers.
160 unicode *hashlink; /* link for external hash chain */
161 u4 key; /* hash key (computed from text) */
162 int length; /* text length */
163 u2 *text; /* pointer to text (each character is 16 Bit) */
164 classinfo *class; /* class pointer if it exists, otherwise NULL */
165 java_objectheader *string; /* string pointer if it exists, otherwise NULL*/
169 /* data structures of remaining constant pool entries *************************/
171 typedef struct { /* Fieldref, Methodref and InterfaceMethodref */
172 classinfo *class; /* class containing this field/method/interface */
173 unicode *name; /* field/method/interface name */
174 unicode *descriptor; /* field/method/interface type descriptor string */
177 typedef struct { /* Integer */
181 typedef struct { /* Float */
185 typedef struct { /* Long */
189 typedef struct { /* Double */
193 typedef struct { /* NameAndType (Field or Method) */
194 unicode *name; /* field/method name */
195 unicode *descriptor; /* field/method type descriptor string */
196 } constant_nameandtype;
198 /* arraydescriptor describes array types. Basic array types contain their
199 type in the arraytype field, objectclass contains a class pointer for
200 arrays of objects (arraytype == ARRAYTYPE_OBJECT), elementdescriptor
201 contains a pointer to an arraydescriptor which describes the element
202 types in the case of arrays of arrays (arraytype == ARRAYTYPE_ARRAY).
205 typedef struct constant_arraydescriptor {
207 classinfo *objectclass;
208 struct constant_arraydescriptor *elementdescriptor;
209 } constant_arraydescriptor;
212 /* data structures of the runtime system **************************************/
214 /* objects *********************************************************************
216 All objects (and arrays) which resides on the heap need the following
217 header at the beginning of the data structure.
220 struct java_objectheader { /* header for all objects */
221 vftbl *vftbl; /* pointer to virtual function table */
226 /* arrays **********************************************************************
228 All arrays are objects (they need the object header with a pointer to a
229 vvftbl (array class table). There is only one class for all arrays. The
230 type of an array is stored directly in the array object. Following types
234 #define ARRAYTYPE_INT 0
235 #define ARRAYTYPE_LONG 1
236 #define ARRAYTYPE_FLOAT 2
237 #define ARRAYTYPE_DOUBLE 3
238 #define ARRAYTYPE_BYTE 4
239 #define ARRAYTYPE_CHAR 5
240 #define ARRAYTYPE_SHORT 6
241 #define ARRAYTYPE_BOOLEAN 7
242 #define ARRAYTYPE_OBJECT 8
243 #define ARRAYTYPE_ARRAY 9
245 typedef struct java_arrayheader { /* header for all arrays */
246 java_objectheader objheader; /* object header */
247 s4 size; /* array size */
248 #ifdef SIZE_FROM_CLASSINFO
249 s4 alignedsize; /* phil */
251 s4 arraytype; /* array type from previous list */
256 /* structs for all kinds of arrays ********************************************/
258 typedef struct java_chararray {
259 java_arrayheader header;
263 typedef struct java_floatheader {
264 java_arrayheader header;
268 typedef struct java_doublearray {
269 java_arrayheader header;
273 /* booleanarray and bytearray need identical memory layout (access methods
274 use the same machine code */
276 typedef struct java_booleanarray {
277 java_arrayheader header;
281 typedef struct java_bytearray {
282 java_arrayheader header;
286 typedef struct java_shortarray {
287 java_arrayheader header;
291 typedef struct java_intarray {
292 java_arrayheader header;
296 typedef struct java_longarray {
297 java_arrayheader header;
301 /* objectarray and arrayarray need identical memory layout (access methods
302 use the same machine code */
304 typedef struct java_objectarray {
305 java_arrayheader header;
306 classinfo *elementtype;
307 java_objectheader *data[1];
310 typedef struct java_arrayarray {
311 java_arrayheader header;
312 constant_arraydescriptor *elementdescriptor;
313 java_arrayheader *data[1];
317 /* field, method and class structures *****************************************/
319 /* fieldinfo ******************************************************************/
321 typedef struct fieldinfo {/* field of a class */
322 s4 flags; /* ACC flags */
323 s4 type; /* basic data type */
324 unicode *name; /* name of field */
325 unicode *descriptor; /* JavaVM descriptor string of field */
327 s4 offset; /* offset from start of object (instance variables) */
329 union { /* storage for static values (class variables) */
340 /* exceptiontable *************************************************************/
342 typedef struct exceptiontable { /* exceptiontable entry in a method */
343 s4 startpc; /* start pc of guarded area (inclusive) */
344 s4 endpc; /* end pc of guarded area (exklusive) */
345 s4 handlerpc; /* pc of exception handler */
346 classinfo *catchtype; /* catchtype of exception (NULL == catchall) */
350 /* methodinfo *****************************************************************/
352 typedef struct methodinfo { /* method structure */
353 s4 flags; /* ACC flags */
354 unicode *name; /* name of method */
355 unicode *descriptor; /* JavaVM descriptor string of method */
356 s4 returntype; /* only temporary valid, return type */
357 s4 paramcount; /* only temporary valid, parameter count */
358 u1 *paramtypes; /* only temporary valid, parameter types */
359 classinfo *class; /* class, the method belongs to */
360 s4 vftblindex; /* index of method in virtual function table
361 (if it is a virtual method) */
362 s4 maxstack; /* maximum stack depth of method */
363 s4 maxlocals; /* maximum number of local variables */
364 s4 jcodelength; /* length of JavaVM code */
365 u1 *jcode; /* pointer to JavaVM code */
367 s4 exceptiontablelength;/* exceptiontable length */
368 exceptiontable *exceptiontable; /* the exceptiontable */
370 u1 *stubroutine; /* stub for compiling or calling natives */
371 s4 mcodelength; /* legth of generated machine code */
372 u1 *mcode; /* pointer to machine code */
373 u1 *entrypoint; /* entry point in machine code */
378 /* classinfo ******************************************************************/
380 struct classinfo { /* class structure */
381 java_objectheader header; /* classes are also objects */
383 s4 flags; /* ACC flags */
384 unicode *name; /* class name */
386 s4 cpcount; /* number of entries in constant pool */
387 u1 *cptags; /* constant pool tags */
388 voidptr *cpinfos; /* pointer to constant pool info structures */
390 classinfo *super; /* super class pointer */
391 classinfo *sub; /* sub class pointer */
392 classinfo *nextsub; /* pointer to next class in sub class list */
394 s4 interfacescount; /* number of interfaces */
395 classinfo **interfaces; /* pointer to interfaces */
397 s4 fieldscount; /* number of fields */
398 fieldinfo *fields; /* field table */
400 s4 methodscount; /* number of methods */
401 methodinfo *methods; /* method table */
403 listnode listnode; /* linkage */
405 bool initialized; /* true, if class already initialised */
406 bool linked; /* true, if class already linked */
407 s4 index; /* hierarchy depth (classes) or index
409 s4 instancesize; /* size of an instance of this class */
410 #ifdef SIZE_FROM_CLASSINFO
411 s4 alignedsize; /* size of an instance, aligned to the
412 allocation size on the heap */
415 vftbl *vftbl; /* pointer to virtual function table */
417 methodinfo *finalizer; /* finalizer method */
418 #ifdef JIT_MARKER_SUPPORT
424 /* virtual function table ******************************************************
426 The vtbl has a bidirectional layout with open ends at both sides.
427 interfacetablelength gives the number of entries of the interface table at
428 the start of the vftbl. The vftbl pointer points to &interfacetable[0].
429 vftbllength gives the number of entries of table at the end of the vftbl.
431 runtime type check (checkcast):
433 Different methods are used for runtime type check depending on the
434 argument of checkcast/instanceof.
436 A check against a class is implemented via relative numbering on the class
437 hierachy tree. The tree is numbered in a depth first traversal setting
438 the base field and the diff field. The diff field gets the result of
439 (high - base) so that a range check can be implemented by an unsigned
440 compare. A sub type test is done by checking the inclusion of base of
441 the sub class in the range of the superclass.
443 A check against an interface is implemented via the interfacevftbl. If the
444 interfacevftbl contains a nonnull value a class is a subclass of this
449 Like standard virtual methods interface methods are called using
450 virtual function tables. All interfaces are numbered sequentially
451 (starting with zero). For each class there exist an interface table
452 of virtual function tables for each implemented interface. The length
453 of the interface table is determined by the highest number of an
454 implemented interface.
456 The following example assumes a class which implements interface 0 and 3:
458 interfacetablelength = 4
461 +-----------+ | method 2 |---> method z
462 | class | | method 1 |---> method y
463 +-----------+ | method 0 |---> method x
464 | ivftbl 0 |----------> +----------+
465 vftblptr ---> +-----------+
466 | ivftbl -1 |--> NULL +----------+
467 | ivftbl -2 |--> NULL | method 1 |---> method x
468 | ivftbl -3 |-----+ | method 0 |---> method a
469 +-----------+ +----> +----------+
476 interfacevftbllength ---> +---------------+
478 *******************************************************************************/
481 methodptr *interfacetable[1]; /* interface table (access via macro) */
483 classinfo *class; /* class, the vtbl belongs to */
485 s4 vftbllength; /* virtual function table length */
486 s4 interfacetablelength; /* interface table length */
488 s4 baseval; /* base for runtime type check */
489 s4 diffval; /* high - base for runtime type check */
491 s4 *interfacevftbllength; /* length of interface vftbls */
493 methodptr table[1]; /* class vftbl */
496 #define VFTBLINTERFACETABLE(v,i) (v)->interfacetable[-i]
499 /* references to some system classes ******************************************/
501 extern classinfo *class_java_lang_Object;
502 extern classinfo *class_java_lang_String;
503 extern classinfo *class_java_lang_ClassCastException;
504 extern classinfo *class_java_lang_NullPointerException;
505 extern classinfo *class_java_lang_ArrayIndexOutOfBoundsException;
506 extern classinfo *class_java_lang_NegativeArraySizeException;
507 extern classinfo *class_java_lang_OutOfMemoryError;
508 extern classinfo *class_java_lang_ArithmeticException;
509 extern classinfo *class_java_lang_ArrayStoreException;
510 extern classinfo *class_java_lang_ThreadDeath;
512 extern classinfo *class_array;
515 /* instances of some system classes *******************************************/
517 extern java_objectheader *proto_java_lang_ClassCastException;
518 extern java_objectheader *proto_java_lang_NullPointerException;
519 extern java_objectheader *proto_java_lang_ArrayIndexOutOfBoundsException;
520 extern java_objectheader *proto_java_lang_NegativeArraySizeException;
521 extern java_objectheader *proto_java_lang_OutOfMemoryError;
522 extern java_objectheader *proto_java_lang_ArithmeticException;
523 extern java_objectheader *proto_java_lang_ArrayStoreException;
524 extern java_objectheader *proto_java_lang_ThreadDeath;
527 /* flag variables *************************************************************/
529 extern bool compileall;
530 extern bool runverbose;
534 /* statistic variables ********************************************************/
536 extern int count_class_infos;
537 extern int count_const_pool_len;
538 extern int count_vftbl_len;
539 extern int count_unicode_len;
540 extern int count_all_methods;
541 extern int count_vmcode_len;
542 extern int count_extable_len;
548 * These are local overrides for various environment variables in Emacs.
549 * Please do not remove this and leave it at the end of the file, where
550 * Emacs will automagically detect them.
551 * ---------------------------------------------------------------------
554 * indent-tabs-mode: t