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 64 1998-11-11 21:11:55Z phil $
17 *******************************************************************************/
22 #define OLD_COMPILER /* if enabled makes old compiler available */
23 #define NEW_GC /* if enabled, includes the new gc. -- phil. */
25 #define STATISTICS /* if enabled collects program statistics */
28 * JIT_MARKER_SUPPORT is the define used to toggle Just-in-time generated
29 * marker functions on and off.
31 * SIZE_FROM_CLASSINFO toggles between the bitmap_based and the new method
32 * of determining the sizes of objects on the heap.
34 #undef JIT_MARKER_SUPPORT /* phil */
35 #undef SIZE_FROM_CLASSINFO
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"
51 /* system dependent types *****************************************************/
53 #include "sysdep/types.h"
56 /* additional data types ******************************************************/
58 typedef void *voidptr; /* generic pointer */
60 typedef int bool; /* boolean data type */
65 typedef void (*functionptr) (); /* generic function pointer */
68 #define MAX_ALIGN 8 /* most generic alignment for JavaVM values */
71 /* shutdown function **********************************************************/
73 void cacao_shutdown(s4 status);
76 /* basic data types ***********************************************************/
78 #define TYPE_INT 0 /* the JavaVM types must numbered in the */
79 #define TYPE_LONG 1 /* same order as the ICMD_Ixxx to ICMD_Axxx */
80 #define TYPE_FLOAT 2 /* instructions (LOAD and STORE) */
81 #define TYPE_DOUBLE 3 /* integer, long, float, double, address */
82 #define TYPE_ADDRESS 4 /* all other types can be numbered arbitrarly */
87 /* Java class file constants **************************************************/
89 #define MAGIC 0xcafebabe
90 #define MINOR_VERSION 3
91 #define MAJOR_VERSION 45
93 #define CONSTANT_Class 7
94 #define CONSTANT_Fieldref 9
95 #define CONSTANT_Methodref 10
96 #define CONSTANT_InterfaceMethodref 11
97 #define CONSTANT_String 8
98 #define CONSTANT_Integer 3
99 #define CONSTANT_Float 4
100 #define CONSTANT_Long 5
101 #define CONSTANT_Double 6
102 #define CONSTANT_NameAndType 12
103 #define CONSTANT_Utf8 1
105 #define CONSTANT_Arraydescriptor 13
106 #define CONSTANT_UNUSED 0
108 #define ACC_PUBLIC 0x0001
109 #define ACC_PRIVATE 0x0002
110 #define ACC_PROTECTED 0x0004
111 #define ACC_STATIC 0x0008
112 #define ACC_FINAL 0x0010
113 #define ACC_SYNCHRONIZED 0x0020
114 #define ACC_VOLATILE 0x0040
115 #define ACC_TRANSIENT 0x0080
116 #define ACC_NATIVE 0x0100
117 #define ACC_INTERFACE 0x0200
118 #define ACC_ABSTRACT 0x0400
121 /* resolve typedef cycles *****************************************************/
123 typedef struct unicode unicode;
124 typedef struct java_objectheader java_objectheader;
125 typedef struct classinfo classinfo;
126 typedef struct vftbl vftbl;
127 typedef u1* methodptr;
130 /* constant pool entries *******************************************************
132 All constant pool entries need a data structure which contain the entrys
133 value. In some cases this structure exist already, in the remaining cases
134 this structure must be generated:
136 kind structure generated?
137 ----------------------------------------------------------------------
138 CONSTANT_Class classinfo no
139 CONSTANT_Fieldref constant_FMIref yes
140 CONSTANT_Methodref constant_FMIref yes
141 CONSTANT_InterfaceMethodref constant_FMIref yes
142 CONSTANT_String unicode no
143 CONSTANT_Integer constant_integer yes
144 CONSTANT_Float constant_float yes
145 CONSTANT_Long constant_long yes
146 CONSTANT_Double constant_double yes
147 CONSTANT_NameAndType constant_nameandtype yes
148 CONSTANT_Utf8 unicode no
149 CONSTANT_Arraydescriptor constant_arraydescriptor yes
152 *******************************************************************************/
154 /* data structures of Unicode symbol *******************************************
156 All Unicode symbols are stored in one global (hash) table, every symbol
157 exists only once. Equal symbols have identical pointers.
161 unicode *hashlink; /* link for external hash chain */
162 u4 key; /* hash key (computed from text) */
163 int length; /* text length */
164 u2 *text; /* pointer to text (each character is 16 Bit) */
165 classinfo *class; /* class pointer if it exists, otherwise NULL */
166 java_objectheader *string; /* string pointer if it exists, otherwise NULL*/
170 /* data structures of remaining constant pool entries *************************/
172 typedef struct { /* Fieldref, Methodref and InterfaceMethodref */
173 classinfo *class; /* class containing this field/method/interface */
174 unicode *name; /* field/method/interface name */
175 unicode *descriptor; /* field/method/interface type descriptor string */
178 typedef struct { /* Integer */
182 typedef struct { /* Float */
186 typedef struct { /* Long */
190 typedef struct { /* Double */
194 typedef struct { /* NameAndType (Field or Method) */
195 unicode *name; /* field/method name */
196 unicode *descriptor; /* field/method type descriptor string */
197 } constant_nameandtype;
199 /* arraydescriptor describes array types. Basic array types contain their
200 type in the arraytype field, objectclass contains a class pointer for
201 arrays of objects (arraytype == ARRAYTYPE_OBJECT), elementdescriptor
202 contains a pointer to an arraydescriptor which describes the element
203 types in the case of arrays of arrays (arraytype == ARRAYTYPE_ARRAY).
206 typedef struct constant_arraydescriptor {
208 classinfo *objectclass;
209 struct constant_arraydescriptor *elementdescriptor;
210 } constant_arraydescriptor;
213 /* data structures of the runtime system **************************************/
215 /* objects *********************************************************************
217 All objects (and arrays) which resides on the heap need the following
218 header at the beginning of the data structure.
221 struct java_objectheader { /* header for all objects */
222 vftbl *vftbl; /* pointer to virtual function table */
227 /* arrays **********************************************************************
229 All arrays are objects (they need the object header with a pointer to a
230 vvftbl (array class table). There is only one class for all arrays. The
231 type of an array is stored directly in the array object. Following types
235 #define ARRAYTYPE_INT 0
236 #define ARRAYTYPE_LONG 1
237 #define ARRAYTYPE_FLOAT 2
238 #define ARRAYTYPE_DOUBLE 3
239 #define ARRAYTYPE_BYTE 4
240 #define ARRAYTYPE_CHAR 5
241 #define ARRAYTYPE_SHORT 6
242 #define ARRAYTYPE_BOOLEAN 7
243 #define ARRAYTYPE_OBJECT 8
244 #define ARRAYTYPE_ARRAY 9
246 typedef struct java_arrayheader { /* header for all arrays */
247 java_objectheader objheader; /* object header */
248 s4 size; /* array size */
249 #ifdef SIZE_FROM_CLASSINFO
250 s4 alignedsize; /* phil */
252 s4 arraytype; /* array type from previous list */
257 /* structs for all kinds of arrays ********************************************/
259 typedef struct java_chararray {
260 java_arrayheader header;
264 typedef struct java_floatheader {
265 java_arrayheader header;
269 typedef struct java_doublearray {
270 java_arrayheader header;
274 /* booleanarray and bytearray need identical memory layout (access methods
275 use the same machine code */
277 typedef struct java_booleanarray {
278 java_arrayheader header;
282 typedef struct java_bytearray {
283 java_arrayheader header;
287 typedef struct java_shortarray {
288 java_arrayheader header;
292 typedef struct java_intarray {
293 java_arrayheader header;
297 typedef struct java_longarray {
298 java_arrayheader header;
302 /* objectarray and arrayarray need identical memory layout (access methods
303 use the same machine code */
305 typedef struct java_objectarray {
306 java_arrayheader header;
307 classinfo *elementtype;
308 java_objectheader *data[1];
311 typedef struct java_arrayarray {
312 java_arrayheader header;
313 constant_arraydescriptor *elementdescriptor;
314 java_arrayheader *data[1];
318 /* field, method and class structures *****************************************/
320 /* fieldinfo ******************************************************************/
322 typedef struct fieldinfo {/* field of a class */
323 s4 flags; /* ACC flags */
324 s4 type; /* basic data type */
325 unicode *name; /* name of field */
326 unicode *descriptor; /* JavaVM descriptor string of field */
328 s4 offset; /* offset from start of object (instance variables) */
330 union { /* storage for static values (class variables) */
341 /* exceptiontable *************************************************************/
343 typedef struct exceptiontable { /* exceptiontable entry in a method */
344 s4 startpc; /* start pc of guarded area (inclusive) */
345 s4 endpc; /* end pc of guarded area (exklusive) */
346 s4 handlerpc; /* pc of exception handler */
347 classinfo *catchtype; /* catchtype of exception (NULL == catchall) */
351 /* methodinfo *****************************************************************/
353 typedef struct methodinfo { /* method structure */
354 s4 flags; /* ACC flags */
355 unicode *name; /* name of method */
356 unicode *descriptor; /* JavaVM descriptor string of method */
357 s4 returntype; /* only temporary valid, return type */
358 s4 paramcount; /* only temporary valid, parameter count */
359 u1 *paramtypes; /* only temporary valid, parameter types */
360 classinfo *class; /* class, the method belongs to */
361 s4 vftblindex; /* index of method in virtual function table
362 (if it is a virtual method) */
363 s4 maxstack; /* maximum stack depth of method */
364 s4 maxlocals; /* maximum number of local variables */
365 s4 jcodelength; /* length of JavaVM code */
366 u1 *jcode; /* pointer to JavaVM code */
368 s4 exceptiontablelength;/* exceptiontable length */
369 exceptiontable *exceptiontable; /* the exceptiontable */
371 u1 *stubroutine; /* stub for compiling or calling natives */
372 s4 mcodelength; /* legth of generated machine code */
373 u1 *mcode; /* pointer to machine code */
374 u1 *entrypoint; /* entry point in machine code */
379 /* classinfo ******************************************************************/
381 struct classinfo { /* class structure */
382 java_objectheader header; /* classes are also objects */
384 s4 flags; /* ACC flags */
385 unicode *name; /* class name */
387 s4 cpcount; /* number of entries in constant pool */
388 u1 *cptags; /* constant pool tags */
389 voidptr *cpinfos; /* pointer to constant pool info structures */
391 classinfo *super; /* super class pointer */
392 classinfo *sub; /* sub class pointer */
393 classinfo *nextsub; /* pointer to next class in sub class list */
395 s4 interfacescount; /* number of interfaces */
396 classinfo **interfaces; /* pointer to interfaces */
398 s4 fieldscount; /* number of fields */
399 fieldinfo *fields; /* field table */
401 s4 methodscount; /* number of methods */
402 methodinfo *methods; /* method table */
404 listnode listnode; /* linkage */
406 bool initialized; /* true, if class already initialised */
407 bool linked; /* true, if class already linked */
408 s4 index; /* hierarchy depth (classes) or index
410 s4 instancesize; /* size of an instance of this class */
411 #ifdef SIZE_FROM_CLASSINFO
412 s4 alignedsize; /* size of an instance, aligned to the
413 allocation size on the heap */
416 vftbl *vftbl; /* pointer to virtual function table */
418 methodinfo *finalizer; /* finalizer method */
419 #ifdef JIT_MARKER_SUPPORT
425 /* virtual function table ******************************************************
427 The vtbl has a bidirectional layout with open ends at both sides.
428 interfacetablelength gives the number of entries of the interface table at
429 the start of the vftbl. The vftbl pointer points to &interfacetable[0].
430 vftbllength gives the number of entries of table at the end of the vftbl.
432 runtime type check (checkcast):
434 Different methods are used for runtime type check depending on the
435 argument of checkcast/instanceof.
437 A check against a class is implemented via relative numbering on the class
438 hierachy tree. The tree is numbered in a depth first traversal setting
439 the base field and the diff field. The diff field gets the result of
440 (high - base) so that a range check can be implemented by an unsigned
441 compare. A sub type test is done by checking the inclusion of base of
442 the sub class in the range of the superclass.
444 A check against an interface is implemented via the interfacevftbl. If the
445 interfacevftbl contains a nonnull value a class is a subclass of this
450 Like standard virtual methods interface methods are called using
451 virtual function tables. All interfaces are numbered sequentially
452 (starting with zero). For each class there exist an interface table
453 of virtual function tables for each implemented interface. The length
454 of the interface table is determined by the highest number of an
455 implemented interface.
457 The following example assumes a class which implements interface 0 and 3:
459 interfacetablelength = 4
462 +-----------+ | method 2 |---> method z
463 | class | | method 1 |---> method y
464 +-----------+ | method 0 |---> method x
465 | ivftbl 0 |----------> +----------+
466 vftblptr ---> +-----------+
467 | ivftbl -1 |--> NULL +----------+
468 | ivftbl -2 |--> NULL | method 1 |---> method x
469 | ivftbl -3 |-----+ | method 0 |---> method a
470 +-----------+ +----> +----------+
477 interfacevftbllength ---> +---------------+
479 *******************************************************************************/
482 methodptr *interfacetable[1]; /* interface table (access via macro) */
484 classinfo *class; /* class, the vtbl belongs to */
486 s4 vftbllength; /* virtual function table length */
487 s4 interfacetablelength; /* interface table length */
489 s4 baseval; /* base for runtime type check */
490 s4 diffval; /* high - base for runtime type check */
492 s4 *interfacevftbllength; /* length of interface vftbls */
494 methodptr table[1]; /* class vftbl */
497 #define VFTBLINTERFACETABLE(v,i) (v)->interfacetable[-i]
500 /* references to some system classes ******************************************/
502 extern classinfo *class_java_lang_Object;
503 extern classinfo *class_java_lang_String;
504 extern classinfo *class_java_lang_ClassCastException;
505 extern classinfo *class_java_lang_NullPointerException;
506 extern classinfo *class_java_lang_ArrayIndexOutOfBoundsException;
507 extern classinfo *class_java_lang_NegativeArraySizeException;
508 extern classinfo *class_java_lang_OutOfMemoryError;
509 extern classinfo *class_java_lang_ArithmeticException;
510 extern classinfo *class_java_lang_ArrayStoreException;
511 extern classinfo *class_java_lang_ThreadDeath;
513 extern classinfo *class_array;
516 /* instances of some system classes *******************************************/
518 extern java_objectheader *proto_java_lang_ClassCastException;
519 extern java_objectheader *proto_java_lang_NullPointerException;
520 extern java_objectheader *proto_java_lang_ArrayIndexOutOfBoundsException;
521 extern java_objectheader *proto_java_lang_NegativeArraySizeException;
522 extern java_objectheader *proto_java_lang_OutOfMemoryError;
523 extern java_objectheader *proto_java_lang_ArithmeticException;
524 extern java_objectheader *proto_java_lang_ArrayStoreException;
525 extern java_objectheader *proto_java_lang_ThreadDeath;
528 /* flag variables *************************************************************/
530 extern bool compileall;
531 extern bool runverbose;
535 /* statistic variables ********************************************************/
537 extern int count_class_infos;
538 extern int count_const_pool_len;
539 extern int count_vftbl_len;
540 extern int count_unicode_len;
541 extern int count_all_methods;
542 extern int count_vmcode_len;
543 extern int count_extable_len;
549 * These are local overrides for various environment variables in Emacs.
550 * Please do not remove this and leave it at the end of the file, where
551 * Emacs will automagically detect them.
552 * ---------------------------------------------------------------------
555 * indent-tabs-mode: t