1 /* typeinfo.h - type system used by the type checker
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 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., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Edwin Steiner
29 $Id: typeinfo.h 3642 2005-11-08 19:01:17Z edwin $
36 #include "vm/global.h"
37 #include "vm/references.h"
39 /* configuration **************************************************************/
42 * TYPECHECK_STATISTICS activates gathering statistical information.
43 * TYPEINFO_DEBUG activates debug checks and debug helpers in typeinfo.c
44 * TYPECHECK_DEBUG activates debug checks in typecheck.c
45 * TYPEINFO_DEBUG_TEST activates the typeinfo test at startup.
46 * TYPECHECK_VERBOSE_IMPORTANT activates important debug messages
47 * TYPECHECK_VERBOSE activates all debug messages
48 * TYPEINFO_VERBOSE activates debug prints in typeinfo.c
50 #ifdef CACAO_TYPECHECK
52 /*#define TYPECHECK_STATISTICS*/
53 #define TYPEINFO_DEBUG
54 /*#define TYPEINFO_VERBOSE*/
55 #define TYPECHECK_DEBUG
56 /*#define TYPEINFO_DEBUG_TEST*/
57 /*#define TYPECHECK_VERBOSE*/
58 /*#define TYPECHECK_VERBOSE_IMPORTANT*/
59 #if defined(TYPECHECK_VERBOSE) || defined(TYPECHECK_VERBOSE_IMPORTANT)
60 #define TYPECHECK_VERBOSE_OPT
65 #ifdef TYPECHECK_VERBOSE_OPT
66 extern bool typecheckverbose;
69 /* resolve typedef cycles *****************************************************/
71 typedef struct typeinfo typeinfo;
72 typedef struct typeinfo_mergedlist typeinfo_mergedlist;
73 typedef struct typedescriptor typedescriptor;
74 typedef struct typevector typevector;
75 typedef struct typeinfo_retaddr_set typeinfo_retaddr_set;
77 /* types **********************************************************************/
79 /* typecheck_result - return type for boolean and tristate functions */
80 /* which may also throw exceptions (typecheck_FAIL). */
82 /* NOTE: Use the enum values, not the uppercase #define macros! */
83 #define TYPECHECK_MAYBE 0x02
84 #define TYPECHECK_FAIL 0x04
87 typecheck_FALSE = false,
88 typecheck_TRUE = true,
89 typecheck_MAYBE = TYPECHECK_MAYBE,
90 typecheck_FAIL = TYPECHECK_FAIL
93 /* check that typecheck_MAYBE is not ambiguous */
94 #if TYPECHECK_MAYBE == true
95 #error "`typecheck_MAYBE` must not be the same as `true`"
97 #if TYPECHECK_MAYBE == false
98 #error "`typecheck_MAYBE` must not be the same as `false`"
101 /* check that typecheck_FAIL is not ambiguous */
102 #if (true & TYPECHECK_FAIL) != 0
103 #error "`true` must not have bit 0x02 set (conflicts with typecheck_FAIL)"
106 /* data structures for the type system ****************************************/
108 /* The typeinfo structure stores detailed information on address types.
109 * (stack elements, variables, etc. with type == TYPE_ADR.)
111 * There are two kinds of address types which can be distinguished by
112 * the value of the typeclass field:
114 * 1) typeclass == NULL: returnAddress type
115 * use TYPEINFO_IS_PRIMITIVE to test for this
117 * 2) typeclass != NULL: reference type
118 * use TYPEINFO_IS_REFERENCE to test for this
120 * Note: For non-address types either there is no typeinfo allocated
121 * or the fields of the typeinfo struct contain undefined values!
122 * DO NOT access the typeinfo for non-address types!
124 * CAUTION: The typeinfo structure should be considered opaque outside of
125 * typeinfo.[ch]. Please use the macros and functions defined here to
126 * access typeinfo structures!
129 /* At all times *exactly one* of the following conditions is true for
130 * a particular typeinfo struct:
132 * A) typeclass == NULL
134 * This is a returnAddress type. The interpretation of the
135 * elementclass field depends on wether this typeinfo describes
136 * a stack slot or a local variable:
138 * stack slot: elementclass is a pointer to a
139 * typeinfo_retaddr_set which contains a return target for
140 * every vector in the current set of local variable vectors.
141 * See typeinfo_retaddr_set and typevector below.
143 * local variable: elementclass is the return target (when cast
146 * Use TYPEINFO_IS_PRIMITIVE to check for this.
147 * Use TYPEINFO_RETURNADDRESS to access the pointer in elementclass.
148 * Don't access other fields of the struct.
150 * B) typeclass == pseudo_class_Null
152 * This is the null-reference type.
153 * Use TYPEINFO_IS_NULLTYPE to check for this.
154 * Don't access other fields of the struct.
156 * C) typeclass == pseudo_class_New
158 * This is an 'uninitialized object' type. elementclass can be
159 * cast to instruction* and points to the NEW instruction
160 * responsible for creating this type.
162 * Use TYPEINFO_NEWOBJECT_INSTRUCTION to access the pointer in
164 * Don't access other fields of the struct.
166 * D) typeclass == pseudo_class_Arraystub
168 * This type is used to represent the result of merging array types
169 * with incompatible component types. An arraystub allows no access
170 * to its components (since their type is undefined), but it allows
171 * operations which act directly on an arbitrary array type (such as
172 * requesting the array size).
174 * NOTE: An array stub does *not* count as an array. It has dimension
177 * Otherwise like a normal class reference type.
178 * Don't access other fields of the struct.
180 * E) typeclass is an array class
182 * An array reference.
183 * elementclass...typeclass of the element type
184 * dimension......dimension of the array (>=1)
185 * elementtype....element type (ARRAYTYPE_...)
186 * merged.........mergedlist of the element type
188 * Use TYPEINFO_IS_ARRAY to check for this case.
190 * The elementclass may be one of the following:
191 * 1) pseudo_class_Arraystub
192 * 2) an unresolved type
193 * 3) a loaded interface
194 * 4) a loaded (non-pseudo-,non-array-)class != (BOOTSTRAP)java.lang.Object
195 * Note: `merged` may be used
196 * 5) (BOOTSTRAP)java.lang.Object
197 * Note: `merged` may be used
199 * For the semantics of the merged field in cases 4) and 5) consult the
200 * corresponding descriptions with `elementclass` replaced by `typeclass`.
202 * F) typeclass is an unresolved type (a symbolic class/interface reference)
204 * The type has not been resolved yet. (Meaning it corresponds to an
205 * unloaded class or interface).
206 * Don't access other fields of the struct.
208 * G) typeclass is a loaded interface
210 * An interface reference type.
211 * Don't access other fields of the struct.
213 * H) typeclass is a loaded (non-pseudo-,non-array-)class != (BOOTSTRAP)java.lang.Object
215 * A loaded class type.
216 * All classref_or_classinfos in u.merged.list (if any) are
217 * loaded subclasses of typeclass (no interfaces, array classes, or
219 * Don't access other fields of the struct.
221 * I) typeclass is (BOOTSTRAP)java.lang.Object
223 * The most general kind of reference type.
224 * In this case u.merged.count and u.merged.list
225 * are valid and may be non-zero.
226 * The classref_or_classinfos in u.merged.list (if any) may be
227 * classes, interfaces, pseudo classes or unresolved types.
228 * Don't access other fields of the struct.
231 /* The following algorithm is used to determine if the type described
232 * by this typeinfo struct supports the interface X: * XXX add MAYBE *
234 * 1) If typeclass is X or a subinterface of X the answer is "yes".
235 * 2) If typeclass is a (pseudo) class implementing X the answer is "yes".
236 * 3) If typeclass is not an array and u.merged.count>0
237 * and all classes/interfaces in u.merged.list implement X
238 * the answer is "yes".
239 * 4) If none of the above is true the answer is "no".
243 * CAUTION: The typeinfo structure should be considered opaque outside of
244 * typeinfo.[ch]. Please use the macros and functions defined here to
245 * access typeinfo structures!
248 classref_or_classinfo typeclass;
249 classref_or_classinfo elementclass; /* valid if dimension>0 */ /* various uses! */
250 typeinfo_mergedlist *merged;
252 u1 elementtype; /* valid if dimension>0 */
255 struct typeinfo_mergedlist {
257 classref_or_classinfo list[1]; /* variable length! */
260 /*-----------------------------------------------------------------------*/
261 /* a typeinfo_retaddr_set stores the set of possible returnAddresses */
262 /* that may be in a particular stack slot at a particular point in the */
265 /* There may be one or more alternative returnAddresses if the */
266 /* instruction can be reached via one or more JSR jumps (among other */
267 /* control-flow paths */
268 /*-----------------------------------------------------------------------*/
270 struct typeinfo_retaddr_set {
271 typeinfo_retaddr_set *alt; /* next alternative in set */
272 void *addr; /* return address */
275 /* a type descriptor stores a basic type and the typeinfo */
276 /* this is used for storing the type of a local variable, and for */
277 /* storing types in the signature of a method */
279 struct typedescriptor {
280 typeinfo info; /* valid if type == TYPE_ADR */
281 u1 type; /* basic type (TYPE_INT, ...) */
284 /*-----------------------------------------------------------------------*/
285 /* typevectors are used to store the types of all local variables */
286 /* at a given point in the program. */
288 /* There may be more than one possible typevector for the local */
289 /* variables at a given instruction if the instruction can be reached */
290 /* via one or more JSR jumps (among other control-flow paths). */
292 /* This is called the set of alternative type vectors at that */
293 /* particular point in the program. */
294 /*-----------------------------------------------------------------------*/
297 typevector *alt; /* next alternative in typevector set */
298 int k; /* for lining up with the stack set */
299 typedescriptor td[1]; /* types of locals, variable length! */
302 /****************************************************************************/
304 /****************************************************************************/
306 /* NOTE: The TYPEINFO macros take typeinfo *structs*, not pointers as
307 * arguments. You have to dereference any pointers.
310 /* typevectors **************************************************************/
312 #define TYPEVECTOR_SIZE(size) \
313 ((sizeof(typevector) - sizeof(typedescriptor)) \
314 + (size)*sizeof(typedescriptor))
316 #define DNEW_TYPEVECTOR(size) \
317 ((typevector*)dump_alloc(TYPEVECTOR_SIZE(size)))
319 #define DMNEW_TYPEVECTOR(num,size) \
320 ((void*)dump_alloc((num) * TYPEVECTOR_SIZE(size)))
322 #define MGET_TYPEVECTOR(array,index,size) \
323 ((typevector*) (((u1*)(array)) + TYPEVECTOR_SIZE(size) * (index)))
325 /* internally used macros ***************************************************/
327 /* internal, don't use this explicitly! */
328 #define TYPEINFO_ALLOCMERGED(mergedlist,count) \
329 do {(mergedlist) = (typeinfo_mergedlist*)dump_alloc( \
330 sizeof(typeinfo_mergedlist) \
331 + ((count)-1)*sizeof(classinfo*));} while(0)
333 /* internal, don't use this explicitly! */
334 #define TYPEINFO_FREEMERGED(mergedlist)
336 /* internal, don't use this explicitly! */
337 #define TYPEINFO_FREEMERGED_IF_ANY(mergedlist)
339 /* macros for type queries **************************************************/
341 #define TYPEINFO_IS_PRIMITIVE(info) \
342 ((info).typeclass.any == NULL)
344 #define TYPEINFO_IS_REFERENCE(info) \
345 ((info).typeclass.any != NULL)
347 #define TYPEINFO_IS_NULLTYPE(info) \
348 ((info).typeclass.cls == pseudo_class_Null)
350 #define TYPEINFO_IS_NEWOBJECT(info) \
351 ((info).typeclass.cls == pseudo_class_New)
353 #define TYPEINFO_IS_JAVA_LANG_CLASS(info) \
354 ((info).typeclass.cls == class_java_lang_Class)
356 /* only use this if TYPEINFO_IS_PRIMITIVE returned true! */
357 #define TYPEINFO_RETURNADDRESS(info) \
358 ((info).elementclass.any)
360 /* only use this if TYPEINFO_IS_NEWOBJECT returned true! */
361 #define TYPEINFO_NEWOBJECT_INSTRUCTION(info) \
362 ((info).elementclass.any)
364 /* only use this if TYPEINFO_IS_JAVA_LANG_CLASS returned true! */
365 #define TYPEINFO_JAVA_LANG_CLASS_CLASSREF(info) \
366 ((info).elementclass.ref)
368 /* macros for array type queries ********************************************/
370 #define TYPEINFO_IS_ARRAY(info) \
371 ( TYPEINFO_IS_REFERENCE(info) \
372 && ((info).dimension != 0) )
374 #define TYPEINFO_IS_SIMPLE_ARRAY(info) \
375 ( ((info).dimension == 1) )
377 #define TYPEINFO_IS_ARRAY_ARRAY(info) \
378 ( ((info).dimension >= 2) )
380 #define TYPEINFO_IS_PRIMITIVE_ARRAY(info,arraytype) \
381 ( TYPEINFO_IS_SIMPLE_ARRAY(info) \
382 && ((info).elementtype == (arraytype)) )
384 #define TYPEINFO_IS_OBJECT_ARRAY(info) \
385 ( TYPEINFO_IS_SIMPLE_ARRAY(info) \
386 && ((info).elementclass.any != NULL) )
388 /* assumes that info describes an array type */
389 #define TYPEINFO_IS_ARRAY_OF_REFS_NOCHECK(info) \
390 ( ((info).elementclass.any != NULL) \
391 || ((info).dimension >= 2) )
393 #define TYPEINFO_IS_ARRAY_OF_REFS(info) \
394 ( TYPEINFO_IS_ARRAY(info) \
395 && TYPEINFO_IS_ARRAY_OF_REFS_NOCHECK(info) )
397 #define TYPE_IS_RETURNADDRESS(type,info) \
398 ( ((type)==TYPE_ADDRESS) \
399 && TYPEINFO_IS_PRIMITIVE(info) )
401 #define TYPE_IS_REFERENCE(type,info) \
402 ( ((type)==TYPE_ADDRESS) \
403 && !TYPEINFO_IS_PRIMITIVE(info) )
405 #define TYPEDESC_IS_RETURNADDRESS(td) \
406 TYPE_IS_RETURNADDRESS((td).type,(td).info)
408 #define TYPEDESC_IS_REFERENCE(td) \
409 TYPE_IS_REFERENCE((td).type,(td).info)
411 /* queries allowing the null type ********************************************/
413 #define TYPEINFO_MAYBE_ARRAY(info) \
414 (TYPEINFO_IS_ARRAY(info) || TYPEINFO_IS_NULLTYPE(info))
416 #define TYPEINFO_MAYBE_PRIMITIVE_ARRAY(info,at) \
417 (TYPEINFO_IS_PRIMITIVE_ARRAY(info,at) || TYPEINFO_IS_NULLTYPE(info))
419 #define TYPEINFO_MAYBE_ARRAY_OF_REFS(info) \
420 (TYPEINFO_IS_ARRAY_OF_REFS(info) || TYPEINFO_IS_NULLTYPE(info))
422 /* macros for initializing typeinfo structures ******************************/
424 #define TYPEINFO_INIT_PRIMITIVE(info) \
425 do {(info).typeclass.any = NULL; \
426 (info).elementclass.any = NULL; \
427 (info).merged = NULL; \
428 (info).dimension = 0; \
429 (info).elementtype = 0;} while(0)
431 #define TYPEINFO_INIT_RETURNADDRESS(info,adr) \
432 do {(info).typeclass.any = NULL; \
433 (info).elementclass.any = (adr); \
434 (info).merged = NULL; \
435 (info).dimension = 0; \
436 (info).elementtype = 0;} while(0)
438 #define TYPEINFO_INIT_NON_ARRAY_CLASSINFO(info,cinfo) \
439 do {(info).typeclass.cls = (cinfo); \
440 (info).elementclass.any = NULL; \
441 (info).merged = NULL; \
442 (info).dimension = 0; \
443 (info).elementtype = 0;} while(0)
445 #define TYPEINFO_INIT_JAVA_LANG_CLASS(info,cr) \
446 do {(info).typeclass.any = class_java_lang_Class; \
447 (info).elementclass.ref = (cr); \
448 (info).merged = NULL; \
449 (info).dimension = 0; \
450 (info).elementtype = 0;} while(0)
452 #define TYPEINFO_INIT_NULLTYPE(info) \
453 TYPEINFO_INIT_NON_ARRAY_CLASSINFO(info,pseudo_class_Null)
455 #define TYPEINFO_INIT_NEWOBJECT(info,instr) \
456 do {(info).typeclass.cls = pseudo_class_New; \
457 (info).elementclass.any = (instr); \
458 (info).merged = NULL; \
459 (info).dimension = 0; \
460 (info).elementtype = 0;} while(0)
462 #define TYPEINFO_INIT_PRIMITIVE_ARRAY(info,arraytype) \
463 typeinfo_init_classinfo(&(info),primitivetype_table[arraytype].arrayclass);
465 /* macros for copying types (destinition is not checked or freed) ***********/
467 /* TYPEINFO_COPY makes a shallow copy, the merged pointer is simply copied. */
468 #define TYPEINFO_COPY(src,dst) \
469 do {(dst) = (src);} while(0)
471 /* TYPEINFO_CLONE makes a deep copy, the merged list (if any) is duplicated
472 * into a newly allocated array.
474 #define TYPEINFO_CLONE(src,dst) \
476 if ((dst).merged) typeinfo_clone(&(src),&(dst));} while(0)
478 /****************************************************************************/
480 /****************************************************************************/
482 /* typevector functions *****************************************************/
484 /* element read-only access */
485 bool typevectorset_checktype(typevector *set,int index,int type);
486 bool typevectorset_checkreference(typevector *set,int index);
487 bool typevectorset_checkretaddr(typevector *set,int index);
488 int typevectorset_copymergedtype(methodinfo *m,typevector *set,int index,typeinfo *dst);
489 int typevectorset_mergedtype(methodinfo *m,typevector *set,int index,typeinfo *temp,typeinfo **result);
491 /* element write access */
492 void typevectorset_store(typevector *set,int index,int type,typeinfo *info);
493 void typevectorset_store_retaddr(typevector *set,int index,typeinfo *info);
494 void typevectorset_store_twoword(typevector *set,int index,int type);
495 bool typevectorset_init_object(typevector *set,void *ins,classref_or_classinfo initclass,int size);
497 /* vector functions */
498 bool typevector_separable_from(typevector *a,typevector *b,int size);
499 typecheck_result typevector_merge(methodinfo *m,typevector *dst,typevector *y,int size);
501 /* vector set functions */
502 typevector *typevectorset_copy(typevector *src,int k,int size);
503 void typevectorset_copy_inplace(typevector *src,typevector *dst,int size);
504 bool typevectorset_separable_with(typevector *set,typevector *add,int size);
505 typecheck_result typevectorset_collapse(methodinfo *m,typevector *dst,int size);
506 void typevectorset_add(typevector *dst,typevector *v,int size);
507 typevector *typevectorset_select(typevector **set,int retindex,void *retaddr);
509 /* inquiry functions (read-only) ********************************************/
511 bool typeinfo_is_array(typeinfo *info);
512 bool typeinfo_is_primitive_array(typeinfo *info,int arraytype);
513 bool typeinfo_is_array_of_refs(typeinfo *info);
515 typecheck_result typeinfo_is_assignable(typeinfo *value,typeinfo *dest);
516 typecheck_result typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest);
518 /* initialization functions *************************************************/
520 /* RETURN VALUE (bool):
521 * true.............ok,
522 * false............an exception has been thrown.
524 * RETURN VALUE (int):
525 * >= 0.............ok,
526 * -1...............an exception has been thrown.
528 void typeinfo_init_classinfo(typeinfo *info,classinfo *c);
529 bool typeinfo_init_class(typeinfo *info,classref_or_classinfo c);
530 bool typeinfo_init_component(typeinfo *srcarray,typeinfo *dst);
532 bool typeinfo_init_from_typedesc(typedesc *desc,u1 *type,typeinfo *info);
533 bool typeinfos_init_from_methoddesc(methoddesc *desc,u1 *typebuf,
535 int buflen,bool twoword,
536 u1 *returntype,typeinfo *returntypeinfo);
537 bool typedescriptor_init_from_typedesc(typedescriptor *td,
539 int typedescriptors_init_from_methoddesc(typedescriptor *td,
541 int buflen,bool twoword,int startindex,
542 typedescriptor *returntype);
544 void typeinfo_clone(typeinfo *src,typeinfo *dest);
546 /* freeing memory ***********************************************************/
548 void typeinfo_free(typeinfo *info);
550 /* functions for merging types **********************************************/
552 typecheck_result typeinfo_merge(methodinfo *m,typeinfo *dest,typeinfo* y);
554 /* debugging helpers ********************************************************/
556 #ifdef TYPEINFO_DEBUG
560 void typeinfo_test();
561 void typeinfo_print_class(FILE *file,classref_or_classinfo c);
562 void typeinfo_print(FILE *file,typeinfo *info,int indent);
563 void typeinfo_print_short(FILE *file,typeinfo *info);
564 void typeinfo_print_type(FILE *file,int type,typeinfo *info);
565 void typeinfo_print_stacktype(FILE *file,int type,typeinfo *info);
566 void typedescriptor_print(FILE *file,typedescriptor *td);
567 void typevector_print(FILE *file,typevector *vec,int size);
568 void typevectorset_print(FILE *file,typevector *set,int size);
570 #endif /* TYPEINFO_DEBUG */
572 #endif /* _TYPEINFO_H */
576 * These are local overrides for various environment variables in Emacs.
577 * Please do not remove this and leave it at the end of the file, where
578 * Emacs will automagically detect them.
579 * ---------------------------------------------------------------------
582 * indent-tabs-mode: t