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 *******************************************************************************/
20 #define __global_h_ /* schani */
22 #define STATISTICS /* andi */
24 /* JIT_MARKER_SUPPORT is the define used to toggle Just-in-time generated
25 marker functions on and off. */
26 #undef JIT_MARKER_SUPPORT /* phil */
28 /* standard includes **********************************************************/
35 #include "toolbox/memory.h"
36 #include "toolbox/chain.h"
37 #include "toolbox/list.h"
38 #include "toolbox/loging.h"
41 /* system dependent types *****************************************************/
43 #include "sysdep/types.h"
46 /* additional data types ******************************************************/
48 typedef void *voidptr; /* generic pointer */
50 typedef int bool; /* boolean data type */
55 typedef void (*functionptr) (); /* generic function pointer */
58 #define MAX_ALIGN 8 /* most generic alignment for JavaVM values */
61 /* shutdown function **********************************************************/
63 void cacao_shutdown(s4 status);
66 /* basic data types ***********************************************************/
68 #define TYPE_INT 0 /* the JavaVM types must numbered in the */
69 #define TYPE_LONG 1 /* same order as the ICMD_Ixxx to ICMD_Axxx */
70 #define TYPE_FLOAT 2 /* instructions (LOAD and STORE) */
71 #define TYPE_DOUBLE 3 /* integer, long, float, double, address */
72 #define TYPE_ADDRESS 4 /* all other types can be numbered arbitrarly */
77 /* Java class file constants **************************************************/
79 #define MAGIC 0xcafebabe
80 #define MINOR_VERSION 3
81 #define MAJOR_VERSION 45
83 #define CONSTANT_Class 7
84 #define CONSTANT_Fieldref 9
85 #define CONSTANT_Methodref 10
86 #define CONSTANT_InterfaceMethodref 11
87 #define CONSTANT_String 8
88 #define CONSTANT_Integer 3
89 #define CONSTANT_Float 4
90 #define CONSTANT_Long 5
91 #define CONSTANT_Double 6
92 #define CONSTANT_NameAndType 12
93 #define CONSTANT_Utf8 1
95 #define CONSTANT_Arraydescriptor 13
96 #define CONSTANT_UNUSED 0
98 #define ACC_PUBLIC 0x0001
99 #define ACC_PRIVATE 0x0002
100 #define ACC_PROTECTED 0x0004
101 #define ACC_STATIC 0x0008
102 #define ACC_FINAL 0x0010
103 #define ACC_SYNCHRONIZED 0x0020
104 #define ACC_VOLATILE 0x0040
105 #define ACC_TRANSIENT 0x0080
106 #define ACC_NATIVE 0x0100
107 #define ACC_INTERFACE 0x0200
108 #define ACC_ABSTRACT 0x0400
111 /* resolve typedef cycles *****************************************************/
113 typedef struct unicode unicode;
114 typedef struct java_objectheader java_objectheader;
115 typedef struct classinfo classinfo;
116 typedef struct vftbl vftbl;
117 typedef u1* methodptr;
120 /* constant pool entries *******************************************************
122 All constant pool entries need a data structure which contain the entrys
123 value. In some cases this structure exist already, in the remaining cases
124 this structure must be generated:
126 kind structure generated?
127 ----------------------------------------------------------------------
128 CONSTANT_Class classinfo no
129 CONSTANT_Fieldref constant_FMIref yes
130 CONSTANT_Methodref constant_FMIref yes
131 CONSTANT_InterfaceMethodref constant_FMIref yes
132 CONSTANT_String unicode no
133 CONSTANT_Integer constant_integer yes
134 CONSTANT_Float constant_float yes
135 CONSTANT_Long constant_long yes
136 CONSTANT_Double constant_double yes
137 CONSTANT_NameAndType constant_nameandtype yes
138 CONSTANT_Utf8 unicode no
139 CONSTANT_Arraydescriptor constant_arraydescriptor yes
142 *******************************************************************************/
144 /* data structures of Unicode symbol *******************************************
146 All Unicode symbols are stored in one global (hash) table, every symbol
147 exists only once. Equal symbols have identical pointers.
151 unicode *hashlink; /* link for external hash chain */
152 u4 key; /* hash key (computed from text) */
153 int length; /* text length */
154 u2 *text; /* pointer to text (each character is 16 Bit) */
155 classinfo *class; /* class pointer if it exists, otherwise NULL */
156 java_objectheader *string; /* string pointer if it exists, otherwise NULL*/
160 /* data structures of remaining constant pool entries *************************/
162 typedef struct { /* Fieldref, Methodref and InterfaceMethodref */
163 classinfo *class; /* class containing this field/method/interface */
164 unicode *name; /* field/method/interface name */
165 unicode *descriptor; /* field/method/interface type descriptor string */
168 typedef struct { /* Integer */
172 typedef struct { /* Float */
176 typedef struct { /* Long */
180 typedef struct { /* Double */
184 typedef struct { /* NameAndType (Field or Method) */
185 unicode *name; /* field/method name */
186 unicode *descriptor; /* field/method type descriptor string */
187 } constant_nameandtype;
189 /* arraydescriptor describes array types. Basic array types contain their
190 type in the arraytype field, objectclass contains a class pointer for
191 arrays of objects (arraytype == ARRAYTYPE_OBJECT), elementdescriptor
192 contains a pointer to an arraydescriptor which describes the element
193 types in the case of arrays of arrays (arraytype == ARRAYTYPE_ARRAY).
196 typedef struct constant_arraydescriptor {
198 classinfo *objectclass;
199 struct constant_arraydescriptor *elementdescriptor;
200 } constant_arraydescriptor;
203 /* data structures of the runtime system **************************************/
205 /* objects *********************************************************************
207 All objects (and arrays) which resides on the heap need the following
208 header at the beginning of the data structure.
211 struct java_objectheader { /* header for all objects */
212 vftbl *vftbl; /* pointer to virtual function table */
217 /* arrays **********************************************************************
219 All arrays are objects (they need the object header with a pointer to a
220 vvftbl (array class table). There is only one class for all arrays. The
221 type of an array is stored directly in the array object. Following types
225 #define ARRAYTYPE_INT 0
226 #define ARRAYTYPE_LONG 1
227 #define ARRAYTYPE_FLOAT 2
228 #define ARRAYTYPE_DOUBLE 3
229 #define ARRAYTYPE_BYTE 4
230 #define ARRAYTYPE_CHAR 5
231 #define ARRAYTYPE_SHORT 6
232 #define ARRAYTYPE_BOOLEAN 7
233 #define ARRAYTYPE_OBJECT 8
234 #define ARRAYTYPE_ARRAY 9
236 typedef struct java_arrayheader { /* header for all arrays */
237 java_objectheader objheader; /* object header */
238 s4 size; /* array size */
239 s4 arraytype; /* array type from previous list */
244 /* structs for all kinds of arrays ********************************************/
246 typedef struct java_chararray {
247 java_arrayheader header;
251 typedef struct java_floatheader {
252 java_arrayheader header;
256 typedef struct java_doublearray {
257 java_arrayheader header;
261 /* booleanarray and bytearray need identical memory layout (access methods
262 use the same machine code */
264 typedef struct java_booleanarray {
265 java_arrayheader header;
269 typedef struct java_bytearray {
270 java_arrayheader header;
274 typedef struct java_shortarray {
275 java_arrayheader header;
279 typedef struct java_intarray {
280 java_arrayheader header;
284 typedef struct java_longarray {
285 java_arrayheader header;
289 /* objectarray and arrayarray need identical memory layout (access methods
290 use the same machine code */
292 typedef struct java_objectarray {
293 java_arrayheader header;
294 classinfo *elementtype;
295 java_objectheader *data[1];
298 typedef struct java_arrayarray {
299 java_arrayheader header;
300 constant_arraydescriptor *elementdescriptor;
301 java_arrayheader *data[1];
305 /* field, method and class structures *****************************************/
307 /* fieldinfo ******************************************************************/
309 typedef struct fieldinfo {/* field of a class */
310 s4 flags; /* ACC flags */
311 s4 type; /* basic data type */
312 unicode *name; /* name of field */
313 unicode *descriptor; /* JavaVM descriptor string of field */
315 s4 offset; /* offset from start of object (instance variables) */
317 union { /* storage for static values (class variables) */
328 /* exceptiontable *************************************************************/
330 typedef struct exceptiontable { /* exceptiontable entry in a method */
331 s4 startpc; /* start pc of guarded area (inclusive) */
332 s4 endpc; /* end pc of guarded area (exklusive) */
333 s4 handlerpc; /* pc of exception handler */
334 classinfo *catchtype; /* catchtype of exception (NULL == catchall) */
338 /* methodinfo *****************************************************************/
340 typedef struct methodinfo { /* method structure */
341 s4 flags; /* ACC flags */
342 unicode *name; /* name of method */
343 unicode *descriptor; /* JavaVM descriptor string of method */
344 s4 returntype; /* only temporary valid, return type */
345 s4 paramcount; /* only temporary valid, parameter count */
346 u1 *paramtypes; /* only temporary valid, parameter types */
347 classinfo *class; /* class, the method belongs to */
348 s4 vftblindex; /* index of method in virtual function table
349 (if it is a virtual method) */
350 s4 maxstack; /* maximum stack depth of method */
351 s4 maxlocals; /* maximum number of local variables */
352 s4 jcodelength; /* length of JavaVM code */
353 u1 *jcode; /* pointer to JavaVM code */
355 s4 exceptiontablelength;/* exceptiontable length */
356 exceptiontable *exceptiontable; /* the exceptiontable */
358 u1 *stubroutine; /* stub for compiling or calling natives */
359 s4 mcodelength; /* legth of generated machine code */
360 u1 *mcode; /* pointer to machine code */
361 u1 *entrypoint; /* entry point in machine code */
366 /* classinfo ******************************************************************/
368 struct classinfo { /* class structure */
369 java_objectheader header; /* classes are also objects */
371 s4 flags; /* ACC flags */
372 unicode *name; /* class name */
374 s4 cpcount; /* number of entries in constant pool */
375 u1 *cptags; /* constant pool tags */
376 voidptr *cpinfos; /* pointer to constant pool info structures */
378 classinfo *super; /* super class pointer */
379 classinfo *sub; /* sub class pointer */
380 classinfo *nextsub; /* pointer to next class in sub class list */
382 s4 interfacescount; /* number of interfaces */
383 classinfo **interfaces; /* pointer to interfaces */
385 s4 fieldscount; /* number of fields */
386 fieldinfo *fields; /* field table */
388 s4 methodscount; /* number of methods */
389 methodinfo *methods; /* method table */
391 listnode listnode; /* linkage */
393 bool initialized; /* true, if class already initialised */
394 bool linked; /* true, if class already linked */
395 s4 index; /* hierarchy depth (classes) or index
397 s4 instancesize; /* size of an instance of this class */
399 vftbl *vftbl; /* pointer to virtual function table */
401 methodinfo *finalizer; /* finalizer method */
402 #ifdef JIT_MARKER_SUPPORT
408 /* virtual function table ******************************************************
410 The vtbl has a bidirectional layout with open ends at both sides.
411 interfacetablelength gives the number of entries of the interface table at
412 the start of the vftbl. The vftbl pointer points to &interfacetable[0].
413 vftbllength gives the number of entries of table at the end of the vftbl.
415 runtime type check (checkcast):
417 Different methods are used for runtime type check depending on the
418 argument of checkcast/instanceof.
420 A check against a class is implemented via relative numbering on the class
421 hierachy tree. The tree is numbered in a depth first traversal setting
422 the base field and the diff field. The diff field gets the result of
423 (high - base) so that a range check can be implemented by an unsigned
424 compare. A sub type test is done by checking the inclusion of base of
425 the sub class in the range of the superclass.
427 A check against an interface is implemented via the interfacevftbl. If the
428 interfacevftbl contains a nonnull value a class is a subclass of this
433 Like standard virtual methods interface methods are called using
434 virtual function tables. All interfaces are numbered sequentially
435 (starting with zero). For each class there exist an interface table
436 of virtual function tables for each implemented interface. The length
437 of the interface table is determined by the highest number of an
438 implemented interface.
440 The following example assumes a class which implements interface 0 and 3:
442 interfacetablelength = 4
445 +-----------+ | method 2 |---> method z
446 | class | | method 1 |---> method y
447 +-----------+ | method 0 |---> method x
448 | ivftbl 0 |----------> +----------+
449 vftblptr ---> +-----------+
450 | ivftbl -1 |--> NULL +----------+
451 | ivftbl -2 |--> NULL | method 1 |---> method x
452 | ivftbl -3 |-----+ | method 0 |---> method a
453 +-----------+ +----> +----------+
460 interfacevftbllength ---> +---------------+
462 *******************************************************************************/
465 methodptr *interfacetable[1]; /* interface table (access via macro) */
467 classinfo *class; /* class, the vtbl belongs to */
469 s4 vftbllength; /* virtual function table length */
470 s4 interfacetablelength; /* interface table length */
472 s4 baseval; /* base for runtime type check */
473 s4 diffval; /* high - base for runtime type check */
475 s4 *interfacevftbllength; /* length of interface vftbls */
477 methodptr table[1]; /* class vftbl */
480 #define VFTBLINTERFACETABLE(v,i) (v)->interfacetable[-i]
483 /* references to some system classes ******************************************/
485 extern classinfo *class_java_lang_Object;
486 extern classinfo *class_java_lang_String;
487 extern classinfo *class_java_lang_ClassCastException;
488 extern classinfo *class_java_lang_NullPointerException;
489 extern classinfo *class_java_lang_ArrayIndexOutOfBoundsException;
490 extern classinfo *class_java_lang_NegativeArraySizeException;
491 extern classinfo *class_java_lang_OutOfMemoryError;
492 extern classinfo *class_java_lang_ArithmeticException;
493 extern classinfo *class_java_lang_ArrayStoreException;
494 extern classinfo *class_java_lang_ThreadDeath;
496 extern classinfo *class_array;
499 /* instances of some system classes *******************************************/
501 extern java_objectheader *proto_java_lang_ClassCastException;
502 extern java_objectheader *proto_java_lang_NullPointerException;
503 extern java_objectheader *proto_java_lang_ArrayIndexOutOfBoundsException;
504 extern java_objectheader *proto_java_lang_NegativeArraySizeException;
505 extern java_objectheader *proto_java_lang_OutOfMemoryError;
506 extern java_objectheader *proto_java_lang_ArithmeticException;
507 extern java_objectheader *proto_java_lang_ArrayStoreException;
508 extern java_objectheader *proto_java_lang_ThreadDeath;
511 /* flag variables *************************************************************/
513 extern bool compileall;
514 extern bool runverbose;
518 /* statistic variables ********************************************************/
520 extern int count_class_infos;
521 extern int count_const_pool_len;
522 extern int count_vftbl_len;
523 extern int count_unicode_len;
524 extern int count_all_methods;
525 extern int count_vmcode_len;
526 extern int count_extable_len;
532 * These are local overrides for various environment variables in Emacs.
533 * Please do not remove this and leave it at the end of the file, where
534 * Emacs will automagically detect them.
535 * ---------------------------------------------------------------------
538 * indent-tabs-mode: t