* Removed all Id tags.
[cacao.git] / src / vm / jit / verify / typeinfo.h
1 /* src/vm/jit/verify/typeinfo.h - type system used by the type checker
2
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25 */
26
27 #ifndef _TYPEINFO_H
28 #define _TYPEINFO_H
29
30 /* resolve typedef cycles *****************************************************/
31
32 typedef struct typeinfo typeinfo;
33 typedef struct typeinfo_mergedlist typeinfo_mergedlist;
34 typedef struct typedescriptor typedescriptor;
35
36 #include "config.h"
37 #include "vm/types.h"
38
39 #include "vm/global.h"
40
41 #include "vmcore/references.h"
42
43
44 /* configuration **************************************************************/
45
46 /*
47  * TYPECHECK_STATISTICS activates gathering statistical information.
48  * TYPEINFO_DEBUG activates debug checks and debug helpers in typeinfo.c
49  * TYPECHECK_DEBUG activates debug checks in typecheck.c
50  * TYPEINFO_DEBUG_TEST activates the typeinfo test at startup.
51  * TYPECHECK_VERBOSE_IMPORTANT activates important debug messages
52  * TYPECHECK_VERBOSE activates all debug messages
53  * TYPEINFO_VERBOSE activates debug prints in typeinfo.c
54  */
55 #ifdef ENABLE_VERIFIER
56 #ifndef NDEBUG
57 /*#define TYPECHECK_STATISTICS*/
58 #define TYPEINFO_DEBUG
59 /*#define TYPEINFO_VERBOSE*/
60 #define TYPECHECK_DEBUG
61 /*#define TYPEINFO_DEBUG_TEST*/
62 /*#define TYPECHECK_VERBOSE*/
63 /*#define TYPECHECK_VERBOSE_IMPORTANT*/
64 #if defined(TYPECHECK_VERBOSE) || defined(TYPECHECK_VERBOSE_IMPORTANT)
65 #define TYPECHECK_VERBOSE_OPT
66 #endif
67 #endif
68 #endif
69
70 #ifdef TYPECHECK_VERBOSE_OPT
71 extern bool opt_typecheckverbose;
72 #endif
73
74 /* types **********************************************************************/
75
76 /* typecheck_result - return type for boolean and tristate  functions     */
77 /*                    which may also throw exceptions (typecheck_FAIL).   */
78
79 /* NOTE: Use the enum values, not the uppercase #define macros!          */
80 #define TYPECHECK_MAYBE  0x02
81 #define TYPECHECK_FAIL   0x04
82
83 typedef enum {
84         typecheck_FALSE = false,
85         typecheck_TRUE  = true,
86         typecheck_MAYBE = TYPECHECK_MAYBE,
87         typecheck_FAIL  = TYPECHECK_FAIL
88 } typecheck_result;
89
90 /* check that typecheck_MAYBE is not ambiguous */
91 #if TYPECHECK_MAYBE == true
92 #error "`typecheck_MAYBE` must not be the same as `true`"
93 #endif
94 #if TYPECHECK_MAYBE == false
95 #error "`typecheck_MAYBE` must not be the same as `false`"
96 #endif
97
98 /* check that typecheck_FAIL is not ambiguous */
99 #if (true & TYPECHECK_FAIL) != 0
100 #error "`true` must not have bit 0x02 set (conflicts with typecheck_FAIL)"
101 #endif
102
103 /* data structures for the type system ****************************************/
104
105 /* The typeinfo structure stores detailed information on address types.
106  * (stack elements, variables, etc. with type == TYPE_ADR.)
107  *
108  * There are two kinds of address types which can be distinguished by
109  * the value of the typeclass field:
110  *
111  * 1) typeclass == NULL: returnAddress type
112  *                       use TYPEINFO_IS_PRIMITIVE to test for this
113  *
114  * 2) typeclass != NULL: reference type
115  *                       use TYPEINFO_IS_REFERENCE to test for this
116  *
117  * Note: For non-address types either there is no typeinfo allocated
118  * or the fields of the typeinfo struct contain undefined values!
119  * DO NOT access the typeinfo for non-address types!
120  *
121  * CAUTION: The typeinfo structure should be considered opaque outside of
122  *          typeinfo.[ch]. Please use the macros and functions defined here to
123  *          access typeinfo structures!
124  */
125
126 /* At all times *exactly one* of the following conditions is true for
127  * a particular typeinfo struct:
128  *
129  * A) typeclass == NULL
130  *
131  *        This is a returnAddress type.
132  *
133  *        Use TYPEINFO_IS_PRIMITIVE to check for this.
134  *        Use TYPEINFO_RETURNADDRESS to access the pointer in elementclass.
135  *        Don't access other fields of the struct.
136  *
137  * B) typeclass == pseudo_class_Null
138  *
139  *        This is the null-reference type. 
140  *        Use TYPEINFO_IS_NULLTYPE to check for this.
141  *        Don't access other fields of the struct.
142  *
143  * C) typeclass == pseudo_class_New
144  *
145  *        This is an 'uninitialized object' type. elementclass can be
146  *        cast to instruction* and points to the NEW instruction
147  *        responsible for creating this type.
148  *
149  *        Use TYPEINFO_NEWOBJECT_INSTRUCTION to access the pointer in
150  *        elementclass.
151  *        Don't access other fields of the struct.
152  *
153  * D) typeclass == pseudo_class_Arraystub
154  *
155  *        This type is used to represent the result of merging array types
156  *        with incompatible component types. An arraystub allows no access
157  *        to its components (since their type is undefined), but it allows
158  *        operations which act directly on an arbitrary array type (such as
159  *        requesting the array size).
160  *
161  *        NOTE: An array stub does *not* count as an array. It has dimension
162  *              zero.
163  *
164  *        Otherwise like a normal class reference type.
165  *        Don't access other fields of the struct.
166  *
167  * E) typeclass is an array class
168  *
169  *        An array reference.
170  *            elementclass...typeclass of the element type
171  *            dimension......dimension of the array (>=1)
172  *            elementtype....element type (ARRAYTYPE_...)
173  *            merged.........mergedlist of the element type
174  *
175  *        Use TYPEINFO_IS_ARRAY to check for this case.
176  *
177  *        The elementclass may be one of the following:
178  *        1) pseudo_class_Arraystub
179  *        2) an unresolved type
180  *        3) a loaded interface
181  *        4) a loaded (non-pseudo-,non-array-)class != (BOOTSTRAP)java.lang.Object
182  *                Note: `merged` may be used
183  *        5) (BOOTSTRAP)java.lang.Object
184  *                Note: `merged` may be used
185  *
186  *        For the semantics of the merged field in cases 4) and 5) consult the 
187  *        corresponding descriptions with `elementclass` replaced by `typeclass`.
188  *
189  * F) typeclass is an unresolved type (a symbolic class/interface reference)
190  *
191  *        The type has not been resolved yet. (Meaning it corresponds to an
192  *        unloaded class or interface).
193  *        Don't access other fields of the struct.
194  *
195  * G) typeclass is a loaded interface
196  *
197  *        An interface reference type.
198  *        Don't access other fields of the struct.
199  *
200  * H) typeclass is a loaded (non-pseudo-,non-array-)class != (BOOTSTRAP)java.lang.Object
201  *
202  *        A loaded class type.
203  *        All classref_or_classinfos in u.merged.list (if any) are
204  *        loaded subclasses of typeclass (no interfaces, array classes, or
205  *        unresolved types).
206  *        Don't access other fields of the struct.
207  *
208  * I) typeclass is (BOOTSTRAP)java.lang.Object
209  *
210  *        The most general kind of reference type.
211  *        In this case u.merged.count and u.merged.list
212  *        are valid and may be non-zero.
213  *        The classref_or_classinfos in u.merged.list (if any) may be
214  *        classes, interfaces, pseudo classes or unresolved types.
215  *        Don't access other fields of the struct.
216  */
217
218 /* The following algorithm is used to determine if the type described
219  * by this typeinfo struct supports the interface X:  * XXX add MAYBE *
220  *
221  *     1) If typeclass is X or a subinterface of X the answer is "yes".
222  *     2) If typeclass is a (pseudo) class implementing X the answer is "yes".
223  *     3) If typeclass is not an array and u.merged.count>0
224  *        and all classes/interfaces in u.merged.list implement X
225  *        the answer is "yes".
226  *     4) If none of the above is true the answer is "no".
227  */
228
229 /*
230  * CAUTION: The typeinfo structure should be considered opaque outside of
231  *          typeinfo.[ch]. Please use the macros and functions defined here to
232  *          access typeinfo structures!
233  */
234 struct typeinfo {
235         classref_or_classinfo  typeclass;
236         classref_or_classinfo  elementclass; /* valid if dimension>0 */ /* various uses! */
237         typeinfo_mergedlist   *merged;
238         u1                     dimension;
239         u1                     elementtype;  /* valid if dimension>0           */
240 };
241
242 struct typeinfo_mergedlist {
243         s4                    count;
244         classref_or_classinfo list[1];       /* variable length!                        */
245 };
246
247 /* a type descriptor stores a basic type and the typeinfo                */
248 /* this is used for storing the type of a local variable, and for        */
249 /* storing types in the signature of a method                            */
250
251 struct typedescriptor {
252         typeinfo        typeinfo; /* valid if type == TYPE_ADR               */
253         u1              type;     /* basic type (TYPE_INT, ...)              */
254 };
255
256 /****************************************************************************/
257 /* MACROS                                                                   */
258 /****************************************************************************/
259
260 /* NOTE: The TYPEINFO macros take typeinfo *structs*, not pointers as
261  *       arguments.  You have to dereference any pointers.
262  */
263
264 /* typevectors **************************************************************/
265
266 #define TYPEVECTOR_SIZE(size)                                           \
267     ((size) * sizeof(varinfo)) 
268
269 #define DNEW_TYPEVECTOR(size)                                           \
270     ((varinfo*)dump_alloc(TYPEVECTOR_SIZE(size)))
271
272 #define DMNEW_TYPEVECTOR(num,size)                                              \
273     ((void*)dump_alloc((num) * TYPEVECTOR_SIZE(size)))
274
275 #define MGET_TYPEVECTOR(array,index,size) \
276     ((varinfo*) (((u1*)(array)) + TYPEVECTOR_SIZE(size) * (index)))
277
278 /* internally used macros ***************************************************/
279
280 /* internal, don't use this explicitly! */
281 #define TYPEINFO_ALLOCMERGED(mergedlist,count)                  \
282     do {(mergedlist) = (typeinfo_mergedlist*)dump_alloc(        \
283             sizeof(typeinfo_mergedlist)                         \
284             + ((count)-1)*sizeof(classinfo*));} while(0)
285
286 /* internal, don't use this explicitly! */
287 #define TYPEINFO_FREEMERGED(mergedlist)
288
289 /* internal, don't use this explicitly! */
290 #define TYPEINFO_FREEMERGED_IF_ANY(mergedlist)
291
292 /* macros for type queries **************************************************/
293
294 #define TYPEINFO_IS_PRIMITIVE(info)                             \
295             ((info).typeclass.any == NULL)
296
297 #define TYPEINFO_IS_REFERENCE(info)                             \
298             ((info).typeclass.any != NULL)
299
300 #define TYPEINFO_IS_NULLTYPE(info)                              \
301             ((info).typeclass.cls == pseudo_class_Null)
302
303 #define TYPEINFO_IS_NEWOBJECT(info)                             \
304             ((info).typeclass.cls == pseudo_class_New)
305
306 #define TYPEINFO_IS_JAVA_LANG_CLASS(info)                       \
307             ((info).typeclass.cls == class_java_lang_Class)
308
309 /* only use this if TYPEINFO_IS_PRIMITIVE returned true! */
310 #define TYPEINFO_RETURNADDRESS(info)                            \
311             ((info).elementclass.any)
312
313 /* only use this if TYPEINFO_IS_NEWOBJECT returned true! */
314 #define TYPEINFO_NEWOBJECT_INSTRUCTION(info)                    \
315                 ((info).elementclass.any)
316
317 /* only use this if TYPEINFO_IS_JAVA_LANG_CLASS returned true! */
318 #define TYPEINFO_JAVA_LANG_CLASS_CLASSREF(info)                 \
319                 ((info).elementclass.ref)
320
321 /* macros for array type queries ********************************************/
322
323 #define TYPEINFO_IS_ARRAY(info)                                 \
324             ( TYPEINFO_IS_REFERENCE(info)                       \
325               && ((info).dimension != 0) )
326
327 #define TYPEINFO_IS_SIMPLE_ARRAY(info)                          \
328             ( ((info).dimension == 1) )
329
330 #define TYPEINFO_IS_ARRAY_ARRAY(info)                           \
331             ( ((info).dimension >= 2) )
332
333 #define TYPEINFO_IS_PRIMITIVE_ARRAY(info,arraytype)             \
334             ( TYPEINFO_IS_SIMPLE_ARRAY(info)                    \
335               && ((info).elementtype == (arraytype)) )
336
337 #define TYPEINFO_IS_OBJECT_ARRAY(info)                          \
338             ( TYPEINFO_IS_SIMPLE_ARRAY(info)                    \
339               && ((info).elementclass.any != NULL) )
340
341 /* assumes that info describes an array type */
342 #define TYPEINFO_IS_ARRAY_OF_REFS_NOCHECK(info)                 \
343             ( ((info).elementclass.any != NULL)                 \
344               || ((info).dimension >= 2) )
345
346 #define TYPEINFO_IS_ARRAY_OF_REFS(info)                         \
347             ( TYPEINFO_IS_ARRAY(info)                           \
348               && TYPEINFO_IS_ARRAY_OF_REFS_NOCHECK(info) )
349
350 #define TYPE_IS_RETURNADDRESS(type,info)                        \
351             ( ((type)==TYPE_RET)                                \
352               && TYPEINFO_IS_PRIMITIVE(info) )
353
354 #define TYPE_IS_REFERENCE(type,info)                            \
355             ( ((type)==TYPE_ADR)                                \
356               && !TYPEINFO_IS_PRIMITIVE(info) )
357
358 #define TYPEDESC_IS_RETURNADDRESS(td)                           \
359             TYPE_IS_RETURNADDRESS((td).type,(td).typeinfo)
360
361 #define TYPEDESC_IS_REFERENCE(td)                               \
362             TYPE_IS_REFERENCE((td).type,(td).typeinfo)
363
364 /* queries allowing the null type ********************************************/
365
366 #define TYPEINFO_MAYBE_ARRAY(info)                              \
367     (TYPEINFO_IS_ARRAY(info) || TYPEINFO_IS_NULLTYPE(info))
368
369 #define TYPEINFO_MAYBE_PRIMITIVE_ARRAY(info,at)                 \
370     (TYPEINFO_IS_PRIMITIVE_ARRAY(info,at) || TYPEINFO_IS_NULLTYPE(info))
371
372 #define TYPEINFO_MAYBE_ARRAY_OF_REFS(info)                      \
373     (TYPEINFO_IS_ARRAY_OF_REFS(info) || TYPEINFO_IS_NULLTYPE(info))
374
375 /* macros for initializing typeinfo structures ******************************/
376
377 #define TYPEINFO_INIT_PRIMITIVE(info)                           \
378          do {(info).typeclass.any = NULL;                       \
379              (info).elementclass.any = NULL;                    \
380              (info).merged = NULL;                              \
381              (info).dimension = 0;                              \
382              (info).elementtype = 0;} while(0)
383
384 #define TYPEINFO_INIT_RETURNADDRESS(info,adr)                   \
385          do {(info).typeclass.any = NULL;                       \
386              (info).elementclass.any = (adr);                   \
387              (info).merged = NULL;                              \
388              (info).dimension = 0;                              \
389              (info).elementtype = 0;} while(0)
390
391 #define TYPEINFO_INIT_NON_ARRAY_CLASSINFO(info,cinfo)   \
392          do {(info).typeclass.cls = (cinfo);            \
393              (info).elementclass.any = NULL;            \
394              (info).merged = NULL;                      \
395              (info).dimension = 0;                      \
396              (info).elementtype = 0;} while(0)
397
398 #define TYPEINFO_INIT_JAVA_LANG_CLASS(info,c)                   \
399          do {(info).typeclass.any = class_java_lang_Class;      \
400              (info).elementclass = (c);                         \
401              (info).merged = NULL;                              \
402              (info).dimension = 0;                              \
403              (info).elementtype = 0;} while(0)
404
405 #define TYPEINFO_INIT_NULLTYPE(info)                            \
406             TYPEINFO_INIT_NON_ARRAY_CLASSINFO(info,pseudo_class_Null)
407
408 #define TYPEINFO_INIT_NEWOBJECT(info,instr)             \
409          do {(info).typeclass.cls = pseudo_class_New;   \
410              (info).elementclass.any = (instr);         \
411              (info).merged = NULL;                      \
412              (info).dimension = 0;                      \
413              (info).elementtype = 0;} while(0)
414
415 #define TYPEINFO_INIT_PRIMITIVE_ARRAY(info,arraytype)                   \
416     typeinfo_init_classinfo(&(info),primitivetype_table[arraytype].arrayclass);
417
418 /* macros for copying types (destinition is not checked or freed) ***********/
419
420 /* TYPEINFO_COPY makes a shallow copy, the merged pointer is simply copied. */
421 #define TYPEINFO_COPY(src,dst)                                  \
422     do {(dst) = (src);} while(0)
423
424 /* TYPEINFO_CLONE makes a deep copy, the merged list (if any) is duplicated
425  * into a newly allocated array.
426  */
427 #define TYPEINFO_CLONE(src,dst)                                 \
428     do {(dst) = (src);                                          \
429         if ((dst).merged) typeinfo_clone(&(src),&(dst));} while(0)
430
431 /****************************************************************************/
432 /* FUNCTIONS                                                                */
433 /****************************************************************************/
434
435 /* typevector functions *****************************************************/
436
437 /* element read-only access */
438 bool typevector_checktype(varinfo *set,int index,int type);
439 bool typevector_checkreference(varinfo *set,int index);
440 bool typevector_checkretaddr(varinfo *set,int index);
441
442 /* element write access */
443 void typevector_store(varinfo *set,int index,int type,typeinfo *info);
444 void typevector_store_retaddr(varinfo *set,int index,typeinfo *info);
445 bool typevector_init_object(varinfo *set,void *ins,classref_or_classinfo initclass,int size);
446
447 /* vector functions */
448 varinfo *typevector_copy(varinfo *src,int size);
449 void typevector_copy_inplace(varinfo *src,varinfo *dst,int size);
450 typecheck_result typevector_merge(methodinfo *m,varinfo *dst,varinfo *y,int size);
451
452 /* inquiry functions (read-only) ********************************************/
453
454 bool typeinfo_is_array(typeinfo *info);
455 bool typeinfo_is_primitive_array(typeinfo *info,int arraytype);
456 bool typeinfo_is_array_of_refs(typeinfo *info);
457
458 typecheck_result typeinfo_is_assignable(typeinfo *value,typeinfo *dest);
459 typecheck_result typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest);
460
461 /* initialization functions *************************************************/
462
463 /* RETURN VALUE (bool):
464  *     true.............ok,
465  *     false............an exception has been thrown.
466  *
467  * RETURN VALUE (int):
468  *     >= 0.............ok,
469  *     -1...............an exception has been thrown.
470  */
471 void typeinfo_init_classinfo(typeinfo *info,classinfo *c);
472 bool typeinfo_init_class(typeinfo *info,classref_or_classinfo c);
473 bool typeinfo_init_component(typeinfo *srcarray,typeinfo *dst);
474
475 bool typeinfo_init_from_typedesc(typedesc *desc,u1 *type,typeinfo *info);
476 bool typeinfos_init_from_methoddesc(methoddesc *desc,u1 *typebuf,
477                                    typeinfo *infobuf,
478                                    int buflen,bool twoword,
479                                    u1 *returntype,typeinfo *returntypeinfo);
480 bool  typedescriptor_init_from_typedesc(typedescriptor *td,
481                                                                             typedesc *desc);
482 bool  typeinfo_init_varinfo_from_typedesc(varinfo *var,
483                                                                             typedesc *desc);
484 int  typedescriptors_init_from_methoddesc(typedescriptor *td,
485                                                                                   methoddesc *desc,
486                                                                                   int buflen,bool twoword,int startindex,
487                                                                                   typedescriptor *returntype);
488 bool typeinfo_init_varinfos_from_methoddesc(varinfo *vars,
489                                                                                   methoddesc *desc,
490                                                                                   int buflen, int startindex,
491                                                                                   s4 *map,
492                                                                                   typedescriptor *returntype);
493
494 void typeinfo_clone(typeinfo *src,typeinfo *dest);
495
496 /* freeing memory ***********************************************************/
497
498 void typeinfo_free(typeinfo *info);
499
500 /* functions for merging types **********************************************/
501
502 typecheck_result typeinfo_merge(methodinfo *m,typeinfo *dest,typeinfo* y);
503
504 /* debugging helpers ********************************************************/
505
506 #ifdef TYPEINFO_DEBUG
507
508 #include <stdio.h>
509
510 void typeinfo_test();
511 void typeinfo_print_class(FILE *file,classref_or_classinfo c);
512 void typeinfo_print(FILE *file,typeinfo *info,int indent);
513 void typeinfo_print_short(FILE *file,typeinfo *info);
514 void typeinfo_print_type(FILE *file,int type,typeinfo *info);
515 void typedescriptor_print(FILE *file,typedescriptor *td);
516 void typevector_print(FILE *file,varinfo *vec,int size);
517
518 #endif /* TYPEINFO_DEBUG */
519
520 #endif /* _TYPEINFO_H */
521
522
523 /*
524  * These are local overrides for various environment variables in Emacs.
525  * Please do not remove this and leave it at the end of the file, where
526  * Emacs will automagically detect them.
527  * ---------------------------------------------------------------------
528  * Local variables:
529  * mode: c
530  * indent-tabs-mode: t
531  * c-basic-offset: 4
532  * tab-width: 4
533  * End:
534  */