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))
105 mark_entity_visited(ent);
109 if (type_visited(tp))
111 mark_type_visited(tp);
117 /* execute pre method */
122 switch (get_kind(tore.ent)) {
124 cont.typ = get_entity_owner(ent);
125 do_type_walk(cont, pre, post, env);
126 cont.typ = get_entity_type(ent);
127 do_type_walk(cont, pre, post, env);
129 /* walk over the value types */
130 if (ent->initializer != NULL) {
131 walk_initializer(ent->initializer, pre, post, env);
132 } else if (entity_has_compound_ent_values(ent)) {
133 n_mem = get_compound_ent_n_values(ent);
134 for (i = 0; i < n_mem; ++i) {
135 n = get_compound_ent_value(ent, i);
136 irn_type_walker(n, pre, post, env);
141 switch (get_type_tpop_code(tp)) {
143 n_types = get_class_n_supertypes(tp);
144 for (i = 0; i < n_types; ++i) {
145 cont.typ = get_class_supertype(tp, i);
146 do_type_walk(cont, pre, post, env);
148 n_mem = get_class_n_members(tp);
149 for (i = 0; i < n_mem; ++i) {
150 cont.ent = get_class_member(tp, i);
151 do_type_walk(cont, pre, post, env);
153 n_types = get_class_n_subtypes(tp);
154 for (i = 0; i < n_types; ++i) {
155 cont.typ = get_class_subtype(tp, i);
156 do_type_walk(cont, pre, post, env);
161 n_mem = get_struct_n_members(tp);
162 for (i = 0; i < n_mem; ++i) {
163 cont.ent = get_struct_member(tp, i);
164 do_type_walk(cont, pre, post, env);
169 n_mem = get_method_n_params(tp);
170 for (i = 0; i < n_mem; ++i) {
171 cont.typ = get_method_param_type(tp, i);
172 do_type_walk(cont, pre, post, env);
174 n_mem = get_method_n_ress(tp);
175 for (i = 0; i < n_mem; ++i) {
176 cont.typ = get_method_res_type(tp, i);
177 do_type_walk(cont, pre, post, env);
182 n_mem = get_union_n_members(tp);
183 for (i = 0; i < n_mem; ++i) {
184 cont.ent = get_union_member(tp, i);
185 do_type_walk(cont, pre, post, env);
190 cont.typ = get_array_element_type(tp);
191 do_type_walk(cont, pre, post, env);
192 cont.ent = get_array_element_entity(tp);
193 do_type_walk(cont, pre, post, env);
196 case tpo_enumeration:
201 cont.typ = get_pointer_points_to_type(tp);
202 do_type_walk(cont, pre, post, env);
211 case tpo_uninitialized:
212 assert(0 && "Faulty type");
215 break; /* end case k_type */
218 printf(" *** Faulty type or entity! \n");
222 /* execute post method */
227 /** Check whether node contains types or entities as an attribute.
228 If so start a walk over that information. */
229 static void irn_type_walker(
230 ir_node *node, type_walk_func *pre, type_walk_func *post, void *env)
236 cont.ent = get_irn_entity_attr(node);
238 do_type_walk(cont, pre, post, env);
239 cont.typ = get_irn_type_attr(node);
241 do_type_walk(cont, pre, post, env);
244 /** Check whether node contains types or entities as an attribute.
245 If so start a walk over that information. */
246 static void start_type_walk(ir_node *node, void *ctx)
248 type_walk_env *env = ctx;
250 type_walk_func *post;
257 irn_type_walker(node, pre, post, envi);
260 /* walker: walks over all types */
261 void type_walk(type_walk_func *pre, type_walk_func *post, void *env)
263 int i, n_types = get_irp_n_types();
266 irp_reserve_resources(irp, IR_RESOURCE_TYPE_VISITED);
267 inc_master_type_visited();
268 for (i = 0; i < n_types; ++i) {
269 cont.typ = get_irp_type(i);
270 do_type_walk(cont, pre, post, env);
272 cont.typ = get_glob_type();
273 do_type_walk(cont, pre, post, env);
274 irp_free_resources(irp, IR_RESOURCE_TYPE_VISITED);
277 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 = IR_SEGMENT_FIRST; i <= IR_SEGMENT_LAST; ++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 irp_reserve_resources(irp, IR_RESOURCE_TYPE_VISITED);
328 inc_master_type_visited();
329 irg_walk(get_irg_end(irg), start_type_walk, NULL, &type_env);
331 cont.ent = get_irg_entity(irg);
332 do_type_walk(cont, pre, post, env);
334 cont.typ = get_irg_frame_type(irg);
335 do_type_walk(cont, pre, post, env);
337 current_ir_graph = rem;
338 irp_free_resources(irp, IR_RESOURCE_TYPE_VISITED);
341 static void type_walk_s2s_2(type_or_ent tore,
343 type_walk_func *post,
350 switch (get_kind(tore.ent)) {
352 if (entity_visited(tore.ent)) return;
355 if (type_visited(tore.typ)) return;
362 switch (get_kind(tore.typ)) {
365 ir_type *tp = tore.typ;
366 mark_type_visited(tp);
367 switch (get_type_tpop_code(tp)) {
370 n = get_class_n_supertypes(tp);
371 for (i = 0; i < n; ++i) {
372 cont.typ = get_class_supertype(tp, i);
373 type_walk_s2s_2(cont, pre, post, env);
375 /* execute pre method */
379 n = get_class_n_subtypes(tp);
380 for (i = 0; i < n; ++i) {
381 cont.typ = get_class_subtype(tp, i);
382 type_walk_s2s_2(cont, pre, post, env);
385 /* execute post method */
394 case tpo_enumeration:
400 printf(" *** Faulty type! \n");
403 } break; /* end case k_type */
408 printf(" *** Faulty type or entity! \n");
413 void type_walk_super2sub(type_walk_func *pre,
414 type_walk_func *post,
418 int i, n_types = get_irp_n_types();
420 irp_reserve_resources(irp, IR_RESOURCE_TYPE_VISITED);
421 inc_master_type_visited();
422 cont.typ = get_glob_type();
423 type_walk_s2s_2(cont, pre, post, env);
424 for (i = 0; i < n_types; ++i) {
425 cont.typ = get_irp_type(i);
426 type_walk_s2s_2(cont, pre, post, env);
428 irp_free_resources(irp, IR_RESOURCE_TYPE_VISITED);
431 /*****************************************************************************/
433 static void type_walk_super_2(type_or_ent tore, type_walk_func *pre,
434 type_walk_func *post, void *env)
440 switch (get_kind(tore.ent)) {
442 if (entity_visited(tore.ent))
446 if (type_visited(tore.typ))
454 switch (get_kind(tore.typ)) {
457 ir_type *tp = tore.typ;
458 mark_type_visited(tp);
459 switch (get_type_tpop_code(tp)) {
462 /* execute pre method */
466 n = get_class_n_supertypes(tp);
467 for (i = 0; i < n; ++i) {
468 cont.typ = get_class_supertype(tp, i);
469 type_walk_super_2(cont, pre, post, env);
472 /* execute post method */
481 case tpo_enumeration:
487 printf(" *** Faulty type! \n");
490 } break; /* end case k_type */
495 printf(" *** Faulty type or entity! \n");
500 void type_walk_super(type_walk_func *pre, type_walk_func *post, void *env)
502 int i, n_types = get_irp_n_types();
505 irp_reserve_resources(irp, IR_RESOURCE_TYPE_VISITED);
506 inc_master_type_visited();
507 cont.typ = get_glob_type();
508 type_walk_super_2(cont, pre, post, env);
509 for (i = 0; i < n_types; ++i) {
510 cont.typ = get_irp_type(i);
511 type_walk_super_2(cont, pre, post, env);
513 irp_free_resources(irp, IR_RESOURCE_TYPE_VISITED);
516 /*****************************************************************************/
519 static void class_walk_s2s_2(ir_type *tp, class_walk_func *pre,
520 class_walk_func *post, void *env)
525 if (type_visited(tp)) return;
527 assert(is_Class_type(tp));
528 /* Assure all supertypes are visited before */
529 n = get_class_n_supertypes(tp);
530 for (i = 0; i < n; ++i) {
531 if (type_not_visited(get_class_supertype(tp, i)))
535 mark_type_visited(tp);
537 /* execute pre method */
541 n = get_class_n_subtypes(tp);
542 for (i = 0; i < n; ++i) {
543 class_walk_s2s_2(get_class_subtype(tp, i), pre, post, env);
545 /* execute post method */
550 void class_walk_super2sub(class_walk_func *pre,
551 class_walk_func *post,
554 int i, n_types = get_irp_n_types();
557 irp_reserve_resources(irp, IR_RESOURCE_TYPE_VISITED);
558 inc_master_type_visited();
559 for (i = 0; i < n_types; i++) {
560 tp = get_irp_type(i);
561 if (is_Class_type(tp) &&
562 (get_class_n_supertypes(tp) == 0) &&
563 type_not_visited(tp)) {
564 assert(! is_frame_type(tp));
565 assert(tp != get_glob_type());
566 class_walk_s2s_2(tp, pre, post, env);
569 irp_free_resources(irp, IR_RESOURCE_TYPE_VISITED);
573 /* Walks over all entities in the type */
574 void walk_types_entities(ir_type *tp,
575 entity_walk_func *doit,
580 switch (get_type_tpop_code(tp)) {
582 n = get_class_n_members(tp);
583 for (i = 0; i < n; ++i)
584 doit(get_class_member(tp, i), env);
587 n = get_struct_n_members(tp);
588 for (i = 0; i < n; ++i)
589 doit(get_struct_member(tp, i), env);
592 n = get_union_n_members(tp);
593 for (i = 0; i < n; ++i)
594 doit(get_union_member(tp, i), env);
597 doit(get_array_element_entity(tp), env);
600 case tpo_enumeration: