Changed the makefile system to autoconf/automake.
[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 115 1999-01-20 01:52:45Z 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 #define SIZE_FROM_CLASSINFO
36
37 /* standard includes **********************************************************/
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <time.h>
43
44 #include "toolbox/memory.h"
45 #include "toolbox/chain.h"
46 #include "toolbox/list.h"
47 #include "toolbox/loging.h"
48
49 /* system dependent types *****************************************************/
50
51 #include "types.h"
52
53
54 /* additional data types ******************************************************/
55
56 typedef void *voidptr;          /* generic pointer */
57
58 typedef int   bool;             /* boolean data type */
59
60 #define true  1
61 #define false 0
62
63 typedef void (*functionptr) (); /* generic function pointer */
64
65
66 #define MAX_ALIGN 8             /* most generic alignment for JavaVM values   */
67
68
69 /* shutdown function **********************************************************/
70
71 void cacao_shutdown(s4 status);
72
73
74 /* basic data types ***********************************************************/
75
76 #define TYPE_INT      0         /* the JavaVM types must numbered in the      */
77 #define TYPE_LONG     1         /* same order as the ICMD_Ixxx to ICMD_Axxx   */
78 #define TYPE_FLOAT    2         /* instructions (LOAD and STORE)              */
79 #define TYPE_DOUBLE   3         /* integer, long, float, double, address      */
80 #define TYPE_ADDRESS  4         /* all other types can be numbered arbitrarly */
81
82 #define TYPE_VOID    10
83
84
85 /* Java class file constants **************************************************/
86
87 #define MAGIC         0xcafebabe
88 #define MINOR_VERSION 3
89 #define MAJOR_VERSION 45
90
91 #define CONSTANT_Class                 7
92 #define CONSTANT_Fieldref              9
93 #define CONSTANT_Methodref            10
94 #define CONSTANT_InterfaceMethodref   11
95 #define CONSTANT_String                8
96 #define CONSTANT_Integer               3
97 #define CONSTANT_Float                 4
98 #define CONSTANT_Long                  5
99 #define CONSTANT_Double                6
100 #define CONSTANT_NameAndType          12
101 #define CONSTANT_Utf8                  1
102
103 #define CONSTANT_Arraydescriptor      13
104 #define CONSTANT_UNUSED                0
105
106 #define ACC_PUBLIC                0x0001
107 #define ACC_PRIVATE               0x0002
108 #define ACC_PROTECTED             0x0004
109 #define ACC_STATIC                0x0008
110 #define ACC_FINAL                 0x0010
111 #define ACC_SYNCHRONIZED          0x0020
112 #define ACC_VOLATILE              0x0040
113 #define ACC_TRANSIENT             0x0080
114 #define ACC_NATIVE                0x0100
115 #define ACC_INTERFACE             0x0200
116 #define ACC_ABSTRACT              0x0400
117
118
119 /* resolve typedef cycles *****************************************************/
120
121 typedef struct unicode unicode;
122 typedef struct java_objectheader java_objectheader; 
123 typedef struct classinfo classinfo; 
124 typedef struct vftbl vftbl;
125 typedef u1* methodptr;
126
127
128 /* constant pool entries *******************************************************
129
130         All constant pool entries need a data structure which contain the entrys
131         value. In some cases this structure exist already, in the remaining cases
132         this structure must be generated:
133
134                 kind                      structure                     generated?
135         ----------------------------------------------------------------------
136     CONSTANT_Class               classinfo                           no
137     CONSTANT_Fieldref            constant_FMIref                    yes
138     CONSTANT_Methodref           constant_FMIref                    yes
139     CONSTANT_InterfaceMethodref  constant_FMIref                    yes
140     CONSTANT_String              unicode                             no
141     CONSTANT_Integer             constant_integer                   yes
142     CONSTANT_Float               constant_float                     yes
143     CONSTANT_Long                constant_long                      yes
144     CONSTANT_Double              constant_double                    yes
145     CONSTANT_NameAndType         constant_nameandtype               yes
146     CONSTANT_Utf8                unicode                             no
147     CONSTANT_Arraydescriptor     constant_arraydescriptor           yes
148     CONSTANT_UNUSED              -
149
150 *******************************************************************************/
151
152 /* data structures of Unicode symbol *******************************************
153
154         All Unicode symbols are stored in one global (hash) table, every symbol
155         exists only once. Equal symbols have identical pointers.
156 */
157
158 struct unicode {
159         unicode   *hashlink;        /* link for external hash chain               */
160         u4         key;             /* hash key (computed from text)              */
161         int        length;          /* text length                                */           
162         u2        *text;            /* pointer to text (each character is 16 Bit) */
163         classinfo *class;           /* class pointer if it exists, otherwise NULL */
164         java_objectheader *string;  /* string pointer if it exists, otherwise NULL*/ 
165 };
166
167
168 /* data structures of remaining constant pool entries *************************/
169
170 typedef struct {            /* Fieldref, Methodref and InterfaceMethodref     */
171         classinfo *class;       /* class containing this field/method/interface   */
172         unicode   *name;        /* field/method/interface name                    */
173         unicode   *descriptor;  /* field/method/interface type descriptor string  */
174 } constant_FMIref;
175
176 typedef struct {            /* Integer                                        */
177         s4 value;
178 } constant_integer;
179         
180 typedef struct {            /* Float                                          */
181         float value;
182 } constant_float;
183
184 typedef struct {            /* Long                                           */
185         s8 value;
186 } constant_long;
187         
188 typedef struct {            /* Double                                         */
189         double value;
190 } constant_double;
191
192 typedef struct {            /* NameAndType (Field or Method)                  */
193         unicode *name;          /* field/method name                              */
194         unicode *descriptor;    /* field/method type descriptor string            */
195 } constant_nameandtype;
196
197 /*  arraydescriptor describes array types. Basic array types contain their
198         type in the arraytype field, objectclass contains a class pointer for
199         arrays of objects (arraytype == ARRAYTYPE_OBJECT), elementdescriptor
200         contains a pointer to an arraydescriptor which describes the element
201         types in the case of arrays of arrays (arraytype == ARRAYTYPE_ARRAY).
202 */
203
204 typedef struct constant_arraydescriptor {
205         int arraytype;
206         classinfo *objectclass;
207         struct constant_arraydescriptor *elementdescriptor;
208 } constant_arraydescriptor;
209
210
211 /* data structures of the runtime system **************************************/
212
213 /* objects *********************************************************************
214
215         All objects (and arrays) which resides on the heap need the following
216         header at the beginning of the data structure.
217 */
218
219 struct java_objectheader {              /* header for all objects             */
220         vftbl *vftbl;                       /* pointer to virtual function table  */
221 };
222
223
224
225 /* arrays **********************************************************************
226
227         All arrays are objects (they need the object header with a pointer to a
228         vvftbl (array class table). There is only one class for all arrays. The
229         type of an array is stored directly in the array object. Following types
230         are defined:
231 */
232
233 #define ARRAYTYPE_INT      0
234 #define ARRAYTYPE_LONG     1
235 #define ARRAYTYPE_FLOAT    2
236 #define ARRAYTYPE_DOUBLE   3
237 #define ARRAYTYPE_BYTE     4
238 #define ARRAYTYPE_CHAR     5
239 #define ARRAYTYPE_SHORT    6
240 #define ARRAYTYPE_BOOLEAN  7
241 #define ARRAYTYPE_OBJECT   8
242 #define ARRAYTYPE_ARRAY    9
243
244 typedef struct java_arrayheader {       /* header for all arrays              */
245         java_objectheader objheader;        /* object header                      */
246         s4 size;                            /* array size                         */
247 #ifdef SIZE_FROM_CLASSINFO
248         s4 alignedsize; /* phil */
249 #endif
250         s4 arraytype;                       /* array type from previous list      */
251 } java_arrayheader;
252
253
254
255 /* structs for all kinds of arrays ********************************************/
256
257 typedef struct java_chararray {
258         java_arrayheader header;
259         u2 data[1];
260 } java_chararray;
261
262 typedef struct java_floatheader {
263         java_arrayheader header;
264         float data[1];
265 } java_floatarray;
266
267 typedef struct java_doublearray {
268         java_arrayheader header;
269         double data[1];
270 } java_doublearray;
271
272 /*  booleanarray and bytearray need identical memory layout (access methods
273     use the same machine code */
274
275 typedef struct java_booleanarray {
276         java_arrayheader header;
277         u1 data[1];
278 } java_booleanarray;
279
280 typedef struct java_bytearray {
281         java_arrayheader header;
282         s1 data[1];
283 } java_bytearray;
284
285 typedef struct java_shortarray {
286         java_arrayheader header;
287         s2 data[1];
288 } java_shortarray;
289
290 typedef struct java_intarray {
291         java_arrayheader header;
292         s4 data[1];
293 } java_intarray;
294
295 typedef struct java_longarray {
296         java_arrayheader header;
297         s8 data[1];
298 } java_longarray;
299
300 /*  objectarray and arrayarray need identical memory layout (access methods
301     use the same machine code */
302
303 typedef struct java_objectarray {
304         java_arrayheader header;
305         classinfo *elementtype;
306         java_objectheader *data[1];
307 } java_objectarray;
308
309 typedef struct java_arrayarray {
310         java_arrayheader header;
311         constant_arraydescriptor *elementdescriptor;
312         java_arrayheader *data[1];
313 } java_arrayarray;
314
315
316 /* field, method and class structures *****************************************/
317
318 /* fieldinfo ******************************************************************/
319
320 typedef struct fieldinfo {/* field of a class                                 */
321         s4       flags;       /* ACC flags                                        */
322         s4       type;        /* basic data type                                  */
323         unicode *name;        /* name of field                                    */
324         unicode *descriptor;  /* JavaVM descriptor string of field                */
325         
326         s4       offset;      /* offset from start of object (instance variables) */
327
328         union {               /* storage for static values (class variables)      */
329                 s4 i; 
330                 s8 l;
331                 float f;
332                 double d;
333                 void *a; 
334         } value;
335
336 } fieldinfo;
337
338
339 /* exceptiontable *************************************************************/
340
341 typedef struct exceptiontable { /* exceptiontable entry in a method           */ 
342         s4         startpc;         /* start pc of guarded area (inclusive)       */
343         s4         endpc;           /* end pc of guarded area (exklusive)         */
344         s4         handlerpc;       /* pc of exception handler                    */
345         classinfo *catchtype;       /* catchtype of exception (NULL == catchall)  */
346 } exceptiontable;
347
348
349 /* methodinfo *****************************************************************/
350
351 typedef struct methodinfo {         /* method structure                       */
352         s4             flags;               /* ACC flags                              */
353         unicode   *name;                /* name of method                         */
354         unicode   *descriptor;          /* JavaVM descriptor string of method     */
355         s4         returntype;          /* only temporary valid, return type      */
356         s4         paramcount;          /* only temporary valid, parameter count  */
357         u1        *paramtypes;          /* only temporary valid, parameter types  */
358         classinfo *class;               /* class, the method belongs to           */
359         s4         vftblindex;          /* index of method in virtual function table
360                                            (if it is a virtual method)            */
361         s4         maxstack;            /* maximum stack depth of method          */
362         s4         maxlocals;           /* maximum number of local variables      */
363         s4         jcodelength;         /* length of JavaVM code                  */
364         u1        *jcode;               /* pointer to JavaVM code                 */
365
366         s4         exceptiontablelength;/* exceptiontable length                  */
367         exceptiontable *exceptiontable; /* the exceptiontable                     */
368
369         u1        *stubroutine;         /* stub for compiling or calling natives  */    
370         s4         mcodelength;         /* legth of generated machine code        */
371         u1        *mcode;               /* pointer to machine code                */
372         u1        *entrypoint;          /* entry point in machine code            */
373
374 } methodinfo;
375
376
377 /* classinfo ******************************************************************/
378
379 struct classinfo {                /* class structure                          */
380         java_objectheader header;     /* classes are also objects                 */
381
382         s4          flags;            /* ACC flags                                */
383         unicode    *name;             /* class name                               */ 
384
385         s4          cpcount;          /* number of entries in constant pool       */
386         u1         *cptags;           /* constant pool tags                       */
387         voidptr    *cpinfos;          /* pointer to constant pool info structures */
388
389         classinfo  *super;            /* super class pointer                      */
390         classinfo  *sub;              /* sub class pointer                        */
391         classinfo  *nextsub;          /* pointer to next class in sub class list  */
392
393         s4          interfacescount;  /* number of interfaces                     */
394         classinfo **interfaces;       /* pointer to interfaces                    */
395
396         s4          fieldscount;      /* number of fields                         */
397         fieldinfo  *fields;           /* field table                              */
398
399         s4          methodscount;     /* number of methods                        */
400         methodinfo *methods;          /* method table                             */
401
402         listnode    listnode;         /* linkage                                  */
403
404         bool        initialized;      /* true, if class already initialised       */ 
405         bool        linked;           /* true, if class already linked            */
406         s4                      index;            /* hierarchy depth (classes) or index
407                                          (interfaces)                             */ 
408         s4          instancesize;     /* size of an instance of this class        */
409 #ifdef SIZE_FROM_CLASSINFO
410         s4          alignedsize;      /* size of an instance, aligned to the 
411                                                                          allocation size on the heap */
412 #endif
413
414         vftbl      *vftbl;            /* pointer to virtual function table        */
415
416         methodinfo *finalizer;        /* finalizer method                         */
417 #ifdef JIT_MARKER_SUPPORT
418         methodinfo *marker; 
419 #endif
420 };
421
422
423 /* virtual function table ******************************************************
424
425         The vtbl has a bidirectional layout with open ends at both sides.
426         interfacetablelength gives the number of entries of the interface table at
427         the start of the vftbl. The vftbl pointer points to &interfacetable[0].
428         vftbllength gives the number of entries of table at the end of the vftbl.
429
430         runtime type check (checkcast):
431
432         Different methods are used for runtime type check depending on the
433         argument of checkcast/instanceof.
434         
435         A check against a class is implemented via relative numbering on the class
436         hierachy tree. The tree is numbered in a depth first traversal setting
437         the base field and the diff field. The diff field gets the result of
438         (high - base) so that a range check can be implemented by an unsigned
439         compare. A sub type test is done by checking the inclusion of base of
440         the sub class in the range of the superclass.
441
442         A check against an interface is implemented via the interfacevftbl. If the
443         interfacevftbl contains a nonnull value a class is a subclass of this
444         interface.
445
446         interfacetable:
447
448         Like standard virtual methods interface methods are called using
449         virtual function tables. All interfaces are numbered sequentially
450         (starting with zero). For each class there exist an interface table
451         of virtual function tables for each implemented interface. The length
452         of the interface table is determined by the highest number of an
453         implemented interface.
454
455         The following example assumes a class which implements interface 0 and 3:
456
457         interfacetablelength = 4
458
459                   | ...       |            +----------+
460                       +-----------+            | method 2 |---> method z
461                       | class     |            | method 1 |---> method y
462                       +-----------+            | method 0 |---> method x
463                       | ivftbl  0 |----------> +----------+
464         vftblptr ---> +-----------+
465                   | ivftbl -1 |--> NULL    +----------+
466                   | ivftbl -2 |--> NULL    | method 1 |---> method x
467                   | ivftbl -3 |-----+      | method 0 |---> method a
468                   +-----------+     +----> +----------+
469      
470                               +---------------+
471                                   | length 3 = 2  |
472                                   | length 2 = 0  |
473                                   | length 1 = 0  |
474                                   | length 0 = 3  |
475         interfacevftbllength ---> +---------------+
476
477 *******************************************************************************/
478
479 struct vftbl {
480         methodptr   *interfacetable[1];    /* interface table (access via macro)  */
481
482         classinfo   *class;                /* class, the vtbl belongs to          */
483
484         s4           vftbllength;          /* virtual function table length       */
485         s4           interfacetablelength; /* interface table length              */
486
487         s4           baseval;              /* base for runtime type check         */
488         s4           diffval;              /* high - base for runtime type check  */
489
490         s4          *interfacevftbllength; /* length of interface vftbls          */
491         
492         methodptr    table[1];             /* class vftbl                         */
493 };
494
495 #define VFTBLINTERFACETABLE(v,i)       (v)->interfacetable[-i]
496
497
498 /* references to some system classes ******************************************/
499
500 extern classinfo *class_java_lang_Object;
501 extern classinfo *class_java_lang_String;
502 extern classinfo *class_java_lang_ClassCastException;
503 extern classinfo *class_java_lang_NullPointerException;
504 extern classinfo *class_java_lang_ArrayIndexOutOfBoundsException;
505 extern classinfo *class_java_lang_NegativeArraySizeException;
506 extern classinfo *class_java_lang_OutOfMemoryError;
507 extern classinfo *class_java_lang_ArithmeticException;
508 extern classinfo *class_java_lang_ArrayStoreException;
509 extern classinfo *class_java_lang_ThreadDeath;
510  
511 extern classinfo *class_array;
512
513
514 /* instances of some system classes *******************************************/
515
516 extern java_objectheader *proto_java_lang_ClassCastException;
517 extern java_objectheader *proto_java_lang_NullPointerException;
518 extern java_objectheader *proto_java_lang_ArrayIndexOutOfBoundsException;
519 extern java_objectheader *proto_java_lang_NegativeArraySizeException;
520 extern java_objectheader *proto_java_lang_OutOfMemoryError;
521 extern java_objectheader *proto_java_lang_ArithmeticException;
522 extern java_objectheader *proto_java_lang_ArrayStoreException;
523 extern java_objectheader *proto_java_lang_ThreadDeath;
524
525
526 /* flag variables *************************************************************/
527
528 extern bool compileall;
529 extern bool runverbose;         
530 extern bool verbose;         
531
532
533 /* statistic variables ********************************************************/
534
535 extern int count_class_infos;
536 extern int count_const_pool_len;
537 extern int count_vftbl_len;
538 extern int count_unicode_len;
539 extern int count_all_methods;
540 extern int count_vmcode_len;
541 extern int count_extable_len;
542
543 #endif
544
545
546 /*
547  * These are local overrides for various environment variables in Emacs.
548  * Please do not remove this and leave it at the end of the file, where
549  * Emacs will automagically detect them.
550  * ---------------------------------------------------------------------
551  * Local variables:
552  * mode: c
553  * indent-tabs-mode: t
554  * c-basic-offset: 4
555  * tab-width: 4
556  * End:
557  */