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