/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/External/isl/isl_ast.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2012-2013 Ecole Normale Superieure |
3 | | * |
4 | | * Use of this software is governed by the MIT license |
5 | | * |
6 | | * Written by Sven Verdoolaege, |
7 | | * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France |
8 | | */ |
9 | | |
10 | | #include <string.h> |
11 | | |
12 | | #include <isl/id.h> |
13 | | #include <isl/val.h> |
14 | | #include <isl_ast_private.h> |
15 | | |
16 | | #undef BASE |
17 | | #define BASE ast_expr |
18 | | |
19 | | #include <isl_list_templ.c> |
20 | | |
21 | | #undef BASE |
22 | | #define BASE ast_node |
23 | | |
24 | | #include <isl_list_templ.c> |
25 | | |
26 | | isl_ctx *isl_ast_print_options_get_ctx( |
27 | | __isl_keep isl_ast_print_options *options) |
28 | 0 | { |
29 | 0 | return options ? options->ctx : NULL; |
30 | 0 | } |
31 | | |
32 | | __isl_give isl_ast_print_options *isl_ast_print_options_alloc(isl_ctx *ctx) |
33 | 154 | { |
34 | 154 | isl_ast_print_options *options; |
35 | 154 | |
36 | 154 | options = isl_calloc_type(ctx, isl_ast_print_options); |
37 | 154 | if (!options) |
38 | 0 | return NULL; |
39 | 154 | |
40 | 154 | options->ctx = ctx; |
41 | 154 | isl_ctx_ref(ctx); |
42 | 154 | options->ref = 1; |
43 | 154 | |
44 | 154 | return options; |
45 | 154 | } |
46 | | |
47 | | __isl_give isl_ast_print_options *isl_ast_print_options_dup( |
48 | | __isl_keep isl_ast_print_options *options) |
49 | 0 | { |
50 | 0 | isl_ctx *ctx; |
51 | 0 | isl_ast_print_options *dup; |
52 | 0 |
|
53 | 0 | if (!options) |
54 | 0 | return NULL; |
55 | 0 | |
56 | 0 | ctx = isl_ast_print_options_get_ctx(options); |
57 | 0 | dup = isl_ast_print_options_alloc(ctx); |
58 | 0 | if (!dup) |
59 | 0 | return NULL; |
60 | 0 | |
61 | 0 | dup->print_for = options->print_for; |
62 | 0 | dup->print_for_user = options->print_for_user; |
63 | 0 | dup->print_user = options->print_user; |
64 | 0 | dup->print_user_user = options->print_user_user; |
65 | 0 |
|
66 | 0 | return dup; |
67 | 0 | } |
68 | | |
69 | | __isl_give isl_ast_print_options *isl_ast_print_options_cow( |
70 | | __isl_take isl_ast_print_options *options) |
71 | 156 | { |
72 | 156 | if (!options) |
73 | 0 | return NULL; |
74 | 156 | |
75 | 156 | if (options->ref == 1) |
76 | 156 | return options; |
77 | 0 | options->ref--; |
78 | 0 | return isl_ast_print_options_dup(options); |
79 | 0 | } |
80 | | |
81 | | __isl_give isl_ast_print_options *isl_ast_print_options_copy( |
82 | | __isl_keep isl_ast_print_options *options) |
83 | 781 | { |
84 | 781 | if (!options) |
85 | 0 | return NULL; |
86 | 781 | |
87 | 781 | options->ref++; |
88 | 781 | return options; |
89 | 781 | } |
90 | | |
91 | | __isl_null isl_ast_print_options *isl_ast_print_options_free( |
92 | | __isl_take isl_ast_print_options *options) |
93 | 935 | { |
94 | 935 | if (!options) |
95 | 0 | return NULL; |
96 | 935 | |
97 | 935 | if (--options->ref > 0) |
98 | 781 | return NULL; |
99 | 154 | |
100 | 154 | isl_ctx_deref(options->ctx); |
101 | 154 | |
102 | 154 | free(options); |
103 | 154 | return NULL; |
104 | 154 | } |
105 | | |
106 | | /* Set the print_user callback of "options" to "print_user". |
107 | | * |
108 | | * If this callback is set, then it used to print user nodes in the AST. |
109 | | * Otherwise, the expression associated to the user node is printed. |
110 | | */ |
111 | | __isl_give isl_ast_print_options *isl_ast_print_options_set_print_user( |
112 | | __isl_take isl_ast_print_options *options, |
113 | | __isl_give isl_printer *(*print_user)(__isl_take isl_printer *p, |
114 | | __isl_take isl_ast_print_options *options, |
115 | | __isl_keep isl_ast_node *node, void *user), |
116 | | void *user) |
117 | 2 | { |
118 | 2 | options = isl_ast_print_options_cow(options); |
119 | 2 | if (!options) |
120 | 0 | return NULL; |
121 | 2 | |
122 | 2 | options->print_user = print_user; |
123 | 2 | options->print_user_user = user; |
124 | 2 | |
125 | 2 | return options; |
126 | 2 | } |
127 | | |
128 | | /* Set the print_for callback of "options" to "print_for". |
129 | | * |
130 | | * If this callback is set, then it used to print for nodes in the AST. |
131 | | */ |
132 | | __isl_give isl_ast_print_options *isl_ast_print_options_set_print_for( |
133 | | __isl_take isl_ast_print_options *options, |
134 | | __isl_give isl_printer *(*print_for)(__isl_take isl_printer *p, |
135 | | __isl_take isl_ast_print_options *options, |
136 | | __isl_keep isl_ast_node *node, void *user), |
137 | | void *user) |
138 | 154 | { |
139 | 154 | options = isl_ast_print_options_cow(options); |
140 | 154 | if (!options) |
141 | 0 | return NULL; |
142 | 154 | |
143 | 154 | options->print_for = print_for; |
144 | 154 | options->print_for_user = user; |
145 | 154 | |
146 | 154 | return options; |
147 | 154 | } |
148 | | |
149 | | __isl_give isl_ast_expr *isl_ast_expr_copy(__isl_keep isl_ast_expr *expr) |
150 | 19.9k | { |
151 | 19.9k | if (!expr) |
152 | 0 | return NULL; |
153 | 19.9k | |
154 | 19.9k | expr->ref++; |
155 | 19.9k | return expr; |
156 | 19.9k | } |
157 | | |
158 | | __isl_give isl_ast_expr *isl_ast_expr_dup(__isl_keep isl_ast_expr *expr) |
159 | 0 | { |
160 | 0 | int i; |
161 | 0 | isl_ctx *ctx; |
162 | 0 | isl_ast_expr *dup; |
163 | 0 |
|
164 | 0 | if (!expr) |
165 | 0 | return NULL; |
166 | 0 | |
167 | 0 | ctx = isl_ast_expr_get_ctx(expr); |
168 | 0 | switch (expr->type) { |
169 | 0 | case isl_ast_expr_int: |
170 | 0 | dup = isl_ast_expr_from_val(isl_val_copy(expr->u.v)); |
171 | 0 | break; |
172 | 0 | case isl_ast_expr_id: |
173 | 0 | dup = isl_ast_expr_from_id(isl_id_copy(expr->u.id)); |
174 | 0 | break; |
175 | 0 | case isl_ast_expr_op: |
176 | 0 | dup = isl_ast_expr_alloc_op(ctx, |
177 | 0 | expr->u.op.op, expr->u.op.n_arg); |
178 | 0 | if (!dup) |
179 | 0 | return NULL; |
180 | 0 | for (i = 0; i < expr->u.op.n_arg; ++i) |
181 | 0 | dup->u.op.args[i] = |
182 | 0 | isl_ast_expr_copy(expr->u.op.args[i]); |
183 | 0 | break; |
184 | 0 | case isl_ast_expr_error: |
185 | 0 | dup = NULL; |
186 | 0 | } |
187 | 0 |
|
188 | 0 | if (!dup) |
189 | 0 | return NULL; |
190 | 0 | |
191 | 0 | return dup; |
192 | 0 | } |
193 | | |
194 | | __isl_give isl_ast_expr *isl_ast_expr_cow(__isl_take isl_ast_expr *expr) |
195 | 9.96k | { |
196 | 9.96k | if (!expr) |
197 | 0 | return NULL; |
198 | 9.96k | |
199 | 9.96k | if (expr->ref == 1) |
200 | 9.96k | return expr; |
201 | 0 | expr->ref--; |
202 | 0 | return isl_ast_expr_dup(expr); |
203 | 0 | } |
204 | | |
205 | | __isl_null isl_ast_expr *isl_ast_expr_free(__isl_take isl_ast_expr *expr) |
206 | 91.3k | { |
207 | 91.3k | int i; |
208 | 91.3k | |
209 | 91.3k | if (!expr) |
210 | 9.96k | return NULL; |
211 | 81.3k | |
212 | 81.3k | if (--expr->ref > 0) |
213 | 19.9k | return NULL; |
214 | 61.3k | |
215 | 61.3k | isl_ctx_deref(expr->ctx); |
216 | 61.3k | |
217 | 61.3k | switch (expr->type) { |
218 | 61.3k | case isl_ast_expr_int: |
219 | 29.9k | isl_val_free(expr->u.v); |
220 | 29.9k | break; |
221 | 61.3k | case isl_ast_expr_id: |
222 | 13.2k | isl_id_free(expr->u.id); |
223 | 13.2k | break; |
224 | 61.3k | case isl_ast_expr_op: |
225 | 18.1k | if (expr->u.op.args) |
226 | 56.7k | for (i = 0; 18.1k i < expr->u.op.n_arg; ++i38.6k ) |
227 | 38.6k | isl_ast_expr_free(expr->u.op.args[i]); |
228 | 18.1k | free(expr->u.op.args); |
229 | 18.1k | break; |
230 | 61.3k | case isl_ast_expr_error: |
231 | 0 | break; |
232 | 61.3k | } |
233 | 61.3k | |
234 | 61.3k | free(expr); |
235 | 61.3k | return NULL; |
236 | 61.3k | } |
237 | | |
238 | | isl_ctx *isl_ast_expr_get_ctx(__isl_keep isl_ast_expr *expr) |
239 | 17.2k | { |
240 | 17.2k | return expr ? expr->ctx : NULL; |
241 | 17.2k | } |
242 | | |
243 | | enum isl_ast_expr_type isl_ast_expr_get_type(__isl_keep isl_ast_expr *expr) |
244 | 13.2k | { |
245 | 13.2k | return expr ? expr->type : isl_ast_expr_error0 ; |
246 | 13.2k | } |
247 | | |
248 | | /* Return the integer value represented by "expr". |
249 | | */ |
250 | | __isl_give isl_val *isl_ast_expr_get_val(__isl_keep isl_ast_expr *expr) |
251 | 3.93k | { |
252 | 3.93k | if (!expr) |
253 | 0 | return NULL; |
254 | 3.93k | if (expr->type != isl_ast_expr_int) |
255 | 3.93k | isl_die0 (isl_ast_expr_get_ctx(expr), isl_error_invalid, |
256 | 3.93k | "expression not an int", return NULL); |
257 | 3.93k | return isl_val_copy(expr->u.v); |
258 | 3.93k | } |
259 | | |
260 | | __isl_give isl_id *isl_ast_expr_get_id(__isl_keep isl_ast_expr *expr) |
261 | 4.51k | { |
262 | 4.51k | if (!expr) |
263 | 0 | return NULL; |
264 | 4.51k | if (expr->type != isl_ast_expr_id) |
265 | 4.51k | isl_die0 (isl_ast_expr_get_ctx(expr), isl_error_invalid, |
266 | 4.51k | "expression not an identifier", return NULL); |
267 | 4.51k | |
268 | 4.51k | return isl_id_copy(expr->u.id); |
269 | 4.51k | } |
270 | | |
271 | | enum isl_ast_op_type isl_ast_expr_get_op_type(__isl_keep isl_ast_expr *expr) |
272 | 6.42k | { |
273 | 6.42k | if (!expr) |
274 | 0 | return isl_ast_op_error; |
275 | 6.42k | if (expr->type != isl_ast_expr_op) |
276 | 6.42k | isl_die0 (isl_ast_expr_get_ctx(expr), isl_error_invalid, |
277 | 6.42k | "expression not an operation", return isl_ast_op_error); |
278 | 6.42k | return expr->u.op.op; |
279 | 6.42k | } |
280 | | |
281 | | int isl_ast_expr_get_op_n_arg(__isl_keep isl_ast_expr *expr) |
282 | 4.60k | { |
283 | 4.60k | if (!expr) |
284 | 0 | return -1; |
285 | 4.60k | if (expr->type != isl_ast_expr_op) |
286 | 4.60k | isl_die0 (isl_ast_expr_get_ctx(expr), isl_error_invalid, |
287 | 4.60k | "expression not an operation", return -1); |
288 | 4.60k | return expr->u.op.n_arg; |
289 | 4.60k | } |
290 | | |
291 | | __isl_give isl_ast_expr *isl_ast_expr_get_op_arg(__isl_keep isl_ast_expr *expr, |
292 | | int pos) |
293 | 11.2k | { |
294 | 11.2k | if (!expr) |
295 | 0 | return NULL; |
296 | 11.2k | if (expr->type != isl_ast_expr_op) |
297 | 11.2k | isl_die0 (isl_ast_expr_get_ctx(expr), isl_error_invalid, |
298 | 11.2k | "expression not an operation", return NULL); |
299 | 11.2k | if (pos < 0 || pos >= expr->u.op.n_arg) |
300 | 11.2k | isl_die0 (isl_ast_expr_get_ctx(expr), isl_error_invalid, |
301 | 11.2k | "index out of bounds", return NULL); |
302 | 11.2k | |
303 | 11.2k | return isl_ast_expr_copy(expr->u.op.args[pos]); |
304 | 11.2k | } |
305 | | |
306 | | /* Replace the argument at position "pos" of "expr" by "arg". |
307 | | */ |
308 | | __isl_give isl_ast_expr *isl_ast_expr_set_op_arg(__isl_take isl_ast_expr *expr, |
309 | | int pos, __isl_take isl_ast_expr *arg) |
310 | 9.96k | { |
311 | 9.96k | expr = isl_ast_expr_cow(expr); |
312 | 9.96k | if (!expr || !arg) |
313 | 0 | goto error; |
314 | 9.96k | if (expr->type != isl_ast_expr_op) |
315 | 9.96k | isl_die0 (isl_ast_expr_get_ctx(expr), isl_error_invalid, |
316 | 9.96k | "expression not an operation", goto error); |
317 | 9.96k | if (pos < 0 || pos >= expr->u.op.n_arg) |
318 | 9.96k | isl_die0 (isl_ast_expr_get_ctx(expr), isl_error_invalid, |
319 | 9.96k | "index out of bounds", goto error); |
320 | 9.96k | |
321 | 9.96k | isl_ast_expr_free(expr->u.op.args[pos]); |
322 | 9.96k | expr->u.op.args[pos] = arg; |
323 | 9.96k | |
324 | 9.96k | return expr; |
325 | 0 | error: |
326 | 0 | isl_ast_expr_free(arg); |
327 | 0 | return isl_ast_expr_free(expr); |
328 | 9.96k | } |
329 | | |
330 | | /* Is "expr1" equal to "expr2"? |
331 | | */ |
332 | | isl_bool isl_ast_expr_is_equal(__isl_keep isl_ast_expr *expr1, |
333 | | __isl_keep isl_ast_expr *expr2) |
334 | 2 | { |
335 | 2 | int i; |
336 | 2 | |
337 | 2 | if (!expr1 || !expr2) |
338 | 0 | return isl_bool_error; |
339 | 2 | |
340 | 2 | if (expr1 == expr2) |
341 | 1 | return isl_bool_true; |
342 | 1 | if (expr1->type != expr2->type) |
343 | 0 | return isl_bool_false; |
344 | 1 | switch (expr1->type) { |
345 | 1 | case isl_ast_expr_int: |
346 | 0 | return isl_val_eq(expr1->u.v, expr2->u.v); |
347 | 1 | case isl_ast_expr_id: |
348 | 0 | return expr1->u.id == expr2->u.id; |
349 | 1 | case isl_ast_expr_op: |
350 | 1 | if (expr1->u.op.op != expr2->u.op.op) |
351 | 0 | return isl_bool_false; |
352 | 1 | if (expr1->u.op.n_arg != expr2->u.op.n_arg) |
353 | 0 | return isl_bool_false; |
354 | 2 | for (i = 0; 1 i < expr1->u.op.n_arg; ++i1 ) { |
355 | 1 | isl_bool equal; |
356 | 1 | equal = isl_ast_expr_is_equal(expr1->u.op.args[i], |
357 | 1 | expr2->u.op.args[i]); |
358 | 1 | if (equal < 0 || !equal) |
359 | 0 | return equal; |
360 | 1 | } |
361 | 1 | return isl_bool_true; |
362 | 1 | case isl_ast_expr_error: |
363 | 0 | return isl_bool_error; |
364 | 0 | } |
365 | 0 | |
366 | 0 | isl_die(isl_ast_expr_get_ctx(expr1), isl_error_internal, |
367 | 0 | "unhandled case", return isl_bool_error); |
368 | 0 | } |
369 | | |
370 | | /* Create a new operation expression of operation type "op", |
371 | | * with "n_arg" as yet unspecified arguments. |
372 | | */ |
373 | | __isl_give isl_ast_expr *isl_ast_expr_alloc_op(isl_ctx *ctx, |
374 | | enum isl_ast_op_type op, int n_arg) |
375 | 18.1k | { |
376 | 18.1k | isl_ast_expr *expr; |
377 | 18.1k | |
378 | 18.1k | expr = isl_calloc_type(ctx, isl_ast_expr); |
379 | 18.1k | if (!expr) |
380 | 0 | return NULL; |
381 | 18.1k | |
382 | 18.1k | expr->ctx = ctx; |
383 | 18.1k | isl_ctx_ref(ctx); |
384 | 18.1k | expr->ref = 1; |
385 | 18.1k | expr->type = isl_ast_expr_op; |
386 | 18.1k | expr->u.op.op = op; |
387 | 18.1k | expr->u.op.n_arg = n_arg; |
388 | 18.1k | expr->u.op.args = isl_calloc_array(ctx, isl_ast_expr *, n_arg); |
389 | 18.1k | |
390 | 18.1k | if (n_arg && !expr->u.op.args) |
391 | 0 | return isl_ast_expr_free(expr); |
392 | 18.1k | |
393 | 18.1k | return expr; |
394 | 18.1k | } |
395 | | |
396 | | /* Create a new id expression representing "id". |
397 | | */ |
398 | | __isl_give isl_ast_expr *isl_ast_expr_from_id(__isl_take isl_id *id) |
399 | 13.2k | { |
400 | 13.2k | isl_ctx *ctx; |
401 | 13.2k | isl_ast_expr *expr; |
402 | 13.2k | |
403 | 13.2k | if (!id) |
404 | 0 | return NULL; |
405 | 13.2k | |
406 | 13.2k | ctx = isl_id_get_ctx(id); |
407 | 13.2k | expr = isl_calloc_type(ctx, isl_ast_expr); |
408 | 13.2k | if (!expr) |
409 | 0 | goto error; |
410 | 13.2k | |
411 | 13.2k | expr->ctx = ctx; |
412 | 13.2k | isl_ctx_ref(ctx); |
413 | 13.2k | expr->ref = 1; |
414 | 13.2k | expr->type = isl_ast_expr_id; |
415 | 13.2k | expr->u.id = id; |
416 | 13.2k | |
417 | 13.2k | return expr; |
418 | 0 | error: |
419 | 0 | isl_id_free(id); |
420 | 0 | return NULL; |
421 | 13.2k | } |
422 | | |
423 | | /* Create a new integer expression representing "i". |
424 | | */ |
425 | | __isl_give isl_ast_expr *isl_ast_expr_alloc_int_si(isl_ctx *ctx, int i) |
426 | 19.4k | { |
427 | 19.4k | isl_ast_expr *expr; |
428 | 19.4k | |
429 | 19.4k | expr = isl_calloc_type(ctx, isl_ast_expr); |
430 | 19.4k | if (!expr) |
431 | 0 | return NULL; |
432 | 19.4k | |
433 | 19.4k | expr->ctx = ctx; |
434 | 19.4k | isl_ctx_ref(ctx); |
435 | 19.4k | expr->ref = 1; |
436 | 19.4k | expr->type = isl_ast_expr_int; |
437 | 19.4k | expr->u.v = isl_val_int_from_si(ctx, i); |
438 | 19.4k | if (!expr->u.v) |
439 | 0 | return isl_ast_expr_free(expr); |
440 | 19.4k | |
441 | 19.4k | return expr; |
442 | 19.4k | } |
443 | | |
444 | | /* Create a new integer expression representing "v". |
445 | | */ |
446 | | __isl_give isl_ast_expr *isl_ast_expr_from_val(__isl_take isl_val *v) |
447 | 10.4k | { |
448 | 10.4k | isl_ctx *ctx; |
449 | 10.4k | isl_ast_expr *expr; |
450 | 10.4k | |
451 | 10.4k | if (!v) |
452 | 0 | return NULL; |
453 | 10.4k | if (!isl_val_is_int(v)) |
454 | 10.4k | isl_die0 (isl_val_get_ctx(v), isl_error_invalid, |
455 | 10.4k | "expecting integer value", goto error); |
456 | 10.4k | |
457 | 10.4k | ctx = isl_val_get_ctx(v); |
458 | 10.4k | expr = isl_calloc_type(ctx, isl_ast_expr); |
459 | 10.4k | if (!expr) |
460 | 0 | goto error; |
461 | 10.4k | |
462 | 10.4k | expr->ctx = ctx; |
463 | 10.4k | isl_ctx_ref(ctx); |
464 | 10.4k | expr->ref = 1; |
465 | 10.4k | expr->type = isl_ast_expr_int; |
466 | 10.4k | expr->u.v = v; |
467 | 10.4k | |
468 | 10.4k | return expr; |
469 | 0 | error: |
470 | 0 | isl_val_free(v); |
471 | 0 | return NULL; |
472 | 10.4k | } |
473 | | |
474 | | /* Create an expression representing the unary operation "type" applied to |
475 | | * "arg". |
476 | | */ |
477 | | __isl_give isl_ast_expr *isl_ast_expr_alloc_unary(enum isl_ast_op_type type, |
478 | | __isl_take isl_ast_expr *arg) |
479 | 1.11k | { |
480 | 1.11k | isl_ctx *ctx; |
481 | 1.11k | isl_ast_expr *expr = NULL; |
482 | 1.11k | |
483 | 1.11k | if (!arg) |
484 | 0 | return NULL; |
485 | 1.11k | |
486 | 1.11k | ctx = isl_ast_expr_get_ctx(arg); |
487 | 1.11k | expr = isl_ast_expr_alloc_op(ctx, type, 1); |
488 | 1.11k | if (!expr) |
489 | 0 | goto error; |
490 | 1.11k | |
491 | 1.11k | expr->u.op.args[0] = arg; |
492 | 1.11k | |
493 | 1.11k | return expr; |
494 | 0 | error: |
495 | 0 | isl_ast_expr_free(arg); |
496 | 0 | return NULL; |
497 | 1.11k | } |
498 | | |
499 | | /* Create an expression representing the negation of "arg". |
500 | | */ |
501 | | __isl_give isl_ast_expr *isl_ast_expr_neg(__isl_take isl_ast_expr *arg) |
502 | 83 | { |
503 | 83 | return isl_ast_expr_alloc_unary(isl_ast_op_minus, arg); |
504 | 83 | } |
505 | | |
506 | | /* Create an expression representing the address of "expr". |
507 | | */ |
508 | | __isl_give isl_ast_expr *isl_ast_expr_address_of(__isl_take isl_ast_expr *expr) |
509 | 1.02k | { |
510 | 1.02k | if (!expr) |
511 | 0 | return NULL; |
512 | 1.02k | |
513 | 1.02k | if (isl_ast_expr_get_type(expr) != isl_ast_expr_op || |
514 | 1.02k | isl_ast_expr_get_op_type(expr) != isl_ast_op_access) |
515 | 1.02k | isl_die0 (isl_ast_expr_get_ctx(expr), isl_error_invalid, |
516 | 1.02k | "can only take address of access expressions", |
517 | 1.02k | return isl_ast_expr_free(expr)); |
518 | 1.02k | |
519 | 1.02k | return isl_ast_expr_alloc_unary(isl_ast_op_address_of, expr); |
520 | 1.02k | } |
521 | | |
522 | | /* Create an expression representing the binary operation "type" |
523 | | * applied to "expr1" and "expr2". |
524 | | */ |
525 | | __isl_give isl_ast_expr *isl_ast_expr_alloc_binary(enum isl_ast_op_type type, |
526 | | __isl_take isl_ast_expr *expr1, __isl_take isl_ast_expr *expr2) |
527 | 13.6k | { |
528 | 13.6k | isl_ctx *ctx; |
529 | 13.6k | isl_ast_expr *expr = NULL; |
530 | 13.6k | |
531 | 13.6k | if (!expr1 || !expr2) |
532 | 0 | goto error; |
533 | 13.6k | |
534 | 13.6k | ctx = isl_ast_expr_get_ctx(expr1); |
535 | 13.6k | expr = isl_ast_expr_alloc_op(ctx, type, 2); |
536 | 13.6k | if (!expr) |
537 | 0 | goto error; |
538 | 13.6k | |
539 | 13.6k | expr->u.op.args[0] = expr1; |
540 | 13.6k | expr->u.op.args[1] = expr2; |
541 | 13.6k | |
542 | 13.6k | return expr; |
543 | 0 | error: |
544 | 0 | isl_ast_expr_free(expr1); |
545 | 0 | isl_ast_expr_free(expr2); |
546 | 0 | return NULL; |
547 | 13.6k | } |
548 | | |
549 | | /* Create an expression representing the sum of "expr1" and "expr2". |
550 | | */ |
551 | | __isl_give isl_ast_expr *isl_ast_expr_add(__isl_take isl_ast_expr *expr1, |
552 | | __isl_take isl_ast_expr *expr2) |
553 | 5.33k | { |
554 | 5.33k | return isl_ast_expr_alloc_binary(isl_ast_op_add, expr1, expr2); |
555 | 5.33k | } |
556 | | |
557 | | /* Create an expression representing the difference of "expr1" and "expr2". |
558 | | */ |
559 | | __isl_give isl_ast_expr *isl_ast_expr_sub(__isl_take isl_ast_expr *expr1, |
560 | | __isl_take isl_ast_expr *expr2) |
561 | 238 | { |
562 | 238 | return isl_ast_expr_alloc_binary(isl_ast_op_sub, expr1, expr2); |
563 | 238 | } |
564 | | |
565 | | /* Create an expression representing the product of "expr1" and "expr2". |
566 | | */ |
567 | | __isl_give isl_ast_expr *isl_ast_expr_mul(__isl_take isl_ast_expr *expr1, |
568 | | __isl_take isl_ast_expr *expr2) |
569 | 5.43k | { |
570 | 5.43k | return isl_ast_expr_alloc_binary(isl_ast_op_mul, expr1, expr2); |
571 | 5.43k | } |
572 | | |
573 | | /* Create an expression representing the quotient of "expr1" and "expr2". |
574 | | */ |
575 | | __isl_give isl_ast_expr *isl_ast_expr_div(__isl_take isl_ast_expr *expr1, |
576 | | __isl_take isl_ast_expr *expr2) |
577 | 7 | { |
578 | 7 | return isl_ast_expr_alloc_binary(isl_ast_op_div, expr1, expr2); |
579 | 7 | } |
580 | | |
581 | | /* Create an expression representing the quotient of the integer |
582 | | * division of "expr1" by "expr2", where "expr1" is known to be |
583 | | * non-negative. |
584 | | */ |
585 | | __isl_give isl_ast_expr *isl_ast_expr_pdiv_q(__isl_take isl_ast_expr *expr1, |
586 | | __isl_take isl_ast_expr *expr2) |
587 | 0 | { |
588 | 0 | return isl_ast_expr_alloc_binary(isl_ast_op_pdiv_q, expr1, expr2); |
589 | 0 | } |
590 | | |
591 | | /* Create an expression representing the remainder of the integer |
592 | | * division of "expr1" by "expr2", where "expr1" is known to be |
593 | | * non-negative. |
594 | | */ |
595 | | __isl_give isl_ast_expr *isl_ast_expr_pdiv_r(__isl_take isl_ast_expr *expr1, |
596 | | __isl_take isl_ast_expr *expr2) |
597 | 0 | { |
598 | 0 | return isl_ast_expr_alloc_binary(isl_ast_op_pdiv_r, expr1, expr2); |
599 | 0 | } |
600 | | |
601 | | /* Create an expression representing the conjunction of "expr1" and "expr2". |
602 | | */ |
603 | | __isl_give isl_ast_expr *isl_ast_expr_and(__isl_take isl_ast_expr *expr1, |
604 | | __isl_take isl_ast_expr *expr2) |
605 | 392 | { |
606 | 392 | return isl_ast_expr_alloc_binary(isl_ast_op_and, expr1, expr2); |
607 | 392 | } |
608 | | |
609 | | /* Create an expression representing the conjunction of "expr1" and "expr2", |
610 | | * where "expr2" is evaluated only if "expr1" is evaluated to true. |
611 | | */ |
612 | | __isl_give isl_ast_expr *isl_ast_expr_and_then(__isl_take isl_ast_expr *expr1, |
613 | | __isl_take isl_ast_expr *expr2) |
614 | 0 | { |
615 | 0 | return isl_ast_expr_alloc_binary(isl_ast_op_and_then, expr1, expr2); |
616 | 0 | } |
617 | | |
618 | | /* Create an expression representing the disjunction of "expr1" and "expr2". |
619 | | */ |
620 | | __isl_give isl_ast_expr *isl_ast_expr_or(__isl_take isl_ast_expr *expr1, |
621 | | __isl_take isl_ast_expr *expr2) |
622 | 276 | { |
623 | 276 | return isl_ast_expr_alloc_binary(isl_ast_op_or, expr1, expr2); |
624 | 276 | } |
625 | | |
626 | | /* Create an expression representing the disjunction of "expr1" and "expr2", |
627 | | * where "expr2" is evaluated only if "expr1" is evaluated to false. |
628 | | */ |
629 | | __isl_give isl_ast_expr *isl_ast_expr_or_else(__isl_take isl_ast_expr *expr1, |
630 | | __isl_take isl_ast_expr *expr2) |
631 | 0 | { |
632 | 0 | return isl_ast_expr_alloc_binary(isl_ast_op_or_else, expr1, expr2); |
633 | 0 | } |
634 | | |
635 | | /* Create an expression representing "expr1" less than or equal to "expr2". |
636 | | */ |
637 | | __isl_give isl_ast_expr *isl_ast_expr_le(__isl_take isl_ast_expr *expr1, |
638 | | __isl_take isl_ast_expr *expr2) |
639 | 340 | { |
640 | 340 | return isl_ast_expr_alloc_binary(isl_ast_op_le, expr1, expr2); |
641 | 340 | } |
642 | | |
643 | | /* Create an expression representing "expr1" less than "expr2". |
644 | | */ |
645 | | __isl_give isl_ast_expr *isl_ast_expr_lt(__isl_take isl_ast_expr *expr1, |
646 | | __isl_take isl_ast_expr *expr2) |
647 | 0 | { |
648 | 0 | return isl_ast_expr_alloc_binary(isl_ast_op_lt, expr1, expr2); |
649 | 0 | } |
650 | | |
651 | | /* Create an expression representing "expr1" greater than or equal to "expr2". |
652 | | */ |
653 | | __isl_give isl_ast_expr *isl_ast_expr_ge(__isl_take isl_ast_expr *expr1, |
654 | | __isl_take isl_ast_expr *expr2) |
655 | 0 | { |
656 | 0 | return isl_ast_expr_alloc_binary(isl_ast_op_ge, expr1, expr2); |
657 | 0 | } |
658 | | |
659 | | /* Create an expression representing "expr1" greater than "expr2". |
660 | | */ |
661 | | __isl_give isl_ast_expr *isl_ast_expr_gt(__isl_take isl_ast_expr *expr1, |
662 | | __isl_take isl_ast_expr *expr2) |
663 | 0 | { |
664 | 0 | return isl_ast_expr_alloc_binary(isl_ast_op_gt, expr1, expr2); |
665 | 0 | } |
666 | | |
667 | | /* Create an expression representing "expr1" equal to "expr2". |
668 | | */ |
669 | | __isl_give isl_ast_expr *isl_ast_expr_eq(__isl_take isl_ast_expr *expr1, |
670 | | __isl_take isl_ast_expr *expr2) |
671 | 114 | { |
672 | 114 | return isl_ast_expr_alloc_binary(isl_ast_op_eq, expr1, expr2); |
673 | 114 | } |
674 | | |
675 | | /* Create an expression of type "type" with as arguments "arg0" followed |
676 | | * by "arguments". |
677 | | */ |
678 | | static __isl_give isl_ast_expr *ast_expr_with_arguments( |
679 | | enum isl_ast_op_type type, __isl_take isl_ast_expr *arg0, |
680 | | __isl_take isl_ast_expr_list *arguments) |
681 | 0 | { |
682 | 0 | int i, n; |
683 | 0 | isl_ctx *ctx; |
684 | 0 | isl_ast_expr *res = NULL; |
685 | 0 |
|
686 | 0 | if (!arg0 || !arguments) |
687 | 0 | goto error; |
688 | 0 | |
689 | 0 | ctx = isl_ast_expr_get_ctx(arg0); |
690 | 0 | n = isl_ast_expr_list_n_ast_expr(arguments); |
691 | 0 | res = isl_ast_expr_alloc_op(ctx, type, 1 + n); |
692 | 0 | if (!res) |
693 | 0 | goto error; |
694 | 0 | for (i = 0; i < n; ++i) { |
695 | 0 | isl_ast_expr *arg; |
696 | 0 | arg = isl_ast_expr_list_get_ast_expr(arguments, i); |
697 | 0 | res->u.op.args[1 + i] = arg; |
698 | 0 | if (!arg) |
699 | 0 | goto error; |
700 | 0 | } |
701 | 0 | res->u.op.args[0] = arg0; |
702 | 0 |
|
703 | 0 | isl_ast_expr_list_free(arguments); |
704 | 0 | return res; |
705 | 0 | error: |
706 | 0 | isl_ast_expr_free(arg0); |
707 | 0 | isl_ast_expr_list_free(arguments); |
708 | 0 | isl_ast_expr_free(res); |
709 | 0 | return NULL; |
710 | 0 | } |
711 | | |
712 | | /* Create an expression representing an access to "array" with index |
713 | | * expressions "indices". |
714 | | */ |
715 | | __isl_give isl_ast_expr *isl_ast_expr_access(__isl_take isl_ast_expr *array, |
716 | | __isl_take isl_ast_expr_list *indices) |
717 | 0 | { |
718 | 0 | return ast_expr_with_arguments(isl_ast_op_access, array, indices); |
719 | 0 | } |
720 | | |
721 | | /* Create an expression representing a call to "function" with argument |
722 | | * expressions "arguments". |
723 | | */ |
724 | | __isl_give isl_ast_expr *isl_ast_expr_call(__isl_take isl_ast_expr *function, |
725 | | __isl_take isl_ast_expr_list *arguments) |
726 | 0 | { |
727 | 0 | return ast_expr_with_arguments(isl_ast_op_call, function, arguments); |
728 | 0 | } |
729 | | |
730 | | /* For each subexpression of "expr" of type isl_ast_expr_id, |
731 | | * if it appears in "id2expr", then replace it by the corresponding |
732 | | * expression. |
733 | | */ |
734 | | __isl_give isl_ast_expr *isl_ast_expr_substitute_ids( |
735 | | __isl_take isl_ast_expr *expr, __isl_take isl_id_to_ast_expr *id2expr) |
736 | 0 | { |
737 | 0 | int i; |
738 | 0 | isl_maybe_isl_ast_expr m; |
739 | 0 |
|
740 | 0 | if (!expr || !id2expr) |
741 | 0 | goto error; |
742 | 0 | |
743 | 0 | switch (expr->type) { |
744 | 0 | case isl_ast_expr_int: |
745 | 0 | break; |
746 | 0 | case isl_ast_expr_id: |
747 | 0 | m = isl_id_to_ast_expr_try_get(id2expr, expr->u.id); |
748 | 0 | if (m.valid < 0) |
749 | 0 | goto error; |
750 | 0 | if (!m.valid) |
751 | 0 | break; |
752 | 0 | isl_ast_expr_free(expr); |
753 | 0 | expr = m.value; |
754 | 0 | break; |
755 | 0 | case isl_ast_expr_op: |
756 | 0 | for (i = 0; i < expr->u.op.n_arg; ++i) { |
757 | 0 | isl_ast_expr *arg; |
758 | 0 | arg = isl_ast_expr_copy(expr->u.op.args[i]); |
759 | 0 | arg = isl_ast_expr_substitute_ids(arg, |
760 | 0 | isl_id_to_ast_expr_copy(id2expr)); |
761 | 0 | if (arg == expr->u.op.args[i]) { |
762 | 0 | isl_ast_expr_free(arg); |
763 | 0 | continue; |
764 | 0 | } |
765 | 0 | if (!arg) |
766 | 0 | expr = isl_ast_expr_free(expr); |
767 | 0 | expr = isl_ast_expr_cow(expr); |
768 | 0 | if (!expr) { |
769 | 0 | isl_ast_expr_free(arg); |
770 | 0 | break; |
771 | 0 | } |
772 | 0 | isl_ast_expr_free(expr->u.op.args[i]); |
773 | 0 | expr->u.op.args[i] = arg; |
774 | 0 | } |
775 | 0 | break; |
776 | 0 | case isl_ast_expr_error: |
777 | 0 | expr = isl_ast_expr_free(expr); |
778 | 0 | break; |
779 | 0 | } |
780 | 0 | |
781 | 0 | isl_id_to_ast_expr_free(id2expr); |
782 | 0 | return expr; |
783 | 0 | error: |
784 | 0 | isl_ast_expr_free(expr); |
785 | 0 | isl_id_to_ast_expr_free(id2expr); |
786 | 0 | return NULL; |
787 | 0 | } |
788 | | |
789 | | isl_ctx *isl_ast_node_get_ctx(__isl_keep isl_ast_node *node) |
790 | 6.80k | { |
791 | 6.80k | return node ? node->ctx : NULL; |
792 | 6.80k | } |
793 | | |
794 | | enum isl_ast_node_type isl_ast_node_get_type(__isl_keep isl_ast_node *node) |
795 | 5.30k | { |
796 | 5.30k | return node ? node->type : isl_ast_node_error0 ; |
797 | 5.30k | } |
798 | | |
799 | | __isl_give isl_ast_node *isl_ast_node_alloc(isl_ctx *ctx, |
800 | | enum isl_ast_node_type type) |
801 | 3.90k | { |
802 | 3.90k | isl_ast_node *node; |
803 | 3.90k | |
804 | 3.90k | node = isl_calloc_type(ctx, isl_ast_node); |
805 | 3.90k | if (!node) |
806 | 0 | return NULL; |
807 | 3.90k | |
808 | 3.90k | node->ctx = ctx; |
809 | 3.90k | isl_ctx_ref(ctx); |
810 | 3.90k | node->ref = 1; |
811 | 3.90k | node->type = type; |
812 | 3.90k | |
813 | 3.90k | return node; |
814 | 3.90k | } |
815 | | |
816 | | /* Create an if node with the given guard. |
817 | | * |
818 | | * The then body needs to be filled in later. |
819 | | */ |
820 | | __isl_give isl_ast_node *isl_ast_node_alloc_if(__isl_take isl_ast_expr *guard) |
821 | 241 | { |
822 | 241 | isl_ast_node *node; |
823 | 241 | |
824 | 241 | if (!guard) |
825 | 0 | return NULL; |
826 | 241 | |
827 | 241 | node = isl_ast_node_alloc(isl_ast_expr_get_ctx(guard), isl_ast_node_if); |
828 | 241 | if (!node) |
829 | 0 | goto error; |
830 | 241 | node->u.i.guard = guard; |
831 | 241 | |
832 | 241 | return node; |
833 | 0 | error: |
834 | 0 | isl_ast_expr_free(guard); |
835 | 0 | return NULL; |
836 | 241 | } |
837 | | |
838 | | /* Create a for node with the given iterator. |
839 | | * |
840 | | * The remaining fields need to be filled in later. |
841 | | */ |
842 | | __isl_give isl_ast_node *isl_ast_node_alloc_for(__isl_take isl_id *id) |
843 | 749 | { |
844 | 749 | isl_ast_node *node; |
845 | 749 | isl_ctx *ctx; |
846 | 749 | |
847 | 749 | if (!id) |
848 | 0 | return NULL; |
849 | 749 | |
850 | 749 | ctx = isl_id_get_ctx(id); |
851 | 749 | node = isl_ast_node_alloc(ctx, isl_ast_node_for); |
852 | 749 | if (!node) |
853 | 0 | goto error; |
854 | 749 | |
855 | 749 | node->u.f.iterator = isl_ast_expr_from_id(id); |
856 | 749 | if (!node->u.f.iterator) |
857 | 0 | return isl_ast_node_free(node); |
858 | 749 | |
859 | 749 | return node; |
860 | 0 | error: |
861 | 0 | isl_id_free(id); |
862 | 0 | return NULL; |
863 | 749 | } |
864 | | |
865 | | /* Create a mark node, marking "node" with "id". |
866 | | */ |
867 | | __isl_give isl_ast_node *isl_ast_node_alloc_mark(__isl_take isl_id *id, |
868 | | __isl_take isl_ast_node *node) |
869 | 179 | { |
870 | 179 | isl_ctx *ctx; |
871 | 179 | isl_ast_node *mark; |
872 | 179 | |
873 | 179 | if (!id || !node) |
874 | 0 | goto error; |
875 | 179 | |
876 | 179 | ctx = isl_id_get_ctx(id); |
877 | 179 | mark = isl_ast_node_alloc(ctx, isl_ast_node_mark); |
878 | 179 | if (!mark) |
879 | 0 | goto error; |
880 | 179 | |
881 | 179 | mark->u.m.mark = id; |
882 | 179 | mark->u.m.node = node; |
883 | 179 | |
884 | 179 | return mark; |
885 | 0 | error: |
886 | 0 | isl_id_free(id); |
887 | 0 | isl_ast_node_free(node); |
888 | 0 | return NULL; |
889 | 179 | } |
890 | | |
891 | | /* Create a user node evaluating "expr". |
892 | | */ |
893 | | __isl_give isl_ast_node *isl_ast_node_alloc_user(__isl_take isl_ast_expr *expr) |
894 | 2.23k | { |
895 | 2.23k | isl_ctx *ctx; |
896 | 2.23k | isl_ast_node *node; |
897 | 2.23k | |
898 | 2.23k | if (!expr) |
899 | 0 | return NULL; |
900 | 2.23k | |
901 | 2.23k | ctx = isl_ast_expr_get_ctx(expr); |
902 | 2.23k | node = isl_ast_node_alloc(ctx, isl_ast_node_user); |
903 | 2.23k | if (!node) |
904 | 0 | goto error; |
905 | 2.23k | |
906 | 2.23k | node->u.e.expr = expr; |
907 | 2.23k | |
908 | 2.23k | return node; |
909 | 0 | error: |
910 | 0 | isl_ast_expr_free(expr); |
911 | 0 | return NULL; |
912 | 2.23k | } |
913 | | |
914 | | /* Create a block node with the given children. |
915 | | */ |
916 | | __isl_give isl_ast_node *isl_ast_node_alloc_block( |
917 | | __isl_take isl_ast_node_list *list) |
918 | 499 | { |
919 | 499 | isl_ast_node *node; |
920 | 499 | isl_ctx *ctx; |
921 | 499 | |
922 | 499 | if (!list) |
923 | 0 | return NULL; |
924 | 499 | |
925 | 499 | ctx = isl_ast_node_list_get_ctx(list); |
926 | 499 | node = isl_ast_node_alloc(ctx, isl_ast_node_block); |
927 | 499 | if (!node) |
928 | 0 | goto error; |
929 | 499 | |
930 | 499 | node->u.b.children = list; |
931 | 499 | |
932 | 499 | return node; |
933 | 0 | error: |
934 | 0 | isl_ast_node_list_free(list); |
935 | 0 | return NULL; |
936 | 499 | } |
937 | | |
938 | | /* Represent the given list of nodes as a single node, either by |
939 | | * extract the node from a single element list or by creating |
940 | | * a block node with the list of nodes as children. |
941 | | */ |
942 | | __isl_give isl_ast_node *isl_ast_node_from_ast_node_list( |
943 | | __isl_take isl_ast_node_list *list) |
944 | 4.59k | { |
945 | 4.59k | isl_ast_node *node; |
946 | 4.59k | |
947 | 4.59k | if (isl_ast_node_list_n_ast_node(list) != 1) |
948 | 333 | return isl_ast_node_alloc_block(list); |
949 | 4.26k | |
950 | 4.26k | node = isl_ast_node_list_get_ast_node(list, 0); |
951 | 4.26k | isl_ast_node_list_free(list); |
952 | 4.26k | |
953 | 4.26k | return node; |
954 | 4.26k | } |
955 | | |
956 | | __isl_give isl_ast_node *isl_ast_node_copy(__isl_keep isl_ast_node *node) |
957 | 12.9k | { |
958 | 12.9k | if (!node) |
959 | 0 | return NULL; |
960 | 12.9k | |
961 | 12.9k | node->ref++; |
962 | 12.9k | return node; |
963 | 12.9k | } |
964 | | |
965 | | __isl_give isl_ast_node *isl_ast_node_dup(__isl_keep isl_ast_node *node) |
966 | 0 | { |
967 | 0 | isl_ast_node *dup; |
968 | 0 |
|
969 | 0 | if (!node) |
970 | 0 | return NULL; |
971 | 0 | |
972 | 0 | dup = isl_ast_node_alloc(isl_ast_node_get_ctx(node), node->type); |
973 | 0 | if (!dup) |
974 | 0 | return NULL; |
975 | 0 | |
976 | 0 | switch (node->type) { |
977 | 0 | case isl_ast_node_if: |
978 | 0 | dup->u.i.guard = isl_ast_expr_copy(node->u.i.guard); |
979 | 0 | dup->u.i.then = isl_ast_node_copy(node->u.i.then); |
980 | 0 | dup->u.i.else_node = isl_ast_node_copy(node->u.i.else_node); |
981 | 0 | if (!dup->u.i.guard || !dup->u.i.then || |
982 | 0 | (node->u.i.else_node && !dup->u.i.else_node)) |
983 | 0 | return isl_ast_node_free(dup); |
984 | 0 | break; |
985 | 0 | case isl_ast_node_for: |
986 | 0 | dup->u.f.iterator = isl_ast_expr_copy(node->u.f.iterator); |
987 | 0 | dup->u.f.init = isl_ast_expr_copy(node->u.f.init); |
988 | 0 | dup->u.f.cond = isl_ast_expr_copy(node->u.f.cond); |
989 | 0 | dup->u.f.inc = isl_ast_expr_copy(node->u.f.inc); |
990 | 0 | dup->u.f.body = isl_ast_node_copy(node->u.f.body); |
991 | 0 | if (!dup->u.f.iterator || !dup->u.f.init || !dup->u.f.cond || |
992 | 0 | !dup->u.f.inc || !dup->u.f.body) |
993 | 0 | return isl_ast_node_free(dup); |
994 | 0 | break; |
995 | 0 | case isl_ast_node_block: |
996 | 0 | dup->u.b.children = isl_ast_node_list_copy(node->u.b.children); |
997 | 0 | if (!dup->u.b.children) |
998 | 0 | return isl_ast_node_free(dup); |
999 | 0 | break; |
1000 | 0 | case isl_ast_node_mark: |
1001 | 0 | dup->u.m.mark = isl_id_copy(node->u.m.mark); |
1002 | 0 | dup->u.m.node = isl_ast_node_copy(node->u.m.node); |
1003 | 0 | if (!dup->u.m.mark || !dup->u.m.node) |
1004 | 0 | return isl_ast_node_free(dup); |
1005 | 0 | break; |
1006 | 0 | case isl_ast_node_user: |
1007 | 0 | dup->u.e.expr = isl_ast_expr_copy(node->u.e.expr); |
1008 | 0 | if (!dup->u.e.expr) |
1009 | 0 | return isl_ast_node_free(dup); |
1010 | 0 | break; |
1011 | 0 | case isl_ast_node_error: |
1012 | 0 | break; |
1013 | 0 | } |
1014 | 0 | |
1015 | 0 | return dup; |
1016 | 0 | } |
1017 | | |
1018 | | __isl_give isl_ast_node *isl_ast_node_cow(__isl_take isl_ast_node *node) |
1019 | 3.49k | { |
1020 | 3.49k | if (!node) |
1021 | 0 | return NULL; |
1022 | 3.49k | |
1023 | 3.49k | if (node->ref == 1) |
1024 | 3.49k | return node; |
1025 | 0 | node->ref--; |
1026 | 0 | return isl_ast_node_dup(node); |
1027 | 0 | } |
1028 | | |
1029 | | __isl_null isl_ast_node *isl_ast_node_free(__isl_take isl_ast_node *node) |
1030 | 18.0k | { |
1031 | 18.0k | if (!node) |
1032 | 1.20k | return NULL; |
1033 | 16.8k | |
1034 | 16.8k | if (--node->ref > 0) |
1035 | 12.9k | return NULL; |
1036 | 3.90k | |
1037 | 3.90k | switch (node->type) { |
1038 | 3.90k | case isl_ast_node_if: |
1039 | 241 | isl_ast_expr_free(node->u.i.guard); |
1040 | 241 | isl_ast_node_free(node->u.i.then); |
1041 | 241 | isl_ast_node_free(node->u.i.else_node); |
1042 | 241 | break; |
1043 | 3.90k | case isl_ast_node_for: |
1044 | 749 | isl_ast_expr_free(node->u.f.iterator); |
1045 | 749 | isl_ast_expr_free(node->u.f.init); |
1046 | 749 | isl_ast_expr_free(node->u.f.cond); |
1047 | 749 | isl_ast_expr_free(node->u.f.inc); |
1048 | 749 | isl_ast_node_free(node->u.f.body); |
1049 | 749 | break; |
1050 | 3.90k | case isl_ast_node_block: |
1051 | 499 | isl_ast_node_list_free(node->u.b.children); |
1052 | 499 | break; |
1053 | 3.90k | case isl_ast_node_mark: |
1054 | 179 | isl_id_free(node->u.m.mark); |
1055 | 179 | isl_ast_node_free(node->u.m.node); |
1056 | 179 | break; |
1057 | 3.90k | case isl_ast_node_user: |
1058 | 2.23k | isl_ast_expr_free(node->u.e.expr); |
1059 | 2.23k | break; |
1060 | 3.90k | case isl_ast_node_error: |
1061 | 0 | break; |
1062 | 3.90k | } |
1063 | 3.90k | |
1064 | 3.90k | isl_id_free(node->annotation); |
1065 | 3.90k | isl_ctx_deref(node->ctx); |
1066 | 3.90k | free(node); |
1067 | 3.90k | |
1068 | 3.90k | return NULL; |
1069 | 3.90k | } |
1070 | | |
1071 | | /* Replace the body of the for node "node" by "body". |
1072 | | */ |
1073 | | __isl_give isl_ast_node *isl_ast_node_for_set_body( |
1074 | | __isl_take isl_ast_node *node, __isl_take isl_ast_node *body) |
1075 | 749 | { |
1076 | 749 | node = isl_ast_node_cow(node); |
1077 | 749 | if (!node || !body) |
1078 | 0 | goto error; |
1079 | 749 | if (node->type != isl_ast_node_for) |
1080 | 749 | isl_die0 (isl_ast_node_get_ctx(node), isl_error_invalid, |
1081 | 749 | "not a for node", goto error); |
1082 | 749 | |
1083 | 749 | isl_ast_node_free(node->u.f.body); |
1084 | 749 | node->u.f.body = body; |
1085 | 749 | |
1086 | 749 | return node; |
1087 | 0 | error: |
1088 | 0 | isl_ast_node_free(node); |
1089 | 0 | isl_ast_node_free(body); |
1090 | 0 | return NULL; |
1091 | 749 | } |
1092 | | |
1093 | | __isl_give isl_ast_node *isl_ast_node_for_get_body( |
1094 | | __isl_keep isl_ast_node *node) |
1095 | 648 | { |
1096 | 648 | if (!node) |
1097 | 0 | return NULL; |
1098 | 648 | if (node->type != isl_ast_node_for) |
1099 | 648 | isl_die0 (isl_ast_node_get_ctx(node), isl_error_invalid, |
1100 | 648 | "not a for node", return NULL); |
1101 | 648 | return isl_ast_node_copy(node->u.f.body); |
1102 | 648 | } |
1103 | | |
1104 | | /* Mark the given for node as being degenerate. |
1105 | | */ |
1106 | | __isl_give isl_ast_node *isl_ast_node_for_mark_degenerate( |
1107 | | __isl_take isl_ast_node *node) |
1108 | 0 | { |
1109 | 0 | node = isl_ast_node_cow(node); |
1110 | 0 | if (!node) |
1111 | 0 | return NULL; |
1112 | 0 | node->u.f.degenerate = 1; |
1113 | 0 | return node; |
1114 | 0 | } |
1115 | | |
1116 | | isl_bool isl_ast_node_for_is_degenerate(__isl_keep isl_ast_node *node) |
1117 | 0 | { |
1118 | 0 | if (!node) |
1119 | 0 | return isl_bool_error; |
1120 | 0 | if (node->type != isl_ast_node_for) |
1121 | 0 | isl_die(isl_ast_node_get_ctx(node), isl_error_invalid, |
1122 | 0 | "not a for node", return isl_bool_error); |
1123 | 0 | return node->u.f.degenerate; |
1124 | 0 | } |
1125 | | |
1126 | | __isl_give isl_ast_expr *isl_ast_node_for_get_iterator( |
1127 | | __isl_keep isl_ast_node *node) |
1128 | 688 | { |
1129 | 688 | if (!node) |
1130 | 0 | return NULL; |
1131 | 688 | if (node->type != isl_ast_node_for) |
1132 | 688 | isl_die0 (isl_ast_node_get_ctx(node), isl_error_invalid, |
1133 | 688 | "not a for node", return NULL); |
1134 | 688 | return isl_ast_expr_copy(node->u.f.iterator); |
1135 | 688 | } |
1136 | | |
1137 | | __isl_give isl_ast_expr *isl_ast_node_for_get_init( |
1138 | | __isl_keep isl_ast_node *node) |
1139 | 364 | { |
1140 | 364 | if (!node) |
1141 | 0 | return NULL; |
1142 | 364 | if (node->type != isl_ast_node_for) |
1143 | 364 | isl_die0 (isl_ast_node_get_ctx(node), isl_error_invalid, |
1144 | 364 | "not a for node", return NULL); |
1145 | 364 | return isl_ast_expr_copy(node->u.f.init); |
1146 | 364 | } |
1147 | | |
1148 | | /* Return the condition expression of the given for node. |
1149 | | * |
1150 | | * If the for node is degenerate, then the condition is not explicitly |
1151 | | * stored in the node. Instead, it is constructed as |
1152 | | * |
1153 | | * iterator <= init |
1154 | | */ |
1155 | | __isl_give isl_ast_expr *isl_ast_node_for_get_cond( |
1156 | | __isl_keep isl_ast_node *node) |
1157 | 345 | { |
1158 | 345 | if (!node) |
1159 | 0 | return NULL; |
1160 | 345 | if (node->type != isl_ast_node_for) |
1161 | 345 | isl_die0 (isl_ast_node_get_ctx(node), isl_error_invalid, |
1162 | 345 | "not a for node", return NULL); |
1163 | 345 | if (!node->u.f.degenerate) |
1164 | 345 | return isl_ast_expr_copy(node->u.f.cond); |
1165 | 0 | |
1166 | 0 | return isl_ast_expr_alloc_binary(isl_ast_op_le, |
1167 | 0 | isl_ast_expr_copy(node->u.f.iterator), |
1168 | 0 | isl_ast_expr_copy(node->u.f.init)); |
1169 | 0 | } |
1170 | | |
1171 | | /* Return the increment of the given for node. |
1172 | | * |
1173 | | * If the for node is degenerate, then the increment is not explicitly |
1174 | | * stored in the node. We simply return "1". |
1175 | | */ |
1176 | | __isl_give isl_ast_expr *isl_ast_node_for_get_inc( |
1177 | | __isl_keep isl_ast_node *node) |
1178 | 364 | { |
1179 | 364 | if (!node) |
1180 | 0 | return NULL; |
1181 | 364 | if (node->type != isl_ast_node_for) |
1182 | 364 | isl_die0 (isl_ast_node_get_ctx(node), isl_error_invalid, |
1183 | 364 | "not a for node", return NULL); |
1184 | 364 | if (!node->u.f.degenerate) |
1185 | 364 | return isl_ast_expr_copy(node->u.f.inc); |
1186 | 0 | return isl_ast_expr_alloc_int_si(isl_ast_node_get_ctx(node), 1); |
1187 | 0 | } |
1188 | | |
1189 | | /* Replace the then branch of the if node "node" by "child". |
1190 | | */ |
1191 | | __isl_give isl_ast_node *isl_ast_node_if_set_then( |
1192 | | __isl_take isl_ast_node *node, __isl_take isl_ast_node *child) |
1193 | 241 | { |
1194 | 241 | node = isl_ast_node_cow(node); |
1195 | 241 | if (!node || !child) |
1196 | 0 | goto error; |
1197 | 241 | if (node->type != isl_ast_node_if) |
1198 | 241 | isl_die0 (isl_ast_node_get_ctx(node), isl_error_invalid, |
1199 | 241 | "not an if node", goto error); |
1200 | 241 | |
1201 | 241 | isl_ast_node_free(node->u.i.then); |
1202 | 241 | node->u.i.then = child; |
1203 | 241 | |
1204 | 241 | return node; |
1205 | 0 | error: |
1206 | 0 | isl_ast_node_free(node); |
1207 | 0 | isl_ast_node_free(child); |
1208 | 0 | return NULL; |
1209 | 241 | } |
1210 | | |
1211 | | __isl_give isl_ast_node *isl_ast_node_if_get_then( |
1212 | | __isl_keep isl_ast_node *node) |
1213 | 85 | { |
1214 | 85 | if (!node) |
1215 | 0 | return NULL; |
1216 | 85 | if (node->type != isl_ast_node_if) |
1217 | 85 | isl_die0 (isl_ast_node_get_ctx(node), isl_error_invalid, |
1218 | 85 | "not an if node", return NULL); |
1219 | 85 | return isl_ast_node_copy(node->u.i.then); |
1220 | 85 | } |
1221 | | |
1222 | | isl_bool isl_ast_node_if_has_else( |
1223 | | __isl_keep isl_ast_node *node) |
1224 | 85 | { |
1225 | 85 | if (!node) |
1226 | 0 | return isl_bool_error; |
1227 | 85 | if (node->type != isl_ast_node_if) |
1228 | 85 | isl_die0 (isl_ast_node_get_ctx(node), isl_error_invalid, |
1229 | 85 | "not an if node", return isl_bool_error); |
1230 | 85 | return node->u.i.else_node != NULL; |
1231 | 85 | } |
1232 | | |
1233 | | __isl_give isl_ast_node *isl_ast_node_if_get_else( |
1234 | | __isl_keep isl_ast_node *node) |
1235 | 11 | { |
1236 | 11 | if (!node) |
1237 | 0 | return NULL; |
1238 | 11 | if (node->type != isl_ast_node_if) |
1239 | 11 | isl_die0 (isl_ast_node_get_ctx(node), isl_error_invalid, |
1240 | 11 | "not an if node", return NULL); |
1241 | 11 | return isl_ast_node_copy(node->u.i.else_node); |
1242 | 11 | } |
1243 | | |
1244 | | __isl_give isl_ast_expr *isl_ast_node_if_get_cond( |
1245 | | __isl_keep isl_ast_node *node) |
1246 | 85 | { |
1247 | 85 | if (!node) |
1248 | 0 | return NULL; |
1249 | 85 | if (node->type != isl_ast_node_if) |
1250 | 85 | isl_die0 (isl_ast_node_get_ctx(node), isl_error_invalid, |
1251 | 85 | "not a guard node", return NULL); |
1252 | 85 | return isl_ast_expr_copy(node->u.i.guard); |
1253 | 85 | } |
1254 | | |
1255 | | __isl_give isl_ast_node_list *isl_ast_node_block_get_children( |
1256 | | __isl_keep isl_ast_node *node) |
1257 | 237 | { |
1258 | 237 | if (!node) |
1259 | 0 | return NULL; |
1260 | 237 | if (node->type != isl_ast_node_block) |
1261 | 237 | isl_die0 (isl_ast_node_get_ctx(node), isl_error_invalid, |
1262 | 237 | "not a block node", return NULL); |
1263 | 237 | return isl_ast_node_list_copy(node->u.b.children); |
1264 | 237 | } |
1265 | | |
1266 | | __isl_give isl_ast_expr *isl_ast_node_user_get_expr( |
1267 | | __isl_keep isl_ast_node *node) |
1268 | 553 | { |
1269 | 553 | if (!node) |
1270 | 0 | return NULL; |
1271 | 553 | if (node->type != isl_ast_node_user) |
1272 | 553 | isl_die0 (isl_ast_node_get_ctx(node), isl_error_invalid, |
1273 | 553 | "not a user node", return NULL); |
1274 | 553 | |
1275 | 553 | return isl_ast_expr_copy(node->u.e.expr); |
1276 | 553 | } |
1277 | | |
1278 | | /* Return the mark identifier of the mark node "node". |
1279 | | */ |
1280 | | __isl_give isl_id *isl_ast_node_mark_get_id(__isl_keep isl_ast_node *node) |
1281 | 123 | { |
1282 | 123 | if (!node) |
1283 | 0 | return NULL; |
1284 | 123 | if (node->type != isl_ast_node_mark) |
1285 | 123 | isl_die0 (isl_ast_node_get_ctx(node), isl_error_invalid, |
1286 | 123 | "not a mark node", return NULL); |
1287 | 123 | |
1288 | 123 | return isl_id_copy(node->u.m.mark); |
1289 | 123 | } |
1290 | | |
1291 | | /* Return the node marked by mark node "node". |
1292 | | */ |
1293 | | __isl_give isl_ast_node *isl_ast_node_mark_get_node( |
1294 | | __isl_keep isl_ast_node *node) |
1295 | 43 | { |
1296 | 43 | if (!node) |
1297 | 0 | return NULL; |
1298 | 43 | if (node->type != isl_ast_node_mark) |
1299 | 43 | isl_die0 (isl_ast_node_get_ctx(node), isl_error_invalid, |
1300 | 43 | "not a mark node", return NULL); |
1301 | 43 | |
1302 | 43 | return isl_ast_node_copy(node->u.m.node); |
1303 | 43 | } |
1304 | | |
1305 | | __isl_give isl_id *isl_ast_node_get_annotation(__isl_keep isl_ast_node *node) |
1306 | 7.21k | { |
1307 | 7.21k | return node ? isl_id_copy(node->annotation) : NULL; |
1308 | 7.21k | } |
1309 | | |
1310 | | /* Replace node->annotation by "annotation". |
1311 | | */ |
1312 | | __isl_give isl_ast_node *isl_ast_node_set_annotation( |
1313 | | __isl_take isl_ast_node *node, __isl_take isl_id *annotation) |
1314 | 2.50k | { |
1315 | 2.50k | node = isl_ast_node_cow(node); |
1316 | 2.50k | if (!node || !annotation) |
1317 | 0 | goto error; |
1318 | 2.50k | |
1319 | 2.50k | isl_id_free(node->annotation); |
1320 | 2.50k | node->annotation = annotation; |
1321 | 2.50k | |
1322 | 2.50k | return node; |
1323 | 0 | error: |
1324 | 0 | isl_id_free(annotation); |
1325 | 0 | return isl_ast_node_free(node); |
1326 | 2.50k | } |
1327 | | |
1328 | | /* Traverse the elements of "list" and all their descendants |
1329 | | * in depth first preorder. |
1330 | | * |
1331 | | * Return isl_stat_ok on success and isl_stat_error on failure. |
1332 | | */ |
1333 | | static isl_stat nodelist_foreach(__isl_keep isl_ast_node_list *list, |
1334 | | isl_bool (*fn)(__isl_keep isl_ast_node *node, void *user), void *user) |
1335 | 409 | { |
1336 | 409 | int i; |
1337 | 409 | |
1338 | 409 | if (!list) |
1339 | 0 | return isl_stat_error; |
1340 | 409 | |
1341 | 2.55k | for (i = 0; 409 i < list->n; ++i2.14k ) { |
1342 | 2.14k | isl_stat ok; |
1343 | 2.14k | isl_ast_node *node = list->p[i]; |
1344 | 2.14k | |
1345 | 2.14k | ok = isl_ast_node_foreach_descendant_top_down(node, fn, user); |
1346 | 2.14k | if (ok < 0) |
1347 | 0 | return isl_stat_error; |
1348 | 2.14k | } |
1349 | 409 | |
1350 | 409 | return isl_stat_ok; |
1351 | 409 | } |
1352 | | |
1353 | | /* Traverse the descendants of "node" (including the node itself) |
1354 | | * in depth first preorder. |
1355 | | * |
1356 | | * If "fn" returns isl_bool_error on any of the nodes, then the traversal |
1357 | | * is aborted. |
1358 | | * If "fn" returns isl_bool_false on any of the nodes, then the subtree rooted |
1359 | | * at that node is skipped. |
1360 | | * |
1361 | | * Return isl_stat_ok on success and isl_stat_error on failure. |
1362 | | */ |
1363 | | isl_stat isl_ast_node_foreach_descendant_top_down( |
1364 | | __isl_keep isl_ast_node *node, |
1365 | | isl_bool (*fn)(__isl_keep isl_ast_node *node, void *user), void *user) |
1366 | 3.82k | { |
1367 | 3.82k | isl_bool more; |
1368 | 3.82k | isl_stat ok; |
1369 | 3.82k | |
1370 | 3.82k | if (!node) |
1371 | 0 | return isl_stat_error; |
1372 | 3.82k | |
1373 | 3.82k | more = fn(node, user); |
1374 | 3.82k | if (more < 0) |
1375 | 0 | return isl_stat_error; |
1376 | 3.82k | if (!more) |
1377 | 0 | return isl_stat_ok; |
1378 | 3.82k | |
1379 | 3.82k | switch (node->type) { |
1380 | 3.82k | case isl_ast_node_for: |
1381 | 758 | node = node->u.f.body; |
1382 | 758 | return isl_ast_node_foreach_descendant_top_down(node, fn, user); |
1383 | 3.82k | case isl_ast_node_if: |
1384 | 235 | ok = isl_ast_node_foreach_descendant_top_down(node->u.i.then, |
1385 | 235 | fn, user); |
1386 | 235 | if (ok < 0) |
1387 | 0 | return isl_stat_error; |
1388 | 235 | if (!node->u.i.else_node) |
1389 | 204 | return isl_stat_ok; |
1390 | 31 | node = node->u.i.else_node; |
1391 | 31 | return isl_ast_node_foreach_descendant_top_down(node, fn, user); |
1392 | 409 | case isl_ast_node_block: |
1393 | 409 | return nodelist_foreach(node->u.b.children, fn, user); |
1394 | 179 | case isl_ast_node_mark: |
1395 | 179 | node = node->u.m.node; |
1396 | 179 | return isl_ast_node_foreach_descendant_top_down(node, fn, user); |
1397 | 2.24k | case isl_ast_node_user: |
1398 | 2.24k | break; |
1399 | 31 | case isl_ast_node_error: |
1400 | 0 | return isl_stat_error; |
1401 | 2.24k | } |
1402 | 2.24k | |
1403 | 2.24k | return isl_stat_ok; |
1404 | 2.24k | } |
1405 | | |
1406 | | /* Textual C representation of the various operators. |
1407 | | */ |
1408 | | static char *op_str_c[] = { |
1409 | | [isl_ast_op_and] = "&&", |
1410 | | [isl_ast_op_and_then] = "&&", |
1411 | | [isl_ast_op_or] = "||", |
1412 | | [isl_ast_op_or_else] = "||", |
1413 | | [isl_ast_op_max] = "max", |
1414 | | [isl_ast_op_min] = "min", |
1415 | | [isl_ast_op_minus] = "-", |
1416 | | [isl_ast_op_add] = "+", |
1417 | | [isl_ast_op_sub] = "-", |
1418 | | [isl_ast_op_mul] = "*", |
1419 | | [isl_ast_op_fdiv_q] = "floord", |
1420 | | [isl_ast_op_pdiv_q] = "/", |
1421 | | [isl_ast_op_pdiv_r] = "%", |
1422 | | [isl_ast_op_zdiv_r] = "%", |
1423 | | [isl_ast_op_div] = "/", |
1424 | | [isl_ast_op_eq] = "==", |
1425 | | [isl_ast_op_le] = "<=", |
1426 | | [isl_ast_op_ge] = ">=", |
1427 | | [isl_ast_op_lt] = "<", |
1428 | | [isl_ast_op_gt] = ">", |
1429 | | [isl_ast_op_member] = ".", |
1430 | | [isl_ast_op_address_of] = "&" |
1431 | | }; |
1432 | | |
1433 | | /* Precedence in C of the various operators. |
1434 | | * Based on http://en.wikipedia.org/wiki/Operators_in_C_and_C++ |
1435 | | * Lowest value means highest precedence. |
1436 | | */ |
1437 | | static int op_prec[] = { |
1438 | | [isl_ast_op_and] = 13, |
1439 | | [isl_ast_op_and_then] = 13, |
1440 | | [isl_ast_op_or] = 14, |
1441 | | [isl_ast_op_or_else] = 14, |
1442 | | [isl_ast_op_max] = 2, |
1443 | | [isl_ast_op_min] = 2, |
1444 | | [isl_ast_op_minus] = 3, |
1445 | | [isl_ast_op_add] = 6, |
1446 | | [isl_ast_op_sub] = 6, |
1447 | | [isl_ast_op_mul] = 5, |
1448 | | [isl_ast_op_div] = 5, |
1449 | | [isl_ast_op_fdiv_q] = 2, |
1450 | | [isl_ast_op_pdiv_q] = 5, |
1451 | | [isl_ast_op_pdiv_r] = 5, |
1452 | | [isl_ast_op_zdiv_r] = 5, |
1453 | | [isl_ast_op_cond] = 15, |
1454 | | [isl_ast_op_select] = 15, |
1455 | | [isl_ast_op_eq] = 9, |
1456 | | [isl_ast_op_le] = 8, |
1457 | | [isl_ast_op_ge] = 8, |
1458 | | [isl_ast_op_lt] = 8, |
1459 | | [isl_ast_op_gt] = 8, |
1460 | | [isl_ast_op_call] = 2, |
1461 | | [isl_ast_op_access] = 2, |
1462 | | [isl_ast_op_member] = 2, |
1463 | | [isl_ast_op_address_of] = 3 |
1464 | | }; |
1465 | | |
1466 | | /* Is the operator left-to-right associative? |
1467 | | */ |
1468 | | static int op_left[] = { |
1469 | | [isl_ast_op_and] = 1, |
1470 | | [isl_ast_op_and_then] = 1, |
1471 | | [isl_ast_op_or] = 1, |
1472 | | [isl_ast_op_or_else] = 1, |
1473 | | [isl_ast_op_max] = 1, |
1474 | | [isl_ast_op_min] = 1, |
1475 | | [isl_ast_op_minus] = 0, |
1476 | | [isl_ast_op_add] = 1, |
1477 | | [isl_ast_op_sub] = 1, |
1478 | | [isl_ast_op_mul] = 1, |
1479 | | [isl_ast_op_div] = 1, |
1480 | | [isl_ast_op_fdiv_q] = 1, |
1481 | | [isl_ast_op_pdiv_q] = 1, |
1482 | | [isl_ast_op_pdiv_r] = 1, |
1483 | | [isl_ast_op_zdiv_r] = 1, |
1484 | | [isl_ast_op_cond] = 0, |
1485 | | [isl_ast_op_select] = 0, |
1486 | | [isl_ast_op_eq] = 1, |
1487 | | [isl_ast_op_le] = 1, |
1488 | | [isl_ast_op_ge] = 1, |
1489 | | [isl_ast_op_lt] = 1, |
1490 | | [isl_ast_op_gt] = 1, |
1491 | | [isl_ast_op_call] = 1, |
1492 | | [isl_ast_op_access] = 1, |
1493 | | [isl_ast_op_member] = 1, |
1494 | | [isl_ast_op_address_of] = 0 |
1495 | | }; |
1496 | | |
1497 | | static int is_and(enum isl_ast_op_type op) |
1498 | 190 | { |
1499 | 190 | return op == isl_ast_op_and || op == isl_ast_op_and_then164 ; |
1500 | 190 | } |
1501 | | |
1502 | | static int is_or(enum isl_ast_op_type op) |
1503 | 7.39k | { |
1504 | 7.39k | return op == isl_ast_op_or || op == isl_ast_op_or_else7.20k ; |
1505 | 7.39k | } |
1506 | | |
1507 | | static int is_add_sub(enum isl_ast_op_type op) |
1508 | 7.37k | { |
1509 | 7.37k | return op == isl_ast_op_add || op == isl_ast_op_sub1.18k ; |
1510 | 7.37k | } |
1511 | | |
1512 | | static int is_div_mod(enum isl_ast_op_type op) |
1513 | 6.27k | { |
1514 | 6.27k | return op == isl_ast_op_div || |
1515 | 6.27k | op == isl_ast_op_pdiv_r6.27k || |
1516 | 6.27k | op == isl_ast_op_zdiv_r6.26k ; |
1517 | 6.27k | } |
1518 | | |
1519 | | static __isl_give isl_printer *print_ast_expr_c(__isl_take isl_printer *p, |
1520 | | __isl_keep isl_ast_expr *expr); |
1521 | | |
1522 | | /* Do we need/want parentheses around "expr" as a subexpression of |
1523 | | * an "op" operation? If "left" is set, then "expr" is the left-most |
1524 | | * operand. |
1525 | | * |
1526 | | * We only need parentheses if "expr" represents an operation. |
1527 | | * |
1528 | | * If op has a higher precedence than expr->u.op.op, then we need |
1529 | | * parentheses. |
1530 | | * If op and expr->u.op.op have the same precedence, but the operations |
1531 | | * are performed in an order that is different from the associativity, |
1532 | | * then we need parentheses. |
1533 | | * |
1534 | | * An and inside an or technically does not require parentheses, |
1535 | | * but some compilers complain about that, so we add them anyway. |
1536 | | * |
1537 | | * Computations such as "a / b * c" and "a % b + c" can be somewhat |
1538 | | * difficult to read, so we add parentheses for those as well. |
1539 | | */ |
1540 | | static int sub_expr_need_parens(enum isl_ast_op_type op, |
1541 | | __isl_keep isl_ast_expr *expr, int left) |
1542 | 23.3k | { |
1543 | 23.3k | if (expr->type != isl_ast_expr_op) |
1544 | 15.7k | return 0; |
1545 | 7.58k | |
1546 | 7.58k | if (op_prec[expr->u.op.op] > op_prec[op]) |
1547 | 191 | return 1; |
1548 | 7.39k | if (op_prec[expr->u.op.op] == op_prec[op] && left != op_left[op]1.22k ) |
1549 | 1 | return 1; |
1550 | 7.39k | |
1551 | 7.39k | if (is_or(op) && is_and(expr->u.op.op)190 ) |
1552 | 26 | return 1; |
1553 | 7.37k | if (op == isl_ast_op_mul && expr->u.op.op != isl_ast_op_mul2 && |
1554 | 7.37k | op_prec[expr->u.op.op] == op_prec[op]2 ) |
1555 | 0 | return 1; |
1556 | 7.37k | if (is_add_sub(op) && is_div_mod(expr->u.op.op)6.27k ) |
1557 | 10 | return 1; |
1558 | 7.36k | |
1559 | 7.36k | return 0; |
1560 | 7.36k | } |
1561 | | |
1562 | | /* Print "expr" as a subexpression of an "op" operation in C format. |
1563 | | * If "left" is set, then "expr" is the left-most operand. |
1564 | | */ |
1565 | | static __isl_give isl_printer *print_sub_expr_c(__isl_take isl_printer *p, |
1566 | | enum isl_ast_op_type op, __isl_keep isl_ast_expr *expr, int left) |
1567 | 23.3k | { |
1568 | 23.3k | int need_parens; |
1569 | 23.3k | |
1570 | 23.3k | need_parens = sub_expr_need_parens(op, expr, left); |
1571 | 23.3k | |
1572 | 23.3k | if (need_parens) |
1573 | 228 | p = isl_printer_print_str(p, "("); |
1574 | 23.3k | p = print_ast_expr_c(p, expr); |
1575 | 23.3k | if (need_parens) |
1576 | 228 | p = isl_printer_print_str(p, ")"); |
1577 | 23.3k | return p; |
1578 | 23.3k | } |
1579 | | |
1580 | 0 | #define isl_ast_op_last isl_ast_op_address_of |
1581 | | |
1582 | | /* Data structure that holds the user-specified textual |
1583 | | * representations for the operators in C format. |
1584 | | * The entries are either NULL or copies of strings. |
1585 | | * A NULL entry means that the default name should be used. |
1586 | | */ |
1587 | | struct isl_ast_op_names { |
1588 | | char *op_str[isl_ast_op_last + 1]; |
1589 | | }; |
1590 | | |
1591 | | /* Create an empty struct isl_ast_op_names. |
1592 | | */ |
1593 | | static void *create_names(isl_ctx *ctx) |
1594 | 0 | { |
1595 | 0 | return isl_calloc_type(ctx, struct isl_ast_op_names); |
1596 | 0 | } |
1597 | | |
1598 | | /* Free a struct isl_ast_op_names along with all memory |
1599 | | * owned by the struct. |
1600 | | */ |
1601 | | static void free_names(void *user) |
1602 | 0 | { |
1603 | 0 | int i; |
1604 | 0 | struct isl_ast_op_names *names = user; |
1605 | 0 |
|
1606 | 0 | if (!user) |
1607 | 0 | return; |
1608 | 0 | |
1609 | 0 | for (i = 0; i <= isl_ast_op_last; ++i) |
1610 | 0 | free(names->op_str[i]); |
1611 | 0 | free(user); |
1612 | 0 | } |
1613 | | |
1614 | | /* Create an identifier that is used to store |
1615 | | * an isl_ast_op_names note. |
1616 | | */ |
1617 | | static __isl_give isl_id *names_id(isl_ctx *ctx) |
1618 | 11.9k | { |
1619 | 11.9k | return isl_id_alloc(ctx, "isl_ast_op_type_names", NULL); |
1620 | 11.9k | } |
1621 | | |
1622 | | /* Ensure that "p" has a note identified by "id". |
1623 | | * If there is no such note yet, then it is created by "note_create" and |
1624 | | * scheduled do be freed by "note_free". |
1625 | | */ |
1626 | | static __isl_give isl_printer *alloc_note(__isl_take isl_printer *p, |
1627 | | __isl_keep isl_id *id, void *(*note_create)(isl_ctx *), |
1628 | | void (*note_free)(void *)) |
1629 | 0 | { |
1630 | 0 | isl_ctx *ctx; |
1631 | 0 | isl_id *note_id; |
1632 | 0 | isl_bool has_note; |
1633 | 0 | void *note; |
1634 | 0 |
|
1635 | 0 | has_note = isl_printer_has_note(p, id); |
1636 | 0 | if (has_note < 0) |
1637 | 0 | return isl_printer_free(p); |
1638 | 0 | if (has_note) |
1639 | 0 | return p; |
1640 | 0 | |
1641 | 0 | ctx = isl_printer_get_ctx(p); |
1642 | 0 | note = note_create(ctx); |
1643 | 0 | if (!note) |
1644 | 0 | return isl_printer_free(p); |
1645 | 0 | note_id = isl_id_alloc(ctx, NULL, note); |
1646 | 0 | if (!note_id) |
1647 | 0 | note_free(note); |
1648 | 0 | else |
1649 | 0 | note_id = isl_id_set_free_user(note_id, note_free); |
1650 | 0 |
|
1651 | 0 | p = isl_printer_set_note(p, isl_id_copy(id), note_id); |
1652 | 0 |
|
1653 | 0 | return p; |
1654 | 0 | } |
1655 | | |
1656 | | /* Ensure that "p" has an isl_ast_op_names note identified by "id". |
1657 | | */ |
1658 | | static __isl_give isl_printer *alloc_names(__isl_take isl_printer *p, |
1659 | | __isl_keep isl_id *id) |
1660 | 0 | { |
1661 | 0 | return alloc_note(p, id, &create_names, &free_names); |
1662 | 0 | } |
1663 | | |
1664 | | /* Retrieve the note identified by "id" from "p". |
1665 | | * The note is assumed to exist. |
1666 | | */ |
1667 | | static void *get_note(__isl_keep isl_printer *p, __isl_keep isl_id *id) |
1668 | 0 | { |
1669 | 0 | void *note; |
1670 | 0 |
|
1671 | 0 | id = isl_printer_get_note(p, isl_id_copy(id)); |
1672 | 0 | note = isl_id_get_user(id); |
1673 | 0 | isl_id_free(id); |
1674 | 0 |
|
1675 | 0 | return note; |
1676 | 0 | } |
1677 | | |
1678 | | /* Use "name" to print operations of type "type" to "p". |
1679 | | * |
1680 | | * Store the name in an isl_ast_op_names note attached to "p", such that |
1681 | | * it can be retrieved by get_op_str. |
1682 | | */ |
1683 | | __isl_give isl_printer *isl_ast_op_type_set_print_name( |
1684 | | __isl_take isl_printer *p, enum isl_ast_op_type type, |
1685 | | __isl_keep const char *name) |
1686 | 0 | { |
1687 | 0 | isl_id *id; |
1688 | 0 | struct isl_ast_op_names *names; |
1689 | 0 |
|
1690 | 0 | if (!p) |
1691 | 0 | return NULL; |
1692 | 0 | if (type > isl_ast_op_last) |
1693 | 0 | isl_die(isl_printer_get_ctx(p), isl_error_invalid, |
1694 | 0 | "invalid type", return isl_printer_free(p)); |
1695 | 0 |
|
1696 | 0 | id = names_id(isl_printer_get_ctx(p)); |
1697 | 0 | p = alloc_names(p, id); |
1698 | 0 | names = get_note(p, id); |
1699 | 0 | isl_id_free(id); |
1700 | 0 | if (!names) |
1701 | 0 | return isl_printer_free(p); |
1702 | 0 | free(names->op_str[type]); |
1703 | 0 | names->op_str[type] = strdup(name); |
1704 | 0 |
|
1705 | 0 | return p; |
1706 | 0 | } |
1707 | | |
1708 | | /* Return the textual representation of "type" in C format. |
1709 | | * |
1710 | | * If there is a user-specified name in an isl_ast_op_names note |
1711 | | * associated to "p", then return that. |
1712 | | * Otherwise, return the default name in op_str. |
1713 | | */ |
1714 | | static const char *get_op_str_c(__isl_keep isl_printer *p, |
1715 | | enum isl_ast_op_type type) |
1716 | 11.9k | { |
1717 | 11.9k | isl_id *id; |
1718 | 11.9k | isl_bool has_names; |
1719 | 11.9k | struct isl_ast_op_names *names = NULL; |
1720 | 11.9k | |
1721 | 11.9k | id = names_id(isl_printer_get_ctx(p)); |
1722 | 11.9k | has_names = isl_printer_has_note(p, id); |
1723 | 11.9k | if (has_names >= 0 && has_names) |
1724 | 0 | names = get_note(p, id); |
1725 | 11.9k | isl_id_free(id); |
1726 | 11.9k | if (names && names->op_str[type]0 ) |
1727 | 0 | return names->op_str[type]; |
1728 | 11.9k | return op_str_c[type]; |
1729 | 11.9k | } |
1730 | | |
1731 | | /* Print a min or max reduction "expr" in C format. |
1732 | | */ |
1733 | | static __isl_give isl_printer *print_min_max_c(__isl_take isl_printer *p, |
1734 | | __isl_keep isl_ast_expr *expr) |
1735 | 81 | { |
1736 | 81 | int i = 0; |
1737 | 81 | |
1738 | 162 | for (i = 1; i < expr->u.op.n_arg; ++i81 ) { |
1739 | 81 | p = isl_printer_print_str(p, get_op_str_c(p, expr->u.op.op)); |
1740 | 81 | p = isl_printer_print_str(p, "("); |
1741 | 81 | } |
1742 | 81 | p = isl_printer_print_ast_expr(p, expr->u.op.args[0]); |
1743 | 162 | for (i = 1; i < expr->u.op.n_arg; ++i81 ) { |
1744 | 81 | p = isl_printer_print_str(p, ", "); |
1745 | 81 | p = print_ast_expr_c(p, expr->u.op.args[i]); |
1746 | 81 | p = isl_printer_print_str(p, ")"); |
1747 | 81 | } |
1748 | 81 | |
1749 | 81 | return p; |
1750 | 81 | } |
1751 | | |
1752 | | /* Print a function call "expr" in C format. |
1753 | | * |
1754 | | * The first argument represents the function to be called. |
1755 | | */ |
1756 | | static __isl_give isl_printer *print_call_c(__isl_take isl_printer *p, |
1757 | | __isl_keep isl_ast_expr *expr) |
1758 | 1.70k | { |
1759 | 1.70k | int i = 0; |
1760 | 1.70k | |
1761 | 1.70k | p = print_ast_expr_c(p, expr->u.op.args[0]); |
1762 | 1.70k | p = isl_printer_print_str(p, "("); |
1763 | 6.41k | for (i = 1; i < expr->u.op.n_arg; ++i4.70k ) { |
1764 | 4.70k | if (i != 1) |
1765 | 3.01k | p = isl_printer_print_str(p, ", "); |
1766 | 4.70k | p = print_ast_expr_c(p, expr->u.op.args[i]); |
1767 | 4.70k | } |
1768 | 1.70k | p = isl_printer_print_str(p, ")"); |
1769 | 1.70k | |
1770 | 1.70k | return p; |
1771 | 1.70k | } |
1772 | | |
1773 | | /* Print an array access "expr" in C format. |
1774 | | * |
1775 | | * The first argument represents the array being accessed. |
1776 | | */ |
1777 | | static __isl_give isl_printer *print_access_c(__isl_take isl_printer *p, |
1778 | | __isl_keep isl_ast_expr *expr) |
1779 | 218 | { |
1780 | 218 | int i = 0; |
1781 | 218 | |
1782 | 218 | p = print_ast_expr_c(p, expr->u.op.args[0]); |
1783 | 548 | for (i = 1; i < expr->u.op.n_arg; ++i330 ) { |
1784 | 330 | p = isl_printer_print_str(p, "["); |
1785 | 330 | p = print_ast_expr_c(p, expr->u.op.args[i]); |
1786 | 330 | p = isl_printer_print_str(p, "]"); |
1787 | 330 | } |
1788 | 218 | |
1789 | 218 | return p; |
1790 | 218 | } |
1791 | | |
1792 | | /* Print "expr" to "p" in C format. |
1793 | | */ |
1794 | | static __isl_give isl_printer *print_ast_expr_c(__isl_take isl_printer *p, |
1795 | | __isl_keep isl_ast_expr *expr) |
1796 | 33.8k | { |
1797 | 33.8k | if (!p) |
1798 | 0 | return NULL; |
1799 | 33.8k | if (!expr) |
1800 | 0 | return isl_printer_free(p); |
1801 | 33.8k | |
1802 | 33.8k | switch (expr->type) { |
1803 | 33.8k | case isl_ast_expr_op: |
1804 | 13.8k | if (expr->u.op.op == isl_ast_op_call) { |
1805 | 1.70k | p = print_call_c(p, expr); |
1806 | 1.70k | break; |
1807 | 1.70k | } |
1808 | 12.1k | if (expr->u.op.op == isl_ast_op_access) { |
1809 | 218 | p = print_access_c(p, expr); |
1810 | 218 | break; |
1811 | 218 | } |
1812 | 11.9k | if (expr->u.op.n_arg == 1) { |
1813 | 278 | p = isl_printer_print_str(p, |
1814 | 278 | get_op_str_c(p, expr->u.op.op)); |
1815 | 278 | p = print_sub_expr_c(p, expr->u.op.op, |
1816 | 278 | expr->u.op.args[0], 0); |
1817 | 278 | break; |
1818 | 278 | } |
1819 | 11.6k | if (expr->u.op.op == isl_ast_op_fdiv_q) { |
1820 | 13 | const char *name = get_op_str_c(p, isl_ast_op_fdiv_q); |
1821 | 13 | p = isl_printer_print_str(p, name); |
1822 | 13 | p = isl_printer_print_str(p, "("); |
1823 | 13 | p = print_ast_expr_c(p, expr->u.op.args[0]); |
1824 | 13 | p = isl_printer_print_str(p, ", "); |
1825 | 13 | p = print_ast_expr_c(p, expr->u.op.args[1]); |
1826 | 13 | p = isl_printer_print_str(p, ")"); |
1827 | 13 | break; |
1828 | 13 | } |
1829 | 11.6k | if (expr->u.op.op == isl_ast_op_max || |
1830 | 11.6k | expr->u.op.op == isl_ast_op_min11.6k ) { |
1831 | 81 | p = print_min_max_c(p, expr); |
1832 | 81 | break; |
1833 | 81 | } |
1834 | 11.5k | if (expr->u.op.op == isl_ast_op_cond || |
1835 | 11.5k | expr->u.op.op == isl_ast_op_select) { |
1836 | 3 | p = print_ast_expr_c(p, expr->u.op.args[0]); |
1837 | 3 | p = isl_printer_print_str(p, " ? "); |
1838 | 3 | p = print_ast_expr_c(p, expr->u.op.args[1]); |
1839 | 3 | p = isl_printer_print_str(p, " : "); |
1840 | 3 | p = print_ast_expr_c(p, expr->u.op.args[2]); |
1841 | 3 | break; |
1842 | 3 | } |
1843 | 11.5k | if (expr->u.op.n_arg != 2) |
1844 | 11.5k | isl_die0 (isl_printer_get_ctx(p), isl_error_internal, |
1845 | 11.5k | "operation should have two arguments", |
1846 | 11.5k | return isl_printer_free(p)); |
1847 | 11.5k | p = print_sub_expr_c(p, expr->u.op.op, expr->u.op.args[0], 1); |
1848 | 11.5k | if (expr->u.op.op != isl_ast_op_member) |
1849 | 11.5k | p = isl_printer_print_str(p, " "); |
1850 | 11.5k | p = isl_printer_print_str(p, get_op_str_c(p, expr->u.op.op)); |
1851 | 11.5k | if (expr->u.op.op != isl_ast_op_member) |
1852 | 11.5k | p = isl_printer_print_str(p, " "); |
1853 | 11.5k | p = print_sub_expr_c(p, expr->u.op.op, expr->u.op.args[1], 0); |
1854 | 11.5k | break; |
1855 | 11.5k | case isl_ast_expr_id: |
1856 | 10.0k | p = isl_printer_print_str(p, isl_id_get_name(expr->u.id)); |
1857 | 10.0k | break; |
1858 | 11.5k | case isl_ast_expr_int: |
1859 | 9.97k | p = isl_printer_print_val(p, expr->u.v); |
1860 | 9.97k | break; |
1861 | 11.5k | case isl_ast_expr_error: |
1862 | 0 | break; |
1863 | 33.8k | } |
1864 | 33.8k | |
1865 | 33.8k | return p; |
1866 | 33.8k | } |
1867 | | |
1868 | | /* Textual representation of the isl_ast_op_type elements |
1869 | | * for use in a YAML representation of an isl_ast_expr. |
1870 | | */ |
1871 | | static char *op_str[] = { |
1872 | | [isl_ast_op_and] = "and", |
1873 | | [isl_ast_op_and_then] = "and_then", |
1874 | | [isl_ast_op_or] = "or", |
1875 | | [isl_ast_op_or_else] = "or_else", |
1876 | | [isl_ast_op_max] = "max", |
1877 | | [isl_ast_op_min] = "min", |
1878 | | [isl_ast_op_minus] = "minus", |
1879 | | [isl_ast_op_add] = "add", |
1880 | | [isl_ast_op_sub] = "sub", |
1881 | | [isl_ast_op_mul] = "mul", |
1882 | | [isl_ast_op_div] = "div", |
1883 | | [isl_ast_op_fdiv_q] = "fdiv_q", |
1884 | | [isl_ast_op_pdiv_q] = "pdiv_q", |
1885 | | [isl_ast_op_pdiv_r] = "pdiv_r", |
1886 | | [isl_ast_op_zdiv_r] = "zdiv_r", |
1887 | | [isl_ast_op_cond] = "cond", |
1888 | | [isl_ast_op_select] = "select", |
1889 | | [isl_ast_op_eq] = "eq", |
1890 | | [isl_ast_op_le] = "le", |
1891 | | [isl_ast_op_lt] = "lt", |
1892 | | [isl_ast_op_ge] = "ge", |
1893 | | [isl_ast_op_gt] = "gt", |
1894 | | [isl_ast_op_call] = "call", |
1895 | | [isl_ast_op_access] = "access", |
1896 | | [isl_ast_op_member] = "member", |
1897 | | [isl_ast_op_address_of] = "address_of" |
1898 | | }; |
1899 | | |
1900 | | static __isl_give isl_printer *print_ast_expr_isl(__isl_take isl_printer *p, |
1901 | | __isl_keep isl_ast_expr *expr); |
1902 | | |
1903 | | /* Print the arguments of "expr" to "p" in isl format. |
1904 | | * |
1905 | | * If there are no arguments, then nothing needs to be printed. |
1906 | | * Otherwise add an "args" key to the current mapping with as value |
1907 | | * the list of arguments of "expr". |
1908 | | */ |
1909 | | static __isl_give isl_printer *print_arguments(__isl_take isl_printer *p, |
1910 | | __isl_keep isl_ast_expr *expr) |
1911 | 0 | { |
1912 | 0 | int i, n; |
1913 | 0 |
|
1914 | 0 | n = isl_ast_expr_get_op_n_arg(expr); |
1915 | 0 | if (n < 0) |
1916 | 0 | return isl_printer_free(p); |
1917 | 0 | if (n == 0) |
1918 | 0 | return p; |
1919 | 0 | |
1920 | 0 | p = isl_printer_print_str(p, "args"); |
1921 | 0 | p = isl_printer_yaml_next(p); |
1922 | 0 | p = isl_printer_yaml_start_sequence(p); |
1923 | 0 | for (i = 0; i < n; ++i) { |
1924 | 0 | isl_ast_expr *arg; |
1925 | 0 |
|
1926 | 0 | arg = isl_ast_expr_get_op_arg(expr, i); |
1927 | 0 | p = print_ast_expr_isl(p, arg); |
1928 | 0 | isl_ast_expr_free(arg); |
1929 | 0 | p = isl_printer_yaml_next(p); |
1930 | 0 | } |
1931 | 0 | p = isl_printer_yaml_end_sequence(p); |
1932 | 0 |
|
1933 | 0 | return p; |
1934 | 0 | } |
1935 | | |
1936 | | /* Print "expr" to "p" in isl format. |
1937 | | * |
1938 | | * In particular, print the isl_ast_expr as a YAML document. |
1939 | | */ |
1940 | | static __isl_give isl_printer *print_ast_expr_isl(__isl_take isl_printer *p, |
1941 | | __isl_keep isl_ast_expr *expr) |
1942 | 0 | { |
1943 | 0 | enum isl_ast_expr_type type; |
1944 | 0 | enum isl_ast_op_type op; |
1945 | 0 | isl_id *id; |
1946 | 0 | isl_val *v; |
1947 | 0 |
|
1948 | 0 | if (!expr) |
1949 | 0 | return isl_printer_free(p); |
1950 | 0 | |
1951 | 0 | p = isl_printer_yaml_start_mapping(p); |
1952 | 0 | type = isl_ast_expr_get_type(expr); |
1953 | 0 | switch (type) { |
1954 | 0 | case isl_ast_expr_error: |
1955 | 0 | return isl_printer_free(p); |
1956 | 0 | case isl_ast_expr_op: |
1957 | 0 | op = isl_ast_expr_get_op_type(expr); |
1958 | 0 | if (op == isl_ast_op_error) |
1959 | 0 | return isl_printer_free(p); |
1960 | 0 | p = isl_printer_print_str(p, "op"); |
1961 | 0 | p = isl_printer_yaml_next(p); |
1962 | 0 | p = isl_printer_print_str(p, op_str[op]); |
1963 | 0 | p = isl_printer_yaml_next(p); |
1964 | 0 | p = print_arguments(p, expr); |
1965 | 0 | break; |
1966 | 0 | case isl_ast_expr_id: |
1967 | 0 | p = isl_printer_print_str(p, "id"); |
1968 | 0 | p = isl_printer_yaml_next(p); |
1969 | 0 | id = isl_ast_expr_get_id(expr); |
1970 | 0 | p = isl_printer_print_id(p, id); |
1971 | 0 | isl_id_free(id); |
1972 | 0 | break; |
1973 | 0 | case isl_ast_expr_int: |
1974 | 0 | p = isl_printer_print_str(p, "val"); |
1975 | 0 | p = isl_printer_yaml_next(p); |
1976 | 0 | v = isl_ast_expr_get_val(expr); |
1977 | 0 | p = isl_printer_print_val(p, v); |
1978 | 0 | isl_val_free(v); |
1979 | 0 | break; |
1980 | 0 | } |
1981 | 0 | p = isl_printer_yaml_end_mapping(p); |
1982 | 0 |
|
1983 | 0 | return p; |
1984 | 0 | } |
1985 | | |
1986 | | /* Print "expr" to "p". |
1987 | | * |
1988 | | * Only an isl and a C format are supported. |
1989 | | */ |
1990 | | __isl_give isl_printer *isl_printer_print_ast_expr(__isl_take isl_printer *p, |
1991 | | __isl_keep isl_ast_expr *expr) |
1992 | 3.42k | { |
1993 | 3.42k | int format; |
1994 | 3.42k | |
1995 | 3.42k | if (!p) |
1996 | 0 | return NULL; |
1997 | 3.42k | |
1998 | 3.42k | format = isl_printer_get_output_format(p); |
1999 | 3.42k | switch (format) { |
2000 | 3.42k | case 0 ISL_FORMAT_ISL0 : |
2001 | 0 | p = print_ast_expr_isl(p, expr); |
2002 | 0 | break; |
2003 | 3.42k | case ISL_FORMAT_C: |
2004 | 3.42k | p = print_ast_expr_c(p, expr); |
2005 | 3.42k | break; |
2006 | 3.42k | default: |
2007 | 0 | isl_die(isl_printer_get_ctx(p), isl_error_unsupported, |
2008 | 3.42k | "output format not supported for ast_expr", |
2009 | 3.42k | return isl_printer_free(p)); |
2010 | 3.42k | } |
2011 | 3.42k | |
2012 | 3.42k | return p; |
2013 | 3.42k | } |
2014 | | |
2015 | | static __isl_give isl_printer *print_ast_node_isl(__isl_take isl_printer *p, |
2016 | | __isl_keep isl_ast_node *node); |
2017 | | |
2018 | | /* Print a YAML sequence containing the entries in "list" to "p". |
2019 | | */ |
2020 | | static __isl_give isl_printer *print_ast_node_list(__isl_take isl_printer *p, |
2021 | | __isl_keep isl_ast_node_list *list) |
2022 | 0 | { |
2023 | 0 | int i, n; |
2024 | 0 |
|
2025 | 0 | n = isl_ast_node_list_n_ast_node(list); |
2026 | 0 | if (n < 0) |
2027 | 0 | return isl_printer_free(p); |
2028 | 0 | |
2029 | 0 | p = isl_printer_yaml_start_sequence(p); |
2030 | 0 | for (i = 0; i < n; ++i) { |
2031 | 0 | isl_ast_node *node; |
2032 | 0 |
|
2033 | 0 | node = isl_ast_node_list_get_ast_node(list, i); |
2034 | 0 | p = print_ast_node_isl(p, node); |
2035 | 0 | isl_ast_node_free(node); |
2036 | 0 | p = isl_printer_yaml_next(p); |
2037 | 0 | } |
2038 | 0 | p = isl_printer_yaml_end_sequence(p); |
2039 | 0 |
|
2040 | 0 | return p; |
2041 | 0 | } |
2042 | | |
2043 | | /* Print "node" to "p" in "isl format". |
2044 | | * |
2045 | | * In particular, print the isl_ast_node as a YAML document. |
2046 | | */ |
2047 | | static __isl_give isl_printer *print_ast_node_isl(__isl_take isl_printer *p, |
2048 | | __isl_keep isl_ast_node *node) |
2049 | 0 | { |
2050 | 0 | switch (node->type) { |
2051 | 0 | case isl_ast_node_for: |
2052 | 0 | p = isl_printer_yaml_start_mapping(p); |
2053 | 0 | p = isl_printer_print_str(p, "iterator"); |
2054 | 0 | p = isl_printer_yaml_next(p); |
2055 | 0 | p = isl_printer_print_ast_expr(p, node->u.f.iterator); |
2056 | 0 | p = isl_printer_yaml_next(p); |
2057 | 0 | if (node->u.f.degenerate) { |
2058 | 0 | p = isl_printer_print_str(p, "value"); |
2059 | 0 | p = isl_printer_yaml_next(p); |
2060 | 0 | p = isl_printer_print_ast_expr(p, node->u.f.init); |
2061 | 0 | p = isl_printer_yaml_next(p); |
2062 | 0 | } else { |
2063 | 0 | p = isl_printer_print_str(p, "init"); |
2064 | 0 | p = isl_printer_yaml_next(p); |
2065 | 0 | p = isl_printer_print_ast_expr(p, node->u.f.init); |
2066 | 0 | p = isl_printer_yaml_next(p); |
2067 | 0 | p = isl_printer_print_str(p, "cond"); |
2068 | 0 | p = isl_printer_yaml_next(p); |
2069 | 0 | p = isl_printer_print_ast_expr(p, node->u.f.cond); |
2070 | 0 | p = isl_printer_yaml_next(p); |
2071 | 0 | p = isl_printer_print_str(p, "inc"); |
2072 | 0 | p = isl_printer_yaml_next(p); |
2073 | 0 | p = isl_printer_print_ast_expr(p, node->u.f.inc); |
2074 | 0 | p = isl_printer_yaml_next(p); |
2075 | 0 | } |
2076 | 0 | if (node->u.f.body) { |
2077 | 0 | p = isl_printer_print_str(p, "body"); |
2078 | 0 | p = isl_printer_yaml_next(p); |
2079 | 0 | p = isl_printer_print_ast_node(p, node->u.f.body); |
2080 | 0 | p = isl_printer_yaml_next(p); |
2081 | 0 | } |
2082 | 0 | p = isl_printer_yaml_end_mapping(p); |
2083 | 0 | break; |
2084 | 0 | case isl_ast_node_mark: |
2085 | 0 | p = isl_printer_yaml_start_mapping(p); |
2086 | 0 | p = isl_printer_print_str(p, "mark"); |
2087 | 0 | p = isl_printer_yaml_next(p); |
2088 | 0 | p = isl_printer_print_id(p, node->u.m.mark); |
2089 | 0 | p = isl_printer_yaml_next(p); |
2090 | 0 | p = isl_printer_print_str(p, "node"); |
2091 | 0 | p = isl_printer_yaml_next(p); |
2092 | 0 | p = isl_printer_print_ast_node(p, node->u.m.node); |
2093 | 0 | p = isl_printer_yaml_end_mapping(p); |
2094 | 0 | break; |
2095 | 0 | case isl_ast_node_user: |
2096 | 0 | p = isl_printer_yaml_start_mapping(p); |
2097 | 0 | p = isl_printer_print_str(p, "user"); |
2098 | 0 | p = isl_printer_yaml_next(p); |
2099 | 0 | p = isl_printer_print_ast_expr(p, node->u.e.expr); |
2100 | 0 | p = isl_printer_yaml_end_mapping(p); |
2101 | 0 | break; |
2102 | 0 | case isl_ast_node_if: |
2103 | 0 | p = isl_printer_yaml_start_mapping(p); |
2104 | 0 | p = isl_printer_print_str(p, "guard"); |
2105 | 0 | p = isl_printer_yaml_next(p); |
2106 | 0 | p = isl_printer_print_ast_expr(p, node->u.i.guard); |
2107 | 0 | p = isl_printer_yaml_next(p); |
2108 | 0 | if (node->u.i.then) { |
2109 | 0 | p = isl_printer_print_str(p, "then"); |
2110 | 0 | p = isl_printer_yaml_next(p); |
2111 | 0 | p = isl_printer_print_ast_node(p, node->u.i.then); |
2112 | 0 | p = isl_printer_yaml_next(p); |
2113 | 0 | } |
2114 | 0 | if (node->u.i.else_node) { |
2115 | 0 | p = isl_printer_print_str(p, "else"); |
2116 | 0 | p = isl_printer_yaml_next(p); |
2117 | 0 | p = isl_printer_print_ast_node(p, node->u.i.else_node); |
2118 | 0 | } |
2119 | 0 | p = isl_printer_yaml_end_mapping(p); |
2120 | 0 | break; |
2121 | 0 | case isl_ast_node_block: |
2122 | 0 | p = print_ast_node_list(p, node->u.b.children); |
2123 | 0 | break; |
2124 | 0 | case isl_ast_node_error: |
2125 | 0 | break; |
2126 | 0 | } |
2127 | 0 | return p; |
2128 | 0 | } |
2129 | | |
2130 | | /* Do we need to print a block around the body "node" of a for or if node? |
2131 | | * |
2132 | | * If the node is a block, then we need to print a block. |
2133 | | * Also if the node is a degenerate for then we will print it as |
2134 | | * an assignment followed by the body of the for loop, so we need a block |
2135 | | * as well. |
2136 | | * If the node is an if node with an else, then we print a block |
2137 | | * to avoid spurious dangling else warnings emitted by some compilers. |
2138 | | * If the node is a mark, then in principle, we would have to check |
2139 | | * the child of the mark node. However, even if the child would not |
2140 | | * require us to print a block, for readability it is probably best |
2141 | | * to print a block anyway. |
2142 | | * If the ast_always_print_block option has been set, then we print a block. |
2143 | | */ |
2144 | | static int need_block(__isl_keep isl_ast_node *node) |
2145 | 566 | { |
2146 | 566 | isl_ctx *ctx; |
2147 | 566 | |
2148 | 566 | if (node->type == isl_ast_node_block) |
2149 | 129 | return 1; |
2150 | 437 | if (node->type == isl_ast_node_for && node->u.f.degenerate147 ) |
2151 | 0 | return 1; |
2152 | 437 | if (node->type == isl_ast_node_if && node->u.i.else_node9 ) |
2153 | 9 | return 1; |
2154 | 428 | if (node->type == isl_ast_node_mark) |
2155 | 73 | return 1; |
2156 | 355 | |
2157 | 355 | ctx = isl_ast_node_get_ctx(node); |
2158 | 355 | return isl_options_get_ast_always_print_block(ctx); |
2159 | 355 | } |
2160 | | |
2161 | | static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p, |
2162 | | __isl_keep isl_ast_node *node, |
2163 | | __isl_keep isl_ast_print_options *options, int in_block, int in_list); |
2164 | | static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p, |
2165 | | __isl_keep isl_ast_node *node, |
2166 | | __isl_keep isl_ast_print_options *options, int new_line, |
2167 | | int force_block); |
2168 | | |
2169 | | /* Print the body "node" of a for or if node. |
2170 | | * If "else_node" is set, then it is printed as well. |
2171 | | * If "force_block" is set, then print out the body as a block. |
2172 | | * |
2173 | | * We first check if we need to print out a block. |
2174 | | * We always print out a block if there is an else node to make |
2175 | | * sure that the else node is matched to the correct if node. |
2176 | | * For consistency, the corresponding else node is also printed as a block. |
2177 | | * |
2178 | | * If the else node is itself an if, then we print it as |
2179 | | * |
2180 | | * } else if (..) { |
2181 | | * } |
2182 | | * |
2183 | | * Otherwise the else node is printed as |
2184 | | * |
2185 | | * } else { |
2186 | | * node |
2187 | | * } |
2188 | | */ |
2189 | | static __isl_give isl_printer *print_body_c(__isl_take isl_printer *p, |
2190 | | __isl_keep isl_ast_node *node, __isl_keep isl_ast_node *else_node, |
2191 | | __isl_keep isl_ast_print_options *options, int force_block) |
2192 | 604 | { |
2193 | 604 | if (!node) |
2194 | 0 | return isl_printer_free(p); |
2195 | 604 | |
2196 | 604 | if (!force_block && !else_node584 && !need_block(node)566 ) { |
2197 | 355 | p = isl_printer_end_line(p); |
2198 | 355 | p = isl_printer_indent(p, 2); |
2199 | 355 | p = isl_ast_node_print(node, p, |
2200 | 355 | isl_ast_print_options_copy(options)); |
2201 | 355 | p = isl_printer_indent(p, -2); |
2202 | 355 | return p; |
2203 | 355 | } |
2204 | 249 | |
2205 | 249 | p = isl_printer_print_str(p, " {"); |
2206 | 249 | p = isl_printer_end_line(p); |
2207 | 249 | p = isl_printer_indent(p, 2); |
2208 | 249 | p = print_ast_node_c(p, node, options, 1, 0); |
2209 | 249 | p = isl_printer_indent(p, -2); |
2210 | 249 | p = isl_printer_start_line(p); |
2211 | 249 | p = isl_printer_print_str(p, "}"); |
2212 | 249 | if (else_node) { |
2213 | 20 | if (else_node->type == isl_ast_node_if) { |
2214 | 6 | p = isl_printer_print_str(p, " else "); |
2215 | 6 | p = print_if_c(p, else_node, options, 0, 1); |
2216 | 14 | } else { |
2217 | 14 | p = isl_printer_print_str(p, " else"); |
2218 | 14 | p = print_body_c(p, else_node, NULL, options, 1); |
2219 | 14 | } |
2220 | 20 | } else |
2221 | 229 | p = isl_printer_end_line(p); |
2222 | 249 | |
2223 | 249 | return p; |
2224 | 249 | } |
2225 | | |
2226 | | /* Print the start of a compound statement. |
2227 | | */ |
2228 | | static __isl_give isl_printer *start_block(__isl_take isl_printer *p) |
2229 | 64 | { |
2230 | 64 | p = isl_printer_start_line(p); |
2231 | 64 | p = isl_printer_print_str(p, "{"); |
2232 | 64 | p = isl_printer_end_line(p); |
2233 | 64 | p = isl_printer_indent(p, 2); |
2234 | 64 | |
2235 | 64 | return p; |
2236 | 64 | } |
2237 | | |
2238 | | /* Print the end of a compound statement. |
2239 | | */ |
2240 | | static __isl_give isl_printer *end_block(__isl_take isl_printer *p) |
2241 | 64 | { |
2242 | 64 | p = isl_printer_indent(p, -2); |
2243 | 64 | p = isl_printer_start_line(p); |
2244 | 64 | p = isl_printer_print_str(p, "}"); |
2245 | 64 | p = isl_printer_end_line(p); |
2246 | 64 | |
2247 | 64 | return p; |
2248 | 64 | } |
2249 | | |
2250 | | /* Print the for node "node". |
2251 | | * |
2252 | | * If the for node is degenerate, it is printed as |
2253 | | * |
2254 | | * type iterator = init; |
2255 | | * body |
2256 | | * |
2257 | | * Otherwise, it is printed as |
2258 | | * |
2259 | | * for (type iterator = init; cond; iterator += inc) |
2260 | | * body |
2261 | | * |
2262 | | * "in_block" is set if we are currently inside a block. |
2263 | | * "in_list" is set if the current node is not alone in the block. |
2264 | | * If we are not in a block or if the current not is not alone in the block |
2265 | | * then we print a block around a degenerate for loop such that the variable |
2266 | | * declaration will not conflict with any potential other declaration |
2267 | | * of the same variable. |
2268 | | */ |
2269 | | static __isl_give isl_printer *print_for_c(__isl_take isl_printer *p, |
2270 | | __isl_keep isl_ast_node *node, |
2271 | | __isl_keep isl_ast_print_options *options, int in_block, int in_list) |
2272 | 424 | { |
2273 | 424 | isl_id *id; |
2274 | 424 | const char *name; |
2275 | 424 | const char *type; |
2276 | 424 | |
2277 | 424 | type = isl_options_get_ast_iterator_type(isl_printer_get_ctx(p)); |
2278 | 424 | if (!node->u.f.degenerate) { |
2279 | 424 | id = isl_ast_expr_get_id(node->u.f.iterator); |
2280 | 424 | name = isl_id_get_name(id); |
2281 | 424 | isl_id_free(id); |
2282 | 424 | p = isl_printer_start_line(p); |
2283 | 424 | p = isl_printer_print_str(p, "for ("); |
2284 | 424 | p = isl_printer_print_str(p, type); |
2285 | 424 | p = isl_printer_print_str(p, " "); |
2286 | 424 | p = isl_printer_print_str(p, name); |
2287 | 424 | p = isl_printer_print_str(p, " = "); |
2288 | 424 | p = isl_printer_print_ast_expr(p, node->u.f.init); |
2289 | 424 | p = isl_printer_print_str(p, "; "); |
2290 | 424 | p = isl_printer_print_ast_expr(p, node->u.f.cond); |
2291 | 424 | p = isl_printer_print_str(p, "; "); |
2292 | 424 | p = isl_printer_print_str(p, name); |
2293 | 424 | p = isl_printer_print_str(p, " += "); |
2294 | 424 | p = isl_printer_print_ast_expr(p, node->u.f.inc); |
2295 | 424 | p = isl_printer_print_str(p, ")"); |
2296 | 424 | p = print_body_c(p, node->u.f.body, NULL, options, 0); |
2297 | 424 | } else { |
2298 | 0 | id = isl_ast_expr_get_id(node->u.f.iterator); |
2299 | 0 | name = isl_id_get_name(id); |
2300 | 0 | isl_id_free(id); |
2301 | 0 | if (!in_block || in_list) |
2302 | 0 | p = start_block(p); |
2303 | 0 | p = isl_printer_start_line(p); |
2304 | 0 | p = isl_printer_print_str(p, type); |
2305 | 0 | p = isl_printer_print_str(p, " "); |
2306 | 0 | p = isl_printer_print_str(p, name); |
2307 | 0 | p = isl_printer_print_str(p, " = "); |
2308 | 0 | p = isl_printer_print_ast_expr(p, node->u.f.init); |
2309 | 0 | p = isl_printer_print_str(p, ";"); |
2310 | 0 | p = isl_printer_end_line(p); |
2311 | 0 | p = print_ast_node_c(p, node->u.f.body, options, 1, 0); |
2312 | 0 | if (!in_block || in_list) |
2313 | 0 | p = end_block(p); |
2314 | 0 | } |
2315 | 424 | |
2316 | 424 | return p; |
2317 | 424 | } |
2318 | | |
2319 | | /* Print the if node "node". |
2320 | | * If "new_line" is set then the if node should be printed on a new line. |
2321 | | * If "force_block" is set, then print out the body as a block. |
2322 | | */ |
2323 | | static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p, |
2324 | | __isl_keep isl_ast_node *node, |
2325 | | __isl_keep isl_ast_print_options *options, int new_line, |
2326 | | int force_block) |
2327 | 166 | { |
2328 | 166 | if (new_line) |
2329 | 160 | p = isl_printer_start_line(p); |
2330 | 166 | p = isl_printer_print_str(p, "if ("); |
2331 | 166 | p = isl_printer_print_ast_expr(p, node->u.i.guard); |
2332 | 166 | p = isl_printer_print_str(p, ")"); |
2333 | 166 | p = print_body_c(p, node->u.i.then, node->u.i.else_node, options, |
2334 | 166 | force_block); |
2335 | 166 | |
2336 | 166 | return p; |
2337 | 166 | } |
2338 | | |
2339 | | /* Print the "node" to "p". |
2340 | | * |
2341 | | * "in_block" is set if we are currently inside a block. |
2342 | | * If so, we do not print a block around the children of a block node. |
2343 | | * We do this to avoid an extra block around the body of a degenerate |
2344 | | * for node. |
2345 | | * |
2346 | | * "in_list" is set if the current node is not alone in the block. |
2347 | | */ |
2348 | | static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p, |
2349 | | __isl_keep isl_ast_node *node, |
2350 | | __isl_keep isl_ast_print_options *options, int in_block, int in_list) |
2351 | 2.72k | { |
2352 | 2.72k | switch (node->type) { |
2353 | 2.72k | case isl_ast_node_for: |
2354 | 424 | if (options->print_for) |
2355 | 424 | return options->print_for(p, |
2356 | 424 | isl_ast_print_options_copy(options), |
2357 | 424 | node, options->print_for_user); |
2358 | 0 | p = print_for_c(p, node, options, in_block, in_list); |
2359 | 0 | break; |
2360 | 160 | case isl_ast_node_if: |
2361 | 160 | p = print_if_c(p, node, options, 1, 0); |
2362 | 160 | break; |
2363 | 276 | case isl_ast_node_block: |
2364 | 276 | if (!in_block) |
2365 | 64 | p = start_block(p); |
2366 | 276 | p = isl_ast_node_list_print(node->u.b.children, p, options); |
2367 | 276 | if (!in_block) |
2368 | 64 | p = end_block(p); |
2369 | 276 | break; |
2370 | 157 | case isl_ast_node_mark: |
2371 | 157 | p = isl_printer_start_line(p); |
2372 | 157 | p = isl_printer_print_str(p, "// "); |
2373 | 157 | p = isl_printer_print_str(p, isl_id_get_name(node->u.m.mark)); |
2374 | 157 | p = isl_printer_end_line(p); |
2375 | 157 | p = print_ast_node_c(p, node->u.m.node, options, 0, in_list); |
2376 | 157 | break; |
2377 | 1.70k | case isl_ast_node_user: |
2378 | 1.70k | if (options->print_user) |
2379 | 2 | return options->print_user(p, |
2380 | 2 | isl_ast_print_options_copy(options), |
2381 | 2 | node, options->print_user_user); |
2382 | 1.70k | p = isl_printer_start_line(p); |
2383 | 1.70k | p = isl_printer_print_ast_expr(p, node->u.e.expr); |
2384 | 1.70k | p = isl_printer_print_str(p, ";"); |
2385 | 1.70k | p = isl_printer_end_line(p); |
2386 | 1.70k | break; |
2387 | 1.70k | case isl_ast_node_error: |
2388 | 0 | break; |
2389 | 2.29k | } |
2390 | 2.29k | return p; |
2391 | 2.29k | } |
2392 | | |
2393 | | /* Print the for node "node" to "p". |
2394 | | */ |
2395 | | __isl_give isl_printer *isl_ast_node_for_print(__isl_keep isl_ast_node *node, |
2396 | | __isl_take isl_printer *p, __isl_take isl_ast_print_options *options) |
2397 | 424 | { |
2398 | 424 | if (!node || !options) |
2399 | 0 | goto error; |
2400 | 424 | if (node->type != isl_ast_node_for) |
2401 | 424 | isl_die0 (isl_ast_node_get_ctx(node), isl_error_invalid, |
2402 | 424 | "not a for node", goto error); |
2403 | 424 | p = print_for_c(p, node, options, 0, 0); |
2404 | 424 | isl_ast_print_options_free(options); |
2405 | 424 | return p; |
2406 | 0 | error: |
2407 | 0 | isl_ast_print_options_free(options); |
2408 | 0 | isl_printer_free(p); |
2409 | 0 | return NULL; |
2410 | 424 | } |
2411 | | |
2412 | | /* Print the if node "node" to "p". |
2413 | | */ |
2414 | | __isl_give isl_printer *isl_ast_node_if_print(__isl_keep isl_ast_node *node, |
2415 | | __isl_take isl_printer *p, __isl_take isl_ast_print_options *options) |
2416 | 0 | { |
2417 | 0 | if (!node || !options) |
2418 | 0 | goto error; |
2419 | 0 | if (node->type != isl_ast_node_if) |
2420 | 0 | isl_die(isl_ast_node_get_ctx(node), isl_error_invalid, |
2421 | 0 | "not an if node", goto error); |
2422 | 0 | p = print_if_c(p, node, options, 1, 0); |
2423 | 0 | isl_ast_print_options_free(options); |
2424 | 0 | return p; |
2425 | 0 | error: |
2426 | 0 | isl_ast_print_options_free(options); |
2427 | 0 | isl_printer_free(p); |
2428 | 0 | return NULL; |
2429 | 0 | } |
2430 | | |
2431 | | /* Print "node" to "p". |
2432 | | */ |
2433 | | __isl_give isl_printer *isl_ast_node_print(__isl_keep isl_ast_node *node, |
2434 | | __isl_take isl_printer *p, __isl_take isl_ast_print_options *options) |
2435 | 509 | { |
2436 | 509 | if (!options || !node) |
2437 | 0 | goto error; |
2438 | 509 | p = print_ast_node_c(p, node, options, 0, 0); |
2439 | 509 | isl_ast_print_options_free(options); |
2440 | 509 | return p; |
2441 | 0 | error: |
2442 | 0 | isl_ast_print_options_free(options); |
2443 | 0 | isl_printer_free(p); |
2444 | 0 | return NULL; |
2445 | 509 | } |
2446 | | |
2447 | | /* Print "node" to "p". |
2448 | | */ |
2449 | | __isl_give isl_printer *isl_printer_print_ast_node(__isl_take isl_printer *p, |
2450 | | __isl_keep isl_ast_node *node) |
2451 | 0 | { |
2452 | 0 | int format; |
2453 | 0 | isl_ast_print_options *options; |
2454 | 0 |
|
2455 | 0 | if (!p) |
2456 | 0 | return NULL; |
2457 | 0 | |
2458 | 0 | format = isl_printer_get_output_format(p); |
2459 | 0 | switch (format) { |
2460 | 0 | case ISL_FORMAT_ISL: |
2461 | 0 | p = print_ast_node_isl(p, node); |
2462 | 0 | break; |
2463 | 0 | case ISL_FORMAT_C: |
2464 | 0 | options = isl_ast_print_options_alloc(isl_printer_get_ctx(p)); |
2465 | 0 | p = isl_ast_node_print(node, p, options); |
2466 | 0 | break; |
2467 | 0 | default: |
2468 | 0 | isl_die(isl_printer_get_ctx(p), isl_error_unsupported, |
2469 | 0 | "output format not supported for ast_node", |
2470 | 0 | return isl_printer_free(p)); |
2471 | 0 | } |
2472 | 0 |
|
2473 | 0 | return p; |
2474 | 0 | } |
2475 | | |
2476 | | /* Print the list of nodes "list" to "p". |
2477 | | */ |
2478 | | __isl_give isl_printer *isl_ast_node_list_print( |
2479 | | __isl_keep isl_ast_node_list *list, __isl_take isl_printer *p, |
2480 | | __isl_keep isl_ast_print_options *options) |
2481 | 276 | { |
2482 | 276 | int i; |
2483 | 276 | |
2484 | 276 | if (!p || !list || !options) |
2485 | 0 | return isl_printer_free(p); |
2486 | 276 | |
2487 | 2.08k | for (i = 0; 276 i < list->n; ++i1.80k ) |
2488 | 1.80k | p = print_ast_node_c(p, list->p[i], options, 1, 1); |
2489 | 276 | |
2490 | 276 | return p; |
2491 | 276 | } |
2492 | | |
2493 | 0 | #define ISL_AST_MACRO_FLOORD (1 << 0) |
2494 | 0 | #define ISL_AST_MACRO_MIN (1 << 1) |
2495 | 0 | #define ISL_AST_MACRO_MAX (1 << 2) |
2496 | 0 | #define ISL_AST_MACRO_ALL (ISL_AST_MACRO_FLOORD | \ |
2497 | 0 | ISL_AST_MACRO_MIN | \ |
2498 | 0 | ISL_AST_MACRO_MAX) |
2499 | | |
2500 | | /* If "expr" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q |
2501 | | * then set the corresponding bit in "macros". |
2502 | | */ |
2503 | | static int ast_expr_required_macros(__isl_keep isl_ast_expr *expr, int macros) |
2504 | 0 | { |
2505 | 0 | int i; |
2506 | 0 |
|
2507 | 0 | if (macros == ISL_AST_MACRO_ALL) |
2508 | 0 | return macros; |
2509 | 0 | |
2510 | 0 | if (expr->type != isl_ast_expr_op) |
2511 | 0 | return macros; |
2512 | 0 | |
2513 | 0 | if (expr->u.op.op == isl_ast_op_min) |
2514 | 0 | macros |= ISL_AST_MACRO_MIN; |
2515 | 0 | if (expr->u.op.op == isl_ast_op_max) |
2516 | 0 | macros |= ISL_AST_MACRO_MAX; |
2517 | 0 | if (expr->u.op.op == isl_ast_op_fdiv_q) |
2518 | 0 | macros |= ISL_AST_MACRO_FLOORD; |
2519 | 0 |
|
2520 | 0 | for (i = 0; i < expr->u.op.n_arg; ++i) |
2521 | 0 | macros = ast_expr_required_macros(expr->u.op.args[i], macros); |
2522 | 0 |
|
2523 | 0 | return macros; |
2524 | 0 | } |
2525 | | |
2526 | | static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list, |
2527 | | int macros); |
2528 | | |
2529 | | /* If "node" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q |
2530 | | * then set the corresponding bit in "macros". |
2531 | | */ |
2532 | | static int ast_node_required_macros(__isl_keep isl_ast_node *node, int macros) |
2533 | 0 | { |
2534 | 0 | if (macros == ISL_AST_MACRO_ALL) |
2535 | 0 | return macros; |
2536 | 0 | |
2537 | 0 | switch (node->type) { |
2538 | 0 | case isl_ast_node_for: |
2539 | 0 | macros = ast_expr_required_macros(node->u.f.init, macros); |
2540 | 0 | if (!node->u.f.degenerate) { |
2541 | 0 | macros = ast_expr_required_macros(node->u.f.cond, |
2542 | 0 | macros); |
2543 | 0 | macros = ast_expr_required_macros(node->u.f.inc, |
2544 | 0 | macros); |
2545 | 0 | } |
2546 | 0 | macros = ast_node_required_macros(node->u.f.body, macros); |
2547 | 0 | break; |
2548 | 0 | case isl_ast_node_if: |
2549 | 0 | macros = ast_expr_required_macros(node->u.i.guard, macros); |
2550 | 0 | macros = ast_node_required_macros(node->u.i.then, macros); |
2551 | 0 | if (node->u.i.else_node) |
2552 | 0 | macros = ast_node_required_macros(node->u.i.else_node, |
2553 | 0 | macros); |
2554 | 0 | break; |
2555 | 0 | case isl_ast_node_block: |
2556 | 0 | macros = ast_node_list_required_macros(node->u.b.children, |
2557 | 0 | macros); |
2558 | 0 | break; |
2559 | 0 | case isl_ast_node_mark: |
2560 | 0 | macros = ast_node_required_macros(node->u.m.node, macros); |
2561 | 0 | break; |
2562 | 0 | case isl_ast_node_user: |
2563 | 0 | macros = ast_expr_required_macros(node->u.e.expr, macros); |
2564 | 0 | break; |
2565 | 0 | case isl_ast_node_error: |
2566 | 0 | break; |
2567 | 0 | } |
2568 | 0 | |
2569 | 0 | return macros; |
2570 | 0 | } |
2571 | | |
2572 | | /* If "list" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q |
2573 | | * then set the corresponding bit in "macros". |
2574 | | */ |
2575 | | static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list, |
2576 | | int macros) |
2577 | 0 | { |
2578 | 0 | int i; |
2579 | 0 |
|
2580 | 0 | for (i = 0; i < list->n; ++i) |
2581 | 0 | macros = ast_node_required_macros(list->p[i], macros); |
2582 | 0 |
|
2583 | 0 | return macros; |
2584 | 0 | } |
2585 | | |
2586 | | /* Data structure for keeping track of whether a macro definition |
2587 | | * for a given type has already been printed. |
2588 | | * The value is zero if no definition has been printed and non-zero otherwise. |
2589 | | */ |
2590 | | struct isl_ast_op_printed { |
2591 | | char printed[isl_ast_op_last + 1]; |
2592 | | }; |
2593 | | |
2594 | | /* Create an empty struct isl_ast_op_printed. |
2595 | | */ |
2596 | | static void *create_printed(isl_ctx *ctx) |
2597 | 0 | { |
2598 | 0 | return isl_calloc_type(ctx, struct isl_ast_op_printed); |
2599 | 0 | } |
2600 | | |
2601 | | /* Free a struct isl_ast_op_printed. |
2602 | | */ |
2603 | | static void free_printed(void *user) |
2604 | 0 | { |
2605 | 0 | free(user); |
2606 | 0 | } |
2607 | | |
2608 | | /* Ensure that "p" has an isl_ast_op_printed note identified by "id". |
2609 | | */ |
2610 | | static __isl_give isl_printer *alloc_printed(__isl_take isl_printer *p, |
2611 | | __isl_keep isl_id *id) |
2612 | 0 | { |
2613 | 0 | return alloc_note(p, id, &create_printed, &free_printed); |
2614 | 0 | } |
2615 | | |
2616 | | /* Create an identifier that is used to store |
2617 | | * an isl_ast_op_printed note. |
2618 | | */ |
2619 | | static __isl_give isl_id *printed_id(isl_ctx *ctx) |
2620 | 0 | { |
2621 | 0 | return isl_id_alloc(ctx, "isl_ast_op_type_printed", NULL); |
2622 | 0 | } |
2623 | | |
2624 | | /* Did the user specify that a macro definition should only be |
2625 | | * printed once and has a macro definition for "type" already |
2626 | | * been printed to "p"? |
2627 | | * If definitions should only be printed once, but a definition |
2628 | | * for "p" has not yet been printed, then mark it as having been |
2629 | | * printed so that it will not printed again. |
2630 | | * The actual printing is taken care of by the caller. |
2631 | | */ |
2632 | | static isl_bool already_printed_once(__isl_keep isl_printer *p, |
2633 | | enum isl_ast_op_type type) |
2634 | 0 | { |
2635 | 0 | isl_ctx *ctx; |
2636 | 0 | isl_id *id; |
2637 | 0 | struct isl_ast_op_printed *printed; |
2638 | 0 |
|
2639 | 0 | if (!p) |
2640 | 0 | return isl_bool_error; |
2641 | 0 | |
2642 | 0 | ctx = isl_printer_get_ctx(p); |
2643 | 0 | if (!isl_options_get_ast_print_macro_once(ctx)) |
2644 | 0 | return isl_bool_false; |
2645 | 0 | |
2646 | 0 | if (type > isl_ast_op_last) |
2647 | 0 | isl_die(isl_printer_get_ctx(p), isl_error_invalid, |
2648 | 0 | "invalid type", return isl_bool_error); |
2649 | 0 |
|
2650 | 0 | id = printed_id(isl_printer_get_ctx(p)); |
2651 | 0 | p = alloc_printed(p, id); |
2652 | 0 | printed = get_note(p, id); |
2653 | 0 | isl_id_free(id); |
2654 | 0 | if (!printed) |
2655 | 0 | return isl_bool_error; |
2656 | 0 | |
2657 | 0 | if (printed->printed[type]) |
2658 | 0 | return isl_bool_true; |
2659 | 0 | |
2660 | 0 | printed->printed[type] = 1; |
2661 | 0 | return isl_bool_false; |
2662 | 0 | } |
2663 | | |
2664 | | /* Print a macro definition for the operator "type". |
2665 | | * |
2666 | | * If the user has specified that a macro definition should |
2667 | | * only be printed once to any given printer and if the macro definition |
2668 | | * has already been printed to "p", then do not print the definition. |
2669 | | */ |
2670 | | __isl_give isl_printer *isl_ast_op_type_print_macro( |
2671 | | enum isl_ast_op_type type, __isl_take isl_printer *p) |
2672 | 0 | { |
2673 | 0 | isl_bool skip; |
2674 | 0 |
|
2675 | 0 | skip = already_printed_once(p, type); |
2676 | 0 | if (skip < 0) |
2677 | 0 | return isl_printer_free(p); |
2678 | 0 | if (skip) |
2679 | 0 | return p; |
2680 | 0 | |
2681 | 0 | switch (type) { |
2682 | 0 | case isl_ast_op_min: |
2683 | 0 | p = isl_printer_start_line(p); |
2684 | 0 | p = isl_printer_print_str(p, "#define "); |
2685 | 0 | p = isl_printer_print_str(p, get_op_str_c(p, type)); |
2686 | 0 | p = isl_printer_print_str(p, |
2687 | 0 | "(x,y) ((x) < (y) ? (x) : (y))"); |
2688 | 0 | p = isl_printer_end_line(p); |
2689 | 0 | break; |
2690 | 0 | case isl_ast_op_max: |
2691 | 0 | p = isl_printer_start_line(p); |
2692 | 0 | p = isl_printer_print_str(p, "#define "); |
2693 | 0 | p = isl_printer_print_str(p, get_op_str_c(p, type)); |
2694 | 0 | p = isl_printer_print_str(p, |
2695 | 0 | "(x,y) ((x) > (y) ? (x) : (y))"); |
2696 | 0 | p = isl_printer_end_line(p); |
2697 | 0 | break; |
2698 | 0 | case isl_ast_op_fdiv_q: |
2699 | 0 | p = isl_printer_start_line(p); |
2700 | 0 | p = isl_printer_print_str(p, "#define "); |
2701 | 0 | p = isl_printer_print_str(p, get_op_str_c(p, type)); |
2702 | 0 | p = isl_printer_print_str(p, |
2703 | 0 | "(n,d) " |
2704 | 0 | "(((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))"); |
2705 | 0 | p = isl_printer_end_line(p); |
2706 | 0 | break; |
2707 | 0 | default: |
2708 | 0 | break; |
2709 | 0 | } |
2710 | 0 | |
2711 | 0 | return p; |
2712 | 0 | } |
2713 | | |
2714 | | /* Call "fn" for each type of operation represented in the "macros" |
2715 | | * bit vector. |
2716 | | */ |
2717 | | static isl_stat foreach_ast_op_type(int macros, |
2718 | | isl_stat (*fn)(enum isl_ast_op_type type, void *user), void *user) |
2719 | 0 | { |
2720 | 0 | if (macros & ISL_AST_MACRO_MIN && fn(isl_ast_op_min, user) < 0) |
2721 | 0 | return isl_stat_error; |
2722 | 0 | if (macros & ISL_AST_MACRO_MAX && fn(isl_ast_op_max, user) < 0) |
2723 | 0 | return isl_stat_error; |
2724 | 0 | if (macros & ISL_AST_MACRO_FLOORD && fn(isl_ast_op_fdiv_q, user) < 0) |
2725 | 0 | return isl_stat_error; |
2726 | 0 | |
2727 | 0 | return isl_stat_ok; |
2728 | 0 | } |
2729 | | |
2730 | | /* Call "fn" for each type of operation that appears in "expr" |
2731 | | * and that requires a macro definition. |
2732 | | */ |
2733 | | isl_stat isl_ast_expr_foreach_ast_op_type(__isl_keep isl_ast_expr *expr, |
2734 | | isl_stat (*fn)(enum isl_ast_op_type type, void *user), void *user) |
2735 | 0 | { |
2736 | 0 | int macros; |
2737 | 0 |
|
2738 | 0 | if (!expr) |
2739 | 0 | return isl_stat_error; |
2740 | 0 | |
2741 | 0 | macros = ast_expr_required_macros(expr, 0); |
2742 | 0 | return foreach_ast_op_type(macros, fn, user); |
2743 | 0 | } |
2744 | | |
2745 | | /* Call "fn" for each type of operation that appears in "node" |
2746 | | * and that requires a macro definition. |
2747 | | */ |
2748 | | isl_stat isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node *node, |
2749 | | isl_stat (*fn)(enum isl_ast_op_type type, void *user), void *user) |
2750 | 0 | { |
2751 | 0 | int macros; |
2752 | 0 |
|
2753 | 0 | if (!node) |
2754 | 0 | return isl_stat_error; |
2755 | 0 | |
2756 | 0 | macros = ast_node_required_macros(node, 0); |
2757 | 0 | return foreach_ast_op_type(macros, fn, user); |
2758 | 0 | } |
2759 | | |
2760 | | static isl_stat ast_op_type_print_macro(enum isl_ast_op_type type, void *user) |
2761 | 0 | { |
2762 | 0 | isl_printer **p = user; |
2763 | 0 |
|
2764 | 0 | *p = isl_ast_op_type_print_macro(type, *p); |
2765 | 0 |
|
2766 | 0 | return isl_stat_ok; |
2767 | 0 | } |
2768 | | |
2769 | | /* Print macro definitions for all the macros used in the result |
2770 | | * of printing "expr". |
2771 | | */ |
2772 | | __isl_give isl_printer *isl_ast_expr_print_macros( |
2773 | | __isl_keep isl_ast_expr *expr, __isl_take isl_printer *p) |
2774 | 0 | { |
2775 | 0 | if (isl_ast_expr_foreach_ast_op_type(expr, |
2776 | 0 | &ast_op_type_print_macro, &p) < 0) |
2777 | 0 | return isl_printer_free(p); |
2778 | 0 | return p; |
2779 | 0 | } |
2780 | | |
2781 | | /* Print macro definitions for all the macros used in the result |
2782 | | * of printing "node". |
2783 | | */ |
2784 | | __isl_give isl_printer *isl_ast_node_print_macros( |
2785 | | __isl_keep isl_ast_node *node, __isl_take isl_printer *p) |
2786 | 0 | { |
2787 | 0 | if (isl_ast_node_foreach_ast_op_type(node, |
2788 | 0 | &ast_op_type_print_macro, &p) < 0) |
2789 | 0 | return isl_printer_free(p); |
2790 | 0 | return p; |
2791 | 0 | } |
2792 | | |
2793 | | /* Return a string containing C code representing this isl_ast_expr. |
2794 | | */ |
2795 | | __isl_give char *isl_ast_expr_to_C_str(__isl_keep isl_ast_expr *expr) |
2796 | 2 | { |
2797 | 2 | isl_printer *p; |
2798 | 2 | char *str; |
2799 | 2 | |
2800 | 2 | if (!expr) |
2801 | 0 | return NULL; |
2802 | 2 | |
2803 | 2 | p = isl_printer_to_str(isl_ast_expr_get_ctx(expr)); |
2804 | 2 | p = isl_printer_set_output_format(p, ISL_FORMAT_C); |
2805 | 2 | p = isl_printer_print_ast_expr(p, expr); |
2806 | 2 | |
2807 | 2 | str = isl_printer_get_str(p); |
2808 | 2 | |
2809 | 2 | isl_printer_free(p); |
2810 | 2 | |
2811 | 2 | return str; |
2812 | 2 | } |
2813 | | |
2814 | | /* Return a string containing C code representing this isl_ast_node. |
2815 | | */ |
2816 | | __isl_give char *isl_ast_node_to_C_str(__isl_keep isl_ast_node *node) |
2817 | 0 | { |
2818 | 0 | isl_printer *p; |
2819 | 0 | char *str; |
2820 | 0 |
|
2821 | 0 | if (!node) |
2822 | 0 | return NULL; |
2823 | 0 | |
2824 | 0 | p = isl_printer_to_str(isl_ast_node_get_ctx(node)); |
2825 | 0 | p = isl_printer_set_output_format(p, ISL_FORMAT_C); |
2826 | 0 | p = isl_printer_print_ast_node(p, node); |
2827 | 0 |
|
2828 | 0 | str = isl_printer_get_str(p); |
2829 | 0 |
|
2830 | 0 | isl_printer_free(p); |
2831 | 0 |
|
2832 | 0 | return str; |
2833 | 0 | } |