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