2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * @file tr_inheritance.c
22 * @brief Utility routines for inheritance representation
23 * @author Goetz Lindenmaier
30 #include "irgraph_t.h"
38 DEBUG_ONLY(static firm_dbg_module_t *dbg);
40 /* ----------------------------------------------------------------------- */
41 /* Resolve implicit inheritance. */
42 /* ----------------------------------------------------------------------- */
44 ident *default_mangle_inherited_name(const ir_entity *super, const ir_type *clss)
46 return id_mangle_u(new_id_from_str("inh"), id_mangle_u(get_class_ident(clss), get_entity_ident(super)));
49 /** Replicates all entities in all super classes that are not overwritten
50 by an entity of this class. */
51 static void copy_entities_from_superclass(ir_type *clss, void *env)
59 ir_entity *inhent, *thisent;
60 mangle_inherited_name_func *mfunc = *(mangle_inherited_name_func **)env;
62 for (i = 0; i < get_class_n_supertypes(clss); i++) {
63 super = get_class_supertype(clss, i);
64 assert(is_Class_type(super) && "not a class");
65 for (j = 0; j < get_class_n_members(super); j++) {
66 inhent = get_class_member(super, j);
67 /* check whether inhent is already overwritten */
69 for (k = 0; (k < get_class_n_members(clss)) && (overwritten == 0); k++) {
70 thisent = get_class_member(clss, k);
71 for (l = 0; l < get_entity_n_overwrites(thisent); l++) {
72 if (inhent == get_entity_overwrites(thisent, l)) {
73 /* overwritten - do not copy */
81 thisent = copy_entity_own(inhent, clss);
82 add_entity_overwrites(thisent, inhent);
83 if (get_entity_peculiarity(inhent) == peculiarity_existent)
84 set_entity_peculiarity(thisent, peculiarity_inherited);
85 set_entity_ld_ident(thisent, mfunc(inhent, clss));
86 if (get_entity_linkage(inhent) & IR_LINKAGE_CONSTANT) {
87 assert(is_atomic_entity(inhent) && /* @@@ */
88 "Inheritance of constant, compound entities not implemented");
89 add_entity_linkage(thisent, IR_LINKAGE_CONSTANT);
90 set_atomic_ent_value(thisent, get_atomic_ent_value(inhent));
97 /* Resolve implicit inheritance.
99 * Resolves the implicit inheritance supplied by firm.
101 void resolve_inheritance(mangle_inherited_name_func *mfunc)
104 mfunc = default_mangle_inherited_name;
105 class_walk_super2sub(copy_entities_from_superclass, NULL, (void *)&mfunc);
109 /* ----------------------------------------------------------------------- */
110 /* The transitive closure of the subclass/superclass and */
111 /* overwrites/overwrittenby relation. */
113 /* A walk over the ir (O(#types+#entities)) computes the transitive */
114 /* closure. Adding a new type/entity or changing the basic relations in */
115 /* some other way invalidates the transitive closure, i.e., it is not */
116 /* updated by the basic functions. */
118 /* All functions are named as their counterparts for the basic relations, */
119 /* adding the infix 'trans_'. */
120 /* ----------------------------------------------------------------------- */
122 void set_irp_inh_transitive_closure_state(inh_transitive_closure_state s)
124 irp->inh_trans_closure_state = s;
126 void invalidate_irp_inh_transitive_closure_state(void)
128 if (irp->inh_trans_closure_state == inh_transitive_closure_valid)
129 irp->inh_trans_closure_state = inh_transitive_closure_invalid;
131 inh_transitive_closure_state get_irp_inh_transitive_closure_state(void)
133 return irp->inh_trans_closure_state;
136 static void assert_valid_state(void)
138 assert(irp->inh_trans_closure_state == inh_transitive_closure_valid ||
139 irp->inh_trans_closure_state == inh_transitive_closure_invalid);
142 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
143 /* There is a set that extends each entity/type with two new */
144 /* fields: one for the upwards directed relation: 'up' (supertype, */
145 /* overwrites) and one for the downwards directed relation: 'down' (sub- */
146 /* type, overwrittenby. These fields contain psets (and maybe later */
147 /* arrays) listing all subtypes... */
148 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
156 const firm_kind *kind; /**< An entity or type. */
160 /* We use this set for all types and entities. */
161 static set *tr_inh_trans_set = NULL;
164 * Compare two tr_inh_trans_tp entries.
166 static int tr_inh_trans_cmp(const void *e1, const void *e2, size_t size)
168 const tr_inh_trans_tp *ef1 = (const tr_inh_trans_tp*)e1;
169 const tr_inh_trans_tp *ef2 = (const tr_inh_trans_tp*)e2;
172 return ef1->kind != ef2->kind;
176 * calculate the hash value of an tr_inh_trans_tp
178 static inline unsigned int tr_inh_trans_hash(const tr_inh_trans_tp *v)
180 return HASH_PTR(v->kind);
183 /* This always completes successfully. */
184 static tr_inh_trans_tp *get_firm_kind_entry(const firm_kind *k)
186 tr_inh_trans_tp a, *found;
189 if (!tr_inh_trans_set) tr_inh_trans_set = new_set(tr_inh_trans_cmp, 128);
191 found = (tr_inh_trans_tp*)set_find(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
193 a.directions[d_up] = pset_new_ptr(16);
194 a.directions[d_down] = pset_new_ptr(16);
195 found = (tr_inh_trans_tp*)set_insert(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
200 static pset *get_entity_map(const ir_entity *ent, dir d)
202 tr_inh_trans_tp *found;
204 assert(is_entity(ent));
205 found = get_firm_kind_entry((const firm_kind *)ent);
206 return found->directions[d];
209 static pset *get_type_map(const ir_type *tp, dir d)
211 tr_inh_trans_tp *found;
214 found = get_firm_kind_entry((const firm_kind *)tp);
215 return found->directions[d];
220 * Walk over all types reachable from tp in the sub/supertype
221 * relation and compute the closure for the two downwards directed
224 * The walk in the dag formed by the relation is tricky: We must visit
225 * all subtypes before visiting the supertypes. So we first walk down.
226 * Then we can compute the closure for this type. Then we walk up.
227 * As we call ourselves recursive, and walk in both directions, there
228 * can be cycles. So we have to make sure, that if we visit a node
229 * a second time (in a walk up) we do nothing. For this we increment
230 * the master visited flag twice.
231 * If the type is marked with master_flag_visited-1 it is on the stack.
232 * If it is marked with master_flag_visited it is fully processed.
234 * Well, we still miss some candidates ... */
235 static void compute_down_closure(ir_type *tp)
237 pset *myset, *subset;
238 size_t i, n_subtypes, n_members, n_supertypes;
239 ir_visited_t master_visited = get_master_type_visited();
241 assert(is_Class_type(tp));
243 set_type_visited(tp, master_visited-1);
245 /* Recursive descend. */
246 n_subtypes = get_class_n_subtypes(tp);
247 for (i = 0; i < n_subtypes; ++i) {
248 ir_type *stp = get_class_subtype(tp, i);
249 if (get_type_visited(stp) < master_visited-1) {
250 compute_down_closure(stp);
255 myset = get_type_map(tp, d_down);
256 for (i = 0; i < n_subtypes; ++i) {
257 ir_type *stp = get_class_subtype(tp, i);
258 subset = get_type_map(stp, d_down);
259 pset_insert_ptr(myset, stp);
260 pset_insert_pset_ptr(myset, subset);
264 n_members = get_class_n_members(tp);
265 for (i = 0; i < n_members; ++i) {
266 ir_entity *mem = get_class_member(tp, i);
267 size_t j, n_overwrittenby = get_entity_n_overwrittenby(mem);
269 myset = get_entity_map(mem, d_down);
270 for (j = 0; j < n_overwrittenby; ++j) {
271 ir_entity *ov = get_entity_overwrittenby(mem, j);
272 subset = get_entity_map(ov, d_down);
273 pset_insert_ptr(myset, ov);
274 pset_insert_pset_ptr(myset, subset);
278 mark_type_visited(tp);
281 n_supertypes = get_class_n_supertypes(tp);
282 for (i = 0; i < n_supertypes; ++i) {
283 ir_type *stp = get_class_supertype(tp, i);
284 if (get_type_visited(stp) < master_visited-1) {
285 compute_down_closure(stp);
290 static void compute_up_closure(ir_type *tp)
292 pset *myset, *subset;
293 size_t i, n_subtypes, n_members, n_supertypes;
294 ir_visited_t master_visited = get_master_type_visited();
296 assert(is_Class_type(tp));
298 set_type_visited(tp, master_visited-1);
300 /* Recursive descend. */
301 n_supertypes = get_class_n_supertypes(tp);
302 for (i = 0; i < n_supertypes; ++i) {
303 ir_type *stp = get_class_supertype(tp, i);
304 if (get_type_visited(stp) < get_master_type_visited()-1) {
305 compute_up_closure(stp);
310 myset = get_type_map(tp, d_up);
311 for (i = 0; i < n_supertypes; ++i) {
312 ir_type *stp = get_class_supertype(tp, i);
313 subset = get_type_map(stp, d_up);
314 pset_insert_ptr(myset, stp);
315 pset_insert_pset_ptr(myset, subset);
319 n_members = get_class_n_members(tp);
320 for (i = 0; i < n_members; ++i) {
321 ir_entity *mem = get_class_member(tp, i);
322 size_t j, n_overwrites = get_entity_n_overwrites(mem);
324 myset = get_entity_map(mem, d_up);
325 for (j = 0; j < n_overwrites; ++j) {
326 ir_entity *ov = get_entity_overwrites(mem, j);
327 subset = get_entity_map(ov, d_up);
328 pset_insert_pset_ptr(myset, subset);
329 pset_insert_ptr(myset, ov);
333 mark_type_visited(tp);
336 n_subtypes = get_class_n_subtypes(tp);
337 for (i = 0; i < n_subtypes; ++i) {
338 ir_type *stp = get_class_subtype(tp, i);
339 if (get_type_visited(stp) < master_visited-1) {
340 compute_up_closure(stp);
345 /** Compute the transitive closure of the subclass/superclass and
346 * overwrites/overwrittenby relation.
348 * This function walks over the ir (O(#types+#entities)) to compute the
349 * transitive closure. */
350 void compute_inh_transitive_closure(void)
352 size_t i, n_types = get_irp_n_types();
353 free_inh_transitive_closure();
355 /* The 'down' relation */
356 irp_reserve_resources(irp, IR_RESOURCE_TYPE_VISITED);
357 inc_master_type_visited(); /* Inc twice: one if on stack, second if values computed. */
358 inc_master_type_visited();
359 for (i = 0; i < n_types; ++i) {
360 ir_type *tp = get_irp_type(i);
361 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
362 size_t j, n_subtypes = get_class_n_subtypes(tp);
363 int has_unmarked_subtype = 0;
365 assert(get_type_visited(tp) < get_master_type_visited()-1);
366 for (j = 0; j < n_subtypes; ++j) {
367 ir_type *stp = get_class_subtype(tp, j);
368 if (type_not_visited(stp)) {
369 has_unmarked_subtype = 1;
374 /* This is a good starting point. */
375 if (!has_unmarked_subtype)
376 compute_down_closure(tp);
380 /* The 'up' relation */
381 inc_master_type_visited();
382 inc_master_type_visited();
383 for (i = 0; i < n_types; ++i) {
384 ir_type *tp = get_irp_type(i);
385 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
386 size_t j, n_supertypes = get_class_n_supertypes(tp);
387 int has_unmarked_supertype = 0;
389 assert(get_type_visited(tp) < get_master_type_visited()-1);
390 for (j = 0; j < n_supertypes; ++j) {
391 ir_type *stp = get_class_supertype(tp, j);
392 if (type_not_visited(stp)) {
393 has_unmarked_supertype = 1;
398 /* This is a good starting point. */
399 if (!has_unmarked_supertype)
400 compute_up_closure(tp);
404 irp->inh_trans_closure_state = inh_transitive_closure_valid;
405 irp_free_resources(irp, IR_RESOURCE_TYPE_VISITED);
408 /** Free memory occupied by the transitive closure information. */
409 void free_inh_transitive_closure(void)
411 if (tr_inh_trans_set) {
412 tr_inh_trans_tp *elt;
413 for (elt = (tr_inh_trans_tp*)set_first(tr_inh_trans_set); elt != NULL;
414 elt = (tr_inh_trans_tp*)set_next(tr_inh_trans_set)) {
415 del_pset(elt->directions[d_up]);
416 del_pset(elt->directions[d_down]);
418 del_set(tr_inh_trans_set);
419 tr_inh_trans_set = NULL;
421 irp->inh_trans_closure_state = inh_transitive_closure_none;
424 /* - subtype ------------------------------------------------------------- */
426 ir_type *get_class_trans_subtype_first(const ir_type *tp)
428 assert_valid_state();
429 return (ir_type*)pset_first(get_type_map(tp, d_down));
432 ir_type *get_class_trans_subtype_next(const ir_type *tp)
434 assert_valid_state();
435 return (ir_type*)pset_next(get_type_map(tp, d_down));
438 int is_class_trans_subtype(const ir_type *tp, const ir_type *subtp)
440 assert_valid_state();
441 return (pset_find_ptr(get_type_map(tp, d_down), subtp) != NULL);
444 /* - supertype ----------------------------------------------------------- */
446 ir_type *get_class_trans_supertype_first(const ir_type *tp)
448 assert_valid_state();
449 return (ir_type*)pset_first(get_type_map(tp, d_up));
452 ir_type *get_class_trans_supertype_next(const ir_type *tp)
454 assert_valid_state();
455 return (ir_type*)pset_next(get_type_map(tp, d_up));
458 /* - overwrittenby ------------------------------------------------------- */
460 ir_entity *get_entity_trans_overwrittenby_first(const ir_entity *ent)
462 assert_valid_state();
463 return (ir_entity*)pset_first(get_entity_map(ent, d_down));
466 ir_entity *get_entity_trans_overwrittenby_next(const ir_entity *ent)
468 assert_valid_state();
469 return (ir_entity*)pset_next(get_entity_map(ent, d_down));
472 /* - overwrites ---------------------------------------------------------- */
475 /** Iterate over all transitive overwritten entities. */
476 ir_entity *get_entity_trans_overwrites_first(const ir_entity *ent)
478 assert_valid_state();
479 return (ir_entity*)pset_first(get_entity_map(ent, d_up));
482 ir_entity *get_entity_trans_overwrites_next(const ir_entity *ent)
484 assert_valid_state();
485 return (ir_entity*)pset_next(get_entity_map(ent, d_up));
489 /* ----------------------------------------------------------------------- */
490 /* Classify pairs of types/entities in the inheritance relations. */
491 /* ----------------------------------------------------------------------- */
493 /** Returns true if low is subclass of high. */
494 static int check_is_SubClass_of(ir_type *low, ir_type *high)
496 size_t i, n_subtypes;
498 /* depth first search from high downwards. */
499 n_subtypes = get_class_n_subtypes(high);
500 for (i = 0; i < n_subtypes; i++) {
501 ir_type *stp = get_class_subtype(high, i);
502 if (low == stp) return 1;
503 if (is_SubClass_of(low, stp))
509 /* Returns true if low is subclass of high. */
510 int is_SubClass_of(ir_type *low, ir_type *high)
512 assert(is_Class_type(low) && is_Class_type(high));
514 if (low == high) return 1;
516 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
517 pset *m = get_type_map(high, d_down);
518 return pset_find_ptr(m, low) ? 1 : 0;
520 return check_is_SubClass_of(low, high);
524 /* Subclass check for pointers to classes.
526 * Dereferences at both types the same amount of pointer types (as
527 * many as possible). If the remaining types are both class types
528 * and subclasses, returns true, else false. Can also be called with
529 * two class types. */
530 int is_SubClass_ptr_of(ir_type *low, ir_type *high)
532 while (is_Pointer_type(low) && is_Pointer_type(high)) {
533 low = get_pointer_points_to_type(low);
534 high = get_pointer_points_to_type(high);
537 if (is_Class_type(low) && is_Class_type(high))
538 return is_SubClass_of(low, high);
542 int is_overwritten_by(ir_entity *high, ir_entity *low)
544 size_t i, n_overwrittenby;
545 assert(is_entity(low) && is_entity(high));
547 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
548 pset *m = get_entity_map(high, d_down);
549 return pset_find_ptr(m, low) ? 1 : 0;
552 /* depth first search from high downwards. */
553 n_overwrittenby = get_entity_n_overwrittenby(high);
554 for (i = 0; i < n_overwrittenby; i++) {
555 ir_entity *ov = get_entity_overwrittenby(high, i);
556 if (low == ov) return 1;
557 if (is_overwritten_by(low, ov))
563 /** Resolve polymorphy in the inheritance relation.
565 * Returns the dynamically referenced entity if the static entity and the
566 * dynamic type are given.
567 * Search downwards in overwritten tree.
569 * Need two routines because I want to assert the result.
571 static ir_entity *do_resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent)
573 size_t i, n_overwrittenby;
575 ir_type *owner = get_entity_owner(static_ent);
576 if (owner == dynamic_class) return static_ent;
578 // if the owner of the static_ent already is more special than the dynamic
579 // type to check against - stop here.
580 if (! is_SubClass_of(dynamic_class, owner)) return NULL;
582 n_overwrittenby = get_entity_n_overwrittenby(static_ent);
583 for (i = 0; i < n_overwrittenby; ++i) {
584 ir_entity *ent = get_entity_overwrittenby(static_ent, i);
585 ent = do_resolve_ent_polymorphy(dynamic_class, ent);
589 // No further specialization of static_ent has been found
593 /* Resolve polymorphy in the inheritance relation.
595 * Returns the dynamically referenced entity if the static entity and the
596 * dynamic type are given.
597 * Search downwards in overwritten tree.
599 ir_entity *resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent)
602 assert(static_ent && is_entity(static_ent));
604 res = do_resolve_ent_polymorphy(dynamic_class, static_ent);
612 /* ----------------------------------------------------------------------- */
613 /* Class cast state handling. */
614 /* ----------------------------------------------------------------------- */
616 /* - State handling. ----------------------------------------- */
618 void set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s)
620 if (get_irp_class_cast_state() > s)
621 set_irp_class_cast_state(s);
622 irg->class_cast_state = s;
625 ir_class_cast_state get_irg_class_cast_state(const ir_graph *irg)
627 return irg->class_cast_state;
630 void set_irp_class_cast_state(ir_class_cast_state s)
634 for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
635 assert(get_irg_class_cast_state(get_irp_irg(i)) >= s);
637 irp->class_cast_state = s;
640 ir_class_cast_state get_irp_class_cast_state(void)
642 return irp->class_cast_state;
645 const char *get_class_cast_state_string(ir_class_cast_state s)
647 #define X(a) case a: return #a
649 X(ir_class_casts_any);
650 X(ir_class_casts_transitive);
651 X(ir_class_casts_normalized);
652 X(ir_class_casts_state_max);
653 default: return "invalid class cast state";
658 /* - State verification. ------------------------------------- */
660 typedef struct ccs_env {
661 ir_class_cast_state expected_state;
662 ir_class_cast_state worst_situation;
666 * Walker: check Casts.
668 static void verify_irn_class_cast_state(ir_node *n, void *env)
670 ccs_env *ccs = (ccs_env *)env;
671 ir_class_cast_state this_state = ir_class_casts_any;
672 ir_type *fromtype, *totype;
675 if (!is_Cast(n)) return;
677 fromtype = get_irn_typeinfo_type(get_Cast_op(n));
678 totype = get_Cast_type(n);
680 while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
681 totype = get_pointer_points_to_type(totype);
682 fromtype = get_pointer_points_to_type(fromtype);
686 if (!is_Class_type(totype)) return;
688 if (is_SubClass_of(totype, fromtype) ||
689 is_SubClass_of(fromtype, totype)) {
690 this_state = ir_class_casts_transitive;
691 if ((get_class_supertype_index(totype, fromtype) != (size_t)-1) ||
692 (get_class_supertype_index(fromtype, totype) != (size_t)-1) ||
693 fromtype == totype) {
694 this_state = ir_class_casts_normalized;
698 if (!(this_state >= ccs->expected_state)) {
699 ir_printf(" Node is %+F\n", n);
700 ir_printf(" totype %+F\n", totype);
701 ir_printf(" fromtype %+F\n", fromtype);
702 ir_printf(" this_state: %s, exp. state: %s\n",
703 get_class_cast_state_string(this_state),
704 get_class_cast_state_string(ccs->expected_state));
705 assert(this_state >= ccs->expected_state &&
706 "invalid state class cast state setting in graph");
709 if (this_state < ccs->worst_situation)
710 ccs->worst_situation = this_state;
713 /** Verify that the graph meets requirements of state set. */
714 void verify_irg_class_cast_state(ir_graph *irg)
718 FIRM_DBG_REGISTER(dbg, "firm.tr.inheritance");
720 env.expected_state = get_irg_class_cast_state(irg);
721 env.worst_situation = ir_class_casts_normalized;
723 irg_walk_graph(irg, NULL, verify_irn_class_cast_state, &env);
725 if ((env.worst_situation > env.expected_state)) {
726 DB((dbg, LEVEL_1, "Note: class cast state is set lower than reqired "
727 "in graph \n\t%+F\n", irg));
728 DB((dbg, LEVEL_1, " state is %s, reqired is %s\n",
729 get_class_cast_state_string(env.expected_state),
730 get_class_cast_state_string(env.worst_situation)));