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