1 #include <isl_ast_private.h>
2 #include <isl_list_private.h>
7 #include <isl_list_templ.c>
12 #include <isl_list_templ.c>
14 __isl_give isl_ast_print_options
*isl_ast_print_options_alloc(isl_ctx
*ctx
)
16 return isl_calloc_type(ctx
, isl_ast_print_options
);
19 void *isl_ast_print_options_free(__isl_take isl_ast_print_options
*options
)
25 /* Set the print_user callback of "options" to "print_user".
27 * If this callback is set, then it used to print user nodes in the AST.
28 * Otherwise, the expression associated to the user node is printed.
30 __isl_give isl_ast_print_options
*isl_ast_print_options_set_print_user(
31 __isl_take isl_ast_print_options
*options
,
32 __isl_give isl_printer
*(*print_user
)(__isl_take isl_printer
*p
,
33 __isl_keep isl_ast_node
*node
, void *user
),
39 options
->print_user
= print_user
;
40 options
->print_user_user
= user
;
45 /* Set the print_for callback of "options" to "print_for".
47 * If this callback is set, then it used to print for nodes in the AST.
49 __isl_give isl_ast_print_options
*isl_ast_print_options_set_print_for(
50 __isl_take isl_ast_print_options
*options
,
51 __isl_give isl_printer
*(*print_for
)(__isl_take isl_printer
*p
,
52 __isl_keep isl_ast_node
*node
, void *user
),
58 options
->print_for
= print_for
;
59 options
->print_for_user
= user
;
64 __isl_give isl_ast_expr
*isl_ast_expr_copy(__isl_keep isl_ast_expr
*expr
)
73 __isl_give isl_ast_expr
*isl_ast_expr_dup(__isl_keep isl_ast_expr
*expr
)
82 ctx
= isl_ast_expr_get_ctx(expr
);
84 case isl_ast_expr_int
:
85 dup
= isl_ast_expr_alloc_int(ctx
, expr
->u
.i
);
88 dup
= isl_ast_expr_from_id(isl_id_copy(expr
->u
.id
));
91 dup
= isl_ast_expr_alloc_op(ctx
,
92 expr
->u
.op
.op
, expr
->u
.op
.n_arg
);
95 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
)
97 isl_ast_expr_copy(expr
->u
.op
.args
[i
]);
99 case isl_ast_expr_error
:
109 __isl_give isl_ast_expr
*isl_ast_expr_cow(__isl_take isl_ast_expr
*expr
)
117 return isl_ast_expr_dup(expr
);
120 void *isl_ast_expr_free(__isl_take isl_ast_expr
*expr
)
130 isl_ctx_deref(expr
->ctx
);
132 switch (expr
->type
) {
133 case isl_ast_expr_int
:
134 isl_int_clear(expr
->u
.i
);
136 case isl_ast_expr_id
:
137 isl_id_free(expr
->u
.id
);
139 case isl_ast_expr_op
:
140 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
)
141 isl_ast_expr_free(expr
->u
.op
.args
[i
]);
142 free(expr
->u
.op
.args
);
144 case isl_ast_expr_error
:
152 isl_ctx
*isl_ast_expr_get_ctx(__isl_keep isl_ast_expr
*expr
)
154 return expr
? expr
->ctx
: NULL
;
157 enum isl_ast_expr_type
isl_ast_expr_get_type(__isl_keep isl_ast_expr
*expr
)
159 return expr
? expr
->type
: isl_ast_expr_error
;
162 int isl_ast_expr_get_int(__isl_keep isl_ast_expr
*expr
, isl_int
*v
)
166 if (expr
->type
!= isl_ast_expr_int
)
167 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
168 "expression not an int", return -1);
169 isl_int_set(*v
, expr
->u
.i
);
173 __isl_give isl_id
*isl_ast_expr_get_id(__isl_keep isl_ast_expr
*expr
)
177 if (expr
->type
!= isl_ast_expr_id
)
178 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
179 "expression not an identifier", return NULL
);
181 return isl_id_copy(expr
->u
.id
);
184 enum isl_ast_op_type
isl_ast_expr_get_op_type(__isl_keep isl_ast_expr
*expr
)
187 return isl_ast_op_error
;
188 if (expr
->type
!= isl_ast_expr_op
)
189 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
190 "expression not an operation", return isl_ast_op_error
);
191 return expr
->u
.op
.op
;
194 int isl_ast_expr_get_op_n_arg(__isl_keep isl_ast_expr
*expr
)
198 if (expr
->type
!= isl_ast_expr_op
)
199 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
200 "expression not an operation", return -1);
201 return expr
->u
.op
.n_arg
;
204 __isl_give isl_ast_expr
*isl_ast_expr_get_op_arg(__isl_keep isl_ast_expr
*expr
,
209 if (expr
->type
!= isl_ast_expr_op
)
210 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
211 "expression not an operation", return NULL
);
212 if (pos
< 0 || pos
>= expr
->u
.op
.n_arg
)
213 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
214 "index out of bounds", return NULL
);
216 return isl_ast_expr_copy(expr
->u
.op
.args
[pos
]);
219 /* Replace the argument at position "pos" of "expr" by "arg".
221 __isl_give isl_ast_expr
*isl_ast_expr_set_op_arg(__isl_take isl_ast_expr
*expr
,
222 int pos
, __isl_take isl_ast_expr
*arg
)
224 expr
= isl_ast_expr_cow(expr
);
227 if (expr
->type
!= isl_ast_expr_op
)
228 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
229 "expression not an operation", goto error
);
230 if (pos
< 0 || pos
>= expr
->u
.op
.n_arg
)
231 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
232 "index out of bounds", goto error
);
234 isl_ast_expr_free(expr
->u
.op
.args
[pos
]);
235 expr
->u
.op
.args
[pos
] = arg
;
239 isl_ast_expr_free(arg
);
240 return isl_ast_expr_free(expr
);
243 /* Create a new operation expression of operation type "op",
244 * with "n_arg" as yet unspecified arguments.
246 __isl_give isl_ast_expr
*isl_ast_expr_alloc_op(isl_ctx
*ctx
,
247 enum isl_ast_op_type op
, int n_arg
)
251 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
258 expr
->type
= isl_ast_expr_op
;
260 expr
->u
.op
.n_arg
= n_arg
;
261 expr
->u
.op
.args
= isl_calloc_array(ctx
, isl_ast_expr
*, n_arg
);
263 if (!expr
->u
.op
.args
)
264 return isl_ast_expr_free(expr
);
269 /* Create a new id expression representing "id".
271 __isl_give isl_ast_expr
*isl_ast_expr_from_id(__isl_take isl_id
*id
)
279 ctx
= isl_id_get_ctx(id
);
280 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
282 return isl_id_free(id
);
287 expr
->type
= isl_ast_expr_id
;
293 /* Create a new integer expression representing "i".
295 __isl_give isl_ast_expr
*isl_ast_expr_alloc_int_si(isl_ctx
*ctx
, int i
)
299 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
306 expr
->type
= isl_ast_expr_int
;
308 isl_int_init(expr
->u
.i
);
309 isl_int_set_si(expr
->u
.i
, i
);
314 /* Create a new integer expression representing "i".
316 __isl_give isl_ast_expr
*isl_ast_expr_alloc_int(isl_ctx
*ctx
, isl_int i
)
320 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
327 expr
->type
= isl_ast_expr_int
;
329 isl_int_init(expr
->u
.i
);
330 isl_int_set(expr
->u
.i
, i
);
335 /* Create an expression representing the negation of "arg".
337 __isl_give isl_ast_expr
*isl_ast_expr_neg(__isl_take isl_ast_expr
*arg
)
340 isl_ast_expr
*expr
= NULL
;
345 ctx
= isl_ast_expr_get_ctx(arg
);
346 expr
= isl_ast_expr_alloc_op(ctx
, isl_ast_op_minus
, 1);
350 expr
->u
.op
.args
[0] = arg
;
354 isl_ast_expr_free(arg
);
358 /* Create an expression representing the binary operation "type"
359 * applied to "expr1" and "expr2".
361 __isl_give isl_ast_expr
*isl_ast_expr_alloc_binary(enum isl_ast_op_type type
,
362 __isl_take isl_ast_expr
*expr1
, __isl_take isl_ast_expr
*expr2
)
365 isl_ast_expr
*expr
= NULL
;
367 if (!expr1
|| !expr2
)
370 ctx
= isl_ast_expr_get_ctx(expr1
);
371 expr
= isl_ast_expr_alloc_op(ctx
, type
, 2);
375 expr
->u
.op
.args
[0] = expr1
;
376 expr
->u
.op
.args
[1] = expr2
;
380 isl_ast_expr_free(expr1
);
381 isl_ast_expr_free(expr2
);
385 /* Create an expression representing the sum of "expr1" and "expr2".
387 __isl_give isl_ast_expr
*isl_ast_expr_add(__isl_take isl_ast_expr
*expr1
,
388 __isl_take isl_ast_expr
*expr2
)
390 return isl_ast_expr_alloc_binary(isl_ast_op_add
, expr1
, expr2
);
393 /* Create an expression representing the difference of "expr1" and "expr2".
395 __isl_give isl_ast_expr
*isl_ast_expr_sub(__isl_take isl_ast_expr
*expr1
,
396 __isl_take isl_ast_expr
*expr2
)
398 return isl_ast_expr_alloc_binary(isl_ast_op_sub
, expr1
, expr2
);
401 /* Create an expression representing the product of "expr1" and "expr2".
403 __isl_give isl_ast_expr
*isl_ast_expr_mul(__isl_take isl_ast_expr
*expr1
,
404 __isl_take isl_ast_expr
*expr2
)
406 return isl_ast_expr_alloc_binary(isl_ast_op_mul
, expr1
, expr2
);
409 /* Create an expression representing the quotient of "expr1" and "expr2".
411 __isl_give isl_ast_expr
*isl_ast_expr_div(__isl_take isl_ast_expr
*expr1
,
412 __isl_take isl_ast_expr
*expr2
)
414 return isl_ast_expr_alloc_binary(isl_ast_op_div
, expr1
, expr2
);
417 /* Create an expression representing the conjunction of "expr1" and "expr2".
419 __isl_give isl_ast_expr
*isl_ast_expr_and(__isl_take isl_ast_expr
*expr1
,
420 __isl_take isl_ast_expr
*expr2
)
422 return isl_ast_expr_alloc_binary(isl_ast_op_and
, expr1
, expr2
);
425 /* Create an expression representing the disjunction of "expr1" and "expr2".
427 __isl_give isl_ast_expr
*isl_ast_expr_or(__isl_take isl_ast_expr
*expr1
,
428 __isl_take isl_ast_expr
*expr2
)
430 return isl_ast_expr_alloc_binary(isl_ast_op_or
, expr1
, expr2
);
433 isl_ctx
*isl_ast_node_get_ctx(__isl_keep isl_ast_node
*node
)
435 return node
? node
->ctx
: NULL
;
438 enum isl_ast_node_type
isl_ast_node_get_type(__isl_keep isl_ast_node
*node
)
440 return node
? node
->type
: isl_ast_node_error
;
443 __isl_give isl_ast_node
*isl_ast_node_alloc(isl_ctx
*ctx
,
444 enum isl_ast_node_type type
)
448 node
= isl_calloc_type(ctx
, isl_ast_node
);
460 /* Create an if node with the given guard.
462 * The then body needs to be filled in later.
464 __isl_give isl_ast_node
*isl_ast_node_alloc_if(__isl_take isl_ast_expr
*guard
)
471 node
= isl_ast_node_alloc(isl_ast_expr_get_ctx(guard
), isl_ast_node_if
);
474 node
->u
.i
.guard
= guard
;
478 isl_ast_expr_free(guard
);
482 /* Create a for node with the given iterator.
484 * The remaining fields need to be filled in later.
486 __isl_give isl_ast_node
*isl_ast_node_alloc_for(__isl_take isl_id
*id
)
494 ctx
= isl_id_get_ctx(id
);
495 node
= isl_ast_node_alloc(ctx
, isl_ast_node_for
);
499 node
->u
.f
.iterator
= isl_ast_expr_from_id(id
);
500 if (!node
->u
.f
.iterator
)
501 return isl_ast_node_free(node
);
506 /* Create a user node evaluating "expr".
508 __isl_give isl_ast_node
*isl_ast_node_alloc_user(__isl_take isl_ast_expr
*expr
)
516 ctx
= isl_ast_expr_get_ctx(expr
);
517 node
= isl_ast_node_alloc(ctx
, isl_ast_node_user
);
521 node
->u
.e
.expr
= expr
;
525 isl_ast_expr_free(expr
);
529 /* Create a block node with the given children.
531 __isl_give isl_ast_node
*isl_ast_node_alloc_block(
532 __isl_take isl_ast_node_list
*list
)
540 ctx
= isl_ast_node_list_get_ctx(list
);
541 node
= isl_ast_node_alloc(ctx
, isl_ast_node_block
);
545 node
->u
.b
.children
= list
;
549 isl_ast_node_list_free(list
);
553 /* Represent the given list of nodes as a single node, either by
554 * extract the node from a single element list or by creating
555 * a block node with the list of nodes as children.
557 __isl_give isl_ast_node
*isl_ast_node_from_ast_node_list(
558 __isl_take isl_ast_node_list
*list
)
562 if (isl_ast_node_list_n_ast_node(list
) != 1)
563 return isl_ast_node_alloc_block(list
);
565 node
= isl_ast_node_list_get_ast_node(list
, 0);
566 isl_ast_node_list_free(list
);
571 __isl_give isl_ast_node
*isl_ast_node_copy(__isl_keep isl_ast_node
*node
)
580 __isl_give isl_ast_node
*isl_ast_node_dup(__isl_keep isl_ast_node
*node
)
587 dup
= isl_ast_node_alloc(isl_ast_node_get_ctx(node
), node
->type
);
591 switch (node
->type
) {
592 case isl_ast_node_if
:
593 dup
->u
.i
.guard
= isl_ast_expr_copy(node
->u
.i
.guard
);
594 dup
->u
.i
.then
= isl_ast_node_copy(node
->u
.i
.then
);
595 dup
->u
.i
.else_node
= isl_ast_node_copy(node
->u
.i
.else_node
);
596 if (!dup
->u
.i
.guard
|| !dup
->u
.i
.then
||
597 (node
->u
.i
.else_node
&& !dup
->u
.i
.else_node
))
598 return isl_ast_node_free(dup
);
600 case isl_ast_node_for
:
601 dup
->u
.f
.iterator
= isl_ast_expr_copy(node
->u
.f
.iterator
);
602 dup
->u
.f
.init
= isl_ast_expr_copy(node
->u
.f
.init
);
603 dup
->u
.f
.cond
= isl_ast_expr_copy(node
->u
.f
.cond
);
604 dup
->u
.f
.inc
= isl_ast_expr_copy(node
->u
.f
.inc
);
605 dup
->u
.f
.body
= isl_ast_node_copy(node
->u
.f
.body
);
606 if (!dup
->u
.f
.iterator
|| !dup
->u
.f
.init
|| !dup
->u
.f
.cond
||
607 !dup
->u
.f
.inc
|| !dup
->u
.f
.body
)
608 return isl_ast_node_free(dup
);
610 case isl_ast_node_block
:
611 dup
->u
.b
.children
= isl_ast_node_list_copy(node
->u
.b
.children
);
612 if (!dup
->u
.b
.children
)
613 return isl_ast_node_free(dup
);
615 case isl_ast_node_user
:
616 dup
->u
.e
.expr
= isl_ast_expr_copy(node
->u
.e
.expr
);
618 return isl_ast_node_free(dup
);
620 case isl_ast_node_error
:
627 __isl_give isl_ast_node
*isl_ast_node_cow(__isl_take isl_ast_node
*node
)
635 return isl_ast_node_dup(node
);
638 void *isl_ast_node_free(__isl_take isl_ast_node
*node
)
646 switch (node
->type
) {
647 case isl_ast_node_if
:
648 isl_ast_expr_free(node
->u
.i
.guard
);
649 isl_ast_node_free(node
->u
.i
.then
);
650 isl_ast_node_free(node
->u
.i
.else_node
);
652 case isl_ast_node_for
:
653 isl_ast_expr_free(node
->u
.f
.iterator
);
654 isl_ast_expr_free(node
->u
.f
.init
);
655 isl_ast_expr_free(node
->u
.f
.cond
);
656 isl_ast_expr_free(node
->u
.f
.inc
);
657 isl_ast_node_free(node
->u
.f
.body
);
659 case isl_ast_node_block
:
660 isl_ast_node_list_free(node
->u
.b
.children
);
662 case isl_ast_node_user
:
663 isl_ast_expr_free(node
->u
.e
.expr
);
665 case isl_ast_node_error
:
669 isl_id_free(node
->annotation
);
670 isl_ctx_deref(node
->ctx
);
676 /* Replace the body of the for node "node" by "body".
678 __isl_give isl_ast_node
*isl_ast_node_for_set_body(
679 __isl_take isl_ast_node
*node
, __isl_take isl_ast_node
*body
)
681 node
= isl_ast_node_cow(node
);
684 if (node
->type
!= isl_ast_node_for
)
685 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
686 "not a for node", goto error
);
688 isl_ast_node_free(node
->u
.f
.body
);
689 node
->u
.f
.body
= body
;
693 isl_ast_node_free(node
);
694 isl_ast_node_free(body
);
698 __isl_give isl_ast_node
*isl_ast_node_for_get_body(
699 __isl_keep isl_ast_node
*node
)
703 if (node
->type
!= isl_ast_node_for
)
704 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
705 "not a for node", return NULL
);
706 return isl_ast_node_copy(node
->u
.f
.body
);
709 /* Mark the given for node as being degenerate.
711 __isl_give isl_ast_node
*isl_ast_node_for_mark_degenerate(
712 __isl_take isl_ast_node
*node
)
714 node
= isl_ast_node_cow(node
);
717 node
->u
.f
.degenerate
= 1;
721 int isl_ast_node_for_is_degenerate(__isl_keep isl_ast_node
*node
)
725 if (node
->type
!= isl_ast_node_for
)
726 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
727 "not a for node", return -1);
728 return node
->u
.f
.degenerate
;
731 __isl_give isl_ast_expr
*isl_ast_node_for_get_iterator(
732 __isl_keep isl_ast_node
*node
)
736 if (node
->type
!= isl_ast_node_for
)
737 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
738 "not a for node", return NULL
);
739 return isl_ast_expr_copy(node
->u
.f
.iterator
);
742 __isl_give isl_ast_expr
*isl_ast_node_for_get_init(
743 __isl_keep isl_ast_node
*node
)
747 if (node
->type
!= isl_ast_node_for
)
748 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
749 "not a for node", return NULL
);
750 return isl_ast_expr_copy(node
->u
.f
.init
);
753 /* Return the condition expression of the given for node.
755 * If the for node is degenerate, then the condition is not explicitly
756 * stored in the node. Instead, it is constructed as
760 __isl_give isl_ast_expr
*isl_ast_node_for_get_cond(
761 __isl_keep isl_ast_node
*node
)
765 if (node
->type
!= isl_ast_node_for
)
766 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
767 "not a for node", return NULL
);
768 if (!node
->u
.f
.degenerate
)
769 return isl_ast_expr_copy(node
->u
.f
.cond
);
771 return isl_ast_expr_alloc_binary(isl_ast_op_le
,
772 isl_ast_expr_copy(node
->u
.f
.iterator
),
773 isl_ast_expr_copy(node
->u
.f
.init
));
776 /* Return the increment of the given for node.
778 * If the for node is degenerate, then the increment is not explicitly
779 * stored in the node. We simply return "1".
781 __isl_give isl_ast_expr
*isl_ast_node_for_get_inc(
782 __isl_keep isl_ast_node
*node
)
786 if (node
->type
!= isl_ast_node_for
)
787 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
788 "not a for node", return NULL
);
789 if (!node
->u
.f
.degenerate
)
790 return isl_ast_expr_copy(node
->u
.f
.inc
);
791 return isl_ast_expr_alloc_int_si(isl_ast_node_get_ctx(node
), 1);
794 /* Replace the then branch of the if node "node" by "child".
796 __isl_give isl_ast_node
*isl_ast_node_if_set_then(
797 __isl_take isl_ast_node
*node
, __isl_take isl_ast_node
*child
)
799 node
= isl_ast_node_cow(node
);
802 if (node
->type
!= isl_ast_node_if
)
803 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
804 "not an if node", goto error
);
806 isl_ast_node_free(node
->u
.i
.then
);
807 node
->u
.i
.then
= child
;
811 isl_ast_node_free(node
);
812 isl_ast_node_free(child
);
816 __isl_give isl_ast_node
*isl_ast_node_if_get_then(
817 __isl_keep isl_ast_node
*node
)
821 if (node
->type
!= isl_ast_node_if
)
822 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
823 "not an if node", return NULL
);
824 return isl_ast_node_copy(node
->u
.i
.then
);
827 int isl_ast_node_if_has_else(
828 __isl_keep isl_ast_node
*node
)
832 if (node
->type
!= isl_ast_node_if
)
833 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
834 "not an if node", return -1);
835 return node
->u
.i
.else_node
!= NULL
;
838 __isl_give isl_ast_node
*isl_ast_node_if_get_else(
839 __isl_keep isl_ast_node
*node
)
843 if (node
->type
!= isl_ast_node_if
)
844 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
845 "not an if node", return NULL
);
846 return isl_ast_node_copy(node
->u
.i
.else_node
);
849 __isl_give isl_ast_expr
*isl_ast_node_if_get_cond(
850 __isl_keep isl_ast_node
*node
)
854 if (node
->type
!= isl_ast_node_if
)
855 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
856 "not a guard node", return NULL
);
857 return isl_ast_expr_copy(node
->u
.i
.guard
);
860 __isl_give isl_ast_node_list
*isl_ast_node_block_get_children(
861 __isl_keep isl_ast_node
*node
)
865 if (node
->type
!= isl_ast_node_block
)
866 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
867 "not a block node", return NULL
);
868 return isl_ast_node_list_copy(node
->u
.b
.children
);
871 __isl_give isl_ast_expr
*isl_ast_node_user_get_expr(
872 __isl_keep isl_ast_node
*node
)
877 return isl_ast_expr_copy(node
->u
.e
.expr
);
880 __isl_give isl_id
*isl_ast_node_get_annotation(__isl_keep isl_ast_node
*node
)
882 return node
? isl_id_copy(node
->annotation
) : NULL
;
885 /* Replace node->annotation by "annotation".
887 __isl_give isl_ast_node
*isl_ast_node_set_annotation(
888 __isl_take isl_ast_node
*node
, __isl_take isl_id
*annotation
)
890 node
= isl_ast_node_cow(node
);
891 if (!node
|| !annotation
)
894 isl_id_free(node
->annotation
);
895 node
->annotation
= annotation
;
899 isl_id_free(annotation
);
900 return isl_ast_node_free(node
);
903 /* Textual C representation of the various operators.
905 static char *op_str
[] = {
906 [isl_ast_op_and
] = "&&",
907 [isl_ast_op_and_then
] = "&&",
908 [isl_ast_op_or
] = "||",
909 [isl_ast_op_or_else
] = "||",
910 [isl_ast_op_max
] = "max",
911 [isl_ast_op_min
] = "min",
912 [isl_ast_op_minus
] = "-",
913 [isl_ast_op_add
] = "+",
914 [isl_ast_op_sub
] = "-",
915 [isl_ast_op_mul
] = "*",
916 [isl_ast_op_pdiv_q
] = "/",
917 [isl_ast_op_pdiv_r
] = "%",
918 [isl_ast_op_div
] = "/",
919 [isl_ast_op_eq
] = "==",
920 [isl_ast_op_le
] = "<=",
921 [isl_ast_op_ge
] = ">="
924 /* Precedence in C of the various operators.
925 * Based on http://en.wikipedia.org/wiki/Operators_in_C_and_C++
926 * Lowest value means highest precedence.
928 static int op_prec
[] = {
929 [isl_ast_op_and
] = 13,
930 [isl_ast_op_and_then
] = 13,
931 [isl_ast_op_or
] = 14,
932 [isl_ast_op_or_else
] = 14,
933 [isl_ast_op_max
] = 2,
934 [isl_ast_op_min
] = 2,
935 [isl_ast_op_minus
] = 3,
936 [isl_ast_op_add
] = 6,
937 [isl_ast_op_sub
] = 6,
938 [isl_ast_op_mul
] = 5,
939 [isl_ast_op_div
] = 5,
940 [isl_ast_op_fdiv_q
] = 2,
941 [isl_ast_op_pdiv_q
] = 5,
942 [isl_ast_op_pdiv_r
] = 5,
943 [isl_ast_op_cond
] = 15,
944 [isl_ast_op_select
] = 15,
948 [isl_ast_op_call
] = 2
951 /* Is the operator left-to-right associative?
953 static int op_left
[] = {
954 [isl_ast_op_and
] = 1,
955 [isl_ast_op_and_then
] = 1,
957 [isl_ast_op_or_else
] = 1,
958 [isl_ast_op_max
] = 1,
959 [isl_ast_op_min
] = 1,
960 [isl_ast_op_minus
] = 0,
961 [isl_ast_op_add
] = 1,
962 [isl_ast_op_sub
] = 1,
963 [isl_ast_op_mul
] = 1,
964 [isl_ast_op_div
] = 1,
965 [isl_ast_op_fdiv_q
] = 1,
966 [isl_ast_op_pdiv_q
] = 1,
967 [isl_ast_op_pdiv_r
] = 1,
968 [isl_ast_op_cond
] = 0,
969 [isl_ast_op_select
] = 0,
973 [isl_ast_op_call
] = 1
976 static int is_and(enum isl_ast_op_type op
)
978 return op
== isl_ast_op_and
|| op
== isl_ast_op_and_then
;
981 static int is_or(enum isl_ast_op_type op
)
983 return op
== isl_ast_op_or
|| op
== isl_ast_op_or_else
;
986 static int is_add_sub(enum isl_ast_op_type op
)
988 return op
== isl_ast_op_add
|| op
== isl_ast_op_sub
;
991 static int is_div_mod(enum isl_ast_op_type op
)
993 return op
== isl_ast_op_div
|| op
== isl_ast_op_pdiv_r
;
996 /* Do we need/want parentheses around "expr" as a subexpression of
997 * an "op" operation? If "left" is set, then "expr" is the left-most
1000 * We only need parentheses if "expr" represents an operation.
1002 * If op has a higher precedence than expr->u.op.op, then we need
1004 * If op and expr->u.op.op have the same precedence, but the operations
1005 * are performed in an order that is different from the associativity,
1006 * then we need parentheses.
1008 * An and inside an or technically does not require parentheses,
1009 * but some compilers complain about that, so we add them anyway.
1011 * Computations such as "a / b * c" and "a % b + c" can be somewhat
1012 * difficult to read, so we add parentheses for those as well.
1014 static int sub_expr_need_parens(enum isl_ast_op_type op
,
1015 __isl_keep isl_ast_expr
*expr
, int left
)
1017 if (expr
->type
!= isl_ast_expr_op
)
1020 if (op_prec
[expr
->u
.op
.op
] > op_prec
[op
])
1022 if (op_prec
[expr
->u
.op
.op
] == op_prec
[op
] && left
!= op_left
[op
])
1025 if (is_or(op
) && is_and(expr
->u
.op
.op
))
1027 if (op
== isl_ast_op_mul
&& expr
->u
.op
.op
!= isl_ast_op_mul
&&
1028 op_prec
[expr
->u
.op
.op
] == op_prec
[op
])
1030 if (is_add_sub(op
) && is_div_mod(expr
->u
.op
.op
))
1036 /* Print "expr" as a subexpression of an "op" operation.
1037 * If "left" is set, then "expr" is the left-most operand.
1039 static __isl_give isl_printer
*print_sub_expr(__isl_take isl_printer
*p
,
1040 enum isl_ast_op_type op
, __isl_keep isl_ast_expr
*expr
, int left
)
1044 need_parens
= sub_expr_need_parens(op
, expr
, left
);
1047 p
= isl_printer_print_str(p
, "(");
1048 p
= isl_printer_print_ast_expr(p
, expr
);
1050 p
= isl_printer_print_str(p
, ")");
1054 /* Print a min or max reduction "expr".
1056 static __isl_give isl_printer
*print_min_max(__isl_take isl_printer
*p
,
1057 __isl_keep isl_ast_expr
*expr
)
1061 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1062 p
= isl_printer_print_str(p
, op_str
[expr
->u
.op
.op
]);
1063 p
= isl_printer_print_str(p
, "(");
1065 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1066 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1067 p
= isl_printer_print_str(p
, ", ");
1068 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[i
]);
1069 p
= isl_printer_print_str(p
, ")");
1075 /* Print a function call "expr".
1077 * The first argument represents the function to be called.
1079 static __isl_give isl_printer
*print_call(__isl_take isl_printer
*p
,
1080 __isl_keep isl_ast_expr
*expr
)
1084 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1085 p
= isl_printer_print_str(p
, "(");
1086 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1088 p
= isl_printer_print_str(p
, ", ");
1089 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[i
]);
1091 p
= isl_printer_print_str(p
, ")");
1096 /* Print "expr" to "p".
1098 * If we are printing in isl format, then we also print an indication
1099 * of the size of the expression (if it was computed).
1101 __isl_give isl_printer
*isl_printer_print_ast_expr(__isl_take isl_printer
*p
,
1102 __isl_keep isl_ast_expr
*expr
)
1107 return isl_printer_free(p
);
1109 switch (expr
->type
) {
1110 case isl_ast_expr_op
:
1111 if (expr
->u
.op
.op
== isl_ast_op_call
) {
1112 p
= print_call(p
, expr
);
1115 if (expr
->u
.op
.n_arg
== 1) {
1116 p
= isl_printer_print_str(p
, op_str
[expr
->u
.op
.op
]);
1117 p
= print_sub_expr(p
, expr
->u
.op
.op
,
1118 expr
->u
.op
.args
[0], 0);
1121 if (expr
->u
.op
.op
== isl_ast_op_fdiv_q
) {
1122 p
= isl_printer_print_str(p
, "floord(");
1123 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1124 p
= isl_printer_print_str(p
, ", ");
1125 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[1]);
1126 p
= isl_printer_print_str(p
, ")");
1129 if (expr
->u
.op
.op
== isl_ast_op_max
||
1130 expr
->u
.op
.op
== isl_ast_op_min
) {
1131 p
= print_min_max(p
, expr
);
1134 if (expr
->u
.op
.op
== isl_ast_op_cond
||
1135 expr
->u
.op
.op
== isl_ast_op_select
) {
1136 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1137 p
= isl_printer_print_str(p
, " ? ");
1138 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[1]);
1139 p
= isl_printer_print_str(p
, " : ");
1140 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[2]);
1143 if (expr
->u
.op
.n_arg
!= 2)
1144 isl_die(isl_printer_get_ctx(p
), isl_error_internal
,
1145 "operation should have two arguments",
1147 p
= print_sub_expr(p
, expr
->u
.op
.op
, expr
->u
.op
.args
[0], 1);
1148 p
= isl_printer_print_str(p
, " ");
1149 p
= isl_printer_print_str(p
, op_str
[expr
->u
.op
.op
]);
1150 p
= isl_printer_print_str(p
, " ");
1151 p
= print_sub_expr(p
, expr
->u
.op
.op
, expr
->u
.op
.args
[1], 0);
1153 case isl_ast_expr_id
:
1154 p
= isl_printer_print_str(p
, isl_id_get_name(expr
->u
.id
));
1156 case isl_ast_expr_int
:
1157 p
= isl_printer_print_isl_int(p
, expr
->u
.i
);
1159 case isl_ast_expr_error
:
1165 isl_printer_free(p
);
1169 /* Print "node" to "p" in "isl format".
1171 static __isl_give isl_printer
*print_ast_node_isl(__isl_take isl_printer
*p
,
1172 __isl_keep isl_ast_node
*node
)
1174 p
= isl_printer_print_str(p
, "(");
1175 switch (node
->type
) {
1176 case isl_ast_node_for
:
1177 if (node
->u
.f
.degenerate
) {
1178 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1180 p
= isl_printer_print_str(p
, "init: ");
1181 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1182 p
= isl_printer_print_str(p
, ", ");
1183 p
= isl_printer_print_str(p
, "cond: ");
1184 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.cond
);
1185 p
= isl_printer_print_str(p
, ", ");
1186 p
= isl_printer_print_str(p
, "inc: ");
1187 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.inc
);
1189 if (node
->u
.f
.body
) {
1190 p
= isl_printer_print_str(p
, ", ");
1191 p
= isl_printer_print_str(p
, "body: ");
1192 p
= isl_printer_print_ast_node(p
, node
->u
.f
.body
);
1195 case isl_ast_node_user
:
1196 p
= isl_printer_print_ast_expr(p
, node
->u
.e
.expr
);
1198 case isl_ast_node_if
:
1199 p
= isl_printer_print_str(p
, "guard: ");
1200 p
= isl_printer_print_ast_expr(p
, node
->u
.i
.guard
);
1201 if (node
->u
.i
.then
) {
1202 p
= isl_printer_print_str(p
, ", ");
1203 p
= isl_printer_print_str(p
, "then: ");
1204 p
= isl_printer_print_ast_node(p
, node
->u
.i
.then
);
1206 if (node
->u
.i
.else_node
) {
1207 p
= isl_printer_print_str(p
, ", ");
1208 p
= isl_printer_print_str(p
, "else: ");
1209 p
= isl_printer_print_ast_node(p
, node
->u
.i
.else_node
);
1212 case isl_ast_node_block
:
1213 p
= isl_printer_print_ast_node_list(p
, node
->u
.b
.children
);
1218 p
= isl_printer_print_str(p
, ")");
1222 /* Do we need to print a block around the body "node" of a for or if node?
1224 * If the node is a block, then we need to print a block.
1225 * Also if the node is a degenerate for then we will print it as
1226 * an assignment followed by the body of the for loop, so we need a block
1229 static int need_block(__isl_keep isl_ast_node
*node
)
1231 if (node
->type
== isl_ast_node_block
)
1233 if (node
->type
== isl_ast_node_for
&& node
->u
.f
.degenerate
)
1238 static __isl_give isl_printer
*print_ast_node_c(__isl_take isl_printer
*p
,
1239 __isl_keep isl_ast_node
*node
,
1240 __isl_keep isl_ast_print_options
*options
, int in_block
);
1241 static __isl_give isl_printer
*print_if_c(__isl_take isl_printer
*p
,
1242 __isl_keep isl_ast_node
*node
,
1243 __isl_keep isl_ast_print_options
*options
, int new_line
);
1245 /* Print the body "node" of a for or if node.
1246 * If "else_node" is set, then it is printed as well.
1248 * We first check if we need to print out a block.
1249 * We always print out a block if there is an else node to make
1250 * sure that the else node is matched to the correct if node.
1252 * If the else node is itself an if, then we print it as
1256 * Otherwise the else node is printed as
1261 static __isl_give isl_printer
*print_body_c(__isl_take isl_printer
*p
,
1262 __isl_keep isl_ast_node
*node
, __isl_keep isl_ast_node
*else_node
,
1263 __isl_keep isl_ast_print_options
*options
)
1266 return isl_printer_free(p
);
1268 if (!else_node
&& !need_block(node
)) {
1269 p
= isl_printer_end_line(p
);
1270 p
= isl_printer_indent(p
, 2);
1271 p
= isl_ast_node_print(node
, p
, options
);
1272 p
= isl_printer_indent(p
, -2);
1276 p
= isl_printer_print_str(p
, " {");
1277 p
= isl_printer_end_line(p
);
1278 p
= isl_printer_indent(p
, 2);
1279 p
= print_ast_node_c(p
, node
, options
, 1);
1280 p
= isl_printer_indent(p
, -2);
1281 p
= isl_printer_start_line(p
);
1282 p
= isl_printer_print_str(p
, "}");
1284 if (else_node
->type
== isl_ast_node_if
) {
1285 p
= isl_printer_print_str(p
, " else ");
1286 p
= print_if_c(p
, else_node
, options
, 0);
1288 p
= isl_printer_print_str(p
, " else");
1289 p
= print_body_c(p
, else_node
, NULL
, options
);
1292 p
= isl_printer_end_line(p
);
1297 /* Print the for node "node".
1299 * If the for node is degenerate, it is printed as
1301 * type iterator = init;
1304 * Otherwise, it is printed as
1306 * for (type iterator = init; cond; iterator += inc)
1309 * "in_block" is set if we are currently inside a block.
1310 * We simply pass it along to print_ast_node_c in case of a degenerate
1313 static __isl_give isl_printer
*print_for_c(__isl_take isl_printer
*p
,
1314 __isl_keep isl_ast_node
*node
,
1315 __isl_keep isl_ast_print_options
*options
, int in_block
)
1321 type
= isl_options_get_ast_iterator_type(isl_printer_get_ctx(p
));
1322 if (!node
->u
.f
.degenerate
) {
1323 id
= isl_ast_expr_get_id(node
->u
.f
.iterator
);
1324 name
= isl_id_get_name(id
);
1326 p
= isl_printer_start_line(p
);
1327 p
= isl_printer_print_str(p
, "for (");
1328 p
= isl_printer_print_str(p
, type
);
1329 p
= isl_printer_print_str(p
, " ");
1330 p
= isl_printer_print_str(p
, name
);
1331 p
= isl_printer_print_str(p
, " = ");
1332 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1333 p
= isl_printer_print_str(p
, "; ");
1334 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.cond
);
1335 p
= isl_printer_print_str(p
, "; ");
1336 p
= isl_printer_print_str(p
, name
);
1337 p
= isl_printer_print_str(p
, " += ");
1338 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.inc
);
1339 p
= isl_printer_print_str(p
, ")");
1340 p
= print_body_c(p
, node
->u
.f
.body
, NULL
, options
);
1342 id
= isl_ast_expr_get_id(node
->u
.f
.iterator
);
1343 name
= isl_id_get_name(id
);
1345 p
= isl_printer_start_line(p
);
1346 p
= isl_printer_print_str(p
, type
);
1347 p
= isl_printer_print_str(p
, " ");
1348 p
= isl_printer_print_str(p
, name
);
1349 p
= isl_printer_print_str(p
, " = ");
1350 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1351 p
= isl_printer_print_str(p
, ";");
1352 p
= isl_printer_end_line(p
);
1353 p
= print_ast_node_c(p
, node
->u
.f
.body
, options
, in_block
);
1359 /* Print the if node "node".
1360 * If "new_line" is set then the if node should be printed on a new line.
1362 static __isl_give isl_printer
*print_if_c(__isl_take isl_printer
*p
,
1363 __isl_keep isl_ast_node
*node
,
1364 __isl_keep isl_ast_print_options
*options
, int new_line
)
1367 p
= isl_printer_start_line(p
);
1368 p
= isl_printer_print_str(p
, "if (");
1369 p
= isl_printer_print_ast_expr(p
, node
->u
.i
.guard
);
1370 p
= isl_printer_print_str(p
, ")");
1371 p
= print_body_c(p
, node
->u
.i
.then
, node
->u
.i
.else_node
, options
);
1376 /* Print the "node" to "p".
1378 * "in_block" is set if we are currently inside a block.
1379 * If so, we do not print a block around the children of a block node.
1380 * We do this to avoid an extra block around the body of a degenerate
1383 static __isl_give isl_printer
*print_ast_node_c(__isl_take isl_printer
*p
,
1384 __isl_keep isl_ast_node
*node
,
1385 __isl_keep isl_ast_print_options
*options
, int in_block
)
1387 switch (node
->type
) {
1388 case isl_ast_node_for
:
1389 if (options
->print_for
)
1390 return options
->print_for(p
, node
,
1391 options
->print_for_user
);
1392 p
= print_for_c(p
, node
, options
, in_block
);
1394 case isl_ast_node_if
:
1395 p
= print_if_c(p
, node
, options
, 1);
1397 case isl_ast_node_block
:
1399 p
= isl_printer_start_line(p
);
1400 p
= isl_printer_print_str(p
, "{");
1401 p
= isl_printer_end_line(p
);
1402 p
= isl_printer_indent(p
, 2);
1404 p
= isl_ast_node_list_print(node
->u
.b
.children
, p
, options
);
1406 p
= isl_printer_indent(p
, -2);
1407 p
= isl_printer_start_line(p
);
1408 p
= isl_printer_print_str(p
, "}");
1409 p
= isl_printer_end_line(p
);
1412 case isl_ast_node_user
:
1413 if (options
->print_user
)
1414 return options
->print_user(p
, node
,
1415 options
->print_user_user
);
1416 p
= isl_printer_start_line(p
);
1417 p
= isl_printer_print_ast_expr(p
, node
->u
.e
.expr
);
1418 p
= isl_printer_print_str(p
, ";");
1419 p
= isl_printer_end_line(p
);
1421 case isl_ast_node_error
:
1427 /* Print the for node "node" to "p".
1429 __isl_give isl_printer
*isl_ast_node_for_print(__isl_keep isl_ast_node
*node
,
1430 __isl_take isl_printer
*p
, __isl_keep isl_ast_print_options
*options
)
1432 if (!node
|| !options
)
1434 if (node
->type
!= isl_ast_node_for
)
1435 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1436 "not a for node", goto error
);
1437 return print_for_c(p
, node
, options
, 0);
1439 isl_printer_free(p
);
1443 /* Print the if node "node" to "p".
1445 __isl_give isl_printer
*isl_ast_node_if_print(__isl_keep isl_ast_node
*node
,
1446 __isl_take isl_printer
*p
, __isl_keep isl_ast_print_options
*options
)
1448 if (!node
|| !options
)
1450 if (node
->type
!= isl_ast_node_if
)
1451 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1452 "not an if node", goto error
);
1453 return print_if_c(p
, node
, options
, 1);
1455 isl_printer_free(p
);
1459 /* Print "node" to "p".
1461 __isl_give isl_printer
*isl_ast_node_print(__isl_keep isl_ast_node
*node
,
1462 __isl_take isl_printer
*p
, __isl_keep isl_ast_print_options
*options
)
1464 if (!options
|| !node
)
1466 return print_ast_node_c(p
, node
, options
, 0);
1468 isl_printer_free(p
);
1472 /* Print "node" to "p".
1474 __isl_give isl_printer
*isl_printer_print_ast_node(__isl_take isl_printer
*p
,
1475 __isl_keep isl_ast_node
*node
)
1478 isl_ast_print_options
*options
;
1483 format
= isl_printer_get_output_format(p
);
1485 case ISL_FORMAT_ISL
:
1486 p
= print_ast_node_isl(p
, node
);
1489 options
= isl_ast_print_options_alloc(isl_printer_get_ctx(p
));
1490 p
= isl_ast_node_print(node
, p
, options
);
1491 isl_ast_print_options_free(options
);
1494 isl_die(isl_printer_get_ctx(p
), isl_error_unsupported
,
1495 "output format not supported for ast_node",
1496 return isl_printer_free(p
));
1502 /* Print the list of nodes "list" to "p".
1504 __isl_give isl_printer
*isl_ast_node_list_print(
1505 __isl_keep isl_ast_node_list
*list
, __isl_take isl_printer
*p
,
1506 __isl_keep isl_ast_print_options
*options
)
1510 if (!p
|| !list
|| !options
)
1511 return isl_printer_free(p
);
1513 for (i
= 0; i
< list
->n
; ++i
)
1514 p
= print_ast_node_c(p
, list
->p
[i
], options
, 1);
1519 #define ISL_AST_MACRO_FLOORD (1 << 0)
1520 #define ISL_AST_MACRO_MIN (1 << 1)
1521 #define ISL_AST_MACRO_MAX (1 << 2)
1522 #define ISL_AST_MACRO_ALL (ISL_AST_MACRO_FLOORD | \
1523 ISL_AST_MACRO_MIN | \
1526 /* If "expr" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1527 * then set the corresponding bit in "macros".
1529 static int ast_expr_required_macros(__isl_keep isl_ast_expr
*expr
, int macros
)
1533 if (macros
== ISL_AST_MACRO_ALL
)
1536 if (expr
->type
!= isl_ast_expr_op
)
1539 if (expr
->u
.op
.op
== isl_ast_op_min
)
1540 macros
|= ISL_AST_MACRO_MIN
;
1541 if (expr
->u
.op
.op
== isl_ast_op_max
)
1542 macros
|= ISL_AST_MACRO_MAX
;
1543 if (expr
->u
.op
.op
== isl_ast_op_fdiv_q
)
1544 macros
|= ISL_AST_MACRO_FLOORD
;
1546 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
)
1547 macros
= ast_expr_required_macros(expr
->u
.op
.args
[i
], macros
);
1552 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list
*list
,
1555 /* If "node" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1556 * then set the corresponding bit in "macros".
1558 static int ast_node_required_macros(__isl_keep isl_ast_node
*node
, int macros
)
1560 if (macros
== ISL_AST_MACRO_ALL
)
1563 switch (node
->type
) {
1564 case isl_ast_node_for
:
1565 macros
= ast_expr_required_macros(node
->u
.f
.init
, macros
);
1566 if (!node
->u
.f
.degenerate
) {
1567 macros
= ast_expr_required_macros(node
->u
.f
.cond
,
1569 macros
= ast_expr_required_macros(node
->u
.f
.inc
,
1572 macros
= ast_node_required_macros(node
->u
.f
.body
, macros
);
1574 case isl_ast_node_if
:
1575 macros
= ast_expr_required_macros(node
->u
.i
.guard
, macros
);
1576 macros
= ast_node_required_macros(node
->u
.i
.then
, macros
);
1577 if (node
->u
.i
.else_node
)
1578 macros
= ast_node_required_macros(node
->u
.i
.else_node
,
1581 case isl_ast_node_block
:
1582 macros
= ast_node_list_required_macros(node
->u
.b
.children
,
1585 case isl_ast_node_user
:
1586 macros
= ast_expr_required_macros(node
->u
.e
.expr
, macros
);
1588 case isl_ast_node_error
:
1595 /* If "list" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1596 * then set the corresponding bit in "macros".
1598 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list
*list
,
1603 for (i
= 0; i
< list
->n
; ++i
)
1604 macros
= ast_node_required_macros(list
->p
[i
], macros
);
1609 /* Print a macro definition for the operator "type".
1611 __isl_give isl_printer
*isl_ast_op_type_print_macro(
1612 enum isl_ast_op_type type
, __isl_take isl_printer
*p
)
1615 case isl_ast_op_min
:
1616 p
= isl_printer_start_line(p
);
1617 p
= isl_printer_print_str(p
,
1618 "#define min(x,y) ((x) < (y) ? (x) : (y))");
1619 p
= isl_printer_end_line(p
);
1621 case isl_ast_op_max
:
1622 p
= isl_printer_start_line(p
);
1623 p
= isl_printer_print_str(p
,
1624 "#define max(x,y) ((x) > (y) ? (x) : (y))");
1625 p
= isl_printer_end_line(p
);
1627 case isl_ast_op_fdiv_q
:
1628 p
= isl_printer_start_line(p
);
1629 p
= isl_printer_print_str(p
,
1630 "#define floord(n,d) "
1631 "(((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))");
1632 p
= isl_printer_end_line(p
);
1641 /* Call "fn" for each type of operation that appears in "node"
1642 * and that requires a macro definition.
1644 int isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node
*node
,
1645 int (*fn
)(enum isl_ast_op_type type
, void *user
), void *user
)
1652 macros
= ast_node_required_macros(node
, 0);
1654 if (macros
& ISL_AST_MACRO_MIN
&& fn(isl_ast_op_min
, user
) < 0)
1656 if (macros
& ISL_AST_MACRO_MAX
&& fn(isl_ast_op_max
, user
) < 0)
1658 if (macros
& ISL_AST_MACRO_FLOORD
&& fn(isl_ast_op_fdiv_q
, user
) < 0)
1664 static int ast_op_type_print_macro(enum isl_ast_op_type type
, void *user
)
1666 isl_printer
**p
= user
;
1668 *p
= isl_ast_op_type_print_macro(type
, *p
);
1673 /* Print macro definitions for all the macros used in the result
1674 * of printing "node.
1676 __isl_give isl_printer
*isl_ast_node_print_macros(
1677 __isl_keep isl_ast_node
*node
, __isl_take isl_printer
*p
)
1679 if (isl_ast_node_foreach_ast_op_type(node
,
1680 &ast_op_type_print_macro
, &p
) < 0)
1681 return isl_printer_free(p
);