2 * Copyright (C) 1995-2007 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
22 * @file tr_inheritance.c
24 * Project: libFIRM <br>
25 * File name: ir/tr/tr_inheritance.c <br>
26 * Purpose: Utility routines for inheritance representation <br>
27 * Author: Goetz Lindenmaier <br>
30 * Copyright: (c) 2001-2005 Universität Karlsruhe <br>
35 * @see type.h entity.h
45 #include "irgraph_t.h"
55 /* ----------------------------------------------------------------------- */
56 /* Resolve implicit inheritance. */
57 /* ----------------------------------------------------------------------- */
59 ident *default_mangle_inherited_name(ir_entity *super, ir_type *clss) {
60 return mangle_u(new_id_from_str("inh"), mangle_u(get_type_ident(clss), get_entity_ident(super)));
63 /** Replicates all entities in all super classes that are not overwritten
64 by an entity of this class. */
65 static void copy_entities_from_superclass(ir_type *clss, void *env)
69 ir_type *super, *inhenttype;
70 ir_entity *inhent, *thisent;
71 mangle_inherited_name_func *mfunc = *(mangle_inherited_name_func **)env;
73 for(i = 0; i < get_class_n_supertypes(clss); i++) {
74 super = get_class_supertype(clss, i);
75 assert(is_Class_type(super) && "not a class");
76 for(j = 0; j < get_class_n_members(super); j++) {
77 inhent = get_class_member(super, j);
78 inhenttype = get_entity_type(inhent);
79 /* check whether inhent is already overwritten */
81 for (k = 0; (k < get_class_n_members(clss)) && (overwritten == 0); k++) {
82 thisent = get_class_member(clss, k);
83 for(l = 0; l < get_entity_n_overwrites(thisent); l++) {
84 if(inhent == get_entity_overwrites(thisent, l)) {
85 /* overwritten - do not copy */
93 thisent = copy_entity_own(inhent, clss);
94 add_entity_overwrites(thisent, inhent);
95 if (get_entity_peculiarity(inhent) == peculiarity_existent)
96 set_entity_peculiarity(thisent, peculiarity_inherited);
97 set_entity_ld_ident(thisent, mfunc(inhent, clss));
98 if (get_entity_variability(inhent) == variability_constant) {
99 assert(is_atomic_entity(inhent) && /* @@@ */
100 "Inheritance of constant, compound entities not implemented");
101 set_entity_variability(thisent, variability_constant);
102 set_atomic_ent_value(thisent, get_atomic_ent_value(inhent));
109 /* Resolve implicit inheritance.
111 * Resolves the implicit inheritance supplied by firm.
113 void resolve_inheritance(mangle_inherited_name_func *mfunc) {
115 mfunc = default_mangle_inherited_name;
116 class_walk_super2sub(copy_entities_from_superclass, NULL, (void *)&mfunc);
120 /* ----------------------------------------------------------------------- */
121 /* The transitive closure of the subclass/superclass and */
122 /* overwrites/overwrittenby relation. */
124 /* A walk over the ir (O(#types+#entities)) computes the transitive */
125 /* closure. Adding a new type/entity or changing the basic relations in */
126 /* some other way invalidates the transitive closure, i.e., it is not */
127 /* updated by the basic functions. */
129 /* All functions are named as their counterparts for the basic relations, */
130 /* adding the infix 'trans_'. */
131 /* ----------------------------------------------------------------------- */
133 void set_irp_inh_transitive_closure_state(inh_transitive_closure_state s) {
134 irp->inh_trans_closure_state = s;
136 void invalidate_irp_inh_transitive_closure_state(void) {
137 if (irp->inh_trans_closure_state == inh_transitive_closure_valid)
138 irp->inh_trans_closure_state = inh_transitive_closure_invalid;
140 inh_transitive_closure_state get_irp_inh_transitive_closure_state(void) {
141 return irp->inh_trans_closure_state;
144 static void assert_valid_state(void) {
145 assert(irp->inh_trans_closure_state == inh_transitive_closure_valid ||
146 irp->inh_trans_closure_state == inh_transitive_closure_invalid);
149 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
150 /* There is a set that extends each entity/type with two new */
151 /* fields: one for the upwards directed relation: 'up' (supertype, */
152 /* overwrites) and one for the downwards directed relation: 'down' (sub- */
153 /* type, overwrittenby. These fields contain psets (and maybe later */
154 /* arrays) listing all subtypes... */
155 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
163 firm_kind *kind; /* An entity or type. */
167 /* We use this set for all types and entities. */
168 static set *tr_inh_trans_set = NULL;
170 static int tr_inh_trans_cmp(const void *e1, const void *e2, size_t size) {
171 tr_inh_trans_tp *ef1 = (tr_inh_trans_tp *)e1;
172 tr_inh_trans_tp *ef2 = (tr_inh_trans_tp *)e2;
173 return (ef1->kind != ef2->kind);
176 static INLINE unsigned int tr_inh_trans_hash(void *e) {
177 tr_inh_trans_tp *v = e;
178 return HASH_PTR(v->kind);
181 /* This always completes successfully. */
182 static tr_inh_trans_tp* get_firm_kind_entry(firm_kind *k) {
183 tr_inh_trans_tp a, *found;
186 if (!tr_inh_trans_set) tr_inh_trans_set = new_set(tr_inh_trans_cmp, 128);
188 found = set_find(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
190 a.directions[d_up] = pset_new_ptr(16);
191 a.directions[d_down] = pset_new_ptr(16);
192 found = set_insert(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
197 static pset *get_entity_map(ir_entity *ent, dir d) {
198 tr_inh_trans_tp *found;
200 assert(is_entity(ent));
201 found = get_firm_kind_entry((firm_kind *)ent);
202 return found->directions[d];
205 static void add_entity_map(ir_entity *ent, dir d, ir_entity *new) {
206 tr_inh_trans_tp *found;
208 assert(is_entity(ent) && is_entity(new));
209 tr_inh_trans_tp *found = get_firm_kind_entry((firm_kind *)ent);
210 pset_insert_ptr(found->directions[d], new);
213 static pset *get_type_map(ir_type *tp, dir d) {
214 tr_inh_trans_tp *found;
217 found = get_firm_kind_entry((firm_kind *)tp);
218 return found->directions[d];
221 static void add_type_map(ir_type *tp, dir d, type *new) {
222 tr_inh_trans_tp *found;
224 assert(is_type(tp) && is_type(new));
225 found = get_firm_kind_entry((firm_kind *)tp);
226 pset_insert_ptr(found->directions[d], new);
232 * Walk over all types reachable from tp in the sub/supertype
233 * relation and compute the closure for the two downwards directed
236 * The walk in the dag formed by the relation is tricky: We must visit
237 * all subtypes before visiting the supertypes. So we first walk down.
238 * Then we can compute the closure for this type. Then we walk up.
239 * As we call ourselves recursive, and walk in both directions, there
240 * can be cycles. So we have to make sure, that if we visit a node
241 * a second time (in a walk up) we do nothing. For this we increment
242 * the master visited flag twice.
243 * If the type is marked with master_flag_visited-1 it is on the stack.
244 * If it is marked with master_flag_visited it is fully processed.
246 * Well, we still miss some candidates ... */
247 static void compute_down_closure(ir_type *tp) {
248 pset *myset, *subset;
249 int i, n_subtypes, n_members, n_supertypes;
250 unsigned long master_visited = get_master_type_visited();
252 assert(is_Class_type(tp));
254 set_type_visited(tp, master_visited-1);
256 /* Recursive descend. */
257 n_subtypes = get_class_n_subtypes(tp);
258 for (i = 0; i < n_subtypes; ++i) {
259 ir_type *stp = get_class_subtype(tp, i);
260 if (get_type_visited(stp) < master_visited-1) {
261 compute_down_closure(stp);
266 myset = get_type_map(tp, d_down);
267 for (i = 0; i < n_subtypes; ++i) {
268 ir_type *stp = get_class_subtype(tp, i);
269 subset = get_type_map(stp, d_down);
270 pset_insert_ptr(myset, stp);
271 pset_insert_pset_ptr(myset, subset);
275 n_members = get_class_n_members(tp);
276 for (i = 0; i < n_members; ++i) {
277 ir_entity *mem = get_class_member(tp, i);
278 int j, n_overwrittenby = get_entity_n_overwrittenby(mem);
280 myset = get_entity_map(mem, d_down);
281 for (j = 0; j < n_overwrittenby; ++j) {
282 ir_entity *ov = get_entity_overwrittenby(mem, j);
283 subset = get_entity_map(ov, d_down);
284 pset_insert_ptr(myset, ov);
285 pset_insert_pset_ptr(myset, subset);
289 mark_type_visited(tp);
292 n_supertypes = get_class_n_supertypes(tp);
293 for (i = 0; i < n_supertypes; ++i) {
294 ir_type *stp = get_class_supertype(tp, i);
295 if (get_type_visited(stp) < master_visited-1) {
296 compute_down_closure(stp);
301 static void compute_up_closure(ir_type *tp) {
302 pset *myset, *subset;
303 int i, n_subtypes, n_members, n_supertypes;
304 unsigned long master_visited = get_master_type_visited();
306 assert(is_Class_type(tp));
308 set_type_visited(tp, master_visited-1);
310 /* Recursive descend. */
311 n_supertypes = get_class_n_supertypes(tp);
312 for (i = 0; i < n_supertypes; ++i) {
313 ir_type *stp = get_class_supertype(tp, i);
314 if (get_type_visited(stp) < get_master_type_visited()-1) {
315 compute_up_closure(stp);
320 myset = get_type_map(tp, d_up);
321 for (i = 0; i < n_supertypes; ++i) {
322 ir_type *stp = get_class_supertype(tp, i);
323 subset = get_type_map(stp, d_up);
324 pset_insert_ptr(myset, stp);
325 pset_insert_pset_ptr(myset, subset);
329 n_members = get_class_n_members(tp);
330 for (i = 0; i < n_members; ++i) {
331 ir_entity *mem = get_class_member(tp, i);
332 int j, n_overwrites = get_entity_n_overwrites(mem);
334 myset = get_entity_map(mem, d_up);
335 for (j = 0; j < n_overwrites; ++j) {
336 ir_entity *ov = get_entity_overwrites(mem, j);
337 subset = get_entity_map(ov, d_up);
338 pset_insert_pset_ptr(myset, subset);
339 pset_insert_ptr(myset, ov);
343 mark_type_visited(tp);
346 n_subtypes = get_class_n_subtypes(tp);
347 for (i = 0; i < n_subtypes; ++i) {
348 ir_type *stp = get_class_subtype(tp, i);
349 if (get_type_visited(stp) < master_visited-1) {
350 compute_up_closure(stp);
355 /** Compute the transitive closure of the subclass/superclass and
356 * overwrites/overwrittenby relation.
358 * This function walks over the ir (O(#types+#entities)) to compute the
359 * transitive closure. */
360 void compute_inh_transitive_closure(void) {
361 int i, n_types = get_irp_n_types();
362 free_inh_transitive_closure();
364 /* The 'down' relation */
365 inc_master_type_visited(); /* Inc twice: one if on stack, second if values computed. */
366 inc_master_type_visited();
367 for (i = 0; i < n_types; ++i) {
368 ir_type *tp = get_irp_type(i);
369 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
370 int j, n_subtypes = get_class_n_subtypes(tp);
371 int has_unmarked_subtype = 0;
373 assert(get_type_visited(tp) < get_master_type_visited()-1);
374 for (j = 0; j < n_subtypes; ++j) {
375 ir_type *stp = get_class_subtype(tp, j);
376 if (type_not_visited(stp)) {
377 has_unmarked_subtype = 1;
382 /* This is a good starting point. */
383 if (!has_unmarked_subtype)
384 compute_down_closure(tp);
388 /* The 'up' relation */
389 inc_master_type_visited();
390 inc_master_type_visited();
391 for (i = 0; i < n_types; ++i) {
392 ir_type *tp = get_irp_type(i);
393 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
394 int j, n_supertypes = get_class_n_supertypes(tp);
395 int has_unmarked_supertype = 0;
397 assert(get_type_visited(tp) < get_master_type_visited()-1);
398 for (j = 0; j < n_supertypes; ++j) {
399 ir_type *stp = get_class_supertype(tp, j);
400 if (type_not_visited(stp)) {
401 has_unmarked_supertype = 1;
406 /* This is a good starting point. */
407 if (!has_unmarked_supertype)
408 compute_up_closure(tp);
412 irp->inh_trans_closure_state = inh_transitive_closure_valid;
415 /** Free memory occupied by the transitive closure information. */
416 void free_inh_transitive_closure(void) {
417 if (tr_inh_trans_set) {
418 tr_inh_trans_tp *elt;
419 for (elt = set_first(tr_inh_trans_set); elt; elt = set_next(tr_inh_trans_set)) {
420 del_pset(elt->directions[d_up]);
421 del_pset(elt->directions[d_down]);
423 del_set(tr_inh_trans_set);
424 tr_inh_trans_set = NULL;
426 irp->inh_trans_closure_state = inh_transitive_closure_none;
429 /* - subtype ------------------------------------------------------------- */
431 ir_type *get_class_trans_subtype_first(ir_type *tp) {
432 assert_valid_state();
433 return pset_first(get_type_map(tp, d_down));
436 ir_type *get_class_trans_subtype_next (ir_type *tp) {
437 assert_valid_state();
438 return pset_next(get_type_map(tp, d_down));
441 int is_class_trans_subtype (ir_type *tp, ir_type *subtp) {
442 assert_valid_state();
443 return (pset_find_ptr(get_type_map(tp, d_down), subtp) != NULL);
446 /* - supertype ----------------------------------------------------------- */
448 ir_type *get_class_trans_supertype_first(ir_type *tp) {
449 assert_valid_state();
450 return pset_first(get_type_map(tp, d_up));
453 ir_type *get_class_trans_supertype_next (ir_type *tp) {
454 assert_valid_state();
455 return pset_next(get_type_map(tp, d_up));
458 /* - overwrittenby ------------------------------------------------------- */
460 ir_entity *get_entity_trans_overwrittenby_first(ir_entity *ent) {
461 assert_valid_state();
462 return pset_first(get_entity_map(ent, d_down));
465 ir_entity *get_entity_trans_overwrittenby_next (ir_entity *ent) {
466 assert_valid_state();
467 return pset_next(get_entity_map(ent, d_down));
470 /* - overwrites ---------------------------------------------------------- */
473 /** Iterate over all transitive overwritten entities. */
474 ir_entity *get_entity_trans_overwrites_first(ir_entity *ent) {
475 assert_valid_state();
476 return pset_first(get_entity_map(ent, d_up));
479 ir_entity *get_entity_trans_overwrites_next (ir_entity *ent) {
480 assert_valid_state();
481 return pset_next(get_entity_map(ent, d_up));
488 /* ----------------------------------------------------------------------- */
489 /* Classify pairs of types/entities in the inheritance relations. */
490 /* ----------------------------------------------------------------------- */
492 /** Returns true if low is subclass of high. */
493 static int check_is_SubClass_of(ir_type *low, ir_type *high) {
496 /* depth first search from high downwards. */
497 n_subtypes = get_class_n_subtypes(high);
498 for (i = 0; i < n_subtypes; i++) {
499 ir_type *stp = get_class_subtype(high, i);
500 if (low == stp) return 1;
501 if (is_SubClass_of(low, stp))
507 /* Returns true if low is subclass of high. */
508 int is_SubClass_of(ir_type *low, ir_type *high) {
509 assert(is_Class_type(low) && is_Class_type(high));
511 if (low == high) return 1;
513 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
514 pset *m = get_type_map(high, d_down);
515 return pset_find_ptr(m, low) ? 1 : 0;
517 return check_is_SubClass_of(low, high);
521 /* Subclass check for pointers to classes.
523 * Dereferences at both types the same amount of pointer types (as
524 * many as possible). If the remaining types are both class types
525 * and subclasses, returns true, else false. Can also be called with
526 * two class types. */
527 int is_SubClass_ptr_of(ir_type *low, ir_type *high) {
528 while (is_Pointer_type(low) && is_Pointer_type(high)) {
529 low = get_pointer_points_to_type(low);
530 high = get_pointer_points_to_type(high);
533 if (is_Class_type(low) && is_Class_type(high))
534 return is_SubClass_of(low, high);
538 int is_overwritten_by(ir_entity *high, ir_entity *low) {
539 int i, n_overwrittenby;
540 assert(is_entity(low) && is_entity(high));
542 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
543 pset *m = get_entity_map(high, d_down);
544 return pset_find_ptr(m, low) ? 1 : 0;
547 /* depth first search from high downwards. */
548 n_overwrittenby = get_entity_n_overwrittenby(high);
549 for (i = 0; i < n_overwrittenby; i++) {
550 ir_entity *ov = get_entity_overwrittenby(high, i);
551 if (low == ov) return 1;
552 if (is_overwritten_by(low, ov))
558 /** Resolve polymorphy in the inheritance relation.
560 * Returns the dynamically referenced entity if the static entity and the
561 * dynamic type are given.
562 * Search downwards in overwritten tree.
564 * Need two routines because I want to assert the result.
566 static ir_entity *do_resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent) {
567 int i, n_overwrittenby;
569 if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
571 n_overwrittenby = get_entity_n_overwrittenby(static_ent);
572 for (i = 0; i < n_overwrittenby; ++i) {
573 ir_entity *ent = get_entity_overwrittenby(static_ent, i);
574 ent = do_resolve_ent_polymorphy(dynamic_class, ent);
580 /* Resolve polymorphy in the inheritance relation.
582 * Returns the dynamically referenced entity if the static entity and the
583 * dynamic type are given.
584 * Search downwards in overwritten tree. */
585 ir_entity *resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent) {
587 assert(static_ent && is_entity(static_ent));
589 res = do_resolve_ent_polymorphy(dynamic_class, static_ent);
597 /* ----------------------------------------------------------------------- */
598 /* Class cast state handling. */
599 /* ----------------------------------------------------------------------- */
601 /* - State handling. ----------------------------------------- */
603 void set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s) {
604 if (get_irp_class_cast_state() > s) set_irp_class_cast_state(s);
605 irg->class_cast_state = s;
608 ir_class_cast_state get_irg_class_cast_state(ir_graph *irg) {
609 return irg->class_cast_state;
612 void set_irp_class_cast_state(ir_class_cast_state s) {
614 for (i = 0; i < get_irp_n_irgs(); ++i)
615 assert(get_irg_class_cast_state(get_irp_irg(i)) >= s);
616 irp->class_cast_state = s;
619 ir_class_cast_state get_irp_class_cast_state(void) {
620 return irp->class_cast_state;
623 char *get_class_cast_state_string(ir_class_cast_state s) {
624 #define X(a) case a: return #a
626 X(ir_class_casts_any);
627 X(ir_class_casts_transitive);
628 X(ir_class_casts_normalized);
629 X(ir_class_casts_state_max);
630 default: return "invalid class cast state";
635 /* - State verification. ------------------------------------- */
637 typedef struct ccs_env {
638 ir_class_cast_state expected_state;
639 ir_class_cast_state worst_situation;
642 void verify_irn_class_cast_state(ir_node *n, void *env) {
643 ccs_env *ccs = (ccs_env *)env;
644 ir_class_cast_state this_state = ir_class_casts_any;
645 ir_type *fromtype, *totype;
648 if (get_irn_op(n) != op_Cast) return;
650 fromtype = get_irn_typeinfo_type(get_Cast_op(n));
651 totype = get_Cast_type(n);
653 while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
654 totype = get_pointer_points_to_type(totype);
655 fromtype = get_pointer_points_to_type(fromtype);
659 if (!is_Class_type(totype)) return;
661 if (is_SubClass_of(totype, fromtype) ||
662 is_SubClass_of(fromtype, totype) ) {
663 this_state = ir_class_casts_transitive;
664 if ((get_class_supertype_index(totype, fromtype) != -1) ||
665 (get_class_supertype_index(fromtype, totype) != -1) ||
666 fromtype == totype) {
667 /* Das ist doch alt? Aus dem cvs aufgetaucht ...
668 if ((get_class_supertype_index(totype, fromtype) == -1) &&
669 (get_class_supertype_index(fromtype, totype) == -1) ) { */
670 this_state = ir_class_casts_normalized;
674 if (!(this_state >= ccs->expected_state)) {
675 printf(" Node is "); DDMN(n);
676 printf(" totype "); DDMT(totype);
677 printf(" fromtype "); DDMT(fromtype);
678 printf(" this_state: %s, exp. state: %s\n",
679 get_class_cast_state_string(this_state),
680 get_class_cast_state_string(ccs->expected_state));
681 assert(this_state >= ccs->expected_state &&
682 "invalid state class cast state setting in graph");
685 if (this_state < ccs->worst_situation)
686 ccs->worst_situation = this_state;
690 /** Verify that the graph meets requirements of state set. */
691 void verify_irg_class_cast_state(ir_graph *irg) {
694 env.expected_state = get_irg_class_cast_state(irg);
695 env.worst_situation = ir_class_casts_normalized;
697 irg_walk_graph(irg, NULL, verify_irn_class_cast_state, &env);
699 if ((env.worst_situation > env.expected_state) && get_firm_verbosity()) {
700 printf("Note: class cast state is set lower than reqired in graph\n ");
702 printf(" state is %s, reqired is %s\n",
703 get_class_cast_state_string(env.expected_state),
704 get_class_cast_state_string(env.worst_situation));