1 /* global.h - global definitions
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5 M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6 P. Tomsich, J. Wenninger
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Reinhard Grafl
33 $Id: global.h 557 2003-11-02 22:51:59Z twisti $
43 #include "toolbox/list.h"
48 #define STATISTICS /* if enabled collects program statistics */
51 * SIZE_FROM_CLASSINFO toggles between the bitmap_based and the new method
52 * of determining the sizes of objects on the heap.
54 #define SIZE_FROM_CLASSINFO
57 /* additional data types ******************************************************/
59 typedef void *voidptr; /* generic pointer */
61 typedef int bool; /* boolean data type */
66 #define PRIMITIVETYPE_COUNT 9 /* number of primitive types */
68 typedef void (*functionptr) (); /* generic function pointer */
71 #define MAX_ALIGN 8 /* most generic alignment for JavaVM values */
73 /* shutdown function **********************************************************/
75 void cacao_shutdown(s4 status);
78 /* basic data types ***********************************************************/
80 #define TYPE_INT 0 /* the JavaVM types must numbered in the */
81 #define TYPE_LONG 1 /* same order as the ICMD_Ixxx to ICMD_Axxx */
82 #define TYPE_FLOAT 2 /* instructions (LOAD and STORE) */
83 #define TYPE_DOUBLE 3 /* integer, long, float, double, address */
84 #define TYPE_ADDRESS 4 /* all other types can be numbered arbitrarly */
89 /* Java class file constants **************************************************/
91 #define MAGIC 0xcafebabe
92 #define MINOR_VERSION 3
93 #define MAJOR_VERSION 45
95 #define CONSTANT_Class 7
96 #define CONSTANT_Fieldref 9
97 #define CONSTANT_Methodref 10
98 #define CONSTANT_InterfaceMethodref 11
99 #define CONSTANT_String 8
100 #define CONSTANT_Integer 3
101 #define CONSTANT_Float 4
102 #define CONSTANT_Long 5
103 #define CONSTANT_Double 6
104 #define CONSTANT_NameAndType 12
105 #define CONSTANT_Utf8 1
107 #define CONSTANT_Arraydescriptor 13
108 #define CONSTANT_UNUSED 0
110 #define ACC_PUBLIC 0x0001
111 #define ACC_PRIVATE 0x0002
112 #define ACC_PROTECTED 0x0004
113 #define ACC_STATIC 0x0008
114 #define ACC_FINAL 0x0010
115 #define ACC_SYNCHRONIZED 0x0020
116 #define ACC_VOLATILE 0x0040
117 #define ACC_TRANSIENT 0x0080
118 #define ACC_NATIVE 0x0100
119 #define ACC_INTERFACE 0x0200
120 #define ACC_ABSTRACT 0x0400
123 /* resolve typedef cycles *****************************************************/
125 typedef struct utf utf;
126 typedef struct literalstring literalstring;
127 typedef struct java_objectheader java_objectheader;
128 typedef struct classinfo classinfo;
129 typedef struct vftbl vftbl;
130 typedef u1* methodptr;
131 typedef struct fieldinfo fieldinfo;
132 typedef struct methodinfo methodinfo;
135 /* constant pool entries *******************************************************
137 All constant pool entries need a data structure which contain the entrys
138 value. In some cases this structure exist already, in the remaining cases
139 this structure must be generated:
141 kind structure generated?
142 ----------------------------------------------------------------------
143 CONSTANT_Class classinfo no
144 CONSTANT_Fieldref constant_FMIref yes
145 CONSTANT_Methodref constant_FMIref yes
146 CONSTANT_InterfaceMethodref constant_FMIref yes
147 CONSTANT_String unicode no
148 CONSTANT_Integer constant_integer yes
149 CONSTANT_Float constant_float yes
150 CONSTANT_Long constant_long yes
151 CONSTANT_Double constant_double yes
152 CONSTANT_NameAndType constant_nameandtype yes
153 CONSTANT_Utf8 unicode no
154 CONSTANT_Arraydescriptor constant_arraydescriptor yes
157 *******************************************************************************/
159 /* data structures for hashtables ********************************************
162 All utf-symbols, javastrings and classes are stored in global hashtables,
163 so every symbol exists only once. Equal symbols have identical pointers.
164 The functions for adding hashtable elements search the table for the
165 element with the specified name/text and return it on success. Otherwise a
166 new hashtable element is created.
168 The hashtables use external linking for handling collisions. The hashtable
169 structure contains a pointer <ptr> to the array of hashtable slots. The
170 number of hashtable slots and therefore the size of this array is specified
171 by the element <size> of hashtable structure. <entries> contains the number
172 of all hashtable elements stored in the table, including those in the
174 The hashtable element structures (utf, literalstring, classinfo) contain
175 both a pointer to the next hashtable element as a link for the external hash
176 chain and the key of the element. The key is computed from the text of
177 the string or the classname by using up to 8 characters.
179 If the number of entries in the hashtable exceeds twice the size of the
180 hashtableslot-array it is supposed that the average length of the
181 external chains has reached a value beyond 2. Therefore the functions for
182 adding hashtable elements (utf_new, class_new, literalstring_new) double
183 the hashtableslot-array. In this restructuring process all elements have
184 to be inserted into the new hashtable and new external chains must be built.
187 example for the layout of a hashtable:
189 hashtable.ptr--> +-------------------+
193 +-------------------+ +-------------------+ +-------------------+
194 | hashtable element |-->| hashtable element |-->| hashtable element |-->NULL
195 +-------------------+ +-------------------+ +-------------------+
196 | hashtable element |
197 +-------------------+ +-------------------+
198 | hashtable element |-->| hashtable element |-->NULL
199 +-------------------+ +-------------------+
200 | hashtable element |-->NULL
201 +-------------------+
205 +-------------------+
210 /* data structure for utf8 symbols ********************************************/
213 utf *hashlink; /* link for external hash chain */
214 int blength; /* text length in bytes */
215 char *text; /* pointer to text */
219 /* data structure of internal javastrings stored in global hashtable **********/
221 struct literalstring {
222 literalstring *hashlink; /* link for external hash chain */
223 java_objectheader *string;
227 /* data structure for accessing hashtables ************************************/
231 u4 entries; /* number of entries in the table */
232 void **ptr; /* pointer to hashtable */
236 /* data structures of remaining constant pool entries *************************/
238 typedef struct { /* Fieldref, Methodref and InterfaceMethodref */
239 classinfo *class; /* class containing this field/method/interface */
240 utf *name; /* field/method/interface name */
241 utf *descriptor; /* field/method/interface type descriptor string */
245 typedef struct { /* Integer */
250 typedef struct { /* Float */
255 typedef struct { /* Long */
260 typedef struct { /* Double */
265 typedef struct { /* NameAndType (Field or Method) */
266 utf *name; /* field/method name */
267 utf *descriptor; /* field/method type descriptor string */
268 } constant_nameandtype;
272 arraydescriptor describes array types. Basic array types contain their
273 type in the arraytype field, objectclass contains a class pointer for
274 arrays of objects (arraytype == ARRAYTYPE_OBJECT), elementdescriptor
275 contains a pointer to an arraydescriptor which describes the element
276 types in the case of arrays of arrays (arraytype == ARRAYTYPE_ARRAY).
279 typedef struct constant_arraydescriptor {
281 classinfo *objectclass;
282 struct constant_arraydescriptor *elementdescriptor;
283 } constant_arraydescriptor;
286 /* data structures of the runtime system **************************************/
288 /* objects *********************************************************************
290 All objects (and arrays) which resides on the heap need the following
291 header at the beginning of the data structure.
294 struct java_objectheader { /* header for all objects */
295 vftbl *vftbl; /* pointer to virtual function table */
300 /* arrays **********************************************************************
302 All arrays are objects (they need the object header with a pointer to a
303 vvftbl (array class table). There is only one class for all arrays. The
304 type of an array is stored directly in the array object. Following types
308 #define ARRAYTYPE_INT 0
309 #define ARRAYTYPE_LONG 1
310 #define ARRAYTYPE_FLOAT 2
311 #define ARRAYTYPE_DOUBLE 3
312 #define ARRAYTYPE_BYTE 4
313 #define ARRAYTYPE_CHAR 5
314 #define ARRAYTYPE_SHORT 6
315 #define ARRAYTYPE_BOOLEAN 7
316 #define ARRAYTYPE_OBJECT 8
317 #define ARRAYTYPE_ARRAY 9
319 typedef struct java_arrayheader { /* header for all arrays */
320 java_objectheader objheader; /* object header */
321 s4 size; /* array size */
322 #ifdef SIZE_FROM_CLASSINFO
323 s4 alignedsize; /* phil */
325 s4 arraytype; /* array type from previous list */
330 /* structs for all kinds of arrays ********************************************/
332 typedef struct java_chararray {
333 java_arrayheader header;
337 typedef struct java_floatheader {
338 java_arrayheader header;
342 typedef struct java_doublearray {
343 java_arrayheader header;
347 /* booleanarray and bytearray need identical memory layout (access methods
348 use the same machine code */
350 typedef struct java_booleanarray {
351 java_arrayheader header;
355 typedef struct java_bytearray {
356 java_arrayheader header;
360 typedef struct java_shortarray {
361 java_arrayheader header;
365 typedef struct java_intarray {
366 java_arrayheader header;
370 typedef struct java_longarray {
371 java_arrayheader header;
375 /* objectarray and arrayarray need identical memory layout (access methods
376 use the same machine code */
378 typedef struct java_objectarray {
379 java_arrayheader header;
380 classinfo *elementtype;
381 java_objectheader *data[1];
384 typedef struct java_arrayarray {
385 java_arrayheader header;
386 constant_arraydescriptor *elementdescriptor;
387 java_arrayheader *data[1];
391 /* structure for primitive classes ********************************************/
393 typedef struct primitivetypeinfo {
394 classinfo *class_wrap; /* class for wrapping primitive type */
395 classinfo *class_primitive; /* primitive class */
396 char *wrapname; /* name of class for wrapping */
397 char typesig; /* one character type signature */
398 char *name; /* name of primitive class */
402 /* field, method and class structures *****************************************/
405 typedef struct xtafldinfo {
407 classinfo *fldClassType;
408 classSet *XTAclassSet; /* field class type set */
412 /* fieldinfo ******************************************************************/
414 struct fieldinfo { /* field of a class */
415 s4 flags; /* ACC flags */
416 s4 type; /* basic data type */
417 utf *name; /* name of field */
418 utf *descriptor; /* JavaVM descriptor string of field */
420 s4 offset; /* offset from start of object (instance variables) */
422 union { /* storage for static values (class variables) */
435 /* exceptiontable *************************************************************/
437 typedef struct xtable { /* exceptiontable entry in a method */
438 s4 startpc; /* start pc of guarded area (inclusive) */
439 struct basicblock *start;
441 s4 endpc; /* end pc of guarded area (exklusive) */
442 struct basicblock *end;
444 s4 handlerpc; /* pc of exception handler */
445 struct basicblock *handler;
447 classinfo *catchtype; /* catchtype of exception (NULL == catchall) */
448 struct xtable *next; /* used to build a list of exception when */
449 /* loops are copied */
450 struct xtable *down; /* instead of the old array, a list is used */
454 typedef struct exceptiontable { /* exceptiontable entry in a method */
455 s4 startpc; /* start pc of guarded area (inclusive) */
456 s4 endpc; /* end pc of guarded area (exklusive) */
457 s4 handlerpc; /* pc of exception handler */
458 classinfo *catchtype; /* catchtype of exception (NULL == catchall) */
462 /* methodinfo static info ****************************************************/
463 typedef struct xtainfo {
464 s4 XTAmethodUsed; /* XTA if used in callgraph - not used /used */
465 classSet *XTAclassSet; /* method class type set */
466 /*classSet *PartClassSet */ /* method class type set */
468 classSetNode *paramClassSet; /* cone set of methods parameters */
470 methSet *calls; /* methods this method calls */
471 methSet *calledBy; /* methods that call this method */
472 methSet *marked; /* methods that marked by this method */
473 /*methSet *markedBy*/
474 fldSet *fldsUsed; /* fields used by this method */
475 /*methSetNode *interfaceCalls*/ /* methods this method calls as interface */
476 bool chgdSinceLastParse; /* Changed since last parse ? */
479 /* methodinfo *****************************************************************/
481 struct methodinfo { /* method structure */
482 s4 flags; /* ACC flags */
483 utf *name; /* name of method */
484 utf *descriptor; /* JavaVM descriptor string of method */
485 s4 returntype; /* only temporary valid, return type */
486 classinfo *returnclass; /* pointer to classinfo for the rtn type */ /*XTA*/
487 s4 paramcount; /* only temporary valid, parameter count */
488 u1 *paramtypes; /* only temporary valid, parameter types */
489 classinfo **paramclass; /* pointer to classinfo for a parameter */ /*XTA*/
491 classinfo *class; /* class, the method belongs to */
492 s4 vftblindex; /* index of method in virtual function table
493 (if it is a virtual method) */
494 s4 maxstack; /* maximum stack depth of method */
495 s4 maxlocals; /* maximum number of local variables */
496 s4 jcodelength; /* length of JavaVM code */
497 u1 *jcode; /* pointer to JavaVM code */
499 s4 exceptiontablelength;/* exceptiontable length */
500 exceptiontable *exceptiontable;
501 /* the exceptiontable */
503 u1 *stubroutine; /* stub for compiling or calling natives */
504 s4 mcodelength; /* legth of generated machine code */
505 u1 *mcode; /* pointer to machine code */
506 u1 *entrypoint; /* entry point in machine code */
511 s4 methodUsed; /* marked (might be used later) /not used /used */
512 s4 monoPoly; /* call is mono or poly or unknown */ /*RT stats */
513 /* should # method def'd and used be kept after static parse (will it be used?) */
520 /* innerclassinfo *************************************************************/
522 typedef struct innerclassinfo {
523 classinfo *inner_class; /* inner class pointer */
524 classinfo *outer_class; /* outer class pointer */
525 utf *name; /* innerclass name */
526 s4 flags; /* ACC flags */
529 /* classinfo ******************************************************************/
531 struct classinfo { /* class structure */
532 java_objectheader header; /* classes are also objects */
534 s4 flags; /* ACC flags */
535 utf *name; /* class name */
537 s4 cpcount; /* number of entries in constant pool */
538 u1 *cptags; /* constant pool tags */
539 voidptr *cpinfos; /* pointer to constant pool info structures */
541 classinfo *super; /* super class pointer */
542 classinfo *sub; /* sub class pointer */
543 classinfo *nextsub; /* pointer to next class in sub class list */
545 s4 interfacescount; /* number of interfaces */
546 classinfo **interfaces; /* pointer to interfaces */
548 s4 fieldscount; /* number of fields */
549 fieldinfo *fields; /* field table */
551 s4 methodscount; /* number of methods */
552 methodinfo *methods; /* method table */
554 listnode listnode; /* linkage */
556 bool initialized; /* true, if class already initialised */
557 bool linked; /* true, if class already linked */
558 s4 index; /* hierarchy depth (classes) or index
560 s4 instancesize; /* size of an instance of this class */
561 #ifdef SIZE_FROM_CLASSINFO
562 s4 alignedsize; /* size of an instance, aligned to the
563 allocation size on the heap */
566 vftbl *vftbl; /* pointer to virtual function table */
568 methodinfo *finalizer; /* finalizer method */
570 u2 innerclasscount; /* number of inner classes */
571 innerclassinfo *innerclass;
573 classinfo *hashlink; /* link for external hash chain */
574 bool classvftbl; /* has its own copy of the Class vtbl */
576 s4 classUsed; /* 0= not used 1 = used CO-RT */
578 classSetNode *impldBy; /* implemented by class set */
582 /* virtual function table ******************************************************
584 The vtbl has a bidirectional layout with open ends at both sides.
585 interfacetablelength gives the number of entries of the interface table at
586 the start of the vftbl. The vftbl pointer points to &interfacetable[0].
587 vftbllength gives the number of entries of table at the end of the vftbl.
589 runtime type check (checkcast):
591 Different methods are used for runtime type check depending on the
592 argument of checkcast/instanceof.
594 A check against a class is implemented via relative numbering on the class
595 hierachy tree. The tree is numbered in a depth first traversal setting
596 the base field and the diff field. The diff field gets the result of
597 (high - base) so that a range check can be implemented by an unsigned
598 compare. A sub type test is done by checking the inclusion of base of
599 the sub class in the range of the superclass.
601 A check against an interface is implemented via the interfacevftbl. If the
602 interfacevftbl contains a nonnull value a class is a subclass of this
607 Like standard virtual methods interface methods are called using
608 virtual function tables. All interfaces are numbered sequentially
609 (starting with zero). For each class there exist an interface table
610 of virtual function tables for each implemented interface. The length
611 of the interface table is determined by the highest number of an
612 implemented interface.
614 The following example assumes a class which implements interface 0 and 3:
616 interfacetablelength = 4
619 +-----------+ | method 2 |---> method z
620 | class | | method 1 |---> method y
621 +-----------+ | method 0 |---> method x
622 | ivftbl 0 |----------> +----------+
623 vftblptr ---> +-----------+
624 | ivftbl -1 |--> NULL +----------+
625 | ivftbl -2 |--> NULL | method 1 |---> method x
626 | ivftbl -3 |-----+ | method 0 |---> method a
627 +-----------+ +----> +----------+
634 interfacevftbllength ---> +---------------+
636 *******************************************************************************/
639 methodptr *interfacetable[1]; /* interface table (access via macro) */
641 classinfo *class; /* class, the vtbl belongs to */
643 s4 vftbllength; /* virtual function table length */
644 s4 interfacetablelength; /* interface table length */
646 s4 baseval; /* base for runtime type check */
647 s4 diffval; /* high - base for runtime type check */
649 s4 *interfacevftbllength; /* length of interface vftbls */
651 methodptr table[1]; /* class vftbl */
654 #define VFTBLINTERFACETABLE(v,i) (v)->interfacetable[-i]
657 /* references to some system classes ******************************************/
659 extern classinfo *class_java_lang_Object;
660 extern classinfo *class_java_lang_String;
661 extern classinfo *class_java_lang_ClassCastException;
662 extern classinfo *class_java_lang_NullPointerException;
663 extern classinfo *class_java_lang_ArrayIndexOutOfBoundsException;
664 extern classinfo *class_java_lang_NegativeArraySizeException;
665 extern classinfo *class_java_lang_OutOfMemoryError;
666 extern classinfo *class_java_lang_ArithmeticException;
667 extern classinfo *class_java_lang_ArrayStoreException;
668 extern classinfo *class_java_lang_ThreadDeath;
669 extern classinfo *class_array;
671 /* instances of some system classes *******************************************/
673 extern java_objectheader *proto_java_lang_ClassCastException;
674 extern java_objectheader *proto_java_lang_NullPointerException;
675 extern java_objectheader *proto_java_lang_ArrayIndexOutOfBoundsException;
676 extern java_objectheader *proto_java_lang_NegativeArraySizeException;
677 extern java_objectheader *proto_java_lang_OutOfMemoryError;
678 extern java_objectheader *proto_java_lang_ArithmeticException;
679 extern java_objectheader *proto_java_lang_ArrayStoreException;
680 extern java_objectheader *proto_java_lang_ThreadDeath;
683 /* flag variables *************************************************************/
685 extern bool compileall;
686 extern bool runverbose;
688 extern bool opt_rt; /* Rapid Type Analysis for better inlining CO-RT*/
689 extern bool opt_xta; /* X Type Analysis for better inlining CO-XTA*/
690 extern bool opt_vta; /* Variable Type Analysis for better inlining CO-VTA*/
692 extern int pClassHeir;
693 extern int pCallgraph;
697 extern void RT_jit_parse(methodinfo *m);
698 extern void printCallgraph ();
699 extern void printRThierarchyInfo(methodinfo *m);
700 extern void printObjectClassHeirarchy();
702 extern void XTA_jit_parse(methodinfo *m);
704 /* statistic variables ********************************************************/
706 extern int count_class_infos;
707 extern int count_const_pool_len;
708 extern int count_vftbl_len;
709 extern int count_utf_len;
710 extern int count_all_methods;
711 extern int count_vmcode_len;
712 extern int count_extable_len;
713 extern int count_class_loads;
714 extern int count_class_inits;
715 extern int count_utf_new;
716 extern int count_utf_new_found;
718 /* table of primitive types ***************************************************/
720 extern primitivetypeinfo primitivetype_table[PRIMITIVETYPE_COUNT];
722 #endif /* _GLOBAL_H */
726 * These are local overrides for various environment variables in Emacs.
727 * Please do not remove this and leave it at the end of the file, where
728 * Emacs will automagically detect them.
729 * ---------------------------------------------------------------------
732 * indent-tabs-mode: t