1 /* src/vm/jit/verify/typeinfo.h - type system used by the type checker
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
8 This file is part of CACAO.
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.
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.
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
25 $Id: typeinfo.h 7246 2007-01-29 18:49:05Z twisti $
32 /* resolve typedef cycles *****************************************************/
34 typedef struct typeinfo typeinfo;
35 typedef struct typeinfo_mergedlist typeinfo_mergedlist;
36 typedef struct typedescriptor typedescriptor;
41 #include "vm/global.h"
43 #include "vmcore/references.h"
46 /* configuration **************************************************************/
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
57 #ifdef ENABLE_VERIFIER
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
72 #ifdef TYPECHECK_VERBOSE_OPT
73 extern bool opt_typecheckverbose;
76 /* types **********************************************************************/
78 /* typecheck_result - return type for boolean and tristate functions */
79 /* which may also throw exceptions (typecheck_FAIL). */
81 /* NOTE: Use the enum values, not the uppercase #define macros! */
82 #define TYPECHECK_MAYBE 0x02
83 #define TYPECHECK_FAIL 0x04
86 typecheck_FALSE = false,
87 typecheck_TRUE = true,
88 typecheck_MAYBE = TYPECHECK_MAYBE,
89 typecheck_FAIL = TYPECHECK_FAIL
92 /* check that typecheck_MAYBE is not ambiguous */
93 #if TYPECHECK_MAYBE == true
94 #error "`typecheck_MAYBE` must not be the same as `true`"
96 #if TYPECHECK_MAYBE == false
97 #error "`typecheck_MAYBE` must not be the same as `false`"
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)"
105 /* data structures for the type system ****************************************/
107 /* The typeinfo structure stores detailed information on address types.
108 * (stack elements, variables, etc. with type == TYPE_ADR.)
110 * There are two kinds of address types which can be distinguished by
111 * the value of the typeclass field:
113 * 1) typeclass == NULL: returnAddress type
114 * use TYPEINFO_IS_PRIMITIVE to test for this
116 * 2) typeclass != NULL: reference type
117 * use TYPEINFO_IS_REFERENCE to test for this
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!
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!
128 /* At all times *exactly one* of the following conditions is true for
129 * a particular typeinfo struct:
131 * A) typeclass == NULL
133 * This is a returnAddress type.
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.
139 * B) typeclass == pseudo_class_Null
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.
145 * C) typeclass == pseudo_class_New
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.
151 * Use TYPEINFO_NEWOBJECT_INSTRUCTION to access the pointer in
153 * Don't access other fields of the struct.
155 * D) typeclass == pseudo_class_Arraystub
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).
163 * NOTE: An array stub does *not* count as an array. It has dimension
166 * Otherwise like a normal class reference type.
167 * Don't access other fields of the struct.
169 * E) typeclass is an array class
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
177 * Use TYPEINFO_IS_ARRAY to check for this case.
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
188 * For the semantics of the merged field in cases 4) and 5) consult the
189 * corresponding descriptions with `elementclass` replaced by `typeclass`.
191 * F) typeclass is an unresolved type (a symbolic class/interface reference)
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.
197 * G) typeclass is a loaded interface
199 * An interface reference type.
200 * Don't access other fields of the struct.
202 * H) typeclass is a loaded (non-pseudo-,non-array-)class != (BOOTSTRAP)java.lang.Object
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
208 * Don't access other fields of the struct.
210 * I) typeclass is (BOOTSTRAP)java.lang.Object
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.
220 /* The following algorithm is used to determine if the type described
221 * by this typeinfo struct supports the interface X: * XXX add MAYBE *
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".
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!
237 classref_or_classinfo typeclass;
238 classref_or_classinfo elementclass; /* valid if dimension>0 */ /* various uses! */
239 typeinfo_mergedlist *merged;
241 u1 elementtype; /* valid if dimension>0 */
244 struct typeinfo_mergedlist {
246 classref_or_classinfo list[1]; /* variable length! */
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 */
253 struct typedescriptor {
254 typeinfo typeinfo; /* valid if type == TYPE_ADR */
255 u1 type; /* basic type (TYPE_INT, ...) */
258 /****************************************************************************/
260 /****************************************************************************/
262 /* NOTE: The TYPEINFO macros take typeinfo *structs*, not pointers as
263 * arguments. You have to dereference any pointers.
266 /* typevectors **************************************************************/
268 #define TYPEVECTOR_SIZE(size) \
269 ((size) * sizeof(varinfo))
271 #define DNEW_TYPEVECTOR(size) \
272 ((varinfo*)dump_alloc(TYPEVECTOR_SIZE(size)))
274 #define DMNEW_TYPEVECTOR(num,size) \
275 ((void*)dump_alloc((num) * TYPEVECTOR_SIZE(size)))
277 #define MGET_TYPEVECTOR(array,index,size) \
278 ((varinfo*) (((u1*)(array)) + TYPEVECTOR_SIZE(size) * (index)))
280 /* internally used macros ***************************************************/
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)
288 /* internal, don't use this explicitly! */
289 #define TYPEINFO_FREEMERGED(mergedlist)
291 /* internal, don't use this explicitly! */
292 #define TYPEINFO_FREEMERGED_IF_ANY(mergedlist)
294 /* macros for type queries **************************************************/
296 #define TYPEINFO_IS_PRIMITIVE(info) \
297 ((info).typeclass.any == NULL)
299 #define TYPEINFO_IS_REFERENCE(info) \
300 ((info).typeclass.any != NULL)
302 #define TYPEINFO_IS_NULLTYPE(info) \
303 ((info).typeclass.cls == pseudo_class_Null)
305 #define TYPEINFO_IS_NEWOBJECT(info) \
306 ((info).typeclass.cls == pseudo_class_New)
308 #define TYPEINFO_IS_JAVA_LANG_CLASS(info) \
309 ((info).typeclass.cls == class_java_lang_Class)
311 /* only use this if TYPEINFO_IS_PRIMITIVE returned true! */
312 #define TYPEINFO_RETURNADDRESS(info) \
313 ((info).elementclass.any)
315 /* only use this if TYPEINFO_IS_NEWOBJECT returned true! */
316 #define TYPEINFO_NEWOBJECT_INSTRUCTION(info) \
317 ((info).elementclass.any)
319 /* only use this if TYPEINFO_IS_JAVA_LANG_CLASS returned true! */
320 #define TYPEINFO_JAVA_LANG_CLASS_CLASSREF(info) \
321 ((info).elementclass.ref)
323 /* macros for array type queries ********************************************/
325 #define TYPEINFO_IS_ARRAY(info) \
326 ( TYPEINFO_IS_REFERENCE(info) \
327 && ((info).dimension != 0) )
329 #define TYPEINFO_IS_SIMPLE_ARRAY(info) \
330 ( ((info).dimension == 1) )
332 #define TYPEINFO_IS_ARRAY_ARRAY(info) \
333 ( ((info).dimension >= 2) )
335 #define TYPEINFO_IS_PRIMITIVE_ARRAY(info,arraytype) \
336 ( TYPEINFO_IS_SIMPLE_ARRAY(info) \
337 && ((info).elementtype == (arraytype)) )
339 #define TYPEINFO_IS_OBJECT_ARRAY(info) \
340 ( TYPEINFO_IS_SIMPLE_ARRAY(info) \
341 && ((info).elementclass.any != NULL) )
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) )
348 #define TYPEINFO_IS_ARRAY_OF_REFS(info) \
349 ( TYPEINFO_IS_ARRAY(info) \
350 && TYPEINFO_IS_ARRAY_OF_REFS_NOCHECK(info) )
352 #define TYPE_IS_RETURNADDRESS(type,info) \
353 ( ((type)==TYPE_RET) \
354 && TYPEINFO_IS_PRIMITIVE(info) )
356 #define TYPE_IS_REFERENCE(type,info) \
357 ( ((type)==TYPE_ADR) \
358 && !TYPEINFO_IS_PRIMITIVE(info) )
360 #define TYPEDESC_IS_RETURNADDRESS(td) \
361 TYPE_IS_RETURNADDRESS((td).type,(td).typeinfo)
363 #define TYPEDESC_IS_REFERENCE(td) \
364 TYPE_IS_REFERENCE((td).type,(td).typeinfo)
366 /* queries allowing the null type ********************************************/
368 #define TYPEINFO_MAYBE_ARRAY(info) \
369 (TYPEINFO_IS_ARRAY(info) || TYPEINFO_IS_NULLTYPE(info))
371 #define TYPEINFO_MAYBE_PRIMITIVE_ARRAY(info,at) \
372 (TYPEINFO_IS_PRIMITIVE_ARRAY(info,at) || TYPEINFO_IS_NULLTYPE(info))
374 #define TYPEINFO_MAYBE_ARRAY_OF_REFS(info) \
375 (TYPEINFO_IS_ARRAY_OF_REFS(info) || TYPEINFO_IS_NULLTYPE(info))
377 /* macros for initializing typeinfo structures ******************************/
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)
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)
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)
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)
407 #define TYPEINFO_INIT_NULLTYPE(info) \
408 TYPEINFO_INIT_NON_ARRAY_CLASSINFO(info,pseudo_class_Null)
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)
417 #define TYPEINFO_INIT_PRIMITIVE_ARRAY(info,arraytype) \
418 typeinfo_init_classinfo(&(info),primitivetype_table[arraytype].arrayclass);
420 /* macros for copying types (destinition is not checked or freed) ***********/
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)
426 /* TYPEINFO_CLONE makes a deep copy, the merged list (if any) is duplicated
427 * into a newly allocated array.
429 #define TYPEINFO_CLONE(src,dst) \
431 if ((dst).merged) typeinfo_clone(&(src),&(dst));} while(0)
433 /****************************************************************************/
435 /****************************************************************************/
437 /* typevector functions *****************************************************/
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);
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);
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);
454 /* inquiry functions (read-only) ********************************************/
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);
460 typecheck_result typeinfo_is_assignable(typeinfo *value,typeinfo *dest);
461 typecheck_result typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest);
463 /* initialization functions *************************************************/
465 /* RETURN VALUE (bool):
466 * true.............ok,
467 * false............an exception has been thrown.
469 * RETURN VALUE (int):
470 * >= 0.............ok,
471 * -1...............an exception has been thrown.
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);
477 bool typeinfo_init_from_typedesc(typedesc *desc,u1 *type,typeinfo *info);
478 bool typeinfos_init_from_methoddesc(methoddesc *desc,u1 *typebuf,
480 int buflen,bool twoword,
481 u1 *returntype,typeinfo *returntypeinfo);
482 bool typedescriptor_init_from_typedesc(typedescriptor *td,
484 bool typeinfo_init_varinfo_from_typedesc(varinfo *var,
486 int typedescriptors_init_from_methoddesc(typedescriptor *td,
488 int buflen,bool twoword,int startindex,
489 typedescriptor *returntype);
490 bool typeinfo_init_varinfos_from_methoddesc(varinfo *vars,
492 int buflen, int startindex,
494 typedescriptor *returntype);
496 void typeinfo_clone(typeinfo *src,typeinfo *dest);
498 /* freeing memory ***********************************************************/
500 void typeinfo_free(typeinfo *info);
502 /* functions for merging types **********************************************/
504 typecheck_result typeinfo_merge(methodinfo *m,typeinfo *dest,typeinfo* y);
506 /* debugging helpers ********************************************************/
508 #ifdef TYPEINFO_DEBUG
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);
520 #endif /* TYPEINFO_DEBUG */
522 #endif /* _TYPEINFO_H */
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 * ---------------------------------------------------------------------
532 * indent-tabs-mode: t