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
:
221 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
)
222 isl_ast_expr_free(expr
->u
.op
.args
[i
]);
223 free(expr
->u
.op
.args
);
225 case isl_ast_expr_error
:
233 isl_ctx
*isl_ast_expr_get_ctx(__isl_keep isl_ast_expr
*expr
)
235 return expr
? expr
->ctx
: NULL
;
238 enum isl_ast_expr_type
isl_ast_expr_get_type(__isl_keep isl_ast_expr
*expr
)
240 return expr
? expr
->type
: isl_ast_expr_error
;
243 /* Return the integer value represented by "expr".
245 __isl_give isl_val
*isl_ast_expr_get_val(__isl_keep isl_ast_expr
*expr
)
249 if (expr
->type
!= isl_ast_expr_int
)
250 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
251 "expression not an int", return NULL
);
252 return isl_val_copy(expr
->u
.v
);
255 __isl_give isl_id
*isl_ast_expr_get_id(__isl_keep isl_ast_expr
*expr
)
259 if (expr
->type
!= isl_ast_expr_id
)
260 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
261 "expression not an identifier", return NULL
);
263 return isl_id_copy(expr
->u
.id
);
266 enum isl_ast_op_type
isl_ast_expr_get_op_type(__isl_keep isl_ast_expr
*expr
)
269 return isl_ast_op_error
;
270 if (expr
->type
!= isl_ast_expr_op
)
271 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
272 "expression not an operation", return isl_ast_op_error
);
273 return expr
->u
.op
.op
;
276 int isl_ast_expr_get_op_n_arg(__isl_keep isl_ast_expr
*expr
)
280 if (expr
->type
!= isl_ast_expr_op
)
281 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
282 "expression not an operation", return -1);
283 return expr
->u
.op
.n_arg
;
286 __isl_give isl_ast_expr
*isl_ast_expr_get_op_arg(__isl_keep isl_ast_expr
*expr
,
291 if (expr
->type
!= isl_ast_expr_op
)
292 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
293 "expression not an operation", return NULL
);
294 if (pos
< 0 || pos
>= expr
->u
.op
.n_arg
)
295 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
296 "index out of bounds", return NULL
);
298 return isl_ast_expr_copy(expr
->u
.op
.args
[pos
]);
301 /* Replace the argument at position "pos" of "expr" by "arg".
303 __isl_give isl_ast_expr
*isl_ast_expr_set_op_arg(__isl_take isl_ast_expr
*expr
,
304 int pos
, __isl_take isl_ast_expr
*arg
)
306 expr
= isl_ast_expr_cow(expr
);
309 if (expr
->type
!= isl_ast_expr_op
)
310 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
311 "expression not an operation", goto error
);
312 if (pos
< 0 || pos
>= expr
->u
.op
.n_arg
)
313 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
314 "index out of bounds", goto error
);
316 isl_ast_expr_free(expr
->u
.op
.args
[pos
]);
317 expr
->u
.op
.args
[pos
] = arg
;
321 isl_ast_expr_free(arg
);
322 return isl_ast_expr_free(expr
);
325 /* Is "expr1" equal to "expr2"?
327 int isl_ast_expr_is_equal(__isl_keep isl_ast_expr
*expr1
,
328 __isl_keep isl_ast_expr
*expr2
)
332 if (!expr1
|| !expr2
)
337 if (expr1
->type
!= expr2
->type
)
339 switch (expr1
->type
) {
340 case isl_ast_expr_int
:
341 return isl_val_eq(expr1
->u
.v
, expr2
->u
.v
);
342 case isl_ast_expr_id
:
343 return expr1
->u
.id
== expr2
->u
.id
;
344 case isl_ast_expr_op
:
345 if (expr1
->u
.op
.op
!= expr2
->u
.op
.op
)
347 if (expr1
->u
.op
.n_arg
!= expr2
->u
.op
.n_arg
)
349 for (i
= 0; i
< expr1
->u
.op
.n_arg
; ++i
) {
351 equal
= isl_ast_expr_is_equal(expr1
->u
.op
.args
[i
],
352 expr2
->u
.op
.args
[i
]);
354 if (equal
< 0 || !equal
)
358 case isl_ast_expr_error
:
363 /* Create a new operation expression of operation type "op",
364 * with "n_arg" as yet unspecified arguments.
366 __isl_give isl_ast_expr
*isl_ast_expr_alloc_op(isl_ctx
*ctx
,
367 enum isl_ast_op_type op
, int n_arg
)
371 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
378 expr
->type
= isl_ast_expr_op
;
380 expr
->u
.op
.n_arg
= n_arg
;
381 expr
->u
.op
.args
= isl_calloc_array(ctx
, isl_ast_expr
*, n_arg
);
383 if (n_arg
&& !expr
->u
.op
.args
)
384 return isl_ast_expr_free(expr
);
389 /* Create a new id expression representing "id".
391 __isl_give isl_ast_expr
*isl_ast_expr_from_id(__isl_take isl_id
*id
)
399 ctx
= isl_id_get_ctx(id
);
400 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
407 expr
->type
= isl_ast_expr_id
;
416 /* Create a new integer expression representing "i".
418 __isl_give isl_ast_expr
*isl_ast_expr_alloc_int_si(isl_ctx
*ctx
, int i
)
422 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
429 expr
->type
= isl_ast_expr_int
;
430 expr
->u
.v
= isl_val_int_from_si(ctx
, i
);
432 return isl_ast_expr_free(expr
);
437 /* Create a new integer expression representing "v".
439 __isl_give isl_ast_expr
*isl_ast_expr_from_val(__isl_take isl_val
*v
)
446 if (!isl_val_is_int(v
))
447 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
448 "expecting integer value", goto error
);
450 ctx
= isl_val_get_ctx(v
);
451 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
458 expr
->type
= isl_ast_expr_int
;
467 /* Create an expression representing the negation of "arg".
469 __isl_give isl_ast_expr
*isl_ast_expr_neg(__isl_take isl_ast_expr
*arg
)
472 isl_ast_expr
*expr
= NULL
;
477 ctx
= isl_ast_expr_get_ctx(arg
);
478 expr
= isl_ast_expr_alloc_op(ctx
, isl_ast_op_minus
, 1);
482 expr
->u
.op
.args
[0] = arg
;
486 isl_ast_expr_free(arg
);
490 /* Create an expression representing the binary operation "type"
491 * applied to "expr1" and "expr2".
493 __isl_give isl_ast_expr
*isl_ast_expr_alloc_binary(enum isl_ast_op_type type
,
494 __isl_take isl_ast_expr
*expr1
, __isl_take isl_ast_expr
*expr2
)
497 isl_ast_expr
*expr
= NULL
;
499 if (!expr1
|| !expr2
)
502 ctx
= isl_ast_expr_get_ctx(expr1
);
503 expr
= isl_ast_expr_alloc_op(ctx
, type
, 2);
507 expr
->u
.op
.args
[0] = expr1
;
508 expr
->u
.op
.args
[1] = expr2
;
512 isl_ast_expr_free(expr1
);
513 isl_ast_expr_free(expr2
);
517 /* Create an expression representing the sum of "expr1" and "expr2".
519 __isl_give isl_ast_expr
*isl_ast_expr_add(__isl_take isl_ast_expr
*expr1
,
520 __isl_take isl_ast_expr
*expr2
)
522 return isl_ast_expr_alloc_binary(isl_ast_op_add
, expr1
, expr2
);
525 /* Create an expression representing the difference of "expr1" and "expr2".
527 __isl_give isl_ast_expr
*isl_ast_expr_sub(__isl_take isl_ast_expr
*expr1
,
528 __isl_take isl_ast_expr
*expr2
)
530 return isl_ast_expr_alloc_binary(isl_ast_op_sub
, expr1
, expr2
);
533 /* Create an expression representing the product of "expr1" and "expr2".
535 __isl_give isl_ast_expr
*isl_ast_expr_mul(__isl_take isl_ast_expr
*expr1
,
536 __isl_take isl_ast_expr
*expr2
)
538 return isl_ast_expr_alloc_binary(isl_ast_op_mul
, expr1
, expr2
);
541 /* Create an expression representing the quotient of "expr1" and "expr2".
543 __isl_give isl_ast_expr
*isl_ast_expr_div(__isl_take isl_ast_expr
*expr1
,
544 __isl_take isl_ast_expr
*expr2
)
546 return isl_ast_expr_alloc_binary(isl_ast_op_div
, expr1
, expr2
);
549 /* Create an expression representing the conjunction of "expr1" and "expr2".
551 __isl_give isl_ast_expr
*isl_ast_expr_and(__isl_take isl_ast_expr
*expr1
,
552 __isl_take isl_ast_expr
*expr2
)
554 return isl_ast_expr_alloc_binary(isl_ast_op_and
, expr1
, expr2
);
557 /* Create an expression representing the disjunction of "expr1" and "expr2".
559 __isl_give isl_ast_expr
*isl_ast_expr_or(__isl_take isl_ast_expr
*expr1
,
560 __isl_take isl_ast_expr
*expr2
)
562 return isl_ast_expr_alloc_binary(isl_ast_op_or
, expr1
, expr2
);
565 /* Create an expression representing an access to "array" with index
566 * expressions "indices".
568 __isl_give isl_ast_expr
*isl_ast_expr_access(__isl_take isl_ast_expr
*array
,
569 __isl_take isl_ast_expr_list
*indices
)
573 isl_ast_expr
*access
= NULL
;
575 if (!array
|| !indices
)
578 ctx
= isl_ast_expr_get_ctx(array
);
579 n
= isl_ast_expr_list_n_ast_expr(indices
);
580 access
= isl_ast_expr_alloc_op(ctx
, isl_ast_op_access
, 1 + n
);
583 for (i
= 0; i
< n
; ++i
) {
585 index
= isl_ast_expr_list_get_ast_expr(indices
, i
);
586 access
->u
.op
.args
[1 + i
] = index
;
590 access
->u
.op
.args
[0] = array
;
592 isl_ast_expr_list_free(indices
);
595 isl_ast_expr_free(array
);
596 isl_ast_expr_list_free(indices
);
597 isl_ast_expr_free(access
);
601 /* For each subexpression of "expr" of type isl_ast_expr_id,
602 * if it appears in "id2expr", then replace it by the corresponding
605 __isl_give isl_ast_expr
*isl_ast_expr_substitute_ids(
606 __isl_take isl_ast_expr
*expr
, __isl_take isl_id_to_ast_expr
*id2expr
)
611 if (!expr
|| !id2expr
)
614 switch (expr
->type
) {
615 case isl_ast_expr_int
:
617 case isl_ast_expr_id
:
618 if (!isl_id_to_ast_expr_has(id2expr
, expr
->u
.id
))
620 id
= isl_id_copy(expr
->u
.id
);
621 isl_ast_expr_free(expr
);
622 expr
= isl_id_to_ast_expr_get(id2expr
, id
);
624 case isl_ast_expr_op
:
625 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
) {
627 arg
= isl_ast_expr_copy(expr
->u
.op
.args
[i
]);
628 arg
= isl_ast_expr_substitute_ids(arg
,
629 isl_id_to_ast_expr_copy(id2expr
));
630 if (arg
== expr
->u
.op
.args
[i
]) {
631 isl_ast_expr_free(arg
);
635 expr
= isl_ast_expr_free(expr
);
636 expr
= isl_ast_expr_cow(expr
);
638 isl_ast_expr_free(arg
);
641 isl_ast_expr_free(expr
->u
.op
.args
[i
]);
642 expr
->u
.op
.args
[i
] = arg
;
645 case isl_ast_expr_error
:
646 expr
= isl_ast_expr_free(expr
);
650 isl_id_to_ast_expr_free(id2expr
);
653 isl_ast_expr_free(expr
);
654 isl_id_to_ast_expr_free(id2expr
);
658 isl_ctx
*isl_ast_node_get_ctx(__isl_keep isl_ast_node
*node
)
660 return node
? node
->ctx
: NULL
;
663 enum isl_ast_node_type
isl_ast_node_get_type(__isl_keep isl_ast_node
*node
)
665 return node
? node
->type
: isl_ast_node_error
;
668 __isl_give isl_ast_node
*isl_ast_node_alloc(isl_ctx
*ctx
,
669 enum isl_ast_node_type type
)
673 node
= isl_calloc_type(ctx
, isl_ast_node
);
685 /* Create an if node with the given guard.
687 * The then body needs to be filled in later.
689 __isl_give isl_ast_node
*isl_ast_node_alloc_if(__isl_take isl_ast_expr
*guard
)
696 node
= isl_ast_node_alloc(isl_ast_expr_get_ctx(guard
), isl_ast_node_if
);
699 node
->u
.i
.guard
= guard
;
703 isl_ast_expr_free(guard
);
707 /* Create a for node with the given iterator.
709 * The remaining fields need to be filled in later.
711 __isl_give isl_ast_node
*isl_ast_node_alloc_for(__isl_take isl_id
*id
)
719 ctx
= isl_id_get_ctx(id
);
720 node
= isl_ast_node_alloc(ctx
, isl_ast_node_for
);
724 node
->u
.f
.iterator
= isl_ast_expr_from_id(id
);
725 if (!node
->u
.f
.iterator
)
726 return isl_ast_node_free(node
);
731 /* Create a user node evaluating "expr".
733 __isl_give isl_ast_node
*isl_ast_node_alloc_user(__isl_take isl_ast_expr
*expr
)
741 ctx
= isl_ast_expr_get_ctx(expr
);
742 node
= isl_ast_node_alloc(ctx
, isl_ast_node_user
);
746 node
->u
.e
.expr
= expr
;
750 isl_ast_expr_free(expr
);
754 /* Create a block node with the given children.
756 __isl_give isl_ast_node
*isl_ast_node_alloc_block(
757 __isl_take isl_ast_node_list
*list
)
765 ctx
= isl_ast_node_list_get_ctx(list
);
766 node
= isl_ast_node_alloc(ctx
, isl_ast_node_block
);
770 node
->u
.b
.children
= list
;
774 isl_ast_node_list_free(list
);
778 /* Represent the given list of nodes as a single node, either by
779 * extract the node from a single element list or by creating
780 * a block node with the list of nodes as children.
782 __isl_give isl_ast_node
*isl_ast_node_from_ast_node_list(
783 __isl_take isl_ast_node_list
*list
)
787 if (isl_ast_node_list_n_ast_node(list
) != 1)
788 return isl_ast_node_alloc_block(list
);
790 node
= isl_ast_node_list_get_ast_node(list
, 0);
791 isl_ast_node_list_free(list
);
796 __isl_give isl_ast_node
*isl_ast_node_copy(__isl_keep isl_ast_node
*node
)
805 __isl_give isl_ast_node
*isl_ast_node_dup(__isl_keep isl_ast_node
*node
)
812 dup
= isl_ast_node_alloc(isl_ast_node_get_ctx(node
), node
->type
);
816 switch (node
->type
) {
817 case isl_ast_node_if
:
818 dup
->u
.i
.guard
= isl_ast_expr_copy(node
->u
.i
.guard
);
819 dup
->u
.i
.then
= isl_ast_node_copy(node
->u
.i
.then
);
820 dup
->u
.i
.else_node
= isl_ast_node_copy(node
->u
.i
.else_node
);
821 if (!dup
->u
.i
.guard
|| !dup
->u
.i
.then
||
822 (node
->u
.i
.else_node
&& !dup
->u
.i
.else_node
))
823 return isl_ast_node_free(dup
);
825 case isl_ast_node_for
:
826 dup
->u
.f
.iterator
= isl_ast_expr_copy(node
->u
.f
.iterator
);
827 dup
->u
.f
.init
= isl_ast_expr_copy(node
->u
.f
.init
);
828 dup
->u
.f
.cond
= isl_ast_expr_copy(node
->u
.f
.cond
);
829 dup
->u
.f
.inc
= isl_ast_expr_copy(node
->u
.f
.inc
);
830 dup
->u
.f
.body
= isl_ast_node_copy(node
->u
.f
.body
);
831 if (!dup
->u
.f
.iterator
|| !dup
->u
.f
.init
|| !dup
->u
.f
.cond
||
832 !dup
->u
.f
.inc
|| !dup
->u
.f
.body
)
833 return isl_ast_node_free(dup
);
835 case isl_ast_node_block
:
836 dup
->u
.b
.children
= isl_ast_node_list_copy(node
->u
.b
.children
);
837 if (!dup
->u
.b
.children
)
838 return isl_ast_node_free(dup
);
840 case isl_ast_node_user
:
841 dup
->u
.e
.expr
= isl_ast_expr_copy(node
->u
.e
.expr
);
843 return isl_ast_node_free(dup
);
845 case isl_ast_node_error
:
852 __isl_give isl_ast_node
*isl_ast_node_cow(__isl_take isl_ast_node
*node
)
860 return isl_ast_node_dup(node
);
863 __isl_null isl_ast_node
*isl_ast_node_free(__isl_take isl_ast_node
*node
)
871 switch (node
->type
) {
872 case isl_ast_node_if
:
873 isl_ast_expr_free(node
->u
.i
.guard
);
874 isl_ast_node_free(node
->u
.i
.then
);
875 isl_ast_node_free(node
->u
.i
.else_node
);
877 case isl_ast_node_for
:
878 isl_ast_expr_free(node
->u
.f
.iterator
);
879 isl_ast_expr_free(node
->u
.f
.init
);
880 isl_ast_expr_free(node
->u
.f
.cond
);
881 isl_ast_expr_free(node
->u
.f
.inc
);
882 isl_ast_node_free(node
->u
.f
.body
);
884 case isl_ast_node_block
:
885 isl_ast_node_list_free(node
->u
.b
.children
);
887 case isl_ast_node_user
:
888 isl_ast_expr_free(node
->u
.e
.expr
);
890 case isl_ast_node_error
:
894 isl_id_free(node
->annotation
);
895 isl_ctx_deref(node
->ctx
);
901 /* Replace the body of the for node "node" by "body".
903 __isl_give isl_ast_node
*isl_ast_node_for_set_body(
904 __isl_take isl_ast_node
*node
, __isl_take isl_ast_node
*body
)
906 node
= isl_ast_node_cow(node
);
909 if (node
->type
!= isl_ast_node_for
)
910 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
911 "not a for node", goto error
);
913 isl_ast_node_free(node
->u
.f
.body
);
914 node
->u
.f
.body
= body
;
918 isl_ast_node_free(node
);
919 isl_ast_node_free(body
);
923 __isl_give isl_ast_node
*isl_ast_node_for_get_body(
924 __isl_keep isl_ast_node
*node
)
928 if (node
->type
!= isl_ast_node_for
)
929 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
930 "not a for node", return NULL
);
931 return isl_ast_node_copy(node
->u
.f
.body
);
934 /* Mark the given for node as being degenerate.
936 __isl_give isl_ast_node
*isl_ast_node_for_mark_degenerate(
937 __isl_take isl_ast_node
*node
)
939 node
= isl_ast_node_cow(node
);
942 node
->u
.f
.degenerate
= 1;
946 int isl_ast_node_for_is_degenerate(__isl_keep isl_ast_node
*node
)
950 if (node
->type
!= isl_ast_node_for
)
951 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
952 "not a for node", return -1);
953 return node
->u
.f
.degenerate
;
956 __isl_give isl_ast_expr
*isl_ast_node_for_get_iterator(
957 __isl_keep isl_ast_node
*node
)
961 if (node
->type
!= isl_ast_node_for
)
962 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
963 "not a for node", return NULL
);
964 return isl_ast_expr_copy(node
->u
.f
.iterator
);
967 __isl_give isl_ast_expr
*isl_ast_node_for_get_init(
968 __isl_keep isl_ast_node
*node
)
972 if (node
->type
!= isl_ast_node_for
)
973 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
974 "not a for node", return NULL
);
975 return isl_ast_expr_copy(node
->u
.f
.init
);
978 /* Return the condition expression of the given for node.
980 * If the for node is degenerate, then the condition is not explicitly
981 * stored in the node. Instead, it is constructed as
985 __isl_give isl_ast_expr
*isl_ast_node_for_get_cond(
986 __isl_keep isl_ast_node
*node
)
990 if (node
->type
!= isl_ast_node_for
)
991 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
992 "not a for node", return NULL
);
993 if (!node
->u
.f
.degenerate
)
994 return isl_ast_expr_copy(node
->u
.f
.cond
);
996 return isl_ast_expr_alloc_binary(isl_ast_op_le
,
997 isl_ast_expr_copy(node
->u
.f
.iterator
),
998 isl_ast_expr_copy(node
->u
.f
.init
));
1001 /* Return the increment of the given for node.
1003 * If the for node is degenerate, then the increment is not explicitly
1004 * stored in the node. We simply return "1".
1006 __isl_give isl_ast_expr
*isl_ast_node_for_get_inc(
1007 __isl_keep isl_ast_node
*node
)
1011 if (node
->type
!= isl_ast_node_for
)
1012 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1013 "not a for node", return NULL
);
1014 if (!node
->u
.f
.degenerate
)
1015 return isl_ast_expr_copy(node
->u
.f
.inc
);
1016 return isl_ast_expr_alloc_int_si(isl_ast_node_get_ctx(node
), 1);
1019 /* Replace the then branch of the if node "node" by "child".
1021 __isl_give isl_ast_node
*isl_ast_node_if_set_then(
1022 __isl_take isl_ast_node
*node
, __isl_take isl_ast_node
*child
)
1024 node
= isl_ast_node_cow(node
);
1025 if (!node
|| !child
)
1027 if (node
->type
!= isl_ast_node_if
)
1028 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1029 "not an if node", goto error
);
1031 isl_ast_node_free(node
->u
.i
.then
);
1032 node
->u
.i
.then
= child
;
1036 isl_ast_node_free(node
);
1037 isl_ast_node_free(child
);
1041 __isl_give isl_ast_node
*isl_ast_node_if_get_then(
1042 __isl_keep isl_ast_node
*node
)
1046 if (node
->type
!= isl_ast_node_if
)
1047 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1048 "not an if node", return NULL
);
1049 return isl_ast_node_copy(node
->u
.i
.then
);
1052 int isl_ast_node_if_has_else(
1053 __isl_keep isl_ast_node
*node
)
1057 if (node
->type
!= isl_ast_node_if
)
1058 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1059 "not an if node", return -1);
1060 return node
->u
.i
.else_node
!= NULL
;
1063 __isl_give isl_ast_node
*isl_ast_node_if_get_else(
1064 __isl_keep isl_ast_node
*node
)
1068 if (node
->type
!= isl_ast_node_if
)
1069 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1070 "not an if node", return NULL
);
1071 return isl_ast_node_copy(node
->u
.i
.else_node
);
1074 __isl_give isl_ast_expr
*isl_ast_node_if_get_cond(
1075 __isl_keep isl_ast_node
*node
)
1079 if (node
->type
!= isl_ast_node_if
)
1080 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1081 "not a guard node", return NULL
);
1082 return isl_ast_expr_copy(node
->u
.i
.guard
);
1085 __isl_give isl_ast_node_list
*isl_ast_node_block_get_children(
1086 __isl_keep isl_ast_node
*node
)
1090 if (node
->type
!= isl_ast_node_block
)
1091 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1092 "not a block node", return NULL
);
1093 return isl_ast_node_list_copy(node
->u
.b
.children
);
1096 __isl_give isl_ast_expr
*isl_ast_node_user_get_expr(
1097 __isl_keep isl_ast_node
*node
)
1102 return isl_ast_expr_copy(node
->u
.e
.expr
);
1105 __isl_give isl_id
*isl_ast_node_get_annotation(__isl_keep isl_ast_node
*node
)
1107 return node
? isl_id_copy(node
->annotation
) : NULL
;
1110 /* Replace node->annotation by "annotation".
1112 __isl_give isl_ast_node
*isl_ast_node_set_annotation(
1113 __isl_take isl_ast_node
*node
, __isl_take isl_id
*annotation
)
1115 node
= isl_ast_node_cow(node
);
1116 if (!node
|| !annotation
)
1119 isl_id_free(node
->annotation
);
1120 node
->annotation
= annotation
;
1124 isl_id_free(annotation
);
1125 return isl_ast_node_free(node
);
1128 /* Textual C representation of the various operators.
1130 static char *op_str
[] = {
1131 [isl_ast_op_and
] = "&&",
1132 [isl_ast_op_and_then
] = "&&",
1133 [isl_ast_op_or
] = "||",
1134 [isl_ast_op_or_else
] = "||",
1135 [isl_ast_op_max
] = "max",
1136 [isl_ast_op_min
] = "min",
1137 [isl_ast_op_minus
] = "-",
1138 [isl_ast_op_add
] = "+",
1139 [isl_ast_op_sub
] = "-",
1140 [isl_ast_op_mul
] = "*",
1141 [isl_ast_op_pdiv_q
] = "/",
1142 [isl_ast_op_pdiv_r
] = "%",
1143 [isl_ast_op_div
] = "/",
1144 [isl_ast_op_eq
] = "==",
1145 [isl_ast_op_le
] = "<=",
1146 [isl_ast_op_ge
] = ">=",
1147 [isl_ast_op_lt
] = "<",
1148 [isl_ast_op_gt
] = ">",
1149 [isl_ast_op_member
] = "."
1152 /* Precedence in C of the various operators.
1153 * Based on http://en.wikipedia.org/wiki/Operators_in_C_and_C++
1154 * Lowest value means highest precedence.
1156 static int op_prec
[] = {
1157 [isl_ast_op_and
] = 13,
1158 [isl_ast_op_and_then
] = 13,
1159 [isl_ast_op_or
] = 14,
1160 [isl_ast_op_or_else
] = 14,
1161 [isl_ast_op_max
] = 2,
1162 [isl_ast_op_min
] = 2,
1163 [isl_ast_op_minus
] = 3,
1164 [isl_ast_op_add
] = 6,
1165 [isl_ast_op_sub
] = 6,
1166 [isl_ast_op_mul
] = 5,
1167 [isl_ast_op_div
] = 5,
1168 [isl_ast_op_fdiv_q
] = 2,
1169 [isl_ast_op_pdiv_q
] = 5,
1170 [isl_ast_op_pdiv_r
] = 5,
1171 [isl_ast_op_cond
] = 15,
1172 [isl_ast_op_select
] = 15,
1173 [isl_ast_op_eq
] = 9,
1174 [isl_ast_op_le
] = 8,
1175 [isl_ast_op_ge
] = 8,
1176 [isl_ast_op_lt
] = 8,
1177 [isl_ast_op_gt
] = 8,
1178 [isl_ast_op_call
] = 2,
1179 [isl_ast_op_access
] = 2,
1180 [isl_ast_op_member
] = 2
1183 /* Is the operator left-to-right associative?
1185 static int op_left
[] = {
1186 [isl_ast_op_and
] = 1,
1187 [isl_ast_op_and_then
] = 1,
1188 [isl_ast_op_or
] = 1,
1189 [isl_ast_op_or_else
] = 1,
1190 [isl_ast_op_max
] = 1,
1191 [isl_ast_op_min
] = 1,
1192 [isl_ast_op_minus
] = 0,
1193 [isl_ast_op_add
] = 1,
1194 [isl_ast_op_sub
] = 1,
1195 [isl_ast_op_mul
] = 1,
1196 [isl_ast_op_div
] = 1,
1197 [isl_ast_op_fdiv_q
] = 1,
1198 [isl_ast_op_pdiv_q
] = 1,
1199 [isl_ast_op_pdiv_r
] = 1,
1200 [isl_ast_op_cond
] = 0,
1201 [isl_ast_op_select
] = 0,
1202 [isl_ast_op_eq
] = 1,
1203 [isl_ast_op_le
] = 1,
1204 [isl_ast_op_ge
] = 1,
1205 [isl_ast_op_lt
] = 1,
1206 [isl_ast_op_gt
] = 1,
1207 [isl_ast_op_call
] = 1,
1208 [isl_ast_op_access
] = 1,
1209 [isl_ast_op_member
] = 1
1212 static int is_and(enum isl_ast_op_type op
)
1214 return op
== isl_ast_op_and
|| op
== isl_ast_op_and_then
;
1217 static int is_or(enum isl_ast_op_type op
)
1219 return op
== isl_ast_op_or
|| op
== isl_ast_op_or_else
;
1222 static int is_add_sub(enum isl_ast_op_type op
)
1224 return op
== isl_ast_op_add
|| op
== isl_ast_op_sub
;
1227 static int is_div_mod(enum isl_ast_op_type op
)
1229 return op
== isl_ast_op_div
|| op
== isl_ast_op_pdiv_r
;
1232 /* Do we need/want parentheses around "expr" as a subexpression of
1233 * an "op" operation? If "left" is set, then "expr" is the left-most
1236 * We only need parentheses if "expr" represents an operation.
1238 * If op has a higher precedence than expr->u.op.op, then we need
1240 * If op and expr->u.op.op have the same precedence, but the operations
1241 * are performed in an order that is different from the associativity,
1242 * then we need parentheses.
1244 * An and inside an or technically does not require parentheses,
1245 * but some compilers complain about that, so we add them anyway.
1247 * Computations such as "a / b * c" and "a % b + c" can be somewhat
1248 * difficult to read, so we add parentheses for those as well.
1250 static int sub_expr_need_parens(enum isl_ast_op_type op
,
1251 __isl_keep isl_ast_expr
*expr
, int left
)
1253 if (expr
->type
!= isl_ast_expr_op
)
1256 if (op_prec
[expr
->u
.op
.op
] > op_prec
[op
])
1258 if (op_prec
[expr
->u
.op
.op
] == op_prec
[op
] && left
!= op_left
[op
])
1261 if (is_or(op
) && is_and(expr
->u
.op
.op
))
1263 if (op
== isl_ast_op_mul
&& expr
->u
.op
.op
!= isl_ast_op_mul
&&
1264 op_prec
[expr
->u
.op
.op
] == op_prec
[op
])
1266 if (is_add_sub(op
) && is_div_mod(expr
->u
.op
.op
))
1272 /* Print "expr" as a subexpression of an "op" operation.
1273 * If "left" is set, then "expr" is the left-most operand.
1275 static __isl_give isl_printer
*print_sub_expr(__isl_take isl_printer
*p
,
1276 enum isl_ast_op_type op
, __isl_keep isl_ast_expr
*expr
, int left
)
1280 need_parens
= sub_expr_need_parens(op
, expr
, left
);
1283 p
= isl_printer_print_str(p
, "(");
1284 p
= isl_printer_print_ast_expr(p
, expr
);
1286 p
= isl_printer_print_str(p
, ")");
1290 /* Print a min or max reduction "expr".
1292 static __isl_give isl_printer
*print_min_max(__isl_take isl_printer
*p
,
1293 __isl_keep isl_ast_expr
*expr
)
1297 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1298 p
= isl_printer_print_str(p
, op_str
[expr
->u
.op
.op
]);
1299 p
= isl_printer_print_str(p
, "(");
1301 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1302 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1303 p
= isl_printer_print_str(p
, ", ");
1304 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[i
]);
1305 p
= isl_printer_print_str(p
, ")");
1311 /* Print a function call "expr".
1313 * The first argument represents the function to be called.
1315 static __isl_give isl_printer
*print_call(__isl_take isl_printer
*p
,
1316 __isl_keep isl_ast_expr
*expr
)
1320 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1321 p
= isl_printer_print_str(p
, "(");
1322 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1324 p
= isl_printer_print_str(p
, ", ");
1325 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[i
]);
1327 p
= isl_printer_print_str(p
, ")");
1332 /* Print an array access "expr".
1334 * The first argument represents the array being accessed.
1336 static __isl_give isl_printer
*print_access(__isl_take isl_printer
*p
,
1337 __isl_keep isl_ast_expr
*expr
)
1341 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1342 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1343 p
= isl_printer_print_str(p
, "[");
1344 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[i
]);
1345 p
= isl_printer_print_str(p
, "]");
1351 /* Print "expr" to "p".
1353 * If we are printing in isl format, then we also print an indication
1354 * of the size of the expression (if it was computed).
1356 __isl_give isl_printer
*isl_printer_print_ast_expr(__isl_take isl_printer
*p
,
1357 __isl_keep isl_ast_expr
*expr
)
1362 return isl_printer_free(p
);
1364 switch (expr
->type
) {
1365 case isl_ast_expr_op
:
1366 if (expr
->u
.op
.op
== isl_ast_op_call
) {
1367 p
= print_call(p
, expr
);
1370 if (expr
->u
.op
.op
== isl_ast_op_access
) {
1371 p
= print_access(p
, expr
);
1374 if (expr
->u
.op
.n_arg
== 1) {
1375 p
= isl_printer_print_str(p
, op_str
[expr
->u
.op
.op
]);
1376 p
= print_sub_expr(p
, expr
->u
.op
.op
,
1377 expr
->u
.op
.args
[0], 0);
1380 if (expr
->u
.op
.op
== isl_ast_op_fdiv_q
) {
1381 p
= isl_printer_print_str(p
, "floord(");
1382 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1383 p
= isl_printer_print_str(p
, ", ");
1384 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[1]);
1385 p
= isl_printer_print_str(p
, ")");
1388 if (expr
->u
.op
.op
== isl_ast_op_max
||
1389 expr
->u
.op
.op
== isl_ast_op_min
) {
1390 p
= print_min_max(p
, expr
);
1393 if (expr
->u
.op
.op
== isl_ast_op_cond
||
1394 expr
->u
.op
.op
== isl_ast_op_select
) {
1395 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1396 p
= isl_printer_print_str(p
, " ? ");
1397 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[1]);
1398 p
= isl_printer_print_str(p
, " : ");
1399 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[2]);
1402 if (expr
->u
.op
.n_arg
!= 2)
1403 isl_die(isl_printer_get_ctx(p
), isl_error_internal
,
1404 "operation should have two arguments",
1406 p
= print_sub_expr(p
, expr
->u
.op
.op
, expr
->u
.op
.args
[0], 1);
1407 if (expr
->u
.op
.op
!= isl_ast_op_member
)
1408 p
= isl_printer_print_str(p
, " ");
1409 p
= isl_printer_print_str(p
, op_str
[expr
->u
.op
.op
]);
1410 if (expr
->u
.op
.op
!= isl_ast_op_member
)
1411 p
= isl_printer_print_str(p
, " ");
1412 p
= print_sub_expr(p
, expr
->u
.op
.op
, expr
->u
.op
.args
[1], 0);
1414 case isl_ast_expr_id
:
1415 p
= isl_printer_print_str(p
, isl_id_get_name(expr
->u
.id
));
1417 case isl_ast_expr_int
:
1418 p
= isl_printer_print_val(p
, expr
->u
.v
);
1420 case isl_ast_expr_error
:
1426 isl_printer_free(p
);
1430 /* Print "node" to "p" in "isl format".
1432 static __isl_give isl_printer
*print_ast_node_isl(__isl_take isl_printer
*p
,
1433 __isl_keep isl_ast_node
*node
)
1435 p
= isl_printer_print_str(p
, "(");
1436 switch (node
->type
) {
1437 case isl_ast_node_for
:
1438 if (node
->u
.f
.degenerate
) {
1439 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1441 p
= isl_printer_print_str(p
, "init: ");
1442 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1443 p
= isl_printer_print_str(p
, ", ");
1444 p
= isl_printer_print_str(p
, "cond: ");
1445 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.cond
);
1446 p
= isl_printer_print_str(p
, ", ");
1447 p
= isl_printer_print_str(p
, "inc: ");
1448 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.inc
);
1450 if (node
->u
.f
.body
) {
1451 p
= isl_printer_print_str(p
, ", ");
1452 p
= isl_printer_print_str(p
, "body: ");
1453 p
= isl_printer_print_ast_node(p
, node
->u
.f
.body
);
1456 case isl_ast_node_user
:
1457 p
= isl_printer_print_ast_expr(p
, node
->u
.e
.expr
);
1459 case isl_ast_node_if
:
1460 p
= isl_printer_print_str(p
, "guard: ");
1461 p
= isl_printer_print_ast_expr(p
, node
->u
.i
.guard
);
1462 if (node
->u
.i
.then
) {
1463 p
= isl_printer_print_str(p
, ", ");
1464 p
= isl_printer_print_str(p
, "then: ");
1465 p
= isl_printer_print_ast_node(p
, node
->u
.i
.then
);
1467 if (node
->u
.i
.else_node
) {
1468 p
= isl_printer_print_str(p
, ", ");
1469 p
= isl_printer_print_str(p
, "else: ");
1470 p
= isl_printer_print_ast_node(p
, node
->u
.i
.else_node
);
1473 case isl_ast_node_block
:
1474 p
= isl_printer_print_ast_node_list(p
, node
->u
.b
.children
);
1479 p
= isl_printer_print_str(p
, ")");
1483 /* Do we need to print a block around the body "node" of a for or if node?
1485 * If the node is a block, then we need to print a block.
1486 * Also if the node is a degenerate for then we will print it as
1487 * an assignment followed by the body of the for loop, so we need a block
1489 * If the node is an if node with an else, then we print a block
1490 * to avoid spurious dangling else warnings emitted by some compilers.
1492 static int need_block(__isl_keep isl_ast_node
*node
)
1494 if (node
->type
== isl_ast_node_block
)
1496 if (node
->type
== isl_ast_node_for
&& node
->u
.f
.degenerate
)
1498 if (node
->type
== isl_ast_node_if
&& node
->u
.i
.else_node
)
1503 static __isl_give isl_printer
*print_ast_node_c(__isl_take isl_printer
*p
,
1504 __isl_keep isl_ast_node
*node
,
1505 __isl_keep isl_ast_print_options
*options
, int in_block
, int in_list
);
1506 static __isl_give isl_printer
*print_if_c(__isl_take isl_printer
*p
,
1507 __isl_keep isl_ast_node
*node
,
1508 __isl_keep isl_ast_print_options
*options
, int new_line
);
1510 /* Print the body "node" of a for or if node.
1511 * If "else_node" is set, then it is printed as well.
1513 * We first check if we need to print out a block.
1514 * We always print out a block if there is an else node to make
1515 * sure that the else node is matched to the correct if node.
1517 * If the else node is itself an if, then we print it as
1521 * Otherwise the else node is printed as
1526 static __isl_give isl_printer
*print_body_c(__isl_take isl_printer
*p
,
1527 __isl_keep isl_ast_node
*node
, __isl_keep isl_ast_node
*else_node
,
1528 __isl_keep isl_ast_print_options
*options
)
1531 return isl_printer_free(p
);
1533 if (!else_node
&& !need_block(node
)) {
1534 p
= isl_printer_end_line(p
);
1535 p
= isl_printer_indent(p
, 2);
1536 p
= isl_ast_node_print(node
, p
,
1537 isl_ast_print_options_copy(options
));
1538 p
= isl_printer_indent(p
, -2);
1542 p
= isl_printer_print_str(p
, " {");
1543 p
= isl_printer_end_line(p
);
1544 p
= isl_printer_indent(p
, 2);
1545 p
= print_ast_node_c(p
, node
, options
, 1, 0);
1546 p
= isl_printer_indent(p
, -2);
1547 p
= isl_printer_start_line(p
);
1548 p
= isl_printer_print_str(p
, "}");
1550 if (else_node
->type
== isl_ast_node_if
) {
1551 p
= isl_printer_print_str(p
, " else ");
1552 p
= print_if_c(p
, else_node
, options
, 0);
1554 p
= isl_printer_print_str(p
, " else");
1555 p
= print_body_c(p
, else_node
, NULL
, options
);
1558 p
= isl_printer_end_line(p
);
1563 /* Print the start of a compound statement.
1565 static __isl_give isl_printer
*start_block(__isl_take isl_printer
*p
)
1567 p
= isl_printer_start_line(p
);
1568 p
= isl_printer_print_str(p
, "{");
1569 p
= isl_printer_end_line(p
);
1570 p
= isl_printer_indent(p
, 2);
1575 /* Print the end of a compound statement.
1577 static __isl_give isl_printer
*end_block(__isl_take isl_printer
*p
)
1579 p
= isl_printer_indent(p
, -2);
1580 p
= isl_printer_start_line(p
);
1581 p
= isl_printer_print_str(p
, "}");
1582 p
= isl_printer_end_line(p
);
1587 /* Print the for node "node".
1589 * If the for node is degenerate, it is printed as
1591 * type iterator = init;
1594 * Otherwise, it is printed as
1596 * for (type iterator = init; cond; iterator += inc)
1599 * "in_block" is set if we are currently inside a block.
1600 * "in_list" is set if the current node is not alone in the block.
1601 * If we are not in a block or if the current not is not alone in the block
1602 * then we print a block around a degenerate for loop such that the variable
1603 * declaration will not conflict with any potential other declaration
1604 * of the same variable.
1606 static __isl_give isl_printer
*print_for_c(__isl_take isl_printer
*p
,
1607 __isl_keep isl_ast_node
*node
,
1608 __isl_keep isl_ast_print_options
*options
, int in_block
, int in_list
)
1614 type
= isl_options_get_ast_iterator_type(isl_printer_get_ctx(p
));
1615 if (!node
->u
.f
.degenerate
) {
1616 id
= isl_ast_expr_get_id(node
->u
.f
.iterator
);
1617 name
= isl_id_get_name(id
);
1619 p
= isl_printer_start_line(p
);
1620 p
= isl_printer_print_str(p
, "for (");
1621 p
= isl_printer_print_str(p
, type
);
1622 p
= isl_printer_print_str(p
, " ");
1623 p
= isl_printer_print_str(p
, name
);
1624 p
= isl_printer_print_str(p
, " = ");
1625 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1626 p
= isl_printer_print_str(p
, "; ");
1627 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.cond
);
1628 p
= isl_printer_print_str(p
, "; ");
1629 p
= isl_printer_print_str(p
, name
);
1630 p
= isl_printer_print_str(p
, " += ");
1631 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.inc
);
1632 p
= isl_printer_print_str(p
, ")");
1633 p
= print_body_c(p
, node
->u
.f
.body
, NULL
, options
);
1635 id
= isl_ast_expr_get_id(node
->u
.f
.iterator
);
1636 name
= isl_id_get_name(id
);
1638 if (!in_block
|| in_list
)
1640 p
= isl_printer_start_line(p
);
1641 p
= isl_printer_print_str(p
, type
);
1642 p
= isl_printer_print_str(p
, " ");
1643 p
= isl_printer_print_str(p
, name
);
1644 p
= isl_printer_print_str(p
, " = ");
1645 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1646 p
= isl_printer_print_str(p
, ";");
1647 p
= isl_printer_end_line(p
);
1648 p
= print_ast_node_c(p
, node
->u
.f
.body
, options
, 1, 0);
1649 if (!in_block
|| in_list
)
1656 /* Print the if node "node".
1657 * If "new_line" is set then the if node should be printed on a new line.
1659 static __isl_give isl_printer
*print_if_c(__isl_take isl_printer
*p
,
1660 __isl_keep isl_ast_node
*node
,
1661 __isl_keep isl_ast_print_options
*options
, int new_line
)
1664 p
= isl_printer_start_line(p
);
1665 p
= isl_printer_print_str(p
, "if (");
1666 p
= isl_printer_print_ast_expr(p
, node
->u
.i
.guard
);
1667 p
= isl_printer_print_str(p
, ")");
1668 p
= print_body_c(p
, node
->u
.i
.then
, node
->u
.i
.else_node
, options
);
1673 /* Print the "node" to "p".
1675 * "in_block" is set if we are currently inside a block.
1676 * If so, we do not print a block around the children of a block node.
1677 * We do this to avoid an extra block around the body of a degenerate
1680 * "in_list" is set if the current node is not alone in the block.
1682 static __isl_give isl_printer
*print_ast_node_c(__isl_take isl_printer
*p
,
1683 __isl_keep isl_ast_node
*node
,
1684 __isl_keep isl_ast_print_options
*options
, int in_block
, int in_list
)
1686 switch (node
->type
) {
1687 case isl_ast_node_for
:
1688 if (options
->print_for
)
1689 return options
->print_for(p
,
1690 isl_ast_print_options_copy(options
),
1691 node
, options
->print_for_user
);
1692 p
= print_for_c(p
, node
, options
, in_block
, in_list
);
1694 case isl_ast_node_if
:
1695 p
= print_if_c(p
, node
, options
, 1);
1697 case isl_ast_node_block
:
1700 p
= isl_ast_node_list_print(node
->u
.b
.children
, p
, options
);
1704 case isl_ast_node_user
:
1705 if (options
->print_user
)
1706 return options
->print_user(p
,
1707 isl_ast_print_options_copy(options
),
1708 node
, options
->print_user_user
);
1709 p
= isl_printer_start_line(p
);
1710 p
= isl_printer_print_ast_expr(p
, node
->u
.e
.expr
);
1711 p
= isl_printer_print_str(p
, ";");
1712 p
= isl_printer_end_line(p
);
1714 case isl_ast_node_error
:
1720 /* Print the for node "node" to "p".
1722 __isl_give isl_printer
*isl_ast_node_for_print(__isl_keep isl_ast_node
*node
,
1723 __isl_take isl_printer
*p
, __isl_take isl_ast_print_options
*options
)
1725 if (!node
|| !options
)
1727 if (node
->type
!= isl_ast_node_for
)
1728 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1729 "not a for node", goto error
);
1730 p
= print_for_c(p
, node
, options
, 0, 0);
1731 isl_ast_print_options_free(options
);
1734 isl_ast_print_options_free(options
);
1735 isl_printer_free(p
);
1739 /* Print the if node "node" to "p".
1741 __isl_give isl_printer
*isl_ast_node_if_print(__isl_keep isl_ast_node
*node
,
1742 __isl_take isl_printer
*p
, __isl_take isl_ast_print_options
*options
)
1744 if (!node
|| !options
)
1746 if (node
->type
!= isl_ast_node_if
)
1747 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1748 "not an if node", goto error
);
1749 p
= print_if_c(p
, node
, options
, 1);
1750 isl_ast_print_options_free(options
);
1753 isl_ast_print_options_free(options
);
1754 isl_printer_free(p
);
1758 /* Print "node" to "p".
1760 __isl_give isl_printer
*isl_ast_node_print(__isl_keep isl_ast_node
*node
,
1761 __isl_take isl_printer
*p
, __isl_take isl_ast_print_options
*options
)
1763 if (!options
|| !node
)
1765 p
= print_ast_node_c(p
, node
, options
, 0, 0);
1766 isl_ast_print_options_free(options
);
1769 isl_ast_print_options_free(options
);
1770 isl_printer_free(p
);
1774 /* Print "node" to "p".
1776 __isl_give isl_printer
*isl_printer_print_ast_node(__isl_take isl_printer
*p
,
1777 __isl_keep isl_ast_node
*node
)
1780 isl_ast_print_options
*options
;
1785 format
= isl_printer_get_output_format(p
);
1787 case ISL_FORMAT_ISL
:
1788 p
= print_ast_node_isl(p
, node
);
1791 options
= isl_ast_print_options_alloc(isl_printer_get_ctx(p
));
1792 p
= isl_ast_node_print(node
, p
, options
);
1795 isl_die(isl_printer_get_ctx(p
), isl_error_unsupported
,
1796 "output format not supported for ast_node",
1797 return isl_printer_free(p
));
1803 /* Print the list of nodes "list" to "p".
1805 __isl_give isl_printer
*isl_ast_node_list_print(
1806 __isl_keep isl_ast_node_list
*list
, __isl_take isl_printer
*p
,
1807 __isl_keep isl_ast_print_options
*options
)
1811 if (!p
|| !list
|| !options
)
1812 return isl_printer_free(p
);
1814 for (i
= 0; i
< list
->n
; ++i
)
1815 p
= print_ast_node_c(p
, list
->p
[i
], options
, 1, 1);
1820 #define ISL_AST_MACRO_FLOORD (1 << 0)
1821 #define ISL_AST_MACRO_MIN (1 << 1)
1822 #define ISL_AST_MACRO_MAX (1 << 2)
1823 #define ISL_AST_MACRO_ALL (ISL_AST_MACRO_FLOORD | \
1824 ISL_AST_MACRO_MIN | \
1827 /* If "expr" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1828 * then set the corresponding bit in "macros".
1830 static int ast_expr_required_macros(__isl_keep isl_ast_expr
*expr
, int macros
)
1834 if (macros
== ISL_AST_MACRO_ALL
)
1837 if (expr
->type
!= isl_ast_expr_op
)
1840 if (expr
->u
.op
.op
== isl_ast_op_min
)
1841 macros
|= ISL_AST_MACRO_MIN
;
1842 if (expr
->u
.op
.op
== isl_ast_op_max
)
1843 macros
|= ISL_AST_MACRO_MAX
;
1844 if (expr
->u
.op
.op
== isl_ast_op_fdiv_q
)
1845 macros
|= ISL_AST_MACRO_FLOORD
;
1847 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
)
1848 macros
= ast_expr_required_macros(expr
->u
.op
.args
[i
], macros
);
1853 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list
*list
,
1856 /* If "node" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1857 * then set the corresponding bit in "macros".
1859 static int ast_node_required_macros(__isl_keep isl_ast_node
*node
, int macros
)
1861 if (macros
== ISL_AST_MACRO_ALL
)
1864 switch (node
->type
) {
1865 case isl_ast_node_for
:
1866 macros
= ast_expr_required_macros(node
->u
.f
.init
, macros
);
1867 if (!node
->u
.f
.degenerate
) {
1868 macros
= ast_expr_required_macros(node
->u
.f
.cond
,
1870 macros
= ast_expr_required_macros(node
->u
.f
.inc
,
1873 macros
= ast_node_required_macros(node
->u
.f
.body
, macros
);
1875 case isl_ast_node_if
:
1876 macros
= ast_expr_required_macros(node
->u
.i
.guard
, macros
);
1877 macros
= ast_node_required_macros(node
->u
.i
.then
, macros
);
1878 if (node
->u
.i
.else_node
)
1879 macros
= ast_node_required_macros(node
->u
.i
.else_node
,
1882 case isl_ast_node_block
:
1883 macros
= ast_node_list_required_macros(node
->u
.b
.children
,
1886 case isl_ast_node_user
:
1887 macros
= ast_expr_required_macros(node
->u
.e
.expr
, macros
);
1889 case isl_ast_node_error
:
1896 /* If "list" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1897 * then set the corresponding bit in "macros".
1899 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list
*list
,
1904 for (i
= 0; i
< list
->n
; ++i
)
1905 macros
= ast_node_required_macros(list
->p
[i
], macros
);
1910 /* Print a macro definition for the operator "type".
1912 __isl_give isl_printer
*isl_ast_op_type_print_macro(
1913 enum isl_ast_op_type type
, __isl_take isl_printer
*p
)
1916 case isl_ast_op_min
:
1917 p
= isl_printer_start_line(p
);
1918 p
= isl_printer_print_str(p
,
1919 "#define min(x,y) ((x) < (y) ? (x) : (y))");
1920 p
= isl_printer_end_line(p
);
1922 case isl_ast_op_max
:
1923 p
= isl_printer_start_line(p
);
1924 p
= isl_printer_print_str(p
,
1925 "#define max(x,y) ((x) > (y) ? (x) : (y))");
1926 p
= isl_printer_end_line(p
);
1928 case isl_ast_op_fdiv_q
:
1929 p
= isl_printer_start_line(p
);
1930 p
= isl_printer_print_str(p
,
1931 "#define floord(n,d) "
1932 "(((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))");
1933 p
= isl_printer_end_line(p
);
1942 /* Call "fn" for each type of operation that appears in "node"
1943 * and that requires a macro definition.
1945 int isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node
*node
,
1946 int (*fn
)(enum isl_ast_op_type type
, void *user
), void *user
)
1953 macros
= ast_node_required_macros(node
, 0);
1955 if (macros
& ISL_AST_MACRO_MIN
&& fn(isl_ast_op_min
, user
) < 0)
1957 if (macros
& ISL_AST_MACRO_MAX
&& fn(isl_ast_op_max
, user
) < 0)
1959 if (macros
& ISL_AST_MACRO_FLOORD
&& fn(isl_ast_op_fdiv_q
, user
) < 0)
1965 static int ast_op_type_print_macro(enum isl_ast_op_type type
, void *user
)
1967 isl_printer
**p
= user
;
1969 *p
= isl_ast_op_type_print_macro(type
, *p
);
1974 /* Print macro definitions for all the macros used in the result
1975 * of printing "node.
1977 __isl_give isl_printer
*isl_ast_node_print_macros(
1978 __isl_keep isl_ast_node
*node
, __isl_take isl_printer
*p
)
1980 if (isl_ast_node_foreach_ast_op_type(node
,
1981 &ast_op_type_print_macro
, &p
) < 0)
1982 return isl_printer_free(p
);