- fixed type_or_ent type: get rod of casts
[libfirm] / ir / common / firmwalk.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @biref    Walker that touches all Firm data structures
23  * @author   Sebastian Felis
24  * @date     7.2003
25  * @version  $Id$
26  * @summary
27  *  Firm walker over intermediate representation.
28  *
29  *  To initialize the walker, call firm_walk_init(). This function
30  *  collects all specific data from the firm represenation. After
31  *  building the walker information firm_walk() could be called
32  *  serveral times with different flags (options) from specific walker
33  *  or dumper. At least firm_walk_finalizer() should be called to free
34  *  the stored data.
35  *
36  *  This walker could be used for a dumper e.g. a vcg or xml dumper.
37  *
38  *  @note If a specific walker or dumper which uses the link field
39  *        of any firm node, the the wrapper functions set_firm_walk_link()
40  *        and get_firm_walk_link() should be used, because the firm walker
41  *        make use of the link field to store its own data.
42  *  @note Deprecated, better use the stuff in irgwalk.h, typewalk.h
43  */
44 #ifndef FIRM_COMMON_FIRM_WALK_H
45 #define FIRM_COMMON_FIRM_WALK_H
46
47 #include "firm_types.h"
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53
54 /** Returns the link of a firm node.
55  *  Possible firm structures are: entity, type, ir_graph, ir_node and
56  *  ir_mode. Otherwise this function has no effect
57  *
58  *  Derived walker or dumper have to call this function to store data
59  *  to a firm structure. The real link field of firm structure is used
60  *  by this firm walker to collect walking data.
61  *
62  *  @param thing Pointer to a firm structure
63  *  @return Link pointer
64  *
65  *  @note After calling firm_walk_finalize() the stored link
66  *        information may be invalid. */
67 void *get_firm_walk_link(void *thing);
68
69 /** Set the link field of a firm structure.
70  *  Possible firm structures are: entity, type, ir_graph, ir_node and
71  *  ir_mode. Otherwise this function has no effect
72  *
73  *  Derived walker or dumper have to call this function to store data
74  *  to a firm structure. The real link field of firm structure is used
75  *  by this firm walker to collect walking data.
76  *
77  *  @param thing firm structure
78  *  @param link Pointer to link field
79  *
80  *  @note After calling firm_walk_finalize() the stored link
81  *        information may be invalid. */
82 void set_firm_walk_link(void *thing, void *link);
83
84 /** Initialisation function for firm walker callbacks */
85 typedef void firm_walk_init_func(void *env);
86 /** Finalisation function for firm walker callbacks */
87 typedef void firm_walk_finalize_func(void *env);
88
89 /** Mode callback function definition */
90 typedef void firm_walk_mode_func(ir_mode *mode, void *env);
91 /** Type callback function definition */
92 typedef void firm_walk_type_func(ir_type *tp, void *env);
93 /** Entity callback function definition */
94 typedef void firm_walk_entity_func(ir_entity *ent, void *env);
95 /** Graph callback function definition */
96 typedef void firm_walk_graph_func(ir_graph *irg, void *env);
97 /* @{ */
98 /** Block callback function definition */
99 typedef void firm_walk_block_init_func(ir_graph *irg, void *env);
100 typedef void firm_walk_block_func(ir_node *block, void *env);
101 typedef void firm_walk_block_finalize_func(ir_graph *irg, void *env);
102 /* @} */
103 /** Node callback function definition */
104 typedef void firm_walk_node_func (ir_node *irn, void *env);
105
106 /** @enum firm_walk_flags
107  *
108  *  Flags for the firm walker to modify some dumping behavior
109  */
110 typedef enum
111 {
112   FW_WITH_ALL_TYPES     = 1<<0, /**< Collect and dump all types, especially
113                                       unused types.
114                                       @note This flag could be set in
115                                       firm_dumper_init() and is unused in
116                                       firm_dump() */
117   FW_WITH_DOMINATOR     = 1<<1, /**< nyi */
118   FW_WITH_OUTEDGES      = 1<<2, /**< nyi */
119   FW_WITH_LOOPS         = 1<<3, /**< nyi */
120   FW_DUMP_BLOCK_AS_IRN  = 1<<4, /**< Dump all block nodes as irn nodes
121                                       additionally */
122   FW_DUMP_IRN_IN_PREFIX = 1<<5  /**< Dumps all ir nodes in prefix order
123                                       according to the internal firm graph
124                                       structure */
125 } firm_walk_flags;
126
127 /** Interface of the firm walker */
128 typedef struct
129 {
130   /* @{ */
131   /** Interface function to dump all used and internal modes.
132       Internal modes are: BB, X, M and T */
133   firm_walk_init_func *do_mode_init;
134   firm_walk_mode_func *do_mode;
135   firm_walk_finalize_func *do_mode_finalize;
136   /* @} */
137
138   /* @{ */
139   /** Interface to dump all collected types.
140    *
141    *  @note To dump all (not only used types by default) a special walk
142    *        flag must be set for the walker initializer */
143   firm_walk_init_func *do_type_init;
144   firm_walk_type_func *do_type;
145   firm_walk_finalize_func *do_type_finalize;
146   /* @} */
147
148   /* @{ */
149   /** Dumping interface for entities */
150   firm_walk_init_func *do_entity_init;
151   firm_walk_entity_func *do_entity;
152   firm_walk_finalize_func *do_entity_finalize;
153   /* @} */
154
155   /** Dumps all graphs and subnodes.
156    *
157    *  The firm walker dump a graph with its blocks and nodes nested.
158    *  Fist do_graph_init will be called (if defined). For each graph
159    *  do_graph will be call in a loop.
160    *  After all blocks in a graph are dumped, do_graph_post() is called.
161    *  After dumped all graphs, do_graph_finalize will be called.
162    *
163    *  Within do_graph each block will be dumped. First do_block_init,
164    *  for each block do_block and after all dumped blocks
165    *  do_block_finalize.
166    *
167    *  The ir nodes are dumped nested in their blocks as well. Within
168    *  do_block, for each ir node do_node is called in postfix order
169    *  according to the internal firm representation. By changing the
170    *  walking flag, a prefix order is also possible. */
171   firm_walk_init_func *do_graph_init;
172   firm_walk_graph_func *do_graph;
173   firm_walk_graph_func *do_graph_post;
174   firm_walk_finalize_func *do_graph_finalize;
175
176   /* @{ */
177   /** Dumping interface for blocks. If blocks should be handled like
178    *  like a normal ir node, a special walker flag could be set.
179    *  @see do_graph */
180   firm_walk_block_init_func *do_block_init;
181   firm_walk_block_func *do_block;
182   firm_walk_block_func *do_block_post;
183   firm_walk_block_finalize_func *do_block_finalize;
184   /* @} */
185
186   /** Dumping interface for ir nodes
187    *  @see do_graph */
188   firm_walk_node_func *do_node;
189   /* dominator */
190   /* procedures */
191   /* loop */
192   firm_walk_flags flags;
193   /* pointer to environment of interface */
194   void *env;
195 } firm_walk_interface;
196
197
198 /** Initialize the dumper und collect all data from the firm intermediate
199  *  representation
200  *
201  *  @param flags flags */
202 void firm_walk_init(firm_walk_flags flags);
203
204 /** Walker of the firm intermediate representation.
205  *
206  *  The callback functions of the interface will be called nested, e.g. for
207  *  each block: init function of block, do_block, nested function of nodes,
208  *  finalize function of block.
209  *
210  *  - modes
211  *  - types
212  *  - entities
213  *  - ir graphs
214  *    - procedures
215  *    - blocks
216  *    - nodes. Options: dominator, outedges
217  *
218  *  @param wif Stucture of walker interface. In this struct the used callback
219  *             functions are defined.
220  */
221 void firm_walk(firm_walk_interface *wif);
222
223 /** Finalize the walker and frees all stored data for dumping */
224 void firm_walk_finalize(void);
225
226 #ifdef __cplusplus
227 }
228 #endif
229
230
231 #endif