3 * File name: ir/common/firmwalk.c
4 * Purpose: Walker that touches all Firm data structures
5 * Author: Sebastian Felis
9 * Copyright: (c) 2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
32 /** obstack for firm walker */
33 static struct obstack fw_obst;
35 /** This map stores all types of firm */
36 static pmap *mode_map = NULL;
37 /** This map stores all types of firm */
38 static pmap *type_map = NULL;
39 /** This map stores all entities of firm */
40 static pmap *entity_map = NULL;
42 /** Internal data structure of firm walker to collect
43 * some information of firm ir. The collected data will be stored
44 * into the link field of ir node. All graphs have a list of its
45 * blocks and all blocks have a list of their nodes. */
47 ir_node **list; /**< List of blocks or nodes */
48 void *link; /**< Public link field. The public link field of firm nodes
49 is wrapped by set_firm_walk_link and
50 get_firm_walk_link. */
54 /** Access macros to fw_data structure */
55 #define FW_GET_DATA_LIST(s) ((s)->list)
56 #define FW_SET_DATA_LIST(s, t) ((s)->list = (t))
57 #define FW_GET_DATA_LINK(s) ((s)->link)
58 #define FW_SET_DATA_LINK(s, t) ((s)->link = (t))
61 /** Returns own data struct of the firm walker.
63 * If no structure defined (firm link is NULL) allocate a new
64 * struct to the fgd obstack. Only ir graph and block nodes
65 * will allocate this structure.
66 * - ir graph collect its block
67 * - block collect its nodes
70 fw_data *fw_get_data(void *thing)
75 switch (get_kind(thing)) {
77 data = get_irg_link(thing);
78 /* init block list of graph */
81 /* allocate new firm walk structure */
82 data = obstack_alloc(&fw_obst, sizeof(fw_data));
83 memset(data, 0, sizeof(fw_data));
84 set_irg_link(thing, data);
85 /* allocate block list */
86 FW_GET_DATA_LIST(data) = NEW_ARR_F(ir_node *, 0);
90 /* init node list of block */
93 data = get_irn_link(thing);
96 /* allocate new firm walk structure */
97 data = obstack_alloc(&fw_obst, sizeof(fw_data));
98 memset(data, 0, sizeof(fw_data));
99 set_irn_link(thing, data);
100 /* allocate node list */
101 FW_GET_DATA_LIST(data) = NEW_ARR_F(ir_node *, 0);
105 default: {} /* other kinds of firm nodes */
111 /** Free all collected data in ir graphs and nodes.
112 * An ir graph or an ir block node has a list as a
113 * dynamic array, which will be deleted here. */
115 void fw_free_data(void *thing)
117 fw_data *data = NULL;
121 switch (get_kind(thing)) {
123 data = get_irg_link(thing);
124 /* delete block list of graph */
127 DEL_ARR_F(FW_GET_DATA_LIST(data));
128 set_irg_link(thing, NULL);
132 /* delete node list of block */
135 data = get_irn_link(thing);
138 DEL_ARR_F(FW_GET_DATA_LIST(data));
139 set_irn_link(thing, NULL);
143 default: {} /* other kinds of firm nodes */
147 /* documentation in header file */
148 void set_firm_walk_link(void *thing, void *link)
153 switch (get_kind(thing)) {
155 set_entity_link(thing, link);
158 set_type_link(thing, link);
161 data = fw_get_data(thing);
162 FW_SET_DATA_LINK(data, link);
167 data = fw_get_data(thing);
168 FW_SET_DATA_LINK(data, link);
171 set_irn_link(thing, link);
174 set_mode_link(thing, link);
176 default: {} /* other kinds of firm nodes */
180 /* documentation in header file */
181 void *get_firm_walk_link(void *thing)
185 switch (get_kind(thing)) {
187 return get_entity_link(thing);
189 return get_type_link(thing);
191 data = fw_get_data(thing);
192 return FW_GET_DATA_LINK(data);
196 data = fw_get_data(thing);
197 return FW_GET_DATA_LINK(data);
200 return get_irn_link(thing);
202 return get_mode_link(thing);
208 /** Set link field of a ir node to NULL */
210 void fw_clear_link(ir_node * node, void * env)
212 set_irn_link(node, NULL);
215 /** Fill maps of type and entity.
216 * This function will be called by the firm walk initializer
217 * to collect all types and entities of program's firm ir.
218 * All types will be colleced in the hash table type_map
219 * and all entity are stored in entity_map. The mode of an
220 * type will be collected as well.
222 * @param tore Type or entity
223 * @param env Environment pointer (currently unused)
226 void fw_collect_tore(type_or_ent *tore, void *env)
232 switch (get_kind(tore)) {
234 ent = (entity *)tore;
235 /* append entity to list */
236 set_entity_link(ent, NULL);
237 if (!pmap_contains(entity_map, ent))
238 pmap_insert(entity_map, ent, env);
242 mode = get_type_mode(tp);
243 /* append type to list */
244 set_type_link(tp, NULL);
245 if (!pmap_contains(type_map, tp))
246 pmap_insert(type_map, tp, env);
248 /* insert only modes (non atomic types, i.e. class, array or struct
249 have no mode. The link field will be cleared in the walk_do_mode()
250 callback function. */
251 if ((NULL != mode) && (!pmap_contains(mode_map, mode)))
252 pmap_insert(mode_map, mode, env);
258 /** Collect all data from nodes. Block appends itself to
259 * the corresponding ir graph and other nodes appends itself
260 * to block list. Collects also the modes of each node to get
263 * @param irn IR node pointer.
264 * @param env Environment pointer (currently unused)
267 void fw_collect_irn(ir_node *irn, void *env)
270 ir_mode *mode = get_irn_mode(irn);
272 /* The link field will be cleared in the walk_do_mode()
273 callback function. */
274 if ((NULL != mode) && (!pmap_contains(mode_map, mode)))
275 pmap_insert(mode_map, mode, env);
280 /* add this block to ir graph's block list */
281 data = fw_get_data(get_current_ir_graph());
282 ARR_APP1(ir_node *, FW_GET_DATA_LIST(data), irn);
284 /* non block nodes */
287 /* add this node to block's node list */
288 ir_node *block = get_nodes_block(irn);
289 data = fw_get_data(block);
290 ARR_APP1(ir_node *, FW_GET_DATA_LIST(data), irn);
294 /** Irg walker function to free all collected data from nodes */
296 void fw_free_colleted_data(ir_node *irn, void *env)
298 /* Free node list from blocks */
305 /** Initialize all walking data.
307 * Collect all specific data like types, entities, ir graphs, blocks, nodes
308 * from current firm structures.
310 void firm_walk_init(firm_walk_flags flags)
315 obstack_init(&fw_obst);
317 /* Init map of modes and lists of type and entity. If map or list
318 allready exists, free it. */
321 pmap_destroy(mode_map);
323 mode_map = pmap_create();
327 pmap_destroy(type_map);
329 type_map = pmap_create();
333 pmap_destroy(entity_map);
335 entity_map = pmap_create();
337 /* insert internal modes to mode hash. The link field will be cleared
338 in the walk_do_mode() callback function.
339 Other used modes are added by collecting types */
342 ### RG: should be done by inspection the mode of all irn
344 pmap_insert(mode_map, mode_BB, NULL);
345 pmap_insert(mode_map, mode_T, NULL);
346 pmap_insert(mode_map, mode_ANY, NULL);
347 pmap_insert(mode_map, mode_BAD, NULL);
348 pmap_insert(mode_map, mode_X, NULL);
349 pmap_insert(mode_map, mode_M, NULL);
350 pmap_insert(mode_map, mode_b, NULL);
353 /* Collect all types (also unused types) if flag is set */
354 if (FW_WITH_ALL_TYPES & flags)
355 type_walk(fw_collect_tore, NULL, NULL);
357 /* for each ir graph */
358 for (i = 0; i < get_irp_n_irgs(); i++)
360 ir_graph *irg = get_irp_irg(i);
361 set_irg_link(irg, NULL);
363 type_walk_irg(irg, fw_collect_tore, NULL, NULL);
365 irg_walk_graph(irg, fw_clear_link, fw_collect_irn, NULL);
369 /** This function should call after using the firm walker to free
370 * all collected data and frees the used obstack */
371 void firm_walk_finalize(void)
375 /* free all used maps and lists */
376 pmap_destroy(mode_map);
378 pmap_destroy(type_map);
380 pmap_destroy(entity_map);
383 /* free all collected data from ir graphs and nodes */
384 for (i = 0; i < get_irp_n_irgs(); i++)
386 ir_graph *irg = get_irp_irg(i);
388 irg_walk_graph(get_irp_irg(i), NULL, fw_free_colleted_data, NULL);
392 obstack_free(&fw_obst, NULL);
395 /** Dumps the firm ir.
397 * After initializing the firm walker by calling firm_walk_init()
398 * the firm structure could be accessed by definign the firm walk interface
399 * wif. This function could be called serveral times to customize the
400 * walk order or definitions.
402 * @param wif Walk interface which contains the callback function
404 * @see firm_walk_interface */
405 void firm_walk(firm_walk_interface *wif)
407 int irg_i, block_i, block_list_len, irn_i, irn_list_len;
410 ir_node *block, **block_list, **irn_list;
411 ir_graph *saved_irg = current_ir_graph;
413 assert(wif && "firm_walk() in firmwalk.c: No walking interface defined!");
415 /* walk over all modes */
416 if (wif->do_mode_init) wif->do_mode_init(wif->env);
419 for (entry = pmap_first(mode_map); entry; entry = pmap_next(mode_map))
421 set_mode_link((ir_mode*)entry->key, NULL);
422 wif->do_mode(entry->key, wif->env);
425 if (wif->do_mode_finalize) wif->do_mode_finalize(wif->env);
427 /* walk over all types */
428 if (wif->do_type_init) wif->do_type_init(wif->env);
431 for (entry = pmap_first(type_map); entry; entry = pmap_next(type_map))
432 wif->do_type((type *)entry->key, wif->env);
434 if (wif->do_type_finalize) wif->do_type_finalize(wif->env);
436 /* walk over all entities */
437 if (wif->do_entity_init) wif->do_entity_init(wif->env);
440 for (entry = pmap_first(entity_map); entry; entry = pmap_next(entity_map))
441 wif->do_entity((entity *)entry->key, wif->env);
443 if (wif->do_entity_finalize) wif->do_entity_finalize(wif->env);
446 /* Dump graphs ================================================= */
447 if (wif->do_graph_init) wif->do_graph_init(wif->env);
449 for (irg_i = 0; irg_i < get_irp_n_irgs(); irg_i++)
451 current_ir_graph = get_irp_irg(irg_i);
453 /* walk over all ir graph */
454 if (wif->do_graph) wif->do_graph(current_ir_graph, wif->env);
456 /* walk over all irg's block nested ========================== */
457 data = fw_get_data(current_ir_graph);
458 block_list = FW_GET_DATA_LIST(data);
459 block_list_len = ARR_LEN(block_list);
460 for (block_i = 0; block_i < block_list_len; block_i++)
462 if (wif->do_block_init) wif->do_block_init(current_ir_graph, wif->env);
464 block = (ir_node *)block_list[block_i];
465 if (wif->do_block) wif->do_block(block, wif->env);
467 /* walk over all block's ir nodes nested =================== */
468 data = fw_get_data(block);
469 irn_list = FW_GET_DATA_LIST(data);
470 irn_list_len = ARR_LEN(irn_list);
472 /* call block as prefix ir node */
473 if ((wif->do_node) &&
474 (wif->flags & (FW_DUMP_BLOCK_AS_IRN | FW_DUMP_IRN_IN_PREFIX)))
475 wif->do_node(block, wif->env);
477 /* do ir nodes in prefix or postfix order? */
478 if (wif->flags & FW_DUMP_IRN_IN_PREFIX)
479 irn_i = irn_list_len-1;
483 while (irn_i >= 0 && irn_i < irn_list_len)
485 if (wif->do_node) wif->do_node((ir_node *)irn_list[irn_i], wif->env);
487 /* do ir nodes in prefix or postfix order? */
488 if (wif->flags & FW_DUMP_IRN_IN_PREFIX)
493 /* call block as postfix ir node */
494 if ((wif->do_node) &&
495 ((wif->flags & (FW_DUMP_BLOCK_AS_IRN | FW_DUMP_IRN_IN_PREFIX))
496 == FW_DUMP_BLOCK_AS_IRN))
497 wif->do_node(block, wif->env);
499 /* wall over all block's ir nodes nested end =============== */
501 if(wif->do_block_post)
502 wif->do_block_post(block, wif->env);
504 } /* for each block */
506 if (wif->do_block_finalize)
507 wif->do_block_finalize(current_ir_graph, wif->env);
509 /* walk over all irg's block nested end ====================== */
510 if(wif->do_graph_post)
511 wif->do_graph_post(current_ir_graph, wif->env);
513 } /* for each ir graph irg */
515 if(wif->do_graph_finalize)
516 wif->do_graph_finalize(wif->env);
518 /** ### ToDo: Dump const_code_irg ?? No! Dump const code with entities, types etc. */
520 /* restore the state of current_ir_graph */
521 current_ir_graph = saved_irg;