2 * Copyright 2012-2013 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
10 #include <isl_ast_private.h>
15 #include <isl_list_templ.c>
20 #include <isl_list_templ.c>
22 isl_ctx
*isl_ast_print_options_get_ctx(
23 __isl_keep isl_ast_print_options
*options
)
25 return options
? options
->ctx
: NULL
;
28 __isl_give isl_ast_print_options
*isl_ast_print_options_alloc(isl_ctx
*ctx
)
30 isl_ast_print_options
*options
;
32 options
= isl_calloc_type(ctx
, isl_ast_print_options
);
43 __isl_give isl_ast_print_options
*isl_ast_print_options_dup(
44 __isl_keep isl_ast_print_options
*options
)
47 isl_ast_print_options
*dup
;
52 ctx
= isl_ast_print_options_get_ctx(options
);
53 dup
= isl_ast_print_options_alloc(ctx
);
57 dup
->print_for
= options
->print_for
;
58 dup
->print_for_user
= options
->print_for_user
;
59 dup
->print_user
= options
->print_user
;
60 dup
->print_user_user
= options
->print_user_user
;
65 __isl_give isl_ast_print_options
*isl_ast_print_options_cow(
66 __isl_take isl_ast_print_options
*options
)
71 if (options
->ref
== 1)
74 return isl_ast_print_options_dup(options
);
77 __isl_give isl_ast_print_options
*isl_ast_print_options_copy(
78 __isl_keep isl_ast_print_options
*options
)
87 __isl_null isl_ast_print_options
*isl_ast_print_options_free(
88 __isl_take isl_ast_print_options
*options
)
93 if (--options
->ref
> 0)
96 isl_ctx_deref(options
->ctx
);
102 /* Set the print_user callback of "options" to "print_user".
104 * If this callback is set, then it used to print user nodes in the AST.
105 * Otherwise, the expression associated to the user node is printed.
107 __isl_give isl_ast_print_options
*isl_ast_print_options_set_print_user(
108 __isl_take isl_ast_print_options
*options
,
109 __isl_give isl_printer
*(*print_user
)(__isl_take isl_printer
*p
,
110 __isl_take isl_ast_print_options
*options
,
111 __isl_keep isl_ast_node
*node
, void *user
),
114 options
= isl_ast_print_options_cow(options
);
118 options
->print_user
= print_user
;
119 options
->print_user_user
= user
;
124 /* Set the print_for callback of "options" to "print_for".
126 * If this callback is set, then it used to print for nodes in the AST.
128 __isl_give isl_ast_print_options
*isl_ast_print_options_set_print_for(
129 __isl_take isl_ast_print_options
*options
,
130 __isl_give isl_printer
*(*print_for
)(__isl_take isl_printer
*p
,
131 __isl_take isl_ast_print_options
*options
,
132 __isl_keep isl_ast_node
*node
, void *user
),
135 options
= isl_ast_print_options_cow(options
);
139 options
->print_for
= print_for
;
140 options
->print_for_user
= user
;
145 __isl_give isl_ast_expr
*isl_ast_expr_copy(__isl_keep isl_ast_expr
*expr
)
154 __isl_give isl_ast_expr
*isl_ast_expr_dup(__isl_keep isl_ast_expr
*expr
)
163 ctx
= isl_ast_expr_get_ctx(expr
);
164 switch (expr
->type
) {
165 case isl_ast_expr_int
:
166 dup
= isl_ast_expr_from_val(isl_val_copy(expr
->u
.v
));
168 case isl_ast_expr_id
:
169 dup
= isl_ast_expr_from_id(isl_id_copy(expr
->u
.id
));
171 case isl_ast_expr_op
:
172 dup
= isl_ast_expr_alloc_op(ctx
,
173 expr
->u
.op
.op
, expr
->u
.op
.n_arg
);
176 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
)
178 isl_ast_expr_copy(expr
->u
.op
.args
[i
]);
180 case isl_ast_expr_error
:
190 __isl_give isl_ast_expr
*isl_ast_expr_cow(__isl_take isl_ast_expr
*expr
)
198 return isl_ast_expr_dup(expr
);
201 __isl_null isl_ast_expr
*isl_ast_expr_free(__isl_take isl_ast_expr
*expr
)
211 isl_ctx_deref(expr
->ctx
);
213 switch (expr
->type
) {
214 case isl_ast_expr_int
:
215 isl_val_free(expr
->u
.v
);
217 case isl_ast_expr_id
:
218 isl_id_free(expr
->u
.id
);
220 case isl_ast_expr_op
:
222 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
)
223 isl_ast_expr_free(expr
->u
.op
.args
[i
]);
224 free(expr
->u
.op
.args
);
226 case isl_ast_expr_error
:
234 isl_ctx
*isl_ast_expr_get_ctx(__isl_keep isl_ast_expr
*expr
)
236 return expr
? expr
->ctx
: NULL
;
239 enum isl_ast_expr_type
isl_ast_expr_get_type(__isl_keep isl_ast_expr
*expr
)
241 return expr
? expr
->type
: isl_ast_expr_error
;
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 /* Is "expr1" equal to "expr2"?
328 int isl_ast_expr_is_equal(__isl_keep isl_ast_expr
*expr1
,
329 __isl_keep isl_ast_expr
*expr2
)
333 if (!expr1
|| !expr2
)
338 if (expr1
->type
!= expr2
->type
)
340 switch (expr1
->type
) {
341 case isl_ast_expr_int
:
342 return isl_val_eq(expr1
->u
.v
, expr2
->u
.v
);
343 case isl_ast_expr_id
:
344 return expr1
->u
.id
== expr2
->u
.id
;
345 case isl_ast_expr_op
:
346 if (expr1
->u
.op
.op
!= expr2
->u
.op
.op
)
348 if (expr1
->u
.op
.n_arg
!= expr2
->u
.op
.n_arg
)
350 for (i
= 0; i
< expr1
->u
.op
.n_arg
; ++i
) {
352 equal
= isl_ast_expr_is_equal(expr1
->u
.op
.args
[i
],
353 expr2
->u
.op
.args
[i
]);
355 if (equal
< 0 || !equal
)
359 case isl_ast_expr_error
:
364 /* Create a new operation expression of operation type "op",
365 * with "n_arg" as yet unspecified arguments.
367 __isl_give isl_ast_expr
*isl_ast_expr_alloc_op(isl_ctx
*ctx
,
368 enum isl_ast_op_type op
, int n_arg
)
372 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
379 expr
->type
= isl_ast_expr_op
;
381 expr
->u
.op
.n_arg
= n_arg
;
382 expr
->u
.op
.args
= isl_calloc_array(ctx
, isl_ast_expr
*, n_arg
);
384 if (n_arg
&& !expr
->u
.op
.args
)
385 return isl_ast_expr_free(expr
);
390 /* Create a new id expression representing "id".
392 __isl_give isl_ast_expr
*isl_ast_expr_from_id(__isl_take isl_id
*id
)
400 ctx
= isl_id_get_ctx(id
);
401 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
408 expr
->type
= isl_ast_expr_id
;
417 /* Create a new integer expression representing "i".
419 __isl_give isl_ast_expr
*isl_ast_expr_alloc_int_si(isl_ctx
*ctx
, int i
)
423 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
430 expr
->type
= isl_ast_expr_int
;
431 expr
->u
.v
= isl_val_int_from_si(ctx
, i
);
433 return isl_ast_expr_free(expr
);
438 /* Create a new integer expression representing "v".
440 __isl_give isl_ast_expr
*isl_ast_expr_from_val(__isl_take isl_val
*v
)
447 if (!isl_val_is_int(v
))
448 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
449 "expecting integer value", goto error
);
451 ctx
= isl_val_get_ctx(v
);
452 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
459 expr
->type
= isl_ast_expr_int
;
468 /* Create an expression representing the negation of "arg".
470 __isl_give isl_ast_expr
*isl_ast_expr_neg(__isl_take isl_ast_expr
*arg
)
473 isl_ast_expr
*expr
= NULL
;
478 ctx
= isl_ast_expr_get_ctx(arg
);
479 expr
= isl_ast_expr_alloc_op(ctx
, isl_ast_op_minus
, 1);
483 expr
->u
.op
.args
[0] = arg
;
487 isl_ast_expr_free(arg
);
491 /* Create an expression representing the binary operation "type"
492 * applied to "expr1" and "expr2".
494 __isl_give isl_ast_expr
*isl_ast_expr_alloc_binary(enum isl_ast_op_type type
,
495 __isl_take isl_ast_expr
*expr1
, __isl_take isl_ast_expr
*expr2
)
498 isl_ast_expr
*expr
= NULL
;
500 if (!expr1
|| !expr2
)
503 ctx
= isl_ast_expr_get_ctx(expr1
);
504 expr
= isl_ast_expr_alloc_op(ctx
, type
, 2);
508 expr
->u
.op
.args
[0] = expr1
;
509 expr
->u
.op
.args
[1] = expr2
;
513 isl_ast_expr_free(expr1
);
514 isl_ast_expr_free(expr2
);
518 /* Create an expression representing the sum of "expr1" and "expr2".
520 __isl_give isl_ast_expr
*isl_ast_expr_add(__isl_take isl_ast_expr
*expr1
,
521 __isl_take isl_ast_expr
*expr2
)
523 return isl_ast_expr_alloc_binary(isl_ast_op_add
, expr1
, expr2
);
526 /* Create an expression representing the difference of "expr1" and "expr2".
528 __isl_give isl_ast_expr
*isl_ast_expr_sub(__isl_take isl_ast_expr
*expr1
,
529 __isl_take isl_ast_expr
*expr2
)
531 return isl_ast_expr_alloc_binary(isl_ast_op_sub
, expr1
, expr2
);
534 /* Create an expression representing the product of "expr1" and "expr2".
536 __isl_give isl_ast_expr
*isl_ast_expr_mul(__isl_take isl_ast_expr
*expr1
,
537 __isl_take isl_ast_expr
*expr2
)
539 return isl_ast_expr_alloc_binary(isl_ast_op_mul
, expr1
, expr2
);
542 /* Create an expression representing the quotient of "expr1" and "expr2".
544 __isl_give isl_ast_expr
*isl_ast_expr_div(__isl_take isl_ast_expr
*expr1
,
545 __isl_take isl_ast_expr
*expr2
)
547 return isl_ast_expr_alloc_binary(isl_ast_op_div
, expr1
, expr2
);
550 /* Create an expression representing the conjunction of "expr1" and "expr2".
552 __isl_give isl_ast_expr
*isl_ast_expr_and(__isl_take isl_ast_expr
*expr1
,
553 __isl_take isl_ast_expr
*expr2
)
555 return isl_ast_expr_alloc_binary(isl_ast_op_and
, expr1
, expr2
);
558 /* Create an expression representing the disjunction of "expr1" and "expr2".
560 __isl_give isl_ast_expr
*isl_ast_expr_or(__isl_take isl_ast_expr
*expr1
,
561 __isl_take isl_ast_expr
*expr2
)
563 return isl_ast_expr_alloc_binary(isl_ast_op_or
, expr1
, expr2
);
566 /* Create an expression representing an access to "array" with index
567 * expressions "indices".
569 __isl_give isl_ast_expr
*isl_ast_expr_access(__isl_take isl_ast_expr
*array
,
570 __isl_take isl_ast_expr_list
*indices
)
574 isl_ast_expr
*access
= NULL
;
576 if (!array
|| !indices
)
579 ctx
= isl_ast_expr_get_ctx(array
);
580 n
= isl_ast_expr_list_n_ast_expr(indices
);
581 access
= isl_ast_expr_alloc_op(ctx
, isl_ast_op_access
, 1 + n
);
584 for (i
= 0; i
< n
; ++i
) {
586 index
= isl_ast_expr_list_get_ast_expr(indices
, i
);
587 access
->u
.op
.args
[1 + i
] = index
;
591 access
->u
.op
.args
[0] = array
;
593 isl_ast_expr_list_free(indices
);
596 isl_ast_expr_free(array
);
597 isl_ast_expr_list_free(indices
);
598 isl_ast_expr_free(access
);
602 /* For each subexpression of "expr" of type isl_ast_expr_id,
603 * if it appears in "id2expr", then replace it by the corresponding
606 __isl_give isl_ast_expr
*isl_ast_expr_substitute_ids(
607 __isl_take isl_ast_expr
*expr
, __isl_take isl_id_to_ast_expr
*id2expr
)
612 if (!expr
|| !id2expr
)
615 switch (expr
->type
) {
616 case isl_ast_expr_int
:
618 case isl_ast_expr_id
:
619 if (!isl_id_to_ast_expr_has(id2expr
, expr
->u
.id
))
621 id
= isl_id_copy(expr
->u
.id
);
622 isl_ast_expr_free(expr
);
623 expr
= isl_id_to_ast_expr_get(id2expr
, id
);
625 case isl_ast_expr_op
:
626 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
) {
628 arg
= isl_ast_expr_copy(expr
->u
.op
.args
[i
]);
629 arg
= isl_ast_expr_substitute_ids(arg
,
630 isl_id_to_ast_expr_copy(id2expr
));
631 if (arg
== expr
->u
.op
.args
[i
]) {
632 isl_ast_expr_free(arg
);
636 expr
= isl_ast_expr_free(expr
);
637 expr
= isl_ast_expr_cow(expr
);
639 isl_ast_expr_free(arg
);
642 isl_ast_expr_free(expr
->u
.op
.args
[i
]);
643 expr
->u
.op
.args
[i
] = arg
;
646 case isl_ast_expr_error
:
647 expr
= isl_ast_expr_free(expr
);
651 isl_id_to_ast_expr_free(id2expr
);
654 isl_ast_expr_free(expr
);
655 isl_id_to_ast_expr_free(id2expr
);
659 isl_ctx
*isl_ast_node_get_ctx(__isl_keep isl_ast_node
*node
)
661 return node
? node
->ctx
: NULL
;
664 enum isl_ast_node_type
isl_ast_node_get_type(__isl_keep isl_ast_node
*node
)
666 return node
? node
->type
: isl_ast_node_error
;
669 __isl_give isl_ast_node
*isl_ast_node_alloc(isl_ctx
*ctx
,
670 enum isl_ast_node_type type
)
674 node
= isl_calloc_type(ctx
, isl_ast_node
);
686 /* Create an if node with the given guard.
688 * The then body needs to be filled in later.
690 __isl_give isl_ast_node
*isl_ast_node_alloc_if(__isl_take isl_ast_expr
*guard
)
697 node
= isl_ast_node_alloc(isl_ast_expr_get_ctx(guard
), isl_ast_node_if
);
700 node
->u
.i
.guard
= guard
;
704 isl_ast_expr_free(guard
);
708 /* Create a for node with the given iterator.
710 * The remaining fields need to be filled in later.
712 __isl_give isl_ast_node
*isl_ast_node_alloc_for(__isl_take isl_id
*id
)
720 ctx
= isl_id_get_ctx(id
);
721 node
= isl_ast_node_alloc(ctx
, isl_ast_node_for
);
725 node
->u
.f
.iterator
= isl_ast_expr_from_id(id
);
726 if (!node
->u
.f
.iterator
)
727 return isl_ast_node_free(node
);
735 /* Create a user node evaluating "expr".
737 __isl_give isl_ast_node
*isl_ast_node_alloc_user(__isl_take isl_ast_expr
*expr
)
745 ctx
= isl_ast_expr_get_ctx(expr
);
746 node
= isl_ast_node_alloc(ctx
, isl_ast_node_user
);
750 node
->u
.e
.expr
= expr
;
754 isl_ast_expr_free(expr
);
758 /* Create a block node with the given children.
760 __isl_give isl_ast_node
*isl_ast_node_alloc_block(
761 __isl_take isl_ast_node_list
*list
)
769 ctx
= isl_ast_node_list_get_ctx(list
);
770 node
= isl_ast_node_alloc(ctx
, isl_ast_node_block
);
774 node
->u
.b
.children
= list
;
778 isl_ast_node_list_free(list
);
782 /* Represent the given list of nodes as a single node, either by
783 * extract the node from a single element list or by creating
784 * a block node with the list of nodes as children.
786 __isl_give isl_ast_node
*isl_ast_node_from_ast_node_list(
787 __isl_take isl_ast_node_list
*list
)
791 if (isl_ast_node_list_n_ast_node(list
) != 1)
792 return isl_ast_node_alloc_block(list
);
794 node
= isl_ast_node_list_get_ast_node(list
, 0);
795 isl_ast_node_list_free(list
);
800 __isl_give isl_ast_node
*isl_ast_node_copy(__isl_keep isl_ast_node
*node
)
809 __isl_give isl_ast_node
*isl_ast_node_dup(__isl_keep isl_ast_node
*node
)
816 dup
= isl_ast_node_alloc(isl_ast_node_get_ctx(node
), node
->type
);
820 switch (node
->type
) {
821 case isl_ast_node_if
:
822 dup
->u
.i
.guard
= isl_ast_expr_copy(node
->u
.i
.guard
);
823 dup
->u
.i
.then
= isl_ast_node_copy(node
->u
.i
.then
);
824 dup
->u
.i
.else_node
= isl_ast_node_copy(node
->u
.i
.else_node
);
825 if (!dup
->u
.i
.guard
|| !dup
->u
.i
.then
||
826 (node
->u
.i
.else_node
&& !dup
->u
.i
.else_node
))
827 return isl_ast_node_free(dup
);
829 case isl_ast_node_for
:
830 dup
->u
.f
.iterator
= isl_ast_expr_copy(node
->u
.f
.iterator
);
831 dup
->u
.f
.init
= isl_ast_expr_copy(node
->u
.f
.init
);
832 dup
->u
.f
.cond
= isl_ast_expr_copy(node
->u
.f
.cond
);
833 dup
->u
.f
.inc
= isl_ast_expr_copy(node
->u
.f
.inc
);
834 dup
->u
.f
.body
= isl_ast_node_copy(node
->u
.f
.body
);
835 if (!dup
->u
.f
.iterator
|| !dup
->u
.f
.init
|| !dup
->u
.f
.cond
||
836 !dup
->u
.f
.inc
|| !dup
->u
.f
.body
)
837 return isl_ast_node_free(dup
);
839 case isl_ast_node_block
:
840 dup
->u
.b
.children
= isl_ast_node_list_copy(node
->u
.b
.children
);
841 if (!dup
->u
.b
.children
)
842 return isl_ast_node_free(dup
);
844 case isl_ast_node_user
:
845 dup
->u
.e
.expr
= isl_ast_expr_copy(node
->u
.e
.expr
);
847 return isl_ast_node_free(dup
);
849 case isl_ast_node_error
:
856 __isl_give isl_ast_node
*isl_ast_node_cow(__isl_take isl_ast_node
*node
)
864 return isl_ast_node_dup(node
);
867 __isl_null isl_ast_node
*isl_ast_node_free(__isl_take isl_ast_node
*node
)
875 switch (node
->type
) {
876 case isl_ast_node_if
:
877 isl_ast_expr_free(node
->u
.i
.guard
);
878 isl_ast_node_free(node
->u
.i
.then
);
879 isl_ast_node_free(node
->u
.i
.else_node
);
881 case isl_ast_node_for
:
882 isl_ast_expr_free(node
->u
.f
.iterator
);
883 isl_ast_expr_free(node
->u
.f
.init
);
884 isl_ast_expr_free(node
->u
.f
.cond
);
885 isl_ast_expr_free(node
->u
.f
.inc
);
886 isl_ast_node_free(node
->u
.f
.body
);
888 case isl_ast_node_block
:
889 isl_ast_node_list_free(node
->u
.b
.children
);
891 case isl_ast_node_user
:
892 isl_ast_expr_free(node
->u
.e
.expr
);
894 case isl_ast_node_error
:
898 isl_id_free(node
->annotation
);
899 isl_ctx_deref(node
->ctx
);
905 /* Replace the body of the for node "node" by "body".
907 __isl_give isl_ast_node
*isl_ast_node_for_set_body(
908 __isl_take isl_ast_node
*node
, __isl_take isl_ast_node
*body
)
910 node
= isl_ast_node_cow(node
);
913 if (node
->type
!= isl_ast_node_for
)
914 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
915 "not a for node", goto error
);
917 isl_ast_node_free(node
->u
.f
.body
);
918 node
->u
.f
.body
= body
;
922 isl_ast_node_free(node
);
923 isl_ast_node_free(body
);
927 __isl_give isl_ast_node
*isl_ast_node_for_get_body(
928 __isl_keep isl_ast_node
*node
)
932 if (node
->type
!= isl_ast_node_for
)
933 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
934 "not a for node", return NULL
);
935 return isl_ast_node_copy(node
->u
.f
.body
);
938 /* Mark the given for node as being degenerate.
940 __isl_give isl_ast_node
*isl_ast_node_for_mark_degenerate(
941 __isl_take isl_ast_node
*node
)
943 node
= isl_ast_node_cow(node
);
946 node
->u
.f
.degenerate
= 1;
950 int isl_ast_node_for_is_degenerate(__isl_keep isl_ast_node
*node
)
954 if (node
->type
!= isl_ast_node_for
)
955 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
956 "not a for node", return -1);
957 return node
->u
.f
.degenerate
;
960 __isl_give isl_ast_expr
*isl_ast_node_for_get_iterator(
961 __isl_keep isl_ast_node
*node
)
965 if (node
->type
!= isl_ast_node_for
)
966 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
967 "not a for node", return NULL
);
968 return isl_ast_expr_copy(node
->u
.f
.iterator
);
971 __isl_give isl_ast_expr
*isl_ast_node_for_get_init(
972 __isl_keep isl_ast_node
*node
)
976 if (node
->type
!= isl_ast_node_for
)
977 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
978 "not a for node", return NULL
);
979 return isl_ast_expr_copy(node
->u
.f
.init
);
982 /* Return the condition expression of the given for node.
984 * If the for node is degenerate, then the condition is not explicitly
985 * stored in the node. Instead, it is constructed as
989 __isl_give isl_ast_expr
*isl_ast_node_for_get_cond(
990 __isl_keep isl_ast_node
*node
)
994 if (node
->type
!= isl_ast_node_for
)
995 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
996 "not a for node", return NULL
);
997 if (!node
->u
.f
.degenerate
)
998 return isl_ast_expr_copy(node
->u
.f
.cond
);
1000 return isl_ast_expr_alloc_binary(isl_ast_op_le
,
1001 isl_ast_expr_copy(node
->u
.f
.iterator
),
1002 isl_ast_expr_copy(node
->u
.f
.init
));
1005 /* Return the increment of the given for node.
1007 * If the for node is degenerate, then the increment is not explicitly
1008 * stored in the node. We simply return "1".
1010 __isl_give isl_ast_expr
*isl_ast_node_for_get_inc(
1011 __isl_keep isl_ast_node
*node
)
1015 if (node
->type
!= isl_ast_node_for
)
1016 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1017 "not a for node", return NULL
);
1018 if (!node
->u
.f
.degenerate
)
1019 return isl_ast_expr_copy(node
->u
.f
.inc
);
1020 return isl_ast_expr_alloc_int_si(isl_ast_node_get_ctx(node
), 1);
1023 /* Replace the then branch of the if node "node" by "child".
1025 __isl_give isl_ast_node
*isl_ast_node_if_set_then(
1026 __isl_take isl_ast_node
*node
, __isl_take isl_ast_node
*child
)
1028 node
= isl_ast_node_cow(node
);
1029 if (!node
|| !child
)
1031 if (node
->type
!= isl_ast_node_if
)
1032 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1033 "not an if node", goto error
);
1035 isl_ast_node_free(node
->u
.i
.then
);
1036 node
->u
.i
.then
= child
;
1040 isl_ast_node_free(node
);
1041 isl_ast_node_free(child
);
1045 __isl_give isl_ast_node
*isl_ast_node_if_get_then(
1046 __isl_keep isl_ast_node
*node
)
1050 if (node
->type
!= isl_ast_node_if
)
1051 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1052 "not an if node", return NULL
);
1053 return isl_ast_node_copy(node
->u
.i
.then
);
1056 int isl_ast_node_if_has_else(
1057 __isl_keep isl_ast_node
*node
)
1061 if (node
->type
!= isl_ast_node_if
)
1062 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1063 "not an if node", return -1);
1064 return node
->u
.i
.else_node
!= NULL
;
1067 __isl_give isl_ast_node
*isl_ast_node_if_get_else(
1068 __isl_keep isl_ast_node
*node
)
1072 if (node
->type
!= isl_ast_node_if
)
1073 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1074 "not an if node", return NULL
);
1075 return isl_ast_node_copy(node
->u
.i
.else_node
);
1078 __isl_give isl_ast_expr
*isl_ast_node_if_get_cond(
1079 __isl_keep isl_ast_node
*node
)
1083 if (node
->type
!= isl_ast_node_if
)
1084 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1085 "not a guard node", return NULL
);
1086 return isl_ast_expr_copy(node
->u
.i
.guard
);
1089 __isl_give isl_ast_node_list
*isl_ast_node_block_get_children(
1090 __isl_keep isl_ast_node
*node
)
1094 if (node
->type
!= isl_ast_node_block
)
1095 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1096 "not a block node", return NULL
);
1097 return isl_ast_node_list_copy(node
->u
.b
.children
);
1100 __isl_give isl_ast_expr
*isl_ast_node_user_get_expr(
1101 __isl_keep isl_ast_node
*node
)
1106 return isl_ast_expr_copy(node
->u
.e
.expr
);
1109 __isl_give isl_id
*isl_ast_node_get_annotation(__isl_keep isl_ast_node
*node
)
1111 return node
? isl_id_copy(node
->annotation
) : NULL
;
1114 /* Replace node->annotation by "annotation".
1116 __isl_give isl_ast_node
*isl_ast_node_set_annotation(
1117 __isl_take isl_ast_node
*node
, __isl_take isl_id
*annotation
)
1119 node
= isl_ast_node_cow(node
);
1120 if (!node
|| !annotation
)
1123 isl_id_free(node
->annotation
);
1124 node
->annotation
= annotation
;
1128 isl_id_free(annotation
);
1129 return isl_ast_node_free(node
);
1132 /* Textual C representation of the various operators.
1134 static char *op_str
[] = {
1135 [isl_ast_op_and
] = "&&",
1136 [isl_ast_op_and_then
] = "&&",
1137 [isl_ast_op_or
] = "||",
1138 [isl_ast_op_or_else
] = "||",
1139 [isl_ast_op_max
] = "max",
1140 [isl_ast_op_min
] = "min",
1141 [isl_ast_op_minus
] = "-",
1142 [isl_ast_op_add
] = "+",
1143 [isl_ast_op_sub
] = "-",
1144 [isl_ast_op_mul
] = "*",
1145 [isl_ast_op_pdiv_q
] = "/",
1146 [isl_ast_op_pdiv_r
] = "%",
1147 [isl_ast_op_div
] = "/",
1148 [isl_ast_op_eq
] = "==",
1149 [isl_ast_op_le
] = "<=",
1150 [isl_ast_op_ge
] = ">=",
1151 [isl_ast_op_lt
] = "<",
1152 [isl_ast_op_gt
] = ">",
1153 [isl_ast_op_member
] = "."
1156 /* Precedence in C of the various operators.
1157 * Based on http://en.wikipedia.org/wiki/Operators_in_C_and_C++
1158 * Lowest value means highest precedence.
1160 static int op_prec
[] = {
1161 [isl_ast_op_and
] = 13,
1162 [isl_ast_op_and_then
] = 13,
1163 [isl_ast_op_or
] = 14,
1164 [isl_ast_op_or_else
] = 14,
1165 [isl_ast_op_max
] = 2,
1166 [isl_ast_op_min
] = 2,
1167 [isl_ast_op_minus
] = 3,
1168 [isl_ast_op_add
] = 6,
1169 [isl_ast_op_sub
] = 6,
1170 [isl_ast_op_mul
] = 5,
1171 [isl_ast_op_div
] = 5,
1172 [isl_ast_op_fdiv_q
] = 2,
1173 [isl_ast_op_pdiv_q
] = 5,
1174 [isl_ast_op_pdiv_r
] = 5,
1175 [isl_ast_op_cond
] = 15,
1176 [isl_ast_op_select
] = 15,
1177 [isl_ast_op_eq
] = 9,
1178 [isl_ast_op_le
] = 8,
1179 [isl_ast_op_ge
] = 8,
1180 [isl_ast_op_lt
] = 8,
1181 [isl_ast_op_gt
] = 8,
1182 [isl_ast_op_call
] = 2,
1183 [isl_ast_op_access
] = 2,
1184 [isl_ast_op_member
] = 2
1187 /* Is the operator left-to-right associative?
1189 static int op_left
[] = {
1190 [isl_ast_op_and
] = 1,
1191 [isl_ast_op_and_then
] = 1,
1192 [isl_ast_op_or
] = 1,
1193 [isl_ast_op_or_else
] = 1,
1194 [isl_ast_op_max
] = 1,
1195 [isl_ast_op_min
] = 1,
1196 [isl_ast_op_minus
] = 0,
1197 [isl_ast_op_add
] = 1,
1198 [isl_ast_op_sub
] = 1,
1199 [isl_ast_op_mul
] = 1,
1200 [isl_ast_op_div
] = 1,
1201 [isl_ast_op_fdiv_q
] = 1,
1202 [isl_ast_op_pdiv_q
] = 1,
1203 [isl_ast_op_pdiv_r
] = 1,
1204 [isl_ast_op_cond
] = 0,
1205 [isl_ast_op_select
] = 0,
1206 [isl_ast_op_eq
] = 1,
1207 [isl_ast_op_le
] = 1,
1208 [isl_ast_op_ge
] = 1,
1209 [isl_ast_op_lt
] = 1,
1210 [isl_ast_op_gt
] = 1,
1211 [isl_ast_op_call
] = 1,
1212 [isl_ast_op_access
] = 1,
1213 [isl_ast_op_member
] = 1
1216 static int is_and(enum isl_ast_op_type op
)
1218 return op
== isl_ast_op_and
|| op
== isl_ast_op_and_then
;
1221 static int is_or(enum isl_ast_op_type op
)
1223 return op
== isl_ast_op_or
|| op
== isl_ast_op_or_else
;
1226 static int is_add_sub(enum isl_ast_op_type op
)
1228 return op
== isl_ast_op_add
|| op
== isl_ast_op_sub
;
1231 static int is_div_mod(enum isl_ast_op_type op
)
1233 return op
== isl_ast_op_div
|| op
== isl_ast_op_pdiv_r
;
1236 /* Do we need/want parentheses around "expr" as a subexpression of
1237 * an "op" operation? If "left" is set, then "expr" is the left-most
1240 * We only need parentheses if "expr" represents an operation.
1242 * If op has a higher precedence than expr->u.op.op, then we need
1244 * If op and expr->u.op.op have the same precedence, but the operations
1245 * are performed in an order that is different from the associativity,
1246 * then we need parentheses.
1248 * An and inside an or technically does not require parentheses,
1249 * but some compilers complain about that, so we add them anyway.
1251 * Computations such as "a / b * c" and "a % b + c" can be somewhat
1252 * difficult to read, so we add parentheses for those as well.
1254 static int sub_expr_need_parens(enum isl_ast_op_type op
,
1255 __isl_keep isl_ast_expr
*expr
, int left
)
1257 if (expr
->type
!= isl_ast_expr_op
)
1260 if (op_prec
[expr
->u
.op
.op
] > op_prec
[op
])
1262 if (op_prec
[expr
->u
.op
.op
] == op_prec
[op
] && left
!= op_left
[op
])
1265 if (is_or(op
) && is_and(expr
->u
.op
.op
))
1267 if (op
== isl_ast_op_mul
&& expr
->u
.op
.op
!= isl_ast_op_mul
&&
1268 op_prec
[expr
->u
.op
.op
] == op_prec
[op
])
1270 if (is_add_sub(op
) && is_div_mod(expr
->u
.op
.op
))
1276 /* Print "expr" as a subexpression of an "op" operation.
1277 * If "left" is set, then "expr" is the left-most operand.
1279 static __isl_give isl_printer
*print_sub_expr(__isl_take isl_printer
*p
,
1280 enum isl_ast_op_type op
, __isl_keep isl_ast_expr
*expr
, int left
)
1284 need_parens
= sub_expr_need_parens(op
, expr
, left
);
1287 p
= isl_printer_print_str(p
, "(");
1288 p
= isl_printer_print_ast_expr(p
, expr
);
1290 p
= isl_printer_print_str(p
, ")");
1294 /* Print a min or max reduction "expr".
1296 static __isl_give isl_printer
*print_min_max(__isl_take isl_printer
*p
,
1297 __isl_keep isl_ast_expr
*expr
)
1301 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1302 p
= isl_printer_print_str(p
, op_str
[expr
->u
.op
.op
]);
1303 p
= isl_printer_print_str(p
, "(");
1305 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1306 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1307 p
= isl_printer_print_str(p
, ", ");
1308 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[i
]);
1309 p
= isl_printer_print_str(p
, ")");
1315 /* Print a function call "expr".
1317 * The first argument represents the function to be called.
1319 static __isl_give isl_printer
*print_call(__isl_take isl_printer
*p
,
1320 __isl_keep isl_ast_expr
*expr
)
1324 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1325 p
= isl_printer_print_str(p
, "(");
1326 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1328 p
= isl_printer_print_str(p
, ", ");
1329 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[i
]);
1331 p
= isl_printer_print_str(p
, ")");
1336 /* Print an array access "expr".
1338 * The first argument represents the array being accessed.
1340 static __isl_give isl_printer
*print_access(__isl_take isl_printer
*p
,
1341 __isl_keep isl_ast_expr
*expr
)
1345 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1346 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1347 p
= isl_printer_print_str(p
, "[");
1348 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[i
]);
1349 p
= isl_printer_print_str(p
, "]");
1355 /* Print "expr" to "p".
1357 * If we are printing in isl format, then we also print an indication
1358 * of the size of the expression (if it was computed).
1360 __isl_give isl_printer
*isl_printer_print_ast_expr(__isl_take isl_printer
*p
,
1361 __isl_keep isl_ast_expr
*expr
)
1366 return isl_printer_free(p
);
1368 switch (expr
->type
) {
1369 case isl_ast_expr_op
:
1370 if (expr
->u
.op
.op
== isl_ast_op_call
) {
1371 p
= print_call(p
, expr
);
1374 if (expr
->u
.op
.op
== isl_ast_op_access
) {
1375 p
= print_access(p
, expr
);
1378 if (expr
->u
.op
.n_arg
== 1) {
1379 p
= isl_printer_print_str(p
, op_str
[expr
->u
.op
.op
]);
1380 p
= print_sub_expr(p
, expr
->u
.op
.op
,
1381 expr
->u
.op
.args
[0], 0);
1384 if (expr
->u
.op
.op
== isl_ast_op_fdiv_q
) {
1385 p
= isl_printer_print_str(p
, "floord(");
1386 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1387 p
= isl_printer_print_str(p
, ", ");
1388 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[1]);
1389 p
= isl_printer_print_str(p
, ")");
1392 if (expr
->u
.op
.op
== isl_ast_op_max
||
1393 expr
->u
.op
.op
== isl_ast_op_min
) {
1394 p
= print_min_max(p
, expr
);
1397 if (expr
->u
.op
.op
== isl_ast_op_cond
||
1398 expr
->u
.op
.op
== isl_ast_op_select
) {
1399 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1400 p
= isl_printer_print_str(p
, " ? ");
1401 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[1]);
1402 p
= isl_printer_print_str(p
, " : ");
1403 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[2]);
1406 if (expr
->u
.op
.n_arg
!= 2)
1407 isl_die(isl_printer_get_ctx(p
), isl_error_internal
,
1408 "operation should have two arguments",
1410 p
= print_sub_expr(p
, expr
->u
.op
.op
, expr
->u
.op
.args
[0], 1);
1411 if (expr
->u
.op
.op
!= isl_ast_op_member
)
1412 p
= isl_printer_print_str(p
, " ");
1413 p
= isl_printer_print_str(p
, op_str
[expr
->u
.op
.op
]);
1414 if (expr
->u
.op
.op
!= isl_ast_op_member
)
1415 p
= isl_printer_print_str(p
, " ");
1416 p
= print_sub_expr(p
, expr
->u
.op
.op
, expr
->u
.op
.args
[1], 0);
1418 case isl_ast_expr_id
:
1419 p
= isl_printer_print_str(p
, isl_id_get_name(expr
->u
.id
));
1421 case isl_ast_expr_int
:
1422 p
= isl_printer_print_val(p
, expr
->u
.v
);
1424 case isl_ast_expr_error
:
1430 isl_printer_free(p
);
1434 /* Print "node" to "p" in "isl format".
1436 static __isl_give isl_printer
*print_ast_node_isl(__isl_take isl_printer
*p
,
1437 __isl_keep isl_ast_node
*node
)
1439 p
= isl_printer_print_str(p
, "(");
1440 switch (node
->type
) {
1441 case isl_ast_node_for
:
1442 if (node
->u
.f
.degenerate
) {
1443 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1445 p
= isl_printer_print_str(p
, "init: ");
1446 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1447 p
= isl_printer_print_str(p
, ", ");
1448 p
= isl_printer_print_str(p
, "cond: ");
1449 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.cond
);
1450 p
= isl_printer_print_str(p
, ", ");
1451 p
= isl_printer_print_str(p
, "inc: ");
1452 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.inc
);
1454 if (node
->u
.f
.body
) {
1455 p
= isl_printer_print_str(p
, ", ");
1456 p
= isl_printer_print_str(p
, "body: ");
1457 p
= isl_printer_print_ast_node(p
, node
->u
.f
.body
);
1460 case isl_ast_node_user
:
1461 p
= isl_printer_print_ast_expr(p
, node
->u
.e
.expr
);
1463 case isl_ast_node_if
:
1464 p
= isl_printer_print_str(p
, "guard: ");
1465 p
= isl_printer_print_ast_expr(p
, node
->u
.i
.guard
);
1466 if (node
->u
.i
.then
) {
1467 p
= isl_printer_print_str(p
, ", ");
1468 p
= isl_printer_print_str(p
, "then: ");
1469 p
= isl_printer_print_ast_node(p
, node
->u
.i
.then
);
1471 if (node
->u
.i
.else_node
) {
1472 p
= isl_printer_print_str(p
, ", ");
1473 p
= isl_printer_print_str(p
, "else: ");
1474 p
= isl_printer_print_ast_node(p
, node
->u
.i
.else_node
);
1477 case isl_ast_node_block
:
1478 p
= isl_printer_print_ast_node_list(p
, node
->u
.b
.children
);
1483 p
= isl_printer_print_str(p
, ")");
1487 /* Do we need to print a block around the body "node" of a for or if node?
1489 * If the node is a block, then we need to print a block.
1490 * Also if the node is a degenerate for then we will print it as
1491 * an assignment followed by the body of the for loop, so we need a block
1493 * If the node is an if node with an else, then we print a block
1494 * to avoid spurious dangling else warnings emitted by some compilers.
1496 static int need_block(__isl_keep isl_ast_node
*node
)
1498 if (node
->type
== isl_ast_node_block
)
1500 if (node
->type
== isl_ast_node_for
&& node
->u
.f
.degenerate
)
1502 if (node
->type
== isl_ast_node_if
&& node
->u
.i
.else_node
)
1507 static __isl_give isl_printer
*print_ast_node_c(__isl_take isl_printer
*p
,
1508 __isl_keep isl_ast_node
*node
,
1509 __isl_keep isl_ast_print_options
*options
, int in_block
, int in_list
);
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
);
1514 /* Print the body "node" of a for or if node.
1515 * If "else_node" is set, then it is printed as well.
1517 * We first check if we need to print out a block.
1518 * We always print out a block if there is an else node to make
1519 * sure that the else node is matched to the correct if node.
1521 * If the else node is itself an if, then we print it as
1525 * Otherwise the else node is printed as
1530 static __isl_give isl_printer
*print_body_c(__isl_take isl_printer
*p
,
1531 __isl_keep isl_ast_node
*node
, __isl_keep isl_ast_node
*else_node
,
1532 __isl_keep isl_ast_print_options
*options
)
1535 return isl_printer_free(p
);
1537 if (!else_node
&& !need_block(node
)) {
1538 p
= isl_printer_end_line(p
);
1539 p
= isl_printer_indent(p
, 2);
1540 p
= isl_ast_node_print(node
, p
,
1541 isl_ast_print_options_copy(options
));
1542 p
= isl_printer_indent(p
, -2);
1546 p
= isl_printer_print_str(p
, " {");
1547 p
= isl_printer_end_line(p
);
1548 p
= isl_printer_indent(p
, 2);
1549 p
= print_ast_node_c(p
, node
, options
, 1, 0);
1550 p
= isl_printer_indent(p
, -2);
1551 p
= isl_printer_start_line(p
);
1552 p
= isl_printer_print_str(p
, "}");
1554 if (else_node
->type
== isl_ast_node_if
) {
1555 p
= isl_printer_print_str(p
, " else ");
1556 p
= print_if_c(p
, else_node
, options
, 0);
1558 p
= isl_printer_print_str(p
, " else");
1559 p
= print_body_c(p
, else_node
, NULL
, options
);
1562 p
= isl_printer_end_line(p
);
1567 /* Print the start of a compound statement.
1569 static __isl_give isl_printer
*start_block(__isl_take isl_printer
*p
)
1571 p
= isl_printer_start_line(p
);
1572 p
= isl_printer_print_str(p
, "{");
1573 p
= isl_printer_end_line(p
);
1574 p
= isl_printer_indent(p
, 2);
1579 /* Print the end of a compound statement.
1581 static __isl_give isl_printer
*end_block(__isl_take isl_printer
*p
)
1583 p
= isl_printer_indent(p
, -2);
1584 p
= isl_printer_start_line(p
);
1585 p
= isl_printer_print_str(p
, "}");
1586 p
= isl_printer_end_line(p
);
1591 /* Print the for node "node".
1593 * If the for node is degenerate, it is printed as
1595 * type iterator = init;
1598 * Otherwise, it is printed as
1600 * for (type iterator = init; cond; iterator += inc)
1603 * "in_block" is set if we are currently inside a block.
1604 * "in_list" is set if the current node is not alone in the block.
1605 * If we are not in a block or if the current not is not alone in the block
1606 * then we print a block around a degenerate for loop such that the variable
1607 * declaration will not conflict with any potential other declaration
1608 * of the same variable.
1610 static __isl_give isl_printer
*print_for_c(__isl_take isl_printer
*p
,
1611 __isl_keep isl_ast_node
*node
,
1612 __isl_keep isl_ast_print_options
*options
, int in_block
, int in_list
)
1618 type
= isl_options_get_ast_iterator_type(isl_printer_get_ctx(p
));
1619 if (!node
->u
.f
.degenerate
) {
1620 id
= isl_ast_expr_get_id(node
->u
.f
.iterator
);
1621 name
= isl_id_get_name(id
);
1623 p
= isl_printer_start_line(p
);
1624 p
= isl_printer_print_str(p
, "for (");
1625 p
= isl_printer_print_str(p
, type
);
1626 p
= isl_printer_print_str(p
, " ");
1627 p
= isl_printer_print_str(p
, name
);
1628 p
= isl_printer_print_str(p
, " = ");
1629 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1630 p
= isl_printer_print_str(p
, "; ");
1631 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.cond
);
1632 p
= isl_printer_print_str(p
, "; ");
1633 p
= isl_printer_print_str(p
, name
);
1634 p
= isl_printer_print_str(p
, " += ");
1635 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.inc
);
1636 p
= isl_printer_print_str(p
, ")");
1637 p
= print_body_c(p
, node
->u
.f
.body
, NULL
, options
);
1639 id
= isl_ast_expr_get_id(node
->u
.f
.iterator
);
1640 name
= isl_id_get_name(id
);
1642 if (!in_block
|| in_list
)
1644 p
= isl_printer_start_line(p
);
1645 p
= isl_printer_print_str(p
, type
);
1646 p
= isl_printer_print_str(p
, " ");
1647 p
= isl_printer_print_str(p
, name
);
1648 p
= isl_printer_print_str(p
, " = ");
1649 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1650 p
= isl_printer_print_str(p
, ";");
1651 p
= isl_printer_end_line(p
);
1652 p
= print_ast_node_c(p
, node
->u
.f
.body
, options
, 1, 0);
1653 if (!in_block
|| in_list
)
1660 /* Print the if node "node".
1661 * If "new_line" is set then the if node should be printed on a new line.
1663 static __isl_give isl_printer
*print_if_c(__isl_take isl_printer
*p
,
1664 __isl_keep isl_ast_node
*node
,
1665 __isl_keep isl_ast_print_options
*options
, int new_line
)
1668 p
= isl_printer_start_line(p
);
1669 p
= isl_printer_print_str(p
, "if (");
1670 p
= isl_printer_print_ast_expr(p
, node
->u
.i
.guard
);
1671 p
= isl_printer_print_str(p
, ")");
1672 p
= print_body_c(p
, node
->u
.i
.then
, node
->u
.i
.else_node
, options
);
1677 /* Print the "node" to "p".
1679 * "in_block" is set if we are currently inside a block.
1680 * If so, we do not print a block around the children of a block node.
1681 * We do this to avoid an extra block around the body of a degenerate
1684 * "in_list" is set if the current node is not alone in the block.
1686 static __isl_give isl_printer
*print_ast_node_c(__isl_take isl_printer
*p
,
1687 __isl_keep isl_ast_node
*node
,
1688 __isl_keep isl_ast_print_options
*options
, int in_block
, int in_list
)
1690 switch (node
->type
) {
1691 case isl_ast_node_for
:
1692 if (options
->print_for
)
1693 return options
->print_for(p
,
1694 isl_ast_print_options_copy(options
),
1695 node
, options
->print_for_user
);
1696 p
= print_for_c(p
, node
, options
, in_block
, in_list
);
1698 case isl_ast_node_if
:
1699 p
= print_if_c(p
, node
, options
, 1);
1701 case isl_ast_node_block
:
1704 p
= isl_ast_node_list_print(node
->u
.b
.children
, p
, options
);
1708 case isl_ast_node_user
:
1709 if (options
->print_user
)
1710 return options
->print_user(p
,
1711 isl_ast_print_options_copy(options
),
1712 node
, options
->print_user_user
);
1713 p
= isl_printer_start_line(p
);
1714 p
= isl_printer_print_ast_expr(p
, node
->u
.e
.expr
);
1715 p
= isl_printer_print_str(p
, ";");
1716 p
= isl_printer_end_line(p
);
1718 case isl_ast_node_error
:
1724 /* Print the for node "node" to "p".
1726 __isl_give isl_printer
*isl_ast_node_for_print(__isl_keep isl_ast_node
*node
,
1727 __isl_take isl_printer
*p
, __isl_take isl_ast_print_options
*options
)
1729 if (!node
|| !options
)
1731 if (node
->type
!= isl_ast_node_for
)
1732 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1733 "not a for node", goto error
);
1734 p
= print_for_c(p
, node
, options
, 0, 0);
1735 isl_ast_print_options_free(options
);
1738 isl_ast_print_options_free(options
);
1739 isl_printer_free(p
);
1743 /* Print the if node "node" to "p".
1745 __isl_give isl_printer
*isl_ast_node_if_print(__isl_keep isl_ast_node
*node
,
1746 __isl_take isl_printer
*p
, __isl_take isl_ast_print_options
*options
)
1748 if (!node
|| !options
)
1750 if (node
->type
!= isl_ast_node_if
)
1751 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1752 "not an if node", goto error
);
1753 p
= print_if_c(p
, node
, options
, 1);
1754 isl_ast_print_options_free(options
);
1757 isl_ast_print_options_free(options
);
1758 isl_printer_free(p
);
1762 /* Print "node" to "p".
1764 __isl_give isl_printer
*isl_ast_node_print(__isl_keep isl_ast_node
*node
,
1765 __isl_take isl_printer
*p
, __isl_take isl_ast_print_options
*options
)
1767 if (!options
|| !node
)
1769 p
= print_ast_node_c(p
, node
, options
, 0, 0);
1770 isl_ast_print_options_free(options
);
1773 isl_ast_print_options_free(options
);
1774 isl_printer_free(p
);
1778 /* Print "node" to "p".
1780 __isl_give isl_printer
*isl_printer_print_ast_node(__isl_take isl_printer
*p
,
1781 __isl_keep isl_ast_node
*node
)
1784 isl_ast_print_options
*options
;
1789 format
= isl_printer_get_output_format(p
);
1791 case ISL_FORMAT_ISL
:
1792 p
= print_ast_node_isl(p
, node
);
1795 options
= isl_ast_print_options_alloc(isl_printer_get_ctx(p
));
1796 p
= isl_ast_node_print(node
, p
, options
);
1799 isl_die(isl_printer_get_ctx(p
), isl_error_unsupported
,
1800 "output format not supported for ast_node",
1801 return isl_printer_free(p
));
1807 /* Print the list of nodes "list" to "p".
1809 __isl_give isl_printer
*isl_ast_node_list_print(
1810 __isl_keep isl_ast_node_list
*list
, __isl_take isl_printer
*p
,
1811 __isl_keep isl_ast_print_options
*options
)
1815 if (!p
|| !list
|| !options
)
1816 return isl_printer_free(p
);
1818 for (i
= 0; i
< list
->n
; ++i
)
1819 p
= print_ast_node_c(p
, list
->p
[i
], options
, 1, 1);
1824 #define ISL_AST_MACRO_FLOORD (1 << 0)
1825 #define ISL_AST_MACRO_MIN (1 << 1)
1826 #define ISL_AST_MACRO_MAX (1 << 2)
1827 #define ISL_AST_MACRO_ALL (ISL_AST_MACRO_FLOORD | \
1828 ISL_AST_MACRO_MIN | \
1831 /* If "expr" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1832 * then set the corresponding bit in "macros".
1834 static int ast_expr_required_macros(__isl_keep isl_ast_expr
*expr
, int macros
)
1838 if (macros
== ISL_AST_MACRO_ALL
)
1841 if (expr
->type
!= isl_ast_expr_op
)
1844 if (expr
->u
.op
.op
== isl_ast_op_min
)
1845 macros
|= ISL_AST_MACRO_MIN
;
1846 if (expr
->u
.op
.op
== isl_ast_op_max
)
1847 macros
|= ISL_AST_MACRO_MAX
;
1848 if (expr
->u
.op
.op
== isl_ast_op_fdiv_q
)
1849 macros
|= ISL_AST_MACRO_FLOORD
;
1851 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
)
1852 macros
= ast_expr_required_macros(expr
->u
.op
.args
[i
], macros
);
1857 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list
*list
,
1860 /* If "node" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1861 * then set the corresponding bit in "macros".
1863 static int ast_node_required_macros(__isl_keep isl_ast_node
*node
, int macros
)
1865 if (macros
== ISL_AST_MACRO_ALL
)
1868 switch (node
->type
) {
1869 case isl_ast_node_for
:
1870 macros
= ast_expr_required_macros(node
->u
.f
.init
, macros
);
1871 if (!node
->u
.f
.degenerate
) {
1872 macros
= ast_expr_required_macros(node
->u
.f
.cond
,
1874 macros
= ast_expr_required_macros(node
->u
.f
.inc
,
1877 macros
= ast_node_required_macros(node
->u
.f
.body
, macros
);
1879 case isl_ast_node_if
:
1880 macros
= ast_expr_required_macros(node
->u
.i
.guard
, macros
);
1881 macros
= ast_node_required_macros(node
->u
.i
.then
, macros
);
1882 if (node
->u
.i
.else_node
)
1883 macros
= ast_node_required_macros(node
->u
.i
.else_node
,
1886 case isl_ast_node_block
:
1887 macros
= ast_node_list_required_macros(node
->u
.b
.children
,
1890 case isl_ast_node_user
:
1891 macros
= ast_expr_required_macros(node
->u
.e
.expr
, macros
);
1893 case isl_ast_node_error
:
1900 /* If "list" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1901 * then set the corresponding bit in "macros".
1903 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list
*list
,
1908 for (i
= 0; i
< list
->n
; ++i
)
1909 macros
= ast_node_required_macros(list
->p
[i
], macros
);
1914 /* Print a macro definition for the operator "type".
1916 __isl_give isl_printer
*isl_ast_op_type_print_macro(
1917 enum isl_ast_op_type type
, __isl_take isl_printer
*p
)
1920 case isl_ast_op_min
:
1921 p
= isl_printer_start_line(p
);
1922 p
= isl_printer_print_str(p
,
1923 "#define min(x,y) ((x) < (y) ? (x) : (y))");
1924 p
= isl_printer_end_line(p
);
1926 case isl_ast_op_max
:
1927 p
= isl_printer_start_line(p
);
1928 p
= isl_printer_print_str(p
,
1929 "#define max(x,y) ((x) > (y) ? (x) : (y))");
1930 p
= isl_printer_end_line(p
);
1932 case isl_ast_op_fdiv_q
:
1933 p
= isl_printer_start_line(p
);
1934 p
= isl_printer_print_str(p
,
1935 "#define floord(n,d) "
1936 "(((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))");
1937 p
= isl_printer_end_line(p
);
1946 /* Call "fn" for each type of operation that appears in "node"
1947 * and that requires a macro definition.
1949 int isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node
*node
,
1950 int (*fn
)(enum isl_ast_op_type type
, void *user
), void *user
)
1957 macros
= ast_node_required_macros(node
, 0);
1959 if (macros
& ISL_AST_MACRO_MIN
&& fn(isl_ast_op_min
, user
) < 0)
1961 if (macros
& ISL_AST_MACRO_MAX
&& fn(isl_ast_op_max
, user
) < 0)
1963 if (macros
& ISL_AST_MACRO_FLOORD
&& fn(isl_ast_op_fdiv_q
, user
) < 0)
1969 static int ast_op_type_print_macro(enum isl_ast_op_type type
, void *user
)
1971 isl_printer
**p
= user
;
1973 *p
= isl_ast_op_type_print_macro(type
, *p
);
1978 /* Print macro definitions for all the macros used in the result
1979 * of printing "node.
1981 __isl_give isl_printer
*isl_ast_node_print_macros(
1982 __isl_keep isl_ast_node
*node
, __isl_take isl_printer
*p
)
1984 if (isl_ast_node_foreach_ast_op_type(node
,
1985 &ast_op_type_print_macro
, &p
) < 0)
1986 return isl_printer_free(p
);