moved firmext code into the backend dir
[libfirm] / ir / be / grgen / action.h
1 #ifndef _EXT_GRS_ACTION_H_
2 #define _EXT_GRS_ACTION_H_
3
4 /*
5  * Project:     libFIRM/extension module/graph rewriting system
6  * File name:   ext/grs/action.h
7  * Purpose:     provides an interface for the creation of actions
8  *                              (i.e. rewriting rules and patterns to be found without
9  *              performing a rewriting step)
10  * Author:      Veit Batz
11  * Modified by: Andreas Schoesser
12  * Created:             29. August 2005
13  * CVS-ID:      $Id$
14  * Copyright:   (c) 2005 Universität Karlsruhe
15  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
16  */
17
18
19
20 /** represents a node in a grs action */
21 typedef struct _ext_grs_node_t ext_grs_node_t;
22
23 /** represents an edge in a grs action */
24 typedef struct _ext_grs_edge_t ext_grs_edge_t;
25
26 /** represents a pattern, negative or replacement graph */
27 typedef struct _ext_grs_graph_t ext_grs_graph_t;
28
29 /** An action the grs can perform. Possible kinds of actions are rules
30  * and tests. A rule describes the replacement of pattern graphs by a
31  * replacement graph, a test only describes the lookup for occurences of
32  * the pattern graphs, where it is not intended to perform a replacement */
33 typedef struct _ext_grs_action_t ext_grs_action_t;
34
35
36
37
38 /** the kind of a grs action */
39 typedef enum
40 {
41         ext_grs_k_test, /**< a "test" represents a lookup for a given pattern */
42         ext_grs_k_rule /**< a "rule" represents a graph rewriting step */
43 }
44 ext_grs_action_kind_t;
45
46 /** A condition function to be invoked by the matcher. If the condition
47  *  represented by the function does not hold, the current potential match
48  *  will be rejected by the matcher.
49  *
50  *  @param node_map     the current node map during the matching process,
51  *                                      maps the user defined node ids to host graph nodes
52  *  @param edge_map     the current edge map during the matching process,
53  *                                      maps the user defined edge ids to host graph edges
54  *
55  *  @return Return non zero if the condition holds and zero if it does not.
56  *  @note The functions ext_grs_get_pattern_nodes_image() and
57  *        ext_grs_get_pattern_edges_image() provide the images
58  *        of pattern nodes and edges under the current match. */
59 typedef int (* ext_grs_condition_func_t)
60         (ir_node **node_map, const ir_edge_t **edge_map);
61
62 /** A function ptr to be invoked on inserting new edges during the
63  *  rewrite step. The function pointed to is supposed to compute the
64  *  position of the newly inserted edge.
65  *
66  *  @param edge         the replacement edge, the position of which is computed
67  *  @param node_map The node map of the match, which is used in the
68  *                                      current rewrite.
69  *  @param edge_map     The edge map of the match, which is used in the
70  *                                      current rewrite.
71  *
72  *  @return The position of a newly inserted edge, i.e., a non negative
73  *              integer or ext_grs_NO_EDGE_POS. If the result equals
74  *              ext_grs_NO_EDGE_POS the edge is appended to the exisiting edge
75  *              array.
76  *
77  *  @note The functions ext_grs_get_pattern_nodes_image() and
78  *        ext_grs_get_pattern_edges_image() provide the images
79  *        of pattern nodes and edges under the current match. */
80 typedef int (* ext_grs_edge_pos_func_t)
81         (ext_grs_edge_t *edge, ir_node **node_map, const ir_edge_t **edge_map);
82
83 /**
84  * A condition function to be invoked by the matcher. If the function
85  * representing the condition does not hold for the given op, the matching
86  * of a node with this op is prohibited
87  *
88  * @param op    the op to be checked
89  * @return nonzero if the condition holds, zero otherwise
90  * */
91 typedef int (* ext_grs_op_condition_func_t) (ir_op *op);
92
93 /**
94  * A condition function to be invoked by the matcher. If the function
95  * representing the condition does not hold for the given mode, the matching
96  * of a node with this mode is prohibited
97  *
98  * @param mode  the mode to be checked
99  * @return nonzero if the condition holds, zero otherwise
100  * */
101 typedef int (* ext_grs_mode_condition_func_t) (ir_mode *mode);
102
103 /**
104  * A function representing the in-part of a computation, which
105  * has to be evaluated BEFORE performing a graph rewrite step. Every
106  * such function is associated with a function of type
107  * ext_grs_eval_out_func_t representing the out-part of the computation.
108  *
109  *  @param pat_node_map the node map of the match, the rewriting is performed with.
110  *  @param pat_edge_map the edge map of the match, the rewriting is performed with.
111  *
112  *      @returns        A ptr to some user defined data. The ptr returned will
113  *                              passed as input data to the respective eval-out function.
114  */
115 typedef void* (* ext_grs_eval_in_func_t) (
116         ir_node **pat_node_map, const ir_edge_t **pat_edge_map);
117
118 /**
119  * A function representing the out-part of a computation, which
120  * has to be evaluated AFTER performing a graph rewrite step. Every
121  * such function is associated with a function of type
122  * ext_grs_eval_in_func_t representing the in-part of the computation.
123  *
124  *  @param rpl_node_map         the node map of the match, the rewriting is performed with.
125  *  @param rpl_edge_map         the edge map of the match, the rewriting is performed with.
126  *  @param pat_node_map         the node map of the match, the rewriting is performed with.
127  *  @param data                         the pointer of user defined data returned by the respective
128  *                                                      eval-in function
129  */
130 typedef void (* ext_grs_eval_out_func_t) (
131         ir_node **rpl_node_map, const ir_edge_t **rpl_edge_map,
132         ir_node **pat_node_map, void *data);
133
134
135
136 /** an invalid node or edge index, this is needed for negative
137  *  nodes and edges which are NOT matched */
138 #define ext_grs_EDGE_NODE_INDEX_INVALID -99
139 /** an invalid edge position */
140 #define ext_grs_NO_EDGE_POS -99
141
142
143
144
145
146 /** Create a new graph action of the given kind. */
147 ext_grs_action_t *ext_grs_new_action(ext_grs_action_kind_t kind, char *name);
148
149 /** Get the kind of an action. */
150 ext_grs_action_kind_t ext_grs_act_get_kind(ext_grs_action_t *act);
151
152 /** Impose a new negative graph pattern on the given action.
153  *  @note The action must NOT be mature. */
154 ext_grs_graph_t *ext_grs_act_impose_negative(ext_grs_action_t *action);
155
156 /** Get the pattern graph of an action. */
157 ext_grs_graph_t *ext_grs_act_get_pattern(ext_grs_action_t *action);
158
159 /** Get the replacement graph of a rule action. If the action is not
160  *  of kind rule NULL is returned instead.
161  */
162 ext_grs_graph_t *ext_grs_act_get_replacement(ext_grs_action_t *action);
163
164 /** add a new node to a given pattern graph of an unmature action
165  *  @param graph        the pattern, negative or replacement graph the node is added to
166  *  @param name         the new nodes name
167  *  @param op           the new nodes op
168  *  @param mode         the new nodes mode
169  *  @param id           the id of the new node
170  *
171  *  @returns            the new node
172  *
173  *  @note       the id has to be unique over the hole set of nodes of the pattern
174  *                      and all negative graphs of an action, and unique for the set of nodes
175  *                      of the replacement graph of an action.
176  */
177 ext_grs_node_t *ext_grs_act_add_node(ext_grs_graph_t *graph,
178         char *name, ir_op *op, ir_mode *mode, int id);
179
180 /** Add a new node to a negative pattern and relate it with a positive node */
181 ext_grs_node_t *ext_grs_act_add_related_node(ext_grs_graph_t *graph,
182         char *name, ir_op *op, ir_mode *mode, int id, ext_grs_node_t *positive_node);
183
184 /** Add a new node to the replacement pattern and announce to keep it in the host graph */
185 ext_grs_node_t *ext_grs_act_add_node_to_keep(ext_grs_graph_t *graph,
186                                                                                          char *name, ir_op *op, ir_mode *mode, int id, ext_grs_node_t *pattern_node);
187
188 /** Add a new edge to a given pattern graph of an UNmature action.
189  *  @param graph        the pattern, negative or replacement graph the node is added to
190  *  @param name         the new nodes name
191  *  @param arg          the new edges target node
192  *  @param func         the new edges source node
193  *  @param id           the id of the new edge
194  *
195  *  @returns            the new edge
196  *
197  *  @note       the id has to be unique over the hole set of edges of the pattern
198  *                      and all negative graphs of an action, and unique for the set of edges
199  *                      of the replacement graph of an action.
200  */
201 ext_grs_edge_t *ext_grs_act_add_edge(ext_grs_graph_t *graph,
202         char *name, int pos, ext_grs_node_t *arg, ext_grs_node_t *func, int id);
203
204 /** Add a new edge to a negative pattern and relate it with a positive edge */
205 ext_grs_edge_t *ext_grs_act_add_related_edge(ext_grs_graph_t *graph,
206                                                          char *name, int pos, ext_grs_node_t *arg, ext_grs_node_t *func, int id, ext_grs_edge_t *positive_edge);
207
208 /** Add a new edge to the replacement pattern and announce to keep it in the host graph */
209 ext_grs_edge_t *ext_grs_act_add_edge_to_keep(ext_grs_graph_t *graph,
210                                                                                          char *name, int pos, ext_grs_node_t *arg, ext_grs_node_t *func, int id, ext_grs_edge_t *pattern_edge);
211
212 /** Mature an action. It is not possible to make any
213  *  changes to this actions pattern afterwards (except allowing
214  *  the homomorphic matching of nodes).
215  *  @returns nonzero if succesful */
216 int ext_grs_act_mature(ext_grs_action_t *action);
217
218 /** Get a pattern node by its name. */
219 ext_grs_node_t *ext_grs_act_get_node(ext_grs_action_t *action, char *name);
220
221 /** Get a pattern node aiid by its name. */
222 int ext_grs_act_get_node_aiid(ext_grs_action_t *action, char *name);
223 int ext_grs_act_get_node_aiid_n(ext_grs_action_t *action, char *name, int n);
224
225 /** Get a pattern edge by its name. */
226 ext_grs_edge_t *ext_grs_act_get_edge(ext_grs_action_t *action, char *name);
227
228 /** Allow homomorphic matching of two pattern nodes,
229  *  note that these two nodes MUST have the same op and mode
230  *  (including mode ANY) and belong tp the same acion
231  *  @returns zero if fails and nonzero otherwise
232  *  @note the action the given nodes belong to has to be mature */
233 int ext_grs_act_allow_nodes_hom(ext_grs_node_t *n1, ext_grs_node_t *n2);
234
235 /** Assocites a pattern node and a replacement node of a given rule-action
236  *  to be a node which is kept in the rewrting step.
237  *
238  *  @param e1   the pattern edge
239  *  @param e2   the replacement edge
240  *
241  *  @return zero if an error occured, nonzero otherwise
242  */
243 int ext_grs_act_announce_kept_node(ext_grs_node_t *n1, ext_grs_node_t *n2);
244
245 /** Assocites a pattern edge and a replacement edge of a given rule-action
246  *  to be an edge which is kept in the rewriting step. The two edges must
247  *  have the same position or both no position at all. Otherwise an error
248  *  will be reported.
249  *
250  *  @param e1   the pattern edge
251  *  @param e2   the replacement edge
252  *
253  *  @return zero if an error occurred, nonzero otherwise
254  *
255  *  @note if the replacement contains an edge without a valid position which is
256  *    NOT associated with a pattern edge, an error will occur while maturing the
257  *    respective action
258  */
259 int ext_grs_act_announce_kept_edge(ext_grs_edge_t *e1, ext_grs_edge_t *e2);
260
261 /** relate a negative node with a pattern node
262  *
263  *  @param pat_node     the pattern node
264  *  @param neg_node     the negative node
265  *
266  *  @return zero if an error occured, nonzero otherwise
267  *
268  *  @note       The pattern and the negative node must belong to the same action.
269  *                      Furthermore, the action has to be already mature.
270  */
271 int ext_grs_act_relate_neg_node(ext_grs_node_t *pat_node, ext_grs_node_t *neg_node);
272
273 /** relate a negative edge with a pattern edge
274  *
275  *  @param pat_edge     the pattern node
276  *  @param neg_edge     the negative node
277  *
278  *  @return zero if an error occured, nonzero otherwise
279  *
280  *  @note       The pattern and the negative edge must belong to the same action.
281  *                      Furthermore, the action has to be already mature.
282  *
283  */
284 int ext_grs_act_relate_neg_edge(ext_grs_edge_t *pat_edge, ext_grs_edge_t *neg_edge);
285
286 /** Sets the position function of a replacement edge. After rewriting the
287  *  host edge corresponding to that replace edge gets the position computed
288  *  by the position function.
289  *
290  *  @param edge the replacement edge
291  *  @param func the function computing the edge pos
292  *
293  *  @return zero if an error occured, nonzero otherwise
294  *
295  *  @note       Fails if the given edge is not a replace edge or the
296  *                      the action the given edge belongs to is already mature.
297  */
298 int ext_grs_act_set_pos_func(ext_grs_edge_t *edge, ext_grs_edge_pos_func_t func);
299
300 /** Sets the op condition function of a (pattern or negative) node.
301  *  The function checks wether nodes of certain subops may be matched
302  *  with the given (pattern or negative) node or not
303  *
304  *  @param node a (pattern or negative) node
305  *  @param func the op condition function
306  *
307  *  @return zero if an error occured, nonzero otherwise
308  */
309 int ext_grs_act_set_op_condition(
310                 ext_grs_node_t *node, ext_grs_op_condition_func_t func);
311
312 /** Registers a mode condition function of a (pattern or negative) node.
313  *  The function checks wether nodes of certain modes may be matched
314  *  with the given (pattern or negative) node or not
315  *
316  *  @param node a (pattern or negative) node
317  *  @param func the mode condition function
318  *
319  *  @return zero if an error occured, nonzero otherwise
320  */
321 int ext_grs_act_set_mode_condition_func(
322                 ext_grs_node_t *node, ext_grs_mode_condition_func_t func);
323
324 /** Registers a condition function with some pattern or
325  *  negative nodes of an action.
326  *
327  *  @param func         the function representing the condition
328  *  @param graph        the (pattern or negative) graph the given condition
329  *                                      function is associated with
330  *  @param n_nodes      the number of nodes involved in the condition
331  *  @param nodes    the array of involved nodes
332  *  @param n_edges      the number of edges involved in the condition
333  *  @param nodes    the array of involved edges
334  *
335  *  @return zero if an error occured, nonzero otherwise
336
337  *  @note       The given action must NOT be mature!
338  */
339 int ext_grs_act_register_condition(
340                 ext_grs_condition_func_t func,
341                 ext_grs_graph_t *graph,
342                 int n_nodes, ext_grs_node_t **nodes,
343                 int n_edges, ext_grs_edge_t **edges);
344
345 /** Registers a pair of functions representing a computation,
346  *  which shall be evaluated on a rewrite. The in-part is
347  *  computed BEFORE the transformation of the host graph,
348  *  the out-part is computed AFTER that.
349  *
350  *  @param action       the action the eval-out function is associated with
351  *  @param in_func      the function representing the computations in-part
352  *  @param out_func     the function representing the computations out-part
353  *
354  *  @return zero if an error occurred, nonzero otherwise
355  *
356  *  @note       The given action must NOT be mature!
357  */
358 int ext_grs_act_register_eval(ext_grs_action_t *action,
359                 ext_grs_eval_in_func_t in_func, ext_grs_eval_out_func_t out_func);
360
361
362
363
364 #endif /*_EXT_GRS_ACTION_H_*/