• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012-2013 Ecole Normale Superieure
3  *
4  * Use of this software is governed by the MIT license
5  *
6  * Written by Sven Verdoolaege,
7  * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
8  */
9 
10 #include <string.h>
11 
12 #include <isl/id.h>
13 #include <isl/val.h>
14 #include <isl_ast_private.h>
15 
16 #undef EL_BASE
17 #define EL_BASE ast_expr
18 
19 #include <isl_list_templ.c>
20 
21 #undef EL_BASE
22 #define EL_BASE ast_node
23 
24 #include <isl_list_templ.c>
25 
isl_ast_print_options_get_ctx(__isl_keep isl_ast_print_options * options)26 isl_ctx *isl_ast_print_options_get_ctx(
27 	__isl_keep isl_ast_print_options *options)
28 {
29 	return options ? options->ctx : NULL;
30 }
31 
isl_ast_print_options_alloc(isl_ctx * ctx)32 __isl_give isl_ast_print_options *isl_ast_print_options_alloc(isl_ctx *ctx)
33 {
34 	isl_ast_print_options *options;
35 
36 	options = isl_calloc_type(ctx, isl_ast_print_options);
37 	if (!options)
38 		return NULL;
39 
40 	options->ctx = ctx;
41 	isl_ctx_ref(ctx);
42 	options->ref = 1;
43 
44 	return options;
45 }
46 
isl_ast_print_options_dup(__isl_keep isl_ast_print_options * options)47 __isl_give isl_ast_print_options *isl_ast_print_options_dup(
48 	__isl_keep isl_ast_print_options *options)
49 {
50 	isl_ctx *ctx;
51 	isl_ast_print_options *dup;
52 
53 	if (!options)
54 		return NULL;
55 
56 	ctx = isl_ast_print_options_get_ctx(options);
57 	dup = isl_ast_print_options_alloc(ctx);
58 	if (!dup)
59 		return NULL;
60 
61 	dup->print_for = options->print_for;
62 	dup->print_for_user = options->print_for_user;
63 	dup->print_user = options->print_user;
64 	dup->print_user_user = options->print_user_user;
65 
66 	return dup;
67 }
68 
isl_ast_print_options_cow(__isl_take isl_ast_print_options * options)69 __isl_give isl_ast_print_options *isl_ast_print_options_cow(
70 	__isl_take isl_ast_print_options *options)
71 {
72 	if (!options)
73 		return NULL;
74 
75 	if (options->ref == 1)
76 		return options;
77 	options->ref--;
78 	return isl_ast_print_options_dup(options);
79 }
80 
isl_ast_print_options_copy(__isl_keep isl_ast_print_options * options)81 __isl_give isl_ast_print_options *isl_ast_print_options_copy(
82 	__isl_keep isl_ast_print_options *options)
83 {
84 	if (!options)
85 		return NULL;
86 
87 	options->ref++;
88 	return options;
89 }
90 
isl_ast_print_options_free(__isl_take isl_ast_print_options * options)91 __isl_null isl_ast_print_options *isl_ast_print_options_free(
92 	__isl_take isl_ast_print_options *options)
93 {
94 	if (!options)
95 		return NULL;
96 
97 	if (--options->ref > 0)
98 		return NULL;
99 
100 	isl_ctx_deref(options->ctx);
101 
102 	free(options);
103 	return NULL;
104 }
105 
106 /* Set the print_user callback of "options" to "print_user".
107  *
108  * If this callback is set, then it is used to print user nodes in the AST.
109  * Otherwise, the expression associated to the user node is printed.
110  */
isl_ast_print_options_set_print_user(__isl_take isl_ast_print_options * options,__isl_give isl_printer * (* print_user)(__isl_take isl_printer * p,__isl_take isl_ast_print_options * options,__isl_keep isl_ast_node * node,void * user),void * user)111 __isl_give isl_ast_print_options *isl_ast_print_options_set_print_user(
112 	__isl_take isl_ast_print_options *options,
113 	__isl_give isl_printer *(*print_user)(__isl_take isl_printer *p,
114 		__isl_take isl_ast_print_options *options,
115 		__isl_keep isl_ast_node *node, void *user),
116 	void *user)
117 {
118 	options = isl_ast_print_options_cow(options);
119 	if (!options)
120 		return NULL;
121 
122 	options->print_user = print_user;
123 	options->print_user_user = user;
124 
125 	return options;
126 }
127 
128 /* Set the print_for callback of "options" to "print_for".
129  *
130  * If this callback is set, then it used to print for nodes in the AST.
131  */
isl_ast_print_options_set_print_for(__isl_take isl_ast_print_options * options,__isl_give isl_printer * (* print_for)(__isl_take isl_printer * p,__isl_take isl_ast_print_options * options,__isl_keep isl_ast_node * node,void * user),void * user)132 __isl_give isl_ast_print_options *isl_ast_print_options_set_print_for(
133 	__isl_take isl_ast_print_options *options,
134 	__isl_give isl_printer *(*print_for)(__isl_take isl_printer *p,
135 		__isl_take isl_ast_print_options *options,
136 		__isl_keep isl_ast_node *node, void *user),
137 	void *user)
138 {
139 	options = isl_ast_print_options_cow(options);
140 	if (!options)
141 		return NULL;
142 
143 	options->print_for = print_for;
144 	options->print_for_user = user;
145 
146 	return options;
147 }
148 
isl_ast_expr_copy(__isl_keep isl_ast_expr * expr)149 __isl_give isl_ast_expr *isl_ast_expr_copy(__isl_keep isl_ast_expr *expr)
150 {
151 	if (!expr)
152 		return NULL;
153 
154 	expr->ref++;
155 	return expr;
156 }
157 
isl_ast_expr_dup(__isl_keep isl_ast_expr * expr)158 __isl_give isl_ast_expr *isl_ast_expr_dup(__isl_keep isl_ast_expr *expr)
159 {
160 	int i;
161 	isl_ctx *ctx;
162 	isl_ast_expr *dup;
163 
164 	if (!expr)
165 		return NULL;
166 
167 	ctx = isl_ast_expr_get_ctx(expr);
168 	switch (expr->type) {
169 	case isl_ast_expr_int:
170 		dup = isl_ast_expr_from_val(isl_val_copy(expr->u.v));
171 		break;
172 	case isl_ast_expr_id:
173 		dup = isl_ast_expr_from_id(isl_id_copy(expr->u.id));
174 		break;
175 	case isl_ast_expr_op:
176 		dup = isl_ast_expr_alloc_op(ctx,
177 					    expr->u.op.op, expr->u.op.n_arg);
178 		if (!dup)
179 			return NULL;
180 		for (i = 0; i < expr->u.op.n_arg; ++i)
181 			dup->u.op.args[i] =
182 				isl_ast_expr_copy(expr->u.op.args[i]);
183 		break;
184 	case isl_ast_expr_error:
185 		dup = NULL;
186 	}
187 
188 	if (!dup)
189 		return NULL;
190 
191 	return dup;
192 }
193 
isl_ast_expr_cow(__isl_take isl_ast_expr * expr)194 __isl_give isl_ast_expr *isl_ast_expr_cow(__isl_take isl_ast_expr *expr)
195 {
196 	if (!expr)
197 		return NULL;
198 
199 	if (expr->ref == 1)
200 		return expr;
201 	expr->ref--;
202 	return isl_ast_expr_dup(expr);
203 }
204 
isl_ast_expr_free(__isl_take isl_ast_expr * expr)205 __isl_null isl_ast_expr *isl_ast_expr_free(__isl_take isl_ast_expr *expr)
206 {
207 	int i;
208 
209 	if (!expr)
210 		return NULL;
211 
212 	if (--expr->ref > 0)
213 		return NULL;
214 
215 	isl_ctx_deref(expr->ctx);
216 
217 	switch (expr->type) {
218 	case isl_ast_expr_int:
219 		isl_val_free(expr->u.v);
220 		break;
221 	case isl_ast_expr_id:
222 		isl_id_free(expr->u.id);
223 		break;
224 	case isl_ast_expr_op:
225 		if (expr->u.op.args)
226 			for (i = 0; i < expr->u.op.n_arg; ++i)
227 				isl_ast_expr_free(expr->u.op.args[i]);
228 		free(expr->u.op.args);
229 		break;
230 	case isl_ast_expr_error:
231 		break;
232 	}
233 
234 	free(expr);
235 	return NULL;
236 }
237 
isl_ast_expr_get_ctx(__isl_keep isl_ast_expr * expr)238 isl_ctx *isl_ast_expr_get_ctx(__isl_keep isl_ast_expr *expr)
239 {
240 	return expr ? expr->ctx : NULL;
241 }
242 
isl_ast_expr_get_type(__isl_keep isl_ast_expr * expr)243 enum isl_ast_expr_type isl_ast_expr_get_type(__isl_keep isl_ast_expr *expr)
244 {
245 	return expr ? expr->type : isl_ast_expr_error;
246 }
247 
248 /* Return the integer value represented by "expr".
249  */
isl_ast_expr_int_get_val(__isl_keep isl_ast_expr * expr)250 __isl_give isl_val *isl_ast_expr_int_get_val(__isl_keep isl_ast_expr *expr)
251 {
252 	if (!expr)
253 		return NULL;
254 	if (expr->type != isl_ast_expr_int)
255 		isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
256 			"expression not an int", return NULL);
257 	return isl_val_copy(expr->u.v);
258 }
259 
260 /* This is an alternative name for the function above.
261  */
isl_ast_expr_get_val(__isl_keep isl_ast_expr * expr)262 __isl_give isl_val *isl_ast_expr_get_val(__isl_keep isl_ast_expr *expr)
263 {
264 	return isl_ast_expr_int_get_val(expr);
265 }
266 
isl_ast_expr_id_get_id(__isl_keep isl_ast_expr * expr)267 __isl_give isl_id *isl_ast_expr_id_get_id(__isl_keep isl_ast_expr *expr)
268 {
269 	if (!expr)
270 		return NULL;
271 	if (expr->type != isl_ast_expr_id)
272 		isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
273 			"expression not an identifier", return NULL);
274 
275 	return isl_id_copy(expr->u.id);
276 }
277 
278 /* This is an alternative name for the function above.
279  */
isl_ast_expr_get_id(__isl_keep isl_ast_expr * expr)280 __isl_give isl_id *isl_ast_expr_get_id(__isl_keep isl_ast_expr *expr)
281 {
282 	return isl_ast_expr_id_get_id(expr);
283 }
284 
285 /* Return the type of operation represented by "expr".
286  */
isl_ast_expr_op_get_type(__isl_keep isl_ast_expr * expr)287 enum isl_ast_expr_op_type isl_ast_expr_op_get_type(
288 	__isl_keep isl_ast_expr *expr)
289 {
290 	if (!expr)
291 		return isl_ast_expr_op_error;
292 	if (expr->type != isl_ast_expr_op)
293 		isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
294 			"expression not an operation",
295 			return isl_ast_expr_op_error);
296 	return expr->u.op.op;
297 }
298 
299 /* This is an alternative name for the function above.
300  */
isl_ast_expr_get_op_type(__isl_keep isl_ast_expr * expr)301 enum isl_ast_expr_op_type isl_ast_expr_get_op_type(
302 	__isl_keep isl_ast_expr *expr)
303 {
304 	return isl_ast_expr_op_get_type(expr);
305 }
306 
307 /* Return the number of arguments of the operation represented by "expr".
308  */
isl_ast_expr_op_get_n_arg(__isl_keep isl_ast_expr * expr)309 isl_size isl_ast_expr_op_get_n_arg(__isl_keep isl_ast_expr *expr)
310 {
311 	if (!expr)
312 		return isl_size_error;
313 	if (expr->type != isl_ast_expr_op)
314 		isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
315 			"expression not an operation", return isl_size_error);
316 	return expr->u.op.n_arg;
317 }
318 
319 /* This is an alternative name for the function above.
320  */
isl_ast_expr_get_op_n_arg(__isl_keep isl_ast_expr * expr)321 isl_size isl_ast_expr_get_op_n_arg(__isl_keep isl_ast_expr *expr)
322 {
323 	return isl_ast_expr_op_get_n_arg(expr);
324 }
325 
326 /* Return the argument at position "pos" of the operation represented by "expr".
327  */
isl_ast_expr_op_get_arg(__isl_keep isl_ast_expr * expr,int pos)328 __isl_give isl_ast_expr *isl_ast_expr_op_get_arg(__isl_keep isl_ast_expr *expr,
329 	int pos)
330 {
331 	if (!expr)
332 		return NULL;
333 	if (expr->type != isl_ast_expr_op)
334 		isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
335 			"expression not an operation", return NULL);
336 	if (pos < 0 || pos >= expr->u.op.n_arg)
337 		isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
338 			"index out of bounds", return NULL);
339 
340 	return isl_ast_expr_copy(expr->u.op.args[pos]);
341 }
342 
343 /* This is an alternative name for the function above.
344  */
isl_ast_expr_get_op_arg(__isl_keep isl_ast_expr * expr,int pos)345 __isl_give isl_ast_expr *isl_ast_expr_get_op_arg(__isl_keep isl_ast_expr *expr,
346 	int pos)
347 {
348 	return isl_ast_expr_op_get_arg(expr, pos);
349 }
350 
351 /* Replace the argument at position "pos" of "expr" by "arg".
352  */
isl_ast_expr_set_op_arg(__isl_take isl_ast_expr * expr,int pos,__isl_take isl_ast_expr * arg)353 __isl_give isl_ast_expr *isl_ast_expr_set_op_arg(__isl_take isl_ast_expr *expr,
354 	int pos, __isl_take isl_ast_expr *arg)
355 {
356 	expr = isl_ast_expr_cow(expr);
357 	if (!expr || !arg)
358 		goto error;
359 	if (expr->type != isl_ast_expr_op)
360 		isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
361 			"expression not an operation", goto error);
362 	if (pos < 0 || pos >= expr->u.op.n_arg)
363 		isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
364 			"index out of bounds", goto error);
365 
366 	isl_ast_expr_free(expr->u.op.args[pos]);
367 	expr->u.op.args[pos] = arg;
368 
369 	return expr;
370 error:
371 	isl_ast_expr_free(arg);
372 	return isl_ast_expr_free(expr);
373 }
374 
375 /* Is "expr1" equal to "expr2"?
376  */
isl_ast_expr_is_equal(__isl_keep isl_ast_expr * expr1,__isl_keep isl_ast_expr * expr2)377 isl_bool isl_ast_expr_is_equal(__isl_keep isl_ast_expr *expr1,
378 	__isl_keep isl_ast_expr *expr2)
379 {
380 	int i;
381 
382 	if (!expr1 || !expr2)
383 		return isl_bool_error;
384 
385 	if (expr1 == expr2)
386 		return isl_bool_true;
387 	if (expr1->type != expr2->type)
388 		return isl_bool_false;
389 	switch (expr1->type) {
390 	case isl_ast_expr_int:
391 		return isl_val_eq(expr1->u.v, expr2->u.v);
392 	case isl_ast_expr_id:
393 		return isl_bool_ok(expr1->u.id == expr2->u.id);
394 	case isl_ast_expr_op:
395 		if (expr1->u.op.op != expr2->u.op.op)
396 			return isl_bool_false;
397 		if (expr1->u.op.n_arg != expr2->u.op.n_arg)
398 			return isl_bool_false;
399 		for (i = 0; i < expr1->u.op.n_arg; ++i) {
400 			isl_bool equal;
401 			equal = isl_ast_expr_is_equal(expr1->u.op.args[i],
402 							expr2->u.op.args[i]);
403 			if (equal < 0 || !equal)
404 				return equal;
405 		}
406 		return isl_bool_true;
407 	case isl_ast_expr_error:
408 		return isl_bool_error;
409 	}
410 
411 	isl_die(isl_ast_expr_get_ctx(expr1), isl_error_internal,
412 		"unhandled case", return isl_bool_error);
413 }
414 
415 /* Create a new operation expression of operation type "op",
416  * with "n_arg" as yet unspecified arguments.
417  */
isl_ast_expr_alloc_op(isl_ctx * ctx,enum isl_ast_expr_op_type op,int n_arg)418 __isl_give isl_ast_expr *isl_ast_expr_alloc_op(isl_ctx *ctx,
419 	enum isl_ast_expr_op_type op, int n_arg)
420 {
421 	isl_ast_expr *expr;
422 
423 	expr = isl_calloc_type(ctx, isl_ast_expr);
424 	if (!expr)
425 		return NULL;
426 
427 	expr->ctx = ctx;
428 	isl_ctx_ref(ctx);
429 	expr->ref = 1;
430 	expr->type = isl_ast_expr_op;
431 	expr->u.op.op = op;
432 	expr->u.op.n_arg = n_arg;
433 	expr->u.op.args = isl_calloc_array(ctx, isl_ast_expr *, n_arg);
434 
435 	if (n_arg && !expr->u.op.args)
436 		return isl_ast_expr_free(expr);
437 
438 	return expr;
439 }
440 
441 /* Create a new id expression representing "id".
442  */
isl_ast_expr_from_id(__isl_take isl_id * id)443 __isl_give isl_ast_expr *isl_ast_expr_from_id(__isl_take isl_id *id)
444 {
445 	isl_ctx *ctx;
446 	isl_ast_expr *expr;
447 
448 	if (!id)
449 		return NULL;
450 
451 	ctx = isl_id_get_ctx(id);
452 	expr = isl_calloc_type(ctx, isl_ast_expr);
453 	if (!expr)
454 		goto error;
455 
456 	expr->ctx = ctx;
457 	isl_ctx_ref(ctx);
458 	expr->ref = 1;
459 	expr->type = isl_ast_expr_id;
460 	expr->u.id = id;
461 
462 	return expr;
463 error:
464 	isl_id_free(id);
465 	return NULL;
466 }
467 
468 /* Create a new integer expression representing "i".
469  */
isl_ast_expr_alloc_int_si(isl_ctx * ctx,int i)470 __isl_give isl_ast_expr *isl_ast_expr_alloc_int_si(isl_ctx *ctx, int i)
471 {
472 	isl_ast_expr *expr;
473 
474 	expr = isl_calloc_type(ctx, isl_ast_expr);
475 	if (!expr)
476 		return NULL;
477 
478 	expr->ctx = ctx;
479 	isl_ctx_ref(ctx);
480 	expr->ref = 1;
481 	expr->type = isl_ast_expr_int;
482 	expr->u.v = isl_val_int_from_si(ctx, i);
483 	if (!expr->u.v)
484 		return isl_ast_expr_free(expr);
485 
486 	return expr;
487 }
488 
489 /* Create a new integer expression representing "v".
490  */
isl_ast_expr_from_val(__isl_take isl_val * v)491 __isl_give isl_ast_expr *isl_ast_expr_from_val(__isl_take isl_val *v)
492 {
493 	isl_ctx *ctx;
494 	isl_ast_expr *expr;
495 
496 	if (!v)
497 		return NULL;
498 	if (!isl_val_is_int(v))
499 		isl_die(isl_val_get_ctx(v), isl_error_invalid,
500 			"expecting integer value", goto error);
501 
502 	ctx = isl_val_get_ctx(v);
503 	expr = isl_calloc_type(ctx, isl_ast_expr);
504 	if (!expr)
505 		goto error;
506 
507 	expr->ctx = ctx;
508 	isl_ctx_ref(ctx);
509 	expr->ref = 1;
510 	expr->type = isl_ast_expr_int;
511 	expr->u.v = v;
512 
513 	return expr;
514 error:
515 	isl_val_free(v);
516 	return NULL;
517 }
518 
519 /* Create an expression representing the unary operation "type" applied to
520  * "arg".
521  */
isl_ast_expr_alloc_unary(enum isl_ast_expr_op_type type,__isl_take isl_ast_expr * arg)522 __isl_give isl_ast_expr *isl_ast_expr_alloc_unary(
523 	enum isl_ast_expr_op_type type, __isl_take isl_ast_expr *arg)
524 {
525 	isl_ctx *ctx;
526 	isl_ast_expr *expr = NULL;
527 
528 	if (!arg)
529 		return NULL;
530 
531 	ctx = isl_ast_expr_get_ctx(arg);
532 	expr = isl_ast_expr_alloc_op(ctx, type, 1);
533 	if (!expr)
534 		goto error;
535 
536 	expr->u.op.args[0] = arg;
537 
538 	return expr;
539 error:
540 	isl_ast_expr_free(arg);
541 	return NULL;
542 }
543 
544 /* Create an expression representing the negation of "arg".
545  */
isl_ast_expr_neg(__isl_take isl_ast_expr * arg)546 __isl_give isl_ast_expr *isl_ast_expr_neg(__isl_take isl_ast_expr *arg)
547 {
548 	return isl_ast_expr_alloc_unary(isl_ast_expr_op_minus, arg);
549 }
550 
551 /* Create an expression representing the address of "expr".
552  */
isl_ast_expr_address_of(__isl_take isl_ast_expr * expr)553 __isl_give isl_ast_expr *isl_ast_expr_address_of(__isl_take isl_ast_expr *expr)
554 {
555 	if (!expr)
556 		return NULL;
557 
558 	if (isl_ast_expr_get_type(expr) != isl_ast_expr_op ||
559 	    isl_ast_expr_get_op_type(expr) != isl_ast_expr_op_access)
560 		isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
561 			"can only take address of access expressions",
562 			return isl_ast_expr_free(expr));
563 
564 	return isl_ast_expr_alloc_unary(isl_ast_expr_op_address_of, expr);
565 }
566 
567 /* Create an expression representing the binary operation "type"
568  * applied to "expr1" and "expr2".
569  */
isl_ast_expr_alloc_binary(enum isl_ast_expr_op_type type,__isl_take isl_ast_expr * expr1,__isl_take isl_ast_expr * expr2)570 __isl_give isl_ast_expr *isl_ast_expr_alloc_binary(
571 	enum isl_ast_expr_op_type type,
572 	__isl_take isl_ast_expr *expr1, __isl_take isl_ast_expr *expr2)
573 {
574 	isl_ctx *ctx;
575 	isl_ast_expr *expr = NULL;
576 
577 	if (!expr1 || !expr2)
578 		goto error;
579 
580 	ctx = isl_ast_expr_get_ctx(expr1);
581 	expr = isl_ast_expr_alloc_op(ctx, type, 2);
582 	if (!expr)
583 		goto error;
584 
585 	expr->u.op.args[0] = expr1;
586 	expr->u.op.args[1] = expr2;
587 
588 	return expr;
589 error:
590 	isl_ast_expr_free(expr1);
591 	isl_ast_expr_free(expr2);
592 	return NULL;
593 }
594 
595 /* Create an expression representing the sum of "expr1" and "expr2".
596  */
isl_ast_expr_add(__isl_take isl_ast_expr * expr1,__isl_take isl_ast_expr * expr2)597 __isl_give isl_ast_expr *isl_ast_expr_add(__isl_take isl_ast_expr *expr1,
598 	__isl_take isl_ast_expr *expr2)
599 {
600 	return isl_ast_expr_alloc_binary(isl_ast_expr_op_add, expr1, expr2);
601 }
602 
603 /* Create an expression representing the difference of "expr1" and "expr2".
604  */
isl_ast_expr_sub(__isl_take isl_ast_expr * expr1,__isl_take isl_ast_expr * expr2)605 __isl_give isl_ast_expr *isl_ast_expr_sub(__isl_take isl_ast_expr *expr1,
606 	__isl_take isl_ast_expr *expr2)
607 {
608 	return isl_ast_expr_alloc_binary(isl_ast_expr_op_sub, expr1, expr2);
609 }
610 
611 /* Create an expression representing the product of "expr1" and "expr2".
612  */
isl_ast_expr_mul(__isl_take isl_ast_expr * expr1,__isl_take isl_ast_expr * expr2)613 __isl_give isl_ast_expr *isl_ast_expr_mul(__isl_take isl_ast_expr *expr1,
614 	__isl_take isl_ast_expr *expr2)
615 {
616 	return isl_ast_expr_alloc_binary(isl_ast_expr_op_mul, expr1, expr2);
617 }
618 
619 /* Create an expression representing the quotient of "expr1" and "expr2".
620  */
isl_ast_expr_div(__isl_take isl_ast_expr * expr1,__isl_take isl_ast_expr * expr2)621 __isl_give isl_ast_expr *isl_ast_expr_div(__isl_take isl_ast_expr *expr1,
622 	__isl_take isl_ast_expr *expr2)
623 {
624 	return isl_ast_expr_alloc_binary(isl_ast_expr_op_div, expr1, expr2);
625 }
626 
627 /* Create an expression representing the quotient of the integer
628  * division of "expr1" by "expr2", where "expr1" is known to be
629  * non-negative.
630  */
isl_ast_expr_pdiv_q(__isl_take isl_ast_expr * expr1,__isl_take isl_ast_expr * expr2)631 __isl_give isl_ast_expr *isl_ast_expr_pdiv_q(__isl_take isl_ast_expr *expr1,
632 	__isl_take isl_ast_expr *expr2)
633 {
634 	return isl_ast_expr_alloc_binary(isl_ast_expr_op_pdiv_q, expr1, expr2);
635 }
636 
637 /* Create an expression representing the remainder of the integer
638  * division of "expr1" by "expr2", where "expr1" is known to be
639  * non-negative.
640  */
isl_ast_expr_pdiv_r(__isl_take isl_ast_expr * expr1,__isl_take isl_ast_expr * expr2)641 __isl_give isl_ast_expr *isl_ast_expr_pdiv_r(__isl_take isl_ast_expr *expr1,
642 	__isl_take isl_ast_expr *expr2)
643 {
644 	return isl_ast_expr_alloc_binary(isl_ast_expr_op_pdiv_r, expr1, expr2);
645 }
646 
647 /* Create an expression representing the conjunction of "expr1" and "expr2".
648  */
isl_ast_expr_and(__isl_take isl_ast_expr * expr1,__isl_take isl_ast_expr * expr2)649 __isl_give isl_ast_expr *isl_ast_expr_and(__isl_take isl_ast_expr *expr1,
650 	__isl_take isl_ast_expr *expr2)
651 {
652 	return isl_ast_expr_alloc_binary(isl_ast_expr_op_and, expr1, expr2);
653 }
654 
655 /* Create an expression representing the conjunction of "expr1" and "expr2",
656  * where "expr2" is evaluated only if "expr1" is evaluated to true.
657  */
isl_ast_expr_and_then(__isl_take isl_ast_expr * expr1,__isl_take isl_ast_expr * expr2)658 __isl_give isl_ast_expr *isl_ast_expr_and_then(__isl_take isl_ast_expr *expr1,
659 	__isl_take isl_ast_expr *expr2)
660 {
661 	return isl_ast_expr_alloc_binary(isl_ast_expr_op_and_then, expr1, expr2);
662 }
663 
664 /* Create an expression representing the disjunction of "expr1" and "expr2".
665  */
isl_ast_expr_or(__isl_take isl_ast_expr * expr1,__isl_take isl_ast_expr * expr2)666 __isl_give isl_ast_expr *isl_ast_expr_or(__isl_take isl_ast_expr *expr1,
667 	__isl_take isl_ast_expr *expr2)
668 {
669 	return isl_ast_expr_alloc_binary(isl_ast_expr_op_or, expr1, expr2);
670 }
671 
672 /* Create an expression representing the disjunction of "expr1" and "expr2",
673  * where "expr2" is evaluated only if "expr1" is evaluated to false.
674  */
isl_ast_expr_or_else(__isl_take isl_ast_expr * expr1,__isl_take isl_ast_expr * expr2)675 __isl_give isl_ast_expr *isl_ast_expr_or_else(__isl_take isl_ast_expr *expr1,
676 	__isl_take isl_ast_expr *expr2)
677 {
678 	return isl_ast_expr_alloc_binary(isl_ast_expr_op_or_else, expr1, expr2);
679 }
680 
681 /* Create an expression representing "expr1" less than or equal to "expr2".
682  */
isl_ast_expr_le(__isl_take isl_ast_expr * expr1,__isl_take isl_ast_expr * expr2)683 __isl_give isl_ast_expr *isl_ast_expr_le(__isl_take isl_ast_expr *expr1,
684 	__isl_take isl_ast_expr *expr2)
685 {
686 	return isl_ast_expr_alloc_binary(isl_ast_expr_op_le, expr1, expr2);
687 }
688 
689 /* Create an expression representing "expr1" less than "expr2".
690  */
isl_ast_expr_lt(__isl_take isl_ast_expr * expr1,__isl_take isl_ast_expr * expr2)691 __isl_give isl_ast_expr *isl_ast_expr_lt(__isl_take isl_ast_expr *expr1,
692 	__isl_take isl_ast_expr *expr2)
693 {
694 	return isl_ast_expr_alloc_binary(isl_ast_expr_op_lt, expr1, expr2);
695 }
696 
697 /* Create an expression representing "expr1" greater than or equal to "expr2".
698  */
isl_ast_expr_ge(__isl_take isl_ast_expr * expr1,__isl_take isl_ast_expr * expr2)699 __isl_give isl_ast_expr *isl_ast_expr_ge(__isl_take isl_ast_expr *expr1,
700 	__isl_take isl_ast_expr *expr2)
701 {
702 	return isl_ast_expr_alloc_binary(isl_ast_expr_op_ge, expr1, expr2);
703 }
704 
705 /* Create an expression representing "expr1" greater than "expr2".
706  */
isl_ast_expr_gt(__isl_take isl_ast_expr * expr1,__isl_take isl_ast_expr * expr2)707 __isl_give isl_ast_expr *isl_ast_expr_gt(__isl_take isl_ast_expr *expr1,
708 	__isl_take isl_ast_expr *expr2)
709 {
710 	return isl_ast_expr_alloc_binary(isl_ast_expr_op_gt, expr1, expr2);
711 }
712 
713 /* Create an expression representing "expr1" equal to "expr2".
714  */
isl_ast_expr_eq(__isl_take isl_ast_expr * expr1,__isl_take isl_ast_expr * expr2)715 __isl_give isl_ast_expr *isl_ast_expr_eq(__isl_take isl_ast_expr *expr1,
716 	__isl_take isl_ast_expr *expr2)
717 {
718 	return isl_ast_expr_alloc_binary(isl_ast_expr_op_eq, expr1, expr2);
719 }
720 
721 /* Create an expression of type "type" with as arguments "arg0" followed
722  * by "arguments".
723  */
ast_expr_with_arguments(enum isl_ast_expr_op_type type,__isl_take isl_ast_expr * arg0,__isl_take isl_ast_expr_list * arguments)724 static __isl_give isl_ast_expr *ast_expr_with_arguments(
725 	enum isl_ast_expr_op_type type, __isl_take isl_ast_expr *arg0,
726 	__isl_take isl_ast_expr_list *arguments)
727 {
728 	int i;
729 	isl_size n;
730 	isl_ctx *ctx;
731 	isl_ast_expr *res = NULL;
732 
733 	if (!arg0 || !arguments)
734 		goto error;
735 
736 	ctx = isl_ast_expr_get_ctx(arg0);
737 	n = isl_ast_expr_list_n_ast_expr(arguments);
738 	if (n < 0)
739 		goto error;
740 	res = isl_ast_expr_alloc_op(ctx, type, 1 + n);
741 	if (!res)
742 		goto error;
743 	for (i = 0; i < n; ++i) {
744 		isl_ast_expr *arg;
745 		arg = isl_ast_expr_list_get_ast_expr(arguments, i);
746 		res->u.op.args[1 + i] = arg;
747 		if (!arg)
748 			goto error;
749 	}
750 	res->u.op.args[0] = arg0;
751 
752 	isl_ast_expr_list_free(arguments);
753 	return res;
754 error:
755 	isl_ast_expr_free(arg0);
756 	isl_ast_expr_list_free(arguments);
757 	isl_ast_expr_free(res);
758 	return NULL;
759 }
760 
761 /* Create an expression representing an access to "array" with index
762  * expressions "indices".
763  */
isl_ast_expr_access(__isl_take isl_ast_expr * array,__isl_take isl_ast_expr_list * indices)764 __isl_give isl_ast_expr *isl_ast_expr_access(__isl_take isl_ast_expr *array,
765 	__isl_take isl_ast_expr_list *indices)
766 {
767 	return ast_expr_with_arguments(isl_ast_expr_op_access, array, indices);
768 }
769 
770 /* Create an expression representing a call to "function" with argument
771  * expressions "arguments".
772  */
isl_ast_expr_call(__isl_take isl_ast_expr * function,__isl_take isl_ast_expr_list * arguments)773 __isl_give isl_ast_expr *isl_ast_expr_call(__isl_take isl_ast_expr *function,
774 	__isl_take isl_ast_expr_list *arguments)
775 {
776 	return ast_expr_with_arguments(isl_ast_expr_op_call, function, arguments);
777 }
778 
779 /* For each subexpression of "expr" of type isl_ast_expr_id,
780  * if it appears in "id2expr", then replace it by the corresponding
781  * expression.
782  */
isl_ast_expr_substitute_ids(__isl_take isl_ast_expr * expr,__isl_take isl_id_to_ast_expr * id2expr)783 __isl_give isl_ast_expr *isl_ast_expr_substitute_ids(
784 	__isl_take isl_ast_expr *expr, __isl_take isl_id_to_ast_expr *id2expr)
785 {
786 	int i;
787 	isl_maybe_isl_ast_expr m;
788 
789 	if (!expr || !id2expr)
790 		goto error;
791 
792 	switch (expr->type) {
793 	case isl_ast_expr_int:
794 		break;
795 	case isl_ast_expr_id:
796 		m = isl_id_to_ast_expr_try_get(id2expr, expr->u.id);
797 		if (m.valid < 0)
798 			goto error;
799 		if (!m.valid)
800 			break;
801 		isl_ast_expr_free(expr);
802 		expr = m.value;
803 		break;
804 	case isl_ast_expr_op:
805 		for (i = 0; i < expr->u.op.n_arg; ++i) {
806 			isl_ast_expr *arg;
807 			arg = isl_ast_expr_copy(expr->u.op.args[i]);
808 			arg = isl_ast_expr_substitute_ids(arg,
809 					    isl_id_to_ast_expr_copy(id2expr));
810 			if (arg == expr->u.op.args[i]) {
811 				isl_ast_expr_free(arg);
812 				continue;
813 			}
814 			if (!arg)
815 				expr = isl_ast_expr_free(expr);
816 			expr = isl_ast_expr_cow(expr);
817 			if (!expr) {
818 				isl_ast_expr_free(arg);
819 				break;
820 			}
821 			isl_ast_expr_free(expr->u.op.args[i]);
822 			expr->u.op.args[i] = arg;
823 		}
824 		break;
825 	case isl_ast_expr_error:
826 		expr = isl_ast_expr_free(expr);
827 		break;
828 	}
829 
830 	isl_id_to_ast_expr_free(id2expr);
831 	return expr;
832 error:
833 	isl_ast_expr_free(expr);
834 	isl_id_to_ast_expr_free(id2expr);
835 	return NULL;
836 }
837 
isl_ast_node_get_ctx(__isl_keep isl_ast_node * node)838 isl_ctx *isl_ast_node_get_ctx(__isl_keep isl_ast_node *node)
839 {
840 	return node ? node->ctx : NULL;
841 }
842 
isl_ast_node_get_type(__isl_keep isl_ast_node * node)843 enum isl_ast_node_type isl_ast_node_get_type(__isl_keep isl_ast_node *node)
844 {
845 	return node ? node->type : isl_ast_node_error;
846 }
847 
isl_ast_node_alloc(isl_ctx * ctx,enum isl_ast_node_type type)848 __isl_give isl_ast_node *isl_ast_node_alloc(isl_ctx *ctx,
849 	enum isl_ast_node_type type)
850 {
851 	isl_ast_node *node;
852 
853 	node = isl_calloc_type(ctx, isl_ast_node);
854 	if (!node)
855 		return NULL;
856 
857 	node->ctx = ctx;
858 	isl_ctx_ref(ctx);
859 	node->ref = 1;
860 	node->type = type;
861 
862 	return node;
863 }
864 
865 /* Create an if node with the given guard.
866  *
867  * The then body needs to be filled in later.
868  */
isl_ast_node_alloc_if(__isl_take isl_ast_expr * guard)869 __isl_give isl_ast_node *isl_ast_node_alloc_if(__isl_take isl_ast_expr *guard)
870 {
871 	isl_ast_node *node;
872 
873 	if (!guard)
874 		return NULL;
875 
876 	node = isl_ast_node_alloc(isl_ast_expr_get_ctx(guard), isl_ast_node_if);
877 	if (!node)
878 		goto error;
879 	node->u.i.guard = guard;
880 
881 	return node;
882 error:
883 	isl_ast_expr_free(guard);
884 	return NULL;
885 }
886 
887 /* Create a for node with the given iterator.
888  *
889  * The remaining fields need to be filled in later.
890  */
isl_ast_node_alloc_for(__isl_take isl_id * id)891 __isl_give isl_ast_node *isl_ast_node_alloc_for(__isl_take isl_id *id)
892 {
893 	isl_ast_node *node;
894 	isl_ctx *ctx;
895 
896 	if (!id)
897 		return NULL;
898 
899 	ctx = isl_id_get_ctx(id);
900 	node = isl_ast_node_alloc(ctx, isl_ast_node_for);
901 	if (!node)
902 		goto error;
903 
904 	node->u.f.iterator = isl_ast_expr_from_id(id);
905 	if (!node->u.f.iterator)
906 		return isl_ast_node_free(node);
907 
908 	return node;
909 error:
910 	isl_id_free(id);
911 	return NULL;
912 }
913 
914 /* Create a mark node, marking "node" with "id".
915  */
isl_ast_node_alloc_mark(__isl_take isl_id * id,__isl_take isl_ast_node * node)916 __isl_give isl_ast_node *isl_ast_node_alloc_mark(__isl_take isl_id *id,
917 	__isl_take isl_ast_node *node)
918 {
919 	isl_ctx *ctx;
920 	isl_ast_node *mark;
921 
922 	if (!id || !node)
923 		goto error;
924 
925 	ctx = isl_id_get_ctx(id);
926 	mark = isl_ast_node_alloc(ctx, isl_ast_node_mark);
927 	if (!mark)
928 		goto error;
929 
930 	mark->u.m.mark = id;
931 	mark->u.m.node = node;
932 
933 	return mark;
934 error:
935 	isl_id_free(id);
936 	isl_ast_node_free(node);
937 	return NULL;
938 }
939 
940 /* Create a user node evaluating "expr".
941  */
isl_ast_node_alloc_user(__isl_take isl_ast_expr * expr)942 __isl_give isl_ast_node *isl_ast_node_alloc_user(__isl_take isl_ast_expr *expr)
943 {
944 	isl_ctx *ctx;
945 	isl_ast_node *node;
946 
947 	if (!expr)
948 		return NULL;
949 
950 	ctx = isl_ast_expr_get_ctx(expr);
951 	node = isl_ast_node_alloc(ctx, isl_ast_node_user);
952 	if (!node)
953 		goto error;
954 
955 	node->u.e.expr = expr;
956 
957 	return node;
958 error:
959 	isl_ast_expr_free(expr);
960 	return NULL;
961 }
962 
963 /* Create a block node with the given children.
964  */
isl_ast_node_alloc_block(__isl_take isl_ast_node_list * list)965 __isl_give isl_ast_node *isl_ast_node_alloc_block(
966 	__isl_take isl_ast_node_list *list)
967 {
968 	isl_ast_node *node;
969 	isl_ctx *ctx;
970 
971 	if (!list)
972 		return NULL;
973 
974 	ctx = isl_ast_node_list_get_ctx(list);
975 	node = isl_ast_node_alloc(ctx, isl_ast_node_block);
976 	if (!node)
977 		goto error;
978 
979 	node->u.b.children = list;
980 
981 	return node;
982 error:
983 	isl_ast_node_list_free(list);
984 	return NULL;
985 }
986 
987 /* Represent the given list of nodes as a single node, either by
988  * extract the node from a single element list or by creating
989  * a block node with the list of nodes as children.
990  */
isl_ast_node_from_ast_node_list(__isl_take isl_ast_node_list * list)991 __isl_give isl_ast_node *isl_ast_node_from_ast_node_list(
992 	__isl_take isl_ast_node_list *list)
993 {
994 	isl_size n;
995 	isl_ast_node *node;
996 
997 	n = isl_ast_node_list_n_ast_node(list);
998 	if (n < 0)
999 		goto error;
1000 	if (n != 1)
1001 		return isl_ast_node_alloc_block(list);
1002 
1003 	node = isl_ast_node_list_get_ast_node(list, 0);
1004 	isl_ast_node_list_free(list);
1005 
1006 	return node;
1007 error:
1008 	isl_ast_node_list_free(list);
1009 	return NULL;
1010 }
1011 
isl_ast_node_copy(__isl_keep isl_ast_node * node)1012 __isl_give isl_ast_node *isl_ast_node_copy(__isl_keep isl_ast_node *node)
1013 {
1014 	if (!node)
1015 		return NULL;
1016 
1017 	node->ref++;
1018 	return node;
1019 }
1020 
isl_ast_node_dup(__isl_keep isl_ast_node * node)1021 __isl_give isl_ast_node *isl_ast_node_dup(__isl_keep isl_ast_node *node)
1022 {
1023 	isl_ast_node *dup;
1024 
1025 	if (!node)
1026 		return NULL;
1027 
1028 	dup = isl_ast_node_alloc(isl_ast_node_get_ctx(node), node->type);
1029 	if (!dup)
1030 		return NULL;
1031 
1032 	switch (node->type) {
1033 	case isl_ast_node_if:
1034 		dup->u.i.guard = isl_ast_expr_copy(node->u.i.guard);
1035 		dup->u.i.then = isl_ast_node_copy(node->u.i.then);
1036 		dup->u.i.else_node = isl_ast_node_copy(node->u.i.else_node);
1037 		if (!dup->u.i.guard  || !dup->u.i.then ||
1038 		    (node->u.i.else_node && !dup->u.i.else_node))
1039 			return isl_ast_node_free(dup);
1040 		break;
1041 	case isl_ast_node_for:
1042 		dup->u.f.iterator = isl_ast_expr_copy(node->u.f.iterator);
1043 		dup->u.f.init = isl_ast_expr_copy(node->u.f.init);
1044 		dup->u.f.cond = isl_ast_expr_copy(node->u.f.cond);
1045 		dup->u.f.inc = isl_ast_expr_copy(node->u.f.inc);
1046 		dup->u.f.body = isl_ast_node_copy(node->u.f.body);
1047 		if (!dup->u.f.iterator || !dup->u.f.init || !dup->u.f.cond ||
1048 		    !dup->u.f.inc || !dup->u.f.body)
1049 			return isl_ast_node_free(dup);
1050 		break;
1051 	case isl_ast_node_block:
1052 		dup->u.b.children = isl_ast_node_list_copy(node->u.b.children);
1053 		if (!dup->u.b.children)
1054 			return isl_ast_node_free(dup);
1055 		break;
1056 	case isl_ast_node_mark:
1057 		dup->u.m.mark = isl_id_copy(node->u.m.mark);
1058 		dup->u.m.node = isl_ast_node_copy(node->u.m.node);
1059 		if (!dup->u.m.mark || !dup->u.m.node)
1060 			return isl_ast_node_free(dup);
1061 		break;
1062 	case isl_ast_node_user:
1063 		dup->u.e.expr = isl_ast_expr_copy(node->u.e.expr);
1064 		if (!dup->u.e.expr)
1065 			return isl_ast_node_free(dup);
1066 		break;
1067 	case isl_ast_node_error:
1068 		break;
1069 	}
1070 
1071 	return dup;
1072 }
1073 
isl_ast_node_cow(__isl_take isl_ast_node * node)1074 __isl_give isl_ast_node *isl_ast_node_cow(__isl_take isl_ast_node *node)
1075 {
1076 	if (!node)
1077 		return NULL;
1078 
1079 	if (node->ref == 1)
1080 		return node;
1081 	node->ref--;
1082 	return isl_ast_node_dup(node);
1083 }
1084 
isl_ast_node_free(__isl_take isl_ast_node * node)1085 __isl_null isl_ast_node *isl_ast_node_free(__isl_take isl_ast_node *node)
1086 {
1087 	if (!node)
1088 		return NULL;
1089 
1090 	if (--node->ref > 0)
1091 		return NULL;
1092 
1093 	switch (node->type) {
1094 	case isl_ast_node_if:
1095 		isl_ast_expr_free(node->u.i.guard);
1096 		isl_ast_node_free(node->u.i.then);
1097 		isl_ast_node_free(node->u.i.else_node);
1098 		break;
1099 	case isl_ast_node_for:
1100 		isl_ast_expr_free(node->u.f.iterator);
1101 		isl_ast_expr_free(node->u.f.init);
1102 		isl_ast_expr_free(node->u.f.cond);
1103 		isl_ast_expr_free(node->u.f.inc);
1104 		isl_ast_node_free(node->u.f.body);
1105 		break;
1106 	case isl_ast_node_block:
1107 		isl_ast_node_list_free(node->u.b.children);
1108 		break;
1109 	case isl_ast_node_mark:
1110 		isl_id_free(node->u.m.mark);
1111 		isl_ast_node_free(node->u.m.node);
1112 		break;
1113 	case isl_ast_node_user:
1114 		isl_ast_expr_free(node->u.e.expr);
1115 		break;
1116 	case isl_ast_node_error:
1117 		break;
1118 	}
1119 
1120 	isl_id_free(node->annotation);
1121 	isl_ctx_deref(node->ctx);
1122 	free(node);
1123 
1124 	return NULL;
1125 }
1126 
1127 /* Replace the body of the for node "node" by "body".
1128  */
isl_ast_node_for_set_body(__isl_take isl_ast_node * node,__isl_take isl_ast_node * body)1129 __isl_give isl_ast_node *isl_ast_node_for_set_body(
1130 	__isl_take isl_ast_node *node, __isl_take isl_ast_node *body)
1131 {
1132 	node = isl_ast_node_cow(node);
1133 	if (!node || !body)
1134 		goto error;
1135 	if (node->type != isl_ast_node_for)
1136 		isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1137 			"not a for node", goto error);
1138 
1139 	isl_ast_node_free(node->u.f.body);
1140 	node->u.f.body = body;
1141 
1142 	return node;
1143 error:
1144 	isl_ast_node_free(node);
1145 	isl_ast_node_free(body);
1146 	return NULL;
1147 }
1148 
isl_ast_node_for_get_body(__isl_keep isl_ast_node * node)1149 __isl_give isl_ast_node *isl_ast_node_for_get_body(
1150 	__isl_keep isl_ast_node *node)
1151 {
1152 	if (!node)
1153 		return NULL;
1154 	if (node->type != isl_ast_node_for)
1155 		isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1156 			"not a for node", return NULL);
1157 	return isl_ast_node_copy(node->u.f.body);
1158 }
1159 
1160 /* Mark the given for node as being degenerate.
1161  */
isl_ast_node_for_mark_degenerate(__isl_take isl_ast_node * node)1162 __isl_give isl_ast_node *isl_ast_node_for_mark_degenerate(
1163 	__isl_take isl_ast_node *node)
1164 {
1165 	node = isl_ast_node_cow(node);
1166 	if (!node)
1167 		return NULL;
1168 	node->u.f.degenerate = 1;
1169 	return node;
1170 }
1171 
isl_ast_node_for_is_degenerate(__isl_keep isl_ast_node * node)1172 isl_bool isl_ast_node_for_is_degenerate(__isl_keep isl_ast_node *node)
1173 {
1174 	if (!node)
1175 		return isl_bool_error;
1176 	if (node->type != isl_ast_node_for)
1177 		isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1178 			"not a for node", return isl_bool_error);
1179 	return isl_bool_ok(node->u.f.degenerate);
1180 }
1181 
isl_ast_node_for_get_iterator(__isl_keep isl_ast_node * node)1182 __isl_give isl_ast_expr *isl_ast_node_for_get_iterator(
1183 	__isl_keep isl_ast_node *node)
1184 {
1185 	if (!node)
1186 		return NULL;
1187 	if (node->type != isl_ast_node_for)
1188 		isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1189 			"not a for node", return NULL);
1190 	return isl_ast_expr_copy(node->u.f.iterator);
1191 }
1192 
isl_ast_node_for_get_init(__isl_keep isl_ast_node * node)1193 __isl_give isl_ast_expr *isl_ast_node_for_get_init(
1194 	__isl_keep isl_ast_node *node)
1195 {
1196 	if (!node)
1197 		return NULL;
1198 	if (node->type != isl_ast_node_for)
1199 		isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1200 			"not a for node", return NULL);
1201 	return isl_ast_expr_copy(node->u.f.init);
1202 }
1203 
1204 /* Return the condition expression of the given for node.
1205  *
1206  * If the for node is degenerate, then the condition is not explicitly
1207  * stored in the node.  Instead, it is constructed as
1208  *
1209  *	iterator <= init
1210  */
isl_ast_node_for_get_cond(__isl_keep isl_ast_node * node)1211 __isl_give isl_ast_expr *isl_ast_node_for_get_cond(
1212 	__isl_keep isl_ast_node *node)
1213 {
1214 	if (!node)
1215 		return NULL;
1216 	if (node->type != isl_ast_node_for)
1217 		isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1218 			"not a for node", return NULL);
1219 	if (!node->u.f.degenerate)
1220 		return isl_ast_expr_copy(node->u.f.cond);
1221 
1222 	return isl_ast_expr_alloc_binary(isl_ast_expr_op_le,
1223 				isl_ast_expr_copy(node->u.f.iterator),
1224 				isl_ast_expr_copy(node->u.f.init));
1225 }
1226 
1227 /* Return the increment of the given for node.
1228  *
1229  * If the for node is degenerate, then the increment is not explicitly
1230  * stored in the node.  We simply return "1".
1231  */
isl_ast_node_for_get_inc(__isl_keep isl_ast_node * node)1232 __isl_give isl_ast_expr *isl_ast_node_for_get_inc(
1233 	__isl_keep isl_ast_node *node)
1234 {
1235 	if (!node)
1236 		return NULL;
1237 	if (node->type != isl_ast_node_for)
1238 		isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1239 			"not a for node", return NULL);
1240 	if (!node->u.f.degenerate)
1241 		return isl_ast_expr_copy(node->u.f.inc);
1242 	return isl_ast_expr_alloc_int_si(isl_ast_node_get_ctx(node), 1);
1243 }
1244 
1245 /* Replace the then branch of the if node "node" by "child".
1246  */
isl_ast_node_if_set_then(__isl_take isl_ast_node * node,__isl_take isl_ast_node * child)1247 __isl_give isl_ast_node *isl_ast_node_if_set_then(
1248 	__isl_take isl_ast_node *node, __isl_take isl_ast_node *child)
1249 {
1250 	node = isl_ast_node_cow(node);
1251 	if (!node || !child)
1252 		goto error;
1253 	if (node->type != isl_ast_node_if)
1254 		isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1255 			"not an if node", goto error);
1256 
1257 	isl_ast_node_free(node->u.i.then);
1258 	node->u.i.then = child;
1259 
1260 	return node;
1261 error:
1262 	isl_ast_node_free(node);
1263 	isl_ast_node_free(child);
1264 	return NULL;
1265 }
1266 
1267 /* Return the then-node of the given if-node.
1268  */
isl_ast_node_if_get_then_node(__isl_keep isl_ast_node * node)1269 __isl_give isl_ast_node *isl_ast_node_if_get_then_node(
1270 	__isl_keep isl_ast_node *node)
1271 {
1272 	if (!node)
1273 		return NULL;
1274 	if (node->type != isl_ast_node_if)
1275 		isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1276 			"not an if node", return NULL);
1277 	return isl_ast_node_copy(node->u.i.then);
1278 }
1279 
1280 /* This is an alternative name for the function above.
1281  */
isl_ast_node_if_get_then(__isl_keep isl_ast_node * node)1282 __isl_give isl_ast_node *isl_ast_node_if_get_then(
1283 	__isl_keep isl_ast_node *node)
1284 {
1285 	return isl_ast_node_if_get_then_node(node);
1286 }
1287 
1288 /* Does the given if-node have an else-node?
1289  */
isl_ast_node_if_has_else_node(__isl_keep isl_ast_node * node)1290 isl_bool isl_ast_node_if_has_else_node(__isl_keep isl_ast_node *node)
1291 {
1292 	if (!node)
1293 		return isl_bool_error;
1294 	if (node->type != isl_ast_node_if)
1295 		isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1296 			"not an if node", return isl_bool_error);
1297 	return isl_bool_ok(node->u.i.else_node != NULL);
1298 }
1299 
1300 /* This is an alternative name for the function above.
1301  */
isl_ast_node_if_has_else(__isl_keep isl_ast_node * node)1302 isl_bool isl_ast_node_if_has_else(__isl_keep isl_ast_node *node)
1303 {
1304 	return isl_ast_node_if_has_else_node(node);
1305 }
1306 
1307 /* Return the else-node of the given if-node,
1308  * assuming there is one.
1309  */
isl_ast_node_if_get_else_node(__isl_keep isl_ast_node * node)1310 __isl_give isl_ast_node *isl_ast_node_if_get_else_node(
1311 	__isl_keep isl_ast_node *node)
1312 {
1313 	if (!node)
1314 		return NULL;
1315 	if (node->type != isl_ast_node_if)
1316 		isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1317 			"not an if node", return NULL);
1318 	return isl_ast_node_copy(node->u.i.else_node);
1319 }
1320 
1321 /* This is an alternative name for the function above.
1322  */
isl_ast_node_if_get_else(__isl_keep isl_ast_node * node)1323 __isl_give isl_ast_node *isl_ast_node_if_get_else(
1324 	__isl_keep isl_ast_node *node)
1325 {
1326 	return isl_ast_node_if_get_else_node(node);
1327 }
1328 
isl_ast_node_if_get_cond(__isl_keep isl_ast_node * node)1329 __isl_give isl_ast_expr *isl_ast_node_if_get_cond(
1330 	__isl_keep isl_ast_node *node)
1331 {
1332 	if (!node)
1333 		return NULL;
1334 	if (node->type != isl_ast_node_if)
1335 		isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1336 			"not a guard node", return NULL);
1337 	return isl_ast_expr_copy(node->u.i.guard);
1338 }
1339 
isl_ast_node_block_get_children(__isl_keep isl_ast_node * node)1340 __isl_give isl_ast_node_list *isl_ast_node_block_get_children(
1341 	__isl_keep isl_ast_node *node)
1342 {
1343 	if (!node)
1344 		return NULL;
1345 	if (node->type != isl_ast_node_block)
1346 		isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1347 			"not a block node", return NULL);
1348 	return isl_ast_node_list_copy(node->u.b.children);
1349 }
1350 
isl_ast_node_user_get_expr(__isl_keep isl_ast_node * node)1351 __isl_give isl_ast_expr *isl_ast_node_user_get_expr(
1352 	__isl_keep isl_ast_node *node)
1353 {
1354 	if (!node)
1355 		return NULL;
1356 	if (node->type != isl_ast_node_user)
1357 		isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1358 			"not a user node", return NULL);
1359 
1360 	return isl_ast_expr_copy(node->u.e.expr);
1361 }
1362 
1363 /* Return the mark identifier of the mark node "node".
1364  */
isl_ast_node_mark_get_id(__isl_keep isl_ast_node * node)1365 __isl_give isl_id *isl_ast_node_mark_get_id(__isl_keep isl_ast_node *node)
1366 {
1367 	if (!node)
1368 		return NULL;
1369 	if (node->type != isl_ast_node_mark)
1370 		isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1371 			"not a mark node", return NULL);
1372 
1373 	return isl_id_copy(node->u.m.mark);
1374 }
1375 
1376 /* Return the node marked by mark node "node".
1377  */
isl_ast_node_mark_get_node(__isl_keep isl_ast_node * node)1378 __isl_give isl_ast_node *isl_ast_node_mark_get_node(
1379 	__isl_keep isl_ast_node *node)
1380 {
1381 	if (!node)
1382 		return NULL;
1383 	if (node->type != isl_ast_node_mark)
1384 		isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1385 			"not a mark node", return NULL);
1386 
1387 	return isl_ast_node_copy(node->u.m.node);
1388 }
1389 
isl_ast_node_get_annotation(__isl_keep isl_ast_node * node)1390 __isl_give isl_id *isl_ast_node_get_annotation(__isl_keep isl_ast_node *node)
1391 {
1392 	return node ? isl_id_copy(node->annotation) : NULL;
1393 }
1394 
1395 /* Replace node->annotation by "annotation".
1396  */
isl_ast_node_set_annotation(__isl_take isl_ast_node * node,__isl_take isl_id * annotation)1397 __isl_give isl_ast_node *isl_ast_node_set_annotation(
1398 	__isl_take isl_ast_node *node, __isl_take isl_id *annotation)
1399 {
1400 	node = isl_ast_node_cow(node);
1401 	if (!node || !annotation)
1402 		goto error;
1403 
1404 	isl_id_free(node->annotation);
1405 	node->annotation = annotation;
1406 
1407 	return node;
1408 error:
1409 	isl_id_free(annotation);
1410 	return isl_ast_node_free(node);
1411 }
1412 
1413 /* Traverse the elements of "list" and all their descendants
1414  * in depth first preorder.
1415  *
1416  * Return isl_stat_ok on success and isl_stat_error on failure.
1417  */
nodelist_foreach(__isl_keep isl_ast_node_list * list,isl_bool (* fn)(__isl_keep isl_ast_node * node,void * user),void * user)1418 static isl_stat nodelist_foreach(__isl_keep isl_ast_node_list *list,
1419 	isl_bool (*fn)(__isl_keep isl_ast_node *node, void *user), void *user)
1420 {
1421 	int i;
1422 
1423 	if (!list)
1424 		return isl_stat_error;
1425 
1426 	for (i = 0; i < list->n; ++i) {
1427 		isl_stat ok;
1428 		isl_ast_node *node = list->p[i];
1429 
1430 		ok = isl_ast_node_foreach_descendant_top_down(node, fn, user);
1431 		if (ok < 0)
1432 			return isl_stat_error;
1433 	}
1434 
1435 	return isl_stat_ok;
1436 }
1437 
1438 /* Traverse the descendants of "node" (including the node itself)
1439  * in depth first preorder.
1440  *
1441  * If "fn" returns isl_bool_error on any of the nodes, then the traversal
1442  * is aborted.
1443  * If "fn" returns isl_bool_false on any of the nodes, then the subtree rooted
1444  * at that node is skipped.
1445  *
1446  * Return isl_stat_ok on success and isl_stat_error on failure.
1447  */
isl_ast_node_foreach_descendant_top_down(__isl_keep isl_ast_node * node,isl_bool (* fn)(__isl_keep isl_ast_node * node,void * user),void * user)1448 isl_stat isl_ast_node_foreach_descendant_top_down(
1449 	__isl_keep isl_ast_node *node,
1450 	isl_bool (*fn)(__isl_keep isl_ast_node *node, void *user), void *user)
1451 {
1452 	isl_bool more;
1453 	isl_stat ok;
1454 
1455 	if (!node)
1456 		return isl_stat_error;
1457 
1458 	more = fn(node, user);
1459 	if (more < 0)
1460 		return isl_stat_error;
1461 	if (!more)
1462 		return isl_stat_ok;
1463 
1464 	switch (node->type) {
1465 	case isl_ast_node_for:
1466 		node = node->u.f.body;
1467 		return isl_ast_node_foreach_descendant_top_down(node, fn, user);
1468 	case isl_ast_node_if:
1469 		ok = isl_ast_node_foreach_descendant_top_down(node->u.i.then,
1470 								fn, user);
1471 		if (ok < 0)
1472 			return isl_stat_error;
1473 		if (!node->u.i.else_node)
1474 			return isl_stat_ok;
1475 		node = node->u.i.else_node;
1476 		return isl_ast_node_foreach_descendant_top_down(node, fn, user);
1477 	case isl_ast_node_block:
1478 		return nodelist_foreach(node->u.b.children, fn, user);
1479 	case isl_ast_node_mark:
1480 		node = node->u.m.node;
1481 		return isl_ast_node_foreach_descendant_top_down(node, fn, user);
1482 	case isl_ast_node_user:
1483 		break;
1484 	case isl_ast_node_error:
1485 		return isl_stat_error;
1486 	}
1487 
1488 	return isl_stat_ok;
1489 }
1490 
1491 /* Textual C representation of the various operators.
1492  */
1493 static char *op_str_c[] = {
1494 	[isl_ast_expr_op_and] = "&&",
1495 	[isl_ast_expr_op_and_then] = "&&",
1496 	[isl_ast_expr_op_or] = "||",
1497 	[isl_ast_expr_op_or_else] = "||",
1498 	[isl_ast_expr_op_max] = "max",
1499 	[isl_ast_expr_op_min] = "min",
1500 	[isl_ast_expr_op_minus] = "-",
1501 	[isl_ast_expr_op_add] = "+",
1502 	[isl_ast_expr_op_sub] = "-",
1503 	[isl_ast_expr_op_mul] = "*",
1504 	[isl_ast_expr_op_fdiv_q] = "floord",
1505 	[isl_ast_expr_op_pdiv_q] = "/",
1506 	[isl_ast_expr_op_pdiv_r] = "%",
1507 	[isl_ast_expr_op_zdiv_r] = "%",
1508 	[isl_ast_expr_op_div] = "/",
1509 	[isl_ast_expr_op_eq] = "==",
1510 	[isl_ast_expr_op_le] = "<=",
1511 	[isl_ast_expr_op_ge] = ">=",
1512 	[isl_ast_expr_op_lt] = "<",
1513 	[isl_ast_expr_op_gt] = ">",
1514 	[isl_ast_expr_op_member] = ".",
1515 	[isl_ast_expr_op_address_of] = "&"
1516 };
1517 
1518 /* Precedence in C of the various operators.
1519  * Based on http://en.wikipedia.org/wiki/Operators_in_C_and_C++
1520  * Lowest value means highest precedence.
1521  */
1522 static int op_prec[] = {
1523 	[isl_ast_expr_op_and] = 13,
1524 	[isl_ast_expr_op_and_then] = 13,
1525 	[isl_ast_expr_op_or] = 14,
1526 	[isl_ast_expr_op_or_else] = 14,
1527 	[isl_ast_expr_op_max] = 2,
1528 	[isl_ast_expr_op_min] = 2,
1529 	[isl_ast_expr_op_minus] = 3,
1530 	[isl_ast_expr_op_add] = 6,
1531 	[isl_ast_expr_op_sub] = 6,
1532 	[isl_ast_expr_op_mul] = 5,
1533 	[isl_ast_expr_op_div] = 5,
1534 	[isl_ast_expr_op_fdiv_q] = 2,
1535 	[isl_ast_expr_op_pdiv_q] = 5,
1536 	[isl_ast_expr_op_pdiv_r] = 5,
1537 	[isl_ast_expr_op_zdiv_r] = 5,
1538 	[isl_ast_expr_op_cond] = 15,
1539 	[isl_ast_expr_op_select] = 15,
1540 	[isl_ast_expr_op_eq] = 9,
1541 	[isl_ast_expr_op_le] = 8,
1542 	[isl_ast_expr_op_ge] = 8,
1543 	[isl_ast_expr_op_lt] = 8,
1544 	[isl_ast_expr_op_gt] = 8,
1545 	[isl_ast_expr_op_call] = 2,
1546 	[isl_ast_expr_op_access] = 2,
1547 	[isl_ast_expr_op_member] = 2,
1548 	[isl_ast_expr_op_address_of] = 3
1549 };
1550 
1551 /* Is the operator left-to-right associative?
1552  */
1553 static int op_left[] = {
1554 	[isl_ast_expr_op_and] = 1,
1555 	[isl_ast_expr_op_and_then] = 1,
1556 	[isl_ast_expr_op_or] = 1,
1557 	[isl_ast_expr_op_or_else] = 1,
1558 	[isl_ast_expr_op_max] = 1,
1559 	[isl_ast_expr_op_min] = 1,
1560 	[isl_ast_expr_op_minus] = 0,
1561 	[isl_ast_expr_op_add] = 1,
1562 	[isl_ast_expr_op_sub] = 1,
1563 	[isl_ast_expr_op_mul] = 1,
1564 	[isl_ast_expr_op_div] = 1,
1565 	[isl_ast_expr_op_fdiv_q] = 1,
1566 	[isl_ast_expr_op_pdiv_q] = 1,
1567 	[isl_ast_expr_op_pdiv_r] = 1,
1568 	[isl_ast_expr_op_zdiv_r] = 1,
1569 	[isl_ast_expr_op_cond] = 0,
1570 	[isl_ast_expr_op_select] = 0,
1571 	[isl_ast_expr_op_eq] = 1,
1572 	[isl_ast_expr_op_le] = 1,
1573 	[isl_ast_expr_op_ge] = 1,
1574 	[isl_ast_expr_op_lt] = 1,
1575 	[isl_ast_expr_op_gt] = 1,
1576 	[isl_ast_expr_op_call] = 1,
1577 	[isl_ast_expr_op_access] = 1,
1578 	[isl_ast_expr_op_member] = 1,
1579 	[isl_ast_expr_op_address_of] = 0
1580 };
1581 
is_and(enum isl_ast_expr_op_type op)1582 static int is_and(enum isl_ast_expr_op_type op)
1583 {
1584 	return op == isl_ast_expr_op_and || op == isl_ast_expr_op_and_then;
1585 }
1586 
is_or(enum isl_ast_expr_op_type op)1587 static int is_or(enum isl_ast_expr_op_type op)
1588 {
1589 	return op == isl_ast_expr_op_or || op == isl_ast_expr_op_or_else;
1590 }
1591 
is_add_sub(enum isl_ast_expr_op_type op)1592 static int is_add_sub(enum isl_ast_expr_op_type op)
1593 {
1594 	return op == isl_ast_expr_op_add || op == isl_ast_expr_op_sub;
1595 }
1596 
is_div_mod(enum isl_ast_expr_op_type op)1597 static int is_div_mod(enum isl_ast_expr_op_type op)
1598 {
1599 	return op == isl_ast_expr_op_div ||
1600 	       op == isl_ast_expr_op_pdiv_r ||
1601 	       op == isl_ast_expr_op_zdiv_r;
1602 }
1603 
1604 static __isl_give isl_printer *print_ast_expr_c(__isl_take isl_printer *p,
1605 	__isl_keep isl_ast_expr *expr);
1606 
1607 /* Do we need/want parentheses around "expr" as a subexpression of
1608  * an "op" operation?  If "left" is set, then "expr" is the left-most
1609  * operand.
1610  *
1611  * We only need parentheses if "expr" represents an operation.
1612  *
1613  * If op has a higher precedence than expr->u.op.op, then we need
1614  * parentheses.
1615  * If op and expr->u.op.op have the same precedence, but the operations
1616  * are performed in an order that is different from the associativity,
1617  * then we need parentheses.
1618  *
1619  * An and inside an or technically does not require parentheses,
1620  * but some compilers complain about that, so we add them anyway.
1621  *
1622  * Computations such as "a / b * c" and "a % b + c" can be somewhat
1623  * difficult to read, so we add parentheses for those as well.
1624  */
sub_expr_need_parens(enum isl_ast_expr_op_type op,__isl_keep isl_ast_expr * expr,int left)1625 static int sub_expr_need_parens(enum isl_ast_expr_op_type op,
1626 	__isl_keep isl_ast_expr *expr, int left)
1627 {
1628 	if (expr->type != isl_ast_expr_op)
1629 		return 0;
1630 
1631 	if (op_prec[expr->u.op.op] > op_prec[op])
1632 		return 1;
1633 	if (op_prec[expr->u.op.op] == op_prec[op] && left != op_left[op])
1634 		return 1;
1635 
1636 	if (is_or(op) && is_and(expr->u.op.op))
1637 		return 1;
1638 	if (op == isl_ast_expr_op_mul && expr->u.op.op != isl_ast_expr_op_mul &&
1639 	    op_prec[expr->u.op.op] == op_prec[op])
1640 		return 1;
1641 	if (is_add_sub(op) && is_div_mod(expr->u.op.op))
1642 		return 1;
1643 
1644 	return 0;
1645 }
1646 
1647 /* Print "expr" as a subexpression of an "op" operation in C format.
1648  * If "left" is set, then "expr" is the left-most operand.
1649  */
print_sub_expr_c(__isl_take isl_printer * p,enum isl_ast_expr_op_type op,__isl_keep isl_ast_expr * expr,int left)1650 static __isl_give isl_printer *print_sub_expr_c(__isl_take isl_printer *p,
1651 	enum isl_ast_expr_op_type op, __isl_keep isl_ast_expr *expr, int left)
1652 {
1653 	int need_parens;
1654 
1655 	need_parens = sub_expr_need_parens(op, expr, left);
1656 
1657 	if (need_parens)
1658 		p = isl_printer_print_str(p, "(");
1659 	p = print_ast_expr_c(p, expr);
1660 	if (need_parens)
1661 		p = isl_printer_print_str(p, ")");
1662 	return p;
1663 }
1664 
1665 #define isl_ast_expr_op_last	isl_ast_expr_op_address_of
1666 
1667 /* Data structure that holds the user-specified textual
1668  * representations for the operators in C format.
1669  * The entries are either NULL or copies of strings.
1670  * A NULL entry means that the default name should be used.
1671  */
1672 struct isl_ast_expr_op_names {
1673 	char *op_str[isl_ast_expr_op_last + 1];
1674 };
1675 
1676 /* Create an empty struct isl_ast_expr_op_names.
1677  */
create_names(isl_ctx * ctx)1678 static void *create_names(isl_ctx *ctx)
1679 {
1680 	return isl_calloc_type(ctx, struct isl_ast_expr_op_names);
1681 }
1682 
1683 /* Free a struct isl_ast_expr_op_names along with all memory
1684  * owned by the struct.
1685  */
free_names(void * user)1686 static void free_names(void *user)
1687 {
1688 	int i;
1689 	struct isl_ast_expr_op_names *names = user;
1690 
1691 	if (!user)
1692 		return;
1693 
1694 	for (i = 0; i <= isl_ast_expr_op_last; ++i)
1695 		free(names->op_str[i]);
1696 	free(user);
1697 }
1698 
1699 /* Create an identifier that is used to store
1700  * an isl_ast_expr_op_names note.
1701  */
names_id(isl_ctx * ctx)1702 static __isl_give isl_id *names_id(isl_ctx *ctx)
1703 {
1704 	return isl_id_alloc(ctx, "isl_ast_expr_op_type_names", NULL);
1705 }
1706 
1707 /* Ensure that "p" has a note identified by "id".
1708  * If there is no such note yet, then it is created by "note_create" and
1709  * scheduled do be freed by "note_free".
1710  */
alloc_note(__isl_take isl_printer * p,__isl_keep isl_id * id,void * (* note_create)(isl_ctx *),void (* note_free)(void *))1711 static __isl_give isl_printer *alloc_note(__isl_take isl_printer *p,
1712 	__isl_keep isl_id *id, void *(*note_create)(isl_ctx *),
1713 	void (*note_free)(void *))
1714 {
1715 	isl_ctx *ctx;
1716 	isl_id *note_id;
1717 	isl_bool has_note;
1718 	void *note;
1719 
1720 	has_note = isl_printer_has_note(p, id);
1721 	if (has_note < 0)
1722 		return isl_printer_free(p);
1723 	if (has_note)
1724 		return p;
1725 
1726 	ctx = isl_printer_get_ctx(p);
1727 	note = note_create(ctx);
1728 	if (!note)
1729 		return isl_printer_free(p);
1730 	note_id = isl_id_alloc(ctx, NULL, note);
1731 	if (!note_id)
1732 		note_free(note);
1733 	else
1734 		note_id = isl_id_set_free_user(note_id, note_free);
1735 
1736 	p = isl_printer_set_note(p, isl_id_copy(id), note_id);
1737 
1738 	return p;
1739 }
1740 
1741 /* Ensure that "p" has an isl_ast_expr_op_names note identified by "id".
1742  */
alloc_names(__isl_take isl_printer * p,__isl_keep isl_id * id)1743 static __isl_give isl_printer *alloc_names(__isl_take isl_printer *p,
1744 	__isl_keep isl_id *id)
1745 {
1746 	return alloc_note(p, id, &create_names, &free_names);
1747 }
1748 
1749 /* Retrieve the note identified by "id" from "p".
1750  * The note is assumed to exist.
1751  */
get_note(__isl_keep isl_printer * p,__isl_keep isl_id * id)1752 static void *get_note(__isl_keep isl_printer *p, __isl_keep isl_id *id)
1753 {
1754 	void *note;
1755 
1756 	id = isl_printer_get_note(p, isl_id_copy(id));
1757 	note = isl_id_get_user(id);
1758 	isl_id_free(id);
1759 
1760 	return note;
1761 }
1762 
1763 /* Use "name" to print operations of type "type" to "p".
1764  *
1765  * Store the name in an isl_ast_expr_op_names note attached to "p", such that
1766  * it can be retrieved by get_op_str.
1767  */
isl_ast_expr_op_type_set_print_name(__isl_take isl_printer * p,enum isl_ast_expr_op_type type,__isl_keep const char * name)1768 __isl_give isl_printer *isl_ast_expr_op_type_set_print_name(
1769 	__isl_take isl_printer *p, enum isl_ast_expr_op_type type,
1770 	__isl_keep const char *name)
1771 {
1772 	isl_id *id;
1773 	struct isl_ast_expr_op_names *names;
1774 
1775 	if (!p)
1776 		return NULL;
1777 	if (type > isl_ast_expr_op_last)
1778 		isl_die(isl_printer_get_ctx(p), isl_error_invalid,
1779 			"invalid type", return isl_printer_free(p));
1780 
1781 	id = names_id(isl_printer_get_ctx(p));
1782 	p = alloc_names(p, id);
1783 	names = get_note(p, id);
1784 	isl_id_free(id);
1785 	if (!names)
1786 		return isl_printer_free(p);
1787 	free(names->op_str[type]);
1788 	names->op_str[type] = strdup(name);
1789 
1790 	return p;
1791 }
1792 
1793 /* This is an alternative name for the function above.
1794  */
isl_ast_op_type_set_print_name(__isl_take isl_printer * p,enum isl_ast_expr_op_type type,__isl_keep const char * name)1795 __isl_give isl_printer *isl_ast_op_type_set_print_name(
1796 	__isl_take isl_printer *p, enum isl_ast_expr_op_type type,
1797 	__isl_keep const char *name)
1798 {
1799 	return isl_ast_expr_op_type_set_print_name(p, type, name);
1800 }
1801 
1802 /* Return the textual representation of "type" in C format.
1803  *
1804  * If there is a user-specified name in an isl_ast_expr_op_names note
1805  * associated to "p", then return that.
1806  * Otherwise, return the default name in op_str_c.
1807  */
get_op_str_c(__isl_keep isl_printer * p,enum isl_ast_expr_op_type type)1808 static const char *get_op_str_c(__isl_keep isl_printer *p,
1809 	enum isl_ast_expr_op_type type)
1810 {
1811 	isl_id *id;
1812 	isl_bool has_names;
1813 	struct isl_ast_expr_op_names *names = NULL;
1814 
1815 	id = names_id(isl_printer_get_ctx(p));
1816 	has_names = isl_printer_has_note(p, id);
1817 	if (has_names >= 0 && has_names)
1818 		names = get_note(p, id);
1819 	isl_id_free(id);
1820 	if (names && names->op_str[type])
1821 		return names->op_str[type];
1822 	return op_str_c[type];
1823 }
1824 
1825 /* Print a min or max reduction "expr" in C format.
1826  */
print_min_max_c(__isl_take isl_printer * p,__isl_keep isl_ast_expr * expr)1827 static __isl_give isl_printer *print_min_max_c(__isl_take isl_printer *p,
1828 	__isl_keep isl_ast_expr *expr)
1829 {
1830 	int i = 0;
1831 
1832 	for (i = 1; i < expr->u.op.n_arg; ++i) {
1833 		p = isl_printer_print_str(p, get_op_str_c(p, expr->u.op.op));
1834 		p = isl_printer_print_str(p, "(");
1835 	}
1836 	p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1837 	for (i = 1; i < expr->u.op.n_arg; ++i) {
1838 		p = isl_printer_print_str(p, ", ");
1839 		p = print_ast_expr_c(p, expr->u.op.args[i]);
1840 		p = isl_printer_print_str(p, ")");
1841 	}
1842 
1843 	return p;
1844 }
1845 
1846 /* Print a function call "expr" in C format.
1847  *
1848  * The first argument represents the function to be called.
1849  */
print_call_c(__isl_take isl_printer * p,__isl_keep isl_ast_expr * expr)1850 static __isl_give isl_printer *print_call_c(__isl_take isl_printer *p,
1851 	__isl_keep isl_ast_expr *expr)
1852 {
1853 	int i = 0;
1854 
1855 	p = print_ast_expr_c(p, expr->u.op.args[0]);
1856 	p = isl_printer_print_str(p, "(");
1857 	for (i = 1; i < expr->u.op.n_arg; ++i) {
1858 		if (i != 1)
1859 			p = isl_printer_print_str(p, ", ");
1860 		p = print_ast_expr_c(p, expr->u.op.args[i]);
1861 	}
1862 	p = isl_printer_print_str(p, ")");
1863 
1864 	return p;
1865 }
1866 
1867 /* Print an array access "expr" in C format.
1868  *
1869  * The first argument represents the array being accessed.
1870  */
print_access_c(__isl_take isl_printer * p,__isl_keep isl_ast_expr * expr)1871 static __isl_give isl_printer *print_access_c(__isl_take isl_printer *p,
1872 	__isl_keep isl_ast_expr *expr)
1873 {
1874 	int i = 0;
1875 
1876 	p = print_ast_expr_c(p, expr->u.op.args[0]);
1877 	for (i = 1; i < expr->u.op.n_arg; ++i) {
1878 		p = isl_printer_print_str(p, "[");
1879 		p = print_ast_expr_c(p, expr->u.op.args[i]);
1880 		p = isl_printer_print_str(p, "]");
1881 	}
1882 
1883 	return p;
1884 }
1885 
1886 /* Print "expr" to "p" in C format.
1887  */
print_ast_expr_c(__isl_take isl_printer * p,__isl_keep isl_ast_expr * expr)1888 static __isl_give isl_printer *print_ast_expr_c(__isl_take isl_printer *p,
1889 	__isl_keep isl_ast_expr *expr)
1890 {
1891 	if (!p)
1892 		return NULL;
1893 	if (!expr)
1894 		return isl_printer_free(p);
1895 
1896 	switch (expr->type) {
1897 	case isl_ast_expr_op:
1898 		if (expr->u.op.op == isl_ast_expr_op_call) {
1899 			p = print_call_c(p, expr);
1900 			break;
1901 		}
1902 		if (expr->u.op.op == isl_ast_expr_op_access) {
1903 			p = print_access_c(p, expr);
1904 			break;
1905 		}
1906 		if (expr->u.op.n_arg == 1) {
1907 			p = isl_printer_print_str(p,
1908 						get_op_str_c(p, expr->u.op.op));
1909 			p = print_sub_expr_c(p, expr->u.op.op,
1910 						expr->u.op.args[0], 0);
1911 			break;
1912 		}
1913 		if (expr->u.op.op == isl_ast_expr_op_fdiv_q) {
1914 			const char *name;
1915 
1916 			name = get_op_str_c(p, isl_ast_expr_op_fdiv_q);
1917 			p = isl_printer_print_str(p, name);
1918 			p = isl_printer_print_str(p, "(");
1919 			p = print_ast_expr_c(p, expr->u.op.args[0]);
1920 			p = isl_printer_print_str(p, ", ");
1921 			p = print_ast_expr_c(p, expr->u.op.args[1]);
1922 			p = isl_printer_print_str(p, ")");
1923 			break;
1924 		}
1925 		if (expr->u.op.op == isl_ast_expr_op_max ||
1926 		    expr->u.op.op == isl_ast_expr_op_min) {
1927 			p = print_min_max_c(p, expr);
1928 			break;
1929 		}
1930 		if (expr->u.op.op == isl_ast_expr_op_cond ||
1931 		    expr->u.op.op == isl_ast_expr_op_select) {
1932 			p = print_ast_expr_c(p, expr->u.op.args[0]);
1933 			p = isl_printer_print_str(p, " ? ");
1934 			p = print_ast_expr_c(p, expr->u.op.args[1]);
1935 			p = isl_printer_print_str(p, " : ");
1936 			p = print_ast_expr_c(p, expr->u.op.args[2]);
1937 			break;
1938 		}
1939 		if (expr->u.op.n_arg != 2)
1940 			isl_die(isl_printer_get_ctx(p), isl_error_internal,
1941 				"operation should have two arguments",
1942 				return isl_printer_free(p));
1943 		p = print_sub_expr_c(p, expr->u.op.op, expr->u.op.args[0], 1);
1944 		if (expr->u.op.op != isl_ast_expr_op_member)
1945 			p = isl_printer_print_str(p, " ");
1946 		p = isl_printer_print_str(p, get_op_str_c(p, expr->u.op.op));
1947 		if (expr->u.op.op != isl_ast_expr_op_member)
1948 			p = isl_printer_print_str(p, " ");
1949 		p = print_sub_expr_c(p, expr->u.op.op, expr->u.op.args[1], 0);
1950 		break;
1951 	case isl_ast_expr_id:
1952 		p = isl_printer_print_str(p, isl_id_get_name(expr->u.id));
1953 		break;
1954 	case isl_ast_expr_int:
1955 		p = isl_printer_print_val(p, expr->u.v);
1956 		break;
1957 	case isl_ast_expr_error:
1958 		break;
1959 	}
1960 
1961 	return p;
1962 }
1963 
1964 /* Textual representation of the isl_ast_expr_op_type elements
1965  * for use in a YAML representation of an isl_ast_expr.
1966  */
1967 static char *op_str[] = {
1968 	[isl_ast_expr_op_and] = "and",
1969 	[isl_ast_expr_op_and_then] = "and_then",
1970 	[isl_ast_expr_op_or] = "or",
1971 	[isl_ast_expr_op_or_else] = "or_else",
1972 	[isl_ast_expr_op_max] = "max",
1973 	[isl_ast_expr_op_min] = "min",
1974 	[isl_ast_expr_op_minus] = "minus",
1975 	[isl_ast_expr_op_add] = "add",
1976 	[isl_ast_expr_op_sub] = "sub",
1977 	[isl_ast_expr_op_mul] = "mul",
1978 	[isl_ast_expr_op_div] = "div",
1979 	[isl_ast_expr_op_fdiv_q] = "fdiv_q",
1980 	[isl_ast_expr_op_pdiv_q] = "pdiv_q",
1981 	[isl_ast_expr_op_pdiv_r] = "pdiv_r",
1982 	[isl_ast_expr_op_zdiv_r] = "zdiv_r",
1983 	[isl_ast_expr_op_cond] = "cond",
1984 	[isl_ast_expr_op_select] = "select",
1985 	[isl_ast_expr_op_eq] = "eq",
1986 	[isl_ast_expr_op_le] = "le",
1987 	[isl_ast_expr_op_lt] = "lt",
1988 	[isl_ast_expr_op_ge] = "ge",
1989 	[isl_ast_expr_op_gt] = "gt",
1990 	[isl_ast_expr_op_call] = "call",
1991 	[isl_ast_expr_op_access] = "access",
1992 	[isl_ast_expr_op_member] = "member",
1993 	[isl_ast_expr_op_address_of] = "address_of"
1994 };
1995 
1996 static __isl_give isl_printer *print_ast_expr_isl(__isl_take isl_printer *p,
1997 	__isl_keep isl_ast_expr *expr);
1998 
1999 /* Print the arguments of "expr" to "p" in isl format.
2000  *
2001  * If there are no arguments, then nothing needs to be printed.
2002  * Otherwise add an "args" key to the current mapping with as value
2003  * the list of arguments of "expr".
2004  */
print_arguments(__isl_take isl_printer * p,__isl_keep isl_ast_expr * expr)2005 static __isl_give isl_printer *print_arguments(__isl_take isl_printer *p,
2006 	__isl_keep isl_ast_expr *expr)
2007 {
2008 	int i;
2009 	isl_size n;
2010 
2011 	n = isl_ast_expr_get_op_n_arg(expr);
2012 	if (n < 0)
2013 		return isl_printer_free(p);
2014 	if (n == 0)
2015 		return p;
2016 
2017 	p = isl_printer_print_str(p, "args");
2018 	p = isl_printer_yaml_next(p);
2019 	p = isl_printer_yaml_start_sequence(p);
2020 	for (i = 0; i < n; ++i) {
2021 		isl_ast_expr *arg;
2022 
2023 		arg = isl_ast_expr_get_op_arg(expr, i);
2024 		p = print_ast_expr_isl(p, arg);
2025 		isl_ast_expr_free(arg);
2026 		p = isl_printer_yaml_next(p);
2027 	}
2028 	p = isl_printer_yaml_end_sequence(p);
2029 
2030 	return p;
2031 }
2032 
2033 /* Print "expr" to "p" in isl format.
2034  *
2035  * In particular, print the isl_ast_expr as a YAML document.
2036  */
print_ast_expr_isl(__isl_take isl_printer * p,__isl_keep isl_ast_expr * expr)2037 static __isl_give isl_printer *print_ast_expr_isl(__isl_take isl_printer *p,
2038 	__isl_keep isl_ast_expr *expr)
2039 {
2040 	enum isl_ast_expr_type type;
2041 	enum isl_ast_expr_op_type op;
2042 	isl_id *id;
2043 	isl_val *v;
2044 
2045 	if (!expr)
2046 		return isl_printer_free(p);
2047 
2048 	p = isl_printer_yaml_start_mapping(p);
2049 	type = isl_ast_expr_get_type(expr);
2050 	switch (type) {
2051 	case isl_ast_expr_error:
2052 		return isl_printer_free(p);
2053 	case isl_ast_expr_op:
2054 		op = isl_ast_expr_get_op_type(expr);
2055 		if (op == isl_ast_expr_op_error)
2056 			return isl_printer_free(p);
2057 		p = isl_printer_print_str(p, "op");
2058 		p = isl_printer_yaml_next(p);
2059 		p = isl_printer_print_str(p, op_str[op]);
2060 		p = isl_printer_yaml_next(p);
2061 		p = print_arguments(p, expr);
2062 		break;
2063 	case isl_ast_expr_id:
2064 		p = isl_printer_print_str(p, "id");
2065 		p = isl_printer_yaml_next(p);
2066 		id = isl_ast_expr_get_id(expr);
2067 		p = isl_printer_print_id(p, id);
2068 		isl_id_free(id);
2069 		break;
2070 	case isl_ast_expr_int:
2071 		p = isl_printer_print_str(p, "val");
2072 		p = isl_printer_yaml_next(p);
2073 		v = isl_ast_expr_get_val(expr);
2074 		p = isl_printer_print_val(p, v);
2075 		isl_val_free(v);
2076 		break;
2077 	}
2078 	p = isl_printer_yaml_end_mapping(p);
2079 
2080 	return p;
2081 }
2082 
2083 /* Print "expr" to "p".
2084  *
2085  * Only an isl and a C format are supported.
2086  */
isl_printer_print_ast_expr(__isl_take isl_printer * p,__isl_keep isl_ast_expr * expr)2087 __isl_give isl_printer *isl_printer_print_ast_expr(__isl_take isl_printer *p,
2088 	__isl_keep isl_ast_expr *expr)
2089 {
2090 	int format;
2091 
2092 	if (!p)
2093 		return NULL;
2094 
2095 	format = isl_printer_get_output_format(p);
2096 	switch (format) {
2097 	case ISL_FORMAT_ISL:
2098 		p = print_ast_expr_isl(p, expr);
2099 		break;
2100 	case ISL_FORMAT_C:
2101 		p = print_ast_expr_c(p, expr);
2102 		break;
2103 	default:
2104 		isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2105 			"output format not supported for ast_expr",
2106 			return isl_printer_free(p));
2107 	}
2108 
2109 	return p;
2110 }
2111 
2112 static __isl_give isl_printer *print_ast_node_isl(__isl_take isl_printer *p,
2113 	__isl_keep isl_ast_node *node);
2114 
2115 /* Print a YAML sequence containing the entries in "list" to "p".
2116  */
print_ast_node_list(__isl_take isl_printer * p,__isl_keep isl_ast_node_list * list)2117 static __isl_give isl_printer *print_ast_node_list(__isl_take isl_printer *p,
2118 	__isl_keep isl_ast_node_list *list)
2119 {
2120 	int i;
2121 	isl_size n;
2122 
2123 	n = isl_ast_node_list_n_ast_node(list);
2124 	if (n < 0)
2125 		return isl_printer_free(p);
2126 
2127 	p = isl_printer_yaml_start_sequence(p);
2128 	for (i = 0; i < n; ++i) {
2129 		isl_ast_node *node;
2130 
2131 		node = isl_ast_node_list_get_ast_node(list, i);
2132 		p = print_ast_node_isl(p, node);
2133 		isl_ast_node_free(node);
2134 		p = isl_printer_yaml_next(p);
2135 	}
2136 	p = isl_printer_yaml_end_sequence(p);
2137 
2138 	return p;
2139 }
2140 
2141 /* Print "node" to "p" in "isl format".
2142  *
2143  * In particular, print the isl_ast_node as a YAML document.
2144  */
print_ast_node_isl(__isl_take isl_printer * p,__isl_keep isl_ast_node * node)2145 static __isl_give isl_printer *print_ast_node_isl(__isl_take isl_printer *p,
2146 	__isl_keep isl_ast_node *node)
2147 {
2148 	switch (node->type) {
2149 	case isl_ast_node_for:
2150 		p = isl_printer_yaml_start_mapping(p);
2151 		p = isl_printer_print_str(p, "iterator");
2152 		p = isl_printer_yaml_next(p);
2153 		p = isl_printer_print_ast_expr(p, node->u.f.iterator);
2154 		p = isl_printer_yaml_next(p);
2155 		if (node->u.f.degenerate) {
2156 			p = isl_printer_print_str(p, "value");
2157 			p = isl_printer_yaml_next(p);
2158 			p = isl_printer_print_ast_expr(p, node->u.f.init);
2159 			p = isl_printer_yaml_next(p);
2160 		} else {
2161 			p = isl_printer_print_str(p, "init");
2162 			p = isl_printer_yaml_next(p);
2163 			p = isl_printer_print_ast_expr(p, node->u.f.init);
2164 			p = isl_printer_yaml_next(p);
2165 			p = isl_printer_print_str(p, "cond");
2166 			p = isl_printer_yaml_next(p);
2167 			p = isl_printer_print_ast_expr(p, node->u.f.cond);
2168 			p = isl_printer_yaml_next(p);
2169 			p = isl_printer_print_str(p, "inc");
2170 			p = isl_printer_yaml_next(p);
2171 			p = isl_printer_print_ast_expr(p, node->u.f.inc);
2172 			p = isl_printer_yaml_next(p);
2173 		}
2174 		if (node->u.f.body) {
2175 			p = isl_printer_print_str(p, "body");
2176 			p = isl_printer_yaml_next(p);
2177 			p = isl_printer_print_ast_node(p, node->u.f.body);
2178 			p = isl_printer_yaml_next(p);
2179 		}
2180 		p = isl_printer_yaml_end_mapping(p);
2181 		break;
2182 	case isl_ast_node_mark:
2183 		p = isl_printer_yaml_start_mapping(p);
2184 		p = isl_printer_print_str(p, "mark");
2185 		p = isl_printer_yaml_next(p);
2186 		p = isl_printer_print_id(p, node->u.m.mark);
2187 		p = isl_printer_yaml_next(p);
2188 		p = isl_printer_print_str(p, "node");
2189 		p = isl_printer_yaml_next(p);
2190 		p = isl_printer_print_ast_node(p, node->u.m.node);
2191 		p = isl_printer_yaml_end_mapping(p);
2192 		break;
2193 	case isl_ast_node_user:
2194 		p = isl_printer_yaml_start_mapping(p);
2195 		p = isl_printer_print_str(p, "user");
2196 		p = isl_printer_yaml_next(p);
2197 		p = isl_printer_print_ast_expr(p, node->u.e.expr);
2198 		p = isl_printer_yaml_end_mapping(p);
2199 		break;
2200 	case isl_ast_node_if:
2201 		p = isl_printer_yaml_start_mapping(p);
2202 		p = isl_printer_print_str(p, "guard");
2203 		p = isl_printer_yaml_next(p);
2204 		p = isl_printer_print_ast_expr(p, node->u.i.guard);
2205 		p = isl_printer_yaml_next(p);
2206 		if (node->u.i.then) {
2207 			p = isl_printer_print_str(p, "then");
2208 			p = isl_printer_yaml_next(p);
2209 			p = isl_printer_print_ast_node(p, node->u.i.then);
2210 			p = isl_printer_yaml_next(p);
2211 		}
2212 		if (node->u.i.else_node) {
2213 			p = isl_printer_print_str(p, "else");
2214 			p = isl_printer_yaml_next(p);
2215 			p = isl_printer_print_ast_node(p, node->u.i.else_node);
2216 		}
2217 		p = isl_printer_yaml_end_mapping(p);
2218 		break;
2219 	case isl_ast_node_block:
2220 		p = print_ast_node_list(p, node->u.b.children);
2221 		break;
2222 	case isl_ast_node_error:
2223 		break;
2224 	}
2225 	return p;
2226 }
2227 
2228 /* Do we need to print a block around the body "node" of a for or if node?
2229  *
2230  * If the node is a block, then we need to print a block.
2231  * Also if the node is a degenerate for then we will print it as
2232  * an assignment followed by the body of the for loop, so we need a block
2233  * as well.
2234  * If the node is an if node with an else, then we print a block
2235  * to avoid spurious dangling else warnings emitted by some compilers.
2236  * If the node is a mark, then in principle, we would have to check
2237  * the child of the mark node.  However, even if the child would not
2238  * require us to print a block, for readability it is probably best
2239  * to print a block anyway.
2240  * If the ast_always_print_block option has been set, then we print a block.
2241  */
need_block(__isl_keep isl_ast_node * node)2242 static int need_block(__isl_keep isl_ast_node *node)
2243 {
2244 	isl_ctx *ctx;
2245 
2246 	if (node->type == isl_ast_node_block)
2247 		return 1;
2248 	if (node->type == isl_ast_node_for && node->u.f.degenerate)
2249 		return 1;
2250 	if (node->type == isl_ast_node_if && node->u.i.else_node)
2251 		return 1;
2252 	if (node->type == isl_ast_node_mark)
2253 		return 1;
2254 
2255 	ctx = isl_ast_node_get_ctx(node);
2256 	return isl_options_get_ast_always_print_block(ctx);
2257 }
2258 
2259 static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
2260 	__isl_keep isl_ast_node *node,
2261 	__isl_keep isl_ast_print_options *options, int in_block, int in_list);
2262 static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
2263 	__isl_keep isl_ast_node *node,
2264 	__isl_keep isl_ast_print_options *options, int new_line,
2265 	int force_block);
2266 
2267 /* Print the body "node" of a for or if node.
2268  * If "else_node" is set, then it is printed as well.
2269  * If "force_block" is set, then print out the body as a block.
2270  *
2271  * We first check if we need to print out a block.
2272  * We always print out a block if there is an else node to make
2273  * sure that the else node is matched to the correct if node.
2274  * For consistency, the corresponding else node is also printed as a block.
2275  *
2276  * If the else node is itself an if, then we print it as
2277  *
2278  *	} else if (..) {
2279  *	}
2280  *
2281  * Otherwise the else node is printed as
2282  *
2283  *	} else {
2284  *	  node
2285  *	}
2286  */
print_body_c(__isl_take isl_printer * p,__isl_keep isl_ast_node * node,__isl_keep isl_ast_node * else_node,__isl_keep isl_ast_print_options * options,int force_block)2287 static __isl_give isl_printer *print_body_c(__isl_take isl_printer *p,
2288 	__isl_keep isl_ast_node *node, __isl_keep isl_ast_node *else_node,
2289 	__isl_keep isl_ast_print_options *options, int force_block)
2290 {
2291 	if (!node)
2292 		return isl_printer_free(p);
2293 
2294 	if (!force_block && !else_node && !need_block(node)) {
2295 		p = isl_printer_end_line(p);
2296 		p = isl_printer_indent(p, 2);
2297 		p = isl_ast_node_print(node, p,
2298 					isl_ast_print_options_copy(options));
2299 		p = isl_printer_indent(p, -2);
2300 		return p;
2301 	}
2302 
2303 	p = isl_printer_print_str(p, " {");
2304 	p = isl_printer_end_line(p);
2305 	p = isl_printer_indent(p, 2);
2306 	p = print_ast_node_c(p, node, options, 1, 0);
2307 	p = isl_printer_indent(p, -2);
2308 	p = isl_printer_start_line(p);
2309 	p = isl_printer_print_str(p, "}");
2310 	if (else_node) {
2311 		if (else_node->type == isl_ast_node_if) {
2312 			p = isl_printer_print_str(p, " else ");
2313 			p = print_if_c(p, else_node, options, 0, 1);
2314 		} else {
2315 			p = isl_printer_print_str(p, " else");
2316 			p = print_body_c(p, else_node, NULL, options, 1);
2317 		}
2318 	} else
2319 		p = isl_printer_end_line(p);
2320 
2321 	return p;
2322 }
2323 
2324 /* Print the start of a compound statement.
2325  */
start_block(__isl_take isl_printer * p)2326 static __isl_give isl_printer *start_block(__isl_take isl_printer *p)
2327 {
2328 	p = isl_printer_start_line(p);
2329 	p = isl_printer_print_str(p, "{");
2330 	p = isl_printer_end_line(p);
2331 	p = isl_printer_indent(p, 2);
2332 
2333 	return p;
2334 }
2335 
2336 /* Print the end of a compound statement.
2337  */
end_block(__isl_take isl_printer * p)2338 static __isl_give isl_printer *end_block(__isl_take isl_printer *p)
2339 {
2340 	p = isl_printer_indent(p, -2);
2341 	p = isl_printer_start_line(p);
2342 	p = isl_printer_print_str(p, "}");
2343 	p = isl_printer_end_line(p);
2344 
2345 	return p;
2346 }
2347 
2348 /* Print the for node "node".
2349  *
2350  * If the for node is degenerate, it is printed as
2351  *
2352  *	type iterator = init;
2353  *	body
2354  *
2355  * Otherwise, it is printed as
2356  *
2357  *	for (type iterator = init; cond; iterator += inc)
2358  *		body
2359  *
2360  * "in_block" is set if we are currently inside a block.
2361  * "in_list" is set if the current node is not alone in the block.
2362  * If we are not in a block or if the current not is not alone in the block
2363  * then we print a block around a degenerate for loop such that the variable
2364  * declaration will not conflict with any potential other declaration
2365  * of the same variable.
2366  */
print_for_c(__isl_take isl_printer * p,__isl_keep isl_ast_node * node,__isl_keep isl_ast_print_options * options,int in_block,int in_list)2367 static __isl_give isl_printer *print_for_c(__isl_take isl_printer *p,
2368 	__isl_keep isl_ast_node *node,
2369 	__isl_keep isl_ast_print_options *options, int in_block, int in_list)
2370 {
2371 	isl_id *id;
2372 	const char *name;
2373 	const char *type;
2374 
2375 	type = isl_options_get_ast_iterator_type(isl_printer_get_ctx(p));
2376 	if (!node->u.f.degenerate) {
2377 		id = isl_ast_expr_get_id(node->u.f.iterator);
2378 		name = isl_id_get_name(id);
2379 		isl_id_free(id);
2380 		p = isl_printer_start_line(p);
2381 		p = isl_printer_print_str(p, "for (");
2382 		p = isl_printer_print_str(p, type);
2383 		p = isl_printer_print_str(p, " ");
2384 		p = isl_printer_print_str(p, name);
2385 		p = isl_printer_print_str(p, " = ");
2386 		p = isl_printer_print_ast_expr(p, node->u.f.init);
2387 		p = isl_printer_print_str(p, "; ");
2388 		p = isl_printer_print_ast_expr(p, node->u.f.cond);
2389 		p = isl_printer_print_str(p, "; ");
2390 		p = isl_printer_print_str(p, name);
2391 		p = isl_printer_print_str(p, " += ");
2392 		p = isl_printer_print_ast_expr(p, node->u.f.inc);
2393 		p = isl_printer_print_str(p, ")");
2394 		p = print_body_c(p, node->u.f.body, NULL, options, 0);
2395 	} else {
2396 		id = isl_ast_expr_get_id(node->u.f.iterator);
2397 		name = isl_id_get_name(id);
2398 		isl_id_free(id);
2399 		if (!in_block || in_list)
2400 			p = start_block(p);
2401 		p = isl_printer_start_line(p);
2402 		p = isl_printer_print_str(p, type);
2403 		p = isl_printer_print_str(p, " ");
2404 		p = isl_printer_print_str(p, name);
2405 		p = isl_printer_print_str(p, " = ");
2406 		p = isl_printer_print_ast_expr(p, node->u.f.init);
2407 		p = isl_printer_print_str(p, ";");
2408 		p = isl_printer_end_line(p);
2409 		p = print_ast_node_c(p, node->u.f.body, options, 1, 0);
2410 		if (!in_block || in_list)
2411 			p = end_block(p);
2412 	}
2413 
2414 	return p;
2415 }
2416 
2417 /* Print the if node "node".
2418  * If "new_line" is set then the if node should be printed on a new line.
2419  * If "force_block" is set, then print out the body as a block.
2420  */
print_if_c(__isl_take isl_printer * p,__isl_keep isl_ast_node * node,__isl_keep isl_ast_print_options * options,int new_line,int force_block)2421 static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
2422 	__isl_keep isl_ast_node *node,
2423 	__isl_keep isl_ast_print_options *options, int new_line,
2424 	int force_block)
2425 {
2426 	if (new_line)
2427 		p = isl_printer_start_line(p);
2428 	p = isl_printer_print_str(p, "if (");
2429 	p = isl_printer_print_ast_expr(p, node->u.i.guard);
2430 	p = isl_printer_print_str(p, ")");
2431 	p = print_body_c(p, node->u.i.then, node->u.i.else_node, options,
2432 			force_block);
2433 
2434 	return p;
2435 }
2436 
2437 /* Print the "node" to "p".
2438  *
2439  * "in_block" is set if we are currently inside a block.
2440  * If so, we do not print a block around the children of a block node.
2441  * We do this to avoid an extra block around the body of a degenerate
2442  * for node.
2443  *
2444  * "in_list" is set if the current node is not alone in the block.
2445  */
print_ast_node_c(__isl_take isl_printer * p,__isl_keep isl_ast_node * node,__isl_keep isl_ast_print_options * options,int in_block,int in_list)2446 static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
2447 	__isl_keep isl_ast_node *node,
2448 	__isl_keep isl_ast_print_options *options, int in_block, int in_list)
2449 {
2450 	switch (node->type) {
2451 	case isl_ast_node_for:
2452 		if (options->print_for)
2453 			return options->print_for(p,
2454 					isl_ast_print_options_copy(options),
2455 					node, options->print_for_user);
2456 		p = print_for_c(p, node, options, in_block, in_list);
2457 		break;
2458 	case isl_ast_node_if:
2459 		p = print_if_c(p, node, options, 1, 0);
2460 		break;
2461 	case isl_ast_node_block:
2462 		if (!in_block)
2463 			p = start_block(p);
2464 		p = isl_ast_node_list_print(node->u.b.children, p, options);
2465 		if (!in_block)
2466 			p = end_block(p);
2467 		break;
2468 	case isl_ast_node_mark:
2469 		p = isl_printer_start_line(p);
2470 		p = isl_printer_print_str(p, "// ");
2471 		p = isl_printer_print_str(p, isl_id_get_name(node->u.m.mark));
2472 		p = isl_printer_end_line(p);
2473 		p = print_ast_node_c(p, node->u.m.node, options, 0, in_list);
2474 		break;
2475 	case isl_ast_node_user:
2476 		if (options->print_user)
2477 			return options->print_user(p,
2478 					isl_ast_print_options_copy(options),
2479 					node, options->print_user_user);
2480 		p = isl_printer_start_line(p);
2481 		p = isl_printer_print_ast_expr(p, node->u.e.expr);
2482 		p = isl_printer_print_str(p, ";");
2483 		p = isl_printer_end_line(p);
2484 		break;
2485 	case isl_ast_node_error:
2486 		break;
2487 	}
2488 	return p;
2489 }
2490 
2491 /* Print the for node "node" to "p".
2492  */
isl_ast_node_for_print(__isl_keep isl_ast_node * node,__isl_take isl_printer * p,__isl_take isl_ast_print_options * options)2493 __isl_give isl_printer *isl_ast_node_for_print(__isl_keep isl_ast_node *node,
2494 	__isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
2495 {
2496 	if (!node || !options)
2497 		goto error;
2498 	if (node->type != isl_ast_node_for)
2499 		isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
2500 			"not a for node", goto error);
2501 	p = print_for_c(p, node, options, 0, 0);
2502 	isl_ast_print_options_free(options);
2503 	return p;
2504 error:
2505 	isl_ast_print_options_free(options);
2506 	isl_printer_free(p);
2507 	return NULL;
2508 }
2509 
2510 /* Print the if node "node" to "p".
2511  */
isl_ast_node_if_print(__isl_keep isl_ast_node * node,__isl_take isl_printer * p,__isl_take isl_ast_print_options * options)2512 __isl_give isl_printer *isl_ast_node_if_print(__isl_keep isl_ast_node *node,
2513 	__isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
2514 {
2515 	if (!node || !options)
2516 		goto error;
2517 	if (node->type != isl_ast_node_if)
2518 		isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
2519 			"not an if node", goto error);
2520 	p = print_if_c(p, node, options, 1, 0);
2521 	isl_ast_print_options_free(options);
2522 	return p;
2523 error:
2524 	isl_ast_print_options_free(options);
2525 	isl_printer_free(p);
2526 	return NULL;
2527 }
2528 
2529 /* Print "node" to "p".
2530  *
2531  * "node" is assumed to be either the outermost node in an AST or
2532  * a node that is known not to be a block.
2533  * If "node" is a block (and is therefore outermost) and
2534  * if the ast_print_outermost_block options is not set,
2535  * then act as if the printing occurs inside a block, such
2536  * that no "extra" block will get printed.
2537  */
isl_ast_node_print(__isl_keep isl_ast_node * node,__isl_take isl_printer * p,__isl_take isl_ast_print_options * options)2538 __isl_give isl_printer *isl_ast_node_print(__isl_keep isl_ast_node *node,
2539 	__isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
2540 {
2541 	int in_block = 0;
2542 
2543 	if (!options || !node)
2544 		goto error;
2545 	if (node->type == isl_ast_node_block) {
2546 		isl_ctx *ctx;
2547 
2548 		ctx = isl_ast_node_get_ctx(node);
2549 		in_block = !isl_options_get_ast_print_outermost_block(ctx);
2550 	}
2551 	p = print_ast_node_c(p, node, options, in_block, 0);
2552 	isl_ast_print_options_free(options);
2553 	return p;
2554 error:
2555 	isl_ast_print_options_free(options);
2556 	isl_printer_free(p);
2557 	return NULL;
2558 }
2559 
2560 /* Print "node" to "p".
2561  */
isl_printer_print_ast_node(__isl_take isl_printer * p,__isl_keep isl_ast_node * node)2562 __isl_give isl_printer *isl_printer_print_ast_node(__isl_take isl_printer *p,
2563 	__isl_keep isl_ast_node *node)
2564 {
2565 	int format;
2566 	isl_ast_print_options *options;
2567 
2568 	if (!p)
2569 		return NULL;
2570 
2571 	format = isl_printer_get_output_format(p);
2572 	switch (format) {
2573 	case ISL_FORMAT_ISL:
2574 		p = print_ast_node_isl(p, node);
2575 		break;
2576 	case ISL_FORMAT_C:
2577 		options = isl_ast_print_options_alloc(isl_printer_get_ctx(p));
2578 		p = isl_ast_node_print(node, p, options);
2579 		break;
2580 	default:
2581 		isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2582 			"output format not supported for ast_node",
2583 			return isl_printer_free(p));
2584 	}
2585 
2586 	return p;
2587 }
2588 
2589 /* Print the list of nodes "list" to "p".
2590  */
isl_ast_node_list_print(__isl_keep isl_ast_node_list * list,__isl_take isl_printer * p,__isl_keep isl_ast_print_options * options)2591 __isl_give isl_printer *isl_ast_node_list_print(
2592 	__isl_keep isl_ast_node_list *list, __isl_take isl_printer *p,
2593 	__isl_keep isl_ast_print_options *options)
2594 {
2595 	int i;
2596 
2597 	if (!p || !list || !options)
2598 		return isl_printer_free(p);
2599 
2600 	for (i = 0; i < list->n; ++i)
2601 		p = print_ast_node_c(p, list->p[i], options, 1, 1);
2602 
2603 	return p;
2604 }
2605 
2606 #define ISL_AST_MACRO_FDIV_Q	(1 << 0)
2607 #define ISL_AST_MACRO_MIN	(1 << 1)
2608 #define ISL_AST_MACRO_MAX	(1 << 2)
2609 #define ISL_AST_MACRO_ALL	(ISL_AST_MACRO_FDIV_Q | \
2610 				 ISL_AST_MACRO_MIN | \
2611 				 ISL_AST_MACRO_MAX)
2612 
2613 /* If "expr" contains an isl_ast_expr_op_min, isl_ast_expr_op_max or
2614  * isl_ast_expr_op_fdiv_q then set the corresponding bit in "macros".
2615  */
ast_expr_required_macros(__isl_keep isl_ast_expr * expr,int macros)2616 static int ast_expr_required_macros(__isl_keep isl_ast_expr *expr, int macros)
2617 {
2618 	int i;
2619 
2620 	if (macros == ISL_AST_MACRO_ALL)
2621 		return macros;
2622 
2623 	if (expr->type != isl_ast_expr_op)
2624 		return macros;
2625 
2626 	if (expr->u.op.op == isl_ast_expr_op_min)
2627 		macros |= ISL_AST_MACRO_MIN;
2628 	if (expr->u.op.op == isl_ast_expr_op_max)
2629 		macros |= ISL_AST_MACRO_MAX;
2630 	if (expr->u.op.op == isl_ast_expr_op_fdiv_q)
2631 		macros |= ISL_AST_MACRO_FDIV_Q;
2632 
2633 	for (i = 0; i < expr->u.op.n_arg; ++i)
2634 		macros = ast_expr_required_macros(expr->u.op.args[i], macros);
2635 
2636 	return macros;
2637 }
2638 
2639 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
2640 	int macros);
2641 
2642 /* If "node" contains an isl_ast_expr_op_min, isl_ast_expr_op_max or
2643  * isl_ast_expr_op_fdiv_q then set the corresponding bit in "macros".
2644  */
ast_node_required_macros(__isl_keep isl_ast_node * node,int macros)2645 static int ast_node_required_macros(__isl_keep isl_ast_node *node, int macros)
2646 {
2647 	if (macros == ISL_AST_MACRO_ALL)
2648 		return macros;
2649 
2650 	switch (node->type) {
2651 	case isl_ast_node_for:
2652 		macros = ast_expr_required_macros(node->u.f.init, macros);
2653 		if (!node->u.f.degenerate) {
2654 			macros = ast_expr_required_macros(node->u.f.cond,
2655 								macros);
2656 			macros = ast_expr_required_macros(node->u.f.inc,
2657 								macros);
2658 		}
2659 		macros = ast_node_required_macros(node->u.f.body, macros);
2660 		break;
2661 	case isl_ast_node_if:
2662 		macros = ast_expr_required_macros(node->u.i.guard, macros);
2663 		macros = ast_node_required_macros(node->u.i.then, macros);
2664 		if (node->u.i.else_node)
2665 			macros = ast_node_required_macros(node->u.i.else_node,
2666 								macros);
2667 		break;
2668 	case isl_ast_node_block:
2669 		macros = ast_node_list_required_macros(node->u.b.children,
2670 							macros);
2671 		break;
2672 	case isl_ast_node_mark:
2673 		macros = ast_node_required_macros(node->u.m.node, macros);
2674 		break;
2675 	case isl_ast_node_user:
2676 		macros = ast_expr_required_macros(node->u.e.expr, macros);
2677 		break;
2678 	case isl_ast_node_error:
2679 		break;
2680 	}
2681 
2682 	return macros;
2683 }
2684 
2685 /* If "list" contains an isl_ast_expr_op_min, isl_ast_expr_op_max or
2686  * isl_ast_expr_op_fdiv_q then set the corresponding bit in "macros".
2687  */
ast_node_list_required_macros(__isl_keep isl_ast_node_list * list,int macros)2688 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
2689 	int macros)
2690 {
2691 	int i;
2692 
2693 	for (i = 0; i < list->n; ++i)
2694 		macros = ast_node_required_macros(list->p[i], macros);
2695 
2696 	return macros;
2697 }
2698 
2699 /* Data structure for keeping track of whether a macro definition
2700  * for a given type has already been printed.
2701  * The value is zero if no definition has been printed and non-zero otherwise.
2702  */
2703 struct isl_ast_expr_op_printed {
2704 	char printed[isl_ast_expr_op_last + 1];
2705 };
2706 
2707 /* Create an empty struct isl_ast_expr_op_printed.
2708  */
create_printed(isl_ctx * ctx)2709 static void *create_printed(isl_ctx *ctx)
2710 {
2711 	return isl_calloc_type(ctx, struct isl_ast_expr_op_printed);
2712 }
2713 
2714 /* Free a struct isl_ast_expr_op_printed.
2715  */
free_printed(void * user)2716 static void free_printed(void *user)
2717 {
2718 	free(user);
2719 }
2720 
2721 /* Ensure that "p" has an isl_ast_expr_op_printed note identified by "id".
2722  */
alloc_printed(__isl_take isl_printer * p,__isl_keep isl_id * id)2723 static __isl_give isl_printer *alloc_printed(__isl_take isl_printer *p,
2724 	__isl_keep isl_id *id)
2725 {
2726 	return alloc_note(p, id, &create_printed, &free_printed);
2727 }
2728 
2729 /* Create an identifier that is used to store
2730  * an isl_ast_expr_op_printed note.
2731  */
printed_id(isl_ctx * ctx)2732 static __isl_give isl_id *printed_id(isl_ctx *ctx)
2733 {
2734 	return isl_id_alloc(ctx, "isl_ast_expr_op_type_printed", NULL);
2735 }
2736 
2737 /* Did the user specify that a macro definition should only be
2738  * printed once and has a macro definition for "type" already
2739  * been printed to "p"?
2740  * If definitions should only be printed once, but a definition
2741  * for "p" has not yet been printed, then mark it as having been
2742  * printed so that it will not printed again.
2743  * The actual printing is taken care of by the caller.
2744  */
already_printed_once(__isl_keep isl_printer * p,enum isl_ast_expr_op_type type)2745 static isl_bool already_printed_once(__isl_keep isl_printer *p,
2746 	enum isl_ast_expr_op_type type)
2747 {
2748 	isl_ctx *ctx;
2749 	isl_id *id;
2750 	struct isl_ast_expr_op_printed *printed;
2751 
2752 	if (!p)
2753 		return isl_bool_error;
2754 
2755 	ctx = isl_printer_get_ctx(p);
2756 	if (!isl_options_get_ast_print_macro_once(ctx))
2757 		return isl_bool_false;
2758 
2759 	if (type > isl_ast_expr_op_last)
2760 		isl_die(isl_printer_get_ctx(p), isl_error_invalid,
2761 			"invalid type", return isl_bool_error);
2762 
2763 	id = printed_id(isl_printer_get_ctx(p));
2764 	p = alloc_printed(p, id);
2765 	printed = get_note(p, id);
2766 	isl_id_free(id);
2767 	if (!printed)
2768 		return isl_bool_error;
2769 
2770 	if (printed->printed[type])
2771 		return isl_bool_true;
2772 
2773 	printed->printed[type] = 1;
2774 	return isl_bool_false;
2775 }
2776 
2777 /* Print a macro definition for the operator "type".
2778  *
2779  * If the user has specified that a macro definition should
2780  * only be printed once to any given printer and if the macro definition
2781  * has already been printed to "p", then do not print the definition.
2782  */
isl_ast_expr_op_type_print_macro(enum isl_ast_expr_op_type type,__isl_take isl_printer * p)2783 __isl_give isl_printer *isl_ast_expr_op_type_print_macro(
2784 	enum isl_ast_expr_op_type type, __isl_take isl_printer *p)
2785 {
2786 	isl_bool skip;
2787 
2788 	skip = already_printed_once(p, type);
2789 	if (skip < 0)
2790 		return isl_printer_free(p);
2791 	if (skip)
2792 		return p;
2793 
2794 	switch (type) {
2795 	case isl_ast_expr_op_min:
2796 		p = isl_printer_start_line(p);
2797 		p = isl_printer_print_str(p, "#define ");
2798 		p = isl_printer_print_str(p, get_op_str_c(p, type));
2799 		p = isl_printer_print_str(p,
2800 			"(x,y)    ((x) < (y) ? (x) : (y))");
2801 		p = isl_printer_end_line(p);
2802 		break;
2803 	case isl_ast_expr_op_max:
2804 		p = isl_printer_start_line(p);
2805 		p = isl_printer_print_str(p, "#define ");
2806 		p = isl_printer_print_str(p, get_op_str_c(p, type));
2807 		p = isl_printer_print_str(p,
2808 			"(x,y)    ((x) > (y) ? (x) : (y))");
2809 		p = isl_printer_end_line(p);
2810 		break;
2811 	case isl_ast_expr_op_fdiv_q:
2812 		p = isl_printer_start_line(p);
2813 		p = isl_printer_print_str(p, "#define ");
2814 		p = isl_printer_print_str(p, get_op_str_c(p, type));
2815 		p = isl_printer_print_str(p,
2816 			"(n,d) "
2817 			"(((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))");
2818 		p = isl_printer_end_line(p);
2819 		break;
2820 	default:
2821 		break;
2822 	}
2823 
2824 	return p;
2825 }
2826 
2827 /* This is an alternative name for the function above.
2828  */
isl_ast_op_type_print_macro(enum isl_ast_expr_op_type type,__isl_take isl_printer * p)2829 __isl_give isl_printer *isl_ast_op_type_print_macro(
2830 	enum isl_ast_expr_op_type type, __isl_take isl_printer *p)
2831 {
2832 	return isl_ast_expr_op_type_print_macro(type, p);
2833 }
2834 
2835 /* Call "fn" for each type of operation represented in the "macros"
2836  * bit vector.
2837  */
foreach_ast_expr_op_type(int macros,isl_stat (* fn)(enum isl_ast_expr_op_type type,void * user),void * user)2838 static isl_stat foreach_ast_expr_op_type(int macros,
2839 	isl_stat (*fn)(enum isl_ast_expr_op_type type, void *user), void *user)
2840 {
2841 	if (macros & ISL_AST_MACRO_MIN && fn(isl_ast_expr_op_min, user) < 0)
2842 		return isl_stat_error;
2843 	if (macros & ISL_AST_MACRO_MAX && fn(isl_ast_expr_op_max, user) < 0)
2844 		return isl_stat_error;
2845 	if (macros & ISL_AST_MACRO_FDIV_Q &&
2846 	    fn(isl_ast_expr_op_fdiv_q, user) < 0)
2847 		return isl_stat_error;
2848 
2849 	return isl_stat_ok;
2850 }
2851 
2852 /* Call "fn" for each type of operation that appears in "expr"
2853  * and that requires a macro definition.
2854  */
isl_ast_expr_foreach_ast_expr_op_type(__isl_keep isl_ast_expr * expr,isl_stat (* fn)(enum isl_ast_expr_op_type type,void * user),void * user)2855 isl_stat isl_ast_expr_foreach_ast_expr_op_type(__isl_keep isl_ast_expr *expr,
2856 	isl_stat (*fn)(enum isl_ast_expr_op_type type, void *user), void *user)
2857 {
2858 	int macros;
2859 
2860 	if (!expr)
2861 		return isl_stat_error;
2862 
2863 	macros = ast_expr_required_macros(expr, 0);
2864 	return foreach_ast_expr_op_type(macros, fn, user);
2865 }
2866 
2867 /* This is an alternative name for the function above.
2868  */
isl_ast_expr_foreach_ast_op_type(__isl_keep isl_ast_expr * expr,isl_stat (* fn)(enum isl_ast_expr_op_type type,void * user),void * user)2869 isl_stat isl_ast_expr_foreach_ast_op_type(__isl_keep isl_ast_expr *expr,
2870 	isl_stat (*fn)(enum isl_ast_expr_op_type type, void *user), void *user)
2871 {
2872 	return isl_ast_expr_foreach_ast_expr_op_type(expr, fn, user);
2873 }
2874 
2875 /* Call "fn" for each type of operation that appears in "node"
2876  * and that requires a macro definition.
2877  */
isl_ast_node_foreach_ast_expr_op_type(__isl_keep isl_ast_node * node,isl_stat (* fn)(enum isl_ast_expr_op_type type,void * user),void * user)2878 isl_stat isl_ast_node_foreach_ast_expr_op_type(__isl_keep isl_ast_node *node,
2879 	isl_stat (*fn)(enum isl_ast_expr_op_type type, void *user), void *user)
2880 {
2881 	int macros;
2882 
2883 	if (!node)
2884 		return isl_stat_error;
2885 
2886 	macros = ast_node_required_macros(node, 0);
2887 	return foreach_ast_expr_op_type(macros, fn, user);
2888 }
2889 
2890 /* This is an alternative name for the function above.
2891  */
isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node * node,isl_stat (* fn)(enum isl_ast_expr_op_type type,void * user),void * user)2892 isl_stat isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node *node,
2893 	isl_stat (*fn)(enum isl_ast_expr_op_type type, void *user), void *user)
2894 {
2895 	return isl_ast_node_foreach_ast_expr_op_type(node, fn, user);
2896 }
2897 
ast_op_type_print_macro(enum isl_ast_expr_op_type type,void * user)2898 static isl_stat ast_op_type_print_macro(enum isl_ast_expr_op_type type,
2899 	void *user)
2900 {
2901 	isl_printer **p = user;
2902 
2903 	*p = isl_ast_expr_op_type_print_macro(type, *p);
2904 
2905 	return isl_stat_ok;
2906 }
2907 
2908 /* Print macro definitions for all the macros used in the result
2909  * of printing "expr".
2910  */
isl_ast_expr_print_macros(__isl_keep isl_ast_expr * expr,__isl_take isl_printer * p)2911 __isl_give isl_printer *isl_ast_expr_print_macros(
2912 	__isl_keep isl_ast_expr *expr, __isl_take isl_printer *p)
2913 {
2914 	if (isl_ast_expr_foreach_ast_expr_op_type(expr,
2915 					    &ast_op_type_print_macro, &p) < 0)
2916 		return isl_printer_free(p);
2917 	return p;
2918 }
2919 
2920 /* Print macro definitions for all the macros used in the result
2921  * of printing "node".
2922  */
isl_ast_node_print_macros(__isl_keep isl_ast_node * node,__isl_take isl_printer * p)2923 __isl_give isl_printer *isl_ast_node_print_macros(
2924 	__isl_keep isl_ast_node *node, __isl_take isl_printer *p)
2925 {
2926 	if (isl_ast_node_foreach_ast_expr_op_type(node,
2927 					    &ast_op_type_print_macro, &p) < 0)
2928 		return isl_printer_free(p);
2929 	return p;
2930 }
2931 
2932 /* Return a string containing C code representing this isl_ast_expr.
2933  */
isl_ast_expr_to_C_str(__isl_keep isl_ast_expr * expr)2934 __isl_give char *isl_ast_expr_to_C_str(__isl_keep isl_ast_expr *expr)
2935 {
2936 	isl_printer *p;
2937 	char *str;
2938 
2939 	if (!expr)
2940 		return NULL;
2941 
2942 	p = isl_printer_to_str(isl_ast_expr_get_ctx(expr));
2943 	p = isl_printer_set_output_format(p, ISL_FORMAT_C);
2944 	p = isl_printer_print_ast_expr(p, expr);
2945 
2946 	str = isl_printer_get_str(p);
2947 
2948 	isl_printer_free(p);
2949 
2950 	return str;
2951 }
2952 
2953 /* Return a string containing C code representing this isl_ast_node.
2954  */
isl_ast_node_to_C_str(__isl_keep isl_ast_node * node)2955 __isl_give char *isl_ast_node_to_C_str(__isl_keep isl_ast_node *node)
2956 {
2957 	isl_printer *p;
2958 	char *str;
2959 
2960 	if (!node)
2961 		return NULL;
2962 
2963 	p = isl_printer_to_str(isl_ast_node_get_ctx(node));
2964 	p = isl_printer_set_output_format(p, ISL_FORMAT_C);
2965 	p = isl_printer_print_ast_node(p, node);
2966 
2967 	str = isl_printer_get_str(p);
2968 
2969 	isl_printer_free(p);
2970 
2971 	return str;
2972 }
2973