Added an option to enable/disable the new gc
[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                  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
14
15         Last Change: $Id: global.h 64 1998-11-11 21:11:55Z phil $
16
17 *******************************************************************************/
18
19 #ifndef __global_h_
20 #define __global_h_
21
22 #define OLD_COMPILER        /* if enabled makes old compiler available        */
23 #define NEW_GC              /* if enabled, includes the new gc. -- phil.      */
24
25 #define STATISTICS          /* if enabled collects program statistics         */
26
27 /* 
28  * JIT_MARKER_SUPPORT is the define used to toggle Just-in-time generated
29  * marker functions on and off.
30  *
31  * SIZE_FROM_CLASSINFO toggles between the bitmap_based and the new method 
32  * of determining the sizes of objects on the heap.
33  */
34 #undef JIT_MARKER_SUPPORT        /* phil */
35 #undef SIZE_FROM_CLASSINFO
36 //#define SIZE_FROM_CLASSINFO
37
38 /* standard includes **********************************************************/
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <time.h>
44
45 #include "toolbox/memory.h"
46 #include "toolbox/chain.h"
47 #include "toolbox/list.h"
48 #include "toolbox/loging.h"
49
50
51 /* system dependent types *****************************************************/
52
53 #include "sysdep/types.h"
54
55
56 /* additional data types ******************************************************/
57
58 typedef void *voidptr;          /* generic pointer */
59
60 typedef int   bool;             /* boolean data type */
61
62 #define true  1
63 #define false 0
64
65 typedef void (*functionptr) (); /* generic function pointer */
66
67
68 #define MAX_ALIGN 8             /* most generic alignment for JavaVM values   */
69
70
71 /* shutdown function **********************************************************/
72
73 void cacao_shutdown(s4 status);
74
75
76 /* basic data types ***********************************************************/
77
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 */
83
84 #define TYPE_VOID    10
85
86
87 /* Java class file constants **************************************************/
88
89 #define MAGIC         0xcafebabe
90 #define MINOR_VERSION 3
91 #define MAJOR_VERSION 45
92
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
104
105 #define CONSTANT_Arraydescriptor      13
106 #define CONSTANT_UNUSED                0
107
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
119
120
121 /* resolve typedef cycles *****************************************************/
122
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;
128
129
130 /* constant pool entries *******************************************************
131
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:
135
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
150     CONSTANT_UNUSED              -
151
152 *******************************************************************************/
153
154 /* data structures of Unicode symbol *******************************************
155
156         All Unicode symbols are stored in one global (hash) table, every symbol
157         exists only once. Equal symbols have identical pointers.
158 */
159
160 struct unicode {
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*/ 
167 };
168
169
170 /* data structures of remaining constant pool entries *************************/
171
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  */
176 } constant_FMIref;
177
178 typedef struct {            /* Integer                                        */
179         s4 value;
180 } constant_integer;
181         
182 typedef struct {            /* Float                                          */
183         float value;
184 } constant_float;
185
186 typedef struct {            /* Long                                           */
187         s8 value;
188 } constant_long;
189         
190 typedef struct {            /* Double                                         */
191         double value;
192 } constant_double;
193
194 typedef struct {            /* NameAndType (Field or Method)                  */
195         unicode *name;          /* field/method name                              */
196         unicode *descriptor;    /* field/method type descriptor string            */
197 } constant_nameandtype;
198
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).
204 */
205
206 typedef struct constant_arraydescriptor {
207         int arraytype;
208         classinfo *objectclass;
209         struct constant_arraydescriptor *elementdescriptor;
210 } constant_arraydescriptor;
211
212
213 /* data structures of the runtime system **************************************/
214
215 /* objects *********************************************************************
216
217         All objects (and arrays) which resides on the heap need the following
218         header at the beginning of the data structure.
219 */
220
221 struct java_objectheader {              /* header for all objects             */
222         vftbl *vftbl;                       /* pointer to virtual function table  */
223 };
224
225
226
227 /* arrays **********************************************************************
228
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
232         are defined:
233 */
234
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
245
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 */
251 #endif
252         s4 arraytype;                       /* array type from previous list      */
253 } java_arrayheader;
254
255
256
257 /* structs for all kinds of arrays ********************************************/
258
259 typedef struct java_chararray {
260         java_arrayheader header;
261         u2 data[1];
262 } java_chararray;
263
264 typedef struct java_floatheader {
265         java_arrayheader header;
266         float data[1];
267 } java_floatarray;
268
269 typedef struct java_doublearray {
270         java_arrayheader header;
271         double data[1];
272 } java_doublearray;
273
274 /*  booleanarray and bytearray need identical memory layout (access methods
275     use the same machine code */
276
277 typedef struct java_booleanarray {
278         java_arrayheader header;
279         u1 data[1];
280 } java_booleanarray;
281
282 typedef struct java_bytearray {
283         java_arrayheader header;
284         s1 data[1];
285 } java_bytearray;
286
287 typedef struct java_shortarray {
288         java_arrayheader header;
289         s2 data[1];
290 } java_shortarray;
291
292 typedef struct java_intarray {
293         java_arrayheader header;
294         s4 data[1];
295 } java_intarray;
296
297 typedef struct java_longarray {
298         java_arrayheader header;
299         s8 data[1];
300 } java_longarray;
301
302 /*  objectarray and arrayarray need identical memory layout (access methods
303     use the same machine code */
304
305 typedef struct java_objectarray {
306         java_arrayheader header;
307         classinfo *elementtype;
308         java_objectheader *data[1];
309 } java_objectarray;
310
311 typedef struct java_arrayarray {
312         java_arrayheader header;
313         constant_arraydescriptor *elementdescriptor;
314         java_arrayheader *data[1];
315 } java_arrayarray;
316
317
318 /* field, method and class structures *****************************************/
319
320 /* fieldinfo ******************************************************************/
321
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                */
327         
328         s4       offset;      /* offset from start of object (instance variables) */
329
330         union {               /* storage for static values (class variables)      */
331                 s4 i; 
332                 s8 l;
333                 float f;
334                 double d;
335                 void *a; 
336         } value;
337
338 } fieldinfo;
339
340
341 /* exceptiontable *************************************************************/
342
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)  */
348 } exceptiontable;
349
350
351 /* methodinfo *****************************************************************/
352
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                 */
367
368         s4         exceptiontablelength;/* exceptiontable length                  */
369         exceptiontable *exceptiontable; /* the exceptiontable                     */
370
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            */
375
376 } methodinfo;
377
378
379 /* classinfo ******************************************************************/
380
381 struct classinfo {                /* class structure                          */
382         java_objectheader header;     /* classes are also objects                 */
383
384         s4          flags;            /* ACC flags                                */
385         unicode    *name;             /* class name                               */ 
386
387         s4          cpcount;          /* number of entries in constant pool       */
388         u1         *cptags;           /* constant pool tags                       */
389         voidptr    *cpinfos;          /* pointer to constant pool info structures */
390
391         classinfo  *super;            /* super class pointer                      */
392         classinfo  *sub;              /* sub class pointer                        */
393         classinfo  *nextsub;          /* pointer to next class in sub class list  */
394
395         s4          interfacescount;  /* number of interfaces                     */
396         classinfo **interfaces;       /* pointer to interfaces                    */
397
398         s4          fieldscount;      /* number of fields                         */
399         fieldinfo  *fields;           /* field table                              */
400
401         s4          methodscount;     /* number of methods                        */
402         methodinfo *methods;          /* method table                             */
403
404         listnode    listnode;         /* linkage                                  */
405
406         bool        initialized;      /* true, if class already initialised       */ 
407         bool        linked;           /* true, if class already linked            */
408         s4                      index;            /* hierarchy depth (classes) or index
409                                          (interfaces)                             */ 
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 */
414 #endif
415
416         vftbl      *vftbl;            /* pointer to virtual function table        */
417
418         methodinfo *finalizer;        /* finalizer method                         */
419 #ifdef JIT_MARKER_SUPPORT
420         methodinfo *marker; 
421 #endif
422 };
423
424
425 /* virtual function table ******************************************************
426
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.
431
432         runtime type check (checkcast):
433
434         Different methods are used for runtime type check depending on the
435         argument of checkcast/instanceof.
436         
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.
443
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
446         interface.
447
448         interfacetable:
449
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.
456
457         The following example assumes a class which implements interface 0 and 3:
458
459         interfacetablelength = 4
460
461                   | ...       |            +----------+
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                   +-----------+     +----> +----------+
471      
472                               +---------------+
473                                   | length 3 = 2  |
474                                   | length 2 = 0  |
475                                   | length 1 = 0  |
476                                   | length 0 = 3  |
477         interfacevftbllength ---> +---------------+
478
479 *******************************************************************************/
480
481 struct vftbl {
482         methodptr   *interfacetable[1];    /* interface table (access via macro)  */
483
484         classinfo   *class;                /* class, the vtbl belongs to          */
485
486         s4           vftbllength;          /* virtual function table length       */
487         s4           interfacetablelength; /* interface table length              */
488
489         s4           baseval;              /* base for runtime type check         */
490         s4           diffval;              /* high - base for runtime type check  */
491
492         s4          *interfacevftbllength; /* length of interface vftbls          */
493         
494         methodptr    table[1];             /* class vftbl                         */
495 };
496
497 #define VFTBLINTERFACETABLE(v,i)       (v)->interfacetable[-i]
498
499
500 /* references to some system classes ******************************************/
501
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;
512  
513 extern classinfo *class_array;
514
515
516 /* instances of some system classes *******************************************/
517
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;
526
527
528 /* flag variables *************************************************************/
529
530 extern bool compileall;
531 extern bool runverbose;         
532 extern bool verbose;         
533
534
535 /* statistic variables ********************************************************/
536
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;
544
545 #endif
546
547
548 /*
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  * ---------------------------------------------------------------------
553  * Local variables:
554  * mode: c
555  * indent-tabs-mode: t
556  * c-basic-offset: 4
557  * tab-width: 4
558  * End:
559  */