isl_ast_expr_from_aff: try harder to use isl_ast_op_pdiv_{q,r}
[isl.git] / isl_ast.c
blobdbf390441abb632edc622905952cb41decc88676
1 #include <isl_ast_private.h>
2 #include <isl_list_private.h>
4 #undef BASE
5 #define BASE ast_expr
7 #include <isl_list_templ.c>
9 #undef BASE
10 #define BASE ast_node
12 #include <isl_list_templ.c>
14 __isl_give isl_ast_print_options *isl_ast_print_options_alloc(isl_ctx *ctx)
16 return isl_calloc_type(ctx, isl_ast_print_options);
19 void *isl_ast_print_options_free(__isl_take isl_ast_print_options *options)
21 free(options);
22 return NULL;
25 /* Set the print_user callback of "options" to "print_user".
27 * If this callback is set, then it used to print user nodes in the AST.
28 * Otherwise, the expression associated to the user node is printed.
30 __isl_give isl_ast_print_options *isl_ast_print_options_set_print_user(
31 __isl_take isl_ast_print_options *options,
32 __isl_give isl_printer *(*print_user)(__isl_take isl_printer *p,
33 __isl_keep isl_ast_node *node, void *user),
34 void *user)
36 if (!options)
37 return NULL;
39 options->print_user = print_user;
40 options->print_user_user = user;
42 return options;
45 /* Set the print_for callback of "options" to "print_for".
47 * If this callback is set, then it used to print for nodes in the AST.
49 __isl_give isl_ast_print_options *isl_ast_print_options_set_print_for(
50 __isl_take isl_ast_print_options *options,
51 __isl_give isl_printer *(*print_for)(__isl_take isl_printer *p,
52 __isl_keep isl_ast_node *node, void *user),
53 void *user)
55 if (!options)
56 return NULL;
58 options->print_for = print_for;
59 options->print_for_user = user;
61 return options;
64 __isl_give isl_ast_expr *isl_ast_expr_copy(__isl_keep isl_ast_expr *expr)
66 if (!expr)
67 return NULL;
69 expr->ref++;
70 return expr;
73 __isl_give isl_ast_expr *isl_ast_expr_dup(__isl_keep isl_ast_expr *expr)
75 int i;
76 isl_ctx *ctx;
77 isl_ast_expr *dup;
79 if (!expr)
80 return NULL;
82 ctx = isl_ast_expr_get_ctx(expr);
83 switch (expr->type) {
84 case isl_ast_expr_int:
85 dup = isl_ast_expr_alloc_int(ctx, expr->u.i);
86 break;
87 case isl_ast_expr_id:
88 dup = isl_ast_expr_from_id(isl_id_copy(expr->u.id));
89 break;
90 case isl_ast_expr_op:
91 dup = isl_ast_expr_alloc_op(ctx,
92 expr->u.op.op, expr->u.op.n_arg);
93 if (!dup)
94 return NULL;
95 for (i = 0; i < expr->u.op.n_arg; ++i)
96 dup->u.op.args[i] =
97 isl_ast_expr_copy(expr->u.op.args[i]);
98 break;
99 case isl_ast_expr_error:
100 dup = NULL;
103 if (!dup)
104 return NULL;
106 return dup;
109 __isl_give isl_ast_expr *isl_ast_expr_cow(__isl_take isl_ast_expr *expr)
111 if (!expr)
112 return NULL;
114 if (expr->ref == 1)
115 return expr;
116 expr->ref--;
117 return isl_ast_expr_dup(expr);
120 void *isl_ast_expr_free(__isl_take isl_ast_expr *expr)
122 int i;
124 if (!expr)
125 return NULL;
127 if (--expr->ref > 0)
128 return NULL;
130 isl_ctx_deref(expr->ctx);
132 switch (expr->type) {
133 case isl_ast_expr_int:
134 isl_int_clear(expr->u.i);
135 break;
136 case isl_ast_expr_id:
137 isl_id_free(expr->u.id);
138 break;
139 case isl_ast_expr_op:
140 for (i = 0; i < expr->u.op.n_arg; ++i)
141 isl_ast_expr_free(expr->u.op.args[i]);
142 free(expr->u.op.args);
143 break;
144 case isl_ast_expr_error:
145 break;
148 free(expr);
149 return NULL;
152 isl_ctx *isl_ast_expr_get_ctx(__isl_keep isl_ast_expr *expr)
154 return expr ? expr->ctx : NULL;
157 enum isl_ast_expr_type isl_ast_expr_get_type(__isl_keep isl_ast_expr *expr)
159 return expr ? expr->type : isl_ast_expr_error;
162 int isl_ast_expr_get_int(__isl_keep isl_ast_expr *expr, isl_int *v)
164 if (!expr)
165 return -1;
166 if (expr->type != isl_ast_expr_int)
167 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
168 "expression not an int", return -1);
169 isl_int_set(*v, expr->u.i);
170 return 0;
173 __isl_give isl_id *isl_ast_expr_get_id(__isl_keep isl_ast_expr *expr)
175 if (!expr)
176 return NULL;
177 if (expr->type != isl_ast_expr_id)
178 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
179 "expression not an identifier", return NULL);
181 return isl_id_copy(expr->u.id);
184 enum isl_ast_op_type isl_ast_expr_get_op_type(__isl_keep isl_ast_expr *expr)
186 if (!expr)
187 return isl_ast_op_error;
188 if (expr->type != isl_ast_expr_op)
189 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
190 "expression not an operation", return isl_ast_op_error);
191 return expr->u.op.op;
194 int isl_ast_expr_get_op_n_arg(__isl_keep isl_ast_expr *expr)
196 if (!expr)
197 return -1;
198 if (expr->type != isl_ast_expr_op)
199 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
200 "expression not an operation", return -1);
201 return expr->u.op.n_arg;
204 __isl_give isl_ast_expr *isl_ast_expr_get_op_arg(__isl_keep isl_ast_expr *expr,
205 int pos)
207 if (!expr)
208 return NULL;
209 if (expr->type != isl_ast_expr_op)
210 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
211 "expression not an operation", return NULL);
212 if (pos < 0 || pos >= expr->u.op.n_arg)
213 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
214 "index out of bounds", return NULL);
216 return isl_ast_expr_copy(expr->u.op.args[pos]);
219 /* Replace the argument at position "pos" of "expr" by "arg".
221 __isl_give isl_ast_expr *isl_ast_expr_set_op_arg(__isl_take isl_ast_expr *expr,
222 int pos, __isl_take isl_ast_expr *arg)
224 expr = isl_ast_expr_cow(expr);
225 if (!expr || !arg)
226 goto error;
227 if (expr->type != isl_ast_expr_op)
228 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
229 "expression not an operation", goto error);
230 if (pos < 0 || pos >= expr->u.op.n_arg)
231 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
232 "index out of bounds", goto error);
234 isl_ast_expr_free(expr->u.op.args[pos]);
235 expr->u.op.args[pos] = arg;
237 return expr;
238 error:
239 isl_ast_expr_free(arg);
240 return isl_ast_expr_free(expr);
243 /* Create a new operation expression of operation type "op",
244 * with "n_arg" as yet unspecified arguments.
246 __isl_give isl_ast_expr *isl_ast_expr_alloc_op(isl_ctx *ctx,
247 enum isl_ast_op_type op, int n_arg)
249 isl_ast_expr *expr;
251 expr = isl_calloc_type(ctx, isl_ast_expr);
252 if (!expr)
253 return NULL;
255 expr->ctx = ctx;
256 isl_ctx_ref(ctx);
257 expr->ref = 1;
258 expr->type = isl_ast_expr_op;
259 expr->u.op.op = op;
260 expr->u.op.n_arg = n_arg;
261 expr->u.op.args = isl_calloc_array(ctx, isl_ast_expr *, n_arg);
263 if (!expr->u.op.args)
264 return isl_ast_expr_free(expr);
266 return expr;
269 /* Create a new id expression representing "id".
271 __isl_give isl_ast_expr *isl_ast_expr_from_id(__isl_take isl_id *id)
273 isl_ctx *ctx;
274 isl_ast_expr *expr;
276 if (!id)
277 return NULL;
279 ctx = isl_id_get_ctx(id);
280 expr = isl_calloc_type(ctx, isl_ast_expr);
281 if (!expr)
282 return isl_id_free(id);
284 expr->ctx = ctx;
285 isl_ctx_ref(ctx);
286 expr->ref = 1;
287 expr->type = isl_ast_expr_id;
288 expr->u.id = id;
290 return expr;
293 /* Create a new integer expression representing "i".
295 __isl_give isl_ast_expr *isl_ast_expr_alloc_int_si(isl_ctx *ctx, int i)
297 isl_ast_expr *expr;
299 expr = isl_calloc_type(ctx, isl_ast_expr);
300 if (!expr)
301 return NULL;
303 expr->ctx = ctx;
304 isl_ctx_ref(ctx);
305 expr->ref = 1;
306 expr->type = isl_ast_expr_int;
308 isl_int_init(expr->u.i);
309 isl_int_set_si(expr->u.i, i);
311 return expr;
314 /* Create a new integer expression representing "i".
316 __isl_give isl_ast_expr *isl_ast_expr_alloc_int(isl_ctx *ctx, isl_int i)
318 isl_ast_expr *expr;
320 expr = isl_calloc_type(ctx, isl_ast_expr);
321 if (!expr)
322 return NULL;
324 expr->ctx = ctx;
325 isl_ctx_ref(ctx);
326 expr->ref = 1;
327 expr->type = isl_ast_expr_int;
329 isl_int_init(expr->u.i);
330 isl_int_set(expr->u.i, i);
332 return expr;
335 /* Create an expression representing the negation of "arg".
337 __isl_give isl_ast_expr *isl_ast_expr_neg(__isl_take isl_ast_expr *arg)
339 isl_ctx *ctx;
340 isl_ast_expr *expr = NULL;
342 if (!arg)
343 return NULL;
345 ctx = isl_ast_expr_get_ctx(arg);
346 expr = isl_ast_expr_alloc_op(ctx, isl_ast_op_minus, 1);
347 if (!expr)
348 goto error;
350 expr->u.op.args[0] = arg;
352 return expr;
353 error:
354 isl_ast_expr_free(arg);
355 return NULL;
358 /* Create an expression representing the binary operation "type"
359 * applied to "expr1" and "expr2".
361 __isl_give isl_ast_expr *isl_ast_expr_alloc_binary(enum isl_ast_op_type type,
362 __isl_take isl_ast_expr *expr1, __isl_take isl_ast_expr *expr2)
364 isl_ctx *ctx;
365 isl_ast_expr *expr = NULL;
367 if (!expr1 || !expr2)
368 goto error;
370 ctx = isl_ast_expr_get_ctx(expr1);
371 expr = isl_ast_expr_alloc_op(ctx, type, 2);
372 if (!expr)
373 goto error;
375 expr->u.op.args[0] = expr1;
376 expr->u.op.args[1] = expr2;
378 return expr;
379 error:
380 isl_ast_expr_free(expr1);
381 isl_ast_expr_free(expr2);
382 return NULL;
385 /* Create an expression representing the sum of "expr1" and "expr2".
387 __isl_give isl_ast_expr *isl_ast_expr_add(__isl_take isl_ast_expr *expr1,
388 __isl_take isl_ast_expr *expr2)
390 return isl_ast_expr_alloc_binary(isl_ast_op_add, expr1, expr2);
393 /* Create an expression representing the difference of "expr1" and "expr2".
395 __isl_give isl_ast_expr *isl_ast_expr_sub(__isl_take isl_ast_expr *expr1,
396 __isl_take isl_ast_expr *expr2)
398 return isl_ast_expr_alloc_binary(isl_ast_op_sub, expr1, expr2);
401 /* Create an expression representing the product of "expr1" and "expr2".
403 __isl_give isl_ast_expr *isl_ast_expr_mul(__isl_take isl_ast_expr *expr1,
404 __isl_take isl_ast_expr *expr2)
406 return isl_ast_expr_alloc_binary(isl_ast_op_mul, expr1, expr2);
409 /* Create an expression representing the quotient of "expr1" and "expr2".
411 __isl_give isl_ast_expr *isl_ast_expr_div(__isl_take isl_ast_expr *expr1,
412 __isl_take isl_ast_expr *expr2)
414 return isl_ast_expr_alloc_binary(isl_ast_op_div, expr1, expr2);
417 /* Create an expression representing the conjunction of "expr1" and "expr2".
419 __isl_give isl_ast_expr *isl_ast_expr_and(__isl_take isl_ast_expr *expr1,
420 __isl_take isl_ast_expr *expr2)
422 return isl_ast_expr_alloc_binary(isl_ast_op_and, expr1, expr2);
425 /* Create an expression representing the disjunction of "expr1" and "expr2".
427 __isl_give isl_ast_expr *isl_ast_expr_or(__isl_take isl_ast_expr *expr1,
428 __isl_take isl_ast_expr *expr2)
430 return isl_ast_expr_alloc_binary(isl_ast_op_or, expr1, expr2);
433 isl_ctx *isl_ast_node_get_ctx(__isl_keep isl_ast_node *node)
435 return node ? node->ctx : NULL;
438 enum isl_ast_node_type isl_ast_node_get_type(__isl_keep isl_ast_node *node)
440 return node ? node->type : isl_ast_node_error;
443 __isl_give isl_ast_node *isl_ast_node_alloc(isl_ctx *ctx,
444 enum isl_ast_node_type type)
446 isl_ast_node *node;
448 node = isl_calloc_type(ctx, isl_ast_node);
449 if (!node)
450 return NULL;
452 node->ctx = ctx;
453 isl_ctx_ref(ctx);
454 node->ref = 1;
455 node->type = type;
457 return node;
460 /* Create an if node with the given guard.
462 * The then body needs to be filled in later.
464 __isl_give isl_ast_node *isl_ast_node_alloc_if(__isl_take isl_ast_expr *guard)
466 isl_ast_node *node;
468 if (!guard)
469 return NULL;
471 node = isl_ast_node_alloc(isl_ast_expr_get_ctx(guard), isl_ast_node_if);
472 if (!node)
473 goto error;
474 node->u.i.guard = guard;
476 return node;
477 error:
478 isl_ast_expr_free(guard);
479 return NULL;
482 /* Create a for node with the given iterator.
484 * The remaining fields need to be filled in later.
486 __isl_give isl_ast_node *isl_ast_node_alloc_for(__isl_take isl_id *id)
488 isl_ast_node *node;
489 isl_ctx *ctx;
491 if (!id)
492 return NULL;
494 ctx = isl_id_get_ctx(id);
495 node = isl_ast_node_alloc(ctx, isl_ast_node_for);
496 if (!node)
497 return NULL;
499 node->u.f.iterator = isl_ast_expr_from_id(id);
500 if (!node->u.f.iterator)
501 return isl_ast_node_free(node);
503 return node;
506 /* Create a user node evaluating "expr".
508 __isl_give isl_ast_node *isl_ast_node_alloc_user(__isl_take isl_ast_expr *expr)
510 isl_ctx *ctx;
511 isl_ast_node *node;
513 if (!expr)
514 return NULL;
516 ctx = isl_ast_expr_get_ctx(expr);
517 node = isl_ast_node_alloc(ctx, isl_ast_node_user);
518 if (!node)
519 goto error;
521 node->u.e.expr = expr;
523 return node;
524 error:
525 isl_ast_expr_free(expr);
526 return NULL;
529 /* Create a block node with the given children.
531 __isl_give isl_ast_node *isl_ast_node_alloc_block(
532 __isl_take isl_ast_node_list *list)
534 isl_ast_node *node;
535 isl_ctx *ctx;
537 if (!list)
538 return NULL;
540 ctx = isl_ast_node_list_get_ctx(list);
541 node = isl_ast_node_alloc(ctx, isl_ast_node_block);
542 if (!node)
543 goto error;
545 node->u.b.children = list;
547 return node;
548 error:
549 isl_ast_node_list_free(list);
550 return NULL;
553 /* Represent the given list of nodes as a single node, either by
554 * extract the node from a single element list or by creating
555 * a block node with the list of nodes as children.
557 __isl_give isl_ast_node *isl_ast_node_from_ast_node_list(
558 __isl_take isl_ast_node_list *list)
560 isl_ast_node *node;
562 if (isl_ast_node_list_n_ast_node(list) != 1)
563 return isl_ast_node_alloc_block(list);
565 node = isl_ast_node_list_get_ast_node(list, 0);
566 isl_ast_node_list_free(list);
568 return node;
571 __isl_give isl_ast_node *isl_ast_node_copy(__isl_keep isl_ast_node *node)
573 if (!node)
574 return NULL;
576 node->ref++;
577 return node;
580 __isl_give isl_ast_node *isl_ast_node_dup(__isl_keep isl_ast_node *node)
582 isl_ast_node *dup;
584 if (!node)
585 return NULL;
587 dup = isl_ast_node_alloc(isl_ast_node_get_ctx(node), node->type);
588 if (!dup)
589 return NULL;
591 switch (node->type) {
592 case isl_ast_node_if:
593 dup->u.i.guard = isl_ast_expr_copy(node->u.i.guard);
594 dup->u.i.then = isl_ast_node_copy(node->u.i.then);
595 dup->u.i.else_node = isl_ast_node_copy(node->u.i.else_node);
596 if (!dup->u.i.guard || !dup->u.i.then ||
597 (node->u.i.else_node && !dup->u.i.else_node))
598 return isl_ast_node_free(dup);
599 break;
600 case isl_ast_node_for:
601 dup->u.f.iterator = isl_ast_expr_copy(node->u.f.iterator);
602 dup->u.f.init = isl_ast_expr_copy(node->u.f.init);
603 dup->u.f.cond = isl_ast_expr_copy(node->u.f.cond);
604 dup->u.f.inc = isl_ast_expr_copy(node->u.f.inc);
605 dup->u.f.body = isl_ast_node_copy(node->u.f.body);
606 if (!dup->u.f.iterator || !dup->u.f.init || !dup->u.f.cond ||
607 !dup->u.f.inc || !dup->u.f.body)
608 return isl_ast_node_free(dup);
609 break;
610 case isl_ast_node_block:
611 dup->u.b.children = isl_ast_node_list_copy(node->u.b.children);
612 if (!dup->u.b.children)
613 return isl_ast_node_free(dup);
614 break;
615 case isl_ast_node_user:
616 dup->u.e.expr = isl_ast_expr_copy(node->u.e.expr);
617 if (!dup->u.e.expr)
618 return isl_ast_node_free(dup);
619 break;
620 case isl_ast_node_error:
621 break;
624 return dup;
627 __isl_give isl_ast_node *isl_ast_node_cow(__isl_take isl_ast_node *node)
629 if (!node)
630 return NULL;
632 if (node->ref == 1)
633 return node;
634 node->ref--;
635 return isl_ast_node_dup(node);
638 void *isl_ast_node_free(__isl_take isl_ast_node *node)
640 if (!node)
641 return NULL;
643 if (--node->ref > 0)
644 return NULL;
646 switch (node->type) {
647 case isl_ast_node_if:
648 isl_ast_expr_free(node->u.i.guard);
649 isl_ast_node_free(node->u.i.then);
650 isl_ast_node_free(node->u.i.else_node);
651 break;
652 case isl_ast_node_for:
653 isl_ast_expr_free(node->u.f.iterator);
654 isl_ast_expr_free(node->u.f.init);
655 isl_ast_expr_free(node->u.f.cond);
656 isl_ast_expr_free(node->u.f.inc);
657 isl_ast_node_free(node->u.f.body);
658 break;
659 case isl_ast_node_block:
660 isl_ast_node_list_free(node->u.b.children);
661 break;
662 case isl_ast_node_user:
663 isl_ast_expr_free(node->u.e.expr);
664 break;
665 case isl_ast_node_error:
666 break;
669 isl_id_free(node->annotation);
670 isl_ctx_deref(node->ctx);
671 free(node);
673 return NULL;
676 /* Replace the body of the for node "node" by "body".
678 __isl_give isl_ast_node *isl_ast_node_for_set_body(
679 __isl_take isl_ast_node *node, __isl_take isl_ast_node *body)
681 node = isl_ast_node_cow(node);
682 if (!node || !body)
683 goto error;
684 if (node->type != isl_ast_node_for)
685 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
686 "not a for node", goto error);
688 isl_ast_node_free(node->u.f.body);
689 node->u.f.body = body;
691 return node;
692 error:
693 isl_ast_node_free(node);
694 isl_ast_node_free(body);
695 return NULL;
698 __isl_give isl_ast_node *isl_ast_node_for_get_body(
699 __isl_keep isl_ast_node *node)
701 if (!node)
702 return NULL;
703 if (node->type != isl_ast_node_for)
704 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
705 "not a for node", return NULL);
706 return isl_ast_node_copy(node->u.f.body);
709 /* Mark the given for node as being degenerate.
711 __isl_give isl_ast_node *isl_ast_node_for_mark_degenerate(
712 __isl_take isl_ast_node *node)
714 node = isl_ast_node_cow(node);
715 if (!node)
716 return NULL;
717 node->u.f.degenerate = 1;
718 return node;
721 int isl_ast_node_for_is_degenerate(__isl_keep isl_ast_node *node)
723 if (!node)
724 return -1;
725 if (node->type != isl_ast_node_for)
726 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
727 "not a for node", return -1);
728 return node->u.f.degenerate;
731 __isl_give isl_ast_expr *isl_ast_node_for_get_iterator(
732 __isl_keep isl_ast_node *node)
734 if (!node)
735 return NULL;
736 if (node->type != isl_ast_node_for)
737 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
738 "not a for node", return NULL);
739 return isl_ast_expr_copy(node->u.f.iterator);
742 __isl_give isl_ast_expr *isl_ast_node_for_get_init(
743 __isl_keep isl_ast_node *node)
745 if (!node)
746 return NULL;
747 if (node->type != isl_ast_node_for)
748 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
749 "not a for node", return NULL);
750 return isl_ast_expr_copy(node->u.f.init);
753 /* Return the condition expression of the given for node.
755 * If the for node is degenerate, then the condition is not explicitly
756 * stored in the node. Instead, it is constructed as
758 * iterator <= init
760 __isl_give isl_ast_expr *isl_ast_node_for_get_cond(
761 __isl_keep isl_ast_node *node)
763 if (!node)
764 return NULL;
765 if (node->type != isl_ast_node_for)
766 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
767 "not a for node", return NULL);
768 if (!node->u.f.degenerate)
769 return isl_ast_expr_copy(node->u.f.cond);
771 return isl_ast_expr_alloc_binary(isl_ast_op_le,
772 isl_ast_expr_copy(node->u.f.iterator),
773 isl_ast_expr_copy(node->u.f.init));
776 /* Return the increment of the given for node.
778 * If the for node is degenerate, then the increment is not explicitly
779 * stored in the node. We simply return "1".
781 __isl_give isl_ast_expr *isl_ast_node_for_get_inc(
782 __isl_keep isl_ast_node *node)
784 if (!node)
785 return NULL;
786 if (node->type != isl_ast_node_for)
787 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
788 "not a for node", return NULL);
789 if (!node->u.f.degenerate)
790 return isl_ast_expr_copy(node->u.f.inc);
791 return isl_ast_expr_alloc_int_si(isl_ast_node_get_ctx(node), 1);
794 /* Replace the then branch of the if node "node" by "child".
796 __isl_give isl_ast_node *isl_ast_node_if_set_then(
797 __isl_take isl_ast_node *node, __isl_take isl_ast_node *child)
799 node = isl_ast_node_cow(node);
800 if (!node || !child)
801 goto error;
802 if (node->type != isl_ast_node_if)
803 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
804 "not an if node", goto error);
806 isl_ast_node_free(node->u.i.then);
807 node->u.i.then = child;
809 return node;
810 error:
811 isl_ast_node_free(node);
812 isl_ast_node_free(child);
813 return NULL;
816 __isl_give isl_ast_node *isl_ast_node_if_get_then(
817 __isl_keep isl_ast_node *node)
819 if (!node)
820 return NULL;
821 if (node->type != isl_ast_node_if)
822 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
823 "not an if node", return NULL);
824 return isl_ast_node_copy(node->u.i.then);
827 int isl_ast_node_if_has_else(
828 __isl_keep isl_ast_node *node)
830 if (!node)
831 return -1;
832 if (node->type != isl_ast_node_if)
833 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
834 "not an if node", return -1);
835 return node->u.i.else_node != NULL;
838 __isl_give isl_ast_node *isl_ast_node_if_get_else(
839 __isl_keep isl_ast_node *node)
841 if (!node)
842 return NULL;
843 if (node->type != isl_ast_node_if)
844 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
845 "not an if node", return NULL);
846 return isl_ast_node_copy(node->u.i.else_node);
849 __isl_give isl_ast_expr *isl_ast_node_if_get_cond(
850 __isl_keep isl_ast_node *node)
852 if (!node)
853 return NULL;
854 if (node->type != isl_ast_node_if)
855 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
856 "not a guard node", return NULL);
857 return isl_ast_expr_copy(node->u.i.guard);
860 __isl_give isl_ast_node_list *isl_ast_node_block_get_children(
861 __isl_keep isl_ast_node *node)
863 if (!node)
864 return NULL;
865 if (node->type != isl_ast_node_block)
866 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
867 "not a block node", return NULL);
868 return isl_ast_node_list_copy(node->u.b.children);
871 __isl_give isl_ast_expr *isl_ast_node_user_get_expr(
872 __isl_keep isl_ast_node *node)
874 if (!node)
875 return NULL;
877 return isl_ast_expr_copy(node->u.e.expr);
880 __isl_give isl_id *isl_ast_node_get_annotation(__isl_keep isl_ast_node *node)
882 return node ? isl_id_copy(node->annotation) : NULL;
885 /* Replace node->annotation by "annotation".
887 __isl_give isl_ast_node *isl_ast_node_set_annotation(
888 __isl_take isl_ast_node *node, __isl_take isl_id *annotation)
890 node = isl_ast_node_cow(node);
891 if (!node || !annotation)
892 goto error;
894 isl_id_free(node->annotation);
895 node->annotation = annotation;
897 return node;
898 error:
899 isl_id_free(annotation);
900 return isl_ast_node_free(node);
903 /* Textual C representation of the various operators.
905 static char *op_str[] = {
906 [isl_ast_op_and] = "&&",
907 [isl_ast_op_and_then] = "&&",
908 [isl_ast_op_or] = "||",
909 [isl_ast_op_or_else] = "||",
910 [isl_ast_op_max] = "max",
911 [isl_ast_op_min] = "min",
912 [isl_ast_op_minus] = "-",
913 [isl_ast_op_add] = "+",
914 [isl_ast_op_sub] = "-",
915 [isl_ast_op_mul] = "*",
916 [isl_ast_op_pdiv_q] = "/",
917 [isl_ast_op_pdiv_r] = "%",
918 [isl_ast_op_div] = "/",
919 [isl_ast_op_eq] = "==",
920 [isl_ast_op_le] = "<=",
921 [isl_ast_op_ge] = ">="
924 /* Precedence in C of the various operators.
925 * Based on http://en.wikipedia.org/wiki/Operators_in_C_and_C++
926 * Lowest value means highest precedence.
928 static int op_prec[] = {
929 [isl_ast_op_and] = 13,
930 [isl_ast_op_and_then] = 13,
931 [isl_ast_op_or] = 14,
932 [isl_ast_op_or_else] = 14,
933 [isl_ast_op_max] = 2,
934 [isl_ast_op_min] = 2,
935 [isl_ast_op_minus] = 3,
936 [isl_ast_op_add] = 6,
937 [isl_ast_op_sub] = 6,
938 [isl_ast_op_mul] = 5,
939 [isl_ast_op_div] = 5,
940 [isl_ast_op_fdiv_q] = 2,
941 [isl_ast_op_pdiv_q] = 5,
942 [isl_ast_op_pdiv_r] = 5,
943 [isl_ast_op_cond] = 15,
944 [isl_ast_op_select] = 15,
945 [isl_ast_op_eq] = 9,
946 [isl_ast_op_le] = 8,
947 [isl_ast_op_ge] = 8,
948 [isl_ast_op_call] = 2
951 /* Is the operator left-to-right associative?
953 static int op_left[] = {
954 [isl_ast_op_and] = 1,
955 [isl_ast_op_and_then] = 1,
956 [isl_ast_op_or] = 1,
957 [isl_ast_op_or_else] = 1,
958 [isl_ast_op_max] = 1,
959 [isl_ast_op_min] = 1,
960 [isl_ast_op_minus] = 0,
961 [isl_ast_op_add] = 1,
962 [isl_ast_op_sub] = 1,
963 [isl_ast_op_mul] = 1,
964 [isl_ast_op_div] = 1,
965 [isl_ast_op_fdiv_q] = 1,
966 [isl_ast_op_pdiv_q] = 1,
967 [isl_ast_op_pdiv_r] = 1,
968 [isl_ast_op_cond] = 0,
969 [isl_ast_op_select] = 0,
970 [isl_ast_op_eq] = 1,
971 [isl_ast_op_le] = 1,
972 [isl_ast_op_ge] = 1,
973 [isl_ast_op_call] = 1
976 static int is_and(enum isl_ast_op_type op)
978 return op == isl_ast_op_and || op == isl_ast_op_and_then;
981 static int is_or(enum isl_ast_op_type op)
983 return op == isl_ast_op_or || op == isl_ast_op_or_else;
986 static int is_add_sub(enum isl_ast_op_type op)
988 return op == isl_ast_op_add || op == isl_ast_op_sub;
991 static int is_div_mod(enum isl_ast_op_type op)
993 return op == isl_ast_op_div || op == isl_ast_op_pdiv_r;
996 /* Do we need/want parentheses around "expr" as a subexpression of
997 * an "op" operation? If "left" is set, then "expr" is the left-most
998 * operand.
1000 * We only need parentheses if "expr" represents an operation.
1002 * If op has a higher precedence than expr->u.op.op, then we need
1003 * parentheses.
1004 * If op and expr->u.op.op have the same precedence, but the operations
1005 * are performed in an order that is different from the associativity,
1006 * then we need parentheses.
1008 * An and inside an or technically does not require parentheses,
1009 * but some compilers complain about that, so we add them anyway.
1011 * Computations such as "a / b * c" and "a % b + c" can be somewhat
1012 * difficult to read, so we add parentheses for those as well.
1014 static int sub_expr_need_parens(enum isl_ast_op_type op,
1015 __isl_keep isl_ast_expr *expr, int left)
1017 if (expr->type != isl_ast_expr_op)
1018 return 0;
1020 if (op_prec[expr->u.op.op] > op_prec[op])
1021 return 1;
1022 if (op_prec[expr->u.op.op] == op_prec[op] && left != op_left[op])
1023 return 1;
1025 if (is_or(op) && is_and(expr->u.op.op))
1026 return 1;
1027 if (op == isl_ast_op_mul && expr->u.op.op != isl_ast_op_mul &&
1028 op_prec[expr->u.op.op] == op_prec[op])
1029 return 1;
1030 if (is_add_sub(op) && is_div_mod(expr->u.op.op))
1031 return 1;
1033 return 0;
1036 /* Print "expr" as a subexpression of an "op" operation.
1037 * If "left" is set, then "expr" is the left-most operand.
1039 static __isl_give isl_printer *print_sub_expr(__isl_take isl_printer *p,
1040 enum isl_ast_op_type op, __isl_keep isl_ast_expr *expr, int left)
1042 int need_parens;
1044 need_parens = sub_expr_need_parens(op, expr, left);
1046 if (need_parens)
1047 p = isl_printer_print_str(p, "(");
1048 p = isl_printer_print_ast_expr(p, expr);
1049 if (need_parens)
1050 p = isl_printer_print_str(p, ")");
1051 return p;
1054 /* Print a min or max reduction "expr".
1056 static __isl_give isl_printer *print_min_max(__isl_take isl_printer *p,
1057 __isl_keep isl_ast_expr *expr)
1059 int i = 0;
1061 for (i = 1; i < expr->u.op.n_arg; ++i) {
1062 p = isl_printer_print_str(p, op_str[expr->u.op.op]);
1063 p = isl_printer_print_str(p, "(");
1065 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1066 for (i = 1; i < expr->u.op.n_arg; ++i) {
1067 p = isl_printer_print_str(p, ", ");
1068 p = isl_printer_print_ast_expr(p, expr->u.op.args[i]);
1069 p = isl_printer_print_str(p, ")");
1072 return p;
1075 /* Print a function call "expr".
1077 * The first argument represents the function to be called.
1079 static __isl_give isl_printer *print_call(__isl_take isl_printer *p,
1080 __isl_keep isl_ast_expr *expr)
1082 int i = 0;
1084 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1085 p = isl_printer_print_str(p, "(");
1086 for (i = 1; i < expr->u.op.n_arg; ++i) {
1087 if (i != 1)
1088 p = isl_printer_print_str(p, ", ");
1089 p = isl_printer_print_ast_expr(p, expr->u.op.args[i]);
1091 p = isl_printer_print_str(p, ")");
1093 return p;
1096 /* Print "expr" to "p".
1098 * If we are printing in isl format, then we also print an indication
1099 * of the size of the expression (if it was computed).
1101 __isl_give isl_printer *isl_printer_print_ast_expr(__isl_take isl_printer *p,
1102 __isl_keep isl_ast_expr *expr)
1104 if (!p)
1105 return NULL;
1106 if (!expr)
1107 return isl_printer_free(p);
1109 switch (expr->type) {
1110 case isl_ast_expr_op:
1111 if (expr->u.op.op == isl_ast_op_call) {
1112 p = print_call(p, expr);
1113 break;
1115 if (expr->u.op.n_arg == 1) {
1116 p = isl_printer_print_str(p, op_str[expr->u.op.op]);
1117 p = print_sub_expr(p, expr->u.op.op,
1118 expr->u.op.args[0], 0);
1119 break;
1121 if (expr->u.op.op == isl_ast_op_fdiv_q) {
1122 p = isl_printer_print_str(p, "floord(");
1123 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1124 p = isl_printer_print_str(p, ", ");
1125 p = isl_printer_print_ast_expr(p, expr->u.op.args[1]);
1126 p = isl_printer_print_str(p, ")");
1127 break;
1129 if (expr->u.op.op == isl_ast_op_max ||
1130 expr->u.op.op == isl_ast_op_min) {
1131 p = print_min_max(p, expr);
1132 break;
1134 if (expr->u.op.op == isl_ast_op_cond ||
1135 expr->u.op.op == isl_ast_op_select) {
1136 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1137 p = isl_printer_print_str(p, " ? ");
1138 p = isl_printer_print_ast_expr(p, expr->u.op.args[1]);
1139 p = isl_printer_print_str(p, " : ");
1140 p = isl_printer_print_ast_expr(p, expr->u.op.args[2]);
1141 break;
1143 if (expr->u.op.n_arg != 2)
1144 isl_die(isl_printer_get_ctx(p), isl_error_internal,
1145 "operation should have two arguments",
1146 goto error);
1147 p = print_sub_expr(p, expr->u.op.op, expr->u.op.args[0], 1);
1148 p = isl_printer_print_str(p, " ");
1149 p = isl_printer_print_str(p, op_str[expr->u.op.op]);
1150 p = isl_printer_print_str(p, " ");
1151 p = print_sub_expr(p, expr->u.op.op, expr->u.op.args[1], 0);
1152 break;
1153 case isl_ast_expr_id:
1154 p = isl_printer_print_str(p, isl_id_get_name(expr->u.id));
1155 break;
1156 case isl_ast_expr_int:
1157 p = isl_printer_print_isl_int(p, expr->u.i);
1158 break;
1159 case isl_ast_expr_error:
1160 break;
1163 return p;
1164 error:
1165 isl_printer_free(p);
1166 return NULL;
1169 /* Print "node" to "p" in "isl format".
1171 static __isl_give isl_printer *print_ast_node_isl(__isl_take isl_printer *p,
1172 __isl_keep isl_ast_node *node)
1174 p = isl_printer_print_str(p, "(");
1175 switch (node->type) {
1176 case isl_ast_node_for:
1177 if (node->u.f.degenerate) {
1178 p = isl_printer_print_ast_expr(p, node->u.f.init);
1179 } else {
1180 p = isl_printer_print_str(p, "init: ");
1181 p = isl_printer_print_ast_expr(p, node->u.f.init);
1182 p = isl_printer_print_str(p, ", ");
1183 p = isl_printer_print_str(p, "cond: ");
1184 p = isl_printer_print_ast_expr(p, node->u.f.cond);
1185 p = isl_printer_print_str(p, ", ");
1186 p = isl_printer_print_str(p, "inc: ");
1187 p = isl_printer_print_ast_expr(p, node->u.f.inc);
1189 if (node->u.f.body) {
1190 p = isl_printer_print_str(p, ", ");
1191 p = isl_printer_print_str(p, "body: ");
1192 p = isl_printer_print_ast_node(p, node->u.f.body);
1194 break;
1195 case isl_ast_node_user:
1196 p = isl_printer_print_ast_expr(p, node->u.e.expr);
1197 break;
1198 case isl_ast_node_if:
1199 p = isl_printer_print_str(p, "guard: ");
1200 p = isl_printer_print_ast_expr(p, node->u.i.guard);
1201 if (node->u.i.then) {
1202 p = isl_printer_print_str(p, ", ");
1203 p = isl_printer_print_str(p, "then: ");
1204 p = isl_printer_print_ast_node(p, node->u.i.then);
1206 if (node->u.i.else_node) {
1207 p = isl_printer_print_str(p, ", ");
1208 p = isl_printer_print_str(p, "else: ");
1209 p = isl_printer_print_ast_node(p, node->u.i.else_node);
1211 break;
1212 case isl_ast_node_block:
1213 p = isl_printer_print_ast_node_list(p, node->u.b.children);
1214 break;
1215 default:
1216 break;
1218 p = isl_printer_print_str(p, ")");
1219 return p;
1222 /* Do we need to print a block around the body "node" of a for or if node?
1224 * If the node is a block, then we need to print a block.
1225 * Also if the node is a degenerate for then we will print it as
1226 * an assignment followed by the body of the for loop, so we need a block
1227 * as well.
1229 static int need_block(__isl_keep isl_ast_node *node)
1231 if (node->type == isl_ast_node_block)
1232 return 1;
1233 if (node->type == isl_ast_node_for && node->u.f.degenerate)
1234 return 1;
1235 return 0;
1238 static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
1239 __isl_keep isl_ast_node *node,
1240 __isl_keep isl_ast_print_options *options, int in_block);
1241 static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
1242 __isl_keep isl_ast_node *node,
1243 __isl_keep isl_ast_print_options *options, int new_line);
1245 /* Print the body "node" of a for or if node.
1246 * If "else_node" is set, then it is printed as well.
1248 * We first check if we need to print out a block.
1249 * We always print out a block if there is an else node to make
1250 * sure that the else node is matched to the correct if node.
1252 * If the else node is itself an if, then we print it as
1254 * } else if (..)
1256 * Otherwise the else node is printed as
1258 * } else
1259 * node
1261 static __isl_give isl_printer *print_body_c(__isl_take isl_printer *p,
1262 __isl_keep isl_ast_node *node, __isl_keep isl_ast_node *else_node,
1263 __isl_keep isl_ast_print_options *options)
1265 if (!node)
1266 return isl_printer_free(p);
1268 if (!else_node && !need_block(node)) {
1269 p = isl_printer_end_line(p);
1270 p = isl_printer_indent(p, 2);
1271 p = isl_ast_node_print(node, p, options);
1272 p = isl_printer_indent(p, -2);
1273 return p;
1276 p = isl_printer_print_str(p, " {");
1277 p = isl_printer_end_line(p);
1278 p = isl_printer_indent(p, 2);
1279 p = print_ast_node_c(p, node, options, 1);
1280 p = isl_printer_indent(p, -2);
1281 p = isl_printer_start_line(p);
1282 p = isl_printer_print_str(p, "}");
1283 if (else_node) {
1284 if (else_node->type == isl_ast_node_if) {
1285 p = isl_printer_print_str(p, " else ");
1286 p = print_if_c(p, else_node, options, 0);
1287 } else {
1288 p = isl_printer_print_str(p, " else");
1289 p = print_body_c(p, else_node, NULL, options);
1291 } else
1292 p = isl_printer_end_line(p);
1294 return p;
1297 /* Print the for node "node".
1299 * If the for node is degenerate, it is printed as
1301 * type iterator = init;
1302 * body
1304 * Otherwise, it is printed as
1306 * for (type iterator = init; cond; iterator += inc)
1307 * body
1309 * "in_block" is set if we are currently inside a block.
1310 * We simply pass it along to print_ast_node_c in case of a degenerate
1311 * for loop.
1313 static __isl_give isl_printer *print_for_c(__isl_take isl_printer *p,
1314 __isl_keep isl_ast_node *node,
1315 __isl_keep isl_ast_print_options *options, int in_block)
1317 isl_id *id;
1318 const char *name;
1319 const char *type;
1321 type = isl_options_get_ast_iterator_type(isl_printer_get_ctx(p));
1322 if (!node->u.f.degenerate) {
1323 id = isl_ast_expr_get_id(node->u.f.iterator);
1324 name = isl_id_get_name(id);
1325 isl_id_free(id);
1326 p = isl_printer_start_line(p);
1327 p = isl_printer_print_str(p, "for (");
1328 p = isl_printer_print_str(p, type);
1329 p = isl_printer_print_str(p, " ");
1330 p = isl_printer_print_str(p, name);
1331 p = isl_printer_print_str(p, " = ");
1332 p = isl_printer_print_ast_expr(p, node->u.f.init);
1333 p = isl_printer_print_str(p, "; ");
1334 p = isl_printer_print_ast_expr(p, node->u.f.cond);
1335 p = isl_printer_print_str(p, "; ");
1336 p = isl_printer_print_str(p, name);
1337 p = isl_printer_print_str(p, " += ");
1338 p = isl_printer_print_ast_expr(p, node->u.f.inc);
1339 p = isl_printer_print_str(p, ")");
1340 p = print_body_c(p, node->u.f.body, NULL, options);
1341 } else {
1342 id = isl_ast_expr_get_id(node->u.f.iterator);
1343 name = isl_id_get_name(id);
1344 isl_id_free(id);
1345 p = isl_printer_start_line(p);
1346 p = isl_printer_print_str(p, type);
1347 p = isl_printer_print_str(p, " ");
1348 p = isl_printer_print_str(p, name);
1349 p = isl_printer_print_str(p, " = ");
1350 p = isl_printer_print_ast_expr(p, node->u.f.init);
1351 p = isl_printer_print_str(p, ";");
1352 p = isl_printer_end_line(p);
1353 p = print_ast_node_c(p, node->u.f.body, options, in_block);
1356 return p;
1359 /* Print the if node "node".
1360 * If "new_line" is set then the if node should be printed on a new line.
1362 static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
1363 __isl_keep isl_ast_node *node,
1364 __isl_keep isl_ast_print_options *options, int new_line)
1366 if (new_line)
1367 p = isl_printer_start_line(p);
1368 p = isl_printer_print_str(p, "if (");
1369 p = isl_printer_print_ast_expr(p, node->u.i.guard);
1370 p = isl_printer_print_str(p, ")");
1371 p = print_body_c(p, node->u.i.then, node->u.i.else_node, options);
1373 return p;
1376 /* Print the "node" to "p".
1378 * "in_block" is set if we are currently inside a block.
1379 * If so, we do not print a block around the children of a block node.
1380 * We do this to avoid an extra block around the body of a degenerate
1381 * for node.
1383 static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
1384 __isl_keep isl_ast_node *node,
1385 __isl_keep isl_ast_print_options *options, int in_block)
1387 switch (node->type) {
1388 case isl_ast_node_for:
1389 if (options->print_for)
1390 return options->print_for(p, node,
1391 options->print_for_user);
1392 p = print_for_c(p, node, options, in_block);
1393 break;
1394 case isl_ast_node_if:
1395 p = print_if_c(p, node, options, 1);
1396 break;
1397 case isl_ast_node_block:
1398 if (!in_block) {
1399 p = isl_printer_start_line(p);
1400 p = isl_printer_print_str(p, "{");
1401 p = isl_printer_end_line(p);
1402 p = isl_printer_indent(p, 2);
1404 p = isl_ast_node_list_print(node->u.b.children, p, options);
1405 if (!in_block) {
1406 p = isl_printer_indent(p, -2);
1407 p = isl_printer_start_line(p);
1408 p = isl_printer_print_str(p, "}");
1409 p = isl_printer_end_line(p);
1411 break;
1412 case isl_ast_node_user:
1413 if (options->print_user)
1414 return options->print_user(p, node,
1415 options->print_user_user);
1416 p = isl_printer_start_line(p);
1417 p = isl_printer_print_ast_expr(p, node->u.e.expr);
1418 p = isl_printer_print_str(p, ";");
1419 p = isl_printer_end_line(p);
1420 break;
1421 case isl_ast_node_error:
1422 break;
1424 return p;
1427 /* Print the for node "node" to "p".
1429 __isl_give isl_printer *isl_ast_node_for_print(__isl_keep isl_ast_node *node,
1430 __isl_take isl_printer *p, __isl_keep isl_ast_print_options *options)
1432 if (!node || !options)
1433 goto error;
1434 if (node->type != isl_ast_node_for)
1435 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1436 "not a for node", goto error);
1437 return print_for_c(p, node, options, 0);
1438 error:
1439 isl_printer_free(p);
1440 return NULL;
1443 /* Print the if node "node" to "p".
1445 __isl_give isl_printer *isl_ast_node_if_print(__isl_keep isl_ast_node *node,
1446 __isl_take isl_printer *p, __isl_keep isl_ast_print_options *options)
1448 if (!node || !options)
1449 goto error;
1450 if (node->type != isl_ast_node_if)
1451 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1452 "not an if node", goto error);
1453 return print_if_c(p, node, options, 1);
1454 error:
1455 isl_printer_free(p);
1456 return NULL;
1459 /* Print "node" to "p".
1461 __isl_give isl_printer *isl_ast_node_print(__isl_keep isl_ast_node *node,
1462 __isl_take isl_printer *p, __isl_keep isl_ast_print_options *options)
1464 if (!options || !node)
1465 goto error;
1466 return print_ast_node_c(p, node, options, 0);
1467 error:
1468 isl_printer_free(p);
1469 return NULL;
1472 /* Print "node" to "p".
1474 __isl_give isl_printer *isl_printer_print_ast_node(__isl_take isl_printer *p,
1475 __isl_keep isl_ast_node *node)
1477 int format;
1478 isl_ast_print_options *options;
1480 if (!p)
1481 return NULL;
1483 format = isl_printer_get_output_format(p);
1484 switch (format) {
1485 case ISL_FORMAT_ISL:
1486 p = print_ast_node_isl(p, node);
1487 break;
1488 case ISL_FORMAT_C:
1489 options = isl_ast_print_options_alloc(isl_printer_get_ctx(p));
1490 p = isl_ast_node_print(node, p, options);
1491 isl_ast_print_options_free(options);
1492 break;
1493 default:
1494 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
1495 "output format not supported for ast_node",
1496 return isl_printer_free(p));
1499 return p;
1502 /* Print the list of nodes "list" to "p".
1504 __isl_give isl_printer *isl_ast_node_list_print(
1505 __isl_keep isl_ast_node_list *list, __isl_take isl_printer *p,
1506 __isl_keep isl_ast_print_options *options)
1508 int i;
1510 if (!p || !list || !options)
1511 return isl_printer_free(p);
1513 for (i = 0; i < list->n; ++i)
1514 p = print_ast_node_c(p, list->p[i], options, 1);
1516 return p;
1519 #define ISL_AST_MACRO_FLOORD (1 << 0)
1520 #define ISL_AST_MACRO_MIN (1 << 1)
1521 #define ISL_AST_MACRO_MAX (1 << 2)
1522 #define ISL_AST_MACRO_ALL (ISL_AST_MACRO_FLOORD | \
1523 ISL_AST_MACRO_MIN | \
1524 ISL_AST_MACRO_MAX)
1526 /* If "expr" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1527 * then set the corresponding bit in "macros".
1529 static int ast_expr_required_macros(__isl_keep isl_ast_expr *expr, int macros)
1531 int i;
1533 if (macros == ISL_AST_MACRO_ALL)
1534 return macros;
1536 if (expr->type != isl_ast_expr_op)
1537 return macros;
1539 if (expr->u.op.op == isl_ast_op_min)
1540 macros |= ISL_AST_MACRO_MIN;
1541 if (expr->u.op.op == isl_ast_op_max)
1542 macros |= ISL_AST_MACRO_MAX;
1543 if (expr->u.op.op == isl_ast_op_fdiv_q)
1544 macros |= ISL_AST_MACRO_FLOORD;
1546 for (i = 0; i < expr->u.op.n_arg; ++i)
1547 macros = ast_expr_required_macros(expr->u.op.args[i], macros);
1549 return macros;
1552 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
1553 int macros);
1555 /* If "node" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1556 * then set the corresponding bit in "macros".
1558 static int ast_node_required_macros(__isl_keep isl_ast_node *node, int macros)
1560 if (macros == ISL_AST_MACRO_ALL)
1561 return macros;
1563 switch (node->type) {
1564 case isl_ast_node_for:
1565 macros = ast_expr_required_macros(node->u.f.init, macros);
1566 if (!node->u.f.degenerate) {
1567 macros = ast_expr_required_macros(node->u.f.cond,
1568 macros);
1569 macros = ast_expr_required_macros(node->u.f.inc,
1570 macros);
1572 macros = ast_node_required_macros(node->u.f.body, macros);
1573 break;
1574 case isl_ast_node_if:
1575 macros = ast_expr_required_macros(node->u.i.guard, macros);
1576 macros = ast_node_required_macros(node->u.i.then, macros);
1577 if (node->u.i.else_node)
1578 macros = ast_node_required_macros(node->u.i.else_node,
1579 macros);
1580 break;
1581 case isl_ast_node_block:
1582 macros = ast_node_list_required_macros(node->u.b.children,
1583 macros);
1584 break;
1585 case isl_ast_node_user:
1586 macros = ast_expr_required_macros(node->u.e.expr, macros);
1587 break;
1588 case isl_ast_node_error:
1589 break;
1592 return macros;
1595 /* If "list" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1596 * then set the corresponding bit in "macros".
1598 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
1599 int macros)
1601 int i;
1603 for (i = 0; i < list->n; ++i)
1604 macros = ast_node_required_macros(list->p[i], macros);
1606 return macros;
1609 /* Print a macro definition for the operator "type".
1611 __isl_give isl_printer *isl_ast_op_type_print_macro(
1612 enum isl_ast_op_type type, __isl_take isl_printer *p)
1614 switch (type) {
1615 case isl_ast_op_min:
1616 p = isl_printer_start_line(p);
1617 p = isl_printer_print_str(p,
1618 "#define min(x,y) ((x) < (y) ? (x) : (y))");
1619 p = isl_printer_end_line(p);
1620 break;
1621 case isl_ast_op_max:
1622 p = isl_printer_start_line(p);
1623 p = isl_printer_print_str(p,
1624 "#define max(x,y) ((x) > (y) ? (x) : (y))");
1625 p = isl_printer_end_line(p);
1626 break;
1627 case isl_ast_op_fdiv_q:
1628 p = isl_printer_start_line(p);
1629 p = isl_printer_print_str(p,
1630 "#define floord(n,d) "
1631 "(((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))");
1632 p = isl_printer_end_line(p);
1633 break;
1634 default:
1635 break;
1638 return p;
1641 /* Call "fn" for each type of operation that appears in "node"
1642 * and that requires a macro definition.
1644 int isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node *node,
1645 int (*fn)(enum isl_ast_op_type type, void *user), void *user)
1647 int macros;
1649 if (!node)
1650 return -1;
1652 macros = ast_node_required_macros(node, 0);
1654 if (macros & ISL_AST_MACRO_MIN && fn(isl_ast_op_min, user) < 0)
1655 return -1;
1656 if (macros & ISL_AST_MACRO_MAX && fn(isl_ast_op_max, user) < 0)
1657 return -1;
1658 if (macros & ISL_AST_MACRO_FLOORD && fn(isl_ast_op_fdiv_q, user) < 0)
1659 return -1;
1661 return 0;
1664 static int ast_op_type_print_macro(enum isl_ast_op_type type, void *user)
1666 isl_printer **p = user;
1668 *p = isl_ast_op_type_print_macro(type, *p);
1670 return 0;
1673 /* Print macro definitions for all the macros used in the result
1674 * of printing "node.
1676 __isl_give isl_printer *isl_ast_node_print_macros(
1677 __isl_keep isl_ast_node *node, __isl_take isl_printer *p)
1679 if (isl_ast_node_foreach_ast_op_type(node,
1680 &ast_op_type_print_macro, &p) < 0)
1681 return isl_printer_free(p);
1682 return p;