isl_ast_graft_list_extract_hoistable_guard: handle call outside band
[isl.git] / isl_ast.c
blob56ed34008c635f30ff4c2fae80f4251a8f60dba2
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 __isl_null isl_ast_print_options *isl_ast_print_options_free(
88 __isl_take isl_ast_print_options *options)
90 if (!options)
91 return NULL;
93 if (--options->ref > 0)
94 return NULL;
96 isl_ctx_deref(options->ctx);
98 free(options);
99 return NULL;
102 /* Set the print_user callback of "options" to "print_user".
104 * If this callback is set, then it used to print user nodes in the AST.
105 * Otherwise, the expression associated to the user node is printed.
107 __isl_give isl_ast_print_options *isl_ast_print_options_set_print_user(
108 __isl_take isl_ast_print_options *options,
109 __isl_give isl_printer *(*print_user)(__isl_take isl_printer *p,
110 __isl_take isl_ast_print_options *options,
111 __isl_keep isl_ast_node *node, void *user),
112 void *user)
114 options = isl_ast_print_options_cow(options);
115 if (!options)
116 return NULL;
118 options->print_user = print_user;
119 options->print_user_user = user;
121 return options;
124 /* Set the print_for callback of "options" to "print_for".
126 * If this callback is set, then it used to print for nodes in the AST.
128 __isl_give isl_ast_print_options *isl_ast_print_options_set_print_for(
129 __isl_take isl_ast_print_options *options,
130 __isl_give isl_printer *(*print_for)(__isl_take isl_printer *p,
131 __isl_take isl_ast_print_options *options,
132 __isl_keep isl_ast_node *node, void *user),
133 void *user)
135 options = isl_ast_print_options_cow(options);
136 if (!options)
137 return NULL;
139 options->print_for = print_for;
140 options->print_for_user = user;
142 return options;
145 __isl_give isl_ast_expr *isl_ast_expr_copy(__isl_keep isl_ast_expr *expr)
147 if (!expr)
148 return NULL;
150 expr->ref++;
151 return expr;
154 __isl_give isl_ast_expr *isl_ast_expr_dup(__isl_keep isl_ast_expr *expr)
156 int i;
157 isl_ctx *ctx;
158 isl_ast_expr *dup;
160 if (!expr)
161 return NULL;
163 ctx = isl_ast_expr_get_ctx(expr);
164 switch (expr->type) {
165 case isl_ast_expr_int:
166 dup = isl_ast_expr_from_val(isl_val_copy(expr->u.v));
167 break;
168 case isl_ast_expr_id:
169 dup = isl_ast_expr_from_id(isl_id_copy(expr->u.id));
170 break;
171 case isl_ast_expr_op:
172 dup = isl_ast_expr_alloc_op(ctx,
173 expr->u.op.op, expr->u.op.n_arg);
174 if (!dup)
175 return NULL;
176 for (i = 0; i < expr->u.op.n_arg; ++i)
177 dup->u.op.args[i] =
178 isl_ast_expr_copy(expr->u.op.args[i]);
179 break;
180 case isl_ast_expr_error:
181 dup = NULL;
184 if (!dup)
185 return NULL;
187 return dup;
190 __isl_give isl_ast_expr *isl_ast_expr_cow(__isl_take isl_ast_expr *expr)
192 if (!expr)
193 return NULL;
195 if (expr->ref == 1)
196 return expr;
197 expr->ref--;
198 return isl_ast_expr_dup(expr);
201 __isl_null isl_ast_expr *isl_ast_expr_free(__isl_take isl_ast_expr *expr)
203 int i;
205 if (!expr)
206 return NULL;
208 if (--expr->ref > 0)
209 return NULL;
211 isl_ctx_deref(expr->ctx);
213 switch (expr->type) {
214 case isl_ast_expr_int:
215 isl_val_free(expr->u.v);
216 break;
217 case isl_ast_expr_id:
218 isl_id_free(expr->u.id);
219 break;
220 case isl_ast_expr_op:
221 if (expr->u.op.args)
222 for (i = 0; i < expr->u.op.n_arg; ++i)
223 isl_ast_expr_free(expr->u.op.args[i]);
224 free(expr->u.op.args);
225 break;
226 case isl_ast_expr_error:
227 break;
230 free(expr);
231 return NULL;
234 isl_ctx *isl_ast_expr_get_ctx(__isl_keep isl_ast_expr *expr)
236 return expr ? expr->ctx : NULL;
239 enum isl_ast_expr_type isl_ast_expr_get_type(__isl_keep isl_ast_expr *expr)
241 return expr ? expr->type : isl_ast_expr_error;
244 /* Return the integer value represented by "expr".
246 __isl_give isl_val *isl_ast_expr_get_val(__isl_keep isl_ast_expr *expr)
248 if (!expr)
249 return NULL;
250 if (expr->type != isl_ast_expr_int)
251 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
252 "expression not an int", return NULL);
253 return isl_val_copy(expr->u.v);
256 __isl_give isl_id *isl_ast_expr_get_id(__isl_keep isl_ast_expr *expr)
258 if (!expr)
259 return NULL;
260 if (expr->type != isl_ast_expr_id)
261 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
262 "expression not an identifier", return NULL);
264 return isl_id_copy(expr->u.id);
267 enum isl_ast_op_type isl_ast_expr_get_op_type(__isl_keep isl_ast_expr *expr)
269 if (!expr)
270 return isl_ast_op_error;
271 if (expr->type != isl_ast_expr_op)
272 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
273 "expression not an operation", return isl_ast_op_error);
274 return expr->u.op.op;
277 int isl_ast_expr_get_op_n_arg(__isl_keep isl_ast_expr *expr)
279 if (!expr)
280 return -1;
281 if (expr->type != isl_ast_expr_op)
282 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
283 "expression not an operation", return -1);
284 return expr->u.op.n_arg;
287 __isl_give isl_ast_expr *isl_ast_expr_get_op_arg(__isl_keep isl_ast_expr *expr,
288 int pos)
290 if (!expr)
291 return NULL;
292 if (expr->type != isl_ast_expr_op)
293 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
294 "expression not an operation", return NULL);
295 if (pos < 0 || pos >= expr->u.op.n_arg)
296 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
297 "index out of bounds", return NULL);
299 return isl_ast_expr_copy(expr->u.op.args[pos]);
302 /* Replace the argument at position "pos" of "expr" by "arg".
304 __isl_give isl_ast_expr *isl_ast_expr_set_op_arg(__isl_take isl_ast_expr *expr,
305 int pos, __isl_take isl_ast_expr *arg)
307 expr = isl_ast_expr_cow(expr);
308 if (!expr || !arg)
309 goto error;
310 if (expr->type != isl_ast_expr_op)
311 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
312 "expression not an operation", goto error);
313 if (pos < 0 || pos >= expr->u.op.n_arg)
314 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
315 "index out of bounds", goto error);
317 isl_ast_expr_free(expr->u.op.args[pos]);
318 expr->u.op.args[pos] = arg;
320 return expr;
321 error:
322 isl_ast_expr_free(arg);
323 return isl_ast_expr_free(expr);
326 /* Is "expr1" equal to "expr2"?
328 int isl_ast_expr_is_equal(__isl_keep isl_ast_expr *expr1,
329 __isl_keep isl_ast_expr *expr2)
331 int i;
333 if (!expr1 || !expr2)
334 return -1;
336 if (expr1 == expr2)
337 return 1;
338 if (expr1->type != expr2->type)
339 return 0;
340 switch (expr1->type) {
341 case isl_ast_expr_int:
342 return isl_val_eq(expr1->u.v, expr2->u.v);
343 case isl_ast_expr_id:
344 return expr1->u.id == expr2->u.id;
345 case isl_ast_expr_op:
346 if (expr1->u.op.op != expr2->u.op.op)
347 return 0;
348 if (expr1->u.op.n_arg != expr2->u.op.n_arg)
349 return 0;
350 for (i = 0; i < expr1->u.op.n_arg; ++i) {
351 int equal;
352 equal = isl_ast_expr_is_equal(expr1->u.op.args[i],
353 expr2->u.op.args[i]);
354 if (equal < 0 || !equal)
355 return equal;
357 return 1;
358 case isl_ast_expr_error:
359 return -1;
363 /* Create a new operation expression of operation type "op",
364 * with "n_arg" as yet unspecified arguments.
366 __isl_give isl_ast_expr *isl_ast_expr_alloc_op(isl_ctx *ctx,
367 enum isl_ast_op_type op, int n_arg)
369 isl_ast_expr *expr;
371 expr = isl_calloc_type(ctx, isl_ast_expr);
372 if (!expr)
373 return NULL;
375 expr->ctx = ctx;
376 isl_ctx_ref(ctx);
377 expr->ref = 1;
378 expr->type = isl_ast_expr_op;
379 expr->u.op.op = op;
380 expr->u.op.n_arg = n_arg;
381 expr->u.op.args = isl_calloc_array(ctx, isl_ast_expr *, n_arg);
383 if (n_arg && !expr->u.op.args)
384 return isl_ast_expr_free(expr);
386 return expr;
389 /* Create a new id expression representing "id".
391 __isl_give isl_ast_expr *isl_ast_expr_from_id(__isl_take isl_id *id)
393 isl_ctx *ctx;
394 isl_ast_expr *expr;
396 if (!id)
397 return NULL;
399 ctx = isl_id_get_ctx(id);
400 expr = isl_calloc_type(ctx, isl_ast_expr);
401 if (!expr)
402 goto error;
404 expr->ctx = ctx;
405 isl_ctx_ref(ctx);
406 expr->ref = 1;
407 expr->type = isl_ast_expr_id;
408 expr->u.id = id;
410 return expr;
411 error:
412 isl_id_free(id);
413 return NULL;
416 /* Create a new integer expression representing "i".
418 __isl_give isl_ast_expr *isl_ast_expr_alloc_int_si(isl_ctx *ctx, int i)
420 isl_ast_expr *expr;
422 expr = isl_calloc_type(ctx, isl_ast_expr);
423 if (!expr)
424 return NULL;
426 expr->ctx = ctx;
427 isl_ctx_ref(ctx);
428 expr->ref = 1;
429 expr->type = isl_ast_expr_int;
430 expr->u.v = isl_val_int_from_si(ctx, i);
431 if (!expr->u.v)
432 return isl_ast_expr_free(expr);
434 return expr;
437 /* Create a new integer expression representing "v".
439 __isl_give isl_ast_expr *isl_ast_expr_from_val(__isl_take isl_val *v)
441 isl_ctx *ctx;
442 isl_ast_expr *expr;
444 if (!v)
445 return NULL;
446 if (!isl_val_is_int(v))
447 isl_die(isl_val_get_ctx(v), isl_error_invalid,
448 "expecting integer value", goto error);
450 ctx = isl_val_get_ctx(v);
451 expr = isl_calloc_type(ctx, isl_ast_expr);
452 if (!expr)
453 goto error;
455 expr->ctx = ctx;
456 isl_ctx_ref(ctx);
457 expr->ref = 1;
458 expr->type = isl_ast_expr_int;
459 expr->u.v = v;
461 return expr;
462 error:
463 isl_val_free(v);
464 return NULL;
467 /* Create an expression representing the unary operation "type" applied to
468 * "arg".
470 __isl_give isl_ast_expr *isl_ast_expr_alloc_unary(enum isl_ast_op_type type,
471 __isl_take isl_ast_expr *arg)
473 isl_ctx *ctx;
474 isl_ast_expr *expr = NULL;
476 if (!arg)
477 return NULL;
479 ctx = isl_ast_expr_get_ctx(arg);
480 expr = isl_ast_expr_alloc_op(ctx, type, 1);
481 if (!expr)
482 goto error;
484 expr->u.op.args[0] = arg;
486 return expr;
487 error:
488 isl_ast_expr_free(arg);
489 return NULL;
492 /* Create an expression representing the negation of "arg".
494 __isl_give isl_ast_expr *isl_ast_expr_neg(__isl_take isl_ast_expr *arg)
496 return isl_ast_expr_alloc_unary(isl_ast_op_minus, arg);
499 /* Create an expression representing the address of "expr".
501 __isl_give isl_ast_expr *isl_ast_expr_address_of(__isl_take isl_ast_expr *expr)
503 if (!expr)
504 return NULL;
506 if (isl_ast_expr_get_type(expr) != isl_ast_expr_op ||
507 isl_ast_expr_get_op_type(expr) != isl_ast_op_access)
508 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
509 "can only take address of access expressions",
510 return isl_ast_expr_free(expr));
512 return isl_ast_expr_alloc_unary(isl_ast_op_address_of, expr);
515 /* Create an expression representing the binary operation "type"
516 * applied to "expr1" and "expr2".
518 __isl_give isl_ast_expr *isl_ast_expr_alloc_binary(enum isl_ast_op_type type,
519 __isl_take isl_ast_expr *expr1, __isl_take isl_ast_expr *expr2)
521 isl_ctx *ctx;
522 isl_ast_expr *expr = NULL;
524 if (!expr1 || !expr2)
525 goto error;
527 ctx = isl_ast_expr_get_ctx(expr1);
528 expr = isl_ast_expr_alloc_op(ctx, type, 2);
529 if (!expr)
530 goto error;
532 expr->u.op.args[0] = expr1;
533 expr->u.op.args[1] = expr2;
535 return expr;
536 error:
537 isl_ast_expr_free(expr1);
538 isl_ast_expr_free(expr2);
539 return NULL;
542 /* Create an expression representing the sum of "expr1" and "expr2".
544 __isl_give isl_ast_expr *isl_ast_expr_add(__isl_take isl_ast_expr *expr1,
545 __isl_take isl_ast_expr *expr2)
547 return isl_ast_expr_alloc_binary(isl_ast_op_add, expr1, expr2);
550 /* Create an expression representing the difference of "expr1" and "expr2".
552 __isl_give isl_ast_expr *isl_ast_expr_sub(__isl_take isl_ast_expr *expr1,
553 __isl_take isl_ast_expr *expr2)
555 return isl_ast_expr_alloc_binary(isl_ast_op_sub, expr1, expr2);
558 /* Create an expression representing the product of "expr1" and "expr2".
560 __isl_give isl_ast_expr *isl_ast_expr_mul(__isl_take isl_ast_expr *expr1,
561 __isl_take isl_ast_expr *expr2)
563 return isl_ast_expr_alloc_binary(isl_ast_op_mul, expr1, expr2);
566 /* Create an expression representing the quotient of "expr1" and "expr2".
568 __isl_give isl_ast_expr *isl_ast_expr_div(__isl_take isl_ast_expr *expr1,
569 __isl_take isl_ast_expr *expr2)
571 return isl_ast_expr_alloc_binary(isl_ast_op_div, expr1, expr2);
574 /* Create an expression representing the quotient of the integer
575 * division of "expr1" by "expr2", where "expr1" is known to be
576 * non-negative.
578 __isl_give isl_ast_expr *isl_ast_expr_pdiv_q(__isl_take isl_ast_expr *expr1,
579 __isl_take isl_ast_expr *expr2)
581 return isl_ast_expr_alloc_binary(isl_ast_op_pdiv_q, expr1, expr2);
584 /* Create an expression representing the remainder of the integer
585 * division of "expr1" by "expr2", where "expr1" is known to be
586 * non-negative.
588 __isl_give isl_ast_expr *isl_ast_expr_pdiv_r(__isl_take isl_ast_expr *expr1,
589 __isl_take isl_ast_expr *expr2)
591 return isl_ast_expr_alloc_binary(isl_ast_op_pdiv_r, expr1, expr2);
594 /* Create an expression representing the conjunction of "expr1" and "expr2".
596 __isl_give isl_ast_expr *isl_ast_expr_and(__isl_take isl_ast_expr *expr1,
597 __isl_take isl_ast_expr *expr2)
599 return isl_ast_expr_alloc_binary(isl_ast_op_and, expr1, expr2);
602 /* Create an expression representing the conjunction of "expr1" and "expr2",
603 * where "expr2" is evaluated only if "expr1" is evaluated to true.
605 __isl_give isl_ast_expr *isl_ast_expr_and_then(__isl_take isl_ast_expr *expr1,
606 __isl_take isl_ast_expr *expr2)
608 return isl_ast_expr_alloc_binary(isl_ast_op_and_then, expr1, expr2);
611 /* Create an expression representing the disjunction of "expr1" and "expr2".
613 __isl_give isl_ast_expr *isl_ast_expr_or(__isl_take isl_ast_expr *expr1,
614 __isl_take isl_ast_expr *expr2)
616 return isl_ast_expr_alloc_binary(isl_ast_op_or, expr1, expr2);
619 /* Create an expression representing the disjunction of "expr1" and "expr2",
620 * where "expr2" is evaluated only if "expr1" is evaluated to false.
622 __isl_give isl_ast_expr *isl_ast_expr_or_else(__isl_take isl_ast_expr *expr1,
623 __isl_take isl_ast_expr *expr2)
625 return isl_ast_expr_alloc_binary(isl_ast_op_or_else, expr1, expr2);
628 /* Create an expression representing "expr1" less than or equal to "expr2".
630 __isl_give isl_ast_expr *isl_ast_expr_le(__isl_take isl_ast_expr *expr1,
631 __isl_take isl_ast_expr *expr2)
633 return isl_ast_expr_alloc_binary(isl_ast_op_le, expr1, expr2);
636 /* Create an expression representing "expr1" less than "expr2".
638 __isl_give isl_ast_expr *isl_ast_expr_lt(__isl_take isl_ast_expr *expr1,
639 __isl_take isl_ast_expr *expr2)
641 return isl_ast_expr_alloc_binary(isl_ast_op_lt, expr1, expr2);
644 /* Create an expression representing "expr1" greater than or equal to "expr2".
646 __isl_give isl_ast_expr *isl_ast_expr_ge(__isl_take isl_ast_expr *expr1,
647 __isl_take isl_ast_expr *expr2)
649 return isl_ast_expr_alloc_binary(isl_ast_op_ge, expr1, expr2);
652 /* Create an expression representing "expr1" greater than "expr2".
654 __isl_give isl_ast_expr *isl_ast_expr_gt(__isl_take isl_ast_expr *expr1,
655 __isl_take isl_ast_expr *expr2)
657 return isl_ast_expr_alloc_binary(isl_ast_op_gt, expr1, expr2);
660 /* Create an expression representing "expr1" equal to "expr2".
662 __isl_give isl_ast_expr *isl_ast_expr_eq(__isl_take isl_ast_expr *expr1,
663 __isl_take isl_ast_expr *expr2)
665 return isl_ast_expr_alloc_binary(isl_ast_op_eq, expr1, expr2);
668 /* Create an expression of type "type" with as arguments "arg0" followed
669 * by "arguments".
671 static __isl_give isl_ast_expr *ast_expr_with_arguments(
672 enum isl_ast_op_type type, __isl_take isl_ast_expr *arg0,
673 __isl_take isl_ast_expr_list *arguments)
675 int i, n;
676 isl_ctx *ctx;
677 isl_ast_expr *res = NULL;
679 if (!arg0 || !arguments)
680 goto error;
682 ctx = isl_ast_expr_get_ctx(arg0);
683 n = isl_ast_expr_list_n_ast_expr(arguments);
684 res = isl_ast_expr_alloc_op(ctx, type, 1 + n);
685 if (!res)
686 goto error;
687 for (i = 0; i < n; ++i) {
688 isl_ast_expr *arg;
689 arg = isl_ast_expr_list_get_ast_expr(arguments, i);
690 res->u.op.args[1 + i] = arg;
691 if (!arg)
692 goto error;
694 res->u.op.args[0] = arg0;
696 isl_ast_expr_list_free(arguments);
697 return res;
698 error:
699 isl_ast_expr_free(arg0);
700 isl_ast_expr_list_free(arguments);
701 isl_ast_expr_free(res);
702 return NULL;
705 /* Create an expression representing an access to "array" with index
706 * expressions "indices".
708 __isl_give isl_ast_expr *isl_ast_expr_access(__isl_take isl_ast_expr *array,
709 __isl_take isl_ast_expr_list *indices)
711 return ast_expr_with_arguments(isl_ast_op_access, array, indices);
714 /* Create an expression representing a call to "function" with argument
715 * expressions "arguments".
717 __isl_give isl_ast_expr *isl_ast_expr_call(__isl_take isl_ast_expr *function,
718 __isl_take isl_ast_expr_list *arguments)
720 return ast_expr_with_arguments(isl_ast_op_call, function, arguments);
723 /* For each subexpression of "expr" of type isl_ast_expr_id,
724 * if it appears in "id2expr", then replace it by the corresponding
725 * expression.
727 __isl_give isl_ast_expr *isl_ast_expr_substitute_ids(
728 __isl_take isl_ast_expr *expr, __isl_take isl_id_to_ast_expr *id2expr)
730 int i;
731 isl_id *id;
733 if (!expr || !id2expr)
734 goto error;
736 switch (expr->type) {
737 case isl_ast_expr_int:
738 break;
739 case isl_ast_expr_id:
740 if (!isl_id_to_ast_expr_has(id2expr, expr->u.id))
741 break;
742 id = isl_id_copy(expr->u.id);
743 isl_ast_expr_free(expr);
744 expr = isl_id_to_ast_expr_get(id2expr, id);
745 break;
746 case isl_ast_expr_op:
747 for (i = 0; i < expr->u.op.n_arg; ++i) {
748 isl_ast_expr *arg;
749 arg = isl_ast_expr_copy(expr->u.op.args[i]);
750 arg = isl_ast_expr_substitute_ids(arg,
751 isl_id_to_ast_expr_copy(id2expr));
752 if (arg == expr->u.op.args[i]) {
753 isl_ast_expr_free(arg);
754 continue;
756 if (!arg)
757 expr = isl_ast_expr_free(expr);
758 expr = isl_ast_expr_cow(expr);
759 if (!expr) {
760 isl_ast_expr_free(arg);
761 break;
763 isl_ast_expr_free(expr->u.op.args[i]);
764 expr->u.op.args[i] = arg;
766 break;
767 case isl_ast_expr_error:
768 expr = isl_ast_expr_free(expr);
769 break;
772 isl_id_to_ast_expr_free(id2expr);
773 return expr;
774 error:
775 isl_ast_expr_free(expr);
776 isl_id_to_ast_expr_free(id2expr);
777 return NULL;
780 isl_ctx *isl_ast_node_get_ctx(__isl_keep isl_ast_node *node)
782 return node ? node->ctx : NULL;
785 enum isl_ast_node_type isl_ast_node_get_type(__isl_keep isl_ast_node *node)
787 return node ? node->type : isl_ast_node_error;
790 __isl_give isl_ast_node *isl_ast_node_alloc(isl_ctx *ctx,
791 enum isl_ast_node_type type)
793 isl_ast_node *node;
795 node = isl_calloc_type(ctx, isl_ast_node);
796 if (!node)
797 return NULL;
799 node->ctx = ctx;
800 isl_ctx_ref(ctx);
801 node->ref = 1;
802 node->type = type;
804 return node;
807 /* Create an if node with the given guard.
809 * The then body needs to be filled in later.
811 __isl_give isl_ast_node *isl_ast_node_alloc_if(__isl_take isl_ast_expr *guard)
813 isl_ast_node *node;
815 if (!guard)
816 return NULL;
818 node = isl_ast_node_alloc(isl_ast_expr_get_ctx(guard), isl_ast_node_if);
819 if (!node)
820 goto error;
821 node->u.i.guard = guard;
823 return node;
824 error:
825 isl_ast_expr_free(guard);
826 return NULL;
829 /* Create a for node with the given iterator.
831 * The remaining fields need to be filled in later.
833 __isl_give isl_ast_node *isl_ast_node_alloc_for(__isl_take isl_id *id)
835 isl_ast_node *node;
836 isl_ctx *ctx;
838 if (!id)
839 return NULL;
841 ctx = isl_id_get_ctx(id);
842 node = isl_ast_node_alloc(ctx, isl_ast_node_for);
843 if (!node)
844 goto error;
846 node->u.f.iterator = isl_ast_expr_from_id(id);
847 if (!node->u.f.iterator)
848 return isl_ast_node_free(node);
850 return node;
851 error:
852 isl_id_free(id);
853 return NULL;
856 /* Create a user node evaluating "expr".
858 __isl_give isl_ast_node *isl_ast_node_alloc_user(__isl_take isl_ast_expr *expr)
860 isl_ctx *ctx;
861 isl_ast_node *node;
863 if (!expr)
864 return NULL;
866 ctx = isl_ast_expr_get_ctx(expr);
867 node = isl_ast_node_alloc(ctx, isl_ast_node_user);
868 if (!node)
869 goto error;
871 node->u.e.expr = expr;
873 return node;
874 error:
875 isl_ast_expr_free(expr);
876 return NULL;
879 /* Create a block node with the given children.
881 __isl_give isl_ast_node *isl_ast_node_alloc_block(
882 __isl_take isl_ast_node_list *list)
884 isl_ast_node *node;
885 isl_ctx *ctx;
887 if (!list)
888 return NULL;
890 ctx = isl_ast_node_list_get_ctx(list);
891 node = isl_ast_node_alloc(ctx, isl_ast_node_block);
892 if (!node)
893 goto error;
895 node->u.b.children = list;
897 return node;
898 error:
899 isl_ast_node_list_free(list);
900 return NULL;
903 /* Represent the given list of nodes as a single node, either by
904 * extract the node from a single element list or by creating
905 * a block node with the list of nodes as children.
907 __isl_give isl_ast_node *isl_ast_node_from_ast_node_list(
908 __isl_take isl_ast_node_list *list)
910 isl_ast_node *node;
912 if (isl_ast_node_list_n_ast_node(list) != 1)
913 return isl_ast_node_alloc_block(list);
915 node = isl_ast_node_list_get_ast_node(list, 0);
916 isl_ast_node_list_free(list);
918 return node;
921 __isl_give isl_ast_node *isl_ast_node_copy(__isl_keep isl_ast_node *node)
923 if (!node)
924 return NULL;
926 node->ref++;
927 return node;
930 __isl_give isl_ast_node *isl_ast_node_dup(__isl_keep isl_ast_node *node)
932 isl_ast_node *dup;
934 if (!node)
935 return NULL;
937 dup = isl_ast_node_alloc(isl_ast_node_get_ctx(node), node->type);
938 if (!dup)
939 return NULL;
941 switch (node->type) {
942 case isl_ast_node_if:
943 dup->u.i.guard = isl_ast_expr_copy(node->u.i.guard);
944 dup->u.i.then = isl_ast_node_copy(node->u.i.then);
945 dup->u.i.else_node = isl_ast_node_copy(node->u.i.else_node);
946 if (!dup->u.i.guard || !dup->u.i.then ||
947 (node->u.i.else_node && !dup->u.i.else_node))
948 return isl_ast_node_free(dup);
949 break;
950 case isl_ast_node_for:
951 dup->u.f.iterator = isl_ast_expr_copy(node->u.f.iterator);
952 dup->u.f.init = isl_ast_expr_copy(node->u.f.init);
953 dup->u.f.cond = isl_ast_expr_copy(node->u.f.cond);
954 dup->u.f.inc = isl_ast_expr_copy(node->u.f.inc);
955 dup->u.f.body = isl_ast_node_copy(node->u.f.body);
956 if (!dup->u.f.iterator || !dup->u.f.init || !dup->u.f.cond ||
957 !dup->u.f.inc || !dup->u.f.body)
958 return isl_ast_node_free(dup);
959 break;
960 case isl_ast_node_block:
961 dup->u.b.children = isl_ast_node_list_copy(node->u.b.children);
962 if (!dup->u.b.children)
963 return isl_ast_node_free(dup);
964 break;
965 case isl_ast_node_user:
966 dup->u.e.expr = isl_ast_expr_copy(node->u.e.expr);
967 if (!dup->u.e.expr)
968 return isl_ast_node_free(dup);
969 break;
970 case isl_ast_node_error:
971 break;
974 return dup;
977 __isl_give isl_ast_node *isl_ast_node_cow(__isl_take isl_ast_node *node)
979 if (!node)
980 return NULL;
982 if (node->ref == 1)
983 return node;
984 node->ref--;
985 return isl_ast_node_dup(node);
988 __isl_null isl_ast_node *isl_ast_node_free(__isl_take isl_ast_node *node)
990 if (!node)
991 return NULL;
993 if (--node->ref > 0)
994 return NULL;
996 switch (node->type) {
997 case isl_ast_node_if:
998 isl_ast_expr_free(node->u.i.guard);
999 isl_ast_node_free(node->u.i.then);
1000 isl_ast_node_free(node->u.i.else_node);
1001 break;
1002 case isl_ast_node_for:
1003 isl_ast_expr_free(node->u.f.iterator);
1004 isl_ast_expr_free(node->u.f.init);
1005 isl_ast_expr_free(node->u.f.cond);
1006 isl_ast_expr_free(node->u.f.inc);
1007 isl_ast_node_free(node->u.f.body);
1008 break;
1009 case isl_ast_node_block:
1010 isl_ast_node_list_free(node->u.b.children);
1011 break;
1012 case isl_ast_node_user:
1013 isl_ast_expr_free(node->u.e.expr);
1014 break;
1015 case isl_ast_node_error:
1016 break;
1019 isl_id_free(node->annotation);
1020 isl_ctx_deref(node->ctx);
1021 free(node);
1023 return NULL;
1026 /* Replace the body of the for node "node" by "body".
1028 __isl_give isl_ast_node *isl_ast_node_for_set_body(
1029 __isl_take isl_ast_node *node, __isl_take isl_ast_node *body)
1031 node = isl_ast_node_cow(node);
1032 if (!node || !body)
1033 goto error;
1034 if (node->type != isl_ast_node_for)
1035 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1036 "not a for node", goto error);
1038 isl_ast_node_free(node->u.f.body);
1039 node->u.f.body = body;
1041 return node;
1042 error:
1043 isl_ast_node_free(node);
1044 isl_ast_node_free(body);
1045 return NULL;
1048 __isl_give isl_ast_node *isl_ast_node_for_get_body(
1049 __isl_keep isl_ast_node *node)
1051 if (!node)
1052 return NULL;
1053 if (node->type != isl_ast_node_for)
1054 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1055 "not a for node", return NULL);
1056 return isl_ast_node_copy(node->u.f.body);
1059 /* Mark the given for node as being degenerate.
1061 __isl_give isl_ast_node *isl_ast_node_for_mark_degenerate(
1062 __isl_take isl_ast_node *node)
1064 node = isl_ast_node_cow(node);
1065 if (!node)
1066 return NULL;
1067 node->u.f.degenerate = 1;
1068 return node;
1071 int isl_ast_node_for_is_degenerate(__isl_keep isl_ast_node *node)
1073 if (!node)
1074 return -1;
1075 if (node->type != isl_ast_node_for)
1076 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1077 "not a for node", return -1);
1078 return node->u.f.degenerate;
1081 __isl_give isl_ast_expr *isl_ast_node_for_get_iterator(
1082 __isl_keep isl_ast_node *node)
1084 if (!node)
1085 return NULL;
1086 if (node->type != isl_ast_node_for)
1087 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1088 "not a for node", return NULL);
1089 return isl_ast_expr_copy(node->u.f.iterator);
1092 __isl_give isl_ast_expr *isl_ast_node_for_get_init(
1093 __isl_keep isl_ast_node *node)
1095 if (!node)
1096 return NULL;
1097 if (node->type != isl_ast_node_for)
1098 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1099 "not a for node", return NULL);
1100 return isl_ast_expr_copy(node->u.f.init);
1103 /* Return the condition expression of the given for node.
1105 * If the for node is degenerate, then the condition is not explicitly
1106 * stored in the node. Instead, it is constructed as
1108 * iterator <= init
1110 __isl_give isl_ast_expr *isl_ast_node_for_get_cond(
1111 __isl_keep isl_ast_node *node)
1113 if (!node)
1114 return NULL;
1115 if (node->type != isl_ast_node_for)
1116 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1117 "not a for node", return NULL);
1118 if (!node->u.f.degenerate)
1119 return isl_ast_expr_copy(node->u.f.cond);
1121 return isl_ast_expr_alloc_binary(isl_ast_op_le,
1122 isl_ast_expr_copy(node->u.f.iterator),
1123 isl_ast_expr_copy(node->u.f.init));
1126 /* Return the increment of the given for node.
1128 * If the for node is degenerate, then the increment is not explicitly
1129 * stored in the node. We simply return "1".
1131 __isl_give isl_ast_expr *isl_ast_node_for_get_inc(
1132 __isl_keep isl_ast_node *node)
1134 if (!node)
1135 return NULL;
1136 if (node->type != isl_ast_node_for)
1137 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1138 "not a for node", return NULL);
1139 if (!node->u.f.degenerate)
1140 return isl_ast_expr_copy(node->u.f.inc);
1141 return isl_ast_expr_alloc_int_si(isl_ast_node_get_ctx(node), 1);
1144 /* Replace the then branch of the if node "node" by "child".
1146 __isl_give isl_ast_node *isl_ast_node_if_set_then(
1147 __isl_take isl_ast_node *node, __isl_take isl_ast_node *child)
1149 node = isl_ast_node_cow(node);
1150 if (!node || !child)
1151 goto error;
1152 if (node->type != isl_ast_node_if)
1153 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1154 "not an if node", goto error);
1156 isl_ast_node_free(node->u.i.then);
1157 node->u.i.then = child;
1159 return node;
1160 error:
1161 isl_ast_node_free(node);
1162 isl_ast_node_free(child);
1163 return NULL;
1166 __isl_give isl_ast_node *isl_ast_node_if_get_then(
1167 __isl_keep isl_ast_node *node)
1169 if (!node)
1170 return NULL;
1171 if (node->type != isl_ast_node_if)
1172 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1173 "not an if node", return NULL);
1174 return isl_ast_node_copy(node->u.i.then);
1177 int isl_ast_node_if_has_else(
1178 __isl_keep isl_ast_node *node)
1180 if (!node)
1181 return -1;
1182 if (node->type != isl_ast_node_if)
1183 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1184 "not an if node", return -1);
1185 return node->u.i.else_node != NULL;
1188 __isl_give isl_ast_node *isl_ast_node_if_get_else(
1189 __isl_keep isl_ast_node *node)
1191 if (!node)
1192 return NULL;
1193 if (node->type != isl_ast_node_if)
1194 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1195 "not an if node", return NULL);
1196 return isl_ast_node_copy(node->u.i.else_node);
1199 __isl_give isl_ast_expr *isl_ast_node_if_get_cond(
1200 __isl_keep isl_ast_node *node)
1202 if (!node)
1203 return NULL;
1204 if (node->type != isl_ast_node_if)
1205 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1206 "not a guard node", return NULL);
1207 return isl_ast_expr_copy(node->u.i.guard);
1210 __isl_give isl_ast_node_list *isl_ast_node_block_get_children(
1211 __isl_keep isl_ast_node *node)
1213 if (!node)
1214 return NULL;
1215 if (node->type != isl_ast_node_block)
1216 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1217 "not a block node", return NULL);
1218 return isl_ast_node_list_copy(node->u.b.children);
1221 __isl_give isl_ast_expr *isl_ast_node_user_get_expr(
1222 __isl_keep isl_ast_node *node)
1224 if (!node)
1225 return NULL;
1226 if (node->type != isl_ast_node_user)
1227 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1228 "not a user node", return NULL);
1230 return isl_ast_expr_copy(node->u.e.expr);
1233 __isl_give isl_id *isl_ast_node_get_annotation(__isl_keep isl_ast_node *node)
1235 return node ? isl_id_copy(node->annotation) : NULL;
1238 /* Replace node->annotation by "annotation".
1240 __isl_give isl_ast_node *isl_ast_node_set_annotation(
1241 __isl_take isl_ast_node *node, __isl_take isl_id *annotation)
1243 node = isl_ast_node_cow(node);
1244 if (!node || !annotation)
1245 goto error;
1247 isl_id_free(node->annotation);
1248 node->annotation = annotation;
1250 return node;
1251 error:
1252 isl_id_free(annotation);
1253 return isl_ast_node_free(node);
1256 /* Textual C representation of the various operators.
1258 static char *op_str[] = {
1259 [isl_ast_op_and] = "&&",
1260 [isl_ast_op_and_then] = "&&",
1261 [isl_ast_op_or] = "||",
1262 [isl_ast_op_or_else] = "||",
1263 [isl_ast_op_max] = "max",
1264 [isl_ast_op_min] = "min",
1265 [isl_ast_op_minus] = "-",
1266 [isl_ast_op_add] = "+",
1267 [isl_ast_op_sub] = "-",
1268 [isl_ast_op_mul] = "*",
1269 [isl_ast_op_pdiv_q] = "/",
1270 [isl_ast_op_pdiv_r] = "%",
1271 [isl_ast_op_zdiv_r] = "%",
1272 [isl_ast_op_div] = "/",
1273 [isl_ast_op_eq] = "==",
1274 [isl_ast_op_le] = "<=",
1275 [isl_ast_op_ge] = ">=",
1276 [isl_ast_op_lt] = "<",
1277 [isl_ast_op_gt] = ">",
1278 [isl_ast_op_member] = ".",
1279 [isl_ast_op_address_of] = "&"
1282 /* Precedence in C of the various operators.
1283 * Based on http://en.wikipedia.org/wiki/Operators_in_C_and_C++
1284 * Lowest value means highest precedence.
1286 static int op_prec[] = {
1287 [isl_ast_op_and] = 13,
1288 [isl_ast_op_and_then] = 13,
1289 [isl_ast_op_or] = 14,
1290 [isl_ast_op_or_else] = 14,
1291 [isl_ast_op_max] = 2,
1292 [isl_ast_op_min] = 2,
1293 [isl_ast_op_minus] = 3,
1294 [isl_ast_op_add] = 6,
1295 [isl_ast_op_sub] = 6,
1296 [isl_ast_op_mul] = 5,
1297 [isl_ast_op_div] = 5,
1298 [isl_ast_op_fdiv_q] = 2,
1299 [isl_ast_op_pdiv_q] = 5,
1300 [isl_ast_op_pdiv_r] = 5,
1301 [isl_ast_op_zdiv_r] = 5,
1302 [isl_ast_op_cond] = 15,
1303 [isl_ast_op_select] = 15,
1304 [isl_ast_op_eq] = 9,
1305 [isl_ast_op_le] = 8,
1306 [isl_ast_op_ge] = 8,
1307 [isl_ast_op_lt] = 8,
1308 [isl_ast_op_gt] = 8,
1309 [isl_ast_op_call] = 2,
1310 [isl_ast_op_access] = 2,
1311 [isl_ast_op_member] = 2,
1312 [isl_ast_op_address_of] = 3
1315 /* Is the operator left-to-right associative?
1317 static int op_left[] = {
1318 [isl_ast_op_and] = 1,
1319 [isl_ast_op_and_then] = 1,
1320 [isl_ast_op_or] = 1,
1321 [isl_ast_op_or_else] = 1,
1322 [isl_ast_op_max] = 1,
1323 [isl_ast_op_min] = 1,
1324 [isl_ast_op_minus] = 0,
1325 [isl_ast_op_add] = 1,
1326 [isl_ast_op_sub] = 1,
1327 [isl_ast_op_mul] = 1,
1328 [isl_ast_op_div] = 1,
1329 [isl_ast_op_fdiv_q] = 1,
1330 [isl_ast_op_pdiv_q] = 1,
1331 [isl_ast_op_pdiv_r] = 1,
1332 [isl_ast_op_zdiv_r] = 1,
1333 [isl_ast_op_cond] = 0,
1334 [isl_ast_op_select] = 0,
1335 [isl_ast_op_eq] = 1,
1336 [isl_ast_op_le] = 1,
1337 [isl_ast_op_ge] = 1,
1338 [isl_ast_op_lt] = 1,
1339 [isl_ast_op_gt] = 1,
1340 [isl_ast_op_call] = 1,
1341 [isl_ast_op_access] = 1,
1342 [isl_ast_op_member] = 1,
1343 [isl_ast_op_address_of] = 0
1346 static int is_and(enum isl_ast_op_type op)
1348 return op == isl_ast_op_and || op == isl_ast_op_and_then;
1351 static int is_or(enum isl_ast_op_type op)
1353 return op == isl_ast_op_or || op == isl_ast_op_or_else;
1356 static int is_add_sub(enum isl_ast_op_type op)
1358 return op == isl_ast_op_add || op == isl_ast_op_sub;
1361 static int is_div_mod(enum isl_ast_op_type op)
1363 return op == isl_ast_op_div ||
1364 op == isl_ast_op_pdiv_r ||
1365 op == isl_ast_op_zdiv_r;
1368 /* Do we need/want parentheses around "expr" as a subexpression of
1369 * an "op" operation? If "left" is set, then "expr" is the left-most
1370 * operand.
1372 * We only need parentheses if "expr" represents an operation.
1374 * If op has a higher precedence than expr->u.op.op, then we need
1375 * parentheses.
1376 * If op and expr->u.op.op have the same precedence, but the operations
1377 * are performed in an order that is different from the associativity,
1378 * then we need parentheses.
1380 * An and inside an or technically does not require parentheses,
1381 * but some compilers complain about that, so we add them anyway.
1383 * Computations such as "a / b * c" and "a % b + c" can be somewhat
1384 * difficult to read, so we add parentheses for those as well.
1386 static int sub_expr_need_parens(enum isl_ast_op_type op,
1387 __isl_keep isl_ast_expr *expr, int left)
1389 if (expr->type != isl_ast_expr_op)
1390 return 0;
1392 if (op_prec[expr->u.op.op] > op_prec[op])
1393 return 1;
1394 if (op_prec[expr->u.op.op] == op_prec[op] && left != op_left[op])
1395 return 1;
1397 if (is_or(op) && is_and(expr->u.op.op))
1398 return 1;
1399 if (op == isl_ast_op_mul && expr->u.op.op != isl_ast_op_mul &&
1400 op_prec[expr->u.op.op] == op_prec[op])
1401 return 1;
1402 if (is_add_sub(op) && is_div_mod(expr->u.op.op))
1403 return 1;
1405 return 0;
1408 /* Print "expr" as a subexpression of an "op" operation.
1409 * If "left" is set, then "expr" is the left-most operand.
1411 static __isl_give isl_printer *print_sub_expr(__isl_take isl_printer *p,
1412 enum isl_ast_op_type op, __isl_keep isl_ast_expr *expr, int left)
1414 int need_parens;
1416 need_parens = sub_expr_need_parens(op, expr, left);
1418 if (need_parens)
1419 p = isl_printer_print_str(p, "(");
1420 p = isl_printer_print_ast_expr(p, expr);
1421 if (need_parens)
1422 p = isl_printer_print_str(p, ")");
1423 return p;
1426 /* Print a min or max reduction "expr".
1428 static __isl_give isl_printer *print_min_max(__isl_take isl_printer *p,
1429 __isl_keep isl_ast_expr *expr)
1431 int i = 0;
1433 for (i = 1; i < expr->u.op.n_arg; ++i) {
1434 p = isl_printer_print_str(p, op_str[expr->u.op.op]);
1435 p = isl_printer_print_str(p, "(");
1437 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1438 for (i = 1; i < expr->u.op.n_arg; ++i) {
1439 p = isl_printer_print_str(p, ", ");
1440 p = isl_printer_print_ast_expr(p, expr->u.op.args[i]);
1441 p = isl_printer_print_str(p, ")");
1444 return p;
1447 /* Print a function call "expr".
1449 * The first argument represents the function to be called.
1451 static __isl_give isl_printer *print_call(__isl_take isl_printer *p,
1452 __isl_keep isl_ast_expr *expr)
1454 int i = 0;
1456 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1457 p = isl_printer_print_str(p, "(");
1458 for (i = 1; i < expr->u.op.n_arg; ++i) {
1459 if (i != 1)
1460 p = isl_printer_print_str(p, ", ");
1461 p = isl_printer_print_ast_expr(p, expr->u.op.args[i]);
1463 p = isl_printer_print_str(p, ")");
1465 return p;
1468 /* Print an array access "expr".
1470 * The first argument represents the array being accessed.
1472 static __isl_give isl_printer *print_access(__isl_take isl_printer *p,
1473 __isl_keep isl_ast_expr *expr)
1475 int i = 0;
1477 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1478 for (i = 1; i < expr->u.op.n_arg; ++i) {
1479 p = isl_printer_print_str(p, "[");
1480 p = isl_printer_print_ast_expr(p, expr->u.op.args[i]);
1481 p = isl_printer_print_str(p, "]");
1484 return p;
1487 /* Print "expr" to "p".
1489 * If we are printing in isl format, then we also print an indication
1490 * of the size of the expression (if it was computed).
1492 __isl_give isl_printer *isl_printer_print_ast_expr(__isl_take isl_printer *p,
1493 __isl_keep isl_ast_expr *expr)
1495 if (!p)
1496 return NULL;
1497 if (!expr)
1498 return isl_printer_free(p);
1500 switch (expr->type) {
1501 case isl_ast_expr_op:
1502 if (expr->u.op.op == isl_ast_op_call) {
1503 p = print_call(p, expr);
1504 break;
1506 if (expr->u.op.op == isl_ast_op_access) {
1507 p = print_access(p, expr);
1508 break;
1510 if (expr->u.op.n_arg == 1) {
1511 p = isl_printer_print_str(p, op_str[expr->u.op.op]);
1512 p = print_sub_expr(p, expr->u.op.op,
1513 expr->u.op.args[0], 0);
1514 break;
1516 if (expr->u.op.op == isl_ast_op_fdiv_q) {
1517 p = isl_printer_print_str(p, "floord(");
1518 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1519 p = isl_printer_print_str(p, ", ");
1520 p = isl_printer_print_ast_expr(p, expr->u.op.args[1]);
1521 p = isl_printer_print_str(p, ")");
1522 break;
1524 if (expr->u.op.op == isl_ast_op_max ||
1525 expr->u.op.op == isl_ast_op_min) {
1526 p = print_min_max(p, expr);
1527 break;
1529 if (expr->u.op.op == isl_ast_op_cond ||
1530 expr->u.op.op == isl_ast_op_select) {
1531 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1532 p = isl_printer_print_str(p, " ? ");
1533 p = isl_printer_print_ast_expr(p, expr->u.op.args[1]);
1534 p = isl_printer_print_str(p, " : ");
1535 p = isl_printer_print_ast_expr(p, expr->u.op.args[2]);
1536 break;
1538 if (expr->u.op.n_arg != 2)
1539 isl_die(isl_printer_get_ctx(p), isl_error_internal,
1540 "operation should have two arguments",
1541 goto error);
1542 p = print_sub_expr(p, expr->u.op.op, expr->u.op.args[0], 1);
1543 if (expr->u.op.op != isl_ast_op_member)
1544 p = isl_printer_print_str(p, " ");
1545 p = isl_printer_print_str(p, op_str[expr->u.op.op]);
1546 if (expr->u.op.op != isl_ast_op_member)
1547 p = isl_printer_print_str(p, " ");
1548 p = print_sub_expr(p, expr->u.op.op, expr->u.op.args[1], 0);
1549 break;
1550 case isl_ast_expr_id:
1551 p = isl_printer_print_str(p, isl_id_get_name(expr->u.id));
1552 break;
1553 case isl_ast_expr_int:
1554 p = isl_printer_print_val(p, expr->u.v);
1555 break;
1556 case isl_ast_expr_error:
1557 break;
1560 return p;
1561 error:
1562 isl_printer_free(p);
1563 return NULL;
1566 /* Print "node" to "p" in "isl format".
1568 static __isl_give isl_printer *print_ast_node_isl(__isl_take isl_printer *p,
1569 __isl_keep isl_ast_node *node)
1571 p = isl_printer_print_str(p, "(");
1572 switch (node->type) {
1573 case isl_ast_node_for:
1574 if (node->u.f.degenerate) {
1575 p = isl_printer_print_ast_expr(p, node->u.f.init);
1576 } else {
1577 p = isl_printer_print_str(p, "init: ");
1578 p = isl_printer_print_ast_expr(p, node->u.f.init);
1579 p = isl_printer_print_str(p, ", ");
1580 p = isl_printer_print_str(p, "cond: ");
1581 p = isl_printer_print_ast_expr(p, node->u.f.cond);
1582 p = isl_printer_print_str(p, ", ");
1583 p = isl_printer_print_str(p, "inc: ");
1584 p = isl_printer_print_ast_expr(p, node->u.f.inc);
1586 if (node->u.f.body) {
1587 p = isl_printer_print_str(p, ", ");
1588 p = isl_printer_print_str(p, "body: ");
1589 p = isl_printer_print_ast_node(p, node->u.f.body);
1591 break;
1592 case isl_ast_node_user:
1593 p = isl_printer_print_ast_expr(p, node->u.e.expr);
1594 break;
1595 case isl_ast_node_if:
1596 p = isl_printer_print_str(p, "guard: ");
1597 p = isl_printer_print_ast_expr(p, node->u.i.guard);
1598 if (node->u.i.then) {
1599 p = isl_printer_print_str(p, ", ");
1600 p = isl_printer_print_str(p, "then: ");
1601 p = isl_printer_print_ast_node(p, node->u.i.then);
1603 if (node->u.i.else_node) {
1604 p = isl_printer_print_str(p, ", ");
1605 p = isl_printer_print_str(p, "else: ");
1606 p = isl_printer_print_ast_node(p, node->u.i.else_node);
1608 break;
1609 case isl_ast_node_block:
1610 p = isl_printer_print_ast_node_list(p, node->u.b.children);
1611 break;
1612 default:
1613 break;
1615 p = isl_printer_print_str(p, ")");
1616 return p;
1619 /* Do we need to print a block around the body "node" of a for or if node?
1621 * If the node is a block, then we need to print a block.
1622 * Also if the node is a degenerate for then we will print it as
1623 * an assignment followed by the body of the for loop, so we need a block
1624 * as well.
1625 * If the node is an if node with an else, then we print a block
1626 * to avoid spurious dangling else warnings emitted by some compilers.
1627 * If the ast_always_print_block option has been set, then we print a block.
1629 static int need_block(__isl_keep isl_ast_node *node)
1631 isl_ctx *ctx;
1633 if (node->type == isl_ast_node_block)
1634 return 1;
1635 if (node->type == isl_ast_node_for && node->u.f.degenerate)
1636 return 1;
1637 if (node->type == isl_ast_node_if && node->u.i.else_node)
1638 return 1;
1640 ctx = isl_ast_node_get_ctx(node);
1641 return isl_options_get_ast_always_print_block(ctx);
1644 static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
1645 __isl_keep isl_ast_node *node,
1646 __isl_keep isl_ast_print_options *options, int in_block, int in_list);
1647 static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
1648 __isl_keep isl_ast_node *node,
1649 __isl_keep isl_ast_print_options *options, int new_line);
1651 /* Print the body "node" of a for or if node.
1652 * If "else_node" is set, then it is printed as well.
1654 * We first check if we need to print out a block.
1655 * We always print out a block if there is an else node to make
1656 * sure that the else node is matched to the correct if node.
1658 * If the else node is itself an if, then we print it as
1660 * } else if (..)
1662 * Otherwise the else node is printed as
1664 * } else
1665 * node
1667 static __isl_give isl_printer *print_body_c(__isl_take isl_printer *p,
1668 __isl_keep isl_ast_node *node, __isl_keep isl_ast_node *else_node,
1669 __isl_keep isl_ast_print_options *options)
1671 if (!node)
1672 return isl_printer_free(p);
1674 if (!else_node && !need_block(node)) {
1675 p = isl_printer_end_line(p);
1676 p = isl_printer_indent(p, 2);
1677 p = isl_ast_node_print(node, p,
1678 isl_ast_print_options_copy(options));
1679 p = isl_printer_indent(p, -2);
1680 return p;
1683 p = isl_printer_print_str(p, " {");
1684 p = isl_printer_end_line(p);
1685 p = isl_printer_indent(p, 2);
1686 p = print_ast_node_c(p, node, options, 1, 0);
1687 p = isl_printer_indent(p, -2);
1688 p = isl_printer_start_line(p);
1689 p = isl_printer_print_str(p, "}");
1690 if (else_node) {
1691 if (else_node->type == isl_ast_node_if) {
1692 p = isl_printer_print_str(p, " else ");
1693 p = print_if_c(p, else_node, options, 0);
1694 } else {
1695 p = isl_printer_print_str(p, " else");
1696 p = print_body_c(p, else_node, NULL, options);
1698 } else
1699 p = isl_printer_end_line(p);
1701 return p;
1704 /* Print the start of a compound statement.
1706 static __isl_give isl_printer *start_block(__isl_take isl_printer *p)
1708 p = isl_printer_start_line(p);
1709 p = isl_printer_print_str(p, "{");
1710 p = isl_printer_end_line(p);
1711 p = isl_printer_indent(p, 2);
1713 return p;
1716 /* Print the end of a compound statement.
1718 static __isl_give isl_printer *end_block(__isl_take isl_printer *p)
1720 p = isl_printer_indent(p, -2);
1721 p = isl_printer_start_line(p);
1722 p = isl_printer_print_str(p, "}");
1723 p = isl_printer_end_line(p);
1725 return p;
1728 /* Print the for node "node".
1730 * If the for node is degenerate, it is printed as
1732 * type iterator = init;
1733 * body
1735 * Otherwise, it is printed as
1737 * for (type iterator = init; cond; iterator += inc)
1738 * body
1740 * "in_block" is set if we are currently inside a block.
1741 * "in_list" is set if the current node is not alone in the block.
1742 * If we are not in a block or if the current not is not alone in the block
1743 * then we print a block around a degenerate for loop such that the variable
1744 * declaration will not conflict with any potential other declaration
1745 * of the same variable.
1747 static __isl_give isl_printer *print_for_c(__isl_take isl_printer *p,
1748 __isl_keep isl_ast_node *node,
1749 __isl_keep isl_ast_print_options *options, int in_block, int in_list)
1751 isl_id *id;
1752 const char *name;
1753 const char *type;
1755 type = isl_options_get_ast_iterator_type(isl_printer_get_ctx(p));
1756 if (!node->u.f.degenerate) {
1757 id = isl_ast_expr_get_id(node->u.f.iterator);
1758 name = isl_id_get_name(id);
1759 isl_id_free(id);
1760 p = isl_printer_start_line(p);
1761 p = isl_printer_print_str(p, "for (");
1762 p = isl_printer_print_str(p, type);
1763 p = isl_printer_print_str(p, " ");
1764 p = isl_printer_print_str(p, name);
1765 p = isl_printer_print_str(p, " = ");
1766 p = isl_printer_print_ast_expr(p, node->u.f.init);
1767 p = isl_printer_print_str(p, "; ");
1768 p = isl_printer_print_ast_expr(p, node->u.f.cond);
1769 p = isl_printer_print_str(p, "; ");
1770 p = isl_printer_print_str(p, name);
1771 p = isl_printer_print_str(p, " += ");
1772 p = isl_printer_print_ast_expr(p, node->u.f.inc);
1773 p = isl_printer_print_str(p, ")");
1774 p = print_body_c(p, node->u.f.body, NULL, options);
1775 } else {
1776 id = isl_ast_expr_get_id(node->u.f.iterator);
1777 name = isl_id_get_name(id);
1778 isl_id_free(id);
1779 if (!in_block || in_list)
1780 p = start_block(p);
1781 p = isl_printer_start_line(p);
1782 p = isl_printer_print_str(p, type);
1783 p = isl_printer_print_str(p, " ");
1784 p = isl_printer_print_str(p, name);
1785 p = isl_printer_print_str(p, " = ");
1786 p = isl_printer_print_ast_expr(p, node->u.f.init);
1787 p = isl_printer_print_str(p, ";");
1788 p = isl_printer_end_line(p);
1789 p = print_ast_node_c(p, node->u.f.body, options, 1, 0);
1790 if (!in_block || in_list)
1791 p = end_block(p);
1794 return p;
1797 /* Print the if node "node".
1798 * If "new_line" is set then the if node should be printed on a new line.
1800 static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
1801 __isl_keep isl_ast_node *node,
1802 __isl_keep isl_ast_print_options *options, int new_line)
1804 if (new_line)
1805 p = isl_printer_start_line(p);
1806 p = isl_printer_print_str(p, "if (");
1807 p = isl_printer_print_ast_expr(p, node->u.i.guard);
1808 p = isl_printer_print_str(p, ")");
1809 p = print_body_c(p, node->u.i.then, node->u.i.else_node, options);
1811 return p;
1814 /* Print the "node" to "p".
1816 * "in_block" is set if we are currently inside a block.
1817 * If so, we do not print a block around the children of a block node.
1818 * We do this to avoid an extra block around the body of a degenerate
1819 * for node.
1821 * "in_list" is set if the current node is not alone in the block.
1823 static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
1824 __isl_keep isl_ast_node *node,
1825 __isl_keep isl_ast_print_options *options, int in_block, int in_list)
1827 switch (node->type) {
1828 case isl_ast_node_for:
1829 if (options->print_for)
1830 return options->print_for(p,
1831 isl_ast_print_options_copy(options),
1832 node, options->print_for_user);
1833 p = print_for_c(p, node, options, in_block, in_list);
1834 break;
1835 case isl_ast_node_if:
1836 p = print_if_c(p, node, options, 1);
1837 break;
1838 case isl_ast_node_block:
1839 if (!in_block)
1840 p = start_block(p);
1841 p = isl_ast_node_list_print(node->u.b.children, p, options);
1842 if (!in_block)
1843 p = end_block(p);
1844 break;
1845 case isl_ast_node_user:
1846 if (options->print_user)
1847 return options->print_user(p,
1848 isl_ast_print_options_copy(options),
1849 node, options->print_user_user);
1850 p = isl_printer_start_line(p);
1851 p = isl_printer_print_ast_expr(p, node->u.e.expr);
1852 p = isl_printer_print_str(p, ";");
1853 p = isl_printer_end_line(p);
1854 break;
1855 case isl_ast_node_error:
1856 break;
1858 return p;
1861 /* Print the for node "node" to "p".
1863 __isl_give isl_printer *isl_ast_node_for_print(__isl_keep isl_ast_node *node,
1864 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
1866 if (!node || !options)
1867 goto error;
1868 if (node->type != isl_ast_node_for)
1869 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1870 "not a for node", goto error);
1871 p = print_for_c(p, node, options, 0, 0);
1872 isl_ast_print_options_free(options);
1873 return p;
1874 error:
1875 isl_ast_print_options_free(options);
1876 isl_printer_free(p);
1877 return NULL;
1880 /* Print the if node "node" to "p".
1882 __isl_give isl_printer *isl_ast_node_if_print(__isl_keep isl_ast_node *node,
1883 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
1885 if (!node || !options)
1886 goto error;
1887 if (node->type != isl_ast_node_if)
1888 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1889 "not an if node", goto error);
1890 p = print_if_c(p, node, options, 1);
1891 isl_ast_print_options_free(options);
1892 return p;
1893 error:
1894 isl_ast_print_options_free(options);
1895 isl_printer_free(p);
1896 return NULL;
1899 /* Print "node" to "p".
1901 __isl_give isl_printer *isl_ast_node_print(__isl_keep isl_ast_node *node,
1902 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
1904 if (!options || !node)
1905 goto error;
1906 p = print_ast_node_c(p, node, options, 0, 0);
1907 isl_ast_print_options_free(options);
1908 return p;
1909 error:
1910 isl_ast_print_options_free(options);
1911 isl_printer_free(p);
1912 return NULL;
1915 /* Print "node" to "p".
1917 __isl_give isl_printer *isl_printer_print_ast_node(__isl_take isl_printer *p,
1918 __isl_keep isl_ast_node *node)
1920 int format;
1921 isl_ast_print_options *options;
1923 if (!p)
1924 return NULL;
1926 format = isl_printer_get_output_format(p);
1927 switch (format) {
1928 case ISL_FORMAT_ISL:
1929 p = print_ast_node_isl(p, node);
1930 break;
1931 case ISL_FORMAT_C:
1932 options = isl_ast_print_options_alloc(isl_printer_get_ctx(p));
1933 p = isl_ast_node_print(node, p, options);
1934 break;
1935 default:
1936 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
1937 "output format not supported for ast_node",
1938 return isl_printer_free(p));
1941 return p;
1944 /* Print the list of nodes "list" to "p".
1946 __isl_give isl_printer *isl_ast_node_list_print(
1947 __isl_keep isl_ast_node_list *list, __isl_take isl_printer *p,
1948 __isl_keep isl_ast_print_options *options)
1950 int i;
1952 if (!p || !list || !options)
1953 return isl_printer_free(p);
1955 for (i = 0; i < list->n; ++i)
1956 p = print_ast_node_c(p, list->p[i], options, 1, 1);
1958 return p;
1961 #define ISL_AST_MACRO_FLOORD (1 << 0)
1962 #define ISL_AST_MACRO_MIN (1 << 1)
1963 #define ISL_AST_MACRO_MAX (1 << 2)
1964 #define ISL_AST_MACRO_ALL (ISL_AST_MACRO_FLOORD | \
1965 ISL_AST_MACRO_MIN | \
1966 ISL_AST_MACRO_MAX)
1968 /* If "expr" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1969 * then set the corresponding bit in "macros".
1971 static int ast_expr_required_macros(__isl_keep isl_ast_expr *expr, int macros)
1973 int i;
1975 if (macros == ISL_AST_MACRO_ALL)
1976 return macros;
1978 if (expr->type != isl_ast_expr_op)
1979 return macros;
1981 if (expr->u.op.op == isl_ast_op_min)
1982 macros |= ISL_AST_MACRO_MIN;
1983 if (expr->u.op.op == isl_ast_op_max)
1984 macros |= ISL_AST_MACRO_MAX;
1985 if (expr->u.op.op == isl_ast_op_fdiv_q)
1986 macros |= ISL_AST_MACRO_FLOORD;
1988 for (i = 0; i < expr->u.op.n_arg; ++i)
1989 macros = ast_expr_required_macros(expr->u.op.args[i], macros);
1991 return macros;
1994 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
1995 int macros);
1997 /* If "node" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
1998 * then set the corresponding bit in "macros".
2000 static int ast_node_required_macros(__isl_keep isl_ast_node *node, int macros)
2002 if (macros == ISL_AST_MACRO_ALL)
2003 return macros;
2005 switch (node->type) {
2006 case isl_ast_node_for:
2007 macros = ast_expr_required_macros(node->u.f.init, macros);
2008 if (!node->u.f.degenerate) {
2009 macros = ast_expr_required_macros(node->u.f.cond,
2010 macros);
2011 macros = ast_expr_required_macros(node->u.f.inc,
2012 macros);
2014 macros = ast_node_required_macros(node->u.f.body, macros);
2015 break;
2016 case isl_ast_node_if:
2017 macros = ast_expr_required_macros(node->u.i.guard, macros);
2018 macros = ast_node_required_macros(node->u.i.then, macros);
2019 if (node->u.i.else_node)
2020 macros = ast_node_required_macros(node->u.i.else_node,
2021 macros);
2022 break;
2023 case isl_ast_node_block:
2024 macros = ast_node_list_required_macros(node->u.b.children,
2025 macros);
2026 break;
2027 case isl_ast_node_user:
2028 macros = ast_expr_required_macros(node->u.e.expr, macros);
2029 break;
2030 case isl_ast_node_error:
2031 break;
2034 return macros;
2037 /* If "list" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
2038 * then set the corresponding bit in "macros".
2040 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
2041 int macros)
2043 int i;
2045 for (i = 0; i < list->n; ++i)
2046 macros = ast_node_required_macros(list->p[i], macros);
2048 return macros;
2051 /* Print a macro definition for the operator "type".
2053 __isl_give isl_printer *isl_ast_op_type_print_macro(
2054 enum isl_ast_op_type type, __isl_take isl_printer *p)
2056 switch (type) {
2057 case isl_ast_op_min:
2058 p = isl_printer_start_line(p);
2059 p = isl_printer_print_str(p,
2060 "#define min(x,y) ((x) < (y) ? (x) : (y))");
2061 p = isl_printer_end_line(p);
2062 break;
2063 case isl_ast_op_max:
2064 p = isl_printer_start_line(p);
2065 p = isl_printer_print_str(p,
2066 "#define max(x,y) ((x) > (y) ? (x) : (y))");
2067 p = isl_printer_end_line(p);
2068 break;
2069 case isl_ast_op_fdiv_q:
2070 p = isl_printer_start_line(p);
2071 p = isl_printer_print_str(p,
2072 "#define floord(n,d) "
2073 "(((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))");
2074 p = isl_printer_end_line(p);
2075 break;
2076 default:
2077 break;
2080 return p;
2083 /* Call "fn" for each type of operation that appears in "node"
2084 * and that requires a macro definition.
2086 int isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node *node,
2087 int (*fn)(enum isl_ast_op_type type, void *user), void *user)
2089 int macros;
2091 if (!node)
2092 return -1;
2094 macros = ast_node_required_macros(node, 0);
2096 if (macros & ISL_AST_MACRO_MIN && fn(isl_ast_op_min, user) < 0)
2097 return -1;
2098 if (macros & ISL_AST_MACRO_MAX && fn(isl_ast_op_max, user) < 0)
2099 return -1;
2100 if (macros & ISL_AST_MACRO_FLOORD && fn(isl_ast_op_fdiv_q, user) < 0)
2101 return -1;
2103 return 0;
2106 static int ast_op_type_print_macro(enum isl_ast_op_type type, void *user)
2108 isl_printer **p = user;
2110 *p = isl_ast_op_type_print_macro(type, *p);
2112 return 0;
2115 /* Print macro definitions for all the macros used in the result
2116 * of printing "node.
2118 __isl_give isl_printer *isl_ast_node_print_macros(
2119 __isl_keep isl_ast_node *node, __isl_take isl_printer *p)
2121 if (isl_ast_node_foreach_ast_op_type(node,
2122 &ast_op_type_print_macro, &p) < 0)
2123 return isl_printer_free(p);
2124 return p;