1 #ifndef _EXT_GRS_ACTION_H_
2 #define _EXT_GRS_ACTION_H_
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)
11 * Modified by: Andreas Schoesser
12 * Created: 29. August 2005
14 * Copyright: (c) 2005 Universität Karlsruhe
15 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
20 /** represents a node in a grs action */
21 typedef struct _ext_grs_node_t ext_grs_node_t;
23 /** represents an edge in a grs action */
24 typedef struct _ext_grs_edge_t ext_grs_edge_t;
26 /** represents a pattern, negative or replacement graph */
27 typedef struct _ext_grs_graph_t ext_grs_graph_t;
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;
38 /** the kind of a grs action */
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 */
44 ext_grs_action_kind_t;
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.
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
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);
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.
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
69 * @param edge_map The edge map of the match, which is used in the
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
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);
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
88 * @param op the op to be checked
89 * @return nonzero if the condition holds, zero otherwise
91 typedef int (* ext_grs_op_condition_func_t) (ir_op *op);
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
98 * @param mode the mode to be checked
99 * @return nonzero if the condition holds, zero otherwise
101 typedef int (* ext_grs_mode_condition_func_t) (ir_mode *mode);
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.
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.
112 * @returns A ptr to some user defined data. The ptr returned will
113 * passed as input data to the respective eval-out function.
115 typedef void* (* ext_grs_eval_in_func_t) (
116 ir_node **pat_node_map, const ir_edge_t **pat_edge_map);
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.
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
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);
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
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);
149 /** Get the kind of an action. */
150 ext_grs_action_kind_t ext_grs_act_get_kind(ext_grs_action_t *act);
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);
156 /** Get the pattern graph of an action. */
157 ext_grs_graph_t *ext_grs_act_get_pattern(ext_grs_action_t *action);
159 /** Get the replacement graph of a rule action. If the action is not
160 * of kind rule NULL is returned instead.
162 ext_grs_graph_t *ext_grs_act_get_replacement(ext_grs_action_t *action);
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
171 * @returns the new node
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.
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);
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);
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);
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
195 * @returns the new edge
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.
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);
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);
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);
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);
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);
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);
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);
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);
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.
238 * @param e1 the pattern edge
239 * @param e2 the replacement edge
241 * @return zero if an error occured, nonzero otherwise
243 int ext_grs_act_announce_kept_node(ext_grs_node_t *n1, ext_grs_node_t *n2);
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
250 * @param e1 the pattern edge
251 * @param e2 the replacement edge
253 * @return zero if an error occurred, nonzero otherwise
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
259 int ext_grs_act_announce_kept_edge(ext_grs_edge_t *e1, ext_grs_edge_t *e2);
261 /** relate a negative node with a pattern node
263 * @param pat_node the pattern node
264 * @param neg_node the negative node
266 * @return zero if an error occured, nonzero otherwise
268 * @note The pattern and the negative node must belong to the same action.
269 * Furthermore, the action has to be already mature.
271 int ext_grs_act_relate_neg_node(ext_grs_node_t *pat_node, ext_grs_node_t *neg_node);
273 /** relate a negative edge with a pattern edge
275 * @param pat_edge the pattern node
276 * @param neg_edge the negative node
278 * @return zero if an error occured, nonzero otherwise
280 * @note The pattern and the negative edge must belong to the same action.
281 * Furthermore, the action has to be already mature.
284 int ext_grs_act_relate_neg_edge(ext_grs_edge_t *pat_edge, ext_grs_edge_t *neg_edge);
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.
290 * @param edge the replacement edge
291 * @param func the function computing the edge pos
293 * @return zero if an error occured, nonzero otherwise
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.
298 int ext_grs_act_set_pos_func(ext_grs_edge_t *edge, ext_grs_edge_pos_func_t func);
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
304 * @param node a (pattern or negative) node
305 * @param func the op condition function
307 * @return zero if an error occured, nonzero otherwise
309 int ext_grs_act_set_op_condition(
310 ext_grs_node_t *node, ext_grs_op_condition_func_t func);
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
316 * @param node a (pattern or negative) node
317 * @param func the mode condition function
319 * @return zero if an error occured, nonzero otherwise
321 int ext_grs_act_set_mode_condition_func(
322 ext_grs_node_t *node, ext_grs_mode_condition_func_t func);
324 /** Registers a condition function with some pattern or
325 * negative nodes of an action.
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
335 * @return zero if an error occured, nonzero otherwise
337 * @note The given action must NOT be mature!
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);
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.
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
354 * @return zero if an error occurred, nonzero otherwise
356 * @note The given action must NOT be mature!
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);
364 #endif /*_EXT_GRS_ACTION_H_*/