Save.
[cacao.git] / src / vm / global.h
1 /* vm/global.h - global definitions
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Reinhard Grafl
28             Andreas Krall
29
30    Changes: Mark Probst
31             Philipp Tomsich
32                         Edwin Steiner
33             Joseph Wenninger
34
35    $Id: global.h 1959 2005-02-19 11:46:27Z carolyn $
36
37 */
38
39
40 #ifndef _GLOBAL_H
41 #define _GLOBAL_H
42
43 #include "config.h"
44 #include "types.h"
45
46
47 /* additional data types ******************************************************/
48
49 typedef void *voidptr;                  /* generic pointer                    */
50 typedef void (*functionptr) (void);     /* generic function pointer           */
51
52 typedef int   bool;                     /* boolean data type                  */
53
54 #define true  1
55 #define false 0
56
57
58 /* include some data structures ***********************************************/
59
60 #include "vm/utf8.h"
61
62
63 /* resolve typedef cycles *****************************************************/
64
65 typedef struct java_objectheader java_objectheader; 
66 typedef struct classinfo classinfo; 
67 typedef struct _vftbl vftbl_t;
68 typedef u1* methodptr;
69 typedef struct fieldinfo  fieldinfo; 
70 typedef struct exceptiontable exceptiontable;
71 typedef struct methodinfo methodinfo; 
72 typedef struct lineinfo lineinfo; 
73 typedef struct arraydescriptor arraydescriptor;
74
75
76 /* additional includes ********************************************************/
77
78 #include "toolbox/list.h"
79 #include "vm/jit/inline/sets.h"
80
81
82 /* define some cacao paths ****************************************************/
83
84 #define CACAO_LIBRARY_PATH    "/jre/lib/"ARCH_DIR"/"
85 #define CACAO_RT_JAR_PATH     "/jre/lib/rt.jar"
86 #define CACAO_EXT_DIR         "/jre/lib/ext"
87
88
89 #define STATISTICS          /* if enabled collects program statistics         */
90
91 /* 
92  * SIZE_FROM_CLASSINFO toggles between the bitmap_based and the new method 
93  * of determining the sizes of objects on the heap.
94  */
95 #define SIZE_FROM_CLASSINFO
96
97 /*
98  * CACAO_TYPECHECK activates typechecking (part of bytecode verification)
99  */
100 #define CACAO_TYPECHECK
101
102 /*
103  * TYPECHECK_STACK_COMPCAT activates full checking of computational
104  * categories for stack manipulations (POP,POP2,SWAP,DUP,DUP2,DUP_X1,
105  * DUP2_X1,DUP_X2,DUP2_X2).
106  */
107 #define TYPECHECK_STACK_COMPCAT
108
109 /*
110  * Macros for configuration of the typechecking code
111  *
112  * TYPECHECK_STATISTICS activates gathering statistical information.
113  * TYPEINFO_DEBUG activates debug checks and debug helpers in typeinfo.c
114  * TYPECHECK_DEBUG activates debug checks in typecheck.c
115  * TYPEINFO_DEBUG_TEST activates the typeinfo test at startup.
116  * TYPECHECK_VERBOSE_IMPORTANT activates important debug messages
117  * TYPECHECK_VERBOSE activates all debug messages
118  */
119 #ifdef CACAO_TYPECHECK
120 /*#define TYPECHECK_STATISTICS
121 #define TYPEINFO_DEBUG
122 #define TYPECHECK_DEBUG
123 #define TYPEINFO_DEBUG_TEST
124 #define TYPECHECK_VERBOSE
125 #define TYPECHECK_VERBOSE_IMPORTANT*/
126 #if defined(TYPECHECK_VERBOSE) || defined(TYPECHECK_VERBOSE_IMPORTANT)
127 #define TYPECHECK_VERBOSE_OPT
128 #endif
129 #endif
130
131
132 /* if we have threads disabled this one is not defined ************************/
133
134 #if !defined(USE_THREADS)
135 #define THREADSPECIFIC
136 #endif
137
138
139 /* immediate data union */
140
141 typedef union {
142         s4          i;
143         s8          l;
144         float       f;
145         double      d;
146         void       *a;
147         functionptr fp;
148         u1          b[8];
149 } imm_union;
150
151
152 #define PRIMITIVETYPE_COUNT  9  /* number of primitive types */
153
154 /* CAUTION: Don't change the numerical values! These constants are
155  * used as indices into the primitive type table.
156  */
157 #define PRIMITIVETYPE_INT     0
158 #define PRIMITIVETYPE_LONG    1
159 #define PRIMITIVETYPE_FLOAT   2
160 #define PRIMITIVETYPE_DOUBLE  3
161 #define PRIMITIVETYPE_BYTE    4
162 #define PRIMITIVETYPE_CHAR    5
163 #define PRIMITIVETYPE_SHORT   6
164 #define PRIMITIVETYPE_BOOLEAN 7
165 #define PRIMITIVETYPE_VOID    8
166
167
168 #define MAX_ALIGN 8             /* most generic alignment for JavaVM values   */
169
170
171 /* basic data types ***********************************************************/
172
173 /* CAUTION: jit/jit.h relies on these numerical values! */
174 #define TYPE_INT      0         /* the JavaVM types must numbered in the      */
175 #define TYPE_LONG     1         /* same order as the ICMD_Ixxx to ICMD_Axxx   */
176 #define TYPE_FLOAT    2         /* instructions (LOAD and STORE)              */
177 #define TYPE_DOUBLE   3         /* integer, long, float, double, address      */
178 #define TYPE_ADDRESS  4         /* all other types can be numbered arbitrarly */
179
180 #define TYPE_VOID    10
181
182
183 /* Java class file constants **************************************************/
184
185 #define MAGIC             0xCAFEBABE
186 #define MAJOR_VERSION     48
187 #define MINOR_VERSION     0
188
189 #define CONSTANT_Class                 7
190 #define CONSTANT_Fieldref              9
191 #define CONSTANT_Methodref            10
192 #define CONSTANT_InterfaceMethodref   11
193 #define CONSTANT_String                8
194 #define CONSTANT_Integer               3
195 #define CONSTANT_Float                 4
196 #define CONSTANT_Long                  5
197 #define CONSTANT_Double                6
198 #define CONSTANT_NameAndType          12
199 #define CONSTANT_Utf8                  1
200
201 #define CONSTANT_UNUSED                0
202
203 #define ACC_PUBLIC                0x0001
204 #define ACC_PRIVATE               0x0002
205 #define ACC_PROTECTED             0x0004
206 #define ACC_STATIC                0x0008
207 #define ACC_FINAL                 0x0010
208 #define ACC_SUPER                 0x0020
209 #define ACC_SYNCHRONIZED          0x0020
210 #define ACC_VOLATILE              0x0040
211 #define ACC_TRANSIENT             0x0080
212 #define ACC_NATIVE                0x0100
213 #define ACC_INTERFACE             0x0200
214 #define ACC_ABSTRACT              0x0400
215 #define ACC_STRICT                0x0800
216
217
218 /* constant pool entries *******************************************************
219
220         All constant pool entries need a data structure which contain the entrys
221         value. In some cases this structure exist already, in the remaining cases
222         this structure must be generated:
223
224                 kind                      structure                     generated?
225         ----------------------------------------------------------------------
226     CONSTANT_Class               classinfo                           no
227     CONSTANT_Fieldref            constant_FMIref                    yes
228     CONSTANT_Methodref           constant_FMIref                    yes
229     CONSTANT_InterfaceMethodref  constant_FMIref                    yes
230     CONSTANT_String              unicode                             no
231     CONSTANT_Integer             constant_integer                   yes
232     CONSTANT_Float               constant_float                     yes
233     CONSTANT_Long                constant_long                      yes
234     CONSTANT_Double              constant_double                    yes
235     CONSTANT_NameAndType         constant_nameandtype               yes
236     CONSTANT_Utf8                unicode                             no
237     CONSTANT_UNUSED              -
238
239 *******************************************************************************/
240
241 /* data structure for storing information needed for a stacktrace across native functions*/
242
243 typedef struct native_stackframeinfo native_stackframeinfo;
244
245 struct native_stackframeinfo {
246         void *oldThreadspecificHeadValue;
247         void **addressOfThreadspecificHead;
248         methodinfo *method;
249 #ifdef __ALPHA__
250         void *savedpv;
251 #endif
252         void *beginOfJavaStackframe; /*only used if != 0*/
253         void *returnToFromNative;
254
255 #if 0
256         void *returnFromNative;
257         void *addrReturnFromNative;
258         methodinfo *method;
259         struct native_stackframeinfo *next;
260         struct native_stackframeinfo *prev;
261 #endif
262 };
263
264
265 typedef struct stacktraceelement stacktraceelement;
266
267 struct stacktraceelement {
268 #if POINTERSIZE == 8
269         u8 linenumber;
270 #else
271         u4 linenumber;
272 #endif
273         methodinfo *method;
274 };
275
276
277 typedef struct stackTraceBuffer stackTraceBuffer;
278
279 struct stackTraceBuffer {
280         s4                 needsFree;
281         stacktraceelement *start;
282         s4                 size;
283         s4                 full;
284 };
285
286
287
288 /* data structure for calls from c code to java methods */
289
290 struct jni_callblock {
291         u8 itemtype;
292         u8 item;
293 };
294
295 typedef struct jni_callblock jni_callblock;
296
297
298 /* data structures of remaining constant pool entries *************************/
299
300 typedef struct {            /* Fieldref, Methodref and InterfaceMethodref     */
301         classinfo *class;       /* class containing this field/method/interface   */
302         utf       *name;        /* field/method/interface name                    */
303         utf       *descriptor;  /* field/method/interface type descriptor string  */
304 } constant_FMIref;
305
306
307 typedef struct {            /* Integer                                        */
308         s4 value;
309 } constant_integer;
310
311         
312 typedef struct {            /* Float                                          */
313         float value;
314 } constant_float;
315
316
317 typedef struct {            /* Long                                           */
318         s8 value;
319 } constant_long;
320         
321
322 typedef struct {            /* Double                                         */
323         double value;
324 } constant_double;
325
326
327 typedef struct {            /* NameAndType (Field or Method)                  */
328         utf *name;              /* field/method name                              */
329         utf *descriptor;        /* field/method type descriptor string            */
330 } constant_nameandtype;
331
332
333 /* data structures of the runtime system **************************************/
334
335 /* objects *********************************************************************
336
337         All objects (and arrays) which resides on the heap need the following
338         header at the beginning of the data structure.
339 */
340
341 struct java_objectheader {              /* header for all objects             */
342         vftbl_t *vftbl;                     /* pointer to virtual function table  */
343 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
344         void    *monitorPtr;
345 #endif
346 };
347
348
349
350 /* arrays **********************************************************************
351
352         All arrays are objects (they need the object header with a pointer
353         to a vftbl (array class table). There is one class for each array
354         type. The array type is described by an arraydescriptor struct
355         which is referenced by the vftbl.
356 */
357
358 /* CAUTION: Don't change the numerical values! These constants (with
359  * the exception of ARRAYTYPE_OBJECT) are used as indices in the
360  * primitive type table.
361  */
362 #define ARRAYTYPE_INT      PRIMITIVETYPE_INT
363 #define ARRAYTYPE_LONG     PRIMITIVETYPE_LONG
364 #define ARRAYTYPE_FLOAT    PRIMITIVETYPE_FLOAT
365 #define ARRAYTYPE_DOUBLE   PRIMITIVETYPE_DOUBLE
366 #define ARRAYTYPE_BYTE     PRIMITIVETYPE_BYTE
367 #define ARRAYTYPE_CHAR     PRIMITIVETYPE_CHAR
368 #define ARRAYTYPE_SHORT    PRIMITIVETYPE_SHORT
369 #define ARRAYTYPE_BOOLEAN  PRIMITIVETYPE_BOOLEAN
370 #define ARRAYTYPE_OBJECT   PRIMITIVETYPE_VOID     /* don't use as index! */
371
372 typedef struct java_arrayheader {       /* header for all arrays              */
373         java_objectheader objheader;        /* object header                      */
374         s4 size;                            /* array size                         */
375 #ifdef SIZE_FROM_CLASSINFO
376         s4 alignedsize; /* phil */
377 #endif
378 } java_arrayheader;
379
380
381
382 /* structs for all kinds of arrays ********************************************/
383
384 typedef struct java_chararray {
385         java_arrayheader header;
386         u2 data[1];
387 } java_chararray;
388
389 typedef struct java_floatheader {
390         java_arrayheader header;
391         float data[1];
392 } java_floatarray;
393
394 typedef struct java_doublearray {
395         java_arrayheader header;
396         double data[1];
397 } java_doublearray;
398
399 /*  booleanarray and bytearray need identical memory layout (access methods
400     use the same machine code */
401
402 typedef struct java_booleanarray {
403         java_arrayheader header;
404         u1 data[1];
405 } java_booleanarray;
406
407 typedef struct java_bytearray {
408         java_arrayheader header;
409         s1 data[1];
410 } java_bytearray;
411
412 typedef struct java_shortarray {
413         java_arrayheader header;
414         s2 data[1];
415 } java_shortarray;
416
417 typedef struct java_intarray {
418         java_arrayheader header;
419         s4 data[1];
420 } java_intarray;
421
422 typedef struct java_longarray {
423         java_arrayheader header;
424         s8 data[1];
425 } java_longarray;
426
427 /*  objectarray and arrayarray need identical memory layout (access methods
428     use the same machine code */
429
430 typedef struct java_objectarray {
431         java_arrayheader header;
432         java_objectheader *data[1];
433 } java_objectarray;
434
435
436 /* structure for primitive classes ********************************************/
437
438 typedef struct primitivetypeinfo {
439         classinfo *class_wrap;               /* class for wrapping primitive type */
440         classinfo *class_primitive;          /* primitive class                   */
441         char      *wrapname;                 /* name of class for wrapping        */
442         char      typesig;                   /* one character type signature      */
443         char      *name;                     /* name of primitive class           */
444         char      *arrayname;                /* name of primitive array class     */
445         classinfo *arrayclass;               /* primitive array class             */
446         vftbl_t     *arrayvftbl;             /* vftbl of primitive array class    */
447 } primitivetypeinfo;
448
449
450 /* field, method and class structures *****************************************/
451
452 typedef struct xtafldinfo {
453         bool       fieldChecked;                
454         classinfo *fldClassType;
455         classSet  *XTAclassSet;          /* field class type set                  */
456 } xtafldinfo;
457
458
459 /* fieldinfo ******************************************************************/
460
461 struct fieldinfo {            /* field of a class                                 */
462         s4  flags;            /* ACC flags                                        */
463         s4  type;             /* basic data type                                  */
464         utf *name;            /* name of field                                    */
465         utf *descriptor;      /* JavaVM descriptor string of field                */
466         
467         s4  offset;           /* offset from start of object (instance variables) */
468
469         imm_union value;      /* storage for static values (class variables)      */
470
471         classinfo *class;     /* needed by typechecker. Could be optimized        */
472                               /* away by using constant_FMIref instead of         */
473                               /* fieldinfo throughout the compiler.               */
474         
475         xtafldinfo *xta;
476 };
477
478
479 /* exceptiontable *************************************************************/
480
481 struct exceptiontable {         /* exceptiontable entry in a method           */
482         s4              startpc;    /* start pc of guarded area (inclusive)       */
483         struct basicblock *start;
484
485         s4              endpc;      /* end pc of guarded area (exklusive)         */
486         struct basicblock *end;
487
488         s4              handlerpc;  /* pc of exception handler                    */
489         struct basicblock *handler;
490
491         classinfo      *catchtype;  /* catchtype of exception (NULL == catchall)  */
492         exceptiontable *next;       /* used to build a list of exception when     */
493                                     /* loops are copied */
494         exceptiontable *down;       /* instead of the old array, a list is used   */
495 };
496
497
498 /* methodinfo  static info ****************************************************/
499
500 typedef struct xtainfo {
501         s4          XTAmethodUsed;     /* XTA if used in callgraph - not used /used */
502         classSet    *XTAclassSet;      /* method class type set                 */ 
503         /*classSet      *PartClassSet */   /* method class type set                 */ 
504
505         classSetNode    *paramClassSet; /* cone set of methods parameters       */
506         
507         /* Needed for interative checking */
508         methSet         *calls;            /* Edges - methods this method calls                 */ 
509         methSet         *calledBy;         /* Edges - methods that call this method         */ 
510         methSet         *marked;  /*not in Dez*/         /* methods that marked by this method    */ 
511         methSet         *markedBy;
512         fldSet          *fldsUsed;         /* fields used by this method             */ 
513         /*methSetNode  *interfaceCalls*/   /* methods this method calls as interface */ 
514         bool           chgdSinceLastParse; /* Changed since last parse ?          */
515 } xtainfo; 
516
517
518 /* lineinfo *****************************************************************/
519
520 struct lineinfo {
521         u2 start_pc;
522         u2 line_number;
523 };
524
525
526 /* methodinfo *****************************************************************/
527
528 struct methodinfo {                 /* method structure                       */
529         java_objectheader header;       /* we need this in jit's monitorenter     */
530         s4          flags;              /* ACC flags                              */
531         utf        *name;               /* name of method                         */
532         utf        *descriptor;         /* JavaVM descriptor string of method     */
533         s4          returntype;         /* only temporary valid, return type      */
534         classinfo  *returnclass;        /* pointer to classinfo for the rtn type  */ /*XTA*/ 
535         s4          paramcount;         /* only temporary valid, parameter count  */
536         u1         *paramtypes;         /* only temporary valid, parameter types  */
537         classinfo **paramclass;         /* pointer to classinfo for a parameter   */ /*XTA*/
538
539         bool        isleafmethod;       /* does method call subroutines           */
540
541         classinfo  *class;              /* class, the method belongs to           */
542         s4          vftblindex;         /* index of method in virtual function    */
543                                         /* table (if it is a virtual method)      */
544         s4          maxstack;           /* maximum stack depth of method          */
545         s4          maxlocals;          /* maximum number of local variables      */
546         s4          jcodelength;        /* length of JavaVM code                  */
547         u1         *jcode;              /* pointer to JavaVM code                 */
548
549         s4          basicblockcount;    /* number of basic blocks                 */
550         struct basicblock *basicblocks; /* points to basic block array            */
551         s4         *basicblockindex;    /* a table which contains for every byte  */
552                                         /* of JavaVM code a basic block index if  */
553                                         /* at this byte is the start of a basic   */
554                                         /* block                                  */
555
556         s4          instructioncount;   /* number of JavaVM instructions          */
557         struct instruction *instructions; /* points to intermediate code instructions */
558
559         s4          stackcount;         /* number of stack elements               */
560         struct stackelement *stack;     /* points to intermediate code instructions */
561
562         s4          exceptiontablelength;/* exceptiontable length                 */
563         exceptiontable *exceptiontable; /* the exceptiontable                     */
564
565         u2          thrownexceptionscount;/* number of exceptions attribute       */
566         classinfo **thrownexceptions;   /* checked exceptions a method may throw  */
567
568         u2          linenumbercount;    /* number of linenumber attributes        */
569         lineinfo   *linenumbers;        /* array of lineinfo items                */
570
571         int       c_debug_nr;           /* a counter to number all BB with an     */
572                                         /* unique value                           */
573
574         u1         *stubroutine;        /* stub for compiling or calling natives  */
575         s4          mcodelength;        /* legth of generated machine code        */
576         functionptr mcode;              /* pointer to machine code                */
577         functionptr entrypoint;         /* entry point in machine code            */
578
579         /*rtainfo   rta;*/
580         xtainfo    *xta;
581
582         s4          methodUsed;         /* marked (might be used later) /not used /used */
583         s4          monoPoly;           /* call is mono or poly or unknown        */ /*RT stats */
584         /* should # method def'd and used be kept after static parse (will it be used?) */
585         s4              subRedefs;
586         s4              subRedefsUsed;
587         s4              nativelyoverloaded; /* used in header.c and only valid there  */
588 };
589
590
591 /* innerclassinfo *************************************************************/
592
593 typedef struct innerclassinfo {
594         classinfo *inner_class;       /* inner class pointer                      */
595         classinfo *outer_class;       /* outer class pointer                      */
596         utf       *name;              /* innerclass name                          */
597         s4         flags;             /* ACC flags                                */
598 } innerclassinfo;
599
600
601 /* classinfo ******************************************************************/
602
603 struct classinfo {                /* class structure                          */
604         java_objectheader header;     /* classes are also objects                 */
605         java_objectarray* signers;
606         struct java_security_ProtectionDomain* pd;
607         struct java_lang_VMClass* vmClass;
608         struct java_lang_reflect_Constructor* constructor;
609
610         s4 initializing_thread;       /* gnu classpath                            */
611         s4 erroneous_state;           /* gnu classpath                            */
612         struct gnu_classpath_RawData* vmData; /* gnu classpath                    */
613
614         s4          flags;            /* ACC flags                                */
615         utf        *name;             /* class name                               */
616
617         s4          cpcount;          /* number of entries in constant pool       */
618         u1         *cptags;           /* constant pool tags                       */
619         voidptr    *cpinfos;          /* pointer to constant pool info structures */
620
621         classinfo  *super;            /* super class pointer                      */
622         classinfo  *sub;              /* sub class pointer                        */
623         classinfo  *nextsub;          /* pointer to next class in sub class list  */
624
625         s4          interfacescount;  /* number of interfaces                     */
626         classinfo **interfaces;       /* pointer to interfaces                    */
627
628         s4          fieldscount;      /* number of fields                         */
629         fieldinfo  *fields;           /* field table                              */
630
631         s4          methodscount;     /* number of methods                        */
632         methodinfo *methods;          /* method table                             */
633
634         listnode    listnode;         /* linkage                                  */
635
636         bool        initialized;      /* true, if class already initialized       */
637         bool        initializing;     /* flag for the compiler                    */
638         bool        loaded;           /* true, if class already loaded            */
639         bool        linked;           /* true, if class already linked            */
640         s4          index;            /* hierarchy depth (classes) or index       */
641                                       /* (interfaces)                             */
642         s4          instancesize;     /* size of an instance of this class        */
643 #ifdef SIZE_FROM_CLASSINFO
644         s4          alignedsize;      /* size of an instance, aligned to the      */
645                                       /* allocation size on the heap              */
646 #endif
647
648         vftbl_t    *vftbl;            /* pointer to virtual function table        */
649
650         methodinfo *finalizer;        /* finalizer method                         */
651
652     u2          innerclasscount;  /* number of inner classes                  */
653     innerclassinfo *innerclass;
654
655     classinfo  *hashlink;         /* link for external hash chain             */
656         bool        classvftbl;       /* has its own copy of the Class vtbl       */
657
658         s4          classUsed;        /* 0= not used 1 = used   CO-RT             */
659
660         classSetNode *impldBy;        /* implemented by class set                 */
661         utf        *packagename;      /* full name of the package                 */
662         utf        *sourcefile;       /* classfile name containing this class     */
663         java_objectheader *classloader; /* NULL for bootstrap classloader         */
664 };
665
666 /* check if class is an array class. Only use for linked classes! */
667 #define CLASS_IS_ARRAY(clsinfo)  ((clsinfo)->vftbl->arraydesc != NULL)
668
669
670 /* virtual function table ******************************************************
671
672         The vtbl has a bidirectional layout with open ends at both sides.
673         interfacetablelength gives the number of entries of the interface table at
674         the start of the vftbl. The vftbl pointer points to &interfacetable[0].
675         vftbllength gives the number of entries of table at the end of the vftbl.
676
677         runtime type check (checkcast):
678
679         Different methods are used for runtime type check depending on the
680         argument of checkcast/instanceof.
681         
682         A check against a class is implemented via relative numbering on the class
683         hierachy tree. The tree is numbered in a depth first traversal setting
684         the base field and the diff field. The diff field gets the result of
685         (high - base) so that a range check can be implemented by an unsigned
686         compare. A sub type test is done by checking the inclusion of base of
687         the sub class in the range of the superclass.
688
689         A check against an interface is implemented via the interfacevftbl. If the
690         interfacevftbl contains a nonnull value a class is a subclass of this
691         interface.
692
693         interfacetable:
694
695         Like standard virtual methods interface methods are called using
696         virtual function tables. All interfaces are numbered sequentially
697         (starting with zero). For each class there exist an interface table
698         of virtual function tables for each implemented interface. The length
699         of the interface table is determined by the highest number of an
700         implemented interface.
701
702         The following example assumes a class which implements interface 0 and 3:
703
704         interfacetablelength = 4
705
706                   | ...       |            +----------+
707                       +-----------+            | method 2 |---> method z
708                       | class     |            | method 1 |---> method y
709                       +-----------+            | method 0 |---> method x
710                       | ivftbl  0 |----------> +----------+
711         vftblptr ---> +-----------+
712                   | ivftbl -1 |--> NULL    +----------+
713                   | ivftbl -2 |--> NULL    | method 1 |---> method x
714                   | ivftbl -3 |-----+      | method 0 |---> method a
715                   +-----------+     +----> +----------+
716      
717                               +---------------+
718                                   | length 3 = 2  |
719                                   | length 2 = 0  |
720                                   | length 1 = 0  |
721                                   | length 0 = 3  |
722         interfacevftbllength ---> +---------------+
723
724 *******************************************************************************/
725
726 struct _vftbl {
727         methodptr   *interfacetable[1];    /* interface table (access via macro)  */
728
729         classinfo   *class;                /* class, the vtbl belongs to          */
730
731         arraydescriptor *arraydesc;        /* for array classes, otherwise NULL   */
732
733         s4           vftbllength;          /* virtual function table length       */
734         s4           interfacetablelength; /* interface table length              */
735
736         s4           baseval;              /* base for runtime type check         */
737                                            /* (-index for interfaces)             */
738         s4           diffval;              /* high - base for runtime type check  */
739
740         s4          *interfacevftbllength; /* length of interface vftbls          */
741         
742         methodptr    table[1];             /* class vftbl                         */
743 };
744
745 #define VFTBLINTERFACETABLE(v,i)       (v)->interfacetable[-i]
746
747
748 /* arraydescriptor ************************************************************
749
750     For every array class an arraydescriptor is allocated which
751     describes the array class.
752         The arraydescriptor is referenced from the vftbl of the array
753         class.
754
755 *******************************************************************************/
756
757 struct arraydescriptor {
758         vftbl_t *componentvftbl; /* vftbl of the component type, NULL for primit. */
759         vftbl_t *elementvftbl;   /* vftbl of the element type, NULL for primitive */
760         s2       arraytype;      /* ARRAYTYPE_* constant                          */
761         s2       dimension;      /* dimension of the array (always >= 1)          */
762         s4       dataoffset;     /* offset of the array data from object pointer  */
763         s4       componentsize;  /* size of a component in bytes                  */
764         s2       elementtype;    /* ARRAYTYPE_* constant                          */
765 };
766
767
768 /* flag variables *************************************************************/
769
770 extern bool cacao_initializing;
771
772 #ifdef TYPECHECK_VERBOSE_OPT
773 extern bool typecheckverbose;
774 #endif
775
776 /*extern int pClassHeir;*/
777 /*extern int pCallgraph;*/
778 /*extern int pOpcodes;*/
779 /*extern int pStats;*/
780
781 /*extern void RT_jit_parse(methodinfo *m);*/
782
783
784 /* table of primitive types ***************************************************/
785
786 /* This array can be indexed by the PRIMITIVETYPE_ and ARRAYTYPE_
787  * constants (except ARRAYTYPE_OBJECT).
788  */
789 extern primitivetypeinfo primitivetype_table[PRIMITIVETYPE_COUNT];
790
791
792 /* macros for descriptor parsing **********************************************/
793
794 /* SKIP_FIELDDESCRIPTOR:
795  * utf_ptr must point to the first character of a field descriptor.
796  * After the macro call utf_ptr points to the first character after
797  * the field descriptor.
798  *
799  * CAUTION: This macro does not check for an unexpected end of the
800  * descriptor. Better use SKIP_FIELDDESCRIPTOR_SAFE.
801  */
802 #define SKIP_FIELDDESCRIPTOR(utf_ptr)                                                   \
803         do { while (*(utf_ptr)=='[') (utf_ptr)++;                                       \
804                 if (*(utf_ptr)++=='L')                                                                  \
805                         while(*(utf_ptr)++ != ';') /* skip */; } while(0)
806
807 /* SKIP_FIELDDESCRIPTOR_SAFE:
808  * utf_ptr must point to the first character of a field descriptor.
809  * After the macro call utf_ptr points to the first character after
810  * the field descriptor.
811  *
812  * Input:
813  *     utf_ptr....points to first char of descriptor
814  *     end_ptr....points to first char after the end of the string
815  *     errorflag..must be initialized (to false) by the caller!
816  * Output:
817  *     utf_ptr....points to first char after the descriptor
818  *     errorflag..set to true if the string ended unexpectedly
819  */
820 #define SKIP_FIELDDESCRIPTOR_SAFE(utf_ptr,end_ptr,errorflag)                    \
821         do { while ((utf_ptr) != (end_ptr) && *(utf_ptr)=='[') (utf_ptr)++;     \
822                 if ((utf_ptr) == (end_ptr))                                                                             \
823                         (errorflag) = true;                                                                                     \
824                 else                                                                                                                    \
825                         if (*(utf_ptr)++=='L') {                                                                        \
826                                 while((utf_ptr) != (end_ptr) && *(utf_ptr)++ != ';')    \
827                                         /* skip */;                                                                                     \
828                                 if ((utf_ptr)[-1] != ';')                                                               \
829                                         (errorflag) = true; }} while(0)
830
831
832 /* Synchronization ************************************************************/
833
834 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
835 void cast_lock();
836 void cast_unlock();
837 void compiler_lock();
838 void compiler_unlock();
839 #endif
840
841
842 /**** Methods: called directly by cacao, which defines the callpath ***/
843 #define MAINCLASS mainstring
844 #define MAINMETH "main"
845 #define MAINDESC "([Ljava/lang/String;)V"
846
847 #define EXITCLASS "java/lang/System"
848 #define EXITMETH  "exit"
849 #define EXITDESC  "(I)V"
850
851 #if defined(USE_THREADS)
852  #define THREADCLASS "java/lang/Thread"
853  #define THREADMETH  "<init>"
854  #define THREADDESC  "(Ljava/lang/VMThread;Ljava/lang/String;IZ)V"
855
856  #define THREADGROUPCLASS "java/lang/ThreadGroup"
857  #define THREADGROUPMETH  "addThread"
858  #define THREADGROUPDESC  "(Ljava/lang/Thread;)V"
859 #endif
860
861 #endif /* _GLOBAL_H */
862
863
864 /*
865  * These are local overrides for various environment variables in Emacs.
866  * Please do not remove this and leave it at the end of the file, where
867  * Emacs will automagically detect them.
868  * ---------------------------------------------------------------------
869  * Local variables:
870  * mode: c
871  * indent-tabs-mode: t
872  * c-basic-offset: 4
873  * tab-width: 4
874  * End:
875  */