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
14 #include <isl_ast_private.h>
17 #define EL_BASE ast_expr
19 #include <isl_list_templ.c>
22 #define EL_BASE ast_node
24 #include <isl_list_templ.c>
26 isl_ctx
*isl_ast_print_options_get_ctx(
27 __isl_keep isl_ast_print_options
*options
)
29 return options
? options
->ctx
: NULL
;
32 __isl_give isl_ast_print_options
*isl_ast_print_options_alloc(isl_ctx
*ctx
)
34 isl_ast_print_options
*options
;
36 options
= isl_calloc_type(ctx
, isl_ast_print_options
);
47 __isl_give isl_ast_print_options
*isl_ast_print_options_dup(
48 __isl_keep isl_ast_print_options
*options
)
51 isl_ast_print_options
*dup
;
56 ctx
= isl_ast_print_options_get_ctx(options
);
57 dup
= isl_ast_print_options_alloc(ctx
);
61 dup
->print_for
= options
->print_for
;
62 dup
->print_for_user
= options
->print_for_user
;
63 dup
->print_user
= options
->print_user
;
64 dup
->print_user_user
= options
->print_user_user
;
69 __isl_give isl_ast_print_options
*isl_ast_print_options_cow(
70 __isl_take isl_ast_print_options
*options
)
75 if (options
->ref
== 1)
78 return isl_ast_print_options_dup(options
);
81 __isl_give isl_ast_print_options
*isl_ast_print_options_copy(
82 __isl_keep isl_ast_print_options
*options
)
91 __isl_null isl_ast_print_options
*isl_ast_print_options_free(
92 __isl_take isl_ast_print_options
*options
)
97 if (--options
->ref
> 0)
100 isl_ctx_deref(options
->ctx
);
106 /* Set the print_user callback of "options" to "print_user".
108 * If this callback is set, then it is used to print user nodes in the AST.
109 * Otherwise, the expression associated to the user node is printed.
111 __isl_give isl_ast_print_options
*isl_ast_print_options_set_print_user(
112 __isl_take isl_ast_print_options
*options
,
113 __isl_give isl_printer
*(*print_user
)(__isl_take isl_printer
*p
,
114 __isl_take isl_ast_print_options
*options
,
115 __isl_keep isl_ast_node
*node
, void *user
),
118 options
= isl_ast_print_options_cow(options
);
122 options
->print_user
= print_user
;
123 options
->print_user_user
= user
;
128 /* Set the print_for callback of "options" to "print_for".
130 * If this callback is set, then it used to print for nodes in the AST.
132 __isl_give isl_ast_print_options
*isl_ast_print_options_set_print_for(
133 __isl_take isl_ast_print_options
*options
,
134 __isl_give isl_printer
*(*print_for
)(__isl_take isl_printer
*p
,
135 __isl_take isl_ast_print_options
*options
,
136 __isl_keep isl_ast_node
*node
, void *user
),
139 options
= isl_ast_print_options_cow(options
);
143 options
->print_for
= print_for
;
144 options
->print_for_user
= user
;
149 __isl_give isl_ast_expr
*isl_ast_expr_copy(__isl_keep isl_ast_expr
*expr
)
158 __isl_give isl_ast_expr
*isl_ast_expr_dup(__isl_keep isl_ast_expr
*expr
)
167 ctx
= isl_ast_expr_get_ctx(expr
);
168 switch (expr
->type
) {
169 case isl_ast_expr_int
:
170 dup
= isl_ast_expr_from_val(isl_val_copy(expr
->u
.v
));
172 case isl_ast_expr_id
:
173 dup
= isl_ast_expr_from_id(isl_id_copy(expr
->u
.id
));
175 case isl_ast_expr_op
:
176 dup
= isl_ast_expr_alloc_op(ctx
,
177 expr
->u
.op
.op
, expr
->u
.op
.n_arg
);
180 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
)
182 isl_ast_expr_copy(expr
->u
.op
.args
[i
]);
184 case isl_ast_expr_error
:
194 __isl_give isl_ast_expr
*isl_ast_expr_cow(__isl_take isl_ast_expr
*expr
)
202 return isl_ast_expr_dup(expr
);
205 __isl_null isl_ast_expr
*isl_ast_expr_free(__isl_take isl_ast_expr
*expr
)
215 isl_ctx_deref(expr
->ctx
);
217 switch (expr
->type
) {
218 case isl_ast_expr_int
:
219 isl_val_free(expr
->u
.v
);
221 case isl_ast_expr_id
:
222 isl_id_free(expr
->u
.id
);
224 case isl_ast_expr_op
:
226 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
)
227 isl_ast_expr_free(expr
->u
.op
.args
[i
]);
228 free(expr
->u
.op
.args
);
230 case isl_ast_expr_error
:
238 isl_ctx
*isl_ast_expr_get_ctx(__isl_keep isl_ast_expr
*expr
)
240 return expr
? expr
->ctx
: NULL
;
243 enum isl_ast_expr_type
isl_ast_expr_get_type(__isl_keep isl_ast_expr
*expr
)
245 return expr
? expr
->type
: isl_ast_expr_error
;
248 /* Return the integer value represented by "expr".
250 __isl_give isl_val
*isl_ast_expr_int_get_val(__isl_keep isl_ast_expr
*expr
)
254 if (expr
->type
!= isl_ast_expr_int
)
255 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
256 "expression not an int", return NULL
);
257 return isl_val_copy(expr
->u
.v
);
260 /* This is an alternative name for the function above.
262 __isl_give isl_val
*isl_ast_expr_get_val(__isl_keep isl_ast_expr
*expr
)
264 return isl_ast_expr_int_get_val(expr
);
267 __isl_give isl_id
*isl_ast_expr_id_get_id(__isl_keep isl_ast_expr
*expr
)
271 if (expr
->type
!= isl_ast_expr_id
)
272 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
273 "expression not an identifier", return NULL
);
275 return isl_id_copy(expr
->u
.id
);
278 /* This is an alternative name for the function above.
280 __isl_give isl_id
*isl_ast_expr_get_id(__isl_keep isl_ast_expr
*expr
)
282 return isl_ast_expr_id_get_id(expr
);
285 /* Return the type of operation represented by "expr".
287 enum isl_ast_expr_op_type
isl_ast_expr_op_get_type(
288 __isl_keep isl_ast_expr
*expr
)
291 return isl_ast_expr_op_error
;
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",
295 return isl_ast_expr_op_error
);
296 return expr
->u
.op
.op
;
299 /* This is an alternative name for the function above.
301 enum isl_ast_expr_op_type
isl_ast_expr_get_op_type(
302 __isl_keep isl_ast_expr
*expr
)
304 return isl_ast_expr_op_get_type(expr
);
307 /* Return the number of arguments of the operation represented by "expr".
309 isl_size
isl_ast_expr_op_get_n_arg(__isl_keep isl_ast_expr
*expr
)
312 return isl_size_error
;
313 if (expr
->type
!= isl_ast_expr_op
)
314 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
315 "expression not an operation", return isl_size_error
);
316 return expr
->u
.op
.n_arg
;
319 /* This is an alternative name for the function above.
321 isl_size
isl_ast_expr_get_op_n_arg(__isl_keep isl_ast_expr
*expr
)
323 return isl_ast_expr_op_get_n_arg(expr
);
326 /* Return the argument at position "pos" of the operation represented by "expr".
328 __isl_give isl_ast_expr
*isl_ast_expr_op_get_arg(__isl_keep isl_ast_expr
*expr
,
333 if (expr
->type
!= isl_ast_expr_op
)
334 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
335 "expression not an operation", return NULL
);
336 if (pos
< 0 || pos
>= expr
->u
.op
.n_arg
)
337 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
338 "index out of bounds", return NULL
);
340 return isl_ast_expr_copy(expr
->u
.op
.args
[pos
]);
343 /* This is an alternative name for the function above.
345 __isl_give isl_ast_expr
*isl_ast_expr_get_op_arg(__isl_keep isl_ast_expr
*expr
,
348 return isl_ast_expr_op_get_arg(expr
, pos
);
351 /* Replace the argument at position "pos" of "expr" by "arg".
353 __isl_give isl_ast_expr
*isl_ast_expr_set_op_arg(__isl_take isl_ast_expr
*expr
,
354 int pos
, __isl_take isl_ast_expr
*arg
)
356 expr
= isl_ast_expr_cow(expr
);
359 if (expr
->type
!= isl_ast_expr_op
)
360 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
361 "expression not an operation", goto error
);
362 if (pos
< 0 || pos
>= expr
->u
.op
.n_arg
)
363 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
364 "index out of bounds", goto error
);
366 isl_ast_expr_free(expr
->u
.op
.args
[pos
]);
367 expr
->u
.op
.args
[pos
] = arg
;
371 isl_ast_expr_free(arg
);
372 return isl_ast_expr_free(expr
);
375 /* Is "expr1" equal to "expr2"?
377 isl_bool
isl_ast_expr_is_equal(__isl_keep isl_ast_expr
*expr1
,
378 __isl_keep isl_ast_expr
*expr2
)
382 if (!expr1
|| !expr2
)
383 return isl_bool_error
;
386 return isl_bool_true
;
387 if (expr1
->type
!= expr2
->type
)
388 return isl_bool_false
;
389 switch (expr1
->type
) {
390 case isl_ast_expr_int
:
391 return isl_val_eq(expr1
->u
.v
, expr2
->u
.v
);
392 case isl_ast_expr_id
:
393 return isl_bool_ok(expr1
->u
.id
== expr2
->u
.id
);
394 case isl_ast_expr_op
:
395 if (expr1
->u
.op
.op
!= expr2
->u
.op
.op
)
396 return isl_bool_false
;
397 if (expr1
->u
.op
.n_arg
!= expr2
->u
.op
.n_arg
)
398 return isl_bool_false
;
399 for (i
= 0; i
< expr1
->u
.op
.n_arg
; ++i
) {
401 equal
= isl_ast_expr_is_equal(expr1
->u
.op
.args
[i
],
402 expr2
->u
.op
.args
[i
]);
403 if (equal
< 0 || !equal
)
406 return isl_bool_true
;
407 case isl_ast_expr_error
:
408 return isl_bool_error
;
411 isl_die(isl_ast_expr_get_ctx(expr1
), isl_error_internal
,
412 "unhandled case", return isl_bool_error
);
415 /* Create a new operation expression of operation type "op",
416 * with "n_arg" as yet unspecified arguments.
418 __isl_give isl_ast_expr
*isl_ast_expr_alloc_op(isl_ctx
*ctx
,
419 enum isl_ast_expr_op_type op
, int n_arg
)
423 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
430 expr
->type
= isl_ast_expr_op
;
432 expr
->u
.op
.n_arg
= n_arg
;
433 expr
->u
.op
.args
= isl_calloc_array(ctx
, isl_ast_expr
*, n_arg
);
435 if (n_arg
&& !expr
->u
.op
.args
)
436 return isl_ast_expr_free(expr
);
441 /* Create a new id expression representing "id".
443 __isl_give isl_ast_expr
*isl_ast_expr_from_id(__isl_take isl_id
*id
)
451 ctx
= isl_id_get_ctx(id
);
452 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
459 expr
->type
= isl_ast_expr_id
;
468 /* Create a new integer expression representing "i".
470 __isl_give isl_ast_expr
*isl_ast_expr_alloc_int_si(isl_ctx
*ctx
, int i
)
474 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
481 expr
->type
= isl_ast_expr_int
;
482 expr
->u
.v
= isl_val_int_from_si(ctx
, i
);
484 return isl_ast_expr_free(expr
);
489 /* Create a new integer expression representing "v".
491 __isl_give isl_ast_expr
*isl_ast_expr_from_val(__isl_take isl_val
*v
)
498 if (!isl_val_is_int(v
))
499 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
500 "expecting integer value", goto error
);
502 ctx
= isl_val_get_ctx(v
);
503 expr
= isl_calloc_type(ctx
, isl_ast_expr
);
510 expr
->type
= isl_ast_expr_int
;
519 /* Create an expression representing the unary operation "type" applied to
522 __isl_give isl_ast_expr
*isl_ast_expr_alloc_unary(
523 enum isl_ast_expr_op_type type
, __isl_take isl_ast_expr
*arg
)
526 isl_ast_expr
*expr
= NULL
;
531 ctx
= isl_ast_expr_get_ctx(arg
);
532 expr
= isl_ast_expr_alloc_op(ctx
, type
, 1);
536 expr
->u
.op
.args
[0] = arg
;
540 isl_ast_expr_free(arg
);
544 /* Create an expression representing the negation of "arg".
546 __isl_give isl_ast_expr
*isl_ast_expr_neg(__isl_take isl_ast_expr
*arg
)
548 return isl_ast_expr_alloc_unary(isl_ast_expr_op_minus
, arg
);
551 /* Create an expression representing the address of "expr".
553 __isl_give isl_ast_expr
*isl_ast_expr_address_of(__isl_take isl_ast_expr
*expr
)
558 if (isl_ast_expr_get_type(expr
) != isl_ast_expr_op
||
559 isl_ast_expr_get_op_type(expr
) != isl_ast_expr_op_access
)
560 isl_die(isl_ast_expr_get_ctx(expr
), isl_error_invalid
,
561 "can only take address of access expressions",
562 return isl_ast_expr_free(expr
));
564 return isl_ast_expr_alloc_unary(isl_ast_expr_op_address_of
, expr
);
567 /* Create an expression representing the binary operation "type"
568 * applied to "expr1" and "expr2".
570 __isl_give isl_ast_expr
*isl_ast_expr_alloc_binary(
571 enum isl_ast_expr_op_type type
,
572 __isl_take isl_ast_expr
*expr1
, __isl_take isl_ast_expr
*expr2
)
575 isl_ast_expr
*expr
= NULL
;
577 if (!expr1
|| !expr2
)
580 ctx
= isl_ast_expr_get_ctx(expr1
);
581 expr
= isl_ast_expr_alloc_op(ctx
, type
, 2);
585 expr
->u
.op
.args
[0] = expr1
;
586 expr
->u
.op
.args
[1] = expr2
;
590 isl_ast_expr_free(expr1
);
591 isl_ast_expr_free(expr2
);
595 /* Create an expression representing the sum of "expr1" and "expr2".
597 __isl_give isl_ast_expr
*isl_ast_expr_add(__isl_take isl_ast_expr
*expr1
,
598 __isl_take isl_ast_expr
*expr2
)
600 return isl_ast_expr_alloc_binary(isl_ast_expr_op_add
, expr1
, expr2
);
603 /* Create an expression representing the difference of "expr1" and "expr2".
605 __isl_give isl_ast_expr
*isl_ast_expr_sub(__isl_take isl_ast_expr
*expr1
,
606 __isl_take isl_ast_expr
*expr2
)
608 return isl_ast_expr_alloc_binary(isl_ast_expr_op_sub
, expr1
, expr2
);
611 /* Create an expression representing the product of "expr1" and "expr2".
613 __isl_give isl_ast_expr
*isl_ast_expr_mul(__isl_take isl_ast_expr
*expr1
,
614 __isl_take isl_ast_expr
*expr2
)
616 return isl_ast_expr_alloc_binary(isl_ast_expr_op_mul
, expr1
, expr2
);
619 /* Create an expression representing the quotient of "expr1" and "expr2".
621 __isl_give isl_ast_expr
*isl_ast_expr_div(__isl_take isl_ast_expr
*expr1
,
622 __isl_take isl_ast_expr
*expr2
)
624 return isl_ast_expr_alloc_binary(isl_ast_expr_op_div
, expr1
, expr2
);
627 /* Create an expression representing the quotient of the integer
628 * division of "expr1" by "expr2", where "expr1" is known to be
631 __isl_give isl_ast_expr
*isl_ast_expr_pdiv_q(__isl_take isl_ast_expr
*expr1
,
632 __isl_take isl_ast_expr
*expr2
)
634 return isl_ast_expr_alloc_binary(isl_ast_expr_op_pdiv_q
, expr1
, expr2
);
637 /* Create an expression representing the remainder of the integer
638 * division of "expr1" by "expr2", where "expr1" is known to be
641 __isl_give isl_ast_expr
*isl_ast_expr_pdiv_r(__isl_take isl_ast_expr
*expr1
,
642 __isl_take isl_ast_expr
*expr2
)
644 return isl_ast_expr_alloc_binary(isl_ast_expr_op_pdiv_r
, expr1
, expr2
);
647 /* Create an expression representing the conjunction of "expr1" and "expr2".
649 __isl_give isl_ast_expr
*isl_ast_expr_and(__isl_take isl_ast_expr
*expr1
,
650 __isl_take isl_ast_expr
*expr2
)
652 return isl_ast_expr_alloc_binary(isl_ast_expr_op_and
, expr1
, expr2
);
655 /* Create an expression representing the conjunction of "expr1" and "expr2",
656 * where "expr2" is evaluated only if "expr1" is evaluated to true.
658 __isl_give isl_ast_expr
*isl_ast_expr_and_then(__isl_take isl_ast_expr
*expr1
,
659 __isl_take isl_ast_expr
*expr2
)
661 return isl_ast_expr_alloc_binary(isl_ast_expr_op_and_then
, expr1
, expr2
);
664 /* Create an expression representing the disjunction of "expr1" and "expr2".
666 __isl_give isl_ast_expr
*isl_ast_expr_or(__isl_take isl_ast_expr
*expr1
,
667 __isl_take isl_ast_expr
*expr2
)
669 return isl_ast_expr_alloc_binary(isl_ast_expr_op_or
, expr1
, expr2
);
672 /* Create an expression representing the disjunction of "expr1" and "expr2",
673 * where "expr2" is evaluated only if "expr1" is evaluated to false.
675 __isl_give isl_ast_expr
*isl_ast_expr_or_else(__isl_take isl_ast_expr
*expr1
,
676 __isl_take isl_ast_expr
*expr2
)
678 return isl_ast_expr_alloc_binary(isl_ast_expr_op_or_else
, expr1
, expr2
);
681 /* Create an expression representing "expr1" less than or equal to "expr2".
683 __isl_give isl_ast_expr
*isl_ast_expr_le(__isl_take isl_ast_expr
*expr1
,
684 __isl_take isl_ast_expr
*expr2
)
686 return isl_ast_expr_alloc_binary(isl_ast_expr_op_le
, expr1
, expr2
);
689 /* Create an expression representing "expr1" less than "expr2".
691 __isl_give isl_ast_expr
*isl_ast_expr_lt(__isl_take isl_ast_expr
*expr1
,
692 __isl_take isl_ast_expr
*expr2
)
694 return isl_ast_expr_alloc_binary(isl_ast_expr_op_lt
, expr1
, expr2
);
697 /* Create an expression representing "expr1" greater than or equal to "expr2".
699 __isl_give isl_ast_expr
*isl_ast_expr_ge(__isl_take isl_ast_expr
*expr1
,
700 __isl_take isl_ast_expr
*expr2
)
702 return isl_ast_expr_alloc_binary(isl_ast_expr_op_ge
, expr1
, expr2
);
705 /* Create an expression representing "expr1" greater than "expr2".
707 __isl_give isl_ast_expr
*isl_ast_expr_gt(__isl_take isl_ast_expr
*expr1
,
708 __isl_take isl_ast_expr
*expr2
)
710 return isl_ast_expr_alloc_binary(isl_ast_expr_op_gt
, expr1
, expr2
);
713 /* Create an expression representing "expr1" equal to "expr2".
715 __isl_give isl_ast_expr
*isl_ast_expr_eq(__isl_take isl_ast_expr
*expr1
,
716 __isl_take isl_ast_expr
*expr2
)
718 return isl_ast_expr_alloc_binary(isl_ast_expr_op_eq
, expr1
, expr2
);
721 /* Create an expression of type "type" with as arguments "arg0" followed
724 static __isl_give isl_ast_expr
*ast_expr_with_arguments(
725 enum isl_ast_expr_op_type type
, __isl_take isl_ast_expr
*arg0
,
726 __isl_take isl_ast_expr_list
*arguments
)
731 isl_ast_expr
*res
= NULL
;
733 if (!arg0
|| !arguments
)
736 ctx
= isl_ast_expr_get_ctx(arg0
);
737 n
= isl_ast_expr_list_n_ast_expr(arguments
);
740 res
= isl_ast_expr_alloc_op(ctx
, type
, 1 + n
);
743 for (i
= 0; i
< n
; ++i
) {
745 arg
= isl_ast_expr_list_get_ast_expr(arguments
, i
);
746 res
->u
.op
.args
[1 + i
] = arg
;
750 res
->u
.op
.args
[0] = arg0
;
752 isl_ast_expr_list_free(arguments
);
755 isl_ast_expr_free(arg0
);
756 isl_ast_expr_list_free(arguments
);
757 isl_ast_expr_free(res
);
761 /* Create an expression representing an access to "array" with index
762 * expressions "indices".
764 __isl_give isl_ast_expr
*isl_ast_expr_access(__isl_take isl_ast_expr
*array
,
765 __isl_take isl_ast_expr_list
*indices
)
767 return ast_expr_with_arguments(isl_ast_expr_op_access
, array
, indices
);
770 /* Create an expression representing a call to "function" with argument
771 * expressions "arguments".
773 __isl_give isl_ast_expr
*isl_ast_expr_call(__isl_take isl_ast_expr
*function
,
774 __isl_take isl_ast_expr_list
*arguments
)
776 return ast_expr_with_arguments(isl_ast_expr_op_call
, function
, arguments
);
779 /* For each subexpression of "expr" of type isl_ast_expr_id,
780 * if it appears in "id2expr", then replace it by the corresponding
783 __isl_give isl_ast_expr
*isl_ast_expr_substitute_ids(
784 __isl_take isl_ast_expr
*expr
, __isl_take isl_id_to_ast_expr
*id2expr
)
787 isl_maybe_isl_ast_expr m
;
789 if (!expr
|| !id2expr
)
792 switch (expr
->type
) {
793 case isl_ast_expr_int
:
795 case isl_ast_expr_id
:
796 m
= isl_id_to_ast_expr_try_get(id2expr
, expr
->u
.id
);
801 isl_ast_expr_free(expr
);
804 case isl_ast_expr_op
:
805 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
) {
807 arg
= isl_ast_expr_copy(expr
->u
.op
.args
[i
]);
808 arg
= isl_ast_expr_substitute_ids(arg
,
809 isl_id_to_ast_expr_copy(id2expr
));
810 if (arg
== expr
->u
.op
.args
[i
]) {
811 isl_ast_expr_free(arg
);
815 expr
= isl_ast_expr_free(expr
);
816 expr
= isl_ast_expr_cow(expr
);
818 isl_ast_expr_free(arg
);
821 isl_ast_expr_free(expr
->u
.op
.args
[i
]);
822 expr
->u
.op
.args
[i
] = arg
;
825 case isl_ast_expr_error
:
826 expr
= isl_ast_expr_free(expr
);
830 isl_id_to_ast_expr_free(id2expr
);
833 isl_ast_expr_free(expr
);
834 isl_id_to_ast_expr_free(id2expr
);
838 isl_ctx
*isl_ast_node_get_ctx(__isl_keep isl_ast_node
*node
)
840 return node
? node
->ctx
: NULL
;
843 enum isl_ast_node_type
isl_ast_node_get_type(__isl_keep isl_ast_node
*node
)
845 return node
? node
->type
: isl_ast_node_error
;
848 __isl_give isl_ast_node
*isl_ast_node_alloc(isl_ctx
*ctx
,
849 enum isl_ast_node_type type
)
853 node
= isl_calloc_type(ctx
, isl_ast_node
);
865 /* Create an if node with the given guard.
867 * The then body needs to be filled in later.
869 __isl_give isl_ast_node
*isl_ast_node_alloc_if(__isl_take isl_ast_expr
*guard
)
876 node
= isl_ast_node_alloc(isl_ast_expr_get_ctx(guard
), isl_ast_node_if
);
879 node
->u
.i
.guard
= guard
;
883 isl_ast_expr_free(guard
);
887 /* Create a for node with the given iterator.
889 * The remaining fields need to be filled in later.
891 __isl_give isl_ast_node
*isl_ast_node_alloc_for(__isl_take isl_id
*id
)
899 ctx
= isl_id_get_ctx(id
);
900 node
= isl_ast_node_alloc(ctx
, isl_ast_node_for
);
904 node
->u
.f
.iterator
= isl_ast_expr_from_id(id
);
905 if (!node
->u
.f
.iterator
)
906 return isl_ast_node_free(node
);
914 /* Create a mark node, marking "node" with "id".
916 __isl_give isl_ast_node
*isl_ast_node_alloc_mark(__isl_take isl_id
*id
,
917 __isl_take isl_ast_node
*node
)
925 ctx
= isl_id_get_ctx(id
);
926 mark
= isl_ast_node_alloc(ctx
, isl_ast_node_mark
);
931 mark
->u
.m
.node
= node
;
936 isl_ast_node_free(node
);
940 /* Create a user node evaluating "expr".
942 __isl_give isl_ast_node
*isl_ast_node_alloc_user(__isl_take isl_ast_expr
*expr
)
950 ctx
= isl_ast_expr_get_ctx(expr
);
951 node
= isl_ast_node_alloc(ctx
, isl_ast_node_user
);
955 node
->u
.e
.expr
= expr
;
959 isl_ast_expr_free(expr
);
963 /* Create a block node with the given children.
965 __isl_give isl_ast_node
*isl_ast_node_alloc_block(
966 __isl_take isl_ast_node_list
*list
)
974 ctx
= isl_ast_node_list_get_ctx(list
);
975 node
= isl_ast_node_alloc(ctx
, isl_ast_node_block
);
979 node
->u
.b
.children
= list
;
983 isl_ast_node_list_free(list
);
987 /* Represent the given list of nodes as a single node, either by
988 * extract the node from a single element list or by creating
989 * a block node with the list of nodes as children.
991 __isl_give isl_ast_node
*isl_ast_node_from_ast_node_list(
992 __isl_take isl_ast_node_list
*list
)
997 n
= isl_ast_node_list_n_ast_node(list
);
1001 return isl_ast_node_alloc_block(list
);
1003 node
= isl_ast_node_list_get_ast_node(list
, 0);
1004 isl_ast_node_list_free(list
);
1008 isl_ast_node_list_free(list
);
1012 __isl_give isl_ast_node
*isl_ast_node_copy(__isl_keep isl_ast_node
*node
)
1021 __isl_give isl_ast_node
*isl_ast_node_dup(__isl_keep isl_ast_node
*node
)
1028 dup
= isl_ast_node_alloc(isl_ast_node_get_ctx(node
), node
->type
);
1032 switch (node
->type
) {
1033 case isl_ast_node_if
:
1034 dup
->u
.i
.guard
= isl_ast_expr_copy(node
->u
.i
.guard
);
1035 dup
->u
.i
.then
= isl_ast_node_copy(node
->u
.i
.then
);
1036 dup
->u
.i
.else_node
= isl_ast_node_copy(node
->u
.i
.else_node
);
1037 if (!dup
->u
.i
.guard
|| !dup
->u
.i
.then
||
1038 (node
->u
.i
.else_node
&& !dup
->u
.i
.else_node
))
1039 return isl_ast_node_free(dup
);
1041 case isl_ast_node_for
:
1042 dup
->u
.f
.iterator
= isl_ast_expr_copy(node
->u
.f
.iterator
);
1043 dup
->u
.f
.init
= isl_ast_expr_copy(node
->u
.f
.init
);
1044 dup
->u
.f
.cond
= isl_ast_expr_copy(node
->u
.f
.cond
);
1045 dup
->u
.f
.inc
= isl_ast_expr_copy(node
->u
.f
.inc
);
1046 dup
->u
.f
.body
= isl_ast_node_copy(node
->u
.f
.body
);
1047 if (!dup
->u
.f
.iterator
|| !dup
->u
.f
.init
|| !dup
->u
.f
.cond
||
1048 !dup
->u
.f
.inc
|| !dup
->u
.f
.body
)
1049 return isl_ast_node_free(dup
);
1051 case isl_ast_node_block
:
1052 dup
->u
.b
.children
= isl_ast_node_list_copy(node
->u
.b
.children
);
1053 if (!dup
->u
.b
.children
)
1054 return isl_ast_node_free(dup
);
1056 case isl_ast_node_mark
:
1057 dup
->u
.m
.mark
= isl_id_copy(node
->u
.m
.mark
);
1058 dup
->u
.m
.node
= isl_ast_node_copy(node
->u
.m
.node
);
1059 if (!dup
->u
.m
.mark
|| !dup
->u
.m
.node
)
1060 return isl_ast_node_free(dup
);
1062 case isl_ast_node_user
:
1063 dup
->u
.e
.expr
= isl_ast_expr_copy(node
->u
.e
.expr
);
1065 return isl_ast_node_free(dup
);
1067 case isl_ast_node_error
:
1071 if (!node
->annotation
)
1073 dup
->annotation
= isl_id_copy(node
->annotation
);
1074 if (!dup
->annotation
)
1075 return isl_ast_node_free(dup
);
1080 __isl_give isl_ast_node
*isl_ast_node_cow(__isl_take isl_ast_node
*node
)
1088 return isl_ast_node_dup(node
);
1091 __isl_null isl_ast_node
*isl_ast_node_free(__isl_take isl_ast_node
*node
)
1096 if (--node
->ref
> 0)
1099 switch (node
->type
) {
1100 case isl_ast_node_if
:
1101 isl_ast_expr_free(node
->u
.i
.guard
);
1102 isl_ast_node_free(node
->u
.i
.then
);
1103 isl_ast_node_free(node
->u
.i
.else_node
);
1105 case isl_ast_node_for
:
1106 isl_ast_expr_free(node
->u
.f
.iterator
);
1107 isl_ast_expr_free(node
->u
.f
.init
);
1108 isl_ast_expr_free(node
->u
.f
.cond
);
1109 isl_ast_expr_free(node
->u
.f
.inc
);
1110 isl_ast_node_free(node
->u
.f
.body
);
1112 case isl_ast_node_block
:
1113 isl_ast_node_list_free(node
->u
.b
.children
);
1115 case isl_ast_node_mark
:
1116 isl_id_free(node
->u
.m
.mark
);
1117 isl_ast_node_free(node
->u
.m
.node
);
1119 case isl_ast_node_user
:
1120 isl_ast_expr_free(node
->u
.e
.expr
);
1122 case isl_ast_node_error
:
1126 isl_id_free(node
->annotation
);
1127 isl_ctx_deref(node
->ctx
);
1133 /* Replace the body of the for node "node" by "body".
1135 __isl_give isl_ast_node
*isl_ast_node_for_set_body(
1136 __isl_take isl_ast_node
*node
, __isl_take isl_ast_node
*body
)
1138 node
= isl_ast_node_cow(node
);
1141 if (node
->type
!= isl_ast_node_for
)
1142 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1143 "not a for node", goto error
);
1145 isl_ast_node_free(node
->u
.f
.body
);
1146 node
->u
.f
.body
= body
;
1150 isl_ast_node_free(node
);
1151 isl_ast_node_free(body
);
1155 __isl_give isl_ast_node
*isl_ast_node_for_get_body(
1156 __isl_keep isl_ast_node
*node
)
1160 if (node
->type
!= isl_ast_node_for
)
1161 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1162 "not a for node", return NULL
);
1163 return isl_ast_node_copy(node
->u
.f
.body
);
1166 /* Mark the given for node as being degenerate.
1168 __isl_give isl_ast_node
*isl_ast_node_for_mark_degenerate(
1169 __isl_take isl_ast_node
*node
)
1171 node
= isl_ast_node_cow(node
);
1174 node
->u
.f
.degenerate
= 1;
1178 isl_bool
isl_ast_node_for_is_degenerate(__isl_keep isl_ast_node
*node
)
1181 return isl_bool_error
;
1182 if (node
->type
!= isl_ast_node_for
)
1183 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1184 "not a for node", return isl_bool_error
);
1185 return isl_bool_ok(node
->u
.f
.degenerate
);
1188 __isl_give isl_ast_expr
*isl_ast_node_for_get_iterator(
1189 __isl_keep isl_ast_node
*node
)
1193 if (node
->type
!= isl_ast_node_for
)
1194 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1195 "not a for node", return NULL
);
1196 return isl_ast_expr_copy(node
->u
.f
.iterator
);
1199 __isl_give isl_ast_expr
*isl_ast_node_for_get_init(
1200 __isl_keep isl_ast_node
*node
)
1204 if (node
->type
!= isl_ast_node_for
)
1205 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1206 "not a for node", return NULL
);
1207 return isl_ast_expr_copy(node
->u
.f
.init
);
1210 /* Return the condition expression of the given for node.
1212 * If the for node is degenerate, then the condition is not explicitly
1213 * stored in the node. Instead, it is constructed as
1217 __isl_give isl_ast_expr
*isl_ast_node_for_get_cond(
1218 __isl_keep isl_ast_node
*node
)
1222 if (node
->type
!= isl_ast_node_for
)
1223 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1224 "not a for node", return NULL
);
1225 if (!node
->u
.f
.degenerate
)
1226 return isl_ast_expr_copy(node
->u
.f
.cond
);
1228 return isl_ast_expr_alloc_binary(isl_ast_expr_op_le
,
1229 isl_ast_expr_copy(node
->u
.f
.iterator
),
1230 isl_ast_expr_copy(node
->u
.f
.init
));
1233 /* Return the increment of the given for node.
1235 * If the for node is degenerate, then the increment is not explicitly
1236 * stored in the node. We simply return "1".
1238 __isl_give isl_ast_expr
*isl_ast_node_for_get_inc(
1239 __isl_keep isl_ast_node
*node
)
1243 if (node
->type
!= isl_ast_node_for
)
1244 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1245 "not a for node", return NULL
);
1246 if (!node
->u
.f
.degenerate
)
1247 return isl_ast_expr_copy(node
->u
.f
.inc
);
1248 return isl_ast_expr_alloc_int_si(isl_ast_node_get_ctx(node
), 1);
1251 /* Replace the then branch of the if node "node" by "child".
1253 __isl_give isl_ast_node
*isl_ast_node_if_set_then(
1254 __isl_take isl_ast_node
*node
, __isl_take isl_ast_node
*child
)
1256 node
= isl_ast_node_cow(node
);
1257 if (!node
|| !child
)
1259 if (node
->type
!= isl_ast_node_if
)
1260 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1261 "not an if node", goto error
);
1263 isl_ast_node_free(node
->u
.i
.then
);
1264 node
->u
.i
.then
= child
;
1268 isl_ast_node_free(node
);
1269 isl_ast_node_free(child
);
1273 /* Return the then-node of the given if-node.
1275 __isl_give isl_ast_node
*isl_ast_node_if_get_then_node(
1276 __isl_keep isl_ast_node
*node
)
1280 if (node
->type
!= isl_ast_node_if
)
1281 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1282 "not an if node", return NULL
);
1283 return isl_ast_node_copy(node
->u
.i
.then
);
1286 /* This is an alternative name for the function above.
1288 __isl_give isl_ast_node
*isl_ast_node_if_get_then(
1289 __isl_keep isl_ast_node
*node
)
1291 return isl_ast_node_if_get_then_node(node
);
1294 /* Does the given if-node have an else-node?
1296 isl_bool
isl_ast_node_if_has_else_node(__isl_keep isl_ast_node
*node
)
1299 return isl_bool_error
;
1300 if (node
->type
!= isl_ast_node_if
)
1301 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1302 "not an if node", return isl_bool_error
);
1303 return isl_bool_ok(node
->u
.i
.else_node
!= NULL
);
1306 /* This is an alternative name for the function above.
1308 isl_bool
isl_ast_node_if_has_else(__isl_keep isl_ast_node
*node
)
1310 return isl_ast_node_if_has_else_node(node
);
1313 /* Return the else-node of the given if-node,
1314 * assuming there is one.
1316 __isl_give isl_ast_node
*isl_ast_node_if_get_else_node(
1317 __isl_keep isl_ast_node
*node
)
1321 if (node
->type
!= isl_ast_node_if
)
1322 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1323 "not an if node", return NULL
);
1324 return isl_ast_node_copy(node
->u
.i
.else_node
);
1327 /* This is an alternative name for the function above.
1329 __isl_give isl_ast_node
*isl_ast_node_if_get_else(
1330 __isl_keep isl_ast_node
*node
)
1332 return isl_ast_node_if_get_else_node(node
);
1335 __isl_give isl_ast_expr
*isl_ast_node_if_get_cond(
1336 __isl_keep isl_ast_node
*node
)
1340 if (node
->type
!= isl_ast_node_if
)
1341 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1342 "not a guard node", return NULL
);
1343 return isl_ast_expr_copy(node
->u
.i
.guard
);
1346 __isl_give isl_ast_node_list
*isl_ast_node_block_get_children(
1347 __isl_keep isl_ast_node
*node
)
1351 if (node
->type
!= isl_ast_node_block
)
1352 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1353 "not a block node", return NULL
);
1354 return isl_ast_node_list_copy(node
->u
.b
.children
);
1357 __isl_give isl_ast_expr
*isl_ast_node_user_get_expr(
1358 __isl_keep isl_ast_node
*node
)
1362 if (node
->type
!= isl_ast_node_user
)
1363 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1364 "not a user node", return NULL
);
1366 return isl_ast_expr_copy(node
->u
.e
.expr
);
1369 /* Return the mark identifier of the mark node "node".
1371 __isl_give isl_id
*isl_ast_node_mark_get_id(__isl_keep isl_ast_node
*node
)
1375 if (node
->type
!= isl_ast_node_mark
)
1376 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1377 "not a mark node", return NULL
);
1379 return isl_id_copy(node
->u
.m
.mark
);
1382 /* Return the node marked by mark node "node".
1384 __isl_give isl_ast_node
*isl_ast_node_mark_get_node(
1385 __isl_keep isl_ast_node
*node
)
1389 if (node
->type
!= isl_ast_node_mark
)
1390 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
1391 "not a mark node", return NULL
);
1393 return isl_ast_node_copy(node
->u
.m
.node
);
1396 __isl_give isl_id
*isl_ast_node_get_annotation(__isl_keep isl_ast_node
*node
)
1398 return node
? isl_id_copy(node
->annotation
) : NULL
;
1401 /* Replace node->annotation by "annotation".
1403 __isl_give isl_ast_node
*isl_ast_node_set_annotation(
1404 __isl_take isl_ast_node
*node
, __isl_take isl_id
*annotation
)
1406 node
= isl_ast_node_cow(node
);
1407 if (!node
|| !annotation
)
1410 isl_id_free(node
->annotation
);
1411 node
->annotation
= annotation
;
1415 isl_id_free(annotation
);
1416 return isl_ast_node_free(node
);
1419 /* Traverse the elements of "list" and all their descendants
1420 * in depth first preorder.
1422 * Return isl_stat_ok on success and isl_stat_error on failure.
1424 static isl_stat
nodelist_foreach(__isl_keep isl_ast_node_list
*list
,
1425 isl_bool (*fn
)(__isl_keep isl_ast_node
*node
, void *user
), void *user
)
1430 return isl_stat_error
;
1432 for (i
= 0; i
< list
->n
; ++i
) {
1434 isl_ast_node
*node
= list
->p
[i
];
1436 ok
= isl_ast_node_foreach_descendant_top_down(node
, fn
, user
);
1438 return isl_stat_error
;
1444 /* Traverse the descendants of "node" (including the node itself)
1445 * in depth first preorder.
1447 * If "fn" returns isl_bool_error on any of the nodes, then the traversal
1449 * If "fn" returns isl_bool_false on any of the nodes, then the subtree rooted
1450 * at that node is skipped.
1452 * Return isl_stat_ok on success and isl_stat_error on failure.
1454 isl_stat
isl_ast_node_foreach_descendant_top_down(
1455 __isl_keep isl_ast_node
*node
,
1456 isl_bool (*fn
)(__isl_keep isl_ast_node
*node
, void *user
), void *user
)
1462 return isl_stat_error
;
1464 more
= fn(node
, user
);
1466 return isl_stat_error
;
1470 switch (node
->type
) {
1471 case isl_ast_node_for
:
1472 node
= node
->u
.f
.body
;
1473 return isl_ast_node_foreach_descendant_top_down(node
, fn
, user
);
1474 case isl_ast_node_if
:
1475 ok
= isl_ast_node_foreach_descendant_top_down(node
->u
.i
.then
,
1478 return isl_stat_error
;
1479 if (!node
->u
.i
.else_node
)
1481 node
= node
->u
.i
.else_node
;
1482 return isl_ast_node_foreach_descendant_top_down(node
, fn
, user
);
1483 case isl_ast_node_block
:
1484 return nodelist_foreach(node
->u
.b
.children
, fn
, user
);
1485 case isl_ast_node_mark
:
1486 node
= node
->u
.m
.node
;
1487 return isl_ast_node_foreach_descendant_top_down(node
, fn
, user
);
1488 case isl_ast_node_user
:
1490 case isl_ast_node_error
:
1491 return isl_stat_error
;
1497 /* Textual C representation of the various operators.
1499 static char *op_str_c
[] = {
1500 [isl_ast_expr_op_and
] = "&&",
1501 [isl_ast_expr_op_and_then
] = "&&",
1502 [isl_ast_expr_op_or
] = "||",
1503 [isl_ast_expr_op_or_else
] = "||",
1504 [isl_ast_expr_op_max
] = "max",
1505 [isl_ast_expr_op_min
] = "min",
1506 [isl_ast_expr_op_minus
] = "-",
1507 [isl_ast_expr_op_add
] = "+",
1508 [isl_ast_expr_op_sub
] = "-",
1509 [isl_ast_expr_op_mul
] = "*",
1510 [isl_ast_expr_op_fdiv_q
] = "floord",
1511 [isl_ast_expr_op_pdiv_q
] = "/",
1512 [isl_ast_expr_op_pdiv_r
] = "%",
1513 [isl_ast_expr_op_zdiv_r
] = "%",
1514 [isl_ast_expr_op_div
] = "/",
1515 [isl_ast_expr_op_eq
] = "==",
1516 [isl_ast_expr_op_le
] = "<=",
1517 [isl_ast_expr_op_ge
] = ">=",
1518 [isl_ast_expr_op_lt
] = "<",
1519 [isl_ast_expr_op_gt
] = ">",
1520 [isl_ast_expr_op_member
] = ".",
1521 [isl_ast_expr_op_address_of
] = "&"
1524 /* Precedence in C of the various operators.
1525 * Based on http://en.wikipedia.org/wiki/Operators_in_C_and_C++
1526 * Lowest value means highest precedence.
1528 static int op_prec
[] = {
1529 [isl_ast_expr_op_and
] = 13,
1530 [isl_ast_expr_op_and_then
] = 13,
1531 [isl_ast_expr_op_or
] = 14,
1532 [isl_ast_expr_op_or_else
] = 14,
1533 [isl_ast_expr_op_max
] = 2,
1534 [isl_ast_expr_op_min
] = 2,
1535 [isl_ast_expr_op_minus
] = 3,
1536 [isl_ast_expr_op_add
] = 6,
1537 [isl_ast_expr_op_sub
] = 6,
1538 [isl_ast_expr_op_mul
] = 5,
1539 [isl_ast_expr_op_div
] = 5,
1540 [isl_ast_expr_op_fdiv_q
] = 2,
1541 [isl_ast_expr_op_pdiv_q
] = 5,
1542 [isl_ast_expr_op_pdiv_r
] = 5,
1543 [isl_ast_expr_op_zdiv_r
] = 5,
1544 [isl_ast_expr_op_cond
] = 15,
1545 [isl_ast_expr_op_select
] = 15,
1546 [isl_ast_expr_op_eq
] = 9,
1547 [isl_ast_expr_op_le
] = 8,
1548 [isl_ast_expr_op_ge
] = 8,
1549 [isl_ast_expr_op_lt
] = 8,
1550 [isl_ast_expr_op_gt
] = 8,
1551 [isl_ast_expr_op_call
] = 2,
1552 [isl_ast_expr_op_access
] = 2,
1553 [isl_ast_expr_op_member
] = 2,
1554 [isl_ast_expr_op_address_of
] = 3
1557 /* Is the operator left-to-right associative?
1559 static int op_left
[] = {
1560 [isl_ast_expr_op_and
] = 1,
1561 [isl_ast_expr_op_and_then
] = 1,
1562 [isl_ast_expr_op_or
] = 1,
1563 [isl_ast_expr_op_or_else
] = 1,
1564 [isl_ast_expr_op_max
] = 1,
1565 [isl_ast_expr_op_min
] = 1,
1566 [isl_ast_expr_op_minus
] = 0,
1567 [isl_ast_expr_op_add
] = 1,
1568 [isl_ast_expr_op_sub
] = 1,
1569 [isl_ast_expr_op_mul
] = 1,
1570 [isl_ast_expr_op_div
] = 1,
1571 [isl_ast_expr_op_fdiv_q
] = 1,
1572 [isl_ast_expr_op_pdiv_q
] = 1,
1573 [isl_ast_expr_op_pdiv_r
] = 1,
1574 [isl_ast_expr_op_zdiv_r
] = 1,
1575 [isl_ast_expr_op_cond
] = 0,
1576 [isl_ast_expr_op_select
] = 0,
1577 [isl_ast_expr_op_eq
] = 1,
1578 [isl_ast_expr_op_le
] = 1,
1579 [isl_ast_expr_op_ge
] = 1,
1580 [isl_ast_expr_op_lt
] = 1,
1581 [isl_ast_expr_op_gt
] = 1,
1582 [isl_ast_expr_op_call
] = 1,
1583 [isl_ast_expr_op_access
] = 1,
1584 [isl_ast_expr_op_member
] = 1,
1585 [isl_ast_expr_op_address_of
] = 0
1588 static int is_and(enum isl_ast_expr_op_type op
)
1590 return op
== isl_ast_expr_op_and
|| op
== isl_ast_expr_op_and_then
;
1593 static int is_or(enum isl_ast_expr_op_type op
)
1595 return op
== isl_ast_expr_op_or
|| op
== isl_ast_expr_op_or_else
;
1598 static int is_add_sub(enum isl_ast_expr_op_type op
)
1600 return op
== isl_ast_expr_op_add
|| op
== isl_ast_expr_op_sub
;
1603 static int is_div_mod(enum isl_ast_expr_op_type op
)
1605 return op
== isl_ast_expr_op_div
||
1606 op
== isl_ast_expr_op_pdiv_r
||
1607 op
== isl_ast_expr_op_zdiv_r
;
1610 static __isl_give isl_printer
*print_ast_expr_c(__isl_take isl_printer
*p
,
1611 __isl_keep isl_ast_expr
*expr
);
1613 /* Do we need/want parentheses around "expr" as a subexpression of
1614 * an "op" operation? If "left" is set, then "expr" is the left-most
1617 * We only need parentheses if "expr" represents an operation.
1619 * If op has a higher precedence than expr->u.op.op, then we need
1621 * If op and expr->u.op.op have the same precedence, but the operations
1622 * are performed in an order that is different from the associativity,
1623 * then we need parentheses.
1625 * An and inside an or technically does not require parentheses,
1626 * but some compilers complain about that, so we add them anyway.
1628 * Computations such as "a / b * c" and "a % b + c" can be somewhat
1629 * difficult to read, so we add parentheses for those as well.
1631 static int sub_expr_need_parens(enum isl_ast_expr_op_type op
,
1632 __isl_keep isl_ast_expr
*expr
, int left
)
1634 if (expr
->type
!= isl_ast_expr_op
)
1637 if (op_prec
[expr
->u
.op
.op
] > op_prec
[op
])
1639 if (op_prec
[expr
->u
.op
.op
] == op_prec
[op
] && left
!= op_left
[op
])
1642 if (is_or(op
) && is_and(expr
->u
.op
.op
))
1644 if (op
== isl_ast_expr_op_mul
&& expr
->u
.op
.op
!= isl_ast_expr_op_mul
&&
1645 op_prec
[expr
->u
.op
.op
] == op_prec
[op
])
1647 if (is_add_sub(op
) && is_div_mod(expr
->u
.op
.op
))
1653 /* Print "expr" as a subexpression of an "op" operation in C format.
1654 * If "left" is set, then "expr" is the left-most operand.
1656 static __isl_give isl_printer
*print_sub_expr_c(__isl_take isl_printer
*p
,
1657 enum isl_ast_expr_op_type op
, __isl_keep isl_ast_expr
*expr
, int left
)
1661 need_parens
= sub_expr_need_parens(op
, expr
, left
);
1664 p
= isl_printer_print_str(p
, "(");
1665 p
= print_ast_expr_c(p
, expr
);
1667 p
= isl_printer_print_str(p
, ")");
1671 #define isl_ast_expr_op_last isl_ast_expr_op_address_of
1673 /* Data structure that holds the user-specified textual
1674 * representations for the operators in C format.
1675 * The entries are either NULL or copies of strings.
1676 * A NULL entry means that the default name should be used.
1678 struct isl_ast_expr_op_names
{
1679 char *op_str
[isl_ast_expr_op_last
+ 1];
1682 /* Create an empty struct isl_ast_expr_op_names.
1684 static void *create_names(isl_ctx
*ctx
)
1686 return isl_calloc_type(ctx
, struct isl_ast_expr_op_names
);
1689 /* Free a struct isl_ast_expr_op_names along with all memory
1690 * owned by the struct.
1692 static void free_names(void *user
)
1695 struct isl_ast_expr_op_names
*names
= user
;
1700 for (i
= 0; i
<= isl_ast_expr_op_last
; ++i
)
1701 free(names
->op_str
[i
]);
1705 /* Create an identifier that is used to store
1706 * an isl_ast_expr_op_names note.
1708 static __isl_give isl_id
*names_id(isl_ctx
*ctx
)
1710 return isl_id_alloc(ctx
, "isl_ast_expr_op_type_names", NULL
);
1713 /* Ensure that "p" has a note identified by "id".
1714 * If there is no such note yet, then it is created by "note_create" and
1715 * scheduled do be freed by "note_free".
1717 static __isl_give isl_printer
*alloc_note(__isl_take isl_printer
*p
,
1718 __isl_keep isl_id
*id
, void *(*note_create
)(isl_ctx
*),
1719 void (*note_free
)(void *))
1726 has_note
= isl_printer_has_note(p
, id
);
1728 return isl_printer_free(p
);
1732 ctx
= isl_printer_get_ctx(p
);
1733 note
= note_create(ctx
);
1735 return isl_printer_free(p
);
1736 note_id
= isl_id_alloc(ctx
, NULL
, note
);
1740 note_id
= isl_id_set_free_user(note_id
, note_free
);
1742 p
= isl_printer_set_note(p
, isl_id_copy(id
), note_id
);
1747 /* Ensure that "p" has an isl_ast_expr_op_names note identified by "id".
1749 static __isl_give isl_printer
*alloc_names(__isl_take isl_printer
*p
,
1750 __isl_keep isl_id
*id
)
1752 return alloc_note(p
, id
, &create_names
, &free_names
);
1755 /* Retrieve the note identified by "id" from "p".
1756 * The note is assumed to exist.
1758 static void *get_note(__isl_keep isl_printer
*p
, __isl_keep isl_id
*id
)
1762 id
= isl_printer_get_note(p
, isl_id_copy(id
));
1763 note
= isl_id_get_user(id
);
1769 /* Use "name" to print operations of type "type" to "p".
1771 * Store the name in an isl_ast_expr_op_names note attached to "p", such that
1772 * it can be retrieved by get_op_str.
1774 __isl_give isl_printer
*isl_ast_expr_op_type_set_print_name(
1775 __isl_take isl_printer
*p
, enum isl_ast_expr_op_type type
,
1776 __isl_keep
const char *name
)
1779 struct isl_ast_expr_op_names
*names
;
1783 if (type
> isl_ast_expr_op_last
)
1784 isl_die(isl_printer_get_ctx(p
), isl_error_invalid
,
1785 "invalid type", return isl_printer_free(p
));
1787 id
= names_id(isl_printer_get_ctx(p
));
1788 p
= alloc_names(p
, id
);
1789 names
= get_note(p
, id
);
1792 return isl_printer_free(p
);
1793 free(names
->op_str
[type
]);
1794 names
->op_str
[type
] = strdup(name
);
1799 /* This is an alternative name for the function above.
1801 __isl_give isl_printer
*isl_ast_op_type_set_print_name(
1802 __isl_take isl_printer
*p
, enum isl_ast_expr_op_type type
,
1803 __isl_keep
const char *name
)
1805 return isl_ast_expr_op_type_set_print_name(p
, type
, name
);
1808 /* Return the textual representation of "type" in C format.
1810 * If there is a user-specified name in an isl_ast_expr_op_names note
1811 * associated to "p", then return that.
1812 * Otherwise, return the default name in op_str_c.
1814 static const char *get_op_str_c(__isl_keep isl_printer
*p
,
1815 enum isl_ast_expr_op_type type
)
1819 struct isl_ast_expr_op_names
*names
= NULL
;
1821 id
= names_id(isl_printer_get_ctx(p
));
1822 has_names
= isl_printer_has_note(p
, id
);
1823 if (has_names
>= 0 && has_names
)
1824 names
= get_note(p
, id
);
1826 if (names
&& names
->op_str
[type
])
1827 return names
->op_str
[type
];
1828 return op_str_c
[type
];
1831 /* Print a min or max reduction "expr" in C format.
1833 static __isl_give isl_printer
*print_min_max_c(__isl_take isl_printer
*p
,
1834 __isl_keep isl_ast_expr
*expr
)
1838 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1839 p
= isl_printer_print_str(p
, get_op_str_c(p
, expr
->u
.op
.op
));
1840 p
= isl_printer_print_str(p
, "(");
1842 p
= isl_printer_print_ast_expr(p
, expr
->u
.op
.args
[0]);
1843 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1844 p
= isl_printer_print_str(p
, ", ");
1845 p
= print_ast_expr_c(p
, expr
->u
.op
.args
[i
]);
1846 p
= isl_printer_print_str(p
, ")");
1852 /* Print a function call "expr" in C format.
1854 * The first argument represents the function to be called.
1856 static __isl_give isl_printer
*print_call_c(__isl_take isl_printer
*p
,
1857 __isl_keep isl_ast_expr
*expr
)
1861 p
= print_ast_expr_c(p
, expr
->u
.op
.args
[0]);
1862 p
= isl_printer_print_str(p
, "(");
1863 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1865 p
= isl_printer_print_str(p
, ", ");
1866 p
= print_ast_expr_c(p
, expr
->u
.op
.args
[i
]);
1868 p
= isl_printer_print_str(p
, ")");
1873 /* Print an array access "expr" in C format.
1875 * The first argument represents the array being accessed.
1877 static __isl_give isl_printer
*print_access_c(__isl_take isl_printer
*p
,
1878 __isl_keep isl_ast_expr
*expr
)
1882 p
= print_ast_expr_c(p
, expr
->u
.op
.args
[0]);
1883 for (i
= 1; i
< expr
->u
.op
.n_arg
; ++i
) {
1884 p
= isl_printer_print_str(p
, "[");
1885 p
= print_ast_expr_c(p
, expr
->u
.op
.args
[i
]);
1886 p
= isl_printer_print_str(p
, "]");
1892 /* Print "expr" to "p" in C format.
1894 static __isl_give isl_printer
*print_ast_expr_c(__isl_take isl_printer
*p
,
1895 __isl_keep isl_ast_expr
*expr
)
1900 return isl_printer_free(p
);
1902 switch (expr
->type
) {
1903 case isl_ast_expr_op
:
1904 if (expr
->u
.op
.op
== isl_ast_expr_op_call
) {
1905 p
= print_call_c(p
, expr
);
1908 if (expr
->u
.op
.op
== isl_ast_expr_op_access
) {
1909 p
= print_access_c(p
, expr
);
1912 if (expr
->u
.op
.n_arg
== 1) {
1913 p
= isl_printer_print_str(p
,
1914 get_op_str_c(p
, expr
->u
.op
.op
));
1915 p
= print_sub_expr_c(p
, expr
->u
.op
.op
,
1916 expr
->u
.op
.args
[0], 0);
1919 if (expr
->u
.op
.op
== isl_ast_expr_op_fdiv_q
) {
1922 name
= get_op_str_c(p
, isl_ast_expr_op_fdiv_q
);
1923 p
= isl_printer_print_str(p
, name
);
1924 p
= isl_printer_print_str(p
, "(");
1925 p
= print_ast_expr_c(p
, expr
->u
.op
.args
[0]);
1926 p
= isl_printer_print_str(p
, ", ");
1927 p
= print_ast_expr_c(p
, expr
->u
.op
.args
[1]);
1928 p
= isl_printer_print_str(p
, ")");
1931 if (expr
->u
.op
.op
== isl_ast_expr_op_max
||
1932 expr
->u
.op
.op
== isl_ast_expr_op_min
) {
1933 p
= print_min_max_c(p
, expr
);
1936 if (expr
->u
.op
.op
== isl_ast_expr_op_cond
||
1937 expr
->u
.op
.op
== isl_ast_expr_op_select
) {
1938 p
= print_ast_expr_c(p
, expr
->u
.op
.args
[0]);
1939 p
= isl_printer_print_str(p
, " ? ");
1940 p
= print_ast_expr_c(p
, expr
->u
.op
.args
[1]);
1941 p
= isl_printer_print_str(p
, " : ");
1942 p
= print_ast_expr_c(p
, expr
->u
.op
.args
[2]);
1945 if (expr
->u
.op
.n_arg
!= 2)
1946 isl_die(isl_printer_get_ctx(p
), isl_error_internal
,
1947 "operation should have two arguments",
1948 return isl_printer_free(p
));
1949 p
= print_sub_expr_c(p
, expr
->u
.op
.op
, expr
->u
.op
.args
[0], 1);
1950 if (expr
->u
.op
.op
!= isl_ast_expr_op_member
)
1951 p
= isl_printer_print_str(p
, " ");
1952 p
= isl_printer_print_str(p
, get_op_str_c(p
, expr
->u
.op
.op
));
1953 if (expr
->u
.op
.op
!= isl_ast_expr_op_member
)
1954 p
= isl_printer_print_str(p
, " ");
1955 p
= print_sub_expr_c(p
, expr
->u
.op
.op
, expr
->u
.op
.args
[1], 0);
1957 case isl_ast_expr_id
:
1958 p
= isl_printer_print_str(p
, isl_id_get_name(expr
->u
.id
));
1960 case isl_ast_expr_int
:
1961 p
= isl_printer_print_val(p
, expr
->u
.v
);
1963 case isl_ast_expr_error
:
1970 /* Textual representation of the isl_ast_expr_op_type elements
1971 * for use in a YAML representation of an isl_ast_expr.
1973 static char *op_str
[] = {
1974 [isl_ast_expr_op_and
] = "and",
1975 [isl_ast_expr_op_and_then
] = "and_then",
1976 [isl_ast_expr_op_or
] = "or",
1977 [isl_ast_expr_op_or_else
] = "or_else",
1978 [isl_ast_expr_op_max
] = "max",
1979 [isl_ast_expr_op_min
] = "min",
1980 [isl_ast_expr_op_minus
] = "minus",
1981 [isl_ast_expr_op_add
] = "add",
1982 [isl_ast_expr_op_sub
] = "sub",
1983 [isl_ast_expr_op_mul
] = "mul",
1984 [isl_ast_expr_op_div
] = "div",
1985 [isl_ast_expr_op_fdiv_q
] = "fdiv_q",
1986 [isl_ast_expr_op_pdiv_q
] = "pdiv_q",
1987 [isl_ast_expr_op_pdiv_r
] = "pdiv_r",
1988 [isl_ast_expr_op_zdiv_r
] = "zdiv_r",
1989 [isl_ast_expr_op_cond
] = "cond",
1990 [isl_ast_expr_op_select
] = "select",
1991 [isl_ast_expr_op_eq
] = "eq",
1992 [isl_ast_expr_op_le
] = "le",
1993 [isl_ast_expr_op_lt
] = "lt",
1994 [isl_ast_expr_op_ge
] = "ge",
1995 [isl_ast_expr_op_gt
] = "gt",
1996 [isl_ast_expr_op_call
] = "call",
1997 [isl_ast_expr_op_access
] = "access",
1998 [isl_ast_expr_op_member
] = "member",
1999 [isl_ast_expr_op_address_of
] = "address_of"
2002 static __isl_give isl_printer
*print_ast_expr_isl(__isl_take isl_printer
*p
,
2003 __isl_keep isl_ast_expr
*expr
);
2005 /* Print the arguments of "expr" to "p" in isl format.
2007 * If there are no arguments, then nothing needs to be printed.
2008 * Otherwise add an "args" key to the current mapping with as value
2009 * the list of arguments of "expr".
2011 static __isl_give isl_printer
*print_arguments(__isl_take isl_printer
*p
,
2012 __isl_keep isl_ast_expr
*expr
)
2017 n
= isl_ast_expr_get_op_n_arg(expr
);
2019 return isl_printer_free(p
);
2023 p
= isl_printer_print_str(p
, "args");
2024 p
= isl_printer_yaml_next(p
);
2025 p
= isl_printer_yaml_start_sequence(p
);
2026 for (i
= 0; i
< n
; ++i
) {
2029 arg
= isl_ast_expr_get_op_arg(expr
, i
);
2030 p
= print_ast_expr_isl(p
, arg
);
2031 isl_ast_expr_free(arg
);
2032 p
= isl_printer_yaml_next(p
);
2034 p
= isl_printer_yaml_end_sequence(p
);
2039 /* Print "expr" to "p" in isl format.
2041 * In particular, print the isl_ast_expr as a YAML document.
2043 static __isl_give isl_printer
*print_ast_expr_isl(__isl_take isl_printer
*p
,
2044 __isl_keep isl_ast_expr
*expr
)
2046 enum isl_ast_expr_type type
;
2047 enum isl_ast_expr_op_type op
;
2052 return isl_printer_free(p
);
2054 p
= isl_printer_yaml_start_mapping(p
);
2055 type
= isl_ast_expr_get_type(expr
);
2057 case isl_ast_expr_error
:
2058 return isl_printer_free(p
);
2059 case isl_ast_expr_op
:
2060 op
= isl_ast_expr_get_op_type(expr
);
2061 if (op
== isl_ast_expr_op_error
)
2062 return isl_printer_free(p
);
2063 p
= isl_printer_print_str(p
, "op");
2064 p
= isl_printer_yaml_next(p
);
2065 p
= isl_printer_print_str(p
, op_str
[op
]);
2066 p
= isl_printer_yaml_next(p
);
2067 p
= print_arguments(p
, expr
);
2069 case isl_ast_expr_id
:
2070 p
= isl_printer_print_str(p
, "id");
2071 p
= isl_printer_yaml_next(p
);
2072 id
= isl_ast_expr_get_id(expr
);
2073 p
= isl_printer_print_id(p
, id
);
2076 case isl_ast_expr_int
:
2077 p
= isl_printer_print_str(p
, "val");
2078 p
= isl_printer_yaml_next(p
);
2079 v
= isl_ast_expr_get_val(expr
);
2080 p
= isl_printer_print_val(p
, v
);
2084 p
= isl_printer_yaml_end_mapping(p
);
2089 /* Print "expr" to "p".
2091 * Only an isl and a C format are supported.
2093 __isl_give isl_printer
*isl_printer_print_ast_expr(__isl_take isl_printer
*p
,
2094 __isl_keep isl_ast_expr
*expr
)
2101 format
= isl_printer_get_output_format(p
);
2103 case ISL_FORMAT_ISL
:
2104 p
= print_ast_expr_isl(p
, expr
);
2107 p
= print_ast_expr_c(p
, expr
);
2110 isl_die(isl_printer_get_ctx(p
), isl_error_unsupported
,
2111 "output format not supported for ast_expr",
2112 return isl_printer_free(p
));
2118 static __isl_give isl_printer
*print_ast_node_isl(__isl_take isl_printer
*p
,
2119 __isl_keep isl_ast_node
*node
);
2121 /* Print a YAML sequence containing the entries in "list" to "p".
2123 static __isl_give isl_printer
*print_ast_node_list(__isl_take isl_printer
*p
,
2124 __isl_keep isl_ast_node_list
*list
)
2129 n
= isl_ast_node_list_n_ast_node(list
);
2131 return isl_printer_free(p
);
2133 p
= isl_printer_yaml_start_sequence(p
);
2134 for (i
= 0; i
< n
; ++i
) {
2137 node
= isl_ast_node_list_get_ast_node(list
, i
);
2138 p
= print_ast_node_isl(p
, node
);
2139 isl_ast_node_free(node
);
2140 p
= isl_printer_yaml_next(p
);
2142 p
= isl_printer_yaml_end_sequence(p
);
2147 /* Print "node" to "p" in "isl format".
2149 * In particular, print the isl_ast_node as a YAML document.
2151 static __isl_give isl_printer
*print_ast_node_isl(__isl_take isl_printer
*p
,
2152 __isl_keep isl_ast_node
*node
)
2154 switch (node
->type
) {
2155 case isl_ast_node_for
:
2156 p
= isl_printer_yaml_start_mapping(p
);
2157 p
= isl_printer_print_str(p
, "iterator");
2158 p
= isl_printer_yaml_next(p
);
2159 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.iterator
);
2160 p
= isl_printer_yaml_next(p
);
2161 if (node
->u
.f
.degenerate
) {
2162 p
= isl_printer_print_str(p
, "value");
2163 p
= isl_printer_yaml_next(p
);
2164 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
2165 p
= isl_printer_yaml_next(p
);
2167 p
= isl_printer_print_str(p
, "init");
2168 p
= isl_printer_yaml_next(p
);
2169 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
2170 p
= isl_printer_yaml_next(p
);
2171 p
= isl_printer_print_str(p
, "cond");
2172 p
= isl_printer_yaml_next(p
);
2173 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.cond
);
2174 p
= isl_printer_yaml_next(p
);
2175 p
= isl_printer_print_str(p
, "inc");
2176 p
= isl_printer_yaml_next(p
);
2177 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.inc
);
2178 p
= isl_printer_yaml_next(p
);
2180 if (node
->u
.f
.body
) {
2181 p
= isl_printer_print_str(p
, "body");
2182 p
= isl_printer_yaml_next(p
);
2183 p
= isl_printer_print_ast_node(p
, node
->u
.f
.body
);
2184 p
= isl_printer_yaml_next(p
);
2186 p
= isl_printer_yaml_end_mapping(p
);
2188 case isl_ast_node_mark
:
2189 p
= isl_printer_yaml_start_mapping(p
);
2190 p
= isl_printer_print_str(p
, "mark");
2191 p
= isl_printer_yaml_next(p
);
2192 p
= isl_printer_print_id(p
, node
->u
.m
.mark
);
2193 p
= isl_printer_yaml_next(p
);
2194 p
= isl_printer_print_str(p
, "node");
2195 p
= isl_printer_yaml_next(p
);
2196 p
= isl_printer_print_ast_node(p
, node
->u
.m
.node
);
2197 p
= isl_printer_yaml_end_mapping(p
);
2199 case isl_ast_node_user
:
2200 p
= isl_printer_yaml_start_mapping(p
);
2201 p
= isl_printer_print_str(p
, "user");
2202 p
= isl_printer_yaml_next(p
);
2203 p
= isl_printer_print_ast_expr(p
, node
->u
.e
.expr
);
2204 p
= isl_printer_yaml_end_mapping(p
);
2206 case isl_ast_node_if
:
2207 p
= isl_printer_yaml_start_mapping(p
);
2208 p
= isl_printer_print_str(p
, "guard");
2209 p
= isl_printer_yaml_next(p
);
2210 p
= isl_printer_print_ast_expr(p
, node
->u
.i
.guard
);
2211 p
= isl_printer_yaml_next(p
);
2212 if (node
->u
.i
.then
) {
2213 p
= isl_printer_print_str(p
, "then");
2214 p
= isl_printer_yaml_next(p
);
2215 p
= isl_printer_print_ast_node(p
, node
->u
.i
.then
);
2216 p
= isl_printer_yaml_next(p
);
2218 if (node
->u
.i
.else_node
) {
2219 p
= isl_printer_print_str(p
, "else");
2220 p
= isl_printer_yaml_next(p
);
2221 p
= isl_printer_print_ast_node(p
, node
->u
.i
.else_node
);
2223 p
= isl_printer_yaml_end_mapping(p
);
2225 case isl_ast_node_block
:
2226 p
= print_ast_node_list(p
, node
->u
.b
.children
);
2228 case isl_ast_node_error
:
2234 /* Do we need to print a block around the body "node" of a for or if node?
2236 * If the node is a block, then we need to print a block.
2237 * Also if the node is a degenerate for then we will print it as
2238 * an assignment followed by the body of the for loop, so we need a block
2240 * If the node is an if node with an else, then we print a block
2241 * to avoid spurious dangling else warnings emitted by some compilers.
2242 * If the node is a mark, then in principle, we would have to check
2243 * the child of the mark node. However, even if the child would not
2244 * require us to print a block, for readability it is probably best
2245 * to print a block anyway.
2246 * If the ast_always_print_block option has been set, then we print a block.
2248 static int need_block(__isl_keep isl_ast_node
*node
)
2252 if (node
->type
== isl_ast_node_block
)
2254 if (node
->type
== isl_ast_node_for
&& node
->u
.f
.degenerate
)
2256 if (node
->type
== isl_ast_node_if
&& node
->u
.i
.else_node
)
2258 if (node
->type
== isl_ast_node_mark
)
2261 ctx
= isl_ast_node_get_ctx(node
);
2262 return isl_options_get_ast_always_print_block(ctx
);
2265 static __isl_give isl_printer
*print_ast_node_c(__isl_take isl_printer
*p
,
2266 __isl_keep isl_ast_node
*node
,
2267 __isl_keep isl_ast_print_options
*options
, int in_block
, int in_list
);
2268 static __isl_give isl_printer
*print_if_c(__isl_take isl_printer
*p
,
2269 __isl_keep isl_ast_node
*node
,
2270 __isl_keep isl_ast_print_options
*options
, int new_line
,
2273 /* Print the body "node" of a for or if node.
2274 * If "else_node" is set, then it is printed as well.
2275 * If "force_block" is set, then print out the body as a block.
2277 * We first check if we need to print out a block.
2278 * We always print out a block if there is an else node to make
2279 * sure that the else node is matched to the correct if node.
2280 * For consistency, the corresponding else node is also printed as a block.
2282 * If the else node is itself an if, then we print it as
2287 * Otherwise the else node is printed as
2293 static __isl_give isl_printer
*print_body_c(__isl_take isl_printer
*p
,
2294 __isl_keep isl_ast_node
*node
, __isl_keep isl_ast_node
*else_node
,
2295 __isl_keep isl_ast_print_options
*options
, int force_block
)
2298 return isl_printer_free(p
);
2300 if (!force_block
&& !else_node
&& !need_block(node
)) {
2301 p
= isl_printer_end_line(p
);
2302 p
= isl_printer_indent(p
, 2);
2303 p
= isl_ast_node_print(node
, p
,
2304 isl_ast_print_options_copy(options
));
2305 p
= isl_printer_indent(p
, -2);
2309 p
= isl_printer_print_str(p
, " {");
2310 p
= isl_printer_end_line(p
);
2311 p
= isl_printer_indent(p
, 2);
2312 p
= print_ast_node_c(p
, node
, options
, 1, 0);
2313 p
= isl_printer_indent(p
, -2);
2314 p
= isl_printer_start_line(p
);
2315 p
= isl_printer_print_str(p
, "}");
2317 if (else_node
->type
== isl_ast_node_if
) {
2318 p
= isl_printer_print_str(p
, " else ");
2319 p
= print_if_c(p
, else_node
, options
, 0, 1);
2321 p
= isl_printer_print_str(p
, " else");
2322 p
= print_body_c(p
, else_node
, NULL
, options
, 1);
2325 p
= isl_printer_end_line(p
);
2330 /* Print the start of a compound statement.
2332 static __isl_give isl_printer
*start_block(__isl_take isl_printer
*p
)
2334 p
= isl_printer_start_line(p
);
2335 p
= isl_printer_print_str(p
, "{");
2336 p
= isl_printer_end_line(p
);
2337 p
= isl_printer_indent(p
, 2);
2342 /* Print the end of a compound statement.
2344 static __isl_give isl_printer
*end_block(__isl_take isl_printer
*p
)
2346 p
= isl_printer_indent(p
, -2);
2347 p
= isl_printer_start_line(p
);
2348 p
= isl_printer_print_str(p
, "}");
2349 p
= isl_printer_end_line(p
);
2354 /* Print the for node "node".
2356 * If the for node is degenerate, it is printed as
2358 * type iterator = init;
2361 * Otherwise, it is printed as
2363 * for (type iterator = init; cond; iterator += inc)
2366 * "in_block" is set if we are currently inside a block.
2367 * "in_list" is set if the current node is not alone in the block.
2368 * If we are not in a block or if the current not is not alone in the block
2369 * then we print a block around a degenerate for loop such that the variable
2370 * declaration will not conflict with any potential other declaration
2371 * of the same variable.
2373 static __isl_give isl_printer
*print_for_c(__isl_take isl_printer
*p
,
2374 __isl_keep isl_ast_node
*node
,
2375 __isl_keep isl_ast_print_options
*options
, int in_block
, int in_list
)
2381 type
= isl_options_get_ast_iterator_type(isl_printer_get_ctx(p
));
2382 if (!node
->u
.f
.degenerate
) {
2383 id
= isl_ast_expr_get_id(node
->u
.f
.iterator
);
2384 name
= isl_id_get_name(id
);
2386 p
= isl_printer_start_line(p
);
2387 p
= isl_printer_print_str(p
, "for (");
2388 p
= isl_printer_print_str(p
, type
);
2389 p
= isl_printer_print_str(p
, " ");
2390 p
= isl_printer_print_str(p
, name
);
2391 p
= isl_printer_print_str(p
, " = ");
2392 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
2393 p
= isl_printer_print_str(p
, "; ");
2394 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.cond
);
2395 p
= isl_printer_print_str(p
, "; ");
2396 p
= isl_printer_print_str(p
, name
);
2397 p
= isl_printer_print_str(p
, " += ");
2398 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.inc
);
2399 p
= isl_printer_print_str(p
, ")");
2400 p
= print_body_c(p
, node
->u
.f
.body
, NULL
, options
, 0);
2402 id
= isl_ast_expr_get_id(node
->u
.f
.iterator
);
2403 name
= isl_id_get_name(id
);
2405 if (!in_block
|| in_list
)
2407 p
= isl_printer_start_line(p
);
2408 p
= isl_printer_print_str(p
, type
);
2409 p
= isl_printer_print_str(p
, " ");
2410 p
= isl_printer_print_str(p
, name
);
2411 p
= isl_printer_print_str(p
, " = ");
2412 p
= isl_printer_print_ast_expr(p
, node
->u
.f
.init
);
2413 p
= isl_printer_print_str(p
, ";");
2414 p
= isl_printer_end_line(p
);
2415 p
= print_ast_node_c(p
, node
->u
.f
.body
, options
, 1, 0);
2416 if (!in_block
|| in_list
)
2423 /* Print the if node "node".
2424 * If "new_line" is set then the if node should be printed on a new line.
2425 * If "force_block" is set, then print out the body as a block.
2427 static __isl_give isl_printer
*print_if_c(__isl_take isl_printer
*p
,
2428 __isl_keep isl_ast_node
*node
,
2429 __isl_keep isl_ast_print_options
*options
, int new_line
,
2433 p
= isl_printer_start_line(p
);
2434 p
= isl_printer_print_str(p
, "if (");
2435 p
= isl_printer_print_ast_expr(p
, node
->u
.i
.guard
);
2436 p
= isl_printer_print_str(p
, ")");
2437 p
= print_body_c(p
, node
->u
.i
.then
, node
->u
.i
.else_node
, options
,
2443 /* Print the "node" to "p".
2445 * "in_block" is set if we are currently inside a block.
2446 * If so, we do not print a block around the children of a block node.
2447 * We do this to avoid an extra block around the body of a degenerate
2450 * "in_list" is set if the current node is not alone in the block.
2452 static __isl_give isl_printer
*print_ast_node_c(__isl_take isl_printer
*p
,
2453 __isl_keep isl_ast_node
*node
,
2454 __isl_keep isl_ast_print_options
*options
, int in_block
, int in_list
)
2456 switch (node
->type
) {
2457 case isl_ast_node_for
:
2458 if (options
->print_for
)
2459 return options
->print_for(p
,
2460 isl_ast_print_options_copy(options
),
2461 node
, options
->print_for_user
);
2462 p
= print_for_c(p
, node
, options
, in_block
, in_list
);
2464 case isl_ast_node_if
:
2465 p
= print_if_c(p
, node
, options
, 1, 0);
2467 case isl_ast_node_block
:
2470 p
= isl_ast_node_list_print(node
->u
.b
.children
, p
, options
);
2474 case isl_ast_node_mark
:
2475 p
= isl_printer_start_line(p
);
2476 p
= isl_printer_print_str(p
, "// ");
2477 p
= isl_printer_print_str(p
, isl_id_get_name(node
->u
.m
.mark
));
2478 p
= isl_printer_end_line(p
);
2479 p
= print_ast_node_c(p
, node
->u
.m
.node
, options
, 0, in_list
);
2481 case isl_ast_node_user
:
2482 if (options
->print_user
)
2483 return options
->print_user(p
,
2484 isl_ast_print_options_copy(options
),
2485 node
, options
->print_user_user
);
2486 p
= isl_printer_start_line(p
);
2487 p
= isl_printer_print_ast_expr(p
, node
->u
.e
.expr
);
2488 p
= isl_printer_print_str(p
, ";");
2489 p
= isl_printer_end_line(p
);
2491 case isl_ast_node_error
:
2497 /* Print the for node "node" to "p".
2499 __isl_give isl_printer
*isl_ast_node_for_print(__isl_keep isl_ast_node
*node
,
2500 __isl_take isl_printer
*p
, __isl_take isl_ast_print_options
*options
)
2502 if (!node
|| !options
)
2504 if (node
->type
!= isl_ast_node_for
)
2505 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
2506 "not a for node", goto error
);
2507 p
= print_for_c(p
, node
, options
, 0, 0);
2508 isl_ast_print_options_free(options
);
2511 isl_ast_print_options_free(options
);
2512 isl_printer_free(p
);
2516 /* Print the if node "node" to "p".
2518 __isl_give isl_printer
*isl_ast_node_if_print(__isl_keep isl_ast_node
*node
,
2519 __isl_take isl_printer
*p
, __isl_take isl_ast_print_options
*options
)
2521 if (!node
|| !options
)
2523 if (node
->type
!= isl_ast_node_if
)
2524 isl_die(isl_ast_node_get_ctx(node
), isl_error_invalid
,
2525 "not an if node", goto error
);
2526 p
= print_if_c(p
, node
, options
, 1, 0);
2527 isl_ast_print_options_free(options
);
2530 isl_ast_print_options_free(options
);
2531 isl_printer_free(p
);
2535 /* Print "node" to "p".
2537 * "node" is assumed to be either the outermost node in an AST or
2538 * a node that is known not to be a block.
2539 * If "node" is a block (and is therefore outermost) and
2540 * if the ast_print_outermost_block options is not set,
2541 * then act as if the printing occurs inside a block, such
2542 * that no "extra" block will get printed.
2544 __isl_give isl_printer
*isl_ast_node_print(__isl_keep isl_ast_node
*node
,
2545 __isl_take isl_printer
*p
, __isl_take isl_ast_print_options
*options
)
2549 if (!options
|| !node
)
2551 if (node
->type
== isl_ast_node_block
) {
2554 ctx
= isl_ast_node_get_ctx(node
);
2555 in_block
= !isl_options_get_ast_print_outermost_block(ctx
);
2557 p
= print_ast_node_c(p
, node
, options
, in_block
, 0);
2558 isl_ast_print_options_free(options
);
2561 isl_ast_print_options_free(options
);
2562 isl_printer_free(p
);
2566 /* Print "node" to "p".
2568 __isl_give isl_printer
*isl_printer_print_ast_node(__isl_take isl_printer
*p
,
2569 __isl_keep isl_ast_node
*node
)
2572 isl_ast_print_options
*options
;
2577 format
= isl_printer_get_output_format(p
);
2579 case ISL_FORMAT_ISL
:
2580 p
= print_ast_node_isl(p
, node
);
2583 options
= isl_ast_print_options_alloc(isl_printer_get_ctx(p
));
2584 p
= isl_ast_node_print(node
, p
, options
);
2587 isl_die(isl_printer_get_ctx(p
), isl_error_unsupported
,
2588 "output format not supported for ast_node",
2589 return isl_printer_free(p
));
2595 /* Print the list of nodes "list" to "p".
2597 __isl_give isl_printer
*isl_ast_node_list_print(
2598 __isl_keep isl_ast_node_list
*list
, __isl_take isl_printer
*p
,
2599 __isl_keep isl_ast_print_options
*options
)
2603 if (!p
|| !list
|| !options
)
2604 return isl_printer_free(p
);
2606 for (i
= 0; i
< list
->n
; ++i
)
2607 p
= print_ast_node_c(p
, list
->p
[i
], options
, 1, 1);
2612 #define ISL_AST_MACRO_FDIV_Q (1 << 0)
2613 #define ISL_AST_MACRO_MIN (1 << 1)
2614 #define ISL_AST_MACRO_MAX (1 << 2)
2615 #define ISL_AST_MACRO_ALL (ISL_AST_MACRO_FDIV_Q | \
2616 ISL_AST_MACRO_MIN | \
2619 /* If "expr" contains an isl_ast_expr_op_min, isl_ast_expr_op_max or
2620 * isl_ast_expr_op_fdiv_q then set the corresponding bit in "macros".
2622 static int ast_expr_required_macros(__isl_keep isl_ast_expr
*expr
, int macros
)
2626 if (macros
== ISL_AST_MACRO_ALL
)
2629 if (expr
->type
!= isl_ast_expr_op
)
2632 if (expr
->u
.op
.op
== isl_ast_expr_op_min
)
2633 macros
|= ISL_AST_MACRO_MIN
;
2634 if (expr
->u
.op
.op
== isl_ast_expr_op_max
)
2635 macros
|= ISL_AST_MACRO_MAX
;
2636 if (expr
->u
.op
.op
== isl_ast_expr_op_fdiv_q
)
2637 macros
|= ISL_AST_MACRO_FDIV_Q
;
2639 for (i
= 0; i
< expr
->u
.op
.n_arg
; ++i
)
2640 macros
= ast_expr_required_macros(expr
->u
.op
.args
[i
], macros
);
2645 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list
*list
,
2648 /* If "node" contains an isl_ast_expr_op_min, isl_ast_expr_op_max or
2649 * isl_ast_expr_op_fdiv_q then set the corresponding bit in "macros".
2651 static int ast_node_required_macros(__isl_keep isl_ast_node
*node
, int macros
)
2653 if (macros
== ISL_AST_MACRO_ALL
)
2656 switch (node
->type
) {
2657 case isl_ast_node_for
:
2658 macros
= ast_expr_required_macros(node
->u
.f
.init
, macros
);
2659 if (!node
->u
.f
.degenerate
) {
2660 macros
= ast_expr_required_macros(node
->u
.f
.cond
,
2662 macros
= ast_expr_required_macros(node
->u
.f
.inc
,
2665 macros
= ast_node_required_macros(node
->u
.f
.body
, macros
);
2667 case isl_ast_node_if
:
2668 macros
= ast_expr_required_macros(node
->u
.i
.guard
, macros
);
2669 macros
= ast_node_required_macros(node
->u
.i
.then
, macros
);
2670 if (node
->u
.i
.else_node
)
2671 macros
= ast_node_required_macros(node
->u
.i
.else_node
,
2674 case isl_ast_node_block
:
2675 macros
= ast_node_list_required_macros(node
->u
.b
.children
,
2678 case isl_ast_node_mark
:
2679 macros
= ast_node_required_macros(node
->u
.m
.node
, macros
);
2681 case isl_ast_node_user
:
2682 macros
= ast_expr_required_macros(node
->u
.e
.expr
, macros
);
2684 case isl_ast_node_error
:
2691 /* If "list" contains an isl_ast_expr_op_min, isl_ast_expr_op_max or
2692 * isl_ast_expr_op_fdiv_q then set the corresponding bit in "macros".
2694 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list
*list
,
2699 for (i
= 0; i
< list
->n
; ++i
)
2700 macros
= ast_node_required_macros(list
->p
[i
], macros
);
2705 /* Data structure for keeping track of whether a macro definition
2706 * for a given type has already been printed.
2707 * The value is zero if no definition has been printed and non-zero otherwise.
2709 struct isl_ast_expr_op_printed
{
2710 char printed
[isl_ast_expr_op_last
+ 1];
2713 /* Create an empty struct isl_ast_expr_op_printed.
2715 static void *create_printed(isl_ctx
*ctx
)
2717 return isl_calloc_type(ctx
, struct isl_ast_expr_op_printed
);
2720 /* Free a struct isl_ast_expr_op_printed.
2722 static void free_printed(void *user
)
2727 /* Ensure that "p" has an isl_ast_expr_op_printed note identified by "id".
2729 static __isl_give isl_printer
*alloc_printed(__isl_take isl_printer
*p
,
2730 __isl_keep isl_id
*id
)
2732 return alloc_note(p
, id
, &create_printed
, &free_printed
);
2735 /* Create an identifier that is used to store
2736 * an isl_ast_expr_op_printed note.
2738 static __isl_give isl_id
*printed_id(isl_ctx
*ctx
)
2740 return isl_id_alloc(ctx
, "isl_ast_expr_op_type_printed", NULL
);
2743 /* Did the user specify that a macro definition should only be
2744 * printed once and has a macro definition for "type" already
2745 * been printed to "p"?
2746 * If definitions should only be printed once, but a definition
2747 * for "p" has not yet been printed, then mark it as having been
2748 * printed so that it will not printed again.
2749 * The actual printing is taken care of by the caller.
2751 static isl_bool
already_printed_once(__isl_keep isl_printer
*p
,
2752 enum isl_ast_expr_op_type type
)
2756 struct isl_ast_expr_op_printed
*printed
;
2759 return isl_bool_error
;
2761 ctx
= isl_printer_get_ctx(p
);
2762 if (!isl_options_get_ast_print_macro_once(ctx
))
2763 return isl_bool_false
;
2765 if (type
> isl_ast_expr_op_last
)
2766 isl_die(isl_printer_get_ctx(p
), isl_error_invalid
,
2767 "invalid type", return isl_bool_error
);
2769 id
= printed_id(isl_printer_get_ctx(p
));
2770 p
= alloc_printed(p
, id
);
2771 printed
= get_note(p
, id
);
2774 return isl_bool_error
;
2776 if (printed
->printed
[type
])
2777 return isl_bool_true
;
2779 printed
->printed
[type
] = 1;
2780 return isl_bool_false
;
2783 /* Print a macro definition for the operator "type".
2785 * If the user has specified that a macro definition should
2786 * only be printed once to any given printer and if the macro definition
2787 * has already been printed to "p", then do not print the definition.
2789 __isl_give isl_printer
*isl_ast_expr_op_type_print_macro(
2790 enum isl_ast_expr_op_type type
, __isl_take isl_printer
*p
)
2794 skip
= already_printed_once(p
, type
);
2796 return isl_printer_free(p
);
2801 case isl_ast_expr_op_min
:
2802 p
= isl_printer_start_line(p
);
2803 p
= isl_printer_print_str(p
, "#define ");
2804 p
= isl_printer_print_str(p
, get_op_str_c(p
, type
));
2805 p
= isl_printer_print_str(p
,
2806 "(x,y) ((x) < (y) ? (x) : (y))");
2807 p
= isl_printer_end_line(p
);
2809 case isl_ast_expr_op_max
:
2810 p
= isl_printer_start_line(p
);
2811 p
= isl_printer_print_str(p
, "#define ");
2812 p
= isl_printer_print_str(p
, get_op_str_c(p
, type
));
2813 p
= isl_printer_print_str(p
,
2814 "(x,y) ((x) > (y) ? (x) : (y))");
2815 p
= isl_printer_end_line(p
);
2817 case isl_ast_expr_op_fdiv_q
:
2818 p
= isl_printer_start_line(p
);
2819 p
= isl_printer_print_str(p
, "#define ");
2820 p
= isl_printer_print_str(p
, get_op_str_c(p
, type
));
2821 p
= isl_printer_print_str(p
,
2823 "(((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))");
2824 p
= isl_printer_end_line(p
);
2833 /* This is an alternative name for the function above.
2835 __isl_give isl_printer
*isl_ast_op_type_print_macro(
2836 enum isl_ast_expr_op_type type
, __isl_take isl_printer
*p
)
2838 return isl_ast_expr_op_type_print_macro(type
, p
);
2841 /* Call "fn" for each type of operation represented in the "macros"
2844 static isl_stat
foreach_ast_expr_op_type(int macros
,
2845 isl_stat (*fn
)(enum isl_ast_expr_op_type type
, void *user
), void *user
)
2847 if (macros
& ISL_AST_MACRO_MIN
&& fn(isl_ast_expr_op_min
, user
) < 0)
2848 return isl_stat_error
;
2849 if (macros
& ISL_AST_MACRO_MAX
&& fn(isl_ast_expr_op_max
, user
) < 0)
2850 return isl_stat_error
;
2851 if (macros
& ISL_AST_MACRO_FDIV_Q
&&
2852 fn(isl_ast_expr_op_fdiv_q
, user
) < 0)
2853 return isl_stat_error
;
2858 /* Call "fn" for each type of operation that appears in "expr"
2859 * and that requires a macro definition.
2861 isl_stat
isl_ast_expr_foreach_ast_expr_op_type(__isl_keep isl_ast_expr
*expr
,
2862 isl_stat (*fn
)(enum isl_ast_expr_op_type type
, void *user
), void *user
)
2867 return isl_stat_error
;
2869 macros
= ast_expr_required_macros(expr
, 0);
2870 return foreach_ast_expr_op_type(macros
, fn
, user
);
2873 /* This is an alternative name for the function above.
2875 isl_stat
isl_ast_expr_foreach_ast_op_type(__isl_keep isl_ast_expr
*expr
,
2876 isl_stat (*fn
)(enum isl_ast_expr_op_type type
, void *user
), void *user
)
2878 return isl_ast_expr_foreach_ast_expr_op_type(expr
, fn
, user
);
2881 /* Call "fn" for each type of operation that appears in "node"
2882 * and that requires a macro definition.
2884 isl_stat
isl_ast_node_foreach_ast_expr_op_type(__isl_keep isl_ast_node
*node
,
2885 isl_stat (*fn
)(enum isl_ast_expr_op_type type
, void *user
), void *user
)
2890 return isl_stat_error
;
2892 macros
= ast_node_required_macros(node
, 0);
2893 return foreach_ast_expr_op_type(macros
, fn
, user
);
2896 /* This is an alternative name for the function above.
2898 isl_stat
isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node
*node
,
2899 isl_stat (*fn
)(enum isl_ast_expr_op_type type
, void *user
), void *user
)
2901 return isl_ast_node_foreach_ast_expr_op_type(node
, fn
, user
);
2904 static isl_stat
ast_op_type_print_macro(enum isl_ast_expr_op_type type
,
2907 isl_printer
**p
= user
;
2909 *p
= isl_ast_expr_op_type_print_macro(type
, *p
);
2914 /* Print macro definitions for all the macros used in the result
2915 * of printing "expr".
2917 __isl_give isl_printer
*isl_ast_expr_print_macros(
2918 __isl_keep isl_ast_expr
*expr
, __isl_take isl_printer
*p
)
2920 if (isl_ast_expr_foreach_ast_expr_op_type(expr
,
2921 &ast_op_type_print_macro
, &p
) < 0)
2922 return isl_printer_free(p
);
2926 /* Print macro definitions for all the macros used in the result
2927 * of printing "node".
2929 __isl_give isl_printer
*isl_ast_node_print_macros(
2930 __isl_keep isl_ast_node
*node
, __isl_take isl_printer
*p
)
2932 if (isl_ast_node_foreach_ast_expr_op_type(node
,
2933 &ast_op_type_print_macro
, &p
) < 0)
2934 return isl_printer_free(p
);
2938 /* Return a string containing C code representing this isl_ast_expr.
2940 __isl_give
char *isl_ast_expr_to_C_str(__isl_keep isl_ast_expr
*expr
)
2948 p
= isl_printer_to_str(isl_ast_expr_get_ctx(expr
));
2949 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
2950 p
= isl_printer_print_ast_expr(p
, expr
);
2952 str
= isl_printer_get_str(p
);
2954 isl_printer_free(p
);
2959 /* Return a string containing C code representing this isl_ast_node.
2961 __isl_give
char *isl_ast_node_to_C_str(__isl_keep isl_ast_node
*node
)
2969 p
= isl_printer_to_str(isl_ast_node_get_ctx(node
));
2970 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
2971 p
= isl_printer_print_ast_node(p
, node
);
2973 str
= isl_printer_get_str(p
);
2975 isl_printer_free(p
);