pn2adg: use function domain to construct AST in XML output
[ppn.git] / adg.cc
blob3f317c15de863dc742dba36102318800d0858ab6
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 */
5 #include <isa/adg.h>
7 /* Return a C representation of the variable.
8 * The pointer is only valid during the lifetime of the object
9 * or until the next time get_expr() is called.
11 * The pointer is saved in this->expr so that it can be free
12 * when the object is destroyed.
14 __isl_keep const char *adg_var::get_expr()
16 isl_printer *p;
18 if (!access)
19 return NULL;
21 free(expr);
23 p = isl_printer_to_str(isl_pw_multi_aff_get_ctx(access));
24 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
25 p = isl_printer_print_pw_multi_aff(p, access);
26 expr = isl_printer_get_str(p);
27 isl_printer_free(p);
29 return expr;
32 /* Return an adg_expr that corresponds to the affine expression "aff".
33 * If such an adg_expr already exists in controls, then we return that.
34 * Otherwise, we construct a new one with a unique name.
36 adg_expr *adg::to_control(__isl_take isl_aff *aff)
38 int i;
39 adg_expr *expr;
40 isl_pw_aff *pa;
42 pa = isl_pw_aff_from_aff(aff);
44 for (i = 0; i < controls.size(); ++i)
45 if (isl_pw_aff_plain_is_equal(pa, controls[i]->expr))
46 break;
48 if (i < controls.size()) {
49 isl_pw_aff_free(pa);
50 expr = controls[i];
51 } else {
52 char buf[10];
54 expr = new adg_expr;
55 snprintf(buf, sizeof(buf), "e%zd", controls.size());
56 expr->name = isl_id_alloc(isl_pw_aff_get_ctx(pa), buf, NULL);
57 expr->expr = pa;
58 controls.push_back(expr);
61 return new adg_expr(*expr);
64 /* Find and return a pointer to the node that has the give name.
66 adg_node *adg::node_by_id(__isl_take isl_id *id)
68 int i;
70 for (i = 0; i < nodes.size(); ++i)
71 if (nodes[i]->name == id)
72 break;
73 isl_id_free(id);
74 return i < nodes.size() ? nodes[i] : NULL;
77 int adg_var::is_equal(adg_var *var2)
79 if (!isl_pw_multi_aff_plain_is_equal(access, var2->access))
80 return 0;
81 if (type != var2->type)
82 return 0;
83 if (!expr != !var2->expr)
84 return 0;
85 if (expr && strcmp(expr, var2->expr))
86 return 0;
88 return 1;
91 int adg_expr::is_equal(adg_expr *expr2)
93 if (name != expr2->name)
94 return 0;
95 if (!isl_pw_aff_plain_is_equal(expr, expr2->expr))
96 return 0;
98 return 1;
101 int adg_domain::is_equal(adg_domain *domain2)
103 if (!isl_set_is_equal(bounds, domain2->bounds))
104 return 0;
106 if (filters.size() != domain2->filters.size())
107 return 0;
108 for (int i = 0; i < filters.size(); ++i)
109 if (!filters[i]->is_equal(domain2->filters[i]))
110 return 0;
112 return 1;
115 int adg_gate::is_equal(adg_gate *gate2)
117 if (name != gate2->name)
118 return 0;
119 if (node_name != gate2->node_name)
120 return 0;
121 if (!in->is_equal(gate2->in))
122 return 0;
123 if (!out->is_equal(gate2->out))
124 return 0;
125 if (!domain->is_equal(gate2->domain))
126 return 0;
128 return 1;
131 int adg_port::is_equal(adg_port *port2)
133 if (name != port2->name)
134 return 0;
135 if (node_name != port2->node_name)
136 return 0;
137 if (edge_name != port2->edge_name)
138 return 0;
140 if (vars.size() != port2->vars.size())
141 return 0;
142 for (int i = 0; i < vars.size(); ++i)
143 if (!vars[i]->is_equal(port2->vars[i]))
144 return 0;
146 if (!domain->is_equal(port2->domain))
147 return 0;
149 return 1;
152 int adg_function::is_equal(adg_function *function2)
154 if (name != function2->name)
155 return 0;
156 if (!domain->is_equal(function2->domain))
157 return 0;
159 if (in.size() != function2->in.size())
160 return 0;
161 for (int i = 0; i < in.size(); ++i)
162 if (!in[i]->is_equal(function2->in[i]))
163 return 0;
165 if (out.size() != function2->out.size())
166 return 0;
167 for (int i = 0; i < out.size(); ++i)
168 if (!out[i]->is_equal(function2->out[i]))
169 return 0;
171 if (ctrl.size() != function2->ctrl.size())
172 return 0;
173 for (int i = 0; i < ctrl.size(); ++i)
174 if (!ctrl[i]->is_equal(function2->ctrl[i]))
175 return 0;
177 return 1;
180 int adg_node::is_equal(adg_node *node2)
182 if (name != node2->name)
183 return 0;
185 if (input_ports.size() != node2->input_ports.size())
186 return 0;
187 for (int i = 0; i < input_ports.size(); ++i)
188 if (!input_ports[i]->is_equal(node2->input_ports[i]))
189 return 0;
191 if (gates.size() != node2->gates.size())
192 return 0;
193 for (int i = 0; i < gates.size(); ++i)
194 if (!gates[i]->is_equal(node2->gates[i]))
195 return 0;
197 if (output_ports.size() != node2->output_ports.size())
198 return 0;
199 for (int i = 0; i < output_ports.size(); ++i)
200 if (!output_ports[i]->is_equal(node2->output_ports[i]))
201 return 0;
203 if (expressions.size() != node2->expressions.size())
204 return 0;
205 for (int i = 0; i < expressions.size(); ++i)
206 if (!expressions[i]->is_equal(node2->expressions[i]))
207 return 0;
209 if (!function->is_equal(node2->function))
210 return 0;
211 if (!domain->is_equal(node2->domain))
212 return 0;
213 if (!isl_map_is_equal(schedule, node2->schedule))
214 return 0;
216 return 1;
219 int adg_arg::is_equal(adg_arg *arg2)
221 if (type != arg2->type)
222 return 0;
223 if (!var != !arg2->var)
224 return 0;
225 if (var && !var->is_equal(arg2->var))
226 return 0;
228 return 1;
231 int adg_edge::is_equal(adg_edge *edge2)
233 if (name != edge2->name)
234 return 0;
235 if (from_node_name != edge2->from_node_name)
236 return 0;
237 if (from_port_name != edge2->from_port_name)
238 return 0;
239 if (to_node_name != edge2->to_node_name)
240 return 0;
241 if (to_port_name != edge2->to_port_name)
242 return 0;
243 if (type != edge2->type)
244 return 0;
245 if (!isl_multi_aff_plain_is_equal(map, edge2->map))
246 return 0;
247 if (isl_int_ne(value_size, edge2->value_size))
248 return 0;
250 return 1;
253 int adg_param::is_equal(adg_param *param2)
255 if (name != param2->name)
256 return 0;
257 if (!isl_aff_plain_is_equal(lb, param2->lb))
258 return 0;
259 if (!isl_aff_plain_is_equal(ub, param2->ub))
260 return 0;
261 if (!isl_aff_plain_is_equal(val, param2->val))
262 return 0;
264 return 1;
267 int adg::is_equal(adg *adg2)
269 if (name != adg2->name)
270 return 0;
271 if (!isl_set_is_equal(context, adg2->context))
272 return 0;
273 if (!isl_map_is_equal(iterator_map, adg2->iterator_map))
274 return 0;
276 if (params.size() != adg2->params.size())
277 return 0;
278 for (int i = 0; i < params.size(); ++i)
279 if (!params[i]->is_equal(adg2->params[i]))
280 return 0;
282 if (nodes.size() != adg2->nodes.size())
283 return 0;
284 for (int i = 0; i < nodes.size(); ++i)
285 if (!nodes[i]->is_equal(adg2->nodes[i]))
286 return 0;
288 if (edges.size() != adg2->edges.size())
289 return 0;
290 for (int i = 0; i < edges.size(); ++i)
291 if (!edges[i]->is_equal(adg2->edges[i]))
292 return 0;
294 if (all_iterators.size() != adg2->all_iterators.size())
295 return 0;
296 for (int i = 0; i < all_iterators.size(); ++i)
297 if (all_iterators[i] != adg2->all_iterators[i])
298 return 0;
300 if (iterators.size() != adg2->iterators.size())
301 return 0;
302 for (int i = 0; i < iterators.size(); ++i)
303 if (iterators[i] != adg2->iterators[i])
304 return 0;
306 return 1;