5 * File name: ir/ana2/pto.c
9 * Created: Mon 18 Oct 2004
11 * Copyright: (c) 1999-2004 Universität Karlsruhe
12 * Licence: This file is protected by GPL - GNU GENERAL PUBLIC LICENSE.
20 # include "typalise.h"
25 # endif /* not defined TRUE */
39 static long ta_id = 0;
44 static typalise_t *typalise_proj (ir_node*);
48 Ctors, Dtors for typalise_t-s
50 static typalise_t *ta_exact (type *tp)
52 typalise_t *ta = (typalise_t*) xmalloc (sizeof (typalise_t));
53 ta->kind = type_exact;
57 assert (is_class_type (tp));
62 static typalise_t *ta_types (lset_t *set)
64 typalise_t *ta = (typalise_t*) xmalloc (sizeof (typalise_t));
65 ta->kind = type_types;
72 static typalise_t *ta_type (type *tp)
74 typalise_t *ta = (typalise_t*) xmalloc (sizeof (typalise_t));
79 assert (is_class_type (tp));
84 static void ta_delete (typalise_t *ta)
86 if (type_types == ta->kind) {
87 lset_destroy (ta->res.types);
93 ta->kind = type_invalid;
99 Helpers for inheritance, overwriting and stuff
102 Find out whether otype is a subtype of stype.
103 Return non-zero iff otype is a subtype of stype.
105 static int is_subtype (type *otype, type *stype)
107 int n_sub = get_class_n_subtypes (stype);
111 if (otype == stype) {
115 for (i = 0; (!is_sub) && (i < n_sub); i ++) {
116 type *sub = get_class_subtype (stype, i);
118 is_sub |= is_subtype (otype, sub);
127 Compute the closure of all subtypes of otype (including otype
130 static void _collect_subtypes (type *otype, lset_t *set)
132 lset_insert (set, otype);
134 int n_sub = get_class_n_subtypes (otype);
137 for (i = 0; i < n_sub; i ++) {
138 type *sub = get_class_subtype (otype, i);
140 _collect_subtypes (sub, set);
144 static lset_t *subtype_closure (type *otype)
146 lset_t *set = lset_create ();
148 _collect_subtypes (otype, set);
154 Helper method for get_owner_types
156 static void _collect_owner_types (entity *method, ir_graph *graph, lset_t *tps)
158 /* search DOWNwards in clazz hierarchy */
160 if ((peculiarity_description == get_entity_peculiarity (method)) ||
161 (peculiarity_inherited == get_entity_peculiarity (method))) {
162 lset_insert (tps, get_entity_owner (method));
163 } else if (peculiarity_existent == get_entity_peculiarity (method)) {
164 ir_graph *ex_graph = get_entity_irg (method);
166 if ((NULL == ex_graph) || (ex_graph == graph)) {
167 /* wtf? they define the same graph again? well, whatever: */
168 lset_insert (tps, get_entity_owner (method));
170 /* aha: they define a new graph. can't have that, so bail out */
175 int n_over = get_entity_n_overwrittenby (method);
178 for (i = 0; i < n_over; i ++) {
179 entity *ometh = get_entity_overwrittenby (method, i);
181 _collect_owner_types (ometh, graph, tps);
187 Collect all classes that use the given implementation of a method.
189 static lset_t *get_owner_types (ir_graph *graph)
191 lset_t *tps = lset_create ();
192 entity *meth = get_irg_entity (graph);
194 _collect_owner_types (meth, graph, tps);
200 Return a list containing all types of 'set' which are a subtype of 'type'.
202 static lset_t *filter_for_type (lset_t *set, type *stype)
204 type *curs = (type*) lset_first (set);
205 lset_t *lset = lset_create ();
207 while (NULL != curs) {
208 if (is_subtype (curs, stype)) {
209 lset_insert (lset, curs);
212 curs = lset_next (set);
222 Join 'one' and 'two'; both args are deallocated, result is freshly
225 static typalise_t *ta_join (typalise_t *one, typalise_t *two)
227 typalise_t *res = NULL;
230 case (type_invalid): { /* shut up, gcc */ }
233 case (type_invalid): { /* shut up, gcc */ }
235 if (one->res.type == two->res.type) {
238 lset_t *set = lset_create ();
239 lset_insert (set, one->res.type);
240 lset_insert (set, two->res.type);
241 res = ta_types (set);
249 lset_insert (two->res.types, one->res.type);
255 if (is_subtype (one->res.type, two->res.type)) {
259 lset_t *closure = subtype_closure (two->res.type);
260 lset_insert (closure, one->res.type);
271 case (type_invalid): { /* shut up, gcc */ }
273 res = ta_join (two, one);
276 lset_insert_all (one->res.types, two->res.types);
282 lset_t *closure = subtype_closure (two->res.type);
283 lset_append (one->res.types, closure);
293 case (type_invalid): { /* shut up, gcc */ }
295 res = ta_join (two, one);
298 res = ta_join (two, one);
301 type *one_type = one->res.type;
302 type *two_type = two->res.type;
304 if (is_subtype (one_type, two_type)) {
307 } else if (is_subtype (two_type, one_type)) {
311 lset_t *one_closure = subtype_closure (one->res.type);
312 lset_t *two_closure = subtype_closure (two->res.type);
314 lset_append (one_closure, two_closure);
319 res = ta_types (one_closure);
326 assert (res && "no result");
333 static const char *ta_name (typalise_t *ta)
335 # define BUF_SIZE 1024
336 static char buf [BUF_SIZE];
338 int len = sprintf (buf, "[%d] ", ta->id);
341 case (type_invalid): { /* shut up, gcc */ }
343 len += sprintf (buf+len, "only ");
344 strncat (buf, get_type_name (ta->res.type), BUF_SIZE);
347 len += sprintf (buf+len, "one_of ");
349 type *iter = lset_first (ta->res.types);
351 int size = BUF_SIZE - len - 1;
352 while ((NULL != iter) && (0 < size)) {
353 char *dest = strncat (buf, get_type_name (iter), size);
356 iter = lset_next (ta->res.types);
360 len += sprintf (buf+len, "poly ");
361 strncat (buf, get_type_name (ta->res.type), BUF_SIZE);
366 /* # undef BUF_SIZE */
368 # endif /* SHUT_UP_GCC */
371 Find out whether the given clazz uses the given implementation of a
372 method. Presumably, this is because clazz inherits the graph as
373 the implementation for a method.
375 static int uses_graph (type *clazz, entity *meth, ir_graph *graph)
377 type *g_clazz = get_entity_owner (meth);
379 if (g_clazz == clazz) {
383 if (peculiarity_existent == get_entity_peculiarity (meth)) {
384 ir_graph *g_graph = get_entity_irg (meth);
386 if (g_graph != graph) {
391 /* else inherited or description */
394 int n_over = get_entity_n_overwrittenby (meth); /* DOWN-wards */
396 for (i = 0; (i < n_over) && (!use); i ++) {
397 entity *over = get_entity_overwrittenby (meth, i);
399 use |= uses_graph (clazz, over, graph);
406 Check whether the given typalise_t includes the given type.
408 static int ta_supports (typalise_t *ta, ir_graph *graph)
411 case (type_invalid): { /* shut up, gcc */ }
414 lset_t *tps = get_owner_types (graph);
416 if (lset_contains (tps, ta->res.type)) {
425 entity *meth = get_irg_entity (graph);
426 type *tp = get_entity_owner (meth);
427 int res = is_subtype (tp, ta->res.type);
432 res = uses_graph (ta->res.type, meth, graph);
438 type *tp = get_entity_owner (get_irg_entity (graph));
440 return (lset_contains (ta->res.types, tp));
444 assert (0 && "invalid ta");
448 /* =========== WHAT ELSE ? =========== */
451 Helper to typalise (ir_node*)
454 Find an approximation to the given proj node's value's types
456 static typalise_t *typalise_proj (ir_node *proj)
458 typalise_t *res = NULL;
459 ir_node *proj_in = get_Proj_pred (proj);
461 if (iro_Proj == get_irn_opcode (proj_in)) {
462 /* fprintf (stdout, "\tProj (Proj)\n"); */
464 proj_in = get_Proj_pred (proj_in);
465 if (iro_Start == get_irn_opcode (proj_in)) {
466 ir_graph *graph = get_irn_irg (proj);
467 entity *meth = get_irg_entity (graph);
469 long n = get_Proj_proj (proj);
473 type *tp = get_entity_owner (meth);
475 /* res = ta_exact (tp); */
476 res = ta_type (tp); /* TODO */
479 type *tp = get_method_param_type (get_entity_type (meth), n);
480 if (is_pointer_type (tp)) {
481 tp = get_pointer_points_to_type (tp);
486 } else if (iro_Call == get_irn_opcode (proj_in)) {
487 /* call result ... 'whatever' */
488 /* hey, this is redundant (or the check for iro_Call further down) */
489 ir_node *call_ptr = get_Call_ptr (proj_in);
491 res = typalise (call_ptr);
493 fprintf (stdout, "\n Proj (Proj (%s)) not handled\n",
494 get_op_name (get_irn_op (proj_in)));
498 opcode op = get_irn_opcode (proj_in);
499 if ((iro_Load != op) && (iro_Alloc != op) && (iro_Call != op)) {
500 fprintf (stdout, "\n Proj (%s) not handled\n",
501 get_op_name (get_irn_op (proj_in)));
504 res = typalise (proj_in); /* everything else */
505 /* Proj (Load), Proj (New), Proj (Call) */
517 Given a set of graphs and a typalise_t, return the method (s) in
518 the set that are supported by the typalise_t. Also, deallocates
521 lset_t *filter_for_ta (lset_t *set, typalise_t *ta)
523 lset_t *res = lset_create ();
524 ir_graph *curs = (ir_graph*) lset_first (set);
526 while (NULL != curs) {
527 if (ta_supports (ta, curs)) {
528 lset_insert (res, curs);
531 curs = lset_next (set);
540 For the given ptr, do a quick check about what (class) types may be
543 typalise_t *typalise (ir_node *node)
545 opcode op = get_irn_opcode (node);
546 typalise_t *res = NULL;
550 /* casts always succeed */
551 typalise_t *ta = NULL;
552 type *tp = get_Cast_type (node);
554 if (is_pointer_type (tp)) {
555 tp = get_pointer_points_to_type (tp);
557 assert (is_class_type (tp));
559 ta = typalise (get_Cast_op (node));
561 if (NULL == ta) { /* no type found */
563 } else if (type_exact == ta->kind) { /* one type found */
564 /* nothing (maybe check cast? */
565 } else if (type_type == ta->kind) { /* some types found */
566 if (is_subtype (tp, ta->res.type)) {
567 ta->res.type = tp; /* assume cast is correct */
569 /* should assert (is_subtype (ta->res.type, tp)) */
571 } else if (type_types == ta->kind) {
572 lset_t *ftp = filter_for_type (ta->res.types, tp);
573 lset_destroy (ta->res.types);
581 res = typalise_proj (node);
585 ir_node *load_ptr = get_Load_ptr (node);
587 res = typalise (load_ptr);
592 /* it's call (sel (ptr)) or load (sel (ptr)) */
593 entity *ent = get_Sel_entity (node);
594 type *tp = get_entity_type (ent);
596 if (is_method_type (tp)) {
597 tp = get_entity_type (ent);
598 tp = get_method_res_type (tp, 0);
600 if (is_pointer_type (tp)) {
601 tp = get_pointer_points_to_type (tp);
605 } else if (is_class_type (tp)) {
606 tp = get_entity_type (ent);
608 if (is_pointer_type (tp)) {
609 tp = get_pointer_points_to_type (tp);
613 } else if (is_pointer_type (tp)) {
614 tp = get_pointer_points_to_type (tp);
617 assert (0 && "select not handled");
622 int n_ins = get_irn_arity (node);
624 ir_node *phi_in = NULL;
625 typalise_t *ta = NULL;
626 /* assert (0 && "Do we ever get here?"); */ /* apparently, we do. */
628 for (i = 0; i < n_ins; i ++) {
629 phi_in = get_irn_n (node, i);
630 ta = (NULL == ta) ? typalise (phi_in) : ta_join (ta, typalise (phi_in));
637 type *type = get_Alloc_type (node);
638 res = ta_exact (type);
642 /* presumably call (sel (proj (call))) */
643 ir_node *ptr = get_Call_ptr (node);
645 if (iro_Sel == get_irn_opcode (ptr)) {
646 meth = get_Sel_entity (ptr);
647 } else if (iro_SymConst == get_irn_opcode (ptr)) {
648 if (get_SymConst_kind (ptr) == symconst_addr_ent) {
649 meth = get_SymConst_entity (ptr);
651 meth = NULL; /* WTF? */
656 type *tp = get_method_res_type ((type*) meth, 0);
659 /* could be anything */
660 /* fprintf (stdout, "meth= (null)"); */
664 fprintf (stdout, "]\n");
668 case (iro_SymConst): {
669 if (get_SymConst_kind (node) == symconst_type_tag) {
670 type *tp = get_SymConst_type (node);
673 } else if (get_SymConst_kind (node) == symconst_addr_ent) {
674 entity *ent = get_SymConst_entity (node);
675 type *tp = get_entity_type (ent);
676 tp = get_pointer_points_to_type (tp);
677 assert (is_class_type (tp));
679 res = ta_type (tp); /* can't use ta_exact */
681 fprintf (stdout, "can't handle SymConst %s?\n",
682 get_op_name (get_irn_op (node)));
693 fprintf (stdout, "what's with %s?\n", get_op_name (get_irn_op (node)));
707 Revision 1.2 2004/10/22 09:53:10 liekweg
708 Correctly handle proj_args
710 Revision 1.1 2004/10/21 11:09:37 liekweg
711 Moved memwalk stuf into irmemwalk
712 Moved lset stuff into lset
713 Moved typalise stuff into typalise