bidirectional layout of vftbl/interfaces added
[cacao.git] / src / vm / global.h
1 /****************************** global.h ***************************************
2
3         Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
4
5         See file COPYRIGHT for information on usage and disclaimer of warranties
6
7         Contains global definitions which are used in the whole program, includes
8         some files and contains global used macros.
9
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
14
15         Last Change: 1998/10/30
16
17 *******************************************************************************/
18
19 #ifndef __global_h_
20 #define __global_h_                        /* schani */
21
22 #define STATISTICS                         /* andi   */
23
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   */
27
28 /***************************** standard includes ******************************/
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <time.h>
34
35 #include "toolbox/memory.h"
36 #include "toolbox/chain.h"
37 #include "toolbox/list.h"
38 #include "toolbox/loging.h"
39
40
41 /**************************** system dependent types **************************/
42
43 #include "sysdep/types.h"
44
45
46 /**************************** additional data types ***************************/
47
48 typedef void *voidptr;          /* generic pointer */
49
50
51 typedef u1    bool;             /* boolean data type */
52
53 #define true  1
54 #define false 0
55
56 typedef void (*functionptr) (); /* generic function pointer */
57
58
59 #define MAX_ALIGN 8             /* most generic alignment for JavaVM values   */
60
61
62 /**************************** shutdown function *******************************/
63
64 void cacao_shutdown(s4 status);
65
66
67 /**************************** basic data types ********************************/
68
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*/
74
75 #define TYPE_VOID    10
76
77
78 /**************************** Java class file constants ***********************/
79
80 #define MAGIC         0xcafebabe
81 #define MINOR_VERSION 3
82 #define MAJOR_VERSION 45
83
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
95
96 #define CONSTANT_Arraydescriptor     13
97 #define CONSTANT_UNUSED               0
98
99
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
111
112
113
114 /**************************** resolve typedef-cycles **************************/
115
116 typedef struct unicode unicode;
117 typedef struct classinfo classinfo; 
118 typedef struct vftbl vftbl;
119 typedef u1* methodptr;
120
121
122 /********************** data structures of UNICODE symbol *********************/
123
124 struct unicode {
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 */ 
134 };
135
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
139            Zeigervergleich */
140
141
142 /************ data structures of remaining constant pool entries **************/
143
144
145 typedef struct {
146         classinfo *class;
147         unicode   *name;
148         unicode   *descriptor;
149 } constant_FMIref;
150
151
152 typedef struct {
153         s4 value;
154 } constant_integer;
155         
156 typedef struct {
157         float value;
158 } constant_float;
159
160 typedef struct {
161         s8 value;
162 } constant_long;
163         
164 typedef struct {
165         double value;
166 } constant_double;
167
168
169 typedef struct {
170         unicode *name;
171         unicode *descriptor;
172 } constant_nameandtype;
173
174
175 typedef struct constant_arraydescriptor {
176         int arraytype;
177         classinfo *objectclass;
178         struct constant_arraydescriptor *elementdescriptor;
179 } constant_arraydescriptor;
180
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.
191         */
192
193
194
195 /********* Anmerkungen zum Constant-Pool:
196
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:
205         
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
220     CONSTANT_UNUSED              -
221
222 *******************************/
223
224
225
226 /***************** Die Datenstrukturen fuer das Laufzeitsystem ****************/
227
228
229         /********* Objekte **********
230
231         Alle Objekte (und Arrays), die am Heap gespeichert werden, m"ussen eine 
232         folgende spezielle Datenstruktur ganz vorne stehen haben: 
233
234         */
235
236 typedef struct java_objectheader {    /* Der Header f"ur alle Objekte */
237         vftbl *vftbl;                     /* Zeiger auf die Function Table */
238 } java_objectheader;
239
240
241
242 /********* Arrays ***********
243         
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.
249         Die Typen sind: */
250
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
261
262
263 /** Der Header f"ur ein Java-Array **/
264
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 */
269 } java_arrayheader;
270
271
272
273 /** Die Unterschiedlichen Strukturen f"ur alle Typen von Arrays **/
274
275 typedef struct java_chararray {
276         java_arrayheader header;
277         u2 data[1];
278 } java_chararray;
279
280 typedef struct java_floatheader {
281         java_arrayheader header;
282         float data[1];
283 } java_floatarray;
284
285 typedef struct java_doublearray {
286         java_arrayheader header;
287         double data[1];
288 } java_doublearray;
289
290
291 /* achtung: die beiden Stukturen booleanarray und bytearray m"ussen 
292       identisches memory-layout haben, weil mit den selben Funktionen 
293       darauf zugegriffen wird */
294
295 typedef struct java_booleanarray {
296         java_arrayheader header;
297         u1 data[1];
298 } java_booleanarray;
299
300 typedef struct java_bytearray {
301         java_arrayheader header;
302         s1 data[1];
303 } java_bytearray;
304
305 typedef struct java_shortarray {
306         java_arrayheader header;
307         s2 data[1];
308 } java_shortarray;
309
310 typedef struct java_intarray {
311         java_arrayheader header;
312         s4 data[1];
313 } java_intarray;
314
315 typedef struct java_longarray {
316         java_arrayheader header;
317         s8 data[1];
318 } java_longarray;
319
320
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 !!!! */
324
325 typedef struct java_objectarray {
326         java_arrayheader header;
327         classinfo *elementtype;
328         java_objectheader *data[1];
329 } java_objectarray;
330
331 typedef struct java_arrayarray {
332         java_arrayheader header;
333         constant_arraydescriptor *elementdescriptor;
334         java_arrayheader *data[1];
335 } java_arrayarray;
336
337
338
339
340 /******************** class, field and method structures **********************/
341
342
343 /********************** structure: fieldinfo **********************************/
344
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                */
350         
351         s4       offset;      /* offset from start of object (instance variables) */
352
353         union {               /* storage for static values (class variables)      */
354                 s4 i; 
355                 s8 l;
356                 float f;
357                 double d;
358                 void *a; 
359         } value;
360
361 } fieldinfo;
362
363
364 /********************** structure: exceptiontable *****************************/
365
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)  */
371 } exceptiontable;
372
373
374 /********************** structure: methodinfo *********************************/
375
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                 */
390
391         s4         exceptiontablelength;/* exceptiontable length                  */
392         exceptiontable *exceptiontable; /* the exceptiontable                     */
393
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            */
398
399 } methodinfo;
400
401
402 /********************** structure: classinfo **********************************/
403
404 struct classinfo {                /* class structure                          */
405         java_objectheader header;     /* classes are also objects                 */
406
407         s4          flags;            /* ACC flags                                */
408         unicode    *name;             /* class name                               */ 
409
410         s4          cpcount;          /* number of entries in constant pool       */
411         u1         *cptags;           /* constant pool tags                       */
412         voidptr    *cpinfos;          /* pointer to constant pool info structures */
413
414         classinfo  *super;            /* super class pointer                      */
415         classinfo  *sub;              /* sub class pointer                        */
416         classinfo  *nextsub;          /* pointer to next class in sub class list  */
417
418         s4          interfacescount;  /* number of interfaces                     */
419         classinfo **interfaces;       /* pointer to interfaces                    */
420
421         s4          fieldscount;      /* number of fields                         */
422         fieldinfo  *fields;           /* field table                              */
423
424         s4          methodscount;     /* number of methods                        */
425         methodinfo *methods;          /* method table                             */
426
427         listnode    listnode;         /* linkage                                  */
428
429         bool        initialized;      /* true, if class already initialised       */ 
430         bool        linked;           /* true, if class already linked            */
431         s4                      index;            /* hierarchy depth (classes) or index
432                                          (interfaces)                             */ 
433         s4          instancesize;     /* size of an instance of this class        */
434
435         vftbl      *vftbl;            /* pointer to virtual function table        */
436
437         methodinfo *finalizer;        /* finalizer method                         */
438 #ifdef JIT_MARKER_SUPPORT
439         methodinfo *marker; 
440 #endif
441 };
442         
443
444 struct vftbl {
445         classinfo  *class;                /* Class, the function table belongs to */
446
447         s4          vftbllength;          /* virtual function table length        */
448         s4          interfacetablelength; /* interface table length               */   
449
450         s4          lowclassval;          /* low value for relative numbering     */   
451         s4          highclassval;         /* high value for relative numbering    */   
452
453         u4         *interfacevftbllength; /* see description below                */   
454         methodptr **interfacevftbl;
455         
456         methodptr   table[1];
457 };
458
459 /*********** Anmerkungen zur Interfacetable: 
460
461         "Ahnlich wie die 'normalen' virtuellen Methoden k"onnen auch die
462         Interface-Methoden mit Hilfe einer Art Virtual Function Table 
463         aufgerufen werden.
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.
468
469         z.B. Nehmen wir an, eine Klasse implementiert zwei Interfaces (die durch
470         die Nummerierung die Indizes 0 und 3 bekommen haben)
471
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  |-----+     ------------- 
478                         --------------     |
479                                            +---> -------------
480                                                  | Methode 0 |---> Methode X
481                                                  | Methode 1 |---> Methode A
482                                                  -------------
483                               ---------------
484         interfacevftlblength ---> | Wert 0 = 3  |
485                                   | Wert 1 = 0  |
486                                   | Wert 2 = 0  |
487                                   | Wert 3 = 2  |
488                                   ---------------
489
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.
496
497 ****************/
498
499
500
501 /********************** references to some system classes  ********************/
502
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;
513  
514 extern classinfo *class_array;
515
516
517 /********************** instances of some system classes **********************/
518
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;
527
528
529 /******************************* flag variables *******************************/
530
531 extern bool compileall;
532 extern bool runverbose;         
533 extern bool verbose;         
534                                 
535
536 /******************************* trace variables ******************************/
537
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;
545
546 #endif
547
548
549 /*
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  * ---------------------------------------------------------------------
554  * Local variables:
555  * mode: c
556  * indent-tabs-mode: t
557  * c-basic-offset: 4
558  * tab-width: 4
559  * End:
560  */