isl_ast_expr_get_op_n_arg: return isl_size
[isl.git] / isl_output.c
blob58dbd287f2ae8476dbcee0f65b5b95c96a27ddec
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <stdlib.h>
16 #include <string.h>
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
19 #include <isl/set.h>
20 #include <isl_seq.h>
21 #include <isl_polynomial_private.h>
22 #include <isl_printer_private.h>
23 #include <isl_space_private.h>
24 #include <isl_mat_private.h>
25 #include <isl_vec_private.h>
26 #include <isl/union_set.h>
27 #include <isl/union_map.h>
28 #include <isl/constraint.h>
29 #include <isl_local_space_private.h>
30 #include <isl_aff_private.h>
31 #include <isl_val_private.h>
32 #include <isl_constraint_private.h>
33 #include <isl/ast_build.h>
34 #include <isl_sort.h>
35 #include <isl_output_private.h>
37 #include <bset_to_bmap.c>
38 #include <set_to_map.c>
39 #include <uset_to_umap.c>
41 static const char *s_to[2] = { " -> ", " \\to " };
42 static const char *s_and[2] = { " and ", " \\wedge " };
43 static const char *s_or[2] = { " or ", " \\vee " };
44 static const char *s_le[2] = { "<=", "\\le" };
45 static const char *s_ge[2] = { ">=", "\\ge" };
46 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
47 static const char *s_close_set[2] = { " }", " \\,\\}" };
48 static const char *s_open_list[2] = { "[", "(" };
49 static const char *s_close_list[2] = { "]", ")" };
50 static const char *s_such_that[2] = { " : ", " \\mid " };
51 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
52 static const char *s_close_exists[2] = { ")", "" };
53 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
54 static const char *s_mod[2] = { "mod", "\\bmod" };
55 static const char *s_param_prefix[2] = { "p", "p_" };
56 static const char *s_input_prefix[2] = { "i", "i_" };
57 static const char *s_output_prefix[2] = { "o", "o_" };
59 static __isl_give isl_printer *print_constraint_polylib(
60 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
62 int i;
63 isl_size n_in = isl_basic_map_dim(bmap, isl_dim_in);
64 isl_size n_out = isl_basic_map_dim(bmap, isl_dim_out);
65 isl_size nparam = isl_basic_map_dim(bmap, isl_dim_param);
66 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
68 if (n_in < 0 || n_out < 0 || nparam < 0)
69 return isl_printer_free(p);
71 p = isl_printer_start_line(p);
72 p = isl_printer_print_int(p, ineq);
73 for (i = 0; i < n_out; ++i) {
74 p = isl_printer_print_str(p, " ");
75 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
77 for (i = 0; i < n_in; ++i) {
78 p = isl_printer_print_str(p, " ");
79 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
81 for (i = 0; i < bmap->n_div; ++i) {
82 p = isl_printer_print_str(p, " ");
83 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
85 for (i = 0; i < nparam; ++i) {
86 p = isl_printer_print_str(p, " ");
87 p = isl_printer_print_isl_int(p, c[1+i]);
89 p = isl_printer_print_str(p, " ");
90 p = isl_printer_print_isl_int(p, c[0]);
91 p = isl_printer_end_line(p);
92 return p;
95 static __isl_give isl_printer *print_constraints_polylib(
96 struct isl_basic_map *bmap, __isl_take isl_printer *p)
98 int i;
100 p = isl_printer_set_isl_int_width(p, 5);
102 for (i = 0; i < bmap->n_eq; ++i)
103 p = print_constraint_polylib(bmap, 0, i, p);
104 for (i = 0; i < bmap->n_ineq; ++i)
105 p = print_constraint_polylib(bmap, 1, i, p);
107 return p;
110 static __isl_give isl_printer *bset_print_constraints_polylib(
111 struct isl_basic_set *bset, __isl_take isl_printer *p)
113 return print_constraints_polylib(bset_to_bmap(bset), p);
116 static __isl_give isl_printer *isl_basic_map_print_polylib(
117 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
119 isl_size total;
121 total = isl_basic_map_dim(bmap, isl_dim_all);
122 if (total < 0)
123 return isl_printer_free(p);
124 p = isl_printer_start_line(p);
125 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
126 p = isl_printer_print_str(p, " ");
127 p = isl_printer_print_int(p, 1 + total + 1);
128 if (ext) {
129 isl_size n_in = isl_basic_map_dim(bmap, isl_dim_in);
130 isl_size n_out = isl_basic_map_dim(bmap, isl_dim_out);
131 isl_size n_div = isl_basic_map_dim(bmap, isl_dim_div);
132 isl_size nparam = isl_basic_map_dim(bmap, isl_dim_param);
134 if (n_in < 0 || n_out < 0 || n_div < 0 || nparam < 0)
135 return isl_printer_free(p);
137 p = isl_printer_print_str(p, " ");
138 p = isl_printer_print_int(p, n_out);
139 p = isl_printer_print_str(p, " ");
140 p = isl_printer_print_int(p, n_in);
141 p = isl_printer_print_str(p, " ");
142 p = isl_printer_print_int(p, n_div);
143 p = isl_printer_print_str(p, " ");
144 p = isl_printer_print_int(p, nparam);
146 p = isl_printer_end_line(p);
147 return print_constraints_polylib(bmap, p);
150 static __isl_give isl_printer *isl_basic_set_print_polylib(
151 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
153 return isl_basic_map_print_polylib(bset_to_bmap(bset), p, ext);
156 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
157 __isl_take isl_printer *p, int ext)
159 int i;
161 p = isl_printer_start_line(p);
162 p = isl_printer_print_int(p, map->n);
163 p = isl_printer_end_line(p);
164 for (i = 0; i < map->n; ++i) {
165 p = isl_printer_start_line(p);
166 p = isl_printer_end_line(p);
167 p = isl_basic_map_print_polylib(map->p[i], p, ext);
169 return p;
172 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
173 __isl_take isl_printer *p, int ext)
175 return isl_map_print_polylib(set_to_map(set), p, ext);
178 static isl_size count_same_name(__isl_keep isl_space *space,
179 enum isl_dim_type type, unsigned pos, const char *name)
181 enum isl_dim_type t;
182 int p;
183 isl_size s;
184 int count = 0;
186 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
187 s = t == type ? pos : isl_space_dim(space, t);
188 if (s < 0)
189 return isl_size_error;
190 for (p = 0; p < s; ++p) {
191 const char *n = isl_space_get_dim_name(space, t, p);
192 if (n && !strcmp(n, name))
193 count++;
196 return count;
199 /* Print the name of the variable of type "type" and position "pos"
200 * in "space" to "p".
202 static __isl_give isl_printer *print_name(__isl_keep isl_space *space,
203 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
204 int latex)
206 const char *name;
207 char buffer[20];
208 isl_size primes;
210 name = type == isl_dim_div ? NULL
211 : isl_space_get_dim_name(space, type, pos);
213 if (!name) {
214 const char *prefix;
215 if (type == isl_dim_param)
216 prefix = s_param_prefix[latex];
217 else if (type == isl_dim_div)
218 prefix = s_div_prefix[latex];
219 else if (isl_space_is_set(space) || type == isl_dim_in)
220 prefix = s_input_prefix[latex];
221 else
222 prefix = s_output_prefix[latex];
223 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
224 name = buffer;
226 primes = count_same_name(space, name == buffer ? isl_dim_div : type,
227 pos, name);
228 if (primes < 0)
229 return isl_printer_free(p);
230 p = isl_printer_print_str(p, name);
231 while (primes-- > 0)
232 p = isl_printer_print_str(p, "'");
233 return p;
236 static isl_stat pos2type(__isl_keep isl_space *space,
237 enum isl_dim_type *type, unsigned *pos)
239 isl_size n_in = isl_space_dim(space, isl_dim_in);
240 isl_size n_out = isl_space_dim(space, isl_dim_out);
241 isl_size nparam = isl_space_dim(space, isl_dim_param);
243 if (n_in < 0 || n_out < 0 || nparam < 0)
244 return isl_stat_error;
246 if (*pos < 1 + nparam) {
247 *type = isl_dim_param;
248 *pos -= 1;
249 } else if (*pos < 1 + nparam + n_in) {
250 *type = isl_dim_in;
251 *pos -= 1 + nparam;
252 } else if (*pos < 1 + nparam + n_in + n_out) {
253 *type = isl_dim_out;
254 *pos -= 1 + nparam + n_in;
255 } else {
256 *type = isl_dim_div;
257 *pos -= 1 + nparam + n_in + n_out;
260 return isl_stat_ok;
263 /* Can the div expression of the integer division at position "row" of "div"
264 * be printed?
265 * In particular, are the div expressions available and does the selected
266 * variable have a known explicit representation?
267 * Furthermore, the Omega format does not allow any div expressions
268 * to be printed.
270 static isl_bool can_print_div_expr(__isl_keep isl_printer *p,
271 __isl_keep isl_mat *div, int pos)
273 if (p->output_format == ISL_FORMAT_OMEGA)
274 return isl_bool_false;
275 if (!div)
276 return isl_bool_false;
277 return !isl_int_is_zero(div->row[pos][0]);
280 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
281 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
283 static __isl_give isl_printer *print_term(__isl_keep isl_space *space,
284 __isl_keep isl_mat *div,
285 isl_int c, unsigned pos, __isl_take isl_printer *p, int latex)
287 enum isl_dim_type type;
288 int print_div_def;
290 if (!p || !space)
291 return isl_printer_free(p);
293 if (pos == 0)
294 return isl_printer_print_isl_int(p, c);
296 if (pos2type(space, &type, &pos) < 0)
297 return isl_printer_free(p);
298 print_div_def = type == isl_dim_div && can_print_div_expr(p, div, pos);
300 if (isl_int_is_one(c))
302 else if (isl_int_is_negone(c))
303 p = isl_printer_print_str(p, "-");
304 else {
305 p = isl_printer_print_isl_int(p, c);
306 if (p->output_format == ISL_FORMAT_C || print_div_def)
307 p = isl_printer_print_str(p, "*");
309 if (print_div_def)
310 p = print_div(space, div, pos, p);
311 else
312 p = print_name(space, p, type, pos, latex);
313 return p;
316 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *dim,
317 __isl_keep isl_mat *div,
318 __isl_take isl_printer *p, isl_int *c, int len)
320 int i;
321 int first;
323 for (i = 0, first = 1; i < len; ++i) {
324 int flip = 0;
325 if (isl_int_is_zero(c[i]))
326 continue;
327 if (!first) {
328 if (isl_int_is_neg(c[i])) {
329 flip = 1;
330 isl_int_neg(c[i], c[i]);
331 p = isl_printer_print_str(p, " - ");
332 } else
333 p = isl_printer_print_str(p, " + ");
335 first = 0;
336 p = print_term(dim, div, c[i], i, p, 0);
337 if (flip)
338 isl_int_neg(c[i], c[i]);
340 if (first)
341 p = isl_printer_print_str(p, "0");
342 return p;
345 /* Print an affine expression "c"
346 * to "p", with the variable names taken from "space" and
347 * the integer division definitions taken from "div".
349 static __isl_give isl_printer *print_affine(__isl_take isl_printer *p,
350 __isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c)
352 isl_size n_div, total;
353 unsigned len;
355 total = isl_space_dim(space, isl_dim_all);
356 n_div = isl_mat_rows(div);
357 if (total < 0 || n_div < 0)
358 return isl_printer_free(p);
359 len = 1 + total + n_div;
360 return print_affine_of_len(space, div, p, c, len);
363 /* offset is the offset of local_space inside data->type of data->space.
365 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
366 __isl_keep isl_space *local_space, enum isl_dim_type local_type,
367 struct isl_print_space_data *data, int offset)
369 int i;
370 isl_size dim;
372 if (data->space != local_space && local_type == isl_dim_out)
373 offset += local_space->n_in;
375 dim = isl_space_dim(local_space, local_type);
376 if (dim < 0)
377 return isl_printer_free(p);
378 for (i = 0; i < dim; ++i) {
379 if (i)
380 p = isl_printer_print_str(p, ", ");
381 if (data->print_dim)
382 p = data->print_dim(p, data, offset + i);
383 else
384 p = print_name(data->space, p, data->type, offset + i,
385 data->latex);
387 return p;
390 static __isl_give isl_printer *print_var_list(__isl_take isl_printer *p,
391 __isl_keep isl_space *space, enum isl_dim_type type)
393 struct isl_print_space_data data = { .space = space, .type = type };
395 return print_nested_var_list(p, space, type, &data, 0);
398 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
399 __isl_keep isl_space *local_dim,
400 struct isl_print_space_data *data, int offset);
402 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
403 __isl_keep isl_space *local_space, enum isl_dim_type local_type,
404 struct isl_print_space_data *data, int offset)
406 const char *name = NULL;
407 isl_size n = isl_space_dim(local_space, local_type);
409 if (n < 0)
410 return isl_printer_free(p);
411 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
412 name = isl_space_get_tuple_name(local_space, local_type);
413 if (name) {
414 if (data->latex)
415 p = isl_printer_print_str(p, "\\mathrm{");
416 p = isl_printer_print_str(p, name);
417 if (data->latex)
418 p = isl_printer_print_str(p, "}");
421 if (!data->latex || n != 1 || name)
422 p = isl_printer_print_str(p, s_open_list[data->latex]);
423 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
424 local_space->nested[local_type - isl_dim_in]) {
425 if (data->space != local_space && local_type == isl_dim_out)
426 offset += local_space->n_in;
427 p = print_nested_map_dim(p,
428 local_space->nested[local_type - isl_dim_in],
429 data, offset);
430 } else
431 p = print_nested_var_list(p, local_space, local_type, data,
432 offset);
433 if (!data->latex || n != 1 || name)
434 p = isl_printer_print_str(p, s_close_list[data->latex]);
435 return p;
438 static __isl_give isl_printer *print_tuple(__isl_keep isl_space *dim,
439 __isl_take isl_printer *p, enum isl_dim_type type,
440 struct isl_print_space_data *data)
442 data->space = dim;
443 data->type = type;
444 return print_nested_tuple(p, dim, type, data, 0);
447 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
448 __isl_keep isl_space *local_dim,
449 struct isl_print_space_data *data, int offset)
451 p = print_nested_tuple(p, local_dim, isl_dim_in, data, offset);
452 p = isl_printer_print_str(p, s_to[data->latex]);
453 p = print_nested_tuple(p, local_dim, isl_dim_out, data, offset);
455 return p;
458 __isl_give isl_printer *isl_print_space(__isl_keep isl_space *space,
459 __isl_take isl_printer *p, int rational,
460 struct isl_print_space_data *data)
462 if (rational && !data->latex)
463 p = isl_printer_print_str(p, "rat: ");
464 if (isl_space_is_params(space))
466 else if (isl_space_is_set(space))
467 p = print_tuple(space, p, isl_dim_set, data);
468 else {
469 p = print_tuple(space, p, isl_dim_in, data);
470 p = isl_printer_print_str(p, s_to[data->latex]);
471 p = print_tuple(space, p, isl_dim_out, data);
474 return p;
477 static __isl_give isl_printer *print_omega_parameters(
478 __isl_keep isl_space *space, __isl_take isl_printer *p)
480 isl_size nparam = isl_space_dim(space, isl_dim_param);
482 if (nparam < 0)
483 return isl_printer_free(p);
484 if (nparam == 0)
485 return p;
487 p = isl_printer_start_line(p);
488 p = isl_printer_print_str(p, "symbolic ");
489 p = print_var_list(p, space, isl_dim_param);
490 p = isl_printer_print_str(p, ";");
491 p = isl_printer_end_line(p);
492 return p;
495 /* Does the inequality constraint following "i" in "bmap"
496 * have an opposite value for the same last coefficient?
497 * "last" is the position of the last coefficient of inequality "i".
498 * If the next constraint is a div constraint, then it is ignored
499 * since div constraints are not printed.
501 static isl_bool next_is_opposite(__isl_keep isl_basic_map *bmap, int i,
502 int last)
504 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
505 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
507 if (total < 0)
508 return isl_bool_error;
509 if (i + 1 >= bmap->n_ineq)
510 return isl_bool_false;
511 if (isl_seq_last_non_zero(bmap->ineq[i + 1], 1 + total) != last)
512 return isl_bool_false;
513 if (last >= o_div) {
514 isl_bool is_div;
515 is_div = isl_basic_map_is_div_constraint(bmap,
516 bmap->ineq[i + 1], last - o_div);
517 if (is_div < 0)
518 return isl_bool_error;
519 if (is_div)
520 return isl_bool_false;
522 return isl_int_abs_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]) &&
523 !isl_int_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]);
526 /* Return a string representation of the operator used when
527 * printing a constraint where the LHS is greater than or equal to the LHS
528 * (sign > 0) or smaller than or equal to the LHS (sign < 0).
529 * If "strict" is set, then return the strict version of the comparison
530 * operator.
532 static const char *constraint_op(int sign, int strict, int latex)
534 if (strict)
535 return sign < 0 ? "<" : ">";
536 if (sign < 0)
537 return s_le[latex];
538 else
539 return s_ge[latex];
542 /* Print one side of a constraint "c" to "p", with
543 * the variable names taken from "space" and the integer division definitions
544 * taken from "div".
545 * "last" is the position of the last non-zero coefficient.
546 * Let c' be the result of zeroing out this coefficient, then
547 * the partial constraint
549 * c' op
551 * is printed.
553 static __isl_give isl_printer *print_half_constraint(__isl_take isl_printer *p,
554 __isl_keep isl_space *space, __isl_keep isl_mat *div,
555 isl_int *c, int last, const char *op, int latex)
557 isl_int_set_si(c[last], 0);
558 p = print_affine(p, space, div, c);
560 p = isl_printer_print_str(p, " ");
561 p = isl_printer_print_str(p, op);
562 p = isl_printer_print_str(p, " ");
564 return p;
567 /* Print a constraint "c" to "p", with the variable names
568 * taken from "space" and the integer division definitions taken from "div".
569 * "last" is the position of the last non-zero coefficient, which is
570 * moreover assumed to be negative.
571 * Let c' be the result of zeroing out this coefficient, then
572 * the constraint is printed in the form
574 * -c[last] op c'
576 static __isl_give isl_printer *print_constraint(__isl_take isl_printer *p,
577 __isl_keep isl_space *space, __isl_keep isl_mat *div,
578 isl_int *c, int last, const char *op, int latex)
580 isl_int_abs(c[last], c[last]);
582 p = print_term(space, div, c[last], last, p, latex);
584 p = isl_printer_print_str(p, " ");
585 p = isl_printer_print_str(p, op);
586 p = isl_printer_print_str(p, " ");
588 isl_int_set_si(c[last], 0);
589 p = print_affine(p, space, div, c);
591 return p;
594 /* Given an integer division
596 * floor(f/m)
598 * at position "pos" in "div", print the corresponding modulo expression
600 * (f) mod m
602 * to "p". The variable names are taken from "space", while any
603 * nested integer division definitions are taken from "div".
605 static __isl_give isl_printer *print_mod(__isl_take isl_printer *p,
606 __isl_keep isl_space *space, __isl_keep isl_mat *div, int pos,
607 int latex)
609 if (!p || !div)
610 return isl_printer_free(p);
612 p = isl_printer_print_str(p, "(");
613 p = print_affine_of_len(space, div, p,
614 div->row[pos] + 1, div->n_col - 1);
615 p = isl_printer_print_str(p, ") ");
616 p = isl_printer_print_str(p, s_mod[latex]);
617 p = isl_printer_print_str(p, " ");
618 p = isl_printer_print_isl_int(p, div->row[pos][0]);
619 return p;
622 /* Can the equality constraints "c" be printed as a modulo constraint?
623 * In particular, is of the form
625 * f - a m floor(g/m) = 0,
627 * with c = -a m the coefficient at position "pos"?
628 * Return the position of the corresponding integer division if so.
629 * Return the number of integer divisions if not.
630 * Return -1 on error.
632 * Modulo constraints are currently not printed in C format.
633 * Other than that, "pos" needs to correspond to an integer division
634 * with explicit representation and "c" needs to be a multiple
635 * of the denominator of the integer division.
637 static int print_as_modulo_pos(__isl_keep isl_printer *p,
638 __isl_keep isl_space *space, __isl_keep isl_mat *div, unsigned pos,
639 isl_int c)
641 isl_bool can_print;
642 isl_size n_div;
643 enum isl_dim_type type;
645 n_div = isl_mat_rows(div);
646 if (!p || !space || n_div < 0)
647 return -1;
648 if (p->output_format == ISL_FORMAT_C)
649 return n_div;
650 if (pos2type(space, &type, &pos) < 0)
651 return -1;
652 if (type != isl_dim_div)
653 return n_div;
654 can_print = can_print_div_expr(p, div, pos);
655 if (can_print < 0)
656 return -1;
657 if (!can_print)
658 return n_div;
659 if (!isl_int_is_divisible_by(c, div->row[pos][0]))
660 return n_div;
661 return pos;
664 /* Print equality constraint "c" to "p" as a modulo constraint,
665 * with the variable names taken from "space" and
666 * the integer division definitions taken from "div".
667 * "last" is the position of the last non-zero coefficient, which is
668 * moreover assumed to be negative and a multiple of the denominator
669 * of the corresponding integer division. "div_pos" is the corresponding
670 * position in the sequence of integer divisions.
672 * The equality is of the form
674 * f - a m floor(g/m) = 0.
676 * Print it as
678 * a (g mod m) = -f + a g
680 static __isl_give isl_printer *print_eq_mod_constraint(
681 __isl_take isl_printer *p, __isl_keep isl_space *space,
682 __isl_keep isl_mat *div, unsigned div_pos,
683 isl_int *c, int last, int latex)
685 isl_ctx *ctx;
686 int multiple;
688 ctx = isl_printer_get_ctx(p);
689 isl_int_divexact(c[last], c[last], div->row[div_pos][0]);
690 isl_int_abs(c[last], c[last]);
691 multiple = !isl_int_is_one(c[last]);
692 if (multiple) {
693 p = isl_printer_print_isl_int(p, c[last]);
694 p = isl_printer_print_str(p, "*(");
696 p = print_mod(p, space, div, div_pos, latex);
697 if (multiple)
698 p = isl_printer_print_str(p, ")");
699 p = isl_printer_print_str(p, " = ");
700 isl_seq_combine(c, ctx->negone, c,
701 c[last], div->row[div_pos] + 1, last);
702 isl_int_set_si(c[last], 0);
703 p = print_affine(p, space, div, c);
704 return p;
707 /* Print equality constraint "c" to "p", with the variable names
708 * taken from "space" and the integer division definitions taken from "div".
709 * "last" is the position of the last non-zero coefficient, which is
710 * moreover assumed to be negative.
712 * If possible, print the equality constraint as a modulo constraint.
714 static __isl_give isl_printer *print_eq_constraint(__isl_take isl_printer *p,
715 __isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c,
716 int last, int latex)
718 isl_size n_div;
719 int div_pos;
721 n_div = isl_mat_rows(div);
722 div_pos = print_as_modulo_pos(p, space, div, last, c[last]);
723 if (n_div < 0 || div_pos < 0)
724 return isl_printer_free(p);
725 if (div_pos < n_div)
726 return print_eq_mod_constraint(p, space, div, div_pos,
727 c, last, latex);
728 return print_constraint(p, space, div, c, last, "=", latex);
731 /* Print the constraints of "bmap" to "p".
732 * The names of the variables are taken from "space" and
733 * the integer division definitions are taken from "div".
734 * Div constraints are only printed in "dump" mode.
735 * The constraints are sorted prior to printing (except in "dump" mode).
737 * If x is the last variable with a non-zero coefficient,
738 * then a lower bound
740 * f - a x >= 0
742 * is printed as
744 * a x <= f
746 * while an upper bound
748 * f + a x >= 0
750 * is printed as
752 * a x >= -f
754 * If the next constraint has an opposite sign for the same last coefficient,
755 * then it is printed as
757 * f >= a x
759 * or
761 * -f <= a x
763 * instead. In fact, the "a x" part is not printed explicitly, but
764 * reused from the next constraint, which is therefore treated as
765 * a first constraint in the conjunction.
767 * If the constant term of "f" is -1, then "f" is replaced by "f + 1" and
768 * the comparison operator is replaced by the strict variant.
769 * Essentially, ">= 1" is replaced by "> 0".
771 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
772 __isl_keep isl_space *space, __isl_keep isl_mat *div,
773 __isl_take isl_printer *p, int latex)
775 int i;
776 isl_vec *c = NULL;
777 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
778 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
779 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
780 int first = 1;
781 int dump;
783 if (total < 0 || !p)
784 return isl_printer_free(p);
785 bmap = isl_basic_map_copy(bmap);
786 dump = p->dump;
787 if (!dump)
788 bmap = isl_basic_map_sort_constraints(bmap);
789 if (!bmap)
790 goto error;
792 c = isl_vec_alloc(bmap->ctx, 1 + total);
793 if (!c)
794 goto error;
796 for (i = bmap->n_eq - 1; i >= 0; --i) {
797 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
798 if (l < 0) {
799 if (i != bmap->n_eq - 1)
800 p = isl_printer_print_str(p, s_and[latex]);
801 p = isl_printer_print_str(p, "0 = 0");
802 continue;
804 if (!first)
805 p = isl_printer_print_str(p, s_and[latex]);
806 if (isl_int_is_neg(bmap->eq[i][l]))
807 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
808 else
809 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
810 p = print_eq_constraint(p, space, div, c->el, l, latex);
811 first = 0;
813 for (i = 0; i < bmap->n_ineq; ++i) {
814 isl_bool combine;
815 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
816 int strict;
817 int s;
818 const char *op;
819 if (l < 0)
820 continue;
821 if (!dump && l >= o_div &&
822 can_print_div_expr(p, div, l - o_div)) {
823 isl_bool is_div;
824 is_div = isl_basic_map_is_div_constraint(bmap,
825 bmap->ineq[i], l - o_div);
826 if (is_div < 0)
827 goto error;
828 if (is_div)
829 continue;
831 if (!first)
832 p = isl_printer_print_str(p, s_and[latex]);
833 s = isl_int_sgn(bmap->ineq[i][l]);
834 strict = !rational && isl_int_is_negone(bmap->ineq[i][0]);
835 if (s < 0)
836 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
837 else
838 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
839 if (strict)
840 isl_int_set_si(c->el[0], 0);
841 combine = dump ? isl_bool_false : next_is_opposite(bmap, i, l);
842 if (combine < 0)
843 goto error;
844 if (combine) {
845 op = constraint_op(-s, strict, latex);
846 p = print_half_constraint(p, space, div, c->el, l,
847 op, latex);
848 first = 1;
849 } else {
850 op = constraint_op(s, strict, latex);
851 p = print_constraint(p, space, div, c->el, l,
852 op, latex);
853 first = 0;
857 isl_basic_map_free(bmap);
858 isl_vec_free(c);
860 return p;
861 error:
862 isl_basic_map_free(bmap);
863 isl_vec_free(c);
864 isl_printer_free(p);
865 return NULL;
868 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
869 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
871 int c;
873 if (!p || !div)
874 return isl_printer_free(p);
876 c = p->output_format == ISL_FORMAT_C;
877 p = isl_printer_print_str(p, c ? "floord(" : "floor((");
878 p = print_affine_of_len(dim, div, p,
879 div->row[pos] + 1, div->n_col - 1);
880 p = isl_printer_print_str(p, c ? ", " : ")/");
881 p = isl_printer_print_isl_int(p, div->row[pos][0]);
882 p = isl_printer_print_str(p, ")");
883 return p;
886 /* Print a comma separated list of div names, except those that have
887 * a definition that can be printed.
888 * If "print_defined_divs" is set, then those div names are printed
889 * as well, along with their definitions.
891 static __isl_give isl_printer *print_div_list(__isl_take isl_printer *p,
892 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex,
893 int print_defined_divs)
895 int i;
896 int first = 1;
897 isl_size n_div;
899 n_div = isl_mat_rows(div);
900 if (!p || !space || n_div < 0)
901 return isl_printer_free(p);
903 for (i = 0; i < n_div; ++i) {
904 if (!print_defined_divs && can_print_div_expr(p, div, i))
905 continue;
906 if (!first)
907 p = isl_printer_print_str(p, ", ");
908 p = print_name(space, p, isl_dim_div, i, latex);
909 first = 0;
910 if (!can_print_div_expr(p, div, i))
911 continue;
912 p = isl_printer_print_str(p, " = ");
913 p = print_div(space, div, i, p);
916 return p;
919 /* Does printing an object with local variables described by "div"
920 * require an "exists" clause?
921 * That is, are there any local variables without an explicit representation?
922 * An exists clause is also needed in "dump" mode because
923 * explicit div representations are not printed inline in that case.
925 static isl_bool need_exists(__isl_keep isl_printer *p, __isl_keep isl_mat *div)
927 int i;
928 isl_size n;
930 n = isl_mat_rows(div);
931 if (!p || n < 0)
932 return isl_bool_error;
933 if (n == 0)
934 return isl_bool_false;
935 if (p->dump)
936 return isl_bool_true;
937 for (i = 0; i < n; ++i)
938 if (!can_print_div_expr(p, div, i))
939 return isl_bool_true;
940 return isl_bool_false;
943 /* Print the start of an exists clause, i.e.,
945 * (exists variables:
947 * In dump mode, local variables with an explicit definition are printed
948 * as well because they will not be printed inline.
950 static __isl_give isl_printer *open_exists(__isl_take isl_printer *p,
951 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex)
953 int dump;
955 if (!p)
956 return NULL;
958 dump = p->dump;
959 p = isl_printer_print_str(p, s_open_exists[latex]);
960 p = print_div_list(p, space, div, latex, dump);
961 p = isl_printer_print_str(p, ": ");
963 return p;
966 /* Remove the explicit representations of all local variables in "div".
968 static __isl_give isl_mat *mark_all_unknown(__isl_take isl_mat *div)
970 int i;
971 isl_size n_div;
973 n_div = isl_mat_rows(div);
974 if (n_div < 0)
975 return isl_mat_free(div);
977 for (i = 0; i < n_div; ++i)
978 div = isl_mat_set_element_si(div, i, 0, 0);
979 return div;
982 /* Print the constraints of "bmap" to "p".
983 * The names of the variables are taken from "space".
984 * "latex" is set if the constraints should be printed in LaTeX format.
985 * Do not print inline explicit div representations in "dump" mode.
987 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
988 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
990 int dump;
991 isl_mat *div;
992 isl_bool exists;
994 if (!p)
995 return NULL;
996 dump = p->dump;
997 div = isl_basic_map_get_divs(bmap);
998 exists = need_exists(p, div);
999 if (exists >= 0 && exists)
1000 p = open_exists(p, space, div, latex);
1002 if (dump)
1003 div = mark_all_unknown(div);
1004 p = print_constraints(bmap, space, div, p, latex);
1005 isl_mat_free(div);
1007 if (exists >= 0 && exists)
1008 p = isl_printer_print_str(p, s_close_exists[latex]);
1009 return p;
1012 /* Print a colon followed by the constraints of "bmap"
1013 * to "p", provided there are any constraints.
1014 * The names of the variables are taken from "space".
1015 * "latex" is set if the constraints should be printed in LaTeX format.
1017 static __isl_give isl_printer *print_optional_disjunct(
1018 __isl_keep isl_basic_map *bmap, __isl_keep isl_space *space,
1019 __isl_take isl_printer *p, int latex)
1021 if (isl_basic_map_plain_is_universe(bmap))
1022 return p;
1024 p = isl_printer_print_str(p, ": ");
1025 p = print_disjunct(bmap, space, p, latex);
1027 return p;
1030 static __isl_give isl_printer *basic_map_print_omega(
1031 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
1033 p = isl_printer_print_str(p, "{ [");
1034 p = print_var_list(p, bmap->dim, isl_dim_in);
1035 p = isl_printer_print_str(p, "] -> [");
1036 p = print_var_list(p, bmap->dim, isl_dim_out);
1037 p = isl_printer_print_str(p, "] ");
1038 p = print_optional_disjunct(bmap, bmap->dim, p, 0);
1039 p = isl_printer_print_str(p, " }");
1040 return p;
1043 static __isl_give isl_printer *basic_set_print_omega(
1044 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
1046 p = isl_printer_print_str(p, "{ [");
1047 p = print_var_list(p, bset->dim, isl_dim_set);
1048 p = isl_printer_print_str(p, "] ");
1049 p = print_optional_disjunct(bset, bset->dim, p, 0);
1050 p = isl_printer_print_str(p, " }");
1051 return p;
1054 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
1055 __isl_take isl_printer *p)
1057 int i;
1059 for (i = 0; i < map->n; ++i) {
1060 if (i)
1061 p = isl_printer_print_str(p, " union ");
1062 p = basic_map_print_omega(map->p[i], p);
1064 return p;
1067 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
1068 __isl_take isl_printer *p)
1070 int i;
1072 for (i = 0; i < set->n; ++i) {
1073 if (i)
1074 p = isl_printer_print_str(p, " union ");
1075 p = basic_set_print_omega(set->p[i], p);
1077 return p;
1080 /* Print the list of parameters in "space", followed by an arrow, to "p",
1081 * if there are any parameters.
1083 static __isl_give isl_printer *print_param_tuple(__isl_take isl_printer *p,
1084 __isl_keep isl_space *space, struct isl_print_space_data *data)
1086 isl_size nparam;
1088 nparam = isl_space_dim(space, isl_dim_param);
1089 if (!p || nparam < 0)
1090 return isl_printer_free(p);
1091 if (nparam == 0)
1092 return p;
1094 p = print_tuple(space, p, isl_dim_param, data);
1095 p = isl_printer_print_str(p, s_to[data->latex]);
1097 return p;
1100 static __isl_give isl_printer *isl_basic_map_print_isl(
1101 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
1102 int latex)
1104 struct isl_print_space_data data = { .latex = latex };
1105 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1107 p = print_param_tuple(p, bmap->dim, &data);
1108 p = isl_printer_print_str(p, "{ ");
1109 p = isl_print_space(bmap->dim, p, rational, &data);
1110 p = isl_printer_print_str(p, " : ");
1111 p = print_disjunct(bmap, bmap->dim, p, latex);
1112 p = isl_printer_print_str(p, " }");
1113 return p;
1116 /* Print the disjuncts of a map (or set) "map" to "p".
1117 * The names of the variables are taken from "space".
1118 * "latex" is set if the constraints should be printed in LaTeX format.
1120 static __isl_give isl_printer *print_disjuncts_core(__isl_keep isl_map *map,
1121 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1123 int i;
1125 if (map->n == 0)
1126 p = isl_printer_print_str(p, "false");
1127 for (i = 0; i < map->n; ++i) {
1128 if (i)
1129 p = isl_printer_print_str(p, s_or[latex]);
1130 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
1131 p = isl_printer_print_str(p, "(");
1132 p = print_disjunct(map->p[i], space, p, latex);
1133 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
1134 p = isl_printer_print_str(p, ")");
1136 return p;
1139 /* Print the disjuncts of a map (or set) "map" to "p".
1140 * The names of the variables are taken from "space".
1141 * "hull" describes constraints shared by all disjuncts of "map".
1142 * "latex" is set if the constraints should be printed in LaTeX format.
1144 * Print the disjuncts as a conjunction of "hull" and
1145 * the result of removing the constraints of "hull" from "map".
1146 * If this result turns out to be the universe, then simply print "hull".
1148 static __isl_give isl_printer *print_disjuncts_in_hull(__isl_keep isl_map *map,
1149 __isl_keep isl_space *space, __isl_take isl_basic_map *hull,
1150 __isl_take isl_printer *p, int latex)
1152 isl_bool is_universe;
1154 p = print_disjunct(hull, space, p, latex);
1155 map = isl_map_plain_gist_basic_map(isl_map_copy(map), hull);
1156 is_universe = isl_map_plain_is_universe(map);
1157 if (is_universe < 0)
1158 goto error;
1159 if (!is_universe) {
1160 p = isl_printer_print_str(p, s_and[latex]);
1161 p = isl_printer_print_str(p, "(");
1162 p = print_disjuncts_core(map, space, p, latex);
1163 p = isl_printer_print_str(p, ")");
1165 isl_map_free(map);
1167 return p;
1168 error:
1169 isl_map_free(map);
1170 isl_printer_free(p);
1171 return NULL;
1174 /* Print the disjuncts of a map (or set) "map" to "p".
1175 * The names of the variables are taken from "space".
1176 * "latex" is set if the constraints should be printed in LaTeX format.
1178 * If there are at least two disjuncts and "dump" mode is not turned out,
1179 * check for any shared constraints among all disjuncts.
1180 * If there are any, then print them separately in print_disjuncts_in_hull.
1182 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
1183 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1185 if (isl_map_plain_is_universe(map))
1186 return p;
1188 p = isl_printer_print_str(p, s_such_that[latex]);
1189 if (!p)
1190 return NULL;
1192 if (!p->dump && map->n >= 2) {
1193 isl_basic_map *hull;
1194 isl_bool is_universe;
1196 hull = isl_map_plain_unshifted_simple_hull(isl_map_copy(map));
1197 is_universe = isl_basic_map_plain_is_universe(hull);
1198 if (is_universe < 0)
1199 p = isl_printer_free(p);
1200 else if (!is_universe)
1201 return print_disjuncts_in_hull(map, space, hull,
1202 p, latex);
1203 isl_basic_map_free(hull);
1206 return print_disjuncts_core(map, space, p, latex);
1209 /* Print the disjuncts of a map (or set).
1210 * The names of the variables are taken from "space".
1211 * "latex" is set if the constraints should be printed in LaTeX format.
1213 * If the map turns out to be a universal parameter domain, then
1214 * we need to print the colon. Otherwise, the output looks identical
1215 * to the empty set.
1217 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
1218 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1220 if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
1221 return isl_printer_print_str(p, s_such_that[latex]);
1222 else
1223 return print_disjuncts(map, space, p, latex);
1226 /* Print the disjuncts of a set.
1227 * The names of the variables are taken from "space".
1228 * "latex" is set if the constraints should be printed in LaTeX format.
1230 static __isl_give isl_printer *print_disjuncts_set(__isl_keep isl_set *set,
1231 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1233 return print_disjuncts_map(set_to_map(set), space, p, latex);
1236 struct isl_aff_split {
1237 isl_basic_map *aff;
1238 isl_map *map;
1241 static void free_split(__isl_take struct isl_aff_split *split, int n)
1243 int i;
1245 if (!split)
1246 return;
1248 for (i = 0; i < n; ++i) {
1249 isl_basic_map_free(split[i].aff);
1250 isl_map_free(split[i].map);
1253 free(split);
1256 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
1258 int i, j;
1259 isl_size nparam, n_in, n_out, total;
1261 bmap = isl_basic_map_cow(bmap);
1262 if (!bmap)
1263 return NULL;
1264 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
1265 goto error;
1267 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1268 n_in = isl_basic_map_dim(bmap, isl_dim_in);
1269 n_out = isl_basic_map_dim(bmap, isl_dim_out);
1270 total = isl_basic_map_dim(bmap, isl_dim_all);
1271 if (n_in < 0 || n_out < 0 || nparam < 0 || total < 0)
1272 return isl_basic_map_free(bmap);
1274 for (i = bmap->n_eq - 1; i >= 0; --i) {
1275 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
1276 if (j >= nparam && j < nparam + n_in + n_out &&
1277 (isl_int_is_one(bmap->eq[i][1 + j]) ||
1278 isl_int_is_negone(bmap->eq[i][1 + j])))
1279 continue;
1280 if (isl_basic_map_drop_equality(bmap, i) < 0)
1281 goto error;
1284 bmap = isl_basic_map_finalize(bmap);
1286 return bmap;
1287 error:
1288 isl_basic_map_free(bmap);
1289 return NULL;
1292 static int aff_split_cmp(const void *p1, const void *p2, void *user)
1294 const struct isl_aff_split *s1, *s2;
1295 s1 = (const struct isl_aff_split *) p1;
1296 s2 = (const struct isl_aff_split *) p2;
1298 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
1301 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
1302 __isl_keep isl_basic_map *aff)
1304 int i, j;
1305 int v_div;
1307 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1308 if (v_div < 0 || !aff)
1309 goto error;
1311 for (i = bmap->n_eq - 1; i >= 0; --i) {
1312 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + v_div,
1313 bmap->n_div) != -1)
1314 continue;
1315 for (j = 0; j < aff->n_eq; ++j) {
1316 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + v_div) &&
1317 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + v_div))
1318 continue;
1319 if (isl_basic_map_drop_equality(bmap, i) < 0)
1320 goto error;
1321 break;
1325 return bmap;
1326 error:
1327 isl_basic_map_free(bmap);
1328 return NULL;
1331 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
1333 int i, n;
1334 struct isl_aff_split *split;
1335 isl_ctx *ctx;
1337 ctx = isl_map_get_ctx(map);
1338 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
1339 if (!split)
1340 return NULL;
1342 for (i = 0; i < map->n; ++i) {
1343 isl_basic_map *bmap;
1344 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
1345 bmap = isl_basic_map_copy(map->p[i]);
1346 bmap = isl_basic_map_cow(bmap);
1347 bmap = drop_aff(bmap, split[i].aff);
1348 split[i].map = isl_map_from_basic_map(bmap);
1349 if (!split[i].aff || !split[i].map)
1350 goto error;
1353 if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
1354 &aff_split_cmp, NULL) < 0)
1355 goto error;
1357 n = map->n;
1358 for (i = n - 1; i >= 1; --i) {
1359 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
1360 split[i].aff))
1361 continue;
1362 isl_basic_map_free(split[i].aff);
1363 split[i - 1].map = isl_map_union(split[i - 1].map,
1364 split[i].map);
1365 if (i != n - 1)
1366 split[i] = split[n - 1];
1367 split[n - 1].aff = NULL;
1368 split[n - 1].map = NULL;
1369 --n;
1372 return split;
1373 error:
1374 free_split(split, map->n);
1375 return NULL;
1378 static int defining_equality(__isl_keep isl_basic_map *eq,
1379 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
1381 int i;
1382 isl_size total;
1384 total = isl_basic_map_dim(eq, isl_dim_all);
1385 if (total < 0)
1386 return -1;
1388 pos += isl_space_offset(dim, type);
1390 for (i = 0; i < eq->n_eq; ++i) {
1391 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
1392 continue;
1393 if (isl_int_is_one(eq->eq[i][1 + pos]))
1394 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
1395 return i;
1398 return -1;
1401 /* Print dimension "pos" of data->space to "p".
1403 * data->user is assumed to be an isl_basic_map keeping track of equalities.
1405 * If the current dimension is defined by these equalities, then print
1406 * the corresponding expression, assigned to the name of the dimension
1407 * if there is any. Otherwise, print the name of the dimension.
1409 static __isl_give isl_printer *print_dim_eq(__isl_take isl_printer *p,
1410 struct isl_print_space_data *data, unsigned pos)
1412 isl_basic_map *eq = data->user;
1413 int j;
1415 j = defining_equality(eq, data->space, data->type, pos);
1416 if (j >= 0) {
1417 if (isl_space_has_dim_name(data->space, data->type, pos)) {
1418 p = print_name(data->space, p, data->type, pos,
1419 data->latex);
1420 p = isl_printer_print_str(p, " = ");
1422 pos += 1 + isl_space_offset(data->space, data->type);
1423 p = print_affine_of_len(data->space, NULL, p, eq->eq[j], pos);
1424 } else {
1425 p = print_name(data->space, p, data->type, pos, data->latex);
1428 return p;
1431 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
1432 struct isl_aff_split *split, int n, __isl_keep isl_space *space)
1434 struct isl_print_space_data data = { 0 };
1435 int i;
1436 int rational;
1438 data.print_dim = &print_dim_eq;
1439 for (i = 0; i < n; ++i) {
1440 if (!split[i].map)
1441 break;
1442 rational = split[i].map->n > 0 &&
1443 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
1444 if (i)
1445 p = isl_printer_print_str(p, "; ");
1446 data.user = split[i].aff;
1447 p = isl_print_space(space, p, rational, &data);
1448 p = print_disjuncts_map(split[i].map, space, p, 0);
1451 return p;
1454 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
1455 __isl_take isl_printer *p)
1457 struct isl_print_space_data data = { 0 };
1458 struct isl_aff_split *split = NULL;
1459 int rational;
1461 if (!p || !map)
1462 return isl_printer_free(p);
1463 if (!p->dump && map->n > 0)
1464 split = split_aff(map);
1465 if (split) {
1466 p = print_split_map(p, split, map->n, map->dim);
1467 } else {
1468 rational = map->n > 0 &&
1469 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
1470 p = isl_print_space(map->dim, p, rational, &data);
1471 p = print_disjuncts_map(map, map->dim, p, 0);
1473 free_split(split, map->n);
1474 return p;
1477 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
1478 __isl_take isl_printer *p)
1480 struct isl_print_space_data data = { 0 };
1482 p = print_param_tuple(p, map->dim, &data);
1483 p = isl_printer_print_str(p, s_open_set[0]);
1484 p = isl_map_print_isl_body(map, p);
1485 p = isl_printer_print_str(p, s_close_set[0]);
1486 return p;
1489 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
1490 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
1492 struct isl_print_space_data data = { 0 };
1494 data.latex = 1;
1495 p = print_param_tuple(p, map->dim, &data);
1496 p = isl_printer_print_str(p, s_open_set[1]);
1497 data.print_dim = &print_dim_eq;
1498 data.user = aff;
1499 p = isl_print_space(map->dim, p, 0, &data);
1500 p = print_disjuncts_map(map, map->dim, p, 1);
1501 p = isl_printer_print_str(p, s_close_set[1]);
1503 return p;
1506 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
1507 __isl_take isl_printer *p)
1509 int i;
1510 struct isl_aff_split *split = NULL;
1512 if (map->n > 0)
1513 split = split_aff(map);
1515 if (!split)
1516 return print_latex_map(map, p, NULL);
1518 for (i = 0; i < map->n; ++i) {
1519 if (!split[i].map)
1520 break;
1521 if (i)
1522 p = isl_printer_print_str(p, " \\cup ");
1523 p = print_latex_map(split[i].map, p, split[i].aff);
1526 free_split(split, map->n);
1527 return p;
1530 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
1531 __isl_keep isl_basic_map *bmap)
1533 if (!p || !bmap)
1534 goto error;
1535 if (p->output_format == ISL_FORMAT_ISL)
1536 return isl_basic_map_print_isl(bmap, p, 0);
1537 else if (p->output_format == ISL_FORMAT_OMEGA)
1538 return basic_map_print_omega(bmap, p);
1539 isl_assert(bmap->ctx, 0, goto error);
1540 error:
1541 isl_printer_free(p);
1542 return NULL;
1545 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
1546 __isl_keep isl_basic_set *bset)
1548 if (!p || !bset)
1549 goto error;
1551 if (p->output_format == ISL_FORMAT_ISL)
1552 return isl_basic_map_print_isl(bset, p, 0);
1553 else if (p->output_format == ISL_FORMAT_POLYLIB)
1554 return isl_basic_set_print_polylib(bset, p, 0);
1555 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1556 return isl_basic_set_print_polylib(bset, p, 1);
1557 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
1558 return bset_print_constraints_polylib(bset, p);
1559 else if (p->output_format == ISL_FORMAT_OMEGA)
1560 return basic_set_print_omega(bset, p);
1561 isl_assert(p->ctx, 0, goto error);
1562 error:
1563 isl_printer_free(p);
1564 return NULL;
1567 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1568 __isl_keep isl_set *set)
1570 if (!p || !set)
1571 goto error;
1572 if (p->output_format == ISL_FORMAT_ISL)
1573 return isl_map_print_isl(set_to_map(set), p);
1574 else if (p->output_format == ISL_FORMAT_POLYLIB)
1575 return isl_set_print_polylib(set, p, 0);
1576 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1577 return isl_set_print_polylib(set, p, 1);
1578 else if (p->output_format == ISL_FORMAT_OMEGA)
1579 return isl_set_print_omega(set, p);
1580 else if (p->output_format == ISL_FORMAT_LATEX)
1581 return isl_map_print_latex(set_to_map(set), p);
1582 isl_assert(set->ctx, 0, goto error);
1583 error:
1584 isl_printer_free(p);
1585 return NULL;
1588 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1589 __isl_keep isl_map *map)
1591 if (!p || !map)
1592 goto error;
1594 if (p->output_format == ISL_FORMAT_ISL)
1595 return isl_map_print_isl(map, p);
1596 else if (p->output_format == ISL_FORMAT_POLYLIB)
1597 return isl_map_print_polylib(map, p, 0);
1598 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1599 return isl_map_print_polylib(map, p, 1);
1600 else if (p->output_format == ISL_FORMAT_OMEGA)
1601 return isl_map_print_omega(map, p);
1602 else if (p->output_format == ISL_FORMAT_LATEX)
1603 return isl_map_print_latex(map, p);
1604 isl_assert(map->ctx, 0, goto error);
1605 error:
1606 isl_printer_free(p);
1607 return NULL;
1610 struct isl_union_print_data {
1611 isl_printer *p;
1612 int first;
1615 static isl_stat print_map_body(__isl_take isl_map *map, void *user)
1617 struct isl_union_print_data *data;
1618 data = (struct isl_union_print_data *)user;
1620 if (!data->first)
1621 data->p = isl_printer_print_str(data->p, "; ");
1622 data->first = 0;
1624 data->p = isl_map_print_isl_body(map, data->p);
1625 isl_map_free(map);
1627 return isl_stat_ok;
1630 /* Print the body of "umap" (everything except the parameter declarations)
1631 * to "p" in isl format.
1633 static __isl_give isl_printer *isl_printer_print_union_map_isl_body(
1634 __isl_take isl_printer *p, __isl_keep isl_union_map *umap)
1636 struct isl_union_print_data data;
1638 p = isl_printer_print_str(p, s_open_set[0]);
1639 data.p = p;
1640 data.first = 1;
1641 isl_union_map_foreach_map(umap, &print_map_body, &data);
1642 p = data.p;
1643 p = isl_printer_print_str(p, s_close_set[0]);
1644 return p;
1647 /* Print the body of "uset" (everything except the parameter declarations)
1648 * to "p" in isl format.
1650 static __isl_give isl_printer *isl_printer_print_union_set_isl_body(
1651 __isl_take isl_printer *p, __isl_keep isl_union_set *uset)
1653 return isl_printer_print_union_map_isl_body(p, uset_to_umap(uset));
1656 /* Print the isl_union_map "umap" to "p" in isl format.
1658 static __isl_give isl_printer *isl_union_map_print_isl(
1659 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1661 struct isl_print_space_data space_data = { 0 };
1662 isl_space *space;
1664 space = isl_union_map_get_space(umap);
1665 p = print_param_tuple(p, space, &space_data);
1666 isl_space_free(space);
1668 p = isl_printer_print_union_map_isl_body(p, umap);
1670 return p;
1673 static isl_stat print_latex_map_body(__isl_take isl_map *map, void *user)
1675 struct isl_union_print_data *data;
1676 data = (struct isl_union_print_data *)user;
1678 if (!data->first)
1679 data->p = isl_printer_print_str(data->p, " \\cup ");
1680 data->first = 0;
1682 data->p = isl_map_print_latex(map, data->p);
1683 isl_map_free(map);
1685 return isl_stat_ok;
1688 static __isl_give isl_printer *isl_union_map_print_latex(
1689 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1691 struct isl_union_print_data data = { p, 1 };
1692 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1693 p = data.p;
1694 return p;
1697 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1698 __isl_keep isl_union_map *umap)
1700 if (!p || !umap)
1701 goto error;
1703 if (p->output_format == ISL_FORMAT_ISL)
1704 return isl_union_map_print_isl(umap, p);
1705 if (p->output_format == ISL_FORMAT_LATEX)
1706 return isl_union_map_print_latex(umap, p);
1708 isl_die(p->ctx, isl_error_invalid,
1709 "invalid output format for isl_union_map", goto error);
1710 error:
1711 isl_printer_free(p);
1712 return NULL;
1715 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1716 __isl_keep isl_union_set *uset)
1718 if (!p || !uset)
1719 goto error;
1721 if (p->output_format == ISL_FORMAT_ISL)
1722 return isl_union_map_print_isl(uset_to_umap(uset), p);
1723 if (p->output_format == ISL_FORMAT_LATEX)
1724 return isl_union_map_print_latex(uset_to_umap(uset), p);
1726 isl_die(p->ctx, isl_error_invalid,
1727 "invalid output format for isl_union_set", goto error);
1728 error:
1729 isl_printer_free(p);
1730 return NULL;
1733 static int poly_rec_n_non_zero(__isl_keep isl_poly_rec *rec)
1735 int i;
1736 int n;
1738 if (!rec)
1739 return -1;
1741 for (i = 0, n = 0; i < rec->n; ++i) {
1742 isl_bool is_zero = isl_poly_is_zero(rec->p[i]);
1744 if (is_zero < 0)
1745 return -1;
1746 if (!is_zero)
1747 ++n;
1750 return n;
1753 static __isl_give isl_printer *poly_print_cst(__isl_keep isl_poly *poly,
1754 __isl_take isl_printer *p, int first)
1756 isl_poly_cst *cst;
1757 int neg;
1759 cst = isl_poly_as_cst(poly);
1760 if (!cst)
1761 goto error;
1762 neg = !first && isl_int_is_neg(cst->n);
1763 if (!first)
1764 p = isl_printer_print_str(p, neg ? " - " : " + ");
1765 if (neg)
1766 isl_int_neg(cst->n, cst->n);
1767 if (isl_int_is_zero(cst->d)) {
1768 int sgn = isl_int_sgn(cst->n);
1769 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1770 sgn == 0 ? "NaN" : "infty");
1771 } else
1772 p = isl_printer_print_isl_int(p, cst->n);
1773 if (neg)
1774 isl_int_neg(cst->n, cst->n);
1775 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1776 p = isl_printer_print_str(p, "/");
1777 p = isl_printer_print_isl_int(p, cst->d);
1779 return p;
1780 error:
1781 isl_printer_free(p);
1782 return NULL;
1785 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1786 __isl_keep isl_space *space, __isl_keep isl_mat *div, int var)
1788 isl_size total;
1790 total = isl_space_dim(space, isl_dim_all);
1791 if (total < 0)
1792 return isl_printer_free(p);
1793 if (var < total)
1794 p = print_term(space, NULL, space->ctx->one, 1 + var, p, 0);
1795 else
1796 p = print_div(space, div, var - total, p);
1797 return p;
1800 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1801 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1803 p = print_base(p, dim, div, var);
1804 if (exp == 1)
1805 return p;
1806 if (p->output_format == ISL_FORMAT_C) {
1807 int i;
1808 for (i = 1; i < exp; ++i) {
1809 p = isl_printer_print_str(p, "*");
1810 p = print_base(p, dim, div, var);
1812 } else {
1813 p = isl_printer_print_str(p, "^");
1814 p = isl_printer_print_int(p, exp);
1816 return p;
1819 /* Print the polynomial "poly" defined over the domain space "space" and
1820 * local variables defined by "div" to "p".
1822 static __isl_give isl_printer *poly_print(__isl_keep isl_poly *poly,
1823 __isl_keep isl_space *space, __isl_keep isl_mat *div,
1824 __isl_take isl_printer *p)
1826 int i, n, first, print_parens;
1827 isl_bool is_cst;
1828 isl_poly_rec *rec;
1830 is_cst = isl_poly_is_cst(poly);
1831 if (!p || is_cst < 0 || !space || !div)
1832 goto error;
1834 if (is_cst)
1835 return poly_print_cst(poly, p, 1);
1837 rec = isl_poly_as_rec(poly);
1838 n = poly_rec_n_non_zero(rec);
1839 if (n < 0)
1840 return isl_printer_free(p);
1841 print_parens = n > 1;
1842 if (print_parens)
1843 p = isl_printer_print_str(p, "(");
1844 for (i = 0, first = 1; i < rec->n; ++i) {
1845 isl_bool is_zero = isl_poly_is_zero(rec->p[i]);
1846 isl_bool is_one = isl_poly_is_one(rec->p[i]);
1847 isl_bool is_negone = isl_poly_is_negone(rec->p[i]);
1848 isl_bool is_cst = isl_poly_is_cst(rec->p[i]);
1850 if (is_zero < 0 || is_one < 0 || is_negone < 0)
1851 return isl_printer_free(p);
1852 if (is_zero)
1853 continue;
1854 if (is_negone) {
1855 if (!i)
1856 p = isl_printer_print_str(p, "-1");
1857 else if (first)
1858 p = isl_printer_print_str(p, "-");
1859 else
1860 p = isl_printer_print_str(p, " - ");
1861 } else if (is_cst && !is_one)
1862 p = poly_print_cst(rec->p[i], p, first);
1863 else {
1864 if (!first)
1865 p = isl_printer_print_str(p, " + ");
1866 if (i == 0 || !is_one)
1867 p = poly_print(rec->p[i], space, div, p);
1869 first = 0;
1870 if (i == 0)
1871 continue;
1872 if (!is_one && !is_negone)
1873 p = isl_printer_print_str(p, " * ");
1874 p = print_pow(p, space, div, rec->poly.var, i);
1876 if (print_parens)
1877 p = isl_printer_print_str(p, ")");
1878 return p;
1879 error:
1880 isl_printer_free(p);
1881 return NULL;
1884 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1885 __isl_keep isl_qpolynomial *qp)
1887 if (!p || !qp)
1888 goto error;
1889 p = poly_print(qp->poly, qp->dim, qp->div, p);
1890 return p;
1891 error:
1892 isl_printer_free(p);
1893 return NULL;
1896 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1897 __isl_keep isl_qpolynomial *qp)
1899 struct isl_print_space_data data = { 0 };
1901 if (!p || !qp)
1902 goto error;
1904 p = print_param_tuple(p, qp->dim, &data);
1905 p = isl_printer_print_str(p, "{ ");
1906 if (!isl_space_is_params(qp->dim)) {
1907 p = isl_print_space(qp->dim, p, 0, &data);
1908 p = isl_printer_print_str(p, " -> ");
1910 p = print_qpolynomial(p, qp);
1911 p = isl_printer_print_str(p, " }");
1912 return p;
1913 error:
1914 isl_printer_free(p);
1915 return NULL;
1918 /* Print the quasi-polynomial "qp" to "p" in C format, with the variable names
1919 * taken from the domain space "space".
1921 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1922 __isl_keep isl_space *space, __isl_keep isl_qpolynomial *qp)
1924 isl_bool is_one;
1925 isl_val *den;
1927 den = isl_qpolynomial_get_den(qp);
1928 qp = isl_qpolynomial_copy(qp);
1929 qp = isl_qpolynomial_scale_val(qp, isl_val_copy(den));
1930 is_one = isl_val_is_one(den);
1931 if (is_one < 0)
1932 p = isl_printer_free(p);
1933 if (!is_one)
1934 p = isl_printer_print_str(p, "(");
1935 if (qp)
1936 p = poly_print(qp->poly, space, qp->div, p);
1937 else
1938 p = isl_printer_free(p);
1939 if (!is_one) {
1940 p = isl_printer_print_str(p, ")/");
1941 p = isl_printer_print_val(p, den);
1943 isl_qpolynomial_free(qp);
1944 isl_val_free(den);
1945 return p;
1948 __isl_give isl_printer *isl_printer_print_qpolynomial(
1949 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1951 if (!p || !qp)
1952 goto error;
1954 if (p->output_format == ISL_FORMAT_ISL)
1955 return print_qpolynomial_isl(p, qp);
1956 else if (p->output_format == ISL_FORMAT_C)
1957 return print_qpolynomial_c(p, qp->dim, qp);
1958 else
1959 isl_die(qp->dim->ctx, isl_error_unsupported,
1960 "output format not supported for isl_qpolynomials",
1961 goto error);
1962 error:
1963 isl_printer_free(p);
1964 return NULL;
1967 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1968 unsigned output_format)
1970 isl_printer *p;
1972 if (!qp)
1973 return;
1975 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1976 p = isl_printer_to_file(qp->dim->ctx, out);
1977 p = isl_printer_print_qpolynomial(p, qp);
1978 isl_printer_free(p);
1981 static __isl_give isl_printer *qpolynomial_fold_print(
1982 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1984 int i;
1986 if (fold->type == isl_fold_min)
1987 p = isl_printer_print_str(p, "min");
1988 else if (fold->type == isl_fold_max)
1989 p = isl_printer_print_str(p, "max");
1990 p = isl_printer_print_str(p, "(");
1991 for (i = 0; i < fold->n; ++i) {
1992 if (i)
1993 p = isl_printer_print_str(p, ", ");
1994 p = print_qpolynomial(p, fold->qp[i]);
1996 p = isl_printer_print_str(p, ")");
1997 return p;
2000 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
2001 FILE *out, unsigned output_format)
2003 isl_printer *p;
2005 if (!fold)
2006 return;
2008 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
2010 p = isl_printer_to_file(fold->dim->ctx, out);
2011 p = isl_printer_print_qpolynomial_fold(p, fold);
2013 isl_printer_free(p);
2016 static __isl_give isl_printer *isl_pwqp_print_isl_body(
2017 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2019 struct isl_print_space_data data = { 0 };
2020 int i = 0;
2022 for (i = 0; i < pwqp->n; ++i) {
2023 isl_space *space;
2025 if (i)
2026 p = isl_printer_print_str(p, "; ");
2027 space = isl_qpolynomial_get_domain_space(pwqp->p[i].qp);
2028 if (!isl_space_is_params(space)) {
2029 p = isl_print_space(space, p, 0, &data);
2030 p = isl_printer_print_str(p, " -> ");
2032 p = print_qpolynomial(p, pwqp->p[i].qp);
2033 p = print_disjuncts(set_to_map(pwqp->p[i].set), space, p, 0);
2034 isl_space_free(space);
2037 return p;
2040 static __isl_give isl_printer *print_pw_qpolynomial_isl(
2041 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2043 struct isl_print_space_data data = { 0 };
2045 if (!p || !pwqp)
2046 goto error;
2048 p = print_param_tuple(p, pwqp->dim, &data);
2049 p = isl_printer_print_str(p, "{ ");
2050 if (pwqp->n == 0) {
2051 if (!isl_space_is_set(pwqp->dim)) {
2052 p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
2053 p = isl_printer_print_str(p, " -> ");
2055 p = isl_printer_print_str(p, "0");
2057 p = isl_pwqp_print_isl_body(p, pwqp);
2058 p = isl_printer_print_str(p, " }");
2059 return p;
2060 error:
2061 isl_printer_free(p);
2062 return NULL;
2065 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
2066 unsigned output_format)
2068 isl_printer *p;
2070 if (!pwqp)
2071 return;
2073 p = isl_printer_to_file(pwqp->dim->ctx, out);
2074 p = isl_printer_set_output_format(p, output_format);
2075 p = isl_printer_print_pw_qpolynomial(p, pwqp);
2077 isl_printer_free(p);
2080 static __isl_give isl_printer *isl_pwf_print_isl_body(
2081 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2083 struct isl_print_space_data data = { 0 };
2084 int i = 0;
2086 for (i = 0; i < pwf->n; ++i) {
2087 isl_space *space;
2089 if (i)
2090 p = isl_printer_print_str(p, "; ");
2091 space = isl_qpolynomial_fold_get_domain_space(pwf->p[i].fold);
2092 if (!isl_space_is_params(space)) {
2093 p = isl_print_space(space, p, 0, &data);
2094 p = isl_printer_print_str(p, " -> ");
2096 p = qpolynomial_fold_print(pwf->p[i].fold, p);
2097 p = print_disjuncts(set_to_map(pwf->p[i].set), space, p, 0);
2098 isl_space_free(space);
2101 return p;
2104 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
2105 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2107 struct isl_print_space_data data = { 0 };
2109 p = print_param_tuple(p, pwf->dim, &data);
2110 p = isl_printer_print_str(p, "{ ");
2111 if (pwf->n == 0) {
2112 if (!isl_space_is_set(pwf->dim)) {
2113 p = print_tuple(pwf->dim, p, isl_dim_in, &data);
2114 p = isl_printer_print_str(p, " -> ");
2116 p = isl_printer_print_str(p, "0");
2118 p = isl_pwf_print_isl_body(p, pwf);
2119 p = isl_printer_print_str(p, " }");
2120 return p;
2123 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2124 __isl_keep isl_local_space *ls, isl_int *c);
2126 /* We skip the constraint if it is implied by the div expression.
2128 * *first indicates whether this is the first constraint in the conjunction and
2129 * is updated if the constraint is actually printed.
2131 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
2132 __isl_keep isl_local_space *ls, isl_int *c, const char *op, int *first)
2134 unsigned o_div;
2135 isl_size n_div;
2136 int div;
2138 o_div = isl_local_space_offset(ls, isl_dim_div);
2139 n_div = isl_local_space_dim(ls, isl_dim_div);
2140 if (n_div < 0)
2141 return isl_printer_free(p);
2142 div = isl_seq_last_non_zero(c + o_div, n_div);
2143 if (div >= 0) {
2144 isl_bool is_div = isl_local_space_is_div_constraint(ls, c, div);
2145 if (is_div < 0)
2146 return isl_printer_free(p);
2147 if (is_div)
2148 return p;
2151 if (!*first)
2152 p = isl_printer_print_str(p, " && ");
2154 p = print_ls_affine_c(p, ls, c);
2155 p = isl_printer_print_str(p, " ");
2156 p = isl_printer_print_str(p, op);
2157 p = isl_printer_print_str(p, " 0");
2159 *first = 0;
2161 return p;
2164 static __isl_give isl_printer *print_ls_partial_affine_c(
2165 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2166 isl_int *c, unsigned len);
2168 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
2169 __isl_keep isl_space *space, __isl_keep isl_basic_set *bset)
2171 int i, j;
2172 int first = 1;
2173 isl_size n_div = isl_basic_set_dim(bset, isl_dim_div);
2174 isl_size total = isl_basic_set_dim(bset, isl_dim_all);
2175 isl_mat *div;
2176 isl_local_space *ls;
2178 if (n_div < 0 || total < 0)
2179 return isl_printer_free(p);
2181 total -= n_div;
2182 div = isl_basic_set_get_divs(bset);
2183 ls = isl_local_space_alloc_div(isl_space_copy(space), div);
2184 for (i = 0; i < bset->n_eq; ++i) {
2185 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
2186 if (j < 0)
2187 p = print_constraint_c(p, ls,
2188 bset->eq[i], "==", &first);
2189 else {
2190 if (i)
2191 p = isl_printer_print_str(p, " && ");
2192 p = isl_printer_print_str(p, "(");
2193 p = print_ls_partial_affine_c(p, ls, bset->eq[i],
2194 1 + total + j);
2195 p = isl_printer_print_str(p, ") % ");
2196 p = isl_printer_print_isl_int(p,
2197 bset->eq[i][1 + total + j]);
2198 p = isl_printer_print_str(p, " == 0");
2199 first = 0;
2202 for (i = 0; i < bset->n_ineq; ++i)
2203 p = print_constraint_c(p, ls, bset->ineq[i], ">=", &first);
2204 isl_local_space_free(ls);
2205 return p;
2208 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
2209 __isl_keep isl_space *space, __isl_keep isl_set *set)
2211 int i;
2213 if (!set)
2214 return isl_printer_free(p);
2216 if (set->n == 0)
2217 p = isl_printer_print_str(p, "0");
2219 for (i = 0; i < set->n; ++i) {
2220 if (i)
2221 p = isl_printer_print_str(p, " || ");
2222 if (set->n > 1)
2223 p = isl_printer_print_str(p, "(");
2224 p = print_basic_set_c(p, space, set->p[i]);
2225 if (set->n > 1)
2226 p = isl_printer_print_str(p, ")");
2228 return p;
2231 /* Print the piecewise quasi-polynomial "pwqp" to "p" in C format.
2233 static __isl_give isl_printer *print_pw_qpolynomial_c(
2234 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2236 int i;
2237 isl_space *space;
2239 space = isl_pw_qpolynomial_get_domain_space(pwqp);
2240 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set)) {
2241 p = print_qpolynomial_c(p, space, pwqp->p[0].qp);
2242 isl_space_free(space);
2243 return p;
2246 for (i = 0; i < pwqp->n; ++i) {
2247 p = isl_printer_print_str(p, "(");
2248 p = print_set_c(p, space, pwqp->p[i].set);
2249 p = isl_printer_print_str(p, ") ? (");
2250 p = print_qpolynomial_c(p, space, pwqp->p[i].qp);
2251 p = isl_printer_print_str(p, ") : ");
2254 isl_space_free(space);
2255 p = isl_printer_print_str(p, "0");
2256 return p;
2259 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
2260 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2262 if (!p || !pwqp)
2263 goto error;
2265 if (p->output_format == ISL_FORMAT_ISL)
2266 return print_pw_qpolynomial_isl(p, pwqp);
2267 else if (p->output_format == ISL_FORMAT_C)
2268 return print_pw_qpolynomial_c(p, pwqp);
2269 isl_assert(p->ctx, 0, goto error);
2270 error:
2271 isl_printer_free(p);
2272 return NULL;
2275 static isl_stat print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
2277 struct isl_union_print_data *data;
2278 data = (struct isl_union_print_data *)user;
2280 if (!data->first)
2281 data->p = isl_printer_print_str(data->p, "; ");
2282 data->first = 0;
2284 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
2285 isl_pw_qpolynomial_free(pwqp);
2287 return isl_stat_ok;
2290 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
2291 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2293 struct isl_union_print_data data;
2294 struct isl_print_space_data space_data = { 0 };
2295 isl_space *space;
2297 space = isl_union_pw_qpolynomial_get_space(upwqp);
2298 p = print_param_tuple(p, space, &space_data);
2299 isl_space_free(space);
2300 p = isl_printer_print_str(p, "{ ");
2301 data.p = p;
2302 data.first = 1;
2303 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
2304 &data);
2305 p = data.p;
2306 p = isl_printer_print_str(p, " }");
2307 return p;
2310 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
2311 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2313 if (!p || !upwqp)
2314 goto error;
2316 if (p->output_format == ISL_FORMAT_ISL)
2317 return print_union_pw_qpolynomial_isl(p, upwqp);
2318 isl_die(p->ctx, isl_error_invalid,
2319 "invalid output format for isl_union_pw_qpolynomial",
2320 goto error);
2321 error:
2322 isl_printer_free(p);
2323 return NULL;
2326 /* Print the quasi-polynomial reduction "fold" to "p" in C format,
2327 * with the variable names taken from the domain space "space".
2329 static __isl_give isl_printer *print_qpolynomial_fold_c(
2330 __isl_take isl_printer *p, __isl_keep isl_space *space,
2331 __isl_keep isl_qpolynomial_fold *fold)
2333 int i;
2335 for (i = 0; i < fold->n - 1; ++i)
2336 if (fold->type == isl_fold_min)
2337 p = isl_printer_print_str(p, "min(");
2338 else if (fold->type == isl_fold_max)
2339 p = isl_printer_print_str(p, "max(");
2341 for (i = 0; i < fold->n; ++i) {
2342 if (i)
2343 p = isl_printer_print_str(p, ", ");
2344 p = print_qpolynomial_c(p, space, fold->qp[i]);
2345 if (i)
2346 p = isl_printer_print_str(p, ")");
2348 return p;
2351 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
2352 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
2354 if (!p || !fold)
2355 goto error;
2356 if (p->output_format == ISL_FORMAT_ISL)
2357 return qpolynomial_fold_print(fold, p);
2358 else if (p->output_format == ISL_FORMAT_C)
2359 return print_qpolynomial_fold_c(p, fold->dim, fold);
2360 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2361 goto error);
2362 error:
2363 isl_printer_free(p);
2364 return NULL;
2367 /* Print the piecewise quasi-polynomial reduction "pwf" to "p" in C format.
2369 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
2370 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2372 int i;
2373 isl_space *space;
2375 space = isl_pw_qpolynomial_fold_get_domain_space(pwf);
2376 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set)) {
2377 p = print_qpolynomial_fold_c(p, space, pwf->p[0].fold);
2378 isl_space_free(space);
2379 return p;
2382 for (i = 0; i < pwf->n; ++i) {
2383 p = isl_printer_print_str(p, "(");
2384 p = print_set_c(p, space, pwf->p[i].set);
2385 p = isl_printer_print_str(p, ") ? (");
2386 p = print_qpolynomial_fold_c(p, space, pwf->p[i].fold);
2387 p = isl_printer_print_str(p, ") : ");
2390 isl_space_free(space);
2391 p = isl_printer_print_str(p, "0");
2392 return p;
2395 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
2396 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2398 if (!p || !pwf)
2399 goto error;
2401 if (p->output_format == ISL_FORMAT_ISL)
2402 return print_pw_qpolynomial_fold_isl(p, pwf);
2403 else if (p->output_format == ISL_FORMAT_C)
2404 return print_pw_qpolynomial_fold_c(p, pwf);
2405 isl_assert(p->ctx, 0, goto error);
2406 error:
2407 isl_printer_free(p);
2408 return NULL;
2411 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
2412 FILE *out, unsigned output_format)
2414 isl_printer *p;
2416 if (!pwf)
2417 return;
2419 p = isl_printer_to_file(pwf->dim->ctx, out);
2420 p = isl_printer_set_output_format(p, output_format);
2421 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
2423 isl_printer_free(p);
2426 static isl_stat print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf,
2427 void *user)
2429 struct isl_union_print_data *data;
2430 data = (struct isl_union_print_data *)user;
2432 if (!data->first)
2433 data->p = isl_printer_print_str(data->p, "; ");
2434 data->first = 0;
2436 data->p = isl_pwf_print_isl_body(data->p, pwf);
2437 isl_pw_qpolynomial_fold_free(pwf);
2439 return isl_stat_ok;
2442 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2443 __isl_take isl_printer *p,
2444 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2446 struct isl_union_print_data data;
2447 struct isl_print_space_data space_data = { 0 };
2448 isl_space *space;
2450 space = isl_union_pw_qpolynomial_fold_get_space(upwf);
2451 p = print_param_tuple(p, space, &space_data);
2452 isl_space_free(space);
2453 p = isl_printer_print_str(p, "{ ");
2454 data.p = p;
2455 data.first = 1;
2456 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2457 &print_pwf_body, &data);
2458 p = data.p;
2459 p = isl_printer_print_str(p, " }");
2460 return p;
2463 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2464 __isl_take isl_printer *p,
2465 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2467 if (!p || !upwf)
2468 goto error;
2470 if (p->output_format == ISL_FORMAT_ISL)
2471 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2472 isl_die(p->ctx, isl_error_invalid,
2473 "invalid output format for isl_union_pw_qpolynomial_fold",
2474 goto error);
2475 error:
2476 isl_printer_free(p);
2477 return NULL;
2480 /* Print the isl_constraint "c" to "p".
2482 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
2483 __isl_keep isl_constraint *c)
2485 struct isl_print_space_data data = { 0 };
2486 isl_local_space *ls;
2487 isl_space *space;
2488 isl_bool exists;
2490 if (!p || !c)
2491 goto error;
2493 ls = isl_constraint_get_local_space(c);
2494 if (!ls)
2495 return isl_printer_free(p);
2496 space = isl_local_space_get_space(ls);
2497 p = print_param_tuple(p, space, &data);
2498 p = isl_printer_print_str(p, "{ ");
2499 p = isl_print_space(space, p, 0, &data);
2500 p = isl_printer_print_str(p, " : ");
2501 exists = need_exists(p, ls->div);
2502 if (exists < 0)
2503 p = isl_printer_free(p);
2504 if (exists >= 0 && exists)
2505 p = open_exists(p, space, ls->div, 0);
2506 p = print_affine_of_len(space, ls->div, p, c->v->el, c->v->size);
2507 if (isl_constraint_is_equality(c))
2508 p = isl_printer_print_str(p, " = 0");
2509 else
2510 p = isl_printer_print_str(p, " >= 0");
2511 if (exists >= 0 && exists)
2512 p = isl_printer_print_str(p, s_close_exists[0]);
2513 p = isl_printer_print_str(p, " }");
2514 isl_space_free(space);
2515 isl_local_space_free(ls);
2517 return p;
2518 error:
2519 isl_printer_free(p);
2520 return NULL;
2523 static __isl_give isl_printer *isl_printer_print_space_isl(
2524 __isl_take isl_printer *p, __isl_keep isl_space *space)
2526 struct isl_print_space_data data = { 0 };
2528 if (!space)
2529 goto error;
2531 p = print_param_tuple(p, space, &data);
2533 p = isl_printer_print_str(p, "{ ");
2534 if (isl_space_is_params(space))
2535 p = isl_printer_print_str(p, s_such_that[0]);
2536 else
2537 p = isl_print_space(space, p, 0, &data);
2538 p = isl_printer_print_str(p, " }");
2540 return p;
2541 error:
2542 isl_printer_free(p);
2543 return NULL;
2546 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
2547 __isl_keep isl_space *space)
2549 if (!p || !space)
2550 return isl_printer_free(p);
2551 if (p->output_format == ISL_FORMAT_ISL)
2552 return isl_printer_print_space_isl(p, space);
2553 else if (p->output_format == ISL_FORMAT_OMEGA)
2554 return print_omega_parameters(space, p);
2556 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
2557 "output format not supported for space",
2558 return isl_printer_free(p));
2561 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
2562 __isl_keep isl_local_space *ls)
2564 struct isl_print_space_data data = { 0 };
2565 isl_size n_div;
2567 n_div = isl_local_space_dim(ls, isl_dim_div);
2568 if (n_div < 0)
2569 goto error;
2571 p = print_param_tuple(p, ls->dim, &data);
2572 p = isl_printer_print_str(p, "{ ");
2573 p = isl_print_space(ls->dim, p, 0, &data);
2574 if (n_div > 0) {
2575 p = isl_printer_print_str(p, " : ");
2576 p = isl_printer_print_str(p, s_open_exists[0]);
2577 p = print_div_list(p, ls->dim, ls->div, 0, 1);
2578 p = isl_printer_print_str(p, s_close_exists[0]);
2579 } else if (isl_space_is_params(ls->dim))
2580 p = isl_printer_print_str(p, s_such_that[0]);
2581 p = isl_printer_print_str(p, " }");
2582 return p;
2583 error:
2584 isl_printer_free(p);
2585 return NULL;
2588 /* Print the (potentially rational) affine expression "aff" to "p",
2589 * with the variable names taken from "space".
2591 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2592 __isl_keep isl_space *space, __isl_keep isl_aff *aff)
2594 isl_size total;
2596 if (isl_aff_is_nan(aff))
2597 return isl_printer_print_str(p, "NaN");
2599 total = isl_local_space_dim(aff->ls, isl_dim_all);
2600 if (total < 0)
2601 return isl_printer_free(p);
2602 p = isl_printer_print_str(p, "(");
2603 p = print_affine_of_len(space, aff->ls->div, p,
2604 aff->v->el + 1, 1 + total);
2605 if (isl_int_is_one(aff->v->el[0]))
2606 p = isl_printer_print_str(p, ")");
2607 else {
2608 p = isl_printer_print_str(p, ")/");
2609 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2612 return p;
2615 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2616 __isl_keep isl_aff *aff)
2618 struct isl_print_space_data data = { 0 };
2620 if (isl_space_is_params(aff->ls->dim))
2622 else {
2623 p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
2624 p = isl_printer_print_str(p, " -> ");
2626 p = isl_printer_print_str(p, "[");
2627 p = print_aff_body(p, aff->ls->dim, aff);
2628 p = isl_printer_print_str(p, "]");
2630 return p;
2633 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2634 __isl_keep isl_aff *aff)
2636 struct isl_print_space_data data = { 0 };
2638 if (!aff)
2639 goto error;
2641 p = print_param_tuple(p, aff->ls->dim, &data);
2642 p = isl_printer_print_str(p, "{ ");
2643 p = print_aff(p, aff);
2644 p = isl_printer_print_str(p, " }");
2645 return p;
2646 error:
2647 isl_printer_free(p);
2648 return NULL;
2651 /* Print the body of an isl_pw_aff, i.e., a semicolon delimited
2652 * sequence of affine expressions, each followed by constraints.
2654 static __isl_give isl_printer *print_pw_aff_body(
2655 __isl_take isl_printer *p, __isl_keep isl_pw_aff *pa)
2657 int i;
2659 if (!pa)
2660 return isl_printer_free(p);
2662 for (i = 0; i < pa->n; ++i) {
2663 isl_space *space;
2665 if (i)
2666 p = isl_printer_print_str(p, "; ");
2667 p = print_aff(p, pa->p[i].aff);
2668 space = isl_aff_get_domain_space(pa->p[i].aff);
2669 p = print_disjuncts(set_to_map(pa->p[i].set), space, p, 0);
2670 isl_space_free(space);
2673 return p;
2676 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2677 __isl_keep isl_pw_aff *pwaff)
2679 struct isl_print_space_data data = { 0 };
2681 if (!pwaff)
2682 goto error;
2684 p = print_param_tuple(p, pwaff->dim, &data);
2685 p = isl_printer_print_str(p, "{ ");
2686 p = print_pw_aff_body(p, pwaff);
2687 p = isl_printer_print_str(p, " }");
2688 return p;
2689 error:
2690 isl_printer_free(p);
2691 return NULL;
2694 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2695 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2697 if (type == isl_dim_div) {
2698 p = isl_printer_print_str(p, "floord(");
2699 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2700 p = isl_printer_print_str(p, ", ");
2701 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2702 p = isl_printer_print_str(p, ")");
2703 } else {
2704 const char *name;
2706 name = isl_space_get_dim_name(ls->dim, type, pos);
2707 if (!name)
2708 name = "UNNAMED";
2709 p = isl_printer_print_str(p, name);
2711 return p;
2714 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2715 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2717 enum isl_dim_type type;
2719 if (!p || !ls)
2720 return isl_printer_free(p);
2722 if (pos == 0)
2723 return isl_printer_print_isl_int(p, c);
2725 if (isl_int_is_one(c))
2727 else if (isl_int_is_negone(c))
2728 p = isl_printer_print_str(p, "-");
2729 else {
2730 p = isl_printer_print_isl_int(p, c);
2731 p = isl_printer_print_str(p, "*");
2733 if (pos2type(ls->dim, &type, &pos) < 0)
2734 return isl_printer_free(p);
2735 p = print_ls_name_c(p, ls, type, pos);
2736 return p;
2739 static __isl_give isl_printer *print_ls_partial_affine_c(
2740 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2741 isl_int *c, unsigned len)
2743 int i;
2744 int first;
2746 for (i = 0, first = 1; i < len; ++i) {
2747 int flip = 0;
2748 if (isl_int_is_zero(c[i]))
2749 continue;
2750 if (!first) {
2751 if (isl_int_is_neg(c[i])) {
2752 flip = 1;
2753 isl_int_neg(c[i], c[i]);
2754 p = isl_printer_print_str(p, " - ");
2755 } else
2756 p = isl_printer_print_str(p, " + ");
2758 first = 0;
2759 p = print_ls_term_c(p, ls, c[i], i);
2760 if (flip)
2761 isl_int_neg(c[i], c[i]);
2763 if (first)
2764 p = isl_printer_print_str(p, "0");
2765 return p;
2768 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2769 __isl_keep isl_local_space *ls, isl_int *c)
2771 isl_size total = isl_local_space_dim(ls, isl_dim_all);
2773 if (total < 0)
2774 return isl_printer_free(p);
2775 return print_ls_partial_affine_c(p, ls, c, 1 + total);
2778 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2779 __isl_keep isl_aff *aff)
2781 isl_size total;
2783 total = isl_local_space_dim(aff->ls, isl_dim_all);
2784 if (total < 0)
2785 return isl_printer_free(p);
2786 if (!isl_int_is_one(aff->v->el[0]))
2787 p = isl_printer_print_str(p, "(");
2788 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2789 if (!isl_int_is_one(aff->v->el[0])) {
2790 p = isl_printer_print_str(p, ")/");
2791 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2793 return p;
2796 /* In the C format, we cannot express that "pwaff" may be undefined
2797 * on parts of the domain space. We therefore assume that the expression
2798 * will only be evaluated on its definition domain and compute the gist
2799 * of each cell with respect to this domain.
2801 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2802 __isl_keep isl_pw_aff *pwaff)
2804 isl_set *domain;
2805 isl_ast_build *build;
2806 isl_ast_expr *expr;
2808 if (pwaff->n < 1)
2809 isl_die(p->ctx, isl_error_unsupported,
2810 "cannot print empty isl_pw_aff in C format",
2811 return isl_printer_free(p));
2813 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2814 build = isl_ast_build_from_context(domain);
2815 expr = isl_ast_build_expr_from_pw_aff(build, isl_pw_aff_copy(pwaff));
2816 p = isl_printer_print_ast_expr(p, expr);
2817 isl_ast_expr_free(expr);
2818 isl_ast_build_free(build);
2820 return p;
2823 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2824 __isl_keep isl_aff *aff)
2826 if (!p || !aff)
2827 goto error;
2829 if (p->output_format == ISL_FORMAT_ISL)
2830 return print_aff_isl(p, aff);
2831 else if (p->output_format == ISL_FORMAT_C)
2832 return print_aff_c(p, aff);
2833 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2834 goto error);
2835 error:
2836 isl_printer_free(p);
2837 return NULL;
2840 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2841 __isl_keep isl_pw_aff *pwaff)
2843 if (!p || !pwaff)
2844 goto error;
2846 if (p->output_format == ISL_FORMAT_ISL)
2847 return print_pw_aff_isl(p, pwaff);
2848 else if (p->output_format == ISL_FORMAT_C)
2849 return print_pw_aff_c(p, pwaff);
2850 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2851 goto error);
2852 error:
2853 isl_printer_free(p);
2854 return NULL;
2857 /* Print "pa" in a sequence of isl_pw_affs delimited by semicolons.
2858 * Each isl_pw_aff itself is also printed as semicolon delimited
2859 * sequence of pieces.
2860 * If data->first = 1, then this is the first in the sequence.
2861 * Update data->first to tell the next element that it is not the first.
2863 static isl_stat print_pw_aff_body_wrap(__isl_take isl_pw_aff *pa,
2864 void *user)
2866 struct isl_union_print_data *data;
2867 data = (struct isl_union_print_data *) user;
2869 if (!data->first)
2870 data->p = isl_printer_print_str(data->p, "; ");
2871 data->first = 0;
2873 data->p = print_pw_aff_body(data->p, pa);
2874 isl_pw_aff_free(pa);
2876 return data->p ? isl_stat_ok : isl_stat_error;
2879 /* Print the body of an isl_union_pw_aff, i.e., a semicolon delimited
2880 * sequence of affine expressions, each followed by constraints,
2881 * with the sequence enclosed in braces.
2883 static __isl_give isl_printer *print_union_pw_aff_body(
2884 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2886 struct isl_union_print_data data = { p, 1 };
2888 p = isl_printer_print_str(p, s_open_set[0]);
2889 data.p = p;
2890 if (isl_union_pw_aff_foreach_pw_aff(upa,
2891 &print_pw_aff_body_wrap, &data) < 0)
2892 data.p = isl_printer_free(p);
2893 p = data.p;
2894 p = isl_printer_print_str(p, s_close_set[0]);
2896 return p;
2899 /* Print the isl_union_pw_aff "upa" to "p" in isl format.
2901 * The individual isl_pw_affs are delimited by a semicolon.
2903 static __isl_give isl_printer *print_union_pw_aff_isl(
2904 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2906 struct isl_print_space_data data = { 0 };
2907 isl_space *space;
2909 space = isl_union_pw_aff_get_space(upa);
2910 p = print_param_tuple(p, space, &data);
2911 isl_space_free(space);
2912 p = print_union_pw_aff_body(p, upa);
2913 return p;
2916 /* Print the isl_union_pw_aff "upa" to "p".
2918 * We currently only support an isl format.
2920 __isl_give isl_printer *isl_printer_print_union_pw_aff(
2921 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2923 if (!p || !upa)
2924 return isl_printer_free(p);
2926 if (p->output_format == ISL_FORMAT_ISL)
2927 return print_union_pw_aff_isl(p, upa);
2928 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2929 "unsupported output format", return isl_printer_free(p));
2932 /* Print dimension "pos" of data->space to "p".
2934 * data->user is assumed to be an isl_multi_aff.
2936 * If the current dimension is an output dimension, then print
2937 * the corresponding expression. Otherwise, print the name of the dimension.
2939 static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
2940 struct isl_print_space_data *data, unsigned pos)
2942 isl_multi_aff *ma = data->user;
2944 if (data->type == isl_dim_out) {
2945 isl_space *space;
2947 space = isl_multi_aff_get_domain_space(ma);
2948 p = print_aff_body(p, space, ma->u.p[pos]);
2949 isl_space_free(space);
2950 } else {
2951 p = print_name(data->space, p, data->type, pos, data->latex);
2954 return p;
2957 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2958 __isl_keep isl_multi_aff *maff)
2960 struct isl_print_space_data data = { 0 };
2962 data.print_dim = &print_dim_ma;
2963 data.user = maff;
2964 return isl_print_space(maff->space, p, 0, &data);
2967 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2968 __isl_keep isl_multi_aff *maff)
2970 struct isl_print_space_data data = { 0 };
2972 if (!maff)
2973 goto error;
2975 p = print_param_tuple(p, maff->space, &data);
2976 p = isl_printer_print_str(p, "{ ");
2977 p = print_multi_aff(p, maff);
2978 p = isl_printer_print_str(p, " }");
2979 return p;
2980 error:
2981 isl_printer_free(p);
2982 return NULL;
2985 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2986 __isl_keep isl_multi_aff *maff)
2988 if (!p || !maff)
2989 goto error;
2991 if (p->output_format == ISL_FORMAT_ISL)
2992 return print_multi_aff_isl(p, maff);
2993 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2994 goto error);
2995 error:
2996 isl_printer_free(p);
2997 return NULL;
3000 static __isl_give isl_printer *print_pw_multi_aff_body(
3001 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3003 int i;
3005 if (!pma)
3006 goto error;
3008 for (i = 0; i < pma->n; ++i) {
3009 isl_space *space;
3011 if (i)
3012 p = isl_printer_print_str(p, "; ");
3013 p = print_multi_aff(p, pma->p[i].maff);
3014 space = isl_multi_aff_get_domain_space(pma->p[i].maff);
3015 p = print_disjuncts(set_to_map(pma->p[i].set), space, p, 0);
3016 isl_space_free(space);
3018 return p;
3019 error:
3020 isl_printer_free(p);
3021 return NULL;
3024 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
3025 __isl_keep isl_pw_multi_aff *pma)
3027 struct isl_print_space_data data = { 0 };
3029 if (!pma)
3030 goto error;
3032 p = print_param_tuple(p, pma->dim, &data);
3033 p = isl_printer_print_str(p, "{ ");
3034 p = print_pw_multi_aff_body(p, pma);
3035 p = isl_printer_print_str(p, " }");
3036 return p;
3037 error:
3038 isl_printer_free(p);
3039 return NULL;
3042 /* Print the unnamed, single-dimensional piecewise multi affine expression "pma"
3043 * to "p".
3045 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
3046 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3048 int i;
3049 isl_space *space;
3051 space = isl_pw_multi_aff_get_domain_space(pma);
3052 for (i = 0; i < pma->n - 1; ++i) {
3053 p = isl_printer_print_str(p, "(");
3054 p = print_set_c(p, space, pma->p[i].set);
3055 p = isl_printer_print_str(p, ") ? (");
3056 p = print_aff_c(p, pma->p[i].maff->u.p[0]);
3057 p = isl_printer_print_str(p, ") : ");
3059 isl_space_free(space);
3061 return print_aff_c(p, pma->p[pma->n - 1].maff->u.p[0]);
3064 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
3065 __isl_keep isl_pw_multi_aff *pma)
3067 isl_size n;
3068 const char *name;
3070 if (!pma)
3071 goto error;
3072 if (pma->n < 1)
3073 isl_die(p->ctx, isl_error_unsupported,
3074 "cannot print empty isl_pw_multi_aff in C format",
3075 goto error);
3076 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
3077 if (n < 0)
3078 return isl_printer_free(p);
3079 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
3080 if (!name && n == 1)
3081 return print_unnamed_pw_multi_aff_c(p, pma);
3082 if (!name)
3083 isl_die(p->ctx, isl_error_unsupported,
3084 "cannot print unnamed isl_pw_multi_aff in C format",
3085 goto error);
3087 p = isl_printer_print_str(p, name);
3088 if (n != 0)
3089 isl_die(p->ctx, isl_error_unsupported,
3090 "not supported yet", goto error);
3092 return p;
3093 error:
3094 isl_printer_free(p);
3095 return NULL;
3098 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
3099 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3101 if (!p || !pma)
3102 goto error;
3104 if (p->output_format == ISL_FORMAT_ISL)
3105 return print_pw_multi_aff_isl(p, pma);
3106 if (p->output_format == ISL_FORMAT_C)
3107 return print_pw_multi_aff_c(p, pma);
3108 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3109 goto error);
3110 error:
3111 isl_printer_free(p);
3112 return NULL;
3115 static isl_stat print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
3116 void *user)
3118 struct isl_union_print_data *data;
3119 data = (struct isl_union_print_data *) user;
3121 if (!data->first)
3122 data->p = isl_printer_print_str(data->p, "; ");
3123 data->first = 0;
3125 data->p = print_pw_multi_aff_body(data->p, pma);
3126 isl_pw_multi_aff_free(pma);
3128 return isl_stat_ok;
3131 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
3132 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3134 struct isl_union_print_data data;
3135 struct isl_print_space_data space_data = { 0 };
3136 isl_space *space;
3138 space = isl_union_pw_multi_aff_get_space(upma);
3139 p = print_param_tuple(p, space, &space_data);
3140 isl_space_free(space);
3141 p = isl_printer_print_str(p, s_open_set[0]);
3142 data.p = p;
3143 data.first = 1;
3144 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3145 &print_pw_multi_aff_body_wrap, &data);
3146 p = data.p;
3147 p = isl_printer_print_str(p, s_close_set[0]);
3148 return p;
3151 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
3152 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3154 if (!p || !upma)
3155 goto error;
3157 if (p->output_format == ISL_FORMAT_ISL)
3158 return print_union_pw_multi_aff_isl(p, upma);
3159 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3160 goto error);
3161 error:
3162 isl_printer_free(p);
3163 return NULL;
3166 /* Print dimension "pos" of data->space to "p".
3168 * data->user is assumed to be an isl_multi_pw_aff.
3170 * If the current dimension is an output dimension, then print
3171 * the corresponding piecewise affine expression.
3172 * Otherwise, print the name of the dimension.
3174 static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
3175 struct isl_print_space_data *data, unsigned pos)
3177 int i;
3178 int need_parens;
3179 isl_space *space;
3180 isl_multi_pw_aff *mpa = data->user;
3181 isl_pw_aff *pa;
3183 if (data->type != isl_dim_out)
3184 return print_name(data->space, p, data->type, pos, data->latex);
3186 pa = mpa->u.p[pos];
3187 if (pa->n == 0)
3188 return isl_printer_print_str(p, "(0 : false)");
3190 need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
3191 if (need_parens)
3192 p = isl_printer_print_str(p, "(");
3193 space = isl_multi_pw_aff_get_domain_space(mpa);
3194 for (i = 0; i < pa->n; ++i) {
3196 if (i)
3197 p = isl_printer_print_str(p, "; ");
3198 p = print_aff_body(p, space, pa->p[i].aff);
3199 p = print_disjuncts(pa->p[i].set, space, p, 0);
3201 isl_space_free(space);
3202 if (need_parens)
3203 p = isl_printer_print_str(p, ")");
3205 return p;
3208 /* Print "mpa" to "p" in isl format.
3210 * If "mpa" is zero-dimensional and has a non-trivial explicit domain,
3211 * then it is printed after the tuple of affine expressions.
3213 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
3214 __isl_keep isl_multi_pw_aff *mpa)
3216 struct isl_print_space_data data = { 0 };
3217 isl_bool has_domain;
3219 if (!mpa)
3220 return isl_printer_free(p);
3222 p = print_param_tuple(p, mpa->space, &data);
3223 p = isl_printer_print_str(p, "{ ");
3224 data.print_dim = &print_dim_mpa;
3225 data.user = mpa;
3226 p = isl_print_space(mpa->space, p, 0, &data);
3227 has_domain = isl_multi_pw_aff_has_non_trivial_domain(mpa);
3228 if (has_domain < 0)
3229 return isl_printer_free(p);
3230 if (has_domain) {
3231 isl_space *space;
3233 space = isl_space_domain(isl_space_copy(mpa->space));
3234 p = print_disjuncts_set(mpa->u.dom, space, p, 0);
3235 isl_space_free(space);
3237 p = isl_printer_print_str(p, " }");
3238 return p;
3241 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
3242 __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
3244 if (!p || !mpa)
3245 return isl_printer_free(p);
3247 if (p->output_format == ISL_FORMAT_ISL)
3248 return print_multi_pw_aff_isl(p, mpa);
3249 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3250 return isl_printer_free(p));
3253 /* Print dimension "pos" of data->space to "p".
3255 * data->user is assumed to be an isl_multi_val.
3257 * If the current dimension is an output dimension, then print
3258 * the corresponding value. Otherwise, print the name of the dimension.
3260 static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
3261 struct isl_print_space_data *data, unsigned pos)
3263 isl_multi_val *mv = data->user;
3265 if (data->type == isl_dim_out)
3266 return isl_printer_print_val(p, mv->u.p[pos]);
3267 else
3268 return print_name(data->space, p, data->type, pos, data->latex);
3271 /* Print the isl_multi_val "mv" to "p" in isl format.
3273 static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
3274 __isl_keep isl_multi_val *mv)
3276 struct isl_print_space_data data = { 0 };
3278 if (!mv)
3279 return isl_printer_free(p);
3281 p = print_param_tuple(p, mv->space, &data);
3282 p = isl_printer_print_str(p, "{ ");
3283 data.print_dim = &print_dim_mv;
3284 data.user = mv;
3285 p = isl_print_space(mv->space, p, 0, &data);
3286 p = isl_printer_print_str(p, " }");
3287 return p;
3290 /* Print the isl_multi_val "mv" to "p".
3292 * Currently only supported in isl format.
3294 __isl_give isl_printer *isl_printer_print_multi_val(
3295 __isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
3297 if (!p || !mv)
3298 return isl_printer_free(p);
3300 if (p->output_format == ISL_FORMAT_ISL)
3301 return print_multi_val_isl(p, mv);
3302 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3303 return isl_printer_free(p));
3306 /* Print dimension "pos" of data->space to "p".
3308 * data->user is assumed to be an isl_multi_union_pw_aff.
3310 * The current dimension is necessarily a set dimension, so
3311 * we print the corresponding isl_union_pw_aff, including
3312 * the braces.
3314 static __isl_give isl_printer *print_union_pw_aff_dim(__isl_take isl_printer *p,
3315 struct isl_print_space_data *data, unsigned pos)
3317 isl_multi_union_pw_aff *mupa = data->user;
3318 isl_union_pw_aff *upa;
3320 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, pos);
3321 p = print_union_pw_aff_body(p, upa);
3322 isl_union_pw_aff_free(upa);
3324 return p;
3327 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3329 * If "mupa" is zero-dimensional and has a non-trivial explicit domain,
3330 * then it is printed after the tuple of affine expressions.
3331 * In order to clarify that this domain belongs to the expression,
3332 * the tuple along with the domain are placed inside parentheses.
3333 * If "mupa" has any parameters, then the opening parenthesis
3334 * appears after the parameter declarations.
3336 static __isl_give isl_printer *print_multi_union_pw_aff_isl(
3337 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3339 struct isl_print_space_data data = { 0 };
3340 isl_bool has_domain;
3341 isl_space *space;
3343 if (!mupa)
3344 return isl_printer_free(p);
3345 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
3346 if (has_domain < 0)
3347 return isl_printer_free(p);
3349 space = isl_multi_union_pw_aff_get_space(mupa);
3350 p = print_param_tuple(p, space, &data);
3352 if (has_domain)
3353 p = isl_printer_print_str(p, "(");
3355 data.print_dim = &print_union_pw_aff_dim;
3356 data.user = mupa;
3358 p = isl_print_space(space, p, 0, &data);
3359 isl_space_free(space);
3361 if (has_domain) {
3362 p = isl_printer_print_str(p, " : ");
3363 p = isl_printer_print_union_set_isl_body(p, mupa->u.dom);
3364 p = isl_printer_print_str(p, ")");
3367 return p;
3370 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3372 * We currently only support an isl format.
3374 __isl_give isl_printer *isl_printer_print_multi_union_pw_aff(
3375 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3377 if (!p || !mupa)
3378 return isl_printer_free(p);
3380 if (p->output_format == ISL_FORMAT_ISL)
3381 return print_multi_union_pw_aff_isl(p, mupa);
3382 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
3383 "unsupported output format", return isl_printer_free(p));