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 */
323 pmap_insert(mode_map, mode_BB, NULL);
324 pmap_insert(mode_map, mode_T, NULL);
325 pmap_insert(mode_map, mode_ANY, NULL);
326 pmap_insert(mode_map, mode_BAD, NULL);
327 pmap_insert(mode_map, mode_X, NULL);
328 pmap_insert(mode_map, mode_M, NULL);
329 pmap_insert(mode_map, mode_b, NULL);
331 // Collect all types (also unused types) if flag is set
332 if (FW_WITH_ALL_TYPES & flags)
333 type_walk(fw_collect_tore, NULL, NULL);
336 for (i = 0; i < get_irp_n_irgs(); i++)
338 ir_graph *irg = get_irp_irg(i);
339 set_irg_link(irg, NULL);
341 type_walk_irg(irg, fw_collect_tore, NULL, NULL);
343 irg_walk_graph(irg, fw_clear_link, fw_collect_irn, NULL);
347 /** This function should call after using the firm walker to free
348 * all collected data and frees the used obstack */
349 void firm_walk_finalize(void)
353 /* free all used maps and lists */
354 pmap_destroy(mode_map);
356 pmap_destroy(type_map);
358 pmap_destroy(entity_map);
361 // free all collected data from ir graphs and nodes
362 for (i = 0; i < get_irp_n_irgs(); i++)
364 ir_graph *irg = get_irp_irg(i);
366 irg_walk_graph(get_irp_irg(i), NULL, fw_free_colleted_data, NULL);
370 obstack_free(&fw_obst, NULL);
373 /** Dumps the firm ir.
375 * After initializing the firm walker by calling firm_walk_init()
376 * the firm structure could be accessed by definign the firm walk interface
377 * wif. This function could be called serveral times to customize the
378 * walk order or definitions.
380 * @param wif Walk interface which contains the callback function
382 * @see firm_walk_interface */
383 void firm_walk(firm_walk_interface *wif)
385 int irg_i, block_i, block_list_len, irn_i, irn_list_len;
389 ir_node *block, **block_list, **irn_list;
391 assert(wif && "firm_walk() in firmwalk.c: No walking interface defined!");
393 /* walk over all modes */
394 if (wif->do_mode_init) wif->do_mode_init(wif->env);
397 for (entry = pmap_first(mode_map); entry; entry = pmap_next(mode_map))
399 set_mode_link((ir_mode*)entry->key, NULL);
400 wif->do_mode(entry->key, wif->env);
403 if (wif->do_mode_finalize) wif->do_mode_finalize(wif->env);
405 /* walk over all types */
406 if (wif->do_type_init) wif->do_type_init(wif->env);
409 for (entry = pmap_first(type_map); entry; entry = pmap_next(type_map))
410 wif->do_type((type *)entry->key, wif->env);
412 if (wif->do_type_finalize) wif->do_type_finalize(wif->env);
414 /* walk over all entities */
415 if (wif->do_entity_init) wif->do_entity_init(wif->env);
418 for (entry = pmap_first(entity_map); entry; entry = pmap_next(entity_map))
419 wif->do_entity((entity *)entry->key, wif->env);
421 if (wif->do_entity_finalize) wif->do_entity_finalize(wif->env);
424 /* Dump graphs ================================================= */
425 if (wif->do_graph_init) wif->do_graph_init(wif->env);
427 for (irg_i = 0; irg_i < get_irp_n_irgs(); irg_i++)
429 irg = get_irp_irg(irg_i);
431 /* walk over all ir graph */
432 if (wif->do_graph) wif->do_graph(irg, wif->env);
434 /* walk over all irg's block nested ========================== */
435 data = fw_get_data(irg);
436 block_list = FW_GET_DATA_LIST(data);
437 block_list_len = ARR_LEN(block_list);
438 for (block_i = 0; block_i < block_list_len; block_i++)
440 if (wif->do_block_init) wif->do_block_init(irg, wif->env);
442 block = (ir_node *)block_list[block_i];
443 if (wif->do_block) wif->do_block(block, wif->env);
445 /* walk over all block's ir nodes nested =================== */
446 data = fw_get_data(block);
447 irn_list = FW_GET_DATA_LIST(data);
448 irn_list_len = ARR_LEN(irn_list);
450 // call block as prefix ir node
451 if ((wif->do_node) &&
452 (wif->flags & FW_DUMP_BLOCK_AS_IRN & !FW_DUMP_IRN_IN_PREFIX))
453 wif->do_node(block, wif->env);
455 // do ir nodes in prefix or postfix order?
456 if (wif->flags & FW_DUMP_IRN_IN_PREFIX)
457 irn_i = irn_list_len-1;
461 while (irn_i >= 0 && irn_i < irn_list_len)
463 if (wif->do_node) wif->do_node((ir_node *)irn_list[irn_i], wif->env);
465 // do ir nodes in prefix or postfix order?
466 if (wif->flags & FW_DUMP_IRN_IN_PREFIX)
471 // call block as postfix ir node
472 if ((wif->do_node) &&
473 (wif->flags & (FW_DUMP_BLOCK_AS_IRN | FW_DUMP_IRN_IN_PREFIX)))
474 wif->do_node(block, wif->env);
476 /* wall over all block's ir nodes nested end =============== */
478 if (wif->do_block_finalize) wif->do_block_finalize(irg, wif->env);
481 /* walk over all irg's block nested end ====================== */
483 } // for each ir graph irg
484 if (wif->do_graph_finalize) wif->do_graph_finalize(wif->env);
486 /** ### ToDo: Dump const_code_irg ?? */