Merge branch 'maint'
[isl.git] / isl_output.c
blob6dd5890874e7b5e4774c443b8b0e439ea0919640
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_map.h>
27 #include <isl/constraint.h>
28 #include <isl_local_space_private.h>
29 #include <isl_aff_private.h>
30 #include <isl_val_private.h>
31 #include <isl_constraint_private.h>
32 #include <isl/ast_build.h>
33 #include <isl_sort.h>
34 #include <isl_output_private.h>
36 #include <bset_to_bmap.c>
37 #include <set_to_map.c>
39 static const char *s_to[2] = { " -> ", " \\to " };
40 static const char *s_and[2] = { " and ", " \\wedge " };
41 static const char *s_or[2] = { " or ", " \\vee " };
42 static const char *s_le[2] = { "<=", "\\le" };
43 static const char *s_ge[2] = { ">=", "\\ge" };
44 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
45 static const char *s_close_set[2] = { " }", " \\,\\}" };
46 static const char *s_open_list[2] = { "[", "(" };
47 static const char *s_close_list[2] = { "]", ")" };
48 static const char *s_such_that[2] = { " : ", " \\mid " };
49 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
50 static const char *s_close_exists[2] = { ")", "" };
51 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
52 static const char *s_param_prefix[2] = { "p", "p_" };
53 static const char *s_input_prefix[2] = { "i", "i_" };
54 static const char *s_output_prefix[2] = { "o", "o_" };
56 static __isl_give isl_printer *print_constraint_polylib(
57 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
59 int i;
60 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
61 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
62 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
63 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
65 p = isl_printer_start_line(p);
66 p = isl_printer_print_int(p, ineq);
67 for (i = 0; i < n_out; ++i) {
68 p = isl_printer_print_str(p, " ");
69 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
71 for (i = 0; i < n_in; ++i) {
72 p = isl_printer_print_str(p, " ");
73 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
75 for (i = 0; i < bmap->n_div; ++i) {
76 p = isl_printer_print_str(p, " ");
77 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
79 for (i = 0; i < nparam; ++i) {
80 p = isl_printer_print_str(p, " ");
81 p = isl_printer_print_isl_int(p, c[1+i]);
83 p = isl_printer_print_str(p, " ");
84 p = isl_printer_print_isl_int(p, c[0]);
85 p = isl_printer_end_line(p);
86 return p;
89 static __isl_give isl_printer *print_constraints_polylib(
90 struct isl_basic_map *bmap, __isl_take isl_printer *p)
92 int i;
94 p = isl_printer_set_isl_int_width(p, 5);
96 for (i = 0; i < bmap->n_eq; ++i)
97 p = print_constraint_polylib(bmap, 0, i, p);
98 for (i = 0; i < bmap->n_ineq; ++i)
99 p = print_constraint_polylib(bmap, 1, i, p);
101 return p;
104 static __isl_give isl_printer *bset_print_constraints_polylib(
105 struct isl_basic_set *bset, __isl_take isl_printer *p)
107 return print_constraints_polylib(bset_to_bmap(bset), p);
110 static __isl_give isl_printer *isl_basic_map_print_polylib(
111 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
113 unsigned total = isl_basic_map_total_dim(bmap);
114 p = isl_printer_start_line(p);
115 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
116 p = isl_printer_print_str(p, " ");
117 p = isl_printer_print_int(p, 1 + total + 1);
118 if (ext) {
119 p = isl_printer_print_str(p, " ");
120 p = isl_printer_print_int(p,
121 isl_basic_map_dim(bmap, isl_dim_out));
122 p = isl_printer_print_str(p, " ");
123 p = isl_printer_print_int(p,
124 isl_basic_map_dim(bmap, isl_dim_in));
125 p = isl_printer_print_str(p, " ");
126 p = isl_printer_print_int(p,
127 isl_basic_map_dim(bmap, isl_dim_div));
128 p = isl_printer_print_str(p, " ");
129 p = isl_printer_print_int(p,
130 isl_basic_map_dim(bmap, isl_dim_param));
132 p = isl_printer_end_line(p);
133 return print_constraints_polylib(bmap, p);
136 static __isl_give isl_printer *isl_basic_set_print_polylib(
137 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
139 return isl_basic_map_print_polylib(bset_to_bmap(bset), p, ext);
142 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
143 __isl_take isl_printer *p, int ext)
145 int i;
147 p = isl_printer_start_line(p);
148 p = isl_printer_print_int(p, map->n);
149 p = isl_printer_end_line(p);
150 for (i = 0; i < map->n; ++i) {
151 p = isl_printer_start_line(p);
152 p = isl_printer_end_line(p);
153 p = isl_basic_map_print_polylib(map->p[i], p, ext);
155 return p;
158 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
159 __isl_take isl_printer *p, int ext)
161 return isl_map_print_polylib(set_to_map(set), p, ext);
164 static int count_same_name(__isl_keep isl_space *dim,
165 enum isl_dim_type type, unsigned pos, const char *name)
167 enum isl_dim_type t;
168 unsigned p, s;
169 int count = 0;
171 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
172 s = t == type ? pos : isl_space_dim(dim, t);
173 for (p = 0; p < s; ++p) {
174 const char *n = isl_space_get_dim_name(dim, t, p);
175 if (n && !strcmp(n, name))
176 count++;
179 return count;
182 /* Print the name of the variable of type "type" and position "pos"
183 * in "space" to "p".
185 static __isl_give isl_printer *print_name(__isl_keep isl_space *space,
186 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
187 int latex)
189 const char *name;
190 char buffer[20];
191 int primes;
193 name = type == isl_dim_div ? NULL
194 : isl_space_get_dim_name(space, type, pos);
196 if (!name) {
197 const char *prefix;
198 if (type == isl_dim_param)
199 prefix = s_param_prefix[latex];
200 else if (type == isl_dim_div)
201 prefix = s_div_prefix[latex];
202 else if (isl_space_is_set(space) || type == isl_dim_in)
203 prefix = s_input_prefix[latex];
204 else
205 prefix = s_output_prefix[latex];
206 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
207 name = buffer;
209 primes = count_same_name(space, name == buffer ? isl_dim_div : type,
210 pos, name);
211 p = isl_printer_print_str(p, name);
212 while (primes-- > 0)
213 p = isl_printer_print_str(p, "'");
214 return p;
217 static enum isl_dim_type pos2type(__isl_keep isl_space *dim, unsigned *pos)
219 enum isl_dim_type type;
220 unsigned n_in = isl_space_dim(dim, isl_dim_in);
221 unsigned n_out = isl_space_dim(dim, isl_dim_out);
222 unsigned nparam = isl_space_dim(dim, isl_dim_param);
224 if (*pos < 1 + nparam) {
225 type = isl_dim_param;
226 *pos -= 1;
227 } else if (*pos < 1 + nparam + n_in) {
228 type = isl_dim_in;
229 *pos -= 1 + nparam;
230 } else if (*pos < 1 + nparam + n_in + n_out) {
231 type = isl_dim_out;
232 *pos -= 1 + nparam + n_in;
233 } else {
234 type = isl_dim_div;
235 *pos -= 1 + nparam + n_in + n_out;
238 return type;
241 /* Can the div expression of the integer division at position "row" of "div"
242 * be printed?
243 * In particular, are the div expressions available and does the selected
244 * variable have a known explicit representation?
245 * Furthermore, the Omega format does not allow any div expressions
246 * to be printed.
248 static isl_bool can_print_div_expr(__isl_keep isl_printer *p,
249 __isl_keep isl_mat *div, int pos)
251 if (p->output_format == ISL_FORMAT_OMEGA)
252 return isl_bool_false;
253 if (!div)
254 return isl_bool_false;
255 return !isl_int_is_zero(div->row[pos][0]);
258 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
259 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
261 static __isl_give isl_printer *print_term(__isl_keep isl_space *space,
262 __isl_keep isl_mat *div,
263 isl_int c, unsigned pos, __isl_take isl_printer *p, int latex)
265 enum isl_dim_type type;
266 int print_div_def;
268 if (pos == 0)
269 return isl_printer_print_isl_int(p, c);
271 type = pos2type(space, &pos);
272 print_div_def = type == isl_dim_div && can_print_div_expr(p, div, pos);
274 if (isl_int_is_one(c))
276 else if (isl_int_is_negone(c))
277 p = isl_printer_print_str(p, "-");
278 else {
279 p = isl_printer_print_isl_int(p, c);
280 if (p->output_format == ISL_FORMAT_C || print_div_def)
281 p = isl_printer_print_str(p, "*");
283 if (print_div_def)
284 p = print_div(space, div, pos, p);
285 else
286 p = print_name(space, p, type, pos, latex);
287 return p;
290 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *dim,
291 __isl_keep isl_mat *div,
292 __isl_take isl_printer *p, isl_int *c, int len)
294 int i;
295 int first;
297 for (i = 0, first = 1; i < len; ++i) {
298 int flip = 0;
299 if (isl_int_is_zero(c[i]))
300 continue;
301 if (!first) {
302 if (isl_int_is_neg(c[i])) {
303 flip = 1;
304 isl_int_neg(c[i], c[i]);
305 p = isl_printer_print_str(p, " - ");
306 } else
307 p = isl_printer_print_str(p, " + ");
309 first = 0;
310 p = print_term(dim, div, c[i], i, p, 0);
311 if (flip)
312 isl_int_neg(c[i], c[i]);
314 if (first)
315 p = isl_printer_print_str(p, "0");
316 return p;
319 /* Print an affine expression "c" corresponding to a constraint in "bmap"
320 * to "p", with the variable names taken from "space" and
321 * the integer division definitions taken from "div".
323 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
324 __isl_keep isl_space *space, __isl_keep isl_mat *div,
325 __isl_take isl_printer *p, isl_int *c)
327 unsigned len = 1 + isl_basic_map_total_dim(bmap);
328 return print_affine_of_len(space, div, p, c, len);
331 /* offset is the offset of local_dim inside data->type of data->space.
333 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
334 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
335 struct isl_print_space_data *data, int offset)
337 int i;
339 if (data->space != local_dim && local_type == isl_dim_out)
340 offset += local_dim->n_in;
342 for (i = 0; i < isl_space_dim(local_dim, local_type); ++i) {
343 if (i)
344 p = isl_printer_print_str(p, ", ");
345 if (data->print_dim)
346 p = data->print_dim(p, data, offset + i);
347 else
348 p = print_name(data->space, p, data->type, offset + i,
349 data->latex);
351 return p;
354 static __isl_give isl_printer *print_var_list(__isl_take isl_printer *p,
355 __isl_keep isl_space *space, enum isl_dim_type type)
357 struct isl_print_space_data data = { .space = space, .type = type };
359 return print_nested_var_list(p, space, type, &data, 0);
362 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
363 __isl_keep isl_space *local_dim,
364 struct isl_print_space_data *data, int offset);
366 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
367 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
368 struct isl_print_space_data *data, int offset)
370 const char *name = NULL;
371 unsigned n = isl_space_dim(local_dim, local_type);
372 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
373 name = isl_space_get_tuple_name(local_dim, local_type);
374 if (name) {
375 if (data->latex)
376 p = isl_printer_print_str(p, "\\mathrm{");
377 p = isl_printer_print_str(p, name);
378 if (data->latex)
379 p = isl_printer_print_str(p, "}");
382 if (!data->latex || n != 1 || name)
383 p = isl_printer_print_str(p, s_open_list[data->latex]);
384 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
385 local_dim->nested[local_type - isl_dim_in]) {
386 if (data->space != local_dim && local_type == isl_dim_out)
387 offset += local_dim->n_in;
388 p = print_nested_map_dim(p,
389 local_dim->nested[local_type - isl_dim_in],
390 data, offset);
391 } else
392 p = print_nested_var_list(p, local_dim, local_type, data,
393 offset);
394 if (!data->latex || n != 1 || name)
395 p = isl_printer_print_str(p, s_close_list[data->latex]);
396 return p;
399 static __isl_give isl_printer *print_tuple(__isl_keep isl_space *dim,
400 __isl_take isl_printer *p, enum isl_dim_type type,
401 struct isl_print_space_data *data)
403 data->space = dim;
404 data->type = type;
405 return print_nested_tuple(p, dim, type, data, 0);
408 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
409 __isl_keep isl_space *local_dim,
410 struct isl_print_space_data *data, int offset)
412 p = print_nested_tuple(p, local_dim, isl_dim_in, data, offset);
413 p = isl_printer_print_str(p, s_to[data->latex]);
414 p = print_nested_tuple(p, local_dim, isl_dim_out, data, offset);
416 return p;
419 __isl_give isl_printer *isl_print_space(__isl_keep isl_space *space,
420 __isl_take isl_printer *p, int rational,
421 struct isl_print_space_data *data)
423 if (rational && !data->latex)
424 p = isl_printer_print_str(p, "rat: ");
425 if (isl_space_is_params(space))
427 else if (isl_space_is_set(space))
428 p = print_tuple(space, p, isl_dim_set, data);
429 else {
430 p = print_tuple(space, p, isl_dim_in, data);
431 p = isl_printer_print_str(p, s_to[data->latex]);
432 p = print_tuple(space, p, isl_dim_out, data);
435 return p;
438 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_space *dim,
439 __isl_take isl_printer *p)
441 if (isl_space_dim(dim, isl_dim_param) == 0)
442 return p;
444 p = isl_printer_start_line(p);
445 p = isl_printer_print_str(p, "symbolic ");
446 p = print_var_list(p, dim, isl_dim_param);
447 p = isl_printer_print_str(p, ";");
448 p = isl_printer_end_line(p);
449 return p;
452 /* Does the inequality constraint following "i" in "bmap"
453 * have an opposite value for the same last coefficient?
454 * "last" is the position of the last coefficient of inequality "i".
455 * If the next constraint is a div constraint, then it is ignored
456 * since div constraints are not printed.
458 static int next_is_opposite(__isl_keep isl_basic_map *bmap, int i, int last)
460 unsigned total = isl_basic_map_total_dim(bmap);
461 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
463 if (i + 1 >= bmap->n_ineq)
464 return 0;
465 if (isl_seq_last_non_zero(bmap->ineq[i + 1], 1 + total) != last)
466 return 0;
467 if (last >= o_div) {
468 isl_bool is_div;
469 is_div = isl_basic_map_is_div_constraint(bmap,
470 bmap->ineq[i + 1], last - o_div);
471 if (is_div < 0)
472 return -1;
473 if (is_div)
474 return 0;
476 return isl_int_abs_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]) &&
477 !isl_int_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]);
480 /* Return a string representation of the operator used when
481 * printing a constraint where the LHS is greater than or equal to the LHS
482 * (sign > 0) or smaller than or equal to the LHS (sign < 0).
483 * If "strict" is set, then return the strict version of the comparison
484 * operator.
486 static const char *constraint_op(int sign, int strict, int latex)
488 if (strict)
489 return sign < 0 ? "<" : ">";
490 if (sign < 0)
491 return s_le[latex];
492 else
493 return s_ge[latex];
496 /* Print one side of a constraint "c" from "bmap" to "p", with
497 * the variable names taken from "space" and the integer division definitions
498 * taken from "div".
499 * "last" is the position of the last non-zero coefficient.
500 * Let c' be the result of zeroing out this coefficient, then
501 * the partial constraint
503 * c' op
505 * is printed.
506 * "first_constraint" is set if this is the first constraint
507 * in the conjunction.
509 static __isl_give isl_printer *print_half_constraint(struct isl_basic_map *bmap,
510 __isl_keep isl_space *space, __isl_keep isl_mat *div,
511 __isl_take isl_printer *p, isl_int *c, int last, const char *op,
512 int first_constraint, int latex)
514 if (!first_constraint)
515 p = isl_printer_print_str(p, s_and[latex]);
517 isl_int_set_si(c[last], 0);
518 p = print_affine(bmap, space, div, p, c);
520 p = isl_printer_print_str(p, " ");
521 p = isl_printer_print_str(p, op);
522 p = isl_printer_print_str(p, " ");
524 return p;
527 /* Print a constraint "c" from "bmap" to "p", with the variable names
528 * taken from "space" and the integer division definitions taken from "div".
529 * "last" is the position of the last non-zero coefficient, which is
530 * moreover assumed to be negative.
531 * Let c' be the result of zeroing out this coefficient, then
532 * the constraint is printed in the form
534 * -c[last] op c'
536 * "first_constraint" is set if this is the first constraint
537 * in the conjunction.
539 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
540 __isl_keep isl_space *space, __isl_keep isl_mat *div,
541 __isl_take isl_printer *p,
542 isl_int *c, int last, const char *op, int first_constraint, int latex)
544 if (!first_constraint)
545 p = isl_printer_print_str(p, s_and[latex]);
547 isl_int_abs(c[last], c[last]);
549 p = print_term(space, div, c[last], last, p, latex);
551 p = isl_printer_print_str(p, " ");
552 p = isl_printer_print_str(p, op);
553 p = isl_printer_print_str(p, " ");
555 isl_int_set_si(c[last], 0);
556 p = print_affine(bmap, space, div, p, c);
558 return p;
561 /* Print the constraints of "bmap" to "p".
562 * The names of the variables are taken from "space" and
563 * the integer division definitions are taken from "div".
564 * Div constraints are only printed in "dump" mode.
565 * The constraints are sorted prior to printing (except in "dump" mode).
567 * If x is the last variable with a non-zero coefficient,
568 * then a lower bound
570 * f - a x >= 0
572 * is printed as
574 * a x <= f
576 * while an upper bound
578 * f + a x >= 0
580 * is printed as
582 * a x >= -f
584 * If the next constraint has an opposite sign for the same last coefficient,
585 * then it is printed as
587 * f >= a x
589 * or
591 * -f <= a x
593 * instead. In fact, the "a x" part is not printed explicitly, but
594 * reused from the next constraint, which is therefore treated as
595 * a first constraint in the conjunction.
597 * If the constant term of "f" is -1, then "f" is replaced by "f + 1" and
598 * the comparison operator is replaced by the strict variant.
599 * Essentially, ">= 1" is replaced by "> 0".
601 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
602 __isl_keep isl_space *space, __isl_keep isl_mat *div,
603 __isl_take isl_printer *p, int latex)
605 int i;
606 isl_vec *c = NULL;
607 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
608 unsigned total = isl_basic_map_total_dim(bmap);
609 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
610 int first = 1;
611 int dump;
613 if (!p)
614 return NULL;
615 bmap = isl_basic_map_copy(bmap);
616 dump = p->dump;
617 if (!dump)
618 bmap = isl_basic_map_sort_constraints(bmap);
619 if (!bmap)
620 goto error;
622 c = isl_vec_alloc(bmap->ctx, 1 + total);
623 if (!c)
624 goto error;
626 for (i = bmap->n_eq - 1; i >= 0; --i) {
627 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
628 if (l < 0) {
629 if (i != bmap->n_eq - 1)
630 p = isl_printer_print_str(p, s_and[latex]);
631 p = isl_printer_print_str(p, "0 = 0");
632 continue;
634 if (isl_int_is_neg(bmap->eq[i][l]))
635 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
636 else
637 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
638 p = print_constraint(bmap, space, div, p, c->el, l,
639 "=", first, latex);
640 first = 0;
642 for (i = 0; i < bmap->n_ineq; ++i) {
643 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
644 int strict;
645 int s;
646 const char *op;
647 if (l < 0)
648 continue;
649 if (!dump && l >= o_div &&
650 can_print_div_expr(p, div, l - o_div)) {
651 isl_bool is_div;
652 is_div = isl_basic_map_is_div_constraint(bmap,
653 bmap->ineq[i], l - o_div);
654 if (is_div < 0)
655 goto error;
656 if (is_div)
657 continue;
659 s = isl_int_sgn(bmap->ineq[i][l]);
660 strict = !rational && isl_int_is_negone(bmap->ineq[i][0]);
661 if (s < 0)
662 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
663 else
664 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
665 if (strict)
666 isl_int_set_si(c->el[0], 0);
667 if (!dump && next_is_opposite(bmap, i, l)) {
668 op = constraint_op(-s, strict, latex);
669 p = print_half_constraint(bmap, space, div, p, c->el, l,
670 op, first, latex);
671 first = 1;
672 } else {
673 op = constraint_op(s, strict, latex);
674 p = print_constraint(bmap, space, div, p, c->el, l,
675 op, first, latex);
676 first = 0;
680 isl_basic_map_free(bmap);
681 isl_vec_free(c);
683 return p;
684 error:
685 isl_basic_map_free(bmap);
686 isl_vec_free(c);
687 isl_printer_free(p);
688 return NULL;
691 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
692 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
694 int c;
696 if (!p || !div)
697 return isl_printer_free(p);
699 c = p->output_format == ISL_FORMAT_C;
700 p = isl_printer_print_str(p, c ? "floord(" : "floor((");
701 p = print_affine_of_len(dim, div, p,
702 div->row[pos] + 1, div->n_col - 1);
703 p = isl_printer_print_str(p, c ? ", " : ")/");
704 p = isl_printer_print_isl_int(p, div->row[pos][0]);
705 p = isl_printer_print_str(p, ")");
706 return p;
709 /* Print a comma separated list of div names, except those that have
710 * a definition that can be printed.
711 * If "print_defined_divs" is set, then those div names are printed
712 * as well, along with their definitions.
714 static __isl_give isl_printer *print_div_list(__isl_take isl_printer *p,
715 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex,
716 int print_defined_divs)
718 int i;
719 int first = 1;
720 unsigned n_div;
722 if (!p || !space || !div)
723 return isl_printer_free(p);
725 n_div = isl_mat_rows(div);
727 for (i = 0; i < n_div; ++i) {
728 if (!print_defined_divs && can_print_div_expr(p, div, i))
729 continue;
730 if (!first)
731 p = isl_printer_print_str(p, ", ");
732 p = print_name(space, p, isl_dim_div, i, latex);
733 first = 0;
734 if (!can_print_div_expr(p, div, i))
735 continue;
736 p = isl_printer_print_str(p, " = ");
737 p = print_div(space, div, i, p);
740 return p;
743 /* Does printing an object with local variables described by "div"
744 * require an "exists" clause?
745 * That is, are there any local variables without an explicit representation?
746 * An exists clause is also needed in "dump" mode because
747 * explicit div representations are not printed inline in that case.
749 static isl_bool need_exists(__isl_keep isl_printer *p, __isl_keep isl_mat *div)
751 int i, n;
753 if (!p || !div)
754 return isl_bool_error;
755 n = isl_mat_rows(div);
756 if (n == 0)
757 return isl_bool_false;
758 if (p->dump)
759 return isl_bool_true;
760 for (i = 0; i < n; ++i)
761 if (!can_print_div_expr(p, div, i))
762 return isl_bool_true;
763 return isl_bool_false;
766 /* Print the start of an exists clause, i.e.,
768 * (exists variables:
770 * In dump mode, local variables with an explicit definition are printed
771 * as well because they will not be printed inline.
773 static __isl_give isl_printer *open_exists(__isl_take isl_printer *p,
774 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex)
776 int dump;
778 if (!p)
779 return NULL;
781 dump = p->dump;
782 p = isl_printer_print_str(p, s_open_exists[latex]);
783 p = print_div_list(p, space, div, latex, dump);
784 p = isl_printer_print_str(p, ": ");
786 return p;
789 /* Print the constraints of "bmap" to "p".
790 * The names of the variables are taken from "space".
791 * "latex" is set if the constraints should be printed in LaTeX format.
792 * Do not print inline explicit div representations in "dump" mode.
794 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
795 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
797 int dump;
798 isl_mat *div;
799 isl_bool exists;
801 if (!p)
802 return NULL;
803 dump = p->dump;
804 div = isl_basic_map_get_divs(bmap);
805 exists = need_exists(p, div);
806 if (exists >= 0 && exists)
807 p = open_exists(p, space, div, latex);
809 if (dump)
810 div = isl_mat_free(div);
811 p = print_constraints(bmap, space, div, p, latex);
812 isl_mat_free(div);
814 if (exists >= 0 && exists)
815 p = isl_printer_print_str(p, s_close_exists[latex]);
816 return p;
819 /* Print a colon followed by the constraints of "bmap"
820 * to "p", provided there are any constraints.
821 * The names of the variables are taken from "space".
822 * "latex" is set if the constraints should be printed in LaTeX format.
824 static __isl_give isl_printer *print_optional_disjunct(
825 __isl_keep isl_basic_map *bmap, __isl_keep isl_space *space,
826 __isl_take isl_printer *p, int latex)
828 if (isl_basic_map_plain_is_universe(bmap))
829 return p;
831 p = isl_printer_print_str(p, ": ");
832 p = print_disjunct(bmap, space, p, latex);
834 return p;
837 static __isl_give isl_printer *basic_map_print_omega(
838 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
840 p = isl_printer_print_str(p, "{ [");
841 p = print_var_list(p, bmap->dim, isl_dim_in);
842 p = isl_printer_print_str(p, "] -> [");
843 p = print_var_list(p, bmap->dim, isl_dim_out);
844 p = isl_printer_print_str(p, "] ");
845 p = print_optional_disjunct(bmap, bmap->dim, p, 0);
846 p = isl_printer_print_str(p, " }");
847 return p;
850 static __isl_give isl_printer *basic_set_print_omega(
851 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
853 p = isl_printer_print_str(p, "{ [");
854 p = print_var_list(p, bset->dim, isl_dim_set);
855 p = isl_printer_print_str(p, "] ");
856 p = print_optional_disjunct(bset, bset->dim, p, 0);
857 p = isl_printer_print_str(p, " }");
858 return p;
861 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
862 __isl_take isl_printer *p)
864 int i;
866 for (i = 0; i < map->n; ++i) {
867 if (i)
868 p = isl_printer_print_str(p, " union ");
869 p = basic_map_print_omega(map->p[i], p);
871 return p;
874 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
875 __isl_take isl_printer *p)
877 int i;
879 for (i = 0; i < set->n; ++i) {
880 if (i)
881 p = isl_printer_print_str(p, " union ");
882 p = basic_set_print_omega(set->p[i], p);
884 return p;
887 /* Print the list of parameters in "space", followed by an arrow, to "p",
888 * if there are any parameters.
890 static __isl_give isl_printer *print_param_tuple(__isl_take isl_printer *p,
891 __isl_keep isl_space *space, struct isl_print_space_data *data)
893 if (!p || !space)
894 return isl_printer_free(p);
895 if (isl_space_dim(space, isl_dim_param) == 0)
896 return p;
898 p = print_tuple(space, p, isl_dim_param, data);
899 p = isl_printer_print_str(p, s_to[data->latex]);
901 return p;
904 static __isl_give isl_printer *isl_basic_map_print_isl(
905 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
906 int latex)
908 struct isl_print_space_data data = { .latex = latex };
909 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
911 p = print_param_tuple(p, bmap->dim, &data);
912 p = isl_printer_print_str(p, "{ ");
913 p = isl_print_space(bmap->dim, p, rational, &data);
914 p = isl_printer_print_str(p, " : ");
915 p = print_disjunct(bmap, bmap->dim, p, latex);
916 p = isl_printer_print_str(p, " }");
917 return p;
920 /* Print the disjuncts of a map (or set) "map" to "p".
921 * The names of the variables are taken from "space".
922 * "latex" is set if the constraints should be printed in LaTeX format.
924 static __isl_give isl_printer *print_disjuncts_core(__isl_keep isl_map *map,
925 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
927 int i;
929 if (map->n == 0)
930 p = isl_printer_print_str(p, "1 = 0");
931 for (i = 0; i < map->n; ++i) {
932 if (i)
933 p = isl_printer_print_str(p, s_or[latex]);
934 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
935 p = isl_printer_print_str(p, "(");
936 p = print_disjunct(map->p[i], space, p, latex);
937 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
938 p = isl_printer_print_str(p, ")");
940 return p;
943 /* Print the disjuncts of a map (or set) "map" to "p".
944 * The names of the variables are taken from "space".
945 * "hull" describes constraints shared by all disjuncts of "map".
946 * "latex" is set if the constraints should be printed in LaTeX format.
948 * Print the disjuncts as a conjunction of "hull" and
949 * the result of removing the constraints of "hull" from "map".
950 * If this result turns out to be the universe, then simply print "hull".
952 static __isl_give isl_printer *print_disjuncts_in_hull(__isl_keep isl_map *map,
953 __isl_keep isl_space *space, __isl_take isl_basic_map *hull,
954 __isl_take isl_printer *p, int latex)
956 isl_bool is_universe;
958 p = print_disjunct(hull, space, p, latex);
959 map = isl_map_plain_gist_basic_map(isl_map_copy(map), hull);
960 is_universe = isl_map_plain_is_universe(map);
961 if (is_universe < 0)
962 goto error;
963 if (!is_universe) {
964 p = isl_printer_print_str(p, s_and[latex]);
965 p = isl_printer_print_str(p, "(");
966 p = print_disjuncts_core(map, space, p, latex);
967 p = isl_printer_print_str(p, ")");
969 isl_map_free(map);
971 return p;
972 error:
973 isl_map_free(map);
974 isl_printer_free(p);
975 return NULL;
978 /* Print the disjuncts of a map (or set) "map" to "p".
979 * The names of the variables are taken from "space".
980 * "latex" is set if the constraints should be printed in LaTeX format.
982 * If there are at least two disjuncts and "dump" mode is not turned out,
983 * check for any shared constraints among all disjuncts.
984 * If there are any, then print them separately in print_disjuncts_in_hull.
986 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
987 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
989 if (isl_map_plain_is_universe(map))
990 return p;
992 p = isl_printer_print_str(p, s_such_that[latex]);
993 if (!p)
994 return NULL;
996 if (!p->dump && map->n >= 2) {
997 isl_basic_map *hull;
998 isl_bool is_universe;
1000 hull = isl_map_plain_unshifted_simple_hull(isl_map_copy(map));
1001 is_universe = isl_basic_map_plain_is_universe(hull);
1002 if (is_universe < 0)
1003 p = isl_printer_free(p);
1004 else if (!is_universe)
1005 return print_disjuncts_in_hull(map, space, hull,
1006 p, latex);
1007 isl_basic_map_free(hull);
1010 return print_disjuncts_core(map, space, p, latex);
1013 /* Print the disjuncts of a map (or set).
1014 * The names of the variables are taken from "space".
1015 * "latex" is set if the constraints should be printed in LaTeX format.
1017 * If the map turns out to be a universal parameter domain, then
1018 * we need to print the colon. Otherwise, the output looks identical
1019 * to the empty set.
1021 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
1022 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1024 if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
1025 return isl_printer_print_str(p, s_such_that[latex]);
1026 else
1027 return print_disjuncts(map, space, p, latex);
1030 struct isl_aff_split {
1031 isl_basic_map *aff;
1032 isl_map *map;
1035 static void free_split(__isl_take struct isl_aff_split *split, int n)
1037 int i;
1039 if (!split)
1040 return;
1042 for (i = 0; i < n; ++i) {
1043 isl_basic_map_free(split[i].aff);
1044 isl_map_free(split[i].map);
1047 free(split);
1050 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
1052 int i, j;
1053 unsigned nparam, n_in, n_out, total;
1055 bmap = isl_basic_map_cow(bmap);
1056 if (!bmap)
1057 return NULL;
1058 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
1059 goto error;
1061 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1062 n_in = isl_basic_map_dim(bmap, isl_dim_in);
1063 n_out = isl_basic_map_dim(bmap, isl_dim_out);
1064 total = isl_basic_map_dim(bmap, isl_dim_all);
1065 for (i = bmap->n_eq - 1; i >= 0; --i) {
1066 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
1067 if (j >= nparam && j < nparam + n_in + n_out &&
1068 (isl_int_is_one(bmap->eq[i][1 + j]) ||
1069 isl_int_is_negone(bmap->eq[i][1 + j])))
1070 continue;
1071 if (isl_basic_map_drop_equality(bmap, i) < 0)
1072 goto error;
1075 bmap = isl_basic_map_finalize(bmap);
1077 return bmap;
1078 error:
1079 isl_basic_map_free(bmap);
1080 return NULL;
1083 static int aff_split_cmp(const void *p1, const void *p2, void *user)
1085 const struct isl_aff_split *s1, *s2;
1086 s1 = (const struct isl_aff_split *) p1;
1087 s2 = (const struct isl_aff_split *) p2;
1089 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
1092 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
1093 __isl_keep isl_basic_map *aff)
1095 int i, j;
1096 unsigned total;
1098 if (!bmap || !aff)
1099 goto error;
1101 total = isl_space_dim(bmap->dim, isl_dim_all);
1103 for (i = bmap->n_eq - 1; i >= 0; --i) {
1104 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
1105 bmap->n_div) != -1)
1106 continue;
1107 for (j = 0; j < aff->n_eq; ++j) {
1108 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
1109 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
1110 continue;
1111 if (isl_basic_map_drop_equality(bmap, i) < 0)
1112 goto error;
1113 break;
1117 return bmap;
1118 error:
1119 isl_basic_map_free(bmap);
1120 return NULL;
1123 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
1125 int i, n;
1126 struct isl_aff_split *split;
1127 isl_ctx *ctx;
1129 ctx = isl_map_get_ctx(map);
1130 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
1131 if (!split)
1132 return NULL;
1134 for (i = 0; i < map->n; ++i) {
1135 isl_basic_map *bmap;
1136 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
1137 bmap = isl_basic_map_copy(map->p[i]);
1138 bmap = isl_basic_map_cow(bmap);
1139 bmap = drop_aff(bmap, split[i].aff);
1140 split[i].map = isl_map_from_basic_map(bmap);
1141 if (!split[i].aff || !split[i].map)
1142 goto error;
1145 if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
1146 &aff_split_cmp, NULL) < 0)
1147 goto error;
1149 n = map->n;
1150 for (i = n - 1; i >= 1; --i) {
1151 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
1152 split[i].aff))
1153 continue;
1154 isl_basic_map_free(split[i].aff);
1155 split[i - 1].map = isl_map_union(split[i - 1].map,
1156 split[i].map);
1157 if (i != n - 1)
1158 split[i] = split[n - 1];
1159 split[n - 1].aff = NULL;
1160 split[n - 1].map = NULL;
1161 --n;
1164 return split;
1165 error:
1166 free_split(split, map->n);
1167 return NULL;
1170 static int defining_equality(__isl_keep isl_basic_map *eq,
1171 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
1173 int i;
1174 unsigned total;
1176 if (!eq)
1177 return -1;
1179 pos += isl_space_offset(dim, type);
1180 total = isl_basic_map_total_dim(eq);
1182 for (i = 0; i < eq->n_eq; ++i) {
1183 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
1184 continue;
1185 if (isl_int_is_one(eq->eq[i][1 + pos]))
1186 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
1187 return i;
1190 return -1;
1193 /* Print dimension "pos" of data->space to "p".
1195 * data->user is assumed to be an isl_basic_map keeping track of equalities.
1197 * If the current dimension is defined by these equalities, then print
1198 * the corresponding expression, assigned to the name of the dimension
1199 * if there is any. Otherwise, print the name of the dimension.
1201 static __isl_give isl_printer *print_dim_eq(__isl_take isl_printer *p,
1202 struct isl_print_space_data *data, unsigned pos)
1204 isl_basic_map *eq = data->user;
1205 int j;
1207 j = defining_equality(eq, data->space, data->type, pos);
1208 if (j >= 0) {
1209 if (isl_space_has_dim_name(data->space, data->type, pos)) {
1210 p = print_name(data->space, p, data->type, pos,
1211 data->latex);
1212 p = isl_printer_print_str(p, " = ");
1214 pos += 1 + isl_space_offset(data->space, data->type);
1215 p = print_affine_of_len(data->space, NULL, p, eq->eq[j], pos);
1216 } else {
1217 p = print_name(data->space, p, data->type, pos, data->latex);
1220 return p;
1223 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
1224 struct isl_aff_split *split, int n, __isl_keep isl_space *space)
1226 struct isl_print_space_data data = { 0 };
1227 int i;
1228 int rational;
1230 data.print_dim = &print_dim_eq;
1231 for (i = 0; i < n; ++i) {
1232 if (!split[i].map)
1233 break;
1234 rational = split[i].map->n > 0 &&
1235 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
1236 if (i)
1237 p = isl_printer_print_str(p, "; ");
1238 data.user = split[i].aff;
1239 p = isl_print_space(space, p, rational, &data);
1240 p = print_disjuncts_map(split[i].map, space, p, 0);
1243 return p;
1246 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
1247 __isl_take isl_printer *p)
1249 struct isl_print_space_data data = { 0 };
1250 struct isl_aff_split *split = NULL;
1251 int rational;
1253 if (!p || !map)
1254 return isl_printer_free(p);
1255 if (!p->dump && map->n > 0)
1256 split = split_aff(map);
1257 if (split) {
1258 p = print_split_map(p, split, map->n, map->dim);
1259 } else {
1260 rational = map->n > 0 &&
1261 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
1262 p = isl_print_space(map->dim, p, rational, &data);
1263 p = print_disjuncts_map(map, map->dim, p, 0);
1265 free_split(split, map->n);
1266 return p;
1269 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
1270 __isl_take isl_printer *p)
1272 struct isl_print_space_data data = { 0 };
1274 p = print_param_tuple(p, map->dim, &data);
1275 p = isl_printer_print_str(p, s_open_set[0]);
1276 p = isl_map_print_isl_body(map, p);
1277 p = isl_printer_print_str(p, s_close_set[0]);
1278 return p;
1281 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
1282 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
1284 struct isl_print_space_data data = { 0 };
1286 data.latex = 1;
1287 p = print_param_tuple(p, map->dim, &data);
1288 p = isl_printer_print_str(p, s_open_set[1]);
1289 data.print_dim = &print_dim_eq;
1290 data.user = aff;
1291 p = isl_print_space(map->dim, p, 0, &data);
1292 p = print_disjuncts_map(map, map->dim, p, 1);
1293 p = isl_printer_print_str(p, s_close_set[1]);
1295 return p;
1298 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
1299 __isl_take isl_printer *p)
1301 int i;
1302 struct isl_aff_split *split = NULL;
1304 if (map->n > 0)
1305 split = split_aff(map);
1307 if (!split)
1308 return print_latex_map(map, p, NULL);
1310 for (i = 0; i < map->n; ++i) {
1311 if (!split[i].map)
1312 break;
1313 if (i)
1314 p = isl_printer_print_str(p, " \\cup ");
1315 p = print_latex_map(split[i].map, p, split[i].aff);
1318 free_split(split, map->n);
1319 return p;
1322 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
1323 __isl_keep isl_basic_map *bmap)
1325 if (!p || !bmap)
1326 goto error;
1327 if (p->output_format == ISL_FORMAT_ISL)
1328 return isl_basic_map_print_isl(bmap, p, 0);
1329 else if (p->output_format == ISL_FORMAT_OMEGA)
1330 return basic_map_print_omega(bmap, p);
1331 isl_assert(bmap->ctx, 0, goto error);
1332 error:
1333 isl_printer_free(p);
1334 return NULL;
1337 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
1338 __isl_keep isl_basic_set *bset)
1340 if (!p || !bset)
1341 goto error;
1343 if (p->output_format == ISL_FORMAT_ISL)
1344 return isl_basic_map_print_isl(bset, p, 0);
1345 else if (p->output_format == ISL_FORMAT_POLYLIB)
1346 return isl_basic_set_print_polylib(bset, p, 0);
1347 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1348 return isl_basic_set_print_polylib(bset, p, 1);
1349 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
1350 return bset_print_constraints_polylib(bset, p);
1351 else if (p->output_format == ISL_FORMAT_OMEGA)
1352 return basic_set_print_omega(bset, p);
1353 isl_assert(p->ctx, 0, goto error);
1354 error:
1355 isl_printer_free(p);
1356 return NULL;
1359 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1360 __isl_keep isl_set *set)
1362 if (!p || !set)
1363 goto error;
1364 if (p->output_format == ISL_FORMAT_ISL)
1365 return isl_map_print_isl(set_to_map(set), p);
1366 else if (p->output_format == ISL_FORMAT_POLYLIB)
1367 return isl_set_print_polylib(set, p, 0);
1368 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1369 return isl_set_print_polylib(set, p, 1);
1370 else if (p->output_format == ISL_FORMAT_OMEGA)
1371 return isl_set_print_omega(set, p);
1372 else if (p->output_format == ISL_FORMAT_LATEX)
1373 return isl_map_print_latex(set_to_map(set), p);
1374 isl_assert(set->ctx, 0, goto error);
1375 error:
1376 isl_printer_free(p);
1377 return NULL;
1380 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1381 __isl_keep isl_map *map)
1383 if (!p || !map)
1384 goto error;
1386 if (p->output_format == ISL_FORMAT_ISL)
1387 return isl_map_print_isl(map, p);
1388 else if (p->output_format == ISL_FORMAT_POLYLIB)
1389 return isl_map_print_polylib(map, p, 0);
1390 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1391 return isl_map_print_polylib(map, p, 1);
1392 else if (p->output_format == ISL_FORMAT_OMEGA)
1393 return isl_map_print_omega(map, p);
1394 else if (p->output_format == ISL_FORMAT_LATEX)
1395 return isl_map_print_latex(map, p);
1396 isl_assert(map->ctx, 0, goto error);
1397 error:
1398 isl_printer_free(p);
1399 return NULL;
1402 struct isl_union_print_data {
1403 isl_printer *p;
1404 int first;
1407 static isl_stat print_map_body(__isl_take isl_map *map, void *user)
1409 struct isl_union_print_data *data;
1410 data = (struct isl_union_print_data *)user;
1412 if (!data->first)
1413 data->p = isl_printer_print_str(data->p, "; ");
1414 data->first = 0;
1416 data->p = isl_map_print_isl_body(map, data->p);
1417 isl_map_free(map);
1419 return isl_stat_ok;
1422 static __isl_give isl_printer *isl_union_map_print_isl(
1423 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1425 struct isl_union_print_data data;
1426 struct isl_print_space_data space_data = { 0 };
1427 isl_space *space;
1429 space = isl_union_map_get_space(umap);
1430 p = print_param_tuple(p, space, &space_data);
1431 isl_space_free(space);
1432 p = isl_printer_print_str(p, s_open_set[0]);
1433 data.p = p;
1434 data.first = 1;
1435 isl_union_map_foreach_map(umap, &print_map_body, &data);
1436 p = data.p;
1437 p = isl_printer_print_str(p, s_close_set[0]);
1438 return p;
1441 static isl_stat print_latex_map_body(__isl_take isl_map *map, void *user)
1443 struct isl_union_print_data *data;
1444 data = (struct isl_union_print_data *)user;
1446 if (!data->first)
1447 data->p = isl_printer_print_str(data->p, " \\cup ");
1448 data->first = 0;
1450 data->p = isl_map_print_latex(map, data->p);
1451 isl_map_free(map);
1453 return isl_stat_ok;
1456 static __isl_give isl_printer *isl_union_map_print_latex(
1457 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1459 struct isl_union_print_data data = { p, 1 };
1460 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1461 p = data.p;
1462 return p;
1465 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1466 __isl_keep isl_union_map *umap)
1468 if (!p || !umap)
1469 goto error;
1471 if (p->output_format == ISL_FORMAT_ISL)
1472 return isl_union_map_print_isl(umap, p);
1473 if (p->output_format == ISL_FORMAT_LATEX)
1474 return isl_union_map_print_latex(umap, p);
1476 isl_die(p->ctx, isl_error_invalid,
1477 "invalid output format for isl_union_map", goto error);
1478 error:
1479 isl_printer_free(p);
1480 return NULL;
1483 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1484 __isl_keep isl_union_set *uset)
1486 if (!p || !uset)
1487 goto error;
1489 if (p->output_format == ISL_FORMAT_ISL)
1490 return isl_union_map_print_isl((isl_union_map *)uset, p);
1491 if (p->output_format == ISL_FORMAT_LATEX)
1492 return isl_union_map_print_latex((isl_union_map *)uset, p);
1494 isl_die(p->ctx, isl_error_invalid,
1495 "invalid output format for isl_union_set", goto error);
1496 error:
1497 isl_printer_free(p);
1498 return NULL;
1501 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1503 int i;
1504 int n;
1506 for (i = 0, n = 0; i < rec->n; ++i)
1507 if (!isl_upoly_is_zero(rec->p[i]))
1508 ++n;
1510 return n;
1513 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1514 __isl_take isl_printer *p, int first)
1516 struct isl_upoly_cst *cst;
1517 int neg;
1519 cst = isl_upoly_as_cst(up);
1520 if (!cst)
1521 goto error;
1522 neg = !first && isl_int_is_neg(cst->n);
1523 if (!first)
1524 p = isl_printer_print_str(p, neg ? " - " : " + ");
1525 if (neg)
1526 isl_int_neg(cst->n, cst->n);
1527 if (isl_int_is_zero(cst->d)) {
1528 int sgn = isl_int_sgn(cst->n);
1529 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1530 sgn == 0 ? "NaN" : "infty");
1531 } else
1532 p = isl_printer_print_isl_int(p, cst->n);
1533 if (neg)
1534 isl_int_neg(cst->n, cst->n);
1535 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1536 p = isl_printer_print_str(p, "/");
1537 p = isl_printer_print_isl_int(p, cst->d);
1539 return p;
1540 error:
1541 isl_printer_free(p);
1542 return NULL;
1545 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1546 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var)
1548 unsigned total;
1550 total = isl_space_dim(dim, isl_dim_all);
1551 if (var < total)
1552 p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 0);
1553 else
1554 p = print_div(dim, div, var - total, p);
1555 return p;
1558 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1559 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1561 p = print_base(p, dim, div, var);
1562 if (exp == 1)
1563 return p;
1564 if (p->output_format == ISL_FORMAT_C) {
1565 int i;
1566 for (i = 1; i < exp; ++i) {
1567 p = isl_printer_print_str(p, "*");
1568 p = print_base(p, dim, div, var);
1570 } else {
1571 p = isl_printer_print_str(p, "^");
1572 p = isl_printer_print_int(p, exp);
1574 return p;
1577 /* Print the polynomial "up" defined over the domain space "space" and
1578 * local variables defined by "div" to "p".
1579 * If "outer" is set, then "up" is not nested inside another polynomial.
1581 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1582 __isl_keep isl_space *space, __isl_keep isl_mat *div,
1583 __isl_take isl_printer *p, int outer)
1585 int i, n, first, print_parens;
1586 struct isl_upoly_rec *rec;
1588 if (!p || !up || !space || !div)
1589 goto error;
1591 if (isl_upoly_is_cst(up))
1592 return upoly_print_cst(up, p, 1);
1594 rec = isl_upoly_as_rec(up);
1595 if (!rec)
1596 goto error;
1597 n = upoly_rec_n_non_zero(rec);
1598 print_parens = n > 1 ||
1599 (outer && rec->up.var >= isl_space_dim(space, isl_dim_all));
1600 if (print_parens)
1601 p = isl_printer_print_str(p, "(");
1602 for (i = 0, first = 1; i < rec->n; ++i) {
1603 if (isl_upoly_is_zero(rec->p[i]))
1604 continue;
1605 if (isl_upoly_is_negone(rec->p[i])) {
1606 if (!i)
1607 p = isl_printer_print_str(p, "-1");
1608 else if (first)
1609 p = isl_printer_print_str(p, "-");
1610 else
1611 p = isl_printer_print_str(p, " - ");
1612 } else if (isl_upoly_is_cst(rec->p[i]) &&
1613 !isl_upoly_is_one(rec->p[i]))
1614 p = upoly_print_cst(rec->p[i], p, first);
1615 else {
1616 if (!first)
1617 p = isl_printer_print_str(p, " + ");
1618 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1619 p = upoly_print(rec->p[i], space, div, p, 0);
1621 first = 0;
1622 if (i == 0)
1623 continue;
1624 if (!isl_upoly_is_one(rec->p[i]) &&
1625 !isl_upoly_is_negone(rec->p[i]))
1626 p = isl_printer_print_str(p, " * ");
1627 p = print_pow(p, space, div, rec->up.var, i);
1629 if (print_parens)
1630 p = isl_printer_print_str(p, ")");
1631 return p;
1632 error:
1633 isl_printer_free(p);
1634 return NULL;
1637 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1638 __isl_keep isl_qpolynomial *qp)
1640 if (!p || !qp)
1641 goto error;
1642 p = upoly_print(qp->upoly, qp->dim, qp->div, p, 1);
1643 return p;
1644 error:
1645 isl_printer_free(p);
1646 return NULL;
1649 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1650 __isl_keep isl_qpolynomial *qp)
1652 struct isl_print_space_data data = { 0 };
1654 if (!p || !qp)
1655 goto error;
1657 p = print_param_tuple(p, qp->dim, &data);
1658 p = isl_printer_print_str(p, "{ ");
1659 if (!isl_space_is_params(qp->dim)) {
1660 p = isl_print_space(qp->dim, p, 0, &data);
1661 p = isl_printer_print_str(p, " -> ");
1663 p = print_qpolynomial(p, qp);
1664 p = isl_printer_print_str(p, " }");
1665 return p;
1666 error:
1667 isl_printer_free(p);
1668 return NULL;
1671 /* Print the quasi-polynomial "qp" to "p" in C format, with the variable names
1672 * taken from the domain space "space".
1674 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1675 __isl_keep isl_space *space, __isl_keep isl_qpolynomial *qp)
1677 isl_int den;
1679 isl_int_init(den);
1680 isl_qpolynomial_get_den(qp, &den);
1681 if (!isl_int_is_one(den)) {
1682 isl_qpolynomial *f;
1683 p = isl_printer_print_str(p, "(");
1684 qp = isl_qpolynomial_copy(qp);
1685 f = isl_qpolynomial_rat_cst_on_domain(isl_space_copy(qp->dim),
1686 den, qp->dim->ctx->one);
1687 qp = isl_qpolynomial_mul(qp, f);
1689 if (qp)
1690 p = upoly_print(qp->upoly, space, qp->div, p, 0);
1691 else
1692 p = isl_printer_free(p);
1693 if (!isl_int_is_one(den)) {
1694 p = isl_printer_print_str(p, ")/");
1695 p = isl_printer_print_isl_int(p, den);
1696 isl_qpolynomial_free(qp);
1698 isl_int_clear(den);
1699 return p;
1702 __isl_give isl_printer *isl_printer_print_qpolynomial(
1703 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1705 if (!p || !qp)
1706 goto error;
1708 if (p->output_format == ISL_FORMAT_ISL)
1709 return print_qpolynomial_isl(p, qp);
1710 else if (p->output_format == ISL_FORMAT_C)
1711 return print_qpolynomial_c(p, qp->dim, qp);
1712 else
1713 isl_die(qp->dim->ctx, isl_error_unsupported,
1714 "output format not supported for isl_qpolynomials",
1715 goto error);
1716 error:
1717 isl_printer_free(p);
1718 return NULL;
1721 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1722 unsigned output_format)
1724 isl_printer *p;
1726 if (!qp)
1727 return;
1729 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1730 p = isl_printer_to_file(qp->dim->ctx, out);
1731 p = isl_printer_print_qpolynomial(p, qp);
1732 isl_printer_free(p);
1735 static __isl_give isl_printer *qpolynomial_fold_print(
1736 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1738 int i;
1740 if (fold->type == isl_fold_min)
1741 p = isl_printer_print_str(p, "min");
1742 else if (fold->type == isl_fold_max)
1743 p = isl_printer_print_str(p, "max");
1744 p = isl_printer_print_str(p, "(");
1745 for (i = 0; i < fold->n; ++i) {
1746 if (i)
1747 p = isl_printer_print_str(p, ", ");
1748 p = print_qpolynomial(p, fold->qp[i]);
1750 p = isl_printer_print_str(p, ")");
1751 return p;
1754 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1755 FILE *out, unsigned output_format)
1757 isl_printer *p;
1759 if (!fold)
1760 return;
1762 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1764 p = isl_printer_to_file(fold->dim->ctx, out);
1765 p = isl_printer_print_qpolynomial_fold(p, fold);
1767 isl_printer_free(p);
1770 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1771 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1773 struct isl_print_space_data data = { 0 };
1774 int i = 0;
1776 for (i = 0; i < pwqp->n; ++i) {
1777 isl_space *space;
1779 if (i)
1780 p = isl_printer_print_str(p, "; ");
1781 space = isl_qpolynomial_get_domain_space(pwqp->p[i].qp);
1782 if (!isl_space_is_params(space)) {
1783 p = isl_print_space(space, p, 0, &data);
1784 p = isl_printer_print_str(p, " -> ");
1786 p = print_qpolynomial(p, pwqp->p[i].qp);
1787 p = print_disjuncts(set_to_map(pwqp->p[i].set), space, p, 0);
1788 isl_space_free(space);
1791 return p;
1794 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1795 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1797 struct isl_print_space_data data = { 0 };
1799 if (!p || !pwqp)
1800 goto error;
1802 p = print_param_tuple(p, pwqp->dim, &data);
1803 p = isl_printer_print_str(p, "{ ");
1804 if (pwqp->n == 0) {
1805 if (!isl_space_is_set(pwqp->dim)) {
1806 p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
1807 p = isl_printer_print_str(p, " -> ");
1809 p = isl_printer_print_str(p, "0");
1811 p = isl_pwqp_print_isl_body(p, pwqp);
1812 p = isl_printer_print_str(p, " }");
1813 return p;
1814 error:
1815 isl_printer_free(p);
1816 return NULL;
1819 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1820 unsigned output_format)
1822 isl_printer *p;
1824 if (!pwqp)
1825 return;
1827 p = isl_printer_to_file(pwqp->dim->ctx, out);
1828 p = isl_printer_set_output_format(p, output_format);
1829 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1831 isl_printer_free(p);
1834 static __isl_give isl_printer *isl_pwf_print_isl_body(
1835 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1837 struct isl_print_space_data data = { 0 };
1838 int i = 0;
1840 for (i = 0; i < pwf->n; ++i) {
1841 isl_space *space;
1843 if (i)
1844 p = isl_printer_print_str(p, "; ");
1845 space = isl_qpolynomial_fold_get_domain_space(pwf->p[i].fold);
1846 if (!isl_space_is_params(space)) {
1847 p = isl_print_space(space, p, 0, &data);
1848 p = isl_printer_print_str(p, " -> ");
1850 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1851 p = print_disjuncts(set_to_map(pwf->p[i].set), space, p, 0);
1852 isl_space_free(space);
1855 return p;
1858 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1859 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1861 struct isl_print_space_data data = { 0 };
1863 p = print_param_tuple(p, pwf->dim, &data);
1864 p = isl_printer_print_str(p, "{ ");
1865 if (pwf->n == 0) {
1866 if (!isl_space_is_set(pwf->dim)) {
1867 p = print_tuple(pwf->dim, p, isl_dim_in, &data);
1868 p = isl_printer_print_str(p, " -> ");
1870 p = isl_printer_print_str(p, "0");
1872 p = isl_pwf_print_isl_body(p, pwf);
1873 p = isl_printer_print_str(p, " }");
1874 return p;
1877 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1878 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c);
1880 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1881 __isl_keep isl_space *dim,
1882 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1884 if (type == isl_dim_div) {
1885 p = isl_printer_print_str(p, "floord(");
1886 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
1887 p = isl_printer_print_str(p, ", ");
1888 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1889 p = isl_printer_print_str(p, ")");
1890 } else {
1891 const char *name;
1893 name = isl_space_get_dim_name(dim, type, pos);
1894 if (!name)
1895 name = "UNNAMED";
1896 p = isl_printer_print_str(p, name);
1898 return p;
1901 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1902 __isl_keep isl_space *dim,
1903 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1905 enum isl_dim_type type;
1907 if (pos == 0)
1908 return isl_printer_print_isl_int(p, c);
1910 if (isl_int_is_one(c))
1912 else if (isl_int_is_negone(c))
1913 p = isl_printer_print_str(p, "-");
1914 else {
1915 p = isl_printer_print_isl_int(p, c);
1916 p = isl_printer_print_str(p, "*");
1918 type = pos2type(dim, &pos);
1919 p = print_name_c(p, dim, bset, type, pos);
1920 return p;
1923 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
1924 __isl_keep isl_space *dim,
1925 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
1927 int i;
1928 int first;
1930 for (i = 0, first = 1; i < len; ++i) {
1931 int flip = 0;
1932 if (isl_int_is_zero(c[i]))
1933 continue;
1934 if (!first) {
1935 if (isl_int_is_neg(c[i])) {
1936 flip = 1;
1937 isl_int_neg(c[i], c[i]);
1938 p = isl_printer_print_str(p, " - ");
1939 } else
1940 p = isl_printer_print_str(p, " + ");
1942 first = 0;
1943 p = print_term_c(p, dim, bset, c[i], i);
1944 if (flip)
1945 isl_int_neg(c[i], c[i]);
1947 if (first)
1948 p = isl_printer_print_str(p, "0");
1949 return p;
1952 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1953 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c)
1955 unsigned len = 1 + isl_basic_set_total_dim(bset);
1956 return print_partial_affine_c(p, dim, bset, c, len);
1959 /* We skip the constraint if it is implied by the div expression.
1961 * *first indicates whether this is the first constraint in the conjunction and
1962 * is updated if the constraint is actually printed.
1964 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1965 __isl_keep isl_space *dim,
1966 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int *first)
1968 unsigned o_div;
1969 unsigned n_div;
1970 int div;
1972 o_div = isl_basic_set_offset(bset, isl_dim_div);
1973 n_div = isl_basic_set_dim(bset, isl_dim_div);
1974 div = isl_seq_last_non_zero(c + o_div, n_div);
1975 if (div >= 0) {
1976 isl_bool is_div = isl_basic_set_is_div_constraint(bset, c, div);
1977 if (is_div < 0)
1978 return isl_printer_free(p);
1979 if (is_div)
1980 return p;
1983 if (!*first)
1984 p = isl_printer_print_str(p, " && ");
1986 p = print_affine_c(p, dim, bset, c);
1987 p = isl_printer_print_str(p, " ");
1988 p = isl_printer_print_str(p, op);
1989 p = isl_printer_print_str(p, " 0");
1991 *first = 0;
1993 return p;
1996 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1997 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset)
1999 int i, j;
2000 int first = 1;
2001 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
2002 unsigned total = isl_basic_set_total_dim(bset) - n_div;
2004 for (i = 0; i < bset->n_eq; ++i) {
2005 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
2006 if (j < 0)
2007 p = print_constraint_c(p, dim, bset,
2008 bset->eq[i], "==", &first);
2009 else {
2010 if (i)
2011 p = isl_printer_print_str(p, " && ");
2012 p = isl_printer_print_str(p, "(");
2013 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
2014 1 + total + j);
2015 p = isl_printer_print_str(p, ") % ");
2016 p = isl_printer_print_isl_int(p,
2017 bset->eq[i][1 + total + j]);
2018 p = isl_printer_print_str(p, " == 0");
2019 first = 0;
2022 for (i = 0; i < bset->n_ineq; ++i)
2023 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
2024 &first);
2025 return p;
2028 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
2029 __isl_keep isl_space *dim, __isl_keep isl_set *set)
2031 int i;
2033 if (!set)
2034 return isl_printer_free(p);
2036 if (set->n == 0)
2037 p = isl_printer_print_str(p, "0");
2039 for (i = 0; i < set->n; ++i) {
2040 if (i)
2041 p = isl_printer_print_str(p, " || ");
2042 if (set->n > 1)
2043 p = isl_printer_print_str(p, "(");
2044 p = print_basic_set_c(p, dim, set->p[i]);
2045 if (set->n > 1)
2046 p = isl_printer_print_str(p, ")");
2048 return p;
2051 /* Print the piecewise quasi-polynomial "pwqp" to "p" in C format.
2053 static __isl_give isl_printer *print_pw_qpolynomial_c(
2054 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2056 int i;
2057 isl_space *space;
2059 space = isl_pw_qpolynomial_get_domain_space(pwqp);
2060 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set)) {
2061 p = print_qpolynomial_c(p, space, pwqp->p[0].qp);
2062 isl_space_free(space);
2063 return p;
2066 for (i = 0; i < pwqp->n; ++i) {
2067 p = isl_printer_print_str(p, "(");
2068 p = print_set_c(p, space, pwqp->p[i].set);
2069 p = isl_printer_print_str(p, ") ? (");
2070 p = print_qpolynomial_c(p, space, pwqp->p[i].qp);
2071 p = isl_printer_print_str(p, ") : ");
2074 isl_space_free(space);
2075 p = isl_printer_print_str(p, "0");
2076 return p;
2079 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
2080 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2082 if (!p || !pwqp)
2083 goto error;
2085 if (p->output_format == ISL_FORMAT_ISL)
2086 return print_pw_qpolynomial_isl(p, pwqp);
2087 else if (p->output_format == ISL_FORMAT_C)
2088 return print_pw_qpolynomial_c(p, pwqp);
2089 isl_assert(p->ctx, 0, goto error);
2090 error:
2091 isl_printer_free(p);
2092 return NULL;
2095 static isl_stat print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
2097 struct isl_union_print_data *data;
2098 data = (struct isl_union_print_data *)user;
2100 if (!data->first)
2101 data->p = isl_printer_print_str(data->p, "; ");
2102 data->first = 0;
2104 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
2105 isl_pw_qpolynomial_free(pwqp);
2107 return isl_stat_ok;
2110 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
2111 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2113 struct isl_union_print_data data;
2114 struct isl_print_space_data space_data = { 0 };
2115 isl_space *space;
2117 space = isl_union_pw_qpolynomial_get_space(upwqp);
2118 p = print_param_tuple(p, space, &space_data);
2119 isl_space_free(space);
2120 p = isl_printer_print_str(p, "{ ");
2121 data.p = p;
2122 data.first = 1;
2123 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
2124 &data);
2125 p = data.p;
2126 p = isl_printer_print_str(p, " }");
2127 return p;
2130 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
2131 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2133 if (!p || !upwqp)
2134 goto error;
2136 if (p->output_format == ISL_FORMAT_ISL)
2137 return print_union_pw_qpolynomial_isl(p, upwqp);
2138 isl_die(p->ctx, isl_error_invalid,
2139 "invalid output format for isl_union_pw_qpolynomial",
2140 goto error);
2141 error:
2142 isl_printer_free(p);
2143 return NULL;
2146 /* Print the quasi-polynomial reduction "fold" to "p" in C format,
2147 * with the variable names taken from the domain space "space".
2149 static __isl_give isl_printer *print_qpolynomial_fold_c(
2150 __isl_take isl_printer *p, __isl_keep isl_space *space,
2151 __isl_keep isl_qpolynomial_fold *fold)
2153 int i;
2155 for (i = 0; i < fold->n - 1; ++i)
2156 if (fold->type == isl_fold_min)
2157 p = isl_printer_print_str(p, "min(");
2158 else if (fold->type == isl_fold_max)
2159 p = isl_printer_print_str(p, "max(");
2161 for (i = 0; i < fold->n; ++i) {
2162 if (i)
2163 p = isl_printer_print_str(p, ", ");
2164 p = print_qpolynomial_c(p, space, fold->qp[i]);
2165 if (i)
2166 p = isl_printer_print_str(p, ")");
2168 return p;
2171 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
2172 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
2174 if (!p || !fold)
2175 goto error;
2176 if (p->output_format == ISL_FORMAT_ISL)
2177 return qpolynomial_fold_print(fold, p);
2178 else if (p->output_format == ISL_FORMAT_C)
2179 return print_qpolynomial_fold_c(p, fold->dim, fold);
2180 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2181 goto error);
2182 error:
2183 isl_printer_free(p);
2184 return NULL;
2187 /* Print the piecewise quasi-polynomial reduction "pwf" to "p" in C format.
2189 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
2190 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2192 int i;
2193 isl_space *space;
2195 space = isl_pw_qpolynomial_fold_get_domain_space(pwf);
2196 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set)) {
2197 p = print_qpolynomial_fold_c(p, space, pwf->p[0].fold);
2198 isl_space_free(space);
2199 return p;
2202 for (i = 0; i < pwf->n; ++i) {
2203 p = isl_printer_print_str(p, "(");
2204 p = print_set_c(p, space, pwf->p[i].set);
2205 p = isl_printer_print_str(p, ") ? (");
2206 p = print_qpolynomial_fold_c(p, space, pwf->p[i].fold);
2207 p = isl_printer_print_str(p, ") : ");
2210 isl_space_free(space);
2211 p = isl_printer_print_str(p, "0");
2212 return p;
2215 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
2216 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2218 if (!p || !pwf)
2219 goto error;
2221 if (p->output_format == ISL_FORMAT_ISL)
2222 return print_pw_qpolynomial_fold_isl(p, pwf);
2223 else if (p->output_format == ISL_FORMAT_C)
2224 return print_pw_qpolynomial_fold_c(p, pwf);
2225 isl_assert(p->ctx, 0, goto error);
2226 error:
2227 isl_printer_free(p);
2228 return NULL;
2231 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
2232 FILE *out, unsigned output_format)
2234 isl_printer *p;
2236 if (!pwf)
2237 return;
2239 p = isl_printer_to_file(pwf->dim->ctx, out);
2240 p = isl_printer_set_output_format(p, output_format);
2241 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
2243 isl_printer_free(p);
2246 static isl_stat print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf,
2247 void *user)
2249 struct isl_union_print_data *data;
2250 data = (struct isl_union_print_data *)user;
2252 if (!data->first)
2253 data->p = isl_printer_print_str(data->p, "; ");
2254 data->first = 0;
2256 data->p = isl_pwf_print_isl_body(data->p, pwf);
2257 isl_pw_qpolynomial_fold_free(pwf);
2259 return isl_stat_ok;
2262 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2263 __isl_take isl_printer *p,
2264 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2266 struct isl_union_print_data data;
2267 struct isl_print_space_data space_data = { 0 };
2268 isl_space *space;
2270 space = isl_union_pw_qpolynomial_fold_get_space(upwf);
2271 p = print_param_tuple(p, space, &space_data);
2272 isl_space_free(space);
2273 p = isl_printer_print_str(p, "{ ");
2274 data.p = p;
2275 data.first = 1;
2276 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2277 &print_pwf_body, &data);
2278 p = data.p;
2279 p = isl_printer_print_str(p, " }");
2280 return p;
2283 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2284 __isl_take isl_printer *p,
2285 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2287 if (!p || !upwf)
2288 goto error;
2290 if (p->output_format == ISL_FORMAT_ISL)
2291 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2292 isl_die(p->ctx, isl_error_invalid,
2293 "invalid output format for isl_union_pw_qpolynomial_fold",
2294 goto error);
2295 error:
2296 isl_printer_free(p);
2297 return NULL;
2300 /* Print the isl_constraint "c" to "p".
2302 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
2303 __isl_keep isl_constraint *c)
2305 struct isl_print_space_data data = { 0 };
2306 isl_local_space *ls;
2307 isl_space *space;
2308 isl_bool exists;
2310 if (!p || !c)
2311 goto error;
2313 ls = isl_constraint_get_local_space(c);
2314 if (!ls)
2315 return isl_printer_free(p);
2316 space = isl_local_space_get_space(ls);
2317 p = print_param_tuple(p, space, &data);
2318 p = isl_printer_print_str(p, "{ ");
2319 p = isl_print_space(space, p, 0, &data);
2320 p = isl_printer_print_str(p, " : ");
2321 exists = need_exists(p, ls->div);
2322 if (exists < 0)
2323 p = isl_printer_free(p);
2324 if (exists >= 0 && exists)
2325 p = open_exists(p, space, ls->div, 0);
2326 p = print_affine_of_len(space, ls->div, p, c->v->el, c->v->size);
2327 if (isl_constraint_is_equality(c))
2328 p = isl_printer_print_str(p, " = 0");
2329 else
2330 p = isl_printer_print_str(p, " >= 0");
2331 if (exists >= 0 && exists)
2332 p = isl_printer_print_str(p, s_close_exists[0]);
2333 p = isl_printer_print_str(p, " }");
2334 isl_space_free(space);
2335 isl_local_space_free(ls);
2337 return p;
2338 error:
2339 isl_printer_free(p);
2340 return NULL;
2343 static __isl_give isl_printer *isl_printer_print_space_isl(
2344 __isl_take isl_printer *p, __isl_keep isl_space *space)
2346 struct isl_print_space_data data = { 0 };
2348 if (!space)
2349 goto error;
2351 p = print_param_tuple(p, space, &data);
2353 p = isl_printer_print_str(p, "{ ");
2354 if (isl_space_is_params(space))
2355 p = isl_printer_print_str(p, s_such_that[0]);
2356 else
2357 p = isl_print_space(space, p, 0, &data);
2358 p = isl_printer_print_str(p, " }");
2360 return p;
2361 error:
2362 isl_printer_free(p);
2363 return NULL;
2366 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
2367 __isl_keep isl_space *space)
2369 if (!p || !space)
2370 return isl_printer_free(p);
2371 if (p->output_format == ISL_FORMAT_ISL)
2372 return isl_printer_print_space_isl(p, space);
2373 else if (p->output_format == ISL_FORMAT_OMEGA)
2374 return print_omega_parameters(space, p);
2376 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
2377 "output format not supported for space",
2378 return isl_printer_free(p));
2381 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
2382 __isl_keep isl_local_space *ls)
2384 struct isl_print_space_data data = { 0 };
2385 unsigned n_div;
2387 if (!ls)
2388 goto error;
2390 p = print_param_tuple(p, ls->dim, &data);
2391 p = isl_printer_print_str(p, "{ ");
2392 p = isl_print_space(ls->dim, p, 0, &data);
2393 n_div = isl_local_space_dim(ls, isl_dim_div);
2394 if (n_div > 0) {
2395 p = isl_printer_print_str(p, " : ");
2396 p = isl_printer_print_str(p, s_open_exists[0]);
2397 p = print_div_list(p, ls->dim, ls->div, 0, 1);
2398 p = isl_printer_print_str(p, s_close_exists[0]);
2399 } else if (isl_space_is_params(ls->dim))
2400 p = isl_printer_print_str(p, s_such_that[0]);
2401 p = isl_printer_print_str(p, " }");
2402 return p;
2403 error:
2404 isl_printer_free(p);
2405 return NULL;
2408 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2409 __isl_keep isl_aff *aff)
2411 unsigned total;
2413 if (isl_aff_is_nan(aff))
2414 return isl_printer_print_str(p, "NaN");
2416 total = isl_local_space_dim(aff->ls, isl_dim_all);
2417 p = isl_printer_print_str(p, "(");
2418 p = print_affine_of_len(aff->ls->dim, aff->ls->div, p,
2419 aff->v->el + 1, 1 + total);
2420 if (isl_int_is_one(aff->v->el[0]))
2421 p = isl_printer_print_str(p, ")");
2422 else {
2423 p = isl_printer_print_str(p, ")/");
2424 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2427 return p;
2430 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2431 __isl_keep isl_aff *aff)
2433 struct isl_print_space_data data = { 0 };
2435 if (isl_space_is_params(aff->ls->dim))
2437 else {
2438 p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
2439 p = isl_printer_print_str(p, " -> ");
2441 p = isl_printer_print_str(p, "[");
2442 p = print_aff_body(p, aff);
2443 p = isl_printer_print_str(p, "]");
2445 return p;
2448 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2449 __isl_keep isl_aff *aff)
2451 struct isl_print_space_data data = { 0 };
2453 if (!aff)
2454 goto error;
2456 p = print_param_tuple(p, aff->ls->dim, &data);
2457 p = isl_printer_print_str(p, "{ ");
2458 p = print_aff(p, aff);
2459 p = isl_printer_print_str(p, " }");
2460 return p;
2461 error:
2462 isl_printer_free(p);
2463 return NULL;
2466 /* Print the body of an isl_pw_aff, i.e., a semicolon delimited
2467 * sequence of affine expressions, each followed by constraints.
2469 static __isl_give isl_printer *print_pw_aff_body(
2470 __isl_take isl_printer *p, __isl_keep isl_pw_aff *pa)
2472 int i;
2474 if (!pa)
2475 return isl_printer_free(p);
2477 for (i = 0; i < pa->n; ++i) {
2478 isl_space *space;
2480 if (i)
2481 p = isl_printer_print_str(p, "; ");
2482 p = print_aff(p, pa->p[i].aff);
2483 space = isl_aff_get_domain_space(pa->p[i].aff);
2484 p = print_disjuncts(set_to_map(pa->p[i].set), space, p, 0);
2485 isl_space_free(space);
2488 return p;
2491 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2492 __isl_keep isl_pw_aff *pwaff)
2494 struct isl_print_space_data data = { 0 };
2496 if (!pwaff)
2497 goto error;
2499 p = print_param_tuple(p, pwaff->dim, &data);
2500 p = isl_printer_print_str(p, "{ ");
2501 p = print_pw_aff_body(p, pwaff);
2502 p = isl_printer_print_str(p, " }");
2503 return p;
2504 error:
2505 isl_printer_free(p);
2506 return NULL;
2509 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2510 __isl_keep isl_local_space *ls, isl_int *c);
2512 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2513 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2515 if (type == isl_dim_div) {
2516 p = isl_printer_print_str(p, "floord(");
2517 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2518 p = isl_printer_print_str(p, ", ");
2519 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2520 p = isl_printer_print_str(p, ")");
2521 } else {
2522 const char *name;
2524 name = isl_space_get_dim_name(ls->dim, type, pos);
2525 if (!name)
2526 name = "UNNAMED";
2527 p = isl_printer_print_str(p, name);
2529 return p;
2532 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2533 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2535 enum isl_dim_type type;
2537 if (pos == 0)
2538 return isl_printer_print_isl_int(p, c);
2540 if (isl_int_is_one(c))
2542 else if (isl_int_is_negone(c))
2543 p = isl_printer_print_str(p, "-");
2544 else {
2545 p = isl_printer_print_isl_int(p, c);
2546 p = isl_printer_print_str(p, "*");
2548 type = pos2type(ls->dim, &pos);
2549 p = print_ls_name_c(p, ls, type, pos);
2550 return p;
2553 static __isl_give isl_printer *print_ls_partial_affine_c(
2554 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2555 isl_int *c, unsigned len)
2557 int i;
2558 int first;
2560 for (i = 0, first = 1; i < len; ++i) {
2561 int flip = 0;
2562 if (isl_int_is_zero(c[i]))
2563 continue;
2564 if (!first) {
2565 if (isl_int_is_neg(c[i])) {
2566 flip = 1;
2567 isl_int_neg(c[i], c[i]);
2568 p = isl_printer_print_str(p, " - ");
2569 } else
2570 p = isl_printer_print_str(p, " + ");
2572 first = 0;
2573 p = print_ls_term_c(p, ls, c[i], i);
2574 if (flip)
2575 isl_int_neg(c[i], c[i]);
2577 if (first)
2578 p = isl_printer_print_str(p, "0");
2579 return p;
2582 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2583 __isl_keep isl_local_space *ls, isl_int *c)
2585 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2586 return print_ls_partial_affine_c(p, ls, c, len);
2589 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2590 __isl_keep isl_aff *aff)
2592 unsigned total;
2594 total = isl_local_space_dim(aff->ls, isl_dim_all);
2595 if (!isl_int_is_one(aff->v->el[0]))
2596 p = isl_printer_print_str(p, "(");
2597 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2598 if (!isl_int_is_one(aff->v->el[0])) {
2599 p = isl_printer_print_str(p, ")/");
2600 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2602 return p;
2605 /* In the C format, we cannot express that "pwaff" may be undefined
2606 * on parts of the domain space. We therefore assume that the expression
2607 * will only be evaluated on its definition domain and compute the gist
2608 * of each cell with respect to this domain.
2610 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2611 __isl_keep isl_pw_aff *pwaff)
2613 isl_set *domain;
2614 isl_ast_build *build;
2615 isl_ast_expr *expr;
2617 if (pwaff->n < 1)
2618 isl_die(p->ctx, isl_error_unsupported,
2619 "cannot print empty isl_pw_aff in C format",
2620 return isl_printer_free(p));
2622 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2623 build = isl_ast_build_from_context(domain);
2624 expr = isl_ast_build_expr_from_pw_aff(build, isl_pw_aff_copy(pwaff));
2625 p = isl_printer_print_ast_expr(p, expr);
2626 isl_ast_expr_free(expr);
2627 isl_ast_build_free(build);
2629 return p;
2632 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2633 __isl_keep isl_aff *aff)
2635 if (!p || !aff)
2636 goto error;
2638 if (p->output_format == ISL_FORMAT_ISL)
2639 return print_aff_isl(p, aff);
2640 else if (p->output_format == ISL_FORMAT_C)
2641 return print_aff_c(p, aff);
2642 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2643 goto error);
2644 error:
2645 isl_printer_free(p);
2646 return NULL;
2649 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2650 __isl_keep isl_pw_aff *pwaff)
2652 if (!p || !pwaff)
2653 goto error;
2655 if (p->output_format == ISL_FORMAT_ISL)
2656 return print_pw_aff_isl(p, pwaff);
2657 else if (p->output_format == ISL_FORMAT_C)
2658 return print_pw_aff_c(p, pwaff);
2659 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2660 goto error);
2661 error:
2662 isl_printer_free(p);
2663 return NULL;
2666 /* Print "pa" in a sequence of isl_pw_affs delimited by semicolons.
2667 * Each isl_pw_aff itself is also printed as semicolon delimited
2668 * sequence of pieces.
2669 * If data->first = 1, then this is the first in the sequence.
2670 * Update data->first to tell the next element that it is not the first.
2672 static isl_stat print_pw_aff_body_wrap(__isl_take isl_pw_aff *pa,
2673 void *user)
2675 struct isl_union_print_data *data;
2676 data = (struct isl_union_print_data *) user;
2678 if (!data->first)
2679 data->p = isl_printer_print_str(data->p, "; ");
2680 data->first = 0;
2682 data->p = print_pw_aff_body(data->p, pa);
2683 isl_pw_aff_free(pa);
2685 return data->p ? isl_stat_ok : isl_stat_error;
2688 /* Print the body of an isl_union_pw_aff, i.e., a semicolon delimited
2689 * sequence of affine expressions, each followed by constraints,
2690 * with the sequence enclosed in braces.
2692 static __isl_give isl_printer *print_union_pw_aff_body(
2693 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2695 struct isl_union_print_data data = { p, 1 };
2697 p = isl_printer_print_str(p, s_open_set[0]);
2698 data.p = p;
2699 if (isl_union_pw_aff_foreach_pw_aff(upa,
2700 &print_pw_aff_body_wrap, &data) < 0)
2701 data.p = isl_printer_free(p);
2702 p = data.p;
2703 p = isl_printer_print_str(p, s_close_set[0]);
2705 return p;
2708 /* Print the isl_union_pw_aff "upa" to "p" in isl format.
2710 * The individual isl_pw_affs are delimited by a semicolon.
2712 static __isl_give isl_printer *print_union_pw_aff_isl(
2713 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2715 struct isl_print_space_data data = { 0 };
2716 isl_space *space;
2718 space = isl_union_pw_aff_get_space(upa);
2719 p = print_param_tuple(p, space, &data);
2720 isl_space_free(space);
2721 p = print_union_pw_aff_body(p, upa);
2722 return p;
2725 /* Print the isl_union_pw_aff "upa" to "p".
2727 * We currently only support an isl format.
2729 __isl_give isl_printer *isl_printer_print_union_pw_aff(
2730 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2732 if (!p || !upa)
2733 return isl_printer_free(p);
2735 if (p->output_format == ISL_FORMAT_ISL)
2736 return print_union_pw_aff_isl(p, upa);
2737 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2738 "unsupported output format", return isl_printer_free(p));
2741 /* Print dimension "pos" of data->space to "p".
2743 * data->user is assumed to be an isl_multi_aff.
2745 * If the current dimension is an output dimension, then print
2746 * the corresponding expression. Otherwise, print the name of the dimension.
2748 static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
2749 struct isl_print_space_data *data, unsigned pos)
2751 isl_multi_aff *ma = data->user;
2753 if (data->type == isl_dim_out)
2754 p = print_aff_body(p, ma->p[pos]);
2755 else
2756 p = print_name(data->space, p, data->type, pos, data->latex);
2758 return p;
2761 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2762 __isl_keep isl_multi_aff *maff)
2764 struct isl_print_space_data data = { 0 };
2766 data.print_dim = &print_dim_ma;
2767 data.user = maff;
2768 return isl_print_space(maff->space, p, 0, &data);
2771 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2772 __isl_keep isl_multi_aff *maff)
2774 struct isl_print_space_data data = { 0 };
2776 if (!maff)
2777 goto error;
2779 p = print_param_tuple(p, maff->space, &data);
2780 p = isl_printer_print_str(p, "{ ");
2781 p = print_multi_aff(p, maff);
2782 p = isl_printer_print_str(p, " }");
2783 return p;
2784 error:
2785 isl_printer_free(p);
2786 return NULL;
2789 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2790 __isl_keep isl_multi_aff *maff)
2792 if (!p || !maff)
2793 goto error;
2795 if (p->output_format == ISL_FORMAT_ISL)
2796 return print_multi_aff_isl(p, maff);
2797 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2798 goto error);
2799 error:
2800 isl_printer_free(p);
2801 return NULL;
2804 static __isl_give isl_printer *print_pw_multi_aff_body(
2805 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2807 int i;
2809 if (!pma)
2810 goto error;
2812 for (i = 0; i < pma->n; ++i) {
2813 isl_space *space;
2815 if (i)
2816 p = isl_printer_print_str(p, "; ");
2817 p = print_multi_aff(p, pma->p[i].maff);
2818 space = isl_multi_aff_get_domain_space(pma->p[i].maff);
2819 p = print_disjuncts(set_to_map(pma->p[i].set), space, p, 0);
2820 isl_space_free(space);
2822 return p;
2823 error:
2824 isl_printer_free(p);
2825 return NULL;
2828 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
2829 __isl_keep isl_pw_multi_aff *pma)
2831 struct isl_print_space_data data = { 0 };
2833 if (!pma)
2834 goto error;
2836 p = print_param_tuple(p, pma->dim, &data);
2837 p = isl_printer_print_str(p, "{ ");
2838 p = print_pw_multi_aff_body(p, pma);
2839 p = isl_printer_print_str(p, " }");
2840 return p;
2841 error:
2842 isl_printer_free(p);
2843 return NULL;
2846 /* Print the unnamed, single-dimensional piecewise multi affine expression "pma"
2847 * to "p".
2849 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
2850 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2852 int i;
2853 isl_space *space;
2855 space = isl_pw_multi_aff_get_domain_space(pma);
2856 for (i = 0; i < pma->n - 1; ++i) {
2857 p = isl_printer_print_str(p, "(");
2858 p = print_set_c(p, space, pma->p[i].set);
2859 p = isl_printer_print_str(p, ") ? (");
2860 p = print_aff_c(p, pma->p[i].maff->p[0]);
2861 p = isl_printer_print_str(p, ") : ");
2863 isl_space_free(space);
2865 return print_aff_c(p, pma->p[pma->n - 1].maff->p[0]);
2868 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
2869 __isl_keep isl_pw_multi_aff *pma)
2871 int n;
2872 const char *name;
2874 if (!pma)
2875 goto error;
2876 if (pma->n < 1)
2877 isl_die(p->ctx, isl_error_unsupported,
2878 "cannot print empty isl_pw_multi_aff in C format",
2879 goto error);
2880 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
2881 if (!name && isl_pw_multi_aff_dim(pma, isl_dim_out) == 1)
2882 return print_unnamed_pw_multi_aff_c(p, pma);
2883 if (!name)
2884 isl_die(p->ctx, isl_error_unsupported,
2885 "cannot print unnamed isl_pw_multi_aff in C format",
2886 goto error);
2888 p = isl_printer_print_str(p, name);
2889 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
2890 if (n != 0)
2891 isl_die(p->ctx, isl_error_unsupported,
2892 "not supported yet", goto error);
2894 return p;
2895 error:
2896 isl_printer_free(p);
2897 return NULL;
2900 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
2901 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2903 if (!p || !pma)
2904 goto error;
2906 if (p->output_format == ISL_FORMAT_ISL)
2907 return print_pw_multi_aff_isl(p, pma);
2908 if (p->output_format == ISL_FORMAT_C)
2909 return print_pw_multi_aff_c(p, pma);
2910 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2911 goto error);
2912 error:
2913 isl_printer_free(p);
2914 return NULL;
2917 static isl_stat print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
2918 void *user)
2920 struct isl_union_print_data *data;
2921 data = (struct isl_union_print_data *) user;
2923 if (!data->first)
2924 data->p = isl_printer_print_str(data->p, "; ");
2925 data->first = 0;
2927 data->p = print_pw_multi_aff_body(data->p, pma);
2928 isl_pw_multi_aff_free(pma);
2930 return isl_stat_ok;
2933 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
2934 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2936 struct isl_union_print_data data;
2937 struct isl_print_space_data space_data = { 0 };
2938 isl_space *space;
2940 space = isl_union_pw_multi_aff_get_space(upma);
2941 p = print_param_tuple(p, space, &space_data);
2942 isl_space_free(space);
2943 p = isl_printer_print_str(p, s_open_set[0]);
2944 data.p = p;
2945 data.first = 1;
2946 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
2947 &print_pw_multi_aff_body_wrap, &data);
2948 p = data.p;
2949 p = isl_printer_print_str(p, s_close_set[0]);
2950 return p;
2953 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
2954 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2956 if (!p || !upma)
2957 goto error;
2959 if (p->output_format == ISL_FORMAT_ISL)
2960 return print_union_pw_multi_aff_isl(p, upma);
2961 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2962 goto error);
2963 error:
2964 isl_printer_free(p);
2965 return NULL;
2968 /* Print dimension "pos" of data->space to "p".
2970 * data->user is assumed to be an isl_multi_pw_aff.
2972 * If the current dimension is an output dimension, then print
2973 * the corresponding piecewise affine expression.
2974 * Otherwise, print the name of the dimension.
2976 static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
2977 struct isl_print_space_data *data, unsigned pos)
2979 int i;
2980 int need_parens;
2981 isl_multi_pw_aff *mpa = data->user;
2982 isl_pw_aff *pa;
2984 if (data->type != isl_dim_out)
2985 return print_name(data->space, p, data->type, pos, data->latex);
2987 pa = mpa->p[pos];
2988 if (pa->n == 0)
2989 return isl_printer_print_str(p, "(0 : 1 = 0)");
2991 need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
2992 if (need_parens)
2993 p = isl_printer_print_str(p, "(");
2994 for (i = 0; i < pa->n; ++i) {
2995 isl_space *space;
2997 if (i)
2998 p = isl_printer_print_str(p, "; ");
2999 p = print_aff_body(p, pa->p[i].aff);
3000 space = isl_aff_get_domain_space(pa->p[i].aff);
3001 p = print_disjuncts(pa->p[i].set, space, p, 0);
3002 isl_space_free(space);
3004 if (need_parens)
3005 p = isl_printer_print_str(p, ")");
3007 return p;
3010 /* Print "mpa" to "p" in isl format.
3012 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
3013 __isl_keep isl_multi_pw_aff *mpa)
3015 struct isl_print_space_data data = { 0 };
3017 if (!mpa)
3018 return isl_printer_free(p);
3020 p = print_param_tuple(p, mpa->space, &data);
3021 p = isl_printer_print_str(p, "{ ");
3022 data.print_dim = &print_dim_mpa;
3023 data.user = mpa;
3024 p = isl_print_space(mpa->space, p, 0, &data);
3025 p = isl_printer_print_str(p, " }");
3026 return p;
3029 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
3030 __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
3032 if (!p || !mpa)
3033 return isl_printer_free(p);
3035 if (p->output_format == ISL_FORMAT_ISL)
3036 return print_multi_pw_aff_isl(p, mpa);
3037 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3038 return isl_printer_free(p));
3041 /* Print dimension "pos" of data->space to "p".
3043 * data->user is assumed to be an isl_multi_val.
3045 * If the current dimension is an output dimension, then print
3046 * the corresponding value. Otherwise, print the name of the dimension.
3048 static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
3049 struct isl_print_space_data *data, unsigned pos)
3051 isl_multi_val *mv = data->user;
3053 if (data->type == isl_dim_out)
3054 return isl_printer_print_val(p, mv->p[pos]);
3055 else
3056 return print_name(data->space, p, data->type, pos, data->latex);
3059 /* Print the isl_multi_val "mv" to "p" in isl format.
3061 static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
3062 __isl_keep isl_multi_val *mv)
3064 struct isl_print_space_data data = { 0 };
3066 if (!mv)
3067 return isl_printer_free(p);
3069 p = print_param_tuple(p, mv->space, &data);
3070 p = isl_printer_print_str(p, "{ ");
3071 data.print_dim = &print_dim_mv;
3072 data.user = mv;
3073 p = isl_print_space(mv->space, p, 0, &data);
3074 p = isl_printer_print_str(p, " }");
3075 return p;
3078 /* Print the isl_multi_val "mv" to "p".
3080 * Currently only supported in isl format.
3082 __isl_give isl_printer *isl_printer_print_multi_val(
3083 __isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
3085 if (!p || !mv)
3086 return isl_printer_free(p);
3088 if (p->output_format == ISL_FORMAT_ISL)
3089 return print_multi_val_isl(p, mv);
3090 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3091 return isl_printer_free(p));
3094 /* Print dimension "pos" of data->space to "p".
3096 * data->user is assumed to be an isl_multi_union_pw_aff.
3098 * The current dimension is necessarily a set dimension, so
3099 * we print the corresponding isl_union_pw_aff, including
3100 * the braces.
3102 static __isl_give isl_printer *print_union_pw_aff_dim(__isl_take isl_printer *p,
3103 struct isl_print_space_data *data, unsigned pos)
3105 isl_multi_union_pw_aff *mupa = data->user;
3106 isl_union_pw_aff *upa;
3108 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, pos);
3109 p = print_union_pw_aff_body(p, upa);
3110 isl_union_pw_aff_free(upa);
3112 return p;
3115 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3117 static __isl_give isl_printer *print_multi_union_pw_aff_isl(
3118 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3120 struct isl_print_space_data data = { 0 };
3121 isl_space *space;
3123 space = isl_multi_union_pw_aff_get_space(mupa);
3124 p = print_param_tuple(p, space, &data);
3126 data.print_dim = &print_union_pw_aff_dim;
3127 data.user = mupa;
3129 p = isl_print_space(space, p, 0, &data);
3130 isl_space_free(space);
3132 return p;
3135 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3137 * We currently only support an isl format.
3139 __isl_give isl_printer *isl_printer_print_multi_union_pw_aff(
3140 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3142 if (!p || !mupa)
3143 return isl_printer_free(p);
3145 if (p->output_format == ISL_FORMAT_ISL)
3146 return print_multi_union_pw_aff_isl(p, mupa);
3147 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
3148 "unsupported output format", return isl_printer_free(p));