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 Walker that touches all Firm data structures
23 * @author Sebastian Felis
43 /** obstack for firm walker */
44 static struct obstack fw_obst;
46 /** This map stores all types of firm */
47 static pmap *type_map = NULL;
48 /** This map stores all entities of firm */
49 static pmap *entity_map = NULL;
51 /** Internal data structure of firm walker to collect
52 * some information of firm ir. The collected data will be stored
53 * into the link field of ir node. All graphs have a list of its
54 * blocks and all blocks have a list of their nodes. */
56 ir_node **list; /**< List of blocks or nodes */
57 void *link; /**< Public link field. The public link field of firm nodes
58 is wrapped by set_firm_walk_link and
59 get_firm_walk_link. */
63 /** Access macros to fw_data structure */
64 #define FW_GET_DATA_LIST(s) ((s)->list)
65 #define FW_SET_DATA_LIST(s, t) ((s)->list = (t))
66 #define FW_GET_DATA_LINK(s) ((s)->link)
67 #define FW_SET_DATA_LINK(s, t) ((s)->link = (t))
70 /** Returns own data struct of the firm walker.
72 * If no structure defined (firm link is NULL) allocate a new
73 * struct to the fgd obstack. Only ir graph and block nodes
74 * will allocate this structure.
75 * - ir graph collect its block
76 * - block collect its nodes
79 fw_data *fw_get_data(void *thing)
84 switch (get_kind(thing)) {
86 data = get_irg_link(thing);
87 /* init block list of graph */
90 /* allocate new firm walk structure */
91 data = obstack_alloc(&fw_obst, sizeof(fw_data));
92 memset(data, 0, sizeof(fw_data));
93 set_irg_link(thing, data);
94 /* allocate block list */
95 FW_GET_DATA_LIST(data) = NEW_ARR_F(ir_node *, 0);
99 /* init node list of block */
102 data = get_irn_link(thing);
105 /* allocate new firm walk structure */
106 data = obstack_alloc(&fw_obst, sizeof(fw_data));
107 memset(data, 0, sizeof(fw_data));
108 set_irn_link(thing, data);
109 /* allocate node list */
110 FW_GET_DATA_LIST(data) = NEW_ARR_F(ir_node *, 0);
114 default: {} /* other kinds of firm nodes */
120 /** Free all collected data in ir graphs and nodes.
121 * An ir graph or an ir block node has a list as a
122 * dynamic array, which will be deleted here. */
124 void fw_free_data(void *thing)
126 fw_data *data = NULL;
130 switch (get_kind(thing)) {
132 data = get_irg_link(thing);
133 /* delete block list of graph */
136 DEL_ARR_F(FW_GET_DATA_LIST(data));
137 set_irg_link(thing, NULL);
141 /* delete node list of block */
144 data = get_irn_link(thing);
147 DEL_ARR_F(FW_GET_DATA_LIST(data));
148 set_irn_link(thing, NULL);
152 default: {} /* other kinds of firm nodes */
156 /* documentation in header file */
157 void set_firm_walk_link(void *thing, void *link)
162 switch (get_kind(thing)) {
164 set_entity_link(thing, link);
167 set_type_link(thing, link);
170 data = fw_get_data(thing);
171 FW_SET_DATA_LINK(data, link);
176 data = fw_get_data(thing);
177 FW_SET_DATA_LINK(data, link);
180 set_irn_link(thing, link);
183 set_mode_link(thing, link);
185 default: {} /* other kinds of firm nodes */
189 /* documentation in header file */
190 void *get_firm_walk_link(void *thing)
194 switch (get_kind(thing)) {
196 return get_entity_link(thing);
198 return get_type_link(thing);
200 data = fw_get_data(thing);
201 return FW_GET_DATA_LINK(data);
205 data = fw_get_data(thing);
206 return FW_GET_DATA_LINK(data);
209 return get_irn_link(thing);
211 return get_mode_link(thing);
217 /** Fill maps of type and entity.
218 * This function will be called by the firm walk initializer
219 * to collect all types and entities of program's firm ir.
220 * All types will be collected in the hash table type_map
221 * and all entity are stored in entity_map. The mode of an
222 * type will be collected as well.
224 * @param tore Type or entity
225 * @param env Environment pointer (currently unused)
228 void fw_collect_tore(type_or_ent tore, void *env)
233 switch (get_kind(tore.ent)) {
236 /* append entity to list */
237 set_entity_link(ent, NULL);
238 if (!pmap_contains(entity_map, ent))
239 pmap_insert(entity_map, ent, env);
244 /* append type to list */
245 set_type_link(tp, NULL);
246 if (!pmap_contains(type_map, tp))
247 pmap_insert(type_map, tp, env);
253 /** Collect all data from nodes. Block appends itself to
254 * the corresponding ir graph and other nodes appends itself
255 * to block list. Collects also the modes of each node to get
258 * @param irn IR node pointer.
259 * @param env Environment pointer (currently unused)
262 void fw_collect_irn(ir_node *irn, void *env)
269 /* add this block to ir graph's block list */
270 data = fw_get_data(get_current_ir_graph());
271 ARR_APP1(ir_node *, FW_GET_DATA_LIST(data), irn);
273 /* non block nodes */
275 /* add this node to block's node list */
276 ir_node *block = get_nodes_block(irn);
277 data = fw_get_data(block);
278 ARR_APP1(ir_node *, FW_GET_DATA_LIST(data), irn);
282 /** Irg walker function to free all collected data from nodes */
284 void fw_free_colleted_data(ir_node *irn, void *env)
287 /* Free node list from blocks */
294 /** Initialize all walking data.
296 * Collect all specific data like types, entities, ir graphs, blocks, nodes
297 * from current firm structures.
299 void firm_walk_init(firm_walk_flags flags)
304 obstack_init(&fw_obst);
306 /* Init map of type and entity. If map or list
307 already exists, free it. */
309 pmap_destroy(type_map);
310 type_map = pmap_create();
313 pmap_destroy(entity_map);
314 entity_map = pmap_create();
316 /* Collect all types (also unused types) if flag is set */
317 if (FW_WITH_ALL_TYPES & flags)
318 type_walk(fw_collect_tore, NULL, NULL);
320 /* for each ir graph */
321 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
322 ir_graph *irg = get_irp_irg(i);
323 set_irg_link(irg, NULL);
325 type_walk_irg(irg, fw_collect_tore, NULL, NULL);
327 irg_walk_graph(irg, firm_clear_link, fw_collect_irn, NULL);
331 /** This function should call after using the firm walker to free
332 * all collected data and frees the used obstack */
333 void firm_walk_finalize(void)
337 /* free all used maps and lists */
338 pmap_destroy(type_map);
340 pmap_destroy(entity_map);
343 /* free all collected data from ir graphs and nodes */
344 for (i = 0; i < get_irp_n_irgs(); i++)
346 ir_graph *irg = get_irp_irg(i);
348 irg_walk_graph(get_irp_irg(i), NULL, fw_free_colleted_data, NULL);
352 obstack_free(&fw_obst, NULL);
355 /** Dumps the firm ir.
357 * After initializing the firm walker by calling firm_walk_init()
358 * the firm structure could be accessed by defining the firm walk interface
359 * wif. This function could be called several times to customize the
360 * walk order or definitions.
362 * @param wif Walk interface which contains the callback function
364 * @see firm_walk_interface */
365 void firm_walk(firm_walk_interface *wif)
367 int mode_i, irg_i, block_i, block_list_len, irn_i, irn_list_len;
370 ir_node *block, **block_list, **irn_list;
371 ir_graph *saved_irg = current_ir_graph;
373 assert(wif && "firm_walk() in firmwalk.c: No walking interface defined!");
375 /* walk over all modes */
376 if (wif->do_mode_init) wif->do_mode_init(wif->env);
379 for (mode_i = get_irp_n_modes() - 1; mode_i >= 0; --mode_i)
380 wif->do_mode(get_irp_mode(mode_i), wif->env);
382 if (wif->do_mode_finalize) wif->do_mode_finalize(wif->env);
384 /* walk over all types */
385 if (wif->do_type_init) wif->do_type_init(wif->env);
388 for (entry = pmap_first(type_map); entry; entry = pmap_next(type_map))
389 wif->do_type((ir_type *)entry->key, wif->env);
391 if (wif->do_type_finalize) wif->do_type_finalize(wif->env);
393 /* walk over all entities */
394 if (wif->do_entity_init) wif->do_entity_init(wif->env);
397 for (entry = pmap_first(entity_map); entry; entry = pmap_next(entity_map))
398 wif->do_entity((ir_entity *)entry->key, wif->env);
400 if (wif->do_entity_finalize) wif->do_entity_finalize(wif->env);
403 /* Dump graphs ================================================= */
404 if (wif->do_graph_init) wif->do_graph_init(wif->env);
406 for (irg_i = 0; irg_i < get_irp_n_irgs(); irg_i++)
408 current_ir_graph = get_irp_irg(irg_i);
410 /* walk over all ir graph */
411 if (wif->do_graph) wif->do_graph(current_ir_graph, wif->env);
413 /* walk over all irg's block nested ========================== */
414 data = fw_get_data(current_ir_graph);
415 block_list = FW_GET_DATA_LIST(data);
416 block_list_len = ARR_LEN(block_list);
417 for (block_i = 0; block_i < block_list_len; block_i++)
419 if (wif->do_block_init) wif->do_block_init(current_ir_graph, wif->env);
421 block = (ir_node *)block_list[block_i];
422 if (wif->do_block) wif->do_block(block, wif->env);
424 /* walk over all block's ir nodes nested =================== */
425 data = fw_get_data(block);
426 irn_list = FW_GET_DATA_LIST(data);
427 irn_list_len = ARR_LEN(irn_list);
429 /* call block as prefix ir node */
430 if ((wif->do_node) &&
431 (wif->flags & (FW_DUMP_BLOCK_AS_IRN | FW_DUMP_IRN_IN_PREFIX)))
432 wif->do_node(block, wif->env);
434 /* do ir nodes in prefix or postfix order? */
435 if (wif->flags & FW_DUMP_IRN_IN_PREFIX)
436 irn_i = irn_list_len-1;
440 while (irn_i >= 0 && irn_i < irn_list_len)
442 if (wif->do_node) wif->do_node((ir_node *)irn_list[irn_i], wif->env);
444 /* do ir nodes in prefix or postfix order? */
445 if (wif->flags & FW_DUMP_IRN_IN_PREFIX)
450 /* call block as postfix ir node */
451 if ((wif->do_node) &&
452 ((wif->flags & (FW_DUMP_BLOCK_AS_IRN | FW_DUMP_IRN_IN_PREFIX))
453 == FW_DUMP_BLOCK_AS_IRN))
454 wif->do_node(block, wif->env);
456 /* wall over all block's ir nodes nested end =============== */
458 if(wif->do_block_post)
459 wif->do_block_post(block, wif->env);
461 } /* for each block */
463 if (wif->do_block_finalize)
464 wif->do_block_finalize(current_ir_graph, wif->env);
466 /* walk over all irg's block nested end ====================== */
467 if(wif->do_graph_post)
468 wif->do_graph_post(current_ir_graph, wif->env);
470 } /* for each ir graph irg */
472 if(wif->do_graph_finalize)
473 wif->do_graph_finalize(wif->env);
475 /** ### ToDo: Dump const_code_irg ?? No! Dump const code with entities, types etc. */
477 /* restore the state of current_ir_graph */
478 current_ir_graph = saved_irg;