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 Changes: Andreas Krall EMAIL: cacao@complang.tuwien.ac.at
12 Mark Probst EMAIL: cacao@complang.tuwien.ac.at
13 Philipp Tomsich 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 */
51 typedef u1 bool; /* boolean data type */
56 typedef void (*functionptr) (); /* generic function pointer */
59 #define MAX_ALIGN 8 /* most generic alignment for JavaVM values */
62 /**************************** shutdown function *******************************/
64 void cacao_shutdown(s4 status);
67 /**************************** basic data types ********************************/
69 #define TYPE_INT 0 /* the JavaVM types must numbered in the */
70 #define TYPE_LONG 1 /* same order as the ICMD_Ixxx to ICMD_Axxx */
71 #define TYPE_FLOAT 2 /* instructions (LOAD and STORE) */
72 #define TYPE_DOUBLE 3 /* integer, long, float, double, address */
73 #define TYPE_ADDRESS 4 /* all other types can be numbered arbitrarily*/
78 /**************************** Java class file constants ***********************/
80 #define MAGIC 0xcafebabe
81 #define MINOR_VERSION 3
82 #define MAJOR_VERSION 45
84 #define CONSTANT_Class 7
85 #define CONSTANT_Fieldref 9
86 #define CONSTANT_Methodref 10
87 #define CONSTANT_InterfaceMethodref 11
88 #define CONSTANT_String 8
89 #define CONSTANT_Integer 3
90 #define CONSTANT_Float 4
91 #define CONSTANT_Long 5
92 #define CONSTANT_Double 6
93 #define CONSTANT_NameAndType 12
94 #define CONSTANT_Utf8 1
96 #define CONSTANT_Arraydescriptor 13
97 #define CONSTANT_UNUSED 0
100 #define ACC_PUBLIC 0x0001
101 #define ACC_PRIVATE 0x0002
102 #define ACC_PROTECTED 0x0004
103 #define ACC_STATIC 0x0008
104 #define ACC_FINAL 0x0010
105 #define ACC_SYNCHRONIZED 0x0020
106 #define ACC_VOLATILE 0x0040
107 #define ACC_TRANSIENT 0x0080
108 #define ACC_NATIVE 0x0100
109 #define ACC_INTERFACE 0x0200
110 #define ACC_ABSTRACT 0x0400
114 /**************************** resolve typedef-cycles **************************/
116 typedef struct unicode unicode;
117 typedef struct classinfo classinfo;
118 typedef struct vftbl vftbl;
119 typedef u1* methodptr;
122 /********************** data structures of UNICODE symbol *********************/
125 unicode *hashlink; /* externe Verkettung f"ur die unicode-Hashtabelle */
126 u4 key; /* Hash-Schl"ussel (h"angt nur vom Text ab) */
127 int length; /* L"ange des Textes */
128 u2 *text; /* Zeiger auf den Text (jeder Buchstabe 16 Bit) */
129 classinfo *class; /* gegebenenfalls Referenz auf die Klasse dieses
130 Namens (oder NULL, wenn es keine solche gibt) */
131 struct java_objectheader *string;
132 /* gegebenenfalls Referenz auf einen konstanten
133 String mit dem entsprechenden Wert */
136 /* Alle Unicode-Symbole werden in einer einzigen globalen Tabelle
137 (Hashtabelle) verwaltet, jedes Symbol wird nur einmal angelegt.
138 -> Speicherersparnis, und "Uberpr"ufung auf Gleichheit durch einfachen
142 /************ data structures of remaining constant pool entries **************/
172 } constant_nameandtype;
175 typedef struct constant_arraydescriptor {
177 classinfo *objectclass;
178 struct constant_arraydescriptor *elementdescriptor;
179 } constant_arraydescriptor;
181 /* Mit einem Arraydescriptor kann ein Array-Typ dargestellt werden.
182 Bei normalen Arrays (z.B. Array von Bytes,...) gen"ugt dazu,
183 dass das Feld arraytype die entsprechende Kennzahl enth"alt
184 (z.B. ARRAYTYPE_BYTE).
185 Bei Arrays von Objekten (arraytype=ARRAYTYPE_OBJECT) muss das
186 Feld objectclass auf die Klassenstruktur der m"oglichen
187 Element-Objekte zeigen.
188 Bei Arrays von Arrays (arraytype=ARRAYTYPE_ARRAY) muss das
189 Feld elementdescriptor auf eine weiter arraydescriptor-Struktur
190 zeigen, die die Element-Typen beschreibt.
195 /********* Anmerkungen zum Constant-Pool:
197 Die Typen der Eintr"age in den Constant-Pool werden durch die oben
198 definierten CONSTANT_.. Werte angegeben.
199 Bei allen Typen muss zus"atzlich noch eine Datenstruktur hinzugef"ugt
200 werden, die den wirklichen Wert angibt.
201 Bei manchen Typen reicht es, einen Verweis auf eine schon bereits
202 existierende Struktur (z.B. unicode-Texte) einzutragen, bei anderen
203 muss diese Struktur erst extra erzeugt werden.
204 Ich habe folgende Datenstrukturen f"ur diese Typen verwendet:
206 Typ Struktur extra erzeugt?
207 ----------------------------------------------------------------------
208 CONSTANT_Class classinfo nein
209 CONSTANT_Fieldref constant_FMIref ja
210 CONSTANT_Methodref constant_FMIref ja
211 CONSTANT_InterfaceMethodref constant_FMIref ja
212 CONSTANT_String unicode nein
213 CONSTANT_Integer constant_integer ja
214 CONSTANT_Float constant_float ja
215 CONSTANT_Long constant_long ja
216 CONSTANT_Double constant_double ja
217 CONSTANT_NameAndType constant_nameandtype ja
218 CONSTANT_Utf8 unicode nein
219 CONSTANT_Arraydescriptor constant_arraydescriptor ja
222 *******************************/
226 /***************** Die Datenstrukturen fuer das Laufzeitsystem ****************/
229 /********* Objekte **********
231 Alle Objekte (und Arrays), die am Heap gespeichert werden, m"ussen eine
232 folgende spezielle Datenstruktur ganz vorne stehen haben:
236 typedef struct java_objectheader { /* Der Header f"ur alle Objekte */
237 vftbl *vftbl; /* Zeiger auf die Function Table */
242 /********* Arrays ***********
244 Alle Arrays in Java sind auch gleichzeitig Objekte (d.h. sie haben auch
245 den obligatorischen Object-Header und darin einen Verweis auf eine Klasse)
246 Es gibt aber (der Einfachheit halber) nur eine einzige Klasse f"ur alle
247 m"oglichen Typen von Arrays, deshalb wird der tats"achliche Typ in einem
248 Feld im Array-Objekt selbst gespeichert.
251 #define ARRAYTYPE_INT 0
252 #define ARRAYTYPE_LONG 1
253 #define ARRAYTYPE_FLOAT 2
254 #define ARRAYTYPE_DOUBLE 3
255 #define ARRAYTYPE_BYTE 4
256 #define ARRAYTYPE_CHAR 5
257 #define ARRAYTYPE_SHORT 6
258 #define ARRAYTYPE_BOOLEAN 7
259 #define ARRAYTYPE_OBJECT 8
260 #define ARRAYTYPE_ARRAY 9
263 /** Der Header f"ur ein Java-Array **/
265 typedef struct java_arrayheader { /* Der Arrayheader f"ur alle Arrays */
266 java_objectheader objheader; /* Der Object-Header */
267 s4 size; /* Gr"osse des Arrays */
268 s4 arraytype; /* Typ der Elemente */
273 /** Die Unterschiedlichen Strukturen f"ur alle Typen von Arrays **/
275 typedef struct java_chararray {
276 java_arrayheader header;
280 typedef struct java_floatheader {
281 java_arrayheader header;
285 typedef struct java_doublearray {
286 java_arrayheader header;
291 /* achtung: die beiden Stukturen booleanarray und bytearray m"ussen
292 identisches memory-layout haben, weil mit den selben Funktionen
293 darauf zugegriffen wird */
295 typedef struct java_booleanarray {
296 java_arrayheader header;
300 typedef struct java_bytearray {
301 java_arrayheader header;
305 typedef struct java_shortarray {
306 java_arrayheader header;
310 typedef struct java_intarray {
311 java_arrayheader header;
315 typedef struct java_longarray {
316 java_arrayheader header;
321 /* ACHTUNG: die beiden folgenden Strukturen m"ussen unbedingt gleiches
322 Memory-Layout haben, weil mit ein und der selben Funktion auf die
323 data-Eintr"age beider Typen zugegriffen wird !!!! */
325 typedef struct java_objectarray {
326 java_arrayheader header;
327 classinfo *elementtype;
328 java_objectheader *data[1];
331 typedef struct java_arrayarray {
332 java_arrayheader header;
333 constant_arraydescriptor *elementdescriptor;
334 java_arrayheader *data[1];
340 /******************** class, field and method structures **********************/
343 /********************** structure: fieldinfo **********************************/
345 typedef struct fieldinfo {/* field of a class */
346 s4 flags; /* ACC flags */
347 s4 type; /* basic data type */
348 unicode *name; /* name of field */
349 unicode *descriptor; /* JavaVM descriptor string of field */
351 s4 offset; /* offset from start of object (instance variables) */
353 union { /* storage for static values (class variables) */
364 /********************** structure: exceptiontable *****************************/
366 typedef struct exceptiontable { /* exceptiontable entry in a method */
367 s4 startpc; /* start pc of guarded area (inclusive) */
368 s4 endpc; /* end pc of guarded area (exklusive) */
369 s4 handlerpc; /* pc of exception handler */
370 classinfo *catchtype; /* catchtype of exception (NULL == catchall) */
374 /********************** structure: methodinfo *********************************/
376 typedef struct methodinfo { /* method structure */
377 s4 flags; /* ACC flags */
378 unicode *name; /* name of method */
379 unicode *descriptor; /* JavaVM descriptor string of method */
380 s4 returntype; /* only temporary valid, return type */
381 s4 paramcount; /* only temporary valid, parameter count */
382 u1 *paramtypes; /* only temporary valid, parameter types */
383 classinfo *class; /* class, the method belongs to */
384 u4 vftblindex; /* index of method in virtual function table
385 (if it is a virtual method) */
386 s4 maxstack; /* maximum stack depth of method */
387 s4 maxlocals; /* maximum number of local variables */
388 u4 jcodelength; /* length of JavaVM code */
389 u1 *jcode; /* pointer to JavaVM code */
391 s4 exceptiontablelength;/* exceptiontable length */
392 exceptiontable *exceptiontable; /* the exceptiontable */
394 u1 *stubroutine; /* stub for compiling or calling natives */
395 u4 mcodelength; /* legth of generated machine code */
396 u1 *mcode; /* pointer to machine code */
397 u1 *entrypoint; /* entry point in machine code */
402 /********************** structure: classinfo **********************************/
404 struct classinfo { /* class structure */
405 java_objectheader header; /* classes are also objects */
407 s4 flags; /* ACC flags */
408 unicode *name; /* class name */
410 s4 cpcount; /* number of entries in constant pool */
411 u1 *cptags; /* constant pool tags */
412 voidptr *cpinfos; /* pointer to constant pool info structures */
414 classinfo *super; /* super class pointer */
415 classinfo *sub; /* sub class pointer */
416 classinfo *nextsub; /* pointer to next class in sub class list */
418 s4 interfacescount; /* number of interfaces */
419 classinfo **interfaces; /* pointer to interfaces */
421 s4 fieldscount; /* number of fields */
422 fieldinfo *fields; /* field table */
424 s4 methodscount; /* number of methods */
425 methodinfo *methods; /* method table */
427 listnode listnode; /* linkage */
429 bool initialized; /* true, if class already initialised */
430 bool linked; /* true, if class already linked */
431 s4 index; /* hierarchy depth (classes) or index
433 s4 instancesize; /* size of an instance of this class */
435 vftbl *vftbl; /* pointer to virtual function table */
437 methodinfo *finalizer; /* finalizer method */
438 #ifdef JIT_MARKER_SUPPORT
445 classinfo *class; /* Class, the function table belongs to */
447 s4 vftbllength; /* virtual function table length */
448 s4 interfacetablelength; /* interface table length */
450 s4 lowclassval; /* low value for relative numbering */
451 s4 highclassval; /* high value for relative numbering */
453 u4 *interfacevftbllength; /* see description below */
454 methodptr **interfacevftbl;
459 /*********** Anmerkungen zur Interfacetable:
461 "Ahnlich wie die 'normalen' virtuellen Methoden k"onnen auch die
462 Interface-Methoden mit Hilfe einer Art Virtual Function Table
464 Dazu werden alle Interfaces im System fortlaufend nummeriert (beginnend
465 bei 0), und f"ur jede Klasse wird eine ganze Tabelle von
466 Virtual Function Tables erzeugt, n"amlich zu jedem Interface, das die
467 Klasse implementiert, eine.
469 z.B. Nehmen wir an, eine Klasse implementiert zwei Interfaces (die durch
470 die Nummerierung die Indizes 0 und 3 bekommen haben)
472 Das sieht dann ungef"ahr so aus:
473 -------------- -------------
474 interfacevftbl ---> | Eintrag 0 |---------> | Methode 0 |---> Methode X
475 | Eintrag 1 |--> NULL | Methode 1 |---> Methode Y
476 | Eintrag 2 |--> NULL | Methode 2 |---> Methode Z
477 | Eintrag 3 |-----+ -------------
480 | Methode 0 |---> Methode X
481 | Methode 1 |---> Methode A
484 interfacevftlblength ---> | Wert 0 = 3 |
490 Der Aufruf einer Interface-Methode geht dann so vor sich:
491 Zur Compilezeit steht der Index (i) des Interfaces und die Stelle (s), wo
492 in der entsprechenden Untertabelle die Methode eingetragen ist, schon fest.
493 Also muss zur Laufzeit nur mehr der n-te Eintrag aus der Interfacetable
494 gelesen werden, von dieser Tabelle aus wird der s-te Eintrag geholt,
495 und diese Methode wird angesprungen.
501 /********************** references to some system classes ********************/
503 extern classinfo *class_java_lang_Object;
504 extern classinfo *class_java_lang_String;
505 extern classinfo *class_java_lang_ClassCastException;
506 extern classinfo *class_java_lang_NullPointerException;
507 extern classinfo *class_java_lang_ArrayIndexOutOfBoundsException;
508 extern classinfo *class_java_lang_NegativeArraySizeException;
509 extern classinfo *class_java_lang_OutOfMemoryError;
510 extern classinfo *class_java_lang_ArithmeticException;
511 extern classinfo *class_java_lang_ArrayStoreException;
512 extern classinfo *class_java_lang_ThreadDeath;
514 extern classinfo *class_array;
517 /********************** instances of some system classes **********************/
519 extern java_objectheader *proto_java_lang_ClassCastException;
520 extern java_objectheader *proto_java_lang_NullPointerException;
521 extern java_objectheader *proto_java_lang_ArrayIndexOutOfBoundsException;
522 extern java_objectheader *proto_java_lang_NegativeArraySizeException;
523 extern java_objectheader *proto_java_lang_OutOfMemoryError;
524 extern java_objectheader *proto_java_lang_ArithmeticException;
525 extern java_objectheader *proto_java_lang_ArrayStoreException;
526 extern java_objectheader *proto_java_lang_ThreadDeath;
529 /******************************* flag variables *******************************/
531 extern bool compileall;
532 extern bool runverbose;
536 /******************************* trace variables ******************************/
538 extern int count_class_infos;
539 extern int count_const_pool_len;
540 extern int count_vftbl_len;
541 extern int count_unicode_len;
542 extern int count_all_methods;
543 extern int count_vmcode_len;
544 extern int count_extable_len;
550 * These are local overrides for various environment variables in Emacs.
551 * Please do not remove this and leave it at the end of the file, where
552 * Emacs will automagically detect them.
553 * ---------------------------------------------------------------------
556 * indent-tabs-mode: t