isl_output.c: print_disjunct: avoid invalid access on error
[isl.git] / isl_output.c
blob05efd3d66c438145fa872ff39fa5600199db0cb6
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/ast_build.h>
32 #include <isl_sort.h>
33 #include <isl_output_private.h>
35 static const char *s_to[2] = { " -> ", " \\to " };
36 static const char *s_and[2] = { " and ", " \\wedge " };
37 static const char *s_or[2] = { " or ", " \\vee " };
38 static const char *s_le[2] = { "<=", "\\le" };
39 static const char *s_ge[2] = { ">=", "\\ge" };
40 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
41 static const char *s_close_set[2] = { " }", " \\,\\}" };
42 static const char *s_open_list[2] = { "[", "(" };
43 static const char *s_close_list[2] = { "]", ")" };
44 static const char *s_such_that[2] = { " : ", " \\mid " };
45 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
46 static const char *s_close_exists[2] = { ")", "" };
47 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
48 static const char *s_param_prefix[2] = { "p", "p_" };
49 static const char *s_input_prefix[2] = { "i", "i_" };
50 static const char *s_output_prefix[2] = { "o", "o_" };
52 static __isl_give isl_printer *print_constraint_polylib(
53 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
55 int i;
56 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
57 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
58 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
59 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
61 p = isl_printer_start_line(p);
62 p = isl_printer_print_int(p, ineq);
63 for (i = 0; i < n_out; ++i) {
64 p = isl_printer_print_str(p, " ");
65 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
67 for (i = 0; i < n_in; ++i) {
68 p = isl_printer_print_str(p, " ");
69 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
71 for (i = 0; i < bmap->n_div; ++i) {
72 p = isl_printer_print_str(p, " ");
73 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
75 for (i = 0; i < nparam; ++i) {
76 p = isl_printer_print_str(p, " ");
77 p = isl_printer_print_isl_int(p, c[1+i]);
79 p = isl_printer_print_str(p, " ");
80 p = isl_printer_print_isl_int(p, c[0]);
81 p = isl_printer_end_line(p);
82 return p;
85 static __isl_give isl_printer *print_constraints_polylib(
86 struct isl_basic_map *bmap, __isl_take isl_printer *p)
88 int i;
90 p = isl_printer_set_isl_int_width(p, 5);
92 for (i = 0; i < bmap->n_eq; ++i)
93 p = print_constraint_polylib(bmap, 0, i, p);
94 for (i = 0; i < bmap->n_ineq; ++i)
95 p = print_constraint_polylib(bmap, 1, i, p);
97 return p;
100 static __isl_give isl_printer *bset_print_constraints_polylib(
101 struct isl_basic_set *bset, __isl_take isl_printer *p)
103 return print_constraints_polylib((struct isl_basic_map *)bset, p);
106 static __isl_give isl_printer *isl_basic_map_print_polylib(
107 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
109 unsigned total = isl_basic_map_total_dim(bmap);
110 p = isl_printer_start_line(p);
111 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
112 p = isl_printer_print_str(p, " ");
113 p = isl_printer_print_int(p, 1 + total + 1);
114 if (ext) {
115 p = isl_printer_print_str(p, " ");
116 p = isl_printer_print_int(p,
117 isl_basic_map_dim(bmap, isl_dim_out));
118 p = isl_printer_print_str(p, " ");
119 p = isl_printer_print_int(p,
120 isl_basic_map_dim(bmap, isl_dim_in));
121 p = isl_printer_print_str(p, " ");
122 p = isl_printer_print_int(p,
123 isl_basic_map_dim(bmap, isl_dim_div));
124 p = isl_printer_print_str(p, " ");
125 p = isl_printer_print_int(p,
126 isl_basic_map_dim(bmap, isl_dim_param));
128 p = isl_printer_end_line(p);
129 return print_constraints_polylib(bmap, p);
132 static __isl_give isl_printer *isl_basic_set_print_polylib(
133 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
135 return isl_basic_map_print_polylib((struct isl_basic_map *)bset, p, ext);
138 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
139 __isl_take isl_printer *p, int ext)
141 int i;
143 p = isl_printer_start_line(p);
144 p = isl_printer_print_int(p, map->n);
145 p = isl_printer_end_line(p);
146 for (i = 0; i < map->n; ++i) {
147 p = isl_printer_start_line(p);
148 p = isl_printer_end_line(p);
149 p = isl_basic_map_print_polylib(map->p[i], p, ext);
151 return p;
154 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
155 __isl_take isl_printer *p, int ext)
157 return isl_map_print_polylib((struct isl_map *)set, p, ext);
160 static int count_same_name(__isl_keep isl_space *dim,
161 enum isl_dim_type type, unsigned pos, const char *name)
163 enum isl_dim_type t;
164 unsigned p, s;
165 int count = 0;
167 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
168 s = t == type ? pos : isl_space_dim(dim, t);
169 for (p = 0; p < s; ++p) {
170 const char *n = isl_space_get_dim_name(dim, t, p);
171 if (n && !strcmp(n, name))
172 count++;
175 return count;
178 /* Print the name of the variable of type "type" and position "pos"
179 * in "space" to "p".
181 static __isl_give isl_printer *print_name(__isl_keep isl_space *space,
182 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
183 int latex)
185 const char *name;
186 char buffer[20];
187 int primes;
189 name = type == isl_dim_div ? NULL
190 : isl_space_get_dim_name(space, type, pos);
192 if (!name) {
193 const char *prefix;
194 if (type == isl_dim_param)
195 prefix = s_param_prefix[latex];
196 else if (type == isl_dim_div)
197 prefix = s_div_prefix[latex];
198 else if (isl_space_is_set(space) || type == isl_dim_in)
199 prefix = s_input_prefix[latex];
200 else
201 prefix = s_output_prefix[latex];
202 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
203 name = buffer;
205 primes = count_same_name(space, name == buffer ? isl_dim_div : type,
206 pos, name);
207 p = isl_printer_print_str(p, name);
208 while (primes-- > 0)
209 p = isl_printer_print_str(p, "'");
210 return p;
213 static enum isl_dim_type pos2type(__isl_keep isl_space *dim, unsigned *pos)
215 enum isl_dim_type type;
216 unsigned n_in = isl_space_dim(dim, isl_dim_in);
217 unsigned n_out = isl_space_dim(dim, isl_dim_out);
218 unsigned nparam = isl_space_dim(dim, isl_dim_param);
220 if (*pos < 1 + nparam) {
221 type = isl_dim_param;
222 *pos -= 1;
223 } else if (*pos < 1 + nparam + n_in) {
224 type = isl_dim_in;
225 *pos -= 1 + nparam;
226 } else if (*pos < 1 + nparam + n_in + n_out) {
227 type = isl_dim_out;
228 *pos -= 1 + nparam + n_in;
229 } else {
230 type = isl_dim_div;
231 *pos -= 1 + nparam + n_in + n_out;
234 return type;
237 /* Can the div expression of the integer division at position "row" of "div"
238 * be printed?
239 * In particular, are the div expressions available and does the selected
240 * variable have a known explicit representation?
241 * Furthermore, the Omega format does not allow and div expressions
242 * to be printed.
244 static isl_bool can_print_div_expr(__isl_keep isl_printer *p,
245 __isl_keep isl_mat *div, int pos)
247 if (p->output_format == ISL_FORMAT_OMEGA)
248 return isl_bool_false;
249 if (!div)
250 return isl_bool_false;
251 return !isl_int_is_zero(div->row[pos][0]);
254 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
255 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
257 static __isl_give isl_printer *print_term(__isl_keep isl_space *space,
258 __isl_keep isl_mat *div,
259 isl_int c, unsigned pos, __isl_take isl_printer *p, int latex)
261 enum isl_dim_type type;
262 int print_div_def;
264 if (pos == 0)
265 return isl_printer_print_isl_int(p, c);
267 type = pos2type(space, &pos);
268 print_div_def = type == isl_dim_div && can_print_div_expr(p, div, pos);
270 if (isl_int_is_one(c))
272 else if (isl_int_is_negone(c))
273 p = isl_printer_print_str(p, "-");
274 else {
275 p = isl_printer_print_isl_int(p, c);
276 if (p->output_format == ISL_FORMAT_C || print_div_def)
277 p = isl_printer_print_str(p, "*");
279 if (print_div_def)
280 p = print_div(space, div, pos, p);
281 else
282 p = print_name(space, p, type, pos, latex);
283 return p;
286 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *dim,
287 __isl_keep isl_mat *div,
288 __isl_take isl_printer *p, isl_int *c, int len)
290 int i;
291 int first;
293 for (i = 0, first = 1; i < len; ++i) {
294 int flip = 0;
295 if (isl_int_is_zero(c[i]))
296 continue;
297 if (!first) {
298 if (isl_int_is_neg(c[i])) {
299 flip = 1;
300 isl_int_neg(c[i], c[i]);
301 p = isl_printer_print_str(p, " - ");
302 } else
303 p = isl_printer_print_str(p, " + ");
305 first = 0;
306 p = print_term(dim, div, c[i], i, p, 0);
307 if (flip)
308 isl_int_neg(c[i], c[i]);
310 if (first)
311 p = isl_printer_print_str(p, "0");
312 return p;
315 /* Print an affine expression "c" corresponding to a constraint in "bmap"
316 * to "p", with the variable names taken from "space" and
317 * the integer division definitions taken from "div".
319 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
320 __isl_keep isl_space *space, __isl_keep isl_mat *div,
321 __isl_take isl_printer *p, isl_int *c)
323 unsigned len = 1 + isl_basic_map_total_dim(bmap);
324 return print_affine_of_len(space, div, p, c, len);
327 /* offset is the offset of local_dim inside data->type of data->space.
329 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
330 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
331 struct isl_print_space_data *data, int offset)
333 int i;
335 if (data->space != local_dim && local_type == isl_dim_out)
336 offset += local_dim->n_in;
338 for (i = 0; i < isl_space_dim(local_dim, local_type); ++i) {
339 if (i)
340 p = isl_printer_print_str(p, ", ");
341 if (data->print_dim)
342 p = data->print_dim(p, data, offset + i);
343 else
344 p = print_name(data->space, p, data->type, offset + i,
345 data->latex);
347 return p;
350 static __isl_give isl_printer *print_var_list(__isl_take isl_printer *p,
351 __isl_keep isl_space *space, enum isl_dim_type type)
353 struct isl_print_space_data data = { .space = space, .type = type };
355 return print_nested_var_list(p, space, type, &data, 0);
358 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
359 __isl_keep isl_space *local_dim,
360 struct isl_print_space_data *data, int offset);
362 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
363 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
364 struct isl_print_space_data *data, int offset)
366 const char *name = NULL;
367 unsigned n = isl_space_dim(local_dim, local_type);
368 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
369 name = isl_space_get_tuple_name(local_dim, local_type);
370 if (name) {
371 if (data->latex)
372 p = isl_printer_print_str(p, "\\mathrm{");
373 p = isl_printer_print_str(p, name);
374 if (data->latex)
375 p = isl_printer_print_str(p, "}");
378 if (!data->latex || n != 1 || name)
379 p = isl_printer_print_str(p, s_open_list[data->latex]);
380 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
381 local_dim->nested[local_type - isl_dim_in]) {
382 if (data->space != local_dim && local_type == isl_dim_out)
383 offset += local_dim->n_in;
384 p = print_nested_map_dim(p,
385 local_dim->nested[local_type - isl_dim_in],
386 data, offset);
387 } else
388 p = print_nested_var_list(p, local_dim, local_type, data,
389 offset);
390 if (!data->latex || n != 1 || name)
391 p = isl_printer_print_str(p, s_close_list[data->latex]);
392 return p;
395 static __isl_give isl_printer *print_tuple(__isl_keep isl_space *dim,
396 __isl_take isl_printer *p, enum isl_dim_type type,
397 struct isl_print_space_data *data)
399 data->space = dim;
400 data->type = type;
401 return print_nested_tuple(p, dim, type, data, 0);
404 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
405 __isl_keep isl_space *local_dim,
406 struct isl_print_space_data *data, int offset)
408 p = print_nested_tuple(p, local_dim, isl_dim_in, data, offset);
409 p = isl_printer_print_str(p, s_to[data->latex]);
410 p = print_nested_tuple(p, local_dim, isl_dim_out, data, offset);
412 return p;
415 __isl_give isl_printer *isl_print_space(__isl_keep isl_space *space,
416 __isl_take isl_printer *p, int rational,
417 struct isl_print_space_data *data)
419 if (rational && !data->latex)
420 p = isl_printer_print_str(p, "rat: ");
421 if (isl_space_is_params(space))
423 else if (isl_space_is_set(space))
424 p = print_tuple(space, p, isl_dim_set, data);
425 else {
426 p = print_tuple(space, p, isl_dim_in, data);
427 p = isl_printer_print_str(p, s_to[data->latex]);
428 p = print_tuple(space, p, isl_dim_out, data);
431 return p;
434 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_space *dim,
435 __isl_take isl_printer *p)
437 if (isl_space_dim(dim, isl_dim_param) == 0)
438 return p;
440 p = isl_printer_start_line(p);
441 p = isl_printer_print_str(p, "symbolic ");
442 p = print_var_list(p, dim, isl_dim_param);
443 p = isl_printer_print_str(p, ";");
444 p = isl_printer_end_line(p);
445 return p;
448 /* Does the inequality constraint following "i" in "bmap"
449 * have an opposite value for the same last coefficient?
450 * "last" is the position of the last coefficient of inequality "i".
451 * If the next constraint is a div constraint, then it is ignored
452 * since div constraints are not printed.
454 static int next_is_opposite(__isl_keep isl_basic_map *bmap, int i, int last)
456 unsigned total = isl_basic_map_total_dim(bmap);
457 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
459 if (i + 1 >= bmap->n_ineq)
460 return 0;
461 if (isl_seq_last_non_zero(bmap->ineq[i + 1], 1 + total) != last)
462 return 0;
463 if (last >= o_div &&
464 isl_basic_map_is_div_constraint(bmap, bmap->ineq[i + 1],
465 last - o_div))
466 return 0;
467 return isl_int_abs_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]) &&
468 !isl_int_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]);
471 /* Return a string representation of the operator used when
472 * printing a constraint where the LHS is greater than or equal to the LHS
473 * (sign > 0) or smaller than or equal to the LHS (sign < 0).
474 * If "strict" is set, then return the strict version of the comparison
475 * operator.
477 static const char *constraint_op(int sign, int strict, int latex)
479 if (strict)
480 return sign < 0 ? "<" : ">";
481 if (sign < 0)
482 return s_le[latex];
483 else
484 return s_ge[latex];
487 /* Print one side of a constraint "c" from "bmap" to "p", with
488 * the variable names taken from "space" and the integer division definitions
489 * taken from "div".
490 * "last" is the position of the last non-zero coefficient.
491 * Let c' be the result of zeroing out this coefficient, then
492 * the partial constraint
494 * c' op
496 * is printed.
497 * "first_constraint" is set if this is the first constraint
498 * in the conjunction.
500 static __isl_give isl_printer *print_half_constraint(struct isl_basic_map *bmap,
501 __isl_keep isl_space *space, __isl_keep isl_mat *div,
502 __isl_take isl_printer *p, isl_int *c, int last, const char *op,
503 int first_constraint, int latex)
505 if (!first_constraint)
506 p = isl_printer_print_str(p, s_and[latex]);
508 isl_int_set_si(c[last], 0);
509 p = print_affine(bmap, space, div, p, c);
511 p = isl_printer_print_str(p, " ");
512 p = isl_printer_print_str(p, op);
513 p = isl_printer_print_str(p, " ");
515 return p;
518 /* Print a constraint "c" from "bmap" to "p", with the variable names
519 * taken from "space" and the integer division definitions taken from "div".
520 * "last" is the position of the last non-zero coefficient, which is
521 * moreover assumed to be negative.
522 * Let c' be the result of zeroing out this coefficient, then
523 * the constraint is printed in the form
525 * -c[last] op c'
527 * "first_constraint" is set if this is the first constraint
528 * in the conjunction.
530 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
531 __isl_keep isl_space *space, __isl_keep isl_mat *div,
532 __isl_take isl_printer *p,
533 isl_int *c, int last, const char *op, int first_constraint, int latex)
535 if (!first_constraint)
536 p = isl_printer_print_str(p, s_and[latex]);
538 isl_int_abs(c[last], c[last]);
540 p = print_term(space, div, c[last], last, p, latex);
542 p = isl_printer_print_str(p, " ");
543 p = isl_printer_print_str(p, op);
544 p = isl_printer_print_str(p, " ");
546 isl_int_set_si(c[last], 0);
547 p = print_affine(bmap, space, div, p, c);
549 return p;
552 /* Print the constraints of "bmap" to "p".
553 * The names of the variables are taken from "space" and
554 * the integer division definitions are taken from "div".
555 * Div constraints are only printed in "dump" mode.
556 * The constraints are sorted prior to printing (except in "dump" mode).
558 * If x is the last variable with a non-zero coefficient,
559 * then a lower bound
561 * f - a x >= 0
563 * is printed as
565 * a x <= f
567 * while an upper bound
569 * f + a x >= 0
571 * is printed as
573 * a x >= -f
575 * If the next constraint has an opposite sign for the same last coefficient,
576 * then it is printed as
578 * f >= a x
580 * or
582 * -f <= a x
584 * instead. In fact, the "a x" part is not printed explicitly, but
585 * reused from the next constraint, which is therefore treated as
586 * a first constraint in the conjunction.
588 * If the constant term of "f" is -1, then "f" is replaced by "f + 1" and
589 * the comparison operator is replaced by the strict variant.
590 * Essentially, ">= 1" is replaced by "> 0".
592 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
593 __isl_keep isl_space *space, __isl_keep isl_mat *div,
594 __isl_take isl_printer *p, int latex)
596 int i;
597 isl_vec *c = NULL;
598 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
599 unsigned total = isl_basic_map_total_dim(bmap);
600 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
601 int first = 1;
603 bmap = isl_basic_map_copy(bmap);
604 if (!p->dump)
605 bmap = isl_basic_map_sort_constraints(bmap);
606 if (!bmap)
607 goto error;
609 c = isl_vec_alloc(bmap->ctx, 1 + total);
610 if (!c)
611 goto error;
613 for (i = bmap->n_eq - 1; i >= 0; --i) {
614 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
615 if (l < 0) {
616 if (i != bmap->n_eq - 1)
617 p = isl_printer_print_str(p, s_and[latex]);
618 p = isl_printer_print_str(p, "0 = 0");
619 continue;
621 if (isl_int_is_neg(bmap->eq[i][l]))
622 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
623 else
624 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
625 p = print_constraint(bmap, space, div, p, c->el, l,
626 "=", first, latex);
627 first = 0;
629 for (i = 0; i < bmap->n_ineq; ++i) {
630 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
631 int strict;
632 int s;
633 const char *op;
634 if (l < 0)
635 continue;
636 if (!p->dump && l >= o_div &&
637 can_print_div_expr(p, div, l - o_div) &&
638 isl_basic_map_is_div_constraint(bmap, bmap->ineq[i],
639 l - o_div))
640 continue;
641 s = isl_int_sgn(bmap->ineq[i][l]);
642 strict = !rational && isl_int_is_negone(bmap->ineq[i][0]);
643 if (s < 0)
644 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
645 else
646 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
647 if (strict)
648 isl_int_set_si(c->el[0], 0);
649 if (!p->dump && next_is_opposite(bmap, i, l)) {
650 op = constraint_op(-s, strict, latex);
651 p = print_half_constraint(bmap, space, div, p, c->el, l,
652 op, first, latex);
653 first = 1;
654 } else {
655 op = constraint_op(s, strict, latex);
656 p = print_constraint(bmap, space, div, p, c->el, l,
657 op, first, latex);
658 first = 0;
662 isl_basic_map_free(bmap);
663 isl_vec_free(c);
665 return p;
666 error:
667 isl_basic_map_free(bmap);
668 isl_vec_free(c);
669 isl_printer_free(p);
670 return NULL;
673 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
674 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
676 int c;
678 if (!p || !div)
679 return isl_printer_free(p);
681 c = p->output_format == ISL_FORMAT_C;
682 p = isl_printer_print_str(p, c ? "floord(" : "floor((");
683 p = print_affine_of_len(dim, div, p,
684 div->row[pos] + 1, div->n_col - 1);
685 p = isl_printer_print_str(p, c ? ", " : ")/");
686 p = isl_printer_print_isl_int(p, div->row[pos][0]);
687 p = isl_printer_print_str(p, ")");
688 return p;
691 /* Print a comma separated list of div names, except those that have
692 * a definition that can be printed.
693 * If "print_defined_divs" is set, then those div names are printed
694 * as well, along with their definitions.
696 static __isl_give isl_printer *print_div_list(__isl_take isl_printer *p,
697 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex,
698 int print_defined_divs)
700 int i;
701 int first = 1;
702 unsigned n_div;
704 if (!p || !space || !div)
705 return isl_printer_free(p);
707 n_div = isl_mat_rows(div);
709 for (i = 0; i < n_div; ++i) {
710 if (!print_defined_divs && can_print_div_expr(p, div, i))
711 continue;
712 if (!first)
713 p = isl_printer_print_str(p, ", ");
714 p = print_name(space, p, isl_dim_div, i, latex);
715 first = 0;
716 if (!can_print_div_expr(p, div, i))
717 continue;
718 p = isl_printer_print_str(p, " = ");
719 p = print_div(space, div, i, p);
722 return p;
725 /* Does printing "bmap" require an "exists" clause?
726 * That is, are there any local variables without an explicit representation?
728 static isl_bool need_exists(__isl_keep isl_printer *p,
729 __isl_keep isl_basic_map *bmap, __isl_keep isl_mat *div)
731 int i;
733 if (!p || !bmap)
734 return isl_bool_error;
735 if (bmap->n_div == 0)
736 return isl_bool_false;
737 for (i = 0; i < bmap->n_div; ++i)
738 if (!can_print_div_expr(p, div, i))
739 return isl_bool_true;
740 return isl_bool_false;
743 /* Print the constraints of "bmap" to "p".
744 * The names of the variables are taken from "space".
745 * "latex" is set if the constraints should be printed in LaTeX format.
746 * Do not print inline explicit div representations in "dump" mode.
748 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
749 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
751 int dump;
752 isl_mat *div;
753 isl_bool exists;
755 if (!p)
756 return NULL;
757 dump = p->dump;
758 div = isl_basic_map_get_divs(bmap);
759 if (dump)
760 exists = bmap->n_div > 0;
761 else
762 exists = need_exists(p, bmap, div);
763 if (exists >= 0 && exists) {
764 p = isl_printer_print_str(p, s_open_exists[latex]);
765 p = print_div_list(p, space, div, latex, dump);
766 p = isl_printer_print_str(p, ": ");
769 if (dump)
770 div = isl_mat_free(div);
771 p = print_constraints(bmap, space, div, p, latex);
772 isl_mat_free(div);
774 if (exists >= 0 && exists)
775 p = isl_printer_print_str(p, s_close_exists[latex]);
776 return p;
779 /* Print a colon followed by the constraints of "bmap"
780 * to "p", provided there are any constraints.
781 * The names of the variables are taken from "space".
782 * "latex" is set if the constraints should be printed in LaTeX format.
784 static __isl_give isl_printer *print_optional_disjunct(
785 __isl_keep isl_basic_map *bmap, __isl_keep isl_space *space,
786 __isl_take isl_printer *p, int latex)
788 if (isl_basic_map_plain_is_universe(bmap))
789 return p;
791 p = isl_printer_print_str(p, ": ");
792 p = print_disjunct(bmap, space, p, latex);
794 return p;
797 static __isl_give isl_printer *basic_map_print_omega(
798 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
800 p = isl_printer_print_str(p, "{ [");
801 p = print_var_list(p, bmap->dim, isl_dim_in);
802 p = isl_printer_print_str(p, "] -> [");
803 p = print_var_list(p, bmap->dim, isl_dim_out);
804 p = isl_printer_print_str(p, "] ");
805 p = print_optional_disjunct(bmap, bmap->dim, p, 0);
806 p = isl_printer_print_str(p, " }");
807 return p;
810 static __isl_give isl_printer *basic_set_print_omega(
811 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
813 p = isl_printer_print_str(p, "{ [");
814 p = print_var_list(p, bset->dim, isl_dim_set);
815 p = isl_printer_print_str(p, "] ");
816 p = print_optional_disjunct(bset, bset->dim, p, 0);
817 p = isl_printer_print_str(p, " }");
818 return p;
821 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
822 __isl_take isl_printer *p)
824 int i;
826 for (i = 0; i < map->n; ++i) {
827 if (i)
828 p = isl_printer_print_str(p, " union ");
829 p = basic_map_print_omega(map->p[i], p);
831 return p;
834 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
835 __isl_take isl_printer *p)
837 int i;
839 for (i = 0; i < set->n; ++i) {
840 if (i)
841 p = isl_printer_print_str(p, " union ");
842 p = basic_set_print_omega(set->p[i], p);
844 return p;
847 static __isl_give isl_printer *isl_basic_map_print_isl(
848 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
849 int latex)
851 struct isl_print_space_data data = { .latex = latex };
852 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
854 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
855 p = print_tuple(bmap->dim, p, isl_dim_param, &data);
856 p = isl_printer_print_str(p, " -> ");
858 p = isl_printer_print_str(p, "{ ");
859 p = isl_print_space(bmap->dim, p, rational, &data);
860 p = isl_printer_print_str(p, " : ");
861 p = print_disjunct(bmap, bmap->dim, p, latex);
862 p = isl_printer_print_str(p, " }");
863 return p;
866 /* Print the disjuncts of a map (or set) "map" to "p".
867 * The names of the variables are taken from "space".
868 * "latex" is set if the constraints should be printed in LaTeX format.
870 static __isl_give isl_printer *print_disjuncts_core(__isl_keep isl_map *map,
871 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
873 int i;
875 if (map->n == 0)
876 p = isl_printer_print_str(p, "1 = 0");
877 for (i = 0; i < map->n; ++i) {
878 if (i)
879 p = isl_printer_print_str(p, s_or[latex]);
880 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
881 p = isl_printer_print_str(p, "(");
882 p = print_disjunct(map->p[i], space, p, latex);
883 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
884 p = isl_printer_print_str(p, ")");
886 return p;
889 /* Print the disjuncts of a map (or set) "map" to "p".
890 * The names of the variables are taken from "space".
891 * "hull" describes constraints shared by all disjuncts of "map".
892 * "latex" is set if the constraints should be printed in LaTeX format.
894 * Print the disjuncts as a conjunction of "hull" and
895 * the result of removing the constraints of "hull" from "map".
896 * If this result turns out to be the universe, then simply print "hull".
898 static __isl_give isl_printer *print_disjuncts_in_hull(__isl_keep isl_map *map,
899 __isl_keep isl_space *space, __isl_take isl_basic_map *hull,
900 __isl_take isl_printer *p, int latex)
902 isl_bool is_universe;
904 p = print_disjunct(hull, space, p, latex);
905 map = isl_map_plain_gist_basic_map(isl_map_copy(map), hull);
906 is_universe = isl_map_plain_is_universe(map);
907 if (is_universe < 0)
908 goto error;
909 if (!is_universe) {
910 p = isl_printer_print_str(p, s_and[latex]);
911 p = isl_printer_print_str(p, "(");
912 p = print_disjuncts_core(map, space, p, latex);
913 p = isl_printer_print_str(p, ")");
915 isl_map_free(map);
917 return p;
918 error:
919 isl_map_free(map);
920 isl_printer_free(p);
921 return NULL;
924 /* Print the disjuncts of a map (or set) "map" to "p".
925 * The names of the variables are taken from "space".
926 * "latex" is set if the constraints should be printed in LaTeX format.
928 * If there are at least two disjuncts and "dump" mode is not turned out,
929 * check for any shared constraints among all disjuncts.
930 * If there are any, then print them separately in print_disjuncts_in_hull.
932 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
933 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
935 if (isl_map_plain_is_universe(map))
936 return p;
938 p = isl_printer_print_str(p, s_such_that[latex]);
939 if (!p)
940 return NULL;
942 if (!p->dump && map->n >= 2) {
943 isl_basic_map *hull;
944 isl_bool is_universe;
946 hull = isl_map_plain_unshifted_simple_hull(isl_map_copy(map));
947 is_universe = isl_basic_map_plain_is_universe(hull);
948 if (is_universe < 0)
949 p = isl_printer_free(p);
950 else if (!is_universe)
951 return print_disjuncts_in_hull(map, space, hull,
952 p, latex);
953 isl_basic_map_free(hull);
956 return print_disjuncts_core(map, space, p, latex);
959 /* Print the disjuncts of a map (or set).
960 * The names of the variables are taken from "space".
961 * "latex" is set if the constraints should be printed in LaTeX format.
963 * If the map turns out to be a universal parameter domain, then
964 * we need to print the colon. Otherwise, the output looks identical
965 * to the empty set.
967 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
968 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
970 if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
971 return isl_printer_print_str(p, s_such_that[latex]);
972 else
973 return print_disjuncts(map, space, p, latex);
976 struct isl_aff_split {
977 isl_basic_map *aff;
978 isl_map *map;
981 static void free_split(__isl_take struct isl_aff_split *split, int n)
983 int i;
985 if (!split)
986 return;
988 for (i = 0; i < n; ++i) {
989 isl_basic_map_free(split[i].aff);
990 isl_map_free(split[i].map);
993 free(split);
996 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
998 int i, j;
999 unsigned nparam, n_in, n_out, total;
1001 bmap = isl_basic_map_cow(bmap);
1002 if (!bmap)
1003 return NULL;
1004 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
1005 goto error;
1007 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1008 n_in = isl_basic_map_dim(bmap, isl_dim_in);
1009 n_out = isl_basic_map_dim(bmap, isl_dim_out);
1010 total = isl_basic_map_dim(bmap, isl_dim_all);
1011 for (i = bmap->n_eq - 1; i >= 0; --i) {
1012 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
1013 if (j >= nparam && j < nparam + n_in + n_out &&
1014 (isl_int_is_one(bmap->eq[i][1 + j]) ||
1015 isl_int_is_negone(bmap->eq[i][1 + j])))
1016 continue;
1017 if (isl_basic_map_drop_equality(bmap, i) < 0)
1018 goto error;
1021 bmap = isl_basic_map_finalize(bmap);
1023 return bmap;
1024 error:
1025 isl_basic_map_free(bmap);
1026 return NULL;
1029 static int aff_split_cmp(const void *p1, const void *p2, void *user)
1031 const struct isl_aff_split *s1, *s2;
1032 s1 = (const struct isl_aff_split *) p1;
1033 s2 = (const struct isl_aff_split *) p2;
1035 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
1038 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
1039 __isl_keep isl_basic_map *aff)
1041 int i, j;
1042 unsigned total;
1044 if (!bmap || !aff)
1045 goto error;
1047 total = isl_space_dim(bmap->dim, isl_dim_all);
1049 for (i = bmap->n_eq - 1; i >= 0; --i) {
1050 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
1051 bmap->n_div) != -1)
1052 continue;
1053 for (j = 0; j < aff->n_eq; ++j) {
1054 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
1055 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
1056 continue;
1057 if (isl_basic_map_drop_equality(bmap, i) < 0)
1058 goto error;
1059 break;
1063 return bmap;
1064 error:
1065 isl_basic_map_free(bmap);
1066 return NULL;
1069 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
1071 int i, n;
1072 struct isl_aff_split *split;
1073 isl_ctx *ctx;
1075 ctx = isl_map_get_ctx(map);
1076 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
1077 if (!split)
1078 return NULL;
1080 for (i = 0; i < map->n; ++i) {
1081 isl_basic_map *bmap;
1082 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
1083 bmap = isl_basic_map_copy(map->p[i]);
1084 bmap = isl_basic_map_cow(bmap);
1085 bmap = drop_aff(bmap, split[i].aff);
1086 split[i].map = isl_map_from_basic_map(bmap);
1087 if (!split[i].aff || !split[i].map)
1088 goto error;
1091 if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
1092 &aff_split_cmp, NULL) < 0)
1093 goto error;
1095 n = map->n;
1096 for (i = n - 1; i >= 1; --i) {
1097 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
1098 split[i].aff))
1099 continue;
1100 isl_basic_map_free(split[i].aff);
1101 split[i - 1].map = isl_map_union(split[i - 1].map,
1102 split[i].map);
1103 if (i != n - 1)
1104 split[i] = split[n - 1];
1105 split[n - 1].aff = NULL;
1106 split[n - 1].map = NULL;
1107 --n;
1110 return split;
1111 error:
1112 free_split(split, map->n);
1113 return NULL;
1116 static int defining_equality(__isl_keep isl_basic_map *eq,
1117 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
1119 int i;
1120 unsigned total;
1122 if (!eq)
1123 return -1;
1125 pos += isl_space_offset(dim, type);
1126 total = isl_basic_map_total_dim(eq);
1128 for (i = 0; i < eq->n_eq; ++i) {
1129 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
1130 continue;
1131 if (isl_int_is_one(eq->eq[i][1 + pos]))
1132 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
1133 return i;
1136 return -1;
1139 /* Print dimension "pos" of data->space to "p".
1141 * data->user is assumed to be an isl_basic_map keeping track of equalities.
1143 * If the current dimension is defined by these equalities, then print
1144 * the corresponding expression, assigned to the name of the dimension
1145 * if there is any. Otherwise, print the name of the dimension.
1147 static __isl_give isl_printer *print_dim_eq(__isl_take isl_printer *p,
1148 struct isl_print_space_data *data, unsigned pos)
1150 isl_basic_map *eq = data->user;
1151 int j;
1153 j = defining_equality(eq, data->space, data->type, pos);
1154 if (j >= 0) {
1155 if (isl_space_has_dim_name(data->space, data->type, pos)) {
1156 p = print_name(data->space, p, data->type, pos,
1157 data->latex);
1158 p = isl_printer_print_str(p, " = ");
1160 pos += 1 + isl_space_offset(data->space, data->type);
1161 p = print_affine_of_len(data->space, NULL, p, eq->eq[j], pos);
1162 } else {
1163 p = print_name(data->space, p, data->type, pos, data->latex);
1166 return p;
1169 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
1170 struct isl_aff_split *split, int n, __isl_keep isl_space *space)
1172 struct isl_print_space_data data = { 0 };
1173 int i;
1174 int rational;
1176 data.print_dim = &print_dim_eq;
1177 for (i = 0; i < n; ++i) {
1178 if (!split[i].map)
1179 break;
1180 rational = split[i].map->n > 0 &&
1181 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
1182 if (i)
1183 p = isl_printer_print_str(p, "; ");
1184 data.user = split[i].aff;
1185 p = isl_print_space(space, p, rational, &data);
1186 p = print_disjuncts_map(split[i].map, space, p, 0);
1189 return p;
1192 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
1193 __isl_take isl_printer *p)
1195 struct isl_print_space_data data = { 0 };
1196 struct isl_aff_split *split = NULL;
1197 int rational;
1199 if (!p || !map)
1200 return isl_printer_free(p);
1201 if (!p->dump && map->n > 0)
1202 split = split_aff(map);
1203 if (split) {
1204 p = print_split_map(p, split, map->n, map->dim);
1205 } else {
1206 rational = map->n > 0 &&
1207 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
1208 p = isl_print_space(map->dim, p, rational, &data);
1209 p = print_disjuncts_map(map, map->dim, p, 0);
1211 free_split(split, map->n);
1212 return p;
1215 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
1216 __isl_take isl_printer *p)
1218 struct isl_print_space_data data = { 0 };
1220 if (isl_map_dim(map, isl_dim_param) > 0) {
1221 p = print_tuple(map->dim, p, isl_dim_param, &data);
1222 p = isl_printer_print_str(p, s_to[0]);
1224 p = isl_printer_print_str(p, s_open_set[0]);
1225 p = isl_map_print_isl_body(map, p);
1226 p = isl_printer_print_str(p, s_close_set[0]);
1227 return p;
1230 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
1231 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
1233 struct isl_print_space_data data = { 0 };
1235 data.latex = 1;
1236 if (isl_map_dim(map, isl_dim_param) > 0) {
1237 p = print_tuple(map->dim, p, isl_dim_param, &data);
1238 p = isl_printer_print_str(p, s_to[1]);
1240 p = isl_printer_print_str(p, s_open_set[1]);
1241 data.print_dim = &print_dim_eq;
1242 data.user = aff;
1243 p = isl_print_space(map->dim, p, 0, &data);
1244 p = print_disjuncts_map(map, map->dim, p, 1);
1245 p = isl_printer_print_str(p, s_close_set[1]);
1247 return p;
1250 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
1251 __isl_take isl_printer *p)
1253 int i;
1254 struct isl_aff_split *split = NULL;
1256 if (map->n > 0)
1257 split = split_aff(map);
1259 if (!split)
1260 return print_latex_map(map, p, NULL);
1262 for (i = 0; i < map->n; ++i) {
1263 if (!split[i].map)
1264 break;
1265 if (i)
1266 p = isl_printer_print_str(p, " \\cup ");
1267 p = print_latex_map(split[i].map, p, split[i].aff);
1270 free_split(split, map->n);
1271 return p;
1274 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
1275 __isl_keep isl_basic_map *bmap)
1277 if (!p || !bmap)
1278 goto error;
1279 if (p->output_format == ISL_FORMAT_ISL)
1280 return isl_basic_map_print_isl(bmap, p, 0);
1281 else if (p->output_format == ISL_FORMAT_OMEGA)
1282 return basic_map_print_omega(bmap, p);
1283 isl_assert(bmap->ctx, 0, goto error);
1284 error:
1285 isl_printer_free(p);
1286 return NULL;
1289 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
1290 __isl_keep isl_basic_set *bset)
1292 if (!p || !bset)
1293 goto error;
1295 if (p->output_format == ISL_FORMAT_ISL)
1296 return isl_basic_map_print_isl(bset, p, 0);
1297 else if (p->output_format == ISL_FORMAT_POLYLIB)
1298 return isl_basic_set_print_polylib(bset, p, 0);
1299 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1300 return isl_basic_set_print_polylib(bset, p, 1);
1301 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
1302 return bset_print_constraints_polylib(bset, p);
1303 else if (p->output_format == ISL_FORMAT_OMEGA)
1304 return basic_set_print_omega(bset, p);
1305 isl_assert(p->ctx, 0, goto error);
1306 error:
1307 isl_printer_free(p);
1308 return NULL;
1311 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1312 __isl_keep isl_set *set)
1314 if (!p || !set)
1315 goto error;
1316 if (p->output_format == ISL_FORMAT_ISL)
1317 return isl_map_print_isl((isl_map *)set, p);
1318 else if (p->output_format == ISL_FORMAT_POLYLIB)
1319 return isl_set_print_polylib(set, p, 0);
1320 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1321 return isl_set_print_polylib(set, p, 1);
1322 else if (p->output_format == ISL_FORMAT_OMEGA)
1323 return isl_set_print_omega(set, p);
1324 else if (p->output_format == ISL_FORMAT_LATEX)
1325 return isl_map_print_latex((isl_map *)set, p);
1326 isl_assert(set->ctx, 0, goto error);
1327 error:
1328 isl_printer_free(p);
1329 return NULL;
1332 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1333 __isl_keep isl_map *map)
1335 if (!p || !map)
1336 goto error;
1338 if (p->output_format == ISL_FORMAT_ISL)
1339 return isl_map_print_isl(map, p);
1340 else if (p->output_format == ISL_FORMAT_POLYLIB)
1341 return isl_map_print_polylib(map, p, 0);
1342 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1343 return isl_map_print_polylib(map, p, 1);
1344 else if (p->output_format == ISL_FORMAT_OMEGA)
1345 return isl_map_print_omega(map, p);
1346 else if (p->output_format == ISL_FORMAT_LATEX)
1347 return isl_map_print_latex(map, p);
1348 isl_assert(map->ctx, 0, goto error);
1349 error:
1350 isl_printer_free(p);
1351 return NULL;
1354 struct isl_union_print_data {
1355 isl_printer *p;
1356 int first;
1359 static isl_stat print_map_body(__isl_take isl_map *map, void *user)
1361 struct isl_union_print_data *data;
1362 data = (struct isl_union_print_data *)user;
1364 if (!data->first)
1365 data->p = isl_printer_print_str(data->p, "; ");
1366 data->first = 0;
1368 data->p = isl_map_print_isl_body(map, data->p);
1369 isl_map_free(map);
1371 return isl_stat_ok;
1374 static __isl_give isl_printer *isl_union_map_print_isl(
1375 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1377 struct isl_union_print_data data;
1378 struct isl_print_space_data space_data = { 0 };
1379 isl_space *dim;
1381 dim = isl_union_map_get_space(umap);
1382 if (isl_space_dim(dim, isl_dim_param) > 0) {
1383 p = print_tuple(dim, p, isl_dim_param, &space_data);
1384 p = isl_printer_print_str(p, s_to[0]);
1386 isl_space_free(dim);
1387 p = isl_printer_print_str(p, s_open_set[0]);
1388 data.p = p;
1389 data.first = 1;
1390 isl_union_map_foreach_map(umap, &print_map_body, &data);
1391 p = data.p;
1392 p = isl_printer_print_str(p, s_close_set[0]);
1393 return p;
1396 static isl_stat print_latex_map_body(__isl_take isl_map *map, void *user)
1398 struct isl_union_print_data *data;
1399 data = (struct isl_union_print_data *)user;
1401 if (!data->first)
1402 data->p = isl_printer_print_str(data->p, " \\cup ");
1403 data->first = 0;
1405 data->p = isl_map_print_latex(map, data->p);
1406 isl_map_free(map);
1408 return isl_stat_ok;
1411 static __isl_give isl_printer *isl_union_map_print_latex(
1412 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1414 struct isl_union_print_data data = { p, 1 };
1415 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1416 p = data.p;
1417 return p;
1420 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1421 __isl_keep isl_union_map *umap)
1423 if (!p || !umap)
1424 goto error;
1426 if (p->output_format == ISL_FORMAT_ISL)
1427 return isl_union_map_print_isl(umap, p);
1428 if (p->output_format == ISL_FORMAT_LATEX)
1429 return isl_union_map_print_latex(umap, p);
1431 isl_die(p->ctx, isl_error_invalid,
1432 "invalid output format for isl_union_map", goto error);
1433 error:
1434 isl_printer_free(p);
1435 return NULL;
1438 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1439 __isl_keep isl_union_set *uset)
1441 if (!p || !uset)
1442 goto error;
1444 if (p->output_format == ISL_FORMAT_ISL)
1445 return isl_union_map_print_isl((isl_union_map *)uset, p);
1446 if (p->output_format == ISL_FORMAT_LATEX)
1447 return isl_union_map_print_latex((isl_union_map *)uset, p);
1449 isl_die(p->ctx, isl_error_invalid,
1450 "invalid output format for isl_union_set", goto error);
1451 error:
1452 isl_printer_free(p);
1453 return NULL;
1456 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1458 int i;
1459 int n;
1461 for (i = 0, n = 0; i < rec->n; ++i)
1462 if (!isl_upoly_is_zero(rec->p[i]))
1463 ++n;
1465 return n;
1468 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1469 __isl_take isl_printer *p, int first)
1471 struct isl_upoly_cst *cst;
1472 int neg;
1474 cst = isl_upoly_as_cst(up);
1475 if (!cst)
1476 goto error;
1477 neg = !first && isl_int_is_neg(cst->n);
1478 if (!first)
1479 p = isl_printer_print_str(p, neg ? " - " : " + ");
1480 if (neg)
1481 isl_int_neg(cst->n, cst->n);
1482 if (isl_int_is_zero(cst->d)) {
1483 int sgn = isl_int_sgn(cst->n);
1484 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1485 sgn == 0 ? "NaN" : "infty");
1486 } else
1487 p = isl_printer_print_isl_int(p, cst->n);
1488 if (neg)
1489 isl_int_neg(cst->n, cst->n);
1490 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1491 p = isl_printer_print_str(p, "/");
1492 p = isl_printer_print_isl_int(p, cst->d);
1494 return p;
1495 error:
1496 isl_printer_free(p);
1497 return NULL;
1500 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1501 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var)
1503 unsigned total;
1505 total = isl_space_dim(dim, isl_dim_all);
1506 if (var < total)
1507 p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 0);
1508 else
1509 p = print_div(dim, div, var - total, p);
1510 return p;
1513 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1514 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1516 p = print_base(p, dim, div, var);
1517 if (exp == 1)
1518 return p;
1519 if (p->output_format == ISL_FORMAT_C) {
1520 int i;
1521 for (i = 1; i < exp; ++i) {
1522 p = isl_printer_print_str(p, "*");
1523 p = print_base(p, dim, div, var);
1525 } else {
1526 p = isl_printer_print_str(p, "^");
1527 p = isl_printer_print_int(p, exp);
1529 return p;
1532 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1533 __isl_keep isl_space *dim, __isl_keep isl_mat *div,
1534 __isl_take isl_printer *p, int outer)
1536 int i, n, first, print_parens;
1537 struct isl_upoly_rec *rec;
1539 if (!p || !up || !dim || !div)
1540 goto error;
1542 if (isl_upoly_is_cst(up))
1543 return upoly_print_cst(up, p, 1);
1545 rec = isl_upoly_as_rec(up);
1546 if (!rec)
1547 goto error;
1548 n = upoly_rec_n_non_zero(rec);
1549 print_parens = n > 1 ||
1550 (outer && rec->up.var >= isl_space_dim(dim, isl_dim_all));
1551 if (print_parens)
1552 p = isl_printer_print_str(p, "(");
1553 for (i = 0, first = 1; i < rec->n; ++i) {
1554 if (isl_upoly_is_zero(rec->p[i]))
1555 continue;
1556 if (isl_upoly_is_negone(rec->p[i])) {
1557 if (!i)
1558 p = isl_printer_print_str(p, "-1");
1559 else if (first)
1560 p = isl_printer_print_str(p, "-");
1561 else
1562 p = isl_printer_print_str(p, " - ");
1563 } else if (isl_upoly_is_cst(rec->p[i]) &&
1564 !isl_upoly_is_one(rec->p[i]))
1565 p = upoly_print_cst(rec->p[i], p, first);
1566 else {
1567 if (!first)
1568 p = isl_printer_print_str(p, " + ");
1569 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1570 p = upoly_print(rec->p[i], dim, div, p, 0);
1572 first = 0;
1573 if (i == 0)
1574 continue;
1575 if (!isl_upoly_is_one(rec->p[i]) &&
1576 !isl_upoly_is_negone(rec->p[i]))
1577 p = isl_printer_print_str(p, " * ");
1578 p = print_pow(p, dim, div, rec->up.var, i);
1580 if (print_parens)
1581 p = isl_printer_print_str(p, ")");
1582 return p;
1583 error:
1584 isl_printer_free(p);
1585 return NULL;
1588 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1589 __isl_keep isl_qpolynomial *qp)
1591 if (!p || !qp)
1592 goto error;
1593 p = upoly_print(qp->upoly, qp->dim, qp->div, p, 1);
1594 return p;
1595 error:
1596 isl_printer_free(p);
1597 return NULL;
1600 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1601 __isl_keep isl_qpolynomial *qp)
1603 struct isl_print_space_data data = { 0 };
1605 if (!p || !qp)
1606 goto error;
1608 if (isl_space_dim(qp->dim, isl_dim_param) > 0) {
1609 p = print_tuple(qp->dim, p, isl_dim_param, &data);
1610 p = isl_printer_print_str(p, " -> ");
1612 p = isl_printer_print_str(p, "{ ");
1613 if (!isl_space_is_params(qp->dim)) {
1614 p = isl_print_space(qp->dim, p, 0, &data);
1615 p = isl_printer_print_str(p, " -> ");
1617 p = print_qpolynomial(p, qp);
1618 p = isl_printer_print_str(p, " }");
1619 return p;
1620 error:
1621 isl_printer_free(p);
1622 return NULL;
1625 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1626 __isl_keep isl_space *dim, __isl_keep isl_qpolynomial *qp)
1628 isl_int den;
1630 isl_int_init(den);
1631 isl_qpolynomial_get_den(qp, &den);
1632 if (!isl_int_is_one(den)) {
1633 isl_qpolynomial *f;
1634 p = isl_printer_print_str(p, "(");
1635 qp = isl_qpolynomial_copy(qp);
1636 f = isl_qpolynomial_rat_cst_on_domain(isl_space_copy(qp->dim),
1637 den, qp->dim->ctx->one);
1638 qp = isl_qpolynomial_mul(qp, f);
1640 if (qp)
1641 p = upoly_print(qp->upoly, dim, qp->div, p, 0);
1642 else
1643 p = isl_printer_free(p);
1644 if (!isl_int_is_one(den)) {
1645 p = isl_printer_print_str(p, ")/");
1646 p = isl_printer_print_isl_int(p, den);
1647 isl_qpolynomial_free(qp);
1649 isl_int_clear(den);
1650 return p;
1653 __isl_give isl_printer *isl_printer_print_qpolynomial(
1654 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1656 if (!p || !qp)
1657 goto error;
1659 if (p->output_format == ISL_FORMAT_ISL)
1660 return print_qpolynomial_isl(p, qp);
1661 else if (p->output_format == ISL_FORMAT_C)
1662 return print_qpolynomial_c(p, qp->dim, qp);
1663 else
1664 isl_die(qp->dim->ctx, isl_error_unsupported,
1665 "output format not supported for isl_qpolynomials",
1666 goto error);
1667 error:
1668 isl_printer_free(p);
1669 return NULL;
1672 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1673 unsigned output_format)
1675 isl_printer *p;
1677 if (!qp)
1678 return;
1680 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1681 p = isl_printer_to_file(qp->dim->ctx, out);
1682 p = isl_printer_print_qpolynomial(p, qp);
1683 isl_printer_free(p);
1686 static __isl_give isl_printer *qpolynomial_fold_print(
1687 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1689 int i;
1691 if (fold->type == isl_fold_min)
1692 p = isl_printer_print_str(p, "min");
1693 else if (fold->type == isl_fold_max)
1694 p = isl_printer_print_str(p, "max");
1695 p = isl_printer_print_str(p, "(");
1696 for (i = 0; i < fold->n; ++i) {
1697 if (i)
1698 p = isl_printer_print_str(p, ", ");
1699 p = print_qpolynomial(p, fold->qp[i]);
1701 p = isl_printer_print_str(p, ")");
1702 return p;
1705 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1706 FILE *out, unsigned output_format)
1708 isl_printer *p;
1710 if (!fold)
1711 return;
1713 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1715 p = isl_printer_to_file(fold->dim->ctx, out);
1716 p = isl_printer_print_qpolynomial_fold(p, fold);
1718 isl_printer_free(p);
1721 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1722 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1724 struct isl_print_space_data data = { 0 };
1725 int i = 0;
1727 for (i = 0; i < pwqp->n; ++i) {
1728 isl_space *space;
1730 if (i)
1731 p = isl_printer_print_str(p, "; ");
1732 space = isl_qpolynomial_get_domain_space(pwqp->p[i].qp);
1733 if (!isl_space_is_params(space)) {
1734 p = isl_print_space(space, p, 0, &data);
1735 p = isl_printer_print_str(p, " -> ");
1737 p = print_qpolynomial(p, pwqp->p[i].qp);
1738 p = print_disjuncts((isl_map *)pwqp->p[i].set, space, p, 0);
1739 isl_space_free(space);
1742 return p;
1745 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1746 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1748 struct isl_print_space_data data = { 0 };
1750 if (!p || !pwqp)
1751 goto error;
1753 if (isl_space_dim(pwqp->dim, isl_dim_param) > 0) {
1754 p = print_tuple(pwqp->dim, p, isl_dim_param, &data);
1755 p = isl_printer_print_str(p, " -> ");
1757 p = isl_printer_print_str(p, "{ ");
1758 if (pwqp->n == 0) {
1759 if (!isl_space_is_set(pwqp->dim)) {
1760 p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
1761 p = isl_printer_print_str(p, " -> ");
1763 p = isl_printer_print_str(p, "0");
1765 p = isl_pwqp_print_isl_body(p, pwqp);
1766 p = isl_printer_print_str(p, " }");
1767 return p;
1768 error:
1769 isl_printer_free(p);
1770 return NULL;
1773 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1774 unsigned output_format)
1776 isl_printer *p;
1778 if (!pwqp)
1779 return;
1781 p = isl_printer_to_file(pwqp->dim->ctx, out);
1782 p = isl_printer_set_output_format(p, output_format);
1783 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1785 isl_printer_free(p);
1788 static __isl_give isl_printer *isl_pwf_print_isl_body(
1789 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1791 struct isl_print_space_data data = { 0 };
1792 int i = 0;
1794 for (i = 0; i < pwf->n; ++i) {
1795 isl_space *space;
1797 if (i)
1798 p = isl_printer_print_str(p, "; ");
1799 space = isl_qpolynomial_fold_get_domain_space(pwf->p[i].fold);
1800 if (!isl_space_is_params(space)) {
1801 p = isl_print_space(space, p, 0, &data);
1802 p = isl_printer_print_str(p, " -> ");
1804 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1805 p = print_disjuncts((isl_map *)pwf->p[i].set, space, p, 0);
1806 isl_space_free(space);
1809 return p;
1812 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1813 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1815 struct isl_print_space_data data = { 0 };
1817 if (isl_space_dim(pwf->dim, isl_dim_param) > 0) {
1818 p = print_tuple(pwf->dim, p, isl_dim_param, &data);
1819 p = isl_printer_print_str(p, " -> ");
1821 p = isl_printer_print_str(p, "{ ");
1822 if (pwf->n == 0) {
1823 if (!isl_space_is_set(pwf->dim)) {
1824 p = print_tuple(pwf->dim, p, isl_dim_in, &data);
1825 p = isl_printer_print_str(p, " -> ");
1827 p = isl_printer_print_str(p, "0");
1829 p = isl_pwf_print_isl_body(p, pwf);
1830 p = isl_printer_print_str(p, " }");
1831 return p;
1834 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1835 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c);
1837 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1838 __isl_keep isl_space *dim,
1839 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1841 if (type == isl_dim_div) {
1842 p = isl_printer_print_str(p, "floord(");
1843 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
1844 p = isl_printer_print_str(p, ", ");
1845 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1846 p = isl_printer_print_str(p, ")");
1847 } else {
1848 const char *name;
1850 name = isl_space_get_dim_name(dim, type, pos);
1851 if (!name)
1852 name = "UNNAMED";
1853 p = isl_printer_print_str(p, name);
1855 return p;
1858 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1859 __isl_keep isl_space *dim,
1860 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1862 enum isl_dim_type type;
1864 if (pos == 0)
1865 return isl_printer_print_isl_int(p, c);
1867 if (isl_int_is_one(c))
1869 else if (isl_int_is_negone(c))
1870 p = isl_printer_print_str(p, "-");
1871 else {
1872 p = isl_printer_print_isl_int(p, c);
1873 p = isl_printer_print_str(p, "*");
1875 type = pos2type(dim, &pos);
1876 p = print_name_c(p, dim, bset, type, pos);
1877 return p;
1880 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
1881 __isl_keep isl_space *dim,
1882 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
1884 int i;
1885 int first;
1887 for (i = 0, first = 1; i < len; ++i) {
1888 int flip = 0;
1889 if (isl_int_is_zero(c[i]))
1890 continue;
1891 if (!first) {
1892 if (isl_int_is_neg(c[i])) {
1893 flip = 1;
1894 isl_int_neg(c[i], c[i]);
1895 p = isl_printer_print_str(p, " - ");
1896 } else
1897 p = isl_printer_print_str(p, " + ");
1899 first = 0;
1900 p = print_term_c(p, dim, bset, c[i], i);
1901 if (flip)
1902 isl_int_neg(c[i], c[i]);
1904 if (first)
1905 p = isl_printer_print_str(p, "0");
1906 return p;
1909 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1910 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c)
1912 unsigned len = 1 + isl_basic_set_total_dim(bset);
1913 return print_partial_affine_c(p, dim, bset, c, len);
1916 /* We skip the constraint if it is implied by the div expression.
1918 * *first indicates whether this is the first constraint in the conjunction and
1919 * is updated if the constraint is actually printed.
1921 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1922 __isl_keep isl_space *dim,
1923 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int *first)
1925 unsigned o_div;
1926 unsigned n_div;
1927 int div;
1929 o_div = isl_basic_set_offset(bset, isl_dim_div);
1930 n_div = isl_basic_set_dim(bset, isl_dim_div);
1931 div = isl_seq_last_non_zero(c + o_div, n_div);
1932 if (div >= 0 && isl_basic_set_is_div_constraint(bset, c, div))
1933 return p;
1935 if (!*first)
1936 p = isl_printer_print_str(p, " && ");
1938 p = print_affine_c(p, dim, bset, c);
1939 p = isl_printer_print_str(p, " ");
1940 p = isl_printer_print_str(p, op);
1941 p = isl_printer_print_str(p, " 0");
1943 *first = 0;
1945 return p;
1948 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1949 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset)
1951 int i, j;
1952 int first = 1;
1953 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
1954 unsigned total = isl_basic_set_total_dim(bset) - n_div;
1956 for (i = 0; i < bset->n_eq; ++i) {
1957 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
1958 if (j < 0)
1959 p = print_constraint_c(p, dim, bset,
1960 bset->eq[i], "==", &first);
1961 else {
1962 if (i)
1963 p = isl_printer_print_str(p, " && ");
1964 p = isl_printer_print_str(p, "(");
1965 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
1966 1 + total + j);
1967 p = isl_printer_print_str(p, ") % ");
1968 p = isl_printer_print_isl_int(p,
1969 bset->eq[i][1 + total + j]);
1970 p = isl_printer_print_str(p, " == 0");
1971 first = 0;
1974 for (i = 0; i < bset->n_ineq; ++i)
1975 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
1976 &first);
1977 return p;
1980 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1981 __isl_keep isl_space *dim, __isl_keep isl_set *set)
1983 int i;
1985 if (!set)
1986 return isl_printer_free(p);
1988 if (set->n == 0)
1989 p = isl_printer_print_str(p, "0");
1991 for (i = 0; i < set->n; ++i) {
1992 if (i)
1993 p = isl_printer_print_str(p, " || ");
1994 if (set->n > 1)
1995 p = isl_printer_print_str(p, "(");
1996 p = print_basic_set_c(p, dim, set->p[i]);
1997 if (set->n > 1)
1998 p = isl_printer_print_str(p, ")");
2000 return p;
2003 static __isl_give isl_printer *print_pw_qpolynomial_c(
2004 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2006 int i;
2008 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set))
2009 return print_qpolynomial_c(p, pwqp->dim, pwqp->p[0].qp);
2011 for (i = 0; i < pwqp->n; ++i) {
2012 p = isl_printer_print_str(p, "(");
2013 p = print_set_c(p, pwqp->dim, pwqp->p[i].set);
2014 p = isl_printer_print_str(p, ") ? (");
2015 p = print_qpolynomial_c(p, pwqp->dim, pwqp->p[i].qp);
2016 p = isl_printer_print_str(p, ") : ");
2019 p = isl_printer_print_str(p, "0");
2020 return p;
2023 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
2024 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2026 if (!p || !pwqp)
2027 goto error;
2029 if (p->output_format == ISL_FORMAT_ISL)
2030 return print_pw_qpolynomial_isl(p, pwqp);
2031 else if (p->output_format == ISL_FORMAT_C)
2032 return print_pw_qpolynomial_c(p, pwqp);
2033 isl_assert(p->ctx, 0, goto error);
2034 error:
2035 isl_printer_free(p);
2036 return NULL;
2039 static isl_stat print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
2041 struct isl_union_print_data *data;
2042 data = (struct isl_union_print_data *)user;
2044 if (!data->first)
2045 data->p = isl_printer_print_str(data->p, "; ");
2046 data->first = 0;
2048 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
2049 isl_pw_qpolynomial_free(pwqp);
2051 return isl_stat_ok;
2054 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
2055 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2057 struct isl_union_print_data data;
2058 struct isl_print_space_data space_data = { 0 };
2059 isl_space *dim;
2061 dim = isl_union_pw_qpolynomial_get_space(upwqp);
2062 if (isl_space_dim(dim, isl_dim_param) > 0) {
2063 p = print_tuple(dim, p, isl_dim_param, &space_data);
2064 p = isl_printer_print_str(p, " -> ");
2066 isl_space_free(dim);
2067 p = isl_printer_print_str(p, "{ ");
2068 data.p = p;
2069 data.first = 1;
2070 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
2071 &data);
2072 p = data.p;
2073 p = isl_printer_print_str(p, " }");
2074 return p;
2077 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
2078 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2080 if (!p || !upwqp)
2081 goto error;
2083 if (p->output_format == ISL_FORMAT_ISL)
2084 return print_union_pw_qpolynomial_isl(p, upwqp);
2085 isl_die(p->ctx, isl_error_invalid,
2086 "invalid output format for isl_union_pw_qpolynomial",
2087 goto error);
2088 error:
2089 isl_printer_free(p);
2090 return NULL;
2093 static __isl_give isl_printer *print_qpolynomial_fold_c(
2094 __isl_take isl_printer *p, __isl_keep isl_space *dim,
2095 __isl_keep isl_qpolynomial_fold *fold)
2097 int i;
2099 for (i = 0; i < fold->n - 1; ++i)
2100 if (fold->type == isl_fold_min)
2101 p = isl_printer_print_str(p, "min(");
2102 else if (fold->type == isl_fold_max)
2103 p = isl_printer_print_str(p, "max(");
2105 for (i = 0; i < fold->n; ++i) {
2106 if (i)
2107 p = isl_printer_print_str(p, ", ");
2108 p = print_qpolynomial_c(p, dim, fold->qp[i]);
2109 if (i)
2110 p = isl_printer_print_str(p, ")");
2112 return p;
2115 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
2116 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
2118 if (!p || !fold)
2119 goto error;
2120 if (p->output_format == ISL_FORMAT_ISL)
2121 return qpolynomial_fold_print(fold, p);
2122 else if (p->output_format == ISL_FORMAT_C)
2123 return print_qpolynomial_fold_c(p, fold->dim, fold);
2124 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2125 goto error);
2126 error:
2127 isl_printer_free(p);
2128 return NULL;
2131 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
2132 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2134 int i;
2136 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set))
2137 return print_qpolynomial_fold_c(p, pwf->dim, pwf->p[0].fold);
2139 for (i = 0; i < pwf->n; ++i) {
2140 p = isl_printer_print_str(p, "(");
2141 p = print_set_c(p, pwf->dim, pwf->p[i].set);
2142 p = isl_printer_print_str(p, ") ? (");
2143 p = print_qpolynomial_fold_c(p, pwf->dim, pwf->p[i].fold);
2144 p = isl_printer_print_str(p, ") : ");
2147 p = isl_printer_print_str(p, "0");
2148 return p;
2151 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
2152 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2154 if (!p || !pwf)
2155 goto error;
2157 if (p->output_format == ISL_FORMAT_ISL)
2158 return print_pw_qpolynomial_fold_isl(p, pwf);
2159 else if (p->output_format == ISL_FORMAT_C)
2160 return print_pw_qpolynomial_fold_c(p, pwf);
2161 isl_assert(p->ctx, 0, goto error);
2162 error:
2163 isl_printer_free(p);
2164 return NULL;
2167 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
2168 FILE *out, unsigned output_format)
2170 isl_printer *p;
2172 if (!pwf)
2173 return;
2175 p = isl_printer_to_file(pwf->dim->ctx, out);
2176 p = isl_printer_set_output_format(p, output_format);
2177 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
2179 isl_printer_free(p);
2182 static isl_stat print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf,
2183 void *user)
2185 struct isl_union_print_data *data;
2186 data = (struct isl_union_print_data *)user;
2188 if (!data->first)
2189 data->p = isl_printer_print_str(data->p, "; ");
2190 data->first = 0;
2192 data->p = isl_pwf_print_isl_body(data->p, pwf);
2193 isl_pw_qpolynomial_fold_free(pwf);
2195 return isl_stat_ok;
2198 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2199 __isl_take isl_printer *p,
2200 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2202 struct isl_union_print_data data;
2203 struct isl_print_space_data space_data = { 0 };
2204 isl_space *dim;
2206 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
2207 if (isl_space_dim(dim, isl_dim_param) > 0) {
2208 p = print_tuple(dim, p, isl_dim_param, &space_data);
2209 p = isl_printer_print_str(p, " -> ");
2211 isl_space_free(dim);
2212 p = isl_printer_print_str(p, "{ ");
2213 data.p = p;
2214 data.first = 1;
2215 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2216 &print_pwf_body, &data);
2217 p = data.p;
2218 p = isl_printer_print_str(p, " }");
2219 return p;
2222 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2223 __isl_take isl_printer *p,
2224 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2226 if (!p || !upwf)
2227 goto error;
2229 if (p->output_format == ISL_FORMAT_ISL)
2230 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2231 isl_die(p->ctx, isl_error_invalid,
2232 "invalid output format for isl_union_pw_qpolynomial_fold",
2233 goto error);
2234 error:
2235 isl_printer_free(p);
2236 return NULL;
2239 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
2240 __isl_keep isl_constraint *c)
2242 isl_basic_map *bmap;
2244 if (!p || !c)
2245 goto error;
2247 bmap = isl_basic_map_from_constraint(isl_constraint_copy(c));
2248 p = isl_printer_print_basic_map(p, bmap);
2249 isl_basic_map_free(bmap);
2250 return p;
2251 error:
2252 isl_printer_free(p);
2253 return NULL;
2256 static __isl_give isl_printer *isl_printer_print_space_isl(
2257 __isl_take isl_printer *p, __isl_keep isl_space *space)
2259 struct isl_print_space_data data = { 0 };
2261 if (!space)
2262 goto error;
2264 if (isl_space_dim(space, isl_dim_param) > 0) {
2265 p = print_tuple(space, p, isl_dim_param, &data);
2266 p = isl_printer_print_str(p, " -> ");
2269 p = isl_printer_print_str(p, "{ ");
2270 if (isl_space_is_params(space))
2271 p = isl_printer_print_str(p, s_such_that[0]);
2272 else
2273 p = isl_print_space(space, p, 0, &data);
2274 p = isl_printer_print_str(p, " }");
2276 return p;
2277 error:
2278 isl_printer_free(p);
2279 return NULL;
2282 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
2283 __isl_keep isl_space *space)
2285 if (!p || !space)
2286 return isl_printer_free(p);
2287 if (p->output_format == ISL_FORMAT_ISL)
2288 return isl_printer_print_space_isl(p, space);
2289 else if (p->output_format == ISL_FORMAT_OMEGA)
2290 return print_omega_parameters(space, p);
2292 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
2293 "output format not supported for space",
2294 return isl_printer_free(p));
2297 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
2298 __isl_keep isl_local_space *ls)
2300 struct isl_print_space_data data = { 0 };
2301 unsigned n_div;
2303 if (!ls)
2304 goto error;
2306 if (isl_local_space_dim(ls, isl_dim_param) > 0) {
2307 p = print_tuple(ls->dim, p, isl_dim_param, &data);
2308 p = isl_printer_print_str(p, " -> ");
2310 p = isl_printer_print_str(p, "{ ");
2311 p = isl_print_space(ls->dim, p, 0, &data);
2312 n_div = isl_local_space_dim(ls, isl_dim_div);
2313 if (n_div > 0) {
2314 p = isl_printer_print_str(p, " : ");
2315 p = isl_printer_print_str(p, s_open_exists[0]);
2316 p = print_div_list(p, ls->dim, ls->div, 0, 1);
2317 p = isl_printer_print_str(p, s_close_exists[0]);
2318 } else if (isl_space_is_params(ls->dim))
2319 p = isl_printer_print_str(p, s_such_that[0]);
2320 p = isl_printer_print_str(p, " }");
2321 return p;
2322 error:
2323 isl_printer_free(p);
2324 return NULL;
2327 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2328 __isl_keep isl_aff *aff)
2330 unsigned total;
2332 if (isl_aff_is_nan(aff))
2333 return isl_printer_print_str(p, "NaN");
2335 total = isl_local_space_dim(aff->ls, isl_dim_all);
2336 p = isl_printer_print_str(p, "(");
2337 p = print_affine_of_len(aff->ls->dim, aff->ls->div, p,
2338 aff->v->el + 1, 1 + total);
2339 if (isl_int_is_one(aff->v->el[0]))
2340 p = isl_printer_print_str(p, ")");
2341 else {
2342 p = isl_printer_print_str(p, ")/");
2343 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2346 return p;
2349 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2350 __isl_keep isl_aff *aff)
2352 struct isl_print_space_data data = { 0 };
2354 if (isl_space_is_params(aff->ls->dim))
2356 else {
2357 p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
2358 p = isl_printer_print_str(p, " -> ");
2360 p = isl_printer_print_str(p, "[");
2361 p = print_aff_body(p, aff);
2362 p = isl_printer_print_str(p, "]");
2364 return p;
2367 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2368 __isl_keep isl_aff *aff)
2370 struct isl_print_space_data data = { 0 };
2372 if (!aff)
2373 goto error;
2375 if (isl_local_space_dim(aff->ls, isl_dim_param) > 0) {
2376 p = print_tuple(aff->ls->dim, p, isl_dim_param, &data);
2377 p = isl_printer_print_str(p, " -> ");
2379 p = isl_printer_print_str(p, "{ ");
2380 p = print_aff(p, aff);
2381 p = isl_printer_print_str(p, " }");
2382 return p;
2383 error:
2384 isl_printer_free(p);
2385 return NULL;
2388 /* Print the body of an isl_pw_aff, i.e., a semicolon delimited
2389 * sequence of affine expressions, each followed by constraints.
2391 static __isl_give isl_printer *print_pw_aff_body(
2392 __isl_take isl_printer *p, __isl_keep isl_pw_aff *pa)
2394 int i;
2396 if (!pa)
2397 return isl_printer_free(p);
2399 for (i = 0; i < pa->n; ++i) {
2400 isl_space *space;
2402 if (i)
2403 p = isl_printer_print_str(p, "; ");
2404 p = print_aff(p, pa->p[i].aff);
2405 space = isl_aff_get_domain_space(pa->p[i].aff);
2406 p = print_disjuncts((isl_map *)pa->p[i].set, space, p, 0);
2407 isl_space_free(space);
2410 return p;
2413 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2414 __isl_keep isl_pw_aff *pwaff)
2416 struct isl_print_space_data data = { 0 };
2418 if (!pwaff)
2419 goto error;
2421 if (isl_space_dim(pwaff->dim, isl_dim_param) > 0) {
2422 p = print_tuple(pwaff->dim, p, isl_dim_param, &data);
2423 p = isl_printer_print_str(p, " -> ");
2425 p = isl_printer_print_str(p, "{ ");
2426 p = print_pw_aff_body(p, pwaff);
2427 p = isl_printer_print_str(p, " }");
2428 return p;
2429 error:
2430 isl_printer_free(p);
2431 return NULL;
2434 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2435 __isl_keep isl_local_space *ls, isl_int *c);
2437 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2438 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2440 if (type == isl_dim_div) {
2441 p = isl_printer_print_str(p, "floord(");
2442 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2443 p = isl_printer_print_str(p, ", ");
2444 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2445 p = isl_printer_print_str(p, ")");
2446 } else {
2447 const char *name;
2449 name = isl_space_get_dim_name(ls->dim, type, pos);
2450 if (!name)
2451 name = "UNNAMED";
2452 p = isl_printer_print_str(p, name);
2454 return p;
2457 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2458 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2460 enum isl_dim_type type;
2462 if (pos == 0)
2463 return isl_printer_print_isl_int(p, c);
2465 if (isl_int_is_one(c))
2467 else if (isl_int_is_negone(c))
2468 p = isl_printer_print_str(p, "-");
2469 else {
2470 p = isl_printer_print_isl_int(p, c);
2471 p = isl_printer_print_str(p, "*");
2473 type = pos2type(ls->dim, &pos);
2474 p = print_ls_name_c(p, ls, type, pos);
2475 return p;
2478 static __isl_give isl_printer *print_ls_partial_affine_c(
2479 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2480 isl_int *c, unsigned len)
2482 int i;
2483 int first;
2485 for (i = 0, first = 1; i < len; ++i) {
2486 int flip = 0;
2487 if (isl_int_is_zero(c[i]))
2488 continue;
2489 if (!first) {
2490 if (isl_int_is_neg(c[i])) {
2491 flip = 1;
2492 isl_int_neg(c[i], c[i]);
2493 p = isl_printer_print_str(p, " - ");
2494 } else
2495 p = isl_printer_print_str(p, " + ");
2497 first = 0;
2498 p = print_ls_term_c(p, ls, c[i], i);
2499 if (flip)
2500 isl_int_neg(c[i], c[i]);
2502 if (first)
2503 p = isl_printer_print_str(p, "0");
2504 return p;
2507 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2508 __isl_keep isl_local_space *ls, isl_int *c)
2510 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2511 return print_ls_partial_affine_c(p, ls, c, len);
2514 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2515 __isl_keep isl_aff *aff)
2517 unsigned total;
2519 total = isl_local_space_dim(aff->ls, isl_dim_all);
2520 if (!isl_int_is_one(aff->v->el[0]))
2521 p = isl_printer_print_str(p, "(");
2522 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2523 if (!isl_int_is_one(aff->v->el[0])) {
2524 p = isl_printer_print_str(p, ")/");
2525 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2527 return p;
2530 /* In the C format, we cannot express that "pwaff" may be undefined
2531 * on parts of the domain space. We therefore assume that the expression
2532 * will only be evaluated on its definition domain and compute the gist
2533 * of each cell with respect to this domain.
2535 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2536 __isl_keep isl_pw_aff *pwaff)
2538 isl_set *domain;
2539 isl_ast_build *build;
2540 isl_ast_expr *expr;
2542 if (pwaff->n < 1)
2543 isl_die(p->ctx, isl_error_unsupported,
2544 "cannot print empty isl_pw_aff in C format",
2545 return isl_printer_free(p));
2547 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2548 build = isl_ast_build_from_context(domain);
2549 expr = isl_ast_build_expr_from_pw_aff(build, isl_pw_aff_copy(pwaff));
2550 p = isl_printer_print_ast_expr(p, expr);
2551 isl_ast_expr_free(expr);
2552 isl_ast_build_free(build);
2554 return p;
2557 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2558 __isl_keep isl_aff *aff)
2560 if (!p || !aff)
2561 goto error;
2563 if (p->output_format == ISL_FORMAT_ISL)
2564 return print_aff_isl(p, aff);
2565 else if (p->output_format == ISL_FORMAT_C)
2566 return print_aff_c(p, aff);
2567 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2568 goto error);
2569 error:
2570 isl_printer_free(p);
2571 return NULL;
2574 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2575 __isl_keep isl_pw_aff *pwaff)
2577 if (!p || !pwaff)
2578 goto error;
2580 if (p->output_format == ISL_FORMAT_ISL)
2581 return print_pw_aff_isl(p, pwaff);
2582 else if (p->output_format == ISL_FORMAT_C)
2583 return print_pw_aff_c(p, pwaff);
2584 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2585 goto error);
2586 error:
2587 isl_printer_free(p);
2588 return NULL;
2591 /* Print "pa" in a sequence of isl_pw_affs delimited by semicolons.
2592 * Each isl_pw_aff itself is also printed as semicolon delimited
2593 * sequence of pieces.
2594 * If data->first = 1, then this is the first in the sequence.
2595 * Update data->first to tell the next element that it is not the first.
2597 static isl_stat print_pw_aff_body_wrap(__isl_take isl_pw_aff *pa,
2598 void *user)
2600 struct isl_union_print_data *data;
2601 data = (struct isl_union_print_data *) user;
2603 if (!data->first)
2604 data->p = isl_printer_print_str(data->p, "; ");
2605 data->first = 0;
2607 data->p = print_pw_aff_body(data->p, pa);
2608 isl_pw_aff_free(pa);
2610 return data->p ? isl_stat_ok : isl_stat_error;
2613 /* Print the body of an isl_union_pw_aff, i.e., a semicolon delimited
2614 * sequence of affine expressions, each followed by constraints,
2615 * with the sequence enclosed in braces.
2617 static __isl_give isl_printer *print_union_pw_aff_body(
2618 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2620 struct isl_union_print_data data = { p, 1 };
2622 p = isl_printer_print_str(p, s_open_set[0]);
2623 data.p = p;
2624 if (isl_union_pw_aff_foreach_pw_aff(upa,
2625 &print_pw_aff_body_wrap, &data) < 0)
2626 data.p = isl_printer_free(p);
2627 p = data.p;
2628 p = isl_printer_print_str(p, s_close_set[0]);
2630 return p;
2633 /* Print the isl_union_pw_aff "upa" to "p" in isl format.
2635 * The individual isl_pw_affs are delimited by a semicolon.
2637 static __isl_give isl_printer *print_union_pw_aff_isl(
2638 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2640 struct isl_print_space_data data = { 0 };
2641 isl_space *space;
2643 space = isl_union_pw_aff_get_space(upa);
2644 if (isl_space_dim(space, isl_dim_param) > 0) {
2645 p = print_tuple(space, p, isl_dim_param, &data);
2646 p = isl_printer_print_str(p, s_to[0]);
2648 isl_space_free(space);
2649 p = print_union_pw_aff_body(p, upa);
2650 return p;
2653 /* Print the isl_union_pw_aff "upa" to "p".
2655 * We currently only support an isl format.
2657 __isl_give isl_printer *isl_printer_print_union_pw_aff(
2658 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2660 if (!p || !upa)
2661 return isl_printer_free(p);
2663 if (p->output_format == ISL_FORMAT_ISL)
2664 return print_union_pw_aff_isl(p, upa);
2665 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2666 "unsupported output format", return isl_printer_free(p));
2669 /* Print dimension "pos" of data->space to "p".
2671 * data->user is assumed to be an isl_multi_aff.
2673 * If the current dimension is an output dimension, then print
2674 * the corresponding expression. Otherwise, print the name of the dimension.
2676 static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
2677 struct isl_print_space_data *data, unsigned pos)
2679 isl_multi_aff *ma = data->user;
2681 if (data->type == isl_dim_out)
2682 p = print_aff_body(p, ma->p[pos]);
2683 else
2684 p = print_name(data->space, p, data->type, pos, data->latex);
2686 return p;
2689 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2690 __isl_keep isl_multi_aff *maff)
2692 struct isl_print_space_data data = { 0 };
2694 data.print_dim = &print_dim_ma;
2695 data.user = maff;
2696 return isl_print_space(maff->space, p, 0, &data);
2699 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2700 __isl_keep isl_multi_aff *maff)
2702 struct isl_print_space_data data = { 0 };
2704 if (!maff)
2705 goto error;
2707 if (isl_space_dim(maff->space, isl_dim_param) > 0) {
2708 p = print_tuple(maff->space, p, isl_dim_param, &data);
2709 p = isl_printer_print_str(p, " -> ");
2711 p = isl_printer_print_str(p, "{ ");
2712 p = print_multi_aff(p, maff);
2713 p = isl_printer_print_str(p, " }");
2714 return p;
2715 error:
2716 isl_printer_free(p);
2717 return NULL;
2720 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2721 __isl_keep isl_multi_aff *maff)
2723 if (!p || !maff)
2724 goto error;
2726 if (p->output_format == ISL_FORMAT_ISL)
2727 return print_multi_aff_isl(p, maff);
2728 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2729 goto error);
2730 error:
2731 isl_printer_free(p);
2732 return NULL;
2735 static __isl_give isl_printer *print_pw_multi_aff_body(
2736 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2738 int i;
2740 if (!pma)
2741 goto error;
2743 for (i = 0; i < pma->n; ++i) {
2744 isl_space *space;
2746 if (i)
2747 p = isl_printer_print_str(p, "; ");
2748 p = print_multi_aff(p, pma->p[i].maff);
2749 space = isl_multi_aff_get_domain_space(pma->p[i].maff);
2750 p = print_disjuncts((isl_map *)pma->p[i].set, space, p, 0);
2751 isl_space_free(space);
2753 return p;
2754 error:
2755 isl_printer_free(p);
2756 return NULL;
2759 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
2760 __isl_keep isl_pw_multi_aff *pma)
2762 struct isl_print_space_data data = { 0 };
2764 if (!pma)
2765 goto error;
2767 if (isl_space_dim(pma->dim, isl_dim_param) > 0) {
2768 p = print_tuple(pma->dim, p, isl_dim_param, &data);
2769 p = isl_printer_print_str(p, " -> ");
2771 p = isl_printer_print_str(p, "{ ");
2772 p = print_pw_multi_aff_body(p, pma);
2773 p = isl_printer_print_str(p, " }");
2774 return p;
2775 error:
2776 isl_printer_free(p);
2777 return NULL;
2780 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
2781 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2783 int i;
2785 for (i = 0; i < pma->n - 1; ++i) {
2786 p = isl_printer_print_str(p, "(");
2787 p = print_set_c(p, pma->dim, pma->p[i].set);
2788 p = isl_printer_print_str(p, ") ? (");
2789 p = print_aff_c(p, pma->p[i].maff->p[0]);
2790 p = isl_printer_print_str(p, ") : ");
2793 return print_aff_c(p, pma->p[pma->n - 1].maff->p[0]);
2796 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
2797 __isl_keep isl_pw_multi_aff *pma)
2799 int n;
2800 const char *name;
2802 if (!pma)
2803 goto error;
2804 if (pma->n < 1)
2805 isl_die(p->ctx, isl_error_unsupported,
2806 "cannot print empty isl_pw_multi_aff in C format",
2807 goto error);
2808 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
2809 if (!name && isl_pw_multi_aff_dim(pma, isl_dim_out) == 1)
2810 return print_unnamed_pw_multi_aff_c(p, pma);
2811 if (!name)
2812 isl_die(p->ctx, isl_error_unsupported,
2813 "cannot print unnamed isl_pw_multi_aff in C format",
2814 goto error);
2816 p = isl_printer_print_str(p, name);
2817 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
2818 if (n != 0)
2819 isl_die(p->ctx, isl_error_unsupported,
2820 "not supported yet", goto error);
2822 return p;
2823 error:
2824 isl_printer_free(p);
2825 return NULL;
2828 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
2829 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2831 if (!p || !pma)
2832 goto error;
2834 if (p->output_format == ISL_FORMAT_ISL)
2835 return print_pw_multi_aff_isl(p, pma);
2836 if (p->output_format == ISL_FORMAT_C)
2837 return print_pw_multi_aff_c(p, pma);
2838 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2839 goto error);
2840 error:
2841 isl_printer_free(p);
2842 return NULL;
2845 static isl_stat print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
2846 void *user)
2848 struct isl_union_print_data *data;
2849 data = (struct isl_union_print_data *) user;
2851 if (!data->first)
2852 data->p = isl_printer_print_str(data->p, "; ");
2853 data->first = 0;
2855 data->p = print_pw_multi_aff_body(data->p, pma);
2856 isl_pw_multi_aff_free(pma);
2858 return isl_stat_ok;
2861 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
2862 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2864 struct isl_union_print_data data;
2865 struct isl_print_space_data space_data = { 0 };
2866 isl_space *space;
2868 space = isl_union_pw_multi_aff_get_space(upma);
2869 if (isl_space_dim(space, isl_dim_param) > 0) {
2870 p = print_tuple(space, p, isl_dim_param, &space_data);
2871 p = isl_printer_print_str(p, s_to[0]);
2873 isl_space_free(space);
2874 p = isl_printer_print_str(p, s_open_set[0]);
2875 data.p = p;
2876 data.first = 1;
2877 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
2878 &print_pw_multi_aff_body_wrap, &data);
2879 p = data.p;
2880 p = isl_printer_print_str(p, s_close_set[0]);
2881 return p;
2884 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
2885 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2887 if (!p || !upma)
2888 goto error;
2890 if (p->output_format == ISL_FORMAT_ISL)
2891 return print_union_pw_multi_aff_isl(p, upma);
2892 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2893 goto error);
2894 error:
2895 isl_printer_free(p);
2896 return NULL;
2899 /* Print dimension "pos" of data->space to "p".
2901 * data->user is assumed to be an isl_multi_pw_aff.
2903 * If the current dimension is an output dimension, then print
2904 * the corresponding piecewise affine expression.
2905 * Otherwise, print the name of the dimension.
2907 static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
2908 struct isl_print_space_data *data, unsigned pos)
2910 int i;
2911 int need_parens;
2912 isl_multi_pw_aff *mpa = data->user;
2913 isl_pw_aff *pa;
2915 if (data->type != isl_dim_out)
2916 return print_name(data->space, p, data->type, pos, data->latex);
2918 pa = mpa->p[pos];
2919 if (pa->n == 0)
2920 return isl_printer_print_str(p, "(0 : 1 = 0)");
2922 need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
2923 if (need_parens)
2924 p = isl_printer_print_str(p, "(");
2925 for (i = 0; i < pa->n; ++i) {
2926 isl_space *space;
2928 if (i)
2929 p = isl_printer_print_str(p, "; ");
2930 p = print_aff_body(p, pa->p[i].aff);
2931 space = isl_aff_get_domain_space(pa->p[i].aff);
2932 p = print_disjuncts(pa->p[i].set, space, p, 0);
2933 isl_space_free(space);
2935 if (need_parens)
2936 p = isl_printer_print_str(p, ")");
2938 return p;
2941 /* Print "mpa" to "p" in isl format.
2943 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
2944 __isl_keep isl_multi_pw_aff *mpa)
2946 struct isl_print_space_data data = { 0 };
2948 if (!mpa)
2949 return isl_printer_free(p);
2951 if (isl_space_dim(mpa->space, isl_dim_param) > 0) {
2952 p = print_tuple(mpa->space, p, isl_dim_param, &data);
2953 p = isl_printer_print_str(p, " -> ");
2955 p = isl_printer_print_str(p, "{ ");
2956 data.print_dim = &print_dim_mpa;
2957 data.user = mpa;
2958 p = isl_print_space(mpa->space, p, 0, &data);
2959 p = isl_printer_print_str(p, " }");
2960 return p;
2963 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
2964 __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
2966 if (!p || !mpa)
2967 return isl_printer_free(p);
2969 if (p->output_format == ISL_FORMAT_ISL)
2970 return print_multi_pw_aff_isl(p, mpa);
2971 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2972 return isl_printer_free(p));
2975 /* Print dimension "pos" of data->space to "p".
2977 * data->user is assumed to be an isl_multi_val.
2979 * If the current dimension is an output dimension, then print
2980 * the corresponding value. Otherwise, print the name of the dimension.
2982 static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
2983 struct isl_print_space_data *data, unsigned pos)
2985 isl_multi_val *mv = data->user;
2987 if (data->type == isl_dim_out)
2988 return isl_printer_print_val(p, mv->p[pos]);
2989 else
2990 return print_name(data->space, p, data->type, pos, data->latex);
2993 /* Print the isl_multi_val "mv" to "p" in isl format.
2995 static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
2996 __isl_keep isl_multi_val *mv)
2998 struct isl_print_space_data data = { 0 };
3000 if (!mv)
3001 return isl_printer_free(p);
3003 if (isl_space_dim(mv->space, isl_dim_param) > 0) {
3004 p = print_tuple(mv->space, p, isl_dim_param, &data);
3005 p = isl_printer_print_str(p, " -> ");
3007 p = isl_printer_print_str(p, "{ ");
3008 data.print_dim = &print_dim_mv;
3009 data.user = mv;
3010 p = isl_print_space(mv->space, p, 0, &data);
3011 p = isl_printer_print_str(p, " }");
3012 return p;
3015 /* Print the isl_multi_val "mv" to "p".
3017 * Currently only supported in isl format.
3019 __isl_give isl_printer *isl_printer_print_multi_val(
3020 __isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
3022 if (!p || !mv)
3023 return isl_printer_free(p);
3025 if (p->output_format == ISL_FORMAT_ISL)
3026 return print_multi_val_isl(p, mv);
3027 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3028 return isl_printer_free(p));
3031 /* Print dimension "pos" of data->space to "p".
3033 * data->user is assumed to be an isl_multi_union_pw_aff.
3035 * The current dimension is necessarily a set dimension, so
3036 * we print the corresponding isl_union_pw_aff, including
3037 * the braces.
3039 static __isl_give isl_printer *print_union_pw_aff_dim(__isl_take isl_printer *p,
3040 struct isl_print_space_data *data, unsigned pos)
3042 isl_multi_union_pw_aff *mupa = data->user;
3043 isl_union_pw_aff *upa;
3045 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, pos);
3046 p = print_union_pw_aff_body(p, upa);
3047 isl_union_pw_aff_free(upa);
3049 return p;
3052 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3054 static __isl_give isl_printer *print_multi_union_pw_aff_isl(
3055 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3057 struct isl_print_space_data data = { 0 };
3058 isl_space *space;
3060 space = isl_multi_union_pw_aff_get_space(mupa);
3061 if (isl_space_dim(space, isl_dim_param) > 0) {
3062 struct isl_print_space_data space_data = { 0 };
3063 p = print_tuple(space, p, isl_dim_param, &space_data);
3064 p = isl_printer_print_str(p, s_to[0]);
3067 data.print_dim = &print_union_pw_aff_dim;
3068 data.user = mupa;
3070 p = isl_print_space(space, p, 0, &data);
3071 isl_space_free(space);
3073 return p;
3076 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3078 * We currently only support an isl format.
3080 __isl_give isl_printer *isl_printer_print_multi_union_pw_aff(
3081 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3083 if (!p || !mupa)
3084 return isl_printer_free(p);
3086 if (p->output_format == ISL_FORMAT_ISL)
3087 return print_multi_union_pw_aff_isl(p, mupa);
3088 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
3089 "unsupported output format", return isl_printer_free(p));