isl_space_match: return isl_bool
[isl.git] / isl_ast.c
blob0e0a982b4a596590604a0c52d34ca49eec164eab
1 /*
2 * Copyright 2012-2013 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
8 */
10 #include <string.h>
12 #include <isl_ast_private.h>
14 #undef BASE
15 #define BASE ast_expr
17 #include <isl_list_templ.c>
19 #undef BASE
20 #define BASE ast_node
22 #include <isl_list_templ.c>
24 isl_ctx *isl_ast_print_options_get_ctx(
25 __isl_keep isl_ast_print_options *options)
27 return options ? options->ctx : NULL;
30 __isl_give isl_ast_print_options *isl_ast_print_options_alloc(isl_ctx *ctx)
32 isl_ast_print_options *options;
34 options = isl_calloc_type(ctx, isl_ast_print_options);
35 if (!options)
36 return NULL;
38 options->ctx = ctx;
39 isl_ctx_ref(ctx);
40 options->ref = 1;
42 return options;
45 __isl_give isl_ast_print_options *isl_ast_print_options_dup(
46 __isl_keep isl_ast_print_options *options)
48 isl_ctx *ctx;
49 isl_ast_print_options *dup;
51 if (!options)
52 return NULL;
54 ctx = isl_ast_print_options_get_ctx(options);
55 dup = isl_ast_print_options_alloc(ctx);
56 if (!dup)
57 return NULL;
59 dup->print_for = options->print_for;
60 dup->print_for_user = options->print_for_user;
61 dup->print_user = options->print_user;
62 dup->print_user_user = options->print_user_user;
64 return dup;
67 __isl_give isl_ast_print_options *isl_ast_print_options_cow(
68 __isl_take isl_ast_print_options *options)
70 if (!options)
71 return NULL;
73 if (options->ref == 1)
74 return options;
75 options->ref--;
76 return isl_ast_print_options_dup(options);
79 __isl_give isl_ast_print_options *isl_ast_print_options_copy(
80 __isl_keep isl_ast_print_options *options)
82 if (!options)
83 return NULL;
85 options->ref++;
86 return options;
89 __isl_null isl_ast_print_options *isl_ast_print_options_free(
90 __isl_take isl_ast_print_options *options)
92 if (!options)
93 return NULL;
95 if (--options->ref > 0)
96 return NULL;
98 isl_ctx_deref(options->ctx);
100 free(options);
101 return NULL;
104 /* Set the print_user callback of "options" to "print_user".
106 * If this callback is set, then it used to print user nodes in the AST.
107 * Otherwise, the expression associated to the user node is printed.
109 __isl_give isl_ast_print_options *isl_ast_print_options_set_print_user(
110 __isl_take isl_ast_print_options *options,
111 __isl_give isl_printer *(*print_user)(__isl_take isl_printer *p,
112 __isl_take isl_ast_print_options *options,
113 __isl_keep isl_ast_node *node, void *user),
114 void *user)
116 options = isl_ast_print_options_cow(options);
117 if (!options)
118 return NULL;
120 options->print_user = print_user;
121 options->print_user_user = user;
123 return options;
126 /* Set the print_for callback of "options" to "print_for".
128 * If this callback is set, then it used to print for nodes in the AST.
130 __isl_give isl_ast_print_options *isl_ast_print_options_set_print_for(
131 __isl_take isl_ast_print_options *options,
132 __isl_give isl_printer *(*print_for)(__isl_take isl_printer *p,
133 __isl_take isl_ast_print_options *options,
134 __isl_keep isl_ast_node *node, void *user),
135 void *user)
137 options = isl_ast_print_options_cow(options);
138 if (!options)
139 return NULL;
141 options->print_for = print_for;
142 options->print_for_user = user;
144 return options;
147 __isl_give isl_ast_expr *isl_ast_expr_copy(__isl_keep isl_ast_expr *expr)
149 if (!expr)
150 return NULL;
152 expr->ref++;
153 return expr;
156 __isl_give isl_ast_expr *isl_ast_expr_dup(__isl_keep isl_ast_expr *expr)
158 int i;
159 isl_ctx *ctx;
160 isl_ast_expr *dup;
162 if (!expr)
163 return NULL;
165 ctx = isl_ast_expr_get_ctx(expr);
166 switch (expr->type) {
167 case isl_ast_expr_int:
168 dup = isl_ast_expr_from_val(isl_val_copy(expr->u.v));
169 break;
170 case isl_ast_expr_id:
171 dup = isl_ast_expr_from_id(isl_id_copy(expr->u.id));
172 break;
173 case isl_ast_expr_op:
174 dup = isl_ast_expr_alloc_op(ctx,
175 expr->u.op.op, expr->u.op.n_arg);
176 if (!dup)
177 return NULL;
178 for (i = 0; i < expr->u.op.n_arg; ++i)
179 dup->u.op.args[i] =
180 isl_ast_expr_copy(expr->u.op.args[i]);
181 break;
182 case isl_ast_expr_error:
183 dup = NULL;
186 if (!dup)
187 return NULL;
189 return dup;
192 __isl_give isl_ast_expr *isl_ast_expr_cow(__isl_take isl_ast_expr *expr)
194 if (!expr)
195 return NULL;
197 if (expr->ref == 1)
198 return expr;
199 expr->ref--;
200 return isl_ast_expr_dup(expr);
203 __isl_null isl_ast_expr *isl_ast_expr_free(__isl_take isl_ast_expr *expr)
205 int i;
207 if (!expr)
208 return NULL;
210 if (--expr->ref > 0)
211 return NULL;
213 isl_ctx_deref(expr->ctx);
215 switch (expr->type) {
216 case isl_ast_expr_int:
217 isl_val_free(expr->u.v);
218 break;
219 case isl_ast_expr_id:
220 isl_id_free(expr->u.id);
221 break;
222 case isl_ast_expr_op:
223 if (expr->u.op.args)
224 for (i = 0; i < expr->u.op.n_arg; ++i)
225 isl_ast_expr_free(expr->u.op.args[i]);
226 free(expr->u.op.args);
227 break;
228 case isl_ast_expr_error:
229 break;
232 free(expr);
233 return NULL;
236 isl_ctx *isl_ast_expr_get_ctx(__isl_keep isl_ast_expr *expr)
238 return expr ? expr->ctx : NULL;
241 enum isl_ast_expr_type isl_ast_expr_get_type(__isl_keep isl_ast_expr *expr)
243 return expr ? expr->type : isl_ast_expr_error;
246 /* Return the integer value represented by "expr".
248 __isl_give isl_val *isl_ast_expr_get_val(__isl_keep isl_ast_expr *expr)
250 if (!expr)
251 return NULL;
252 if (expr->type != isl_ast_expr_int)
253 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
254 "expression not an int", return NULL);
255 return isl_val_copy(expr->u.v);
258 __isl_give isl_id *isl_ast_expr_get_id(__isl_keep isl_ast_expr *expr)
260 if (!expr)
261 return NULL;
262 if (expr->type != isl_ast_expr_id)
263 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
264 "expression not an identifier", return NULL);
266 return isl_id_copy(expr->u.id);
269 enum isl_ast_op_type isl_ast_expr_get_op_type(__isl_keep isl_ast_expr *expr)
271 if (!expr)
272 return isl_ast_op_error;
273 if (expr->type != isl_ast_expr_op)
274 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
275 "expression not an operation", return isl_ast_op_error);
276 return expr->u.op.op;
279 int isl_ast_expr_get_op_n_arg(__isl_keep isl_ast_expr *expr)
281 if (!expr)
282 return -1;
283 if (expr->type != isl_ast_expr_op)
284 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
285 "expression not an operation", return -1);
286 return expr->u.op.n_arg;
289 __isl_give isl_ast_expr *isl_ast_expr_get_op_arg(__isl_keep isl_ast_expr *expr,
290 int pos)
292 if (!expr)
293 return NULL;
294 if (expr->type != isl_ast_expr_op)
295 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
296 "expression not an operation", return NULL);
297 if (pos < 0 || pos >= expr->u.op.n_arg)
298 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
299 "index out of bounds", return NULL);
301 return isl_ast_expr_copy(expr->u.op.args[pos]);
304 /* Replace the argument at position "pos" of "expr" by "arg".
306 __isl_give isl_ast_expr *isl_ast_expr_set_op_arg(__isl_take isl_ast_expr *expr,
307 int pos, __isl_take isl_ast_expr *arg)
309 expr = isl_ast_expr_cow(expr);
310 if (!expr || !arg)
311 goto error;
312 if (expr->type != isl_ast_expr_op)
313 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
314 "expression not an operation", goto error);
315 if (pos < 0 || pos >= expr->u.op.n_arg)
316 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
317 "index out of bounds", goto error);
319 isl_ast_expr_free(expr->u.op.args[pos]);
320 expr->u.op.args[pos] = arg;
322 return expr;
323 error:
324 isl_ast_expr_free(arg);
325 return isl_ast_expr_free(expr);
328 /* Is "expr1" equal to "expr2"?
330 isl_bool isl_ast_expr_is_equal(__isl_keep isl_ast_expr *expr1,
331 __isl_keep isl_ast_expr *expr2)
333 int i;
335 if (!expr1 || !expr2)
336 return isl_bool_error;
338 if (expr1 == expr2)
339 return isl_bool_true;
340 if (expr1->type != expr2->type)
341 return isl_bool_false;
342 switch (expr1->type) {
343 case isl_ast_expr_int:
344 return isl_val_eq(expr1->u.v, expr2->u.v);
345 case isl_ast_expr_id:
346 return expr1->u.id == expr2->u.id;
347 case isl_ast_expr_op:
348 if (expr1->u.op.op != expr2->u.op.op)
349 return isl_bool_false;
350 if (expr1->u.op.n_arg != expr2->u.op.n_arg)
351 return isl_bool_false;
352 for (i = 0; i < expr1->u.op.n_arg; ++i) {
353 isl_bool equal;
354 equal = isl_ast_expr_is_equal(expr1->u.op.args[i],
355 expr2->u.op.args[i]);
356 if (equal < 0 || !equal)
357 return equal;
359 return 1;
360 case isl_ast_expr_error:
361 return isl_bool_error;
364 isl_die(isl_ast_expr_get_ctx(expr1), isl_error_internal,
365 "unhandled case", return isl_bool_error);
368 /* Create a new operation expression of operation type "op",
369 * with "n_arg" as yet unspecified arguments.
371 __isl_give isl_ast_expr *isl_ast_expr_alloc_op(isl_ctx *ctx,
372 enum isl_ast_op_type op, int n_arg)
374 isl_ast_expr *expr;
376 expr = isl_calloc_type(ctx, isl_ast_expr);
377 if (!expr)
378 return NULL;
380 expr->ctx = ctx;
381 isl_ctx_ref(ctx);
382 expr->ref = 1;
383 expr->type = isl_ast_expr_op;
384 expr->u.op.op = op;
385 expr->u.op.n_arg = n_arg;
386 expr->u.op.args = isl_calloc_array(ctx, isl_ast_expr *, n_arg);
388 if (n_arg && !expr->u.op.args)
389 return isl_ast_expr_free(expr);
391 return expr;
394 /* Create a new id expression representing "id".
396 __isl_give isl_ast_expr *isl_ast_expr_from_id(__isl_take isl_id *id)
398 isl_ctx *ctx;
399 isl_ast_expr *expr;
401 if (!id)
402 return NULL;
404 ctx = isl_id_get_ctx(id);
405 expr = isl_calloc_type(ctx, isl_ast_expr);
406 if (!expr)
407 goto error;
409 expr->ctx = ctx;
410 isl_ctx_ref(ctx);
411 expr->ref = 1;
412 expr->type = isl_ast_expr_id;
413 expr->u.id = id;
415 return expr;
416 error:
417 isl_id_free(id);
418 return NULL;
421 /* Create a new integer expression representing "i".
423 __isl_give isl_ast_expr *isl_ast_expr_alloc_int_si(isl_ctx *ctx, int i)
425 isl_ast_expr *expr;
427 expr = isl_calloc_type(ctx, isl_ast_expr);
428 if (!expr)
429 return NULL;
431 expr->ctx = ctx;
432 isl_ctx_ref(ctx);
433 expr->ref = 1;
434 expr->type = isl_ast_expr_int;
435 expr->u.v = isl_val_int_from_si(ctx, i);
436 if (!expr->u.v)
437 return isl_ast_expr_free(expr);
439 return expr;
442 /* Create a new integer expression representing "v".
444 __isl_give isl_ast_expr *isl_ast_expr_from_val(__isl_take isl_val *v)
446 isl_ctx *ctx;
447 isl_ast_expr *expr;
449 if (!v)
450 return NULL;
451 if (!isl_val_is_int(v))
452 isl_die(isl_val_get_ctx(v), isl_error_invalid,
453 "expecting integer value", goto error);
455 ctx = isl_val_get_ctx(v);
456 expr = isl_calloc_type(ctx, isl_ast_expr);
457 if (!expr)
458 goto error;
460 expr->ctx = ctx;
461 isl_ctx_ref(ctx);
462 expr->ref = 1;
463 expr->type = isl_ast_expr_int;
464 expr->u.v = v;
466 return expr;
467 error:
468 isl_val_free(v);
469 return NULL;
472 /* Create an expression representing the unary operation "type" applied to
473 * "arg".
475 __isl_give isl_ast_expr *isl_ast_expr_alloc_unary(enum isl_ast_op_type type,
476 __isl_take isl_ast_expr *arg)
478 isl_ctx *ctx;
479 isl_ast_expr *expr = NULL;
481 if (!arg)
482 return NULL;
484 ctx = isl_ast_expr_get_ctx(arg);
485 expr = isl_ast_expr_alloc_op(ctx, type, 1);
486 if (!expr)
487 goto error;
489 expr->u.op.args[0] = arg;
491 return expr;
492 error:
493 isl_ast_expr_free(arg);
494 return NULL;
497 /* Create an expression representing the negation of "arg".
499 __isl_give isl_ast_expr *isl_ast_expr_neg(__isl_take isl_ast_expr *arg)
501 return isl_ast_expr_alloc_unary(isl_ast_op_minus, arg);
504 /* Create an expression representing the address of "expr".
506 __isl_give isl_ast_expr *isl_ast_expr_address_of(__isl_take isl_ast_expr *expr)
508 if (!expr)
509 return NULL;
511 if (isl_ast_expr_get_type(expr) != isl_ast_expr_op ||
512 isl_ast_expr_get_op_type(expr) != isl_ast_op_access)
513 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
514 "can only take address of access expressions",
515 return isl_ast_expr_free(expr));
517 return isl_ast_expr_alloc_unary(isl_ast_op_address_of, expr);
520 /* Create an expression representing the binary operation "type"
521 * applied to "expr1" and "expr2".
523 __isl_give isl_ast_expr *isl_ast_expr_alloc_binary(enum isl_ast_op_type type,
524 __isl_take isl_ast_expr *expr1, __isl_take isl_ast_expr *expr2)
526 isl_ctx *ctx;
527 isl_ast_expr *expr = NULL;
529 if (!expr1 || !expr2)
530 goto error;
532 ctx = isl_ast_expr_get_ctx(expr1);
533 expr = isl_ast_expr_alloc_op(ctx, type, 2);
534 if (!expr)
535 goto error;
537 expr->u.op.args[0] = expr1;
538 expr->u.op.args[1] = expr2;
540 return expr;
541 error:
542 isl_ast_expr_free(expr1);
543 isl_ast_expr_free(expr2);
544 return NULL;
547 /* Create an expression representing the sum of "expr1" and "expr2".
549 __isl_give isl_ast_expr *isl_ast_expr_add(__isl_take isl_ast_expr *expr1,
550 __isl_take isl_ast_expr *expr2)
552 return isl_ast_expr_alloc_binary(isl_ast_op_add, expr1, expr2);
555 /* Create an expression representing the difference of "expr1" and "expr2".
557 __isl_give isl_ast_expr *isl_ast_expr_sub(__isl_take isl_ast_expr *expr1,
558 __isl_take isl_ast_expr *expr2)
560 return isl_ast_expr_alloc_binary(isl_ast_op_sub, expr1, expr2);
563 /* Create an expression representing the product of "expr1" and "expr2".
565 __isl_give isl_ast_expr *isl_ast_expr_mul(__isl_take isl_ast_expr *expr1,
566 __isl_take isl_ast_expr *expr2)
568 return isl_ast_expr_alloc_binary(isl_ast_op_mul, expr1, expr2);
571 /* Create an expression representing the quotient of "expr1" and "expr2".
573 __isl_give isl_ast_expr *isl_ast_expr_div(__isl_take isl_ast_expr *expr1,
574 __isl_take isl_ast_expr *expr2)
576 return isl_ast_expr_alloc_binary(isl_ast_op_div, expr1, expr2);
579 /* Create an expression representing the quotient of the integer
580 * division of "expr1" by "expr2", where "expr1" is known to be
581 * non-negative.
583 __isl_give isl_ast_expr *isl_ast_expr_pdiv_q(__isl_take isl_ast_expr *expr1,
584 __isl_take isl_ast_expr *expr2)
586 return isl_ast_expr_alloc_binary(isl_ast_op_pdiv_q, expr1, expr2);
589 /* Create an expression representing the remainder of the integer
590 * division of "expr1" by "expr2", where "expr1" is known to be
591 * non-negative.
593 __isl_give isl_ast_expr *isl_ast_expr_pdiv_r(__isl_take isl_ast_expr *expr1,
594 __isl_take isl_ast_expr *expr2)
596 return isl_ast_expr_alloc_binary(isl_ast_op_pdiv_r, expr1, expr2);
599 /* Create an expression representing the conjunction of "expr1" and "expr2".
601 __isl_give isl_ast_expr *isl_ast_expr_and(__isl_take isl_ast_expr *expr1,
602 __isl_take isl_ast_expr *expr2)
604 return isl_ast_expr_alloc_binary(isl_ast_op_and, expr1, expr2);
607 /* Create an expression representing the conjunction of "expr1" and "expr2",
608 * where "expr2" is evaluated only if "expr1" is evaluated to true.
610 __isl_give isl_ast_expr *isl_ast_expr_and_then(__isl_take isl_ast_expr *expr1,
611 __isl_take isl_ast_expr *expr2)
613 return isl_ast_expr_alloc_binary(isl_ast_op_and_then, expr1, expr2);
616 /* Create an expression representing the disjunction of "expr1" and "expr2".
618 __isl_give isl_ast_expr *isl_ast_expr_or(__isl_take isl_ast_expr *expr1,
619 __isl_take isl_ast_expr *expr2)
621 return isl_ast_expr_alloc_binary(isl_ast_op_or, expr1, expr2);
624 /* Create an expression representing the disjunction of "expr1" and "expr2",
625 * where "expr2" is evaluated only if "expr1" is evaluated to false.
627 __isl_give isl_ast_expr *isl_ast_expr_or_else(__isl_take isl_ast_expr *expr1,
628 __isl_take isl_ast_expr *expr2)
630 return isl_ast_expr_alloc_binary(isl_ast_op_or_else, expr1, expr2);
633 /* Create an expression representing "expr1" less than or equal to "expr2".
635 __isl_give isl_ast_expr *isl_ast_expr_le(__isl_take isl_ast_expr *expr1,
636 __isl_take isl_ast_expr *expr2)
638 return isl_ast_expr_alloc_binary(isl_ast_op_le, expr1, expr2);
641 /* Create an expression representing "expr1" less than "expr2".
643 __isl_give isl_ast_expr *isl_ast_expr_lt(__isl_take isl_ast_expr *expr1,
644 __isl_take isl_ast_expr *expr2)
646 return isl_ast_expr_alloc_binary(isl_ast_op_lt, expr1, expr2);
649 /* Create an expression representing "expr1" greater than or equal to "expr2".
651 __isl_give isl_ast_expr *isl_ast_expr_ge(__isl_take isl_ast_expr *expr1,
652 __isl_take isl_ast_expr *expr2)
654 return isl_ast_expr_alloc_binary(isl_ast_op_ge, expr1, expr2);
657 /* Create an expression representing "expr1" greater than "expr2".
659 __isl_give isl_ast_expr *isl_ast_expr_gt(__isl_take isl_ast_expr *expr1,
660 __isl_take isl_ast_expr *expr2)
662 return isl_ast_expr_alloc_binary(isl_ast_op_gt, expr1, expr2);
665 /* Create an expression representing "expr1" equal to "expr2".
667 __isl_give isl_ast_expr *isl_ast_expr_eq(__isl_take isl_ast_expr *expr1,
668 __isl_take isl_ast_expr *expr2)
670 return isl_ast_expr_alloc_binary(isl_ast_op_eq, expr1, expr2);
673 /* Create an expression of type "type" with as arguments "arg0" followed
674 * by "arguments".
676 static __isl_give isl_ast_expr *ast_expr_with_arguments(
677 enum isl_ast_op_type type, __isl_take isl_ast_expr *arg0,
678 __isl_take isl_ast_expr_list *arguments)
680 int i, n;
681 isl_ctx *ctx;
682 isl_ast_expr *res = NULL;
684 if (!arg0 || !arguments)
685 goto error;
687 ctx = isl_ast_expr_get_ctx(arg0);
688 n = isl_ast_expr_list_n_ast_expr(arguments);
689 res = isl_ast_expr_alloc_op(ctx, type, 1 + n);
690 if (!res)
691 goto error;
692 for (i = 0; i < n; ++i) {
693 isl_ast_expr *arg;
694 arg = isl_ast_expr_list_get_ast_expr(arguments, i);
695 res->u.op.args[1 + i] = arg;
696 if (!arg)
697 goto error;
699 res->u.op.args[0] = arg0;
701 isl_ast_expr_list_free(arguments);
702 return res;
703 error:
704 isl_ast_expr_free(arg0);
705 isl_ast_expr_list_free(arguments);
706 isl_ast_expr_free(res);
707 return NULL;
710 /* Create an expression representing an access to "array" with index
711 * expressions "indices".
713 __isl_give isl_ast_expr *isl_ast_expr_access(__isl_take isl_ast_expr *array,
714 __isl_take isl_ast_expr_list *indices)
716 return ast_expr_with_arguments(isl_ast_op_access, array, indices);
719 /* Create an expression representing a call to "function" with argument
720 * expressions "arguments".
722 __isl_give isl_ast_expr *isl_ast_expr_call(__isl_take isl_ast_expr *function,
723 __isl_take isl_ast_expr_list *arguments)
725 return ast_expr_with_arguments(isl_ast_op_call, function, arguments);
728 /* For each subexpression of "expr" of type isl_ast_expr_id,
729 * if it appears in "id2expr", then replace it by the corresponding
730 * expression.
732 __isl_give isl_ast_expr *isl_ast_expr_substitute_ids(
733 __isl_take isl_ast_expr *expr, __isl_take isl_id_to_ast_expr *id2expr)
735 int i;
736 isl_maybe_isl_ast_expr m;
738 if (!expr || !id2expr)
739 goto error;
741 switch (expr->type) {
742 case isl_ast_expr_int:
743 break;
744 case isl_ast_expr_id:
745 m = isl_id_to_ast_expr_try_get(id2expr, expr->u.id);
746 if (m.valid < 0)
747 goto error;
748 if (!m.valid)
749 break;
750 isl_ast_expr_free(expr);
751 expr = m.value;
752 break;
753 case isl_ast_expr_op:
754 for (i = 0; i < expr->u.op.n_arg; ++i) {
755 isl_ast_expr *arg;
756 arg = isl_ast_expr_copy(expr->u.op.args[i]);
757 arg = isl_ast_expr_substitute_ids(arg,
758 isl_id_to_ast_expr_copy(id2expr));
759 if (arg == expr->u.op.args[i]) {
760 isl_ast_expr_free(arg);
761 continue;
763 if (!arg)
764 expr = isl_ast_expr_free(expr);
765 expr = isl_ast_expr_cow(expr);
766 if (!expr) {
767 isl_ast_expr_free(arg);
768 break;
770 isl_ast_expr_free(expr->u.op.args[i]);
771 expr->u.op.args[i] = arg;
773 break;
774 case isl_ast_expr_error:
775 expr = isl_ast_expr_free(expr);
776 break;
779 isl_id_to_ast_expr_free(id2expr);
780 return expr;
781 error:
782 isl_ast_expr_free(expr);
783 isl_id_to_ast_expr_free(id2expr);
784 return NULL;
787 isl_ctx *isl_ast_node_get_ctx(__isl_keep isl_ast_node *node)
789 return node ? node->ctx : NULL;
792 enum isl_ast_node_type isl_ast_node_get_type(__isl_keep isl_ast_node *node)
794 return node ? node->type : isl_ast_node_error;
797 __isl_give isl_ast_node *isl_ast_node_alloc(isl_ctx *ctx,
798 enum isl_ast_node_type type)
800 isl_ast_node *node;
802 node = isl_calloc_type(ctx, isl_ast_node);
803 if (!node)
804 return NULL;
806 node->ctx = ctx;
807 isl_ctx_ref(ctx);
808 node->ref = 1;
809 node->type = type;
811 return node;
814 /* Create an if node with the given guard.
816 * The then body needs to be filled in later.
818 __isl_give isl_ast_node *isl_ast_node_alloc_if(__isl_take isl_ast_expr *guard)
820 isl_ast_node *node;
822 if (!guard)
823 return NULL;
825 node = isl_ast_node_alloc(isl_ast_expr_get_ctx(guard), isl_ast_node_if);
826 if (!node)
827 goto error;
828 node->u.i.guard = guard;
830 return node;
831 error:
832 isl_ast_expr_free(guard);
833 return NULL;
836 /* Create a for node with the given iterator.
838 * The remaining fields need to be filled in later.
840 __isl_give isl_ast_node *isl_ast_node_alloc_for(__isl_take isl_id *id)
842 isl_ast_node *node;
843 isl_ctx *ctx;
845 if (!id)
846 return NULL;
848 ctx = isl_id_get_ctx(id);
849 node = isl_ast_node_alloc(ctx, isl_ast_node_for);
850 if (!node)
851 goto error;
853 node->u.f.iterator = isl_ast_expr_from_id(id);
854 if (!node->u.f.iterator)
855 return isl_ast_node_free(node);
857 return node;
858 error:
859 isl_id_free(id);
860 return NULL;
863 /* Create a mark node, marking "node" with "id".
865 __isl_give isl_ast_node *isl_ast_node_alloc_mark(__isl_take isl_id *id,
866 __isl_take isl_ast_node *node)
868 isl_ctx *ctx;
869 isl_ast_node *mark;
871 if (!id || !node)
872 goto error;
874 ctx = isl_id_get_ctx(id);
875 mark = isl_ast_node_alloc(ctx, isl_ast_node_mark);
876 if (!mark)
877 goto error;
879 mark->u.m.mark = id;
880 mark->u.m.node = node;
882 return mark;
883 error:
884 isl_id_free(id);
885 isl_ast_node_free(node);
886 return NULL;
889 /* Create a user node evaluating "expr".
891 __isl_give isl_ast_node *isl_ast_node_alloc_user(__isl_take isl_ast_expr *expr)
893 isl_ctx *ctx;
894 isl_ast_node *node;
896 if (!expr)
897 return NULL;
899 ctx = isl_ast_expr_get_ctx(expr);
900 node = isl_ast_node_alloc(ctx, isl_ast_node_user);
901 if (!node)
902 goto error;
904 node->u.e.expr = expr;
906 return node;
907 error:
908 isl_ast_expr_free(expr);
909 return NULL;
912 /* Create a block node with the given children.
914 __isl_give isl_ast_node *isl_ast_node_alloc_block(
915 __isl_take isl_ast_node_list *list)
917 isl_ast_node *node;
918 isl_ctx *ctx;
920 if (!list)
921 return NULL;
923 ctx = isl_ast_node_list_get_ctx(list);
924 node = isl_ast_node_alloc(ctx, isl_ast_node_block);
925 if (!node)
926 goto error;
928 node->u.b.children = list;
930 return node;
931 error:
932 isl_ast_node_list_free(list);
933 return NULL;
936 /* Represent the given list of nodes as a single node, either by
937 * extract the node from a single element list or by creating
938 * a block node with the list of nodes as children.
940 __isl_give isl_ast_node *isl_ast_node_from_ast_node_list(
941 __isl_take isl_ast_node_list *list)
943 isl_ast_node *node;
945 if (isl_ast_node_list_n_ast_node(list) != 1)
946 return isl_ast_node_alloc_block(list);
948 node = isl_ast_node_list_get_ast_node(list, 0);
949 isl_ast_node_list_free(list);
951 return node;
954 __isl_give isl_ast_node *isl_ast_node_copy(__isl_keep isl_ast_node *node)
956 if (!node)
957 return NULL;
959 node->ref++;
960 return node;
963 __isl_give isl_ast_node *isl_ast_node_dup(__isl_keep isl_ast_node *node)
965 isl_ast_node *dup;
967 if (!node)
968 return NULL;
970 dup = isl_ast_node_alloc(isl_ast_node_get_ctx(node), node->type);
971 if (!dup)
972 return NULL;
974 switch (node->type) {
975 case isl_ast_node_if:
976 dup->u.i.guard = isl_ast_expr_copy(node->u.i.guard);
977 dup->u.i.then = isl_ast_node_copy(node->u.i.then);
978 dup->u.i.else_node = isl_ast_node_copy(node->u.i.else_node);
979 if (!dup->u.i.guard || !dup->u.i.then ||
980 (node->u.i.else_node && !dup->u.i.else_node))
981 return isl_ast_node_free(dup);
982 break;
983 case isl_ast_node_for:
984 dup->u.f.iterator = isl_ast_expr_copy(node->u.f.iterator);
985 dup->u.f.init = isl_ast_expr_copy(node->u.f.init);
986 dup->u.f.cond = isl_ast_expr_copy(node->u.f.cond);
987 dup->u.f.inc = isl_ast_expr_copy(node->u.f.inc);
988 dup->u.f.body = isl_ast_node_copy(node->u.f.body);
989 if (!dup->u.f.iterator || !dup->u.f.init || !dup->u.f.cond ||
990 !dup->u.f.inc || !dup->u.f.body)
991 return isl_ast_node_free(dup);
992 break;
993 case isl_ast_node_block:
994 dup->u.b.children = isl_ast_node_list_copy(node->u.b.children);
995 if (!dup->u.b.children)
996 return isl_ast_node_free(dup);
997 break;
998 case isl_ast_node_mark:
999 dup->u.m.mark = isl_id_copy(node->u.m.mark);
1000 dup->u.m.node = isl_ast_node_copy(node->u.m.node);
1001 if (!dup->u.m.mark || !dup->u.m.node)
1002 return isl_ast_node_free(dup);
1003 break;
1004 case isl_ast_node_user:
1005 dup->u.e.expr = isl_ast_expr_copy(node->u.e.expr);
1006 if (!dup->u.e.expr)
1007 return isl_ast_node_free(dup);
1008 break;
1009 case isl_ast_node_error:
1010 break;
1013 return dup;
1016 __isl_give isl_ast_node *isl_ast_node_cow(__isl_take isl_ast_node *node)
1018 if (!node)
1019 return NULL;
1021 if (node->ref == 1)
1022 return node;
1023 node->ref--;
1024 return isl_ast_node_dup(node);
1027 __isl_null isl_ast_node *isl_ast_node_free(__isl_take isl_ast_node *node)
1029 if (!node)
1030 return NULL;
1032 if (--node->ref > 0)
1033 return NULL;
1035 switch (node->type) {
1036 case isl_ast_node_if:
1037 isl_ast_expr_free(node->u.i.guard);
1038 isl_ast_node_free(node->u.i.then);
1039 isl_ast_node_free(node->u.i.else_node);
1040 break;
1041 case isl_ast_node_for:
1042 isl_ast_expr_free(node->u.f.iterator);
1043 isl_ast_expr_free(node->u.f.init);
1044 isl_ast_expr_free(node->u.f.cond);
1045 isl_ast_expr_free(node->u.f.inc);
1046 isl_ast_node_free(node->u.f.body);
1047 break;
1048 case isl_ast_node_block:
1049 isl_ast_node_list_free(node->u.b.children);
1050 break;
1051 case isl_ast_node_mark:
1052 isl_id_free(node->u.m.mark);
1053 isl_ast_node_free(node->u.m.node);
1054 break;
1055 case isl_ast_node_user:
1056 isl_ast_expr_free(node->u.e.expr);
1057 break;
1058 case isl_ast_node_error:
1059 break;
1062 isl_id_free(node->annotation);
1063 isl_ctx_deref(node->ctx);
1064 free(node);
1066 return NULL;
1069 /* Replace the body of the for node "node" by "body".
1071 __isl_give isl_ast_node *isl_ast_node_for_set_body(
1072 __isl_take isl_ast_node *node, __isl_take isl_ast_node *body)
1074 node = isl_ast_node_cow(node);
1075 if (!node || !body)
1076 goto error;
1077 if (node->type != isl_ast_node_for)
1078 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1079 "not a for node", goto error);
1081 isl_ast_node_free(node->u.f.body);
1082 node->u.f.body = body;
1084 return node;
1085 error:
1086 isl_ast_node_free(node);
1087 isl_ast_node_free(body);
1088 return NULL;
1091 __isl_give isl_ast_node *isl_ast_node_for_get_body(
1092 __isl_keep isl_ast_node *node)
1094 if (!node)
1095 return NULL;
1096 if (node->type != isl_ast_node_for)
1097 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1098 "not a for node", return NULL);
1099 return isl_ast_node_copy(node->u.f.body);
1102 /* Mark the given for node as being degenerate.
1104 __isl_give isl_ast_node *isl_ast_node_for_mark_degenerate(
1105 __isl_take isl_ast_node *node)
1107 node = isl_ast_node_cow(node);
1108 if (!node)
1109 return NULL;
1110 node->u.f.degenerate = 1;
1111 return node;
1114 isl_bool isl_ast_node_for_is_degenerate(__isl_keep isl_ast_node *node)
1116 if (!node)
1117 return isl_bool_error;
1118 if (node->type != isl_ast_node_for)
1119 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1120 "not a for node", return isl_bool_error);
1121 return node->u.f.degenerate;
1124 __isl_give isl_ast_expr *isl_ast_node_for_get_iterator(
1125 __isl_keep isl_ast_node *node)
1127 if (!node)
1128 return NULL;
1129 if (node->type != isl_ast_node_for)
1130 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1131 "not a for node", return NULL);
1132 return isl_ast_expr_copy(node->u.f.iterator);
1135 __isl_give isl_ast_expr *isl_ast_node_for_get_init(
1136 __isl_keep isl_ast_node *node)
1138 if (!node)
1139 return NULL;
1140 if (node->type != isl_ast_node_for)
1141 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1142 "not a for node", return NULL);
1143 return isl_ast_expr_copy(node->u.f.init);
1146 /* Return the condition expression of the given for node.
1148 * If the for node is degenerate, then the condition is not explicitly
1149 * stored in the node. Instead, it is constructed as
1151 * iterator <= init
1153 __isl_give isl_ast_expr *isl_ast_node_for_get_cond(
1154 __isl_keep isl_ast_node *node)
1156 if (!node)
1157 return NULL;
1158 if (node->type != isl_ast_node_for)
1159 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1160 "not a for node", return NULL);
1161 if (!node->u.f.degenerate)
1162 return isl_ast_expr_copy(node->u.f.cond);
1164 return isl_ast_expr_alloc_binary(isl_ast_op_le,
1165 isl_ast_expr_copy(node->u.f.iterator),
1166 isl_ast_expr_copy(node->u.f.init));
1169 /* Return the increment of the given for node.
1171 * If the for node is degenerate, then the increment is not explicitly
1172 * stored in the node. We simply return "1".
1174 __isl_give isl_ast_expr *isl_ast_node_for_get_inc(
1175 __isl_keep isl_ast_node *node)
1177 if (!node)
1178 return NULL;
1179 if (node->type != isl_ast_node_for)
1180 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1181 "not a for node", return NULL);
1182 if (!node->u.f.degenerate)
1183 return isl_ast_expr_copy(node->u.f.inc);
1184 return isl_ast_expr_alloc_int_si(isl_ast_node_get_ctx(node), 1);
1187 /* Replace the then branch of the if node "node" by "child".
1189 __isl_give isl_ast_node *isl_ast_node_if_set_then(
1190 __isl_take isl_ast_node *node, __isl_take isl_ast_node *child)
1192 node = isl_ast_node_cow(node);
1193 if (!node || !child)
1194 goto error;
1195 if (node->type != isl_ast_node_if)
1196 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1197 "not an if node", goto error);
1199 isl_ast_node_free(node->u.i.then);
1200 node->u.i.then = child;
1202 return node;
1203 error:
1204 isl_ast_node_free(node);
1205 isl_ast_node_free(child);
1206 return NULL;
1209 __isl_give isl_ast_node *isl_ast_node_if_get_then(
1210 __isl_keep isl_ast_node *node)
1212 if (!node)
1213 return NULL;
1214 if (node->type != isl_ast_node_if)
1215 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1216 "not an if node", return NULL);
1217 return isl_ast_node_copy(node->u.i.then);
1220 isl_bool isl_ast_node_if_has_else(
1221 __isl_keep isl_ast_node *node)
1223 if (!node)
1224 return isl_bool_error;
1225 if (node->type != isl_ast_node_if)
1226 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1227 "not an if node", return isl_bool_error);
1228 return node->u.i.else_node != NULL;
1231 __isl_give isl_ast_node *isl_ast_node_if_get_else(
1232 __isl_keep isl_ast_node *node)
1234 if (!node)
1235 return NULL;
1236 if (node->type != isl_ast_node_if)
1237 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1238 "not an if node", return NULL);
1239 return isl_ast_node_copy(node->u.i.else_node);
1242 __isl_give isl_ast_expr *isl_ast_node_if_get_cond(
1243 __isl_keep isl_ast_node *node)
1245 if (!node)
1246 return NULL;
1247 if (node->type != isl_ast_node_if)
1248 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1249 "not a guard node", return NULL);
1250 return isl_ast_expr_copy(node->u.i.guard);
1253 __isl_give isl_ast_node_list *isl_ast_node_block_get_children(
1254 __isl_keep isl_ast_node *node)
1256 if (!node)
1257 return NULL;
1258 if (node->type != isl_ast_node_block)
1259 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1260 "not a block node", return NULL);
1261 return isl_ast_node_list_copy(node->u.b.children);
1264 __isl_give isl_ast_expr *isl_ast_node_user_get_expr(
1265 __isl_keep isl_ast_node *node)
1267 if (!node)
1268 return NULL;
1269 if (node->type != isl_ast_node_user)
1270 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1271 "not a user node", return NULL);
1273 return isl_ast_expr_copy(node->u.e.expr);
1276 /* Return the mark identifier of the mark node "node".
1278 __isl_give isl_id *isl_ast_node_mark_get_id(__isl_keep isl_ast_node *node)
1280 if (!node)
1281 return NULL;
1282 if (node->type != isl_ast_node_mark)
1283 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1284 "not a mark node", return NULL);
1286 return isl_id_copy(node->u.m.mark);
1289 /* Return the node marked by mark node "node".
1291 __isl_give isl_ast_node *isl_ast_node_mark_get_node(
1292 __isl_keep isl_ast_node *node)
1294 if (!node)
1295 return NULL;
1296 if (node->type != isl_ast_node_mark)
1297 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1298 "not a mark node", return NULL);
1300 return isl_ast_node_copy(node->u.m.node);
1303 __isl_give isl_id *isl_ast_node_get_annotation(__isl_keep isl_ast_node *node)
1305 return node ? isl_id_copy(node->annotation) : NULL;
1308 /* Replace node->annotation by "annotation".
1310 __isl_give isl_ast_node *isl_ast_node_set_annotation(
1311 __isl_take isl_ast_node *node, __isl_take isl_id *annotation)
1313 node = isl_ast_node_cow(node);
1314 if (!node || !annotation)
1315 goto error;
1317 isl_id_free(node->annotation);
1318 node->annotation = annotation;
1320 return node;
1321 error:
1322 isl_id_free(annotation);
1323 return isl_ast_node_free(node);
1326 /* Traverse the elements of "list" and all their descendants
1327 * in depth first preorder.
1329 * Return isl_stat_ok on success and isl_stat_error on failure.
1331 static isl_stat nodelist_foreach(__isl_keep isl_ast_node_list *list,
1332 isl_bool (*fn)(__isl_keep isl_ast_node *node, void *user), void *user)
1334 int i;
1336 if (!list)
1337 return isl_stat_error;
1339 for (i = 0; i < list->n; ++i) {
1340 isl_stat ok;
1341 isl_ast_node *node = list->p[i];
1343 ok = isl_ast_node_foreach_descendant_top_down(node, fn, user);
1344 if (ok < 0)
1345 return isl_stat_error;
1348 return isl_stat_ok;
1351 /* Traverse the descendants of "node" (including the node itself)
1352 * in depth first preorder.
1354 * If "fn" returns isl_bool_error on any of the nodes, then the traversal
1355 * is aborted.
1356 * If "fn" returns isl_bool_false on any of the nodes, then the subtree rooted
1357 * at that node is skipped.
1359 * Return isl_stat_ok on success and isl_stat_error on failure.
1361 isl_stat isl_ast_node_foreach_descendant_top_down(
1362 __isl_keep isl_ast_node *node,
1363 isl_bool (*fn)(__isl_keep isl_ast_node *node, void *user), void *user)
1365 isl_bool more;
1366 isl_stat ok;
1368 if (!node)
1369 return isl_stat_error;
1371 more = fn(node, user);
1372 if (more < 0)
1373 return isl_stat_error;
1374 if (!more)
1375 return isl_stat_ok;
1377 switch (node->type) {
1378 case isl_ast_node_for:
1379 node = node->u.f.body;
1380 return isl_ast_node_foreach_descendant_top_down(node, fn, user);
1381 case isl_ast_node_if:
1382 ok = isl_ast_node_foreach_descendant_top_down(node->u.i.then,
1383 fn, user);
1384 if (ok < 0)
1385 return isl_stat_error;
1386 if (!node->u.i.else_node)
1387 return isl_stat_ok;
1388 node = node->u.i.else_node;
1389 return isl_ast_node_foreach_descendant_top_down(node, fn, user);
1390 case isl_ast_node_block:
1391 return nodelist_foreach(node->u.b.children, fn, user);
1392 case isl_ast_node_mark:
1393 node = node->u.m.node;
1394 return isl_ast_node_foreach_descendant_top_down(node, fn, user);
1395 case isl_ast_node_user:
1396 break;
1397 case isl_ast_node_error:
1398 return isl_stat_error;
1401 return isl_stat_ok;
1404 /* Textual C representation of the various operators.
1406 static char *op_str[] = {
1407 [isl_ast_op_and] = "&&",
1408 [isl_ast_op_and_then] = "&&",
1409 [isl_ast_op_or] = "||",
1410 [isl_ast_op_or_else] = "||",
1411 [isl_ast_op_max] = "max",
1412 [isl_ast_op_min] = "min",
1413 [isl_ast_op_minus] = "-",
1414 [isl_ast_op_add] = "+",
1415 [isl_ast_op_sub] = "-",
1416 [isl_ast_op_mul] = "*",
1417 [isl_ast_op_fdiv_q] = "floord",
1418 [isl_ast_op_pdiv_q] = "/",
1419 [isl_ast_op_pdiv_r] = "%",
1420 [isl_ast_op_zdiv_r] = "%",
1421 [isl_ast_op_div] = "/",
1422 [isl_ast_op_eq] = "==",
1423 [isl_ast_op_le] = "<=",
1424 [isl_ast_op_ge] = ">=",
1425 [isl_ast_op_lt] = "<",
1426 [isl_ast_op_gt] = ">",
1427 [isl_ast_op_member] = ".",
1428 [isl_ast_op_address_of] = "&"
1431 /* Precedence in C of the various operators.
1432 * Based on http://en.wikipedia.org/wiki/Operators_in_C_and_C++
1433 * Lowest value means highest precedence.
1435 static int op_prec[] = {
1436 [isl_ast_op_and] = 13,
1437 [isl_ast_op_and_then] = 13,
1438 [isl_ast_op_or] = 14,
1439 [isl_ast_op_or_else] = 14,
1440 [isl_ast_op_max] = 2,
1441 [isl_ast_op_min] = 2,
1442 [isl_ast_op_minus] = 3,
1443 [isl_ast_op_add] = 6,
1444 [isl_ast_op_sub] = 6,
1445 [isl_ast_op_mul] = 5,
1446 [isl_ast_op_div] = 5,
1447 [isl_ast_op_fdiv_q] = 2,
1448 [isl_ast_op_pdiv_q] = 5,
1449 [isl_ast_op_pdiv_r] = 5,
1450 [isl_ast_op_zdiv_r] = 5,
1451 [isl_ast_op_cond] = 15,
1452 [isl_ast_op_select] = 15,
1453 [isl_ast_op_eq] = 9,
1454 [isl_ast_op_le] = 8,
1455 [isl_ast_op_ge] = 8,
1456 [isl_ast_op_lt] = 8,
1457 [isl_ast_op_gt] = 8,
1458 [isl_ast_op_call] = 2,
1459 [isl_ast_op_access] = 2,
1460 [isl_ast_op_member] = 2,
1461 [isl_ast_op_address_of] = 3
1464 /* Is the operator left-to-right associative?
1466 static int op_left[] = {
1467 [isl_ast_op_and] = 1,
1468 [isl_ast_op_and_then] = 1,
1469 [isl_ast_op_or] = 1,
1470 [isl_ast_op_or_else] = 1,
1471 [isl_ast_op_max] = 1,
1472 [isl_ast_op_min] = 1,
1473 [isl_ast_op_minus] = 0,
1474 [isl_ast_op_add] = 1,
1475 [isl_ast_op_sub] = 1,
1476 [isl_ast_op_mul] = 1,
1477 [isl_ast_op_div] = 1,
1478 [isl_ast_op_fdiv_q] = 1,
1479 [isl_ast_op_pdiv_q] = 1,
1480 [isl_ast_op_pdiv_r] = 1,
1481 [isl_ast_op_zdiv_r] = 1,
1482 [isl_ast_op_cond] = 0,
1483 [isl_ast_op_select] = 0,
1484 [isl_ast_op_eq] = 1,
1485 [isl_ast_op_le] = 1,
1486 [isl_ast_op_ge] = 1,
1487 [isl_ast_op_lt] = 1,
1488 [isl_ast_op_gt] = 1,
1489 [isl_ast_op_call] = 1,
1490 [isl_ast_op_access] = 1,
1491 [isl_ast_op_member] = 1,
1492 [isl_ast_op_address_of] = 0
1495 static int is_and(enum isl_ast_op_type op)
1497 return op == isl_ast_op_and || op == isl_ast_op_and_then;
1500 static int is_or(enum isl_ast_op_type op)
1502 return op == isl_ast_op_or || op == isl_ast_op_or_else;
1505 static int is_add_sub(enum isl_ast_op_type op)
1507 return op == isl_ast_op_add || op == isl_ast_op_sub;
1510 static int is_div_mod(enum isl_ast_op_type op)
1512 return op == isl_ast_op_div ||
1513 op == isl_ast_op_pdiv_r ||
1514 op == isl_ast_op_zdiv_r;
1517 /* Do we need/want parentheses around "expr" as a subexpression of
1518 * an "op" operation? If "left" is set, then "expr" is the left-most
1519 * operand.
1521 * We only need parentheses if "expr" represents an operation.
1523 * If op has a higher precedence than expr->u.op.op, then we need
1524 * parentheses.
1525 * If op and expr->u.op.op have the same precedence, but the operations
1526 * are performed in an order that is different from the associativity,
1527 * then we need parentheses.
1529 * An and inside an or technically does not require parentheses,
1530 * but some compilers complain about that, so we add them anyway.
1532 * Computations such as "a / b * c" and "a % b + c" can be somewhat
1533 * difficult to read, so we add parentheses for those as well.
1535 static int sub_expr_need_parens(enum isl_ast_op_type op,
1536 __isl_keep isl_ast_expr *expr, int left)
1538 if (expr->type != isl_ast_expr_op)
1539 return 0;
1541 if (op_prec[expr->u.op.op] > op_prec[op])
1542 return 1;
1543 if (op_prec[expr->u.op.op] == op_prec[op] && left != op_left[op])
1544 return 1;
1546 if (is_or(op) && is_and(expr->u.op.op))
1547 return 1;
1548 if (op == isl_ast_op_mul && expr->u.op.op != isl_ast_op_mul &&
1549 op_prec[expr->u.op.op] == op_prec[op])
1550 return 1;
1551 if (is_add_sub(op) && is_div_mod(expr->u.op.op))
1552 return 1;
1554 return 0;
1557 /* Print "expr" as a subexpression of an "op" operation.
1558 * If "left" is set, then "expr" is the left-most operand.
1560 static __isl_give isl_printer *print_sub_expr(__isl_take isl_printer *p,
1561 enum isl_ast_op_type op, __isl_keep isl_ast_expr *expr, int left)
1563 int need_parens;
1565 need_parens = sub_expr_need_parens(op, expr, left);
1567 if (need_parens)
1568 p = isl_printer_print_str(p, "(");
1569 p = isl_printer_print_ast_expr(p, expr);
1570 if (need_parens)
1571 p = isl_printer_print_str(p, ")");
1572 return p;
1575 #define isl_ast_op_last isl_ast_op_address_of
1577 /* Data structure that holds the user-specified textual
1578 * representations for the operators.
1579 * The entries are either NULL or copies of strings.
1580 * A NULL entry means that the default name should be used.
1582 struct isl_ast_op_names {
1583 char *op_str[isl_ast_op_last + 1];
1586 /* Create an empty struct isl_ast_op_names.
1588 static void *create_names(isl_ctx *ctx)
1590 return isl_calloc_type(ctx, struct isl_ast_op_names);
1593 /* Free a struct isl_ast_op_names along with all memory
1594 * owned by the struct.
1596 static void free_names(void *user)
1598 int i;
1599 struct isl_ast_op_names *names = user;
1601 if (!user)
1602 return;
1604 for (i = 0; i <= isl_ast_op_last; ++i)
1605 free(names->op_str[i]);
1606 free(user);
1609 /* Create an identifier that is used to store
1610 * an isl_ast_op_names note.
1612 static __isl_give isl_id *names_id(isl_ctx *ctx)
1614 return isl_id_alloc(ctx, "isl_ast_op_type_names", NULL);
1617 /* Ensure that "p" has a note identified by "id".
1618 * If there is no such note yet, then it is created by "note_create" and
1619 * scheduled do be freed by "note_free".
1621 static __isl_give isl_printer *alloc_note(__isl_take isl_printer *p,
1622 __isl_keep isl_id *id, void *(*note_create)(isl_ctx *),
1623 void (*note_free)(void *))
1625 isl_ctx *ctx;
1626 isl_id *note_id;
1627 isl_bool has_note;
1628 void *note;
1630 has_note = isl_printer_has_note(p, id);
1631 if (has_note < 0)
1632 return isl_printer_free(p);
1633 if (has_note)
1634 return p;
1636 ctx = isl_printer_get_ctx(p);
1637 note = note_create(ctx);
1638 if (!note)
1639 return isl_printer_free(p);
1640 note_id = isl_id_alloc(ctx, NULL, note);
1641 if (!note_id)
1642 note_free(note);
1643 else
1644 note_id = isl_id_set_free_user(note_id, note_free);
1646 p = isl_printer_set_note(p, isl_id_copy(id), note_id);
1648 return p;
1651 /* Ensure that "p" has an isl_ast_op_names note identified by "id".
1653 static __isl_give isl_printer *alloc_names(__isl_take isl_printer *p,
1654 __isl_keep isl_id *id)
1656 return alloc_note(p, id, &create_names, &free_names);
1659 /* Retrieve the note identified by "id" from "p".
1660 * The note is assumed to exist.
1662 static void *get_note(__isl_keep isl_printer *p, __isl_keep isl_id *id)
1664 void *note;
1666 id = isl_printer_get_note(p, isl_id_copy(id));
1667 note = isl_id_get_user(id);
1668 isl_id_free(id);
1670 return note;
1673 /* Use "name" to print operations of type "type" to "p".
1675 * Store the name in an isl_ast_op_names note attached to "p", such that
1676 * it can be retrieved by get_op_str.
1678 __isl_give isl_printer *isl_ast_op_type_set_print_name(
1679 __isl_take isl_printer *p, enum isl_ast_op_type type,
1680 __isl_keep const char *name)
1682 isl_id *id;
1683 struct isl_ast_op_names *names;
1685 if (!p)
1686 return NULL;
1687 if (type > isl_ast_op_last)
1688 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
1689 "invalid type", return isl_printer_free(p));
1691 id = names_id(isl_printer_get_ctx(p));
1692 p = alloc_names(p, id);
1693 names = get_note(p, id);
1694 isl_id_free(id);
1695 if (!names)
1696 return isl_printer_free(p);
1697 free(names->op_str[type]);
1698 names->op_str[type] = strdup(name);
1700 return p;
1703 /* Return the textual representation of "type".
1705 * If there is a user-specified name in an isl_ast_op_names note
1706 * associated to "p", then return that.
1707 * Otherwise, return the default name in op_str.
1709 static const char *get_op_str(__isl_keep isl_printer *p,
1710 enum isl_ast_op_type type)
1712 isl_id *id;
1713 isl_bool has_names;
1714 struct isl_ast_op_names *names = NULL;
1716 id = names_id(isl_printer_get_ctx(p));
1717 has_names = isl_printer_has_note(p, id);
1718 if (has_names >= 0 && has_names)
1719 names = get_note(p, id);
1720 isl_id_free(id);
1721 if (names && names->op_str[type])
1722 return names->op_str[type];
1723 return op_str[type];
1726 /* Print a min or max reduction "expr".
1728 static __isl_give isl_printer *print_min_max(__isl_take isl_printer *p,
1729 __isl_keep isl_ast_expr *expr)
1731 int i = 0;
1733 for (i = 1; i < expr->u.op.n_arg; ++i) {
1734 p = isl_printer_print_str(p, get_op_str(p, expr->u.op.op));
1735 p = isl_printer_print_str(p, "(");
1737 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1738 for (i = 1; i < expr->u.op.n_arg; ++i) {
1739 p = isl_printer_print_str(p, ", ");
1740 p = isl_printer_print_ast_expr(p, expr->u.op.args[i]);
1741 p = isl_printer_print_str(p, ")");
1744 return p;
1747 /* Print a function call "expr".
1749 * The first argument represents the function to be called.
1751 static __isl_give isl_printer *print_call(__isl_take isl_printer *p,
1752 __isl_keep isl_ast_expr *expr)
1754 int i = 0;
1756 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1757 p = isl_printer_print_str(p, "(");
1758 for (i = 1; i < expr->u.op.n_arg; ++i) {
1759 if (i != 1)
1760 p = isl_printer_print_str(p, ", ");
1761 p = isl_printer_print_ast_expr(p, expr->u.op.args[i]);
1763 p = isl_printer_print_str(p, ")");
1765 return p;
1768 /* Print an array access "expr".
1770 * The first argument represents the array being accessed.
1772 static __isl_give isl_printer *print_access(__isl_take isl_printer *p,
1773 __isl_keep isl_ast_expr *expr)
1775 int i = 0;
1777 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1778 for (i = 1; i < expr->u.op.n_arg; ++i) {
1779 p = isl_printer_print_str(p, "[");
1780 p = isl_printer_print_ast_expr(p, expr->u.op.args[i]);
1781 p = isl_printer_print_str(p, "]");
1784 return p;
1787 /* Print "expr" to "p".
1789 * If we are printing in isl format, then we also print an indication
1790 * of the size of the expression (if it was computed).
1792 __isl_give isl_printer *isl_printer_print_ast_expr(__isl_take isl_printer *p,
1793 __isl_keep isl_ast_expr *expr)
1795 if (!p)
1796 return NULL;
1797 if (!expr)
1798 return isl_printer_free(p);
1800 switch (expr->type) {
1801 case isl_ast_expr_op:
1802 if (expr->u.op.op == isl_ast_op_call) {
1803 p = print_call(p, expr);
1804 break;
1806 if (expr->u.op.op == isl_ast_op_access) {
1807 p = print_access(p, expr);
1808 break;
1810 if (expr->u.op.n_arg == 1) {
1811 p = isl_printer_print_str(p,
1812 get_op_str(p, expr->u.op.op));
1813 p = print_sub_expr(p, expr->u.op.op,
1814 expr->u.op.args[0], 0);
1815 break;
1817 if (expr->u.op.op == isl_ast_op_fdiv_q) {
1818 const char *name = get_op_str(p, isl_ast_op_fdiv_q);
1819 p = isl_printer_print_str(p, name);
1820 p = isl_printer_print_str(p, "(");
1821 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1822 p = isl_printer_print_str(p, ", ");
1823 p = isl_printer_print_ast_expr(p, expr->u.op.args[1]);
1824 p = isl_printer_print_str(p, ")");
1825 break;
1827 if (expr->u.op.op == isl_ast_op_max ||
1828 expr->u.op.op == isl_ast_op_min) {
1829 p = print_min_max(p, expr);
1830 break;
1832 if (expr->u.op.op == isl_ast_op_cond ||
1833 expr->u.op.op == isl_ast_op_select) {
1834 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1835 p = isl_printer_print_str(p, " ? ");
1836 p = isl_printer_print_ast_expr(p, expr->u.op.args[1]);
1837 p = isl_printer_print_str(p, " : ");
1838 p = isl_printer_print_ast_expr(p, expr->u.op.args[2]);
1839 break;
1841 if (expr->u.op.n_arg != 2)
1842 isl_die(isl_printer_get_ctx(p), isl_error_internal,
1843 "operation should have two arguments",
1844 goto error);
1845 p = print_sub_expr(p, expr->u.op.op, expr->u.op.args[0], 1);
1846 if (expr->u.op.op != isl_ast_op_member)
1847 p = isl_printer_print_str(p, " ");
1848 p = isl_printer_print_str(p, get_op_str(p, expr->u.op.op));
1849 if (expr->u.op.op != isl_ast_op_member)
1850 p = isl_printer_print_str(p, " ");
1851 p = print_sub_expr(p, expr->u.op.op, expr->u.op.args[1], 0);
1852 break;
1853 case isl_ast_expr_id:
1854 p = isl_printer_print_str(p, isl_id_get_name(expr->u.id));
1855 break;
1856 case isl_ast_expr_int:
1857 p = isl_printer_print_val(p, expr->u.v);
1858 break;
1859 case isl_ast_expr_error:
1860 break;
1863 return p;
1864 error:
1865 isl_printer_free(p);
1866 return NULL;
1869 /* Print "node" to "p" in "isl format".
1871 static __isl_give isl_printer *print_ast_node_isl(__isl_take isl_printer *p,
1872 __isl_keep isl_ast_node *node)
1874 p = isl_printer_print_str(p, "(");
1875 switch (node->type) {
1876 case isl_ast_node_for:
1877 if (node->u.f.degenerate) {
1878 p = isl_printer_print_ast_expr(p, node->u.f.init);
1879 } else {
1880 p = isl_printer_print_str(p, "init: ");
1881 p = isl_printer_print_ast_expr(p, node->u.f.init);
1882 p = isl_printer_print_str(p, ", ");
1883 p = isl_printer_print_str(p, "cond: ");
1884 p = isl_printer_print_ast_expr(p, node->u.f.cond);
1885 p = isl_printer_print_str(p, ", ");
1886 p = isl_printer_print_str(p, "inc: ");
1887 p = isl_printer_print_ast_expr(p, node->u.f.inc);
1889 if (node->u.f.body) {
1890 p = isl_printer_print_str(p, ", ");
1891 p = isl_printer_print_str(p, "body: ");
1892 p = isl_printer_print_ast_node(p, node->u.f.body);
1894 break;
1895 case isl_ast_node_mark:
1896 p = isl_printer_print_str(p, "mark: ");
1897 p = isl_printer_print_id(p, node->u.m.mark);
1898 p = isl_printer_print_str(p, ", ");
1899 p = isl_printer_print_str(p, "node: ");
1900 p = isl_printer_print_ast_node(p, node->u.m.node);
1901 case isl_ast_node_user:
1902 p = isl_printer_print_ast_expr(p, node->u.e.expr);
1903 break;
1904 case isl_ast_node_if:
1905 p = isl_printer_print_str(p, "guard: ");
1906 p = isl_printer_print_ast_expr(p, node->u.i.guard);
1907 if (node->u.i.then) {
1908 p = isl_printer_print_str(p, ", ");
1909 p = isl_printer_print_str(p, "then: ");
1910 p = isl_printer_print_ast_node(p, node->u.i.then);
1912 if (node->u.i.else_node) {
1913 p = isl_printer_print_str(p, ", ");
1914 p = isl_printer_print_str(p, "else: ");
1915 p = isl_printer_print_ast_node(p, node->u.i.else_node);
1917 break;
1918 case isl_ast_node_block:
1919 p = isl_printer_print_ast_node_list(p, node->u.b.children);
1920 break;
1921 case isl_ast_node_error:
1922 break;
1924 p = isl_printer_print_str(p, ")");
1925 return p;
1928 /* Do we need to print a block around the body "node" of a for or if node?
1930 * If the node is a block, then we need to print a block.
1931 * Also if the node is a degenerate for then we will print it as
1932 * an assignment followed by the body of the for loop, so we need a block
1933 * as well.
1934 * If the node is an if node with an else, then we print a block
1935 * to avoid spurious dangling else warnings emitted by some compilers.
1936 * If the node is a mark, then in principle, we would have to check
1937 * the child of the mark node. However, even if the child would not
1938 * require us to print a block, for readability it is probably best
1939 * to print a block anyway.
1940 * If the ast_always_print_block option has been set, then we print a block.
1942 static int need_block(__isl_keep isl_ast_node *node)
1944 isl_ctx *ctx;
1946 if (node->type == isl_ast_node_block)
1947 return 1;
1948 if (node->type == isl_ast_node_for && node->u.f.degenerate)
1949 return 1;
1950 if (node->type == isl_ast_node_if && node->u.i.else_node)
1951 return 1;
1952 if (node->type == isl_ast_node_mark)
1953 return 1;
1955 ctx = isl_ast_node_get_ctx(node);
1956 return isl_options_get_ast_always_print_block(ctx);
1959 static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
1960 __isl_keep isl_ast_node *node,
1961 __isl_keep isl_ast_print_options *options, int in_block, int in_list);
1962 static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
1963 __isl_keep isl_ast_node *node,
1964 __isl_keep isl_ast_print_options *options, int new_line,
1965 int force_block);
1967 /* Print the body "node" of a for or if node.
1968 * If "else_node" is set, then it is printed as well.
1969 * If "force_block" is set, then print out the body as a block.
1971 * We first check if we need to print out a block.
1972 * We always print out a block if there is an else node to make
1973 * sure that the else node is matched to the correct if node.
1974 * For consistency, the corresponding else node is also printed as a block.
1976 * If the else node is itself an if, then we print it as
1978 * } else if (..) {
1981 * Otherwise the else node is printed as
1983 * } else {
1984 * node
1987 static __isl_give isl_printer *print_body_c(__isl_take isl_printer *p,
1988 __isl_keep isl_ast_node *node, __isl_keep isl_ast_node *else_node,
1989 __isl_keep isl_ast_print_options *options, int force_block)
1991 if (!node)
1992 return isl_printer_free(p);
1994 if (!force_block && !else_node && !need_block(node)) {
1995 p = isl_printer_end_line(p);
1996 p = isl_printer_indent(p, 2);
1997 p = isl_ast_node_print(node, p,
1998 isl_ast_print_options_copy(options));
1999 p = isl_printer_indent(p, -2);
2000 return p;
2003 p = isl_printer_print_str(p, " {");
2004 p = isl_printer_end_line(p);
2005 p = isl_printer_indent(p, 2);
2006 p = print_ast_node_c(p, node, options, 1, 0);
2007 p = isl_printer_indent(p, -2);
2008 p = isl_printer_start_line(p);
2009 p = isl_printer_print_str(p, "}");
2010 if (else_node) {
2011 if (else_node->type == isl_ast_node_if) {
2012 p = isl_printer_print_str(p, " else ");
2013 p = print_if_c(p, else_node, options, 0, 1);
2014 } else {
2015 p = isl_printer_print_str(p, " else");
2016 p = print_body_c(p, else_node, NULL, options, 1);
2018 } else
2019 p = isl_printer_end_line(p);
2021 return p;
2024 /* Print the start of a compound statement.
2026 static __isl_give isl_printer *start_block(__isl_take isl_printer *p)
2028 p = isl_printer_start_line(p);
2029 p = isl_printer_print_str(p, "{");
2030 p = isl_printer_end_line(p);
2031 p = isl_printer_indent(p, 2);
2033 return p;
2036 /* Print the end of a compound statement.
2038 static __isl_give isl_printer *end_block(__isl_take isl_printer *p)
2040 p = isl_printer_indent(p, -2);
2041 p = isl_printer_start_line(p);
2042 p = isl_printer_print_str(p, "}");
2043 p = isl_printer_end_line(p);
2045 return p;
2048 /* Print the for node "node".
2050 * If the for node is degenerate, it is printed as
2052 * type iterator = init;
2053 * body
2055 * Otherwise, it is printed as
2057 * for (type iterator = init; cond; iterator += inc)
2058 * body
2060 * "in_block" is set if we are currently inside a block.
2061 * "in_list" is set if the current node is not alone in the block.
2062 * If we are not in a block or if the current not is not alone in the block
2063 * then we print a block around a degenerate for loop such that the variable
2064 * declaration will not conflict with any potential other declaration
2065 * of the same variable.
2067 static __isl_give isl_printer *print_for_c(__isl_take isl_printer *p,
2068 __isl_keep isl_ast_node *node,
2069 __isl_keep isl_ast_print_options *options, int in_block, int in_list)
2071 isl_id *id;
2072 const char *name;
2073 const char *type;
2075 type = isl_options_get_ast_iterator_type(isl_printer_get_ctx(p));
2076 if (!node->u.f.degenerate) {
2077 id = isl_ast_expr_get_id(node->u.f.iterator);
2078 name = isl_id_get_name(id);
2079 isl_id_free(id);
2080 p = isl_printer_start_line(p);
2081 p = isl_printer_print_str(p, "for (");
2082 p = isl_printer_print_str(p, type);
2083 p = isl_printer_print_str(p, " ");
2084 p = isl_printer_print_str(p, name);
2085 p = isl_printer_print_str(p, " = ");
2086 p = isl_printer_print_ast_expr(p, node->u.f.init);
2087 p = isl_printer_print_str(p, "; ");
2088 p = isl_printer_print_ast_expr(p, node->u.f.cond);
2089 p = isl_printer_print_str(p, "; ");
2090 p = isl_printer_print_str(p, name);
2091 p = isl_printer_print_str(p, " += ");
2092 p = isl_printer_print_ast_expr(p, node->u.f.inc);
2093 p = isl_printer_print_str(p, ")");
2094 p = print_body_c(p, node->u.f.body, NULL, options, 0);
2095 } else {
2096 id = isl_ast_expr_get_id(node->u.f.iterator);
2097 name = isl_id_get_name(id);
2098 isl_id_free(id);
2099 if (!in_block || in_list)
2100 p = start_block(p);
2101 p = isl_printer_start_line(p);
2102 p = isl_printer_print_str(p, type);
2103 p = isl_printer_print_str(p, " ");
2104 p = isl_printer_print_str(p, name);
2105 p = isl_printer_print_str(p, " = ");
2106 p = isl_printer_print_ast_expr(p, node->u.f.init);
2107 p = isl_printer_print_str(p, ";");
2108 p = isl_printer_end_line(p);
2109 p = print_ast_node_c(p, node->u.f.body, options, 1, 0);
2110 if (!in_block || in_list)
2111 p = end_block(p);
2114 return p;
2117 /* Print the if node "node".
2118 * If "new_line" is set then the if node should be printed on a new line.
2119 * If "force_block" is set, then print out the body as a block.
2121 static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
2122 __isl_keep isl_ast_node *node,
2123 __isl_keep isl_ast_print_options *options, int new_line,
2124 int force_block)
2126 if (new_line)
2127 p = isl_printer_start_line(p);
2128 p = isl_printer_print_str(p, "if (");
2129 p = isl_printer_print_ast_expr(p, node->u.i.guard);
2130 p = isl_printer_print_str(p, ")");
2131 p = print_body_c(p, node->u.i.then, node->u.i.else_node, options,
2132 force_block);
2134 return p;
2137 /* Print the "node" to "p".
2139 * "in_block" is set if we are currently inside a block.
2140 * If so, we do not print a block around the children of a block node.
2141 * We do this to avoid an extra block around the body of a degenerate
2142 * for node.
2144 * "in_list" is set if the current node is not alone in the block.
2146 static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
2147 __isl_keep isl_ast_node *node,
2148 __isl_keep isl_ast_print_options *options, int in_block, int in_list)
2150 switch (node->type) {
2151 case isl_ast_node_for:
2152 if (options->print_for)
2153 return options->print_for(p,
2154 isl_ast_print_options_copy(options),
2155 node, options->print_for_user);
2156 p = print_for_c(p, node, options, in_block, in_list);
2157 break;
2158 case isl_ast_node_if:
2159 p = print_if_c(p, node, options, 1, 0);
2160 break;
2161 case isl_ast_node_block:
2162 if (!in_block)
2163 p = start_block(p);
2164 p = isl_ast_node_list_print(node->u.b.children, p, options);
2165 if (!in_block)
2166 p = end_block(p);
2167 break;
2168 case isl_ast_node_mark:
2169 p = isl_printer_start_line(p);
2170 p = isl_printer_print_str(p, "// ");
2171 p = isl_printer_print_str(p, isl_id_get_name(node->u.m.mark));
2172 p = isl_printer_end_line(p);
2173 p = print_ast_node_c(p, node->u.m.node, options, 0, in_list);
2174 break;
2175 case isl_ast_node_user:
2176 if (options->print_user)
2177 return options->print_user(p,
2178 isl_ast_print_options_copy(options),
2179 node, options->print_user_user);
2180 p = isl_printer_start_line(p);
2181 p = isl_printer_print_ast_expr(p, node->u.e.expr);
2182 p = isl_printer_print_str(p, ";");
2183 p = isl_printer_end_line(p);
2184 break;
2185 case isl_ast_node_error:
2186 break;
2188 return p;
2191 /* Print the for node "node" to "p".
2193 __isl_give isl_printer *isl_ast_node_for_print(__isl_keep isl_ast_node *node,
2194 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
2196 if (!node || !options)
2197 goto error;
2198 if (node->type != isl_ast_node_for)
2199 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
2200 "not a for node", goto error);
2201 p = print_for_c(p, node, options, 0, 0);
2202 isl_ast_print_options_free(options);
2203 return p;
2204 error:
2205 isl_ast_print_options_free(options);
2206 isl_printer_free(p);
2207 return NULL;
2210 /* Print the if node "node" to "p".
2212 __isl_give isl_printer *isl_ast_node_if_print(__isl_keep isl_ast_node *node,
2213 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
2215 if (!node || !options)
2216 goto error;
2217 if (node->type != isl_ast_node_if)
2218 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
2219 "not an if node", goto error);
2220 p = print_if_c(p, node, options, 1, 0);
2221 isl_ast_print_options_free(options);
2222 return p;
2223 error:
2224 isl_ast_print_options_free(options);
2225 isl_printer_free(p);
2226 return NULL;
2229 /* Print "node" to "p".
2231 __isl_give isl_printer *isl_ast_node_print(__isl_keep isl_ast_node *node,
2232 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
2234 if (!options || !node)
2235 goto error;
2236 p = print_ast_node_c(p, node, options, 0, 0);
2237 isl_ast_print_options_free(options);
2238 return p;
2239 error:
2240 isl_ast_print_options_free(options);
2241 isl_printer_free(p);
2242 return NULL;
2245 /* Print "node" to "p".
2247 __isl_give isl_printer *isl_printer_print_ast_node(__isl_take isl_printer *p,
2248 __isl_keep isl_ast_node *node)
2250 int format;
2251 isl_ast_print_options *options;
2253 if (!p)
2254 return NULL;
2256 format = isl_printer_get_output_format(p);
2257 switch (format) {
2258 case ISL_FORMAT_ISL:
2259 p = print_ast_node_isl(p, node);
2260 break;
2261 case ISL_FORMAT_C:
2262 options = isl_ast_print_options_alloc(isl_printer_get_ctx(p));
2263 p = isl_ast_node_print(node, p, options);
2264 break;
2265 default:
2266 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2267 "output format not supported for ast_node",
2268 return isl_printer_free(p));
2271 return p;
2274 /* Print the list of nodes "list" to "p".
2276 __isl_give isl_printer *isl_ast_node_list_print(
2277 __isl_keep isl_ast_node_list *list, __isl_take isl_printer *p,
2278 __isl_keep isl_ast_print_options *options)
2280 int i;
2282 if (!p || !list || !options)
2283 return isl_printer_free(p);
2285 for (i = 0; i < list->n; ++i)
2286 p = print_ast_node_c(p, list->p[i], options, 1, 1);
2288 return p;
2291 #define ISL_AST_MACRO_FLOORD (1 << 0)
2292 #define ISL_AST_MACRO_MIN (1 << 1)
2293 #define ISL_AST_MACRO_MAX (1 << 2)
2294 #define ISL_AST_MACRO_ALL (ISL_AST_MACRO_FLOORD | \
2295 ISL_AST_MACRO_MIN | \
2296 ISL_AST_MACRO_MAX)
2298 /* If "expr" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
2299 * then set the corresponding bit in "macros".
2301 static int ast_expr_required_macros(__isl_keep isl_ast_expr *expr, int macros)
2303 int i;
2305 if (macros == ISL_AST_MACRO_ALL)
2306 return macros;
2308 if (expr->type != isl_ast_expr_op)
2309 return macros;
2311 if (expr->u.op.op == isl_ast_op_min)
2312 macros |= ISL_AST_MACRO_MIN;
2313 if (expr->u.op.op == isl_ast_op_max)
2314 macros |= ISL_AST_MACRO_MAX;
2315 if (expr->u.op.op == isl_ast_op_fdiv_q)
2316 macros |= ISL_AST_MACRO_FLOORD;
2318 for (i = 0; i < expr->u.op.n_arg; ++i)
2319 macros = ast_expr_required_macros(expr->u.op.args[i], macros);
2321 return macros;
2324 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
2325 int macros);
2327 /* If "node" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
2328 * then set the corresponding bit in "macros".
2330 static int ast_node_required_macros(__isl_keep isl_ast_node *node, int macros)
2332 if (macros == ISL_AST_MACRO_ALL)
2333 return macros;
2335 switch (node->type) {
2336 case isl_ast_node_for:
2337 macros = ast_expr_required_macros(node->u.f.init, macros);
2338 if (!node->u.f.degenerate) {
2339 macros = ast_expr_required_macros(node->u.f.cond,
2340 macros);
2341 macros = ast_expr_required_macros(node->u.f.inc,
2342 macros);
2344 macros = ast_node_required_macros(node->u.f.body, macros);
2345 break;
2346 case isl_ast_node_if:
2347 macros = ast_expr_required_macros(node->u.i.guard, macros);
2348 macros = ast_node_required_macros(node->u.i.then, macros);
2349 if (node->u.i.else_node)
2350 macros = ast_node_required_macros(node->u.i.else_node,
2351 macros);
2352 break;
2353 case isl_ast_node_block:
2354 macros = ast_node_list_required_macros(node->u.b.children,
2355 macros);
2356 break;
2357 case isl_ast_node_mark:
2358 macros = ast_node_required_macros(node->u.m.node, macros);
2359 break;
2360 case isl_ast_node_user:
2361 macros = ast_expr_required_macros(node->u.e.expr, macros);
2362 break;
2363 case isl_ast_node_error:
2364 break;
2367 return macros;
2370 /* If "list" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
2371 * then set the corresponding bit in "macros".
2373 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
2374 int macros)
2376 int i;
2378 for (i = 0; i < list->n; ++i)
2379 macros = ast_node_required_macros(list->p[i], macros);
2381 return macros;
2384 /* Data structure for keeping track of whether a macro definition
2385 * for a given type has already been printed.
2386 * The value is zero if no definition has been printed and non-zero otherwise.
2388 struct isl_ast_op_printed {
2389 char printed[isl_ast_op_last + 1];
2392 /* Create an empty struct isl_ast_op_printed.
2394 static void *create_printed(isl_ctx *ctx)
2396 return isl_calloc_type(ctx, struct isl_ast_op_printed);
2399 /* Free a struct isl_ast_op_printed.
2401 static void free_printed(void *user)
2403 free(user);
2406 /* Ensure that "p" has an isl_ast_op_printed note identified by "id".
2408 static __isl_give isl_printer *alloc_printed(__isl_take isl_printer *p,
2409 __isl_keep isl_id *id)
2411 return alloc_note(p, id, &create_printed, &free_printed);
2414 /* Create an identifier that is used to store
2415 * an isl_ast_op_printed note.
2417 static __isl_give isl_id *printed_id(isl_ctx *ctx)
2419 return isl_id_alloc(ctx, "isl_ast_op_type_printed", NULL);
2422 /* Did the user specify that a macro definition should only be
2423 * printed once and has a macro definition for "type" already
2424 * been printed to "p"?
2425 * If definitions should only be printed once, but a definition
2426 * for "p" has not yet been printed, then mark it as having been
2427 * printed so that it will not printed again.
2428 * The actual printing is taken care of by the caller.
2430 static isl_bool already_printed_once(__isl_keep isl_printer *p,
2431 enum isl_ast_op_type type)
2433 isl_ctx *ctx;
2434 isl_id *id;
2435 struct isl_ast_op_printed *printed;
2437 if (!p)
2438 return isl_bool_error;
2440 ctx = isl_printer_get_ctx(p);
2441 if (!isl_options_get_ast_print_macro_once(ctx))
2442 return isl_bool_false;
2444 if (type > isl_ast_op_last)
2445 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
2446 "invalid type", return isl_bool_error);
2448 id = printed_id(isl_printer_get_ctx(p));
2449 p = alloc_printed(p, id);
2450 printed = get_note(p, id);
2451 isl_id_free(id);
2452 if (!printed)
2453 return isl_bool_error;
2455 if (printed->printed[type])
2456 return isl_bool_true;
2458 printed->printed[type] = 1;
2459 return isl_bool_false;
2462 /* Print a macro definition for the operator "type".
2464 * If the user has specified that a macro definition should
2465 * only be printed once to any given printer and if the macro definition
2466 * has already been printed to "p", then do not print the definition.
2468 __isl_give isl_printer *isl_ast_op_type_print_macro(
2469 enum isl_ast_op_type type, __isl_take isl_printer *p)
2471 isl_bool skip;
2473 skip = already_printed_once(p, type);
2474 if (skip < 0)
2475 return isl_printer_free(p);
2476 if (skip)
2477 return p;
2479 switch (type) {
2480 case isl_ast_op_min:
2481 p = isl_printer_start_line(p);
2482 p = isl_printer_print_str(p, "#define ");
2483 p = isl_printer_print_str(p, get_op_str(p, type));
2484 p = isl_printer_print_str(p,
2485 "(x,y) ((x) < (y) ? (x) : (y))");
2486 p = isl_printer_end_line(p);
2487 break;
2488 case isl_ast_op_max:
2489 p = isl_printer_start_line(p);
2490 p = isl_printer_print_str(p, "#define ");
2491 p = isl_printer_print_str(p, get_op_str(p, type));
2492 p = isl_printer_print_str(p,
2493 "(x,y) ((x) > (y) ? (x) : (y))");
2494 p = isl_printer_end_line(p);
2495 break;
2496 case isl_ast_op_fdiv_q:
2497 p = isl_printer_start_line(p);
2498 p = isl_printer_print_str(p, "#define ");
2499 p = isl_printer_print_str(p, get_op_str(p, type));
2500 p = isl_printer_print_str(p,
2501 "(n,d) "
2502 "(((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))");
2503 p = isl_printer_end_line(p);
2504 break;
2505 default:
2506 break;
2509 return p;
2512 /* Call "fn" for each type of operation represented in the "macros"
2513 * bit vector.
2515 static isl_stat foreach_ast_op_type(int macros,
2516 isl_stat (*fn)(enum isl_ast_op_type type, void *user), void *user)
2518 if (macros & ISL_AST_MACRO_MIN && fn(isl_ast_op_min, user) < 0)
2519 return isl_stat_error;
2520 if (macros & ISL_AST_MACRO_MAX && fn(isl_ast_op_max, user) < 0)
2521 return isl_stat_error;
2522 if (macros & ISL_AST_MACRO_FLOORD && fn(isl_ast_op_fdiv_q, user) < 0)
2523 return isl_stat_error;
2525 return isl_stat_ok;
2528 /* Call "fn" for each type of operation that appears in "expr"
2529 * and that requires a macro definition.
2531 isl_stat isl_ast_expr_foreach_ast_op_type(__isl_keep isl_ast_expr *expr,
2532 isl_stat (*fn)(enum isl_ast_op_type type, void *user), void *user)
2534 int macros;
2536 if (!expr)
2537 return isl_stat_error;
2539 macros = ast_expr_required_macros(expr, 0);
2540 return foreach_ast_op_type(macros, fn, user);
2543 /* Call "fn" for each type of operation that appears in "node"
2544 * and that requires a macro definition.
2546 isl_stat isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node *node,
2547 isl_stat (*fn)(enum isl_ast_op_type type, void *user), void *user)
2549 int macros;
2551 if (!node)
2552 return isl_stat_error;
2554 macros = ast_node_required_macros(node, 0);
2555 return foreach_ast_op_type(macros, fn, user);
2558 static isl_stat ast_op_type_print_macro(enum isl_ast_op_type type, void *user)
2560 isl_printer **p = user;
2562 *p = isl_ast_op_type_print_macro(type, *p);
2564 return isl_stat_ok;
2567 /* Print macro definitions for all the macros used in the result
2568 * of printing "expr".
2570 __isl_give isl_printer *isl_ast_expr_print_macros(
2571 __isl_keep isl_ast_expr *expr, __isl_take isl_printer *p)
2573 if (isl_ast_expr_foreach_ast_op_type(expr,
2574 &ast_op_type_print_macro, &p) < 0)
2575 return isl_printer_free(p);
2576 return p;
2579 /* Print macro definitions for all the macros used in the result
2580 * of printing "node".
2582 __isl_give isl_printer *isl_ast_node_print_macros(
2583 __isl_keep isl_ast_node *node, __isl_take isl_printer *p)
2585 if (isl_ast_node_foreach_ast_op_type(node,
2586 &ast_op_type_print_macro, &p) < 0)
2587 return isl_printer_free(p);
2588 return p;
2591 /* Return a string containing C code representing this isl_ast_expr.
2593 __isl_give char *isl_ast_expr_to_C_str(__isl_keep isl_ast_expr *expr)
2595 isl_printer *p;
2596 char *str;
2598 if (!expr)
2599 return NULL;
2601 p = isl_printer_to_str(isl_ast_expr_get_ctx(expr));
2602 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
2603 p = isl_printer_print_ast_expr(p, expr);
2605 str = isl_printer_get_str(p);
2607 isl_printer_free(p);
2609 return str;
2612 /* Return a string containing C code representing this isl_ast_node.
2614 __isl_give char *isl_ast_node_to_C_str(__isl_keep isl_ast_node *node)
2616 isl_printer *p;
2617 char *str;
2619 if (!node)
2620 return NULL;
2622 p = isl_printer_to_str(isl_ast_node_get_ctx(node));
2623 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
2624 p = isl_printer_print_ast_node(p, node);
2626 str = isl_printer_get_str(p);
2628 isl_printer_free(p);
2630 return str;