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
)
1105 if (node
->type
!= isl_ast_node_user
)
1106 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1107 "not a user node", return NULL
);
1109 return isl_ast_expr_copy(node
->u
.e
.expr
);
1112 __isl_give isl_id
*isl_ast_node_get_annotation(__isl_keep isl_ast_node
*node
)
1114 return node
? isl_id_copy(node
->annotation
) : NULL
;
1117 /* Replace node->annotation by "annotation".
1119 __isl_give isl_ast_node
*isl_ast_node_set_annotation(
1120 __isl_take isl_ast_node
*node
, __isl_take isl_id
*annotation
)
1122 node
= isl_ast_node_cow(node
);
1123 if (!node
|| !annotation
)
1126 isl_id_free(node
->annotation
);
1127 node
->annotation
= annotation
;
1131 isl_id_free(annotation
);
1132 return isl_ast_node_free(node
);
1135 /* Textual C representation of the various operators.
1137 static char *op_str
[] = {
1138 [isl_ast_op_and
] = "&&",
1139 [isl_ast_op_and_then
] = "&&",
1140 [isl_ast_op_or
] = "||",
1141 [isl_ast_op_or_else
] = "||",
1142 [isl_ast_op_max
] = "max",
1143 [isl_ast_op_min
] = "min",
1144 [isl_ast_op_minus
] = "-",
1145 [isl_ast_op_add
] = "+",
1146 [isl_ast_op_sub
] = "-",
1147 [isl_ast_op_mul
] = "*",
1148 [isl_ast_op_pdiv_q
] = "/",
1149 [isl_ast_op_pdiv_r
] = "%",
1150 [isl_ast_op_div
] = "/",
1151 [isl_ast_op_eq
] = "==",
1152 [isl_ast_op_le
] = "<=",
1153 [isl_ast_op_ge
] = ">=",
1154 [isl_ast_op_lt
] = "<",
1155 [isl_ast_op_gt
] = ">",
1156 [isl_ast_op_member
] = "."
1159 /* Precedence in C of the various operators.
1160 * Based on http://en.wikipedia.org/wiki/Operators_in_C_and_C++
1161 * Lowest value means highest precedence.
1163 static int op_prec
[] = {
1164 [isl_ast_op_and
] = 13,
1165 [isl_ast_op_and_then
] = 13,
1166 [isl_ast_op_or
] = 14,
1167 [isl_ast_op_or_else
] = 14,
1168 [isl_ast_op_max
] = 2,
1169 [isl_ast_op_min
] = 2,
1170 [isl_ast_op_minus
] = 3,
1171 [isl_ast_op_add
] = 6,
1172 [isl_ast_op_sub
] = 6,
1173 [isl_ast_op_mul
] = 5,
1174 [isl_ast_op_div
] = 5,
1175 [isl_ast_op_fdiv_q
] = 2,
1176 [isl_ast_op_pdiv_q
] = 5,
1177 [isl_ast_op_pdiv_r
] = 5,
1178 [isl_ast_op_cond
] = 15,
1179 [isl_ast_op_select
] = 15,
1180 [isl_ast_op_eq
] = 9,
1181 [isl_ast_op_le
] = 8,
1182 [isl_ast_op_ge
] = 8,
1183 [isl_ast_op_lt
] = 8,
1184 [isl_ast_op_gt
] = 8,
1185 [isl_ast_op_call
] = 2,
1186 [isl_ast_op_access
] = 2,
1187 [isl_ast_op_member
] = 2
1190 /* Is the operator left-to-right associative?
1192 static int op_left
[] = {
1193 [isl_ast_op_and
] = 1,
1194 [isl_ast_op_and_then
] = 1,
1195 [isl_ast_op_or
] = 1,
1196 [isl_ast_op_or_else
] = 1,
1197 [isl_ast_op_max
] = 1,
1198 [isl_ast_op_min
] = 1,
1199 [isl_ast_op_minus
] = 0,
1200 [isl_ast_op_add
] = 1,
1201 [isl_ast_op_sub
] = 1,
1202 [isl_ast_op_mul
] = 1,
1203 [isl_ast_op_div
] = 1,
1204 [isl_ast_op_fdiv_q
] = 1,
1205 [isl_ast_op_pdiv_q
] = 1,
1206 [isl_ast_op_pdiv_r
] = 1,
1207 [isl_ast_op_cond
] = 0,
1208 [isl_ast_op_select
] = 0,
1209 [isl_ast_op_eq
] = 1,
1210 [isl_ast_op_le
] = 1,
1211 [isl_ast_op_ge
] = 1,
1212 [isl_ast_op_lt
] = 1,
1213 [isl_ast_op_gt
] = 1,
1214 [isl_ast_op_call
] = 1,
1215 [isl_ast_op_access
] = 1,
1216 [isl_ast_op_member
] = 1
1219 static int is_and(enum isl_ast_op_type op
)
1221 return op
== isl_ast_op_and
|| op
== isl_ast_op_and_then
;
1224 static int is_or(enum isl_ast_op_type op
)
1226 return op
== isl_ast_op_or
|| op
== isl_ast_op_or_else
;
1229 static int is_add_sub(enum isl_ast_op_type op
)
1231 return op
== isl_ast_op_add
|| op
== isl_ast_op_sub
;
1234 static int is_div_mod(enum isl_ast_op_type op
)
1236 return op
== isl_ast_op_div
|| op
== isl_ast_op_pdiv_r
;
1239 /* Do we need/want parentheses around "expr" as a subexpression of
1240 * an "op" operation? If "left" is set, then "expr" is the left-most
1243 * We only need parentheses if "expr" represents an operation.
1245 * If op has a higher precedence than expr->u.op.op, then we need
1247 * If op and expr->u.op.op have the same precedence, but the operations
1248 * are performed in an order that is different from the associativity,
1249 * then we need parentheses.
1251 * An and inside an or technically does not require parentheses,
1252 * but some compilers complain about that, so we add them anyway.
1254 * Computations such as "a / b * c" and "a % b + c" can be somewhat
1255 * difficult to read, so we add parentheses for those as well.
1257 static int sub_expr_need_parens(enum isl_ast_op_type op
,
1258 __isl_keep isl_ast_expr
*expr
, int left
)
1260 if (expr
->type
!= isl_ast_expr_op
)
1263 if (op_prec
[expr
->u
.op
.op
] > op_prec
[op
])
1265 if (op_prec
[expr
->u
.op
.op
] == op_prec
[op
] && left
!= op_left
[op
])
1268 if (is_or(op
) && is_and(expr
->u
.op
.op
))
1270 if (op
== isl_ast_op_mul
&& expr
->u
.op
.op
!= isl_ast_op_mul
&&
1271 op_prec
[expr
->u
.op
.op
] == op_prec
[op
])
1273 if (is_add_sub(op
) && is_div_mod(expr
->u
.op
.op
))
1279 /* Print "expr" as a subexpression of an "op" operation.
1280 * If "left" is set, then "expr" is the left-most operand.
1282 static __isl_give isl_printer
*print_sub_expr(__isl_take isl_printer
*p
,
1283 enum isl_ast_op_type op
, __isl_keep isl_ast_expr
*expr
, int left
)
1287 need_parens
= sub_expr_need_parens(op
, expr
, left
);
1290 p
= isl_printer_print_str(p
, "(");
1291 p
= isl_printer_print_ast_expr(p
, expr
);
1293 p
= isl_printer_print_str(p
, ")");
1297 /* Print a min or max reduction "expr".
1299 static __isl_give isl_printer
*print_min_max(__isl_take isl_printer
*p
,
1300 __isl_keep isl_ast_expr
*expr
)
1304 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1305 p
= isl_printer_print_str(p
, op_str
[expr
->u
.op
.op
]);
1306 p
= isl_printer_print_str(p
, "(");
1308 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1309 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1310 p
= isl_printer_print_str(p
, ", ");
1311 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[i
]);
1312 p
= isl_printer_print_str(p
, ")");
1318 /* Print a function call "expr".
1320 * The first argument represents the function to be called.
1322 static __isl_give isl_printer
*print_call(__isl_take isl_printer
*p
,
1323 __isl_keep isl_ast_expr
*expr
)
1327 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1328 p
= isl_printer_print_str(p
, "(");
1329 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1331 p
= isl_printer_print_str(p
, ", ");
1332 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[i
]);
1334 p
= isl_printer_print_str(p
, ")");
1339 /* Print an array access "expr".
1341 * The first argument represents the array being accessed.
1343 static __isl_give isl_printer
*print_access(__isl_take isl_printer
*p
,
1344 __isl_keep isl_ast_expr
*expr
)
1348 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1349 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1350 p
= isl_printer_print_str(p
, "[");
1351 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[i
]);
1352 p
= isl_printer_print_str(p
, "]");
1358 /* Print "expr" to "p".
1360 * If we are printing in isl format, then we also print an indication
1361 * of the size of the expression (if it was computed).
1363 __isl_give isl_printer
*isl_printer_print_ast_expr(__isl_take isl_printer
*p
,
1364 __isl_keep isl_ast_expr
*expr
)
1369 return isl_printer_free(p
);
1371 switch (expr
->type
) {
1372 case isl_ast_expr_op
:
1373 if (expr
->u
.op
.op
== isl_ast_op_call
) {
1374 p
= print_call(p
, expr
);
1377 if (expr
->u
.op
.op
== isl_ast_op_access
) {
1378 p
= print_access(p
, expr
);
1381 if (expr
->u
.op
.n_arg
== 1) {
1382 p
= isl_printer_print_str(p
, op_str
[expr
->u
.op
.op
]);
1383 p
= print_sub_expr(p
, expr
->u
.op
.op
,
1384 expr
->u
.op
.args
[0], 0);
1387 if (expr
->u
.op
.op
== isl_ast_op_fdiv_q
) {
1388 p
= isl_printer_print_str(p
, "floord(");
1389 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1390 p
= isl_printer_print_str(p
, ", ");
1391 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[1]);
1392 p
= isl_printer_print_str(p
, ")");
1395 if (expr
->u
.op
.op
== isl_ast_op_max
||
1396 expr
->u
.op
.op
== isl_ast_op_min
) {
1397 p
= print_min_max(p
, expr
);
1400 if (expr
->u
.op
.op
== isl_ast_op_cond
||
1401 expr
->u
.op
.op
== isl_ast_op_select
) {
1402 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1403 p
= isl_printer_print_str(p
, " ? ");
1404 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[1]);
1405 p
= isl_printer_print_str(p
, " : ");
1406 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[2]);
1409 if (expr
->u
.op
.n_arg
!= 2)
1410 isl_die(isl_printer_get_ctx(p
), isl_error_internal
,
1411 "operation should have two arguments",
1413 p
= print_sub_expr(p
, expr
->u
.op
.op
, expr
->u
.op
.args
[0], 1);
1414 if (expr
->u
.op
.op
!= isl_ast_op_member
)
1415 p
= isl_printer_print_str(p
, " ");
1416 p
= isl_printer_print_str(p
, op_str
[expr
->u
.op
.op
]);
1417 if (expr
->u
.op
.op
!= isl_ast_op_member
)
1418 p
= isl_printer_print_str(p
, " ");
1419 p
= print_sub_expr(p
, expr
->u
.op
.op
, expr
->u
.op
.args
[1], 0);
1421 case isl_ast_expr_id
:
1422 p
= isl_printer_print_str(p
, isl_id_get_name(expr
->u
.id
));
1424 case isl_ast_expr_int
:
1425 p
= isl_printer_print_val(p
, expr
->u
.v
);
1427 case isl_ast_expr_error
:
1433 isl_printer_free(p
);
1437 /* Print "node" to "p" in "isl format".
1439 static __isl_give isl_printer
*print_ast_node_isl(__isl_take isl_printer
*p
,
1440 __isl_keep isl_ast_node
*node
)
1442 p
= isl_printer_print_str(p
, "(");
1443 switch (node
->type
) {
1444 case isl_ast_node_for
:
1445 if (node
->u
.f
.degenerate
) {
1446 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1448 p
= isl_printer_print_str(p
, "init: ");
1449 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1450 p
= isl_printer_print_str(p
, ", ");
1451 p
= isl_printer_print_str(p
, "cond: ");
1452 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.cond
);
1453 p
= isl_printer_print_str(p
, ", ");
1454 p
= isl_printer_print_str(p
, "inc: ");
1455 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.inc
);
1457 if (node
->u
.f
.body
) {
1458 p
= isl_printer_print_str(p
, ", ");
1459 p
= isl_printer_print_str(p
, "body: ");
1460 p
= isl_printer_print_ast_node(p
, node
->u
.f
.body
);
1463 case isl_ast_node_user
:
1464 p
= isl_printer_print_ast_expr(p
, node
->u
.e
.expr
);
1466 case isl_ast_node_if
:
1467 p
= isl_printer_print_str(p
, "guard: ");
1468 p
= isl_printer_print_ast_expr(p
, node
->u
.i
.guard
);
1469 if (node
->u
.i
.then
) {
1470 p
= isl_printer_print_str(p
, ", ");
1471 p
= isl_printer_print_str(p
, "then: ");
1472 p
= isl_printer_print_ast_node(p
, node
->u
.i
.then
);
1474 if (node
->u
.i
.else_node
) {
1475 p
= isl_printer_print_str(p
, ", ");
1476 p
= isl_printer_print_str(p
, "else: ");
1477 p
= isl_printer_print_ast_node(p
, node
->u
.i
.else_node
);
1480 case isl_ast_node_block
:
1481 p
= isl_printer_print_ast_node_list(p
, node
->u
.b
.children
);
1486 p
= isl_printer_print_str(p
, ")");
1490 /* Do we need to print a block around the body "node" of a for or if node?
1492 * If the node is a block, then we need to print a block.
1493 * Also if the node is a degenerate for then we will print it as
1494 * an assignment followed by the body of the for loop, so we need a block
1496 * If the node is an if node with an else, then we print a block
1497 * to avoid spurious dangling else warnings emitted by some compilers.
1499 static int need_block(__isl_keep isl_ast_node
*node
)
1501 if (node
->type
== isl_ast_node_block
)
1503 if (node
->type
== isl_ast_node_for
&& node
->u
.f
.degenerate
)
1505 if (node
->type
== isl_ast_node_if
&& node
->u
.i
.else_node
)
1510 static __isl_give isl_printer
*print_ast_node_c(__isl_take isl_printer
*p
,
1511 __isl_keep isl_ast_node
*node
,
1512 __isl_keep isl_ast_print_options
*options
, int in_block
, int in_list
);
1513 static __isl_give isl_printer
*print_if_c(__isl_take isl_printer
*p
,
1514 __isl_keep isl_ast_node
*node
,
1515 __isl_keep isl_ast_print_options
*options
, int new_line
);
1517 /* Print the body "node" of a for or if node.
1518 * If "else_node" is set, then it is printed as well.
1520 * We first check if we need to print out a block.
1521 * We always print out a block if there is an else node to make
1522 * sure that the else node is matched to the correct if node.
1524 * If the else node is itself an if, then we print it as
1528 * Otherwise the else node is printed as
1533 static __isl_give isl_printer
*print_body_c(__isl_take isl_printer
*p
,
1534 __isl_keep isl_ast_node
*node
, __isl_keep isl_ast_node
*else_node
,
1535 __isl_keep isl_ast_print_options
*options
)
1538 return isl_printer_free(p
);
1540 if (!else_node
&& !need_block(node
)) {
1541 p
= isl_printer_end_line(p
);
1542 p
= isl_printer_indent(p
, 2);
1543 p
= isl_ast_node_print(node
, p
,
1544 isl_ast_print_options_copy(options
));
1545 p
= isl_printer_indent(p
, -2);
1549 p
= isl_printer_print_str(p
, " {");
1550 p
= isl_printer_end_line(p
);
1551 p
= isl_printer_indent(p
, 2);
1552 p
= print_ast_node_c(p
, node
, options
, 1, 0);
1553 p
= isl_printer_indent(p
, -2);
1554 p
= isl_printer_start_line(p
);
1555 p
= isl_printer_print_str(p
, "}");
1557 if (else_node
->type
== isl_ast_node_if
) {
1558 p
= isl_printer_print_str(p
, " else ");
1559 p
= print_if_c(p
, else_node
, options
, 0);
1561 p
= isl_printer_print_str(p
, " else");
1562 p
= print_body_c(p
, else_node
, NULL
, options
);
1565 p
= isl_printer_end_line(p
);
1570 /* Print the start of a compound statement.
1572 static __isl_give isl_printer
*start_block(__isl_take isl_printer
*p
)
1574 p
= isl_printer_start_line(p
);
1575 p
= isl_printer_print_str(p
, "{");
1576 p
= isl_printer_end_line(p
);
1577 p
= isl_printer_indent(p
, 2);
1582 /* Print the end of a compound statement.
1584 static __isl_give isl_printer
*end_block(__isl_take isl_printer
*p
)
1586 p
= isl_printer_indent(p
, -2);
1587 p
= isl_printer_start_line(p
);
1588 p
= isl_printer_print_str(p
, "}");
1589 p
= isl_printer_end_line(p
);
1594 /* Print the for node "node".
1596 * If the for node is degenerate, it is printed as
1598 * type iterator = init;
1601 * Otherwise, it is printed as
1603 * for (type iterator = init; cond; iterator += inc)
1606 * "in_block" is set if we are currently inside a block.
1607 * "in_list" is set if the current node is not alone in the block.
1608 * If we are not in a block or if the current not is not alone in the block
1609 * then we print a block around a degenerate for loop such that the variable
1610 * declaration will not conflict with any potential other declaration
1611 * of the same variable.
1613 static __isl_give isl_printer
*print_for_c(__isl_take isl_printer
*p
,
1614 __isl_keep isl_ast_node
*node
,
1615 __isl_keep isl_ast_print_options
*options
, int in_block
, int in_list
)
1621 type
= isl_options_get_ast_iterator_type(isl_printer_get_ctx(p
));
1622 if (!node
->u
.f
.degenerate
) {
1623 id
= isl_ast_expr_get_id(node
->u
.f
.iterator
);
1624 name
= isl_id_get_name(id
);
1626 p
= isl_printer_start_line(p
);
1627 p
= isl_printer_print_str(p
, "for (");
1628 p
= isl_printer_print_str(p
, type
);
1629 p
= isl_printer_print_str(p
, " ");
1630 p
= isl_printer_print_str(p
, name
);
1631 p
= isl_printer_print_str(p
, " = ");
1632 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1633 p
= isl_printer_print_str(p
, "; ");
1634 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.cond
);
1635 p
= isl_printer_print_str(p
, "; ");
1636 p
= isl_printer_print_str(p
, name
);
1637 p
= isl_printer_print_str(p
, " += ");
1638 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.inc
);
1639 p
= isl_printer_print_str(p
, ")");
1640 p
= print_body_c(p
, node
->u
.f
.body
, NULL
, options
);
1642 id
= isl_ast_expr_get_id(node
->u
.f
.iterator
);
1643 name
= isl_id_get_name(id
);
1645 if (!in_block
|| in_list
)
1647 p
= isl_printer_start_line(p
);
1648 p
= isl_printer_print_str(p
, type
);
1649 p
= isl_printer_print_str(p
, " ");
1650 p
= isl_printer_print_str(p
, name
);
1651 p
= isl_printer_print_str(p
, " = ");
1652 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1653 p
= isl_printer_print_str(p
, ";");
1654 p
= isl_printer_end_line(p
);
1655 p
= print_ast_node_c(p
, node
->u
.f
.body
, options
, 1, 0);
1656 if (!in_block
|| in_list
)
1663 /* Print the if node "node".
1664 * If "new_line" is set then the if node should be printed on a new line.
1666 static __isl_give isl_printer
*print_if_c(__isl_take isl_printer
*p
,
1667 __isl_keep isl_ast_node
*node
,
1668 __isl_keep isl_ast_print_options
*options
, int new_line
)
1671 p
= isl_printer_start_line(p
);
1672 p
= isl_printer_print_str(p
, "if (");
1673 p
= isl_printer_print_ast_expr(p
, node
->u
.i
.guard
);
1674 p
= isl_printer_print_str(p
, ")");
1675 p
= print_body_c(p
, node
->u
.i
.then
, node
->u
.i
.else_node
, options
);
1680 /* Print the "node" to "p".
1682 * "in_block" is set if we are currently inside a block.
1683 * If so, we do not print a block around the children of a block node.
1684 * We do this to avoid an extra block around the body of a degenerate
1687 * "in_list" is set if the current node is not alone in the block.
1689 static __isl_give isl_printer
*print_ast_node_c(__isl_take isl_printer
*p
,
1690 __isl_keep isl_ast_node
*node
,
1691 __isl_keep isl_ast_print_options
*options
, int in_block
, int in_list
)
1693 switch (node
->type
) {
1694 case isl_ast_node_for
:
1695 if (options
->print_for
)
1696 return options
->print_for(p
,
1697 isl_ast_print_options_copy(options
),
1698 node
, options
->print_for_user
);
1699 p
= print_for_c(p
, node
, options
, in_block
, in_list
);
1701 case isl_ast_node_if
:
1702 p
= print_if_c(p
, node
, options
, 1);
1704 case isl_ast_node_block
:
1707 p
= isl_ast_node_list_print(node
->u
.b
.children
, p
, options
);
1711 case isl_ast_node_user
:
1712 if (options
->print_user
)
1713 return options
->print_user(p
,
1714 isl_ast_print_options_copy(options
),
1715 node
, options
->print_user_user
);
1716 p
= isl_printer_start_line(p
);
1717 p
= isl_printer_print_ast_expr(p
, node
->u
.e
.expr
);
1718 p
= isl_printer_print_str(p
, ";");
1719 p
= isl_printer_end_line(p
);
1721 case isl_ast_node_error
:
1727 /* Print the for node "node" to "p".
1729 __isl_give isl_printer
*isl_ast_node_for_print(__isl_keep isl_ast_node
*node
,
1730 __isl_take isl_printer
*p
, __isl_take isl_ast_print_options
*options
)
1732 if (!node
|| !options
)
1734 if (node
->type
!= isl_ast_node_for
)
1735 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1736 "not a for node", goto error
);
1737 p
= print_for_c(p
, node
, options
, 0, 0);
1738 isl_ast_print_options_free(options
);
1741 isl_ast_print_options_free(options
);
1742 isl_printer_free(p
);
1746 /* Print the if node "node" to "p".
1748 __isl_give isl_printer
*isl_ast_node_if_print(__isl_keep isl_ast_node
*node
,
1749 __isl_take isl_printer
*p
, __isl_take isl_ast_print_options
*options
)
1751 if (!node
|| !options
)
1753 if (node
->type
!= isl_ast_node_if
)
1754 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1755 "not an if node", goto error
);
1756 p
= print_if_c(p
, node
, options
, 1);
1757 isl_ast_print_options_free(options
);
1760 isl_ast_print_options_free(options
);
1761 isl_printer_free(p
);
1765 /* Print "node" to "p".
1767 __isl_give isl_printer
*isl_ast_node_print(__isl_keep isl_ast_node
*node
,
1768 __isl_take isl_printer
*p
, __isl_take isl_ast_print_options
*options
)
1770 if (!options
|| !node
)
1772 p
= print_ast_node_c(p
, node
, options
, 0, 0);
1773 isl_ast_print_options_free(options
);
1776 isl_ast_print_options_free(options
);
1777 isl_printer_free(p
);
1781 /* Print "node" to "p".
1783 __isl_give isl_printer
*isl_printer_print_ast_node(__isl_take isl_printer
*p
,
1784 __isl_keep isl_ast_node
*node
)
1787 isl_ast_print_options
*options
;
1792 format
= isl_printer_get_output_format(p
);
1794 case ISL_FORMAT_ISL
:
1795 p
= print_ast_node_isl(p
, node
);
1798 options
= isl_ast_print_options_alloc(isl_printer_get_ctx(p
));
1799 p
= isl_ast_node_print(node
, p
, options
);
1802 isl_die(isl_printer_get_ctx(p
), isl_error_unsupported
,
1803 "output format not supported for ast_node",
1804 return isl_printer_free(p
));
1810 /* Print the list of nodes "list" to "p".
1812 __isl_give isl_printer
*isl_ast_node_list_print(
1813 __isl_keep isl_ast_node_list
*list
, __isl_take isl_printer
*p
,
1814 __isl_keep isl_ast_print_options
*options
)
1818 if (!p
|| !list
|| !options
)
1819 return isl_printer_free(p
);
1821 for (i
= 0; i
< list
->n
; ++i
)
1822 p
= print_ast_node_c(p
, list
->p
[i
], options
, 1, 1);
1827 #define ISL_AST_MACRO_FLOORD (1 << 0)
1828 #define ISL_AST_MACRO_MIN (1 << 1)
1829 #define ISL_AST_MACRO_MAX (1 << 2)
1830 #define ISL_AST_MACRO_ALL (ISL_AST_MACRO_FLOORD | \
1831 ISL_AST_MACRO_MIN | \
1834 /* If "expr" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1835 * then set the corresponding bit in "macros".
1837 static int ast_expr_required_macros(__isl_keep isl_ast_expr
*expr
, int macros
)
1841 if (macros
== ISL_AST_MACRO_ALL
)
1844 if (expr
->type
!= isl_ast_expr_op
)
1847 if (expr
->u
.op
.op
== isl_ast_op_min
)
1848 macros
|= ISL_AST_MACRO_MIN
;
1849 if (expr
->u
.op
.op
== isl_ast_op_max
)
1850 macros
|= ISL_AST_MACRO_MAX
;
1851 if (expr
->u
.op
.op
== isl_ast_op_fdiv_q
)
1852 macros
|= ISL_AST_MACRO_FLOORD
;
1854 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
)
1855 macros
= ast_expr_required_macros(expr
->u
.op
.args
[i
], macros
);
1860 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list
*list
,
1863 /* If "node" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1864 * then set the corresponding bit in "macros".
1866 static int ast_node_required_macros(__isl_keep isl_ast_node
*node
, int macros
)
1868 if (macros
== ISL_AST_MACRO_ALL
)
1871 switch (node
->type
) {
1872 case isl_ast_node_for
:
1873 macros
= ast_expr_required_macros(node
->u
.f
.init
, macros
);
1874 if (!node
->u
.f
.degenerate
) {
1875 macros
= ast_expr_required_macros(node
->u
.f
.cond
,
1877 macros
= ast_expr_required_macros(node
->u
.f
.inc
,
1880 macros
= ast_node_required_macros(node
->u
.f
.body
, macros
);
1882 case isl_ast_node_if
:
1883 macros
= ast_expr_required_macros(node
->u
.i
.guard
, macros
);
1884 macros
= ast_node_required_macros(node
->u
.i
.then
, macros
);
1885 if (node
->u
.i
.else_node
)
1886 macros
= ast_node_required_macros(node
->u
.i
.else_node
,
1889 case isl_ast_node_block
:
1890 macros
= ast_node_list_required_macros(node
->u
.b
.children
,
1893 case isl_ast_node_user
:
1894 macros
= ast_expr_required_macros(node
->u
.e
.expr
, macros
);
1896 case isl_ast_node_error
:
1903 /* If "list" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1904 * then set the corresponding bit in "macros".
1906 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list
*list
,
1911 for (i
= 0; i
< list
->n
; ++i
)
1912 macros
= ast_node_required_macros(list
->p
[i
], macros
);
1917 /* Print a macro definition for the operator "type".
1919 __isl_give isl_printer
*isl_ast_op_type_print_macro(
1920 enum isl_ast_op_type type
, __isl_take isl_printer
*p
)
1923 case isl_ast_op_min
:
1924 p
= isl_printer_start_line(p
);
1925 p
= isl_printer_print_str(p
,
1926 "#define min(x,y) ((x) < (y) ? (x) : (y))");
1927 p
= isl_printer_end_line(p
);
1929 case isl_ast_op_max
:
1930 p
= isl_printer_start_line(p
);
1931 p
= isl_printer_print_str(p
,
1932 "#define max(x,y) ((x) > (y) ? (x) : (y))");
1933 p
= isl_printer_end_line(p
);
1935 case isl_ast_op_fdiv_q
:
1936 p
= isl_printer_start_line(p
);
1937 p
= isl_printer_print_str(p
,
1938 "#define floord(n,d) "
1939 "(((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))");
1940 p
= isl_printer_end_line(p
);
1949 /* Call "fn" for each type of operation that appears in "node"
1950 * and that requires a macro definition.
1952 int isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node
*node
,
1953 int (*fn
)(enum isl_ast_op_type type
, void *user
), void *user
)
1960 macros
= ast_node_required_macros(node
, 0);
1962 if (macros
& ISL_AST_MACRO_MIN
&& fn(isl_ast_op_min
, user
) < 0)
1964 if (macros
& ISL_AST_MACRO_MAX
&& fn(isl_ast_op_max
, user
) < 0)
1966 if (macros
& ISL_AST_MACRO_FLOORD
&& fn(isl_ast_op_fdiv_q
, user
) < 0)
1972 static int ast_op_type_print_macro(enum isl_ast_op_type type
, void *user
)
1974 isl_printer
**p
= user
;
1976 *p
= isl_ast_op_type_print_macro(type
, *p
);
1981 /* Print macro definitions for all the macros used in the result
1982 * of printing "node.
1984 __isl_give isl_printer
*isl_ast_node_print_macros(
1985 __isl_keep isl_ast_node
*node
, __isl_take isl_printer
*p
)
1987 if (isl_ast_node_foreach_ast_op_type(node
,
1988 &ast_op_type_print_macro
, &p
) < 0)
1989 return isl_printer_free(p
);