1 #include <isl_ast_private.h>
2 #include <isl/val_int.h>
7 #include <isl_list_templ.c>
12 #include <isl_list_templ.c>
14 isl_ctx *isl_ast_print_options_get_ctx(
15 __isl_keep isl_ast_print_options *options)
17 return options ? options->ctx : NULL;
20 __isl_give isl_ast_print_options *isl_ast_print_options_alloc(isl_ctx *ctx)
22 isl_ast_print_options *options;
24 options = isl_calloc_type(ctx, isl_ast_print_options);
35 __isl_give isl_ast_print_options *isl_ast_print_options_dup(
36 __isl_keep isl_ast_print_options *options)
39 isl_ast_print_options *dup;
44 ctx = isl_ast_print_options_get_ctx(options);
45 dup = isl_ast_print_options_alloc(ctx);
49 dup->print_for = options->print_for;
50 dup->print_for_user = options->print_for_user;
51 dup->print_user = options->print_user;
52 dup->print_user_user = options->print_user_user;
57 __isl_give isl_ast_print_options *isl_ast_print_options_cow(
58 __isl_take isl_ast_print_options *options)
63 if (options->ref == 1)
66 return isl_ast_print_options_dup(options);
69 __isl_give isl_ast_print_options *isl_ast_print_options_copy(
70 __isl_keep isl_ast_print_options *options)
79 void *isl_ast_print_options_free(__isl_take isl_ast_print_options *options)
84 if (--options->ref > 0)
87 isl_ctx_deref(options->ctx);
93 /* Set the print_user callback of "options" to "print_user".
95 * If this callback is set, then it used to print user nodes in the AST.
96 * Otherwise, the expression associated to the user node is printed.
98 __isl_give isl_ast_print_options *isl_ast_print_options_set_print_user(
99 __isl_take isl_ast_print_options *options,
100 __isl_give isl_printer *(*print_user)(__isl_take isl_printer *p,
101 __isl_take isl_ast_print_options *options,
102 __isl_keep isl_ast_node *node, void *user),
105 options = isl_ast_print_options_cow(options);
109 options->print_user = print_user;
110 options->print_user_user = user;
115 /* Set the print_for callback of "options" to "print_for".
117 * If this callback is set, then it used to print for nodes in the AST.
119 __isl_give isl_ast_print_options *isl_ast_print_options_set_print_for(
120 __isl_take isl_ast_print_options *options,
121 __isl_give isl_printer *(*print_for)(__isl_take isl_printer *p,
122 __isl_take isl_ast_print_options *options,
123 __isl_keep isl_ast_node *node, void *user),
126 options = isl_ast_print_options_cow(options);
130 options->print_for = print_for;
131 options->print_for_user = user;
136 __isl_give isl_ast_expr *isl_ast_expr_copy(__isl_keep isl_ast_expr *expr)
145 __isl_give isl_ast_expr *isl_ast_expr_dup(__isl_keep isl_ast_expr *expr)
154 ctx = isl_ast_expr_get_ctx(expr);
155 switch (expr->type) {
156 case isl_ast_expr_int:
157 dup = isl_ast_expr_alloc_int(ctx, expr->u.i);
159 case isl_ast_expr_id:
160 dup = isl_ast_expr_from_id(isl_id_copy(expr->u.id));
162 case isl_ast_expr_op:
163 dup = isl_ast_expr_alloc_op(ctx,
164 expr->u.op.op, expr->u.op.n_arg);
167 for (i = 0; i < expr->u.op.n_arg; ++i)
169 isl_ast_expr_copy(expr->u.op.args[i]);
171 case isl_ast_expr_error:
181 __isl_give isl_ast_expr *isl_ast_expr_cow(__isl_take isl_ast_expr *expr)
189 return isl_ast_expr_dup(expr);
192 void *isl_ast_expr_free(__isl_take isl_ast_expr *expr)
202 isl_ctx_deref(expr->ctx);
204 switch (expr->type) {
205 case isl_ast_expr_int:
206 isl_int_clear(expr->u.i);
208 case isl_ast_expr_id:
209 isl_id_free(expr->u.id);
211 case isl_ast_expr_op:
212 for (i = 0; i < expr->u.op.n_arg; ++i)
213 isl_ast_expr_free(expr->u.op.args[i]);
214 free(expr->u.op.args);
216 case isl_ast_expr_error:
224 isl_ctx *isl_ast_expr_get_ctx(__isl_keep isl_ast_expr *expr)
226 return expr ? expr->ctx : NULL;
229 enum isl_ast_expr_type isl_ast_expr_get_type(__isl_keep isl_ast_expr *expr)
231 return expr ? expr->type : isl_ast_expr_error;
234 int isl_ast_expr_get_int(__isl_keep isl_ast_expr *expr, isl_int *v)
238 if (expr->type != isl_ast_expr_int)
239 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
240 "expression not an int", return -1);
241 isl_int_set(*v, expr->u.i);
245 /* Return the integer value represented by "expr".
247 __isl_give isl_val *isl_ast_expr_get_val(__isl_keep isl_ast_expr *expr)
253 ctx = isl_ast_expr_get_ctx(expr);
254 if (expr->type != isl_ast_expr_int)
255 isl_die(ctx, isl_error_invalid,
256 "expression not an int", return NULL);
257 return isl_val_int_from_isl_int(ctx, expr->u.i);
260 __isl_give isl_id *isl_ast_expr_get_id(__isl_keep isl_ast_expr *expr)
264 if (expr->type != isl_ast_expr_id)
265 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
266 "expression not an identifier", return NULL);
268 return isl_id_copy(expr->u.id);
271 enum isl_ast_op_type isl_ast_expr_get_op_type(__isl_keep isl_ast_expr *expr)
274 return isl_ast_op_error;
275 if (expr->type != isl_ast_expr_op)
276 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
277 "expression not an operation", return isl_ast_op_error);
278 return expr->u.op.op;
281 int isl_ast_expr_get_op_n_arg(__isl_keep isl_ast_expr *expr)
285 if (expr->type != isl_ast_expr_op)
286 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
287 "expression not an operation", return -1);
288 return expr->u.op.n_arg;
291 __isl_give isl_ast_expr *isl_ast_expr_get_op_arg(__isl_keep isl_ast_expr *expr,
296 if (expr->type != isl_ast_expr_op)
297 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
298 "expression not an operation", return NULL);
299 if (pos < 0 || pos >= expr->u.op.n_arg)
300 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
301 "index out of bounds", return NULL);
303 return isl_ast_expr_copy(expr->u.op.args[pos]);
306 /* Replace the argument at position "pos" of "expr" by "arg".
308 __isl_give isl_ast_expr *isl_ast_expr_set_op_arg(__isl_take isl_ast_expr *expr,
309 int pos, __isl_take isl_ast_expr *arg)
311 expr = isl_ast_expr_cow(expr);
314 if (expr->type != isl_ast_expr_op)
315 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
316 "expression not an operation", goto error);
317 if (pos < 0 || pos >= expr->u.op.n_arg)
318 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
319 "index out of bounds", goto error);
321 isl_ast_expr_free(expr->u.op.args[pos]);
322 expr->u.op.args[pos] = arg;
326 isl_ast_expr_free(arg);
327 return isl_ast_expr_free(expr);
330 /* Create a new operation expression of operation type "op",
331 * with "n_arg" as yet unspecified arguments.
333 __isl_give isl_ast_expr *isl_ast_expr_alloc_op(isl_ctx *ctx,
334 enum isl_ast_op_type op, int n_arg)
338 expr = isl_calloc_type(ctx, isl_ast_expr);
345 expr->type = isl_ast_expr_op;
347 expr->u.op.n_arg = n_arg;
348 expr->u.op.args = isl_calloc_array(ctx, isl_ast_expr *, n_arg);
350 if (!expr->u.op.args)
351 return isl_ast_expr_free(expr);
356 /* Create a new id expression representing "id".
358 __isl_give isl_ast_expr *isl_ast_expr_from_id(__isl_take isl_id *id)
366 ctx = isl_id_get_ctx(id);
367 expr = isl_calloc_type(ctx, isl_ast_expr);
369 return isl_id_free(id);
374 expr->type = isl_ast_expr_id;
380 /* Create a new integer expression representing "i".
382 __isl_give isl_ast_expr *isl_ast_expr_alloc_int_si(isl_ctx *ctx, int i)
386 expr = isl_calloc_type(ctx, isl_ast_expr);
393 expr->type = isl_ast_expr_int;
395 isl_int_init(expr->u.i);
396 isl_int_set_si(expr->u.i, i);
401 /* Create a new integer expression representing "i".
403 __isl_give isl_ast_expr *isl_ast_expr_alloc_int(isl_ctx *ctx, isl_int i)
407 expr = isl_calloc_type(ctx, isl_ast_expr);
414 expr->type = isl_ast_expr_int;
416 isl_int_init(expr->u.i);
417 isl_int_set(expr->u.i, i);
422 /* Create an expression representing the negation of "arg".
424 __isl_give isl_ast_expr *isl_ast_expr_neg(__isl_take isl_ast_expr *arg)
427 isl_ast_expr *expr = NULL;
432 ctx = isl_ast_expr_get_ctx(arg);
433 expr = isl_ast_expr_alloc_op(ctx, isl_ast_op_minus, 1);
437 expr->u.op.args[0] = arg;
441 isl_ast_expr_free(arg);
445 /* Create an expression representing the binary operation "type"
446 * applied to "expr1" and "expr2".
448 __isl_give isl_ast_expr *isl_ast_expr_alloc_binary(enum isl_ast_op_type type,
449 __isl_take isl_ast_expr *expr1, __isl_take isl_ast_expr *expr2)
452 isl_ast_expr *expr = NULL;
454 if (!expr1 || !expr2)
457 ctx = isl_ast_expr_get_ctx(expr1);
458 expr = isl_ast_expr_alloc_op(ctx, type, 2);
462 expr->u.op.args[0] = expr1;
463 expr->u.op.args[1] = expr2;
467 isl_ast_expr_free(expr1);
468 isl_ast_expr_free(expr2);
472 /* Create an expression representing the sum of "expr1" and "expr2".
474 __isl_give isl_ast_expr *isl_ast_expr_add(__isl_take isl_ast_expr *expr1,
475 __isl_take isl_ast_expr *expr2)
477 return isl_ast_expr_alloc_binary(isl_ast_op_add, expr1, expr2);
480 /* Create an expression representing the difference of "expr1" and "expr2".
482 __isl_give isl_ast_expr *isl_ast_expr_sub(__isl_take isl_ast_expr *expr1,
483 __isl_take isl_ast_expr *expr2)
485 return isl_ast_expr_alloc_binary(isl_ast_op_sub, expr1, expr2);
488 /* Create an expression representing the product of "expr1" and "expr2".
490 __isl_give isl_ast_expr *isl_ast_expr_mul(__isl_take isl_ast_expr *expr1,
491 __isl_take isl_ast_expr *expr2)
493 return isl_ast_expr_alloc_binary(isl_ast_op_mul, expr1, expr2);
496 /* Create an expression representing the quotient of "expr1" and "expr2".
498 __isl_give isl_ast_expr *isl_ast_expr_div(__isl_take isl_ast_expr *expr1,
499 __isl_take isl_ast_expr *expr2)
501 return isl_ast_expr_alloc_binary(isl_ast_op_div, expr1, expr2);
504 /* Create an expression representing the conjunction of "expr1" and "expr2".
506 __isl_give isl_ast_expr *isl_ast_expr_and(__isl_take isl_ast_expr *expr1,
507 __isl_take isl_ast_expr *expr2)
509 return isl_ast_expr_alloc_binary(isl_ast_op_and, expr1, expr2);
512 /* Create an expression representing the disjunction of "expr1" and "expr2".
514 __isl_give isl_ast_expr *isl_ast_expr_or(__isl_take isl_ast_expr *expr1,
515 __isl_take isl_ast_expr *expr2)
517 return isl_ast_expr_alloc_binary(isl_ast_op_or, expr1, expr2);
520 isl_ctx *isl_ast_node_get_ctx(__isl_keep isl_ast_node *node)
522 return node ? node->ctx : NULL;
525 enum isl_ast_node_type isl_ast_node_get_type(__isl_keep isl_ast_node *node)
527 return node ? node->type : isl_ast_node_error;
530 __isl_give isl_ast_node *isl_ast_node_alloc(isl_ctx *ctx,
531 enum isl_ast_node_type type)
535 node = isl_calloc_type(ctx, isl_ast_node);
547 /* Create an if node with the given guard.
549 * The then body needs to be filled in later.
551 __isl_give isl_ast_node *isl_ast_node_alloc_if(__isl_take isl_ast_expr *guard)
558 node = isl_ast_node_alloc(isl_ast_expr_get_ctx(guard), isl_ast_node_if);
561 node->u.i.guard = guard;
565 isl_ast_expr_free(guard);
569 /* Create a for node with the given iterator.
571 * The remaining fields need to be filled in later.
573 __isl_give isl_ast_node *isl_ast_node_alloc_for(__isl_take isl_id *id)
581 ctx = isl_id_get_ctx(id);
582 node = isl_ast_node_alloc(ctx, isl_ast_node_for);
586 node->u.f.iterator = isl_ast_expr_from_id(id);
587 if (!node->u.f.iterator)
588 return isl_ast_node_free(node);
593 /* Create a user node evaluating "expr".
595 __isl_give isl_ast_node *isl_ast_node_alloc_user(__isl_take isl_ast_expr *expr)
603 ctx = isl_ast_expr_get_ctx(expr);
604 node = isl_ast_node_alloc(ctx, isl_ast_node_user);
608 node->u.e.expr = expr;
612 isl_ast_expr_free(expr);
616 /* Create a block node with the given children.
618 __isl_give isl_ast_node *isl_ast_node_alloc_block(
619 __isl_take isl_ast_node_list *list)
627 ctx = isl_ast_node_list_get_ctx(list);
628 node = isl_ast_node_alloc(ctx, isl_ast_node_block);
632 node->u.b.children = list;
636 isl_ast_node_list_free(list);
640 /* Represent the given list of nodes as a single node, either by
641 * extract the node from a single element list or by creating
642 * a block node with the list of nodes as children.
644 __isl_give isl_ast_node *isl_ast_node_from_ast_node_list(
645 __isl_take isl_ast_node_list *list)
649 if (isl_ast_node_list_n_ast_node(list) != 1)
650 return isl_ast_node_alloc_block(list);
652 node = isl_ast_node_list_get_ast_node(list, 0);
653 isl_ast_node_list_free(list);
658 __isl_give isl_ast_node *isl_ast_node_copy(__isl_keep isl_ast_node *node)
667 __isl_give isl_ast_node *isl_ast_node_dup(__isl_keep isl_ast_node *node)
674 dup = isl_ast_node_alloc(isl_ast_node_get_ctx(node), node->type);
678 switch (node->type) {
679 case isl_ast_node_if:
680 dup->u.i.guard = isl_ast_expr_copy(node->u.i.guard);
681 dup->u.i.then = isl_ast_node_copy(node->u.i.then);
682 dup->u.i.else_node = isl_ast_node_copy(node->u.i.else_node);
683 if (!dup->u.i.guard || !dup->u.i.then ||
684 (node->u.i.else_node && !dup->u.i.else_node))
685 return isl_ast_node_free(dup);
687 case isl_ast_node_for:
688 dup->u.f.iterator = isl_ast_expr_copy(node->u.f.iterator);
689 dup->u.f.init = isl_ast_expr_copy(node->u.f.init);
690 dup->u.f.cond = isl_ast_expr_copy(node->u.f.cond);
691 dup->u.f.inc = isl_ast_expr_copy(node->u.f.inc);
692 dup->u.f.body = isl_ast_node_copy(node->u.f.body);
693 if (!dup->u.f.iterator || !dup->u.f.init || !dup->u.f.cond ||
694 !dup->u.f.inc || !dup->u.f.body)
695 return isl_ast_node_free(dup);
697 case isl_ast_node_block:
698 dup->u.b.children = isl_ast_node_list_copy(node->u.b.children);
699 if (!dup->u.b.children)
700 return isl_ast_node_free(dup);
702 case isl_ast_node_user:
703 dup->u.e.expr = isl_ast_expr_copy(node->u.e.expr);
705 return isl_ast_node_free(dup);
707 case isl_ast_node_error:
714 __isl_give isl_ast_node *isl_ast_node_cow(__isl_take isl_ast_node *node)
722 return isl_ast_node_dup(node);
725 void *isl_ast_node_free(__isl_take isl_ast_node *node)
733 switch (node->type) {
734 case isl_ast_node_if:
735 isl_ast_expr_free(node->u.i.guard);
736 isl_ast_node_free(node->u.i.then);
737 isl_ast_node_free(node->u.i.else_node);
739 case isl_ast_node_for:
740 isl_ast_expr_free(node->u.f.iterator);
741 isl_ast_expr_free(node->u.f.init);
742 isl_ast_expr_free(node->u.f.cond);
743 isl_ast_expr_free(node->u.f.inc);
744 isl_ast_node_free(node->u.f.body);
746 case isl_ast_node_block:
747 isl_ast_node_list_free(node->u.b.children);
749 case isl_ast_node_user:
750 isl_ast_expr_free(node->u.e.expr);
752 case isl_ast_node_error:
756 isl_id_free(node->annotation);
757 isl_ctx_deref(node->ctx);
763 /* Replace the body of the for node "node" by "body".
765 __isl_give isl_ast_node *isl_ast_node_for_set_body(
766 __isl_take isl_ast_node *node, __isl_take isl_ast_node *body)
768 node = isl_ast_node_cow(node);
771 if (node->type != isl_ast_node_for)
772 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
773 "not a for node", goto error);
775 isl_ast_node_free(node->u.f.body);
776 node->u.f.body = body;
780 isl_ast_node_free(node);
781 isl_ast_node_free(body);
785 __isl_give isl_ast_node *isl_ast_node_for_get_body(
786 __isl_keep isl_ast_node *node)
790 if (node->type != isl_ast_node_for)
791 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
792 "not a for node", return NULL);
793 return isl_ast_node_copy(node->u.f.body);
796 /* Mark the given for node as being degenerate.
798 __isl_give isl_ast_node *isl_ast_node_for_mark_degenerate(
799 __isl_take isl_ast_node *node)
801 node = isl_ast_node_cow(node);
804 node->u.f.degenerate = 1;
808 int isl_ast_node_for_is_degenerate(__isl_keep isl_ast_node *node)
812 if (node->type != isl_ast_node_for)
813 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
814 "not a for node", return -1);
815 return node->u.f.degenerate;
818 __isl_give isl_ast_expr *isl_ast_node_for_get_iterator(
819 __isl_keep isl_ast_node *node)
823 if (node->type != isl_ast_node_for)
824 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
825 "not a for node", return NULL);
826 return isl_ast_expr_copy(node->u.f.iterator);
829 __isl_give isl_ast_expr *isl_ast_node_for_get_init(
830 __isl_keep isl_ast_node *node)
834 if (node->type != isl_ast_node_for)
835 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
836 "not a for node", return NULL);
837 return isl_ast_expr_copy(node->u.f.init);
840 /* Return the condition expression of the given for node.
842 * If the for node is degenerate, then the condition is not explicitly
843 * stored in the node. Instead, it is constructed as
847 __isl_give isl_ast_expr *isl_ast_node_for_get_cond(
848 __isl_keep isl_ast_node *node)
852 if (node->type != isl_ast_node_for)
853 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
854 "not a for node", return NULL);
855 if (!node->u.f.degenerate)
856 return isl_ast_expr_copy(node->u.f.cond);
858 return isl_ast_expr_alloc_binary(isl_ast_op_le,
859 isl_ast_expr_copy(node->u.f.iterator),
860 isl_ast_expr_copy(node->u.f.init));
863 /* Return the increment of the given for node.
865 * If the for node is degenerate, then the increment is not explicitly
866 * stored in the node. We simply return "1".
868 __isl_give isl_ast_expr *isl_ast_node_for_get_inc(
869 __isl_keep isl_ast_node *node)
873 if (node->type != isl_ast_node_for)
874 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
875 "not a for node", return NULL);
876 if (!node->u.f.degenerate)
877 return isl_ast_expr_copy(node->u.f.inc);
878 return isl_ast_expr_alloc_int_si(isl_ast_node_get_ctx(node), 1);
881 /* Replace the then branch of the if node "node" by "child".
883 __isl_give isl_ast_node *isl_ast_node_if_set_then(
884 __isl_take isl_ast_node *node, __isl_take isl_ast_node *child)
886 node = isl_ast_node_cow(node);
889 if (node->type != isl_ast_node_if)
890 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
891 "not an if node", goto error);
893 isl_ast_node_free(node->u.i.then);
894 node->u.i.then = child;
898 isl_ast_node_free(node);
899 isl_ast_node_free(child);
903 __isl_give isl_ast_node *isl_ast_node_if_get_then(
904 __isl_keep isl_ast_node *node)
908 if (node->type != isl_ast_node_if)
909 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
910 "not an if node", return NULL);
911 return isl_ast_node_copy(node->u.i.then);
914 int isl_ast_node_if_has_else(
915 __isl_keep isl_ast_node *node)
919 if (node->type != isl_ast_node_if)
920 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
921 "not an if node", return -1);
922 return node->u.i.else_node != NULL;
925 __isl_give isl_ast_node *isl_ast_node_if_get_else(
926 __isl_keep isl_ast_node *node)
930 if (node->type != isl_ast_node_if)
931 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
932 "not an if node", return NULL);
933 return isl_ast_node_copy(node->u.i.else_node);
936 __isl_give isl_ast_expr *isl_ast_node_if_get_cond(
937 __isl_keep isl_ast_node *node)
941 if (node->type != isl_ast_node_if)
942 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
943 "not a guard node", return NULL);
944 return isl_ast_expr_copy(node->u.i.guard);
947 __isl_give isl_ast_node_list *isl_ast_node_block_get_children(
948 __isl_keep isl_ast_node *node)
952 if (node->type != isl_ast_node_block)
953 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
954 "not a block node", return NULL);
955 return isl_ast_node_list_copy(node->u.b.children);
958 __isl_give isl_ast_expr *isl_ast_node_user_get_expr(
959 __isl_keep isl_ast_node *node)
964 return isl_ast_expr_copy(node->u.e.expr);
967 __isl_give isl_id *isl_ast_node_get_annotation(__isl_keep isl_ast_node *node)
969 return node ? isl_id_copy(node->annotation) : NULL;
972 /* Replace node->annotation by "annotation".
974 __isl_give isl_ast_node *isl_ast_node_set_annotation(
975 __isl_take isl_ast_node *node, __isl_take isl_id *annotation)
977 node = isl_ast_node_cow(node);
978 if (!node || !annotation)
981 isl_id_free(node->annotation);
982 node->annotation = annotation;
986 isl_id_free(annotation);
987 return isl_ast_node_free(node);
990 /* Textual C representation of the various operators.
992 static char *op_str[] = {
993 [isl_ast_op_and] = "&&",
994 [isl_ast_op_and_then] = "&&",
995 [isl_ast_op_or] = "||",
996 [isl_ast_op_or_else] = "||",
997 [isl_ast_op_max] = "max",
998 [isl_ast_op_min] = "min",
999 [isl_ast_op_minus] = "-",
1000 [isl_ast_op_add] = "+",
1001 [isl_ast_op_sub] = "-",
1002 [isl_ast_op_mul] = "*",
1003 [isl_ast_op_pdiv_q] = "/",
1004 [isl_ast_op_pdiv_r] = "%",
1005 [isl_ast_op_div] = "/",
1006 [isl_ast_op_eq] = "==",
1007 [isl_ast_op_le] = "<=",
1008 [isl_ast_op_ge] = ">=",
1009 [isl_ast_op_lt] = "<",
1010 [isl_ast_op_gt] = ">"
1013 /* Precedence in C of the various operators.
1014 * Based on http://en.wikipedia.org/wiki/Operators_in_C_and_C++
1015 * Lowest value means highest precedence.
1017 static int op_prec[] = {
1018 [isl_ast_op_and] = 13,
1019 [isl_ast_op_and_then] = 13,
1020 [isl_ast_op_or] = 14,
1021 [isl_ast_op_or_else] = 14,
1022 [isl_ast_op_max] = 2,
1023 [isl_ast_op_min] = 2,
1024 [isl_ast_op_minus] = 3,
1025 [isl_ast_op_add] = 6,
1026 [isl_ast_op_sub] = 6,
1027 [isl_ast_op_mul] = 5,
1028 [isl_ast_op_div] = 5,
1029 [isl_ast_op_fdiv_q] = 2,
1030 [isl_ast_op_pdiv_q] = 5,
1031 [isl_ast_op_pdiv_r] = 5,
1032 [isl_ast_op_cond] = 15,
1033 [isl_ast_op_select] = 15,
1034 [isl_ast_op_eq] = 9,
1035 [isl_ast_op_le] = 8,
1036 [isl_ast_op_ge] = 8,
1037 [isl_ast_op_lt] = 8,
1038 [isl_ast_op_gt] = 8,
1039 [isl_ast_op_call] = 2
1042 /* Is the operator left-to-right associative?
1044 static int op_left[] = {
1045 [isl_ast_op_and] = 1,
1046 [isl_ast_op_and_then] = 1,
1047 [isl_ast_op_or] = 1,
1048 [isl_ast_op_or_else] = 1,
1049 [isl_ast_op_max] = 1,
1050 [isl_ast_op_min] = 1,
1051 [isl_ast_op_minus] = 0,
1052 [isl_ast_op_add] = 1,
1053 [isl_ast_op_sub] = 1,
1054 [isl_ast_op_mul] = 1,
1055 [isl_ast_op_div] = 1,
1056 [isl_ast_op_fdiv_q] = 1,
1057 [isl_ast_op_pdiv_q] = 1,
1058 [isl_ast_op_pdiv_r] = 1,
1059 [isl_ast_op_cond] = 0,
1060 [isl_ast_op_select] = 0,
1061 [isl_ast_op_eq] = 1,
1062 [isl_ast_op_le] = 1,
1063 [isl_ast_op_ge] = 1,
1064 [isl_ast_op_lt] = 1,
1065 [isl_ast_op_gt] = 1,
1066 [isl_ast_op_call] = 1
1069 static int is_and(enum isl_ast_op_type op)
1071 return op == isl_ast_op_and || op == isl_ast_op_and_then;
1074 static int is_or(enum isl_ast_op_type op)
1076 return op == isl_ast_op_or || op == isl_ast_op_or_else;
1079 static int is_add_sub(enum isl_ast_op_type op)
1081 return op == isl_ast_op_add || op == isl_ast_op_sub;
1084 static int is_div_mod(enum isl_ast_op_type op)
1086 return op == isl_ast_op_div || op == isl_ast_op_pdiv_r;
1089 /* Do we need/want parentheses around "expr" as a subexpression of
1090 * an "op" operation? If "left" is set, then "expr" is the left-most
1093 * We only need parentheses if "expr" represents an operation.
1095 * If op has a higher precedence than expr->u.op.op, then we need
1097 * If op and expr->u.op.op have the same precedence, but the operations
1098 * are performed in an order that is different from the associativity,
1099 * then we need parentheses.
1101 * An and inside an or technically does not require parentheses,
1102 * but some compilers complain about that, so we add them anyway.
1104 * Computations such as "a / b * c" and "a % b + c" can be somewhat
1105 * difficult to read, so we add parentheses for those as well.
1107 static int sub_expr_need_parens(enum isl_ast_op_type op,
1108 __isl_keep isl_ast_expr *expr, int left)
1110 if (expr->type != isl_ast_expr_op)
1113 if (op_prec[expr->u.op.op] > op_prec[op])
1115 if (op_prec[expr->u.op.op] == op_prec[op] && left != op_left[op])
1118 if (is_or(op) && is_and(expr->u.op.op))
1120 if (op == isl_ast_op_mul && expr->u.op.op != isl_ast_op_mul &&
1121 op_prec[expr->u.op.op] == op_prec[op])
1123 if (is_add_sub(op) && is_div_mod(expr->u.op.op))
1129 /* Print "expr" as a subexpression of an "op" operation.
1130 * If "left" is set, then "expr" is the left-most operand.
1132 static __isl_give isl_printer *print_sub_expr(__isl_take isl_printer *p,
1133 enum isl_ast_op_type op, __isl_keep isl_ast_expr *expr, int left)
1137 need_parens = sub_expr_need_parens(op, expr, left);
1140 p = isl_printer_print_str(p, "(");
1141 p = isl_printer_print_ast_expr(p, expr);
1143 p = isl_printer_print_str(p, ")");
1147 /* Print a min or max reduction "expr".
1149 static __isl_give isl_printer *print_min_max(__isl_take isl_printer *p,
1150 __isl_keep isl_ast_expr *expr)
1154 for (i = 1; i < expr->u.op.n_arg; ++i) {
1155 p = isl_printer_print_str(p, op_str[expr->u.op.op]);
1156 p = isl_printer_print_str(p, "(");
1158 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1159 for (i = 1; i < expr->u.op.n_arg; ++i) {
1160 p = isl_printer_print_str(p, ", ");
1161 p = isl_printer_print_ast_expr(p, expr->u.op.args[i]);
1162 p = isl_printer_print_str(p, ")");
1168 /* Print a function call "expr".
1170 * The first argument represents the function to be called.
1172 static __isl_give isl_printer *print_call(__isl_take isl_printer *p,
1173 __isl_keep isl_ast_expr *expr)
1177 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1178 p = isl_printer_print_str(p, "(");
1179 for (i = 1; i < expr->u.op.n_arg; ++i) {
1181 p = isl_printer_print_str(p, ", ");
1182 p = isl_printer_print_ast_expr(p, expr->u.op.args[i]);
1184 p = isl_printer_print_str(p, ")");
1189 /* Print "expr" to "p".
1191 * If we are printing in isl format, then we also print an indication
1192 * of the size of the expression (if it was computed).
1194 __isl_give isl_printer *isl_printer_print_ast_expr(__isl_take isl_printer *p,
1195 __isl_keep isl_ast_expr *expr)
1200 return isl_printer_free(p);
1202 switch (expr->type) {
1203 case isl_ast_expr_op:
1204 if (expr->u.op.op == isl_ast_op_call) {
1205 p = print_call(p, expr);
1208 if (expr->u.op.n_arg == 1) {
1209 p = isl_printer_print_str(p, op_str[expr->u.op.op]);
1210 p = print_sub_expr(p, expr->u.op.op,
1211 expr->u.op.args[0], 0);
1214 if (expr->u.op.op == isl_ast_op_fdiv_q) {
1215 p = isl_printer_print_str(p, "floord(");
1216 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1217 p = isl_printer_print_str(p, ", ");
1218 p = isl_printer_print_ast_expr(p, expr->u.op.args[1]);
1219 p = isl_printer_print_str(p, ")");
1222 if (expr->u.op.op == isl_ast_op_max ||
1223 expr->u.op.op == isl_ast_op_min) {
1224 p = print_min_max(p, expr);
1227 if (expr->u.op.op == isl_ast_op_cond ||
1228 expr->u.op.op == isl_ast_op_select) {
1229 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1230 p = isl_printer_print_str(p, " ? ");
1231 p = isl_printer_print_ast_expr(p, expr->u.op.args[1]);
1232 p = isl_printer_print_str(p, " : ");
1233 p = isl_printer_print_ast_expr(p, expr->u.op.args[2]);
1236 if (expr->u.op.n_arg != 2)
1237 isl_die(isl_printer_get_ctx(p), isl_error_internal,
1238 "operation should have two arguments",
1240 p = print_sub_expr(p, expr->u.op.op, expr->u.op.args[0], 1);
1241 p = isl_printer_print_str(p, " ");
1242 p = isl_printer_print_str(p, op_str[expr->u.op.op]);
1243 p = isl_printer_print_str(p, " ");
1244 p = print_sub_expr(p, expr->u.op.op, expr->u.op.args[1], 0);
1246 case isl_ast_expr_id:
1247 p = isl_printer_print_str(p, isl_id_get_name(expr->u.id));
1249 case isl_ast_expr_int:
1250 p = isl_printer_print_isl_int(p, expr->u.i);
1252 case isl_ast_expr_error:
1258 isl_printer_free(p);
1262 /* Print "node" to "p" in "isl format".
1264 static __isl_give isl_printer *print_ast_node_isl(__isl_take isl_printer *p,
1265 __isl_keep isl_ast_node *node)
1267 p = isl_printer_print_str(p, "(");
1268 switch (node->type) {
1269 case isl_ast_node_for:
1270 if (node->u.f.degenerate) {
1271 p = isl_printer_print_ast_expr(p, node->u.f.init);
1273 p = isl_printer_print_str(p, "init: ");
1274 p = isl_printer_print_ast_expr(p, node->u.f.init);
1275 p = isl_printer_print_str(p, ", ");
1276 p = isl_printer_print_str(p, "cond: ");
1277 p = isl_printer_print_ast_expr(p, node->u.f.cond);
1278 p = isl_printer_print_str(p, ", ");
1279 p = isl_printer_print_str(p, "inc: ");
1280 p = isl_printer_print_ast_expr(p, node->u.f.inc);
1282 if (node->u.f.body) {
1283 p = isl_printer_print_str(p, ", ");
1284 p = isl_printer_print_str(p, "body: ");
1285 p = isl_printer_print_ast_node(p, node->u.f.body);
1288 case isl_ast_node_user:
1289 p = isl_printer_print_ast_expr(p, node->u.e.expr);
1291 case isl_ast_node_if:
1292 p = isl_printer_print_str(p, "guard: ");
1293 p = isl_printer_print_ast_expr(p, node->u.i.guard);
1294 if (node->u.i.then) {
1295 p = isl_printer_print_str(p, ", ");
1296 p = isl_printer_print_str(p, "then: ");
1297 p = isl_printer_print_ast_node(p, node->u.i.then);
1299 if (node->u.i.else_node) {
1300 p = isl_printer_print_str(p, ", ");
1301 p = isl_printer_print_str(p, "else: ");
1302 p = isl_printer_print_ast_node(p, node->u.i.else_node);
1305 case isl_ast_node_block:
1306 p = isl_printer_print_ast_node_list(p, node->u.b.children);
1311 p = isl_printer_print_str(p, ")");
1315 /* Do we need to print a block around the body "node" of a for or if node?
1317 * If the node is a block, then we need to print a block.
1318 * Also if the node is a degenerate for then we will print it as
1319 * an assignment followed by the body of the for loop, so we need a block
1322 static int need_block(__isl_keep isl_ast_node *node)
1324 if (node->type == isl_ast_node_block)
1326 if (node->type == isl_ast_node_for && node->u.f.degenerate)
1331 static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
1332 __isl_keep isl_ast_node *node,
1333 __isl_keep isl_ast_print_options *options, int in_block, int in_list);
1334 static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
1335 __isl_keep isl_ast_node *node,
1336 __isl_keep isl_ast_print_options *options, int new_line);
1338 /* Print the body "node" of a for or if node.
1339 * If "else_node" is set, then it is printed as well.
1341 * We first check if we need to print out a block.
1342 * We always print out a block if there is an else node to make
1343 * sure that the else node is matched to the correct if node.
1345 * If the else node is itself an if, then we print it as
1349 * Otherwise the else node is printed as
1354 static __isl_give isl_printer *print_body_c(__isl_take isl_printer *p,
1355 __isl_keep isl_ast_node *node, __isl_keep isl_ast_node *else_node,
1356 __isl_keep isl_ast_print_options *options)
1359 return isl_printer_free(p);
1361 if (!else_node && !need_block(node)) {
1362 p = isl_printer_end_line(p);
1363 p = isl_printer_indent(p, 2);
1364 p = isl_ast_node_print(node, p,
1365 isl_ast_print_options_copy(options));
1366 p = isl_printer_indent(p, -2);
1370 p = isl_printer_print_str(p, " {");
1371 p = isl_printer_end_line(p);
1372 p = isl_printer_indent(p, 2);
1373 p = print_ast_node_c(p, node, options, 1, 0);
1374 p = isl_printer_indent(p, -2);
1375 p = isl_printer_start_line(p);
1376 p = isl_printer_print_str(p, "}");
1378 if (else_node->type == isl_ast_node_if) {
1379 p = isl_printer_print_str(p, " else ");
1380 p = print_if_c(p, else_node, options, 0);
1382 p = isl_printer_print_str(p, " else");
1383 p = print_body_c(p, else_node, NULL, options);
1386 p = isl_printer_end_line(p);
1391 /* Print the start of a compound statement.
1393 static __isl_give isl_printer *start_block(__isl_take isl_printer *p)
1395 p = isl_printer_start_line(p);
1396 p = isl_printer_print_str(p, "{");
1397 p = isl_printer_end_line(p);
1398 p = isl_printer_indent(p, 2);
1403 /* Print the end of a compound statement.
1405 static __isl_give isl_printer *end_block(__isl_take isl_printer *p)
1407 p = isl_printer_indent(p, -2);
1408 p = isl_printer_start_line(p);
1409 p = isl_printer_print_str(p, "}");
1410 p = isl_printer_end_line(p);
1415 /* Print the for node "node".
1417 * If the for node is degenerate, it is printed as
1419 * type iterator = init;
1422 * Otherwise, it is printed as
1424 * for (type iterator = init; cond; iterator += inc)
1427 * "in_block" is set if we are currently inside a block.
1428 * "in_list" is set if the current node is not alone in the block.
1429 * If we are not in a block or if the current not is not alone in the block
1430 * then we print a block around a degenerate for loop such that the variable
1431 * declaration will not conflict with any potential other declaration
1432 * of the same variable.
1434 static __isl_give isl_printer *print_for_c(__isl_take isl_printer *p,
1435 __isl_keep isl_ast_node *node,
1436 __isl_keep isl_ast_print_options *options, int in_block, int in_list)
1442 type = isl_options_get_ast_iterator_type(isl_printer_get_ctx(p));
1443 if (!node->u.f.degenerate) {
1444 id = isl_ast_expr_get_id(node->u.f.iterator);
1445 name = isl_id_get_name(id);
1447 p = isl_printer_start_line(p);
1448 p = isl_printer_print_str(p, "for (");
1449 p = isl_printer_print_str(p, type);
1450 p = isl_printer_print_str(p, " ");
1451 p = isl_printer_print_str(p, name);
1452 p = isl_printer_print_str(p, " = ");
1453 p = isl_printer_print_ast_expr(p, node->u.f.init);
1454 p = isl_printer_print_str(p, "; ");
1455 p = isl_printer_print_ast_expr(p, node->u.f.cond);
1456 p = isl_printer_print_str(p, "; ");
1457 p = isl_printer_print_str(p, name);
1458 p = isl_printer_print_str(p, " += ");
1459 p = isl_printer_print_ast_expr(p, node->u.f.inc);
1460 p = isl_printer_print_str(p, ")");
1461 p = print_body_c(p, node->u.f.body, NULL, options);
1463 id = isl_ast_expr_get_id(node->u.f.iterator);
1464 name = isl_id_get_name(id);
1466 if (!in_block || in_list)
1468 p = isl_printer_start_line(p);
1469 p = isl_printer_print_str(p, type);
1470 p = isl_printer_print_str(p, " ");
1471 p = isl_printer_print_str(p, name);
1472 p = isl_printer_print_str(p, " = ");
1473 p = isl_printer_print_ast_expr(p, node->u.f.init);
1474 p = isl_printer_print_str(p, ";");
1475 p = isl_printer_end_line(p);
1476 p = print_ast_node_c(p, node->u.f.body, options, 1, 0);
1477 if (!in_block || in_list)
1484 /* Print the if node "node".
1485 * If "new_line" is set then the if node should be printed on a new line.
1487 static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
1488 __isl_keep isl_ast_node *node,
1489 __isl_keep isl_ast_print_options *options, int new_line)
1492 p = isl_printer_start_line(p);
1493 p = isl_printer_print_str(p, "if (");
1494 p = isl_printer_print_ast_expr(p, node->u.i.guard);
1495 p = isl_printer_print_str(p, ")");
1496 p = print_body_c(p, node->u.i.then, node->u.i.else_node, options);
1501 /* Print the "node" to "p".
1503 * "in_block" is set if we are currently inside a block.
1504 * If so, we do not print a block around the children of a block node.
1505 * We do this to avoid an extra block around the body of a degenerate
1508 * "in_list" is set if the current node is not alone in the block.
1510 static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
1511 __isl_keep isl_ast_node *node,
1512 __isl_keep isl_ast_print_options *options, int in_block, int in_list)
1514 switch (node->type) {
1515 case isl_ast_node_for:
1516 if (options->print_for)
1517 return options->print_for(p,
1518 isl_ast_print_options_copy(options),
1519 node, options->print_for_user);
1520 p = print_for_c(p, node, options, in_block, in_list);
1522 case isl_ast_node_if:
1523 p = print_if_c(p, node, options, 1);
1525 case isl_ast_node_block:
1528 p = isl_ast_node_list_print(node->u.b.children, p, options);
1532 case isl_ast_node_user:
1533 if (options->print_user)
1534 return options->print_user(p,
1535 isl_ast_print_options_copy(options),
1536 node, options->print_user_user);
1537 p = isl_printer_start_line(p);
1538 p = isl_printer_print_ast_expr(p, node->u.e.expr);
1539 p = isl_printer_print_str(p, ";");
1540 p = isl_printer_end_line(p);
1542 case isl_ast_node_error:
1548 /* Print the for node "node" to "p".
1550 __isl_give isl_printer *isl_ast_node_for_print(__isl_keep isl_ast_node *node,
1551 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
1553 if (!node || !options)
1555 if (node->type != isl_ast_node_for)
1556 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1557 "not a for node", goto error);
1558 p = print_for_c(p, node, options, 0, 0);
1559 isl_ast_print_options_free(options);
1562 isl_ast_print_options_free(options);
1563 isl_printer_free(p);
1567 /* Print the if node "node" to "p".
1569 __isl_give isl_printer *isl_ast_node_if_print(__isl_keep isl_ast_node *node,
1570 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
1572 if (!node || !options)
1574 if (node->type != isl_ast_node_if)
1575 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1576 "not an if node", goto error);
1577 p = print_if_c(p, node, options, 1);
1578 isl_ast_print_options_free(options);
1581 isl_ast_print_options_free(options);
1582 isl_printer_free(p);
1586 /* Print "node" to "p".
1588 __isl_give isl_printer *isl_ast_node_print(__isl_keep isl_ast_node *node,
1589 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
1591 if (!options || !node)
1593 p = print_ast_node_c(p, node, options, 0, 0);
1594 isl_ast_print_options_free(options);
1597 isl_ast_print_options_free(options);
1598 isl_printer_free(p);
1602 /* Print "node" to "p".
1604 __isl_give isl_printer *isl_printer_print_ast_node(__isl_take isl_printer *p,
1605 __isl_keep isl_ast_node *node)
1608 isl_ast_print_options *options;
1613 format = isl_printer_get_output_format(p);
1615 case ISL_FORMAT_ISL:
1616 p = print_ast_node_isl(p, node);
1619 options = isl_ast_print_options_alloc(isl_printer_get_ctx(p));
1620 p = isl_ast_node_print(node, p, options);
1623 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
1624 "output format not supported for ast_node",
1625 return isl_printer_free(p));
1631 /* Print the list of nodes "list" to "p".
1633 __isl_give isl_printer *isl_ast_node_list_print(
1634 __isl_keep isl_ast_node_list *list, __isl_take isl_printer *p,
1635 __isl_keep isl_ast_print_options *options)
1639 if (!p || !list || !options)
1640 return isl_printer_free(p);
1642 for (i = 0; i < list->n; ++i)
1643 p = print_ast_node_c(p, list->p[i], options, 1, 1);
1648 #define ISL_AST_MACRO_FLOORD (1 << 0)
1649 #define ISL_AST_MACRO_MIN (1 << 1)
1650 #define ISL_AST_MACRO_MAX (1 << 2)
1651 #define ISL_AST_MACRO_ALL (ISL_AST_MACRO_FLOORD | \
1652 ISL_AST_MACRO_MIN | \
1655 /* If "expr" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1656 * then set the corresponding bit in "macros".
1658 static int ast_expr_required_macros(__isl_keep isl_ast_expr *expr, int macros)
1662 if (macros == ISL_AST_MACRO_ALL)
1665 if (expr->type != isl_ast_expr_op)
1668 if (expr->u.op.op == isl_ast_op_min)
1669 macros |= ISL_AST_MACRO_MIN;
1670 if (expr->u.op.op == isl_ast_op_max)
1671 macros |= ISL_AST_MACRO_MAX;
1672 if (expr->u.op.op == isl_ast_op_fdiv_q)
1673 macros |= ISL_AST_MACRO_FLOORD;
1675 for (i = 0; i < expr->u.op.n_arg; ++i)
1676 macros = ast_expr_required_macros(expr->u.op.args[i], macros);
1681 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
1684 /* If "node" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1685 * then set the corresponding bit in "macros".
1687 static int ast_node_required_macros(__isl_keep isl_ast_node *node, int macros)
1689 if (macros == ISL_AST_MACRO_ALL)
1692 switch (node->type) {
1693 case isl_ast_node_for:
1694 macros = ast_expr_required_macros(node->u.f.init, macros);
1695 if (!node->u.f.degenerate) {
1696 macros = ast_expr_required_macros(node->u.f.cond,
1698 macros = ast_expr_required_macros(node->u.f.inc,
1701 macros = ast_node_required_macros(node->u.f.body, macros);
1703 case isl_ast_node_if:
1704 macros = ast_expr_required_macros(node->u.i.guard, macros);
1705 macros = ast_node_required_macros(node->u.i.then, macros);
1706 if (node->u.i.else_node)
1707 macros = ast_node_required_macros(node->u.i.else_node,
1710 case isl_ast_node_block:
1711 macros = ast_node_list_required_macros(node->u.b.children,
1714 case isl_ast_node_user:
1715 macros = ast_expr_required_macros(node->u.e.expr, macros);
1717 case isl_ast_node_error:
1724 /* If "list" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1725 * then set the corresponding bit in "macros".
1727 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
1732 for (i = 0; i < list->n; ++i)
1733 macros = ast_node_required_macros(list->p[i], macros);
1738 /* Print a macro definition for the operator "type".
1740 __isl_give isl_printer *isl_ast_op_type_print_macro(
1741 enum isl_ast_op_type type, __isl_take isl_printer *p)
1744 case isl_ast_op_min:
1745 p = isl_printer_start_line(p);
1746 p = isl_printer_print_str(p,
1747 "#define min(x,y) ((x) < (y) ? (x) : (y))");
1748 p = isl_printer_end_line(p);
1750 case isl_ast_op_max:
1751 p = isl_printer_start_line(p);
1752 p = isl_printer_print_str(p,
1753 "#define max(x,y) ((x) > (y) ? (x) : (y))");
1754 p = isl_printer_end_line(p);
1756 case isl_ast_op_fdiv_q:
1757 p = isl_printer_start_line(p);
1758 p = isl_printer_print_str(p,
1759 "#define floord(n,d) "
1760 "(((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))");
1761 p = isl_printer_end_line(p);
1770 /* Call "fn" for each type of operation that appears in "node"
1771 * and that requires a macro definition.
1773 int isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node *node,
1774 int (*fn)(enum isl_ast_op_type type, void *user), void *user)
1781 macros = ast_node_required_macros(node, 0);
1783 if (macros & ISL_AST_MACRO_MIN && fn(isl_ast_op_min, user) < 0)
1785 if (macros & ISL_AST_MACRO_MAX && fn(isl_ast_op_max, user) < 0)
1787 if (macros & ISL_AST_MACRO_FLOORD && fn(isl_ast_op_fdiv_q, user) < 0)
1793 static int ast_op_type_print_macro(enum isl_ast_op_type type, void *user)
1795 isl_printer **p = user;
1797 *p = isl_ast_op_type_print_macro(type, *p);
1802 /* Print macro definitions for all the macros used in the result
1803 * of printing "node.
1805 __isl_give isl_printer *isl_ast_node_print_macros(
1806 __isl_keep isl_ast_node *node, __isl_take isl_printer *p)
1808 if (isl_ast_node_foreach_ast_op_type(node,
1809 &ast_op_type_print_macro, &p) < 0)
1810 return isl_printer_free(p);