X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Ftr%2Ftr_inheritance.c;h=7e086e49f7c1f3dfa2271c5670ace96331fedb53;hb=ff244fb7355c6120cf0f15ba7911b473bb91c64b;hp=facfd58b99c6fb4eda292740783dc835499d73a0;hpb=cb99d2d21e049c4cf5e802a8f16fa9ddb52f3e6d;p=libfirm diff --git a/ir/tr/tr_inheritance.c b/ir/tr/tr_inheritance.c index facfd58b9..7e086e49f 100644 --- a/ir/tr/tr_inheritance.c +++ b/ir/tr/tr_inheritance.c @@ -1,9 +1,9 @@ /** * - * @file tp_inheritance.c + * @file tr_inheritance.c * * Project: libFIRM
- * File name: ir/tr/tp_inheritance.c
+ * File name: ir/tr/tr_inheritance.c
* Purpose: Utility routines for inheritance representation
* Author: Goetz Lindenmaier
* Modified by:
@@ -20,31 +20,33 @@ #include "type.h" #include "entity.h" #include "typewalk.h" +#include "irgraph_t.h" #include "irprog_t.h" #include "pset.h" #include "set.h" #include "mangle.h" +#include "irgwalk.h" +#include "irflag.h" //#include ".h" - /* ----------------------------------------------------------------------- */ /* Resolve implicit inheritance. */ /* ----------------------------------------------------------------------- */ -ident *default_mangle_inherited_name(entity *super, type *clss) { - return mangle_u(get_type_ident(clss), get_entity_ident(super)); +ident *default_mangle_inherited_name(entity *super, ir_type *clss) { + return mangle_u(new_id_from_str("inh"), mangle_u(get_type_ident(clss), get_entity_ident(super))); } /** Replicates all entities in all super classes that are not overwritten by an entity of this class. */ -static void copy_entities_from_superclass(type *clss, void *env) +static void copy_entities_from_superclass(ir_type *clss, void *env) { int i, j, k, l; int overwritten; - type *super, *inhenttype; + ir_type *super, *inhenttype; entity *inhent, *thisent; - mangle_inherited_name_func *mfunc = (mangle_inherited_name_func *)env; + mangle_inherited_name_func *mfunc = *(mangle_inherited_name_func **)env; for(i = 0; i < get_class_n_supertypes(clss); i++) { super = get_class_supertype(clss, i); @@ -55,27 +57,28 @@ static void copy_entities_from_superclass(type *clss, void *env) /* check whether inhent is already overwritten */ overwritten = 0; for (k = 0; (k < get_class_n_members(clss)) && (overwritten == 0); k++) { - thisent = get_class_member(clss, k); - for(l = 0; l < get_entity_n_overwrites(thisent); l++) { - if(inhent == get_entity_overwrites(thisent, l)) { - /* overwritten - do not copy */ - overwritten = 1; - break; - } - } + thisent = get_class_member(clss, k); + for(l = 0; l < get_entity_n_overwrites(thisent); l++) { + if(inhent == get_entity_overwrites(thisent, l)) { + /* overwritten - do not copy */ + overwritten = 1; + break; + } + } } /* Inherit entity */ if (!overwritten) { - thisent = copy_entity_own(inhent, clss); - add_entity_overwrites(thisent, inhent); - set_entity_peculiarity(thisent, peculiarity_inherited); - set_entity_ld_ident(thisent, mfunc(inhent, clss)); - if (get_entity_variability(inhent) == variability_constant) { - assert(is_atomic_entity(inhent) && /* @@@ */ - "Inheritance of constant, compound entities not implemented"); - set_entity_variability(thisent, variability_constant); - set_atomic_ent_value(thisent, get_atomic_ent_value(inhent)); - } + thisent = copy_entity_own(inhent, clss); + add_entity_overwrites(thisent, inhent); + if (get_entity_peculiarity(inhent) == peculiarity_existent) + set_entity_peculiarity(thisent, peculiarity_inherited); + set_entity_ld_ident(thisent, mfunc(inhent, clss)); + if (get_entity_variability(inhent) == variability_constant) { + assert(is_atomic_entity(inhent) && /* @@@ */ + "Inheritance of constant, compound entities not implemented"); + set_entity_variability(thisent, variability_constant); + set_atomic_ent_value(thisent, get_atomic_ent_value(inhent)); + } } } } @@ -88,7 +91,7 @@ static void copy_entities_from_superclass(type *clss, void *env) void resolve_inheritance(mangle_inherited_name_func *mfunc) { if (!mfunc) mfunc = default_mangle_inherited_name; - class_walk_super2sub(copy_entities_from_superclass, NULL, (void *)mfunc); + class_walk_super2sub(copy_entities_from_superclass, NULL, (void *)&mfunc); } @@ -129,10 +132,14 @@ static void assert_valid_state(void) { /* arrays) listing all subtypes... */ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ +typedef enum { + d_up = 0, + d_down = 1, +} dir; + typedef struct { firm_kind *kind; /* An entity or type. */ - pset *up; - pset *down; + pset *directions[2]; } tr_inh_trans_tp; /* We use this set for all types and entities. */ @@ -145,15 +152,10 @@ static int tr_inh_trans_cmp(const void *e1, const void *e2, size_t size) { } static INLINE unsigned int tr_inh_trans_hash(void *e) { - void *v = (void *) ((tr_inh_trans_tp *)e)->kind; - return HASH_PTR(v); + tr_inh_trans_tp *v = e; + return HASH_PTR(v->kind); } -typedef enum { - d_up, - d_down, -} dir; - /* This always completes successfully. */ static tr_inh_trans_tp* get_firm_kind_entry(firm_kind *k) { tr_inh_trans_tp a, *found; @@ -163,8 +165,8 @@ static tr_inh_trans_tp* get_firm_kind_entry(firm_kind *k) { found = set_find(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a)); if (!found) { - a.up = pset_new_ptr(16); - a.down = pset_new_ptr(16); + a.directions[d_up] = pset_new_ptr(16); + a.directions[d_down] = pset_new_ptr(16); found = set_insert(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a)); } return found; @@ -175,7 +177,7 @@ static pset *get_entity_map(entity *ent, dir d) { assert(is_entity(ent)); found = get_firm_kind_entry((firm_kind *)ent); - return (d == d_up) ? found->up : found->down; + return found->directions[d]; } /* static void add_entity_map(entity *ent, dir d, entity *new) { @@ -183,27 +185,23 @@ static void add_entity_map(entity *ent, dir d, entity *new) { assert(is_entity(ent) && is_entity(new)); tr_inh_trans_tp *found = get_firm_kind_entry((firm_kind *)ent); - if (d == d_up) - pset_insert_ptr(found->up, new); - else - pset_insert_ptr(found->down, new); + pset_insert_ptr(found->directions[d], new); } */ -static pset *get_type_map(type *tp, dir d) { +static pset *get_type_map(ir_type *tp, dir d) { tr_inh_trans_tp *found; assert(is_type(tp)); found = get_firm_kind_entry((firm_kind *)tp); - return (d == d_up) ? found->up : found->down; + return found->directions[d]; } /* -static void add_type_map(type *tp, dir d, type *new) { +static void add_type_map(ir_type *tp, dir d, type *new) { tr_inh_trans_tp *found; assert(is_type(tp) && is_type(new)); found = get_firm_kind_entry((firm_kind *)tp); - if (d == d_up) pset_insert_ptr(found->up, new); - else pset_insert_ptr(found->down, new); + pset_insert_ptr(found->directions[d], new); } */ @@ -224,7 +222,7 @@ static void add_type_map(type *tp, dir d, type *new) { * If it is marked with master_flag_visited it is fully processed. * * Well, we still miss some candidates ... */ -static void compute_down_closure(type *tp) { +static void compute_down_closure(ir_type *tp) { pset *myset, *subset; int i, n_subtypes, n_members, n_supertypes; unsigned long master_visited = get_master_type_visited(); @@ -236,9 +234,8 @@ static void compute_down_closure(type *tp) { /* Recursive descend. */ n_subtypes = get_class_n_subtypes(tp); for (i = 0; i < n_subtypes; ++i) { - type *stp = get_class_subtype(tp, i); - if (type_not_visited(stp)) { - assert(get_type_visited(tp) < master_visited-1); + ir_type *stp = get_class_subtype(tp, i); + if (get_type_visited(stp) < master_visited-1) { compute_down_closure(stp); } } @@ -246,7 +243,7 @@ static void compute_down_closure(type *tp) { /* types */ myset = get_type_map(tp, d_down); for (i = 0; i < n_subtypes; ++i) { - type *stp = get_class_subtype(tp, i); + ir_type *stp = get_class_subtype(tp, i); subset = get_type_map(stp, d_down); pset_insert_ptr(myset, stp); pset_insert_pset_ptr(myset, subset); @@ -259,11 +256,11 @@ static void compute_down_closure(type *tp) { int j, n_overwrittenby = get_entity_n_overwrittenby(mem); myset = get_entity_map(mem, d_down); - for (j = 0; j > n_overwrittenby; ++j) { + for (j = 0; j < n_overwrittenby; ++j) { entity *ov = get_entity_overwrittenby(mem, j); subset = get_entity_map(ov, d_down); - pset_insert_pset_ptr(myset, subset); pset_insert_ptr(myset, ov); + pset_insert_pset_ptr(myset, subset); } } @@ -272,17 +269,17 @@ static void compute_down_closure(type *tp) { /* Walk up. */ n_supertypes = get_class_n_supertypes(tp); for (i = 0; i < n_supertypes; ++i) { - type *stp = get_class_supertype(tp, i); - if (get_type_visited(tp) < master_visited-1) { + ir_type *stp = get_class_supertype(tp, i); + if (get_type_visited(stp) < master_visited-1) { compute_down_closure(stp); } } } -static void compute_up_closure(type *tp) { +static void compute_up_closure(ir_type *tp) { pset *myset, *subset; int i, n_subtypes, n_members, n_supertypes; - int master_visited = get_master_type_visited(); + unsigned long master_visited = get_master_type_visited(); assert(is_Class_type(tp)); @@ -291,9 +288,8 @@ static void compute_up_closure(type *tp) { /* Recursive descend. */ n_supertypes = get_class_n_supertypes(tp); for (i = 0; i < n_supertypes; ++i) { - type *stp = get_class_supertype(tp, i); - if (type_not_visited(stp)) { - assert(get_type_visited(tp) < get_master_type_visited()-1); + ir_type *stp = get_class_supertype(tp, i); + if (get_type_visited(stp) < get_master_type_visited()-1) { compute_up_closure(stp); } } @@ -301,7 +297,7 @@ static void compute_up_closure(type *tp) { /* types */ myset = get_type_map(tp, d_up); for (i = 0; i < n_supertypes; ++i) { - type *stp = get_class_supertype(tp, i); + ir_type *stp = get_class_supertype(tp, i); subset = get_type_map(stp, d_up); pset_insert_ptr(myset, stp); pset_insert_pset_ptr(myset, subset); @@ -314,7 +310,7 @@ static void compute_up_closure(type *tp) { int j, n_overwrites = get_entity_n_overwrites(mem); myset = get_entity_map(mem, d_up); - for (j = 0; j > n_overwrites; ++j) { + for (j = 0; j < n_overwrites; ++j) { entity *ov = get_entity_overwrites(mem, j); subset = get_entity_map(ov, d_up); pset_insert_pset_ptr(myset, subset); @@ -327,8 +323,8 @@ static void compute_up_closure(type *tp) { /* Walk down. */ n_subtypes = get_class_n_subtypes(tp); for (i = 0; i < n_subtypes; ++i) { - type *stp = get_class_subtype(tp, i); - if (get_type_visited(tp) < master_visited-1) { + ir_type *stp = get_class_subtype(tp, i); + if (get_type_visited(stp) < master_visited-1) { compute_up_closure(stp); } } @@ -347,20 +343,23 @@ void compute_inh_transitive_closure(void) { inc_master_type_visited(); /* Inc twice: one if on stack, second if values computed. */ inc_master_type_visited(); for (i = 0; i < n_types; ++i) { - type *tp = get_irp_type(i); + ir_type *tp = get_irp_type(i); if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */ int j, n_subtypes = get_class_n_subtypes(tp); - int has_unmarked_subtype = false; + int has_unmarked_subtype = 0; assert(get_type_visited(tp) < get_master_type_visited()-1); - for (j = 0; j < n_subtypes && !has_unmarked_subtype; ++j) { - type *stp = get_class_subtype(tp, j); - if (type_not_visited(stp)) has_unmarked_subtype = true; + for (j = 0; j < n_subtypes; ++j) { + ir_type *stp = get_class_subtype(tp, j); + if (type_not_visited(stp)) { + has_unmarked_subtype = 1; + break; + } } /* This is a good starting point. */ if (!has_unmarked_subtype) - compute_down_closure(tp); + compute_down_closure(tp); } } @@ -368,20 +367,23 @@ void compute_inh_transitive_closure(void) { inc_master_type_visited(); inc_master_type_visited(); for (i = 0; i < n_types; ++i) { - type *tp = get_irp_type(i); + ir_type *tp = get_irp_type(i); if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */ int j, n_supertypes = get_class_n_supertypes(tp); - int has_unmarked_supertype = false; + int has_unmarked_supertype = 0; assert(get_type_visited(tp) < get_master_type_visited()-1); - for (j = 0; j < n_supertypes && !has_unmarked_supertype; ++j) { - type *stp = get_class_supertype(tp, j); - if (type_not_visited(stp)) has_unmarked_supertype = true; + for (j = 0; j < n_supertypes; ++j) { + ir_type *stp = get_class_supertype(tp, j); + if (type_not_visited(stp)) { + has_unmarked_supertype = 1; + break; + } } /* This is a good starting point. */ if (!has_unmarked_supertype) - compute_up_closure(tp); + compute_up_closure(tp); } } @@ -393,8 +395,8 @@ void free_inh_transitive_closure(void) { if (tr_inh_trans_set) { tr_inh_trans_tp *elt; for (elt = set_first(tr_inh_trans_set); elt; elt = set_next(tr_inh_trans_set)) { - del_pset(elt->up); - del_pset(elt->down); + del_pset(elt->directions[d_up]); + del_pset(elt->directions[d_down]); } del_set(tr_inh_trans_set); tr_inh_trans_set = NULL; @@ -404,24 +406,29 @@ void free_inh_transitive_closure(void) { /* - subtype ------------------------------------------------------------- */ -type *get_class_trans_subtype_first(type *tp) { +ir_type *get_class_trans_subtype_first(ir_type *tp) { assert_valid_state(); return pset_first(get_type_map(tp, d_down)); } -type *get_class_trans_subtype_next (type *tp) { +ir_type *get_class_trans_subtype_next (ir_type *tp) { assert_valid_state(); return pset_next(get_type_map(tp, d_down)); } +int is_class_trans_subtype (ir_type *tp, ir_type *subtp) { + assert_valid_state(); + return (pset_find_ptr(get_type_map(tp, d_down), subtp) != NULL); +} + /* - supertype ----------------------------------------------------------- */ -type *get_class_trans_supertype_first(type *tp) { +ir_type *get_class_trans_supertype_first(ir_type *tp) { assert_valid_state(); return pset_first(get_type_map(tp, d_up)); } -type *get_class_trans_supertype_next (type *tp) { +ir_type *get_class_trans_supertype_next (ir_type *tp) { assert_valid_state(); return pset_next(get_type_map(tp, d_up)); } @@ -461,7 +468,7 @@ entity *get_entity_trans_overwrites_next (entity *ent) { /* ----------------------------------------------------------------------- */ /* Returns true if low is subclass of high. */ -int is_subclass_of(type *low, type *high) { +int is_SubClass_of(ir_type *low, ir_type *high) { int i, n_subtypes; assert(is_Class_type(low) && is_Class_type(high)); @@ -475,14 +482,32 @@ int is_subclass_of(type *low, type *high) { /* depth first search from high downwards. */ n_subtypes = get_class_n_subtypes(high); for (i = 0; i < n_subtypes; i++) { - type *stp = get_class_subtype(high, i); + ir_type *stp = get_class_subtype(high, i); if (low == stp) return 1; - if (is_subclass_of(low, stp)) + if (is_SubClass_of(low, stp)) return 1; } return 0; } + +/* Subclass check for pointers to classes. + * + * Dereferences at both types the same amount of pointer types (as + * many as possible). If the remaining types are both class types + * and subclasses, returns true, else false. Can also be called with + * two class types. */ +int is_SubClass_ptr_of(ir_type *low, ir_type *high) { + while (is_Pointer_type(low) && is_Pointer_type(high)) { + low = get_pointer_points_to_type(low); + high = get_pointer_points_to_type(high); + } + + if (is_Class_type(low) && is_Class_type(high)) + return is_SubClass_of(low, high); + return 0; +} + int is_overwritten_by(entity *high, entity *low) { int i, n_overwrittenby; assert(is_entity(low) && is_entity(high)); @@ -505,7 +530,7 @@ int is_overwritten_by(entity *high, entity *low) { /** Need two routines because I want to assert the result. */ -static entity *resolve_ent_polymorphy2 (type *dynamic_class, entity *static_ent) { +static entity *resolve_ent_polymorphy2 (ir_type *dynamic_class, entity *static_ent) { int i, n_overwrittenby; entity *res = NULL; @@ -526,7 +551,7 @@ static entity *resolve_ent_polymorphy2 (type *dynamic_class, entity *static_ent) * Returns the dynamically referenced entity if the static entity and the * dynamic type are given. * Search downwards in overwritten tree. */ -entity *resolve_ent_polymorphy(type *dynamic_class, entity *static_ent) { +entity *resolve_ent_polymorphy(ir_type *dynamic_class, entity *static_ent) { entity *res; assert(static_ent && is_entity(static_ent)); @@ -535,3 +560,116 @@ entity *resolve_ent_polymorphy(type *dynamic_class, entity *static_ent) { return res; } + + + +/* ----------------------------------------------------------------------- */ +/* Class cast state handling. */ +/* ----------------------------------------------------------------------- */ + +/* - State handling. ----------------------------------------- */ + +void set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s) { + if (get_irp_class_cast_state() > s) set_irp_class_cast_state(s); + irg->class_cast_state = s; +} + +ir_class_cast_state get_irg_class_cast_state(ir_graph *irg) { + return irg->class_cast_state; +} + +void set_irp_class_cast_state(ir_class_cast_state s) { + int i; + for (i = 0; i < get_irp_n_irgs(); ++i) + assert(get_irg_class_cast_state(get_irp_irg(i)) >= s); + irp->class_cast_state = s; +} + +ir_class_cast_state get_irp_class_cast_state(void) { + return irp->class_cast_state; +} + +char *get_class_cast_state_string(ir_class_cast_state s) { +#define X(a) case a: return #a + switch(s) { + X(ir_class_casts_any); + X(ir_class_casts_transitive); + X(ir_class_casts_normalized); + X(ir_class_casts_state_max); + default: return "invalid class cast state"; + } +#undef X +} + +/* - State verification. ------------------------------------- */ + +typedef struct ccs_env { + ir_class_cast_state expected_state; + ir_class_cast_state worst_situation; +} ccs_env; + +void verify_irn_class_cast_state(ir_node *n, void *env) { + ccs_env *ccs = (ccs_env *)env; + ir_class_cast_state this_state = ir_class_casts_any; + ir_type *fromtype, *totype; + int ref_depth = 0; + + if (get_irn_op(n) != op_Cast) return; + + fromtype = get_irn_typeinfo_type(get_Cast_op(n)); + totype = get_Cast_type(n); + + while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) { + totype = get_pointer_points_to_type(totype); + fromtype = get_pointer_points_to_type(fromtype); + ref_depth++; + } + + if (!is_Class_type(totype)) return; + + if (is_SubClass_of(totype, fromtype) || + is_SubClass_of(fromtype, totype) ) { + this_state = ir_class_casts_transitive; + if ((get_class_supertype_index(totype, fromtype) != -1) || + (get_class_supertype_index(fromtype, totype) != -1) || + fromtype == totype) { + /* Das ist doch alt? Aus dem cvs aufgetaucht ... + if ((get_class_supertype_index(totype, fromtype) == -1) && + (get_class_supertype_index(fromtype, totype) == -1) ) { */ + this_state = ir_class_casts_normalized; + } + } + + if (!(this_state >= ccs->expected_state)) { + printf(" Node is "); DDMN(n); + printf(" totype "); DDMT(totype); + printf(" fromtype "); DDMT(fromtype); + printf(" this_state: %s, exp. state: %s\n", + get_class_cast_state_string(this_state), + get_class_cast_state_string(ccs->expected_state)); + assert(this_state >= ccs->expected_state && + "invalid state class cast state setting in graph"); + } + + if (this_state < ccs->worst_situation) + ccs->worst_situation = this_state; +} + + +/** Verify that the graph meets requirements of state set. */ +void verify_irg_class_cast_state(ir_graph *irg) { + ccs_env env; + + env.expected_state = get_irg_class_cast_state(irg); + env.worst_situation = ir_class_casts_normalized; + + irg_walk_graph(irg, NULL, verify_irn_class_cast_state, &env); + + if ((env.worst_situation > env.expected_state) && get_firm_verbosity()) { + printf("Note: class cast state is set lower than reqired in graph\n "); + DDMG(irg); + printf(" state is %s, reqired is %s\n", + get_class_cast_state_string(env.expected_state), + get_class_cast_state_string(env.worst_situation)); + } +}