class cast states implemented
[libfirm] / ir / tr / tr_inheritance.c
1 /**
2  *
3  * @file tp_inheritance.c
4  *
5  * Project:     libFIRM                                                   <br>
6  * File name:   ir/tr/tp_inheritance.c                                    <br>
7  * Purpose:     Utility routines for inheritance representation           <br>
8  * Author:      Goetz Lindenmaier                                         <br>
9  * Modified by:                                                           <br>
10  * Created:                                                               <br>
11  * Copyright:   (c) 2001-2005 Universität Karlsruhe                       <br>
12  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE. <br>
13  * CVS-ID:      $Id$
14  *
15  *
16  *
17  *  @see  type.h entity.h
18  */
19
20 #include "type.h"
21 #include "entity.h"
22 #include "typewalk.h"
23 #include "irgraph_t.h"
24 #include "irprog_t.h"
25 #include "pset.h"
26 #include "set.h"
27 #include "mangle.h"
28 #include "irgwalk.h"
29 #include "irflag.h"
30 //#include ".h"
31
32
33 /* ----------------------------------------------------------------------- */
34 /* Resolve implicit inheritance.                                           */
35 /* ----------------------------------------------------------------------- */
36
37 ident *default_mangle_inherited_name(entity *super, type *clss) {
38   return mangle_u(new_id_from_str("inh"), mangle_u(get_type_ident(clss), get_entity_ident(super)));
39 }
40
41 /** Replicates all entities in all super classes that are not overwritten
42    by an entity of this class. */
43 static void copy_entities_from_superclass(type *clss, void *env)
44 {
45   int i, j, k, l;
46   int overwritten;
47   type *super, *inhenttype;
48   entity *inhent, *thisent;
49   mangle_inherited_name_func *mfunc = (mangle_inherited_name_func *)env;
50
51   for(i = 0; i < get_class_n_supertypes(clss); i++) {
52     super = get_class_supertype(clss, i);
53     assert(is_Class_type(super) && "not a class");
54     for(j = 0; j < get_class_n_members(super); j++) {
55       inhent = get_class_member(super, j);
56       inhenttype = get_entity_type(inhent);
57       /* check whether inhent is already overwritten */
58       overwritten = 0;
59       for (k = 0; (k < get_class_n_members(clss)) && (overwritten == 0); k++) {
60               thisent = get_class_member(clss, k);
61               for(l = 0; l < get_entity_n_overwrites(thisent); l++) {
62                 if(inhent == get_entity_overwrites(thisent, l)) {
63                   /* overwritten - do not copy */
64                   overwritten = 1;
65                   break;
66                 }
67               }
68       }
69       /* Inherit entity */
70       if (!overwritten) {
71               thisent = copy_entity_own(inhent, clss);
72               add_entity_overwrites(thisent, inhent);
73               set_entity_peculiarity(thisent, peculiarity_inherited);
74               set_entity_ld_ident(thisent, mfunc(inhent, clss));
75               if (get_entity_variability(inhent) == variability_constant) {
76                 assert(is_atomic_entity(inhent) &&  /* @@@ */
77                        "Inheritance of constant, compound entities not implemented");
78                 set_entity_variability(thisent, variability_constant);
79                 set_atomic_ent_value(thisent, get_atomic_ent_value(inhent));
80               }
81       }
82     }
83   }
84 }
85
86 /* Resolve implicit inheritance.
87  *
88  *  Resolves the implicit inheritance supplied by firm.
89  */
90 void resolve_inheritance(mangle_inherited_name_func *mfunc) {
91   if (!mfunc)
92     mfunc = default_mangle_inherited_name;
93   class_walk_super2sub(copy_entities_from_superclass, NULL, (void *)mfunc);
94 }
95
96
97 /* ----------------------------------------------------------------------- */
98 /* The transitive closure of the subclass/superclass and                   */
99 /* overwrites/overwrittenby relation.                                      */
100 /*                                                                         */
101 /* A walk over the ir (O(#types+#entities)) computes the transitive        */
102 /* closure.  Adding a new type/entity or changing the basic relations in   */
103 /* some other way invalidates the transitive closure, i.e., it is not      */
104 /* updated by the basic functions.                                         */
105 /*                                                                         */
106 /* All functions are named as their counterparts for the basic relations,  */
107 /* adding the infix 'trans_'.                                              */
108 /* ----------------------------------------------------------------------- */
109
110 void                        set_irp_inh_transitive_closure_state(inh_transitive_closure_state s) {
111   irp->inh_trans_closure_state = s;
112 }
113 void                        invalidate_irp_inh_transitive_closure_state(void) {
114   if (irp->inh_trans_closure_state == inh_transitive_closure_valid)
115     irp->inh_trans_closure_state = inh_transitive_closure_invalid;
116 }
117 inh_transitive_closure_state get_irp_inh_transitive_closure_state(void) {
118   return irp->inh_trans_closure_state;
119 }
120
121 static void assert_valid_state(void) {
122   assert(irp->inh_trans_closure_state == inh_transitive_closure_valid ||
123          irp->inh_trans_closure_state == inh_transitive_closure_invalid);
124 }
125
126 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
127 /* There is a set that extends each entity/type with two new               */
128 /* fields:  one for the upwards directed relation: 'up' (supertype,        */
129 /* overwrites) and one for the downwards directed relation: 'down' (sub-   */
130 /* type, overwrittenby.  These fields contain psets (and maybe later       */
131 /* arrays) listing all subtypes...                                         */
132 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
133
134 typedef struct {
135   firm_kind *kind;   /* An entity or type. */
136   pset *up;
137   pset *down;
138 } tr_inh_trans_tp;
139
140 /* We use this set for all types and entities.  */
141 static set *tr_inh_trans_set = NULL;
142
143 static int tr_inh_trans_cmp(const void *e1, const void *e2, size_t size) {
144   tr_inh_trans_tp *ef1 = (tr_inh_trans_tp *)e1;
145   tr_inh_trans_tp *ef2 = (tr_inh_trans_tp *)e2;
146   return (ef1->kind != ef2->kind);
147 }
148
149 static INLINE unsigned int tr_inh_trans_hash(void *e) {
150   void *v = (void *) ((tr_inh_trans_tp *)e)->kind;
151   return HASH_PTR(v);
152 }
153
154 typedef enum {
155   d_up,
156   d_down,
157 } dir;
158
159 /* This always completes successfully. */
160 static tr_inh_trans_tp* get_firm_kind_entry(firm_kind *k) {
161   tr_inh_trans_tp a, *found;
162   a.kind = k;
163
164   if (!tr_inh_trans_set) tr_inh_trans_set = new_set(tr_inh_trans_cmp, 128);
165
166   found = set_find(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
167   if (!found) {
168     a.up   = pset_new_ptr(16);
169     a.down = pset_new_ptr(16);
170     found = set_insert(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
171   }
172   return found;
173 }
174
175 static pset *get_entity_map(entity *ent, dir d) {
176   tr_inh_trans_tp *found;
177
178   assert(is_entity(ent));
179   found = get_firm_kind_entry((firm_kind *)ent);
180   return (d == d_up) ? found->up : found->down;
181 }
182 /*
183 static void  add_entity_map(entity *ent, dir d, entity *new) {
184   tr_inh_trans_tp *found;
185
186   assert(is_entity(ent) && is_entity(new));
187   tr_inh_trans_tp *found = get_firm_kind_entry((firm_kind *)ent);
188   if (d == d_up)
189     pset_insert_ptr(found->up,   new);
190   else
191     pset_insert_ptr(found->down, new);
192 }
193 */
194 static pset *get_type_map(type *tp, dir d) {
195   tr_inh_trans_tp *found;
196
197   assert(is_type(tp));
198   found = get_firm_kind_entry((firm_kind *)tp);
199   return (d == d_up) ? found->up : found->down;
200 }
201 /*
202 static void  add_type_map(type *tp, dir d, type *new) {
203   tr_inh_trans_tp *found;
204
205   assert(is_type(tp) && is_type(new));
206   found = get_firm_kind_entry((firm_kind *)tp);
207   if (d == d_up) pset_insert_ptr(found->up,   new);
208   else           pset_insert_ptr(found->down, new);
209 }
210 */
211
212
213 /**
214  * Walk over all types reachable from tp in the sub/supertype
215  * relation and compute the closure for the two downwards directed
216  * relations.
217  *
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.
227  *
228  * Well, we still miss some candidates ... */
229 static void compute_down_closure(type *tp) {
230   pset *myset, *subset;
231   int i, n_subtypes, n_members, n_supertypes;
232   unsigned long master_visited = get_master_type_visited();
233
234   assert(is_Class_type(tp));
235
236   set_type_visited(tp, master_visited-1);
237
238   /* Recursive descend. */
239   n_subtypes = get_class_n_subtypes(tp);
240   for (i = 0; i < n_subtypes; ++i) {
241     type *stp = get_class_subtype(tp, i);
242     if (get_type_visited(stp) < master_visited-1) {
243       compute_down_closure(stp);
244     }
245   }
246
247   /* types */
248   myset = get_type_map(tp, d_down);
249   for (i = 0; i < n_subtypes; ++i) {
250     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);
254   }
255
256   /* entities */
257   n_members = get_class_n_members(tp);
258   for (i = 0; i < n_members; ++i) {
259     entity *mem = get_class_member(tp, i);
260     int j, n_overwrittenby = get_entity_n_overwrittenby(mem);
261
262     myset = get_entity_map(mem, d_down);
263     for (j = 0; j > n_overwrittenby; ++j) {
264       entity *ov = get_entity_overwrittenby(mem, j);
265       subset = get_entity_map(ov, d_down);
266       pset_insert_pset_ptr(myset, subset);
267       pset_insert_ptr(myset, ov);
268     }
269   }
270
271   mark_type_visited(tp);
272
273   /* Walk up. */
274   n_supertypes = get_class_n_supertypes(tp);
275   for (i = 0; i < n_supertypes; ++i) {
276     type *stp = get_class_supertype(tp, i);
277     if (get_type_visited(stp) < master_visited-1) {
278       compute_down_closure(stp);
279     }
280   }
281 }
282
283 static void compute_up_closure(type *tp) {
284   pset *myset, *subset;
285   int i, n_subtypes, n_members, n_supertypes;
286   int master_visited = get_master_type_visited();
287
288   assert(is_Class_type(tp));
289
290   set_type_visited(tp, master_visited-1);
291
292   /* Recursive descend. */
293   n_supertypes = get_class_n_supertypes(tp);
294   for (i = 0; i < n_supertypes; ++i) {
295     type *stp = get_class_supertype(tp, i);
296     if (get_type_visited(stp) < get_master_type_visited()-1) {
297       compute_up_closure(stp);
298     }
299   }
300
301   /* types */
302   myset = get_type_map(tp, d_up);
303   for (i = 0; i < n_supertypes; ++i) {
304     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);
308   }
309
310   /* entities */
311   n_members = get_class_n_members(tp);
312   for (i = 0; i < n_members; ++i) {
313     entity *mem = get_class_member(tp, i);
314     int j, n_overwrites = get_entity_n_overwrites(mem);
315
316     myset = get_entity_map(mem, d_up);
317     for (j = 0; j > n_overwrites; ++j) {
318       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);
322     }
323   }
324
325   mark_type_visited(tp);
326
327   /* Walk down. */
328   n_subtypes = get_class_n_subtypes(tp);
329   for (i = 0; i < n_subtypes; ++i) {
330     type *stp = get_class_subtype(tp, i);
331     if (get_type_visited(stp) < master_visited-1) {
332       compute_up_closure(stp);
333     }
334   }
335 }
336
337 /** Compute the transitive closure of the subclass/superclass and
338  *  overwrites/overwrittenby relation.
339  *
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();
345
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     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 = false;
354
355       assert(get_type_visited(tp) < get_master_type_visited()-1);
356       for (j = 0; j < n_subtypes && !has_unmarked_subtype; ++j) {
357         type *stp = get_class_subtype(tp, j);
358         if (type_not_visited(stp)) has_unmarked_subtype = true;
359       }
360
361       /* This is a good starting point. */
362       if (!has_unmarked_subtype)
363         compute_down_closure(tp);
364     }
365   }
366
367   /* The 'up' relation */
368   inc_master_type_visited();
369   inc_master_type_visited();
370   for (i = 0; i < n_types; ++i) {
371     type *tp = get_irp_type(i);
372     if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
373       int j, n_supertypes = get_class_n_supertypes(tp);
374       int has_unmarked_supertype = false;
375
376       assert(get_type_visited(tp) < get_master_type_visited()-1);
377       for (j = 0; j < n_supertypes && !has_unmarked_supertype; ++j) {
378               type *stp = get_class_supertype(tp, j);
379               if (type_not_visited(stp)) has_unmarked_supertype = true;
380       }
381
382       /* This is a good starting point. */
383       if (!has_unmarked_supertype)
384               compute_up_closure(tp);
385     }
386   }
387
388   irp->inh_trans_closure_state = inh_transitive_closure_valid;
389 }
390
391 /** Free memory occupied by the transitive closure information. */
392 void free_inh_transitive_closure(void) {
393   if (tr_inh_trans_set) {
394     tr_inh_trans_tp *elt;
395     for (elt = set_first(tr_inh_trans_set); elt; elt = set_next(tr_inh_trans_set)) {
396       del_pset(elt->up);
397       del_pset(elt->down);
398     }
399     del_set(tr_inh_trans_set);
400     tr_inh_trans_set = NULL;
401   }
402   irp->inh_trans_closure_state = inh_transitive_closure_none;
403 }
404
405 /* - subtype ------------------------------------------------------------- */
406
407 type *get_class_trans_subtype_first(type *tp) {
408   assert_valid_state();
409   return pset_first(get_type_map(tp, d_down));
410 }
411
412 type *get_class_trans_subtype_next (type *tp) {
413   assert_valid_state();
414   return pset_next(get_type_map(tp, d_down));
415 }
416
417 int is_class_trans_subtype (type *tp, type *subtp) {
418   assert_valid_state();
419   return (pset_find_ptr(get_type_map(tp, d_down), subtp) != NULL);
420 }
421
422 /* - supertype ----------------------------------------------------------- */
423
424 type *get_class_trans_supertype_first(type *tp) {
425   assert_valid_state();
426   return pset_first(get_type_map(tp, d_up));
427 }
428
429 type *get_class_trans_supertype_next (type *tp) {
430   assert_valid_state();
431   return pset_next(get_type_map(tp, d_up));
432 }
433
434 /* - overwrittenby ------------------------------------------------------- */
435
436 entity *get_entity_trans_overwrittenby_first(entity *ent) {
437   assert_valid_state();
438   return pset_first(get_entity_map(ent, d_down));
439 }
440
441 entity *get_entity_trans_overwrittenby_next (entity *ent) {
442   assert_valid_state();
443   return pset_next(get_entity_map(ent, d_down));
444 }
445
446 /* - overwrites ---------------------------------------------------------- */
447
448
449 /** Iterate over all transitive overwritten entities. */
450 entity *get_entity_trans_overwrites_first(entity *ent) {
451   assert_valid_state();
452   return pset_first(get_entity_map(ent, d_up));
453 }
454
455 entity *get_entity_trans_overwrites_next (entity *ent) {
456   assert_valid_state();
457   return pset_next(get_entity_map(ent, d_up));
458 }
459
460
461
462
463
464 /* ----------------------------------------------------------------------- */
465 /* Classify pairs of types/entities in the inheritance relations.          */
466 /* ----------------------------------------------------------------------- */
467
468 /* Returns true if low is subclass of high. */
469 int is_subclass_of(type *low, type *high) {
470   int i, n_subtypes;
471   assert(is_Class_type(low) && is_Class_type(high));
472
473   if (low == high) return 1;
474
475   if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
476     pset *m = get_type_map(high, d_down);
477     return pset_find_ptr(m, low) ? 1 : 0;
478   }
479
480   /* depth first search from high downwards. */
481   n_subtypes = get_class_n_subtypes(high);
482   for (i = 0; i < n_subtypes; i++) {
483     type *stp = get_class_subtype(high, i);
484     if (low == stp) return 1;
485     if (is_subclass_of(low, stp))
486       return 1;
487   }
488   return 0;
489 }
490
491 int is_superclass_of(type *high, type *low) {
492   return is_subclass_of(low, high);
493 }
494
495 int is_overwritten_by(entity *high, entity *low) {
496   int i, n_overwrittenby;
497   assert(is_entity(low) && is_entity(high));
498
499   if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
500     pset *m = get_entity_map(high, d_down);
501     return pset_find_ptr(m, low) ? 1 : 0;
502   }
503
504   /* depth first search from high downwards. */
505   n_overwrittenby = get_entity_n_overwrittenby(high);
506   for (i = 0; i < n_overwrittenby; i++) {
507     entity *ov = get_entity_overwrittenby(high, i);
508     if (low == ov) return 1;
509     if (is_overwritten_by(low, ov))
510       return 1;
511   }
512   return 0;
513 }
514
515
516 /** Need two routines because I want to assert the result. */
517 static entity *resolve_ent_polymorphy2 (type *dynamic_class, entity *static_ent) {
518   int i, n_overwrittenby;
519   entity *res = NULL;
520
521   if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
522
523   n_overwrittenby = get_entity_n_overwrittenby(static_ent);
524   for (i = 0; i < n_overwrittenby; ++i) {
525     res = resolve_ent_polymorphy2(dynamic_class, get_entity_overwrittenby(static_ent, i));
526     if (res)
527       break;
528   }
529
530   return res;
531 }
532
533 /* Resolve polymorphy in the inheritance relation.
534  *
535  * Returns the dynamically referenced entity if the static entity and the
536  * dynamic type are given.
537  * Search downwards in overwritten tree. */
538 entity *resolve_ent_polymorphy(type *dynamic_class, entity *static_ent) {
539   entity *res;
540   assert(static_ent && is_entity(static_ent));
541
542   res = resolve_ent_polymorphy2(dynamic_class, static_ent);
543   assert(res);
544
545   return res;
546 }
547
548
549
550 /* ----------------------------------------------------------------------- */
551 /* Class cast state handling.                                              */
552 /* ----------------------------------------------------------------------- */
553
554 /* - State handling. ----------------------------------------- */
555
556 void set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s) {
557   if (get_irp_class_cast_state() > s) set_irp_class_cast_state(s);
558   irg->class_cast_state = s;
559 }
560
561 ir_class_cast_state get_irg_class_cast_state(ir_graph *irg) {
562   return irg->class_cast_state;
563 }
564
565 void set_irp_class_cast_state(ir_class_cast_state s) {
566   int i;
567   for (i = 0; i < get_irp_n_irgs(); ++i)
568     assert(get_irg_class_cast_state(get_irp_irg(i)) >= s);
569   irp->class_cast_state = s;
570 }
571
572 ir_class_cast_state get_irp_class_cast_state(void) {
573   return irp->class_cast_state;
574 }
575
576 char *get_class_cast_state_string(ir_class_cast_state s) {
577 #define X(a)    case a: return #a
578   switch(s) {
579     X(ir_class_casts_any);
580     X(ir_class_casts_transitive);
581     X(ir_class_casts_normalized);
582     X(ir_class_casts_state_max);
583   default: return "invalid class cast state";
584   }
585 #undef X
586 }
587
588 /* - State verification. ------------------------------------- */
589
590 typedef struct ccs_env {
591   ir_class_cast_state expected_state;
592   ir_class_cast_state worst_situation;
593 } ccs_env;
594
595 void verify_irn_class_cast_state(ir_node *n, void *env) {
596   ccs_env *ccs = (ccs_env *)env;
597   ir_class_cast_state this_state = ir_class_casts_any;
598
599   type *fromtype = get_irn_typeinfo_type(get_Cast_op(n));
600   type *totype   = get_Cast_type(n);
601   int ref_depth = 0;
602
603   while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
604     totype   = get_pointer_points_to_type(totype);
605     fromtype = get_pointer_points_to_type(fromtype);
606     ref_depth++;
607   }
608
609   if (!is_Class_type(totype)) return;
610
611   if (is_subclass_of(totype, fromtype) ||
612       is_subclass_of(fromtype, totype)   ) {
613     this_state = ir_class_casts_transitive;
614     if ((get_class_supertype_index(totype, fromtype) == -1) &&
615         (get_class_supertype_index(fromtype, totype) == -1) ) {
616       this_state = ir_class_casts_normalized;
617     }
618   }
619
620   assert(this_state >= ccs->expected_state &&
621          "invalid state class cast state setting in graph");
622
623   if (this_state < ccs->worst_situation)
624     ccs->worst_situation = this_state;
625 }
626
627
628 /** Verify that the graph meets reqirements of state set. */
629 void verify_irg_class_cast_state(ir_graph *irg) {
630   ccs_env env;
631
632   env.expected_state  = get_irg_class_cast_state(irg);
633   env.worst_situation = ir_class_casts_normalized;
634
635   irg_walk_graph(irg, NULL, verify_irn_class_cast_state, &env);
636
637   if ((env.worst_situation > env.expected_state) && get_firm_verbosity()) {
638     printf("Note:  class cast state is set lower than reqired in graph\n       ");
639     DDMG(irg);
640     printf("       state is %s, reqired is %s\n",
641            get_class_cast_state_string(env.expected_state),
642            get_class_cast_state_string(env.worst_situation));
643   }
644 }