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 548 2003-11-01 19:46:25Z twisti $
17 *******************************************************************************/
26 #define STATISTICS /* if enabled collects program statistics */
29 * SIZE_FROM_CLASSINFO toggles between the bitmap_based and the new method
30 * of determining the sizes of objects on the heap.
32 #define SIZE_FROM_CLASSINFO
34 /* standard includes **********************************************************/
41 #include "toolbox/memory.h"
42 #include "toolbox/chain.h"
43 #include "toolbox/list.h"
44 #include "toolbox/loging.h"
46 /* system dependent types *****************************************************/
51 /* additional data types ******************************************************/
53 typedef void *voidptr; /* generic pointer */
55 typedef int bool; /* boolean data type */
60 #define PRIMITIVETYPE_COUNT 9 /* number of primitive types */
62 typedef void (*functionptr) (); /* generic function pointer */
65 #define MAX_ALIGN 8 /* most generic alignment for JavaVM values */
67 /* shutdown function **********************************************************/
69 void cacao_shutdown(s4 status);
72 /* basic data types ***********************************************************/
74 #define TYPE_INT 0 /* the JavaVM types must numbered in the */
75 #define TYPE_LONG 1 /* same order as the ICMD_Ixxx to ICMD_Axxx */
76 #define TYPE_FLOAT 2 /* instructions (LOAD and STORE) */
77 #define TYPE_DOUBLE 3 /* integer, long, float, double, address */
78 #define TYPE_ADDRESS 4 /* all other types can be numbered arbitrarly */
83 /* Java class file constants **************************************************/
85 #define MAGIC 0xcafebabe
86 #define MINOR_VERSION 3
87 #define MAJOR_VERSION 45
89 #define CONSTANT_Class 7
90 #define CONSTANT_Fieldref 9
91 #define CONSTANT_Methodref 10
92 #define CONSTANT_InterfaceMethodref 11
93 #define CONSTANT_String 8
94 #define CONSTANT_Integer 3
95 #define CONSTANT_Float 4
96 #define CONSTANT_Long 5
97 #define CONSTANT_Double 6
98 #define CONSTANT_NameAndType 12
99 #define CONSTANT_Utf8 1
101 #define CONSTANT_Arraydescriptor 13
102 #define CONSTANT_UNUSED 0
104 #define ACC_PUBLIC 0x0001
105 #define ACC_PRIVATE 0x0002
106 #define ACC_PROTECTED 0x0004
107 #define ACC_STATIC 0x0008
108 #define ACC_FINAL 0x0010
109 #define ACC_SYNCHRONIZED 0x0020
110 #define ACC_VOLATILE 0x0040
111 #define ACC_TRANSIENT 0x0080
112 #define ACC_NATIVE 0x0100
113 #define ACC_INTERFACE 0x0200
114 #define ACC_ABSTRACT 0x0400
117 /* resolve typedef cycles *****************************************************/
119 typedef struct utf utf;
120 typedef struct literalstring literalstring;
121 typedef struct java_objectheader java_objectheader;
122 typedef struct classinfo classinfo;
123 typedef struct vftbl vftbl;
124 typedef u1* methodptr;
125 typedef struct fieldinfo fieldinfo;
126 typedef struct methodinfo methodinfo;
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 for hashtables ********************************************
156 All utf-symbols, javastrings and classes are stored in global hashtables,
157 so every symbol exists only once. Equal symbols have identical pointers.
158 The functions for adding hashtable elements search the table for the
159 element with the specified name/text and return it on success. Otherwise a
160 new hashtable element is created.
162 The hashtables use external linking for handling collisions. The hashtable
163 structure contains a pointer <ptr> to the array of hashtable slots. The
164 number of hashtable slots and therefore the size of this array is specified
165 by the element <size> of hashtable structure. <entries> contains the number
166 of all hashtable elements stored in the table, including those in the
168 The hashtable element structures (utf, literalstring, classinfo) contain
169 both a pointer to the next hashtable element as a link for the external hash
170 chain and the key of the element. The key is computed from the text of
171 the string or the classname by using up to 8 characters.
173 If the number of entries in the hashtable exceeds twice the size of the
174 hashtableslot-array it is supposed that the average length of the
175 external chains has reached a value beyond 2. Therefore the functions for
176 adding hashtable elements (utf_new, class_new, literalstring_new) double
177 the hashtableslot-array. In this restructuring process all elements have
178 to be inserted into the new hashtable and new external chains must be built.
181 example for the layout of a hashtable:
183 hashtable.ptr--> +-------------------+
187 +-------------------+ +-------------------+ +-------------------+
188 | hashtable element |-->| hashtable element |-->| hashtable element |-->NULL
189 +-------------------+ +-------------------+ +-------------------+
190 | hashtable element |
191 +-------------------+ +-------------------+
192 | hashtable element |-->| hashtable element |-->NULL
193 +-------------------+ +-------------------+
194 | hashtable element |-->NULL
195 +-------------------+
199 +-------------------+
204 /* data structure for utf8 symbols ********************************************/
207 utf *hashlink; /* link for external hash chain */
208 int blength; /* text length in bytes */
209 char *text; /* pointer to text */
212 /* data structure of internal javastrings stored in global hashtable **********/
214 struct literalstring {
215 literalstring *hashlink; /* link for external hash chain */
216 java_objectheader *string;
219 /* data structure for accessing hashtables ************************************/
223 u4 entries; /* number of entries in the table */
224 void **ptr; /* pointer to hashtable */
227 /* data structures of remaining constant pool entries *************************/
229 typedef struct { /* Fieldref, Methodref and InterfaceMethodref */
230 classinfo *class; /* class containing this field/method/interface */
231 utf *name; /* field/method/interface name */
232 utf *descriptor; /* field/method/interface type descriptor string */
235 typedef struct { /* Integer */
239 typedef struct { /* Float */
243 typedef struct { /* Long */
247 typedef struct { /* Double */
251 typedef struct { /* NameAndType (Field or Method) */
252 utf *name; /* field/method name */
253 utf *descriptor; /* field/method type descriptor string */
254 } constant_nameandtype;
256 /* arraydescriptor describes array types. Basic array types contain their
257 type in the arraytype field, objectclass contains a class pointer for
258 arrays of objects (arraytype == ARRAYTYPE_OBJECT), elementdescriptor
259 contains a pointer to an arraydescriptor which describes the element
260 types in the case of arrays of arrays (arraytype == ARRAYTYPE_ARRAY).
263 typedef struct constant_arraydescriptor {
265 classinfo *objectclass;
266 struct constant_arraydescriptor *elementdescriptor;
267 } constant_arraydescriptor;
269 #include "jit/sets.h"
271 /* data structures of the runtime system **************************************/
273 /* objects *********************************************************************
275 All objects (and arrays) which resides on the heap need the following
276 header at the beginning of the data structure.
279 struct java_objectheader { /* header for all objects */
280 vftbl *vftbl; /* pointer to virtual function table */
285 /* arrays **********************************************************************
287 All arrays are objects (they need the object header with a pointer to a
288 vvftbl (array class table). There is only one class for all arrays. The
289 type of an array is stored directly in the array object. Following types
293 #define ARRAYTYPE_INT 0
294 #define ARRAYTYPE_LONG 1
295 #define ARRAYTYPE_FLOAT 2
296 #define ARRAYTYPE_DOUBLE 3
297 #define ARRAYTYPE_BYTE 4
298 #define ARRAYTYPE_CHAR 5
299 #define ARRAYTYPE_SHORT 6
300 #define ARRAYTYPE_BOOLEAN 7
301 #define ARRAYTYPE_OBJECT 8
302 #define ARRAYTYPE_ARRAY 9
304 typedef struct java_arrayheader { /* header for all arrays */
305 java_objectheader objheader; /* object header */
306 s4 size; /* array size */
307 #ifdef SIZE_FROM_CLASSINFO
308 s4 alignedsize; /* phil */
310 s4 arraytype; /* array type from previous list */
315 /* structs for all kinds of arrays ********************************************/
317 typedef struct java_chararray {
318 java_arrayheader header;
322 typedef struct java_floatheader {
323 java_arrayheader header;
327 typedef struct java_doublearray {
328 java_arrayheader header;
332 /* booleanarray and bytearray need identical memory layout (access methods
333 use the same machine code */
335 typedef struct java_booleanarray {
336 java_arrayheader header;
340 typedef struct java_bytearray {
341 java_arrayheader header;
345 typedef struct java_shortarray {
346 java_arrayheader header;
350 typedef struct java_intarray {
351 java_arrayheader header;
355 typedef struct java_longarray {
356 java_arrayheader header;
360 /* objectarray and arrayarray need identical memory layout (access methods
361 use the same machine code */
363 typedef struct java_objectarray {
364 java_arrayheader header;
365 classinfo *elementtype;
366 java_objectheader *data[1];
369 typedef struct java_arrayarray {
370 java_arrayheader header;
371 constant_arraydescriptor *elementdescriptor;
372 java_arrayheader *data[1];
376 /* structure for primitive classes ********************************************/
378 typedef struct primitivetypeinfo {
379 classinfo *class_wrap; /* class for wrapping primitive type */
380 classinfo *class_primitive; /* primitive class */
381 char *wrapname; /* name of class for wrapping */
382 char typesig; /* one character type signature */
383 char *name; /* name of primitive class */
386 /* field, method and class structures *****************************************/
388 typedef struct xtafldinfo {
390 classinfo *fldClassType;
391 classSet *XTAclassSet; /* field class type set */
393 /* fieldinfo ******************************************************************/
395 struct fieldinfo { /* field of a class */
396 s4 flags; /* ACC flags */
397 s4 type; /* basic data type */
398 utf *name; /* name of field */
399 utf *descriptor; /* JavaVM descriptor string of field */
401 s4 offset; /* offset from start of object (instance variables) */
403 union { /* storage for static values (class variables) */
417 /* exceptiontable *************************************************************/
419 typedef struct xtable { /* exceptiontable entry in a method */
420 s4 startpc; /* start pc of guarded area (inclusive) */
421 struct basicblock *start;
423 s4 endpc; /* end pc of guarded area (exklusive) */
424 struct basicblock *end;
426 s4 handlerpc; /* pc of exception handler */
427 struct basicblock *handler;
429 classinfo *catchtype; /* catchtype of exception (NULL == catchall) */
430 struct xtable *next; /* used to build a list of exception when */
431 /* loops are copied */
432 struct xtable *down; /* instead of the old array, a list is used */
436 typedef struct exceptiontable { /* exceptiontable entry in a method */
437 s4 startpc; /* start pc of guarded area (inclusive) */
438 s4 endpc; /* end pc of guarded area (exklusive) */
439 s4 handlerpc; /* pc of exception handler */
440 classinfo *catchtype; /* catchtype of exception (NULL == catchall) */
444 /* methodinfo static info ****************************************************/
445 typedef struct xtainfo {
446 s4 XTAmethodUsed; /* XTA if used in callgraph - not used /used */
447 classSet *XTAclassSet; /* method class type set */
448 /*classSet *PartClassSet */ /* method class type set */
450 classSetNode *paramClassSet; /* cone set of methods parameters */
452 methSet *calls; /* methods this method calls */
453 methSet *calledBy; /* methods that call this method */
454 methSet *marked; /* methods that marked by this method */
455 /*methSet *markedBy*/
456 fldSet *fldsUsed; /* fields used by this method */
457 /*methSetNode *interfaceCalls*/ /* methods this method calls as interface */
458 bool chgdSinceLastParse; /* Changed since last parse ? */
461 /* methodinfo *****************************************************************/
463 struct methodinfo { /* method structure */
464 s4 flags; /* ACC flags */
465 utf *name; /* name of method */
466 utf *descriptor; /* JavaVM descriptor string of method */
467 s4 returntype; /* only temporary valid, return type */
468 classinfo *returnclass; /* pointer to classinfo for the rtn type */ /*XTA*/
469 s4 paramcount; /* only temporary valid, parameter count */
470 u1 *paramtypes; /* only temporary valid, parameter types */
471 classinfo **paramclass; /* pointer to classinfo for a parameter */ /*XTA*/
473 classinfo *class; /* class, the method belongs to */
474 s4 vftblindex; /* index of method in virtual function table
475 (if it is a virtual method) */
476 s4 maxstack; /* maximum stack depth of method */
477 s4 maxlocals; /* maximum number of local variables */
478 s4 jcodelength; /* length of JavaVM code */
479 u1 *jcode; /* pointer to JavaVM code */
481 s4 exceptiontablelength;/* exceptiontable length */
482 exceptiontable *exceptiontable;
483 /* the exceptiontable */
485 u1 *stubroutine; /* stub for compiling or calling natives */
486 s4 mcodelength; /* legth of generated machine code */
487 u1 *mcode; /* pointer to machine code */
488 u1 *entrypoint; /* entry point in machine code */
493 s4 methodUsed; /* marked (might be used later) /not used /used */
494 s4 monoPoly; /* call is mono or poly or unknown */ /*RT stats */
495 /* should # method def'd and used be kept after static parse (will it be used?) */
502 /* innerclassinfo *************************************************************/
504 typedef struct innerclassinfo {
505 classinfo *inner_class; /* inner class pointer */
506 classinfo *outer_class; /* outer class pointer */
507 utf *name; /* innerclass name */
508 s4 flags; /* ACC flags */
511 /* classinfo ******************************************************************/
513 struct classinfo { /* class structure */
514 java_objectheader header; /* classes are also objects */
516 s4 flags; /* ACC flags */
517 utf *name; /* class name */
519 s4 cpcount; /* number of entries in constant pool */
520 u1 *cptags; /* constant pool tags */
521 voidptr *cpinfos; /* pointer to constant pool info structures */
523 classinfo *super; /* super class pointer */
524 classinfo *sub; /* sub class pointer */
525 classinfo *nextsub; /* pointer to next class in sub class list */
527 s4 interfacescount; /* number of interfaces */
528 classinfo **interfaces; /* pointer to interfaces */
530 s4 fieldscount; /* number of fields */
531 fieldinfo *fields; /* field table */
533 s4 methodscount; /* number of methods */
534 methodinfo *methods; /* method table */
536 listnode listnode; /* linkage */
538 bool initialized; /* true, if class already initialised */
539 bool linked; /* true, if class already linked */
540 s4 index; /* hierarchy depth (classes) or index
542 s4 instancesize; /* size of an instance of this class */
543 #ifdef SIZE_FROM_CLASSINFO
544 s4 alignedsize; /* size of an instance, aligned to the
545 allocation size on the heap */
548 vftbl *vftbl; /* pointer to virtual function table */
550 methodinfo *finalizer; /* finalizer method */
552 u2 innerclasscount; /* number of inner classes */
553 innerclassinfo *innerclass;
555 classinfo *hashlink; /* link for external hash chain */
556 bool classvftbl; /* has its own copy of the Class vtbl */
558 s4 classUsed; /* 0= not used 1 = used CO-RT */
560 classSetNode *impldBy; /* implemented by class set */
564 /* virtual function table ******************************************************
566 The vtbl has a bidirectional layout with open ends at both sides.
567 interfacetablelength gives the number of entries of the interface table at
568 the start of the vftbl. The vftbl pointer points to &interfacetable[0].
569 vftbllength gives the number of entries of table at the end of the vftbl.
571 runtime type check (checkcast):
573 Different methods are used for runtime type check depending on the
574 argument of checkcast/instanceof.
576 A check against a class is implemented via relative numbering on the class
577 hierachy tree. The tree is numbered in a depth first traversal setting
578 the base field and the diff field. The diff field gets the result of
579 (high - base) so that a range check can be implemented by an unsigned
580 compare. A sub type test is done by checking the inclusion of base of
581 the sub class in the range of the superclass.
583 A check against an interface is implemented via the interfacevftbl. If the
584 interfacevftbl contains a nonnull value a class is a subclass of this
589 Like standard virtual methods interface methods are called using
590 virtual function tables. All interfaces are numbered sequentially
591 (starting with zero). For each class there exist an interface table
592 of virtual function tables for each implemented interface. The length
593 of the interface table is determined by the highest number of an
594 implemented interface.
596 The following example assumes a class which implements interface 0 and 3:
598 interfacetablelength = 4
601 +-----------+ | method 2 |---> method z
602 | class | | method 1 |---> method y
603 +-----------+ | method 0 |---> method x
604 | ivftbl 0 |----------> +----------+
605 vftblptr ---> +-----------+
606 | ivftbl -1 |--> NULL +----------+
607 | ivftbl -2 |--> NULL | method 1 |---> method x
608 | ivftbl -3 |-----+ | method 0 |---> method a
609 +-----------+ +----> +----------+
616 interfacevftbllength ---> +---------------+
618 *******************************************************************************/
621 methodptr *interfacetable[1]; /* interface table (access via macro) */
623 classinfo *class; /* class, the vtbl belongs to */
625 s4 vftbllength; /* virtual function table length */
626 s4 interfacetablelength; /* interface table length */
628 s4 baseval; /* base for runtime type check */
629 s4 diffval; /* high - base for runtime type check */
631 s4 *interfacevftbllength; /* length of interface vftbls */
633 methodptr table[1]; /* class vftbl */
636 #define VFTBLINTERFACETABLE(v,i) (v)->interfacetable[-i]
639 /* references to some system classes ******************************************/
641 extern classinfo *class_java_lang_Object;
642 extern classinfo *class_java_lang_String;
643 extern classinfo *class_java_lang_ClassCastException;
644 extern classinfo *class_java_lang_NullPointerException;
645 extern classinfo *class_java_lang_ArrayIndexOutOfBoundsException;
646 extern classinfo *class_java_lang_NegativeArraySizeException;
647 extern classinfo *class_java_lang_OutOfMemoryError;
648 extern classinfo *class_java_lang_ArithmeticException;
649 extern classinfo *class_java_lang_ArrayStoreException;
650 extern classinfo *class_java_lang_ThreadDeath;
651 extern classinfo *class_array;
653 /* instances of some system classes *******************************************/
655 extern java_objectheader *proto_java_lang_ClassCastException;
656 extern java_objectheader *proto_java_lang_NullPointerException;
657 extern java_objectheader *proto_java_lang_ArrayIndexOutOfBoundsException;
658 extern java_objectheader *proto_java_lang_NegativeArraySizeException;
659 extern java_objectheader *proto_java_lang_OutOfMemoryError;
660 extern java_objectheader *proto_java_lang_ArithmeticException;
661 extern java_objectheader *proto_java_lang_ArrayStoreException;
662 extern java_objectheader *proto_java_lang_ThreadDeath;
665 /* flag variables *************************************************************/
667 extern bool compileall;
668 extern bool runverbose;
670 extern bool opt_rt; /* Rapid Type Analysis for better inlining CO-RT*/
671 extern bool opt_xta; /* X Type Analysis for better inlining CO-XTA*/
672 extern bool opt_vta; /* Variable Type Analysis for better inlining CO-VTA*/
674 extern int pClassHeir;
675 extern int pCallgraph;
679 extern void RT_jit_parse(methodinfo *m);
680 extern void printCallgraph ();
681 extern void printRThierarchyInfo(methodinfo *m);
682 extern void printObjectClassHeirarchy();
684 extern void XTA_jit_parse(methodinfo *m);
686 /* statistic variables ********************************************************/
688 extern int count_class_infos;
689 extern int count_const_pool_len;
690 extern int count_vftbl_len;
691 extern int count_utf_len;
692 extern int count_all_methods;
693 extern int count_vmcode_len;
694 extern int count_extable_len;
695 extern int count_class_loads;
696 extern int count_class_inits;
697 extern int count_utf_new;
698 extern int count_utf_new_found;
700 /* table of primitive types ***************************************************/
702 extern primitivetypeinfo primitivetype_table[PRIMITIVETYPE_COUNT];
708 * These are local overrides for various environment variables in Emacs.
709 * Please do not remove this and leave it at the end of the file, where
710 * Emacs will automagically detect them.
711 * ---------------------------------------------------------------------
714 * indent-tabs-mode: t