2 * Copyright 2012-2013 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
10 #include <isl_ast_private.h>
15 #include <isl_list_templ.c>
20 #include <isl_list_templ.c>
22 isl_ctx
*isl_ast_print_options_get_ctx(
23 __isl_keep isl_ast_print_options
*options
)
25 return options
? options
->ctx
: NULL
;
28 __isl_give isl_ast_print_options
*isl_ast_print_options_alloc(isl_ctx
*ctx
)
30 isl_ast_print_options
*options
;
32 options
= isl_calloc_type(ctx
, isl_ast_print_options
);
43 __isl_give isl_ast_print_options
*isl_ast_print_options_dup(
44 __isl_keep isl_ast_print_options
*options
)
47 isl_ast_print_options
*dup
;
52 ctx
= isl_ast_print_options_get_ctx(options
);
53 dup
= isl_ast_print_options_alloc(ctx
);
57 dup
->print_for
= options
->print_for
;
58 dup
->print_for_user
= options
->print_for_user
;
59 dup
->print_user
= options
->print_user
;
60 dup
->print_user_user
= options
->print_user_user
;
65 __isl_give isl_ast_print_options
*isl_ast_print_options_cow(
66 __isl_take isl_ast_print_options
*options
)
71 if (options
->ref
== 1)
74 return isl_ast_print_options_dup(options
);
77 __isl_give isl_ast_print_options
*isl_ast_print_options_copy(
78 __isl_keep isl_ast_print_options
*options
)
87 __isl_null isl_ast_print_options
*isl_ast_print_options_free(
88 __isl_take isl_ast_print_options
*options
)
93 if (--options
->ref
> 0)
96 isl_ctx_deref(options
->ctx
);
102 /* Set the print_user callback of "options" to "print_user".
104 * If this callback is set, then it used to print user nodes in the AST.
105 * Otherwise, the expression associated to the user node is printed.
107 __isl_give isl_ast_print_options
*isl_ast_print_options_set_print_user(
108 __isl_take isl_ast_print_options
*options
,
109 __isl_give isl_printer
*(*print_user
)(__isl_take isl_printer
*p
,
110 __isl_take isl_ast_print_options
*options
,
111 __isl_keep isl_ast_node
*node
, void *user
),
114 options
= isl_ast_print_options_cow(options
);
118 options
->print_user
= print_user
;
119 options
->print_user_user
= user
;
124 /* Set the print_for callback of "options" to "print_for".
126 * If this callback is set, then it used to print for nodes in the AST.
128 __isl_give isl_ast_print_options
*isl_ast_print_options_set_print_for(
129 __isl_take isl_ast_print_options
*options
,
130 __isl_give isl_printer
*(*print_for
)(__isl_take isl_printer
*p
,
131 __isl_take isl_ast_print_options
*options
,
132 __isl_keep isl_ast_node
*node
, void *user
),
135 options
= isl_ast_print_options_cow(options
);
139 options
->print_for
= print_for
;
140 options
->print_for_user
= user
;
145 __isl_give isl_ast_expr
*isl_ast_expr_copy(__isl_keep isl_ast_expr
*expr
)
154 __isl_give isl_ast_expr
*isl_ast_expr_dup(__isl_keep isl_ast_expr
*expr
)
163 ctx
= isl_ast_expr_get_ctx(expr
);
164 switch (expr
->type
) {
165 case isl_ast_expr_int
:
166 dup
= isl_ast_expr_from_val(isl_val_copy(expr
->u
.v
));
168 case isl_ast_expr_id
:
169 dup
= isl_ast_expr_from_id(isl_id_copy(expr
->u
.id
));
171 case isl_ast_expr_op
:
172 dup
= isl_ast_expr_alloc_op(ctx
,
173 expr
->u
.op
.op
, expr
->u
.op
.n_arg
);
176 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
)
178 isl_ast_expr_copy(expr
->u
.op
.args
[i
]);
180 case isl_ast_expr_error
:
190 __isl_give isl_ast_expr
*isl_ast_expr_cow(__isl_take isl_ast_expr
*expr
)
198 return isl_ast_expr_dup(expr
);
201 __isl_null isl_ast_expr
*isl_ast_expr_free(__isl_take isl_ast_expr
*expr
)
211 isl_ctx_deref(expr
->ctx
);
213 switch (expr
->type
) {
214 case isl_ast_expr_int
:
215 isl_val_free(expr
->u
.v
);
217 case isl_ast_expr_id
:
218 isl_id_free(expr
->u
.id
);
220 case isl_ast_expr_op
:
222 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
)
223 isl_ast_expr_free(expr
->u
.op
.args
[i
]);
224 free(expr
->u
.op
.args
);
226 case isl_ast_expr_error
:
234 isl_ctx
*isl_ast_expr_get_ctx(__isl_keep isl_ast_expr
*expr
)
236 return expr
? expr
->ctx
: NULL
;
239 enum isl_ast_expr_type
isl_ast_expr_get_type(__isl_keep isl_ast_expr
*expr
)
241 return expr
? expr
->type
: isl_ast_expr_error
;
244 /* Return the integer value represented by "expr".
246 __isl_give isl_val
*isl_ast_expr_get_val(__isl_keep isl_ast_expr
*expr
)
250 if (expr
->type
!= isl_ast_expr_int
)
251 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
252 "expression not an int", return NULL
);
253 return isl_val_copy(expr
->u
.v
);
256 __isl_give isl_id
*isl_ast_expr_get_id(__isl_keep isl_ast_expr
*expr
)
260 if (expr
->type
!= isl_ast_expr_id
)
261 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
262 "expression not an identifier", return NULL
);
264 return isl_id_copy(expr
->u
.id
);
267 enum isl_ast_op_type
isl_ast_expr_get_op_type(__isl_keep isl_ast_expr
*expr
)
270 return isl_ast_op_error
;
271 if (expr
->type
!= isl_ast_expr_op
)
272 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
273 "expression not an operation", return isl_ast_op_error
);
274 return expr
->u
.op
.op
;
277 int isl_ast_expr_get_op_n_arg(__isl_keep isl_ast_expr
*expr
)
281 if (expr
->type
!= isl_ast_expr_op
)
282 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
283 "expression not an operation", return -1);
284 return expr
->u
.op
.n_arg
;
287 __isl_give isl_ast_expr
*isl_ast_expr_get_op_arg(__isl_keep isl_ast_expr
*expr
,
292 if (expr
->type
!= isl_ast_expr_op
)
293 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
294 "expression not an operation", return NULL
);
295 if (pos
< 0 || pos
>= expr
->u
.op
.n_arg
)
296 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
297 "index out of bounds", return NULL
);
299 return isl_ast_expr_copy(expr
->u
.op
.args
[pos
]);
302 /* Replace the argument at position "pos" of "expr" by "arg".
304 __isl_give isl_ast_expr
*isl_ast_expr_set_op_arg(__isl_take isl_ast_expr
*expr
,
305 int pos
, __isl_take isl_ast_expr
*arg
)
307 expr
= isl_ast_expr_cow(expr
);
310 if (expr
->type
!= isl_ast_expr_op
)
311 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
312 "expression not an operation", goto error
);
313 if (pos
< 0 || pos
>= expr
->u
.op
.n_arg
)
314 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
315 "index out of bounds", goto error
);
317 isl_ast_expr_free(expr
->u
.op
.args
[pos
]);
318 expr
->u
.op
.args
[pos
] = arg
;
322 isl_ast_expr_free(arg
);
323 return isl_ast_expr_free(expr
);
326 /* Is "expr1" equal to "expr2"?
328 int isl_ast_expr_is_equal(__isl_keep isl_ast_expr
*expr1
,
329 __isl_keep isl_ast_expr
*expr2
)
333 if (!expr1
|| !expr2
)
338 if (expr1
->type
!= expr2
->type
)
340 switch (expr1
->type
) {
341 case isl_ast_expr_int
:
342 return isl_val_eq(expr1
->u
.v
, expr2
->u
.v
);
343 case isl_ast_expr_id
:
344 return expr1
->u
.id
== expr2
->u
.id
;
345 case isl_ast_expr_op
:
346 if (expr1
->u
.op
.op
!= expr2
->u
.op
.op
)
348 if (expr1
->u
.op
.n_arg
!= expr2
->u
.op
.n_arg
)
350 for (i
= 0; i
< expr1
->u
.op
.n_arg
; ++i
) {
352 equal
= isl_ast_expr_is_equal(expr1
->u
.op
.args
[i
],
353 expr2
->u
.op
.args
[i
]);
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 unary operation "type" applied to
470 __isl_give isl_ast_expr
*isl_ast_expr_alloc_unary(enum isl_ast_op_type type
,
471 __isl_take isl_ast_expr
*arg
)
474 isl_ast_expr
*expr
= NULL
;
479 ctx
= isl_ast_expr_get_ctx(arg
);
480 expr
= isl_ast_expr_alloc_op(ctx
, type
, 1);
484 expr
->u
.op
.args
[0] = arg
;
488 isl_ast_expr_free(arg
);
492 /* Create an expression representing the negation of "arg".
494 __isl_give isl_ast_expr
*isl_ast_expr_neg(__isl_take isl_ast_expr
*arg
)
496 return isl_ast_expr_alloc_unary(isl_ast_op_minus
, arg
);
499 /* Create an expression representing the address of "expr".
501 __isl_give isl_ast_expr
*isl_ast_expr_address_of(__isl_take isl_ast_expr
*expr
)
506 if (isl_ast_expr_get_type(expr
) != isl_ast_expr_op
||
507 isl_ast_expr_get_op_type(expr
) != isl_ast_op_access
)
508 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
509 "can only take address of access expressions",
510 return isl_ast_expr_free(expr
));
512 return isl_ast_expr_alloc_unary(isl_ast_op_address_of
, expr
);
515 /* Create an expression representing the binary operation "type"
516 * applied to "expr1" and "expr2".
518 __isl_give isl_ast_expr
*isl_ast_expr_alloc_binary(enum isl_ast_op_type type
,
519 __isl_take isl_ast_expr
*expr1
, __isl_take isl_ast_expr
*expr2
)
522 isl_ast_expr
*expr
= NULL
;
524 if (!expr1
|| !expr2
)
527 ctx
= isl_ast_expr_get_ctx(expr1
);
528 expr
= isl_ast_expr_alloc_op(ctx
, type
, 2);
532 expr
->u
.op
.args
[0] = expr1
;
533 expr
->u
.op
.args
[1] = expr2
;
537 isl_ast_expr_free(expr1
);
538 isl_ast_expr_free(expr2
);
542 /* Create an expression representing the sum of "expr1" and "expr2".
544 __isl_give isl_ast_expr
*isl_ast_expr_add(__isl_take isl_ast_expr
*expr1
,
545 __isl_take isl_ast_expr
*expr2
)
547 return isl_ast_expr_alloc_binary(isl_ast_op_add
, expr1
, expr2
);
550 /* Create an expression representing the difference of "expr1" and "expr2".
552 __isl_give isl_ast_expr
*isl_ast_expr_sub(__isl_take isl_ast_expr
*expr1
,
553 __isl_take isl_ast_expr
*expr2
)
555 return isl_ast_expr_alloc_binary(isl_ast_op_sub
, expr1
, expr2
);
558 /* Create an expression representing the product of "expr1" and "expr2".
560 __isl_give isl_ast_expr
*isl_ast_expr_mul(__isl_take isl_ast_expr
*expr1
,
561 __isl_take isl_ast_expr
*expr2
)
563 return isl_ast_expr_alloc_binary(isl_ast_op_mul
, expr1
, expr2
);
566 /* Create an expression representing the quotient of "expr1" and "expr2".
568 __isl_give isl_ast_expr
*isl_ast_expr_div(__isl_take isl_ast_expr
*expr1
,
569 __isl_take isl_ast_expr
*expr2
)
571 return isl_ast_expr_alloc_binary(isl_ast_op_div
, expr1
, expr2
);
574 /* Create an expression representing the quotient of the integer
575 * division of "expr1" by "expr2", where "expr1" is known to be
578 __isl_give isl_ast_expr
*isl_ast_expr_pdiv_q(__isl_take isl_ast_expr
*expr1
,
579 __isl_take isl_ast_expr
*expr2
)
581 return isl_ast_expr_alloc_binary(isl_ast_op_pdiv_q
, expr1
, expr2
);
584 /* Create an expression representing the remainder of the integer
585 * division of "expr1" by "expr2", where "expr1" is known to be
588 __isl_give isl_ast_expr
*isl_ast_expr_pdiv_r(__isl_take isl_ast_expr
*expr1
,
589 __isl_take isl_ast_expr
*expr2
)
591 return isl_ast_expr_alloc_binary(isl_ast_op_pdiv_r
, expr1
, expr2
);
594 /* Create an expression representing the conjunction of "expr1" and "expr2".
596 __isl_give isl_ast_expr
*isl_ast_expr_and(__isl_take isl_ast_expr
*expr1
,
597 __isl_take isl_ast_expr
*expr2
)
599 return isl_ast_expr_alloc_binary(isl_ast_op_and
, expr1
, expr2
);
602 /* Create an expression representing the conjunction of "expr1" and "expr2",
603 * where "expr2" is evaluated only if "expr1" is evaluated to true.
605 __isl_give isl_ast_expr
*isl_ast_expr_and_then(__isl_take isl_ast_expr
*expr1
,
606 __isl_take isl_ast_expr
*expr2
)
608 return isl_ast_expr_alloc_binary(isl_ast_op_and_then
, expr1
, expr2
);
611 /* Create an expression representing the disjunction of "expr1" and "expr2".
613 __isl_give isl_ast_expr
*isl_ast_expr_or(__isl_take isl_ast_expr
*expr1
,
614 __isl_take isl_ast_expr
*expr2
)
616 return isl_ast_expr_alloc_binary(isl_ast_op_or
, expr1
, expr2
);
619 /* Create an expression representing the disjunction of "expr1" and "expr2",
620 * where "expr2" is evaluated only if "expr1" is evaluated to false.
622 __isl_give isl_ast_expr
*isl_ast_expr_or_else(__isl_take isl_ast_expr
*expr1
,
623 __isl_take isl_ast_expr
*expr2
)
625 return isl_ast_expr_alloc_binary(isl_ast_op_or_else
, expr1
, expr2
);
628 /* Create an expression representing "expr1" less than or equal to "expr2".
630 __isl_give isl_ast_expr
*isl_ast_expr_le(__isl_take isl_ast_expr
*expr1
,
631 __isl_take isl_ast_expr
*expr2
)
633 return isl_ast_expr_alloc_binary(isl_ast_op_le
, expr1
, expr2
);
636 /* Create an expression representing "expr1" less than "expr2".
638 __isl_give isl_ast_expr
*isl_ast_expr_lt(__isl_take isl_ast_expr
*expr1
,
639 __isl_take isl_ast_expr
*expr2
)
641 return isl_ast_expr_alloc_binary(isl_ast_op_lt
, expr1
, expr2
);
644 /* Create an expression representing "expr1" greater than or equal to "expr2".
646 __isl_give isl_ast_expr
*isl_ast_expr_ge(__isl_take isl_ast_expr
*expr1
,
647 __isl_take isl_ast_expr
*expr2
)
649 return isl_ast_expr_alloc_binary(isl_ast_op_ge
, expr1
, expr2
);
652 /* Create an expression representing "expr1" greater than "expr2".
654 __isl_give isl_ast_expr
*isl_ast_expr_gt(__isl_take isl_ast_expr
*expr1
,
655 __isl_take isl_ast_expr
*expr2
)
657 return isl_ast_expr_alloc_binary(isl_ast_op_gt
, expr1
, expr2
);
660 /* Create an expression representing "expr1" equal to "expr2".
662 __isl_give isl_ast_expr
*isl_ast_expr_eq(__isl_take isl_ast_expr
*expr1
,
663 __isl_take isl_ast_expr
*expr2
)
665 return isl_ast_expr_alloc_binary(isl_ast_op_eq
, expr1
, expr2
);
668 /* Create an expression of type "type" with as arguments "arg0" followed
671 static __isl_give isl_ast_expr
*ast_expr_with_arguments(
672 enum isl_ast_op_type type
, __isl_take isl_ast_expr
*arg0
,
673 __isl_take isl_ast_expr_list
*arguments
)
677 isl_ast_expr
*res
= NULL
;
679 if (!arg0
|| !arguments
)
682 ctx
= isl_ast_expr_get_ctx(arg0
);
683 n
= isl_ast_expr_list_n_ast_expr(arguments
);
684 res
= isl_ast_expr_alloc_op(ctx
, type
, 1 + n
);
687 for (i
= 0; i
< n
; ++i
) {
689 arg
= isl_ast_expr_list_get_ast_expr(arguments
, i
);
690 res
->u
.op
.args
[1 + i
] = arg
;
694 res
->u
.op
.args
[0] = arg0
;
696 isl_ast_expr_list_free(arguments
);
699 isl_ast_expr_free(arg0
);
700 isl_ast_expr_list_free(arguments
);
701 isl_ast_expr_free(res
);
705 /* Create an expression representing an access to "array" with index
706 * expressions "indices".
708 __isl_give isl_ast_expr
*isl_ast_expr_access(__isl_take isl_ast_expr
*array
,
709 __isl_take isl_ast_expr_list
*indices
)
711 return ast_expr_with_arguments(isl_ast_op_access
, array
, indices
);
714 /* Create an expression representing a call to "function" with argument
715 * expressions "arguments".
717 __isl_give isl_ast_expr
*isl_ast_expr_call(__isl_take isl_ast_expr
*function
,
718 __isl_take isl_ast_expr_list
*arguments
)
720 return ast_expr_with_arguments(isl_ast_op_call
, function
, arguments
);
723 /* For each subexpression of "expr" of type isl_ast_expr_id,
724 * if it appears in "id2expr", then replace it by the corresponding
727 __isl_give isl_ast_expr
*isl_ast_expr_substitute_ids(
728 __isl_take isl_ast_expr
*expr
, __isl_take isl_id_to_ast_expr
*id2expr
)
733 if (!expr
|| !id2expr
)
736 switch (expr
->type
) {
737 case isl_ast_expr_int
:
739 case isl_ast_expr_id
:
740 if (!isl_id_to_ast_expr_has(id2expr
, expr
->u
.id
))
742 id
= isl_id_copy(expr
->u
.id
);
743 isl_ast_expr_free(expr
);
744 expr
= isl_id_to_ast_expr_get(id2expr
, id
);
746 case isl_ast_expr_op
:
747 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
) {
749 arg
= isl_ast_expr_copy(expr
->u
.op
.args
[i
]);
750 arg
= isl_ast_expr_substitute_ids(arg
,
751 isl_id_to_ast_expr_copy(id2expr
));
752 if (arg
== expr
->u
.op
.args
[i
]) {
753 isl_ast_expr_free(arg
);
757 expr
= isl_ast_expr_free(expr
);
758 expr
= isl_ast_expr_cow(expr
);
760 isl_ast_expr_free(arg
);
763 isl_ast_expr_free(expr
->u
.op
.args
[i
]);
764 expr
->u
.op
.args
[i
] = arg
;
767 case isl_ast_expr_error
:
768 expr
= isl_ast_expr_free(expr
);
772 isl_id_to_ast_expr_free(id2expr
);
775 isl_ast_expr_free(expr
);
776 isl_id_to_ast_expr_free(id2expr
);
780 isl_ctx
*isl_ast_node_get_ctx(__isl_keep isl_ast_node
*node
)
782 return node
? node
->ctx
: NULL
;
785 enum isl_ast_node_type
isl_ast_node_get_type(__isl_keep isl_ast_node
*node
)
787 return node
? node
->type
: isl_ast_node_error
;
790 __isl_give isl_ast_node
*isl_ast_node_alloc(isl_ctx
*ctx
,
791 enum isl_ast_node_type type
)
795 node
= isl_calloc_type(ctx
, isl_ast_node
);
807 /* Create an if node with the given guard.
809 * The then body needs to be filled in later.
811 __isl_give isl_ast_node
*isl_ast_node_alloc_if(__isl_take isl_ast_expr
*guard
)
818 node
= isl_ast_node_alloc(isl_ast_expr_get_ctx(guard
), isl_ast_node_if
);
821 node
->u
.i
.guard
= guard
;
825 isl_ast_expr_free(guard
);
829 /* Create a for node with the given iterator.
831 * The remaining fields need to be filled in later.
833 __isl_give isl_ast_node
*isl_ast_node_alloc_for(__isl_take isl_id
*id
)
841 ctx
= isl_id_get_ctx(id
);
842 node
= isl_ast_node_alloc(ctx
, isl_ast_node_for
);
846 node
->u
.f
.iterator
= isl_ast_expr_from_id(id
);
847 if (!node
->u
.f
.iterator
)
848 return isl_ast_node_free(node
);
856 /* Create a mark node, marking "node" with "id".
858 __isl_give isl_ast_node
*isl_ast_node_alloc_mark(__isl_take isl_id
*id
,
859 __isl_take isl_ast_node
*node
)
867 ctx
= isl_id_get_ctx(id
);
868 mark
= isl_ast_node_alloc(ctx
, isl_ast_node_mark
);
873 mark
->u
.m
.node
= node
;
878 isl_ast_node_free(node
);
882 /* Create a user node evaluating "expr".
884 __isl_give isl_ast_node
*isl_ast_node_alloc_user(__isl_take isl_ast_expr
*expr
)
892 ctx
= isl_ast_expr_get_ctx(expr
);
893 node
= isl_ast_node_alloc(ctx
, isl_ast_node_user
);
897 node
->u
.e
.expr
= expr
;
901 isl_ast_expr_free(expr
);
905 /* Create a block node with the given children.
907 __isl_give isl_ast_node
*isl_ast_node_alloc_block(
908 __isl_take isl_ast_node_list
*list
)
916 ctx
= isl_ast_node_list_get_ctx(list
);
917 node
= isl_ast_node_alloc(ctx
, isl_ast_node_block
);
921 node
->u
.b
.children
= list
;
925 isl_ast_node_list_free(list
);
929 /* Represent the given list of nodes as a single node, either by
930 * extract the node from a single element list or by creating
931 * a block node with the list of nodes as children.
933 __isl_give isl_ast_node
*isl_ast_node_from_ast_node_list(
934 __isl_take isl_ast_node_list
*list
)
938 if (isl_ast_node_list_n_ast_node(list
) != 1)
939 return isl_ast_node_alloc_block(list
);
941 node
= isl_ast_node_list_get_ast_node(list
, 0);
942 isl_ast_node_list_free(list
);
947 __isl_give isl_ast_node
*isl_ast_node_copy(__isl_keep isl_ast_node
*node
)
956 __isl_give isl_ast_node
*isl_ast_node_dup(__isl_keep isl_ast_node
*node
)
963 dup
= isl_ast_node_alloc(isl_ast_node_get_ctx(node
), node
->type
);
967 switch (node
->type
) {
968 case isl_ast_node_if
:
969 dup
->u
.i
.guard
= isl_ast_expr_copy(node
->u
.i
.guard
);
970 dup
->u
.i
.then
= isl_ast_node_copy(node
->u
.i
.then
);
971 dup
->u
.i
.else_node
= isl_ast_node_copy(node
->u
.i
.else_node
);
972 if (!dup
->u
.i
.guard
|| !dup
->u
.i
.then
||
973 (node
->u
.i
.else_node
&& !dup
->u
.i
.else_node
))
974 return isl_ast_node_free(dup
);
976 case isl_ast_node_for
:
977 dup
->u
.f
.iterator
= isl_ast_expr_copy(node
->u
.f
.iterator
);
978 dup
->u
.f
.init
= isl_ast_expr_copy(node
->u
.f
.init
);
979 dup
->u
.f
.cond
= isl_ast_expr_copy(node
->u
.f
.cond
);
980 dup
->u
.f
.inc
= isl_ast_expr_copy(node
->u
.f
.inc
);
981 dup
->u
.f
.body
= isl_ast_node_copy(node
->u
.f
.body
);
982 if (!dup
->u
.f
.iterator
|| !dup
->u
.f
.init
|| !dup
->u
.f
.cond
||
983 !dup
->u
.f
.inc
|| !dup
->u
.f
.body
)
984 return isl_ast_node_free(dup
);
986 case isl_ast_node_block
:
987 dup
->u
.b
.children
= isl_ast_node_list_copy(node
->u
.b
.children
);
988 if (!dup
->u
.b
.children
)
989 return isl_ast_node_free(dup
);
991 case isl_ast_node_mark
:
992 dup
->u
.m
.mark
= isl_id_copy(node
->u
.m
.mark
);
993 dup
->u
.m
.node
= isl_ast_node_copy(node
->u
.m
.node
);
994 if (!dup
->u
.m
.mark
|| !dup
->u
.m
.node
)
995 return isl_ast_node_free(dup
);
997 case isl_ast_node_user
:
998 dup
->u
.e
.expr
= isl_ast_expr_copy(node
->u
.e
.expr
);
1000 return isl_ast_node_free(dup
);
1002 case isl_ast_node_error
:
1009 __isl_give isl_ast_node
*isl_ast_node_cow(__isl_take isl_ast_node
*node
)
1017 return isl_ast_node_dup(node
);
1020 __isl_null isl_ast_node
*isl_ast_node_free(__isl_take isl_ast_node
*node
)
1025 if (--node
->ref
> 0)
1028 switch (node
->type
) {
1029 case isl_ast_node_if
:
1030 isl_ast_expr_free(node
->u
.i
.guard
);
1031 isl_ast_node_free(node
->u
.i
.then
);
1032 isl_ast_node_free(node
->u
.i
.else_node
);
1034 case isl_ast_node_for
:
1035 isl_ast_expr_free(node
->u
.f
.iterator
);
1036 isl_ast_expr_free(node
->u
.f
.init
);
1037 isl_ast_expr_free(node
->u
.f
.cond
);
1038 isl_ast_expr_free(node
->u
.f
.inc
);
1039 isl_ast_node_free(node
->u
.f
.body
);
1041 case isl_ast_node_block
:
1042 isl_ast_node_list_free(node
->u
.b
.children
);
1044 case isl_ast_node_mark
:
1045 isl_id_free(node
->u
.m
.mark
);
1046 isl_ast_node_free(node
->u
.m
.node
);
1048 case isl_ast_node_user
:
1049 isl_ast_expr_free(node
->u
.e
.expr
);
1051 case isl_ast_node_error
:
1055 isl_id_free(node
->annotation
);
1056 isl_ctx_deref(node
->ctx
);
1062 /* Replace the body of the for node "node" by "body".
1064 __isl_give isl_ast_node
*isl_ast_node_for_set_body(
1065 __isl_take isl_ast_node
*node
, __isl_take isl_ast_node
*body
)
1067 node
= isl_ast_node_cow(node
);
1070 if (node
->type
!= isl_ast_node_for
)
1071 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1072 "not a for node", goto error
);
1074 isl_ast_node_free(node
->u
.f
.body
);
1075 node
->u
.f
.body
= body
;
1079 isl_ast_node_free(node
);
1080 isl_ast_node_free(body
);
1084 __isl_give isl_ast_node
*isl_ast_node_for_get_body(
1085 __isl_keep isl_ast_node
*node
)
1089 if (node
->type
!= isl_ast_node_for
)
1090 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1091 "not a for node", return NULL
);
1092 return isl_ast_node_copy(node
->u
.f
.body
);
1095 /* Mark the given for node as being degenerate.
1097 __isl_give isl_ast_node
*isl_ast_node_for_mark_degenerate(
1098 __isl_take isl_ast_node
*node
)
1100 node
= isl_ast_node_cow(node
);
1103 node
->u
.f
.degenerate
= 1;
1107 int isl_ast_node_for_is_degenerate(__isl_keep isl_ast_node
*node
)
1111 if (node
->type
!= isl_ast_node_for
)
1112 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1113 "not a for node", return -1);
1114 return node
->u
.f
.degenerate
;
1117 __isl_give isl_ast_expr
*isl_ast_node_for_get_iterator(
1118 __isl_keep isl_ast_node
*node
)
1122 if (node
->type
!= isl_ast_node_for
)
1123 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1124 "not a for node", return NULL
);
1125 return isl_ast_expr_copy(node
->u
.f
.iterator
);
1128 __isl_give isl_ast_expr
*isl_ast_node_for_get_init(
1129 __isl_keep isl_ast_node
*node
)
1133 if (node
->type
!= isl_ast_node_for
)
1134 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1135 "not a for node", return NULL
);
1136 return isl_ast_expr_copy(node
->u
.f
.init
);
1139 /* Return the condition expression of the given for node.
1141 * If the for node is degenerate, then the condition is not explicitly
1142 * stored in the node. Instead, it is constructed as
1146 __isl_give isl_ast_expr
*isl_ast_node_for_get_cond(
1147 __isl_keep isl_ast_node
*node
)
1151 if (node
->type
!= isl_ast_node_for
)
1152 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1153 "not a for node", return NULL
);
1154 if (!node
->u
.f
.degenerate
)
1155 return isl_ast_expr_copy(node
->u
.f
.cond
);
1157 return isl_ast_expr_alloc_binary(isl_ast_op_le
,
1158 isl_ast_expr_copy(node
->u
.f
.iterator
),
1159 isl_ast_expr_copy(node
->u
.f
.init
));
1162 /* Return the increment of the given for node.
1164 * If the for node is degenerate, then the increment is not explicitly
1165 * stored in the node. We simply return "1".
1167 __isl_give isl_ast_expr
*isl_ast_node_for_get_inc(
1168 __isl_keep isl_ast_node
*node
)
1172 if (node
->type
!= isl_ast_node_for
)
1173 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1174 "not a for node", return NULL
);
1175 if (!node
->u
.f
.degenerate
)
1176 return isl_ast_expr_copy(node
->u
.f
.inc
);
1177 return isl_ast_expr_alloc_int_si(isl_ast_node_get_ctx(node
), 1);
1180 /* Replace the then branch of the if node "node" by "child".
1182 __isl_give isl_ast_node
*isl_ast_node_if_set_then(
1183 __isl_take isl_ast_node
*node
, __isl_take isl_ast_node
*child
)
1185 node
= isl_ast_node_cow(node
);
1186 if (!node
|| !child
)
1188 if (node
->type
!= isl_ast_node_if
)
1189 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1190 "not an if node", goto error
);
1192 isl_ast_node_free(node
->u
.i
.then
);
1193 node
->u
.i
.then
= child
;
1197 isl_ast_node_free(node
);
1198 isl_ast_node_free(child
);
1202 __isl_give isl_ast_node
*isl_ast_node_if_get_then(
1203 __isl_keep isl_ast_node
*node
)
1207 if (node
->type
!= isl_ast_node_if
)
1208 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1209 "not an if node", return NULL
);
1210 return isl_ast_node_copy(node
->u
.i
.then
);
1213 int isl_ast_node_if_has_else(
1214 __isl_keep isl_ast_node
*node
)
1218 if (node
->type
!= isl_ast_node_if
)
1219 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1220 "not an if node", return -1);
1221 return node
->u
.i
.else_node
!= NULL
;
1224 __isl_give isl_ast_node
*isl_ast_node_if_get_else(
1225 __isl_keep isl_ast_node
*node
)
1229 if (node
->type
!= isl_ast_node_if
)
1230 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1231 "not an if node", return NULL
);
1232 return isl_ast_node_copy(node
->u
.i
.else_node
);
1235 __isl_give isl_ast_expr
*isl_ast_node_if_get_cond(
1236 __isl_keep isl_ast_node
*node
)
1240 if (node
->type
!= isl_ast_node_if
)
1241 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1242 "not a guard node", return NULL
);
1243 return isl_ast_expr_copy(node
->u
.i
.guard
);
1246 __isl_give isl_ast_node_list
*isl_ast_node_block_get_children(
1247 __isl_keep isl_ast_node
*node
)
1251 if (node
->type
!= isl_ast_node_block
)
1252 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1253 "not a block node", return NULL
);
1254 return isl_ast_node_list_copy(node
->u
.b
.children
);
1257 __isl_give isl_ast_expr
*isl_ast_node_user_get_expr(
1258 __isl_keep isl_ast_node
*node
)
1262 if (node
->type
!= isl_ast_node_user
)
1263 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1264 "not a user node", return NULL
);
1266 return isl_ast_expr_copy(node
->u
.e
.expr
);
1269 /* Return the mark identifier of the mark node "node".
1271 __isl_give isl_id
*isl_ast_node_mark_get_id(__isl_keep isl_ast_node
*node
)
1275 if (node
->type
!= isl_ast_node_mark
)
1276 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1277 "not a mark node", return NULL
);
1279 return isl_id_copy(node
->u
.m
.mark
);
1282 /* Return the node marked by mark node "node".
1284 __isl_give isl_ast_node
*isl_ast_node_mark_get_node(
1285 __isl_keep isl_ast_node
*node
)
1289 if (node
->type
!= isl_ast_node_mark
)
1290 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1291 "not a mark node", return NULL
);
1293 return isl_ast_node_copy(node
->u
.m
.node
);
1296 __isl_give isl_id
*isl_ast_node_get_annotation(__isl_keep isl_ast_node
*node
)
1298 return node
? isl_id_copy(node
->annotation
) : NULL
;
1301 /* Replace node->annotation by "annotation".
1303 __isl_give isl_ast_node
*isl_ast_node_set_annotation(
1304 __isl_take isl_ast_node
*node
, __isl_take isl_id
*annotation
)
1306 node
= isl_ast_node_cow(node
);
1307 if (!node
|| !annotation
)
1310 isl_id_free(node
->annotation
);
1311 node
->annotation
= annotation
;
1315 isl_id_free(annotation
);
1316 return isl_ast_node_free(node
);
1319 /* Textual C representation of the various operators.
1321 static char *op_str
[] = {
1322 [isl_ast_op_and
] = "&&",
1323 [isl_ast_op_and_then
] = "&&",
1324 [isl_ast_op_or
] = "||",
1325 [isl_ast_op_or_else
] = "||",
1326 [isl_ast_op_max
] = "max",
1327 [isl_ast_op_min
] = "min",
1328 [isl_ast_op_minus
] = "-",
1329 [isl_ast_op_add
] = "+",
1330 [isl_ast_op_sub
] = "-",
1331 [isl_ast_op_mul
] = "*",
1332 [isl_ast_op_pdiv_q
] = "/",
1333 [isl_ast_op_pdiv_r
] = "%",
1334 [isl_ast_op_zdiv_r
] = "%",
1335 [isl_ast_op_div
] = "/",
1336 [isl_ast_op_eq
] = "==",
1337 [isl_ast_op_le
] = "<=",
1338 [isl_ast_op_ge
] = ">=",
1339 [isl_ast_op_lt
] = "<",
1340 [isl_ast_op_gt
] = ">",
1341 [isl_ast_op_member
] = ".",
1342 [isl_ast_op_address_of
] = "&"
1345 /* Precedence in C of the various operators.
1346 * Based on http://en.wikipedia.org/wiki/Operators_in_C_and_C++
1347 * Lowest value means highest precedence.
1349 static int op_prec
[] = {
1350 [isl_ast_op_and
] = 13,
1351 [isl_ast_op_and_then
] = 13,
1352 [isl_ast_op_or
] = 14,
1353 [isl_ast_op_or_else
] = 14,
1354 [isl_ast_op_max
] = 2,
1355 [isl_ast_op_min
] = 2,
1356 [isl_ast_op_minus
] = 3,
1357 [isl_ast_op_add
] = 6,
1358 [isl_ast_op_sub
] = 6,
1359 [isl_ast_op_mul
] = 5,
1360 [isl_ast_op_div
] = 5,
1361 [isl_ast_op_fdiv_q
] = 2,
1362 [isl_ast_op_pdiv_q
] = 5,
1363 [isl_ast_op_pdiv_r
] = 5,
1364 [isl_ast_op_zdiv_r
] = 5,
1365 [isl_ast_op_cond
] = 15,
1366 [isl_ast_op_select
] = 15,
1367 [isl_ast_op_eq
] = 9,
1368 [isl_ast_op_le
] = 8,
1369 [isl_ast_op_ge
] = 8,
1370 [isl_ast_op_lt
] = 8,
1371 [isl_ast_op_gt
] = 8,
1372 [isl_ast_op_call
] = 2,
1373 [isl_ast_op_access
] = 2,
1374 [isl_ast_op_member
] = 2,
1375 [isl_ast_op_address_of
] = 3
1378 /* Is the operator left-to-right associative?
1380 static int op_left
[] = {
1381 [isl_ast_op_and
] = 1,
1382 [isl_ast_op_and_then
] = 1,
1383 [isl_ast_op_or
] = 1,
1384 [isl_ast_op_or_else
] = 1,
1385 [isl_ast_op_max
] = 1,
1386 [isl_ast_op_min
] = 1,
1387 [isl_ast_op_minus
] = 0,
1388 [isl_ast_op_add
] = 1,
1389 [isl_ast_op_sub
] = 1,
1390 [isl_ast_op_mul
] = 1,
1391 [isl_ast_op_div
] = 1,
1392 [isl_ast_op_fdiv_q
] = 1,
1393 [isl_ast_op_pdiv_q
] = 1,
1394 [isl_ast_op_pdiv_r
] = 1,
1395 [isl_ast_op_zdiv_r
] = 1,
1396 [isl_ast_op_cond
] = 0,
1397 [isl_ast_op_select
] = 0,
1398 [isl_ast_op_eq
] = 1,
1399 [isl_ast_op_le
] = 1,
1400 [isl_ast_op_ge
] = 1,
1401 [isl_ast_op_lt
] = 1,
1402 [isl_ast_op_gt
] = 1,
1403 [isl_ast_op_call
] = 1,
1404 [isl_ast_op_access
] = 1,
1405 [isl_ast_op_member
] = 1,
1406 [isl_ast_op_address_of
] = 0
1409 static int is_and(enum isl_ast_op_type op
)
1411 return op
== isl_ast_op_and
|| op
== isl_ast_op_and_then
;
1414 static int is_or(enum isl_ast_op_type op
)
1416 return op
== isl_ast_op_or
|| op
== isl_ast_op_or_else
;
1419 static int is_add_sub(enum isl_ast_op_type op
)
1421 return op
== isl_ast_op_add
|| op
== isl_ast_op_sub
;
1424 static int is_div_mod(enum isl_ast_op_type op
)
1426 return op
== isl_ast_op_div
||
1427 op
== isl_ast_op_pdiv_r
||
1428 op
== isl_ast_op_zdiv_r
;
1431 /* Do we need/want parentheses around "expr" as a subexpression of
1432 * an "op" operation? If "left" is set, then "expr" is the left-most
1435 * We only need parentheses if "expr" represents an operation.
1437 * If op has a higher precedence than expr->u.op.op, then we need
1439 * If op and expr->u.op.op have the same precedence, but the operations
1440 * are performed in an order that is different from the associativity,
1441 * then we need parentheses.
1443 * An and inside an or technically does not require parentheses,
1444 * but some compilers complain about that, so we add them anyway.
1446 * Computations such as "a / b * c" and "a % b + c" can be somewhat
1447 * difficult to read, so we add parentheses for those as well.
1449 static int sub_expr_need_parens(enum isl_ast_op_type op
,
1450 __isl_keep isl_ast_expr
*expr
, int left
)
1452 if (expr
->type
!= isl_ast_expr_op
)
1455 if (op_prec
[expr
->u
.op
.op
] > op_prec
[op
])
1457 if (op_prec
[expr
->u
.op
.op
] == op_prec
[op
] && left
!= op_left
[op
])
1460 if (is_or(op
) && is_and(expr
->u
.op
.op
))
1462 if (op
== isl_ast_op_mul
&& expr
->u
.op
.op
!= isl_ast_op_mul
&&
1463 op_prec
[expr
->u
.op
.op
] == op_prec
[op
])
1465 if (is_add_sub(op
) && is_div_mod(expr
->u
.op
.op
))
1471 /* Print "expr" as a subexpression of an "op" operation.
1472 * If "left" is set, then "expr" is the left-most operand.
1474 static __isl_give isl_printer
*print_sub_expr(__isl_take isl_printer
*p
,
1475 enum isl_ast_op_type op
, __isl_keep isl_ast_expr
*expr
, int left
)
1479 need_parens
= sub_expr_need_parens(op
, expr
, left
);
1482 p
= isl_printer_print_str(p
, "(");
1483 p
= isl_printer_print_ast_expr(p
, expr
);
1485 p
= isl_printer_print_str(p
, ")");
1489 /* Print a min or max reduction "expr".
1491 static __isl_give isl_printer
*print_min_max(__isl_take isl_printer
*p
,
1492 __isl_keep isl_ast_expr
*expr
)
1496 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1497 p
= isl_printer_print_str(p
, op_str
[expr
->u
.op
.op
]);
1498 p
= isl_printer_print_str(p
, "(");
1500 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1501 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1502 p
= isl_printer_print_str(p
, ", ");
1503 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[i
]);
1504 p
= isl_printer_print_str(p
, ")");
1510 /* Print a function call "expr".
1512 * The first argument represents the function to be called.
1514 static __isl_give isl_printer
*print_call(__isl_take isl_printer
*p
,
1515 __isl_keep isl_ast_expr
*expr
)
1519 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1520 p
= isl_printer_print_str(p
, "(");
1521 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1523 p
= isl_printer_print_str(p
, ", ");
1524 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[i
]);
1526 p
= isl_printer_print_str(p
, ")");
1531 /* Print an array access "expr".
1533 * The first argument represents the array being accessed.
1535 static __isl_give isl_printer
*print_access(__isl_take isl_printer
*p
,
1536 __isl_keep isl_ast_expr
*expr
)
1540 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1541 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1542 p
= isl_printer_print_str(p
, "[");
1543 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[i
]);
1544 p
= isl_printer_print_str(p
, "]");
1550 /* Print "expr" to "p".
1552 * If we are printing in isl format, then we also print an indication
1553 * of the size of the expression (if it was computed).
1555 __isl_give isl_printer
*isl_printer_print_ast_expr(__isl_take isl_printer
*p
,
1556 __isl_keep isl_ast_expr
*expr
)
1561 return isl_printer_free(p
);
1563 switch (expr
->type
) {
1564 case isl_ast_expr_op
:
1565 if (expr
->u
.op
.op
== isl_ast_op_call
) {
1566 p
= print_call(p
, expr
);
1569 if (expr
->u
.op
.op
== isl_ast_op_access
) {
1570 p
= print_access(p
, expr
);
1573 if (expr
->u
.op
.n_arg
== 1) {
1574 p
= isl_printer_print_str(p
, op_str
[expr
->u
.op
.op
]);
1575 p
= print_sub_expr(p
, expr
->u
.op
.op
,
1576 expr
->u
.op
.args
[0], 0);
1579 if (expr
->u
.op
.op
== isl_ast_op_fdiv_q
) {
1580 p
= isl_printer_print_str(p
, "floord(");
1581 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1582 p
= isl_printer_print_str(p
, ", ");
1583 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[1]);
1584 p
= isl_printer_print_str(p
, ")");
1587 if (expr
->u
.op
.op
== isl_ast_op_max
||
1588 expr
->u
.op
.op
== isl_ast_op_min
) {
1589 p
= print_min_max(p
, expr
);
1592 if (expr
->u
.op
.op
== isl_ast_op_cond
||
1593 expr
->u
.op
.op
== isl_ast_op_select
) {
1594 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1595 p
= isl_printer_print_str(p
, " ? ");
1596 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[1]);
1597 p
= isl_printer_print_str(p
, " : ");
1598 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[2]);
1601 if (expr
->u
.op
.n_arg
!= 2)
1602 isl_die(isl_printer_get_ctx(p
), isl_error_internal
,
1603 "operation should have two arguments",
1605 p
= print_sub_expr(p
, expr
->u
.op
.op
, expr
->u
.op
.args
[0], 1);
1606 if (expr
->u
.op
.op
!= isl_ast_op_member
)
1607 p
= isl_printer_print_str(p
, " ");
1608 p
= isl_printer_print_str(p
, op_str
[expr
->u
.op
.op
]);
1609 if (expr
->u
.op
.op
!= isl_ast_op_member
)
1610 p
= isl_printer_print_str(p
, " ");
1611 p
= print_sub_expr(p
, expr
->u
.op
.op
, expr
->u
.op
.args
[1], 0);
1613 case isl_ast_expr_id
:
1614 p
= isl_printer_print_str(p
, isl_id_get_name(expr
->u
.id
));
1616 case isl_ast_expr_int
:
1617 p
= isl_printer_print_val(p
, expr
->u
.v
);
1619 case isl_ast_expr_error
:
1625 isl_printer_free(p
);
1629 /* Print "node" to "p" in "isl format".
1631 static __isl_give isl_printer
*print_ast_node_isl(__isl_take isl_printer
*p
,
1632 __isl_keep isl_ast_node
*node
)
1634 p
= isl_printer_print_str(p
, "(");
1635 switch (node
->type
) {
1636 case isl_ast_node_for
:
1637 if (node
->u
.f
.degenerate
) {
1638 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1640 p
= isl_printer_print_str(p
, "init: ");
1641 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1642 p
= isl_printer_print_str(p
, ", ");
1643 p
= isl_printer_print_str(p
, "cond: ");
1644 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.cond
);
1645 p
= isl_printer_print_str(p
, ", ");
1646 p
= isl_printer_print_str(p
, "inc: ");
1647 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.inc
);
1649 if (node
->u
.f
.body
) {
1650 p
= isl_printer_print_str(p
, ", ");
1651 p
= isl_printer_print_str(p
, "body: ");
1652 p
= isl_printer_print_ast_node(p
, node
->u
.f
.body
);
1655 case isl_ast_node_mark
:
1656 p
= isl_printer_print_str(p
, "mark: ");
1657 p
= isl_printer_print_id(p
, node
->u
.m
.mark
);
1658 p
= isl_printer_print_str(p
, "node: ");
1659 p
= isl_printer_print_ast_node(p
, node
->u
.m
.node
);
1660 case isl_ast_node_user
:
1661 p
= isl_printer_print_ast_expr(p
, node
->u
.e
.expr
);
1663 case isl_ast_node_if
:
1664 p
= isl_printer_print_str(p
, "guard: ");
1665 p
= isl_printer_print_ast_expr(p
, node
->u
.i
.guard
);
1666 if (node
->u
.i
.then
) {
1667 p
= isl_printer_print_str(p
, ", ");
1668 p
= isl_printer_print_str(p
, "then: ");
1669 p
= isl_printer_print_ast_node(p
, node
->u
.i
.then
);
1671 if (node
->u
.i
.else_node
) {
1672 p
= isl_printer_print_str(p
, ", ");
1673 p
= isl_printer_print_str(p
, "else: ");
1674 p
= isl_printer_print_ast_node(p
, node
->u
.i
.else_node
);
1677 case isl_ast_node_block
:
1678 p
= isl_printer_print_ast_node_list(p
, node
->u
.b
.children
);
1680 case isl_ast_node_error
:
1683 p
= isl_printer_print_str(p
, ")");
1687 /* Do we need to print a block around the body "node" of a for or if node?
1689 * If the node is a block, then we need to print a block.
1690 * Also if the node is a degenerate for then we will print it as
1691 * an assignment followed by the body of the for loop, so we need a block
1693 * If the node is an if node with an else, then we print a block
1694 * to avoid spurious dangling else warnings emitted by some compilers.
1695 * If the node is a mark, then in principle, we would have to check
1696 * the child of the mark node. However, even if the child would not
1697 * require us to print a block, for readability it is probably best
1698 * to print a block anyway.
1699 * If the ast_always_print_block option has been set, then we print a block.
1701 static int need_block(__isl_keep isl_ast_node
*node
)
1705 if (node
->type
== isl_ast_node_block
)
1707 if (node
->type
== isl_ast_node_for
&& node
->u
.f
.degenerate
)
1709 if (node
->type
== isl_ast_node_if
&& node
->u
.i
.else_node
)
1711 if (node
->type
== isl_ast_node_mark
)
1714 ctx
= isl_ast_node_get_ctx(node
);
1715 return isl_options_get_ast_always_print_block(ctx
);
1718 static __isl_give isl_printer
*print_ast_node_c(__isl_take isl_printer
*p
,
1719 __isl_keep isl_ast_node
*node
,
1720 __isl_keep isl_ast_print_options
*options
, int in_block
, int in_list
);
1721 static __isl_give isl_printer
*print_if_c(__isl_take isl_printer
*p
,
1722 __isl_keep isl_ast_node
*node
,
1723 __isl_keep isl_ast_print_options
*options
, int new_line
);
1725 /* Print the body "node" of a for or if node.
1726 * If "else_node" is set, then it is printed as well.
1728 * We first check if we need to print out a block.
1729 * We always print out a block if there is an else node to make
1730 * sure that the else node is matched to the correct if node.
1732 * If the else node is itself an if, then we print it as
1736 * Otherwise the else node is printed as
1741 static __isl_give isl_printer
*print_body_c(__isl_take isl_printer
*p
,
1742 __isl_keep isl_ast_node
*node
, __isl_keep isl_ast_node
*else_node
,
1743 __isl_keep isl_ast_print_options
*options
)
1746 return isl_printer_free(p
);
1748 if (!else_node
&& !need_block(node
)) {
1749 p
= isl_printer_end_line(p
);
1750 p
= isl_printer_indent(p
, 2);
1751 p
= isl_ast_node_print(node
, p
,
1752 isl_ast_print_options_copy(options
));
1753 p
= isl_printer_indent(p
, -2);
1757 p
= isl_printer_print_str(p
, " {");
1758 p
= isl_printer_end_line(p
);
1759 p
= isl_printer_indent(p
, 2);
1760 p
= print_ast_node_c(p
, node
, options
, 1, 0);
1761 p
= isl_printer_indent(p
, -2);
1762 p
= isl_printer_start_line(p
);
1763 p
= isl_printer_print_str(p
, "}");
1765 if (else_node
->type
== isl_ast_node_if
) {
1766 p
= isl_printer_print_str(p
, " else ");
1767 p
= print_if_c(p
, else_node
, options
, 0);
1769 p
= isl_printer_print_str(p
, " else");
1770 p
= print_body_c(p
, else_node
, NULL
, options
);
1773 p
= isl_printer_end_line(p
);
1778 /* Print the start of a compound statement.
1780 static __isl_give isl_printer
*start_block(__isl_take isl_printer
*p
)
1782 p
= isl_printer_start_line(p
);
1783 p
= isl_printer_print_str(p
, "{");
1784 p
= isl_printer_end_line(p
);
1785 p
= isl_printer_indent(p
, 2);
1790 /* Print the end of a compound statement.
1792 static __isl_give isl_printer
*end_block(__isl_take isl_printer
*p
)
1794 p
= isl_printer_indent(p
, -2);
1795 p
= isl_printer_start_line(p
);
1796 p
= isl_printer_print_str(p
, "}");
1797 p
= isl_printer_end_line(p
);
1802 /* Print the for node "node".
1804 * If the for node is degenerate, it is printed as
1806 * type iterator = init;
1809 * Otherwise, it is printed as
1811 * for (type iterator = init; cond; iterator += inc)
1814 * "in_block" is set if we are currently inside a block.
1815 * "in_list" is set if the current node is not alone in the block.
1816 * If we are not in a block or if the current not is not alone in the block
1817 * then we print a block around a degenerate for loop such that the variable
1818 * declaration will not conflict with any potential other declaration
1819 * of the same variable.
1821 static __isl_give isl_printer
*print_for_c(__isl_take isl_printer
*p
,
1822 __isl_keep isl_ast_node
*node
,
1823 __isl_keep isl_ast_print_options
*options
, int in_block
, int in_list
)
1829 type
= isl_options_get_ast_iterator_type(isl_printer_get_ctx(p
));
1830 if (!node
->u
.f
.degenerate
) {
1831 id
= isl_ast_expr_get_id(node
->u
.f
.iterator
);
1832 name
= isl_id_get_name(id
);
1834 p
= isl_printer_start_line(p
);
1835 p
= isl_printer_print_str(p
, "for (");
1836 p
= isl_printer_print_str(p
, type
);
1837 p
= isl_printer_print_str(p
, " ");
1838 p
= isl_printer_print_str(p
, name
);
1839 p
= isl_printer_print_str(p
, " = ");
1840 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1841 p
= isl_printer_print_str(p
, "; ");
1842 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.cond
);
1843 p
= isl_printer_print_str(p
, "; ");
1844 p
= isl_printer_print_str(p
, name
);
1845 p
= isl_printer_print_str(p
, " += ");
1846 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.inc
);
1847 p
= isl_printer_print_str(p
, ")");
1848 p
= print_body_c(p
, node
->u
.f
.body
, NULL
, options
);
1850 id
= isl_ast_expr_get_id(node
->u
.f
.iterator
);
1851 name
= isl_id_get_name(id
);
1853 if (!in_block
|| in_list
)
1855 p
= isl_printer_start_line(p
);
1856 p
= isl_printer_print_str(p
, type
);
1857 p
= isl_printer_print_str(p
, " ");
1858 p
= isl_printer_print_str(p
, name
);
1859 p
= isl_printer_print_str(p
, " = ");
1860 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
1861 p
= isl_printer_print_str(p
, ";");
1862 p
= isl_printer_end_line(p
);
1863 p
= print_ast_node_c(p
, node
->u
.f
.body
, options
, 1, 0);
1864 if (!in_block
|| in_list
)
1871 /* Print the if node "node".
1872 * If "new_line" is set then the if node should be printed on a new line.
1874 static __isl_give isl_printer
*print_if_c(__isl_take isl_printer
*p
,
1875 __isl_keep isl_ast_node
*node
,
1876 __isl_keep isl_ast_print_options
*options
, int new_line
)
1879 p
= isl_printer_start_line(p
);
1880 p
= isl_printer_print_str(p
, "if (");
1881 p
= isl_printer_print_ast_expr(p
, node
->u
.i
.guard
);
1882 p
= isl_printer_print_str(p
, ")");
1883 p
= print_body_c(p
, node
->u
.i
.then
, node
->u
.i
.else_node
, options
);
1888 /* Print the "node" to "p".
1890 * "in_block" is set if we are currently inside a block.
1891 * If so, we do not print a block around the children of a block node.
1892 * We do this to avoid an extra block around the body of a degenerate
1895 * "in_list" is set if the current node is not alone in the block.
1897 static __isl_give isl_printer
*print_ast_node_c(__isl_take isl_printer
*p
,
1898 __isl_keep isl_ast_node
*node
,
1899 __isl_keep isl_ast_print_options
*options
, int in_block
, int in_list
)
1901 switch (node
->type
) {
1902 case isl_ast_node_for
:
1903 if (options
->print_for
)
1904 return options
->print_for(p
,
1905 isl_ast_print_options_copy(options
),
1906 node
, options
->print_for_user
);
1907 p
= print_for_c(p
, node
, options
, in_block
, in_list
);
1909 case isl_ast_node_if
:
1910 p
= print_if_c(p
, node
, options
, 1);
1912 case isl_ast_node_block
:
1915 p
= isl_ast_node_list_print(node
->u
.b
.children
, p
, options
);
1919 case isl_ast_node_mark
:
1920 p
= isl_printer_start_line(p
);
1921 p
= isl_printer_print_str(p
, "// ");
1922 p
= isl_printer_print_str(p
, isl_id_get_name(node
->u
.m
.mark
));
1923 p
= isl_printer_end_line(p
);
1924 p
= print_ast_node_c(p
, node
->u
.m
.node
, options
, 0, in_list
);
1926 case isl_ast_node_user
:
1927 if (options
->print_user
)
1928 return options
->print_user(p
,
1929 isl_ast_print_options_copy(options
),
1930 node
, options
->print_user_user
);
1931 p
= isl_printer_start_line(p
);
1932 p
= isl_printer_print_ast_expr(p
, node
->u
.e
.expr
);
1933 p
= isl_printer_print_str(p
, ";");
1934 p
= isl_printer_end_line(p
);
1936 case isl_ast_node_error
:
1942 /* Print the for node "node" to "p".
1944 __isl_give isl_printer
*isl_ast_node_for_print(__isl_keep isl_ast_node
*node
,
1945 __isl_take isl_printer
*p
, __isl_take isl_ast_print_options
*options
)
1947 if (!node
|| !options
)
1949 if (node
->type
!= isl_ast_node_for
)
1950 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1951 "not a for node", goto error
);
1952 p
= print_for_c(p
, node
, options
, 0, 0);
1953 isl_ast_print_options_free(options
);
1956 isl_ast_print_options_free(options
);
1957 isl_printer_free(p
);
1961 /* Print the if node "node" to "p".
1963 __isl_give isl_printer
*isl_ast_node_if_print(__isl_keep isl_ast_node
*node
,
1964 __isl_take isl_printer
*p
, __isl_take isl_ast_print_options
*options
)
1966 if (!node
|| !options
)
1968 if (node
->type
!= isl_ast_node_if
)
1969 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1970 "not an if node", goto error
);
1971 p
= print_if_c(p
, node
, options
, 1);
1972 isl_ast_print_options_free(options
);
1975 isl_ast_print_options_free(options
);
1976 isl_printer_free(p
);
1980 /* Print "node" to "p".
1982 __isl_give isl_printer
*isl_ast_node_print(__isl_keep isl_ast_node
*node
,
1983 __isl_take isl_printer
*p
, __isl_take isl_ast_print_options
*options
)
1985 if (!options
|| !node
)
1987 p
= print_ast_node_c(p
, node
, options
, 0, 0);
1988 isl_ast_print_options_free(options
);
1991 isl_ast_print_options_free(options
);
1992 isl_printer_free(p
);
1996 /* Print "node" to "p".
1998 __isl_give isl_printer
*isl_printer_print_ast_node(__isl_take isl_printer
*p
,
1999 __isl_keep isl_ast_node
*node
)
2002 isl_ast_print_options
*options
;
2007 format
= isl_printer_get_output_format(p
);
2009 case ISL_FORMAT_ISL
:
2010 p
= print_ast_node_isl(p
, node
);
2013 options
= isl_ast_print_options_alloc(isl_printer_get_ctx(p
));
2014 p
= isl_ast_node_print(node
, p
, options
);
2017 isl_die(isl_printer_get_ctx(p
), isl_error_unsupported
,
2018 "output format not supported for ast_node",
2019 return isl_printer_free(p
));
2025 /* Print the list of nodes "list" to "p".
2027 __isl_give isl_printer
*isl_ast_node_list_print(
2028 __isl_keep isl_ast_node_list
*list
, __isl_take isl_printer
*p
,
2029 __isl_keep isl_ast_print_options
*options
)
2033 if (!p
|| !list
|| !options
)
2034 return isl_printer_free(p
);
2036 for (i
= 0; i
< list
->n
; ++i
)
2037 p
= print_ast_node_c(p
, list
->p
[i
], options
, 1, 1);
2042 #define ISL_AST_MACRO_FLOORD (1 << 0)
2043 #define ISL_AST_MACRO_MIN (1 << 1)
2044 #define ISL_AST_MACRO_MAX (1 << 2)
2045 #define ISL_AST_MACRO_ALL (ISL_AST_MACRO_FLOORD | \
2046 ISL_AST_MACRO_MIN | \
2049 /* If "expr" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
2050 * then set the corresponding bit in "macros".
2052 static int ast_expr_required_macros(__isl_keep isl_ast_expr
*expr
, int macros
)
2056 if (macros
== ISL_AST_MACRO_ALL
)
2059 if (expr
->type
!= isl_ast_expr_op
)
2062 if (expr
->u
.op
.op
== isl_ast_op_min
)
2063 macros
|= ISL_AST_MACRO_MIN
;
2064 if (expr
->u
.op
.op
== isl_ast_op_max
)
2065 macros
|= ISL_AST_MACRO_MAX
;
2066 if (expr
->u
.op
.op
== isl_ast_op_fdiv_q
)
2067 macros
|= ISL_AST_MACRO_FLOORD
;
2069 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
)
2070 macros
= ast_expr_required_macros(expr
->u
.op
.args
[i
], macros
);
2075 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list
*list
,
2078 /* If "node" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
2079 * then set the corresponding bit in "macros".
2081 static int ast_node_required_macros(__isl_keep isl_ast_node
*node
, int macros
)
2083 if (macros
== ISL_AST_MACRO_ALL
)
2086 switch (node
->type
) {
2087 case isl_ast_node_for
:
2088 macros
= ast_expr_required_macros(node
->u
.f
.init
, macros
);
2089 if (!node
->u
.f
.degenerate
) {
2090 macros
= ast_expr_required_macros(node
->u
.f
.cond
,
2092 macros
= ast_expr_required_macros(node
->u
.f
.inc
,
2095 macros
= ast_node_required_macros(node
->u
.f
.body
, macros
);
2097 case isl_ast_node_if
:
2098 macros
= ast_expr_required_macros(node
->u
.i
.guard
, macros
);
2099 macros
= ast_node_required_macros(node
->u
.i
.then
, macros
);
2100 if (node
->u
.i
.else_node
)
2101 macros
= ast_node_required_macros(node
->u
.i
.else_node
,
2104 case isl_ast_node_block
:
2105 macros
= ast_node_list_required_macros(node
->u
.b
.children
,
2108 case isl_ast_node_mark
:
2109 macros
= ast_node_required_macros(node
->u
.m
.node
, macros
);
2111 case isl_ast_node_user
:
2112 macros
= ast_expr_required_macros(node
->u
.e
.expr
, macros
);
2114 case isl_ast_node_error
:
2121 /* If "list" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
2122 * then set the corresponding bit in "macros".
2124 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list
*list
,
2129 for (i
= 0; i
< list
->n
; ++i
)
2130 macros
= ast_node_required_macros(list
->p
[i
], macros
);
2135 /* Print a macro definition for the operator "type".
2137 __isl_give isl_printer
*isl_ast_op_type_print_macro(
2138 enum isl_ast_op_type type
, __isl_take isl_printer
*p
)
2141 case isl_ast_op_min
:
2142 p
= isl_printer_start_line(p
);
2143 p
= isl_printer_print_str(p
,
2144 "#define min(x,y) ((x) < (y) ? (x) : (y))");
2145 p
= isl_printer_end_line(p
);
2147 case isl_ast_op_max
:
2148 p
= isl_printer_start_line(p
);
2149 p
= isl_printer_print_str(p
,
2150 "#define max(x,y) ((x) > (y) ? (x) : (y))");
2151 p
= isl_printer_end_line(p
);
2153 case isl_ast_op_fdiv_q
:
2154 p
= isl_printer_start_line(p
);
2155 p
= isl_printer_print_str(p
,
2156 "#define floord(n,d) "
2157 "(((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))");
2158 p
= isl_printer_end_line(p
);
2167 /* Call "fn" for each type of operation that appears in "node"
2168 * and that requires a macro definition.
2170 int isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node
*node
,
2171 int (*fn
)(enum isl_ast_op_type type
, void *user
), void *user
)
2178 macros
= ast_node_required_macros(node
, 0);
2180 if (macros
& ISL_AST_MACRO_MIN
&& fn(isl_ast_op_min
, user
) < 0)
2182 if (macros
& ISL_AST_MACRO_MAX
&& fn(isl_ast_op_max
, user
) < 0)
2184 if (macros
& ISL_AST_MACRO_FLOORD
&& fn(isl_ast_op_fdiv_q
, user
) < 0)
2190 static int ast_op_type_print_macro(enum isl_ast_op_type type
, void *user
)
2192 isl_printer
**p
= user
;
2194 *p
= isl_ast_op_type_print_macro(type
, *p
);
2199 /* Print macro definitions for all the macros used in the result
2200 * of printing "node.
2202 __isl_give isl_printer
*isl_ast_node_print_macros(
2203 __isl_keep isl_ast_node
*node
, __isl_take isl_printer
*p
)
2205 if (isl_ast_node_foreach_ast_op_type(node
,
2206 &ast_op_type_print_macro
, &p
) < 0)
2207 return isl_printer_free(p
);