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