3 * @file tr_inheritance.c
5 * Project: libFIRM <br>
6 * File name: ir/tr/tr_inheritance.c <br>
7 * Purpose: Utility routines for inheritance representation <br>
8 * Author: Goetz Lindenmaier <br>
11 * Copyright: (c) 2001-2005 Universität Karlsruhe <br>
12 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE. <br>
17 * @see type.h entity.h
27 #include "irgraph_t.h"
37 /* ----------------------------------------------------------------------- */
38 /* Resolve implicit inheritance. */
39 /* ----------------------------------------------------------------------- */
41 ident *default_mangle_inherited_name(ir_entity *super, ir_type *clss) {
42 return mangle_u(new_id_from_str("inh"), mangle_u(get_type_ident(clss), get_entity_ident(super)));
45 /** Replicates all entities in all super classes that are not overwritten
46 by an entity of this class. */
47 static void copy_entities_from_superclass(ir_type *clss, void *env)
51 ir_type *super, *inhenttype;
52 ir_entity *inhent, *thisent;
53 mangle_inherited_name_func *mfunc = *(mangle_inherited_name_func **)env;
55 for(i = 0; i < get_class_n_supertypes(clss); i++) {
56 super = get_class_supertype(clss, i);
57 assert(is_Class_type(super) && "not a class");
58 for(j = 0; j < get_class_n_members(super); j++) {
59 inhent = get_class_member(super, j);
60 inhenttype = get_entity_type(inhent);
61 /* check whether inhent is already overwritten */
63 for (k = 0; (k < get_class_n_members(clss)) && (overwritten == 0); k++) {
64 thisent = get_class_member(clss, k);
65 for(l = 0; l < get_entity_n_overwrites(thisent); l++) {
66 if(inhent == get_entity_overwrites(thisent, l)) {
67 /* overwritten - do not copy */
75 thisent = copy_entity_own(inhent, clss);
76 add_entity_overwrites(thisent, inhent);
77 if (get_entity_peculiarity(inhent) == peculiarity_existent)
78 set_entity_peculiarity(thisent, peculiarity_inherited);
79 set_entity_ld_ident(thisent, mfunc(inhent, clss));
80 if (get_entity_variability(inhent) == variability_constant) {
81 assert(is_atomic_entity(inhent) && /* @@@ */
82 "Inheritance of constant, compound entities not implemented");
83 set_entity_variability(thisent, variability_constant);
84 set_atomic_ent_value(thisent, get_atomic_ent_value(inhent));
91 /* Resolve implicit inheritance.
93 * Resolves the implicit inheritance supplied by firm.
95 void resolve_inheritance(mangle_inherited_name_func *mfunc) {
97 mfunc = default_mangle_inherited_name;
98 class_walk_super2sub(copy_entities_from_superclass, NULL, (void *)&mfunc);
102 /* ----------------------------------------------------------------------- */
103 /* The transitive closure of the subclass/superclass and */
104 /* overwrites/overwrittenby relation. */
106 /* A walk over the ir (O(#types+#entities)) computes the transitive */
107 /* closure. Adding a new type/entity or changing the basic relations in */
108 /* some other way invalidates the transitive closure, i.e., it is not */
109 /* updated by the basic functions. */
111 /* All functions are named as their counterparts for the basic relations, */
112 /* adding the infix 'trans_'. */
113 /* ----------------------------------------------------------------------- */
115 void set_irp_inh_transitive_closure_state(inh_transitive_closure_state s) {
116 irp->inh_trans_closure_state = s;
118 void invalidate_irp_inh_transitive_closure_state(void) {
119 if (irp->inh_trans_closure_state == inh_transitive_closure_valid)
120 irp->inh_trans_closure_state = inh_transitive_closure_invalid;
122 inh_transitive_closure_state get_irp_inh_transitive_closure_state(void) {
123 return irp->inh_trans_closure_state;
126 static void assert_valid_state(void) {
127 assert(irp->inh_trans_closure_state == inh_transitive_closure_valid ||
128 irp->inh_trans_closure_state == inh_transitive_closure_invalid);
131 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
132 /* There is a set that extends each entity/type with two new */
133 /* fields: one for the upwards directed relation: 'up' (supertype, */
134 /* overwrites) and one for the downwards directed relation: 'down' (sub- */
135 /* type, overwrittenby. These fields contain psets (and maybe later */
136 /* arrays) listing all subtypes... */
137 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
145 firm_kind *kind; /* An entity or type. */
149 /* We use this set for all types and entities. */
150 static set *tr_inh_trans_set = NULL;
152 static int tr_inh_trans_cmp(const void *e1, const void *e2, size_t size) {
153 tr_inh_trans_tp *ef1 = (tr_inh_trans_tp *)e1;
154 tr_inh_trans_tp *ef2 = (tr_inh_trans_tp *)e2;
155 return (ef1->kind != ef2->kind);
158 static INLINE unsigned int tr_inh_trans_hash(void *e) {
159 tr_inh_trans_tp *v = e;
160 return HASH_PTR(v->kind);
163 /* This always completes successfully. */
164 static tr_inh_trans_tp* get_firm_kind_entry(firm_kind *k) {
165 tr_inh_trans_tp a, *found;
168 if (!tr_inh_trans_set) tr_inh_trans_set = new_set(tr_inh_trans_cmp, 128);
170 found = set_find(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
172 a.directions[d_up] = pset_new_ptr(16);
173 a.directions[d_down] = pset_new_ptr(16);
174 found = set_insert(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
179 static pset *get_entity_map(ir_entity *ent, dir d) {
180 tr_inh_trans_tp *found;
182 assert(is_entity(ent));
183 found = get_firm_kind_entry((firm_kind *)ent);
184 return found->directions[d];
187 static void add_entity_map(ir_entity *ent, dir d, ir_entity *new) {
188 tr_inh_trans_tp *found;
190 assert(is_entity(ent) && is_entity(new));
191 tr_inh_trans_tp *found = get_firm_kind_entry((firm_kind *)ent);
192 pset_insert_ptr(found->directions[d], new);
195 static pset *get_type_map(ir_type *tp, dir d) {
196 tr_inh_trans_tp *found;
199 found = get_firm_kind_entry((firm_kind *)tp);
200 return found->directions[d];
203 static void add_type_map(ir_type *tp, dir d, type *new) {
204 tr_inh_trans_tp *found;
206 assert(is_type(tp) && is_type(new));
207 found = get_firm_kind_entry((firm_kind *)tp);
208 pset_insert_ptr(found->directions[d], new);
214 * Walk over all types reachable from tp in the sub/supertype
215 * relation and compute the closure for the two downwards directed
218 * The walk in the dag formed by the relation is tricky: We must visit
219 * all subtypes before visiting the supertypes. So we first walk down.
220 * Then we can compute the closure for this type. Then we walk up.
221 * As we call ourselves recursive, and walk in both directions, there
222 * can be cycles. So we have to make sure, that if we visit a node
223 * a second time (in a walk up) we do nothing. For this we increment
224 * the master visited flag twice.
225 * If the type is marked with master_flag_visited-1 it is on the stack.
226 * If it is marked with master_flag_visited it is fully processed.
228 * Well, we still miss some candidates ... */
229 static void compute_down_closure(ir_type *tp) {
230 pset *myset, *subset;
231 int i, n_subtypes, n_members, n_supertypes;
232 unsigned long master_visited = get_master_type_visited();
234 assert(is_Class_type(tp));
236 set_type_visited(tp, master_visited-1);
238 /* Recursive descend. */
239 n_subtypes = get_class_n_subtypes(tp);
240 for (i = 0; i < n_subtypes; ++i) {
241 ir_type *stp = get_class_subtype(tp, i);
242 if (get_type_visited(stp) < master_visited-1) {
243 compute_down_closure(stp);
248 myset = get_type_map(tp, d_down);
249 for (i = 0; i < n_subtypes; ++i) {
250 ir_type *stp = get_class_subtype(tp, i);
251 subset = get_type_map(stp, d_down);
252 pset_insert_ptr(myset, stp);
253 pset_insert_pset_ptr(myset, subset);
257 n_members = get_class_n_members(tp);
258 for (i = 0; i < n_members; ++i) {
259 ir_entity *mem = get_class_member(tp, i);
260 int j, n_overwrittenby = get_entity_n_overwrittenby(mem);
262 myset = get_entity_map(mem, d_down);
263 for (j = 0; j < n_overwrittenby; ++j) {
264 ir_entity *ov = get_entity_overwrittenby(mem, j);
265 subset = get_entity_map(ov, d_down);
266 pset_insert_ptr(myset, ov);
267 pset_insert_pset_ptr(myset, subset);
271 mark_type_visited(tp);
274 n_supertypes = get_class_n_supertypes(tp);
275 for (i = 0; i < n_supertypes; ++i) {
276 ir_type *stp = get_class_supertype(tp, i);
277 if (get_type_visited(stp) < master_visited-1) {
278 compute_down_closure(stp);
283 static void compute_up_closure(ir_type *tp) {
284 pset *myset, *subset;
285 int i, n_subtypes, n_members, n_supertypes;
286 unsigned long master_visited = get_master_type_visited();
288 assert(is_Class_type(tp));
290 set_type_visited(tp, master_visited-1);
292 /* Recursive descend. */
293 n_supertypes = get_class_n_supertypes(tp);
294 for (i = 0; i < n_supertypes; ++i) {
295 ir_type *stp = get_class_supertype(tp, i);
296 if (get_type_visited(stp) < get_master_type_visited()-1) {
297 compute_up_closure(stp);
302 myset = get_type_map(tp, d_up);
303 for (i = 0; i < n_supertypes; ++i) {
304 ir_type *stp = get_class_supertype(tp, i);
305 subset = get_type_map(stp, d_up);
306 pset_insert_ptr(myset, stp);
307 pset_insert_pset_ptr(myset, subset);
311 n_members = get_class_n_members(tp);
312 for (i = 0; i < n_members; ++i) {
313 ir_entity *mem = get_class_member(tp, i);
314 int j, n_overwrites = get_entity_n_overwrites(mem);
316 myset = get_entity_map(mem, d_up);
317 for (j = 0; j < n_overwrites; ++j) {
318 ir_entity *ov = get_entity_overwrites(mem, j);
319 subset = get_entity_map(ov, d_up);
320 pset_insert_pset_ptr(myset, subset);
321 pset_insert_ptr(myset, ov);
325 mark_type_visited(tp);
328 n_subtypes = get_class_n_subtypes(tp);
329 for (i = 0; i < n_subtypes; ++i) {
330 ir_type *stp = get_class_subtype(tp, i);
331 if (get_type_visited(stp) < master_visited-1) {
332 compute_up_closure(stp);
337 /** Compute the transitive closure of the subclass/superclass and
338 * overwrites/overwrittenby relation.
340 * This function walks over the ir (O(#types+#entities)) to compute the
341 * transitive closure. */
342 void compute_inh_transitive_closure(void) {
343 int i, n_types = get_irp_n_types();
344 free_inh_transitive_closure();
346 /* The 'down' relation */
347 inc_master_type_visited(); /* Inc twice: one if on stack, second if values computed. */
348 inc_master_type_visited();
349 for (i = 0; i < n_types; ++i) {
350 ir_type *tp = get_irp_type(i);
351 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
352 int j, n_subtypes = get_class_n_subtypes(tp);
353 int has_unmarked_subtype = 0;
355 assert(get_type_visited(tp) < get_master_type_visited()-1);
356 for (j = 0; j < n_subtypes; ++j) {
357 ir_type *stp = get_class_subtype(tp, j);
358 if (type_not_visited(stp)) {
359 has_unmarked_subtype = 1;
364 /* This is a good starting point. */
365 if (!has_unmarked_subtype)
366 compute_down_closure(tp);
370 /* The 'up' relation */
371 inc_master_type_visited();
372 inc_master_type_visited();
373 for (i = 0; i < n_types; ++i) {
374 ir_type *tp = get_irp_type(i);
375 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
376 int j, n_supertypes = get_class_n_supertypes(tp);
377 int has_unmarked_supertype = 0;
379 assert(get_type_visited(tp) < get_master_type_visited()-1);
380 for (j = 0; j < n_supertypes; ++j) {
381 ir_type *stp = get_class_supertype(tp, j);
382 if (type_not_visited(stp)) {
383 has_unmarked_supertype = 1;
388 /* This is a good starting point. */
389 if (!has_unmarked_supertype)
390 compute_up_closure(tp);
394 irp->inh_trans_closure_state = inh_transitive_closure_valid;
397 /** Free memory occupied by the transitive closure information. */
398 void free_inh_transitive_closure(void) {
399 if (tr_inh_trans_set) {
400 tr_inh_trans_tp *elt;
401 for (elt = set_first(tr_inh_trans_set); elt; elt = set_next(tr_inh_trans_set)) {
402 del_pset(elt->directions[d_up]);
403 del_pset(elt->directions[d_down]);
405 del_set(tr_inh_trans_set);
406 tr_inh_trans_set = NULL;
408 irp->inh_trans_closure_state = inh_transitive_closure_none;
411 /* - subtype ------------------------------------------------------------- */
413 ir_type *get_class_trans_subtype_first(ir_type *tp) {
414 assert_valid_state();
415 return pset_first(get_type_map(tp, d_down));
418 ir_type *get_class_trans_subtype_next (ir_type *tp) {
419 assert_valid_state();
420 return pset_next(get_type_map(tp, d_down));
423 int is_class_trans_subtype (ir_type *tp, ir_type *subtp) {
424 assert_valid_state();
425 return (pset_find_ptr(get_type_map(tp, d_down), subtp) != NULL);
428 /* - supertype ----------------------------------------------------------- */
430 ir_type *get_class_trans_supertype_first(ir_type *tp) {
431 assert_valid_state();
432 return pset_first(get_type_map(tp, d_up));
435 ir_type *get_class_trans_supertype_next (ir_type *tp) {
436 assert_valid_state();
437 return pset_next(get_type_map(tp, d_up));
440 /* - overwrittenby ------------------------------------------------------- */
442 ir_entity *get_entity_trans_overwrittenby_first(ir_entity *ent) {
443 assert_valid_state();
444 return pset_first(get_entity_map(ent, d_down));
447 ir_entity *get_entity_trans_overwrittenby_next (ir_entity *ent) {
448 assert_valid_state();
449 return pset_next(get_entity_map(ent, d_down));
452 /* - overwrites ---------------------------------------------------------- */
455 /** Iterate over all transitive overwritten entities. */
456 ir_entity *get_entity_trans_overwrites_first(ir_entity *ent) {
457 assert_valid_state();
458 return pset_first(get_entity_map(ent, d_up));
461 ir_entity *get_entity_trans_overwrites_next (ir_entity *ent) {
462 assert_valid_state();
463 return pset_next(get_entity_map(ent, d_up));
470 /* ----------------------------------------------------------------------- */
471 /* Classify pairs of types/entities in the inheritance relations. */
472 /* ----------------------------------------------------------------------- */
474 /** Returns true if low is subclass of high. */
475 static int check_is_SubClass_of(ir_type *low, ir_type *high) {
478 /* depth first search from high downwards. */
479 n_subtypes = get_class_n_subtypes(high);
480 for (i = 0; i < n_subtypes; i++) {
481 ir_type *stp = get_class_subtype(high, i);
482 if (low == stp) return 1;
483 if (is_SubClass_of(low, stp))
489 /* Returns true if low is subclass of high. */
490 int is_SubClass_of(ir_type *low, ir_type *high) {
491 assert(is_Class_type(low) && is_Class_type(high));
493 if (low == high) return 1;
495 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
496 pset *m = get_type_map(high, d_down);
497 return pset_find_ptr(m, low) ? 1 : 0;
499 return check_is_SubClass_of(low, high);
503 /* Subclass check for pointers to classes.
505 * Dereferences at both types the same amount of pointer types (as
506 * many as possible). If the remaining types are both class types
507 * and subclasses, returns true, else false. Can also be called with
508 * two class types. */
509 int is_SubClass_ptr_of(ir_type *low, ir_type *high) {
510 while (is_Pointer_type(low) && is_Pointer_type(high)) {
511 low = get_pointer_points_to_type(low);
512 high = get_pointer_points_to_type(high);
515 if (is_Class_type(low) && is_Class_type(high))
516 return is_SubClass_of(low, high);
520 int is_overwritten_by(ir_entity *high, ir_entity *low) {
521 int i, n_overwrittenby;
522 assert(is_entity(low) && is_entity(high));
524 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
525 pset *m = get_entity_map(high, d_down);
526 return pset_find_ptr(m, low) ? 1 : 0;
529 /* depth first search from high downwards. */
530 n_overwrittenby = get_entity_n_overwrittenby(high);
531 for (i = 0; i < n_overwrittenby; i++) {
532 ir_entity *ov = get_entity_overwrittenby(high, i);
533 if (low == ov) return 1;
534 if (is_overwritten_by(low, ov))
540 /** Resolve polymorphy in the inheritance relation.
542 * Returns the dynamically referenced entity if the static entity and the
543 * dynamic type are given.
544 * Search downwards in overwritten tree.
546 * Need two routines because I want to assert the result.
548 static ir_entity *do_resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent) {
549 int i, n_overwrittenby;
551 if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
553 n_overwrittenby = get_entity_n_overwrittenby(static_ent);
554 for (i = 0; i < n_overwrittenby; ++i) {
555 ir_entity *ent = get_entity_overwrittenby(static_ent, i);
556 ent = do_resolve_ent_polymorphy(dynamic_class, ent);
562 /* Resolve polymorphy in the inheritance relation.
564 * Returns the dynamically referenced entity if the static entity and the
565 * dynamic type are given.
566 * Search downwards in overwritten tree. */
567 ir_entity *resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent) {
569 assert(static_ent && is_entity(static_ent));
571 res = do_resolve_ent_polymorphy(dynamic_class, static_ent);
579 /* ----------------------------------------------------------------------- */
580 /* Class cast state handling. */
581 /* ----------------------------------------------------------------------- */
583 /* - State handling. ----------------------------------------- */
585 void set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s) {
586 if (get_irp_class_cast_state() > s) set_irp_class_cast_state(s);
587 irg->class_cast_state = s;
590 ir_class_cast_state get_irg_class_cast_state(ir_graph *irg) {
591 return irg->class_cast_state;
594 void set_irp_class_cast_state(ir_class_cast_state s) {
596 for (i = 0; i < get_irp_n_irgs(); ++i)
597 assert(get_irg_class_cast_state(get_irp_irg(i)) >= s);
598 irp->class_cast_state = s;
601 ir_class_cast_state get_irp_class_cast_state(void) {
602 return irp->class_cast_state;
605 char *get_class_cast_state_string(ir_class_cast_state s) {
606 #define X(a) case a: return #a
608 X(ir_class_casts_any);
609 X(ir_class_casts_transitive);
610 X(ir_class_casts_normalized);
611 X(ir_class_casts_state_max);
612 default: return "invalid class cast state";
617 /* - State verification. ------------------------------------- */
619 typedef struct ccs_env {
620 ir_class_cast_state expected_state;
621 ir_class_cast_state worst_situation;
624 void verify_irn_class_cast_state(ir_node *n, void *env) {
625 ccs_env *ccs = (ccs_env *)env;
626 ir_class_cast_state this_state = ir_class_casts_any;
627 ir_type *fromtype, *totype;
630 if (get_irn_op(n) != op_Cast) return;
632 fromtype = get_irn_typeinfo_type(get_Cast_op(n));
633 totype = get_Cast_type(n);
635 while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
636 totype = get_pointer_points_to_type(totype);
637 fromtype = get_pointer_points_to_type(fromtype);
641 if (!is_Class_type(totype)) return;
643 if (is_SubClass_of(totype, fromtype) ||
644 is_SubClass_of(fromtype, totype) ) {
645 this_state = ir_class_casts_transitive;
646 if ((get_class_supertype_index(totype, fromtype) != -1) ||
647 (get_class_supertype_index(fromtype, totype) != -1) ||
648 fromtype == totype) {
649 /* Das ist doch alt? Aus dem cvs aufgetaucht ...
650 if ((get_class_supertype_index(totype, fromtype) == -1) &&
651 (get_class_supertype_index(fromtype, totype) == -1) ) { */
652 this_state = ir_class_casts_normalized;
656 if (!(this_state >= ccs->expected_state)) {
657 printf(" Node is "); DDMN(n);
658 printf(" totype "); DDMT(totype);
659 printf(" fromtype "); DDMT(fromtype);
660 printf(" this_state: %s, exp. state: %s\n",
661 get_class_cast_state_string(this_state),
662 get_class_cast_state_string(ccs->expected_state));
663 assert(this_state >= ccs->expected_state &&
664 "invalid state class cast state setting in graph");
667 if (this_state < ccs->worst_situation)
668 ccs->worst_situation = this_state;
672 /** Verify that the graph meets requirements of state set. */
673 void verify_irg_class_cast_state(ir_graph *irg) {
676 env.expected_state = get_irg_class_cast_state(irg);
677 env.worst_situation = ir_class_casts_normalized;
679 irg_walk_graph(irg, NULL, verify_irn_class_cast_state, &env);
681 if ((env.worst_situation > env.expected_state) && get_firm_verbosity()) {
682 printf("Note: class cast state is set lower than reqired in graph\n ");
684 printf(" state is %s, reqired is %s\n",
685 get_class_cast_state_string(env.expected_state),
686 get_class_cast_state_string(env.worst_situation));