15 /** obstack for firm walker */
16 static struct obstack fw_obst;
18 /** This map stores all types of firm */
19 static pmap *mode_map = NULL;
20 /** This map stores all types of firm */
21 static pmap *type_map = NULL;
22 /** This map stores all entities of firm */
23 static pmap *entity_map = NULL;
25 /** Internal data structure of firm walker to collect
26 * some information of firm ir. The collected data will be stored
27 * into the link field of ir node. All graphs have a list of its
28 * blocks and all blocks have a list of their nodes. */
30 ir_node **list; /**< List of blocks or nodes */
31 void *link; /**< Public link field. The public link field of firm nodes
32 is wrapped by set_firm_walk_link and
33 get_firm_walk_link. */
37 /** Access macros to fw_data structure */
38 #define FW_GET_DATA_LIST(s) ((s)->list)
39 #define FW_SET_DATA_LIST(s, t) ((s)->list = (t))
40 #define FW_GET_DATA_LINK(s) ((s)->link)
41 #define FW_SET_DATA_LINK(s, t) ((s)->link = (t))
44 /** Returns own data struct of the firm walker.
46 * If no structure defined (firm link is NULL) allocate a new
47 * struct to the fgd obstack. Only ir graph and block nodes
48 * will allocate this structure.
49 * - ir graph collect its block
50 * - block collect its nodes
53 fw_data *fw_get_data(void *thing)
58 switch (get_kind(thing)) {
60 data = get_irg_link(thing);
61 /* init block list of graph */
64 /* allocate new firm walk structure */
65 data = obstack_alloc(&fw_obst, sizeof(fw_data));
66 memset(data, 0, sizeof(fw_data));
67 set_irg_link(thing, data);
68 /* allocate block list */
69 FW_GET_DATA_LIST(data) = NEW_ARR_F(ir_node *, 0);
73 /* init node list of block */
76 data = get_irn_link(thing);
79 /* allocate new firm walk structure */
80 data = obstack_alloc(&fw_obst, sizeof(fw_data));
81 memset(data, 0, sizeof(fw_data));
82 set_irn_link(thing, data);
83 /* allocate node list */
84 FW_GET_DATA_LIST(data) = NEW_ARR_F(ir_node *, 0);
88 default: {} // other kinds of firm nodes
94 /** Free all collected data in ir graphs and nodes.
95 * An ir graph or an ir block node has a list as a
96 * dynamic array, which will be deleted here. */
98 void fw_free_data(void *thing)
100 fw_data *data = NULL;
104 switch (get_kind(thing)) {
106 data = get_irg_link(thing);
107 /* delete block list of graph */
110 DEL_ARR_F(FW_GET_DATA_LIST(data));
111 set_irg_link(thing, NULL);
115 /* delete node list of block */
118 data = get_irn_link(thing);
121 DEL_ARR_F(FW_GET_DATA_LIST(data));
122 set_irn_link(thing, NULL);
126 default: {} // other kinds of firm nodes
130 // documentation in header file
131 void set_firm_walk_link(void *thing, void *link)
136 switch (get_kind(thing)) {
138 set_entity_link(thing, link);
141 set_type_link(thing, link);
144 data = fw_get_data(thing);
145 FW_SET_DATA_LINK(data, link);
150 data = fw_get_data(thing);
151 FW_SET_DATA_LINK(data, link);
154 set_irn_link(thing, link);
157 set_mode_link(thing, link);
159 default: {} // other kinds of firm nodes
163 // documentation in header file
164 void *get_firm_walk_link(void *thing)
168 switch (get_kind(thing)) {
170 return get_entity_link(thing);
172 return get_type_link(thing);
174 data = fw_get_data(thing);
175 return FW_GET_DATA_LINK(data);
179 data = fw_get_data(thing);
180 return FW_GET_DATA_LINK(data);
183 return get_irn_link(thing);
185 return get_mode_link(thing);
191 /** Set link field of a ir node to NULL */
193 void fw_clear_link(ir_node * node, void * env)
195 set_irn_link(node, NULL);
198 /** Fill maps of type and entity.
199 * This function will be called by the firm walk initializer
200 * to collect all types and entities of program's firm ir.
201 * All types will be colleced in the hash table type_map
202 * and all entity are stored in entity_map. The mode of an
203 * type will be collected as well.
205 * @param tore Type or entity
206 * @param env Environment pointer (currently unused)
209 void fw_collect_tore(type_or_ent *tore, void *env)
215 switch (get_kind(tore)) {
217 ent = (entity *)tore;
218 // append entity to list
219 set_entity_link(ent, NULL);
220 if (!pmap_contains(entity_map, ent))
221 pmap_insert(entity_map, ent, env);
225 mode = get_type_mode(tp);
226 // append type to list
227 set_type_link(tp, NULL);
228 if (!pmap_contains(type_map, tp))
229 pmap_insert(type_map, tp, env);
231 /* insert only modes (non atomic types, i.e. class, array or struct
232 have no mode. The link field will be cleared in the walk_do_mode()
233 callback function. */
234 if ((NULL != mode) && (!pmap_contains(mode_map, mode)))
235 pmap_insert(mode_map, mode, env);
241 /** Collect all data from nodes. Block appends itself to
242 * the corresponding ir graph and other nodes appends itself
243 * to block list. Collects also the modes of each node to get
246 * @param irn IR node pointer.
247 * @param env Environment pointer (currently unused)
250 void fw_collect_irn(ir_node *irn, void *env)
253 ir_mode* mode = get_irn_mode(irn);
255 /* The link field will be cleared in the walk_do_mode()
256 callback function. */
257 if ((NULL != mode) && (!pmap_contains(mode_map, mode)))
258 pmap_insert(mode_map, mode, env);
263 /* add this block to ir graph's block list */
264 data = fw_get_data(get_current_ir_graph());
265 ARR_APP1(ir_node *, FW_GET_DATA_LIST(data), irn);
267 /* non block nodes */
270 /* add this node to block's node list */
271 ir_node *block = get_nodes_Block(irn);
272 data = fw_get_data(block);
273 ARR_APP1(ir_node *, FW_GET_DATA_LIST(data), irn);
277 /** Irg walker function to free all collected data from nodes */
279 void fw_free_colleted_data(ir_node *irn, void *env)
281 /* Free node list from blocks */
288 /** Initialize all walking data.
290 * Collect all specific data like types, entities, ir graphs, blocks, nodes
291 * from current firm structures.
293 void firm_walk_init(firm_walk_flags flags)
298 obstack_init(&fw_obst);
300 /* Init map of modes and lists of type and entity. If map or list
301 allready exists, free it. */
304 pmap_destroy(mode_map);
306 mode_map = pmap_create();
310 pmap_destroy(type_map);
312 type_map = pmap_create();
316 pmap_destroy(entity_map);
318 entity_map = pmap_create();
320 /* insert internal modes to mode hash. The link field will be cleared
321 in the walk_do_mode() callback function.
322 Other used modes are added by collecting types */
325 ### RG: should be done by inspection the mode of all irn
327 pmap_insert(mode_map, mode_BB, NULL);
328 pmap_insert(mode_map, mode_T, NULL);
329 pmap_insert(mode_map, mode_ANY, NULL);
330 pmap_insert(mode_map, mode_BAD, NULL);
331 pmap_insert(mode_map, mode_X, NULL);
332 pmap_insert(mode_map, mode_M, NULL);
333 pmap_insert(mode_map, mode_b, NULL);
336 // Collect all types (also unused types) if flag is set
337 if (FW_WITH_ALL_TYPES & flags)
338 type_walk(fw_collect_tore, NULL, NULL);
341 for (i = 0; i < get_irp_n_irgs(); i++)
343 ir_graph *irg = get_irp_irg(i);
344 set_irg_link(irg, NULL);
346 type_walk_irg(irg, fw_collect_tore, NULL, NULL);
348 irg_walk_graph(irg, fw_clear_link, fw_collect_irn, NULL);
352 /** This function should call after using the firm walker to free
353 * all collected data and frees the used obstack */
354 void firm_walk_finalize(void)
358 /* free all used maps and lists */
359 pmap_destroy(mode_map);
361 pmap_destroy(type_map);
363 pmap_destroy(entity_map);
366 // free all collected data from ir graphs and nodes
367 for (i = 0; i < get_irp_n_irgs(); i++)
369 ir_graph *irg = get_irp_irg(i);
371 irg_walk_graph(get_irp_irg(i), NULL, fw_free_colleted_data, NULL);
375 obstack_free(&fw_obst, NULL);
378 /** Dumps the firm ir.
380 * After initializing the firm walker by calling firm_walk_init()
381 * the firm structure could be accessed by definign the firm walk interface
382 * wif. This function could be called serveral times to customize the
383 * walk order or definitions.
385 * @param wif Walk interface which contains the callback function
387 * @see firm_walk_interface */
388 void firm_walk(firm_walk_interface *wif)
390 int irg_i, block_i, block_list_len, irn_i, irn_list_len;
394 ir_node *block, **block_list, **irn_list;
396 assert(wif && "firm_walk() in firmwalk.c: No walking interface defined!");
398 /* walk over all modes */
399 if (wif->do_mode_init) wif->do_mode_init(wif->env);
402 for (entry = pmap_first(mode_map); entry; entry = pmap_next(mode_map))
404 set_mode_link((ir_mode*)entry->key, NULL);
405 wif->do_mode(entry->key, wif->env);
408 if (wif->do_mode_finalize) wif->do_mode_finalize(wif->env);
410 /* walk over all types */
411 if (wif->do_type_init) wif->do_type_init(wif->env);
414 for (entry = pmap_first(type_map); entry; entry = pmap_next(type_map))
415 wif->do_type((type *)entry->key, wif->env);
417 if (wif->do_type_finalize) wif->do_type_finalize(wif->env);
419 /* walk over all entities */
420 if (wif->do_entity_init) wif->do_entity_init(wif->env);
423 for (entry = pmap_first(entity_map); entry; entry = pmap_next(entity_map))
424 wif->do_entity((entity *)entry->key, wif->env);
426 if (wif->do_entity_finalize) wif->do_entity_finalize(wif->env);
429 /* Dump graphs ================================================= */
430 if (wif->do_graph_init) wif->do_graph_init(wif->env);
432 for (irg_i = 0; irg_i < get_irp_n_irgs(); irg_i++)
434 irg = get_irp_irg(irg_i);
436 /* walk over all ir graph */
437 if (wif->do_graph) wif->do_graph(irg, wif->env);
439 /* walk over all irg's block nested ========================== */
440 data = fw_get_data(irg);
441 block_list = FW_GET_DATA_LIST(data);
442 block_list_len = ARR_LEN(block_list);
443 for (block_i = 0; block_i < block_list_len; block_i++)
445 if (wif->do_block_init) wif->do_block_init(irg, wif->env);
447 block = (ir_node *)block_list[block_i];
448 if (wif->do_block) wif->do_block(block, wif->env);
450 /* walk over all block's ir nodes nested =================== */
451 data = fw_get_data(block);
452 irn_list = FW_GET_DATA_LIST(data);
453 irn_list_len = ARR_LEN(irn_list);
455 // call block as prefix ir node
456 if ((wif->do_node) &&
457 (wif->flags & FW_DUMP_BLOCK_AS_IRN & !FW_DUMP_IRN_IN_PREFIX))
458 wif->do_node(block, wif->env);
460 // do ir nodes in prefix or postfix order?
461 if (wif->flags & FW_DUMP_IRN_IN_PREFIX)
462 irn_i = irn_list_len-1;
466 while (irn_i >= 0 && irn_i < irn_list_len)
468 if (wif->do_node) wif->do_node((ir_node *)irn_list[irn_i], wif->env);
470 // do ir nodes in prefix or postfix order?
471 if (wif->flags & FW_DUMP_IRN_IN_PREFIX)
476 // call block as postfix ir node
477 if ((wif->do_node) &&
478 (wif->flags & (FW_DUMP_BLOCK_AS_IRN | FW_DUMP_IRN_IN_PREFIX)))
479 wif->do_node(block, wif->env);
481 /* wall over all block's ir nodes nested end =============== */
483 if (wif->do_block_finalize) wif->do_block_finalize(irg, wif->env);
486 /* walk over all irg's block nested end ====================== */
488 } // for each ir graph irg
489 if (wif->do_graph_finalize) wif->do_graph_finalize(wif->env);
491 /** ### ToDo: Dump const_code_irg ?? */