Merge branch 'maint'
[isl.git] / isl_ast.c
blob63fc24311ba9443e0be334b5806d0a3d2d5ecd7b
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 <isl_ast_private.h>
12 #undef BASE
13 #define BASE ast_expr
15 #include <isl_list_templ.c>
17 #undef BASE
18 #define BASE ast_node
20 #include <isl_list_templ.c>
22 isl_ctx *isl_ast_print_options_get_ctx(
23 __isl_keep isl_ast_print_options *options)
25 return options ? options->ctx : NULL;
28 __isl_give isl_ast_print_options *isl_ast_print_options_alloc(isl_ctx *ctx)
30 isl_ast_print_options *options;
32 options = isl_calloc_type(ctx, isl_ast_print_options);
33 if (!options)
34 return NULL;
36 options->ctx = ctx;
37 isl_ctx_ref(ctx);
38 options->ref = 1;
40 return options;
43 __isl_give isl_ast_print_options *isl_ast_print_options_dup(
44 __isl_keep isl_ast_print_options *options)
46 isl_ctx *ctx;
47 isl_ast_print_options *dup;
49 if (!options)
50 return NULL;
52 ctx = isl_ast_print_options_get_ctx(options);
53 dup = isl_ast_print_options_alloc(ctx);
54 if (!dup)
55 return NULL;
57 dup->print_for = options->print_for;
58 dup->print_for_user = options->print_for_user;
59 dup->print_user = options->print_user;
60 dup->print_user_user = options->print_user_user;
62 return dup;
65 __isl_give isl_ast_print_options *isl_ast_print_options_cow(
66 __isl_take isl_ast_print_options *options)
68 if (!options)
69 return NULL;
71 if (options->ref == 1)
72 return options;
73 options->ref--;
74 return isl_ast_print_options_dup(options);
77 __isl_give isl_ast_print_options *isl_ast_print_options_copy(
78 __isl_keep isl_ast_print_options *options)
80 if (!options)
81 return NULL;
83 options->ref++;
84 return options;
87 void *isl_ast_print_options_free(__isl_take isl_ast_print_options *options)
89 if (!options)
90 return NULL;
92 if (--options->ref > 0)
93 return NULL;
95 isl_ctx_deref(options->ctx);
97 free(options);
98 return NULL;
101 /* Set the print_user callback of "options" to "print_user".
103 * If this callback is set, then it used to print user nodes in the AST.
104 * Otherwise, the expression associated to the user node is printed.
106 __isl_give isl_ast_print_options *isl_ast_print_options_set_print_user(
107 __isl_take isl_ast_print_options *options,
108 __isl_give isl_printer *(*print_user)(__isl_take isl_printer *p,
109 __isl_take isl_ast_print_options *options,
110 __isl_keep isl_ast_node *node, void *user),
111 void *user)
113 options = isl_ast_print_options_cow(options);
114 if (!options)
115 return NULL;
117 options->print_user = print_user;
118 options->print_user_user = user;
120 return options;
123 /* Set the print_for callback of "options" to "print_for".
125 * If this callback is set, then it used to print for nodes in the AST.
127 __isl_give isl_ast_print_options *isl_ast_print_options_set_print_for(
128 __isl_take isl_ast_print_options *options,
129 __isl_give isl_printer *(*print_for)(__isl_take isl_printer *p,
130 __isl_take isl_ast_print_options *options,
131 __isl_keep isl_ast_node *node, void *user),
132 void *user)
134 options = isl_ast_print_options_cow(options);
135 if (!options)
136 return NULL;
138 options->print_for = print_for;
139 options->print_for_user = user;
141 return options;
144 __isl_give isl_ast_expr *isl_ast_expr_copy(__isl_keep isl_ast_expr *expr)
146 if (!expr)
147 return NULL;
149 expr->ref++;
150 return expr;
153 __isl_give isl_ast_expr *isl_ast_expr_dup(__isl_keep isl_ast_expr *expr)
155 int i;
156 isl_ctx *ctx;
157 isl_ast_expr *dup;
159 if (!expr)
160 return NULL;
162 ctx = isl_ast_expr_get_ctx(expr);
163 switch (expr->type) {
164 case isl_ast_expr_int:
165 dup = isl_ast_expr_from_val(isl_val_copy(expr->u.v));
166 break;
167 case isl_ast_expr_id:
168 dup = isl_ast_expr_from_id(isl_id_copy(expr->u.id));
169 break;
170 case isl_ast_expr_op:
171 dup = isl_ast_expr_alloc_op(ctx,
172 expr->u.op.op, expr->u.op.n_arg);
173 if (!dup)
174 return NULL;
175 for (i = 0; i < expr->u.op.n_arg; ++i)
176 dup->u.op.args[i] =
177 isl_ast_expr_copy(expr->u.op.args[i]);
178 break;
179 case isl_ast_expr_error:
180 dup = NULL;
183 if (!dup)
184 return NULL;
186 return dup;
189 __isl_give isl_ast_expr *isl_ast_expr_cow(__isl_take isl_ast_expr *expr)
191 if (!expr)
192 return NULL;
194 if (expr->ref == 1)
195 return expr;
196 expr->ref--;
197 return isl_ast_expr_dup(expr);
200 void *isl_ast_expr_free(__isl_take isl_ast_expr *expr)
202 int i;
204 if (!expr)
205 return NULL;
207 if (--expr->ref > 0)
208 return NULL;
210 isl_ctx_deref(expr->ctx);
212 switch (expr->type) {
213 case isl_ast_expr_int:
214 isl_val_free(expr->u.v);
215 break;
216 case isl_ast_expr_id:
217 isl_id_free(expr->u.id);
218 break;
219 case isl_ast_expr_op:
220 for (i = 0; i < expr->u.op.n_arg; ++i)
221 isl_ast_expr_free(expr->u.op.args[i]);
222 free(expr->u.op.args);
223 break;
224 case isl_ast_expr_error:
225 break;
228 free(expr);
229 return NULL;
232 isl_ctx *isl_ast_expr_get_ctx(__isl_keep isl_ast_expr *expr)
234 return expr ? expr->ctx : NULL;
237 enum isl_ast_expr_type isl_ast_expr_get_type(__isl_keep isl_ast_expr *expr)
239 return expr ? expr->type : isl_ast_expr_error;
242 /* Return the integer value represented by "expr".
244 __isl_give isl_val *isl_ast_expr_get_val(__isl_keep isl_ast_expr *expr)
246 if (!expr)
247 return NULL;
248 if (expr->type != isl_ast_expr_int)
249 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
250 "expression not an int", return NULL);
251 return isl_val_copy(expr->u.v);
254 __isl_give isl_id *isl_ast_expr_get_id(__isl_keep isl_ast_expr *expr)
256 if (!expr)
257 return NULL;
258 if (expr->type != isl_ast_expr_id)
259 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
260 "expression not an identifier", return NULL);
262 return isl_id_copy(expr->u.id);
265 enum isl_ast_op_type isl_ast_expr_get_op_type(__isl_keep isl_ast_expr *expr)
267 if (!expr)
268 return isl_ast_op_error;
269 if (expr->type != isl_ast_expr_op)
270 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
271 "expression not an operation", return isl_ast_op_error);
272 return expr->u.op.op;
275 int isl_ast_expr_get_op_n_arg(__isl_keep isl_ast_expr *expr)
277 if (!expr)
278 return -1;
279 if (expr->type != isl_ast_expr_op)
280 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
281 "expression not an operation", return -1);
282 return expr->u.op.n_arg;
285 __isl_give isl_ast_expr *isl_ast_expr_get_op_arg(__isl_keep isl_ast_expr *expr,
286 int pos)
288 if (!expr)
289 return NULL;
290 if (expr->type != isl_ast_expr_op)
291 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
292 "expression not an operation", return NULL);
293 if (pos < 0 || pos >= expr->u.op.n_arg)
294 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
295 "index out of bounds", return NULL);
297 return isl_ast_expr_copy(expr->u.op.args[pos]);
300 /* Replace the argument at position "pos" of "expr" by "arg".
302 __isl_give isl_ast_expr *isl_ast_expr_set_op_arg(__isl_take isl_ast_expr *expr,
303 int pos, __isl_take isl_ast_expr *arg)
305 expr = isl_ast_expr_cow(expr);
306 if (!expr || !arg)
307 goto error;
308 if (expr->type != isl_ast_expr_op)
309 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
310 "expression not an operation", goto error);
311 if (pos < 0 || pos >= expr->u.op.n_arg)
312 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
313 "index out of bounds", goto error);
315 isl_ast_expr_free(expr->u.op.args[pos]);
316 expr->u.op.args[pos] = arg;
318 return expr;
319 error:
320 isl_ast_expr_free(arg);
321 return isl_ast_expr_free(expr);
324 /* Is "expr1" equal to "expr2"?
326 int isl_ast_expr_is_equal(__isl_keep isl_ast_expr *expr1,
327 __isl_keep isl_ast_expr *expr2)
329 int i;
331 if (!expr1 || !expr2)
332 return -1;
334 if (expr1 == expr2)
335 return 1;
336 if (expr1->type != expr2->type)
337 return 0;
338 switch (expr1->type) {
339 case isl_ast_expr_int:
340 return isl_val_eq(expr1->u.v, expr2->u.v);
341 case isl_ast_expr_id:
342 return expr1->u.id == expr2->u.id;
343 case isl_ast_expr_op:
344 if (expr1->u.op.op != expr2->u.op.op)
345 return 0;
346 if (expr1->u.op.n_arg != expr2->u.op.n_arg)
347 return 0;
348 for (i = 0; i < expr1->u.op.n_arg; ++i) {
349 int equal;
350 equal = isl_ast_expr_is_equal(expr1->u.op.args[i],
351 expr2->u.op.args[i]);
352 return 0;
353 if (equal < 0 || !equal)
354 return equal;
356 return 1;
357 case isl_ast_expr_error:
358 return -1;
362 /* Create a new operation expression of operation type "op",
363 * with "n_arg" as yet unspecified arguments.
365 __isl_give isl_ast_expr *isl_ast_expr_alloc_op(isl_ctx *ctx,
366 enum isl_ast_op_type op, int n_arg)
368 isl_ast_expr *expr;
370 expr = isl_calloc_type(ctx, isl_ast_expr);
371 if (!expr)
372 return NULL;
374 expr->ctx = ctx;
375 isl_ctx_ref(ctx);
376 expr->ref = 1;
377 expr->type = isl_ast_expr_op;
378 expr->u.op.op = op;
379 expr->u.op.n_arg = n_arg;
380 expr->u.op.args = isl_calloc_array(ctx, isl_ast_expr *, n_arg);
382 if (n_arg && !expr->u.op.args)
383 return isl_ast_expr_free(expr);
385 return expr;
388 /* Create a new id expression representing "id".
390 __isl_give isl_ast_expr *isl_ast_expr_from_id(__isl_take isl_id *id)
392 isl_ctx *ctx;
393 isl_ast_expr *expr;
395 if (!id)
396 return NULL;
398 ctx = isl_id_get_ctx(id);
399 expr = isl_calloc_type(ctx, isl_ast_expr);
400 if (!expr)
401 return isl_id_free(id);
403 expr->ctx = ctx;
404 isl_ctx_ref(ctx);
405 expr->ref = 1;
406 expr->type = isl_ast_expr_id;
407 expr->u.id = id;
409 return expr;
412 /* Create a new integer expression representing "i".
414 __isl_give isl_ast_expr *isl_ast_expr_alloc_int_si(isl_ctx *ctx, int i)
416 isl_ast_expr *expr;
418 expr = isl_calloc_type(ctx, isl_ast_expr);
419 if (!expr)
420 return NULL;
422 expr->ctx = ctx;
423 isl_ctx_ref(ctx);
424 expr->ref = 1;
425 expr->type = isl_ast_expr_int;
426 expr->u.v = isl_val_int_from_si(ctx, i);
427 if (!expr->u.v)
428 return isl_ast_expr_free(expr);
430 return expr;
433 /* Create a new integer expression representing "v".
435 __isl_give isl_ast_expr *isl_ast_expr_from_val(__isl_take isl_val *v)
437 isl_ctx *ctx;
438 isl_ast_expr *expr;
440 if (!v)
441 return NULL;
442 if (!isl_val_is_int(v))
443 isl_die(isl_val_get_ctx(v), isl_error_invalid,
444 "expecting integer value", return isl_val_free(v));
446 ctx = isl_val_get_ctx(v);
447 expr = isl_calloc_type(ctx, isl_ast_expr);
448 if (!expr)
449 return isl_val_free(v);
451 expr->ctx = ctx;
452 isl_ctx_ref(ctx);
453 expr->ref = 1;
454 expr->type = isl_ast_expr_int;
455 expr->u.v = v;
457 return expr;
460 /* Create an expression representing the negation of "arg".
462 __isl_give isl_ast_expr *isl_ast_expr_neg(__isl_take isl_ast_expr *arg)
464 isl_ctx *ctx;
465 isl_ast_expr *expr = NULL;
467 if (!arg)
468 return NULL;
470 ctx = isl_ast_expr_get_ctx(arg);
471 expr = isl_ast_expr_alloc_op(ctx, isl_ast_op_minus, 1);
472 if (!expr)
473 goto error;
475 expr->u.op.args[0] = arg;
477 return expr;
478 error:
479 isl_ast_expr_free(arg);
480 return NULL;
483 /* Create an expression representing the binary operation "type"
484 * applied to "expr1" and "expr2".
486 __isl_give isl_ast_expr *isl_ast_expr_alloc_binary(enum isl_ast_op_type type,
487 __isl_take isl_ast_expr *expr1, __isl_take isl_ast_expr *expr2)
489 isl_ctx *ctx;
490 isl_ast_expr *expr = NULL;
492 if (!expr1 || !expr2)
493 goto error;
495 ctx = isl_ast_expr_get_ctx(expr1);
496 expr = isl_ast_expr_alloc_op(ctx, type, 2);
497 if (!expr)
498 goto error;
500 expr->u.op.args[0] = expr1;
501 expr->u.op.args[1] = expr2;
503 return expr;
504 error:
505 isl_ast_expr_free(expr1);
506 isl_ast_expr_free(expr2);
507 return NULL;
510 /* Create an expression representing the sum of "expr1" and "expr2".
512 __isl_give isl_ast_expr *isl_ast_expr_add(__isl_take isl_ast_expr *expr1,
513 __isl_take isl_ast_expr *expr2)
515 return isl_ast_expr_alloc_binary(isl_ast_op_add, expr1, expr2);
518 /* Create an expression representing the difference of "expr1" and "expr2".
520 __isl_give isl_ast_expr *isl_ast_expr_sub(__isl_take isl_ast_expr *expr1,
521 __isl_take isl_ast_expr *expr2)
523 return isl_ast_expr_alloc_binary(isl_ast_op_sub, expr1, expr2);
526 /* Create an expression representing the product of "expr1" and "expr2".
528 __isl_give isl_ast_expr *isl_ast_expr_mul(__isl_take isl_ast_expr *expr1,
529 __isl_take isl_ast_expr *expr2)
531 return isl_ast_expr_alloc_binary(isl_ast_op_mul, expr1, expr2);
534 /* Create an expression representing the quotient of "expr1" and "expr2".
536 __isl_give isl_ast_expr *isl_ast_expr_div(__isl_take isl_ast_expr *expr1,
537 __isl_take isl_ast_expr *expr2)
539 return isl_ast_expr_alloc_binary(isl_ast_op_div, expr1, expr2);
542 /* Create an expression representing the conjunction of "expr1" and "expr2".
544 __isl_give isl_ast_expr *isl_ast_expr_and(__isl_take isl_ast_expr *expr1,
545 __isl_take isl_ast_expr *expr2)
547 return isl_ast_expr_alloc_binary(isl_ast_op_and, expr1, expr2);
550 /* Create an expression representing the disjunction of "expr1" and "expr2".
552 __isl_give isl_ast_expr *isl_ast_expr_or(__isl_take isl_ast_expr *expr1,
553 __isl_take isl_ast_expr *expr2)
555 return isl_ast_expr_alloc_binary(isl_ast_op_or, expr1, expr2);
558 /* Create an expression representing an access to "array" with index
559 * expressions "indices".
561 __isl_give isl_ast_expr *isl_ast_expr_access(__isl_take isl_ast_expr *array,
562 __isl_take isl_ast_expr_list *indices)
564 int i, n;
565 isl_ctx *ctx;
566 isl_ast_expr *access = NULL;
568 if (!array || !indices)
569 goto error;
571 ctx = isl_ast_expr_get_ctx(array);
572 n = isl_ast_expr_list_n_ast_expr(indices);
573 access = isl_ast_expr_alloc_op(ctx, isl_ast_op_access, 1 + n);
574 if (!access)
575 goto error;
576 for (i = 0; i < n; ++i) {
577 isl_ast_expr *index;
578 index = isl_ast_expr_list_get_ast_expr(indices, i);
579 access->u.op.args[1 + i] = index;
580 if (!index)
581 goto error;
583 access->u.op.args[0] = array;
585 isl_ast_expr_list_free(indices);
586 return access;
587 error:
588 isl_ast_expr_free(array);
589 isl_ast_expr_list_free(indices);
590 isl_ast_expr_free(access);
591 return NULL;
594 /* For each subexpression of "expr" of type isl_ast_expr_id,
595 * if it appears in "id2expr", then replace it by the corresponding
596 * expression.
598 __isl_give isl_ast_expr *isl_ast_expr_substitute_ids(
599 __isl_take isl_ast_expr *expr, __isl_take isl_id_to_ast_expr *id2expr)
601 int i;
602 isl_id *id;
604 if (!expr || !id2expr)
605 goto error;
607 switch (expr->type) {
608 case isl_ast_expr_int:
609 break;
610 case isl_ast_expr_id:
611 if (!isl_id_to_ast_expr_has(id2expr, expr->u.id))
612 break;
613 id = isl_id_copy(expr->u.id);
614 isl_ast_expr_free(expr);
615 expr = isl_id_to_ast_expr_get(id2expr, id);
616 break;
617 case isl_ast_expr_op:
618 for (i = 0; i < expr->u.op.n_arg; ++i) {
619 isl_ast_expr *arg;
620 arg = isl_ast_expr_copy(expr->u.op.args[i]);
621 arg = isl_ast_expr_substitute_ids(arg,
622 isl_id_to_ast_expr_copy(id2expr));
623 if (arg == expr->u.op.args[i]) {
624 isl_ast_expr_free(arg);
625 continue;
627 if (!arg)
628 expr = isl_ast_expr_free(expr);
629 expr = isl_ast_expr_cow(expr);
630 if (!expr) {
631 isl_ast_expr_free(arg);
632 break;
634 isl_ast_expr_free(expr->u.op.args[i]);
635 expr->u.op.args[i] = arg;
637 break;
638 case isl_ast_expr_error:
639 expr = isl_ast_expr_free(expr);
640 break;
643 isl_id_to_ast_expr_free(id2expr);
644 return expr;
645 error:
646 isl_ast_expr_free(expr);
647 isl_id_to_ast_expr_free(id2expr);
648 return NULL;
651 isl_ctx *isl_ast_node_get_ctx(__isl_keep isl_ast_node *node)
653 return node ? node->ctx : NULL;
656 enum isl_ast_node_type isl_ast_node_get_type(__isl_keep isl_ast_node *node)
658 return node ? node->type : isl_ast_node_error;
661 __isl_give isl_ast_node *isl_ast_node_alloc(isl_ctx *ctx,
662 enum isl_ast_node_type type)
664 isl_ast_node *node;
666 node = isl_calloc_type(ctx, isl_ast_node);
667 if (!node)
668 return NULL;
670 node->ctx = ctx;
671 isl_ctx_ref(ctx);
672 node->ref = 1;
673 node->type = type;
675 return node;
678 /* Create an if node with the given guard.
680 * The then body needs to be filled in later.
682 __isl_give isl_ast_node *isl_ast_node_alloc_if(__isl_take isl_ast_expr *guard)
684 isl_ast_node *node;
686 if (!guard)
687 return NULL;
689 node = isl_ast_node_alloc(isl_ast_expr_get_ctx(guard), isl_ast_node_if);
690 if (!node)
691 goto error;
692 node->u.i.guard = guard;
694 return node;
695 error:
696 isl_ast_expr_free(guard);
697 return NULL;
700 /* Create a for node with the given iterator.
702 * The remaining fields need to be filled in later.
704 __isl_give isl_ast_node *isl_ast_node_alloc_for(__isl_take isl_id *id)
706 isl_ast_node *node;
707 isl_ctx *ctx;
709 if (!id)
710 return NULL;
712 ctx = isl_id_get_ctx(id);
713 node = isl_ast_node_alloc(ctx, isl_ast_node_for);
714 if (!node)
715 return NULL;
717 node->u.f.iterator = isl_ast_expr_from_id(id);
718 if (!node->u.f.iterator)
719 return isl_ast_node_free(node);
721 return node;
724 /* Create a user node evaluating "expr".
726 __isl_give isl_ast_node *isl_ast_node_alloc_user(__isl_take isl_ast_expr *expr)
728 isl_ctx *ctx;
729 isl_ast_node *node;
731 if (!expr)
732 return NULL;
734 ctx = isl_ast_expr_get_ctx(expr);
735 node = isl_ast_node_alloc(ctx, isl_ast_node_user);
736 if (!node)
737 goto error;
739 node->u.e.expr = expr;
741 return node;
742 error:
743 isl_ast_expr_free(expr);
744 return NULL;
747 /* Create a block node with the given children.
749 __isl_give isl_ast_node *isl_ast_node_alloc_block(
750 __isl_take isl_ast_node_list *list)
752 isl_ast_node *node;
753 isl_ctx *ctx;
755 if (!list)
756 return NULL;
758 ctx = isl_ast_node_list_get_ctx(list);
759 node = isl_ast_node_alloc(ctx, isl_ast_node_block);
760 if (!node)
761 goto error;
763 node->u.b.children = list;
765 return node;
766 error:
767 isl_ast_node_list_free(list);
768 return NULL;
771 /* Represent the given list of nodes as a single node, either by
772 * extract the node from a single element list or by creating
773 * a block node with the list of nodes as children.
775 __isl_give isl_ast_node *isl_ast_node_from_ast_node_list(
776 __isl_take isl_ast_node_list *list)
778 isl_ast_node *node;
780 if (isl_ast_node_list_n_ast_node(list) != 1)
781 return isl_ast_node_alloc_block(list);
783 node = isl_ast_node_list_get_ast_node(list, 0);
784 isl_ast_node_list_free(list);
786 return node;
789 __isl_give isl_ast_node *isl_ast_node_copy(__isl_keep isl_ast_node *node)
791 if (!node)
792 return NULL;
794 node->ref++;
795 return node;
798 __isl_give isl_ast_node *isl_ast_node_dup(__isl_keep isl_ast_node *node)
800 isl_ast_node *dup;
802 if (!node)
803 return NULL;
805 dup = isl_ast_node_alloc(isl_ast_node_get_ctx(node), node->type);
806 if (!dup)
807 return NULL;
809 switch (node->type) {
810 case isl_ast_node_if:
811 dup->u.i.guard = isl_ast_expr_copy(node->u.i.guard);
812 dup->u.i.then = isl_ast_node_copy(node->u.i.then);
813 dup->u.i.else_node = isl_ast_node_copy(node->u.i.else_node);
814 if (!dup->u.i.guard || !dup->u.i.then ||
815 (node->u.i.else_node && !dup->u.i.else_node))
816 return isl_ast_node_free(dup);
817 break;
818 case isl_ast_node_for:
819 dup->u.f.iterator = isl_ast_expr_copy(node->u.f.iterator);
820 dup->u.f.init = isl_ast_expr_copy(node->u.f.init);
821 dup->u.f.cond = isl_ast_expr_copy(node->u.f.cond);
822 dup->u.f.inc = isl_ast_expr_copy(node->u.f.inc);
823 dup->u.f.body = isl_ast_node_copy(node->u.f.body);
824 if (!dup->u.f.iterator || !dup->u.f.init || !dup->u.f.cond ||
825 !dup->u.f.inc || !dup->u.f.body)
826 return isl_ast_node_free(dup);
827 break;
828 case isl_ast_node_block:
829 dup->u.b.children = isl_ast_node_list_copy(node->u.b.children);
830 if (!dup->u.b.children)
831 return isl_ast_node_free(dup);
832 break;
833 case isl_ast_node_user:
834 dup->u.e.expr = isl_ast_expr_copy(node->u.e.expr);
835 if (!dup->u.e.expr)
836 return isl_ast_node_free(dup);
837 break;
838 case isl_ast_node_error:
839 break;
842 return dup;
845 __isl_give isl_ast_node *isl_ast_node_cow(__isl_take isl_ast_node *node)
847 if (!node)
848 return NULL;
850 if (node->ref == 1)
851 return node;
852 node->ref--;
853 return isl_ast_node_dup(node);
856 void *isl_ast_node_free(__isl_take isl_ast_node *node)
858 if (!node)
859 return NULL;
861 if (--node->ref > 0)
862 return NULL;
864 switch (node->type) {
865 case isl_ast_node_if:
866 isl_ast_expr_free(node->u.i.guard);
867 isl_ast_node_free(node->u.i.then);
868 isl_ast_node_free(node->u.i.else_node);
869 break;
870 case isl_ast_node_for:
871 isl_ast_expr_free(node->u.f.iterator);
872 isl_ast_expr_free(node->u.f.init);
873 isl_ast_expr_free(node->u.f.cond);
874 isl_ast_expr_free(node->u.f.inc);
875 isl_ast_node_free(node->u.f.body);
876 break;
877 case isl_ast_node_block:
878 isl_ast_node_list_free(node->u.b.children);
879 break;
880 case isl_ast_node_user:
881 isl_ast_expr_free(node->u.e.expr);
882 break;
883 case isl_ast_node_error:
884 break;
887 isl_id_free(node->annotation);
888 isl_ctx_deref(node->ctx);
889 free(node);
891 return NULL;
894 /* Replace the body of the for node "node" by "body".
896 __isl_give isl_ast_node *isl_ast_node_for_set_body(
897 __isl_take isl_ast_node *node, __isl_take isl_ast_node *body)
899 node = isl_ast_node_cow(node);
900 if (!node || !body)
901 goto error;
902 if (node->type != isl_ast_node_for)
903 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
904 "not a for node", goto error);
906 isl_ast_node_free(node->u.f.body);
907 node->u.f.body = body;
909 return node;
910 error:
911 isl_ast_node_free(node);
912 isl_ast_node_free(body);
913 return NULL;
916 __isl_give isl_ast_node *isl_ast_node_for_get_body(
917 __isl_keep isl_ast_node *node)
919 if (!node)
920 return NULL;
921 if (node->type != isl_ast_node_for)
922 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
923 "not a for node", return NULL);
924 return isl_ast_node_copy(node->u.f.body);
927 /* Mark the given for node as being degenerate.
929 __isl_give isl_ast_node *isl_ast_node_for_mark_degenerate(
930 __isl_take isl_ast_node *node)
932 node = isl_ast_node_cow(node);
933 if (!node)
934 return NULL;
935 node->u.f.degenerate = 1;
936 return node;
939 int isl_ast_node_for_is_degenerate(__isl_keep isl_ast_node *node)
941 if (!node)
942 return -1;
943 if (node->type != isl_ast_node_for)
944 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
945 "not a for node", return -1);
946 return node->u.f.degenerate;
949 __isl_give isl_ast_expr *isl_ast_node_for_get_iterator(
950 __isl_keep isl_ast_node *node)
952 if (!node)
953 return NULL;
954 if (node->type != isl_ast_node_for)
955 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
956 "not a for node", return NULL);
957 return isl_ast_expr_copy(node->u.f.iterator);
960 __isl_give isl_ast_expr *isl_ast_node_for_get_init(
961 __isl_keep isl_ast_node *node)
963 if (!node)
964 return NULL;
965 if (node->type != isl_ast_node_for)
966 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
967 "not a for node", return NULL);
968 return isl_ast_expr_copy(node->u.f.init);
971 /* Return the condition expression of the given for node.
973 * If the for node is degenerate, then the condition is not explicitly
974 * stored in the node. Instead, it is constructed as
976 * iterator <= init
978 __isl_give isl_ast_expr *isl_ast_node_for_get_cond(
979 __isl_keep isl_ast_node *node)
981 if (!node)
982 return NULL;
983 if (node->type != isl_ast_node_for)
984 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
985 "not a for node", return NULL);
986 if (!node->u.f.degenerate)
987 return isl_ast_expr_copy(node->u.f.cond);
989 return isl_ast_expr_alloc_binary(isl_ast_op_le,
990 isl_ast_expr_copy(node->u.f.iterator),
991 isl_ast_expr_copy(node->u.f.init));
994 /* Return the increment of the given for node.
996 * If the for node is degenerate, then the increment is not explicitly
997 * stored in the node. We simply return "1".
999 __isl_give isl_ast_expr *isl_ast_node_for_get_inc(
1000 __isl_keep isl_ast_node *node)
1002 if (!node)
1003 return NULL;
1004 if (node->type != isl_ast_node_for)
1005 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1006 "not a for node", return NULL);
1007 if (!node->u.f.degenerate)
1008 return isl_ast_expr_copy(node->u.f.inc);
1009 return isl_ast_expr_alloc_int_si(isl_ast_node_get_ctx(node), 1);
1012 /* Replace the then branch of the if node "node" by "child".
1014 __isl_give isl_ast_node *isl_ast_node_if_set_then(
1015 __isl_take isl_ast_node *node, __isl_take isl_ast_node *child)
1017 node = isl_ast_node_cow(node);
1018 if (!node || !child)
1019 goto error;
1020 if (node->type != isl_ast_node_if)
1021 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1022 "not an if node", goto error);
1024 isl_ast_node_free(node->u.i.then);
1025 node->u.i.then = child;
1027 return node;
1028 error:
1029 isl_ast_node_free(node);
1030 isl_ast_node_free(child);
1031 return NULL;
1034 __isl_give isl_ast_node *isl_ast_node_if_get_then(
1035 __isl_keep isl_ast_node *node)
1037 if (!node)
1038 return NULL;
1039 if (node->type != isl_ast_node_if)
1040 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1041 "not an if node", return NULL);
1042 return isl_ast_node_copy(node->u.i.then);
1045 int isl_ast_node_if_has_else(
1046 __isl_keep isl_ast_node *node)
1048 if (!node)
1049 return -1;
1050 if (node->type != isl_ast_node_if)
1051 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1052 "not an if node", return -1);
1053 return node->u.i.else_node != NULL;
1056 __isl_give isl_ast_node *isl_ast_node_if_get_else(
1057 __isl_keep isl_ast_node *node)
1059 if (!node)
1060 return NULL;
1061 if (node->type != isl_ast_node_if)
1062 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1063 "not an if node", return NULL);
1064 return isl_ast_node_copy(node->u.i.else_node);
1067 __isl_give isl_ast_expr *isl_ast_node_if_get_cond(
1068 __isl_keep isl_ast_node *node)
1070 if (!node)
1071 return NULL;
1072 if (node->type != isl_ast_node_if)
1073 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1074 "not a guard node", return NULL);
1075 return isl_ast_expr_copy(node->u.i.guard);
1078 __isl_give isl_ast_node_list *isl_ast_node_block_get_children(
1079 __isl_keep isl_ast_node *node)
1081 if (!node)
1082 return NULL;
1083 if (node->type != isl_ast_node_block)
1084 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1085 "not a block node", return NULL);
1086 return isl_ast_node_list_copy(node->u.b.children);
1089 __isl_give isl_ast_expr *isl_ast_node_user_get_expr(
1090 __isl_keep isl_ast_node *node)
1092 if (!node)
1093 return NULL;
1095 return isl_ast_expr_copy(node->u.e.expr);
1098 __isl_give isl_id *isl_ast_node_get_annotation(__isl_keep isl_ast_node *node)
1100 return node ? isl_id_copy(node->annotation) : NULL;
1103 /* Replace node->annotation by "annotation".
1105 __isl_give isl_ast_node *isl_ast_node_set_annotation(
1106 __isl_take isl_ast_node *node, __isl_take isl_id *annotation)
1108 node = isl_ast_node_cow(node);
1109 if (!node || !annotation)
1110 goto error;
1112 isl_id_free(node->annotation);
1113 node->annotation = annotation;
1115 return node;
1116 error:
1117 isl_id_free(annotation);
1118 return isl_ast_node_free(node);
1121 /* Textual C representation of the various operators.
1123 static char *op_str[] = {
1124 [isl_ast_op_and] = "&&",
1125 [isl_ast_op_and_then] = "&&",
1126 [isl_ast_op_or] = "||",
1127 [isl_ast_op_or_else] = "||",
1128 [isl_ast_op_max] = "max",
1129 [isl_ast_op_min] = "min",
1130 [isl_ast_op_minus] = "-",
1131 [isl_ast_op_add] = "+",
1132 [isl_ast_op_sub] = "-",
1133 [isl_ast_op_mul] = "*",
1134 [isl_ast_op_pdiv_q] = "/",
1135 [isl_ast_op_pdiv_r] = "%",
1136 [isl_ast_op_div] = "/",
1137 [isl_ast_op_eq] = "==",
1138 [isl_ast_op_le] = "<=",
1139 [isl_ast_op_ge] = ">=",
1140 [isl_ast_op_lt] = "<",
1141 [isl_ast_op_gt] = ">"
1144 /* Precedence in C of the various operators.
1145 * Based on http://en.wikipedia.org/wiki/Operators_in_C_and_C++
1146 * Lowest value means highest precedence.
1148 static int op_prec[] = {
1149 [isl_ast_op_and] = 13,
1150 [isl_ast_op_and_then] = 13,
1151 [isl_ast_op_or] = 14,
1152 [isl_ast_op_or_else] = 14,
1153 [isl_ast_op_max] = 2,
1154 [isl_ast_op_min] = 2,
1155 [isl_ast_op_minus] = 3,
1156 [isl_ast_op_add] = 6,
1157 [isl_ast_op_sub] = 6,
1158 [isl_ast_op_mul] = 5,
1159 [isl_ast_op_div] = 5,
1160 [isl_ast_op_fdiv_q] = 2,
1161 [isl_ast_op_pdiv_q] = 5,
1162 [isl_ast_op_pdiv_r] = 5,
1163 [isl_ast_op_cond] = 15,
1164 [isl_ast_op_select] = 15,
1165 [isl_ast_op_eq] = 9,
1166 [isl_ast_op_le] = 8,
1167 [isl_ast_op_ge] = 8,
1168 [isl_ast_op_lt] = 8,
1169 [isl_ast_op_gt] = 8,
1170 [isl_ast_op_call] = 2,
1171 [isl_ast_op_access] = 2
1174 /* Is the operator left-to-right associative?
1176 static int op_left[] = {
1177 [isl_ast_op_and] = 1,
1178 [isl_ast_op_and_then] = 1,
1179 [isl_ast_op_or] = 1,
1180 [isl_ast_op_or_else] = 1,
1181 [isl_ast_op_max] = 1,
1182 [isl_ast_op_min] = 1,
1183 [isl_ast_op_minus] = 0,
1184 [isl_ast_op_add] = 1,
1185 [isl_ast_op_sub] = 1,
1186 [isl_ast_op_mul] = 1,
1187 [isl_ast_op_div] = 1,
1188 [isl_ast_op_fdiv_q] = 1,
1189 [isl_ast_op_pdiv_q] = 1,
1190 [isl_ast_op_pdiv_r] = 1,
1191 [isl_ast_op_cond] = 0,
1192 [isl_ast_op_select] = 0,
1193 [isl_ast_op_eq] = 1,
1194 [isl_ast_op_le] = 1,
1195 [isl_ast_op_ge] = 1,
1196 [isl_ast_op_lt] = 1,
1197 [isl_ast_op_gt] = 1,
1198 [isl_ast_op_call] = 1,
1199 [isl_ast_op_access] = 1
1202 static int is_and(enum isl_ast_op_type op)
1204 return op == isl_ast_op_and || op == isl_ast_op_and_then;
1207 static int is_or(enum isl_ast_op_type op)
1209 return op == isl_ast_op_or || op == isl_ast_op_or_else;
1212 static int is_add_sub(enum isl_ast_op_type op)
1214 return op == isl_ast_op_add || op == isl_ast_op_sub;
1217 static int is_div_mod(enum isl_ast_op_type op)
1219 return op == isl_ast_op_div || op == isl_ast_op_pdiv_r;
1222 /* Do we need/want parentheses around "expr" as a subexpression of
1223 * an "op" operation? If "left" is set, then "expr" is the left-most
1224 * operand.
1226 * We only need parentheses if "expr" represents an operation.
1228 * If op has a higher precedence than expr->u.op.op, then we need
1229 * parentheses.
1230 * If op and expr->u.op.op have the same precedence, but the operations
1231 * are performed in an order that is different from the associativity,
1232 * then we need parentheses.
1234 * An and inside an or technically does not require parentheses,
1235 * but some compilers complain about that, so we add them anyway.
1237 * Computations such as "a / b * c" and "a % b + c" can be somewhat
1238 * difficult to read, so we add parentheses for those as well.
1240 static int sub_expr_need_parens(enum isl_ast_op_type op,
1241 __isl_keep isl_ast_expr *expr, int left)
1243 if (expr->type != isl_ast_expr_op)
1244 return 0;
1246 if (op_prec[expr->u.op.op] > op_prec[op])
1247 return 1;
1248 if (op_prec[expr->u.op.op] == op_prec[op] && left != op_left[op])
1249 return 1;
1251 if (is_or(op) && is_and(expr->u.op.op))
1252 return 1;
1253 if (op == isl_ast_op_mul && expr->u.op.op != isl_ast_op_mul &&
1254 op_prec[expr->u.op.op] == op_prec[op])
1255 return 1;
1256 if (is_add_sub(op) && is_div_mod(expr->u.op.op))
1257 return 1;
1259 return 0;
1262 /* Print "expr" as a subexpression of an "op" operation.
1263 * If "left" is set, then "expr" is the left-most operand.
1265 static __isl_give isl_printer *print_sub_expr(__isl_take isl_printer *p,
1266 enum isl_ast_op_type op, __isl_keep isl_ast_expr *expr, int left)
1268 int need_parens;
1270 need_parens = sub_expr_need_parens(op, expr, left);
1272 if (need_parens)
1273 p = isl_printer_print_str(p, "(");
1274 p = isl_printer_print_ast_expr(p, expr);
1275 if (need_parens)
1276 p = isl_printer_print_str(p, ")");
1277 return p;
1280 /* Print a min or max reduction "expr".
1282 static __isl_give isl_printer *print_min_max(__isl_take isl_printer *p,
1283 __isl_keep isl_ast_expr *expr)
1285 int i = 0;
1287 for (i = 1; i < expr->u.op.n_arg; ++i) {
1288 p = isl_printer_print_str(p, op_str[expr->u.op.op]);
1289 p = isl_printer_print_str(p, "(");
1291 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1292 for (i = 1; i < expr->u.op.n_arg; ++i) {
1293 p = isl_printer_print_str(p, ", ");
1294 p = isl_printer_print_ast_expr(p, expr->u.op.args[i]);
1295 p = isl_printer_print_str(p, ")");
1298 return p;
1301 /* Print a function call "expr".
1303 * The first argument represents the function to be called.
1305 static __isl_give isl_printer *print_call(__isl_take isl_printer *p,
1306 __isl_keep isl_ast_expr *expr)
1308 int i = 0;
1310 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1311 p = isl_printer_print_str(p, "(");
1312 for (i = 1; i < expr->u.op.n_arg; ++i) {
1313 if (i != 1)
1314 p = isl_printer_print_str(p, ", ");
1315 p = isl_printer_print_ast_expr(p, expr->u.op.args[i]);
1317 p = isl_printer_print_str(p, ")");
1319 return p;
1322 /* Print an array access "expr".
1324 * The first argument represents the array being accessed.
1326 static __isl_give isl_printer *print_access(__isl_take isl_printer *p,
1327 __isl_keep isl_ast_expr *expr)
1329 int i = 0;
1331 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1332 for (i = 1; i < expr->u.op.n_arg; ++i) {
1333 p = isl_printer_print_str(p, "[");
1334 p = isl_printer_print_ast_expr(p, expr->u.op.args[i]);
1335 p = isl_printer_print_str(p, "]");
1338 return p;
1341 /* Print "expr" to "p".
1343 * If we are printing in isl format, then we also print an indication
1344 * of the size of the expression (if it was computed).
1346 __isl_give isl_printer *isl_printer_print_ast_expr(__isl_take isl_printer *p,
1347 __isl_keep isl_ast_expr *expr)
1349 if (!p)
1350 return NULL;
1351 if (!expr)
1352 return isl_printer_free(p);
1354 switch (expr->type) {
1355 case isl_ast_expr_op:
1356 if (expr->u.op.op == isl_ast_op_call) {
1357 p = print_call(p, expr);
1358 break;
1360 if (expr->u.op.op == isl_ast_op_access) {
1361 p = print_access(p, expr);
1362 break;
1364 if (expr->u.op.n_arg == 1) {
1365 p = isl_printer_print_str(p, op_str[expr->u.op.op]);
1366 p = print_sub_expr(p, expr->u.op.op,
1367 expr->u.op.args[0], 0);
1368 break;
1370 if (expr->u.op.op == isl_ast_op_fdiv_q) {
1371 p = isl_printer_print_str(p, "floord(");
1372 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1373 p = isl_printer_print_str(p, ", ");
1374 p = isl_printer_print_ast_expr(p, expr->u.op.args[1]);
1375 p = isl_printer_print_str(p, ")");
1376 break;
1378 if (expr->u.op.op == isl_ast_op_max ||
1379 expr->u.op.op == isl_ast_op_min) {
1380 p = print_min_max(p, expr);
1381 break;
1383 if (expr->u.op.op == isl_ast_op_cond ||
1384 expr->u.op.op == isl_ast_op_select) {
1385 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1386 p = isl_printer_print_str(p, " ? ");
1387 p = isl_printer_print_ast_expr(p, expr->u.op.args[1]);
1388 p = isl_printer_print_str(p, " : ");
1389 p = isl_printer_print_ast_expr(p, expr->u.op.args[2]);
1390 break;
1392 if (expr->u.op.n_arg != 2)
1393 isl_die(isl_printer_get_ctx(p), isl_error_internal,
1394 "operation should have two arguments",
1395 goto error);
1396 p = print_sub_expr(p, expr->u.op.op, expr->u.op.args[0], 1);
1397 p = isl_printer_print_str(p, " ");
1398 p = isl_printer_print_str(p, op_str[expr->u.op.op]);
1399 p = isl_printer_print_str(p, " ");
1400 p = print_sub_expr(p, expr->u.op.op, expr->u.op.args[1], 0);
1401 break;
1402 case isl_ast_expr_id:
1403 p = isl_printer_print_str(p, isl_id_get_name(expr->u.id));
1404 break;
1405 case isl_ast_expr_int:
1406 p = isl_printer_print_val(p, expr->u.v);
1407 break;
1408 case isl_ast_expr_error:
1409 break;
1412 return p;
1413 error:
1414 isl_printer_free(p);
1415 return NULL;
1418 /* Print "node" to "p" in "isl format".
1420 static __isl_give isl_printer *print_ast_node_isl(__isl_take isl_printer *p,
1421 __isl_keep isl_ast_node *node)
1423 p = isl_printer_print_str(p, "(");
1424 switch (node->type) {
1425 case isl_ast_node_for:
1426 if (node->u.f.degenerate) {
1427 p = isl_printer_print_ast_expr(p, node->u.f.init);
1428 } else {
1429 p = isl_printer_print_str(p, "init: ");
1430 p = isl_printer_print_ast_expr(p, node->u.f.init);
1431 p = isl_printer_print_str(p, ", ");
1432 p = isl_printer_print_str(p, "cond: ");
1433 p = isl_printer_print_ast_expr(p, node->u.f.cond);
1434 p = isl_printer_print_str(p, ", ");
1435 p = isl_printer_print_str(p, "inc: ");
1436 p = isl_printer_print_ast_expr(p, node->u.f.inc);
1438 if (node->u.f.body) {
1439 p = isl_printer_print_str(p, ", ");
1440 p = isl_printer_print_str(p, "body: ");
1441 p = isl_printer_print_ast_node(p, node->u.f.body);
1443 break;
1444 case isl_ast_node_user:
1445 p = isl_printer_print_ast_expr(p, node->u.e.expr);
1446 break;
1447 case isl_ast_node_if:
1448 p = isl_printer_print_str(p, "guard: ");
1449 p = isl_printer_print_ast_expr(p, node->u.i.guard);
1450 if (node->u.i.then) {
1451 p = isl_printer_print_str(p, ", ");
1452 p = isl_printer_print_str(p, "then: ");
1453 p = isl_printer_print_ast_node(p, node->u.i.then);
1455 if (node->u.i.else_node) {
1456 p = isl_printer_print_str(p, ", ");
1457 p = isl_printer_print_str(p, "else: ");
1458 p = isl_printer_print_ast_node(p, node->u.i.else_node);
1460 break;
1461 case isl_ast_node_block:
1462 p = isl_printer_print_ast_node_list(p, node->u.b.children);
1463 break;
1464 default:
1465 break;
1467 p = isl_printer_print_str(p, ")");
1468 return p;
1471 /* Do we need to print a block around the body "node" of a for or if node?
1473 * If the node is a block, then we need to print a block.
1474 * Also if the node is a degenerate for then we will print it as
1475 * an assignment followed by the body of the for loop, so we need a block
1476 * as well.
1478 static int need_block(__isl_keep isl_ast_node *node)
1480 if (node->type == isl_ast_node_block)
1481 return 1;
1482 if (node->type == isl_ast_node_for && node->u.f.degenerate)
1483 return 1;
1484 return 0;
1487 static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
1488 __isl_keep isl_ast_node *node,
1489 __isl_keep isl_ast_print_options *options, int in_block, int in_list);
1490 static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
1491 __isl_keep isl_ast_node *node,
1492 __isl_keep isl_ast_print_options *options, int new_line);
1494 /* Print the body "node" of a for or if node.
1495 * If "else_node" is set, then it is printed as well.
1497 * We first check if we need to print out a block.
1498 * We always print out a block if there is an else node to make
1499 * sure that the else node is matched to the correct if node.
1501 * If the else node is itself an if, then we print it as
1503 * } else if (..)
1505 * Otherwise the else node is printed as
1507 * } else
1508 * node
1510 static __isl_give isl_printer *print_body_c(__isl_take isl_printer *p,
1511 __isl_keep isl_ast_node *node, __isl_keep isl_ast_node *else_node,
1512 __isl_keep isl_ast_print_options *options)
1514 if (!node)
1515 return isl_printer_free(p);
1517 if (!else_node && !need_block(node)) {
1518 p = isl_printer_end_line(p);
1519 p = isl_printer_indent(p, 2);
1520 p = isl_ast_node_print(node, p,
1521 isl_ast_print_options_copy(options));
1522 p = isl_printer_indent(p, -2);
1523 return p;
1526 p = isl_printer_print_str(p, " {");
1527 p = isl_printer_end_line(p);
1528 p = isl_printer_indent(p, 2);
1529 p = print_ast_node_c(p, node, options, 1, 0);
1530 p = isl_printer_indent(p, -2);
1531 p = isl_printer_start_line(p);
1532 p = isl_printer_print_str(p, "}");
1533 if (else_node) {
1534 if (else_node->type == isl_ast_node_if) {
1535 p = isl_printer_print_str(p, " else ");
1536 p = print_if_c(p, else_node, options, 0);
1537 } else {
1538 p = isl_printer_print_str(p, " else");
1539 p = print_body_c(p, else_node, NULL, options);
1541 } else
1542 p = isl_printer_end_line(p);
1544 return p;
1547 /* Print the start of a compound statement.
1549 static __isl_give isl_printer *start_block(__isl_take isl_printer *p)
1551 p = isl_printer_start_line(p);
1552 p = isl_printer_print_str(p, "{");
1553 p = isl_printer_end_line(p);
1554 p = isl_printer_indent(p, 2);
1556 return p;
1559 /* Print the end of a compound statement.
1561 static __isl_give isl_printer *end_block(__isl_take isl_printer *p)
1563 p = isl_printer_indent(p, -2);
1564 p = isl_printer_start_line(p);
1565 p = isl_printer_print_str(p, "}");
1566 p = isl_printer_end_line(p);
1568 return p;
1571 /* Print the for node "node".
1573 * If the for node is degenerate, it is printed as
1575 * type iterator = init;
1576 * body
1578 * Otherwise, it is printed as
1580 * for (type iterator = init; cond; iterator += inc)
1581 * body
1583 * "in_block" is set if we are currently inside a block.
1584 * "in_list" is set if the current node is not alone in the block.
1585 * If we are not in a block or if the current not is not alone in the block
1586 * then we print a block around a degenerate for loop such that the variable
1587 * declaration will not conflict with any potential other declaration
1588 * of the same variable.
1590 static __isl_give isl_printer *print_for_c(__isl_take isl_printer *p,
1591 __isl_keep isl_ast_node *node,
1592 __isl_keep isl_ast_print_options *options, int in_block, int in_list)
1594 isl_id *id;
1595 const char *name;
1596 const char *type;
1598 type = isl_options_get_ast_iterator_type(isl_printer_get_ctx(p));
1599 if (!node->u.f.degenerate) {
1600 id = isl_ast_expr_get_id(node->u.f.iterator);
1601 name = isl_id_get_name(id);
1602 isl_id_free(id);
1603 p = isl_printer_start_line(p);
1604 p = isl_printer_print_str(p, "for (");
1605 p = isl_printer_print_str(p, type);
1606 p = isl_printer_print_str(p, " ");
1607 p = isl_printer_print_str(p, name);
1608 p = isl_printer_print_str(p, " = ");
1609 p = isl_printer_print_ast_expr(p, node->u.f.init);
1610 p = isl_printer_print_str(p, "; ");
1611 p = isl_printer_print_ast_expr(p, node->u.f.cond);
1612 p = isl_printer_print_str(p, "; ");
1613 p = isl_printer_print_str(p, name);
1614 p = isl_printer_print_str(p, " += ");
1615 p = isl_printer_print_ast_expr(p, node->u.f.inc);
1616 p = isl_printer_print_str(p, ")");
1617 p = print_body_c(p, node->u.f.body, NULL, options);
1618 } else {
1619 id = isl_ast_expr_get_id(node->u.f.iterator);
1620 name = isl_id_get_name(id);
1621 isl_id_free(id);
1622 if (!in_block || in_list)
1623 p = start_block(p);
1624 p = isl_printer_start_line(p);
1625 p = isl_printer_print_str(p, type);
1626 p = isl_printer_print_str(p, " ");
1627 p = isl_printer_print_str(p, name);
1628 p = isl_printer_print_str(p, " = ");
1629 p = isl_printer_print_ast_expr(p, node->u.f.init);
1630 p = isl_printer_print_str(p, ";");
1631 p = isl_printer_end_line(p);
1632 p = print_ast_node_c(p, node->u.f.body, options, 1, 0);
1633 if (!in_block || in_list)
1634 p = end_block(p);
1637 return p;
1640 /* Print the if node "node".
1641 * If "new_line" is set then the if node should be printed on a new line.
1643 static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
1644 __isl_keep isl_ast_node *node,
1645 __isl_keep isl_ast_print_options *options, int new_line)
1647 if (new_line)
1648 p = isl_printer_start_line(p);
1649 p = isl_printer_print_str(p, "if (");
1650 p = isl_printer_print_ast_expr(p, node->u.i.guard);
1651 p = isl_printer_print_str(p, ")");
1652 p = print_body_c(p, node->u.i.then, node->u.i.else_node, options);
1654 return p;
1657 /* Print the "node" to "p".
1659 * "in_block" is set if we are currently inside a block.
1660 * If so, we do not print a block around the children of a block node.
1661 * We do this to avoid an extra block around the body of a degenerate
1662 * for node.
1664 * "in_list" is set if the current node is not alone in the block.
1666 static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
1667 __isl_keep isl_ast_node *node,
1668 __isl_keep isl_ast_print_options *options, int in_block, int in_list)
1670 switch (node->type) {
1671 case isl_ast_node_for:
1672 if (options->print_for)
1673 return options->print_for(p,
1674 isl_ast_print_options_copy(options),
1675 node, options->print_for_user);
1676 p = print_for_c(p, node, options, in_block, in_list);
1677 break;
1678 case isl_ast_node_if:
1679 p = print_if_c(p, node, options, 1);
1680 break;
1681 case isl_ast_node_block:
1682 if (!in_block)
1683 p = start_block(p);
1684 p = isl_ast_node_list_print(node->u.b.children, p, options);
1685 if (!in_block)
1686 p = end_block(p);
1687 break;
1688 case isl_ast_node_user:
1689 if (options->print_user)
1690 return options->print_user(p,
1691 isl_ast_print_options_copy(options),
1692 node, options->print_user_user);
1693 p = isl_printer_start_line(p);
1694 p = isl_printer_print_ast_expr(p, node->u.e.expr);
1695 p = isl_printer_print_str(p, ";");
1696 p = isl_printer_end_line(p);
1697 break;
1698 case isl_ast_node_error:
1699 break;
1701 return p;
1704 /* Print the for node "node" to "p".
1706 __isl_give isl_printer *isl_ast_node_for_print(__isl_keep isl_ast_node *node,
1707 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
1709 if (!node || !options)
1710 goto error;
1711 if (node->type != isl_ast_node_for)
1712 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1713 "not a for node", goto error);
1714 p = print_for_c(p, node, options, 0, 0);
1715 isl_ast_print_options_free(options);
1716 return p;
1717 error:
1718 isl_ast_print_options_free(options);
1719 isl_printer_free(p);
1720 return NULL;
1723 /* Print the if node "node" to "p".
1725 __isl_give isl_printer *isl_ast_node_if_print(__isl_keep isl_ast_node *node,
1726 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
1728 if (!node || !options)
1729 goto error;
1730 if (node->type != isl_ast_node_if)
1731 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1732 "not an if node", goto error);
1733 p = print_if_c(p, node, options, 1);
1734 isl_ast_print_options_free(options);
1735 return p;
1736 error:
1737 isl_ast_print_options_free(options);
1738 isl_printer_free(p);
1739 return NULL;
1742 /* Print "node" to "p".
1744 __isl_give isl_printer *isl_ast_node_print(__isl_keep isl_ast_node *node,
1745 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
1747 if (!options || !node)
1748 goto error;
1749 p = print_ast_node_c(p, node, options, 0, 0);
1750 isl_ast_print_options_free(options);
1751 return p;
1752 error:
1753 isl_ast_print_options_free(options);
1754 isl_printer_free(p);
1755 return NULL;
1758 /* Print "node" to "p".
1760 __isl_give isl_printer *isl_printer_print_ast_node(__isl_take isl_printer *p,
1761 __isl_keep isl_ast_node *node)
1763 int format;
1764 isl_ast_print_options *options;
1766 if (!p)
1767 return NULL;
1769 format = isl_printer_get_output_format(p);
1770 switch (format) {
1771 case ISL_FORMAT_ISL:
1772 p = print_ast_node_isl(p, node);
1773 break;
1774 case ISL_FORMAT_C:
1775 options = isl_ast_print_options_alloc(isl_printer_get_ctx(p));
1776 p = isl_ast_node_print(node, p, options);
1777 break;
1778 default:
1779 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
1780 "output format not supported for ast_node",
1781 return isl_printer_free(p));
1784 return p;
1787 /* Print the list of nodes "list" to "p".
1789 __isl_give isl_printer *isl_ast_node_list_print(
1790 __isl_keep isl_ast_node_list *list, __isl_take isl_printer *p,
1791 __isl_keep isl_ast_print_options *options)
1793 int i;
1795 if (!p || !list || !options)
1796 return isl_printer_free(p);
1798 for (i = 0; i < list->n; ++i)
1799 p = print_ast_node_c(p, list->p[i], options, 1, 1);
1801 return p;
1804 #define ISL_AST_MACRO_FLOORD (1 << 0)
1805 #define ISL_AST_MACRO_MIN (1 << 1)
1806 #define ISL_AST_MACRO_MAX (1 << 2)
1807 #define ISL_AST_MACRO_ALL (ISL_AST_MACRO_FLOORD | \
1808 ISL_AST_MACRO_MIN | \
1809 ISL_AST_MACRO_MAX)
1811 /* If "expr" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1812 * then set the corresponding bit in "macros".
1814 static int ast_expr_required_macros(__isl_keep isl_ast_expr *expr, int macros)
1816 int i;
1818 if (macros == ISL_AST_MACRO_ALL)
1819 return macros;
1821 if (expr->type != isl_ast_expr_op)
1822 return macros;
1824 if (expr->u.op.op == isl_ast_op_min)
1825 macros |= ISL_AST_MACRO_MIN;
1826 if (expr->u.op.op == isl_ast_op_max)
1827 macros |= ISL_AST_MACRO_MAX;
1828 if (expr->u.op.op == isl_ast_op_fdiv_q)
1829 macros |= ISL_AST_MACRO_FLOORD;
1831 for (i = 0; i < expr->u.op.n_arg; ++i)
1832 macros = ast_expr_required_macros(expr->u.op.args[i], macros);
1834 return macros;
1837 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
1838 int macros);
1840 /* If "node" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1841 * then set the corresponding bit in "macros".
1843 static int ast_node_required_macros(__isl_keep isl_ast_node *node, int macros)
1845 if (macros == ISL_AST_MACRO_ALL)
1846 return macros;
1848 switch (node->type) {
1849 case isl_ast_node_for:
1850 macros = ast_expr_required_macros(node->u.f.init, macros);
1851 if (!node->u.f.degenerate) {
1852 macros = ast_expr_required_macros(node->u.f.cond,
1853 macros);
1854 macros = ast_expr_required_macros(node->u.f.inc,
1855 macros);
1857 macros = ast_node_required_macros(node->u.f.body, macros);
1858 break;
1859 case isl_ast_node_if:
1860 macros = ast_expr_required_macros(node->u.i.guard, macros);
1861 macros = ast_node_required_macros(node->u.i.then, macros);
1862 if (node->u.i.else_node)
1863 macros = ast_node_required_macros(node->u.i.else_node,
1864 macros);
1865 break;
1866 case isl_ast_node_block:
1867 macros = ast_node_list_required_macros(node->u.b.children,
1868 macros);
1869 break;
1870 case isl_ast_node_user:
1871 macros = ast_expr_required_macros(node->u.e.expr, macros);
1872 break;
1873 case isl_ast_node_error:
1874 break;
1877 return macros;
1880 /* If "list" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1881 * then set the corresponding bit in "macros".
1883 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
1884 int macros)
1886 int i;
1888 for (i = 0; i < list->n; ++i)
1889 macros = ast_node_required_macros(list->p[i], macros);
1891 return macros;
1894 /* Print a macro definition for the operator "type".
1896 __isl_give isl_printer *isl_ast_op_type_print_macro(
1897 enum isl_ast_op_type type, __isl_take isl_printer *p)
1899 switch (type) {
1900 case isl_ast_op_min:
1901 p = isl_printer_start_line(p);
1902 p = isl_printer_print_str(p,
1903 "#define min(x,y) ((x) < (y) ? (x) : (y))");
1904 p = isl_printer_end_line(p);
1905 break;
1906 case isl_ast_op_max:
1907 p = isl_printer_start_line(p);
1908 p = isl_printer_print_str(p,
1909 "#define max(x,y) ((x) > (y) ? (x) : (y))");
1910 p = isl_printer_end_line(p);
1911 break;
1912 case isl_ast_op_fdiv_q:
1913 p = isl_printer_start_line(p);
1914 p = isl_printer_print_str(p,
1915 "#define floord(n,d) "
1916 "(((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))");
1917 p = isl_printer_end_line(p);
1918 break;
1919 default:
1920 break;
1923 return p;
1926 /* Call "fn" for each type of operation that appears in "node"
1927 * and that requires a macro definition.
1929 int isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node *node,
1930 int (*fn)(enum isl_ast_op_type type, void *user), void *user)
1932 int macros;
1934 if (!node)
1935 return -1;
1937 macros = ast_node_required_macros(node, 0);
1939 if (macros & ISL_AST_MACRO_MIN && fn(isl_ast_op_min, user) < 0)
1940 return -1;
1941 if (macros & ISL_AST_MACRO_MAX && fn(isl_ast_op_max, user) < 0)
1942 return -1;
1943 if (macros & ISL_AST_MACRO_FLOORD && fn(isl_ast_op_fdiv_q, user) < 0)
1944 return -1;
1946 return 0;
1949 static int ast_op_type_print_macro(enum isl_ast_op_type type, void *user)
1951 isl_printer **p = user;
1953 *p = isl_ast_op_type_print_macro(type, *p);
1955 return 0;
1958 /* Print macro definitions for all the macros used in the result
1959 * of printing "node.
1961 __isl_give isl_printer *isl_ast_node_print_macros(
1962 __isl_keep isl_ast_node *node, __isl_take isl_printer *p)
1964 if (isl_ast_node_foreach_ast_op_type(node,
1965 &ast_op_type_print_macro, &p) < 0)
1966 return isl_printer_free(p);
1967 return p;