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_from_val(isl_val_copy(expr->u.v));
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_val_free(expr->u.v);
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 return isl_val_get_num_isl_int(expr->u.v, v);
244 /* Return the integer value represented by "expr".
246 __isl_give isl_val *isl_ast_expr_get_val(__isl_keep isl_ast_expr *expr)
250 if (expr->type != isl_ast_expr_int)
251 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
252 "expression not an int", return NULL);
253 return isl_val_copy(expr->u.v);
256 __isl_give isl_id *isl_ast_expr_get_id(__isl_keep isl_ast_expr *expr)
260 if (expr->type != isl_ast_expr_id)
261 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
262 "expression not an identifier", return NULL);
264 return isl_id_copy(expr->u.id);
267 enum isl_ast_op_type isl_ast_expr_get_op_type(__isl_keep isl_ast_expr *expr)
270 return isl_ast_op_error;
271 if (expr->type != isl_ast_expr_op)
272 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
273 "expression not an operation", return isl_ast_op_error);
274 return expr->u.op.op;
277 int isl_ast_expr_get_op_n_arg(__isl_keep isl_ast_expr *expr)
281 if (expr->type != isl_ast_expr_op)
282 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
283 "expression not an operation", return -1);
284 return expr->u.op.n_arg;
287 __isl_give isl_ast_expr *isl_ast_expr_get_op_arg(__isl_keep isl_ast_expr *expr,
292 if (expr->type != isl_ast_expr_op)
293 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
294 "expression not an operation", return NULL);
295 if (pos < 0 || pos >= expr->u.op.n_arg)
296 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
297 "index out of bounds", return NULL);
299 return isl_ast_expr_copy(expr->u.op.args[pos]);
302 /* Replace the argument at position "pos" of "expr" by "arg".
304 __isl_give isl_ast_expr *isl_ast_expr_set_op_arg(__isl_take isl_ast_expr *expr,
305 int pos, __isl_take isl_ast_expr *arg)
307 expr = isl_ast_expr_cow(expr);
310 if (expr->type != isl_ast_expr_op)
311 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
312 "expression not an operation", goto error);
313 if (pos < 0 || pos >= expr->u.op.n_arg)
314 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
315 "index out of bounds", goto error);
317 isl_ast_expr_free(expr->u.op.args[pos]);
318 expr->u.op.args[pos] = arg;
322 isl_ast_expr_free(arg);
323 return isl_ast_expr_free(expr);
326 /* Create a new operation expression of operation type "op",
327 * with "n_arg" as yet unspecified arguments.
329 __isl_give isl_ast_expr *isl_ast_expr_alloc_op(isl_ctx *ctx,
330 enum isl_ast_op_type op, int n_arg)
334 expr = isl_calloc_type(ctx, isl_ast_expr);
341 expr->type = isl_ast_expr_op;
343 expr->u.op.n_arg = n_arg;
344 expr->u.op.args = isl_calloc_array(ctx, isl_ast_expr *, n_arg);
346 if (!expr->u.op.args)
347 return isl_ast_expr_free(expr);
352 /* Create a new id expression representing "id".
354 __isl_give isl_ast_expr *isl_ast_expr_from_id(__isl_take isl_id *id)
362 ctx = isl_id_get_ctx(id);
363 expr = isl_calloc_type(ctx, isl_ast_expr);
365 return isl_id_free(id);
370 expr->type = isl_ast_expr_id;
376 /* Create a new integer expression representing "i".
378 __isl_give isl_ast_expr *isl_ast_expr_alloc_int_si(isl_ctx *ctx, int i)
382 expr = isl_calloc_type(ctx, isl_ast_expr);
389 expr->type = isl_ast_expr_int;
390 expr->u.v = isl_val_int_from_si(ctx, i);
392 return isl_ast_expr_free(expr);
397 /* Create a new integer expression representing "i".
399 __isl_give isl_ast_expr *isl_ast_expr_alloc_int(isl_ctx *ctx, isl_int i)
403 expr = isl_calloc_type(ctx, isl_ast_expr);
410 expr->type = isl_ast_expr_int;
411 expr->u.v = isl_val_int_from_isl_int(ctx, i);
413 expr = isl_ast_expr_free(expr);
418 /* Create a new integer expression representing "v".
420 __isl_give isl_ast_expr *isl_ast_expr_from_val(__isl_take isl_val *v)
427 if (!isl_val_is_int(v))
428 isl_die(isl_val_get_ctx(v), isl_error_invalid,
429 "expecting integer value", return isl_val_free(v));
431 ctx = isl_val_get_ctx(v);
432 expr = isl_calloc_type(ctx, isl_ast_expr);
434 return isl_val_free(v);
439 expr->type = isl_ast_expr_int;
445 /* Create an expression representing the negation of "arg".
447 __isl_give isl_ast_expr *isl_ast_expr_neg(__isl_take isl_ast_expr *arg)
450 isl_ast_expr *expr = NULL;
455 ctx = isl_ast_expr_get_ctx(arg);
456 expr = isl_ast_expr_alloc_op(ctx, isl_ast_op_minus, 1);
460 expr->u.op.args[0] = arg;
464 isl_ast_expr_free(arg);
468 /* Create an expression representing the binary operation "type"
469 * applied to "expr1" and "expr2".
471 __isl_give isl_ast_expr *isl_ast_expr_alloc_binary(enum isl_ast_op_type type,
472 __isl_take isl_ast_expr *expr1, __isl_take isl_ast_expr *expr2)
475 isl_ast_expr *expr = NULL;
477 if (!expr1 || !expr2)
480 ctx = isl_ast_expr_get_ctx(expr1);
481 expr = isl_ast_expr_alloc_op(ctx, type, 2);
485 expr->u.op.args[0] = expr1;
486 expr->u.op.args[1] = expr2;
490 isl_ast_expr_free(expr1);
491 isl_ast_expr_free(expr2);
495 /* Create an expression representing the sum of "expr1" and "expr2".
497 __isl_give isl_ast_expr *isl_ast_expr_add(__isl_take isl_ast_expr *expr1,
498 __isl_take isl_ast_expr *expr2)
500 return isl_ast_expr_alloc_binary(isl_ast_op_add, expr1, expr2);
503 /* Create an expression representing the difference of "expr1" and "expr2".
505 __isl_give isl_ast_expr *isl_ast_expr_sub(__isl_take isl_ast_expr *expr1,
506 __isl_take isl_ast_expr *expr2)
508 return isl_ast_expr_alloc_binary(isl_ast_op_sub, expr1, expr2);
511 /* Create an expression representing the product of "expr1" and "expr2".
513 __isl_give isl_ast_expr *isl_ast_expr_mul(__isl_take isl_ast_expr *expr1,
514 __isl_take isl_ast_expr *expr2)
516 return isl_ast_expr_alloc_binary(isl_ast_op_mul, expr1, expr2);
519 /* Create an expression representing the quotient of "expr1" and "expr2".
521 __isl_give isl_ast_expr *isl_ast_expr_div(__isl_take isl_ast_expr *expr1,
522 __isl_take isl_ast_expr *expr2)
524 return isl_ast_expr_alloc_binary(isl_ast_op_div, expr1, expr2);
527 /* Create an expression representing the conjunction of "expr1" and "expr2".
529 __isl_give isl_ast_expr *isl_ast_expr_and(__isl_take isl_ast_expr *expr1,
530 __isl_take isl_ast_expr *expr2)
532 return isl_ast_expr_alloc_binary(isl_ast_op_and, expr1, expr2);
535 /* Create an expression representing the disjunction of "expr1" and "expr2".
537 __isl_give isl_ast_expr *isl_ast_expr_or(__isl_take isl_ast_expr *expr1,
538 __isl_take isl_ast_expr *expr2)
540 return isl_ast_expr_alloc_binary(isl_ast_op_or, expr1, expr2);
543 isl_ctx *isl_ast_node_get_ctx(__isl_keep isl_ast_node *node)
545 return node ? node->ctx : NULL;
548 enum isl_ast_node_type isl_ast_node_get_type(__isl_keep isl_ast_node *node)
550 return node ? node->type : isl_ast_node_error;
553 __isl_give isl_ast_node *isl_ast_node_alloc(isl_ctx *ctx,
554 enum isl_ast_node_type type)
558 node = isl_calloc_type(ctx, isl_ast_node);
570 /* Create an if node with the given guard.
572 * The then body needs to be filled in later.
574 __isl_give isl_ast_node *isl_ast_node_alloc_if(__isl_take isl_ast_expr *guard)
581 node = isl_ast_node_alloc(isl_ast_expr_get_ctx(guard), isl_ast_node_if);
584 node->u.i.guard = guard;
588 isl_ast_expr_free(guard);
592 /* Create a for node with the given iterator.
594 * The remaining fields need to be filled in later.
596 __isl_give isl_ast_node *isl_ast_node_alloc_for(__isl_take isl_id *id)
604 ctx = isl_id_get_ctx(id);
605 node = isl_ast_node_alloc(ctx, isl_ast_node_for);
609 node->u.f.iterator = isl_ast_expr_from_id(id);
610 if (!node->u.f.iterator)
611 return isl_ast_node_free(node);
616 /* Create a user node evaluating "expr".
618 __isl_give isl_ast_node *isl_ast_node_alloc_user(__isl_take isl_ast_expr *expr)
626 ctx = isl_ast_expr_get_ctx(expr);
627 node = isl_ast_node_alloc(ctx, isl_ast_node_user);
631 node->u.e.expr = expr;
635 isl_ast_expr_free(expr);
639 /* Create a block node with the given children.
641 __isl_give isl_ast_node *isl_ast_node_alloc_block(
642 __isl_take isl_ast_node_list *list)
650 ctx = isl_ast_node_list_get_ctx(list);
651 node = isl_ast_node_alloc(ctx, isl_ast_node_block);
655 node->u.b.children = list;
659 isl_ast_node_list_free(list);
663 /* Represent the given list of nodes as a single node, either by
664 * extract the node from a single element list or by creating
665 * a block node with the list of nodes as children.
667 __isl_give isl_ast_node *isl_ast_node_from_ast_node_list(
668 __isl_take isl_ast_node_list *list)
672 if (isl_ast_node_list_n_ast_node(list) != 1)
673 return isl_ast_node_alloc_block(list);
675 node = isl_ast_node_list_get_ast_node(list, 0);
676 isl_ast_node_list_free(list);
681 __isl_give isl_ast_node *isl_ast_node_copy(__isl_keep isl_ast_node *node)
690 __isl_give isl_ast_node *isl_ast_node_dup(__isl_keep isl_ast_node *node)
697 dup = isl_ast_node_alloc(isl_ast_node_get_ctx(node), node->type);
701 switch (node->type) {
702 case isl_ast_node_if:
703 dup->u.i.guard = isl_ast_expr_copy(node->u.i.guard);
704 dup->u.i.then = isl_ast_node_copy(node->u.i.then);
705 dup->u.i.else_node = isl_ast_node_copy(node->u.i.else_node);
706 if (!dup->u.i.guard || !dup->u.i.then ||
707 (node->u.i.else_node && !dup->u.i.else_node))
708 return isl_ast_node_free(dup);
710 case isl_ast_node_for:
711 dup->u.f.iterator = isl_ast_expr_copy(node->u.f.iterator);
712 dup->u.f.init = isl_ast_expr_copy(node->u.f.init);
713 dup->u.f.cond = isl_ast_expr_copy(node->u.f.cond);
714 dup->u.f.inc = isl_ast_expr_copy(node->u.f.inc);
715 dup->u.f.body = isl_ast_node_copy(node->u.f.body);
716 if (!dup->u.f.iterator || !dup->u.f.init || !dup->u.f.cond ||
717 !dup->u.f.inc || !dup->u.f.body)
718 return isl_ast_node_free(dup);
720 case isl_ast_node_block:
721 dup->u.b.children = isl_ast_node_list_copy(node->u.b.children);
722 if (!dup->u.b.children)
723 return isl_ast_node_free(dup);
725 case isl_ast_node_user:
726 dup->u.e.expr = isl_ast_expr_copy(node->u.e.expr);
728 return isl_ast_node_free(dup);
730 case isl_ast_node_error:
737 __isl_give isl_ast_node *isl_ast_node_cow(__isl_take isl_ast_node *node)
745 return isl_ast_node_dup(node);
748 void *isl_ast_node_free(__isl_take isl_ast_node *node)
756 switch (node->type) {
757 case isl_ast_node_if:
758 isl_ast_expr_free(node->u.i.guard);
759 isl_ast_node_free(node->u.i.then);
760 isl_ast_node_free(node->u.i.else_node);
762 case isl_ast_node_for:
763 isl_ast_expr_free(node->u.f.iterator);
764 isl_ast_expr_free(node->u.f.init);
765 isl_ast_expr_free(node->u.f.cond);
766 isl_ast_expr_free(node->u.f.inc);
767 isl_ast_node_free(node->u.f.body);
769 case isl_ast_node_block:
770 isl_ast_node_list_free(node->u.b.children);
772 case isl_ast_node_user:
773 isl_ast_expr_free(node->u.e.expr);
775 case isl_ast_node_error:
779 isl_id_free(node->annotation);
780 isl_ctx_deref(node->ctx);
786 /* Replace the body of the for node "node" by "body".
788 __isl_give isl_ast_node *isl_ast_node_for_set_body(
789 __isl_take isl_ast_node *node, __isl_take isl_ast_node *body)
791 node = isl_ast_node_cow(node);
794 if (node->type != isl_ast_node_for)
795 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
796 "not a for node", goto error);
798 isl_ast_node_free(node->u.f.body);
799 node->u.f.body = body;
803 isl_ast_node_free(node);
804 isl_ast_node_free(body);
808 __isl_give isl_ast_node *isl_ast_node_for_get_body(
809 __isl_keep isl_ast_node *node)
813 if (node->type != isl_ast_node_for)
814 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
815 "not a for node", return NULL);
816 return isl_ast_node_copy(node->u.f.body);
819 /* Mark the given for node as being degenerate.
821 __isl_give isl_ast_node *isl_ast_node_for_mark_degenerate(
822 __isl_take isl_ast_node *node)
824 node = isl_ast_node_cow(node);
827 node->u.f.degenerate = 1;
831 int isl_ast_node_for_is_degenerate(__isl_keep isl_ast_node *node)
835 if (node->type != isl_ast_node_for)
836 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
837 "not a for node", return -1);
838 return node->u.f.degenerate;
841 __isl_give isl_ast_expr *isl_ast_node_for_get_iterator(
842 __isl_keep isl_ast_node *node)
846 if (node->type != isl_ast_node_for)
847 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
848 "not a for node", return NULL);
849 return isl_ast_expr_copy(node->u.f.iterator);
852 __isl_give isl_ast_expr *isl_ast_node_for_get_init(
853 __isl_keep isl_ast_node *node)
857 if (node->type != isl_ast_node_for)
858 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
859 "not a for node", return NULL);
860 return isl_ast_expr_copy(node->u.f.init);
863 /* Return the condition expression of the given for node.
865 * If the for node is degenerate, then the condition is not explicitly
866 * stored in the node. Instead, it is constructed as
870 __isl_give isl_ast_expr *isl_ast_node_for_get_cond(
871 __isl_keep isl_ast_node *node)
875 if (node->type != isl_ast_node_for)
876 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
877 "not a for node", return NULL);
878 if (!node->u.f.degenerate)
879 return isl_ast_expr_copy(node->u.f.cond);
881 return isl_ast_expr_alloc_binary(isl_ast_op_le,
882 isl_ast_expr_copy(node->u.f.iterator),
883 isl_ast_expr_copy(node->u.f.init));
886 /* Return the increment of the given for node.
888 * If the for node is degenerate, then the increment is not explicitly
889 * stored in the node. We simply return "1".
891 __isl_give isl_ast_expr *isl_ast_node_for_get_inc(
892 __isl_keep isl_ast_node *node)
896 if (node->type != isl_ast_node_for)
897 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
898 "not a for node", return NULL);
899 if (!node->u.f.degenerate)
900 return isl_ast_expr_copy(node->u.f.inc);
901 return isl_ast_expr_alloc_int_si(isl_ast_node_get_ctx(node), 1);
904 /* Replace the then branch of the if node "node" by "child".
906 __isl_give isl_ast_node *isl_ast_node_if_set_then(
907 __isl_take isl_ast_node *node, __isl_take isl_ast_node *child)
909 node = isl_ast_node_cow(node);
912 if (node->type != isl_ast_node_if)
913 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
914 "not an if node", goto error);
916 isl_ast_node_free(node->u.i.then);
917 node->u.i.then = child;
921 isl_ast_node_free(node);
922 isl_ast_node_free(child);
926 __isl_give isl_ast_node *isl_ast_node_if_get_then(
927 __isl_keep isl_ast_node *node)
931 if (node->type != isl_ast_node_if)
932 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
933 "not an if node", return NULL);
934 return isl_ast_node_copy(node->u.i.then);
937 int isl_ast_node_if_has_else(
938 __isl_keep isl_ast_node *node)
942 if (node->type != isl_ast_node_if)
943 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
944 "not an if node", return -1);
945 return node->u.i.else_node != NULL;
948 __isl_give isl_ast_node *isl_ast_node_if_get_else(
949 __isl_keep isl_ast_node *node)
953 if (node->type != isl_ast_node_if)
954 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
955 "not an if node", return NULL);
956 return isl_ast_node_copy(node->u.i.else_node);
959 __isl_give isl_ast_expr *isl_ast_node_if_get_cond(
960 __isl_keep isl_ast_node *node)
964 if (node->type != isl_ast_node_if)
965 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
966 "not a guard node", return NULL);
967 return isl_ast_expr_copy(node->u.i.guard);
970 __isl_give isl_ast_node_list *isl_ast_node_block_get_children(
971 __isl_keep isl_ast_node *node)
975 if (node->type != isl_ast_node_block)
976 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
977 "not a block node", return NULL);
978 return isl_ast_node_list_copy(node->u.b.children);
981 __isl_give isl_ast_expr *isl_ast_node_user_get_expr(
982 __isl_keep isl_ast_node *node)
987 return isl_ast_expr_copy(node->u.e.expr);
990 __isl_give isl_id *isl_ast_node_get_annotation(__isl_keep isl_ast_node *node)
992 return node ? isl_id_copy(node->annotation) : NULL;
995 /* Replace node->annotation by "annotation".
997 __isl_give isl_ast_node *isl_ast_node_set_annotation(
998 __isl_take isl_ast_node *node, __isl_take isl_id *annotation)
1000 node = isl_ast_node_cow(node);
1001 if (!node || !annotation)
1004 isl_id_free(node->annotation);
1005 node->annotation = annotation;
1009 isl_id_free(annotation);
1010 return isl_ast_node_free(node);
1013 /* Textual C representation of the various operators.
1015 static char *op_str[] = {
1016 [isl_ast_op_and] = "&&",
1017 [isl_ast_op_and_then] = "&&",
1018 [isl_ast_op_or] = "||",
1019 [isl_ast_op_or_else] = "||",
1020 [isl_ast_op_max] = "max",
1021 [isl_ast_op_min] = "min",
1022 [isl_ast_op_minus] = "-",
1023 [isl_ast_op_add] = "+",
1024 [isl_ast_op_sub] = "-",
1025 [isl_ast_op_mul] = "*",
1026 [isl_ast_op_pdiv_q] = "/",
1027 [isl_ast_op_pdiv_r] = "%",
1028 [isl_ast_op_div] = "/",
1029 [isl_ast_op_eq] = "==",
1030 [isl_ast_op_le] = "<=",
1031 [isl_ast_op_ge] = ">=",
1032 [isl_ast_op_lt] = "<",
1033 [isl_ast_op_gt] = ">"
1036 /* Precedence in C of the various operators.
1037 * Based on http://en.wikipedia.org/wiki/Operators_in_C_and_C++
1038 * Lowest value means highest precedence.
1040 static int op_prec[] = {
1041 [isl_ast_op_and] = 13,
1042 [isl_ast_op_and_then] = 13,
1043 [isl_ast_op_or] = 14,
1044 [isl_ast_op_or_else] = 14,
1045 [isl_ast_op_max] = 2,
1046 [isl_ast_op_min] = 2,
1047 [isl_ast_op_minus] = 3,
1048 [isl_ast_op_add] = 6,
1049 [isl_ast_op_sub] = 6,
1050 [isl_ast_op_mul] = 5,
1051 [isl_ast_op_div] = 5,
1052 [isl_ast_op_fdiv_q] = 2,
1053 [isl_ast_op_pdiv_q] = 5,
1054 [isl_ast_op_pdiv_r] = 5,
1055 [isl_ast_op_cond] = 15,
1056 [isl_ast_op_select] = 15,
1057 [isl_ast_op_eq] = 9,
1058 [isl_ast_op_le] = 8,
1059 [isl_ast_op_ge] = 8,
1060 [isl_ast_op_lt] = 8,
1061 [isl_ast_op_gt] = 8,
1062 [isl_ast_op_call] = 2
1065 /* Is the operator left-to-right associative?
1067 static int op_left[] = {
1068 [isl_ast_op_and] = 1,
1069 [isl_ast_op_and_then] = 1,
1070 [isl_ast_op_or] = 1,
1071 [isl_ast_op_or_else] = 1,
1072 [isl_ast_op_max] = 1,
1073 [isl_ast_op_min] = 1,
1074 [isl_ast_op_minus] = 0,
1075 [isl_ast_op_add] = 1,
1076 [isl_ast_op_sub] = 1,
1077 [isl_ast_op_mul] = 1,
1078 [isl_ast_op_div] = 1,
1079 [isl_ast_op_fdiv_q] = 1,
1080 [isl_ast_op_pdiv_q] = 1,
1081 [isl_ast_op_pdiv_r] = 1,
1082 [isl_ast_op_cond] = 0,
1083 [isl_ast_op_select] = 0,
1084 [isl_ast_op_eq] = 1,
1085 [isl_ast_op_le] = 1,
1086 [isl_ast_op_ge] = 1,
1087 [isl_ast_op_lt] = 1,
1088 [isl_ast_op_gt] = 1,
1089 [isl_ast_op_call] = 1
1092 static int is_and(enum isl_ast_op_type op)
1094 return op == isl_ast_op_and || op == isl_ast_op_and_then;
1097 static int is_or(enum isl_ast_op_type op)
1099 return op == isl_ast_op_or || op == isl_ast_op_or_else;
1102 static int is_add_sub(enum isl_ast_op_type op)
1104 return op == isl_ast_op_add || op == isl_ast_op_sub;
1107 static int is_div_mod(enum isl_ast_op_type op)
1109 return op == isl_ast_op_div || op == isl_ast_op_pdiv_r;
1112 /* Do we need/want parentheses around "expr" as a subexpression of
1113 * an "op" operation? If "left" is set, then "expr" is the left-most
1116 * We only need parentheses if "expr" represents an operation.
1118 * If op has a higher precedence than expr->u.op.op, then we need
1120 * If op and expr->u.op.op have the same precedence, but the operations
1121 * are performed in an order that is different from the associativity,
1122 * then we need parentheses.
1124 * An and inside an or technically does not require parentheses,
1125 * but some compilers complain about that, so we add them anyway.
1127 * Computations such as "a / b * c" and "a % b + c" can be somewhat
1128 * difficult to read, so we add parentheses for those as well.
1130 static int sub_expr_need_parens(enum isl_ast_op_type op,
1131 __isl_keep isl_ast_expr *expr, int left)
1133 if (expr->type != isl_ast_expr_op)
1136 if (op_prec[expr->u.op.op] > op_prec[op])
1138 if (op_prec[expr->u.op.op] == op_prec[op] && left != op_left[op])
1141 if (is_or(op) && is_and(expr->u.op.op))
1143 if (op == isl_ast_op_mul && expr->u.op.op != isl_ast_op_mul &&
1144 op_prec[expr->u.op.op] == op_prec[op])
1146 if (is_add_sub(op) && is_div_mod(expr->u.op.op))
1152 /* Print "expr" as a subexpression of an "op" operation.
1153 * If "left" is set, then "expr" is the left-most operand.
1155 static __isl_give isl_printer *print_sub_expr(__isl_take isl_printer *p,
1156 enum isl_ast_op_type op, __isl_keep isl_ast_expr *expr, int left)
1160 need_parens = sub_expr_need_parens(op, expr, left);
1163 p = isl_printer_print_str(p, "(");
1164 p = isl_printer_print_ast_expr(p, expr);
1166 p = isl_printer_print_str(p, ")");
1170 /* Print a min or max reduction "expr".
1172 static __isl_give isl_printer *print_min_max(__isl_take isl_printer *p,
1173 __isl_keep isl_ast_expr *expr)
1177 for (i = 1; i < expr->u.op.n_arg; ++i) {
1178 p = isl_printer_print_str(p, op_str[expr->u.op.op]);
1179 p = isl_printer_print_str(p, "(");
1181 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1182 for (i = 1; i < expr->u.op.n_arg; ++i) {
1183 p = isl_printer_print_str(p, ", ");
1184 p = isl_printer_print_ast_expr(p, expr->u.op.args[i]);
1185 p = isl_printer_print_str(p, ")");
1191 /* Print a function call "expr".
1193 * The first argument represents the function to be called.
1195 static __isl_give isl_printer *print_call(__isl_take isl_printer *p,
1196 __isl_keep isl_ast_expr *expr)
1200 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1201 p = isl_printer_print_str(p, "(");
1202 for (i = 1; i < expr->u.op.n_arg; ++i) {
1204 p = isl_printer_print_str(p, ", ");
1205 p = isl_printer_print_ast_expr(p, expr->u.op.args[i]);
1207 p = isl_printer_print_str(p, ")");
1212 /* Print "expr" to "p".
1214 * If we are printing in isl format, then we also print an indication
1215 * of the size of the expression (if it was computed).
1217 __isl_give isl_printer *isl_printer_print_ast_expr(__isl_take isl_printer *p,
1218 __isl_keep isl_ast_expr *expr)
1223 return isl_printer_free(p);
1225 switch (expr->type) {
1226 case isl_ast_expr_op:
1227 if (expr->u.op.op == isl_ast_op_call) {
1228 p = print_call(p, expr);
1231 if (expr->u.op.n_arg == 1) {
1232 p = isl_printer_print_str(p, op_str[expr->u.op.op]);
1233 p = print_sub_expr(p, expr->u.op.op,
1234 expr->u.op.args[0], 0);
1237 if (expr->u.op.op == isl_ast_op_fdiv_q) {
1238 p = isl_printer_print_str(p, "floord(");
1239 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1240 p = isl_printer_print_str(p, ", ");
1241 p = isl_printer_print_ast_expr(p, expr->u.op.args[1]);
1242 p = isl_printer_print_str(p, ")");
1245 if (expr->u.op.op == isl_ast_op_max ||
1246 expr->u.op.op == isl_ast_op_min) {
1247 p = print_min_max(p, expr);
1250 if (expr->u.op.op == isl_ast_op_cond ||
1251 expr->u.op.op == isl_ast_op_select) {
1252 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1253 p = isl_printer_print_str(p, " ? ");
1254 p = isl_printer_print_ast_expr(p, expr->u.op.args[1]);
1255 p = isl_printer_print_str(p, " : ");
1256 p = isl_printer_print_ast_expr(p, expr->u.op.args[2]);
1259 if (expr->u.op.n_arg != 2)
1260 isl_die(isl_printer_get_ctx(p), isl_error_internal,
1261 "operation should have two arguments",
1263 p = print_sub_expr(p, expr->u.op.op, expr->u.op.args[0], 1);
1264 p = isl_printer_print_str(p, " ");
1265 p = isl_printer_print_str(p, op_str[expr->u.op.op]);
1266 p = isl_printer_print_str(p, " ");
1267 p = print_sub_expr(p, expr->u.op.op, expr->u.op.args[1], 0);
1269 case isl_ast_expr_id:
1270 p = isl_printer_print_str(p, isl_id_get_name(expr->u.id));
1272 case isl_ast_expr_int:
1273 p = isl_printer_print_val(p, expr->u.v);
1275 case isl_ast_expr_error:
1281 isl_printer_free(p);
1285 /* Print "node" to "p" in "isl format".
1287 static __isl_give isl_printer *print_ast_node_isl(__isl_take isl_printer *p,
1288 __isl_keep isl_ast_node *node)
1290 p = isl_printer_print_str(p, "(");
1291 switch (node->type) {
1292 case isl_ast_node_for:
1293 if (node->u.f.degenerate) {
1294 p = isl_printer_print_ast_expr(p, node->u.f.init);
1296 p = isl_printer_print_str(p, "init: ");
1297 p = isl_printer_print_ast_expr(p, node->u.f.init);
1298 p = isl_printer_print_str(p, ", ");
1299 p = isl_printer_print_str(p, "cond: ");
1300 p = isl_printer_print_ast_expr(p, node->u.f.cond);
1301 p = isl_printer_print_str(p, ", ");
1302 p = isl_printer_print_str(p, "inc: ");
1303 p = isl_printer_print_ast_expr(p, node->u.f.inc);
1305 if (node->u.f.body) {
1306 p = isl_printer_print_str(p, ", ");
1307 p = isl_printer_print_str(p, "body: ");
1308 p = isl_printer_print_ast_node(p, node->u.f.body);
1311 case isl_ast_node_user:
1312 p = isl_printer_print_ast_expr(p, node->u.e.expr);
1314 case isl_ast_node_if:
1315 p = isl_printer_print_str(p, "guard: ");
1316 p = isl_printer_print_ast_expr(p, node->u.i.guard);
1317 if (node->u.i.then) {
1318 p = isl_printer_print_str(p, ", ");
1319 p = isl_printer_print_str(p, "then: ");
1320 p = isl_printer_print_ast_node(p, node->u.i.then);
1322 if (node->u.i.else_node) {
1323 p = isl_printer_print_str(p, ", ");
1324 p = isl_printer_print_str(p, "else: ");
1325 p = isl_printer_print_ast_node(p, node->u.i.else_node);
1328 case isl_ast_node_block:
1329 p = isl_printer_print_ast_node_list(p, node->u.b.children);
1334 p = isl_printer_print_str(p, ")");
1338 /* Do we need to print a block around the body "node" of a for or if node?
1340 * If the node is a block, then we need to print a block.
1341 * Also if the node is a degenerate for then we will print it as
1342 * an assignment followed by the body of the for loop, so we need a block
1345 static int need_block(__isl_keep isl_ast_node *node)
1347 if (node->type == isl_ast_node_block)
1349 if (node->type == isl_ast_node_for && node->u.f.degenerate)
1354 static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
1355 __isl_keep isl_ast_node *node,
1356 __isl_keep isl_ast_print_options *options, int in_block, int in_list);
1357 static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
1358 __isl_keep isl_ast_node *node,
1359 __isl_keep isl_ast_print_options *options, int new_line);
1361 /* Print the body "node" of a for or if node.
1362 * If "else_node" is set, then it is printed as well.
1364 * We first check if we need to print out a block.
1365 * We always print out a block if there is an else node to make
1366 * sure that the else node is matched to the correct if node.
1368 * If the else node is itself an if, then we print it as
1372 * Otherwise the else node is printed as
1377 static __isl_give isl_printer *print_body_c(__isl_take isl_printer *p,
1378 __isl_keep isl_ast_node *node, __isl_keep isl_ast_node *else_node,
1379 __isl_keep isl_ast_print_options *options)
1382 return isl_printer_free(p);
1384 if (!else_node && !need_block(node)) {
1385 p = isl_printer_end_line(p);
1386 p = isl_printer_indent(p, 2);
1387 p = isl_ast_node_print(node, p,
1388 isl_ast_print_options_copy(options));
1389 p = isl_printer_indent(p, -2);
1393 p = isl_printer_print_str(p, " {");
1394 p = isl_printer_end_line(p);
1395 p = isl_printer_indent(p, 2);
1396 p = print_ast_node_c(p, node, options, 1, 0);
1397 p = isl_printer_indent(p, -2);
1398 p = isl_printer_start_line(p);
1399 p = isl_printer_print_str(p, "}");
1401 if (else_node->type == isl_ast_node_if) {
1402 p = isl_printer_print_str(p, " else ");
1403 p = print_if_c(p, else_node, options, 0);
1405 p = isl_printer_print_str(p, " else");
1406 p = print_body_c(p, else_node, NULL, options);
1409 p = isl_printer_end_line(p);
1414 /* Print the start of a compound statement.
1416 static __isl_give isl_printer *start_block(__isl_take isl_printer *p)
1418 p = isl_printer_start_line(p);
1419 p = isl_printer_print_str(p, "{");
1420 p = isl_printer_end_line(p);
1421 p = isl_printer_indent(p, 2);
1426 /* Print the end of a compound statement.
1428 static __isl_give isl_printer *end_block(__isl_take isl_printer *p)
1430 p = isl_printer_indent(p, -2);
1431 p = isl_printer_start_line(p);
1432 p = isl_printer_print_str(p, "}");
1433 p = isl_printer_end_line(p);
1438 /* Print the for node "node".
1440 * If the for node is degenerate, it is printed as
1442 * type iterator = init;
1445 * Otherwise, it is printed as
1447 * for (type iterator = init; cond; iterator += inc)
1450 * "in_block" is set if we are currently inside a block.
1451 * "in_list" is set if the current node is not alone in the block.
1452 * If we are not in a block or if the current not is not alone in the block
1453 * then we print a block around a degenerate for loop such that the variable
1454 * declaration will not conflict with any potential other declaration
1455 * of the same variable.
1457 static __isl_give isl_printer *print_for_c(__isl_take isl_printer *p,
1458 __isl_keep isl_ast_node *node,
1459 __isl_keep isl_ast_print_options *options, int in_block, int in_list)
1465 type = isl_options_get_ast_iterator_type(isl_printer_get_ctx(p));
1466 if (!node->u.f.degenerate) {
1467 id = isl_ast_expr_get_id(node->u.f.iterator);
1468 name = isl_id_get_name(id);
1470 p = isl_printer_start_line(p);
1471 p = isl_printer_print_str(p, "for (");
1472 p = isl_printer_print_str(p, type);
1473 p = isl_printer_print_str(p, " ");
1474 p = isl_printer_print_str(p, name);
1475 p = isl_printer_print_str(p, " = ");
1476 p = isl_printer_print_ast_expr(p, node->u.f.init);
1477 p = isl_printer_print_str(p, "; ");
1478 p = isl_printer_print_ast_expr(p, node->u.f.cond);
1479 p = isl_printer_print_str(p, "; ");
1480 p = isl_printer_print_str(p, name);
1481 p = isl_printer_print_str(p, " += ");
1482 p = isl_printer_print_ast_expr(p, node->u.f.inc);
1483 p = isl_printer_print_str(p, ")");
1484 p = print_body_c(p, node->u.f.body, NULL, options);
1486 id = isl_ast_expr_get_id(node->u.f.iterator);
1487 name = isl_id_get_name(id);
1489 if (!in_block || in_list)
1491 p = isl_printer_start_line(p);
1492 p = isl_printer_print_str(p, type);
1493 p = isl_printer_print_str(p, " ");
1494 p = isl_printer_print_str(p, name);
1495 p = isl_printer_print_str(p, " = ");
1496 p = isl_printer_print_ast_expr(p, node->u.f.init);
1497 p = isl_printer_print_str(p, ";");
1498 p = isl_printer_end_line(p);
1499 p = print_ast_node_c(p, node->u.f.body, options, 1, 0);
1500 if (!in_block || in_list)
1507 /* Print the if node "node".
1508 * If "new_line" is set then the if node should be printed on a new line.
1510 static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
1511 __isl_keep isl_ast_node *node,
1512 __isl_keep isl_ast_print_options *options, int new_line)
1515 p = isl_printer_start_line(p);
1516 p = isl_printer_print_str(p, "if (");
1517 p = isl_printer_print_ast_expr(p, node->u.i.guard);
1518 p = isl_printer_print_str(p, ")");
1519 p = print_body_c(p, node->u.i.then, node->u.i.else_node, options);
1524 /* Print the "node" to "p".
1526 * "in_block" is set if we are currently inside a block.
1527 * If so, we do not print a block around the children of a block node.
1528 * We do this to avoid an extra block around the body of a degenerate
1531 * "in_list" is set if the current node is not alone in the block.
1533 static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
1534 __isl_keep isl_ast_node *node,
1535 __isl_keep isl_ast_print_options *options, int in_block, int in_list)
1537 switch (node->type) {
1538 case isl_ast_node_for:
1539 if (options->print_for)
1540 return options->print_for(p,
1541 isl_ast_print_options_copy(options),
1542 node, options->print_for_user);
1543 p = print_for_c(p, node, options, in_block, in_list);
1545 case isl_ast_node_if:
1546 p = print_if_c(p, node, options, 1);
1548 case isl_ast_node_block:
1551 p = isl_ast_node_list_print(node->u.b.children, p, options);
1555 case isl_ast_node_user:
1556 if (options->print_user)
1557 return options->print_user(p,
1558 isl_ast_print_options_copy(options),
1559 node, options->print_user_user);
1560 p = isl_printer_start_line(p);
1561 p = isl_printer_print_ast_expr(p, node->u.e.expr);
1562 p = isl_printer_print_str(p, ";");
1563 p = isl_printer_end_line(p);
1565 case isl_ast_node_error:
1571 /* Print the for node "node" to "p".
1573 __isl_give isl_printer *isl_ast_node_for_print(__isl_keep isl_ast_node *node,
1574 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
1576 if (!node || !options)
1578 if (node->type != isl_ast_node_for)
1579 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1580 "not a for node", goto error);
1581 p = print_for_c(p, node, options, 0, 0);
1582 isl_ast_print_options_free(options);
1585 isl_ast_print_options_free(options);
1586 isl_printer_free(p);
1590 /* Print the if node "node" to "p".
1592 __isl_give isl_printer *isl_ast_node_if_print(__isl_keep isl_ast_node *node,
1593 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
1595 if (!node || !options)
1597 if (node->type != isl_ast_node_if)
1598 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1599 "not an if node", goto error);
1600 p = print_if_c(p, node, options, 1);
1601 isl_ast_print_options_free(options);
1604 isl_ast_print_options_free(options);
1605 isl_printer_free(p);
1609 /* Print "node" to "p".
1611 __isl_give isl_printer *isl_ast_node_print(__isl_keep isl_ast_node *node,
1612 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
1614 if (!options || !node)
1616 p = print_ast_node_c(p, node, options, 0, 0);
1617 isl_ast_print_options_free(options);
1620 isl_ast_print_options_free(options);
1621 isl_printer_free(p);
1625 /* Print "node" to "p".
1627 __isl_give isl_printer *isl_printer_print_ast_node(__isl_take isl_printer *p,
1628 __isl_keep isl_ast_node *node)
1631 isl_ast_print_options *options;
1636 format = isl_printer_get_output_format(p);
1638 case ISL_FORMAT_ISL:
1639 p = print_ast_node_isl(p, node);
1642 options = isl_ast_print_options_alloc(isl_printer_get_ctx(p));
1643 p = isl_ast_node_print(node, p, options);
1646 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
1647 "output format not supported for ast_node",
1648 return isl_printer_free(p));
1654 /* Print the list of nodes "list" to "p".
1656 __isl_give isl_printer *isl_ast_node_list_print(
1657 __isl_keep isl_ast_node_list *list, __isl_take isl_printer *p,
1658 __isl_keep isl_ast_print_options *options)
1662 if (!p || !list || !options)
1663 return isl_printer_free(p);
1665 for (i = 0; i < list->n; ++i)
1666 p = print_ast_node_c(p, list->p[i], options, 1, 1);
1671 #define ISL_AST_MACRO_FLOORD (1 << 0)
1672 #define ISL_AST_MACRO_MIN (1 << 1)
1673 #define ISL_AST_MACRO_MAX (1 << 2)
1674 #define ISL_AST_MACRO_ALL (ISL_AST_MACRO_FLOORD | \
1675 ISL_AST_MACRO_MIN | \
1678 /* If "expr" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1679 * then set the corresponding bit in "macros".
1681 static int ast_expr_required_macros(__isl_keep isl_ast_expr *expr, int macros)
1685 if (macros == ISL_AST_MACRO_ALL)
1688 if (expr->type != isl_ast_expr_op)
1691 if (expr->u.op.op == isl_ast_op_min)
1692 macros |= ISL_AST_MACRO_MIN;
1693 if (expr->u.op.op == isl_ast_op_max)
1694 macros |= ISL_AST_MACRO_MAX;
1695 if (expr->u.op.op == isl_ast_op_fdiv_q)
1696 macros |= ISL_AST_MACRO_FLOORD;
1698 for (i = 0; i < expr->u.op.n_arg; ++i)
1699 macros = ast_expr_required_macros(expr->u.op.args[i], macros);
1704 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
1707 /* If "node" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1708 * then set the corresponding bit in "macros".
1710 static int ast_node_required_macros(__isl_keep isl_ast_node *node, int macros)
1712 if (macros == ISL_AST_MACRO_ALL)
1715 switch (node->type) {
1716 case isl_ast_node_for:
1717 macros = ast_expr_required_macros(node->u.f.init, macros);
1718 if (!node->u.f.degenerate) {
1719 macros = ast_expr_required_macros(node->u.f.cond,
1721 macros = ast_expr_required_macros(node->u.f.inc,
1724 macros = ast_node_required_macros(node->u.f.body, macros);
1726 case isl_ast_node_if:
1727 macros = ast_expr_required_macros(node->u.i.guard, macros);
1728 macros = ast_node_required_macros(node->u.i.then, macros);
1729 if (node->u.i.else_node)
1730 macros = ast_node_required_macros(node->u.i.else_node,
1733 case isl_ast_node_block:
1734 macros = ast_node_list_required_macros(node->u.b.children,
1737 case isl_ast_node_user:
1738 macros = ast_expr_required_macros(node->u.e.expr, macros);
1740 case isl_ast_node_error:
1747 /* If "list" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1748 * then set the corresponding bit in "macros".
1750 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
1755 for (i = 0; i < list->n; ++i)
1756 macros = ast_node_required_macros(list->p[i], macros);
1761 /* Print a macro definition for the operator "type".
1763 __isl_give isl_printer *isl_ast_op_type_print_macro(
1764 enum isl_ast_op_type type, __isl_take isl_printer *p)
1767 case isl_ast_op_min:
1768 p = isl_printer_start_line(p);
1769 p = isl_printer_print_str(p,
1770 "#define min(x,y) ((x) < (y) ? (x) : (y))");
1771 p = isl_printer_end_line(p);
1773 case isl_ast_op_max:
1774 p = isl_printer_start_line(p);
1775 p = isl_printer_print_str(p,
1776 "#define max(x,y) ((x) > (y) ? (x) : (y))");
1777 p = isl_printer_end_line(p);
1779 case isl_ast_op_fdiv_q:
1780 p = isl_printer_start_line(p);
1781 p = isl_printer_print_str(p,
1782 "#define floord(n,d) "
1783 "(((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))");
1784 p = isl_printer_end_line(p);
1793 /* Call "fn" for each type of operation that appears in "node"
1794 * and that requires a macro definition.
1796 int isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node *node,
1797 int (*fn)(enum isl_ast_op_type type, void *user), void *user)
1804 macros = ast_node_required_macros(node, 0);
1806 if (macros & ISL_AST_MACRO_MIN && fn(isl_ast_op_min, user) < 0)
1808 if (macros & ISL_AST_MACRO_MAX && fn(isl_ast_op_max, user) < 0)
1810 if (macros & ISL_AST_MACRO_FLOORD && fn(isl_ast_op_fdiv_q, user) < 0)
1816 static int ast_op_type_print_macro(enum isl_ast_op_type type, void *user)
1818 isl_printer **p = user;
1820 *p = isl_ast_op_type_print_macro(type, *p);
1825 /* Print macro definitions for all the macros used in the result
1826 * of printing "node.
1828 __isl_give isl_printer *isl_ast_node_print_macros(
1829 __isl_keep isl_ast_node *node, __isl_take isl_printer *p)
1831 if (isl_ast_node_foreach_ast_op_type(node,
1832 &ast_op_type_print_macro, &p) < 0)
1833 return isl_printer_free(p);