1 /* typeinfo.c - type system used by the type checker
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5 M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6 P. Tomsich, J. Wenninger
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.c 1067 2004-05-18 10:25:51Z stefan $
39 #include "toolbox/logging.h"
40 #include "toolbox/memory.h"
41 #include "jit/jit.h" /* XXX move typeinfo.* into jit/ */
44 #define CLASS_IMPLEMENTS_INTERFACE(cls,index) \
45 ( ((index) < (cls)->vftbl->interfacetablelength) \
46 && (VFTBLINTERFACETABLE((cls)->vftbl,(index)) != NULL) )
48 /**********************************************************************/
49 /* TYPEVECTOR FUNCTIONS */
50 /**********************************************************************/
53 typevectorset_copy(typevector *src,int k,int size)
55 typevector *dst = DNEW_TYPEVECTOR(size);
57 memcpy(dst,src,TYPEVECTOR_SIZE(size));
60 dst->alt = typevectorset_copy(src->alt,k+1,size);
65 typevectorset_checktype(typevector *vec,int index,int type)
68 if (vec->td[index].type != type)
70 } while ((vec = vec->alt) != NULL);
75 typevectorset_checkreference(typevector *vec,int index)
78 if (!TYPEDESC_IS_REFERENCE(vec->td[index]))
80 } while ((vec = vec->alt) != NULL);
85 typevectorset_checkretaddr(typevector *vec,int index)
88 if (!TYPEDESC_IS_RETURNADDRESS(vec->td[index]))
90 } while ((vec = vec->alt) != NULL);
95 typevectorset_copymergedtype(typevector *vec,int index,typeinfo *dst)
100 td = vec->td + index;
102 TYPEINFO_COPY(td->info,*dst);
107 primitive = TYPEINFO_IS_PRIMITIVE(*dst) ? 1 : 0;
109 while ((vec = vec->alt) != NULL) {
110 td = vec->td + index;
111 if (type != td->type)
114 if (type == TYPE_ADDRESS) {
115 if ((TYPEINFO_IS_PRIMITIVE(td->info) ? 1 : 0) != primitive)
117 typeinfo_merge(dst,&(td->info));
125 typevectorset_mergedtype(typevector *vec,int index,typeinfo *temp,typeinfo **result)
129 return typevectorset_copymergedtype(vec,index,temp);
132 *result = &(vec->td[index].info);
133 return vec->td[index].type;
137 typevectorset_mergedtypeinfo(typevector *vec,int index,typeinfo *temp)
140 int type = typevectorset_mergedtype(vec,index,temp,&result);
141 return (type == TYPE_ADDRESS) ? result : NULL;
145 typevectorset_store(typevector *vec,int index,int type,typeinfo *info)
148 vec->td[index].type = type;
150 TYPEINFO_COPY(*info,vec->td[index].info);
151 if (index > 0 && IS_2_WORD_TYPE(vec->td[index-1].type))
152 vec->td[index-1].type = TYPE_VOID;
153 } while ((vec = vec->alt) != NULL);
157 typevectorset_store_retaddr(typevector *vec,int index,typeinfo *info)
159 typeinfo_retaddr_set *adr;
161 adr = (typeinfo_retaddr_set*) TYPEINFO_RETURNADDRESS(*info);
163 vec->td[index].type = TYPE_ADDRESS;
164 TYPEINFO_INIT_RETURNADDRESS(vec->td[index].info,adr->addr);
165 if (index > 0 && IS_2_WORD_TYPE(vec->td[index-1].type))
166 vec->td[index-1].type = TYPE_VOID;
168 } while ((vec = vec->alt) != NULL);
172 typevectorset_store_twoword(typevector *vec,int index,int type)
175 vec->td[index].type = type;
176 vec->td[index+1].type = TYPE_VOID;
177 if (index > 0 && IS_2_WORD_TYPE(vec->td[index-1].type))
178 vec->td[index-1].type = TYPE_VOID;
179 } while ((vec = vec->alt) != NULL);
183 typevectorset_init_object(typevector *set,void *ins,classinfo *initclass,
188 for (;set; set=set->alt) {
189 for (i=0; i<size; ++i) {
190 if (set->td[i].type == TYPE_ADR
191 && TYPEINFO_IS_NEWOBJECT(set->td[i].info)
192 && TYPEINFO_NEWOBJECT_INSTRUCTION(set->td[i].info) == ins)
194 TYPEINFO_INIT_CLASSINFO(set->td[i].info,initclass);
201 typevector_merge(typevector *dst,typevector *y,int size)
203 bool changed = false;
205 typedescriptor *a = dst->td;
206 typedescriptor *b = y->td;
208 if (a->type != TYPE_VOID && a->type != b->type) {
212 else if (a->type == TYPE_ADDRESS) {
213 if (TYPEINFO_IS_PRIMITIVE(a->info)) {
214 /* 'a' is a returnAddress */
215 if (!TYPEINFO_IS_PRIMITIVE(b->info)
216 || (TYPEINFO_RETURNADDRESS(a->info)
217 != TYPEINFO_RETURNADDRESS(b->info)))
224 /* 'a' is a reference */
225 if (TYPEINFO_IS_PRIMITIVE(b->info)) {
230 changed |= typeinfo_merge(&(a->info),&(b->info));
240 bool typevector_separable_from(typevector *a,typevector *b,int size)
242 typedescriptor *tda = a->td;
243 typedescriptor *tdb = b->td;
244 for (;size--; tda++,tdb++) {
245 if (TYPEDESC_IS_RETURNADDRESS(*tda)
246 && TYPEDESC_IS_RETURNADDRESS(*tdb)
247 && TYPEINFO_RETURNADDRESS(tda->info)
248 != TYPEINFO_RETURNADDRESS(tdb->info))
255 typevectorset_add(typevector *dst,typevector *v,int size)
259 dst->alt = DNEW_TYPEVECTOR(size);
260 memcpy(dst->alt,v,TYPEVECTOR_SIZE(size));
261 dst->alt->alt = NULL;
262 dst->alt->k = dst->k + 1;
266 typevectorset_select(typevector **set,int retindex,void *retaddr)
268 typevector *selected;
270 if (!*set) return NULL;
272 if (TYPEINFO_RETURNADDRESS((*set)->td[retindex].info) == retaddr) {
274 *set = selected->alt;
275 selected->alt = typevectorset_select(set,retindex,retaddr);
278 selected = typevectorset_select(&((*set)->alt),retindex,retaddr);
284 typevectorset_separable_with(typevector *set,typevector *add,int size)
291 for (i=0; i<size; ++i) {
292 if (!TYPEDESC_IS_RETURNADDRESS(add->td[i]))
294 addr = TYPEINFO_RETURNADDRESS(add->td[i].info);
299 if (!TYPEDESC_IS_RETURNADDRESS(v->td[i]))
301 if (TYPEINFO_RETURNADDRESS(v->td[i].info) != addr)
304 if (!v && separable) return true;
313 typevectorset_collapse(typevector *dst,int size)
315 bool changed = false;
318 typevector_merge(dst,dst->alt,size);
319 dst->alt = dst->alt->alt;
325 /**********************************************************************/
326 /* READ-ONLY FUNCTIONS */
327 /* The following functions don't change typeinfo data. */
328 /**********************************************************************/
331 typeinfo_is_array(typeinfo *info)
333 return TYPEINFO_IS_ARRAY(*info);
337 typeinfo_is_primitive_array(typeinfo *info,int arraytype)
339 return TYPEINFO_IS_PRIMITIVE_ARRAY(*info,arraytype);
343 typeinfo_is_array_of_refs(typeinfo *info)
345 return TYPEINFO_IS_ARRAY_OF_REFS(*info);
350 interface_extends_interface(classinfo *cls,classinfo *interf)
354 /* first check direct superinterfaces */
355 for (i=0; i<cls->interfacescount; ++i) {
356 if (cls->interfaces[i] == interf)
360 /* check indirect superinterfaces */
361 for (i=0; i<cls->interfacescount; ++i) {
362 if (interface_extends_interface(cls->interfaces[i],interf))
371 classinfo_implements_interface(classinfo *cls,classinfo *interf)
373 if (cls->flags & ACC_INTERFACE) {
374 /* cls is an interface */
378 /* check superinterfaces */
379 return interface_extends_interface(cls,interf);
382 return CLASS_IMPLEMENTS_INTERFACE(cls,interf->index);
385 bool mergedlist_implements_interface(typeinfo_mergedlist *merged,
391 /* Check if there is an non-empty mergedlist. */
395 /* If all classinfos in the (non-empty) merged array implement the
396 * interface return true, otherwise false.
398 mlist = merged->list;
401 if (!classinfo_implements_interface(*mlist++,interf))
408 merged_implements_interface(classinfo *typeclass,typeinfo_mergedlist *merged,
411 /* primitive types don't support interfaces. */
415 /* the null type can be cast to any interface type. */
416 if (typeclass == pseudo_class_Null)
419 /* check if typeclass implements the interface. */
420 if (classinfo_implements_interface(typeclass,interf))
423 /* check the mergedlist */
424 return (merged && mergedlist_implements_interface(merged,interf));
428 typeinfo_is_assignable(typeinfo *value,typeinfo *dest)
430 /* DEBUG CHECK: dest must not have a merged list. */
431 #ifdef TYPEINFO_DEBUG
433 panic("Internal error: typeinfo_is_assignable on merged destination.");
436 return typeinfo_is_assignable_to_classinfo(value,dest->typeclass);
440 typeinfo_is_assignable_to_classinfo(typeinfo *value,classinfo *dest)
444 cls = value->typeclass;
446 /* assignments of primitive values are not checked here. */
450 /* primitive and reference types are not assignment compatible. */
454 /* maybe we need to load and link the class */
461 #ifdef TYPEINFO_DEBUG
463 panic("Internal error: typeinfo_is_assignable_to_classinfo: unlinked class.");
466 /* the null type can be assigned to any type */
467 if (TYPEINFO_IS_NULLTYPE(*value))
470 /* uninitialized objects are not assignable */
471 if (TYPEINFO_IS_NEWOBJECT(*value))
474 if (dest->flags & ACC_INTERFACE) {
475 /* We are assigning to an interface type. */
476 return merged_implements_interface(cls,value->merged,dest);
479 if (CLASS_IS_ARRAY(dest)) {
480 arraydescriptor *arraydesc = dest->vftbl->arraydesc;
481 int dimension = arraydesc->dimension;
482 classinfo *elementclass = (arraydesc->elementvftbl)
483 ? arraydesc->elementvftbl->class : NULL;
485 /* We are assigning to an array type. */
486 if (!TYPEINFO_IS_ARRAY(*value))
489 /* {Both value and dest are array types.} */
491 /* value must have at least the dimension of dest. */
492 if (value->dimension < dimension)
495 if (value->dimension > dimension) {
496 /* value has higher dimension so we need to check
497 * if its component array can be assigned to the
498 * element type of dest */
500 if (!elementclass) return false;
502 if (elementclass->flags & ACC_INTERFACE) {
503 /* We are assigning to an interface type. */
504 return classinfo_implements_interface(pseudo_class_Arraystub,
508 /* We are assigning to a class type. */
509 return class_issubclass(pseudo_class_Arraystub,elementclass);
512 /* {value and dest have the same dimension} */
514 if (value->elementtype != arraydesc->elementtype)
517 if (value->elementclass) {
518 /* We are assigning an array of objects so we have to
519 * check if the elements are assignable.
522 if (elementclass->flags & ACC_INTERFACE) {
523 /* We are assigning to an interface type. */
525 return merged_implements_interface(value->elementclass,
530 /* We are assigning to a class type. */
531 return class_issubclass(value->elementclass,elementclass);
537 /* {dest is not an array} */
539 /* We are assigning to a class type */
540 if (cls->flags & ACC_INTERFACE)
541 cls = class_java_lang_Object;
543 return class_issubclass(cls,dest);
546 /**********************************************************************/
547 /* INITIALIZATION FUNCTIONS */
548 /* The following functions fill in uninitialized typeinfo structures. */
549 /**********************************************************************/
552 typeinfo_init_from_descriptor(typeinfo *info,char *utf_ptr,char *end_ptr)
557 cls = class_from_descriptor(utf_ptr,end_ptr,&end,
558 CLASSLOAD_NULLPRIMITIVE
561 | CLASSLOAD_CHECKEND);
570 /* a class, interface or array descriptor */
571 TYPEINFO_INIT_CLASSINFO(*info,cls);
573 /* a primitive type */
574 TYPEINFO_INIT_PRIMITIVE(*info);
579 typeinfo_count_method_args(utf *d,bool twoword)
582 char *utf_ptr = d->text;
583 char *end_pos = utf_end(d);
586 /* method descriptor must start with parenthesis */
587 if (*utf_ptr++ != '(') panic ("Missing '(' in method descriptor");
590 /* check arguments */
591 while ((c = *utf_ptr) != ')') {
592 class_from_descriptor(utf_ptr,end_pos,&utf_ptr,
593 CLASSLOAD_SKIP | CLASSLOAD_NOVOID
594 | CLASSLOAD_NULLPRIMITIVE);
596 if (twoword && (c == 'J' || c == 'D'))
597 /* primitive two-word type */
605 typeinfo_init_from_method_args(utf *desc,u1 *typebuf,typeinfo *infobuf,
606 int buflen,bool twoword,
607 int *returntype,typeinfo *returntypeinfo)
609 char *utf_ptr = desc->text; /* current position in utf text */
610 char *end_pos = utf_end(desc); /* points behind utf string */
614 /* method descriptor must start with parenthesis */
615 if (utf_ptr == end_pos || *utf_ptr++ != '(') panic ("Missing '(' in method descriptor");
617 /* check arguments */
618 while (utf_ptr != end_pos && *utf_ptr != ')') {
620 panic("Buffer too small for method arguments.");
622 *typebuf++ = type_from_descriptor(&cls,utf_ptr,end_pos,&utf_ptr,
624 | CLASSLOAD_NULLPRIMITIVE
634 TYPEINFO_INIT_CLASSINFO(*infobuf, cls);
637 TYPEINFO_INIT_PRIMITIVE(*infobuf);
641 if (twoword && (typebuf[-1] == TYPE_LONG || typebuf[-1] == TYPE_DOUBLE)) {
643 panic("Buffer too small for method arguments.");
644 *typebuf++ = TYPE_VOID;
645 TYPEINFO_INIT_PRIMITIVE(*infobuf);
649 utf_ptr++; /* skip ')' */
651 /* check returntype */
653 *returntype = type_from_descriptor(&cls,utf_ptr,end_pos,&utf_ptr,
654 CLASSLOAD_NULLPRIMITIVE
656 | CLASSLOAD_CHECKEND);
658 if (returntypeinfo) {
666 TYPEINFO_INIT_CLASSINFO(*returntypeinfo, cls);
669 TYPEINFO_INIT_PRIMITIVE(*returntypeinfo);
676 typedescriptors_init_from_method_args(typedescriptor *td,
678 int buflen,bool twoword,
679 typedescriptor *returntype)
681 char *utf_ptr = desc->text; /* current position in utf text */
682 char *end_pos = utf_end(desc); /* points behind utf string */
686 /* method descriptor must start with parenthesis */
687 if (utf_ptr == end_pos || *utf_ptr++ != '(') panic ("Missing '(' in method descriptor");
689 /* check arguments */
690 while (utf_ptr != end_pos && *utf_ptr != ')') {
692 panic("Buffer too small for method arguments.");
694 td->type = type_from_descriptor(&cls,utf_ptr,end_pos,&utf_ptr,
696 | CLASSLOAD_NULLPRIMITIVE
706 TYPEINFO_INIT_CLASSINFO(td->info, cls);
709 TYPEINFO_INIT_PRIMITIVE(td->info);
713 if (twoword && (td[-1].type == TYPE_LONG || td[-1].type == TYPE_DOUBLE)) {
715 panic("Buffer too small for method arguments.");
716 td->type = TYPE_VOID;
717 TYPEINFO_INIT_PRIMITIVE(td->info);
721 utf_ptr++; /* skip ')' */
723 /* check returntype */
725 returntype->type = type_from_descriptor(&cls,utf_ptr,end_pos,&utf_ptr,
726 CLASSLOAD_NULLPRIMITIVE
728 | CLASSLOAD_CHECKEND);
736 TYPEINFO_INIT_CLASSINFO(returntype->info,cls);
739 TYPEINFO_INIT_PRIMITIVE(returntype->info);
746 typeinfo_init_component(typeinfo *srcarray,typeinfo *dst)
750 if (TYPEINFO_IS_NULLTYPE(*srcarray)) {
751 TYPEINFO_INIT_NULLTYPE(*dst);
755 if (!TYPEINFO_IS_ARRAY(*srcarray))
756 panic("Trying to access component of non-array");
758 comp = srcarray->typeclass->vftbl->arraydesc->componentvftbl;
760 TYPEINFO_INIT_CLASSINFO(*dst,comp->class);
763 TYPEINFO_INIT_PRIMITIVE(*dst);
766 dst->merged = srcarray->merged;
770 typeinfo_clone(typeinfo *src,typeinfo *dest)
773 classinfo **srclist,**destlist;
781 count = src->merged->count;
782 TYPEINFO_ALLOCMERGED(dest->merged,count);
783 dest->merged->count = count;
785 srclist = src->merged->list;
786 destlist = dest->merged->list;
788 *destlist++ = *srclist++;
792 /**********************************************************************/
793 /* MISCELLANEOUS FUNCTIONS */
794 /**********************************************************************/
797 typeinfo_free(typeinfo *info)
799 TYPEINFO_FREEMERGED_IF_ANY(info->merged);
803 /**********************************************************************/
804 /* MERGING FUNCTIONS */
805 /* The following functions are used to merge the types represented by */
806 /* two typeinfo structures into one typeinfo structure. */
807 /**********************************************************************/
811 typeinfo_merge_error(char *str,typeinfo *x,typeinfo *y) {
812 #ifdef TYPEINFO_DEBUG
813 fprintf(stderr,"Error in typeinfo_merge: %s\n",str);
814 fprintf(stderr,"Typeinfo x:\n");
815 typeinfo_print(stderr,x,1);
816 fprintf(stderr,"Typeinfo y:\n");
817 typeinfo_print(stderr,y,1);
822 /* Condition: clsx != clsy. */
823 /* Returns: true if dest was changed (always true). */
826 typeinfo_merge_two(typeinfo *dest,classinfo *clsx,classinfo *clsy)
828 TYPEINFO_FREEMERGED_IF_ANY(dest->merged);
829 TYPEINFO_ALLOCMERGED(dest->merged,2);
830 dest->merged->count = 2;
832 #ifdef TYPEINFO_DEBUG
834 panic("Internal error: typeinfo_merge_two called with clsx==clsy.");
838 dest->merged->list[0] = clsx;
839 dest->merged->list[1] = clsy;
842 dest->merged->list[0] = clsy;
843 dest->merged->list[1] = clsx;
849 /* Returns: true if dest was changed. */
852 typeinfo_merge_add(typeinfo *dest,typeinfo_mergedlist *m,classinfo *cls)
855 typeinfo_mergedlist *newmerged;
856 classinfo **mlist,**newlist;
861 /* Check if cls is already in the mergedlist m. */
863 if (*mlist++ == cls) {
864 /* cls is in the list, so m is the resulting mergedlist */
865 if (dest->merged == m)
868 /* We have to copy the mergedlist */
869 TYPEINFO_FREEMERGED_IF_ANY(dest->merged);
871 TYPEINFO_ALLOCMERGED(dest->merged,count);
872 dest->merged->count = count;
873 newlist = dest->merged->list;
876 *newlist++ = *mlist++;
882 /* Add cls to the mergedlist. */
884 TYPEINFO_ALLOCMERGED(newmerged,count+1);
885 newmerged->count = count+1;
886 newlist = newmerged->list;
891 *newlist++ = *mlist++;
896 *newlist++ = *mlist++;
899 /* Put the new mergedlist into dest. */
900 TYPEINFO_FREEMERGED_IF_ANY(dest->merged);
901 dest->merged = newmerged;
906 /* Returns: true if dest was changed. */
909 typeinfo_merge_mergedlists(typeinfo *dest,typeinfo_mergedlist *x,
910 typeinfo_mergedlist *y)
914 typeinfo_mergedlist *temp,*result;
915 classinfo **clsx,**clsy,**newlist;
917 /* count the elements that will be in the resulting list */
918 /* (Both lists are sorted, equal elements are counted only once.) */
923 while (countx && county) {
924 if (*clsx == *clsy) {
930 else if (*clsx < *clsy) {
940 count += countx + county;
942 /* {The new mergedlist will have count entries.} */
944 if ((x->count != count) && (y->count == count)) {
945 temp = x; x = y; y = temp;
947 /* {If one of x,y is already the result it is x.} */
948 if (x->count == count) {
949 /* x->merged is equal to the result */
950 if (x == dest->merged)
953 if (!dest->merged || dest->merged->count != count) {
954 TYPEINFO_FREEMERGED_IF_ANY(dest->merged);
955 TYPEINFO_ALLOCMERGED(dest->merged,count);
956 dest->merged->count = count;
959 newlist = dest->merged->list;
962 *newlist++ = *clsx++;
967 /* {We have to merge two lists.} */
969 /* allocate the result list */
970 TYPEINFO_ALLOCMERGED(result,count);
971 result->count = count;
972 newlist = result->list;
974 /* merge the sorted lists */
979 while (countx && county) {
980 if (*clsx == *clsy) {
981 *newlist++ = *clsx++;
986 else if (*clsx < *clsy) {
987 *newlist++ = *clsx++;
991 *newlist++ = *clsy++;
996 *newlist++ = *clsx++;
998 *newlist++ = *clsy++;
1000 /* replace the list in dest with the result list */
1001 TYPEINFO_FREEMERGED_IF_ANY(dest->merged);
1002 dest->merged = result;
1009 typeinfo_merge_nonarrays(typeinfo *dest,
1011 classinfo *clsx,classinfo *clsy,
1012 typeinfo_mergedlist *mergedx,
1013 typeinfo_mergedlist *mergedy)
1015 classinfo *tcls,*common;
1016 typeinfo_mergedlist *tmerged;
1021 #ifdef TYPEINFO_DEBUG
1023 printf("typeinfo_merge_nonarrays:\n");
1024 TYPEINFO_INIT_CLASSINFO(dbgx,clsx);
1025 dbgx.merged = mergedx;
1026 TYPEINFO_INIT_CLASSINFO(dbgy,clsy);
1027 dbgy.merged = mergedy;
1028 typeinfo_print(stdout,&dbgx,4);
1030 typeinfo_print(stdout,&dbgy,4);
1034 /* Common case: clsx == clsy */
1035 /* (This case is very simple unless *both* x and y really represent
1036 * merges of subclasses of clsx==clsy.)
1038 if ((clsx == clsy) && (!mergedx || !mergedy)) {
1040 /* DEBUG */ /* log_text("return simple x"); */
1041 changed = (dest->merged != NULL);
1042 TYPEINFO_FREEMERGED_IF_ANY(dest->merged);
1043 dest->merged = NULL;
1045 /* DEBUG */ /* log_text("returning"); */
1049 /* If clsy is an interface, swap x and y. */
1050 if (clsy->flags & ACC_INTERFACE) {
1051 tcls = clsx; clsx = clsy; clsy = tcls;
1052 tmerged = mergedx; mergedx = mergedy; mergedy = tmerged;
1054 /* {We know: If only one of x,y is an interface it is x.} */
1056 /* Handle merging of interfaces: */
1057 if (clsx->flags & ACC_INTERFACE) {
1058 /* {clsx is an interface and mergedx == NULL.} */
1060 if (clsy->flags & ACC_INTERFACE) {
1061 /* We are merging two interfaces. */
1062 /* {mergedy == NULL} */
1064 /* {We know that clsx!=clsy (see common case at beginning.)} */
1065 *result = class_java_lang_Object;
1066 return typeinfo_merge_two(dest,clsx,clsy);
1069 /* {We know: x is an interface, clsy is a class.} */
1071 /* Check if we are merging an interface with java.lang.Object */
1072 if (clsy == class_java_lang_Object && !mergedy) {
1074 goto return_simple_x;
1078 /* If the type y implements clsx then the result of the merge
1079 * is clsx regardless of mergedy.
1082 if (CLASS_IMPLEMENTS_INTERFACE(clsy,clsx->index)
1083 || mergedlist_implements_interface(mergedy,clsx))
1085 /* y implements x, so the result of the merge is x. */
1086 goto return_simple_x;
1089 /* {We know: x is an interface, the type y a class or a merge
1090 * of subclasses and does not implement x.} */
1092 /* There may still be superinterfaces of x which are implemented
1093 * by y, too, so we have to add clsx to the mergedlist.
1096 /* if x has no superinterfaces we could return a simple java.lang.Object */
1098 common = class_java_lang_Object;
1099 goto merge_with_simple_x;
1102 /* {We know: x and y are classes (not interfaces).} */
1104 /* If *x is deeper in the inheritance hierarchy swap x and y. */
1105 if (clsx->index > clsy->index) {
1106 tcls = clsx; clsx = clsy; clsy = tcls;
1107 tmerged = mergedx; mergedx = mergedy; mergedy = tmerged;
1110 /* {We know: y is at least as deep in the hierarchy as x.} */
1112 /* Find nearest common anchestor for the classes. */
1115 while (tcls->index > common->index)
1117 while (common != tcls) {
1118 common = common->super;
1122 /* {common == nearest common anchestor of clsx and clsy.} */
1124 /* If clsx==common and x is a whole class (not a merge of subclasses)
1125 * then the result of the merge is clsx.
1127 if (clsx == common && !mergedx) {
1128 goto return_simple_x;
1134 return typeinfo_merge_mergedlists(dest,mergedx,mergedy);
1136 return typeinfo_merge_add(dest,mergedx,clsy);
1139 merge_with_simple_x:
1142 return typeinfo_merge_add(dest,mergedy,clsx);
1144 return typeinfo_merge_two(dest,clsx,clsy);
1147 /* Condition: *dest must be a valid initialized typeinfo. */
1148 /* Condition: dest != y. */
1149 /* Returns: true if dest was changed. */
1151 typeinfo_merge(typeinfo *dest,typeinfo* y)
1154 typeinfo *tmp; /* used for swapping */
1156 classinfo *elementclass;
1163 #ifdef TYPEINFO_DEBUG
1164 typeinfo_print(stdout,dest,4);
1165 typeinfo_print(stdout,y,4);
1169 /* Merging something with itself is a nop */
1173 /* Merging two returnAddress types is ok. */
1174 if (!dest->typeclass && !y->typeclass) {
1175 #ifdef TYPEINFO_DEBUG
1176 if (TYPEINFO_RETURNADDRESS(*dest) != TYPEINFO_RETURNADDRESS(*y))
1177 panic("Internal error: typeinfo_merge merges different returnAddresses");
1182 /* Primitive types cannot be merged with reference types */
1183 /* XXX only check this in debug mode? */
1184 if (!dest->typeclass || !y->typeclass)
1185 typeinfo_merge_error("Trying to merge primitive types.",dest,y);
1187 #ifdef TYPEINFO_DEBUG
1188 /* check that no unlinked classes are merged. */
1189 if (!dest->typeclass->linked || !y->typeclass->linked)
1190 typeinfo_merge_error("Trying to merge unlinked class(es).",dest,y);
1193 /* handle uninitialized object types */
1194 /* XXX is there a way we could put this after the common case? */
1195 if (TYPEINFO_IS_NEWOBJECT(*dest) || TYPEINFO_IS_NEWOBJECT(*y)) {
1196 if (!TYPEINFO_IS_NEWOBJECT(*dest) || !TYPEINFO_IS_NEWOBJECT(*y))
1197 typeinfo_merge_error("Trying to merge uninitialized object type.",dest,y);
1198 if (TYPEINFO_NEWOBJECT_INSTRUCTION(*dest)
1199 != TYPEINFO_NEWOBJECT_INSTRUCTION(*y))
1200 typeinfo_merge_error("Trying to merge different uninitialized objects.",dest,y);
1204 /* DEBUG */ /* log_text("Testing common case"); */
1206 /* Common case: class dest == class y */
1207 /* (This case is very simple unless *both* dest and y really represent
1208 * merges of subclasses of class dest==class y.)
1210 if ((dest->typeclass == y->typeclass) && (!dest->merged || !y->merged)) {
1211 changed = (dest->merged != NULL);
1212 TYPEINFO_FREEMERGED_IF_ANY(dest->merged); /* unify if? */
1213 dest->merged = NULL;
1214 /* DEBUG */ /* log_text("common case handled"); */
1218 /* DEBUG */ /* log_text("Handling null types"); */
1220 /* Handle null types: */
1221 if (TYPEINFO_IS_NULLTYPE(*y)) {
1224 if (TYPEINFO_IS_NULLTYPE(*dest)) {
1225 TYPEINFO_FREEMERGED_IF_ANY(dest->merged);
1226 TYPEINFO_CLONE(*y,*dest);
1230 /* This function uses x internally, so x and y can be swapped
1231 * without changing dest. */
1235 /* Handle merging of arrays: */
1236 if (TYPEINFO_IS_ARRAY(*x) && TYPEINFO_IS_ARRAY(*y)) {
1238 /* DEBUG */ /* log_text("Handling arrays"); */
1240 /* Make x the one with lesser dimension */
1241 if (x->dimension > y->dimension) {
1242 tmp = x; x = y; y = tmp;
1245 /* If one array (y) has higher dimension than the other,
1246 * interpret it as an array (same dim. as x) of Arraystubs. */
1247 if (x->dimension < y->dimension) {
1248 dimension = x->dimension;
1249 elementtype = ARRAYTYPE_OBJECT;
1250 elementclass = pseudo_class_Arraystub;
1253 dimension = y->dimension;
1254 elementtype = y->elementtype;
1255 elementclass = y->elementclass;
1258 /* {The arrays are of the same dimension.} */
1260 if (x->elementtype != elementtype) {
1261 /* Different element types are merged, so the resulting array
1262 * type has one accessible dimension less. */
1263 if (--dimension == 0) {
1264 common = pseudo_class_Arraystub;
1266 elementclass = NULL;
1269 common = class_multiarray_of(dimension,pseudo_class_Arraystub);
1270 elementtype = ARRAYTYPE_OBJECT;
1271 elementclass = pseudo_class_Arraystub;
1275 /* {The arrays have the same dimension and elementtype.} */
1277 if (elementtype == ARRAYTYPE_OBJECT) {
1278 /* The elements are references, so their respective
1279 * types must be merged.
1281 changed |= typeinfo_merge_nonarrays(dest,
1285 x->merged,y->merged);
1287 /* DEBUG */ /* log_text("finding resulting array class: "); */
1288 common = class_multiarray_of(dimension,elementclass);
1289 /* DEBUG */ /* utf_display(common->name); printf("\n"); */
1294 /* {We know that at least one of x or y is no array, so the
1295 * result cannot be an array.} */
1297 changed |= typeinfo_merge_nonarrays(dest,
1299 x->typeclass,y->typeclass,
1300 x->merged,y->merged);
1304 elementclass = NULL;
1307 /* Put the new values into dest if neccessary. */
1309 if (dest->typeclass != common) {
1310 dest->typeclass = common;
1313 if (dest->dimension != dimension) {
1314 dest->dimension = dimension;
1317 if (dest->elementtype != elementtype) {
1318 dest->elementtype = elementtype;
1321 if (dest->elementclass != elementclass) {
1322 dest->elementclass = elementclass;
1326 /* DEBUG */ /* log_text("returning from merge"); */
1332 /**********************************************************************/
1333 /* DEBUGGING HELPERS */
1334 /**********************************************************************/
1336 #ifdef TYPEINFO_DEBUG
1340 #include "jit/jit.h"
1342 extern instruction *instr;
1345 typeinfo_test_compare(classinfo **a,classinfo **b)
1347 if (*a == *b) return 0;
1348 if (*a < *b) return -1;
1353 typeinfo_test_parse(typeinfo *info,char *str)
1360 utf *desc = utf_new_char(str);
1362 num = typeinfo_count_method_args(desc,false);
1364 typebuf = DMNEW(u1,num);
1365 infobuf = DMNEW(typeinfo,num);
1367 typeinfo_init_from_method_args(desc,typebuf,infobuf,num,false,
1370 TYPEINFO_ALLOCMERGED(info->merged,num);
1371 info->merged->count = num;
1373 for (i=0; i<num; ++i) {
1374 if (typebuf[i] != TYPE_ADDRESS)
1375 panic("non-reference type in mergedlist");
1376 info->merged->list[i] = infobuf[i].typeclass;
1378 qsort(info->merged->list,num,sizeof(classinfo*),
1379 (int(*)(const void *,const void *))&typeinfo_test_compare);
1382 typeinfo_init_from_method_args(desc,NULL,NULL,0,false,
1387 #define TYPEINFO_TEST_BUFLEN 4000
1390 typeinfo_equal(typeinfo *x,typeinfo *y)
1394 if (x->typeclass != y->typeclass) return false;
1395 if (x->dimension != y->dimension) return false;
1397 if (x->elementclass != y->elementclass) return false;
1398 if (x->elementtype != y->elementtype) return false;
1401 if (TYPEINFO_IS_NEWOBJECT(*x))
1402 if (TYPEINFO_NEWOBJECT_INSTRUCTION(*x)
1403 != TYPEINFO_NEWOBJECT_INSTRUCTION(*y))
1406 if (x->merged || y->merged) {
1407 if (!(x->merged && y->merged)) return false;
1408 if (x->merged->count != y->merged->count) return false;
1409 for (i=0; i<x->merged->count; ++i)
1410 if (x->merged->list[i] != y->merged->list[i])
1417 typeinfo_testmerge(typeinfo *a,typeinfo *b,typeinfo *result,int *failed)
1420 bool changed,changed_should_be;
1422 TYPEINFO_CLONE(*a,dest);
1425 typeinfo_print_short(stdout,&dest);
1427 typeinfo_print_short(stdout,b);
1430 changed = (typeinfo_merge(&dest,b)) ? 1 : 0;
1431 changed_should_be = (!typeinfo_equal(&dest,a)) ? 1 : 0;
1433 printf(" %s\n",(changed) ? "changed" : "=");
1435 if (typeinfo_equal(&dest,result)) {
1437 typeinfo_print_short(stdout,&dest);
1439 if (changed != changed_should_be) {
1440 printf("WRONG RETURN VALUE!\n");
1446 typeinfo_print_short(stdout,&dest);
1448 printf("SHOULD BE ");
1449 typeinfo_print_short(stdout,result);
1456 typeinfo_inc_dimension(typeinfo *info)
1458 if (info->dimension++ == 0) {
1459 info->elementtype = ARRAYTYPE_OBJECT;
1460 info->elementclass = info->typeclass;
1462 info->typeclass = class_array_of(info->typeclass);
1465 #define TYPEINFO_TEST_MAXDIM 10
1468 typeinfo_testrun(char *filename)
1470 char buf[TYPEINFO_TEST_BUFLEN];
1471 char bufa[TYPEINFO_TEST_BUFLEN];
1472 char bufb[TYPEINFO_TEST_BUFLEN];
1473 char bufc[TYPEINFO_TEST_BUFLEN];
1477 FILE *file = fopen(filename,"rt");
1481 panic("could not open typeinfo test file");
1483 while (fgets(buf,TYPEINFO_TEST_BUFLEN,file)) {
1484 if (buf[0] == '#' || !strlen(buf))
1487 res = sscanf(buf,"%s\t%s\t%s\n",bufa,bufb,bufc);
1488 if (res != 3 || !strlen(bufa) || !strlen(bufb) || !strlen(bufc))
1489 panic("Invalid line in typeinfo test file (none of empty, comment or test)");
1491 typeinfo_test_parse(&a,bufa);
1492 typeinfo_test_parse(&b,bufb);
1493 typeinfo_test_parse(&c,bufc);
1495 typeinfo_testmerge(&a,&b,&c,&failed); /* check result */
1496 typeinfo_testmerge(&b,&a,&c,&failed); /* check commutativity */
1498 if (TYPEINFO_IS_NULLTYPE(a)) break;
1499 if (TYPEINFO_IS_NULLTYPE(b)) break;
1500 if (TYPEINFO_IS_NULLTYPE(c)) break;
1502 maxdim = a.dimension;
1503 if (b.dimension > maxdim) maxdim = b.dimension;
1504 if (c.dimension > maxdim) maxdim = c.dimension;
1506 if (maxdim < TYPEINFO_TEST_MAXDIM) {
1507 typeinfo_inc_dimension(&a);
1508 typeinfo_inc_dimension(&b);
1509 typeinfo_inc_dimension(&c);
1511 } while (maxdim < TYPEINFO_TEST_MAXDIM);
1517 fprintf(stderr,"Failed typeinfo_merge tests: %d\n",failed);
1518 panic("Failed test");
1525 log_text("Running typeinfo test file...");
1526 typeinfo_testrun("typeinfo.tst");
1527 log_text("Finished typeinfo test file.");
1531 typeinfo_init_from_fielddescriptor(typeinfo *info,char *desc)
1533 typeinfo_init_from_descriptor(info,desc,desc+strlen(desc));
1536 #define TYPEINFO_MAXINDENT 80
1539 typeinfo_print(FILE *file,typeinfo *info,int indent)
1542 char ind[TYPEINFO_MAXINDENT + 1];
1546 if (indent > TYPEINFO_MAXINDENT) indent = TYPEINFO_MAXINDENT;
1548 for (i=0; i<indent; ++i)
1552 if (TYPEINFO_IS_PRIMITIVE(*info)) {
1553 bptr = (basicblock*) TYPEINFO_RETURNADDRESS(*info);
1555 fprintf(file,"%sreturnAddress (L%03d)\n",ind,bptr->debug_nr);
1557 fprintf(file,"%sprimitive\n",ind);
1561 if (TYPEINFO_IS_NULLTYPE(*info)) {
1562 fprintf(file,"%snull\n",ind);
1566 if (TYPEINFO_IS_NEWOBJECT(*info)) {
1567 ins = (instruction *)TYPEINFO_NEWOBJECT_INSTRUCTION(*info);
1569 fprintf(file,"%sNEW(%d):",ind,ins-instr);
1570 utf_fprint(file,((classinfo *)ins[-1].val.a)->name);
1574 fprintf(file,"%sNEW(this)",ind);
1579 fprintf(file,"%sClass: ",ind);
1580 utf_fprint(file,info->typeclass->name);
1583 if (TYPEINFO_IS_ARRAY(*info)) {
1584 fprintf(file,"%sDimension: %d",ind,(int)info->dimension);
1585 fprintf(file,"\n%sElements: ",ind);
1586 switch (info->elementtype) {
1587 case ARRAYTYPE_INT : fprintf(file,"int\n"); break;
1588 case ARRAYTYPE_LONG : fprintf(file,"long\n"); break;
1589 case ARRAYTYPE_FLOAT : fprintf(file,"float\n"); break;
1590 case ARRAYTYPE_DOUBLE : fprintf(file,"double\n"); break;
1591 case ARRAYTYPE_BYTE : fprintf(file,"byte\n"); break;
1592 case ARRAYTYPE_CHAR : fprintf(file,"char\n"); break;
1593 case ARRAYTYPE_SHORT : fprintf(file,"short\n"); break;
1594 case ARRAYTYPE_BOOLEAN : fprintf(file,"boolean\n"); break;
1596 case ARRAYTYPE_OBJECT:
1597 fprintf(file,"reference: ");
1598 utf_fprint(file,info->elementclass->name);
1603 fprintf(file,"INVALID ARRAYTYPE!\n");
1608 fprintf(file,"%sMerged: ",ind);
1609 for (i=0; i<info->merged->count; ++i) {
1610 if (i) fprintf(file,", ");
1611 utf_fprint(file,info->merged->list[i]->name);
1618 typeinfo_print_short(FILE *file,typeinfo *info)
1624 if (TYPEINFO_IS_PRIMITIVE(*info)) {
1625 bptr = (basicblock*) TYPEINFO_RETURNADDRESS(*info);
1627 fprintf(file,"ret(L%03d)",bptr->debug_nr);
1629 fprintf(file,"primitive");
1633 if (TYPEINFO_IS_NULLTYPE(*info)) {
1634 fprintf(file,"null");
1638 if (TYPEINFO_IS_NEWOBJECT(*info)) {
1639 ins = (instruction *)TYPEINFO_NEWOBJECT_INSTRUCTION(*info);
1641 fprintf(file,"NEW(%d):",ins-instr);
1642 utf_fprint(file,((classinfo *)ins[-1].val.a)->name);
1645 fprintf(file,"NEW(this)");
1649 utf_fprint(file,info->typeclass->name);
1653 for (i=0; i<info->merged->count; ++i) {
1654 if (i) fprintf(file,",");
1655 utf_fprint(file,info->merged->list[i]->name);
1662 typeinfo_print_type(FILE *file,int type,typeinfo *info)
1665 case TYPE_VOID: fprintf(file,"V"); break;
1666 case TYPE_INT: fprintf(file,"I"); break;
1667 case TYPE_FLOAT: fprintf(file,"F"); break;
1668 case TYPE_DOUBLE: fprintf(file,"D"); break;
1669 case TYPE_LONG: fprintf(file,"J"); break;
1671 typeinfo_print_short(file,info);
1680 typeinfo_print_stacktype(FILE *file,int type,typeinfo *info)
1682 if (type == TYPE_ADDRESS && TYPEINFO_IS_PRIMITIVE(*info)) {
1683 typeinfo_retaddr_set *set = (typeinfo_retaddr_set*)
1684 TYPEINFO_RETURNADDRESS(*info);
1685 fprintf(file,"ret(L%03d",((basicblock*)(set->addr))->debug_nr);
1688 fprintf(file,"|L%03d",((basicblock*)(set->addr))->debug_nr);
1694 typeinfo_print_type(file,type,info);
1698 typedescriptor_print(FILE *file,typedescriptor *td)
1700 typeinfo_print_type(file,td->type,&(td->info));
1704 typevector_print(FILE *file,typevector *vec,int size)
1708 fprintf(file,"[%d]",vec->k);
1709 for (i=0; i<size; ++i) {
1710 fprintf(file," %d=",i);
1711 typedescriptor_print(file,vec->td + i);
1716 typevectorset_print(FILE *file,typevector *set,int size)
1721 fprintf(file,"[%d",set->k);
1724 fprintf(file,"|%d",vec->k);
1729 for (i=0; i<size; ++i) {
1730 fprintf(file," %d=",i);
1731 typedescriptor_print(file,set->td + i);
1735 typedescriptor_print(file,vec->td + i);
1741 #endif /* TYPEINFO_DEBUG */
1745 * These are local overrides for various environment variables in Emacs.
1746 * Please do not remove this and leave it at the end of the file, where
1747 * Emacs will automagically detect them.
1748 * ---------------------------------------------------------------------
1751 * indent-tabs-mode: t