isl_schedule_node_has_next_sibling: use isl_bool_ok
[isl.git] / isl_ast.c
blob15d37cba37838bbb2bd9d11836b24d38bf013717
1 /*
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
8 */
10 #include <string.h>
12 #include <isl/id.h>
13 #include <isl/val.h>
14 #include <isl_ast_private.h>
16 #undef BASE
17 #define BASE ast_expr
19 #include <isl_list_templ.c>
21 #undef BASE
22 #define 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);
37 if (!options)
38 return NULL;
40 options->ctx = ctx;
41 isl_ctx_ref(ctx);
42 options->ref = 1;
44 return options;
47 __isl_give isl_ast_print_options *isl_ast_print_options_dup(
48 __isl_keep isl_ast_print_options *options)
50 isl_ctx *ctx;
51 isl_ast_print_options *dup;
53 if (!options)
54 return NULL;
56 ctx = isl_ast_print_options_get_ctx(options);
57 dup = isl_ast_print_options_alloc(ctx);
58 if (!dup)
59 return NULL;
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;
66 return dup;
69 __isl_give isl_ast_print_options *isl_ast_print_options_cow(
70 __isl_take isl_ast_print_options *options)
72 if (!options)
73 return NULL;
75 if (options->ref == 1)
76 return options;
77 options->ref--;
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)
84 if (!options)
85 return NULL;
87 options->ref++;
88 return options;
91 __isl_null isl_ast_print_options *isl_ast_print_options_free(
92 __isl_take isl_ast_print_options *options)
94 if (!options)
95 return NULL;
97 if (--options->ref > 0)
98 return NULL;
100 isl_ctx_deref(options->ctx);
102 free(options);
103 return NULL;
106 /* Set the print_user callback of "options" to "print_user".
108 * If this callback is set, then it 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),
116 void *user)
118 options = isl_ast_print_options_cow(options);
119 if (!options)
120 return NULL;
122 options->print_user = print_user;
123 options->print_user_user = user;
125 return options;
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),
137 void *user)
139 options = isl_ast_print_options_cow(options);
140 if (!options)
141 return NULL;
143 options->print_for = print_for;
144 options->print_for_user = user;
146 return options;
149 __isl_give isl_ast_expr *isl_ast_expr_copy(__isl_keep isl_ast_expr *expr)
151 if (!expr)
152 return NULL;
154 expr->ref++;
155 return expr;
158 __isl_give isl_ast_expr *isl_ast_expr_dup(__isl_keep isl_ast_expr *expr)
160 int i;
161 isl_ctx *ctx;
162 isl_ast_expr *dup;
164 if (!expr)
165 return NULL;
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));
171 break;
172 case isl_ast_expr_id:
173 dup = isl_ast_expr_from_id(isl_id_copy(expr->u.id));
174 break;
175 case isl_ast_expr_op:
176 dup = isl_ast_expr_alloc_op(ctx,
177 expr->u.op.op, expr->u.op.n_arg);
178 if (!dup)
179 return NULL;
180 for (i = 0; i < expr->u.op.n_arg; ++i)
181 dup->u.op.args[i] =
182 isl_ast_expr_copy(expr->u.op.args[i]);
183 break;
184 case isl_ast_expr_error:
185 dup = NULL;
188 if (!dup)
189 return NULL;
191 return dup;
194 __isl_give isl_ast_expr *isl_ast_expr_cow(__isl_take isl_ast_expr *expr)
196 if (!expr)
197 return NULL;
199 if (expr->ref == 1)
200 return expr;
201 expr->ref--;
202 return isl_ast_expr_dup(expr);
205 __isl_null isl_ast_expr *isl_ast_expr_free(__isl_take isl_ast_expr *expr)
207 int i;
209 if (!expr)
210 return NULL;
212 if (--expr->ref > 0)
213 return NULL;
215 isl_ctx_deref(expr->ctx);
217 switch (expr->type) {
218 case isl_ast_expr_int:
219 isl_val_free(expr->u.v);
220 break;
221 case isl_ast_expr_id:
222 isl_id_free(expr->u.id);
223 break;
224 case isl_ast_expr_op:
225 if (expr->u.op.args)
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);
229 break;
230 case isl_ast_expr_error:
231 break;
234 free(expr);
235 return NULL;
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_get_val(__isl_keep isl_ast_expr *expr)
252 if (!expr)
253 return NULL;
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 __isl_give isl_id *isl_ast_expr_get_id(__isl_keep isl_ast_expr *expr)
262 if (!expr)
263 return NULL;
264 if (expr->type != isl_ast_expr_id)
265 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
266 "expression not an identifier", return NULL);
268 return isl_id_copy(expr->u.id);
271 enum isl_ast_op_type isl_ast_expr_get_op_type(__isl_keep isl_ast_expr *expr)
273 if (!expr)
274 return isl_ast_op_error;
275 if (expr->type != isl_ast_expr_op)
276 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
277 "expression not an operation", return isl_ast_op_error);
278 return expr->u.op.op;
281 isl_size isl_ast_expr_get_op_n_arg(__isl_keep isl_ast_expr *expr)
283 if (!expr)
284 return isl_size_error;
285 if (expr->type != isl_ast_expr_op)
286 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
287 "expression not an operation", return isl_size_error);
288 return expr->u.op.n_arg;
291 __isl_give isl_ast_expr *isl_ast_expr_get_op_arg(__isl_keep isl_ast_expr *expr,
292 int pos)
294 if (!expr)
295 return NULL;
296 if (expr->type != isl_ast_expr_op)
297 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
298 "expression not an operation", return NULL);
299 if (pos < 0 || pos >= expr->u.op.n_arg)
300 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
301 "index out of bounds", return NULL);
303 return isl_ast_expr_copy(expr->u.op.args[pos]);
306 /* Replace the argument at position "pos" of "expr" by "arg".
308 __isl_give isl_ast_expr *isl_ast_expr_set_op_arg(__isl_take isl_ast_expr *expr,
309 int pos, __isl_take isl_ast_expr *arg)
311 expr = isl_ast_expr_cow(expr);
312 if (!expr || !arg)
313 goto error;
314 if (expr->type != isl_ast_expr_op)
315 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
316 "expression not an operation", goto error);
317 if (pos < 0 || pos >= expr->u.op.n_arg)
318 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
319 "index out of bounds", goto error);
321 isl_ast_expr_free(expr->u.op.args[pos]);
322 expr->u.op.args[pos] = arg;
324 return expr;
325 error:
326 isl_ast_expr_free(arg);
327 return isl_ast_expr_free(expr);
330 /* Is "expr1" equal to "expr2"?
332 isl_bool isl_ast_expr_is_equal(__isl_keep isl_ast_expr *expr1,
333 __isl_keep isl_ast_expr *expr2)
335 int i;
337 if (!expr1 || !expr2)
338 return isl_bool_error;
340 if (expr1 == expr2)
341 return isl_bool_true;
342 if (expr1->type != expr2->type)
343 return isl_bool_false;
344 switch (expr1->type) {
345 case isl_ast_expr_int:
346 return isl_val_eq(expr1->u.v, expr2->u.v);
347 case isl_ast_expr_id:
348 return expr1->u.id == expr2->u.id;
349 case isl_ast_expr_op:
350 if (expr1->u.op.op != expr2->u.op.op)
351 return isl_bool_false;
352 if (expr1->u.op.n_arg != expr2->u.op.n_arg)
353 return isl_bool_false;
354 for (i = 0; i < expr1->u.op.n_arg; ++i) {
355 isl_bool equal;
356 equal = isl_ast_expr_is_equal(expr1->u.op.args[i],
357 expr2->u.op.args[i]);
358 if (equal < 0 || !equal)
359 return equal;
361 return isl_bool_true;
362 case isl_ast_expr_error:
363 return isl_bool_error;
366 isl_die(isl_ast_expr_get_ctx(expr1), isl_error_internal,
367 "unhandled case", return isl_bool_error);
370 /* Create a new operation expression of operation type "op",
371 * with "n_arg" as yet unspecified arguments.
373 __isl_give isl_ast_expr *isl_ast_expr_alloc_op(isl_ctx *ctx,
374 enum isl_ast_op_type op, int n_arg)
376 isl_ast_expr *expr;
378 expr = isl_calloc_type(ctx, isl_ast_expr);
379 if (!expr)
380 return NULL;
382 expr->ctx = ctx;
383 isl_ctx_ref(ctx);
384 expr->ref = 1;
385 expr->type = isl_ast_expr_op;
386 expr->u.op.op = op;
387 expr->u.op.n_arg = n_arg;
388 expr->u.op.args = isl_calloc_array(ctx, isl_ast_expr *, n_arg);
390 if (n_arg && !expr->u.op.args)
391 return isl_ast_expr_free(expr);
393 return expr;
396 /* Create a new id expression representing "id".
398 __isl_give isl_ast_expr *isl_ast_expr_from_id(__isl_take isl_id *id)
400 isl_ctx *ctx;
401 isl_ast_expr *expr;
403 if (!id)
404 return NULL;
406 ctx = isl_id_get_ctx(id);
407 expr = isl_calloc_type(ctx, isl_ast_expr);
408 if (!expr)
409 goto error;
411 expr->ctx = ctx;
412 isl_ctx_ref(ctx);
413 expr->ref = 1;
414 expr->type = isl_ast_expr_id;
415 expr->u.id = id;
417 return expr;
418 error:
419 isl_id_free(id);
420 return NULL;
423 /* Create a new integer expression representing "i".
425 __isl_give isl_ast_expr *isl_ast_expr_alloc_int_si(isl_ctx *ctx, int i)
427 isl_ast_expr *expr;
429 expr = isl_calloc_type(ctx, isl_ast_expr);
430 if (!expr)
431 return NULL;
433 expr->ctx = ctx;
434 isl_ctx_ref(ctx);
435 expr->ref = 1;
436 expr->type = isl_ast_expr_int;
437 expr->u.v = isl_val_int_from_si(ctx, i);
438 if (!expr->u.v)
439 return isl_ast_expr_free(expr);
441 return expr;
444 /* Create a new integer expression representing "v".
446 __isl_give isl_ast_expr *isl_ast_expr_from_val(__isl_take isl_val *v)
448 isl_ctx *ctx;
449 isl_ast_expr *expr;
451 if (!v)
452 return NULL;
453 if (!isl_val_is_int(v))
454 isl_die(isl_val_get_ctx(v), isl_error_invalid,
455 "expecting integer value", goto error);
457 ctx = isl_val_get_ctx(v);
458 expr = isl_calloc_type(ctx, isl_ast_expr);
459 if (!expr)
460 goto error;
462 expr->ctx = ctx;
463 isl_ctx_ref(ctx);
464 expr->ref = 1;
465 expr->type = isl_ast_expr_int;
466 expr->u.v = v;
468 return expr;
469 error:
470 isl_val_free(v);
471 return NULL;
474 /* Create an expression representing the unary operation "type" applied to
475 * "arg".
477 __isl_give isl_ast_expr *isl_ast_expr_alloc_unary(enum isl_ast_op_type type,
478 __isl_take isl_ast_expr *arg)
480 isl_ctx *ctx;
481 isl_ast_expr *expr = NULL;
483 if (!arg)
484 return NULL;
486 ctx = isl_ast_expr_get_ctx(arg);
487 expr = isl_ast_expr_alloc_op(ctx, type, 1);
488 if (!expr)
489 goto error;
491 expr->u.op.args[0] = arg;
493 return expr;
494 error:
495 isl_ast_expr_free(arg);
496 return NULL;
499 /* Create an expression representing the negation of "arg".
501 __isl_give isl_ast_expr *isl_ast_expr_neg(__isl_take isl_ast_expr *arg)
503 return isl_ast_expr_alloc_unary(isl_ast_op_minus, arg);
506 /* Create an expression representing the address of "expr".
508 __isl_give isl_ast_expr *isl_ast_expr_address_of(__isl_take isl_ast_expr *expr)
510 if (!expr)
511 return NULL;
513 if (isl_ast_expr_get_type(expr) != isl_ast_expr_op ||
514 isl_ast_expr_get_op_type(expr) != isl_ast_op_access)
515 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
516 "can only take address of access expressions",
517 return isl_ast_expr_free(expr));
519 return isl_ast_expr_alloc_unary(isl_ast_op_address_of, expr);
522 /* Create an expression representing the binary operation "type"
523 * applied to "expr1" and "expr2".
525 __isl_give isl_ast_expr *isl_ast_expr_alloc_binary(enum isl_ast_op_type type,
526 __isl_take isl_ast_expr *expr1, __isl_take isl_ast_expr *expr2)
528 isl_ctx *ctx;
529 isl_ast_expr *expr = NULL;
531 if (!expr1 || !expr2)
532 goto error;
534 ctx = isl_ast_expr_get_ctx(expr1);
535 expr = isl_ast_expr_alloc_op(ctx, type, 2);
536 if (!expr)
537 goto error;
539 expr->u.op.args[0] = expr1;
540 expr->u.op.args[1] = expr2;
542 return expr;
543 error:
544 isl_ast_expr_free(expr1);
545 isl_ast_expr_free(expr2);
546 return NULL;
549 /* Create an expression representing the sum of "expr1" and "expr2".
551 __isl_give isl_ast_expr *isl_ast_expr_add(__isl_take isl_ast_expr *expr1,
552 __isl_take isl_ast_expr *expr2)
554 return isl_ast_expr_alloc_binary(isl_ast_op_add, expr1, expr2);
557 /* Create an expression representing the difference of "expr1" and "expr2".
559 __isl_give isl_ast_expr *isl_ast_expr_sub(__isl_take isl_ast_expr *expr1,
560 __isl_take isl_ast_expr *expr2)
562 return isl_ast_expr_alloc_binary(isl_ast_op_sub, expr1, expr2);
565 /* Create an expression representing the product of "expr1" and "expr2".
567 __isl_give isl_ast_expr *isl_ast_expr_mul(__isl_take isl_ast_expr *expr1,
568 __isl_take isl_ast_expr *expr2)
570 return isl_ast_expr_alloc_binary(isl_ast_op_mul, expr1, expr2);
573 /* Create an expression representing the quotient of "expr1" and "expr2".
575 __isl_give isl_ast_expr *isl_ast_expr_div(__isl_take isl_ast_expr *expr1,
576 __isl_take isl_ast_expr *expr2)
578 return isl_ast_expr_alloc_binary(isl_ast_op_div, expr1, expr2);
581 /* Create an expression representing the quotient of the integer
582 * division of "expr1" by "expr2", where "expr1" is known to be
583 * non-negative.
585 __isl_give isl_ast_expr *isl_ast_expr_pdiv_q(__isl_take isl_ast_expr *expr1,
586 __isl_take isl_ast_expr *expr2)
588 return isl_ast_expr_alloc_binary(isl_ast_op_pdiv_q, expr1, expr2);
591 /* Create an expression representing the remainder of the integer
592 * division of "expr1" by "expr2", where "expr1" is known to be
593 * non-negative.
595 __isl_give isl_ast_expr *isl_ast_expr_pdiv_r(__isl_take isl_ast_expr *expr1,
596 __isl_take isl_ast_expr *expr2)
598 return isl_ast_expr_alloc_binary(isl_ast_op_pdiv_r, expr1, expr2);
601 /* Create an expression representing the conjunction of "expr1" and "expr2".
603 __isl_give isl_ast_expr *isl_ast_expr_and(__isl_take isl_ast_expr *expr1,
604 __isl_take isl_ast_expr *expr2)
606 return isl_ast_expr_alloc_binary(isl_ast_op_and, expr1, expr2);
609 /* Create an expression representing the conjunction of "expr1" and "expr2",
610 * where "expr2" is evaluated only if "expr1" is evaluated to true.
612 __isl_give isl_ast_expr *isl_ast_expr_and_then(__isl_take isl_ast_expr *expr1,
613 __isl_take isl_ast_expr *expr2)
615 return isl_ast_expr_alloc_binary(isl_ast_op_and_then, expr1, expr2);
618 /* Create an expression representing the disjunction of "expr1" and "expr2".
620 __isl_give isl_ast_expr *isl_ast_expr_or(__isl_take isl_ast_expr *expr1,
621 __isl_take isl_ast_expr *expr2)
623 return isl_ast_expr_alloc_binary(isl_ast_op_or, expr1, expr2);
626 /* Create an expression representing the disjunction of "expr1" and "expr2",
627 * where "expr2" is evaluated only if "expr1" is evaluated to false.
629 __isl_give isl_ast_expr *isl_ast_expr_or_else(__isl_take isl_ast_expr *expr1,
630 __isl_take isl_ast_expr *expr2)
632 return isl_ast_expr_alloc_binary(isl_ast_op_or_else, expr1, expr2);
635 /* Create an expression representing "expr1" less than or equal to "expr2".
637 __isl_give isl_ast_expr *isl_ast_expr_le(__isl_take isl_ast_expr *expr1,
638 __isl_take isl_ast_expr *expr2)
640 return isl_ast_expr_alloc_binary(isl_ast_op_le, expr1, expr2);
643 /* Create an expression representing "expr1" less than "expr2".
645 __isl_give isl_ast_expr *isl_ast_expr_lt(__isl_take isl_ast_expr *expr1,
646 __isl_take isl_ast_expr *expr2)
648 return isl_ast_expr_alloc_binary(isl_ast_op_lt, expr1, expr2);
651 /* Create an expression representing "expr1" greater than or equal to "expr2".
653 __isl_give isl_ast_expr *isl_ast_expr_ge(__isl_take isl_ast_expr *expr1,
654 __isl_take isl_ast_expr *expr2)
656 return isl_ast_expr_alloc_binary(isl_ast_op_ge, expr1, expr2);
659 /* Create an expression representing "expr1" greater than "expr2".
661 __isl_give isl_ast_expr *isl_ast_expr_gt(__isl_take isl_ast_expr *expr1,
662 __isl_take isl_ast_expr *expr2)
664 return isl_ast_expr_alloc_binary(isl_ast_op_gt, expr1, expr2);
667 /* Create an expression representing "expr1" equal to "expr2".
669 __isl_give isl_ast_expr *isl_ast_expr_eq(__isl_take isl_ast_expr *expr1,
670 __isl_take isl_ast_expr *expr2)
672 return isl_ast_expr_alloc_binary(isl_ast_op_eq, expr1, expr2);
675 /* Create an expression of type "type" with as arguments "arg0" followed
676 * by "arguments".
678 static __isl_give isl_ast_expr *ast_expr_with_arguments(
679 enum isl_ast_op_type type, __isl_take isl_ast_expr *arg0,
680 __isl_take isl_ast_expr_list *arguments)
682 int i;
683 isl_size n;
684 isl_ctx *ctx;
685 isl_ast_expr *res = NULL;
687 if (!arg0 || !arguments)
688 goto error;
690 ctx = isl_ast_expr_get_ctx(arg0);
691 n = isl_ast_expr_list_n_ast_expr(arguments);
692 if (n < 0)
693 goto error;
694 res = isl_ast_expr_alloc_op(ctx, type, 1 + n);
695 if (!res)
696 goto error;
697 for (i = 0; i < n; ++i) {
698 isl_ast_expr *arg;
699 arg = isl_ast_expr_list_get_ast_expr(arguments, i);
700 res->u.op.args[1 + i] = arg;
701 if (!arg)
702 goto error;
704 res->u.op.args[0] = arg0;
706 isl_ast_expr_list_free(arguments);
707 return res;
708 error:
709 isl_ast_expr_free(arg0);
710 isl_ast_expr_list_free(arguments);
711 isl_ast_expr_free(res);
712 return NULL;
715 /* Create an expression representing an access to "array" with index
716 * expressions "indices".
718 __isl_give isl_ast_expr *isl_ast_expr_access(__isl_take isl_ast_expr *array,
719 __isl_take isl_ast_expr_list *indices)
721 return ast_expr_with_arguments(isl_ast_op_access, array, indices);
724 /* Create an expression representing a call to "function" with argument
725 * expressions "arguments".
727 __isl_give isl_ast_expr *isl_ast_expr_call(__isl_take isl_ast_expr *function,
728 __isl_take isl_ast_expr_list *arguments)
730 return ast_expr_with_arguments(isl_ast_op_call, function, arguments);
733 /* For each subexpression of "expr" of type isl_ast_expr_id,
734 * if it appears in "id2expr", then replace it by the corresponding
735 * expression.
737 __isl_give isl_ast_expr *isl_ast_expr_substitute_ids(
738 __isl_take isl_ast_expr *expr, __isl_take isl_id_to_ast_expr *id2expr)
740 int i;
741 isl_maybe_isl_ast_expr m;
743 if (!expr || !id2expr)
744 goto error;
746 switch (expr->type) {
747 case isl_ast_expr_int:
748 break;
749 case isl_ast_expr_id:
750 m = isl_id_to_ast_expr_try_get(id2expr, expr->u.id);
751 if (m.valid < 0)
752 goto error;
753 if (!m.valid)
754 break;
755 isl_ast_expr_free(expr);
756 expr = m.value;
757 break;
758 case isl_ast_expr_op:
759 for (i = 0; i < expr->u.op.n_arg; ++i) {
760 isl_ast_expr *arg;
761 arg = isl_ast_expr_copy(expr->u.op.args[i]);
762 arg = isl_ast_expr_substitute_ids(arg,
763 isl_id_to_ast_expr_copy(id2expr));
764 if (arg == expr->u.op.args[i]) {
765 isl_ast_expr_free(arg);
766 continue;
768 if (!arg)
769 expr = isl_ast_expr_free(expr);
770 expr = isl_ast_expr_cow(expr);
771 if (!expr) {
772 isl_ast_expr_free(arg);
773 break;
775 isl_ast_expr_free(expr->u.op.args[i]);
776 expr->u.op.args[i] = arg;
778 break;
779 case isl_ast_expr_error:
780 expr = isl_ast_expr_free(expr);
781 break;
784 isl_id_to_ast_expr_free(id2expr);
785 return expr;
786 error:
787 isl_ast_expr_free(expr);
788 isl_id_to_ast_expr_free(id2expr);
789 return NULL;
792 isl_ctx *isl_ast_node_get_ctx(__isl_keep isl_ast_node *node)
794 return node ? node->ctx : NULL;
797 enum isl_ast_node_type isl_ast_node_get_type(__isl_keep isl_ast_node *node)
799 return node ? node->type : isl_ast_node_error;
802 __isl_give isl_ast_node *isl_ast_node_alloc(isl_ctx *ctx,
803 enum isl_ast_node_type type)
805 isl_ast_node *node;
807 node = isl_calloc_type(ctx, isl_ast_node);
808 if (!node)
809 return NULL;
811 node->ctx = ctx;
812 isl_ctx_ref(ctx);
813 node->ref = 1;
814 node->type = type;
816 return node;
819 /* Create an if node with the given guard.
821 * The then body needs to be filled in later.
823 __isl_give isl_ast_node *isl_ast_node_alloc_if(__isl_take isl_ast_expr *guard)
825 isl_ast_node *node;
827 if (!guard)
828 return NULL;
830 node = isl_ast_node_alloc(isl_ast_expr_get_ctx(guard), isl_ast_node_if);
831 if (!node)
832 goto error;
833 node->u.i.guard = guard;
835 return node;
836 error:
837 isl_ast_expr_free(guard);
838 return NULL;
841 /* Create a for node with the given iterator.
843 * The remaining fields need to be filled in later.
845 __isl_give isl_ast_node *isl_ast_node_alloc_for(__isl_take isl_id *id)
847 isl_ast_node *node;
848 isl_ctx *ctx;
850 if (!id)
851 return NULL;
853 ctx = isl_id_get_ctx(id);
854 node = isl_ast_node_alloc(ctx, isl_ast_node_for);
855 if (!node)
856 goto error;
858 node->u.f.iterator = isl_ast_expr_from_id(id);
859 if (!node->u.f.iterator)
860 return isl_ast_node_free(node);
862 return node;
863 error:
864 isl_id_free(id);
865 return NULL;
868 /* Create a mark node, marking "node" with "id".
870 __isl_give isl_ast_node *isl_ast_node_alloc_mark(__isl_take isl_id *id,
871 __isl_take isl_ast_node *node)
873 isl_ctx *ctx;
874 isl_ast_node *mark;
876 if (!id || !node)
877 goto error;
879 ctx = isl_id_get_ctx(id);
880 mark = isl_ast_node_alloc(ctx, isl_ast_node_mark);
881 if (!mark)
882 goto error;
884 mark->u.m.mark = id;
885 mark->u.m.node = node;
887 return mark;
888 error:
889 isl_id_free(id);
890 isl_ast_node_free(node);
891 return NULL;
894 /* Create a user node evaluating "expr".
896 __isl_give isl_ast_node *isl_ast_node_alloc_user(__isl_take isl_ast_expr *expr)
898 isl_ctx *ctx;
899 isl_ast_node *node;
901 if (!expr)
902 return NULL;
904 ctx = isl_ast_expr_get_ctx(expr);
905 node = isl_ast_node_alloc(ctx, isl_ast_node_user);
906 if (!node)
907 goto error;
909 node->u.e.expr = expr;
911 return node;
912 error:
913 isl_ast_expr_free(expr);
914 return NULL;
917 /* Create a block node with the given children.
919 __isl_give isl_ast_node *isl_ast_node_alloc_block(
920 __isl_take isl_ast_node_list *list)
922 isl_ast_node *node;
923 isl_ctx *ctx;
925 if (!list)
926 return NULL;
928 ctx = isl_ast_node_list_get_ctx(list);
929 node = isl_ast_node_alloc(ctx, isl_ast_node_block);
930 if (!node)
931 goto error;
933 node->u.b.children = list;
935 return node;
936 error:
937 isl_ast_node_list_free(list);
938 return NULL;
941 /* Represent the given list of nodes as a single node, either by
942 * extract the node from a single element list or by creating
943 * a block node with the list of nodes as children.
945 __isl_give isl_ast_node *isl_ast_node_from_ast_node_list(
946 __isl_take isl_ast_node_list *list)
948 isl_size n;
949 isl_ast_node *node;
951 n = isl_ast_node_list_n_ast_node(list);
952 if (n < 0)
953 goto error;
954 if (n != 1)
955 return isl_ast_node_alloc_block(list);
957 node = isl_ast_node_list_get_ast_node(list, 0);
958 isl_ast_node_list_free(list);
960 return node;
961 error:
962 isl_ast_node_list_free(list);
963 return NULL;
966 __isl_give isl_ast_node *isl_ast_node_copy(__isl_keep isl_ast_node *node)
968 if (!node)
969 return NULL;
971 node->ref++;
972 return node;
975 __isl_give isl_ast_node *isl_ast_node_dup(__isl_keep isl_ast_node *node)
977 isl_ast_node *dup;
979 if (!node)
980 return NULL;
982 dup = isl_ast_node_alloc(isl_ast_node_get_ctx(node), node->type);
983 if (!dup)
984 return NULL;
986 switch (node->type) {
987 case isl_ast_node_if:
988 dup->u.i.guard = isl_ast_expr_copy(node->u.i.guard);
989 dup->u.i.then = isl_ast_node_copy(node->u.i.then);
990 dup->u.i.else_node = isl_ast_node_copy(node->u.i.else_node);
991 if (!dup->u.i.guard || !dup->u.i.then ||
992 (node->u.i.else_node && !dup->u.i.else_node))
993 return isl_ast_node_free(dup);
994 break;
995 case isl_ast_node_for:
996 dup->u.f.iterator = isl_ast_expr_copy(node->u.f.iterator);
997 dup->u.f.init = isl_ast_expr_copy(node->u.f.init);
998 dup->u.f.cond = isl_ast_expr_copy(node->u.f.cond);
999 dup->u.f.inc = isl_ast_expr_copy(node->u.f.inc);
1000 dup->u.f.body = isl_ast_node_copy(node->u.f.body);
1001 if (!dup->u.f.iterator || !dup->u.f.init || !dup->u.f.cond ||
1002 !dup->u.f.inc || !dup->u.f.body)
1003 return isl_ast_node_free(dup);
1004 break;
1005 case isl_ast_node_block:
1006 dup->u.b.children = isl_ast_node_list_copy(node->u.b.children);
1007 if (!dup->u.b.children)
1008 return isl_ast_node_free(dup);
1009 break;
1010 case isl_ast_node_mark:
1011 dup->u.m.mark = isl_id_copy(node->u.m.mark);
1012 dup->u.m.node = isl_ast_node_copy(node->u.m.node);
1013 if (!dup->u.m.mark || !dup->u.m.node)
1014 return isl_ast_node_free(dup);
1015 break;
1016 case isl_ast_node_user:
1017 dup->u.e.expr = isl_ast_expr_copy(node->u.e.expr);
1018 if (!dup->u.e.expr)
1019 return isl_ast_node_free(dup);
1020 break;
1021 case isl_ast_node_error:
1022 break;
1025 return dup;
1028 __isl_give isl_ast_node *isl_ast_node_cow(__isl_take isl_ast_node *node)
1030 if (!node)
1031 return NULL;
1033 if (node->ref == 1)
1034 return node;
1035 node->ref--;
1036 return isl_ast_node_dup(node);
1039 __isl_null isl_ast_node *isl_ast_node_free(__isl_take isl_ast_node *node)
1041 if (!node)
1042 return NULL;
1044 if (--node->ref > 0)
1045 return NULL;
1047 switch (node->type) {
1048 case isl_ast_node_if:
1049 isl_ast_expr_free(node->u.i.guard);
1050 isl_ast_node_free(node->u.i.then);
1051 isl_ast_node_free(node->u.i.else_node);
1052 break;
1053 case isl_ast_node_for:
1054 isl_ast_expr_free(node->u.f.iterator);
1055 isl_ast_expr_free(node->u.f.init);
1056 isl_ast_expr_free(node->u.f.cond);
1057 isl_ast_expr_free(node->u.f.inc);
1058 isl_ast_node_free(node->u.f.body);
1059 break;
1060 case isl_ast_node_block:
1061 isl_ast_node_list_free(node->u.b.children);
1062 break;
1063 case isl_ast_node_mark:
1064 isl_id_free(node->u.m.mark);
1065 isl_ast_node_free(node->u.m.node);
1066 break;
1067 case isl_ast_node_user:
1068 isl_ast_expr_free(node->u.e.expr);
1069 break;
1070 case isl_ast_node_error:
1071 break;
1074 isl_id_free(node->annotation);
1075 isl_ctx_deref(node->ctx);
1076 free(node);
1078 return NULL;
1081 /* Replace the body of the for node "node" by "body".
1083 __isl_give isl_ast_node *isl_ast_node_for_set_body(
1084 __isl_take isl_ast_node *node, __isl_take isl_ast_node *body)
1086 node = isl_ast_node_cow(node);
1087 if (!node || !body)
1088 goto error;
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", goto error);
1093 isl_ast_node_free(node->u.f.body);
1094 node->u.f.body = body;
1096 return node;
1097 error:
1098 isl_ast_node_free(node);
1099 isl_ast_node_free(body);
1100 return NULL;
1103 __isl_give isl_ast_node *isl_ast_node_for_get_body(
1104 __isl_keep isl_ast_node *node)
1106 if (!node)
1107 return NULL;
1108 if (node->type != isl_ast_node_for)
1109 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1110 "not a for node", return NULL);
1111 return isl_ast_node_copy(node->u.f.body);
1114 /* Mark the given for node as being degenerate.
1116 __isl_give isl_ast_node *isl_ast_node_for_mark_degenerate(
1117 __isl_take isl_ast_node *node)
1119 node = isl_ast_node_cow(node);
1120 if (!node)
1121 return NULL;
1122 node->u.f.degenerate = 1;
1123 return node;
1126 isl_bool isl_ast_node_for_is_degenerate(__isl_keep isl_ast_node *node)
1128 if (!node)
1129 return isl_bool_error;
1130 if (node->type != isl_ast_node_for)
1131 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1132 "not a for node", return isl_bool_error);
1133 return node->u.f.degenerate;
1136 __isl_give isl_ast_expr *isl_ast_node_for_get_iterator(
1137 __isl_keep isl_ast_node *node)
1139 if (!node)
1140 return NULL;
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", return NULL);
1144 return isl_ast_expr_copy(node->u.f.iterator);
1147 __isl_give isl_ast_expr *isl_ast_node_for_get_init(
1148 __isl_keep isl_ast_node *node)
1150 if (!node)
1151 return NULL;
1152 if (node->type != isl_ast_node_for)
1153 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1154 "not a for node", return NULL);
1155 return isl_ast_expr_copy(node->u.f.init);
1158 /* Return the condition expression of the given for node.
1160 * If the for node is degenerate, then the condition is not explicitly
1161 * stored in the node. Instead, it is constructed as
1163 * iterator <= init
1165 __isl_give isl_ast_expr *isl_ast_node_for_get_cond(
1166 __isl_keep isl_ast_node *node)
1168 if (!node)
1169 return NULL;
1170 if (node->type != isl_ast_node_for)
1171 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1172 "not a for node", return NULL);
1173 if (!node->u.f.degenerate)
1174 return isl_ast_expr_copy(node->u.f.cond);
1176 return isl_ast_expr_alloc_binary(isl_ast_op_le,
1177 isl_ast_expr_copy(node->u.f.iterator),
1178 isl_ast_expr_copy(node->u.f.init));
1181 /* Return the increment of the given for node.
1183 * If the for node is degenerate, then the increment is not explicitly
1184 * stored in the node. We simply return "1".
1186 __isl_give isl_ast_expr *isl_ast_node_for_get_inc(
1187 __isl_keep isl_ast_node *node)
1189 if (!node)
1190 return NULL;
1191 if (node->type != isl_ast_node_for)
1192 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1193 "not a for node", return NULL);
1194 if (!node->u.f.degenerate)
1195 return isl_ast_expr_copy(node->u.f.inc);
1196 return isl_ast_expr_alloc_int_si(isl_ast_node_get_ctx(node), 1);
1199 /* Replace the then branch of the if node "node" by "child".
1201 __isl_give isl_ast_node *isl_ast_node_if_set_then(
1202 __isl_take isl_ast_node *node, __isl_take isl_ast_node *child)
1204 node = isl_ast_node_cow(node);
1205 if (!node || !child)
1206 goto error;
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", goto error);
1211 isl_ast_node_free(node->u.i.then);
1212 node->u.i.then = child;
1214 return node;
1215 error:
1216 isl_ast_node_free(node);
1217 isl_ast_node_free(child);
1218 return NULL;
1221 __isl_give isl_ast_node *isl_ast_node_if_get_then(
1222 __isl_keep isl_ast_node *node)
1224 if (!node)
1225 return NULL;
1226 if (node->type != isl_ast_node_if)
1227 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1228 "not an if node", return NULL);
1229 return isl_ast_node_copy(node->u.i.then);
1232 isl_bool isl_ast_node_if_has_else(
1233 __isl_keep isl_ast_node *node)
1235 if (!node)
1236 return isl_bool_error;
1237 if (node->type != isl_ast_node_if)
1238 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1239 "not an if node", return isl_bool_error);
1240 return node->u.i.else_node != NULL;
1243 __isl_give isl_ast_node *isl_ast_node_if_get_else(
1244 __isl_keep isl_ast_node *node)
1246 if (!node)
1247 return NULL;
1248 if (node->type != isl_ast_node_if)
1249 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1250 "not an if node", return NULL);
1251 return isl_ast_node_copy(node->u.i.else_node);
1254 __isl_give isl_ast_expr *isl_ast_node_if_get_cond(
1255 __isl_keep isl_ast_node *node)
1257 if (!node)
1258 return NULL;
1259 if (node->type != isl_ast_node_if)
1260 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1261 "not a guard node", return NULL);
1262 return isl_ast_expr_copy(node->u.i.guard);
1265 __isl_give isl_ast_node_list *isl_ast_node_block_get_children(
1266 __isl_keep isl_ast_node *node)
1268 if (!node)
1269 return NULL;
1270 if (node->type != isl_ast_node_block)
1271 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1272 "not a block node", return NULL);
1273 return isl_ast_node_list_copy(node->u.b.children);
1276 __isl_give isl_ast_expr *isl_ast_node_user_get_expr(
1277 __isl_keep isl_ast_node *node)
1279 if (!node)
1280 return NULL;
1281 if (node->type != isl_ast_node_user)
1282 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1283 "not a user node", return NULL);
1285 return isl_ast_expr_copy(node->u.e.expr);
1288 /* Return the mark identifier of the mark node "node".
1290 __isl_give isl_id *isl_ast_node_mark_get_id(__isl_keep isl_ast_node *node)
1292 if (!node)
1293 return NULL;
1294 if (node->type != isl_ast_node_mark)
1295 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1296 "not a mark node", return NULL);
1298 return isl_id_copy(node->u.m.mark);
1301 /* Return the node marked by mark node "node".
1303 __isl_give isl_ast_node *isl_ast_node_mark_get_node(
1304 __isl_keep isl_ast_node *node)
1306 if (!node)
1307 return NULL;
1308 if (node->type != isl_ast_node_mark)
1309 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1310 "not a mark node", return NULL);
1312 return isl_ast_node_copy(node->u.m.node);
1315 __isl_give isl_id *isl_ast_node_get_annotation(__isl_keep isl_ast_node *node)
1317 return node ? isl_id_copy(node->annotation) : NULL;
1320 /* Replace node->annotation by "annotation".
1322 __isl_give isl_ast_node *isl_ast_node_set_annotation(
1323 __isl_take isl_ast_node *node, __isl_take isl_id *annotation)
1325 node = isl_ast_node_cow(node);
1326 if (!node || !annotation)
1327 goto error;
1329 isl_id_free(node->annotation);
1330 node->annotation = annotation;
1332 return node;
1333 error:
1334 isl_id_free(annotation);
1335 return isl_ast_node_free(node);
1338 /* Traverse the elements of "list" and all their descendants
1339 * in depth first preorder.
1341 * Return isl_stat_ok on success and isl_stat_error on failure.
1343 static isl_stat nodelist_foreach(__isl_keep isl_ast_node_list *list,
1344 isl_bool (*fn)(__isl_keep isl_ast_node *node, void *user), void *user)
1346 int i;
1348 if (!list)
1349 return isl_stat_error;
1351 for (i = 0; i < list->n; ++i) {
1352 isl_stat ok;
1353 isl_ast_node *node = list->p[i];
1355 ok = isl_ast_node_foreach_descendant_top_down(node, fn, user);
1356 if (ok < 0)
1357 return isl_stat_error;
1360 return isl_stat_ok;
1363 /* Traverse the descendants of "node" (including the node itself)
1364 * in depth first preorder.
1366 * If "fn" returns isl_bool_error on any of the nodes, then the traversal
1367 * is aborted.
1368 * If "fn" returns isl_bool_false on any of the nodes, then the subtree rooted
1369 * at that node is skipped.
1371 * Return isl_stat_ok on success and isl_stat_error on failure.
1373 isl_stat isl_ast_node_foreach_descendant_top_down(
1374 __isl_keep isl_ast_node *node,
1375 isl_bool (*fn)(__isl_keep isl_ast_node *node, void *user), void *user)
1377 isl_bool more;
1378 isl_stat ok;
1380 if (!node)
1381 return isl_stat_error;
1383 more = fn(node, user);
1384 if (more < 0)
1385 return isl_stat_error;
1386 if (!more)
1387 return isl_stat_ok;
1389 switch (node->type) {
1390 case isl_ast_node_for:
1391 node = node->u.f.body;
1392 return isl_ast_node_foreach_descendant_top_down(node, fn, user);
1393 case isl_ast_node_if:
1394 ok = isl_ast_node_foreach_descendant_top_down(node->u.i.then,
1395 fn, user);
1396 if (ok < 0)
1397 return isl_stat_error;
1398 if (!node->u.i.else_node)
1399 return isl_stat_ok;
1400 node = node->u.i.else_node;
1401 return isl_ast_node_foreach_descendant_top_down(node, fn, user);
1402 case isl_ast_node_block:
1403 return nodelist_foreach(node->u.b.children, fn, user);
1404 case isl_ast_node_mark:
1405 node = node->u.m.node;
1406 return isl_ast_node_foreach_descendant_top_down(node, fn, user);
1407 case isl_ast_node_user:
1408 break;
1409 case isl_ast_node_error:
1410 return isl_stat_error;
1413 return isl_stat_ok;
1416 /* Textual C representation of the various operators.
1418 static char *op_str_c[] = {
1419 [isl_ast_op_and] = "&&",
1420 [isl_ast_op_and_then] = "&&",
1421 [isl_ast_op_or] = "||",
1422 [isl_ast_op_or_else] = "||",
1423 [isl_ast_op_max] = "max",
1424 [isl_ast_op_min] = "min",
1425 [isl_ast_op_minus] = "-",
1426 [isl_ast_op_add] = "+",
1427 [isl_ast_op_sub] = "-",
1428 [isl_ast_op_mul] = "*",
1429 [isl_ast_op_fdiv_q] = "floord",
1430 [isl_ast_op_pdiv_q] = "/",
1431 [isl_ast_op_pdiv_r] = "%",
1432 [isl_ast_op_zdiv_r] = "%",
1433 [isl_ast_op_div] = "/",
1434 [isl_ast_op_eq] = "==",
1435 [isl_ast_op_le] = "<=",
1436 [isl_ast_op_ge] = ">=",
1437 [isl_ast_op_lt] = "<",
1438 [isl_ast_op_gt] = ">",
1439 [isl_ast_op_member] = ".",
1440 [isl_ast_op_address_of] = "&"
1443 /* Precedence in C of the various operators.
1444 * Based on http://en.wikipedia.org/wiki/Operators_in_C_and_C++
1445 * Lowest value means highest precedence.
1447 static int op_prec[] = {
1448 [isl_ast_op_and] = 13,
1449 [isl_ast_op_and_then] = 13,
1450 [isl_ast_op_or] = 14,
1451 [isl_ast_op_or_else] = 14,
1452 [isl_ast_op_max] = 2,
1453 [isl_ast_op_min] = 2,
1454 [isl_ast_op_minus] = 3,
1455 [isl_ast_op_add] = 6,
1456 [isl_ast_op_sub] = 6,
1457 [isl_ast_op_mul] = 5,
1458 [isl_ast_op_div] = 5,
1459 [isl_ast_op_fdiv_q] = 2,
1460 [isl_ast_op_pdiv_q] = 5,
1461 [isl_ast_op_pdiv_r] = 5,
1462 [isl_ast_op_zdiv_r] = 5,
1463 [isl_ast_op_cond] = 15,
1464 [isl_ast_op_select] = 15,
1465 [isl_ast_op_eq] = 9,
1466 [isl_ast_op_le] = 8,
1467 [isl_ast_op_ge] = 8,
1468 [isl_ast_op_lt] = 8,
1469 [isl_ast_op_gt] = 8,
1470 [isl_ast_op_call] = 2,
1471 [isl_ast_op_access] = 2,
1472 [isl_ast_op_member] = 2,
1473 [isl_ast_op_address_of] = 3
1476 /* Is the operator left-to-right associative?
1478 static int op_left[] = {
1479 [isl_ast_op_and] = 1,
1480 [isl_ast_op_and_then] = 1,
1481 [isl_ast_op_or] = 1,
1482 [isl_ast_op_or_else] = 1,
1483 [isl_ast_op_max] = 1,
1484 [isl_ast_op_min] = 1,
1485 [isl_ast_op_minus] = 0,
1486 [isl_ast_op_add] = 1,
1487 [isl_ast_op_sub] = 1,
1488 [isl_ast_op_mul] = 1,
1489 [isl_ast_op_div] = 1,
1490 [isl_ast_op_fdiv_q] = 1,
1491 [isl_ast_op_pdiv_q] = 1,
1492 [isl_ast_op_pdiv_r] = 1,
1493 [isl_ast_op_zdiv_r] = 1,
1494 [isl_ast_op_cond] = 0,
1495 [isl_ast_op_select] = 0,
1496 [isl_ast_op_eq] = 1,
1497 [isl_ast_op_le] = 1,
1498 [isl_ast_op_ge] = 1,
1499 [isl_ast_op_lt] = 1,
1500 [isl_ast_op_gt] = 1,
1501 [isl_ast_op_call] = 1,
1502 [isl_ast_op_access] = 1,
1503 [isl_ast_op_member] = 1,
1504 [isl_ast_op_address_of] = 0
1507 static int is_and(enum isl_ast_op_type op)
1509 return op == isl_ast_op_and || op == isl_ast_op_and_then;
1512 static int is_or(enum isl_ast_op_type op)
1514 return op == isl_ast_op_or || op == isl_ast_op_or_else;
1517 static int is_add_sub(enum isl_ast_op_type op)
1519 return op == isl_ast_op_add || op == isl_ast_op_sub;
1522 static int is_div_mod(enum isl_ast_op_type op)
1524 return op == isl_ast_op_div ||
1525 op == isl_ast_op_pdiv_r ||
1526 op == isl_ast_op_zdiv_r;
1529 static __isl_give isl_printer *print_ast_expr_c(__isl_take isl_printer *p,
1530 __isl_keep isl_ast_expr *expr);
1532 /* Do we need/want parentheses around "expr" as a subexpression of
1533 * an "op" operation? If "left" is set, then "expr" is the left-most
1534 * operand.
1536 * We only need parentheses if "expr" represents an operation.
1538 * If op has a higher precedence than expr->u.op.op, then we need
1539 * parentheses.
1540 * If op and expr->u.op.op have the same precedence, but the operations
1541 * are performed in an order that is different from the associativity,
1542 * then we need parentheses.
1544 * An and inside an or technically does not require parentheses,
1545 * but some compilers complain about that, so we add them anyway.
1547 * Computations such as "a / b * c" and "a % b + c" can be somewhat
1548 * difficult to read, so we add parentheses for those as well.
1550 static int sub_expr_need_parens(enum isl_ast_op_type op,
1551 __isl_keep isl_ast_expr *expr, int left)
1553 if (expr->type != isl_ast_expr_op)
1554 return 0;
1556 if (op_prec[expr->u.op.op] > op_prec[op])
1557 return 1;
1558 if (op_prec[expr->u.op.op] == op_prec[op] && left != op_left[op])
1559 return 1;
1561 if (is_or(op) && is_and(expr->u.op.op))
1562 return 1;
1563 if (op == isl_ast_op_mul && expr->u.op.op != isl_ast_op_mul &&
1564 op_prec[expr->u.op.op] == op_prec[op])
1565 return 1;
1566 if (is_add_sub(op) && is_div_mod(expr->u.op.op))
1567 return 1;
1569 return 0;
1572 /* Print "expr" as a subexpression of an "op" operation in C format.
1573 * If "left" is set, then "expr" is the left-most operand.
1575 static __isl_give isl_printer *print_sub_expr_c(__isl_take isl_printer *p,
1576 enum isl_ast_op_type op, __isl_keep isl_ast_expr *expr, int left)
1578 int need_parens;
1580 need_parens = sub_expr_need_parens(op, expr, left);
1582 if (need_parens)
1583 p = isl_printer_print_str(p, "(");
1584 p = print_ast_expr_c(p, expr);
1585 if (need_parens)
1586 p = isl_printer_print_str(p, ")");
1587 return p;
1590 #define isl_ast_op_last isl_ast_op_address_of
1592 /* Data structure that holds the user-specified textual
1593 * representations for the operators in C format.
1594 * The entries are either NULL or copies of strings.
1595 * A NULL entry means that the default name should be used.
1597 struct isl_ast_op_names {
1598 char *op_str[isl_ast_op_last + 1];
1601 /* Create an empty struct isl_ast_op_names.
1603 static void *create_names(isl_ctx *ctx)
1605 return isl_calloc_type(ctx, struct isl_ast_op_names);
1608 /* Free a struct isl_ast_op_names along with all memory
1609 * owned by the struct.
1611 static void free_names(void *user)
1613 int i;
1614 struct isl_ast_op_names *names = user;
1616 if (!user)
1617 return;
1619 for (i = 0; i <= isl_ast_op_last; ++i)
1620 free(names->op_str[i]);
1621 free(user);
1624 /* Create an identifier that is used to store
1625 * an isl_ast_op_names note.
1627 static __isl_give isl_id *names_id(isl_ctx *ctx)
1629 return isl_id_alloc(ctx, "isl_ast_op_type_names", NULL);
1632 /* Ensure that "p" has a note identified by "id".
1633 * If there is no such note yet, then it is created by "note_create" and
1634 * scheduled do be freed by "note_free".
1636 static __isl_give isl_printer *alloc_note(__isl_take isl_printer *p,
1637 __isl_keep isl_id *id, void *(*note_create)(isl_ctx *),
1638 void (*note_free)(void *))
1640 isl_ctx *ctx;
1641 isl_id *note_id;
1642 isl_bool has_note;
1643 void *note;
1645 has_note = isl_printer_has_note(p, id);
1646 if (has_note < 0)
1647 return isl_printer_free(p);
1648 if (has_note)
1649 return p;
1651 ctx = isl_printer_get_ctx(p);
1652 note = note_create(ctx);
1653 if (!note)
1654 return isl_printer_free(p);
1655 note_id = isl_id_alloc(ctx, NULL, note);
1656 if (!note_id)
1657 note_free(note);
1658 else
1659 note_id = isl_id_set_free_user(note_id, note_free);
1661 p = isl_printer_set_note(p, isl_id_copy(id), note_id);
1663 return p;
1666 /* Ensure that "p" has an isl_ast_op_names note identified by "id".
1668 static __isl_give isl_printer *alloc_names(__isl_take isl_printer *p,
1669 __isl_keep isl_id *id)
1671 return alloc_note(p, id, &create_names, &free_names);
1674 /* Retrieve the note identified by "id" from "p".
1675 * The note is assumed to exist.
1677 static void *get_note(__isl_keep isl_printer *p, __isl_keep isl_id *id)
1679 void *note;
1681 id = isl_printer_get_note(p, isl_id_copy(id));
1682 note = isl_id_get_user(id);
1683 isl_id_free(id);
1685 return note;
1688 /* Use "name" to print operations of type "type" to "p".
1690 * Store the name in an isl_ast_op_names note attached to "p", such that
1691 * it can be retrieved by get_op_str.
1693 __isl_give isl_printer *isl_ast_op_type_set_print_name(
1694 __isl_take isl_printer *p, enum isl_ast_op_type type,
1695 __isl_keep const char *name)
1697 isl_id *id;
1698 struct isl_ast_op_names *names;
1700 if (!p)
1701 return NULL;
1702 if (type > isl_ast_op_last)
1703 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
1704 "invalid type", return isl_printer_free(p));
1706 id = names_id(isl_printer_get_ctx(p));
1707 p = alloc_names(p, id);
1708 names = get_note(p, id);
1709 isl_id_free(id);
1710 if (!names)
1711 return isl_printer_free(p);
1712 free(names->op_str[type]);
1713 names->op_str[type] = strdup(name);
1715 return p;
1718 /* Return the textual representation of "type" in C format.
1720 * If there is a user-specified name in an isl_ast_op_names note
1721 * associated to "p", then return that.
1722 * Otherwise, return the default name in op_str.
1724 static const char *get_op_str_c(__isl_keep isl_printer *p,
1725 enum isl_ast_op_type type)
1727 isl_id *id;
1728 isl_bool has_names;
1729 struct isl_ast_op_names *names = NULL;
1731 id = names_id(isl_printer_get_ctx(p));
1732 has_names = isl_printer_has_note(p, id);
1733 if (has_names >= 0 && has_names)
1734 names = get_note(p, id);
1735 isl_id_free(id);
1736 if (names && names->op_str[type])
1737 return names->op_str[type];
1738 return op_str_c[type];
1741 /* Print a min or max reduction "expr" in C format.
1743 static __isl_give isl_printer *print_min_max_c(__isl_take isl_printer *p,
1744 __isl_keep isl_ast_expr *expr)
1746 int i = 0;
1748 for (i = 1; i < expr->u.op.n_arg; ++i) {
1749 p = isl_printer_print_str(p, get_op_str_c(p, expr->u.op.op));
1750 p = isl_printer_print_str(p, "(");
1752 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1753 for (i = 1; i < expr->u.op.n_arg; ++i) {
1754 p = isl_printer_print_str(p, ", ");
1755 p = print_ast_expr_c(p, expr->u.op.args[i]);
1756 p = isl_printer_print_str(p, ")");
1759 return p;
1762 /* Print a function call "expr" in C format.
1764 * The first argument represents the function to be called.
1766 static __isl_give isl_printer *print_call_c(__isl_take isl_printer *p,
1767 __isl_keep isl_ast_expr *expr)
1769 int i = 0;
1771 p = print_ast_expr_c(p, expr->u.op.args[0]);
1772 p = isl_printer_print_str(p, "(");
1773 for (i = 1; i < expr->u.op.n_arg; ++i) {
1774 if (i != 1)
1775 p = isl_printer_print_str(p, ", ");
1776 p = print_ast_expr_c(p, expr->u.op.args[i]);
1778 p = isl_printer_print_str(p, ")");
1780 return p;
1783 /* Print an array access "expr" in C format.
1785 * The first argument represents the array being accessed.
1787 static __isl_give isl_printer *print_access_c(__isl_take isl_printer *p,
1788 __isl_keep isl_ast_expr *expr)
1790 int i = 0;
1792 p = print_ast_expr_c(p, expr->u.op.args[0]);
1793 for (i = 1; i < expr->u.op.n_arg; ++i) {
1794 p = isl_printer_print_str(p, "[");
1795 p = print_ast_expr_c(p, expr->u.op.args[i]);
1796 p = isl_printer_print_str(p, "]");
1799 return p;
1802 /* Print "expr" to "p" in C format.
1804 static __isl_give isl_printer *print_ast_expr_c(__isl_take isl_printer *p,
1805 __isl_keep isl_ast_expr *expr)
1807 if (!p)
1808 return NULL;
1809 if (!expr)
1810 return isl_printer_free(p);
1812 switch (expr->type) {
1813 case isl_ast_expr_op:
1814 if (expr->u.op.op == isl_ast_op_call) {
1815 p = print_call_c(p, expr);
1816 break;
1818 if (expr->u.op.op == isl_ast_op_access) {
1819 p = print_access_c(p, expr);
1820 break;
1822 if (expr->u.op.n_arg == 1) {
1823 p = isl_printer_print_str(p,
1824 get_op_str_c(p, expr->u.op.op));
1825 p = print_sub_expr_c(p, expr->u.op.op,
1826 expr->u.op.args[0], 0);
1827 break;
1829 if (expr->u.op.op == isl_ast_op_fdiv_q) {
1830 const char *name = get_op_str_c(p, isl_ast_op_fdiv_q);
1831 p = isl_printer_print_str(p, name);
1832 p = isl_printer_print_str(p, "(");
1833 p = print_ast_expr_c(p, expr->u.op.args[0]);
1834 p = isl_printer_print_str(p, ", ");
1835 p = print_ast_expr_c(p, expr->u.op.args[1]);
1836 p = isl_printer_print_str(p, ")");
1837 break;
1839 if (expr->u.op.op == isl_ast_op_max ||
1840 expr->u.op.op == isl_ast_op_min) {
1841 p = print_min_max_c(p, expr);
1842 break;
1844 if (expr->u.op.op == isl_ast_op_cond ||
1845 expr->u.op.op == isl_ast_op_select) {
1846 p = print_ast_expr_c(p, expr->u.op.args[0]);
1847 p = isl_printer_print_str(p, " ? ");
1848 p = print_ast_expr_c(p, expr->u.op.args[1]);
1849 p = isl_printer_print_str(p, " : ");
1850 p = print_ast_expr_c(p, expr->u.op.args[2]);
1851 break;
1853 if (expr->u.op.n_arg != 2)
1854 isl_die(isl_printer_get_ctx(p), isl_error_internal,
1855 "operation should have two arguments",
1856 return isl_printer_free(p));
1857 p = print_sub_expr_c(p, expr->u.op.op, expr->u.op.args[0], 1);
1858 if (expr->u.op.op != isl_ast_op_member)
1859 p = isl_printer_print_str(p, " ");
1860 p = isl_printer_print_str(p, get_op_str_c(p, expr->u.op.op));
1861 if (expr->u.op.op != isl_ast_op_member)
1862 p = isl_printer_print_str(p, " ");
1863 p = print_sub_expr_c(p, expr->u.op.op, expr->u.op.args[1], 0);
1864 break;
1865 case isl_ast_expr_id:
1866 p = isl_printer_print_str(p, isl_id_get_name(expr->u.id));
1867 break;
1868 case isl_ast_expr_int:
1869 p = isl_printer_print_val(p, expr->u.v);
1870 break;
1871 case isl_ast_expr_error:
1872 break;
1875 return p;
1878 /* Textual representation of the isl_ast_op_type elements
1879 * for use in a YAML representation of an isl_ast_expr.
1881 static char *op_str[] = {
1882 [isl_ast_op_and] = "and",
1883 [isl_ast_op_and_then] = "and_then",
1884 [isl_ast_op_or] = "or",
1885 [isl_ast_op_or_else] = "or_else",
1886 [isl_ast_op_max] = "max",
1887 [isl_ast_op_min] = "min",
1888 [isl_ast_op_minus] = "minus",
1889 [isl_ast_op_add] = "add",
1890 [isl_ast_op_sub] = "sub",
1891 [isl_ast_op_mul] = "mul",
1892 [isl_ast_op_div] = "div",
1893 [isl_ast_op_fdiv_q] = "fdiv_q",
1894 [isl_ast_op_pdiv_q] = "pdiv_q",
1895 [isl_ast_op_pdiv_r] = "pdiv_r",
1896 [isl_ast_op_zdiv_r] = "zdiv_r",
1897 [isl_ast_op_cond] = "cond",
1898 [isl_ast_op_select] = "select",
1899 [isl_ast_op_eq] = "eq",
1900 [isl_ast_op_le] = "le",
1901 [isl_ast_op_lt] = "lt",
1902 [isl_ast_op_ge] = "ge",
1903 [isl_ast_op_gt] = "gt",
1904 [isl_ast_op_call] = "call",
1905 [isl_ast_op_access] = "access",
1906 [isl_ast_op_member] = "member",
1907 [isl_ast_op_address_of] = "address_of"
1910 static __isl_give isl_printer *print_ast_expr_isl(__isl_take isl_printer *p,
1911 __isl_keep isl_ast_expr *expr);
1913 /* Print the arguments of "expr" to "p" in isl format.
1915 * If there are no arguments, then nothing needs to be printed.
1916 * Otherwise add an "args" key to the current mapping with as value
1917 * the list of arguments of "expr".
1919 static __isl_give isl_printer *print_arguments(__isl_take isl_printer *p,
1920 __isl_keep isl_ast_expr *expr)
1922 int i;
1923 isl_size n;
1925 n = isl_ast_expr_get_op_n_arg(expr);
1926 if (n < 0)
1927 return isl_printer_free(p);
1928 if (n == 0)
1929 return p;
1931 p = isl_printer_print_str(p, "args");
1932 p = isl_printer_yaml_next(p);
1933 p = isl_printer_yaml_start_sequence(p);
1934 for (i = 0; i < n; ++i) {
1935 isl_ast_expr *arg;
1937 arg = isl_ast_expr_get_op_arg(expr, i);
1938 p = print_ast_expr_isl(p, arg);
1939 isl_ast_expr_free(arg);
1940 p = isl_printer_yaml_next(p);
1942 p = isl_printer_yaml_end_sequence(p);
1944 return p;
1947 /* Print "expr" to "p" in isl format.
1949 * In particular, print the isl_ast_expr as a YAML document.
1951 static __isl_give isl_printer *print_ast_expr_isl(__isl_take isl_printer *p,
1952 __isl_keep isl_ast_expr *expr)
1954 enum isl_ast_expr_type type;
1955 enum isl_ast_op_type op;
1956 isl_id *id;
1957 isl_val *v;
1959 if (!expr)
1960 return isl_printer_free(p);
1962 p = isl_printer_yaml_start_mapping(p);
1963 type = isl_ast_expr_get_type(expr);
1964 switch (type) {
1965 case isl_ast_expr_error:
1966 return isl_printer_free(p);
1967 case isl_ast_expr_op:
1968 op = isl_ast_expr_get_op_type(expr);
1969 if (op == isl_ast_op_error)
1970 return isl_printer_free(p);
1971 p = isl_printer_print_str(p, "op");
1972 p = isl_printer_yaml_next(p);
1973 p = isl_printer_print_str(p, op_str[op]);
1974 p = isl_printer_yaml_next(p);
1975 p = print_arguments(p, expr);
1976 break;
1977 case isl_ast_expr_id:
1978 p = isl_printer_print_str(p, "id");
1979 p = isl_printer_yaml_next(p);
1980 id = isl_ast_expr_get_id(expr);
1981 p = isl_printer_print_id(p, id);
1982 isl_id_free(id);
1983 break;
1984 case isl_ast_expr_int:
1985 p = isl_printer_print_str(p, "val");
1986 p = isl_printer_yaml_next(p);
1987 v = isl_ast_expr_get_val(expr);
1988 p = isl_printer_print_val(p, v);
1989 isl_val_free(v);
1990 break;
1992 p = isl_printer_yaml_end_mapping(p);
1994 return p;
1997 /* Print "expr" to "p".
1999 * Only an isl and a C format are supported.
2001 __isl_give isl_printer *isl_printer_print_ast_expr(__isl_take isl_printer *p,
2002 __isl_keep isl_ast_expr *expr)
2004 int format;
2006 if (!p)
2007 return NULL;
2009 format = isl_printer_get_output_format(p);
2010 switch (format) {
2011 case ISL_FORMAT_ISL:
2012 p = print_ast_expr_isl(p, expr);
2013 break;
2014 case ISL_FORMAT_C:
2015 p = print_ast_expr_c(p, expr);
2016 break;
2017 default:
2018 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2019 "output format not supported for ast_expr",
2020 return isl_printer_free(p));
2023 return p;
2026 static __isl_give isl_printer *print_ast_node_isl(__isl_take isl_printer *p,
2027 __isl_keep isl_ast_node *node);
2029 /* Print a YAML sequence containing the entries in "list" to "p".
2031 static __isl_give isl_printer *print_ast_node_list(__isl_take isl_printer *p,
2032 __isl_keep isl_ast_node_list *list)
2034 int i;
2035 isl_size n;
2037 n = isl_ast_node_list_n_ast_node(list);
2038 if (n < 0)
2039 return isl_printer_free(p);
2041 p = isl_printer_yaml_start_sequence(p);
2042 for (i = 0; i < n; ++i) {
2043 isl_ast_node *node;
2045 node = isl_ast_node_list_get_ast_node(list, i);
2046 p = print_ast_node_isl(p, node);
2047 isl_ast_node_free(node);
2048 p = isl_printer_yaml_next(p);
2050 p = isl_printer_yaml_end_sequence(p);
2052 return p;
2055 /* Print "node" to "p" in "isl format".
2057 * In particular, print the isl_ast_node as a YAML document.
2059 static __isl_give isl_printer *print_ast_node_isl(__isl_take isl_printer *p,
2060 __isl_keep isl_ast_node *node)
2062 switch (node->type) {
2063 case isl_ast_node_for:
2064 p = isl_printer_yaml_start_mapping(p);
2065 p = isl_printer_print_str(p, "iterator");
2066 p = isl_printer_yaml_next(p);
2067 p = isl_printer_print_ast_expr(p, node->u.f.iterator);
2068 p = isl_printer_yaml_next(p);
2069 if (node->u.f.degenerate) {
2070 p = isl_printer_print_str(p, "value");
2071 p = isl_printer_yaml_next(p);
2072 p = isl_printer_print_ast_expr(p, node->u.f.init);
2073 p = isl_printer_yaml_next(p);
2074 } else {
2075 p = isl_printer_print_str(p, "init");
2076 p = isl_printer_yaml_next(p);
2077 p = isl_printer_print_ast_expr(p, node->u.f.init);
2078 p = isl_printer_yaml_next(p);
2079 p = isl_printer_print_str(p, "cond");
2080 p = isl_printer_yaml_next(p);
2081 p = isl_printer_print_ast_expr(p, node->u.f.cond);
2082 p = isl_printer_yaml_next(p);
2083 p = isl_printer_print_str(p, "inc");
2084 p = isl_printer_yaml_next(p);
2085 p = isl_printer_print_ast_expr(p, node->u.f.inc);
2086 p = isl_printer_yaml_next(p);
2088 if (node->u.f.body) {
2089 p = isl_printer_print_str(p, "body");
2090 p = isl_printer_yaml_next(p);
2091 p = isl_printer_print_ast_node(p, node->u.f.body);
2092 p = isl_printer_yaml_next(p);
2094 p = isl_printer_yaml_end_mapping(p);
2095 break;
2096 case isl_ast_node_mark:
2097 p = isl_printer_yaml_start_mapping(p);
2098 p = isl_printer_print_str(p, "mark");
2099 p = isl_printer_yaml_next(p);
2100 p = isl_printer_print_id(p, node->u.m.mark);
2101 p = isl_printer_yaml_next(p);
2102 p = isl_printer_print_str(p, "node");
2103 p = isl_printer_yaml_next(p);
2104 p = isl_printer_print_ast_node(p, node->u.m.node);
2105 p = isl_printer_yaml_end_mapping(p);
2106 break;
2107 case isl_ast_node_user:
2108 p = isl_printer_yaml_start_mapping(p);
2109 p = isl_printer_print_str(p, "user");
2110 p = isl_printer_yaml_next(p);
2111 p = isl_printer_print_ast_expr(p, node->u.e.expr);
2112 p = isl_printer_yaml_end_mapping(p);
2113 break;
2114 case isl_ast_node_if:
2115 p = isl_printer_yaml_start_mapping(p);
2116 p = isl_printer_print_str(p, "guard");
2117 p = isl_printer_yaml_next(p);
2118 p = isl_printer_print_ast_expr(p, node->u.i.guard);
2119 p = isl_printer_yaml_next(p);
2120 if (node->u.i.then) {
2121 p = isl_printer_print_str(p, "then");
2122 p = isl_printer_yaml_next(p);
2123 p = isl_printer_print_ast_node(p, node->u.i.then);
2124 p = isl_printer_yaml_next(p);
2126 if (node->u.i.else_node) {
2127 p = isl_printer_print_str(p, "else");
2128 p = isl_printer_yaml_next(p);
2129 p = isl_printer_print_ast_node(p, node->u.i.else_node);
2131 p = isl_printer_yaml_end_mapping(p);
2132 break;
2133 case isl_ast_node_block:
2134 p = print_ast_node_list(p, node->u.b.children);
2135 break;
2136 case isl_ast_node_error:
2137 break;
2139 return p;
2142 /* Do we need to print a block around the body "node" of a for or if node?
2144 * If the node is a block, then we need to print a block.
2145 * Also if the node is a degenerate for then we will print it as
2146 * an assignment followed by the body of the for loop, so we need a block
2147 * as well.
2148 * If the node is an if node with an else, then we print a block
2149 * to avoid spurious dangling else warnings emitted by some compilers.
2150 * If the node is a mark, then in principle, we would have to check
2151 * the child of the mark node. However, even if the child would not
2152 * require us to print a block, for readability it is probably best
2153 * to print a block anyway.
2154 * If the ast_always_print_block option has been set, then we print a block.
2156 static int need_block(__isl_keep isl_ast_node *node)
2158 isl_ctx *ctx;
2160 if (node->type == isl_ast_node_block)
2161 return 1;
2162 if (node->type == isl_ast_node_for && node->u.f.degenerate)
2163 return 1;
2164 if (node->type == isl_ast_node_if && node->u.i.else_node)
2165 return 1;
2166 if (node->type == isl_ast_node_mark)
2167 return 1;
2169 ctx = isl_ast_node_get_ctx(node);
2170 return isl_options_get_ast_always_print_block(ctx);
2173 static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
2174 __isl_keep isl_ast_node *node,
2175 __isl_keep isl_ast_print_options *options, int in_block, int in_list);
2176 static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
2177 __isl_keep isl_ast_node *node,
2178 __isl_keep isl_ast_print_options *options, int new_line,
2179 int force_block);
2181 /* Print the body "node" of a for or if node.
2182 * If "else_node" is set, then it is printed as well.
2183 * If "force_block" is set, then print out the body as a block.
2185 * We first check if we need to print out a block.
2186 * We always print out a block if there is an else node to make
2187 * sure that the else node is matched to the correct if node.
2188 * For consistency, the corresponding else node is also printed as a block.
2190 * If the else node is itself an if, then we print it as
2192 * } else if (..) {
2195 * Otherwise the else node is printed as
2197 * } else {
2198 * node
2201 static __isl_give isl_printer *print_body_c(__isl_take isl_printer *p,
2202 __isl_keep isl_ast_node *node, __isl_keep isl_ast_node *else_node,
2203 __isl_keep isl_ast_print_options *options, int force_block)
2205 if (!node)
2206 return isl_printer_free(p);
2208 if (!force_block && !else_node && !need_block(node)) {
2209 p = isl_printer_end_line(p);
2210 p = isl_printer_indent(p, 2);
2211 p = isl_ast_node_print(node, p,
2212 isl_ast_print_options_copy(options));
2213 p = isl_printer_indent(p, -2);
2214 return p;
2217 p = isl_printer_print_str(p, " {");
2218 p = isl_printer_end_line(p);
2219 p = isl_printer_indent(p, 2);
2220 p = print_ast_node_c(p, node, options, 1, 0);
2221 p = isl_printer_indent(p, -2);
2222 p = isl_printer_start_line(p);
2223 p = isl_printer_print_str(p, "}");
2224 if (else_node) {
2225 if (else_node->type == isl_ast_node_if) {
2226 p = isl_printer_print_str(p, " else ");
2227 p = print_if_c(p, else_node, options, 0, 1);
2228 } else {
2229 p = isl_printer_print_str(p, " else");
2230 p = print_body_c(p, else_node, NULL, options, 1);
2232 } else
2233 p = isl_printer_end_line(p);
2235 return p;
2238 /* Print the start of a compound statement.
2240 static __isl_give isl_printer *start_block(__isl_take isl_printer *p)
2242 p = isl_printer_start_line(p);
2243 p = isl_printer_print_str(p, "{");
2244 p = isl_printer_end_line(p);
2245 p = isl_printer_indent(p, 2);
2247 return p;
2250 /* Print the end of a compound statement.
2252 static __isl_give isl_printer *end_block(__isl_take isl_printer *p)
2254 p = isl_printer_indent(p, -2);
2255 p = isl_printer_start_line(p);
2256 p = isl_printer_print_str(p, "}");
2257 p = isl_printer_end_line(p);
2259 return p;
2262 /* Print the for node "node".
2264 * If the for node is degenerate, it is printed as
2266 * type iterator = init;
2267 * body
2269 * Otherwise, it is printed as
2271 * for (type iterator = init; cond; iterator += inc)
2272 * body
2274 * "in_block" is set if we are currently inside a block.
2275 * "in_list" is set if the current node is not alone in the block.
2276 * If we are not in a block or if the current not is not alone in the block
2277 * then we print a block around a degenerate for loop such that the variable
2278 * declaration will not conflict with any potential other declaration
2279 * of the same variable.
2281 static __isl_give isl_printer *print_for_c(__isl_take isl_printer *p,
2282 __isl_keep isl_ast_node *node,
2283 __isl_keep isl_ast_print_options *options, int in_block, int in_list)
2285 isl_id *id;
2286 const char *name;
2287 const char *type;
2289 type = isl_options_get_ast_iterator_type(isl_printer_get_ctx(p));
2290 if (!node->u.f.degenerate) {
2291 id = isl_ast_expr_get_id(node->u.f.iterator);
2292 name = isl_id_get_name(id);
2293 isl_id_free(id);
2294 p = isl_printer_start_line(p);
2295 p = isl_printer_print_str(p, "for (");
2296 p = isl_printer_print_str(p, type);
2297 p = isl_printer_print_str(p, " ");
2298 p = isl_printer_print_str(p, name);
2299 p = isl_printer_print_str(p, " = ");
2300 p = isl_printer_print_ast_expr(p, node->u.f.init);
2301 p = isl_printer_print_str(p, "; ");
2302 p = isl_printer_print_ast_expr(p, node->u.f.cond);
2303 p = isl_printer_print_str(p, "; ");
2304 p = isl_printer_print_str(p, name);
2305 p = isl_printer_print_str(p, " += ");
2306 p = isl_printer_print_ast_expr(p, node->u.f.inc);
2307 p = isl_printer_print_str(p, ")");
2308 p = print_body_c(p, node->u.f.body, NULL, options, 0);
2309 } else {
2310 id = isl_ast_expr_get_id(node->u.f.iterator);
2311 name = isl_id_get_name(id);
2312 isl_id_free(id);
2313 if (!in_block || in_list)
2314 p = start_block(p);
2315 p = isl_printer_start_line(p);
2316 p = isl_printer_print_str(p, type);
2317 p = isl_printer_print_str(p, " ");
2318 p = isl_printer_print_str(p, name);
2319 p = isl_printer_print_str(p, " = ");
2320 p = isl_printer_print_ast_expr(p, node->u.f.init);
2321 p = isl_printer_print_str(p, ";");
2322 p = isl_printer_end_line(p);
2323 p = print_ast_node_c(p, node->u.f.body, options, 1, 0);
2324 if (!in_block || in_list)
2325 p = end_block(p);
2328 return p;
2331 /* Print the if node "node".
2332 * If "new_line" is set then the if node should be printed on a new line.
2333 * If "force_block" is set, then print out the body as a block.
2335 static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
2336 __isl_keep isl_ast_node *node,
2337 __isl_keep isl_ast_print_options *options, int new_line,
2338 int force_block)
2340 if (new_line)
2341 p = isl_printer_start_line(p);
2342 p = isl_printer_print_str(p, "if (");
2343 p = isl_printer_print_ast_expr(p, node->u.i.guard);
2344 p = isl_printer_print_str(p, ")");
2345 p = print_body_c(p, node->u.i.then, node->u.i.else_node, options,
2346 force_block);
2348 return p;
2351 /* Print the "node" to "p".
2353 * "in_block" is set if we are currently inside a block.
2354 * If so, we do not print a block around the children of a block node.
2355 * We do this to avoid an extra block around the body of a degenerate
2356 * for node.
2358 * "in_list" is set if the current node is not alone in the block.
2360 static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
2361 __isl_keep isl_ast_node *node,
2362 __isl_keep isl_ast_print_options *options, int in_block, int in_list)
2364 switch (node->type) {
2365 case isl_ast_node_for:
2366 if (options->print_for)
2367 return options->print_for(p,
2368 isl_ast_print_options_copy(options),
2369 node, options->print_for_user);
2370 p = print_for_c(p, node, options, in_block, in_list);
2371 break;
2372 case isl_ast_node_if:
2373 p = print_if_c(p, node, options, 1, 0);
2374 break;
2375 case isl_ast_node_block:
2376 if (!in_block)
2377 p = start_block(p);
2378 p = isl_ast_node_list_print(node->u.b.children, p, options);
2379 if (!in_block)
2380 p = end_block(p);
2381 break;
2382 case isl_ast_node_mark:
2383 p = isl_printer_start_line(p);
2384 p = isl_printer_print_str(p, "// ");
2385 p = isl_printer_print_str(p, isl_id_get_name(node->u.m.mark));
2386 p = isl_printer_end_line(p);
2387 p = print_ast_node_c(p, node->u.m.node, options, 0, in_list);
2388 break;
2389 case isl_ast_node_user:
2390 if (options->print_user)
2391 return options->print_user(p,
2392 isl_ast_print_options_copy(options),
2393 node, options->print_user_user);
2394 p = isl_printer_start_line(p);
2395 p = isl_printer_print_ast_expr(p, node->u.e.expr);
2396 p = isl_printer_print_str(p, ";");
2397 p = isl_printer_end_line(p);
2398 break;
2399 case isl_ast_node_error:
2400 break;
2402 return p;
2405 /* Print the for node "node" to "p".
2407 __isl_give isl_printer *isl_ast_node_for_print(__isl_keep isl_ast_node *node,
2408 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
2410 if (!node || !options)
2411 goto error;
2412 if (node->type != isl_ast_node_for)
2413 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
2414 "not a for node", goto error);
2415 p = print_for_c(p, node, options, 0, 0);
2416 isl_ast_print_options_free(options);
2417 return p;
2418 error:
2419 isl_ast_print_options_free(options);
2420 isl_printer_free(p);
2421 return NULL;
2424 /* Print the if node "node" to "p".
2426 __isl_give isl_printer *isl_ast_node_if_print(__isl_keep isl_ast_node *node,
2427 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
2429 if (!node || !options)
2430 goto error;
2431 if (node->type != isl_ast_node_if)
2432 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
2433 "not an if node", goto error);
2434 p = print_if_c(p, node, options, 1, 0);
2435 isl_ast_print_options_free(options);
2436 return p;
2437 error:
2438 isl_ast_print_options_free(options);
2439 isl_printer_free(p);
2440 return NULL;
2443 /* Print "node" to "p".
2445 __isl_give isl_printer *isl_ast_node_print(__isl_keep isl_ast_node *node,
2446 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
2448 if (!options || !node)
2449 goto error;
2450 p = print_ast_node_c(p, node, options, 0, 0);
2451 isl_ast_print_options_free(options);
2452 return p;
2453 error:
2454 isl_ast_print_options_free(options);
2455 isl_printer_free(p);
2456 return NULL;
2459 /* Print "node" to "p".
2461 __isl_give isl_printer *isl_printer_print_ast_node(__isl_take isl_printer *p,
2462 __isl_keep isl_ast_node *node)
2464 int format;
2465 isl_ast_print_options *options;
2467 if (!p)
2468 return NULL;
2470 format = isl_printer_get_output_format(p);
2471 switch (format) {
2472 case ISL_FORMAT_ISL:
2473 p = print_ast_node_isl(p, node);
2474 break;
2475 case ISL_FORMAT_C:
2476 options = isl_ast_print_options_alloc(isl_printer_get_ctx(p));
2477 p = isl_ast_node_print(node, p, options);
2478 break;
2479 default:
2480 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2481 "output format not supported for ast_node",
2482 return isl_printer_free(p));
2485 return p;
2488 /* Print the list of nodes "list" to "p".
2490 __isl_give isl_printer *isl_ast_node_list_print(
2491 __isl_keep isl_ast_node_list *list, __isl_take isl_printer *p,
2492 __isl_keep isl_ast_print_options *options)
2494 int i;
2496 if (!p || !list || !options)
2497 return isl_printer_free(p);
2499 for (i = 0; i < list->n; ++i)
2500 p = print_ast_node_c(p, list->p[i], options, 1, 1);
2502 return p;
2505 #define ISL_AST_MACRO_FLOORD (1 << 0)
2506 #define ISL_AST_MACRO_MIN (1 << 1)
2507 #define ISL_AST_MACRO_MAX (1 << 2)
2508 #define ISL_AST_MACRO_ALL (ISL_AST_MACRO_FLOORD | \
2509 ISL_AST_MACRO_MIN | \
2510 ISL_AST_MACRO_MAX)
2512 /* If "expr" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
2513 * then set the corresponding bit in "macros".
2515 static int ast_expr_required_macros(__isl_keep isl_ast_expr *expr, int macros)
2517 int i;
2519 if (macros == ISL_AST_MACRO_ALL)
2520 return macros;
2522 if (expr->type != isl_ast_expr_op)
2523 return macros;
2525 if (expr->u.op.op == isl_ast_op_min)
2526 macros |= ISL_AST_MACRO_MIN;
2527 if (expr->u.op.op == isl_ast_op_max)
2528 macros |= ISL_AST_MACRO_MAX;
2529 if (expr->u.op.op == isl_ast_op_fdiv_q)
2530 macros |= ISL_AST_MACRO_FLOORD;
2532 for (i = 0; i < expr->u.op.n_arg; ++i)
2533 macros = ast_expr_required_macros(expr->u.op.args[i], macros);
2535 return macros;
2538 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
2539 int macros);
2541 /* If "node" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
2542 * then set the corresponding bit in "macros".
2544 static int ast_node_required_macros(__isl_keep isl_ast_node *node, int macros)
2546 if (macros == ISL_AST_MACRO_ALL)
2547 return macros;
2549 switch (node->type) {
2550 case isl_ast_node_for:
2551 macros = ast_expr_required_macros(node->u.f.init, macros);
2552 if (!node->u.f.degenerate) {
2553 macros = ast_expr_required_macros(node->u.f.cond,
2554 macros);
2555 macros = ast_expr_required_macros(node->u.f.inc,
2556 macros);
2558 macros = ast_node_required_macros(node->u.f.body, macros);
2559 break;
2560 case isl_ast_node_if:
2561 macros = ast_expr_required_macros(node->u.i.guard, macros);
2562 macros = ast_node_required_macros(node->u.i.then, macros);
2563 if (node->u.i.else_node)
2564 macros = ast_node_required_macros(node->u.i.else_node,
2565 macros);
2566 break;
2567 case isl_ast_node_block:
2568 macros = ast_node_list_required_macros(node->u.b.children,
2569 macros);
2570 break;
2571 case isl_ast_node_mark:
2572 macros = ast_node_required_macros(node->u.m.node, macros);
2573 break;
2574 case isl_ast_node_user:
2575 macros = ast_expr_required_macros(node->u.e.expr, macros);
2576 break;
2577 case isl_ast_node_error:
2578 break;
2581 return macros;
2584 /* If "list" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
2585 * then set the corresponding bit in "macros".
2587 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
2588 int macros)
2590 int i;
2592 for (i = 0; i < list->n; ++i)
2593 macros = ast_node_required_macros(list->p[i], macros);
2595 return macros;
2598 /* Data structure for keeping track of whether a macro definition
2599 * for a given type has already been printed.
2600 * The value is zero if no definition has been printed and non-zero otherwise.
2602 struct isl_ast_op_printed {
2603 char printed[isl_ast_op_last + 1];
2606 /* Create an empty struct isl_ast_op_printed.
2608 static void *create_printed(isl_ctx *ctx)
2610 return isl_calloc_type(ctx, struct isl_ast_op_printed);
2613 /* Free a struct isl_ast_op_printed.
2615 static void free_printed(void *user)
2617 free(user);
2620 /* Ensure that "p" has an isl_ast_op_printed note identified by "id".
2622 static __isl_give isl_printer *alloc_printed(__isl_take isl_printer *p,
2623 __isl_keep isl_id *id)
2625 return alloc_note(p, id, &create_printed, &free_printed);
2628 /* Create an identifier that is used to store
2629 * an isl_ast_op_printed note.
2631 static __isl_give isl_id *printed_id(isl_ctx *ctx)
2633 return isl_id_alloc(ctx, "isl_ast_op_type_printed", NULL);
2636 /* Did the user specify that a macro definition should only be
2637 * printed once and has a macro definition for "type" already
2638 * been printed to "p"?
2639 * If definitions should only be printed once, but a definition
2640 * for "p" has not yet been printed, then mark it as having been
2641 * printed so that it will not printed again.
2642 * The actual printing is taken care of by the caller.
2644 static isl_bool already_printed_once(__isl_keep isl_printer *p,
2645 enum isl_ast_op_type type)
2647 isl_ctx *ctx;
2648 isl_id *id;
2649 struct isl_ast_op_printed *printed;
2651 if (!p)
2652 return isl_bool_error;
2654 ctx = isl_printer_get_ctx(p);
2655 if (!isl_options_get_ast_print_macro_once(ctx))
2656 return isl_bool_false;
2658 if (type > isl_ast_op_last)
2659 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
2660 "invalid type", return isl_bool_error);
2662 id = printed_id(isl_printer_get_ctx(p));
2663 p = alloc_printed(p, id);
2664 printed = get_note(p, id);
2665 isl_id_free(id);
2666 if (!printed)
2667 return isl_bool_error;
2669 if (printed->printed[type])
2670 return isl_bool_true;
2672 printed->printed[type] = 1;
2673 return isl_bool_false;
2676 /* Print a macro definition for the operator "type".
2678 * If the user has specified that a macro definition should
2679 * only be printed once to any given printer and if the macro definition
2680 * has already been printed to "p", then do not print the definition.
2682 __isl_give isl_printer *isl_ast_op_type_print_macro(
2683 enum isl_ast_op_type type, __isl_take isl_printer *p)
2685 isl_bool skip;
2687 skip = already_printed_once(p, type);
2688 if (skip < 0)
2689 return isl_printer_free(p);
2690 if (skip)
2691 return p;
2693 switch (type) {
2694 case isl_ast_op_min:
2695 p = isl_printer_start_line(p);
2696 p = isl_printer_print_str(p, "#define ");
2697 p = isl_printer_print_str(p, get_op_str_c(p, type));
2698 p = isl_printer_print_str(p,
2699 "(x,y) ((x) < (y) ? (x) : (y))");
2700 p = isl_printer_end_line(p);
2701 break;
2702 case isl_ast_op_max:
2703 p = isl_printer_start_line(p);
2704 p = isl_printer_print_str(p, "#define ");
2705 p = isl_printer_print_str(p, get_op_str_c(p, type));
2706 p = isl_printer_print_str(p,
2707 "(x,y) ((x) > (y) ? (x) : (y))");
2708 p = isl_printer_end_line(p);
2709 break;
2710 case isl_ast_op_fdiv_q:
2711 p = isl_printer_start_line(p);
2712 p = isl_printer_print_str(p, "#define ");
2713 p = isl_printer_print_str(p, get_op_str_c(p, type));
2714 p = isl_printer_print_str(p,
2715 "(n,d) "
2716 "(((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))");
2717 p = isl_printer_end_line(p);
2718 break;
2719 default:
2720 break;
2723 return p;
2726 /* Call "fn" for each type of operation represented in the "macros"
2727 * bit vector.
2729 static isl_stat foreach_ast_op_type(int macros,
2730 isl_stat (*fn)(enum isl_ast_op_type type, void *user), void *user)
2732 if (macros & ISL_AST_MACRO_MIN && fn(isl_ast_op_min, user) < 0)
2733 return isl_stat_error;
2734 if (macros & ISL_AST_MACRO_MAX && fn(isl_ast_op_max, user) < 0)
2735 return isl_stat_error;
2736 if (macros & ISL_AST_MACRO_FLOORD && fn(isl_ast_op_fdiv_q, user) < 0)
2737 return isl_stat_error;
2739 return isl_stat_ok;
2742 /* Call "fn" for each type of operation that appears in "expr"
2743 * and that requires a macro definition.
2745 isl_stat isl_ast_expr_foreach_ast_op_type(__isl_keep isl_ast_expr *expr,
2746 isl_stat (*fn)(enum isl_ast_op_type type, void *user), void *user)
2748 int macros;
2750 if (!expr)
2751 return isl_stat_error;
2753 macros = ast_expr_required_macros(expr, 0);
2754 return foreach_ast_op_type(macros, fn, user);
2757 /* Call "fn" for each type of operation that appears in "node"
2758 * and that requires a macro definition.
2760 isl_stat isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node *node,
2761 isl_stat (*fn)(enum isl_ast_op_type type, void *user), void *user)
2763 int macros;
2765 if (!node)
2766 return isl_stat_error;
2768 macros = ast_node_required_macros(node, 0);
2769 return foreach_ast_op_type(macros, fn, user);
2772 static isl_stat ast_op_type_print_macro(enum isl_ast_op_type type, void *user)
2774 isl_printer **p = user;
2776 *p = isl_ast_op_type_print_macro(type, *p);
2778 return isl_stat_ok;
2781 /* Print macro definitions for all the macros used in the result
2782 * of printing "expr".
2784 __isl_give isl_printer *isl_ast_expr_print_macros(
2785 __isl_keep isl_ast_expr *expr, __isl_take isl_printer *p)
2787 if (isl_ast_expr_foreach_ast_op_type(expr,
2788 &ast_op_type_print_macro, &p) < 0)
2789 return isl_printer_free(p);
2790 return p;
2793 /* Print macro definitions for all the macros used in the result
2794 * of printing "node".
2796 __isl_give isl_printer *isl_ast_node_print_macros(
2797 __isl_keep isl_ast_node *node, __isl_take isl_printer *p)
2799 if (isl_ast_node_foreach_ast_op_type(node,
2800 &ast_op_type_print_macro, &p) < 0)
2801 return isl_printer_free(p);
2802 return p;
2805 /* Return a string containing C code representing this isl_ast_expr.
2807 __isl_give char *isl_ast_expr_to_C_str(__isl_keep isl_ast_expr *expr)
2809 isl_printer *p;
2810 char *str;
2812 if (!expr)
2813 return NULL;
2815 p = isl_printer_to_str(isl_ast_expr_get_ctx(expr));
2816 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
2817 p = isl_printer_print_ast_expr(p, expr);
2819 str = isl_printer_get_str(p);
2821 isl_printer_free(p);
2823 return str;
2826 /* Return a string containing C code representing this isl_ast_node.
2828 __isl_give char *isl_ast_node_to_C_str(__isl_keep isl_ast_node *node)
2830 isl_printer *p;
2831 char *str;
2833 if (!node)
2834 return NULL;
2836 p = isl_printer_to_str(isl_ast_node_get_ctx(node));
2837 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
2838 p = isl_printer_print_ast_node(p, node);
2840 str = isl_printer_get_str(p);
2842 isl_printer_free(p);
2844 return str;