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