isl_input.c: accept_affine: fix memory leak on error path
[isl.git] / isl_output.c
blob558ec8ac8c2b2a6a51e5ff09db05795473a6dcd7
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include <string.h>
14 #include <isl_set.h>
15 #include <isl_seq.h>
17 static void print_constraint_polylib(struct isl_basic_map *bmap,
18 int ineq, int n,
19 FILE *out, int indent, const char *prefix, const char *suffix)
21 int i;
22 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
23 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
24 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
25 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
27 fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
28 fprintf(out, "%d", ineq);
29 for (i = 0; i < n_out; ++i) {
30 fprintf(out, " ");
31 isl_int_print(out, c[1+nparam+n_in+i], 5);
33 for (i = 0; i < n_in; ++i) {
34 fprintf(out, " ");
35 isl_int_print(out, c[1+nparam+i], 5);
37 for (i = 0; i < bmap->n_div; ++i) {
38 fprintf(out, " ");
39 isl_int_print(out, c[1+nparam+n_in+n_out+i], 5);
41 for (i = 0; i < nparam; ++i) {
42 fprintf(out, " ");
43 isl_int_print(out, c[1+i], 5);
45 fprintf(out, " ");
46 isl_int_print(out, c[0], 5);
47 fprintf(out, "%s\n", suffix ? suffix : "");
50 static void print_constraints_polylib(struct isl_basic_map *bmap,
51 FILE *out, int indent, const char *prefix, const char *suffix)
53 int i;
55 for (i = 0; i < bmap->n_eq; ++i)
56 print_constraint_polylib(bmap, 0, i, out,
57 indent, prefix, suffix);
58 for (i = 0; i < bmap->n_ineq; ++i)
59 print_constraint_polylib(bmap, 1, i, out,
60 indent, prefix, suffix);
63 static void bset_print_constraints_polylib(struct isl_basic_set *bset,
64 FILE *out, int indent, const char *prefix, const char *suffix)
66 print_constraints_polylib((struct isl_basic_map *)bset,
67 out, indent, prefix, suffix);
70 static void isl_basic_map_print_polylib(struct isl_basic_map *bmap, FILE *out,
71 int indent, const char *prefix, const char *suffix)
73 unsigned total = isl_basic_map_total_dim(bmap);
74 fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
75 fprintf(out, "%d %d", bmap->n_eq + bmap->n_ineq, 1 + total + 1);
76 fprintf(out, "%s\n", suffix ? suffix : "");
77 print_constraints_polylib(bmap, out, indent, prefix, suffix);
80 static void isl_basic_set_print_polylib(struct isl_basic_set *bset, FILE *out,
81 int indent, const char *prefix, const char *suffix)
83 isl_basic_map_print_polylib((struct isl_basic_map *)bset, out,
84 indent, prefix, suffix);
87 static void isl_map_print_polylib(struct isl_map *map, FILE *out, int indent)
89 int i;
91 fprintf(out, "%*s", indent, "");
92 fprintf(out, "%d\n", map->n);
93 for (i = 0; i < map->n; ++i) {
94 fprintf(out, "\n");
95 isl_basic_map_print_polylib(map->p[i], out, indent, NULL, NULL);
99 static void isl_set_print_polylib(struct isl_set *set, FILE *out, int indent)
101 isl_map_print_polylib((struct isl_map *)set, out, indent);
104 static int count_same_name(__isl_keep isl_dim *dim,
105 enum isl_dim_type type, unsigned pos, const char *name)
107 enum isl_dim_type t;
108 unsigned p, s;
109 int count = 0;
111 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
112 s = t == type ? pos : isl_dim_size(dim, t);
113 for (p = 0; p < s; ++p) {
114 const char *n = isl_dim_get_name(dim, t, p);
115 if (n && !strcmp(n, name))
116 count++;
119 return count;
122 static void print_name(struct isl_dim *dim, FILE *out,
123 enum isl_dim_type type, unsigned pos, int set)
125 const char *name;
126 char buffer[20];
127 int primes;
129 name = type == isl_dim_div ? NULL : isl_dim_get_name(dim, type, pos);
131 if (!name) {
132 const char *prefix;
133 if (type == isl_dim_param)
134 prefix = "p";
135 else if (type == isl_dim_div)
136 prefix = "e";
137 else if (set || type == isl_dim_in)
138 prefix = "i";
139 else
140 prefix = "o";
141 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
142 name = buffer;
144 primes = count_same_name(dim, name == buffer ? isl_dim_div : type,
145 pos, name);
146 fprintf(out, "%s", name);
147 while (primes-- > 0)
148 fputc('\'', out);
151 static void print_var_list(struct isl_dim *dim, FILE *out,
152 enum isl_dim_type type, int set)
154 int i;
156 for (i = 0; i < isl_dim_size(dim, type); ++i) {
157 if (i)
158 fprintf(out, ", ");
159 print_name(dim, out, type, i, set);
163 static void print_tuple(__isl_keep isl_dim *dim, FILE *out,
164 enum isl_dim_type type, int set)
166 fprintf(out, "[");
167 print_var_list(dim, out, type, set);
168 fprintf(out, "]");
171 static void print_omega_parameters(struct isl_dim *dim, FILE *out,
172 int indent, const char *prefix, const char *suffix)
174 if (isl_dim_size(dim, isl_dim_param) == 0)
175 return;
177 fprintf(out, "%*s%ssymbolic ", indent, "", prefix ? prefix : "");
178 print_var_list(dim, out, isl_dim_param, 0);
179 fprintf(out, ";%s\n", suffix ? suffix : "");
182 static void print_term(__isl_keep isl_dim *dim,
183 isl_int c, int pos, FILE *out, int set)
185 enum isl_dim_type type;
186 unsigned n_in = isl_dim_size(dim, isl_dim_in);
187 unsigned n_out = isl_dim_size(dim, isl_dim_out);
188 unsigned nparam = isl_dim_size(dim, isl_dim_param);
190 if (pos == 0) {
191 isl_int_print(out, c, 0);
192 return;
195 if (isl_int_is_one(c))
197 else if (isl_int_is_negone(c))
198 fprintf(out, "-");
199 else
200 isl_int_print(out, c, 0);
201 if (pos < 1 + nparam) {
202 type = isl_dim_param;
203 pos -= 1;
204 } else if (pos < 1 + nparam + n_in) {
205 type = isl_dim_in;
206 pos -= 1 + nparam;
207 } else if (pos < 1 + nparam + n_in + n_out) {
208 type = isl_dim_out;
209 pos -= 1 + nparam + n_in;
210 } else {
211 type = isl_dim_div;
212 pos -= 1 + nparam + n_in + n_out;
214 print_name(dim, out, type, pos, set);
217 static void print_affine(__isl_keep isl_basic_map *bmap,
218 __isl_keep isl_dim *dim, FILE *out, isl_int *c, int set)
220 int i;
221 int first;
222 unsigned len = 1 + isl_basic_map_total_dim(bmap);
224 for (i = 0, first = 1; i < len; ++i) {
225 int flip = 0;
226 if (isl_int_is_zero(c[i]))
227 continue;
228 if (!first) {
229 if (isl_int_is_neg(c[i])) {
230 flip = 1;
231 isl_int_neg(c[i], c[i]);
232 fprintf(out, " - ");
233 } else
234 fprintf(out, " + ");
236 first = 0;
237 print_term(dim, c[i], i, out, set);
238 if (flip)
239 isl_int_neg(c[i], c[i]);
241 if (first)
242 fprintf(out, "0");
245 static void print_constraint(struct isl_basic_map *bmap,
246 __isl_keep isl_dim *dim, FILE *out,
247 isl_int *c, int last, const char *op, int first_constraint, int set)
249 if (!first_constraint)
250 fprintf(out, " and ");
252 isl_int_abs(c[last], c[last]);
254 print_term(dim, c[last], last, out, set);
256 fprintf(out, " %s ", op);
258 isl_int_set_si(c[last], 0);
259 print_affine(bmap, dim, out, c, set);
262 static void print_constraints(__isl_keep isl_basic_map *bmap,
263 __isl_keep isl_dim *dim, FILE *out, int set)
265 int i;
266 struct isl_vec *c;
267 unsigned total = isl_basic_map_total_dim(bmap);
269 c = isl_vec_alloc(bmap->ctx, 1 + total);
270 if (!c)
271 return;
273 for (i = bmap->n_eq - 1; i >= 0; --i) {
274 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
275 isl_assert(bmap->ctx, l >= 0, return);
276 if (isl_int_is_neg(bmap->eq[i][l]))
277 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
278 else
279 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
280 print_constraint(bmap, dim, out, c->el, l,
281 "=", i == bmap->n_eq - 1, set);
283 for (i = 0; i < bmap->n_ineq; ++i) {
284 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
285 int s;
286 isl_assert(bmap->ctx, l >= 0, return);
287 s = isl_int_sgn(bmap->ineq[i][l]);
288 if (s < 0)
289 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
290 else
291 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
292 print_constraint(bmap, dim, out, c->el, l,
293 s < 0 ? "<=" : ">=", !bmap->n_eq && !i, set);
296 isl_vec_free(c);
299 static void print_omega_constraints(__isl_keep isl_basic_map *bmap, FILE *out,
300 int set)
302 if (bmap->n_eq + bmap->n_ineq == 0)
303 return;
305 fprintf(out, ": ");
306 if (bmap->n_div > 0) {
307 int i;
308 fprintf(out, "exists (");
309 for (i = 0; i < bmap->n_div; ++i) {
310 if (i)
311 fprintf(out, ", ");
312 print_name(bmap->dim, out, isl_dim_div, i, 0);
314 fprintf(out, ": ");
316 print_constraints(bmap, bmap->dim, out, set);
317 if (bmap->n_div > 0)
318 fprintf(out, ")");
321 static void basic_map_print_omega(struct isl_basic_map *bmap, FILE *out)
323 fprintf(out, "{ [");
324 print_var_list(bmap->dim, out, isl_dim_in, 0);
325 fprintf(out, "] -> [");
326 print_var_list(bmap->dim, out, isl_dim_out, 0);
327 fprintf(out, "] ");
328 print_omega_constraints(bmap, out, 0);
329 fprintf(out, " }");
332 static void isl_basic_map_print_omega(struct isl_basic_map *bmap, FILE *out,
333 int indent, const char *prefix, const char *suffix)
335 print_omega_parameters(bmap->dim, out, indent, prefix, suffix);
337 fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
338 basic_map_print_omega(bmap, out);
339 fprintf(out, "%s\n", suffix ? suffix : "");
342 static void basic_set_print_omega(struct isl_basic_set *bset, FILE *out)
344 fprintf(out, "{ [");
345 print_var_list(bset->dim, out, isl_dim_set, 1);
346 fprintf(out, "] ");
347 print_omega_constraints((isl_basic_map *)bset, out, 1);
348 fprintf(out, " }");
351 static void isl_basic_set_print_omega(struct isl_basic_set *bset, FILE *out,
352 int indent, const char *prefix, const char *suffix)
354 print_omega_parameters(bset->dim, out, indent, prefix, suffix);
356 fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
357 basic_set_print_omega(bset, out);
358 fprintf(out, "%s\n", suffix ? suffix : "");
361 static void isl_map_print_omega(struct isl_map *map, FILE *out, int indent)
363 int i;
365 print_omega_parameters(map->dim, out, indent, "", "");
367 fprintf(out, "%*s", indent, "");
368 for (i = 0; i < map->n; ++i) {
369 if (i)
370 fprintf(out, " union ");
371 basic_map_print_omega(map->p[i], out);
373 fprintf(out, "\n");
376 static void isl_set_print_omega(struct isl_set *set, FILE *out, int indent)
378 int i;
380 print_omega_parameters(set->dim, out, indent, "", "");
382 fprintf(out, "%*s", indent, "");
383 for (i = 0; i < set->n; ++i) {
384 if (i)
385 fprintf(out, " union ");
386 basic_set_print_omega(set->p[i], out);
388 fprintf(out, "\n");
391 static void print_disjunct(__isl_keep isl_basic_map *bmap,
392 __isl_keep isl_dim *dim, FILE *out, int set)
394 if (bmap->n_div > 0) {
395 int i;
396 fprintf(out, "exists (");
397 for (i = 0; i < bmap->n_div; ++i) {
398 if (i)
399 fprintf(out, ", ");
400 print_name(dim, out, isl_dim_div, i, 0);
401 if (isl_int_is_zero(bmap->div[i][0]))
402 continue;
403 fprintf(out, " = [(");
404 print_affine(bmap, dim, out, bmap->div[i] + 1, set);
405 fprintf(out, ")/");
406 isl_int_print(out, bmap->div[i][0], 0);
407 fprintf(out, "]");
409 fprintf(out, ": ");
412 print_constraints(bmap, dim, out, set);
414 if (bmap->n_div > 0)
415 fprintf(out, ")");
418 static void isl_basic_map_print_isl(__isl_keep isl_basic_map *bmap, FILE *out,
419 int indent, const char *prefix, const char *suffix)
421 int i;
423 fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
424 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
425 print_tuple(bmap->dim, out, isl_dim_param, 0);
426 fprintf(out, " -> ");
428 fprintf(out, "{ ");
429 print_tuple(bmap->dim, out, isl_dim_in, 0);
430 fprintf(out, " -> ");
431 print_tuple(bmap->dim, out, isl_dim_out, 0);
432 fprintf(out, " : ");
433 print_disjunct(bmap, bmap->dim, out, 0);
434 fprintf(out, " }%s\n", suffix ? suffix : "");
437 static void isl_basic_set_print_isl(__isl_keep isl_basic_set *bset, FILE *out,
438 int indent, const char *prefix, const char *suffix)
440 int i;
442 fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
443 if (isl_basic_set_dim(bset, isl_dim_param) > 0) {
444 print_tuple(bset->dim, out, isl_dim_param, 0);
445 fprintf(out, " -> ");
447 fprintf(out, "{ ");
448 print_tuple(bset->dim, out, isl_dim_set, 1);
449 fprintf(out, " : ");
450 print_disjunct((isl_basic_map *)bset, bset->dim, out, 1);
451 fprintf(out, " }%s\n", suffix ? suffix : "");
454 static void isl_map_print_isl(__isl_keep isl_map *map, FILE *out, int indent)
456 int i;
458 fprintf(out, "%*s", indent, "");
459 if (isl_map_dim(map, isl_dim_param) > 0) {
460 print_tuple(map->dim, out, isl_dim_param, 0);
461 fprintf(out, " -> ");
463 fprintf(out, "{ ");
464 print_tuple(map->dim, out, isl_dim_in, 0);
465 fprintf(out, " -> ");
466 print_tuple(map->dim, out, isl_dim_out, 0);
467 fprintf(out, " : ");
468 if (map->n == 0)
469 fprintf(out, "1 = 0");
470 for (i = 0; i < map->n; ++i) {
471 if (i)
472 fprintf(out, " or ");
473 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
474 fprintf(out, "(");
475 print_disjunct(map->p[i], map->dim, out, 0);
476 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
477 fprintf(out, ")");
479 fprintf(out, " }\n");
482 static void isl_set_print_isl(__isl_keep isl_set *set, FILE *out, int indent)
484 int i;
486 fprintf(out, "%*s", indent, "");
487 if (isl_set_dim(set, isl_dim_param) > 0) {
488 print_tuple(set->dim, out, isl_dim_param, 0);
489 fprintf(out, " -> ");
491 fprintf(out, "{ ");
492 print_tuple(set->dim, out, isl_dim_set, 1);
493 fprintf(out, " : ");
494 if (set->n == 0)
495 fprintf(out, "1 = 0");
496 for (i = 0; i < set->n; ++i) {
497 if (i)
498 fprintf(out, " or ");
499 print_disjunct((isl_basic_map *)set->p[i], set->dim, out, 1);
501 fprintf(out, " }\n");
504 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
505 const char *prefix, const char *suffix, unsigned output_format)
507 if (!bmap)
508 return;
509 if (output_format == ISL_FORMAT_ISL)
510 isl_basic_map_print_isl(bmap, out, indent, prefix, suffix);
511 else if (output_format == ISL_FORMAT_OMEGA)
512 isl_basic_map_print_omega(bmap, out, indent, prefix, suffix);
513 else
514 isl_assert(bmap->ctx, 0, return);
517 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
518 const char *prefix, const char *suffix, unsigned output_format)
520 if (!bset)
521 return;
522 if (output_format == ISL_FORMAT_ISL)
523 isl_basic_set_print_isl(bset, out, indent, prefix, suffix);
524 else if (output_format == ISL_FORMAT_POLYLIB)
525 isl_basic_set_print_polylib(bset, out, indent, prefix, suffix);
526 else if (output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
527 bset_print_constraints_polylib(bset, out, indent, prefix, suffix);
528 else if (output_format == ISL_FORMAT_OMEGA)
529 isl_basic_set_print_omega(bset, out, indent, prefix, suffix);
530 else
531 isl_assert(bset->ctx, 0, return);
534 void isl_set_print(struct isl_set *set, FILE *out, int indent,
535 unsigned output_format)
537 if (!set)
538 return;
539 if (output_format == ISL_FORMAT_ISL)
540 isl_set_print_isl(set, out, indent);
541 else if (output_format == ISL_FORMAT_POLYLIB)
542 isl_set_print_polylib(set, out, indent);
543 else if (output_format == ISL_FORMAT_OMEGA)
544 isl_set_print_omega(set, out, indent);
545 else
546 isl_assert(set->ctx, 0, return);
549 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
550 unsigned output_format)
552 if (!map)
553 return;
554 if (output_format == ISL_FORMAT_ISL)
555 isl_map_print_isl(map, out, indent);
556 else if (output_format == ISL_FORMAT_POLYLIB)
557 isl_map_print_polylib(map, out, indent);
558 else if (output_format == ISL_FORMAT_OMEGA)
559 isl_map_print_omega(map, out, indent);
560 else
561 isl_assert(map->ctx, 0, return);