1 #include <isl_ast_private.h>
6 #include <isl_list_templ.c>
11 #include <isl_list_templ.c>
13 isl_ctx
*isl_ast_print_options_get_ctx(
14 __isl_keep isl_ast_print_options
*options
)
16 return options
? options
->ctx
: NULL
;
19 __isl_give isl_ast_print_options
*isl_ast_print_options_alloc(isl_ctx
*ctx
)
21 isl_ast_print_options
*options
;
23 options
= isl_calloc_type(ctx
, isl_ast_print_options
);
34 __isl_give isl_ast_print_options
*isl_ast_print_options_dup(
35 __isl_keep isl_ast_print_options
*options
)
38 isl_ast_print_options
*dup
;
43 ctx
= isl_ast_print_options_get_ctx(options
);
44 dup
= isl_ast_print_options_alloc(ctx
);
48 dup
->print_for
= options
->print_for
;
49 dup
->print_for_user
= options
->print_for_user
;
50 dup
->print_user
= options
->print_user
;
51 dup
->print_user_user
= options
->print_user_user
;
56 __isl_give isl_ast_print_options
*isl_ast_print_options_cow(
57 __isl_take isl_ast_print_options
*options
)
62 if (options
->ref
== 1)
65 return isl_ast_print_options_dup(options
);
68 __isl_give isl_ast_print_options
*isl_ast_print_options_copy(
69 __isl_keep isl_ast_print_options
*options
)
78 void *isl_ast_print_options_free(__isl_take isl_ast_print_options
*options
)
83 if (--options
->ref
> 0)
86 isl_ctx_deref(options
->ctx
);
92 /* Set the print_user callback of "options" to "print_user".
94 * If this callback is set, then it used to print user nodes in the AST.
95 * Otherwise, the expression associated to the user node is printed.
97 __isl_give isl_ast_print_options
*isl_ast_print_options_set_print_user(
98 __isl_take isl_ast_print_options
*options
,
99 __isl_give isl_printer
*(*print_user
)(__isl_take isl_printer
*p
,
100 __isl_take isl_ast_print_options
*options
,
101 __isl_keep isl_ast_node
*node
, void *user
),
104 options
= isl_ast_print_options_cow(options
);
108 options
->print_user
= print_user
;
109 options
->print_user_user
= user
;
114 /* Set the print_for callback of "options" to "print_for".
116 * If this callback is set, then it used to print for nodes in the AST.
118 __isl_give isl_ast_print_options
*isl_ast_print_options_set_print_for(
119 __isl_take isl_ast_print_options
*options
,
120 __isl_give isl_printer
*(*print_for
)(__isl_take isl_printer
*p
,
121 __isl_take isl_ast_print_options
*options
,
122 __isl_keep isl_ast_node
*node
, void *user
),
125 options
= isl_ast_print_options_cow(options
);
129 options
->print_for
= print_for
;
130 options
->print_for_user
= user
;
135 __isl_give isl_ast_expr
*isl_ast_expr_copy(__isl_keep isl_ast_expr
*expr
)
144 __isl_give isl_ast_expr
*isl_ast_expr_dup(__isl_keep isl_ast_expr
*expr
)
153 ctx
= isl_ast_expr_get_ctx(expr
);
154 switch (expr
->type
) {
155 case isl_ast_expr_int
:
156 dup
= isl_ast_expr_from_val(isl_val_copy(expr
->u
.v
));
158 case isl_ast_expr_id
:
159 dup
= isl_ast_expr_from_id(isl_id_copy(expr
->u
.id
));
161 case isl_ast_expr_op
:
162 dup
= isl_ast_expr_alloc_op(ctx
,
163 expr
->u
.op
.op
, expr
->u
.op
.n_arg
);
166 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
)
168 isl_ast_expr_copy(expr
->u
.op
.args
[i
]);
170 case isl_ast_expr_error
:
180 __isl_give isl_ast_expr
*isl_ast_expr_cow(__isl_take isl_ast_expr
*expr
)
188 return isl_ast_expr_dup(expr
);
191 void *isl_ast_expr_free(__isl_take isl_ast_expr
*expr
)
201 isl_ctx_deref(expr
->ctx
);
203 switch (expr
->type
) {
204 case isl_ast_expr_int
:
205 isl_val_free(expr
->u
.v
);
207 case isl_ast_expr_id
:
208 isl_id_free(expr
->u
.id
);
210 case isl_ast_expr_op
:
211 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
)
212 isl_ast_expr_free(expr
->u
.op
.args
[i
]);
213 free(expr
->u
.op
.args
);
215 case isl_ast_expr_error
:
223 isl_ctx
*isl_ast_expr_get_ctx(__isl_keep isl_ast_expr
*expr
)
225 return expr
? expr
->ctx
: NULL
;
228 enum isl_ast_expr_type
isl_ast_expr_get_type(__isl_keep isl_ast_expr
*expr
)
230 return expr
? expr
->type
: isl_ast_expr_error
;
233 /* Return the integer value represented by "expr".
235 __isl_give isl_val
*isl_ast_expr_get_val(__isl_keep isl_ast_expr
*expr
)
239 if (expr
->type
!= isl_ast_expr_int
)
240 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
241 "expression not an int", return NULL
);
242 return isl_val_copy(expr
->u
.v
);
245 __isl_give isl_id
*isl_ast_expr_get_id(__isl_keep isl_ast_expr
*expr
)
249 if (expr
->type
!= isl_ast_expr_id
)
250 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
251 "expression not an identifier", return NULL
);
253 return isl_id_copy(expr
->u
.id
);
256 enum isl_ast_op_type
isl_ast_expr_get_op_type(__isl_keep isl_ast_expr
*expr
)
259 return isl_ast_op_error
;
260 if (expr
->type
!= isl_ast_expr_op
)
261 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
262 "expression not an operation", return isl_ast_op_error
);
263 return expr
->u
.op
.op
;
266 int isl_ast_expr_get_op_n_arg(__isl_keep isl_ast_expr
*expr
)
270 if (expr
->type
!= isl_ast_expr_op
)
271 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
272 "expression not an operation", return -1);
273 return expr
->u
.op
.n_arg
;
276 __isl_give isl_ast_expr
*isl_ast_expr_get_op_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 NULL
);
284 if (pos
< 0 || pos
>= expr
->u
.op
.n_arg
)
285 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
286 "index out of bounds", return NULL
);
288 return isl_ast_expr_copy(expr
->u
.op
.args
[pos
]);
291 /* Replace the argument at position "pos" of "expr" by "arg".
293 __isl_give isl_ast_expr
*isl_ast_expr_set_op_arg(__isl_take isl_ast_expr
*expr
,
294 int pos
, __isl_take isl_ast_expr
*arg
)
296 expr
= isl_ast_expr_cow(expr
);
299 if (expr
->type
!= isl_ast_expr_op
)
300 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
301 "expression not an operation", goto error
);
302 if (pos
< 0 || pos
>= expr
->u
.op
.n_arg
)
303 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
304 "index out of bounds", goto error
);
306 isl_ast_expr_free(expr
->u
.op
.args
[pos
]);
307 expr
->u
.op
.args
[pos
] = arg
;
311 isl_ast_expr_free(arg
);
312 return isl_ast_expr_free(expr
);
315 /* Create a new operation expression of operation type "op",
316 * with "n_arg" as yet unspecified arguments.
318 __isl_give isl_ast_expr
*isl_ast_expr_alloc_op(isl_ctx
*ctx
,
319 enum isl_ast_op_type op
, int n_arg
)
323 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
330 expr
->type
= isl_ast_expr_op
;
332 expr
->u
.op
.n_arg
= n_arg
;
333 expr
->u
.op
.args
= isl_calloc_array(ctx
, isl_ast_expr
*, n_arg
);
335 if (n_arg
&& !expr
->u
.op
.args
)
336 return isl_ast_expr_free(expr
);
341 /* Create a new id expression representing "id".
343 __isl_give isl_ast_expr
*isl_ast_expr_from_id(__isl_take isl_id
*id
)
351 ctx
= isl_id_get_ctx(id
);
352 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
354 return isl_id_free(id
);
359 expr
->type
= isl_ast_expr_id
;
365 /* Create a new integer expression representing "i".
367 __isl_give isl_ast_expr
*isl_ast_expr_alloc_int_si(isl_ctx
*ctx
, int i
)
371 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
378 expr
->type
= isl_ast_expr_int
;
379 expr
->u
.v
= isl_val_int_from_si(ctx
, i
);
381 return isl_ast_expr_free(expr
);
386 /* Create a new integer expression representing "v".
388 __isl_give isl_ast_expr
*isl_ast_expr_from_val(__isl_take isl_val
*v
)
395 if (!isl_val_is_int(v
))
396 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
397 "expecting integer value", return isl_val_free(v
));
399 ctx
= isl_val_get_ctx(v
);
400 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
402 return isl_val_free(v
);
407 expr
->type
= isl_ast_expr_int
;
413 /* Create an expression representing the negation of "arg".
415 __isl_give isl_ast_expr
*isl_ast_expr_neg(__isl_take isl_ast_expr
*arg
)
418 isl_ast_expr
*expr
= NULL
;
423 ctx
= isl_ast_expr_get_ctx(arg
);
424 expr
= isl_ast_expr_alloc_op(ctx
, isl_ast_op_minus
, 1);
428 expr
->u
.op
.args
[0] = arg
;
432 isl_ast_expr_free(arg
);
436 /* Create an expression representing the binary operation "type"
437 * applied to "expr1" and "expr2".
439 __isl_give isl_ast_expr
*isl_ast_expr_alloc_binary(enum isl_ast_op_type type
,
440 __isl_take isl_ast_expr
*expr1
, __isl_take isl_ast_expr
*expr2
)
443 isl_ast_expr
*expr
= NULL
;
445 if (!expr1
|| !expr2
)
448 ctx
= isl_ast_expr_get_ctx(expr1
);
449 expr
= isl_ast_expr_alloc_op(ctx
, type
, 2);
453 expr
->u
.op
.args
[0] = expr1
;
454 expr
->u
.op
.args
[1] = expr2
;
458 isl_ast_expr_free(expr1
);
459 isl_ast_expr_free(expr2
);
463 /* Create an expression representing the sum of "expr1" and "expr2".
465 __isl_give isl_ast_expr
*isl_ast_expr_add(__isl_take isl_ast_expr
*expr1
,
466 __isl_take isl_ast_expr
*expr2
)
468 return isl_ast_expr_alloc_binary(isl_ast_op_add
, expr1
, expr2
);
471 /* Create an expression representing the difference of "expr1" and "expr2".
473 __isl_give isl_ast_expr
*isl_ast_expr_sub(__isl_take isl_ast_expr
*expr1
,
474 __isl_take isl_ast_expr
*expr2
)
476 return isl_ast_expr_alloc_binary(isl_ast_op_sub
, expr1
, expr2
);
479 /* Create an expression representing the product of "expr1" and "expr2".
481 __isl_give isl_ast_expr
*isl_ast_expr_mul(__isl_take isl_ast_expr
*expr1
,
482 __isl_take isl_ast_expr
*expr2
)
484 return isl_ast_expr_alloc_binary(isl_ast_op_mul
, expr1
, expr2
);
487 /* Create an expression representing the quotient of "expr1" and "expr2".
489 __isl_give isl_ast_expr
*isl_ast_expr_div(__isl_take isl_ast_expr
*expr1
,
490 __isl_take isl_ast_expr
*expr2
)
492 return isl_ast_expr_alloc_binary(isl_ast_op_div
, expr1
, expr2
);
495 /* Create an expression representing the conjunction of "expr1" and "expr2".
497 __isl_give isl_ast_expr
*isl_ast_expr_and(__isl_take isl_ast_expr
*expr1
,
498 __isl_take isl_ast_expr
*expr2
)
500 return isl_ast_expr_alloc_binary(isl_ast_op_and
, expr1
, expr2
);
503 /* Create an expression representing the disjunction of "expr1" and "expr2".
505 __isl_give isl_ast_expr
*isl_ast_expr_or(__isl_take isl_ast_expr
*expr1
,
506 __isl_take isl_ast_expr
*expr2
)
508 return isl_ast_expr_alloc_binary(isl_ast_op_or
, expr1
, expr2
);
511 isl_ctx
*isl_ast_node_get_ctx(__isl_keep isl_ast_node
*node
)
513 return node
? node
->ctx
: NULL
;
516 enum isl_ast_node_type
isl_ast_node_get_type(__isl_keep isl_ast_node
*node
)
518 return node
? node
->type
: isl_ast_node_error
;
521 __isl_give isl_ast_node
*isl_ast_node_alloc(isl_ctx
*ctx
,
522 enum isl_ast_node_type type
)
526 node
= isl_calloc_type(ctx
, isl_ast_node
);
538 /* Create an if node with the given guard.
540 * The then body needs to be filled in later.
542 __isl_give isl_ast_node
*isl_ast_node_alloc_if(__isl_take isl_ast_expr
*guard
)
549 node
= isl_ast_node_alloc(isl_ast_expr_get_ctx(guard
), isl_ast_node_if
);
552 node
->u
.i
.guard
= guard
;
556 isl_ast_expr_free(guard
);
560 /* Create a for node with the given iterator.
562 * The remaining fields need to be filled in later.
564 __isl_give isl_ast_node
*isl_ast_node_alloc_for(__isl_take isl_id
*id
)
572 ctx
= isl_id_get_ctx(id
);
573 node
= isl_ast_node_alloc(ctx
, isl_ast_node_for
);
577 node
->u
.f
.iterator
= isl_ast_expr_from_id(id
);
578 if (!node
->u
.f
.iterator
)
579 return isl_ast_node_free(node
);
584 /* Create a user node evaluating "expr".
586 __isl_give isl_ast_node
*isl_ast_node_alloc_user(__isl_take isl_ast_expr
*expr
)
594 ctx
= isl_ast_expr_get_ctx(expr
);
595 node
= isl_ast_node_alloc(ctx
, isl_ast_node_user
);
599 node
->u
.e
.expr
= expr
;
603 isl_ast_expr_free(expr
);
607 /* Create a block node with the given children.
609 __isl_give isl_ast_node
*isl_ast_node_alloc_block(
610 __isl_take isl_ast_node_list
*list
)
618 ctx
= isl_ast_node_list_get_ctx(list
);
619 node
= isl_ast_node_alloc(ctx
, isl_ast_node_block
);
623 node
->u
.b
.children
= list
;
627 isl_ast_node_list_free(list
);
631 /* Represent the given list of nodes as a single node, either by
632 * extract the node from a single element list or by creating
633 * a block node with the list of nodes as children.
635 __isl_give isl_ast_node
*isl_ast_node_from_ast_node_list(
636 __isl_take isl_ast_node_list
*list
)
640 if (isl_ast_node_list_n_ast_node(list
) != 1)
641 return isl_ast_node_alloc_block(list
);
643 node
= isl_ast_node_list_get_ast_node(list
, 0);
644 isl_ast_node_list_free(list
);
649 __isl_give isl_ast_node
*isl_ast_node_copy(__isl_keep isl_ast_node
*node
)
658 __isl_give isl_ast_node
*isl_ast_node_dup(__isl_keep isl_ast_node
*node
)
665 dup
= isl_ast_node_alloc(isl_ast_node_get_ctx(node
), node
->type
);
669 switch (node
->type
) {
670 case isl_ast_node_if
:
671 dup
->u
.i
.guard
= isl_ast_expr_copy(node
->u
.i
.guard
);
672 dup
->u
.i
.then
= isl_ast_node_copy(node
->u
.i
.then
);
673 dup
->u
.i
.else_node
= isl_ast_node_copy(node
->u
.i
.else_node
);
674 if (!dup
->u
.i
.guard
|| !dup
->u
.i
.then
||
675 (node
->u
.i
.else_node
&& !dup
->u
.i
.else_node
))
676 return isl_ast_node_free(dup
);
678 case isl_ast_node_for
:
679 dup
->u
.f
.iterator
= isl_ast_expr_copy(node
->u
.f
.iterator
);
680 dup
->u
.f
.init
= isl_ast_expr_copy(node
->u
.f
.init
);
681 dup
->u
.f
.cond
= isl_ast_expr_copy(node
->u
.f
.cond
);
682 dup
->u
.f
.inc
= isl_ast_expr_copy(node
->u
.f
.inc
);
683 dup
->u
.f
.body
= isl_ast_node_copy(node
->u
.f
.body
);
684 if (!dup
->u
.f
.iterator
|| !dup
->u
.f
.init
|| !dup
->u
.f
.cond
||
685 !dup
->u
.f
.inc
|| !dup
->u
.f
.body
)
686 return isl_ast_node_free(dup
);
688 case isl_ast_node_block
:
689 dup
->u
.b
.children
= isl_ast_node_list_copy(node
->u
.b
.children
);
690 if (!dup
->u
.b
.children
)
691 return isl_ast_node_free(dup
);
693 case isl_ast_node_user
:
694 dup
->u
.e
.expr
= isl_ast_expr_copy(node
->u
.e
.expr
);
696 return isl_ast_node_free(dup
);
698 case isl_ast_node_error
:
705 __isl_give isl_ast_node
*isl_ast_node_cow(__isl_take isl_ast_node
*node
)
713 return isl_ast_node_dup(node
);
716 void *isl_ast_node_free(__isl_take isl_ast_node
*node
)
724 switch (node
->type
) {
725 case isl_ast_node_if
:
726 isl_ast_expr_free(node
->u
.i
.guard
);
727 isl_ast_node_free(node
->u
.i
.then
);
728 isl_ast_node_free(node
->u
.i
.else_node
);
730 case isl_ast_node_for
:
731 isl_ast_expr_free(node
->u
.f
.iterator
);
732 isl_ast_expr_free(node
->u
.f
.init
);
733 isl_ast_expr_free(node
->u
.f
.cond
);
734 isl_ast_expr_free(node
->u
.f
.inc
);
735 isl_ast_node_free(node
->u
.f
.body
);
737 case isl_ast_node_block
:
738 isl_ast_node_list_free(node
->u
.b
.children
);
740 case isl_ast_node_user
:
741 isl_ast_expr_free(node
->u
.e
.expr
);
743 case isl_ast_node_error
:
747 isl_id_free(node
->annotation
);
748 isl_ctx_deref(node
->ctx
);
754 /* Replace the body of the for node "node" by "body".
756 __isl_give isl_ast_node
*isl_ast_node_for_set_body(
757 __isl_take isl_ast_node
*node
, __isl_take isl_ast_node
*body
)
759 node
= isl_ast_node_cow(node
);
762 if (node
->type
!= isl_ast_node_for
)
763 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
764 "not a for node", goto error
);
766 isl_ast_node_free(node
->u
.f
.body
);
767 node
->u
.f
.body
= body
;
771 isl_ast_node_free(node
);
772 isl_ast_node_free(body
);
776 __isl_give isl_ast_node
*isl_ast_node_for_get_body(
777 __isl_keep isl_ast_node
*node
)
781 if (node
->type
!= isl_ast_node_for
)
782 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
783 "not a for node", return NULL
);
784 return isl_ast_node_copy(node
->u
.f
.body
);
787 /* Mark the given for node as being degenerate.
789 __isl_give isl_ast_node
*isl_ast_node_for_mark_degenerate(
790 __isl_take isl_ast_node
*node
)
792 node
= isl_ast_node_cow(node
);
795 node
->u
.f
.degenerate
= 1;
799 int isl_ast_node_for_is_degenerate(__isl_keep isl_ast_node
*node
)
803 if (node
->type
!= isl_ast_node_for
)
804 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
805 "not a for node", return -1);
806 return node
->u
.f
.degenerate
;
809 __isl_give isl_ast_expr
*isl_ast_node_for_get_iterator(
810 __isl_keep isl_ast_node
*node
)
814 if (node
->type
!= isl_ast_node_for
)
815 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
816 "not a for node", return NULL
);
817 return isl_ast_expr_copy(node
->u
.f
.iterator
);
820 __isl_give isl_ast_expr
*isl_ast_node_for_get_init(
821 __isl_keep isl_ast_node
*node
)
825 if (node
->type
!= isl_ast_node_for
)
826 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
827 "not a for node", return NULL
);
828 return isl_ast_expr_copy(node
->u
.f
.init
);
831 /* Return the condition expression of the given for node.
833 * If the for node is degenerate, then the condition is not explicitly
834 * stored in the node. Instead, it is constructed as
838 __isl_give isl_ast_expr
*isl_ast_node_for_get_cond(
839 __isl_keep isl_ast_node
*node
)
843 if (node
->type
!= isl_ast_node_for
)
844 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
845 "not a for node", return NULL
);
846 if (!node
->u
.f
.degenerate
)
847 return isl_ast_expr_copy(node
->u
.f
.cond
);
849 return isl_ast_expr_alloc_binary(isl_ast_op_le
,
850 isl_ast_expr_copy(node
->u
.f
.iterator
),
851 isl_ast_expr_copy(node
->u
.f
.init
));
854 /* Return the increment of the given for node.
856 * If the for node is degenerate, then the increment is not explicitly
857 * stored in the node. We simply return "1".
859 __isl_give isl_ast_expr
*isl_ast_node_for_get_inc(
860 __isl_keep isl_ast_node
*node
)
864 if (node
->type
!= isl_ast_node_for
)
865 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
866 "not a for node", return NULL
);
867 if (!node
->u
.f
.degenerate
)
868 return isl_ast_expr_copy(node
->u
.f
.inc
);
869 return isl_ast_expr_alloc_int_si(isl_ast_node_get_ctx(node
), 1);
872 /* Replace the then branch of the if node "node" by "child".
874 __isl_give isl_ast_node
*isl_ast_node_if_set_then(
875 __isl_take isl_ast_node
*node
, __isl_take isl_ast_node
*child
)
877 node
= isl_ast_node_cow(node
);
880 if (node
->type
!= isl_ast_node_if
)
881 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
882 "not an if node", goto error
);
884 isl_ast_node_free(node
->u
.i
.then
);
885 node
->u
.i
.then
= child
;
889 isl_ast_node_free(node
);
890 isl_ast_node_free(child
);
894 __isl_give isl_ast_node
*isl_ast_node_if_get_then(
895 __isl_keep isl_ast_node
*node
)
899 if (node
->type
!= isl_ast_node_if
)
900 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
901 "not an if node", return NULL
);
902 return isl_ast_node_copy(node
->u
.i
.then
);
905 int isl_ast_node_if_has_else(
906 __isl_keep isl_ast_node
*node
)
910 if (node
->type
!= isl_ast_node_if
)
911 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
912 "not an if node", return -1);
913 return node
->u
.i
.else_node
!= NULL
;
916 __isl_give isl_ast_node
*isl_ast_node_if_get_else(
917 __isl_keep isl_ast_node
*node
)
921 if (node
->type
!= isl_ast_node_if
)
922 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
923 "not an if node", return NULL
);
924 return isl_ast_node_copy(node
->u
.i
.else_node
);
927 __isl_give isl_ast_expr
*isl_ast_node_if_get_cond(
928 __isl_keep isl_ast_node
*node
)
932 if (node
->type
!= isl_ast_node_if
)
933 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
934 "not a guard node", return NULL
);
935 return isl_ast_expr_copy(node
->u
.i
.guard
);
938 __isl_give isl_ast_node_list
*isl_ast_node_block_get_children(
939 __isl_keep isl_ast_node
*node
)
943 if (node
->type
!= isl_ast_node_block
)
944 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
945 "not a block node", return NULL
);
946 return isl_ast_node_list_copy(node
->u
.b
.children
);
949 __isl_give isl_ast_expr
*isl_ast_node_user_get_expr(
950 __isl_keep isl_ast_node
*node
)
955 return isl_ast_expr_copy(node
->u
.e
.expr
);
958 __isl_give isl_id
*isl_ast_node_get_annotation(__isl_keep isl_ast_node
*node
)
960 return node
? isl_id_copy(node
->annotation
) : NULL
;
963 /* Replace node->annotation by "annotation".
965 __isl_give isl_ast_node
*isl_ast_node_set_annotation(
966 __isl_take isl_ast_node
*node
, __isl_take isl_id
*annotation
)
968 node
= isl_ast_node_cow(node
);
969 if (!node
|| !annotation
)
972 isl_id_free(node
->annotation
);
973 node
->annotation
= annotation
;
977 isl_id_free(annotation
);
978 return isl_ast_node_free(node
);
981 /* Textual C representation of the various operators.
983 static char *op_str
[] = {
984 [isl_ast_op_and
] = "&&",
985 [isl_ast_op_and_then
] = "&&",
986 [isl_ast_op_or
] = "||",
987 [isl_ast_op_or_else
] = "||",
988 [isl_ast_op_max
] = "max",
989 [isl_ast_op_min
] = "min",
990 [isl_ast_op_minus
] = "-",
991 [isl_ast_op_add
] = "+",
992 [isl_ast_op_sub
] = "-",
993 [isl_ast_op_mul
] = "*",
994 [isl_ast_op_pdiv_q
] = "/",
995 [isl_ast_op_pdiv_r
] = "%",
996 [isl_ast_op_div
] = "/",
997 [isl_ast_op_eq
] = "==",
998 [isl_ast_op_le
] = "<=",
999 [isl_ast_op_ge
] = ">=",
1000 [isl_ast_op_lt
] = "<",
1001 [isl_ast_op_gt
] = ">"
1004 /* Precedence in C of the various operators.
1005 * Based on http://en.wikipedia.org/wiki/Operators_in_C_and_C++
1006 * Lowest value means highest precedence.
1008 static int op_prec
[] = {
1009 [isl_ast_op_and
] = 13,
1010 [isl_ast_op_and_then
] = 13,
1011 [isl_ast_op_or
] = 14,
1012 [isl_ast_op_or_else
] = 14,
1013 [isl_ast_op_max
] = 2,
1014 [isl_ast_op_min
] = 2,
1015 [isl_ast_op_minus
] = 3,
1016 [isl_ast_op_add
] = 6,
1017 [isl_ast_op_sub
] = 6,
1018 [isl_ast_op_mul
] = 5,
1019 [isl_ast_op_div
] = 5,
1020 [isl_ast_op_fdiv_q
] = 2,
1021 [isl_ast_op_pdiv_q
] = 5,
1022 [isl_ast_op_pdiv_r
] = 5,
1023 [isl_ast_op_cond
] = 15,
1024 [isl_ast_op_select
] = 15,
1025 [isl_ast_op_eq
] = 9,
1026 [isl_ast_op_le
] = 8,
1027 [isl_ast_op_ge
] = 8,
1028 [isl_ast_op_lt
] = 8,
1029 [isl_ast_op_gt
] = 8,
1030 [isl_ast_op_call
] = 2
1033 /* Is the operator left-to-right associative?
1035 static int op_left
[] = {
1036 [isl_ast_op_and
] = 1,
1037 [isl_ast_op_and_then
] = 1,
1038 [isl_ast_op_or
] = 1,
1039 [isl_ast_op_or_else
] = 1,
1040 [isl_ast_op_max
] = 1,
1041 [isl_ast_op_min
] = 1,
1042 [isl_ast_op_minus
] = 0,
1043 [isl_ast_op_add
] = 1,
1044 [isl_ast_op_sub
] = 1,
1045 [isl_ast_op_mul
] = 1,
1046 [isl_ast_op_div
] = 1,
1047 [isl_ast_op_fdiv_q
] = 1,
1048 [isl_ast_op_pdiv_q
] = 1,
1049 [isl_ast_op_pdiv_r
] = 1,
1050 [isl_ast_op_cond
] = 0,
1051 [isl_ast_op_select
] = 0,
1052 [isl_ast_op_eq
] = 1,
1053 [isl_ast_op_le
] = 1,
1054 [isl_ast_op_ge
] = 1,
1055 [isl_ast_op_lt
] = 1,
1056 [isl_ast_op_gt
] = 1,
1057 [isl_ast_op_call
] = 1
1060 static int is_and(enum isl_ast_op_type op
)
1062 return op
== isl_ast_op_and
|| op
== isl_ast_op_and_then
;
1065 static int is_or(enum isl_ast_op_type op
)
1067 return op
== isl_ast_op_or
|| op
== isl_ast_op_or_else
;
1070 static int is_add_sub(enum isl_ast_op_type op
)
1072 return op
== isl_ast_op_add
|| op
== isl_ast_op_sub
;
1075 static int is_div_mod(enum isl_ast_op_type op
)
1077 return op
== isl_ast_op_div
|| op
== isl_ast_op_pdiv_r
;
1080 /* Do we need/want parentheses around "expr" as a subexpression of
1081 * an "op" operation? If "left" is set, then "expr" is the left-most
1084 * We only need parentheses if "expr" represents an operation.
1086 * If op has a higher precedence than expr->u.op.op, then we need
1088 * If op and expr->u.op.op have the same precedence, but the operations
1089 * are performed in an order that is different from the associativity,
1090 * then we need parentheses.
1092 * An and inside an or technically does not require parentheses,
1093 * but some compilers complain about that, so we add them anyway.
1095 * Computations such as "a / b * c" and "a % b + c" can be somewhat
1096 * difficult to read, so we add parentheses for those as well.
1098 static int sub_expr_need_parens(enum isl_ast_op_type op
,
1099 __isl_keep isl_ast_expr
*expr
, int left
)
1101 if (expr
->type
!= isl_ast_expr_op
)
1104 if (op_prec
[expr
->u
.op
.op
] > op_prec
[op
])
1106 if (op_prec
[expr
->u
.op
.op
] == op_prec
[op
] && left
!= op_left
[op
])
1109 if (is_or(op
) && is_and(expr
->u
.op
.op
))
1111 if (op
== isl_ast_op_mul
&& expr
->u
.op
.op
!= isl_ast_op_mul
&&
1112 op_prec
[expr
->u
.op
.op
] == op_prec
[op
])
1114 if (is_add_sub(op
) && is_div_mod(expr
->u
.op
.op
))
1120 /* Print "expr" as a subexpression of an "op" operation.
1121 * If "left" is set, then "expr" is the left-most operand.
1123 static __isl_give isl_printer
*print_sub_expr(__isl_take isl_printer
*p
,
1124 enum isl_ast_op_type op
, __isl_keep isl_ast_expr
*expr
, int left
)
1128 need_parens
= sub_expr_need_parens(op
, expr
, left
);
1131 p
= isl_printer_print_str(p
, "(");
1132 p
= isl_printer_print_ast_expr(p
, expr
);
1134 p
= isl_printer_print_str(p
, ")");
1138 /* Print a min or max reduction "expr".
1140 static __isl_give isl_printer
*print_min_max(__isl_take isl_printer
*p
,
1141 __isl_keep isl_ast_expr
*expr
)
1145 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1146 p
= isl_printer_print_str(p
, op_str
[expr
->u
.op
.op
]);
1147 p
= isl_printer_print_str(p
, "(");
1149 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1150 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1151 p
= isl_printer_print_str(p
, ", ");
1152 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[i
]);
1153 p
= isl_printer_print_str(p
, ")");
1159 /* Print a function call "expr".
1161 * The first argument represents the function to be called.
1163 static __isl_give isl_printer
*print_call(__isl_take isl_printer
*p
,
1164 __isl_keep isl_ast_expr
*expr
)
1168 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1169 p
= isl_printer_print_str(p
, "(");
1170 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1172 p
= isl_printer_print_str(p
, ", ");
1173 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[i
]);
1175 p
= isl_printer_print_str(p
, ")");
1180 /* Print "expr" to "p".
1182 * If we are printing in isl format, then we also print an indication
1183 * of the size of the expression (if it was computed).
1185 __isl_give isl_printer
*isl_printer_print_ast_expr(__isl_take isl_printer
*p
,
1186 __isl_keep isl_ast_expr
*expr
)
1191 return isl_printer_free(p
);
1193 switch (expr
->type
) {
1194 case isl_ast_expr_op
:
1195 if (expr
->u
.op
.op
== isl_ast_op_call
) {
1196 p
= print_call(p
, expr
);
1199 if (expr
->u
.op
.n_arg
== 1) {
1200 p
= isl_printer_print_str(p
, op_str
[expr
->u
.op
.op
]);
1201 p
= print_sub_expr(p
, expr
->u
.op
.op
,
1202 expr
->u
.op
.args
[0], 0);
1205 if (expr
->u
.op
.op
== isl_ast_op_fdiv_q
) {
1206 p
= isl_printer_print_str(p
, "floord(");
1207 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1208 p
= isl_printer_print_str(p
, ", ");
1209 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[1]);
1210 p
= isl_printer_print_str(p
, ")");
1213 if (expr
->u
.op
.op
== isl_ast_op_max
||
1214 expr
->u
.op
.op
== isl_ast_op_min
) {
1215 p
= print_min_max(p
, expr
);
1218 if (expr
->u
.op
.op
== isl_ast_op_cond
||
1219 expr
->u
.op
.op
== isl_ast_op_select
) {
1220 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1221 p
= isl_printer_print_str(p
, " ? ");
1222 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[1]);
1223 p
= isl_printer_print_str(p
, " : ");
1224 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[2]);
1227 if (expr
->u
.op
.n_arg
!= 2)
1228 isl_die(isl_printer_get_ctx(p
), isl_error_internal
,
1229 "operation should have two arguments",
1231 p
= print_sub_expr(p
, expr
->u
.op
.op
, expr
->u
.op
.args
[0], 1);
1232 p
= isl_printer_print_str(p
, " ");
1233 p
= isl_printer_print_str(p
, op_str
[expr
->u
.op
.op
]);
1234 p
= isl_printer_print_str(p
, " ");
1235 p
= print_sub_expr(p
, expr
->u
.op
.op
, expr
->u
.op
.args
[1], 0);
1237 case isl_ast_expr_id
:
1238 p
= isl_printer_print_str(p
, isl_id_get_name(expr
->u
.id
));
1240 case isl_ast_expr_int
:
1241 p
= isl_printer_print_val(p
, expr
->u
.v
);
1243 case isl_ast_expr_error
:
1249 isl_printer_free(p
);
1253 /* Print "node" to "p" in "isl format".
1255 static __isl_give isl_printer
*print_ast_node_isl(__isl_take isl_printer
*p
,
1256 __isl_keep isl_ast_node
*node
)
1258 p
= isl_printer_print_str(p
, "(");
1259 switch (node
->type
) {
1260 case isl_ast_node_for
:
1261 if (node
->u
.f
.degenerate
) {
1262 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1264 p
= isl_printer_print_str(p
, "init: ");
1265 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1266 p
= isl_printer_print_str(p
, ", ");
1267 p
= isl_printer_print_str(p
, "cond: ");
1268 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.cond
);
1269 p
= isl_printer_print_str(p
, ", ");
1270 p
= isl_printer_print_str(p
, "inc: ");
1271 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.inc
);
1273 if (node
->u
.f
.body
) {
1274 p
= isl_printer_print_str(p
, ", ");
1275 p
= isl_printer_print_str(p
, "body: ");
1276 p
= isl_printer_print_ast_node(p
, node
->u
.f
.body
);
1279 case isl_ast_node_user
:
1280 p
= isl_printer_print_ast_expr(p
, node
->u
.e
.expr
);
1282 case isl_ast_node_if
:
1283 p
= isl_printer_print_str(p
, "guard: ");
1284 p
= isl_printer_print_ast_expr(p
, node
->u
.i
.guard
);
1285 if (node
->u
.i
.then
) {
1286 p
= isl_printer_print_str(p
, ", ");
1287 p
= isl_printer_print_str(p
, "then: ");
1288 p
= isl_printer_print_ast_node(p
, node
->u
.i
.then
);
1290 if (node
->u
.i
.else_node
) {
1291 p
= isl_printer_print_str(p
, ", ");
1292 p
= isl_printer_print_str(p
, "else: ");
1293 p
= isl_printer_print_ast_node(p
, node
->u
.i
.else_node
);
1296 case isl_ast_node_block
:
1297 p
= isl_printer_print_ast_node_list(p
, node
->u
.b
.children
);
1302 p
= isl_printer_print_str(p
, ")");
1306 /* Do we need to print a block around the body "node" of a for or if node?
1308 * If the node is a block, then we need to print a block.
1309 * Also if the node is a degenerate for then we will print it as
1310 * an assignment followed by the body of the for loop, so we need a block
1313 static int need_block(__isl_keep isl_ast_node
*node
)
1315 if (node
->type
== isl_ast_node_block
)
1317 if (node
->type
== isl_ast_node_for
&& node
->u
.f
.degenerate
)
1322 static __isl_give isl_printer
*print_ast_node_c(__isl_take isl_printer
*p
,
1323 __isl_keep isl_ast_node
*node
,
1324 __isl_keep isl_ast_print_options
*options
, int in_block
, int in_list
);
1325 static __isl_give isl_printer
*print_if_c(__isl_take isl_printer
*p
,
1326 __isl_keep isl_ast_node
*node
,
1327 __isl_keep isl_ast_print_options
*options
, int new_line
);
1329 /* Print the body "node" of a for or if node.
1330 * If "else_node" is set, then it is printed as well.
1332 * We first check if we need to print out a block.
1333 * We always print out a block if there is an else node to make
1334 * sure that the else node is matched to the correct if node.
1336 * If the else node is itself an if, then we print it as
1340 * Otherwise the else node is printed as
1345 static __isl_give isl_printer
*print_body_c(__isl_take isl_printer
*p
,
1346 __isl_keep isl_ast_node
*node
, __isl_keep isl_ast_node
*else_node
,
1347 __isl_keep isl_ast_print_options
*options
)
1350 return isl_printer_free(p
);
1352 if (!else_node
&& !need_block(node
)) {
1353 p
= isl_printer_end_line(p
);
1354 p
= isl_printer_indent(p
, 2);
1355 p
= isl_ast_node_print(node
, p
,
1356 isl_ast_print_options_copy(options
));
1357 p
= isl_printer_indent(p
, -2);
1361 p
= isl_printer_print_str(p
, " {");
1362 p
= isl_printer_end_line(p
);
1363 p
= isl_printer_indent(p
, 2);
1364 p
= print_ast_node_c(p
, node
, options
, 1, 0);
1365 p
= isl_printer_indent(p
, -2);
1366 p
= isl_printer_start_line(p
);
1367 p
= isl_printer_print_str(p
, "}");
1369 if (else_node
->type
== isl_ast_node_if
) {
1370 p
= isl_printer_print_str(p
, " else ");
1371 p
= print_if_c(p
, else_node
, options
, 0);
1373 p
= isl_printer_print_str(p
, " else");
1374 p
= print_body_c(p
, else_node
, NULL
, options
);
1377 p
= isl_printer_end_line(p
);
1382 /* Print the start of a compound statement.
1384 static __isl_give isl_printer
*start_block(__isl_take isl_printer
*p
)
1386 p
= isl_printer_start_line(p
);
1387 p
= isl_printer_print_str(p
, "{");
1388 p
= isl_printer_end_line(p
);
1389 p
= isl_printer_indent(p
, 2);
1394 /* Print the end of a compound statement.
1396 static __isl_give isl_printer
*end_block(__isl_take isl_printer
*p
)
1398 p
= isl_printer_indent(p
, -2);
1399 p
= isl_printer_start_line(p
);
1400 p
= isl_printer_print_str(p
, "}");
1401 p
= isl_printer_end_line(p
);
1406 /* Print the for node "node".
1408 * If the for node is degenerate, it is printed as
1410 * type iterator = init;
1413 * Otherwise, it is printed as
1415 * for (type iterator = init; cond; iterator += inc)
1418 * "in_block" is set if we are currently inside a block.
1419 * "in_list" is set if the current node is not alone in the block.
1420 * If we are not in a block or if the current not is not alone in the block
1421 * then we print a block around a degenerate for loop such that the variable
1422 * declaration will not conflict with any potential other declaration
1423 * of the same variable.
1425 static __isl_give isl_printer
*print_for_c(__isl_take isl_printer
*p
,
1426 __isl_keep isl_ast_node
*node
,
1427 __isl_keep isl_ast_print_options
*options
, int in_block
, int in_list
)
1433 type
= isl_options_get_ast_iterator_type(isl_printer_get_ctx(p
));
1434 if (!node
->u
.f
.degenerate
) {
1435 id
= isl_ast_expr_get_id(node
->u
.f
.iterator
);
1436 name
= isl_id_get_name(id
);
1438 p
= isl_printer_start_line(p
);
1439 p
= isl_printer_print_str(p
, "for (");
1440 p
= isl_printer_print_str(p
, type
);
1441 p
= isl_printer_print_str(p
, " ");
1442 p
= isl_printer_print_str(p
, name
);
1443 p
= isl_printer_print_str(p
, " = ");
1444 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1445 p
= isl_printer_print_str(p
, "; ");
1446 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.cond
);
1447 p
= isl_printer_print_str(p
, "; ");
1448 p
= isl_printer_print_str(p
, name
);
1449 p
= isl_printer_print_str(p
, " += ");
1450 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.inc
);
1451 p
= isl_printer_print_str(p
, ")");
1452 p
= print_body_c(p
, node
->u
.f
.body
, NULL
, options
);
1454 id
= isl_ast_expr_get_id(node
->u
.f
.iterator
);
1455 name
= isl_id_get_name(id
);
1457 if (!in_block
|| in_list
)
1459 p
= isl_printer_start_line(p
);
1460 p
= isl_printer_print_str(p
, type
);
1461 p
= isl_printer_print_str(p
, " ");
1462 p
= isl_printer_print_str(p
, name
);
1463 p
= isl_printer_print_str(p
, " = ");
1464 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1465 p
= isl_printer_print_str(p
, ";");
1466 p
= isl_printer_end_line(p
);
1467 p
= print_ast_node_c(p
, node
->u
.f
.body
, options
, 1, 0);
1468 if (!in_block
|| in_list
)
1475 /* Print the if node "node".
1476 * If "new_line" is set then the if node should be printed on a new line.
1478 static __isl_give isl_printer
*print_if_c(__isl_take isl_printer
*p
,
1479 __isl_keep isl_ast_node
*node
,
1480 __isl_keep isl_ast_print_options
*options
, int new_line
)
1483 p
= isl_printer_start_line(p
);
1484 p
= isl_printer_print_str(p
, "if (");
1485 p
= isl_printer_print_ast_expr(p
, node
->u
.i
.guard
);
1486 p
= isl_printer_print_str(p
, ")");
1487 p
= print_body_c(p
, node
->u
.i
.then
, node
->u
.i
.else_node
, options
);
1492 /* Print the "node" to "p".
1494 * "in_block" is set if we are currently inside a block.
1495 * If so, we do not print a block around the children of a block node.
1496 * We do this to avoid an extra block around the body of a degenerate
1499 * "in_list" is set if the current node is not alone in the block.
1501 static __isl_give isl_printer
*print_ast_node_c(__isl_take isl_printer
*p
,
1502 __isl_keep isl_ast_node
*node
,
1503 __isl_keep isl_ast_print_options
*options
, int in_block
, int in_list
)
1505 switch (node
->type
) {
1506 case isl_ast_node_for
:
1507 if (options
->print_for
)
1508 return options
->print_for(p
,
1509 isl_ast_print_options_copy(options
),
1510 node
, options
->print_for_user
);
1511 p
= print_for_c(p
, node
, options
, in_block
, in_list
);
1513 case isl_ast_node_if
:
1514 p
= print_if_c(p
, node
, options
, 1);
1516 case isl_ast_node_block
:
1519 p
= isl_ast_node_list_print(node
->u
.b
.children
, p
, options
);
1523 case isl_ast_node_user
:
1524 if (options
->print_user
)
1525 return options
->print_user(p
,
1526 isl_ast_print_options_copy(options
),
1527 node
, options
->print_user_user
);
1528 p
= isl_printer_start_line(p
);
1529 p
= isl_printer_print_ast_expr(p
, node
->u
.e
.expr
);
1530 p
= isl_printer_print_str(p
, ";");
1531 p
= isl_printer_end_line(p
);
1533 case isl_ast_node_error
:
1539 /* Print the for node "node" to "p".
1541 __isl_give isl_printer
*isl_ast_node_for_print(__isl_keep isl_ast_node
*node
,
1542 __isl_take isl_printer
*p
, __isl_take isl_ast_print_options
*options
)
1544 if (!node
|| !options
)
1546 if (node
->type
!= isl_ast_node_for
)
1547 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1548 "not a for node", goto error
);
1549 p
= print_for_c(p
, node
, options
, 0, 0);
1550 isl_ast_print_options_free(options
);
1553 isl_ast_print_options_free(options
);
1554 isl_printer_free(p
);
1558 /* Print the if node "node" to "p".
1560 __isl_give isl_printer
*isl_ast_node_if_print(__isl_keep isl_ast_node
*node
,
1561 __isl_take isl_printer
*p
, __isl_take isl_ast_print_options
*options
)
1563 if (!node
|| !options
)
1565 if (node
->type
!= isl_ast_node_if
)
1566 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1567 "not an if node", goto error
);
1568 p
= print_if_c(p
, node
, options
, 1);
1569 isl_ast_print_options_free(options
);
1572 isl_ast_print_options_free(options
);
1573 isl_printer_free(p
);
1577 /* Print "node" to "p".
1579 __isl_give isl_printer
*isl_ast_node_print(__isl_keep isl_ast_node
*node
,
1580 __isl_take isl_printer
*p
, __isl_take isl_ast_print_options
*options
)
1582 if (!options
|| !node
)
1584 p
= print_ast_node_c(p
, node
, options
, 0, 0);
1585 isl_ast_print_options_free(options
);
1588 isl_ast_print_options_free(options
);
1589 isl_printer_free(p
);
1593 /* Print "node" to "p".
1595 __isl_give isl_printer
*isl_printer_print_ast_node(__isl_take isl_printer
*p
,
1596 __isl_keep isl_ast_node
*node
)
1599 isl_ast_print_options
*options
;
1604 format
= isl_printer_get_output_format(p
);
1606 case ISL_FORMAT_ISL
:
1607 p
= print_ast_node_isl(p
, node
);
1610 options
= isl_ast_print_options_alloc(isl_printer_get_ctx(p
));
1611 p
= isl_ast_node_print(node
, p
, options
);
1614 isl_die(isl_printer_get_ctx(p
), isl_error_unsupported
,
1615 "output format not supported for ast_node",
1616 return isl_printer_free(p
));
1622 /* Print the list of nodes "list" to "p".
1624 __isl_give isl_printer
*isl_ast_node_list_print(
1625 __isl_keep isl_ast_node_list
*list
, __isl_take isl_printer
*p
,
1626 __isl_keep isl_ast_print_options
*options
)
1630 if (!p
|| !list
|| !options
)
1631 return isl_printer_free(p
);
1633 for (i
= 0; i
< list
->n
; ++i
)
1634 p
= print_ast_node_c(p
, list
->p
[i
], options
, 1, 1);
1639 #define ISL_AST_MACRO_FLOORD (1 << 0)
1640 #define ISL_AST_MACRO_MIN (1 << 1)
1641 #define ISL_AST_MACRO_MAX (1 << 2)
1642 #define ISL_AST_MACRO_ALL (ISL_AST_MACRO_FLOORD | \
1643 ISL_AST_MACRO_MIN | \
1646 /* If "expr" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1647 * then set the corresponding bit in "macros".
1649 static int ast_expr_required_macros(__isl_keep isl_ast_expr
*expr
, int macros
)
1653 if (macros
== ISL_AST_MACRO_ALL
)
1656 if (expr
->type
!= isl_ast_expr_op
)
1659 if (expr
->u
.op
.op
== isl_ast_op_min
)
1660 macros
|= ISL_AST_MACRO_MIN
;
1661 if (expr
->u
.op
.op
== isl_ast_op_max
)
1662 macros
|= ISL_AST_MACRO_MAX
;
1663 if (expr
->u
.op
.op
== isl_ast_op_fdiv_q
)
1664 macros
|= ISL_AST_MACRO_FLOORD
;
1666 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
)
1667 macros
= ast_expr_required_macros(expr
->u
.op
.args
[i
], macros
);
1672 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list
*list
,
1675 /* If "node" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1676 * then set the corresponding bit in "macros".
1678 static int ast_node_required_macros(__isl_keep isl_ast_node
*node
, int macros
)
1680 if (macros
== ISL_AST_MACRO_ALL
)
1683 switch (node
->type
) {
1684 case isl_ast_node_for
:
1685 macros
= ast_expr_required_macros(node
->u
.f
.init
, macros
);
1686 if (!node
->u
.f
.degenerate
) {
1687 macros
= ast_expr_required_macros(node
->u
.f
.cond
,
1689 macros
= ast_expr_required_macros(node
->u
.f
.inc
,
1692 macros
= ast_node_required_macros(node
->u
.f
.body
, macros
);
1694 case isl_ast_node_if
:
1695 macros
= ast_expr_required_macros(node
->u
.i
.guard
, macros
);
1696 macros
= ast_node_required_macros(node
->u
.i
.then
, macros
);
1697 if (node
->u
.i
.else_node
)
1698 macros
= ast_node_required_macros(node
->u
.i
.else_node
,
1701 case isl_ast_node_block
:
1702 macros
= ast_node_list_required_macros(node
->u
.b
.children
,
1705 case isl_ast_node_user
:
1706 macros
= ast_expr_required_macros(node
->u
.e
.expr
, macros
);
1708 case isl_ast_node_error
:
1715 /* If "list" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1716 * then set the corresponding bit in "macros".
1718 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list
*list
,
1723 for (i
= 0; i
< list
->n
; ++i
)
1724 macros
= ast_node_required_macros(list
->p
[i
], macros
);
1729 /* Print a macro definition for the operator "type".
1731 __isl_give isl_printer
*isl_ast_op_type_print_macro(
1732 enum isl_ast_op_type type
, __isl_take isl_printer
*p
)
1735 case isl_ast_op_min
:
1736 p
= isl_printer_start_line(p
);
1737 p
= isl_printer_print_str(p
,
1738 "#define min(x,y) ((x) < (y) ? (x) : (y))");
1739 p
= isl_printer_end_line(p
);
1741 case isl_ast_op_max
:
1742 p
= isl_printer_start_line(p
);
1743 p
= isl_printer_print_str(p
,
1744 "#define max(x,y) ((x) > (y) ? (x) : (y))");
1745 p
= isl_printer_end_line(p
);
1747 case isl_ast_op_fdiv_q
:
1748 p
= isl_printer_start_line(p
);
1749 p
= isl_printer_print_str(p
,
1750 "#define floord(n,d) "
1751 "(((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))");
1752 p
= isl_printer_end_line(p
);
1761 /* Call "fn" for each type of operation that appears in "node"
1762 * and that requires a macro definition.
1764 int isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node
*node
,
1765 int (*fn
)(enum isl_ast_op_type type
, void *user
), void *user
)
1772 macros
= ast_node_required_macros(node
, 0);
1774 if (macros
& ISL_AST_MACRO_MIN
&& fn(isl_ast_op_min
, user
) < 0)
1776 if (macros
& ISL_AST_MACRO_MAX
&& fn(isl_ast_op_max
, user
) < 0)
1778 if (macros
& ISL_AST_MACRO_FLOORD
&& fn(isl_ast_op_fdiv_q
, user
) < 0)
1784 static int ast_op_type_print_macro(enum isl_ast_op_type type
, void *user
)
1786 isl_printer
**p
= user
;
1788 *p
= isl_ast_op_type_print_macro(type
, *p
);
1793 /* Print macro definitions for all the macros used in the result
1794 * of printing "node.
1796 __isl_give isl_printer
*isl_ast_node_print_macros(
1797 __isl_keep isl_ast_node
*node
, __isl_take isl_printer
*p
)
1799 if (isl_ast_node_foreach_ast_op_type(node
,
1800 &ast_op_type_print_macro
, &p
) < 0)
1801 return isl_printer_free(p
);