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