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 void *isl_ast_print_options_free(__isl_take isl_ast_print_options
*options
)
92 if (--options
->ref
> 0)
95 isl_ctx_deref(options
->ctx
);
101 /* Set the print_user callback of "options" to "print_user".
103 * If this callback is set, then it used to print user nodes in the AST.
104 * Otherwise, the expression associated to the user node is printed.
106 __isl_give isl_ast_print_options
*isl_ast_print_options_set_print_user(
107 __isl_take isl_ast_print_options
*options
,
108 __isl_give isl_printer
*(*print_user
)(__isl_take isl_printer
*p
,
109 __isl_take isl_ast_print_options
*options
,
110 __isl_keep isl_ast_node
*node
, void *user
),
113 options
= isl_ast_print_options_cow(options
);
117 options
->print_user
= print_user
;
118 options
->print_user_user
= user
;
123 /* Set the print_for callback of "options" to "print_for".
125 * If this callback is set, then it used to print for nodes in the AST.
127 __isl_give isl_ast_print_options
*isl_ast_print_options_set_print_for(
128 __isl_take isl_ast_print_options
*options
,
129 __isl_give isl_printer
*(*print_for
)(__isl_take isl_printer
*p
,
130 __isl_take isl_ast_print_options
*options
,
131 __isl_keep isl_ast_node
*node
, void *user
),
134 options
= isl_ast_print_options_cow(options
);
138 options
->print_for
= print_for
;
139 options
->print_for_user
= user
;
144 __isl_give isl_ast_expr
*isl_ast_expr_copy(__isl_keep isl_ast_expr
*expr
)
153 __isl_give isl_ast_expr
*isl_ast_expr_dup(__isl_keep isl_ast_expr
*expr
)
162 ctx
= isl_ast_expr_get_ctx(expr
);
163 switch (expr
->type
) {
164 case isl_ast_expr_int
:
165 dup
= isl_ast_expr_from_val(isl_val_copy(expr
->u
.v
));
167 case isl_ast_expr_id
:
168 dup
= isl_ast_expr_from_id(isl_id_copy(expr
->u
.id
));
170 case isl_ast_expr_op
:
171 dup
= isl_ast_expr_alloc_op(ctx
,
172 expr
->u
.op
.op
, expr
->u
.op
.n_arg
);
175 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
)
177 isl_ast_expr_copy(expr
->u
.op
.args
[i
]);
179 case isl_ast_expr_error
:
189 __isl_give isl_ast_expr
*isl_ast_expr_cow(__isl_take isl_ast_expr
*expr
)
197 return isl_ast_expr_dup(expr
);
200 void *isl_ast_expr_free(__isl_take isl_ast_expr
*expr
)
210 isl_ctx_deref(expr
->ctx
);
212 switch (expr
->type
) {
213 case isl_ast_expr_int
:
214 isl_val_free(expr
->u
.v
);
216 case isl_ast_expr_id
:
217 isl_id_free(expr
->u
.id
);
219 case isl_ast_expr_op
:
220 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
)
221 isl_ast_expr_free(expr
->u
.op
.args
[i
]);
222 free(expr
->u
.op
.args
);
224 case isl_ast_expr_error
:
232 isl_ctx
*isl_ast_expr_get_ctx(__isl_keep isl_ast_expr
*expr
)
234 return expr
? expr
->ctx
: NULL
;
237 enum isl_ast_expr_type
isl_ast_expr_get_type(__isl_keep isl_ast_expr
*expr
)
239 return expr
? expr
->type
: isl_ast_expr_error
;
242 /* Return the integer value represented by "expr".
244 __isl_give isl_val
*isl_ast_expr_get_val(__isl_keep isl_ast_expr
*expr
)
248 if (expr
->type
!= isl_ast_expr_int
)
249 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
250 "expression not an int", return NULL
);
251 return isl_val_copy(expr
->u
.v
);
254 __isl_give isl_id
*isl_ast_expr_get_id(__isl_keep isl_ast_expr
*expr
)
258 if (expr
->type
!= isl_ast_expr_id
)
259 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
260 "expression not an identifier", return NULL
);
262 return isl_id_copy(expr
->u
.id
);
265 enum isl_ast_op_type
isl_ast_expr_get_op_type(__isl_keep isl_ast_expr
*expr
)
268 return isl_ast_op_error
;
269 if (expr
->type
!= isl_ast_expr_op
)
270 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
271 "expression not an operation", return isl_ast_op_error
);
272 return expr
->u
.op
.op
;
275 int isl_ast_expr_get_op_n_arg(__isl_keep isl_ast_expr
*expr
)
279 if (expr
->type
!= isl_ast_expr_op
)
280 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
281 "expression not an operation", return -1);
282 return expr
->u
.op
.n_arg
;
285 __isl_give isl_ast_expr
*isl_ast_expr_get_op_arg(__isl_keep isl_ast_expr
*expr
,
290 if (expr
->type
!= isl_ast_expr_op
)
291 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
292 "expression not an operation", return NULL
);
293 if (pos
< 0 || pos
>= expr
->u
.op
.n_arg
)
294 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
295 "index out of bounds", return NULL
);
297 return isl_ast_expr_copy(expr
->u
.op
.args
[pos
]);
300 /* Replace the argument at position "pos" of "expr" by "arg".
302 __isl_give isl_ast_expr
*isl_ast_expr_set_op_arg(__isl_take isl_ast_expr
*expr
,
303 int pos
, __isl_take isl_ast_expr
*arg
)
305 expr
= isl_ast_expr_cow(expr
);
308 if (expr
->type
!= isl_ast_expr_op
)
309 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
310 "expression not an operation", goto error
);
311 if (pos
< 0 || pos
>= expr
->u
.op
.n_arg
)
312 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
313 "index out of bounds", goto error
);
315 isl_ast_expr_free(expr
->u
.op
.args
[pos
]);
316 expr
->u
.op
.args
[pos
] = arg
;
320 isl_ast_expr_free(arg
);
321 return isl_ast_expr_free(expr
);
324 /* Is "expr1" equal to "expr2"?
326 int isl_ast_expr_is_equal(__isl_keep isl_ast_expr
*expr1
,
327 __isl_keep isl_ast_expr
*expr2
)
331 if (!expr1
|| !expr2
)
336 if (expr1
->type
!= expr2
->type
)
338 switch (expr1
->type
) {
339 case isl_ast_expr_int
:
340 return isl_val_eq(expr1
->u
.v
, expr2
->u
.v
);
341 case isl_ast_expr_id
:
342 return expr1
->u
.id
== expr2
->u
.id
;
343 case isl_ast_expr_op
:
344 if (expr1
->u
.op
.op
!= expr2
->u
.op
.op
)
346 if (expr1
->u
.op
.n_arg
!= expr2
->u
.op
.n_arg
)
348 for (i
= 0; i
< expr1
->u
.op
.n_arg
; ++i
) {
350 equal
= isl_ast_expr_is_equal(expr1
->u
.op
.args
[i
],
351 expr2
->u
.op
.args
[i
]);
353 if (equal
< 0 || !equal
)
357 case isl_ast_expr_error
:
362 /* Create a new operation expression of operation type "op",
363 * with "n_arg" as yet unspecified arguments.
365 __isl_give isl_ast_expr
*isl_ast_expr_alloc_op(isl_ctx
*ctx
,
366 enum isl_ast_op_type op
, int n_arg
)
370 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
377 expr
->type
= isl_ast_expr_op
;
379 expr
->u
.op
.n_arg
= n_arg
;
380 expr
->u
.op
.args
= isl_calloc_array(ctx
, isl_ast_expr
*, n_arg
);
382 if (n_arg
&& !expr
->u
.op
.args
)
383 return isl_ast_expr_free(expr
);
388 /* Create a new id expression representing "id".
390 __isl_give isl_ast_expr
*isl_ast_expr_from_id(__isl_take isl_id
*id
)
398 ctx
= isl_id_get_ctx(id
);
399 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
401 return isl_id_free(id
);
406 expr
->type
= isl_ast_expr_id
;
412 /* Create a new integer expression representing "i".
414 __isl_give isl_ast_expr
*isl_ast_expr_alloc_int_si(isl_ctx
*ctx
, int i
)
418 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
425 expr
->type
= isl_ast_expr_int
;
426 expr
->u
.v
= isl_val_int_from_si(ctx
, i
);
428 return isl_ast_expr_free(expr
);
433 /* Create a new integer expression representing "v".
435 __isl_give isl_ast_expr
*isl_ast_expr_from_val(__isl_take isl_val
*v
)
442 if (!isl_val_is_int(v
))
443 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
444 "expecting integer value", return isl_val_free(v
));
446 ctx
= isl_val_get_ctx(v
);
447 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
449 return isl_val_free(v
);
454 expr
->type
= isl_ast_expr_int
;
460 /* Create an expression representing the negation of "arg".
462 __isl_give isl_ast_expr
*isl_ast_expr_neg(__isl_take isl_ast_expr
*arg
)
465 isl_ast_expr
*expr
= NULL
;
470 ctx
= isl_ast_expr_get_ctx(arg
);
471 expr
= isl_ast_expr_alloc_op(ctx
, isl_ast_op_minus
, 1);
475 expr
->u
.op
.args
[0] = arg
;
479 isl_ast_expr_free(arg
);
483 /* Create an expression representing the binary operation "type"
484 * applied to "expr1" and "expr2".
486 __isl_give isl_ast_expr
*isl_ast_expr_alloc_binary(enum isl_ast_op_type type
,
487 __isl_take isl_ast_expr
*expr1
, __isl_take isl_ast_expr
*expr2
)
490 isl_ast_expr
*expr
= NULL
;
492 if (!expr1
|| !expr2
)
495 ctx
= isl_ast_expr_get_ctx(expr1
);
496 expr
= isl_ast_expr_alloc_op(ctx
, type
, 2);
500 expr
->u
.op
.args
[0] = expr1
;
501 expr
->u
.op
.args
[1] = expr2
;
505 isl_ast_expr_free(expr1
);
506 isl_ast_expr_free(expr2
);
510 /* Create an expression representing the sum of "expr1" and "expr2".
512 __isl_give isl_ast_expr
*isl_ast_expr_add(__isl_take isl_ast_expr
*expr1
,
513 __isl_take isl_ast_expr
*expr2
)
515 return isl_ast_expr_alloc_binary(isl_ast_op_add
, expr1
, expr2
);
518 /* Create an expression representing the difference of "expr1" and "expr2".
520 __isl_give isl_ast_expr
*isl_ast_expr_sub(__isl_take isl_ast_expr
*expr1
,
521 __isl_take isl_ast_expr
*expr2
)
523 return isl_ast_expr_alloc_binary(isl_ast_op_sub
, expr1
, expr2
);
526 /* Create an expression representing the product of "expr1" and "expr2".
528 __isl_give isl_ast_expr
*isl_ast_expr_mul(__isl_take isl_ast_expr
*expr1
,
529 __isl_take isl_ast_expr
*expr2
)
531 return isl_ast_expr_alloc_binary(isl_ast_op_mul
, expr1
, expr2
);
534 /* Create an expression representing the quotient of "expr1" and "expr2".
536 __isl_give isl_ast_expr
*isl_ast_expr_div(__isl_take isl_ast_expr
*expr1
,
537 __isl_take isl_ast_expr
*expr2
)
539 return isl_ast_expr_alloc_binary(isl_ast_op_div
, expr1
, expr2
);
542 /* Create an expression representing the conjunction of "expr1" and "expr2".
544 __isl_give isl_ast_expr
*isl_ast_expr_and(__isl_take isl_ast_expr
*expr1
,
545 __isl_take isl_ast_expr
*expr2
)
547 return isl_ast_expr_alloc_binary(isl_ast_op_and
, expr1
, expr2
);
550 /* Create an expression representing the disjunction of "expr1" and "expr2".
552 __isl_give isl_ast_expr
*isl_ast_expr_or(__isl_take isl_ast_expr
*expr1
,
553 __isl_take isl_ast_expr
*expr2
)
555 return isl_ast_expr_alloc_binary(isl_ast_op_or
, expr1
, expr2
);
558 /* Create an expression representing an access to "array" with index
559 * expressions "indices".
561 __isl_give isl_ast_expr
*isl_ast_expr_access(__isl_take isl_ast_expr
*array
,
562 __isl_take isl_ast_expr_list
*indices
)
566 isl_ast_expr
*access
= NULL
;
568 if (!array
|| !indices
)
571 ctx
= isl_ast_expr_get_ctx(array
);
572 n
= isl_ast_expr_list_n_ast_expr(indices
);
573 access
= isl_ast_expr_alloc_op(ctx
, isl_ast_op_access
, 1 + n
);
576 for (i
= 0; i
< n
; ++i
) {
578 index
= isl_ast_expr_list_get_ast_expr(indices
, i
);
579 access
->u
.op
.args
[1 + i
] = index
;
583 access
->u
.op
.args
[0] = array
;
585 isl_ast_expr_list_free(indices
);
588 isl_ast_expr_free(array
);
589 isl_ast_expr_list_free(indices
);
590 isl_ast_expr_free(access
);
594 /* For each subexpression of "expr" of type isl_ast_expr_id,
595 * if it appears in "id2expr", then replace it by the corresponding
598 __isl_give isl_ast_expr
*isl_ast_expr_substitute_ids(
599 __isl_take isl_ast_expr
*expr
, __isl_take isl_id_to_ast_expr
*id2expr
)
604 if (!expr
|| !id2expr
)
607 switch (expr
->type
) {
608 case isl_ast_expr_int
:
610 case isl_ast_expr_id
:
611 if (!isl_id_to_ast_expr_has(id2expr
, expr
->u
.id
))
613 id
= isl_id_copy(expr
->u
.id
);
614 isl_ast_expr_free(expr
);
615 expr
= isl_id_to_ast_expr_get(id2expr
, id
);
617 case isl_ast_expr_op
:
618 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
) {
620 arg
= isl_ast_expr_copy(expr
->u
.op
.args
[i
]);
621 arg
= isl_ast_expr_substitute_ids(arg
,
622 isl_id_to_ast_expr_copy(id2expr
));
623 if (arg
== expr
->u
.op
.args
[i
]) {
624 isl_ast_expr_free(arg
);
628 expr
= isl_ast_expr_free(expr
);
629 expr
= isl_ast_expr_cow(expr
);
631 isl_ast_expr_free(arg
);
634 isl_ast_expr_free(expr
->u
.op
.args
[i
]);
635 expr
->u
.op
.args
[i
] = arg
;
638 case isl_ast_expr_error
:
639 expr
= isl_ast_expr_free(expr
);
643 isl_id_to_ast_expr_free(id2expr
);
646 isl_ast_expr_free(expr
);
647 isl_id_to_ast_expr_free(id2expr
);
651 isl_ctx
*isl_ast_node_get_ctx(__isl_keep isl_ast_node
*node
)
653 return node
? node
->ctx
: NULL
;
656 enum isl_ast_node_type
isl_ast_node_get_type(__isl_keep isl_ast_node
*node
)
658 return node
? node
->type
: isl_ast_node_error
;
661 __isl_give isl_ast_node
*isl_ast_node_alloc(isl_ctx
*ctx
,
662 enum isl_ast_node_type type
)
666 node
= isl_calloc_type(ctx
, isl_ast_node
);
678 /* Create an if node with the given guard.
680 * The then body needs to be filled in later.
682 __isl_give isl_ast_node
*isl_ast_node_alloc_if(__isl_take isl_ast_expr
*guard
)
689 node
= isl_ast_node_alloc(isl_ast_expr_get_ctx(guard
), isl_ast_node_if
);
692 node
->u
.i
.guard
= guard
;
696 isl_ast_expr_free(guard
);
700 /* Create a for node with the given iterator.
702 * The remaining fields need to be filled in later.
704 __isl_give isl_ast_node
*isl_ast_node_alloc_for(__isl_take isl_id
*id
)
712 ctx
= isl_id_get_ctx(id
);
713 node
= isl_ast_node_alloc(ctx
, isl_ast_node_for
);
717 node
->u
.f
.iterator
= isl_ast_expr_from_id(id
);
718 if (!node
->u
.f
.iterator
)
719 return isl_ast_node_free(node
);
724 /* Create a user node evaluating "expr".
726 __isl_give isl_ast_node
*isl_ast_node_alloc_user(__isl_take isl_ast_expr
*expr
)
734 ctx
= isl_ast_expr_get_ctx(expr
);
735 node
= isl_ast_node_alloc(ctx
, isl_ast_node_user
);
739 node
->u
.e
.expr
= expr
;
743 isl_ast_expr_free(expr
);
747 /* Create a block node with the given children.
749 __isl_give isl_ast_node
*isl_ast_node_alloc_block(
750 __isl_take isl_ast_node_list
*list
)
758 ctx
= isl_ast_node_list_get_ctx(list
);
759 node
= isl_ast_node_alloc(ctx
, isl_ast_node_block
);
763 node
->u
.b
.children
= list
;
767 isl_ast_node_list_free(list
);
771 /* Represent the given list of nodes as a single node, either by
772 * extract the node from a single element list or by creating
773 * a block node with the list of nodes as children.
775 __isl_give isl_ast_node
*isl_ast_node_from_ast_node_list(
776 __isl_take isl_ast_node_list
*list
)
780 if (isl_ast_node_list_n_ast_node(list
) != 1)
781 return isl_ast_node_alloc_block(list
);
783 node
= isl_ast_node_list_get_ast_node(list
, 0);
784 isl_ast_node_list_free(list
);
789 __isl_give isl_ast_node
*isl_ast_node_copy(__isl_keep isl_ast_node
*node
)
798 __isl_give isl_ast_node
*isl_ast_node_dup(__isl_keep isl_ast_node
*node
)
805 dup
= isl_ast_node_alloc(isl_ast_node_get_ctx(node
), node
->type
);
809 switch (node
->type
) {
810 case isl_ast_node_if
:
811 dup
->u
.i
.guard
= isl_ast_expr_copy(node
->u
.i
.guard
);
812 dup
->u
.i
.then
= isl_ast_node_copy(node
->u
.i
.then
);
813 dup
->u
.i
.else_node
= isl_ast_node_copy(node
->u
.i
.else_node
);
814 if (!dup
->u
.i
.guard
|| !dup
->u
.i
.then
||
815 (node
->u
.i
.else_node
&& !dup
->u
.i
.else_node
))
816 return isl_ast_node_free(dup
);
818 case isl_ast_node_for
:
819 dup
->u
.f
.iterator
= isl_ast_expr_copy(node
->u
.f
.iterator
);
820 dup
->u
.f
.init
= isl_ast_expr_copy(node
->u
.f
.init
);
821 dup
->u
.f
.cond
= isl_ast_expr_copy(node
->u
.f
.cond
);
822 dup
->u
.f
.inc
= isl_ast_expr_copy(node
->u
.f
.inc
);
823 dup
->u
.f
.body
= isl_ast_node_copy(node
->u
.f
.body
);
824 if (!dup
->u
.f
.iterator
|| !dup
->u
.f
.init
|| !dup
->u
.f
.cond
||
825 !dup
->u
.f
.inc
|| !dup
->u
.f
.body
)
826 return isl_ast_node_free(dup
);
828 case isl_ast_node_block
:
829 dup
->u
.b
.children
= isl_ast_node_list_copy(node
->u
.b
.children
);
830 if (!dup
->u
.b
.children
)
831 return isl_ast_node_free(dup
);
833 case isl_ast_node_user
:
834 dup
->u
.e
.expr
= isl_ast_expr_copy(node
->u
.e
.expr
);
836 return isl_ast_node_free(dup
);
838 case isl_ast_node_error
:
845 __isl_give isl_ast_node
*isl_ast_node_cow(__isl_take isl_ast_node
*node
)
853 return isl_ast_node_dup(node
);
856 void *isl_ast_node_free(__isl_take isl_ast_node
*node
)
864 switch (node
->type
) {
865 case isl_ast_node_if
:
866 isl_ast_expr_free(node
->u
.i
.guard
);
867 isl_ast_node_free(node
->u
.i
.then
);
868 isl_ast_node_free(node
->u
.i
.else_node
);
870 case isl_ast_node_for
:
871 isl_ast_expr_free(node
->u
.f
.iterator
);
872 isl_ast_expr_free(node
->u
.f
.init
);
873 isl_ast_expr_free(node
->u
.f
.cond
);
874 isl_ast_expr_free(node
->u
.f
.inc
);
875 isl_ast_node_free(node
->u
.f
.body
);
877 case isl_ast_node_block
:
878 isl_ast_node_list_free(node
->u
.b
.children
);
880 case isl_ast_node_user
:
881 isl_ast_expr_free(node
->u
.e
.expr
);
883 case isl_ast_node_error
:
887 isl_id_free(node
->annotation
);
888 isl_ctx_deref(node
->ctx
);
894 /* Replace the body of the for node "node" by "body".
896 __isl_give isl_ast_node
*isl_ast_node_for_set_body(
897 __isl_take isl_ast_node
*node
, __isl_take isl_ast_node
*body
)
899 node
= isl_ast_node_cow(node
);
902 if (node
->type
!= isl_ast_node_for
)
903 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
904 "not a for node", goto error
);
906 isl_ast_node_free(node
->u
.f
.body
);
907 node
->u
.f
.body
= body
;
911 isl_ast_node_free(node
);
912 isl_ast_node_free(body
);
916 __isl_give isl_ast_node
*isl_ast_node_for_get_body(
917 __isl_keep isl_ast_node
*node
)
921 if (node
->type
!= isl_ast_node_for
)
922 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
923 "not a for node", return NULL
);
924 return isl_ast_node_copy(node
->u
.f
.body
);
927 /* Mark the given for node as being degenerate.
929 __isl_give isl_ast_node
*isl_ast_node_for_mark_degenerate(
930 __isl_take isl_ast_node
*node
)
932 node
= isl_ast_node_cow(node
);
935 node
->u
.f
.degenerate
= 1;
939 int isl_ast_node_for_is_degenerate(__isl_keep isl_ast_node
*node
)
943 if (node
->type
!= isl_ast_node_for
)
944 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
945 "not a for node", return -1);
946 return node
->u
.f
.degenerate
;
949 __isl_give isl_ast_expr
*isl_ast_node_for_get_iterator(
950 __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 NULL
);
957 return isl_ast_expr_copy(node
->u
.f
.iterator
);
960 __isl_give isl_ast_expr
*isl_ast_node_for_get_init(
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
.init
);
971 /* Return the condition expression of the given for node.
973 * If the for node is degenerate, then the condition is not explicitly
974 * stored in the node. Instead, it is constructed as
978 __isl_give isl_ast_expr
*isl_ast_node_for_get_cond(
979 __isl_keep isl_ast_node
*node
)
983 if (node
->type
!= isl_ast_node_for
)
984 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
985 "not a for node", return NULL
);
986 if (!node
->u
.f
.degenerate
)
987 return isl_ast_expr_copy(node
->u
.f
.cond
);
989 return isl_ast_expr_alloc_binary(isl_ast_op_le
,
990 isl_ast_expr_copy(node
->u
.f
.iterator
),
991 isl_ast_expr_copy(node
->u
.f
.init
));
994 /* Return the increment of the given for node.
996 * If the for node is degenerate, then the increment is not explicitly
997 * stored in the node. We simply return "1".
999 __isl_give isl_ast_expr
*isl_ast_node_for_get_inc(
1000 __isl_keep isl_ast_node
*node
)
1004 if (node
->type
!= isl_ast_node_for
)
1005 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1006 "not a for node", return NULL
);
1007 if (!node
->u
.f
.degenerate
)
1008 return isl_ast_expr_copy(node
->u
.f
.inc
);
1009 return isl_ast_expr_alloc_int_si(isl_ast_node_get_ctx(node
), 1);
1012 /* Replace the then branch of the if node "node" by "child".
1014 __isl_give isl_ast_node
*isl_ast_node_if_set_then(
1015 __isl_take isl_ast_node
*node
, __isl_take isl_ast_node
*child
)
1017 node
= isl_ast_node_cow(node
);
1018 if (!node
|| !child
)
1020 if (node
->type
!= isl_ast_node_if
)
1021 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1022 "not an if node", goto error
);
1024 isl_ast_node_free(node
->u
.i
.then
);
1025 node
->u
.i
.then
= child
;
1029 isl_ast_node_free(node
);
1030 isl_ast_node_free(child
);
1034 __isl_give isl_ast_node
*isl_ast_node_if_get_then(
1035 __isl_keep isl_ast_node
*node
)
1039 if (node
->type
!= isl_ast_node_if
)
1040 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1041 "not an if node", return NULL
);
1042 return isl_ast_node_copy(node
->u
.i
.then
);
1045 int isl_ast_node_if_has_else(
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 -1);
1053 return node
->u
.i
.else_node
!= NULL
;
1056 __isl_give isl_ast_node
*isl_ast_node_if_get_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 NULL
);
1064 return isl_ast_node_copy(node
->u
.i
.else_node
);
1067 __isl_give isl_ast_expr
*isl_ast_node_if_get_cond(
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 a guard node", return NULL
);
1075 return isl_ast_expr_copy(node
->u
.i
.guard
);
1078 __isl_give isl_ast_node_list
*isl_ast_node_block_get_children(
1079 __isl_keep isl_ast_node
*node
)
1083 if (node
->type
!= isl_ast_node_block
)
1084 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1085 "not a block node", return NULL
);
1086 return isl_ast_node_list_copy(node
->u
.b
.children
);
1089 __isl_give isl_ast_expr
*isl_ast_node_user_get_expr(
1090 __isl_keep isl_ast_node
*node
)
1095 return isl_ast_expr_copy(node
->u
.e
.expr
);
1098 __isl_give isl_id
*isl_ast_node_get_annotation(__isl_keep isl_ast_node
*node
)
1100 return node
? isl_id_copy(node
->annotation
) : NULL
;
1103 /* Replace node->annotation by "annotation".
1105 __isl_give isl_ast_node
*isl_ast_node_set_annotation(
1106 __isl_take isl_ast_node
*node
, __isl_take isl_id
*annotation
)
1108 node
= isl_ast_node_cow(node
);
1109 if (!node
|| !annotation
)
1112 isl_id_free(node
->annotation
);
1113 node
->annotation
= annotation
;
1117 isl_id_free(annotation
);
1118 return isl_ast_node_free(node
);
1121 /* Textual C representation of the various operators.
1123 static char *op_str
[] = {
1124 [isl_ast_op_and
] = "&&",
1125 [isl_ast_op_and_then
] = "&&",
1126 [isl_ast_op_or
] = "||",
1127 [isl_ast_op_or_else
] = "||",
1128 [isl_ast_op_max
] = "max",
1129 [isl_ast_op_min
] = "min",
1130 [isl_ast_op_minus
] = "-",
1131 [isl_ast_op_add
] = "+",
1132 [isl_ast_op_sub
] = "-",
1133 [isl_ast_op_mul
] = "*",
1134 [isl_ast_op_pdiv_q
] = "/",
1135 [isl_ast_op_pdiv_r
] = "%",
1136 [isl_ast_op_div
] = "/",
1137 [isl_ast_op_eq
] = "==",
1138 [isl_ast_op_le
] = "<=",
1139 [isl_ast_op_ge
] = ">=",
1140 [isl_ast_op_lt
] = "<",
1141 [isl_ast_op_gt
] = ">",
1142 [isl_ast_op_member
] = "."
1145 /* Precedence in C of the various operators.
1146 * Based on http://en.wikipedia.org/wiki/Operators_in_C_and_C++
1147 * Lowest value means highest precedence.
1149 static int op_prec
[] = {
1150 [isl_ast_op_and
] = 13,
1151 [isl_ast_op_and_then
] = 13,
1152 [isl_ast_op_or
] = 14,
1153 [isl_ast_op_or_else
] = 14,
1154 [isl_ast_op_max
] = 2,
1155 [isl_ast_op_min
] = 2,
1156 [isl_ast_op_minus
] = 3,
1157 [isl_ast_op_add
] = 6,
1158 [isl_ast_op_sub
] = 6,
1159 [isl_ast_op_mul
] = 5,
1160 [isl_ast_op_div
] = 5,
1161 [isl_ast_op_fdiv_q
] = 2,
1162 [isl_ast_op_pdiv_q
] = 5,
1163 [isl_ast_op_pdiv_r
] = 5,
1164 [isl_ast_op_cond
] = 15,
1165 [isl_ast_op_select
] = 15,
1166 [isl_ast_op_eq
] = 9,
1167 [isl_ast_op_le
] = 8,
1168 [isl_ast_op_ge
] = 8,
1169 [isl_ast_op_lt
] = 8,
1170 [isl_ast_op_gt
] = 8,
1171 [isl_ast_op_call
] = 2,
1172 [isl_ast_op_access
] = 2,
1173 [isl_ast_op_member
] = 2
1176 /* Is the operator left-to-right associative?
1178 static int op_left
[] = {
1179 [isl_ast_op_and
] = 1,
1180 [isl_ast_op_and_then
] = 1,
1181 [isl_ast_op_or
] = 1,
1182 [isl_ast_op_or_else
] = 1,
1183 [isl_ast_op_max
] = 1,
1184 [isl_ast_op_min
] = 1,
1185 [isl_ast_op_minus
] = 0,
1186 [isl_ast_op_add
] = 1,
1187 [isl_ast_op_sub
] = 1,
1188 [isl_ast_op_mul
] = 1,
1189 [isl_ast_op_div
] = 1,
1190 [isl_ast_op_fdiv_q
] = 1,
1191 [isl_ast_op_pdiv_q
] = 1,
1192 [isl_ast_op_pdiv_r
] = 1,
1193 [isl_ast_op_cond
] = 0,
1194 [isl_ast_op_select
] = 0,
1195 [isl_ast_op_eq
] = 1,
1196 [isl_ast_op_le
] = 1,
1197 [isl_ast_op_ge
] = 1,
1198 [isl_ast_op_lt
] = 1,
1199 [isl_ast_op_gt
] = 1,
1200 [isl_ast_op_call
] = 1,
1201 [isl_ast_op_access
] = 1,
1202 [isl_ast_op_member
] = 1
1205 static int is_and(enum isl_ast_op_type op
)
1207 return op
== isl_ast_op_and
|| op
== isl_ast_op_and_then
;
1210 static int is_or(enum isl_ast_op_type op
)
1212 return op
== isl_ast_op_or
|| op
== isl_ast_op_or_else
;
1215 static int is_add_sub(enum isl_ast_op_type op
)
1217 return op
== isl_ast_op_add
|| op
== isl_ast_op_sub
;
1220 static int is_div_mod(enum isl_ast_op_type op
)
1222 return op
== isl_ast_op_div
|| op
== isl_ast_op_pdiv_r
;
1225 /* Do we need/want parentheses around "expr" as a subexpression of
1226 * an "op" operation? If "left" is set, then "expr" is the left-most
1229 * We only need parentheses if "expr" represents an operation.
1231 * If op has a higher precedence than expr->u.op.op, then we need
1233 * If op and expr->u.op.op have the same precedence, but the operations
1234 * are performed in an order that is different from the associativity,
1235 * then we need parentheses.
1237 * An and inside an or technically does not require parentheses,
1238 * but some compilers complain about that, so we add them anyway.
1240 * Computations such as "a / b * c" and "a % b + c" can be somewhat
1241 * difficult to read, so we add parentheses for those as well.
1243 static int sub_expr_need_parens(enum isl_ast_op_type op
,
1244 __isl_keep isl_ast_expr
*expr
, int left
)
1246 if (expr
->type
!= isl_ast_expr_op
)
1249 if (op_prec
[expr
->u
.op
.op
] > op_prec
[op
])
1251 if (op_prec
[expr
->u
.op
.op
] == op_prec
[op
] && left
!= op_left
[op
])
1254 if (is_or(op
) && is_and(expr
->u
.op
.op
))
1256 if (op
== isl_ast_op_mul
&& expr
->u
.op
.op
!= isl_ast_op_mul
&&
1257 op_prec
[expr
->u
.op
.op
] == op_prec
[op
])
1259 if (is_add_sub(op
) && is_div_mod(expr
->u
.op
.op
))
1265 /* Print "expr" as a subexpression of an "op" operation.
1266 * If "left" is set, then "expr" is the left-most operand.
1268 static __isl_give isl_printer
*print_sub_expr(__isl_take isl_printer
*p
,
1269 enum isl_ast_op_type op
, __isl_keep isl_ast_expr
*expr
, int left
)
1273 need_parens
= sub_expr_need_parens(op
, expr
, left
);
1276 p
= isl_printer_print_str(p
, "(");
1277 p
= isl_printer_print_ast_expr(p
, expr
);
1279 p
= isl_printer_print_str(p
, ")");
1283 /* Print a min or max reduction "expr".
1285 static __isl_give isl_printer
*print_min_max(__isl_take isl_printer
*p
,
1286 __isl_keep isl_ast_expr
*expr
)
1290 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1291 p
= isl_printer_print_str(p
, op_str
[expr
->u
.op
.op
]);
1292 p
= isl_printer_print_str(p
, "(");
1294 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1295 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1296 p
= isl_printer_print_str(p
, ", ");
1297 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[i
]);
1298 p
= isl_printer_print_str(p
, ")");
1304 /* Print a function call "expr".
1306 * The first argument represents the function to be called.
1308 static __isl_give isl_printer
*print_call(__isl_take isl_printer
*p
,
1309 __isl_keep isl_ast_expr
*expr
)
1313 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1314 p
= isl_printer_print_str(p
, "(");
1315 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1317 p
= isl_printer_print_str(p
, ", ");
1318 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[i
]);
1320 p
= isl_printer_print_str(p
, ")");
1325 /* Print an array access "expr".
1327 * The first argument represents the array being accessed.
1329 static __isl_give isl_printer
*print_access(__isl_take isl_printer
*p
,
1330 __isl_keep isl_ast_expr
*expr
)
1334 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1335 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1336 p
= isl_printer_print_str(p
, "[");
1337 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[i
]);
1338 p
= isl_printer_print_str(p
, "]");
1344 /* Print "expr" to "p".
1346 * If we are printing in isl format, then we also print an indication
1347 * of the size of the expression (if it was computed).
1349 __isl_give isl_printer
*isl_printer_print_ast_expr(__isl_take isl_printer
*p
,
1350 __isl_keep isl_ast_expr
*expr
)
1355 return isl_printer_free(p
);
1357 switch (expr
->type
) {
1358 case isl_ast_expr_op
:
1359 if (expr
->u
.op
.op
== isl_ast_op_call
) {
1360 p
= print_call(p
, expr
);
1363 if (expr
->u
.op
.op
== isl_ast_op_access
) {
1364 p
= print_access(p
, expr
);
1367 if (expr
->u
.op
.n_arg
== 1) {
1368 p
= isl_printer_print_str(p
, op_str
[expr
->u
.op
.op
]);
1369 p
= print_sub_expr(p
, expr
->u
.op
.op
,
1370 expr
->u
.op
.args
[0], 0);
1373 if (expr
->u
.op
.op
== isl_ast_op_fdiv_q
) {
1374 p
= isl_printer_print_str(p
, "floord(");
1375 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1376 p
= isl_printer_print_str(p
, ", ");
1377 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[1]);
1378 p
= isl_printer_print_str(p
, ")");
1381 if (expr
->u
.op
.op
== isl_ast_op_max
||
1382 expr
->u
.op
.op
== isl_ast_op_min
) {
1383 p
= print_min_max(p
, expr
);
1386 if (expr
->u
.op
.op
== isl_ast_op_cond
||
1387 expr
->u
.op
.op
== isl_ast_op_select
) {
1388 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1389 p
= isl_printer_print_str(p
, " ? ");
1390 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[1]);
1391 p
= isl_printer_print_str(p
, " : ");
1392 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[2]);
1395 if (expr
->u
.op
.n_arg
!= 2)
1396 isl_die(isl_printer_get_ctx(p
), isl_error_internal
,
1397 "operation should have two arguments",
1399 p
= print_sub_expr(p
, expr
->u
.op
.op
, expr
->u
.op
.args
[0], 1);
1400 if (expr
->u
.op
.op
!= isl_ast_op_member
)
1401 p
= isl_printer_print_str(p
, " ");
1402 p
= isl_printer_print_str(p
, op_str
[expr
->u
.op
.op
]);
1403 if (expr
->u
.op
.op
!= isl_ast_op_member
)
1404 p
= isl_printer_print_str(p
, " ");
1405 p
= print_sub_expr(p
, expr
->u
.op
.op
, expr
->u
.op
.args
[1], 0);
1407 case isl_ast_expr_id
:
1408 p
= isl_printer_print_str(p
, isl_id_get_name(expr
->u
.id
));
1410 case isl_ast_expr_int
:
1411 p
= isl_printer_print_val(p
, expr
->u
.v
);
1413 case isl_ast_expr_error
:
1419 isl_printer_free(p
);
1423 /* Print "node" to "p" in "isl format".
1425 static __isl_give isl_printer
*print_ast_node_isl(__isl_take isl_printer
*p
,
1426 __isl_keep isl_ast_node
*node
)
1428 p
= isl_printer_print_str(p
, "(");
1429 switch (node
->type
) {
1430 case isl_ast_node_for
:
1431 if (node
->u
.f
.degenerate
) {
1432 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1434 p
= isl_printer_print_str(p
, "init: ");
1435 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1436 p
= isl_printer_print_str(p
, ", ");
1437 p
= isl_printer_print_str(p
, "cond: ");
1438 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.cond
);
1439 p
= isl_printer_print_str(p
, ", ");
1440 p
= isl_printer_print_str(p
, "inc: ");
1441 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.inc
);
1443 if (node
->u
.f
.body
) {
1444 p
= isl_printer_print_str(p
, ", ");
1445 p
= isl_printer_print_str(p
, "body: ");
1446 p
= isl_printer_print_ast_node(p
, node
->u
.f
.body
);
1449 case isl_ast_node_user
:
1450 p
= isl_printer_print_ast_expr(p
, node
->u
.e
.expr
);
1452 case isl_ast_node_if
:
1453 p
= isl_printer_print_str(p
, "guard: ");
1454 p
= isl_printer_print_ast_expr(p
, node
->u
.i
.guard
);
1455 if (node
->u
.i
.then
) {
1456 p
= isl_printer_print_str(p
, ", ");
1457 p
= isl_printer_print_str(p
, "then: ");
1458 p
= isl_printer_print_ast_node(p
, node
->u
.i
.then
);
1460 if (node
->u
.i
.else_node
) {
1461 p
= isl_printer_print_str(p
, ", ");
1462 p
= isl_printer_print_str(p
, "else: ");
1463 p
= isl_printer_print_ast_node(p
, node
->u
.i
.else_node
);
1466 case isl_ast_node_block
:
1467 p
= isl_printer_print_ast_node_list(p
, node
->u
.b
.children
);
1472 p
= isl_printer_print_str(p
, ")");
1476 /* Do we need to print a block around the body "node" of a for or if node?
1478 * If the node is a block, then we need to print a block.
1479 * Also if the node is a degenerate for then we will print it as
1480 * an assignment followed by the body of the for loop, so we need a block
1482 * If the node is an if node with an else, then we print a block
1483 * to avoid spurious dangling else warnings emitted by some compilers.
1485 static int need_block(__isl_keep isl_ast_node
*node
)
1487 if (node
->type
== isl_ast_node_block
)
1489 if (node
->type
== isl_ast_node_for
&& node
->u
.f
.degenerate
)
1491 if (node
->type
== isl_ast_node_if
&& node
->u
.i
.else_node
)
1496 static __isl_give isl_printer
*print_ast_node_c(__isl_take isl_printer
*p
,
1497 __isl_keep isl_ast_node
*node
,
1498 __isl_keep isl_ast_print_options
*options
, int in_block
, int in_list
);
1499 static __isl_give isl_printer
*print_if_c(__isl_take isl_printer
*p
,
1500 __isl_keep isl_ast_node
*node
,
1501 __isl_keep isl_ast_print_options
*options
, int new_line
);
1503 /* Print the body "node" of a for or if node.
1504 * If "else_node" is set, then it is printed as well.
1506 * We first check if we need to print out a block.
1507 * We always print out a block if there is an else node to make
1508 * sure that the else node is matched to the correct if node.
1510 * If the else node is itself an if, then we print it as
1514 * Otherwise the else node is printed as
1519 static __isl_give isl_printer
*print_body_c(__isl_take isl_printer
*p
,
1520 __isl_keep isl_ast_node
*node
, __isl_keep isl_ast_node
*else_node
,
1521 __isl_keep isl_ast_print_options
*options
)
1524 return isl_printer_free(p
);
1526 if (!else_node
&& !need_block(node
)) {
1527 p
= isl_printer_end_line(p
);
1528 p
= isl_printer_indent(p
, 2);
1529 p
= isl_ast_node_print(node
, p
,
1530 isl_ast_print_options_copy(options
));
1531 p
= isl_printer_indent(p
, -2);
1535 p
= isl_printer_print_str(p
, " {");
1536 p
= isl_printer_end_line(p
);
1537 p
= isl_printer_indent(p
, 2);
1538 p
= print_ast_node_c(p
, node
, options
, 1, 0);
1539 p
= isl_printer_indent(p
, -2);
1540 p
= isl_printer_start_line(p
);
1541 p
= isl_printer_print_str(p
, "}");
1543 if (else_node
->type
== isl_ast_node_if
) {
1544 p
= isl_printer_print_str(p
, " else ");
1545 p
= print_if_c(p
, else_node
, options
, 0);
1547 p
= isl_printer_print_str(p
, " else");
1548 p
= print_body_c(p
, else_node
, NULL
, options
);
1551 p
= isl_printer_end_line(p
);
1556 /* Print the start of a compound statement.
1558 static __isl_give isl_printer
*start_block(__isl_take isl_printer
*p
)
1560 p
= isl_printer_start_line(p
);
1561 p
= isl_printer_print_str(p
, "{");
1562 p
= isl_printer_end_line(p
);
1563 p
= isl_printer_indent(p
, 2);
1568 /* Print the end of a compound statement.
1570 static __isl_give isl_printer
*end_block(__isl_take isl_printer
*p
)
1572 p
= isl_printer_indent(p
, -2);
1573 p
= isl_printer_start_line(p
);
1574 p
= isl_printer_print_str(p
, "}");
1575 p
= isl_printer_end_line(p
);
1580 /* Print the for node "node".
1582 * If the for node is degenerate, it is printed as
1584 * type iterator = init;
1587 * Otherwise, it is printed as
1589 * for (type iterator = init; cond; iterator += inc)
1592 * "in_block" is set if we are currently inside a block.
1593 * "in_list" is set if the current node is not alone in the block.
1594 * If we are not in a block or if the current not is not alone in the block
1595 * then we print a block around a degenerate for loop such that the variable
1596 * declaration will not conflict with any potential other declaration
1597 * of the same variable.
1599 static __isl_give isl_printer
*print_for_c(__isl_take isl_printer
*p
,
1600 __isl_keep isl_ast_node
*node
,
1601 __isl_keep isl_ast_print_options
*options
, int in_block
, int in_list
)
1607 type
= isl_options_get_ast_iterator_type(isl_printer_get_ctx(p
));
1608 if (!node
->u
.f
.degenerate
) {
1609 id
= isl_ast_expr_get_id(node
->u
.f
.iterator
);
1610 name
= isl_id_get_name(id
);
1612 p
= isl_printer_start_line(p
);
1613 p
= isl_printer_print_str(p
, "for (");
1614 p
= isl_printer_print_str(p
, type
);
1615 p
= isl_printer_print_str(p
, " ");
1616 p
= isl_printer_print_str(p
, name
);
1617 p
= isl_printer_print_str(p
, " = ");
1618 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1619 p
= isl_printer_print_str(p
, "; ");
1620 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.cond
);
1621 p
= isl_printer_print_str(p
, "; ");
1622 p
= isl_printer_print_str(p
, name
);
1623 p
= isl_printer_print_str(p
, " += ");
1624 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.inc
);
1625 p
= isl_printer_print_str(p
, ")");
1626 p
= print_body_c(p
, node
->u
.f
.body
, NULL
, options
);
1628 id
= isl_ast_expr_get_id(node
->u
.f
.iterator
);
1629 name
= isl_id_get_name(id
);
1631 if (!in_block
|| in_list
)
1633 p
= isl_printer_start_line(p
);
1634 p
= isl_printer_print_str(p
, type
);
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
.init
);
1639 p
= isl_printer_print_str(p
, ";");
1640 p
= isl_printer_end_line(p
);
1641 p
= print_ast_node_c(p
, node
->u
.f
.body
, options
, 1, 0);
1642 if (!in_block
|| in_list
)
1649 /* Print the if node "node".
1650 * If "new_line" is set then the if node should be printed on a new line.
1652 static __isl_give isl_printer
*print_if_c(__isl_take isl_printer
*p
,
1653 __isl_keep isl_ast_node
*node
,
1654 __isl_keep isl_ast_print_options
*options
, int new_line
)
1657 p
= isl_printer_start_line(p
);
1658 p
= isl_printer_print_str(p
, "if (");
1659 p
= isl_printer_print_ast_expr(p
, node
->u
.i
.guard
);
1660 p
= isl_printer_print_str(p
, ")");
1661 p
= print_body_c(p
, node
->u
.i
.then
, node
->u
.i
.else_node
, options
);
1666 /* Print the "node" to "p".
1668 * "in_block" is set if we are currently inside a block.
1669 * If so, we do not print a block around the children of a block node.
1670 * We do this to avoid an extra block around the body of a degenerate
1673 * "in_list" is set if the current node is not alone in the block.
1675 static __isl_give isl_printer
*print_ast_node_c(__isl_take isl_printer
*p
,
1676 __isl_keep isl_ast_node
*node
,
1677 __isl_keep isl_ast_print_options
*options
, int in_block
, int in_list
)
1679 switch (node
->type
) {
1680 case isl_ast_node_for
:
1681 if (options
->print_for
)
1682 return options
->print_for(p
,
1683 isl_ast_print_options_copy(options
),
1684 node
, options
->print_for_user
);
1685 p
= print_for_c(p
, node
, options
, in_block
, in_list
);
1687 case isl_ast_node_if
:
1688 p
= print_if_c(p
, node
, options
, 1);
1690 case isl_ast_node_block
:
1693 p
= isl_ast_node_list_print(node
->u
.b
.children
, p
, options
);
1697 case isl_ast_node_user
:
1698 if (options
->print_user
)
1699 return options
->print_user(p
,
1700 isl_ast_print_options_copy(options
),
1701 node
, options
->print_user_user
);
1702 p
= isl_printer_start_line(p
);
1703 p
= isl_printer_print_ast_expr(p
, node
->u
.e
.expr
);
1704 p
= isl_printer_print_str(p
, ";");
1705 p
= isl_printer_end_line(p
);
1707 case isl_ast_node_error
:
1713 /* Print the for node "node" to "p".
1715 __isl_give isl_printer
*isl_ast_node_for_print(__isl_keep isl_ast_node
*node
,
1716 __isl_take isl_printer
*p
, __isl_take isl_ast_print_options
*options
)
1718 if (!node
|| !options
)
1720 if (node
->type
!= isl_ast_node_for
)
1721 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1722 "not a for node", goto error
);
1723 p
= print_for_c(p
, node
, options
, 0, 0);
1724 isl_ast_print_options_free(options
);
1727 isl_ast_print_options_free(options
);
1728 isl_printer_free(p
);
1732 /* Print the if node "node" to "p".
1734 __isl_give isl_printer
*isl_ast_node_if_print(__isl_keep isl_ast_node
*node
,
1735 __isl_take isl_printer
*p
, __isl_take isl_ast_print_options
*options
)
1737 if (!node
|| !options
)
1739 if (node
->type
!= isl_ast_node_if
)
1740 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1741 "not an if node", goto error
);
1742 p
= print_if_c(p
, node
, options
, 1);
1743 isl_ast_print_options_free(options
);
1746 isl_ast_print_options_free(options
);
1747 isl_printer_free(p
);
1751 /* Print "node" to "p".
1753 __isl_give isl_printer
*isl_ast_node_print(__isl_keep isl_ast_node
*node
,
1754 __isl_take isl_printer
*p
, __isl_take isl_ast_print_options
*options
)
1756 if (!options
|| !node
)
1758 p
= print_ast_node_c(p
, node
, options
, 0, 0);
1759 isl_ast_print_options_free(options
);
1762 isl_ast_print_options_free(options
);
1763 isl_printer_free(p
);
1767 /* Print "node" to "p".
1769 __isl_give isl_printer
*isl_printer_print_ast_node(__isl_take isl_printer
*p
,
1770 __isl_keep isl_ast_node
*node
)
1773 isl_ast_print_options
*options
;
1778 format
= isl_printer_get_output_format(p
);
1780 case ISL_FORMAT_ISL
:
1781 p
= print_ast_node_isl(p
, node
);
1784 options
= isl_ast_print_options_alloc(isl_printer_get_ctx(p
));
1785 p
= isl_ast_node_print(node
, p
, options
);
1788 isl_die(isl_printer_get_ctx(p
), isl_error_unsupported
,
1789 "output format not supported for ast_node",
1790 return isl_printer_free(p
));
1796 /* Print the list of nodes "list" to "p".
1798 __isl_give isl_printer
*isl_ast_node_list_print(
1799 __isl_keep isl_ast_node_list
*list
, __isl_take isl_printer
*p
,
1800 __isl_keep isl_ast_print_options
*options
)
1804 if (!p
|| !list
|| !options
)
1805 return isl_printer_free(p
);
1807 for (i
= 0; i
< list
->n
; ++i
)
1808 p
= print_ast_node_c(p
, list
->p
[i
], options
, 1, 1);
1813 #define ISL_AST_MACRO_FLOORD (1 << 0)
1814 #define ISL_AST_MACRO_MIN (1 << 1)
1815 #define ISL_AST_MACRO_MAX (1 << 2)
1816 #define ISL_AST_MACRO_ALL (ISL_AST_MACRO_FLOORD | \
1817 ISL_AST_MACRO_MIN | \
1820 /* If "expr" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1821 * then set the corresponding bit in "macros".
1823 static int ast_expr_required_macros(__isl_keep isl_ast_expr
*expr
, int macros
)
1827 if (macros
== ISL_AST_MACRO_ALL
)
1830 if (expr
->type
!= isl_ast_expr_op
)
1833 if (expr
->u
.op
.op
== isl_ast_op_min
)
1834 macros
|= ISL_AST_MACRO_MIN
;
1835 if (expr
->u
.op
.op
== isl_ast_op_max
)
1836 macros
|= ISL_AST_MACRO_MAX
;
1837 if (expr
->u
.op
.op
== isl_ast_op_fdiv_q
)
1838 macros
|= ISL_AST_MACRO_FLOORD
;
1840 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
)
1841 macros
= ast_expr_required_macros(expr
->u
.op
.args
[i
], macros
);
1846 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list
*list
,
1849 /* If "node" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1850 * then set the corresponding bit in "macros".
1852 static int ast_node_required_macros(__isl_keep isl_ast_node
*node
, int macros
)
1854 if (macros
== ISL_AST_MACRO_ALL
)
1857 switch (node
->type
) {
1858 case isl_ast_node_for
:
1859 macros
= ast_expr_required_macros(node
->u
.f
.init
, macros
);
1860 if (!node
->u
.f
.degenerate
) {
1861 macros
= ast_expr_required_macros(node
->u
.f
.cond
,
1863 macros
= ast_expr_required_macros(node
->u
.f
.inc
,
1866 macros
= ast_node_required_macros(node
->u
.f
.body
, macros
);
1868 case isl_ast_node_if
:
1869 macros
= ast_expr_required_macros(node
->u
.i
.guard
, macros
);
1870 macros
= ast_node_required_macros(node
->u
.i
.then
, macros
);
1871 if (node
->u
.i
.else_node
)
1872 macros
= ast_node_required_macros(node
->u
.i
.else_node
,
1875 case isl_ast_node_block
:
1876 macros
= ast_node_list_required_macros(node
->u
.b
.children
,
1879 case isl_ast_node_user
:
1880 macros
= ast_expr_required_macros(node
->u
.e
.expr
, macros
);
1882 case isl_ast_node_error
:
1889 /* If "list" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1890 * then set the corresponding bit in "macros".
1892 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list
*list
,
1897 for (i
= 0; i
< list
->n
; ++i
)
1898 macros
= ast_node_required_macros(list
->p
[i
], macros
);
1903 /* Print a macro definition for the operator "type".
1905 __isl_give isl_printer
*isl_ast_op_type_print_macro(
1906 enum isl_ast_op_type type
, __isl_take isl_printer
*p
)
1909 case isl_ast_op_min
:
1910 p
= isl_printer_start_line(p
);
1911 p
= isl_printer_print_str(p
,
1912 "#define min(x,y) ((x) < (y) ? (x) : (y))");
1913 p
= isl_printer_end_line(p
);
1915 case isl_ast_op_max
:
1916 p
= isl_printer_start_line(p
);
1917 p
= isl_printer_print_str(p
,
1918 "#define max(x,y) ((x) > (y) ? (x) : (y))");
1919 p
= isl_printer_end_line(p
);
1921 case isl_ast_op_fdiv_q
:
1922 p
= isl_printer_start_line(p
);
1923 p
= isl_printer_print_str(p
,
1924 "#define floord(n,d) "
1925 "(((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))");
1926 p
= isl_printer_end_line(p
);
1935 /* Call "fn" for each type of operation that appears in "node"
1936 * and that requires a macro definition.
1938 int isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node
*node
,
1939 int (*fn
)(enum isl_ast_op_type type
, void *user
), void *user
)
1946 macros
= ast_node_required_macros(node
, 0);
1948 if (macros
& ISL_AST_MACRO_MIN
&& fn(isl_ast_op_min
, user
) < 0)
1950 if (macros
& ISL_AST_MACRO_MAX
&& fn(isl_ast_op_max
, user
) < 0)
1952 if (macros
& ISL_AST_MACRO_FLOORD
&& fn(isl_ast_op_fdiv_q
, user
) < 0)
1958 static int ast_op_type_print_macro(enum isl_ast_op_type type
, void *user
)
1960 isl_printer
**p
= user
;
1962 *p
= isl_ast_op_type_print_macro(type
, *p
);
1967 /* Print macro definitions for all the macros used in the result
1968 * of printing "node.
1970 __isl_give isl_printer
*isl_ast_node_print_macros(
1971 __isl_keep isl_ast_node
*node
, __isl_take isl_printer
*p
)
1973 if (isl_ast_node_foreach_ast_op_type(node
,
1974 &ast_op_type_print_macro
, &p
) < 0)
1975 return isl_printer_free(p
);