2 * Copyright (C) 1995-2008 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 * @brief Functionality to modify the type graph.
23 * @author Goetz Lindenmaier
27 * Traverse the type information. The walker walks the whole ir graph
28 * to find the distinct type trees in the type graph forest.
29 * - execute the pre function before recursion
30 * - execute the post function after recursion
41 #include "irgraph_t.h"
47 * The walker environment
49 typedef struct type_walk_env {
50 type_walk_func *pre; /**< Pre-walker function */
51 type_walk_func *post; /**< Post-walker function */
52 void *env; /**< environment for walker functions */
55 /* a walker for irn's */
56 static void irn_type_walker(
57 ir_node *node, type_walk_func *pre, type_walk_func *post, void *env);
59 static void walk_initializer(ir_initializer_t *initializer,
60 type_walk_func *pre, type_walk_func *post,
63 switch(initializer->kind) {
64 case IR_INITIALIZER_CONST:
65 irn_type_walker(initializer->consti.value, pre, post, env);
67 case IR_INITIALIZER_TARVAL:
68 case IR_INITIALIZER_NULL:
71 case IR_INITIALIZER_COMPOUND: {
73 for(i = 0; i < initializer->compound.n_initializers; ++i) {
74 ir_initializer_t *subinitializer
75 = initializer->compound.initializers[i];
76 walk_initializer(subinitializer, pre, post, env);
81 panic("invalid initializer found");
85 * Main walker: walks over all used types/entities of a
88 static void do_type_walk(type_or_ent tore,
93 int i, n_types, n_mem;
94 ir_entity *ent = NULL;
100 switch (get_kind(tore.ent)) {
103 if (entity_visited(ent))
107 tp = skip_tid(tore.typ);
108 if (type_visited(tp))
115 /* execute pre method */
120 switch (get_kind(tore.ent)) {
122 mark_entity_visited(ent);
123 cont.typ = get_entity_owner(ent);
124 do_type_walk(cont, pre, post, env);
125 cont.typ = get_entity_type(ent);
126 do_type_walk(cont, pre, post, env);
128 if (get_entity_variability(ent) != variability_uninitialized) {
129 /* walk over the value types */
130 if (ent->has_initializer) {
131 walk_initializer(ent->attr.initializer, pre, post, env);
132 } else if (is_atomic_entity(ent)) {
133 n = get_atomic_ent_value(ent);
134 irn_type_walker(n, pre, post, env);
136 n_mem = get_compound_ent_n_values(ent);
137 for (i = 0; i < n_mem; ++i) {
138 n = get_compound_ent_value(ent, i);
139 irn_type_walker(n, pre, post, env);
145 mark_type_visited(tp);
146 switch (get_type_tpop_code(tp)) {
148 n_types = get_class_n_supertypes(tp);
149 for (i = 0; i < n_types; ++i) {
150 cont.typ = get_class_supertype(tp, i);
151 do_type_walk(cont, pre, post, env);
153 n_mem = get_class_n_members(tp);
154 for (i = 0; i < n_mem; ++i) {
155 cont.ent = get_class_member(tp, i);
156 do_type_walk(cont, pre, post, env);
158 n_types = get_class_n_subtypes(tp);
159 for (i = 0; i < n_types; ++i) {
160 cont.typ = get_class_subtype(tp, i);
161 do_type_walk(cont, pre, post, env);
166 n_mem = get_struct_n_members(tp);
167 for (i = 0; i < n_mem; ++i) {
168 cont.ent = get_struct_member(tp, i);
169 do_type_walk(cont, pre, post, env);
174 n_mem = get_method_n_params(tp);
175 for (i = 0; i < n_mem; ++i) {
176 cont.typ = get_method_param_type(tp, i);
177 do_type_walk(cont, pre, post, env);
179 n_mem = get_method_n_ress(tp);
180 for (i = 0; i < n_mem; ++i) {
181 cont.typ = get_method_res_type(tp, i);
182 do_type_walk(cont, pre, post, env);
187 n_mem = get_union_n_members(tp);
188 for (i = 0; i < n_mem; ++i) {
189 cont.ent = get_union_member(tp, i);
190 do_type_walk(cont, pre, post, env);
195 cont.typ = get_array_element_type(tp);
196 do_type_walk(cont, pre, post, env);
197 cont.ent = get_array_element_entity(tp);
198 do_type_walk(cont, pre, post, env);
201 case tpo_enumeration:
206 cont.typ = get_pointer_points_to_type(tp);
207 do_type_walk(cont, pre, post, env);
216 case tpo_uninitialized:
217 assert(0 && "Faulty type");
220 break; /* end case k_type */
223 printf(" *** Faulty type or entity! \n");
227 /* execute post method */
232 /** Check whether node contains types or entities as an attribute.
233 If so start a walk over that information. */
234 static void irn_type_walker(
235 ir_node *node, type_walk_func *pre, type_walk_func *post, void *env)
241 cont.ent = get_irn_entity_attr(node);
243 do_type_walk(cont, pre, post, env);
244 cont.typ = get_irn_type_attr(node);
246 do_type_walk(cont, pre, post, env);
249 /** Check whether node contains types or entities as an attribute.
250 If so start a walk over that information. */
251 static void start_type_walk(ir_node *node, void *ctx) {
252 type_walk_env *env = ctx;
254 type_walk_func *post;
261 irn_type_walker(node, pre, post, envi);
264 /* walker: walks over all types */
265 void type_walk(type_walk_func *pre, type_walk_func *post, void *env) {
266 int i, n_types = get_irp_n_types();
269 inc_master_type_visited();
270 for (i = 0; i < n_types; ++i) {
271 cont.typ = get_irp_type(i);
272 do_type_walk(cont, pre, post, env);
274 cont.typ = get_glob_type();
275 do_type_walk(cont, pre, post, env);
278 void type_walk_prog(type_walk_func *pre, type_walk_func *post, void *env) {
279 int i, n_irgs = get_irp_n_irgs();
282 type_walk(pre, post, env);
284 for (i = 0; i < n_irgs; ++i) {
285 ir_graph *irg = get_irp_irg(i);
286 cont.typ = get_irg_frame_type(irg);
287 do_type_walk(cont, pre, post, env);
289 cont.typ = get_method_value_param_type(get_entity_type(get_irg_entity(irg)));
291 do_type_walk(cont, pre, post, env);
294 for (i = 0; i < IR_SEGMENT_COUNT; ++i) {
295 cont.typ = get_segment_type((ir_segment_t) i);
297 do_type_walk(cont, pre, post, env);
301 void type_walk_irg(ir_graph *irg,
303 type_walk_func *post,
306 ir_graph *rem = current_ir_graph;
307 /* this is needed to pass the parameters to the walker that actually
308 walks the type information */
309 type_walk_env type_env;
313 type_env.post = post;
316 current_ir_graph = irg;
318 /* We walk over the irg to find all IR-nodes that contain an attribute
319 with type information. If we find one we call a type walker to
320 touch the reachable type information.
321 The same type can be referenced by several IR-nodes. To avoid
322 repeated visits of the same type node we must decrease the
323 type visited flag for each walk. This is done in start_type_walk().
324 Here we initially increase the flag. We only call do_type_walk that does
325 not increase the flag.
327 inc_master_type_visited();
328 irg_walk(get_irg_end(irg), start_type_walk, NULL, &type_env);
330 cont.ent = get_irg_entity(irg);
331 do_type_walk(cont, pre, post, env);
333 cont.typ = get_irg_frame_type(irg);
334 do_type_walk(cont, pre, post, env);
336 current_ir_graph = rem;
339 static void type_walk_s2s_2(type_or_ent tore,
341 type_walk_func *post,
348 switch (get_kind(tore.ent)) {
350 if (entity_visited(tore.ent)) return;
353 if (type_id == get_type_tpop(tore.typ)) {
354 cont.typ = skip_tid(tore.typ);
355 type_walk_s2s_2(cont, pre, post, env);
358 if (type_visited(tore.typ)) return;
365 switch (get_kind(tore.typ)) {
368 ir_type *tp = tore.typ;
369 mark_type_visited(tp);
370 switch (get_type_tpop_code(tp)) {
373 n = get_class_n_supertypes(tp);
374 for (i = 0; i < n; ++i) {
375 cont.typ = get_class_supertype(tp, i);
376 type_walk_s2s_2(cont, pre, post, env);
378 /* execute pre method */
383 n = get_class_n_subtypes(tp);
384 for (i = 0; i < n; ++i) {
385 cont.typ = get_class_subtype(tp, i);
386 type_walk_s2s_2(cont, pre, post, env);
389 /* execute post method */
398 case tpo_enumeration:
404 printf(" *** Faulty type! \n");
407 } break; /* end case k_type */
412 printf(" *** Faulty type or entity! \n");
417 void type_walk_super2sub(type_walk_func *pre,
418 type_walk_func *post,
422 int i, n_types = get_irp_n_types();
424 inc_master_type_visited();
425 cont.typ = get_glob_type();
426 type_walk_s2s_2(cont, pre, post, env);
427 for (i = 0; i < n_types; ++i) {
428 cont.typ = get_irp_type(i);
429 type_walk_s2s_2(cont, pre, post, env);
433 /*****************************************************************************/
436 type_walk_super_2(type_or_ent tore,
438 type_walk_func *post,
444 switch (get_kind(tore.ent)) {
446 if (entity_visited(tore.ent))
450 if (type_id == get_type_tpop(tore.typ)) {
451 cont.typ = skip_tid(tore.typ);
452 type_walk_super_2(cont, pre, post, env);
455 if (type_visited(tore.typ))
463 switch (get_kind(tore.typ)) {
466 ir_type *tp = tore.typ;
467 mark_type_visited(tp);
468 switch (get_type_tpop_code(tp)) {
471 /* execute pre method */
476 n = get_class_n_supertypes(tp);
477 for (i = 0; i < n; ++i) {
478 cont.typ = get_class_supertype(tp, i);
479 type_walk_super_2(cont, pre, post, env);
482 /* execute post method */
491 case tpo_enumeration:
497 printf(" *** Faulty type! \n");
500 } break; /* end case k_type */
505 printf(" *** Faulty type or entity! \n");
510 void type_walk_super(type_walk_func *pre,
511 type_walk_func *post,
513 int i, n_types = get_irp_n_types();
516 inc_master_type_visited();
517 cont.typ = get_glob_type();
518 type_walk_super_2(cont, pre, post, env);
519 for (i = 0; i < n_types; ++i) {
520 cont.typ = get_irp_type(i);
521 type_walk_super_2(cont, pre, post, env);
525 /*****************************************************************************/
529 class_walk_s2s_2(ir_type *tp,
530 class_walk_func *pre,
531 class_walk_func *post,
537 if (type_visited(tp)) return;
539 assert(is_Class_type(tp));
540 /* Assure all supertypes are visited before */
541 n = get_class_n_supertypes(tp);
542 for (i = 0; i < n; ++i) {
543 if (type_not_visited(get_class_supertype(tp, i)))
547 mark_type_visited(tp);
549 /* execute pre method */
554 n = get_class_n_subtypes(tp);
555 for (i = 0; i < n; ++i) {
556 class_walk_s2s_2(get_class_subtype(tp, i), pre, post, env);
558 /* execute post method */
563 void class_walk_super2sub(class_walk_func *pre,
564 class_walk_func *post,
567 int i, n_types = get_irp_n_types();
570 inc_master_type_visited();
571 for (i = 0; i < n_types; i++) {
572 tp = get_irp_type(i);
573 if (is_Class_type(tp) &&
574 (get_class_n_supertypes(tp) == 0) &&
575 type_not_visited(tp)) {
576 assert(! is_frame_type(tp));
577 assert(tp != get_glob_type());
578 class_walk_s2s_2(tp, pre, post, env);
584 /* Walks over all entities in the type */
585 void walk_types_entities(ir_type *tp,
586 entity_walk_func *doit,
591 switch (get_type_tpop_code(tp)) {
593 n = get_class_n_members(tp);
594 for (i = 0; i < n; ++i)
595 doit(get_class_member(tp, i), env);
598 n = get_struct_n_members(tp);
599 for (i = 0; i < n; ++i)
600 doit(get_struct_member(tp, i), env);
603 n = get_union_n_members(tp);
604 for (i = 0; i < n; ++i)
605 doit(get_union_member(tp, i), env);
608 doit(get_array_element_entity(tp), env);
611 case tpo_enumeration: