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