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 922 2004-02-24 13:26:24Z edwin $
39 #include "toolbox/loging.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 #ifdef TYPEINFO_DEBUG
456 panic("Internal error: typeinfo_is_assignable_to_classinfo: unlinked class.");
459 /* the null type can be assigned to any type */
460 if (TYPEINFO_IS_NULLTYPE(*value))
463 /* uninitialized objects are not assignable */
464 if (TYPEINFO_IS_NEWOBJECT(*value))
467 if (dest->flags & ACC_INTERFACE) {
468 /* We are assigning to an interface type. */
469 return merged_implements_interface(cls,value->merged,dest);
472 if (CLASS_IS_ARRAY(dest)) {
473 arraydescriptor *arraydesc = dest->vftbl->arraydesc;
474 int dimension = arraydesc->dimension;
475 classinfo *elementclass = (arraydesc->elementvftbl)
476 ? arraydesc->elementvftbl->class : NULL;
478 /* We are assigning to an array type. */
479 if (!TYPEINFO_IS_ARRAY(*value))
482 /* {Both value and dest are array types.} */
484 /* value must have at least the dimension of dest. */
485 if (value->dimension < dimension)
488 if (value->dimension > dimension) {
489 /* value has higher dimension so we need to check
490 * if its component array can be assigned to the
491 * element type of dest */
493 if (!elementclass) return false;
495 if (elementclass->flags & ACC_INTERFACE) {
496 /* We are assigning to an interface type. */
497 return classinfo_implements_interface(pseudo_class_Arraystub,
501 /* We are assigning to a class type. */
502 return class_issubclass(pseudo_class_Arraystub,elementclass);
505 /* {value and dest have the same dimension} */
507 if (value->elementtype != arraydesc->elementtype)
510 if (value->elementclass) {
511 /* We are assigning an array of objects so we have to
512 * check if the elements are assignable.
515 if (elementclass->flags & ACC_INTERFACE) {
516 /* We are assigning to an interface type. */
518 return merged_implements_interface(value->elementclass,
523 /* We are assigning to a class type. */
524 return class_issubclass(value->elementclass,elementclass);
530 /* {dest is not an array} */
532 /* We are assigning to a class type */
533 if (cls->flags & ACC_INTERFACE)
534 cls = class_java_lang_Object;
536 return class_issubclass(cls,dest);
539 /**********************************************************************/
540 /* INITIALIZATION FUNCTIONS */
541 /* The following functions fill in uninitialized typeinfo structures. */
542 /**********************************************************************/
545 typeinfo_init_from_descriptor(typeinfo *info,char *utf_ptr,char *end_ptr)
550 cls = class_from_descriptor(utf_ptr,end_ptr,&end,
551 CLASSLOAD_NULLPRIMITIVE
554 | CLASSLOAD_CHECKEND);
557 /* a class, interface or array descriptor */
558 TYPEINFO_INIT_CLASSINFO(*info,cls);
560 /* a primitive type */
561 TYPEINFO_INIT_PRIMITIVE(*info);
565 typeinfo_count_method_args(utf *d,bool twoword)
568 char *utf_ptr = d->text;
569 char *end_pos = utf_end(d);
572 /* method descriptor must start with parenthesis */
573 if (*utf_ptr++ != '(') panic ("Missing '(' in method descriptor");
576 /* check arguments */
577 while ((c = *utf_ptr) != ')') {
578 class_from_descriptor(utf_ptr,end_pos,&utf_ptr,
579 CLASSLOAD_SKIP | CLASSLOAD_NOVOID
580 | CLASSLOAD_NULLPRIMITIVE);
582 if (twoword && (c == 'J' || c == 'D'))
583 /* primitive two-word type */
591 typeinfo_init_from_method_args(utf *desc,u1 *typebuf,typeinfo *infobuf,
592 int buflen,bool twoword,
593 int *returntype,typeinfo *returntypeinfo)
595 char *utf_ptr = desc->text; /* current position in utf text */
596 char *end_pos = utf_end(desc); /* points behind utf string */
600 /* method descriptor must start with parenthesis */
601 if (utf_ptr == end_pos || *utf_ptr++ != '(') panic ("Missing '(' in method descriptor");
603 /* check arguments */
604 while (utf_ptr != end_pos && *utf_ptr != ')') {
606 panic("Buffer too small for method arguments.");
608 *typebuf++ = type_from_descriptor(&cls,utf_ptr,end_pos,&utf_ptr,
610 | CLASSLOAD_NULLPRIMITIVE
614 TYPEINFO_INIT_CLASSINFO(*infobuf,cls);
616 TYPEINFO_INIT_PRIMITIVE(*infobuf);
620 if (twoword && (typebuf[-1] == TYPE_LONG || typebuf[-1] == TYPE_DOUBLE)) {
622 panic("Buffer too small for method arguments.");
623 *typebuf++ = TYPE_VOID;
624 TYPEINFO_INIT_PRIMITIVE(*infobuf);
628 utf_ptr++; /* skip ')' */
630 /* check returntype */
632 *returntype = type_from_descriptor(&cls,utf_ptr,end_pos,&utf_ptr,
633 CLASSLOAD_NULLPRIMITIVE
635 | CLASSLOAD_CHECKEND);
636 if (returntypeinfo) {
638 TYPEINFO_INIT_CLASSINFO(*returntypeinfo,cls);
640 TYPEINFO_INIT_PRIMITIVE(*returntypeinfo);
646 typedescriptors_init_from_method_args(typedescriptor *td,
648 int buflen,bool twoword,
649 typedescriptor *returntype)
651 char *utf_ptr = desc->text; /* current position in utf text */
652 char *end_pos = utf_end(desc); /* points behind utf string */
656 /* method descriptor must start with parenthesis */
657 if (utf_ptr == end_pos || *utf_ptr++ != '(') panic ("Missing '(' in method descriptor");
659 /* check arguments */
660 while (utf_ptr != end_pos && *utf_ptr != ')') {
662 panic("Buffer too small for method arguments.");
664 td->type = type_from_descriptor(&cls,utf_ptr,end_pos,&utf_ptr,
666 | CLASSLOAD_NULLPRIMITIVE
670 TYPEINFO_INIT_CLASSINFO(td->info,cls);
672 TYPEINFO_INIT_PRIMITIVE(td->info);
676 if (twoword && (td[-1].type == TYPE_LONG || td[-1].type == TYPE_DOUBLE)) {
678 panic("Buffer too small for method arguments.");
679 td->type = TYPE_VOID;
680 TYPEINFO_INIT_PRIMITIVE(td->info);
684 utf_ptr++; /* skip ')' */
686 /* check returntype */
688 returntype->type = type_from_descriptor(&cls,utf_ptr,end_pos,&utf_ptr,
689 CLASSLOAD_NULLPRIMITIVE
691 | CLASSLOAD_CHECKEND);
693 TYPEINFO_INIT_CLASSINFO(returntype->info,cls);
695 TYPEINFO_INIT_PRIMITIVE(returntype->info);
701 typeinfo_init_component(typeinfo *srcarray,typeinfo *dst)
705 if (TYPEINFO_IS_NULLTYPE(*srcarray)) {
706 TYPEINFO_INIT_NULLTYPE(*dst);
710 if (!TYPEINFO_IS_ARRAY(*srcarray))
711 panic("Trying to access component of non-array");
713 comp = srcarray->typeclass->vftbl->arraydesc->componentvftbl;
715 TYPEINFO_INIT_CLASSINFO(*dst,comp->class);
718 TYPEINFO_INIT_PRIMITIVE(*dst);
721 dst->merged = srcarray->merged;
725 typeinfo_clone(typeinfo *src,typeinfo *dest)
728 classinfo **srclist,**destlist;
736 count = src->merged->count;
737 TYPEINFO_ALLOCMERGED(dest->merged,count);
738 dest->merged->count = count;
740 srclist = src->merged->list;
741 destlist = dest->merged->list;
743 *destlist++ = *srclist++;
747 /**********************************************************************/
748 /* MISCELLANEOUS FUNCTIONS */
749 /**********************************************************************/
752 typeinfo_free(typeinfo *info)
754 TYPEINFO_FREEMERGED_IF_ANY(info->merged);
758 /**********************************************************************/
759 /* MERGING FUNCTIONS */
760 /* The following functions are used to merge the types represented by */
761 /* two typeinfo structures into one typeinfo structure. */
762 /**********************************************************************/
766 typeinfo_merge_error(char *str,typeinfo *x,typeinfo *y) {
767 #ifdef TYPEINFO_DEBUG
768 fprintf(stderr,"Error in typeinfo_merge: %s\n",str);
769 fprintf(stderr,"Typeinfo x:\n");
770 typeinfo_print(stderr,x,1);
771 fprintf(stderr,"Typeinfo y:\n");
772 typeinfo_print(stderr,y,1);
777 /* Condition: clsx != clsy. */
778 /* Returns: true if dest was changed (always true). */
781 typeinfo_merge_two(typeinfo *dest,classinfo *clsx,classinfo *clsy)
783 TYPEINFO_FREEMERGED_IF_ANY(dest->merged);
784 TYPEINFO_ALLOCMERGED(dest->merged,2);
785 dest->merged->count = 2;
787 #ifdef TYPEINFO_DEBUG
789 panic("Internal error: typeinfo_merge_two called with clsx==clsy.");
793 dest->merged->list[0] = clsx;
794 dest->merged->list[1] = clsy;
797 dest->merged->list[0] = clsy;
798 dest->merged->list[1] = clsx;
804 /* Returns: true if dest was changed. */
807 typeinfo_merge_add(typeinfo *dest,typeinfo_mergedlist *m,classinfo *cls)
810 typeinfo_mergedlist *newmerged;
811 classinfo **mlist,**newlist;
816 /* Check if cls is already in the mergedlist m. */
818 if (*mlist++ == cls) {
819 /* cls is in the list, so m is the resulting mergedlist */
820 if (dest->merged == m)
823 /* We have to copy the mergedlist */
824 TYPEINFO_FREEMERGED_IF_ANY(dest->merged);
826 TYPEINFO_ALLOCMERGED(dest->merged,count);
827 dest->merged->count = count;
828 newlist = dest->merged->list;
831 *newlist++ = *mlist++;
837 /* Add cls to the mergedlist. */
839 TYPEINFO_ALLOCMERGED(newmerged,count+1);
840 newmerged->count = count+1;
841 newlist = newmerged->list;
846 *newlist++ = *mlist++;
851 *newlist++ = *mlist++;
854 /* Put the new mergedlist into dest. */
855 TYPEINFO_FREEMERGED_IF_ANY(dest->merged);
856 dest->merged = newmerged;
861 /* Returns: true if dest was changed. */
864 typeinfo_merge_mergedlists(typeinfo *dest,typeinfo_mergedlist *x,
865 typeinfo_mergedlist *y)
869 typeinfo_mergedlist *temp,*result;
870 classinfo **clsx,**clsy,**newlist;
872 /* count the elements that will be in the resulting list */
873 /* (Both lists are sorted, equal elements are counted only once.) */
878 while (countx && county) {
879 if (*clsx == *clsy) {
885 else if (*clsx < *clsy) {
895 count += countx + county;
897 /* {The new mergedlist will have count entries.} */
899 if ((x->count != count) && (y->count == count)) {
900 temp = x; x = y; y = temp;
902 /* {If one of x,y is already the result it is x.} */
903 if (x->count == count) {
904 /* x->merged is equal to the result */
905 if (x == dest->merged)
908 if (!dest->merged || dest->merged->count != count) {
909 TYPEINFO_FREEMERGED_IF_ANY(dest->merged);
910 TYPEINFO_ALLOCMERGED(dest->merged,count);
911 dest->merged->count = count;
914 newlist = dest->merged->list;
917 *newlist++ = *clsx++;
922 /* {We have to merge two lists.} */
924 /* allocate the result list */
925 TYPEINFO_ALLOCMERGED(result,count);
926 result->count = count;
927 newlist = result->list;
929 /* merge the sorted lists */
934 while (countx && county) {
935 if (*clsx == *clsy) {
936 *newlist++ = *clsx++;
941 else if (*clsx < *clsy) {
942 *newlist++ = *clsx++;
946 *newlist++ = *clsy++;
951 *newlist++ = *clsx++;
953 *newlist++ = *clsy++;
955 /* replace the list in dest with the result list */
956 TYPEINFO_FREEMERGED_IF_ANY(dest->merged);
957 dest->merged = result;
964 typeinfo_merge_nonarrays(typeinfo *dest,
966 classinfo *clsx,classinfo *clsy,
967 typeinfo_mergedlist *mergedx,
968 typeinfo_mergedlist *mergedy)
970 classinfo *tcls,*common;
971 typeinfo_mergedlist *tmerged;
976 #ifdef TYPEINFO_DEBUG
978 printf("typeinfo_merge_nonarrays:\n");
979 TYPEINFO_INIT_CLASSINFO(dbgx,clsx);
980 dbgx.merged = mergedx;
981 TYPEINFO_INIT_CLASSINFO(dbgy,clsy);
982 dbgy.merged = mergedy;
983 typeinfo_print(stdout,&dbgx,4);
985 typeinfo_print(stdout,&dbgy,4);
989 /* Common case: clsx == clsy */
990 /* (This case is very simple unless *both* x and y really represent
991 * merges of subclasses of clsx==clsy.)
993 if ((clsx == clsy) && (!mergedx || !mergedy)) {
995 /* DEBUG */ /* log_text("return simple x"); */
996 changed = (dest->merged != NULL);
997 TYPEINFO_FREEMERGED_IF_ANY(dest->merged);
1000 /* DEBUG */ /* log_text("returning"); */
1004 /* If clsy is an interface, swap x and y. */
1005 if (clsy->flags & ACC_INTERFACE) {
1006 tcls = clsx; clsx = clsy; clsy = tcls;
1007 tmerged = mergedx; mergedx = mergedy; mergedy = tmerged;
1009 /* {We know: If only one of x,y is an interface it is x.} */
1011 /* Handle merging of interfaces: */
1012 if (clsx->flags & ACC_INTERFACE) {
1013 /* {clsx is an interface and mergedx == NULL.} */
1015 if (clsy->flags & ACC_INTERFACE) {
1016 /* We are merging two interfaces. */
1017 /* {mergedy == NULL} */
1019 /* {We know that clsx!=clsy (see common case at beginning.)} */
1020 *result = class_java_lang_Object;
1021 return typeinfo_merge_two(dest,clsx,clsy);
1024 /* {We know: x is an interface, clsy is a class.} */
1026 /* Check if we are merging an interface with java.lang.Object */
1027 if (clsy == class_java_lang_Object && !mergedy) {
1029 goto return_simple_x;
1033 /* If the type y implements clsx then the result of the merge
1034 * is clsx regardless of mergedy.
1037 if (CLASS_IMPLEMENTS_INTERFACE(clsy,clsx->index)
1038 || mergedlist_implements_interface(mergedy,clsx))
1040 /* y implements x, so the result of the merge is x. */
1041 goto return_simple_x;
1044 /* {We know: x is an interface, the type y a class or a merge
1045 * of subclasses and does not implement x.} */
1047 /* There may still be superinterfaces of x which are implemented
1048 * by y, too, so we have to add clsx to the mergedlist.
1051 /* if x has no superinterfaces we could return a simple java.lang.Object */
1053 common = class_java_lang_Object;
1054 goto merge_with_simple_x;
1057 /* {We know: x and y are classes (not interfaces).} */
1059 /* If *x is deeper in the inheritance hierarchy swap x and y. */
1060 if (clsx->index > clsy->index) {
1061 tcls = clsx; clsx = clsy; clsy = tcls;
1062 tmerged = mergedx; mergedx = mergedy; mergedy = tmerged;
1065 /* {We know: y is at least as deep in the hierarchy as x.} */
1067 /* Find nearest common anchestor for the classes. */
1070 while (tcls->index > common->index)
1072 while (common != tcls) {
1073 common = common->super;
1077 /* {common == nearest common anchestor of clsx and clsy.} */
1079 /* If clsx==common and x is a whole class (not a merge of subclasses)
1080 * then the result of the merge is clsx.
1082 if (clsx == common && !mergedx) {
1083 goto return_simple_x;
1089 return typeinfo_merge_mergedlists(dest,mergedx,mergedy);
1091 return typeinfo_merge_add(dest,mergedx,clsy);
1094 merge_with_simple_x:
1097 return typeinfo_merge_add(dest,mergedy,clsx);
1099 return typeinfo_merge_two(dest,clsx,clsy);
1102 /* Condition: *dest must be a valid initialized typeinfo. */
1103 /* Condition: dest != y. */
1104 /* Returns: true if dest was changed. */
1106 typeinfo_merge(typeinfo *dest,typeinfo* y)
1109 typeinfo *tmp; /* used for swapping */
1111 classinfo *elementclass;
1118 #ifdef TYPEINFO_DEBUG
1119 typeinfo_print(stdout,dest,4);
1120 typeinfo_print(stdout,y,4);
1124 /* Merging something with itself is a nop */
1128 /* Merging two returnAddress types is ok. */
1129 if (!dest->typeclass && !y->typeclass) {
1130 #ifdef TYPEINFO_DEBUG
1131 if (TYPEINFO_RETURNADDRESS(*dest) != TYPEINFO_RETURNADDRESS(*y))
1132 panic("Internal error: typeinfo_merge merges different returnAddresses");
1137 /* Primitive types cannot be merged with reference types */
1138 /* XXX only check this in debug mode? */
1139 if (!dest->typeclass || !y->typeclass)
1140 typeinfo_merge_error("Trying to merge primitive types.",dest,y);
1142 #ifdef TYPEINFO_DEBUG
1143 /* check that no unlinked classes are merged. */
1144 if (!dest->typeclass->linked || !y->typeclass->linked)
1145 typeinfo_merge_error("Trying to merge unlinked class(es).",dest,y);
1148 /* handle uninitialized object types */
1149 /* XXX is there a way we could put this after the common case? */
1150 if (TYPEINFO_IS_NEWOBJECT(*dest) || TYPEINFO_IS_NEWOBJECT(*y)) {
1151 if (!TYPEINFO_IS_NEWOBJECT(*dest) || !TYPEINFO_IS_NEWOBJECT(*y))
1152 typeinfo_merge_error("Trying to merge uninitialized object type.",dest,y);
1153 if (TYPEINFO_NEWOBJECT_INSTRUCTION(*dest)
1154 != TYPEINFO_NEWOBJECT_INSTRUCTION(*y))
1155 typeinfo_merge_error("Trying to merge different uninitialized objects.",dest,y);
1159 /* DEBUG */ /* log_text("Testing common case"); */
1161 /* Common case: class dest == class y */
1162 /* (This case is very simple unless *both* dest and y really represent
1163 * merges of subclasses of class dest==class y.)
1165 if ((dest->typeclass == y->typeclass) && (!dest->merged || !y->merged)) {
1166 changed = (dest->merged != NULL);
1167 TYPEINFO_FREEMERGED_IF_ANY(dest->merged); /* unify if? */
1168 dest->merged = NULL;
1169 /* DEBUG */ /* log_text("common case handled"); */
1173 /* DEBUG */ /* log_text("Handling null types"); */
1175 /* Handle null types: */
1176 if (TYPEINFO_IS_NULLTYPE(*y)) {
1179 if (TYPEINFO_IS_NULLTYPE(*dest)) {
1180 TYPEINFO_FREEMERGED_IF_ANY(dest->merged);
1181 TYPEINFO_CLONE(*y,*dest);
1185 /* This function uses x internally, so x and y can be swapped
1186 * without changing dest. */
1190 /* Handle merging of arrays: */
1191 if (TYPEINFO_IS_ARRAY(*x) && TYPEINFO_IS_ARRAY(*y)) {
1193 /* DEBUG */ /* log_text("Handling arrays"); */
1195 /* Make x the one with lesser dimension */
1196 if (x->dimension > y->dimension) {
1197 tmp = x; x = y; y = tmp;
1200 /* If one array (y) has higher dimension than the other,
1201 * interpret it as an array (same dim. as x) of Arraystubs. */
1202 if (x->dimension < y->dimension) {
1203 dimension = x->dimension;
1204 elementtype = ARRAYTYPE_OBJECT;
1205 elementclass = pseudo_class_Arraystub;
1208 dimension = y->dimension;
1209 elementtype = y->elementtype;
1210 elementclass = y->elementclass;
1213 /* {The arrays are of the same dimension.} */
1215 if (x->elementtype != elementtype) {
1216 /* Different element types are merged, so the resulting array
1217 * type has one accessible dimension less. */
1218 if (--dimension == 0) {
1219 common = pseudo_class_Arraystub;
1221 elementclass = NULL;
1224 common = class_multiarray_of(dimension,pseudo_class_Arraystub);
1225 elementtype = ARRAYTYPE_OBJECT;
1226 elementclass = pseudo_class_Arraystub;
1230 /* {The arrays have the same dimension and elementtype.} */
1232 if (elementtype == ARRAYTYPE_OBJECT) {
1233 /* The elements are references, so their respective
1234 * types must be merged.
1236 changed |= typeinfo_merge_nonarrays(dest,
1240 x->merged,y->merged);
1242 /* DEBUG */ /* log_text("finding resulting array class: "); */
1243 common = class_multiarray_of(dimension,elementclass);
1244 /* DEBUG */ /* utf_display(common->name); printf("\n"); */
1249 /* {We know that at least one of x or y is no array, so the
1250 * result cannot be an array.} */
1252 changed |= typeinfo_merge_nonarrays(dest,
1254 x->typeclass,y->typeclass,
1255 x->merged,y->merged);
1259 elementclass = NULL;
1262 /* Put the new values into dest if neccessary. */
1264 if (dest->typeclass != common) {
1265 dest->typeclass = common;
1268 if (dest->dimension != dimension) {
1269 dest->dimension = dimension;
1272 if (dest->elementtype != elementtype) {
1273 dest->elementtype = elementtype;
1276 if (dest->elementclass != elementclass) {
1277 dest->elementclass = elementclass;
1281 /* DEBUG */ /* log_text("returning from merge"); */
1287 /**********************************************************************/
1288 /* DEBUGGING HELPERS */
1289 /**********************************************************************/
1291 #ifdef TYPEINFO_DEBUG
1295 #include "jit/jit.h"
1297 extern instruction *instr;
1300 typeinfo_test_compare(classinfo **a,classinfo **b)
1302 if (*a == *b) return 0;
1303 if (*a < *b) return -1;
1308 typeinfo_test_parse(typeinfo *info,char *str)
1315 utf *desc = utf_new_char(str);
1317 num = typeinfo_count_method_args(desc,false);
1319 typebuf = DMNEW(u1,num);
1320 infobuf = DMNEW(typeinfo,num);
1322 typeinfo_init_from_method_args(desc,typebuf,infobuf,num,false,
1325 TYPEINFO_ALLOCMERGED(info->merged,num);
1326 info->merged->count = num;
1328 for (i=0; i<num; ++i) {
1329 if (typebuf[i] != TYPE_ADDRESS)
1330 panic("non-reference type in mergedlist");
1331 info->merged->list[i] = infobuf[i].typeclass;
1333 qsort(info->merged->list,num,sizeof(classinfo*),
1334 (int(*)(const void *,const void *))&typeinfo_test_compare);
1337 typeinfo_init_from_method_args(desc,NULL,NULL,0,false,
1342 #define TYPEINFO_TEST_BUFLEN 4000
1345 typeinfo_equal(typeinfo *x,typeinfo *y)
1349 if (x->typeclass != y->typeclass) return false;
1350 if (x->dimension != y->dimension) return false;
1352 if (x->elementclass != y->elementclass) return false;
1353 if (x->elementtype != y->elementtype) return false;
1356 if (TYPEINFO_IS_NEWOBJECT(*x))
1357 if (TYPEINFO_NEWOBJECT_INSTRUCTION(*x)
1358 != TYPEINFO_NEWOBJECT_INSTRUCTION(*y))
1361 if (x->merged || y->merged) {
1362 if (!(x->merged && y->merged)) return false;
1363 if (x->merged->count != y->merged->count) return false;
1364 for (i=0; i<x->merged->count; ++i)
1365 if (x->merged->list[i] != y->merged->list[i])
1372 typeinfo_testmerge(typeinfo *a,typeinfo *b,typeinfo *result,int *failed)
1375 bool changed,changed_should_be;
1377 TYPEINFO_CLONE(*a,dest);
1380 typeinfo_print_short(stdout,&dest);
1382 typeinfo_print_short(stdout,b);
1385 changed = (typeinfo_merge(&dest,b)) ? 1 : 0;
1386 changed_should_be = (!typeinfo_equal(&dest,a)) ? 1 : 0;
1388 printf(" %s\n",(changed) ? "changed" : "=");
1390 if (typeinfo_equal(&dest,result)) {
1392 typeinfo_print_short(stdout,&dest);
1394 if (changed != changed_should_be) {
1395 printf("WRONG RETURN VALUE!\n");
1401 typeinfo_print_short(stdout,&dest);
1403 printf("SHOULD BE ");
1404 typeinfo_print_short(stdout,result);
1411 typeinfo_inc_dimension(typeinfo *info)
1413 if (info->dimension++ == 0) {
1414 info->elementtype = ARRAYTYPE_OBJECT;
1415 info->elementclass = info->typeclass;
1417 info->typeclass = class_array_of(info->typeclass);
1420 #define TYPEINFO_TEST_MAXDIM 10
1423 typeinfo_testrun(char *filename)
1425 char buf[TYPEINFO_TEST_BUFLEN];
1426 char bufa[TYPEINFO_TEST_BUFLEN];
1427 char bufb[TYPEINFO_TEST_BUFLEN];
1428 char bufc[TYPEINFO_TEST_BUFLEN];
1432 FILE *file = fopen(filename,"rt");
1436 panic("could not open typeinfo test file");
1438 while (fgets(buf,TYPEINFO_TEST_BUFLEN,file)) {
1439 if (buf[0] == '#' || !strlen(buf))
1442 res = sscanf(buf,"%s\t%s\t%s\n",bufa,bufb,bufc);
1443 if (res != 3 || !strlen(bufa) || !strlen(bufb) || !strlen(bufc))
1444 panic("Invalid line in typeinfo test file (none of empty, comment or test)");
1446 typeinfo_test_parse(&a,bufa);
1447 typeinfo_test_parse(&b,bufb);
1448 typeinfo_test_parse(&c,bufc);
1450 typeinfo_testmerge(&a,&b,&c,&failed); /* check result */
1451 typeinfo_testmerge(&b,&a,&c,&failed); /* check commutativity */
1453 if (TYPEINFO_IS_NULLTYPE(a)) break;
1454 if (TYPEINFO_IS_NULLTYPE(b)) break;
1455 if (TYPEINFO_IS_NULLTYPE(c)) break;
1457 maxdim = a.dimension;
1458 if (b.dimension > maxdim) maxdim = b.dimension;
1459 if (c.dimension > maxdim) maxdim = c.dimension;
1461 if (maxdim < TYPEINFO_TEST_MAXDIM) {
1462 typeinfo_inc_dimension(&a);
1463 typeinfo_inc_dimension(&b);
1464 typeinfo_inc_dimension(&c);
1466 } while (maxdim < TYPEINFO_TEST_MAXDIM);
1472 fprintf(stderr,"Failed typeinfo_merge tests: %d\n",failed);
1473 panic("Failed test");
1480 log_text("Running typeinfo test file...");
1481 typeinfo_testrun("typeinfo.tst");
1482 log_text("Finished typeinfo test file.");
1486 typeinfo_init_from_fielddescriptor(typeinfo *info,char *desc)
1488 typeinfo_init_from_descriptor(info,desc,desc+strlen(desc));
1491 #define TYPEINFO_MAXINDENT 80
1494 typeinfo_print(FILE *file,typeinfo *info,int indent)
1497 char ind[TYPEINFO_MAXINDENT + 1];
1501 if (indent > TYPEINFO_MAXINDENT) indent = TYPEINFO_MAXINDENT;
1503 for (i=0; i<indent; ++i)
1507 if (TYPEINFO_IS_PRIMITIVE(*info)) {
1508 bptr = (basicblock*) TYPEINFO_RETURNADDRESS(*info);
1510 fprintf(file,"%sreturnAddress (L%03d)\n",ind,bptr->debug_nr);
1512 fprintf(file,"%sprimitive\n",ind);
1516 if (TYPEINFO_IS_NULLTYPE(*info)) {
1517 fprintf(file,"%snull\n",ind);
1521 if (TYPEINFO_IS_NEWOBJECT(*info)) {
1522 ins = (instruction *)TYPEINFO_NEWOBJECT_INSTRUCTION(*info);
1524 fprintf(file,"%sNEW(%d):",ind,ins-instr);
1525 utf_fprint(file,((classinfo *)ins[-1].val.a)->name);
1529 fprintf(file,"%sNEW(this)",ind);
1534 fprintf(file,"%sClass: ",ind);
1535 utf_fprint(file,info->typeclass->name);
1538 if (TYPEINFO_IS_ARRAY(*info)) {
1539 fprintf(file,"%sDimension: %d",ind,(int)info->dimension);
1540 fprintf(file,"\n%sElements: ",ind);
1541 switch (info->elementtype) {
1542 case ARRAYTYPE_INT : fprintf(file,"int\n"); break;
1543 case ARRAYTYPE_LONG : fprintf(file,"long\n"); break;
1544 case ARRAYTYPE_FLOAT : fprintf(file,"float\n"); break;
1545 case ARRAYTYPE_DOUBLE : fprintf(file,"double\n"); break;
1546 case ARRAYTYPE_BYTE : fprintf(file,"byte\n"); break;
1547 case ARRAYTYPE_CHAR : fprintf(file,"char\n"); break;
1548 case ARRAYTYPE_SHORT : fprintf(file,"short\n"); break;
1549 case ARRAYTYPE_BOOLEAN : fprintf(file,"boolean\n"); break;
1551 case ARRAYTYPE_OBJECT:
1552 fprintf(file,"reference: ");
1553 utf_fprint(file,info->elementclass->name);
1558 fprintf(file,"INVALID ARRAYTYPE!\n");
1563 fprintf(file,"%sMerged: ",ind);
1564 for (i=0; i<info->merged->count; ++i) {
1565 if (i) fprintf(file,", ");
1566 utf_fprint(file,info->merged->list[i]->name);
1573 typeinfo_print_short(FILE *file,typeinfo *info)
1579 if (TYPEINFO_IS_PRIMITIVE(*info)) {
1580 bptr = (basicblock*) TYPEINFO_RETURNADDRESS(*info);
1582 fprintf(file,"ret(L%03d)",bptr->debug_nr);
1584 fprintf(file,"primitive");
1588 if (TYPEINFO_IS_NULLTYPE(*info)) {
1589 fprintf(file,"null");
1593 if (TYPEINFO_IS_NEWOBJECT(*info)) {
1594 ins = (instruction *)TYPEINFO_NEWOBJECT_INSTRUCTION(*info);
1596 fprintf(file,"NEW(%d):",ins-instr);
1597 utf_fprint(file,((classinfo *)ins[-1].val.a)->name);
1600 fprintf(file,"NEW(this)");
1604 utf_fprint(file,info->typeclass->name);
1608 for (i=0; i<info->merged->count; ++i) {
1609 if (i) fprintf(file,",");
1610 utf_fprint(file,info->merged->list[i]->name);
1617 typeinfo_print_type(FILE *file,int type,typeinfo *info)
1620 case TYPE_VOID: fprintf(file,"V"); break;
1621 case TYPE_INT: fprintf(file,"I"); break;
1622 case TYPE_FLOAT: fprintf(file,"F"); break;
1623 case TYPE_DOUBLE: fprintf(file,"D"); break;
1624 case TYPE_LONG: fprintf(file,"J"); break;
1626 typeinfo_print_short(file,info);
1635 typeinfo_print_stacktype(FILE *file,int type,typeinfo *info)
1637 if (type == TYPE_ADDRESS && TYPEINFO_IS_PRIMITIVE(*info)) {
1638 typeinfo_retaddr_set *set = (typeinfo_retaddr_set*)
1639 TYPEINFO_RETURNADDRESS(*info);
1640 fprintf(file,"ret(L%03d",((basicblock*)(set->addr))->debug_nr);
1643 fprintf(file,"|L%03d",((basicblock*)(set->addr))->debug_nr);
1649 typeinfo_print_type(file,type,info);
1653 typedescriptor_print(FILE *file,typedescriptor *td)
1655 typeinfo_print_type(file,td->type,&(td->info));
1659 typevector_print(FILE *file,typevector *vec,int size)
1663 fprintf(file,"[%d]",vec->k);
1664 for (i=0; i<size; ++i) {
1665 fprintf(file," %d=",i);
1666 typedescriptor_print(file,vec->td + i);
1671 typevectorset_print(FILE *file,typevector *set,int size)
1676 fprintf(file,"[%d",set->k);
1679 fprintf(file,"|%d",vec->k);
1684 for (i=0; i<size; ++i) {
1685 fprintf(file," %d=",i);
1686 typedescriptor_print(file,set->td + i);
1690 typedescriptor_print(file,vec->td + i);
1696 #endif /* TYPEINFO_DEBUG */
1700 * These are local overrides for various environment variables in Emacs.
1701 * Please do not remove this and leave it at the end of the file, where
1702 * Emacs will automagically detect them.
1703 * ---------------------------------------------------------------------
1706 * indent-tabs-mode: t