1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
4 * Author: Goetz Lindenmaier
6 * traverse the type information. The walker walks the whole ir graph
7 * to find the distinct type trees in the type graph forest.
8 * - execute the pre function before recursion
9 * - execute the post function after recursion
21 #include "irgraph_t.h"
24 #include "type_or_entity.h"
27 /* Make types visible to allow most efficient access */
31 typedef struct type_walk_env {
38 static void type_walk_2(type_or_ent *tore,
39 void (pre)(type_or_ent*, void*),
40 void (post)(type_or_ent*, void*),
46 switch (get_kind(tore)) {
48 if (((entity *)tore)->visit >= type_visited) return;
51 if(type_id == get_type_tpop((type*)tore)) {
52 type_walk_2((type_or_ent *)skip_tid((type *)tore), pre, post, env);
55 if (((type *)tore)->visit >= type_visited) return;
61 /* execute pre method */
66 switch (get_kind(tore)) {
70 entity *ent = (entity *)tore;
71 ent->visit = type_visited;
72 type_walk_2((type_or_ent *)get_entity_owner(ent), pre, post, env);
73 type_walk_2((type_or_ent *)get_entity_type(ent), pre, post, env);
78 type *tp = (type *)tore;
79 mark_type_visited(tp);
80 switch (get_type_tpop_code(tp)) {
83 for (i=0; i<get_class_n_supertypes(tp); i++)
84 type_walk_2((type_or_ent *)get_class_supertype(tp, i), pre, post, env);
85 for (i=0; i<get_class_n_members(tp); i++)
86 type_walk_2((type_or_ent *)get_class_member(tp, i), pre, post, env);
87 for (i=0; i<get_class_n_subtypes(tp); i++)
88 type_walk_2((type_or_ent *)get_class_subtype(tp, i), pre, post, env);
93 for (i=0; i<get_struct_n_members(tp); i++)
94 type_walk_2((type_or_ent *)get_struct_member(tp, i), pre, post, env);
99 for (i = 0; i < get_method_n_params(tp); i++)
100 type_walk_2((type_or_ent *)get_method_param_type(tp, i), pre, post, env);
101 for (i = 0; i < get_method_n_ress(tp); i++)
102 type_walk_2((type_or_ent *)get_method_res_type(tp, i), pre, post, env);
107 for (i = 0; i < get_union_n_members(tp); i++)
108 type_walk_2((type_or_ent *)get_union_member(tp, i), pre, post, env);
112 type_walk_2((type_or_ent *)get_array_element_type(tp),
114 type_walk_2((type_or_ent *)get_array_element_entity(tp),
117 case tpo_enumeration:
121 type_walk_2((type_or_ent *)get_pointer_points_to_type(tp),
129 printf(" *** Faulty type! \n");
132 } break; /* end case k_type */
134 printf(" *** Faulty type or entity! \n");
138 /* execute post method */
145 static void start_type_walk(ir_node *node, void *env) {
146 void *pre = ((type_walk_env *)env)->pre;
147 void *post = ((type_walk_env *)env)->post;
148 void *envi = ((type_walk_env *)env)->env;
152 switch (get_irn_opcode(node)) { /* node label */
154 if ( (get_SymConst_kind(node) == type_tag)
155 || (get_SymConst_kind(node) == size))
156 type_walk_2((type_or_ent *)get_SymConst_type(node), pre, post, envi);
159 type_walk_2((type_or_ent *)get_Sel_entity(node), pre, post, envi);
162 type_walk_2((type_or_ent *)get_Call_type(node), pre, post, envi);
165 type_walk_2((type_or_ent *)get_Alloc_type(node), pre, post, envi);
168 type_walk_2((type_or_ent *)get_Free_type(node), pre, post, envi);
175 void type_walk(void (pre)(type_or_ent*, void*),
176 void (post)(type_or_ent*, void*),
180 /*type_walk_2((type_or_ent *)get_glob_type(), pre, post, env);
181 global type is on the list visited below, too. */
182 for (i = 0; i < get_irp_n_types(); i++) {
183 type_walk_2((type_or_ent *)get_irp_type(i), pre, post, env);
185 type_walk_2((type_or_ent *)get_glob_type(), pre, post, env);
188 void type_walk_irg (ir_graph *irg,
189 void (pre)(type_or_ent*, void*),
190 void (post)(type_or_ent*, void*),
193 /* this is needed to pass the parameters to the walker that actually
194 walks the type information */
195 type_walk_env* type_env;
196 type_env = (type_walk_env *) malloc (sizeof(type_walk_env));
198 type_env->post = post;
202 irg_walk(get_irg_end(irg), start_type_walk, NULL, type_env);
204 type_walk_2((type_or_ent *)get_irg_ent(irg), pre, post, env);
210 static void type_walk_s2s_2(type_or_ent *tore,
211 void (pre)(type_or_ent*, void*),
212 void (post)(type_or_ent*, void*),
218 switch (get_kind(tore)) {
220 if (((entity *)tore)->visit >= type_visited) return;
223 if(type_id == get_type_tpop((type*)tore)) {
224 type_walk_s2s_2((type_or_ent *)skip_tid((type *)tore), pre, post, env);
227 if (((type *)tore)->visit >= type_visited) return;
234 switch (get_kind(tore)) {
237 type *tp = (type *)tore;
238 mark_type_visited(tp);
239 switch (get_type_tpop_code(tp)) {
242 for (i = 0; i < get_class_n_supertypes(tp); i++) {
243 type_walk_s2s_2((type_or_ent *)get_class_supertype(tp, i), pre,
246 /* execute pre method */
249 tp = skip_tid((type*)tore);
251 for (i = 0; i < get_class_n_subtypes(tp); i++) {
252 type_walk_s2s_2((type_or_ent *)get_class_subtype(tp, i), pre,
256 /* execute post method */
265 case tpo_enumeration:
272 printf(" *** Faulty type! \n");
275 } break; /* end case k_type */
280 printf(" *** Faulty type or entity! \n");
286 void type_walk_super2sub(void (pre)(type_or_ent*, void*),
287 void (post)(type_or_ent*, void*),
292 type_walk_s2s_2((type_or_ent *)get_glob_type(), pre, post, env);
293 for (i = 0; i < get_irp_n_types(); i++) {
294 tp = get_irp_type(i);
295 type_walk_s2s_2((type_or_ent *)tp, pre, post, env);
299 /*****************************************************************************/
302 type_walk_super_2(type_or_ent *tore,
303 void (pre)(type_or_ent*, void*),
304 void (post)(type_or_ent*, void*),
310 switch (get_kind(tore)) {
312 if (((entity *)tore)->visit >= type_visited) return;
315 if(type_id == get_type_tpop((type*)tore)) {
316 type_walk_super_2((type_or_ent *)skip_tid((type *)tore), pre, post, env);
319 if (((type *)tore)->visit >= type_visited) return;
326 switch (get_kind(tore)) {
329 type *tp = (type *)tore;
330 mark_type_visited(tp);
331 switch (get_type_tpop_code(tp)) {
334 /* execute pre method */
337 tp = skip_tid((type*)tore);
339 for (i = 0; i < get_class_n_supertypes(tp); i++) {
340 type_walk_super_2((type_or_ent *)get_class_supertype(tp, i), pre,
344 /* execute post method */
353 case tpo_enumeration:
360 printf(" *** Faulty type! \n");
363 } break; /* end case k_type */
368 printf(" *** Faulty type or entity! \n");
374 void type_walk_super(void (pre)(type_or_ent*, void*),
375 void (post)(type_or_ent*, void*),
380 type_walk_super_2((type_or_ent *)get_glob_type(), pre, post, env);
381 for (i = 0; i < get_irp_n_types(); i++) {
382 tp = get_irp_type(i);
383 type_walk_super_2((type_or_ent *)tp, pre, post, env);
387 /*****************************************************************************/
391 class_walk_s2s_2(type *tp,
392 void (pre)(type*, void*),
393 void (post)(type*, void*),
399 if (tp->visit >= type_visited) return;
401 assert(is_class_type(tp));
402 /* Assure all supertypes are visited before */
403 for (i=0; i < get_class_n_supertypes(tp); i++) {
404 if (get_type_visited(get_class_supertype(tp, i)) < type_visited)
408 mark_type_visited(tp);
410 /* execute pre method */
414 tp = skip_tid((type*)tp);
415 for (i=0; i<get_class_n_subtypes(tp); i++) {
416 class_walk_s2s_2(get_class_subtype(tp, i), pre, post, env);
418 /* execute post method */
426 void class_walk_super2sub(void (pre)(type*, void*),
427 void (post)(type*, void*),
433 for (i = 0; i < get_irp_n_types(); i++) {
434 tp = get_irp_type(i);
435 if (is_class_type(tp) &&
436 (get_class_n_supertypes(tp) == 0) &&
437 (tp->visit < type_visited) &&
438 (!is_frame_type(tp)) &&
439 (tp != get_glob_type())) {
440 class_walk_s2s_2(tp, pre, post, env);
445 void class_walk_super2sub(void (pre)(type*, void*),
446 void (post)(type*, void*),
452 for (i = 0; i < get_irp_n_types(); i++) {
453 tp = get_irp_type(i);
454 if (is_class_type(tp) &&
455 (get_class_n_supertypes(tp) == 0) &&
456 (tp->visit < type_visited)) {
457 assert(!is_frame_type(tp));
458 assert(tp != get_glob_type());
459 class_walk_s2s_2(tp, pre, post, env);
465 /* Walks over all entities in the type */
466 void walk_types_entities(type *tp,
467 void (doit)(entity*, void*),
470 switch(get_type_tpop_code(tp)) {
472 for (i=0; i<get_class_n_members(tp); i++)
473 doit(get_class_member(tp, i), env);
476 for (i=0; i<get_struct_n_members(tp); i++)
477 doit(get_struct_member(tp, i), env);
480 for (i = 0; i < get_union_n_members(tp); i++)
481 doit(get_union_member(tp, i), env);
484 doit(get_array_element_entity(tp), env);
487 case tpo_enumeration: