introduce new schedule API
[isl.git] / isl_ast.c
blob547a7456733401b1f5ad5e29dd906a2b24684101
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 <isl_ast_private.h>
12 #undef BASE
13 #define BASE ast_expr
15 #include <isl_list_templ.c>
17 #undef BASE
18 #define BASE ast_node
20 #include <isl_list_templ.c>
22 isl_ctx *isl_ast_print_options_get_ctx(
23 __isl_keep isl_ast_print_options *options)
25 return options ? options->ctx : NULL;
28 __isl_give isl_ast_print_options *isl_ast_print_options_alloc(isl_ctx *ctx)
30 isl_ast_print_options *options;
32 options = isl_calloc_type(ctx, isl_ast_print_options);
33 if (!options)
34 return NULL;
36 options->ctx = ctx;
37 isl_ctx_ref(ctx);
38 options->ref = 1;
40 return options;
43 __isl_give isl_ast_print_options *isl_ast_print_options_dup(
44 __isl_keep isl_ast_print_options *options)
46 isl_ctx *ctx;
47 isl_ast_print_options *dup;
49 if (!options)
50 return NULL;
52 ctx = isl_ast_print_options_get_ctx(options);
53 dup = isl_ast_print_options_alloc(ctx);
54 if (!dup)
55 return NULL;
57 dup->print_for = options->print_for;
58 dup->print_for_user = options->print_for_user;
59 dup->print_user = options->print_user;
60 dup->print_user_user = options->print_user_user;
62 return dup;
65 __isl_give isl_ast_print_options *isl_ast_print_options_cow(
66 __isl_take isl_ast_print_options *options)
68 if (!options)
69 return NULL;
71 if (options->ref == 1)
72 return options;
73 options->ref--;
74 return isl_ast_print_options_dup(options);
77 __isl_give isl_ast_print_options *isl_ast_print_options_copy(
78 __isl_keep isl_ast_print_options *options)
80 if (!options)
81 return NULL;
83 options->ref++;
84 return options;
87 void *isl_ast_print_options_free(__isl_take isl_ast_print_options *options)
89 if (!options)
90 return NULL;
92 if (--options->ref > 0)
93 return NULL;
95 isl_ctx_deref(options->ctx);
97 free(options);
98 return NULL;
101 /* Set the print_user callback of "options" to "print_user".
103 * If this callback is set, then it used to print user nodes in the AST.
104 * Otherwise, the expression associated to the user node is printed.
106 __isl_give isl_ast_print_options *isl_ast_print_options_set_print_user(
107 __isl_take isl_ast_print_options *options,
108 __isl_give isl_printer *(*print_user)(__isl_take isl_printer *p,
109 __isl_take isl_ast_print_options *options,
110 __isl_keep isl_ast_node *node, void *user),
111 void *user)
113 options = isl_ast_print_options_cow(options);
114 if (!options)
115 return NULL;
117 options->print_user = print_user;
118 options->print_user_user = user;
120 return options;
123 /* Set the print_for callback of "options" to "print_for".
125 * If this callback is set, then it used to print for nodes in the AST.
127 __isl_give isl_ast_print_options *isl_ast_print_options_set_print_for(
128 __isl_take isl_ast_print_options *options,
129 __isl_give isl_printer *(*print_for)(__isl_take isl_printer *p,
130 __isl_take isl_ast_print_options *options,
131 __isl_keep isl_ast_node *node, void *user),
132 void *user)
134 options = isl_ast_print_options_cow(options);
135 if (!options)
136 return NULL;
138 options->print_for = print_for;
139 options->print_for_user = user;
141 return options;
144 __isl_give isl_ast_expr *isl_ast_expr_copy(__isl_keep isl_ast_expr *expr)
146 if (!expr)
147 return NULL;
149 expr->ref++;
150 return expr;
153 __isl_give isl_ast_expr *isl_ast_expr_dup(__isl_keep isl_ast_expr *expr)
155 int i;
156 isl_ctx *ctx;
157 isl_ast_expr *dup;
159 if (!expr)
160 return NULL;
162 ctx = isl_ast_expr_get_ctx(expr);
163 switch (expr->type) {
164 case isl_ast_expr_int:
165 dup = isl_ast_expr_from_val(isl_val_copy(expr->u.v));
166 break;
167 case isl_ast_expr_id:
168 dup = isl_ast_expr_from_id(isl_id_copy(expr->u.id));
169 break;
170 case isl_ast_expr_op:
171 dup = isl_ast_expr_alloc_op(ctx,
172 expr->u.op.op, expr->u.op.n_arg);
173 if (!dup)
174 return NULL;
175 for (i = 0; i < expr->u.op.n_arg; ++i)
176 dup->u.op.args[i] =
177 isl_ast_expr_copy(expr->u.op.args[i]);
178 break;
179 case isl_ast_expr_error:
180 dup = NULL;
183 if (!dup)
184 return NULL;
186 return dup;
189 __isl_give isl_ast_expr *isl_ast_expr_cow(__isl_take isl_ast_expr *expr)
191 if (!expr)
192 return NULL;
194 if (expr->ref == 1)
195 return expr;
196 expr->ref--;
197 return isl_ast_expr_dup(expr);
200 void *isl_ast_expr_free(__isl_take isl_ast_expr *expr)
202 int i;
204 if (!expr)
205 return NULL;
207 if (--expr->ref > 0)
208 return NULL;
210 isl_ctx_deref(expr->ctx);
212 switch (expr->type) {
213 case isl_ast_expr_int:
214 isl_val_free(expr->u.v);
215 break;
216 case isl_ast_expr_id:
217 isl_id_free(expr->u.id);
218 break;
219 case isl_ast_expr_op:
220 for (i = 0; i < expr->u.op.n_arg; ++i)
221 isl_ast_expr_free(expr->u.op.args[i]);
222 free(expr->u.op.args);
223 break;
224 case isl_ast_expr_error:
225 break;
228 free(expr);
229 return NULL;
232 isl_ctx *isl_ast_expr_get_ctx(__isl_keep isl_ast_expr *expr)
234 return expr ? expr->ctx : NULL;
237 enum isl_ast_expr_type isl_ast_expr_get_type(__isl_keep isl_ast_expr *expr)
239 return expr ? expr->type : isl_ast_expr_error;
242 /* Return the integer value represented by "expr".
244 __isl_give isl_val *isl_ast_expr_get_val(__isl_keep isl_ast_expr *expr)
246 if (!expr)
247 return NULL;
248 if (expr->type != isl_ast_expr_int)
249 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
250 "expression not an int", return NULL);
251 return isl_val_copy(expr->u.v);
254 __isl_give isl_id *isl_ast_expr_get_id(__isl_keep isl_ast_expr *expr)
256 if (!expr)
257 return NULL;
258 if (expr->type != isl_ast_expr_id)
259 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
260 "expression not an identifier", return NULL);
262 return isl_id_copy(expr->u.id);
265 enum isl_ast_op_type isl_ast_expr_get_op_type(__isl_keep isl_ast_expr *expr)
267 if (!expr)
268 return isl_ast_op_error;
269 if (expr->type != isl_ast_expr_op)
270 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
271 "expression not an operation", return isl_ast_op_error);
272 return expr->u.op.op;
275 int isl_ast_expr_get_op_n_arg(__isl_keep isl_ast_expr *expr)
277 if (!expr)
278 return -1;
279 if (expr->type != isl_ast_expr_op)
280 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
281 "expression not an operation", return -1);
282 return expr->u.op.n_arg;
285 __isl_give isl_ast_expr *isl_ast_expr_get_op_arg(__isl_keep isl_ast_expr *expr,
286 int pos)
288 if (!expr)
289 return NULL;
290 if (expr->type != isl_ast_expr_op)
291 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
292 "expression not an operation", return NULL);
293 if (pos < 0 || pos >= expr->u.op.n_arg)
294 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
295 "index out of bounds", return NULL);
297 return isl_ast_expr_copy(expr->u.op.args[pos]);
300 /* Replace the argument at position "pos" of "expr" by "arg".
302 __isl_give isl_ast_expr *isl_ast_expr_set_op_arg(__isl_take isl_ast_expr *expr,
303 int pos, __isl_take isl_ast_expr *arg)
305 expr = isl_ast_expr_cow(expr);
306 if (!expr || !arg)
307 goto error;
308 if (expr->type != isl_ast_expr_op)
309 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
310 "expression not an operation", goto error);
311 if (pos < 0 || pos >= expr->u.op.n_arg)
312 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
313 "index out of bounds", goto error);
315 isl_ast_expr_free(expr->u.op.args[pos]);
316 expr->u.op.args[pos] = arg;
318 return expr;
319 error:
320 isl_ast_expr_free(arg);
321 return isl_ast_expr_free(expr);
324 /* Is "expr1" equal to "expr2"?
326 int isl_ast_expr_is_equal(__isl_keep isl_ast_expr *expr1,
327 __isl_keep isl_ast_expr *expr2)
329 int i;
331 if (!expr1 || !expr2)
332 return -1;
334 if (expr1 == expr2)
335 return 1;
336 if (expr1->type != expr2->type)
337 return 0;
338 switch (expr1->type) {
339 case isl_ast_expr_int:
340 return isl_val_eq(expr1->u.v, expr2->u.v);
341 case isl_ast_expr_id:
342 return expr1->u.id == expr2->u.id;
343 case isl_ast_expr_op:
344 if (expr1->u.op.op != expr2->u.op.op)
345 return 0;
346 if (expr1->u.op.n_arg != expr2->u.op.n_arg)
347 return 0;
348 for (i = 0; i < expr1->u.op.n_arg; ++i) {
349 int equal;
350 equal = isl_ast_expr_is_equal(expr1->u.op.args[i],
351 expr2->u.op.args[i]);
352 return 0;
353 if (equal < 0 || !equal)
354 return equal;
356 return 1;
357 case isl_ast_expr_error:
358 return -1;
362 /* Create a new operation expression of operation type "op",
363 * with "n_arg" as yet unspecified arguments.
365 __isl_give isl_ast_expr *isl_ast_expr_alloc_op(isl_ctx *ctx,
366 enum isl_ast_op_type op, int n_arg)
368 isl_ast_expr *expr;
370 expr = isl_calloc_type(ctx, isl_ast_expr);
371 if (!expr)
372 return NULL;
374 expr->ctx = ctx;
375 isl_ctx_ref(ctx);
376 expr->ref = 1;
377 expr->type = isl_ast_expr_op;
378 expr->u.op.op = op;
379 expr->u.op.n_arg = n_arg;
380 expr->u.op.args = isl_calloc_array(ctx, isl_ast_expr *, n_arg);
382 if (n_arg && !expr->u.op.args)
383 return isl_ast_expr_free(expr);
385 return expr;
388 /* Create a new id expression representing "id".
390 __isl_give isl_ast_expr *isl_ast_expr_from_id(__isl_take isl_id *id)
392 isl_ctx *ctx;
393 isl_ast_expr *expr;
395 if (!id)
396 return NULL;
398 ctx = isl_id_get_ctx(id);
399 expr = isl_calloc_type(ctx, isl_ast_expr);
400 if (!expr)
401 return isl_id_free(id);
403 expr->ctx = ctx;
404 isl_ctx_ref(ctx);
405 expr->ref = 1;
406 expr->type = isl_ast_expr_id;
407 expr->u.id = id;
409 return expr;
412 /* Create a new integer expression representing "i".
414 __isl_give isl_ast_expr *isl_ast_expr_alloc_int_si(isl_ctx *ctx, int i)
416 isl_ast_expr *expr;
418 expr = isl_calloc_type(ctx, isl_ast_expr);
419 if (!expr)
420 return NULL;
422 expr->ctx = ctx;
423 isl_ctx_ref(ctx);
424 expr->ref = 1;
425 expr->type = isl_ast_expr_int;
426 expr->u.v = isl_val_int_from_si(ctx, i);
427 if (!expr->u.v)
428 return isl_ast_expr_free(expr);
430 return expr;
433 /* Create a new integer expression representing "v".
435 __isl_give isl_ast_expr *isl_ast_expr_from_val(__isl_take isl_val *v)
437 isl_ctx *ctx;
438 isl_ast_expr *expr;
440 if (!v)
441 return NULL;
442 if (!isl_val_is_int(v))
443 isl_die(isl_val_get_ctx(v), isl_error_invalid,
444 "expecting integer value", return isl_val_free(v));
446 ctx = isl_val_get_ctx(v);
447 expr = isl_calloc_type(ctx, isl_ast_expr);
448 if (!expr)
449 return isl_val_free(v);
451 expr->ctx = ctx;
452 isl_ctx_ref(ctx);
453 expr->ref = 1;
454 expr->type = isl_ast_expr_int;
455 expr->u.v = v;
457 return expr;
460 /* Create an expression representing the negation of "arg".
462 __isl_give isl_ast_expr *isl_ast_expr_neg(__isl_take isl_ast_expr *arg)
464 isl_ctx *ctx;
465 isl_ast_expr *expr = NULL;
467 if (!arg)
468 return NULL;
470 ctx = isl_ast_expr_get_ctx(arg);
471 expr = isl_ast_expr_alloc_op(ctx, isl_ast_op_minus, 1);
472 if (!expr)
473 goto error;
475 expr->u.op.args[0] = arg;
477 return expr;
478 error:
479 isl_ast_expr_free(arg);
480 return NULL;
483 /* Create an expression representing the binary operation "type"
484 * applied to "expr1" and "expr2".
486 __isl_give isl_ast_expr *isl_ast_expr_alloc_binary(enum isl_ast_op_type type,
487 __isl_take isl_ast_expr *expr1, __isl_take isl_ast_expr *expr2)
489 isl_ctx *ctx;
490 isl_ast_expr *expr = NULL;
492 if (!expr1 || !expr2)
493 goto error;
495 ctx = isl_ast_expr_get_ctx(expr1);
496 expr = isl_ast_expr_alloc_op(ctx, type, 2);
497 if (!expr)
498 goto error;
500 expr->u.op.args[0] = expr1;
501 expr->u.op.args[1] = expr2;
503 return expr;
504 error:
505 isl_ast_expr_free(expr1);
506 isl_ast_expr_free(expr2);
507 return NULL;
510 /* Create an expression representing the sum of "expr1" and "expr2".
512 __isl_give isl_ast_expr *isl_ast_expr_add(__isl_take isl_ast_expr *expr1,
513 __isl_take isl_ast_expr *expr2)
515 return isl_ast_expr_alloc_binary(isl_ast_op_add, expr1, expr2);
518 /* Create an expression representing the difference of "expr1" and "expr2".
520 __isl_give isl_ast_expr *isl_ast_expr_sub(__isl_take isl_ast_expr *expr1,
521 __isl_take isl_ast_expr *expr2)
523 return isl_ast_expr_alloc_binary(isl_ast_op_sub, expr1, expr2);
526 /* Create an expression representing the product of "expr1" and "expr2".
528 __isl_give isl_ast_expr *isl_ast_expr_mul(__isl_take isl_ast_expr *expr1,
529 __isl_take isl_ast_expr *expr2)
531 return isl_ast_expr_alloc_binary(isl_ast_op_mul, expr1, expr2);
534 /* Create an expression representing the quotient of "expr1" and "expr2".
536 __isl_give isl_ast_expr *isl_ast_expr_div(__isl_take isl_ast_expr *expr1,
537 __isl_take isl_ast_expr *expr2)
539 return isl_ast_expr_alloc_binary(isl_ast_op_div, expr1, expr2);
542 /* Create an expression representing the conjunction of "expr1" and "expr2".
544 __isl_give isl_ast_expr *isl_ast_expr_and(__isl_take isl_ast_expr *expr1,
545 __isl_take isl_ast_expr *expr2)
547 return isl_ast_expr_alloc_binary(isl_ast_op_and, expr1, expr2);
550 /* Create an expression representing the disjunction of "expr1" and "expr2".
552 __isl_give isl_ast_expr *isl_ast_expr_or(__isl_take isl_ast_expr *expr1,
553 __isl_take isl_ast_expr *expr2)
555 return isl_ast_expr_alloc_binary(isl_ast_op_or, expr1, expr2);
558 /* Create an expression representing an access to "array" with index
559 * expressions "indices".
561 __isl_give isl_ast_expr *isl_ast_expr_access(__isl_take isl_ast_expr *array,
562 __isl_take isl_ast_expr_list *indices)
564 int i, n;
565 isl_ctx *ctx;
566 isl_ast_expr *access = NULL;
568 if (!array || !indices)
569 goto error;
571 ctx = isl_ast_expr_get_ctx(array);
572 n = isl_ast_expr_list_n_ast_expr(indices);
573 access = isl_ast_expr_alloc_op(ctx, isl_ast_op_access, 1 + n);
574 if (!access)
575 goto error;
576 for (i = 0; i < n; ++i) {
577 isl_ast_expr *index;
578 index = isl_ast_expr_list_get_ast_expr(indices, i);
579 access->u.op.args[1 + i] = index;
580 if (!index)
581 goto error;
583 access->u.op.args[0] = array;
585 isl_ast_expr_list_free(indices);
586 return access;
587 error:
588 isl_ast_expr_free(array);
589 isl_ast_expr_list_free(indices);
590 isl_ast_expr_free(access);
591 return NULL;
594 /* For each subexpression of "expr" of type isl_ast_expr_id,
595 * if it appears in "id2expr", then replace it by the corresponding
596 * expression.
598 __isl_give isl_ast_expr *isl_ast_expr_substitute_ids(
599 __isl_take isl_ast_expr *expr, __isl_take isl_id_to_ast_expr *id2expr)
601 int i;
602 isl_id *id;
604 if (!expr || !id2expr)
605 goto error;
607 switch (expr->type) {
608 case isl_ast_expr_int:
609 break;
610 case isl_ast_expr_id:
611 if (!isl_id_to_ast_expr_has(id2expr, expr->u.id))
612 break;
613 id = isl_id_copy(expr->u.id);
614 isl_ast_expr_free(expr);
615 expr = isl_id_to_ast_expr_get(id2expr, id);
616 break;
617 case isl_ast_expr_op:
618 for (i = 0; i < expr->u.op.n_arg; ++i) {
619 isl_ast_expr *arg;
620 arg = isl_ast_expr_copy(expr->u.op.args[i]);
621 arg = isl_ast_expr_substitute_ids(arg,
622 isl_id_to_ast_expr_copy(id2expr));
623 if (arg == expr->u.op.args[i]) {
624 isl_ast_expr_free(arg);
625 continue;
627 if (!arg)
628 expr = isl_ast_expr_free(expr);
629 expr = isl_ast_expr_cow(expr);
630 if (!expr) {
631 isl_ast_expr_free(arg);
632 break;
634 isl_ast_expr_free(expr->u.op.args[i]);
635 expr->u.op.args[i] = arg;
637 break;
638 case isl_ast_expr_error:
639 expr = isl_ast_expr_free(expr);
640 break;
643 isl_id_to_ast_expr_free(id2expr);
644 return expr;
645 error:
646 isl_ast_expr_free(expr);
647 isl_id_to_ast_expr_free(id2expr);
648 return NULL;
651 isl_ctx *isl_ast_node_get_ctx(__isl_keep isl_ast_node *node)
653 return node ? node->ctx : NULL;
656 enum isl_ast_node_type isl_ast_node_get_type(__isl_keep isl_ast_node *node)
658 return node ? node->type : isl_ast_node_error;
661 __isl_give isl_ast_node *isl_ast_node_alloc(isl_ctx *ctx,
662 enum isl_ast_node_type type)
664 isl_ast_node *node;
666 node = isl_calloc_type(ctx, isl_ast_node);
667 if (!node)
668 return NULL;
670 node->ctx = ctx;
671 isl_ctx_ref(ctx);
672 node->ref = 1;
673 node->type = type;
675 return node;
678 /* Create an if node with the given guard.
680 * The then body needs to be filled in later.
682 __isl_give isl_ast_node *isl_ast_node_alloc_if(__isl_take isl_ast_expr *guard)
684 isl_ast_node *node;
686 if (!guard)
687 return NULL;
689 node = isl_ast_node_alloc(isl_ast_expr_get_ctx(guard), isl_ast_node_if);
690 if (!node)
691 goto error;
692 node->u.i.guard = guard;
694 return node;
695 error:
696 isl_ast_expr_free(guard);
697 return NULL;
700 /* Create a for node with the given iterator.
702 * The remaining fields need to be filled in later.
704 __isl_give isl_ast_node *isl_ast_node_alloc_for(__isl_take isl_id *id)
706 isl_ast_node *node;
707 isl_ctx *ctx;
709 if (!id)
710 return NULL;
712 ctx = isl_id_get_ctx(id);
713 node = isl_ast_node_alloc(ctx, isl_ast_node_for);
714 if (!node)
715 return NULL;
717 node->u.f.iterator = isl_ast_expr_from_id(id);
718 if (!node->u.f.iterator)
719 return isl_ast_node_free(node);
721 return node;
724 /* Create a user node evaluating "expr".
726 __isl_give isl_ast_node *isl_ast_node_alloc_user(__isl_take isl_ast_expr *expr)
728 isl_ctx *ctx;
729 isl_ast_node *node;
731 if (!expr)
732 return NULL;
734 ctx = isl_ast_expr_get_ctx(expr);
735 node = isl_ast_node_alloc(ctx, isl_ast_node_user);
736 if (!node)
737 goto error;
739 node->u.e.expr = expr;
741 return node;
742 error:
743 isl_ast_expr_free(expr);
744 return NULL;
747 /* Create a block node with the given children.
749 __isl_give isl_ast_node *isl_ast_node_alloc_block(
750 __isl_take isl_ast_node_list *list)
752 isl_ast_node *node;
753 isl_ctx *ctx;
755 if (!list)
756 return NULL;
758 ctx = isl_ast_node_list_get_ctx(list);
759 node = isl_ast_node_alloc(ctx, isl_ast_node_block);
760 if (!node)
761 goto error;
763 node->u.b.children = list;
765 return node;
766 error:
767 isl_ast_node_list_free(list);
768 return NULL;
771 /* Represent the given list of nodes as a single node, either by
772 * extract the node from a single element list or by creating
773 * a block node with the list of nodes as children.
775 __isl_give isl_ast_node *isl_ast_node_from_ast_node_list(
776 __isl_take isl_ast_node_list *list)
778 isl_ast_node *node;
780 if (isl_ast_node_list_n_ast_node(list) != 1)
781 return isl_ast_node_alloc_block(list);
783 node = isl_ast_node_list_get_ast_node(list, 0);
784 isl_ast_node_list_free(list);
786 return node;
789 __isl_give isl_ast_node *isl_ast_node_copy(__isl_keep isl_ast_node *node)
791 if (!node)
792 return NULL;
794 node->ref++;
795 return node;
798 __isl_give isl_ast_node *isl_ast_node_dup(__isl_keep isl_ast_node *node)
800 isl_ast_node *dup;
802 if (!node)
803 return NULL;
805 dup = isl_ast_node_alloc(isl_ast_node_get_ctx(node), node->type);
806 if (!dup)
807 return NULL;
809 switch (node->type) {
810 case isl_ast_node_if:
811 dup->u.i.guard = isl_ast_expr_copy(node->u.i.guard);
812 dup->u.i.then = isl_ast_node_copy(node->u.i.then);
813 dup->u.i.else_node = isl_ast_node_copy(node->u.i.else_node);
814 if (!dup->u.i.guard || !dup->u.i.then ||
815 (node->u.i.else_node && !dup->u.i.else_node))
816 return isl_ast_node_free(dup);
817 break;
818 case isl_ast_node_for:
819 dup->u.f.iterator = isl_ast_expr_copy(node->u.f.iterator);
820 dup->u.f.init = isl_ast_expr_copy(node->u.f.init);
821 dup->u.f.cond = isl_ast_expr_copy(node->u.f.cond);
822 dup->u.f.inc = isl_ast_expr_copy(node->u.f.inc);
823 dup->u.f.body = isl_ast_node_copy(node->u.f.body);
824 if (!dup->u.f.iterator || !dup->u.f.init || !dup->u.f.cond ||
825 !dup->u.f.inc || !dup->u.f.body)
826 return isl_ast_node_free(dup);
827 break;
828 case isl_ast_node_block:
829 dup->u.b.children = isl_ast_node_list_copy(node->u.b.children);
830 if (!dup->u.b.children)
831 return isl_ast_node_free(dup);
832 break;
833 case isl_ast_node_user:
834 dup->u.e.expr = isl_ast_expr_copy(node->u.e.expr);
835 if (!dup->u.e.expr)
836 return isl_ast_node_free(dup);
837 break;
838 case isl_ast_node_error:
839 break;
842 return dup;
845 __isl_give isl_ast_node *isl_ast_node_cow(__isl_take isl_ast_node *node)
847 if (!node)
848 return NULL;
850 if (node->ref == 1)
851 return node;
852 node->ref--;
853 return isl_ast_node_dup(node);
856 void *isl_ast_node_free(__isl_take isl_ast_node *node)
858 if (!node)
859 return NULL;
861 if (--node->ref > 0)
862 return NULL;
864 switch (node->type) {
865 case isl_ast_node_if:
866 isl_ast_expr_free(node->u.i.guard);
867 isl_ast_node_free(node->u.i.then);
868 isl_ast_node_free(node->u.i.else_node);
869 break;
870 case isl_ast_node_for:
871 isl_ast_expr_free(node->u.f.iterator);
872 isl_ast_expr_free(node->u.f.init);
873 isl_ast_expr_free(node->u.f.cond);
874 isl_ast_expr_free(node->u.f.inc);
875 isl_ast_node_free(node->u.f.body);
876 break;
877 case isl_ast_node_block:
878 isl_ast_node_list_free(node->u.b.children);
879 break;
880 case isl_ast_node_user:
881 isl_ast_expr_free(node->u.e.expr);
882 break;
883 case isl_ast_node_error:
884 break;
887 isl_id_free(node->annotation);
888 isl_ctx_deref(node->ctx);
889 free(node);
891 return NULL;
894 /* Replace the body of the for node "node" by "body".
896 __isl_give isl_ast_node *isl_ast_node_for_set_body(
897 __isl_take isl_ast_node *node, __isl_take isl_ast_node *body)
899 node = isl_ast_node_cow(node);
900 if (!node || !body)
901 goto error;
902 if (node->type != isl_ast_node_for)
903 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
904 "not a for node", goto error);
906 isl_ast_node_free(node->u.f.body);
907 node->u.f.body = body;
909 return node;
910 error:
911 isl_ast_node_free(node);
912 isl_ast_node_free(body);
913 return NULL;
916 __isl_give isl_ast_node *isl_ast_node_for_get_body(
917 __isl_keep isl_ast_node *node)
919 if (!node)
920 return NULL;
921 if (node->type != isl_ast_node_for)
922 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
923 "not a for node", return NULL);
924 return isl_ast_node_copy(node->u.f.body);
927 /* Mark the given for node as being degenerate.
929 __isl_give isl_ast_node *isl_ast_node_for_mark_degenerate(
930 __isl_take isl_ast_node *node)
932 node = isl_ast_node_cow(node);
933 if (!node)
934 return NULL;
935 node->u.f.degenerate = 1;
936 return node;
939 int isl_ast_node_for_is_degenerate(__isl_keep isl_ast_node *node)
941 if (!node)
942 return -1;
943 if (node->type != isl_ast_node_for)
944 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
945 "not a for node", return -1);
946 return node->u.f.degenerate;
949 __isl_give isl_ast_expr *isl_ast_node_for_get_iterator(
950 __isl_keep isl_ast_node *node)
952 if (!node)
953 return NULL;
954 if (node->type != isl_ast_node_for)
955 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
956 "not a for node", return NULL);
957 return isl_ast_expr_copy(node->u.f.iterator);
960 __isl_give isl_ast_expr *isl_ast_node_for_get_init(
961 __isl_keep isl_ast_node *node)
963 if (!node)
964 return NULL;
965 if (node->type != isl_ast_node_for)
966 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
967 "not a for node", return NULL);
968 return isl_ast_expr_copy(node->u.f.init);
971 /* Return the condition expression of the given for node.
973 * If the for node is degenerate, then the condition is not explicitly
974 * stored in the node. Instead, it is constructed as
976 * iterator <= init
978 __isl_give isl_ast_expr *isl_ast_node_for_get_cond(
979 __isl_keep isl_ast_node *node)
981 if (!node)
982 return NULL;
983 if (node->type != isl_ast_node_for)
984 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
985 "not a for node", return NULL);
986 if (!node->u.f.degenerate)
987 return isl_ast_expr_copy(node->u.f.cond);
989 return isl_ast_expr_alloc_binary(isl_ast_op_le,
990 isl_ast_expr_copy(node->u.f.iterator),
991 isl_ast_expr_copy(node->u.f.init));
994 /* Return the increment of the given for node.
996 * If the for node is degenerate, then the increment is not explicitly
997 * stored in the node. We simply return "1".
999 __isl_give isl_ast_expr *isl_ast_node_for_get_inc(
1000 __isl_keep isl_ast_node *node)
1002 if (!node)
1003 return NULL;
1004 if (node->type != isl_ast_node_for)
1005 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1006 "not a for node", return NULL);
1007 if (!node->u.f.degenerate)
1008 return isl_ast_expr_copy(node->u.f.inc);
1009 return isl_ast_expr_alloc_int_si(isl_ast_node_get_ctx(node), 1);
1012 /* Replace the then branch of the if node "node" by "child".
1014 __isl_give isl_ast_node *isl_ast_node_if_set_then(
1015 __isl_take isl_ast_node *node, __isl_take isl_ast_node *child)
1017 node = isl_ast_node_cow(node);
1018 if (!node || !child)
1019 goto error;
1020 if (node->type != isl_ast_node_if)
1021 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1022 "not an if node", goto error);
1024 isl_ast_node_free(node->u.i.then);
1025 node->u.i.then = child;
1027 return node;
1028 error:
1029 isl_ast_node_free(node);
1030 isl_ast_node_free(child);
1031 return NULL;
1034 __isl_give isl_ast_node *isl_ast_node_if_get_then(
1035 __isl_keep isl_ast_node *node)
1037 if (!node)
1038 return NULL;
1039 if (node->type != isl_ast_node_if)
1040 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1041 "not an if node", return NULL);
1042 return isl_ast_node_copy(node->u.i.then);
1045 int isl_ast_node_if_has_else(
1046 __isl_keep isl_ast_node *node)
1048 if (!node)
1049 return -1;
1050 if (node->type != isl_ast_node_if)
1051 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1052 "not an if node", return -1);
1053 return node->u.i.else_node != NULL;
1056 __isl_give isl_ast_node *isl_ast_node_if_get_else(
1057 __isl_keep isl_ast_node *node)
1059 if (!node)
1060 return NULL;
1061 if (node->type != isl_ast_node_if)
1062 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1063 "not an if node", return NULL);
1064 return isl_ast_node_copy(node->u.i.else_node);
1067 __isl_give isl_ast_expr *isl_ast_node_if_get_cond(
1068 __isl_keep isl_ast_node *node)
1070 if (!node)
1071 return NULL;
1072 if (node->type != isl_ast_node_if)
1073 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1074 "not a guard node", return NULL);
1075 return isl_ast_expr_copy(node->u.i.guard);
1078 __isl_give isl_ast_node_list *isl_ast_node_block_get_children(
1079 __isl_keep isl_ast_node *node)
1081 if (!node)
1082 return NULL;
1083 if (node->type != isl_ast_node_block)
1084 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1085 "not a block node", return NULL);
1086 return isl_ast_node_list_copy(node->u.b.children);
1089 __isl_give isl_ast_expr *isl_ast_node_user_get_expr(
1090 __isl_keep isl_ast_node *node)
1092 if (!node)
1093 return NULL;
1095 return isl_ast_expr_copy(node->u.e.expr);
1098 __isl_give isl_id *isl_ast_node_get_annotation(__isl_keep isl_ast_node *node)
1100 return node ? isl_id_copy(node->annotation) : NULL;
1103 /* Replace node->annotation by "annotation".
1105 __isl_give isl_ast_node *isl_ast_node_set_annotation(
1106 __isl_take isl_ast_node *node, __isl_take isl_id *annotation)
1108 node = isl_ast_node_cow(node);
1109 if (!node || !annotation)
1110 goto error;
1112 isl_id_free(node->annotation);
1113 node->annotation = annotation;
1115 return node;
1116 error:
1117 isl_id_free(annotation);
1118 return isl_ast_node_free(node);
1121 /* Textual C representation of the various operators.
1123 static char *op_str[] = {
1124 [isl_ast_op_and] = "&&",
1125 [isl_ast_op_and_then] = "&&",
1126 [isl_ast_op_or] = "||",
1127 [isl_ast_op_or_else] = "||",
1128 [isl_ast_op_max] = "max",
1129 [isl_ast_op_min] = "min",
1130 [isl_ast_op_minus] = "-",
1131 [isl_ast_op_add] = "+",
1132 [isl_ast_op_sub] = "-",
1133 [isl_ast_op_mul] = "*",
1134 [isl_ast_op_pdiv_q] = "/",
1135 [isl_ast_op_pdiv_r] = "%",
1136 [isl_ast_op_div] = "/",
1137 [isl_ast_op_eq] = "==",
1138 [isl_ast_op_le] = "<=",
1139 [isl_ast_op_ge] = ">=",
1140 [isl_ast_op_lt] = "<",
1141 [isl_ast_op_gt] = ">",
1142 [isl_ast_op_member] = "."
1145 /* Precedence in C of the various operators.
1146 * Based on http://en.wikipedia.org/wiki/Operators_in_C_and_C++
1147 * Lowest value means highest precedence.
1149 static int op_prec[] = {
1150 [isl_ast_op_and] = 13,
1151 [isl_ast_op_and_then] = 13,
1152 [isl_ast_op_or] = 14,
1153 [isl_ast_op_or_else] = 14,
1154 [isl_ast_op_max] = 2,
1155 [isl_ast_op_min] = 2,
1156 [isl_ast_op_minus] = 3,
1157 [isl_ast_op_add] = 6,
1158 [isl_ast_op_sub] = 6,
1159 [isl_ast_op_mul] = 5,
1160 [isl_ast_op_div] = 5,
1161 [isl_ast_op_fdiv_q] = 2,
1162 [isl_ast_op_pdiv_q] = 5,
1163 [isl_ast_op_pdiv_r] = 5,
1164 [isl_ast_op_cond] = 15,
1165 [isl_ast_op_select] = 15,
1166 [isl_ast_op_eq] = 9,
1167 [isl_ast_op_le] = 8,
1168 [isl_ast_op_ge] = 8,
1169 [isl_ast_op_lt] = 8,
1170 [isl_ast_op_gt] = 8,
1171 [isl_ast_op_call] = 2,
1172 [isl_ast_op_access] = 2,
1173 [isl_ast_op_member] = 2
1176 /* Is the operator left-to-right associative?
1178 static int op_left[] = {
1179 [isl_ast_op_and] = 1,
1180 [isl_ast_op_and_then] = 1,
1181 [isl_ast_op_or] = 1,
1182 [isl_ast_op_or_else] = 1,
1183 [isl_ast_op_max] = 1,
1184 [isl_ast_op_min] = 1,
1185 [isl_ast_op_minus] = 0,
1186 [isl_ast_op_add] = 1,
1187 [isl_ast_op_sub] = 1,
1188 [isl_ast_op_mul] = 1,
1189 [isl_ast_op_div] = 1,
1190 [isl_ast_op_fdiv_q] = 1,
1191 [isl_ast_op_pdiv_q] = 1,
1192 [isl_ast_op_pdiv_r] = 1,
1193 [isl_ast_op_cond] = 0,
1194 [isl_ast_op_select] = 0,
1195 [isl_ast_op_eq] = 1,
1196 [isl_ast_op_le] = 1,
1197 [isl_ast_op_ge] = 1,
1198 [isl_ast_op_lt] = 1,
1199 [isl_ast_op_gt] = 1,
1200 [isl_ast_op_call] = 1,
1201 [isl_ast_op_access] = 1,
1202 [isl_ast_op_member] = 1
1205 static int is_and(enum isl_ast_op_type op)
1207 return op == isl_ast_op_and || op == isl_ast_op_and_then;
1210 static int is_or(enum isl_ast_op_type op)
1212 return op == isl_ast_op_or || op == isl_ast_op_or_else;
1215 static int is_add_sub(enum isl_ast_op_type op)
1217 return op == isl_ast_op_add || op == isl_ast_op_sub;
1220 static int is_div_mod(enum isl_ast_op_type op)
1222 return op == isl_ast_op_div || op == isl_ast_op_pdiv_r;
1225 /* Do we need/want parentheses around "expr" as a subexpression of
1226 * an "op" operation? If "left" is set, then "expr" is the left-most
1227 * operand.
1229 * We only need parentheses if "expr" represents an operation.
1231 * If op has a higher precedence than expr->u.op.op, then we need
1232 * parentheses.
1233 * If op and expr->u.op.op have the same precedence, but the operations
1234 * are performed in an order that is different from the associativity,
1235 * then we need parentheses.
1237 * An and inside an or technically does not require parentheses,
1238 * but some compilers complain about that, so we add them anyway.
1240 * Computations such as "a / b * c" and "a % b + c" can be somewhat
1241 * difficult to read, so we add parentheses for those as well.
1243 static int sub_expr_need_parens(enum isl_ast_op_type op,
1244 __isl_keep isl_ast_expr *expr, int left)
1246 if (expr->type != isl_ast_expr_op)
1247 return 0;
1249 if (op_prec[expr->u.op.op] > op_prec[op])
1250 return 1;
1251 if (op_prec[expr->u.op.op] == op_prec[op] && left != op_left[op])
1252 return 1;
1254 if (is_or(op) && is_and(expr->u.op.op))
1255 return 1;
1256 if (op == isl_ast_op_mul && expr->u.op.op != isl_ast_op_mul &&
1257 op_prec[expr->u.op.op] == op_prec[op])
1258 return 1;
1259 if (is_add_sub(op) && is_div_mod(expr->u.op.op))
1260 return 1;
1262 return 0;
1265 /* Print "expr" as a subexpression of an "op" operation.
1266 * If "left" is set, then "expr" is the left-most operand.
1268 static __isl_give isl_printer *print_sub_expr(__isl_take isl_printer *p,
1269 enum isl_ast_op_type op, __isl_keep isl_ast_expr *expr, int left)
1271 int need_parens;
1273 need_parens = sub_expr_need_parens(op, expr, left);
1275 if (need_parens)
1276 p = isl_printer_print_str(p, "(");
1277 p = isl_printer_print_ast_expr(p, expr);
1278 if (need_parens)
1279 p = isl_printer_print_str(p, ")");
1280 return p;
1283 /* Print a min or max reduction "expr".
1285 static __isl_give isl_printer *print_min_max(__isl_take isl_printer *p,
1286 __isl_keep isl_ast_expr *expr)
1288 int i = 0;
1290 for (i = 1; i < expr->u.op.n_arg; ++i) {
1291 p = isl_printer_print_str(p, op_str[expr->u.op.op]);
1292 p = isl_printer_print_str(p, "(");
1294 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1295 for (i = 1; i < expr->u.op.n_arg; ++i) {
1296 p = isl_printer_print_str(p, ", ");
1297 p = isl_printer_print_ast_expr(p, expr->u.op.args[i]);
1298 p = isl_printer_print_str(p, ")");
1301 return p;
1304 /* Print a function call "expr".
1306 * The first argument represents the function to be called.
1308 static __isl_give isl_printer *print_call(__isl_take isl_printer *p,
1309 __isl_keep isl_ast_expr *expr)
1311 int i = 0;
1313 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1314 p = isl_printer_print_str(p, "(");
1315 for (i = 1; i < expr->u.op.n_arg; ++i) {
1316 if (i != 1)
1317 p = isl_printer_print_str(p, ", ");
1318 p = isl_printer_print_ast_expr(p, expr->u.op.args[i]);
1320 p = isl_printer_print_str(p, ")");
1322 return p;
1325 /* Print an array access "expr".
1327 * The first argument represents the array being accessed.
1329 static __isl_give isl_printer *print_access(__isl_take isl_printer *p,
1330 __isl_keep isl_ast_expr *expr)
1332 int i = 0;
1334 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1335 for (i = 1; i < expr->u.op.n_arg; ++i) {
1336 p = isl_printer_print_str(p, "[");
1337 p = isl_printer_print_ast_expr(p, expr->u.op.args[i]);
1338 p = isl_printer_print_str(p, "]");
1341 return p;
1344 /* Print "expr" to "p".
1346 * If we are printing in isl format, then we also print an indication
1347 * of the size of the expression (if it was computed).
1349 __isl_give isl_printer *isl_printer_print_ast_expr(__isl_take isl_printer *p,
1350 __isl_keep isl_ast_expr *expr)
1352 if (!p)
1353 return NULL;
1354 if (!expr)
1355 return isl_printer_free(p);
1357 switch (expr->type) {
1358 case isl_ast_expr_op:
1359 if (expr->u.op.op == isl_ast_op_call) {
1360 p = print_call(p, expr);
1361 break;
1363 if (expr->u.op.op == isl_ast_op_access) {
1364 p = print_access(p, expr);
1365 break;
1367 if (expr->u.op.n_arg == 1) {
1368 p = isl_printer_print_str(p, op_str[expr->u.op.op]);
1369 p = print_sub_expr(p, expr->u.op.op,
1370 expr->u.op.args[0], 0);
1371 break;
1373 if (expr->u.op.op == isl_ast_op_fdiv_q) {
1374 p = isl_printer_print_str(p, "floord(");
1375 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1376 p = isl_printer_print_str(p, ", ");
1377 p = isl_printer_print_ast_expr(p, expr->u.op.args[1]);
1378 p = isl_printer_print_str(p, ")");
1379 break;
1381 if (expr->u.op.op == isl_ast_op_max ||
1382 expr->u.op.op == isl_ast_op_min) {
1383 p = print_min_max(p, expr);
1384 break;
1386 if (expr->u.op.op == isl_ast_op_cond ||
1387 expr->u.op.op == isl_ast_op_select) {
1388 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1389 p = isl_printer_print_str(p, " ? ");
1390 p = isl_printer_print_ast_expr(p, expr->u.op.args[1]);
1391 p = isl_printer_print_str(p, " : ");
1392 p = isl_printer_print_ast_expr(p, expr->u.op.args[2]);
1393 break;
1395 if (expr->u.op.n_arg != 2)
1396 isl_die(isl_printer_get_ctx(p), isl_error_internal,
1397 "operation should have two arguments",
1398 goto error);
1399 p = print_sub_expr(p, expr->u.op.op, expr->u.op.args[0], 1);
1400 if (expr->u.op.op != isl_ast_op_member)
1401 p = isl_printer_print_str(p, " ");
1402 p = isl_printer_print_str(p, op_str[expr->u.op.op]);
1403 if (expr->u.op.op != isl_ast_op_member)
1404 p = isl_printer_print_str(p, " ");
1405 p = print_sub_expr(p, expr->u.op.op, expr->u.op.args[1], 0);
1406 break;
1407 case isl_ast_expr_id:
1408 p = isl_printer_print_str(p, isl_id_get_name(expr->u.id));
1409 break;
1410 case isl_ast_expr_int:
1411 p = isl_printer_print_val(p, expr->u.v);
1412 break;
1413 case isl_ast_expr_error:
1414 break;
1417 return p;
1418 error:
1419 isl_printer_free(p);
1420 return NULL;
1423 /* Print "node" to "p" in "isl format".
1425 static __isl_give isl_printer *print_ast_node_isl(__isl_take isl_printer *p,
1426 __isl_keep isl_ast_node *node)
1428 p = isl_printer_print_str(p, "(");
1429 switch (node->type) {
1430 case isl_ast_node_for:
1431 if (node->u.f.degenerate) {
1432 p = isl_printer_print_ast_expr(p, node->u.f.init);
1433 } else {
1434 p = isl_printer_print_str(p, "init: ");
1435 p = isl_printer_print_ast_expr(p, node->u.f.init);
1436 p = isl_printer_print_str(p, ", ");
1437 p = isl_printer_print_str(p, "cond: ");
1438 p = isl_printer_print_ast_expr(p, node->u.f.cond);
1439 p = isl_printer_print_str(p, ", ");
1440 p = isl_printer_print_str(p, "inc: ");
1441 p = isl_printer_print_ast_expr(p, node->u.f.inc);
1443 if (node->u.f.body) {
1444 p = isl_printer_print_str(p, ", ");
1445 p = isl_printer_print_str(p, "body: ");
1446 p = isl_printer_print_ast_node(p, node->u.f.body);
1448 break;
1449 case isl_ast_node_user:
1450 p = isl_printer_print_ast_expr(p, node->u.e.expr);
1451 break;
1452 case isl_ast_node_if:
1453 p = isl_printer_print_str(p, "guard: ");
1454 p = isl_printer_print_ast_expr(p, node->u.i.guard);
1455 if (node->u.i.then) {
1456 p = isl_printer_print_str(p, ", ");
1457 p = isl_printer_print_str(p, "then: ");
1458 p = isl_printer_print_ast_node(p, node->u.i.then);
1460 if (node->u.i.else_node) {
1461 p = isl_printer_print_str(p, ", ");
1462 p = isl_printer_print_str(p, "else: ");
1463 p = isl_printer_print_ast_node(p, node->u.i.else_node);
1465 break;
1466 case isl_ast_node_block:
1467 p = isl_printer_print_ast_node_list(p, node->u.b.children);
1468 break;
1469 default:
1470 break;
1472 p = isl_printer_print_str(p, ")");
1473 return p;
1476 /* Do we need to print a block around the body "node" of a for or if node?
1478 * If the node is a block, then we need to print a block.
1479 * Also if the node is a degenerate for then we will print it as
1480 * an assignment followed by the body of the for loop, so we need a block
1481 * as well.
1483 static int need_block(__isl_keep isl_ast_node *node)
1485 if (node->type == isl_ast_node_block)
1486 return 1;
1487 if (node->type == isl_ast_node_for && node->u.f.degenerate)
1488 return 1;
1489 return 0;
1492 static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
1493 __isl_keep isl_ast_node *node,
1494 __isl_keep isl_ast_print_options *options, int in_block, int in_list);
1495 static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
1496 __isl_keep isl_ast_node *node,
1497 __isl_keep isl_ast_print_options *options, int new_line);
1499 /* Print the body "node" of a for or if node.
1500 * If "else_node" is set, then it is printed as well.
1502 * We first check if we need to print out a block.
1503 * We always print out a block if there is an else node to make
1504 * sure that the else node is matched to the correct if node.
1506 * If the else node is itself an if, then we print it as
1508 * } else if (..)
1510 * Otherwise the else node is printed as
1512 * } else
1513 * node
1515 static __isl_give isl_printer *print_body_c(__isl_take isl_printer *p,
1516 __isl_keep isl_ast_node *node, __isl_keep isl_ast_node *else_node,
1517 __isl_keep isl_ast_print_options *options)
1519 if (!node)
1520 return isl_printer_free(p);
1522 if (!else_node && !need_block(node)) {
1523 p = isl_printer_end_line(p);
1524 p = isl_printer_indent(p, 2);
1525 p = isl_ast_node_print(node, p,
1526 isl_ast_print_options_copy(options));
1527 p = isl_printer_indent(p, -2);
1528 return p;
1531 p = isl_printer_print_str(p, " {");
1532 p = isl_printer_end_line(p);
1533 p = isl_printer_indent(p, 2);
1534 p = print_ast_node_c(p, node, options, 1, 0);
1535 p = isl_printer_indent(p, -2);
1536 p = isl_printer_start_line(p);
1537 p = isl_printer_print_str(p, "}");
1538 if (else_node) {
1539 if (else_node->type == isl_ast_node_if) {
1540 p = isl_printer_print_str(p, " else ");
1541 p = print_if_c(p, else_node, options, 0);
1542 } else {
1543 p = isl_printer_print_str(p, " else");
1544 p = print_body_c(p, else_node, NULL, options);
1546 } else
1547 p = isl_printer_end_line(p);
1549 return p;
1552 /* Print the start of a compound statement.
1554 static __isl_give isl_printer *start_block(__isl_take isl_printer *p)
1556 p = isl_printer_start_line(p);
1557 p = isl_printer_print_str(p, "{");
1558 p = isl_printer_end_line(p);
1559 p = isl_printer_indent(p, 2);
1561 return p;
1564 /* Print the end of a compound statement.
1566 static __isl_give isl_printer *end_block(__isl_take isl_printer *p)
1568 p = isl_printer_indent(p, -2);
1569 p = isl_printer_start_line(p);
1570 p = isl_printer_print_str(p, "}");
1571 p = isl_printer_end_line(p);
1573 return p;
1576 /* Print the for node "node".
1578 * If the for node is degenerate, it is printed as
1580 * type iterator = init;
1581 * body
1583 * Otherwise, it is printed as
1585 * for (type iterator = init; cond; iterator += inc)
1586 * body
1588 * "in_block" is set if we are currently inside a block.
1589 * "in_list" is set if the current node is not alone in the block.
1590 * If we are not in a block or if the current not is not alone in the block
1591 * then we print a block around a degenerate for loop such that the variable
1592 * declaration will not conflict with any potential other declaration
1593 * of the same variable.
1595 static __isl_give isl_printer *print_for_c(__isl_take isl_printer *p,
1596 __isl_keep isl_ast_node *node,
1597 __isl_keep isl_ast_print_options *options, int in_block, int in_list)
1599 isl_id *id;
1600 const char *name;
1601 const char *type;
1603 type = isl_options_get_ast_iterator_type(isl_printer_get_ctx(p));
1604 if (!node->u.f.degenerate) {
1605 id = isl_ast_expr_get_id(node->u.f.iterator);
1606 name = isl_id_get_name(id);
1607 isl_id_free(id);
1608 p = isl_printer_start_line(p);
1609 p = isl_printer_print_str(p, "for (");
1610 p = isl_printer_print_str(p, type);
1611 p = isl_printer_print_str(p, " ");
1612 p = isl_printer_print_str(p, name);
1613 p = isl_printer_print_str(p, " = ");
1614 p = isl_printer_print_ast_expr(p, node->u.f.init);
1615 p = isl_printer_print_str(p, "; ");
1616 p = isl_printer_print_ast_expr(p, node->u.f.cond);
1617 p = isl_printer_print_str(p, "; ");
1618 p = isl_printer_print_str(p, name);
1619 p = isl_printer_print_str(p, " += ");
1620 p = isl_printer_print_ast_expr(p, node->u.f.inc);
1621 p = isl_printer_print_str(p, ")");
1622 p = print_body_c(p, node->u.f.body, NULL, options);
1623 } else {
1624 id = isl_ast_expr_get_id(node->u.f.iterator);
1625 name = isl_id_get_name(id);
1626 isl_id_free(id);
1627 if (!in_block || in_list)
1628 p = start_block(p);
1629 p = isl_printer_start_line(p);
1630 p = isl_printer_print_str(p, type);
1631 p = isl_printer_print_str(p, " ");
1632 p = isl_printer_print_str(p, name);
1633 p = isl_printer_print_str(p, " = ");
1634 p = isl_printer_print_ast_expr(p, node->u.f.init);
1635 p = isl_printer_print_str(p, ";");
1636 p = isl_printer_end_line(p);
1637 p = print_ast_node_c(p, node->u.f.body, options, 1, 0);
1638 if (!in_block || in_list)
1639 p = end_block(p);
1642 return p;
1645 /* Print the if node "node".
1646 * If "new_line" is set then the if node should be printed on a new line.
1648 static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
1649 __isl_keep isl_ast_node *node,
1650 __isl_keep isl_ast_print_options *options, int new_line)
1652 if (new_line)
1653 p = isl_printer_start_line(p);
1654 p = isl_printer_print_str(p, "if (");
1655 p = isl_printer_print_ast_expr(p, node->u.i.guard);
1656 p = isl_printer_print_str(p, ")");
1657 p = print_body_c(p, node->u.i.then, node->u.i.else_node, options);
1659 return p;
1662 /* Print the "node" to "p".
1664 * "in_block" is set if we are currently inside a block.
1665 * If so, we do not print a block around the children of a block node.
1666 * We do this to avoid an extra block around the body of a degenerate
1667 * for node.
1669 * "in_list" is set if the current node is not alone in the block.
1671 static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
1672 __isl_keep isl_ast_node *node,
1673 __isl_keep isl_ast_print_options *options, int in_block, int in_list)
1675 switch (node->type) {
1676 case isl_ast_node_for:
1677 if (options->print_for)
1678 return options->print_for(p,
1679 isl_ast_print_options_copy(options),
1680 node, options->print_for_user);
1681 p = print_for_c(p, node, options, in_block, in_list);
1682 break;
1683 case isl_ast_node_if:
1684 p = print_if_c(p, node, options, 1);
1685 break;
1686 case isl_ast_node_block:
1687 if (!in_block)
1688 p = start_block(p);
1689 p = isl_ast_node_list_print(node->u.b.children, p, options);
1690 if (!in_block)
1691 p = end_block(p);
1692 break;
1693 case isl_ast_node_user:
1694 if (options->print_user)
1695 return options->print_user(p,
1696 isl_ast_print_options_copy(options),
1697 node, options->print_user_user);
1698 p = isl_printer_start_line(p);
1699 p = isl_printer_print_ast_expr(p, node->u.e.expr);
1700 p = isl_printer_print_str(p, ";");
1701 p = isl_printer_end_line(p);
1702 break;
1703 case isl_ast_node_error:
1704 break;
1706 return p;
1709 /* Print the for node "node" to "p".
1711 __isl_give isl_printer *isl_ast_node_for_print(__isl_keep isl_ast_node *node,
1712 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
1714 if (!node || !options)
1715 goto error;
1716 if (node->type != isl_ast_node_for)
1717 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1718 "not a for node", goto error);
1719 p = print_for_c(p, node, options, 0, 0);
1720 isl_ast_print_options_free(options);
1721 return p;
1722 error:
1723 isl_ast_print_options_free(options);
1724 isl_printer_free(p);
1725 return NULL;
1728 /* Print the if node "node" to "p".
1730 __isl_give isl_printer *isl_ast_node_if_print(__isl_keep isl_ast_node *node,
1731 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
1733 if (!node || !options)
1734 goto error;
1735 if (node->type != isl_ast_node_if)
1736 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1737 "not an if node", goto error);
1738 p = print_if_c(p, node, options, 1);
1739 isl_ast_print_options_free(options);
1740 return p;
1741 error:
1742 isl_ast_print_options_free(options);
1743 isl_printer_free(p);
1744 return NULL;
1747 /* Print "node" to "p".
1749 __isl_give isl_printer *isl_ast_node_print(__isl_keep isl_ast_node *node,
1750 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
1752 if (!options || !node)
1753 goto error;
1754 p = print_ast_node_c(p, node, options, 0, 0);
1755 isl_ast_print_options_free(options);
1756 return p;
1757 error:
1758 isl_ast_print_options_free(options);
1759 isl_printer_free(p);
1760 return NULL;
1763 /* Print "node" to "p".
1765 __isl_give isl_printer *isl_printer_print_ast_node(__isl_take isl_printer *p,
1766 __isl_keep isl_ast_node *node)
1768 int format;
1769 isl_ast_print_options *options;
1771 if (!p)
1772 return NULL;
1774 format = isl_printer_get_output_format(p);
1775 switch (format) {
1776 case ISL_FORMAT_ISL:
1777 p = print_ast_node_isl(p, node);
1778 break;
1779 case ISL_FORMAT_C:
1780 options = isl_ast_print_options_alloc(isl_printer_get_ctx(p));
1781 p = isl_ast_node_print(node, p, options);
1782 break;
1783 default:
1784 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
1785 "output format not supported for ast_node",
1786 return isl_printer_free(p));
1789 return p;
1792 /* Print the list of nodes "list" to "p".
1794 __isl_give isl_printer *isl_ast_node_list_print(
1795 __isl_keep isl_ast_node_list *list, __isl_take isl_printer *p,
1796 __isl_keep isl_ast_print_options *options)
1798 int i;
1800 if (!p || !list || !options)
1801 return isl_printer_free(p);
1803 for (i = 0; i < list->n; ++i)
1804 p = print_ast_node_c(p, list->p[i], options, 1, 1);
1806 return p;
1809 #define ISL_AST_MACRO_FLOORD (1 << 0)
1810 #define ISL_AST_MACRO_MIN (1 << 1)
1811 #define ISL_AST_MACRO_MAX (1 << 2)
1812 #define ISL_AST_MACRO_ALL (ISL_AST_MACRO_FLOORD | \
1813 ISL_AST_MACRO_MIN | \
1814 ISL_AST_MACRO_MAX)
1816 /* If "expr" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1817 * then set the corresponding bit in "macros".
1819 static int ast_expr_required_macros(__isl_keep isl_ast_expr *expr, int macros)
1821 int i;
1823 if (macros == ISL_AST_MACRO_ALL)
1824 return macros;
1826 if (expr->type != isl_ast_expr_op)
1827 return macros;
1829 if (expr->u.op.op == isl_ast_op_min)
1830 macros |= ISL_AST_MACRO_MIN;
1831 if (expr->u.op.op == isl_ast_op_max)
1832 macros |= ISL_AST_MACRO_MAX;
1833 if (expr->u.op.op == isl_ast_op_fdiv_q)
1834 macros |= ISL_AST_MACRO_FLOORD;
1836 for (i = 0; i < expr->u.op.n_arg; ++i)
1837 macros = ast_expr_required_macros(expr->u.op.args[i], macros);
1839 return macros;
1842 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
1843 int macros);
1845 /* If "node" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1846 * then set the corresponding bit in "macros".
1848 static int ast_node_required_macros(__isl_keep isl_ast_node *node, int macros)
1850 if (macros == ISL_AST_MACRO_ALL)
1851 return macros;
1853 switch (node->type) {
1854 case isl_ast_node_for:
1855 macros = ast_expr_required_macros(node->u.f.init, macros);
1856 if (!node->u.f.degenerate) {
1857 macros = ast_expr_required_macros(node->u.f.cond,
1858 macros);
1859 macros = ast_expr_required_macros(node->u.f.inc,
1860 macros);
1862 macros = ast_node_required_macros(node->u.f.body, macros);
1863 break;
1864 case isl_ast_node_if:
1865 macros = ast_expr_required_macros(node->u.i.guard, macros);
1866 macros = ast_node_required_macros(node->u.i.then, macros);
1867 if (node->u.i.else_node)
1868 macros = ast_node_required_macros(node->u.i.else_node,
1869 macros);
1870 break;
1871 case isl_ast_node_block:
1872 macros = ast_node_list_required_macros(node->u.b.children,
1873 macros);
1874 break;
1875 case isl_ast_node_user:
1876 macros = ast_expr_required_macros(node->u.e.expr, macros);
1877 break;
1878 case isl_ast_node_error:
1879 break;
1882 return macros;
1885 /* If "list" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1886 * then set the corresponding bit in "macros".
1888 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
1889 int macros)
1891 int i;
1893 for (i = 0; i < list->n; ++i)
1894 macros = ast_node_required_macros(list->p[i], macros);
1896 return macros;
1899 /* Print a macro definition for the operator "type".
1901 __isl_give isl_printer *isl_ast_op_type_print_macro(
1902 enum isl_ast_op_type type, __isl_take isl_printer *p)
1904 switch (type) {
1905 case isl_ast_op_min:
1906 p = isl_printer_start_line(p);
1907 p = isl_printer_print_str(p,
1908 "#define min(x,y) ((x) < (y) ? (x) : (y))");
1909 p = isl_printer_end_line(p);
1910 break;
1911 case isl_ast_op_max:
1912 p = isl_printer_start_line(p);
1913 p = isl_printer_print_str(p,
1914 "#define max(x,y) ((x) > (y) ? (x) : (y))");
1915 p = isl_printer_end_line(p);
1916 break;
1917 case isl_ast_op_fdiv_q:
1918 p = isl_printer_start_line(p);
1919 p = isl_printer_print_str(p,
1920 "#define floord(n,d) "
1921 "(((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))");
1922 p = isl_printer_end_line(p);
1923 break;
1924 default:
1925 break;
1928 return p;
1931 /* Call "fn" for each type of operation that appears in "node"
1932 * and that requires a macro definition.
1934 int isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node *node,
1935 int (*fn)(enum isl_ast_op_type type, void *user), void *user)
1937 int macros;
1939 if (!node)
1940 return -1;
1942 macros = ast_node_required_macros(node, 0);
1944 if (macros & ISL_AST_MACRO_MIN && fn(isl_ast_op_min, user) < 0)
1945 return -1;
1946 if (macros & ISL_AST_MACRO_MAX && fn(isl_ast_op_max, user) < 0)
1947 return -1;
1948 if (macros & ISL_AST_MACRO_FLOORD && fn(isl_ast_op_fdiv_q, user) < 0)
1949 return -1;
1951 return 0;
1954 static int ast_op_type_print_macro(enum isl_ast_op_type type, void *user)
1956 isl_printer **p = user;
1958 *p = isl_ast_op_type_print_macro(type, *p);
1960 return 0;
1963 /* Print macro definitions for all the macros used in the result
1964 * of printing "node.
1966 __isl_give isl_printer *isl_ast_node_print_macros(
1967 __isl_keep isl_ast_node *node, __isl_take isl_printer *p)
1969 if (isl_ast_node_foreach_ast_op_type(node,
1970 &ast_op_type_print_macro, &p) < 0)
1971 return isl_printer_free(p);
1972 return p;