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: 1998/10/30
17 *******************************************************************************/
22 #define OLD_COMPILER /* if enabled makes old compiler available */
24 #define STATISTICS /* if enabled collects program statistics */
26 /* JIT_MARKER_SUPPORT is the define used to toggle Just-in-time generated
27 marker functions on and off.
29 #undef JIT_MARKER_SUPPORT /* phil */
31 /* standard includes **********************************************************/
38 #include "toolbox/memory.h"
39 #include "toolbox/chain.h"
40 #include "toolbox/list.h"
41 #include "toolbox/loging.h"
44 /* system dependent types *****************************************************/
46 #include "sysdep/types.h"
49 /* additional data types ******************************************************/
51 typedef void *voidptr; /* generic pointer */
53 typedef int bool; /* boolean data type */
58 typedef void (*functionptr) (); /* generic function pointer */
61 #define MAX_ALIGN 8 /* most generic alignment for JavaVM values */
64 /* shutdown function **********************************************************/
66 void cacao_shutdown(s4 status);
69 /* basic data types ***********************************************************/
71 #define TYPE_INT 0 /* the JavaVM types must numbered in the */
72 #define TYPE_LONG 1 /* same order as the ICMD_Ixxx to ICMD_Axxx */
73 #define TYPE_FLOAT 2 /* instructions (LOAD and STORE) */
74 #define TYPE_DOUBLE 3 /* integer, long, float, double, address */
75 #define TYPE_ADDRESS 4 /* all other types can be numbered arbitrarly */
80 /* Java class file constants **************************************************/
82 #define MAGIC 0xcafebabe
83 #define MINOR_VERSION 3
84 #define MAJOR_VERSION 45
86 #define CONSTANT_Class 7
87 #define CONSTANT_Fieldref 9
88 #define CONSTANT_Methodref 10
89 #define CONSTANT_InterfaceMethodref 11
90 #define CONSTANT_String 8
91 #define CONSTANT_Integer 3
92 #define CONSTANT_Float 4
93 #define CONSTANT_Long 5
94 #define CONSTANT_Double 6
95 #define CONSTANT_NameAndType 12
96 #define CONSTANT_Utf8 1
98 #define CONSTANT_Arraydescriptor 13
99 #define CONSTANT_UNUSED 0
101 #define ACC_PUBLIC 0x0001
102 #define ACC_PRIVATE 0x0002
103 #define ACC_PROTECTED 0x0004
104 #define ACC_STATIC 0x0008
105 #define ACC_FINAL 0x0010
106 #define ACC_SYNCHRONIZED 0x0020
107 #define ACC_VOLATILE 0x0040
108 #define ACC_TRANSIENT 0x0080
109 #define ACC_NATIVE 0x0100
110 #define ACC_INTERFACE 0x0200
111 #define ACC_ABSTRACT 0x0400
114 /* resolve typedef cycles *****************************************************/
116 typedef struct unicode unicode;
117 typedef struct java_objectheader java_objectheader;
118 typedef struct classinfo classinfo;
119 typedef struct vftbl vftbl;
120 typedef u1* methodptr;
123 /* constant pool entries *******************************************************
125 All constant pool entries need a data structure which contain the entrys
126 value. In some cases this structure exist already, in the remaining cases
127 this structure must be generated:
129 kind structure generated?
130 ----------------------------------------------------------------------
131 CONSTANT_Class classinfo no
132 CONSTANT_Fieldref constant_FMIref yes
133 CONSTANT_Methodref constant_FMIref yes
134 CONSTANT_InterfaceMethodref constant_FMIref yes
135 CONSTANT_String unicode no
136 CONSTANT_Integer constant_integer yes
137 CONSTANT_Float constant_float yes
138 CONSTANT_Long constant_long yes
139 CONSTANT_Double constant_double yes
140 CONSTANT_NameAndType constant_nameandtype yes
141 CONSTANT_Utf8 unicode no
142 CONSTANT_Arraydescriptor constant_arraydescriptor yes
145 *******************************************************************************/
147 /* data structures of Unicode symbol *******************************************
149 All Unicode symbols are stored in one global (hash) table, every symbol
150 exists only once. Equal symbols have identical pointers.
154 unicode *hashlink; /* link for external hash chain */
155 u4 key; /* hash key (computed from text) */
156 int length; /* text length */
157 u2 *text; /* pointer to text (each character is 16 Bit) */
158 classinfo *class; /* class pointer if it exists, otherwise NULL */
159 java_objectheader *string; /* string pointer if it exists, otherwise NULL*/
163 /* data structures of remaining constant pool entries *************************/
165 typedef struct { /* Fieldref, Methodref and InterfaceMethodref */
166 classinfo *class; /* class containing this field/method/interface */
167 unicode *name; /* field/method/interface name */
168 unicode *descriptor; /* field/method/interface type descriptor string */
171 typedef struct { /* Integer */
175 typedef struct { /* Float */
179 typedef struct { /* Long */
183 typedef struct { /* Double */
187 typedef struct { /* NameAndType (Field or Method) */
188 unicode *name; /* field/method name */
189 unicode *descriptor; /* field/method type descriptor string */
190 } constant_nameandtype;
192 /* arraydescriptor describes array types. Basic array types contain their
193 type in the arraytype field, objectclass contains a class pointer for
194 arrays of objects (arraytype == ARRAYTYPE_OBJECT), elementdescriptor
195 contains a pointer to an arraydescriptor which describes the element
196 types in the case of arrays of arrays (arraytype == ARRAYTYPE_ARRAY).
199 typedef struct constant_arraydescriptor {
201 classinfo *objectclass;
202 struct constant_arraydescriptor *elementdescriptor;
203 } constant_arraydescriptor;
206 /* data structures of the runtime system **************************************/
208 /* objects *********************************************************************
210 All objects (and arrays) which resides on the heap need the following
211 header at the beginning of the data structure.
214 struct java_objectheader { /* header for all objects */
215 vftbl *vftbl; /* pointer to virtual function table */
220 /* arrays **********************************************************************
222 All arrays are objects (they need the object header with a pointer to a
223 vvftbl (array class table). There is only one class for all arrays. The
224 type of an array is stored directly in the array object. Following types
228 #define ARRAYTYPE_INT 0
229 #define ARRAYTYPE_LONG 1
230 #define ARRAYTYPE_FLOAT 2
231 #define ARRAYTYPE_DOUBLE 3
232 #define ARRAYTYPE_BYTE 4
233 #define ARRAYTYPE_CHAR 5
234 #define ARRAYTYPE_SHORT 6
235 #define ARRAYTYPE_BOOLEAN 7
236 #define ARRAYTYPE_OBJECT 8
237 #define ARRAYTYPE_ARRAY 9
239 typedef struct java_arrayheader { /* header for all arrays */
240 java_objectheader objheader; /* object header */
241 s4 size; /* array size */
242 s4 arraytype; /* array type from previous list */
247 /* structs for all kinds of arrays ********************************************/
249 typedef struct java_chararray {
250 java_arrayheader header;
254 typedef struct java_floatheader {
255 java_arrayheader header;
259 typedef struct java_doublearray {
260 java_arrayheader header;
264 /* booleanarray and bytearray need identical memory layout (access methods
265 use the same machine code */
267 typedef struct java_booleanarray {
268 java_arrayheader header;
272 typedef struct java_bytearray {
273 java_arrayheader header;
277 typedef struct java_shortarray {
278 java_arrayheader header;
282 typedef struct java_intarray {
283 java_arrayheader header;
287 typedef struct java_longarray {
288 java_arrayheader header;
292 /* objectarray and arrayarray need identical memory layout (access methods
293 use the same machine code */
295 typedef struct java_objectarray {
296 java_arrayheader header;
297 classinfo *elementtype;
298 java_objectheader *data[1];
301 typedef struct java_arrayarray {
302 java_arrayheader header;
303 constant_arraydescriptor *elementdescriptor;
304 java_arrayheader *data[1];
308 /* field, method and class structures *****************************************/
310 /* fieldinfo ******************************************************************/
312 typedef struct fieldinfo {/* field of a class */
313 s4 flags; /* ACC flags */
314 s4 type; /* basic data type */
315 unicode *name; /* name of field */
316 unicode *descriptor; /* JavaVM descriptor string of field */
318 s4 offset; /* offset from start of object (instance variables) */
320 union { /* storage for static values (class variables) */
331 /* exceptiontable *************************************************************/
333 typedef struct exceptiontable { /* exceptiontable entry in a method */
334 s4 startpc; /* start pc of guarded area (inclusive) */
335 s4 endpc; /* end pc of guarded area (exklusive) */
336 s4 handlerpc; /* pc of exception handler */
337 classinfo *catchtype; /* catchtype of exception (NULL == catchall) */
341 /* methodinfo *****************************************************************/
343 typedef struct methodinfo { /* method structure */
344 s4 flags; /* ACC flags */
345 unicode *name; /* name of method */
346 unicode *descriptor; /* JavaVM descriptor string of method */
347 s4 returntype; /* only temporary valid, return type */
348 s4 paramcount; /* only temporary valid, parameter count */
349 u1 *paramtypes; /* only temporary valid, parameter types */
350 classinfo *class; /* class, the method belongs to */
351 s4 vftblindex; /* index of method in virtual function table
352 (if it is a virtual method) */
353 s4 maxstack; /* maximum stack depth of method */
354 s4 maxlocals; /* maximum number of local variables */
355 s4 jcodelength; /* length of JavaVM code */
356 u1 *jcode; /* pointer to JavaVM code */
358 s4 exceptiontablelength;/* exceptiontable length */
359 exceptiontable *exceptiontable; /* the exceptiontable */
361 u1 *stubroutine; /* stub for compiling or calling natives */
362 s4 mcodelength; /* legth of generated machine code */
363 u1 *mcode; /* pointer to machine code */
364 u1 *entrypoint; /* entry point in machine code */
369 /* classinfo ******************************************************************/
371 struct classinfo { /* class structure */
372 java_objectheader header; /* classes are also objects */
374 s4 flags; /* ACC flags */
375 unicode *name; /* class name */
377 s4 cpcount; /* number of entries in constant pool */
378 u1 *cptags; /* constant pool tags */
379 voidptr *cpinfos; /* pointer to constant pool info structures */
381 classinfo *super; /* super class pointer */
382 classinfo *sub; /* sub class pointer */
383 classinfo *nextsub; /* pointer to next class in sub class list */
385 s4 interfacescount; /* number of interfaces */
386 classinfo **interfaces; /* pointer to interfaces */
388 s4 fieldscount; /* number of fields */
389 fieldinfo *fields; /* field table */
391 s4 methodscount; /* number of methods */
392 methodinfo *methods; /* method table */
394 listnode listnode; /* linkage */
396 bool initialized; /* true, if class already initialised */
397 bool linked; /* true, if class already linked */
398 s4 index; /* hierarchy depth (classes) or index
400 s4 instancesize; /* size of an instance of this class */
402 vftbl *vftbl; /* pointer to virtual function table */
404 methodinfo *finalizer; /* finalizer method */
405 #ifdef JIT_MARKER_SUPPORT
411 /* virtual function table ******************************************************
413 The vtbl has a bidirectional layout with open ends at both sides.
414 interfacetablelength gives the number of entries of the interface table at
415 the start of the vftbl. The vftbl pointer points to &interfacetable[0].
416 vftbllength gives the number of entries of table at the end of the vftbl.
418 runtime type check (checkcast):
420 Different methods are used for runtime type check depending on the
421 argument of checkcast/instanceof.
423 A check against a class is implemented via relative numbering on the class
424 hierachy tree. The tree is numbered in a depth first traversal setting
425 the base field and the diff field. The diff field gets the result of
426 (high - base) so that a range check can be implemented by an unsigned
427 compare. A sub type test is done by checking the inclusion of base of
428 the sub class in the range of the superclass.
430 A check against an interface is implemented via the interfacevftbl. If the
431 interfacevftbl contains a nonnull value a class is a subclass of this
436 Like standard virtual methods interface methods are called using
437 virtual function tables. All interfaces are numbered sequentially
438 (starting with zero). For each class there exist an interface table
439 of virtual function tables for each implemented interface. The length
440 of the interface table is determined by the highest number of an
441 implemented interface.
443 The following example assumes a class which implements interface 0 and 3:
445 interfacetablelength = 4
448 +-----------+ | method 2 |---> method z
449 | class | | method 1 |---> method y
450 +-----------+ | method 0 |---> method x
451 | ivftbl 0 |----------> +----------+
452 vftblptr ---> +-----------+
453 | ivftbl -1 |--> NULL +----------+
454 | ivftbl -2 |--> NULL | method 1 |---> method x
455 | ivftbl -3 |-----+ | method 0 |---> method a
456 +-----------+ +----> +----------+
463 interfacevftbllength ---> +---------------+
465 *******************************************************************************/
468 methodptr *interfacetable[1]; /* interface table (access via macro) */
470 classinfo *class; /* class, the vtbl belongs to */
472 s4 vftbllength; /* virtual function table length */
473 s4 interfacetablelength; /* interface table length */
475 s4 baseval; /* base for runtime type check */
476 s4 diffval; /* high - base for runtime type check */
478 s4 *interfacevftbllength; /* length of interface vftbls */
480 methodptr table[1]; /* class vftbl */
483 #define VFTBLINTERFACETABLE(v,i) (v)->interfacetable[-i]
486 /* references to some system classes ******************************************/
488 extern classinfo *class_java_lang_Object;
489 extern classinfo *class_java_lang_String;
490 extern classinfo *class_java_lang_ClassCastException;
491 extern classinfo *class_java_lang_NullPointerException;
492 extern classinfo *class_java_lang_ArrayIndexOutOfBoundsException;
493 extern classinfo *class_java_lang_NegativeArraySizeException;
494 extern classinfo *class_java_lang_OutOfMemoryError;
495 extern classinfo *class_java_lang_ArithmeticException;
496 extern classinfo *class_java_lang_ArrayStoreException;
497 extern classinfo *class_java_lang_ThreadDeath;
499 extern classinfo *class_array;
502 /* instances of some system classes *******************************************/
504 extern java_objectheader *proto_java_lang_ClassCastException;
505 extern java_objectheader *proto_java_lang_NullPointerException;
506 extern java_objectheader *proto_java_lang_ArrayIndexOutOfBoundsException;
507 extern java_objectheader *proto_java_lang_NegativeArraySizeException;
508 extern java_objectheader *proto_java_lang_OutOfMemoryError;
509 extern java_objectheader *proto_java_lang_ArithmeticException;
510 extern java_objectheader *proto_java_lang_ArrayStoreException;
511 extern java_objectheader *proto_java_lang_ThreadDeath;
514 /* flag variables *************************************************************/
516 extern bool compileall;
517 extern bool runverbose;
521 /* statistic variables ********************************************************/
523 extern int count_class_infos;
524 extern int count_const_pool_len;
525 extern int count_vftbl_len;
526 extern int count_unicode_len;
527 extern int count_all_methods;
528 extern int count_vmcode_len;
529 extern int count_extable_len;
535 * These are local overrides for various environment variables in Emacs.
536 * Please do not remove this and leave it at the end of the file, where
537 * Emacs will automagically detect them.
538 * ---------------------------------------------------------------------
541 * indent-tabs-mode: t