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
30 /* resolve typedef cycles *****************************************************/
32 typedef struct typeinfo typeinfo;
33 typedef struct typeinfo_mergedlist typeinfo_mergedlist;
34 typedef struct typedescriptor typedescriptor;
39 #include "vm/global.h"
41 #include "vmcore/references.h"
44 /* configuration **************************************************************/
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
55 #ifdef ENABLE_VERIFIER
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
70 #ifdef TYPECHECK_VERBOSE_OPT
71 extern bool opt_typecheckverbose;
74 /* types **********************************************************************/
76 /* typecheck_result - return type for boolean and tristate functions */
77 /* which may also throw exceptions (typecheck_FAIL). */
79 /* NOTE: Use the enum values, not the uppercase #define macros! */
80 #define TYPECHECK_MAYBE 0x02
81 #define TYPECHECK_FAIL 0x04
84 typecheck_FALSE = false,
85 typecheck_TRUE = true,
86 typecheck_MAYBE = TYPECHECK_MAYBE,
87 typecheck_FAIL = TYPECHECK_FAIL
90 /* check that typecheck_MAYBE is not ambiguous */
91 #if TYPECHECK_MAYBE == true
92 #error "`typecheck_MAYBE` must not be the same as `true`"
94 #if TYPECHECK_MAYBE == false
95 #error "`typecheck_MAYBE` must not be the same as `false`"
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)"
103 /* data structures for the type system ****************************************/
105 /* The typeinfo structure stores detailed information on address types.
106 * (stack elements, variables, etc. with type == TYPE_ADR.)
108 * There are two kinds of address types which can be distinguished by
109 * the value of the typeclass field:
111 * 1) typeclass == NULL: returnAddress type
112 * use TYPEINFO_IS_PRIMITIVE to test for this
114 * 2) typeclass != NULL: reference type
115 * use TYPEINFO_IS_REFERENCE to test for this
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!
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!
126 /* At all times *exactly one* of the following conditions is true for
127 * a particular typeinfo struct:
129 * A) typeclass == NULL
131 * This is a returnAddress type.
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.
137 * B) typeclass == pseudo_class_Null
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.
143 * C) typeclass == pseudo_class_New
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.
149 * Use TYPEINFO_NEWOBJECT_INSTRUCTION to access the pointer in
151 * Don't access other fields of the struct.
153 * D) typeclass == pseudo_class_Arraystub
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).
161 * NOTE: An array stub does *not* count as an array. It has dimension
164 * Otherwise like a normal class reference type.
165 * Don't access other fields of the struct.
167 * E) typeclass is an array class
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
175 * Use TYPEINFO_IS_ARRAY to check for this case.
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
186 * For the semantics of the merged field in cases 4) and 5) consult the
187 * corresponding descriptions with `elementclass` replaced by `typeclass`.
189 * F) typeclass is an unresolved type (a symbolic class/interface reference)
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.
195 * G) typeclass is a loaded interface
197 * An interface reference type.
198 * Don't access other fields of the struct.
200 * H) typeclass is a loaded (non-pseudo-,non-array-)class != (BOOTSTRAP)java.lang.Object
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
206 * Don't access other fields of the struct.
208 * I) typeclass is (BOOTSTRAP)java.lang.Object
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.
218 /* The following algorithm is used to determine if the type described
219 * by this typeinfo struct supports the interface X: * XXX add MAYBE *
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".
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!
235 classref_or_classinfo typeclass;
236 classref_or_classinfo elementclass; /* valid if dimension>0 */ /* various uses! */
237 typeinfo_mergedlist *merged;
239 u1 elementtype; /* valid if dimension>0 */
242 struct typeinfo_mergedlist {
244 classref_or_classinfo list[1]; /* variable length! */
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 */
251 struct typedescriptor {
252 typeinfo typeinfo; /* valid if type == TYPE_ADR */
253 u1 type; /* basic type (TYPE_INT, ...) */
256 /****************************************************************************/
258 /****************************************************************************/
260 /* NOTE: The TYPEINFO macros take typeinfo *structs*, not pointers as
261 * arguments. You have to dereference any pointers.
264 /* typevectors **************************************************************/
266 #define TYPEVECTOR_SIZE(size) \
267 ((size) * sizeof(varinfo))
269 #define DNEW_TYPEVECTOR(size) \
270 ((varinfo*)dump_alloc(TYPEVECTOR_SIZE(size)))
272 #define DMNEW_TYPEVECTOR(num,size) \
273 ((void*)dump_alloc((num) * TYPEVECTOR_SIZE(size)))
275 #define MGET_TYPEVECTOR(array,index,size) \
276 ((varinfo*) (((u1*)(array)) + TYPEVECTOR_SIZE(size) * (index)))
278 /* internally used macros ***************************************************/
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)
286 /* internal, don't use this explicitly! */
287 #define TYPEINFO_FREEMERGED(mergedlist)
289 /* internal, don't use this explicitly! */
290 #define TYPEINFO_FREEMERGED_IF_ANY(mergedlist)
292 /* macros for type queries **************************************************/
294 #define TYPEINFO_IS_PRIMITIVE(info) \
295 ((info).typeclass.any == NULL)
297 #define TYPEINFO_IS_REFERENCE(info) \
298 ((info).typeclass.any != NULL)
300 #define TYPEINFO_IS_NULLTYPE(info) \
301 ((info).typeclass.cls == pseudo_class_Null)
303 #define TYPEINFO_IS_NEWOBJECT(info) \
304 ((info).typeclass.cls == pseudo_class_New)
306 #define TYPEINFO_IS_JAVA_LANG_CLASS(info) \
307 ((info).typeclass.cls == class_java_lang_Class)
309 /* only use this if TYPEINFO_IS_PRIMITIVE returned true! */
310 #define TYPEINFO_RETURNADDRESS(info) \
311 ((info).elementclass.any)
313 /* only use this if TYPEINFO_IS_NEWOBJECT returned true! */
314 #define TYPEINFO_NEWOBJECT_INSTRUCTION(info) \
315 ((info).elementclass.any)
317 /* only use this if TYPEINFO_IS_JAVA_LANG_CLASS returned true! */
318 #define TYPEINFO_JAVA_LANG_CLASS_CLASSREF(info) \
319 ((info).elementclass.ref)
321 /* macros for array type queries ********************************************/
323 #define TYPEINFO_IS_ARRAY(info) \
324 ( TYPEINFO_IS_REFERENCE(info) \
325 && ((info).dimension != 0) )
327 #define TYPEINFO_IS_SIMPLE_ARRAY(info) \
328 ( ((info).dimension == 1) )
330 #define TYPEINFO_IS_ARRAY_ARRAY(info) \
331 ( ((info).dimension >= 2) )
333 #define TYPEINFO_IS_PRIMITIVE_ARRAY(info,arraytype) \
334 ( TYPEINFO_IS_SIMPLE_ARRAY(info) \
335 && ((info).elementtype == (arraytype)) )
337 #define TYPEINFO_IS_OBJECT_ARRAY(info) \
338 ( TYPEINFO_IS_SIMPLE_ARRAY(info) \
339 && ((info).elementclass.any != NULL) )
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) )
346 #define TYPEINFO_IS_ARRAY_OF_REFS(info) \
347 ( TYPEINFO_IS_ARRAY(info) \
348 && TYPEINFO_IS_ARRAY_OF_REFS_NOCHECK(info) )
350 #define TYPE_IS_RETURNADDRESS(type,info) \
351 ( ((type)==TYPE_RET) \
352 && TYPEINFO_IS_PRIMITIVE(info) )
354 #define TYPE_IS_REFERENCE(type,info) \
355 ( ((type)==TYPE_ADR) \
356 && !TYPEINFO_IS_PRIMITIVE(info) )
358 #define TYPEDESC_IS_RETURNADDRESS(td) \
359 TYPE_IS_RETURNADDRESS((td).type,(td).typeinfo)
361 #define TYPEDESC_IS_REFERENCE(td) \
362 TYPE_IS_REFERENCE((td).type,(td).typeinfo)
364 /* queries allowing the null type ********************************************/
366 #define TYPEINFO_MAYBE_ARRAY(info) \
367 (TYPEINFO_IS_ARRAY(info) || TYPEINFO_IS_NULLTYPE(info))
369 #define TYPEINFO_MAYBE_PRIMITIVE_ARRAY(info,at) \
370 (TYPEINFO_IS_PRIMITIVE_ARRAY(info,at) || TYPEINFO_IS_NULLTYPE(info))
372 #define TYPEINFO_MAYBE_ARRAY_OF_REFS(info) \
373 (TYPEINFO_IS_ARRAY_OF_REFS(info) || TYPEINFO_IS_NULLTYPE(info))
375 /* macros for initializing typeinfo structures ******************************/
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)
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)
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)
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)
405 #define TYPEINFO_INIT_NULLTYPE(info) \
406 TYPEINFO_INIT_NON_ARRAY_CLASSINFO(info,pseudo_class_Null)
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)
415 #define TYPEINFO_INIT_PRIMITIVE_ARRAY(info,arraytype) \
416 typeinfo_init_classinfo(&(info),primitivetype_table[arraytype].arrayclass);
418 /* macros for copying types (destinition is not checked or freed) ***********/
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)
424 /* TYPEINFO_CLONE makes a deep copy, the merged list (if any) is duplicated
425 * into a newly allocated array.
427 #define TYPEINFO_CLONE(src,dst) \
429 if ((dst).merged) typeinfo_clone(&(src),&(dst));} while(0)
431 /****************************************************************************/
433 /****************************************************************************/
435 /* typevector functions *****************************************************/
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);
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);
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);
452 /* inquiry functions (read-only) ********************************************/
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);
458 typecheck_result typeinfo_is_assignable(typeinfo *value,typeinfo *dest);
459 typecheck_result typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest);
461 /* initialization functions *************************************************/
463 /* RETURN VALUE (bool):
464 * true.............ok,
465 * false............an exception has been thrown.
467 * RETURN VALUE (int):
468 * >= 0.............ok,
469 * -1...............an exception has been thrown.
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);
475 bool typeinfo_init_from_typedesc(typedesc *desc,u1 *type,typeinfo *info);
476 bool typeinfos_init_from_methoddesc(methoddesc *desc,u1 *typebuf,
478 int buflen,bool twoword,
479 u1 *returntype,typeinfo *returntypeinfo);
480 bool typedescriptor_init_from_typedesc(typedescriptor *td,
482 bool typeinfo_init_varinfo_from_typedesc(varinfo *var,
484 int typedescriptors_init_from_methoddesc(typedescriptor *td,
486 int buflen,bool twoword,int startindex,
487 typedescriptor *returntype);
488 bool typeinfo_init_varinfos_from_methoddesc(varinfo *vars,
490 int buflen, int startindex,
492 typedescriptor *returntype);
494 void typeinfo_clone(typeinfo *src,typeinfo *dest);
496 /* freeing memory ***********************************************************/
498 void typeinfo_free(typeinfo *info);
500 /* functions for merging types **********************************************/
502 typecheck_result typeinfo_merge(methodinfo *m,typeinfo *dest,typeinfo* y);
504 /* debugging helpers ********************************************************/
506 #ifdef TYPEINFO_DEBUG
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);
518 #endif /* TYPEINFO_DEBUG */
520 #endif /* _TYPEINFO_H */
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 * ---------------------------------------------------------------------
530 * indent-tabs-mode: t