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 139 1999-11-11 19:21:30Z 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 #define PRIMITIVETYPE_COUNT 9 /* number of primitive types */
66 typedef void (*functionptr) (); /* generic function pointer */
69 #define MAX_ALIGN 8 /* most generic alignment for JavaVM values */
72 /* shutdown function **********************************************************/
74 void cacao_shutdown(s4 status);
77 /* basic data types ***********************************************************/
79 #define TYPE_INT 0 /* the JavaVM types must numbered in the */
80 #define TYPE_LONG 1 /* same order as the ICMD_Ixxx to ICMD_Axxx */
81 #define TYPE_FLOAT 2 /* instructions (LOAD and STORE) */
82 #define TYPE_DOUBLE 3 /* integer, long, float, double, address */
83 #define TYPE_ADDRESS 4 /* all other types can be numbered arbitrarly */
88 /* Java class file constants **************************************************/
90 #define MAGIC 0xcafebabe
91 #define MINOR_VERSION 3
92 #define MAJOR_VERSION 45
94 #define CONSTANT_Class 7
95 #define CONSTANT_Fieldref 9
96 #define CONSTANT_Methodref 10
97 #define CONSTANT_InterfaceMethodref 11
98 #define CONSTANT_String 8
99 #define CONSTANT_Integer 3
100 #define CONSTANT_Float 4
101 #define CONSTANT_Long 5
102 #define CONSTANT_Double 6
103 #define CONSTANT_NameAndType 12
104 #define CONSTANT_Utf8 1
106 #define CONSTANT_Arraydescriptor 13
107 #define CONSTANT_UNUSED 0
109 #define ACC_PUBLIC 0x0001
110 #define ACC_PRIVATE 0x0002
111 #define ACC_PROTECTED 0x0004
112 #define ACC_STATIC 0x0008
113 #define ACC_FINAL 0x0010
114 #define ACC_SYNCHRONIZED 0x0020
115 #define ACC_VOLATILE 0x0040
116 #define ACC_TRANSIENT 0x0080
117 #define ACC_NATIVE 0x0100
118 #define ACC_INTERFACE 0x0200
119 #define ACC_ABSTRACT 0x0400
122 /* resolve typedef cycles *****************************************************/
124 typedef struct utf utf;
125 typedef struct literalstring literalstring;
126 typedef struct java_objectheader java_objectheader;
127 typedef struct classinfo classinfo;
128 typedef struct vftbl vftbl;
129 typedef u1* methodptr;
132 /* constant pool entries *******************************************************
134 All constant pool entries need a data structure which contain the entrys
135 value. In some cases this structure exist already, in the remaining cases
136 this structure must be generated:
138 kind structure generated?
139 ----------------------------------------------------------------------
140 CONSTANT_Class classinfo no
141 CONSTANT_Fieldref constant_FMIref yes
142 CONSTANT_Methodref constant_FMIref yes
143 CONSTANT_InterfaceMethodref constant_FMIref yes
144 CONSTANT_String unicode no
145 CONSTANT_Integer constant_integer yes
146 CONSTANT_Float constant_float yes
147 CONSTANT_Long constant_long yes
148 CONSTANT_Double constant_double yes
149 CONSTANT_NameAndType constant_nameandtype yes
150 CONSTANT_Utf8 unicode no
151 CONSTANT_Arraydescriptor constant_arraydescriptor yes
154 *******************************************************************************/
156 /* data structures for hashtables ********************************************
159 All utf-symbols, javastrings and classes are stored in global hashtables,
160 so every symbol exists only once. Equal symbols have identical pointers.
161 The functions for adding hashtable elements search the table for the
162 element with the specified name/text and return it on success. Otherwise a
163 new hashtable element is created.
165 The hashtables use external linking for handling collisions. The hashtable
166 structure contains a pointer <ptr> to the array of hashtable slots. The
167 number of hashtable slots and therefore the size of this array is specified
168 by the element <size> of hashtable structure. <entries> contains the number
169 of all hashtable elements stored in the table, including those in the
171 The hashtable element structures (utf, literalstring, classinfo) contain
172 both a pointer to the next hashtable element as a link for the external hash
173 chain and the key of the element. The key is computed from the text of
174 the string or the classname by using up to 8 characters.
176 If the number of entries in the hashtable exceeds twice the size of the
177 hashtableslot-array it is supposed that the average length of the
178 external chains has reached a value beyond 2. Therefore the functions for
179 adding hashtable elements (utf_new, class_new, literalstring_new) double
180 the hashtableslot-array. In this restructuring process all elements have
181 to be inserted into the new hashtable and new external chains must be built.
184 example for the layout of a hashtable:
186 hashtable.ptr--> +-------------------+
190 +-------------------+ +-------------------+ +-------------------+
191 | hashtable element |-->| hashtable element |-->| hashtable element |-->NULL
192 +-------------------+ +-------------------+ +-------------------+
193 | hashtable element |
194 +-------------------+ +-------------------+
195 | hashtable element |-->| hashtable element |-->NULL
196 +-------------------+ +-------------------+
197 | hashtable element |-->NULL
198 +-------------------+
202 +-------------------+
207 /* data structure for utf8 symbols ********************************************/
210 utf *hashlink; /* link for external hash chain */
211 int blength; /* text length in bytes */
212 char *text; /* pointer to text */
215 /* data structure of internal javastrings stored in global hashtable **********/
217 struct literalstring {
218 literalstring *hashlink; /* link for external hash chain */
219 java_objectheader *string;
222 /* data structure for accessing hashtables ************************************/
226 u4 entries; /* number of entries in the table */
227 void **ptr; /* pointer to hashtable */
230 /* data structures of remaining constant pool entries *************************/
232 typedef struct { /* Fieldref, Methodref and InterfaceMethodref */
233 classinfo *class; /* class containing this field/method/interface */
234 utf *name; /* field/method/interface name */
235 utf *descriptor; /* field/method/interface type descriptor string */
238 typedef struct { /* Integer */
242 typedef struct { /* Float */
246 typedef struct { /* Long */
250 typedef struct { /* Double */
254 typedef struct { /* NameAndType (Field or Method) */
255 utf *name; /* field/method name */
256 utf *descriptor; /* field/method type descriptor string */
257 } constant_nameandtype;
259 /* arraydescriptor describes array types. Basic array types contain their
260 type in the arraytype field, objectclass contains a class pointer for
261 arrays of objects (arraytype == ARRAYTYPE_OBJECT), elementdescriptor
262 contains a pointer to an arraydescriptor which describes the element
263 types in the case of arrays of arrays (arraytype == ARRAYTYPE_ARRAY).
266 typedef struct constant_arraydescriptor {
268 classinfo *objectclass;
269 struct constant_arraydescriptor *elementdescriptor;
270 } constant_arraydescriptor;
273 /* data structures of the runtime system **************************************/
275 /* objects *********************************************************************
277 All objects (and arrays) which resides on the heap need the following
278 header at the beginning of the data structure.
281 struct java_objectheader { /* header for all objects */
282 vftbl *vftbl; /* pointer to virtual function table */
287 /* arrays **********************************************************************
289 All arrays are objects (they need the object header with a pointer to a
290 vvftbl (array class table). There is only one class for all arrays. The
291 type of an array is stored directly in the array object. Following types
295 #define ARRAYTYPE_INT 0
296 #define ARRAYTYPE_LONG 1
297 #define ARRAYTYPE_FLOAT 2
298 #define ARRAYTYPE_DOUBLE 3
299 #define ARRAYTYPE_BYTE 4
300 #define ARRAYTYPE_CHAR 5
301 #define ARRAYTYPE_SHORT 6
302 #define ARRAYTYPE_BOOLEAN 7
303 #define ARRAYTYPE_OBJECT 8
304 #define ARRAYTYPE_ARRAY 9
306 typedef struct java_arrayheader { /* header for all arrays */
307 java_objectheader objheader; /* object header */
308 s4 size; /* array size */
309 #ifdef SIZE_FROM_CLASSINFO
310 s4 alignedsize; /* phil */
312 s4 arraytype; /* array type from previous list */
317 /* structs for all kinds of arrays ********************************************/
319 typedef struct java_chararray {
320 java_arrayheader header;
324 typedef struct java_floatheader {
325 java_arrayheader header;
329 typedef struct java_doublearray {
330 java_arrayheader header;
334 /* booleanarray and bytearray need identical memory layout (access methods
335 use the same machine code */
337 typedef struct java_booleanarray {
338 java_arrayheader header;
342 typedef struct java_bytearray {
343 java_arrayheader header;
347 typedef struct java_shortarray {
348 java_arrayheader header;
352 typedef struct java_intarray {
353 java_arrayheader header;
357 typedef struct java_longarray {
358 java_arrayheader header;
362 /* objectarray and arrayarray need identical memory layout (access methods
363 use the same machine code */
365 typedef struct java_objectarray {
366 java_arrayheader header;
367 classinfo *elementtype;
368 java_objectheader *data[1];
371 typedef struct java_arrayarray {
372 java_arrayheader header;
373 constant_arraydescriptor *elementdescriptor;
374 java_arrayheader *data[1];
378 /* structure for primitive classes ********************************************/
380 typedef struct primitivetypeinfo {
381 classinfo *class_wrap; /* class for wrapping primitive type */
382 classinfo *class_primitive; /* primitive class */
383 char *wrapname; /* name of class for wrapping */
384 char typesig; /* one character type signature */
385 char *name; /* name of primitive class */
388 /* field, method and class structures *****************************************/
390 /* fieldinfo ******************************************************************/
392 typedef struct fieldinfo {/* field of a class */
393 s4 flags; /* ACC flags */
394 s4 type; /* basic data type */
395 utf *name; /* name of field */
396 utf *descriptor; /* JavaVM descriptor string of field */
398 s4 offset; /* offset from start of object (instance variables) */
400 union { /* storage for static values (class variables) */
412 /* exceptiontable *************************************************************/
414 typedef struct xtable { /* exceptiontable entry in a method */
415 s4 startpc; /* start pc of guarded area (inclusive) */
416 struct basicblock *start;
418 s4 endpc; /* end pc of guarded area (exklusive) */
419 struct basicblock *end;
421 s4 handlerpc; /* pc of exception handler */
422 struct basicblock *handler;
424 classinfo *catchtype; /* catchtype of exception (NULL == catchall) */
425 struct xtable *next; /* used to build a list of exception when */
426 /* loops are copied */
427 struct xtable *down; /* instead of the old array, a list is used */
431 typedef struct exceptiontable { /* exceptiontable entry in a method */
432 s4 startpc; /* start pc of guarded area (inclusive) */
433 s4 endpc; /* end pc of guarded area (exklusive) */
434 s4 handlerpc; /* pc of exception handler */
435 classinfo *catchtype; /* catchtype of exception (NULL == catchall) */
439 /* methodinfo *****************************************************************/
441 typedef struct methodinfo { /* method structure */
442 s4 flags; /* ACC flags */
443 utf *name; /* name of method */
444 utf *descriptor; /* JavaVM descriptor string of method */
445 s4 returntype; /* only temporary valid, return type */
446 s4 paramcount; /* only temporary valid, parameter count */
447 u1 *paramtypes; /* only temporary valid, parameter types */
448 classinfo *class; /* class, the method belongs to */
449 s4 vftblindex; /* index of method in virtual function table
450 (if it is a virtual method) */
451 s4 maxstack; /* maximum stack depth of method */
452 s4 maxlocals; /* maximum number of local variables */
453 s4 jcodelength; /* length of JavaVM code */
454 u1 *jcode; /* pointer to JavaVM code */
456 s4 exceptiontablelength;/* exceptiontable length */
457 exceptiontable *exceptiontable;
458 /* the exceptiontable */
460 u1 *stubroutine; /* stub for compiling or calling natives */
461 s4 mcodelength; /* legth of generated machine code */
462 u1 *mcode; /* pointer to machine code */
463 u1 *entrypoint; /* entry point in machine code */
468 /* innerclassinfo *************************************************************/
470 typedef struct innerclassinfo {
471 classinfo *inner_class; /* inner class pointer */
472 classinfo *outer_class; /* outer class pointer */
473 utf *name; /* innerclass name */
474 s4 flags; /* ACC flags */
477 /* classinfo ******************************************************************/
479 struct classinfo { /* class structure */
480 java_objectheader header; /* classes are also objects */
482 s4 flags; /* ACC flags */
483 utf *name; /* class name */
485 s4 cpcount; /* number of entries in constant pool */
486 u1 *cptags; /* constant pool tags */
487 voidptr *cpinfos; /* pointer to constant pool info structures */
489 classinfo *super; /* super class pointer */
490 classinfo *sub; /* sub class pointer */
491 classinfo *nextsub; /* pointer to next class in sub class list */
493 s4 interfacescount; /* number of interfaces */
494 classinfo **interfaces; /* pointer to interfaces */
496 s4 fieldscount; /* number of fields */
497 fieldinfo *fields; /* field table */
499 s4 methodscount; /* number of methods */
500 methodinfo *methods; /* method table */
502 listnode listnode; /* linkage */
504 bool initialized; /* true, if class already initialised */
505 bool linked; /* true, if class already linked */
506 s4 index; /* hierarchy depth (classes) or index
508 s4 instancesize; /* size of an instance of this class */
509 #ifdef SIZE_FROM_CLASSINFO
510 s4 alignedsize; /* size of an instance, aligned to the
511 allocation size on the heap */
514 vftbl *vftbl; /* pointer to virtual function table */
516 methodinfo *finalizer; /* finalizer method */
517 #ifdef JIT_MARKER_SUPPORT
521 u2 innerclasscount; /* number of inner classes */
522 innerclassinfo *innerclass;
524 classinfo *hashlink; /* link for external hash chain */
528 /* virtual function table ******************************************************
530 The vtbl has a bidirectional layout with open ends at both sides.
531 interfacetablelength gives the number of entries of the interface table at
532 the start of the vftbl. The vftbl pointer points to &interfacetable[0].
533 vftbllength gives the number of entries of table at the end of the vftbl.
535 runtime type check (checkcast):
537 Different methods are used for runtime type check depending on the
538 argument of checkcast/instanceof.
540 A check against a class is implemented via relative numbering on the class
541 hierachy tree. The tree is numbered in a depth first traversal setting
542 the base field and the diff field. The diff field gets the result of
543 (high - base) so that a range check can be implemented by an unsigned
544 compare. A sub type test is done by checking the inclusion of base of
545 the sub class in the range of the superclass.
547 A check against an interface is implemented via the interfacevftbl. If the
548 interfacevftbl contains a nonnull value a class is a subclass of this
553 Like standard virtual methods interface methods are called using
554 virtual function tables. All interfaces are numbered sequentially
555 (starting with zero). For each class there exist an interface table
556 of virtual function tables for each implemented interface. The length
557 of the interface table is determined by the highest number of an
558 implemented interface.
560 The following example assumes a class which implements interface 0 and 3:
562 interfacetablelength = 4
565 +-----------+ | method 2 |---> method z
566 | class | | method 1 |---> method y
567 +-----------+ | method 0 |---> method x
568 | ivftbl 0 |----------> +----------+
569 vftblptr ---> +-----------+
570 | ivftbl -1 |--> NULL +----------+
571 | ivftbl -2 |--> NULL | method 1 |---> method x
572 | ivftbl -3 |-----+ | method 0 |---> method a
573 +-----------+ +----> +----------+
580 interfacevftbllength ---> +---------------+
582 *******************************************************************************/
585 methodptr *interfacetable[1]; /* interface table (access via macro) */
587 classinfo *class; /* class, the vtbl belongs to */
589 s4 vftbllength; /* virtual function table length */
590 s4 interfacetablelength; /* interface table length */
592 s4 baseval; /* base for runtime type check */
593 s4 diffval; /* high - base for runtime type check */
595 s4 *interfacevftbllength; /* length of interface vftbls */
597 methodptr table[1]; /* class vftbl */
600 #define VFTBLINTERFACETABLE(v,i) (v)->interfacetable[-i]
603 /* references to some system classes ******************************************/
605 extern classinfo *class_java_lang_Object;
606 extern classinfo *class_java_lang_String;
607 extern classinfo *class_java_lang_ClassCastException;
608 extern classinfo *class_java_lang_NullPointerException;
609 extern classinfo *class_java_lang_ArrayIndexOutOfBoundsException;
610 extern classinfo *class_java_lang_NegativeArraySizeException;
611 extern classinfo *class_java_lang_OutOfMemoryError;
612 extern classinfo *class_java_lang_ArithmeticException;
613 extern classinfo *class_java_lang_ArrayStoreException;
614 extern classinfo *class_java_lang_ThreadDeath;
615 extern classinfo *class_array;
617 /* instances of some system classes *******************************************/
619 extern java_objectheader *proto_java_lang_ClassCastException;
620 extern java_objectheader *proto_java_lang_NullPointerException;
621 extern java_objectheader *proto_java_lang_ArrayIndexOutOfBoundsException;
622 extern java_objectheader *proto_java_lang_NegativeArraySizeException;
623 extern java_objectheader *proto_java_lang_OutOfMemoryError;
624 extern java_objectheader *proto_java_lang_ArithmeticException;
625 extern java_objectheader *proto_java_lang_ArrayStoreException;
626 extern java_objectheader *proto_java_lang_ThreadDeath;
629 /* flag variables *************************************************************/
631 extern bool compileall;
632 extern bool runverbose;
636 /* statistic variables ********************************************************/
638 extern int count_class_infos;
639 extern int count_const_pool_len;
640 extern int count_vftbl_len;
641 extern int count_utf_len;
642 extern int count_all_methods;
643 extern int count_vmcode_len;
644 extern int count_extable_len;
645 extern int count_class_loads;
646 extern int count_class_inits;
647 extern int count_utf_new;
648 extern int count_utf_new_found;
650 /* table of primitive types ***************************************************/
652 extern primitivetypeinfo primitivetype_table[PRIMITIVETYPE_COUNT];
658 * These are local overrides for various environment variables in Emacs.
659 * Please do not remove this and leave it at the end of the file, where
660 * Emacs will automagically detect them.
661 * ---------------------------------------------------------------------
664 * indent-tabs-mode: t