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