isl_map_read: make sure polylib constraint coefficients are on a single line
[platform/upstream/isl.git] / isl_input.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  * Copyright 2010      INRIA Saclay
4  *
5  * Use of this software is governed by the GNU LGPLv2.1 license
6  *
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 
11  */
12
13 #include <ctype.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <strings.h>
17 #include <isl_set.h>
18 #include <isl_seq.h>
19 #include "isl_stream.h"
20 #include "isl_map_private.h"
21
22 struct variable {
23         char                    *name;
24         int                      pos;
25         struct variable         *next;
26 };
27
28 struct vars {
29         struct isl_ctx  *ctx;
30         int              n;
31         struct variable *v;
32 };
33
34 static struct vars *vars_new(struct isl_ctx *ctx)
35 {
36         struct vars *v;
37         v = isl_alloc_type(ctx, struct vars);
38         if (!v)
39                 return NULL;
40         v->ctx = ctx;
41         v->n = 0;
42         v->v = NULL;
43         return v;
44 }
45
46 static void variable_free(struct variable *var)
47 {
48         while (var) {
49                 struct variable *next = var->next;
50                 free(var->name);
51                 free(var);
52                 var = next;
53         }
54 }
55
56 static void vars_free(struct vars *v)
57 {
58         if (!v)
59                 return;
60         variable_free(v->v);
61         free(v);
62 }
63
64 static struct variable *variable_new(struct vars *v, const char *name, int len,
65                                 int pos)
66 {
67         struct variable *var;
68         var = isl_alloc_type(v->ctx, struct variable);
69         if (!var)
70                 goto error;
71         var->name = strdup(name);
72         var->name[len] = '\0';
73         var->pos = pos;
74         var->next = v->v;
75         return var;
76 error:
77         variable_free(v->v);
78         return NULL;
79 }
80
81 static int vars_pos(struct vars *v, const char *s, int len)
82 {
83         int pos;
84         struct variable *q;
85
86         if (len == -1)
87                 len = strlen(s);
88         for (q = v->v; q; q = q->next) {
89                 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
90                         break;
91         }
92         if (q)
93                 pos = q->pos;
94         else {
95                 pos = v->n;
96                 v->v = variable_new(v, s, len, v->n);
97                 if (!v->v)
98                         return -1;
99                 v->n++;
100         }
101         return pos;
102 }
103
104 static struct vars *read_var_list(struct isl_stream *s, struct vars *v)
105 {
106         struct isl_token *tok;
107
108         while ((tok = isl_stream_next_token(s)) != NULL) {
109                 int p;
110                 int n = v->n;
111
112                 if (tok->type != ISL_TOKEN_IDENT)
113                         break;
114
115                 p = vars_pos(v, tok->u.s, -1);
116                 if (p < 0)
117                         goto error;
118                 if (p < n) {
119                         isl_stream_error(s, tok, "expecting unique identifier");
120                         goto error;
121                 }
122                 isl_token_free(tok);
123                 tok = isl_stream_next_token(s);
124                 if (!tok || tok->type != ',')
125                         break;
126
127                 isl_token_free(tok);
128         }
129         if (tok)
130                 isl_stream_push_token(s, tok);
131
132         return v;
133 error:
134         isl_token_free(tok);
135         vars_free(v);
136         return NULL;
137 }
138
139 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v)
140 {
141         struct isl_token *tok = NULL;
142         struct isl_vec *aff;
143         int sign = 1;
144
145         aff = isl_vec_alloc(v->ctx, 1 + v->n);
146         isl_seq_clr(aff->el, aff->size);
147         if (!aff)
148                 return NULL;
149
150         for (;;) {
151                 tok = isl_stream_next_token(s);
152                 if (!tok) {
153                         isl_stream_error(s, NULL, "unexpected EOF");
154                         goto error;
155                 }
156                 if (tok->type == ISL_TOKEN_IDENT) {
157                         int n = v->n;
158                         int pos = vars_pos(v, tok->u.s, -1);
159                         if (pos < 0)
160                                 goto error;
161                         if (pos >= n) {
162                                 isl_stream_error(s, tok, "unknown identifier");
163                                 goto error;
164                         }
165                         if (sign > 0)
166                                 isl_int_add_ui(aff->el[1 + pos],
167                                                aff->el[1 + pos], 1);
168                         else
169                                 isl_int_sub_ui(aff->el[1 + pos],
170                                                aff->el[1 + pos], 1);
171                         sign = 1;
172                 } else if (tok->type == ISL_TOKEN_VALUE) {
173                         struct isl_token *tok2;
174                         int n = v->n;
175                         int pos = -1;
176                         tok2 = isl_stream_next_token(s);
177                         if (tok2 && tok2->type == ISL_TOKEN_IDENT) {
178                                 pos = vars_pos(v, tok2->u.s, -1);
179                                 if (pos < 0)
180                                         goto error;
181                                 if (pos >= n) {
182                                         isl_stream_error(s, tok2,
183                                                 "unknown identifier");
184                                         isl_token_free(tok2);
185                                         goto error;
186                                 }
187                                 isl_token_free(tok2);
188                         } else if (tok2)
189                                 isl_stream_push_token(s, tok2);
190                         if (sign < 0)
191                                 isl_int_neg(tok->u.v, tok->u.v);
192                         isl_int_add(aff->el[1 + pos],
193                                         aff->el[1 + pos], tok->u.v);
194                         sign = 1;
195                 } else if (tok->type == '-') {
196                         sign = -sign;
197                 } else if (tok->type == '+') {
198                         /* nothing */
199                 } else {
200                         isl_stream_push_token(s, tok);
201                         break;
202                 }
203                 isl_token_free(tok);
204         }
205
206         return aff;
207 error:
208         isl_vec_free(aff);
209         return NULL;
210 }
211
212 static struct isl_basic_map *add_div_definition(struct isl_stream *s,
213         struct vars *v, struct isl_basic_map *bmap, int k)
214 {
215         struct isl_token *tok;
216         int seen_paren = 0;
217         struct isl_vec *aff;
218
219         if (isl_stream_eat(s, '['))
220                 goto error;
221
222         tok = isl_stream_next_token(s);
223         if (!tok)
224                 goto error;
225         if (tok->type == '(') {
226                 seen_paren = 1;
227                 isl_token_free(tok);
228         } else
229                 isl_stream_push_token(s, tok);
230
231         aff = accept_affine(s, v);
232         if (!aff)
233                 goto error;
234
235         isl_seq_cpy(bmap->div[k] + 1, aff->el, aff->size);
236
237         isl_vec_free(aff);
238
239         if (seen_paren && isl_stream_eat(s, ')'))
240                 goto error;
241         if (isl_stream_eat(s, '/'))
242                 goto error;
243
244         tok = isl_stream_next_token(s);
245         if (!tok)
246                 goto error;
247         if (tok->type != ISL_TOKEN_VALUE) {
248                 isl_stream_error(s, tok, "expected denominator");
249                 isl_stream_push_token(s, tok);
250                 goto error;
251         }
252         isl_int_set(bmap->div[k][0], tok->u.v);
253         isl_token_free(tok);
254
255         if (isl_stream_eat(s, ']'))
256                 goto error;
257
258         if (isl_basic_map_add_div_constraints(bmap, k) < 0)
259                 goto error;
260
261         return bmap;
262 error:
263         isl_basic_map_free(bmap);
264         return NULL;
265 }
266
267 static struct isl_basic_map *read_defined_var_list(struct isl_stream *s,
268         struct vars *v, struct isl_basic_map *bmap)
269 {
270         struct isl_token *tok;
271
272         while ((tok = isl_stream_next_token(s)) != NULL) {
273                 int k;
274                 int p;
275                 int n = v->n;
276                 unsigned total = isl_basic_map_total_dim(bmap);
277
278                 if (tok->type != ISL_TOKEN_IDENT)
279                         break;
280
281                 p = vars_pos(v, tok->u.s, -1);
282                 if (p < 0)
283                         goto error;
284                 if (p < n) {
285                         isl_stream_error(s, tok, "expecting unique identifier");
286                         goto error;
287                 }
288                 isl_token_free(tok);
289
290                 bmap = isl_basic_map_cow(bmap);
291                 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
292                                                 1, 0, 2);
293
294                 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
295                         goto error;
296                 isl_seq_clr(bmap->div[k], 1 + 1 + total);
297
298                 tok = isl_stream_next_token(s);
299                 if (tok && tok->type == '=') {
300                         isl_token_free(tok);
301                         bmap = add_div_definition(s, v, bmap, k);
302                         tok = isl_stream_next_token(s);
303                 }
304
305                 if (!tok || tok->type != ',')
306                         break;
307
308                 isl_token_free(tok);
309         }
310         if (tok)
311                 isl_stream_push_token(s, tok);
312
313         return bmap;
314 error:
315         isl_token_free(tok);
316         isl_basic_map_free(bmap);
317         return NULL;
318 }
319
320 static struct vars *read_tuple(struct isl_stream *s, struct vars *v)
321 {
322         struct isl_token *tok;
323
324         tok = isl_stream_next_token(s);
325         if (!tok || tok->type != '[') {
326                 isl_stream_error(s, tok, "expecting '['");
327                 goto error;
328         }
329         isl_token_free(tok);
330         v = read_var_list(s, v);
331         tok = isl_stream_next_token(s);
332         if (!tok || tok->type != ']') {
333                 isl_stream_error(s, tok, "expecting ']'");
334                 goto error;
335         }
336         isl_token_free(tok);
337
338         return v;
339 error:
340         if (tok)
341                 isl_token_free(tok);
342         vars_free(v);
343         return NULL;
344 }
345
346 static struct isl_basic_map *add_constraints(struct isl_stream *s,
347         struct vars **v, struct isl_basic_map *bmap);
348
349 static struct isl_basic_map *add_exists(struct isl_stream *s,
350         struct vars **v, struct isl_basic_map *bmap)
351 {
352         struct isl_token *tok;
353         int n = (*v)->n;
354         int extra;
355         int seen_paren = 0;
356         int i;
357         unsigned total;
358
359         tok = isl_stream_next_token(s);
360         if (!tok)
361                 goto error;
362         if (tok->type == '(') {
363                 seen_paren = 1;
364                 isl_token_free(tok);
365         } else
366                 isl_stream_push_token(s, tok);
367
368         bmap = read_defined_var_list(s, *v, bmap);
369         if (!bmap)
370                 goto error;
371
372         if (isl_stream_eat(s, ':'))
373                 goto error;
374         bmap = add_constraints(s, v, bmap);
375         if (seen_paren && isl_stream_eat(s, ')'))
376                 goto error;
377         return bmap;
378 error:
379         isl_basic_map_free(bmap);
380         return NULL;
381 }
382
383 static struct isl_basic_map *add_constraint(struct isl_stream *s,
384         struct vars **v, struct isl_basic_map *bmap)
385 {
386         unsigned total = isl_basic_map_total_dim(bmap);
387         int k;
388         struct isl_token *tok = NULL;
389         struct isl_vec *aff1 = NULL, *aff2 = NULL;
390
391         tok = isl_stream_next_token(s);
392         if (!tok)
393                 goto error;
394         if (tok->type == ISL_TOKEN_EXISTS) {
395                 isl_token_free(tok);
396                 return add_exists(s, v, bmap);
397         }
398         isl_stream_push_token(s, tok);
399         tok = NULL;
400
401         bmap = isl_basic_map_cow(bmap);
402         bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
403         k = isl_basic_map_alloc_inequality(bmap);
404         if (k < 0)
405                 goto error;
406
407         aff1 = accept_affine(s, *v);
408         if (!aff1)
409                 goto error;
410         tok = isl_stream_next_token(s);
411         switch (tok->type) {
412         case ISL_TOKEN_LT:
413         case ISL_TOKEN_GT:
414         case ISL_TOKEN_LE:
415         case ISL_TOKEN_GE:
416         case '=':
417                 break;
418         default:
419                 isl_stream_error(s, tok, "missing operator");
420                 isl_stream_push_token(s, tok);
421                 tok = NULL;
422                 goto error;
423         }
424         aff2 = accept_affine(s, *v);
425         if (!aff2)
426                 goto error;
427         isl_assert(aff1->ctx, aff1->size == 1 + total, goto error);
428         isl_assert(aff2->ctx, aff2->size == 1 + total, goto error);
429
430         if (tok->type == ISL_TOKEN_LE)
431                 isl_seq_combine(bmap->ineq[k], (*v)->ctx->negone, aff1->el,
432                                                (*v)->ctx->one, aff2->el,
433                                                aff1->size);
434         else if (tok->type == ISL_TOKEN_GE)
435                 isl_seq_combine(bmap->ineq[k], (*v)->ctx->one, aff1->el,
436                                                (*v)->ctx->negone, aff2->el,
437                                                aff1->size);
438         else if (tok->type == ISL_TOKEN_LT) {
439                 isl_seq_combine(bmap->ineq[k], (*v)->ctx->negone, aff1->el,
440                                                (*v)->ctx->one, aff2->el,
441                                                aff1->size);
442                 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
443         } else if (tok->type == ISL_TOKEN_GT) {
444                 isl_seq_combine(bmap->ineq[k], (*v)->ctx->one, aff1->el,
445                                                (*v)->ctx->negone, aff2->el,
446                                                aff1->size);
447                 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
448         } else {
449                 isl_seq_combine(bmap->ineq[k], (*v)->ctx->one, aff1->el,
450                                                (*v)->ctx->negone, aff2->el,
451                                                aff1->size);
452                 isl_basic_map_inequality_to_equality(bmap, k);
453         }
454         isl_token_free(tok);
455         isl_vec_free(aff1);
456         isl_vec_free(aff2);
457
458         return bmap;
459 error:
460         if (tok)
461                 isl_token_free(tok);
462         isl_vec_free(aff1);
463         isl_vec_free(aff2);
464         isl_basic_map_free(bmap);
465         return NULL;
466 }
467
468 static struct isl_basic_map *add_constraints(struct isl_stream *s,
469         struct vars **v, struct isl_basic_map *bmap)
470 {
471         struct isl_token *tok;
472
473         for (;;) {
474                 bmap = add_constraint(s, v, bmap);
475                 if (!bmap)
476                         return NULL;
477                 tok = isl_stream_next_token(s);
478                 if (!tok) {
479                         isl_stream_error(s, NULL, "unexpected EOF");
480                         goto error;
481                 }
482                 if (tok->type != ISL_TOKEN_AND)
483                         break;
484                 isl_token_free(tok);
485         }
486         isl_stream_push_token(s, tok);
487
488         return bmap;
489 error:
490         if (tok)
491                 isl_token_free(tok);
492         isl_basic_map_free(bmap);
493         return NULL;
494 }
495
496 static struct isl_basic_map *read_disjunct(struct isl_stream *s,
497         struct vars **v, __isl_take isl_dim *dim)
498 {
499         struct isl_basic_map *bmap;
500
501         bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
502         if (!bmap)
503                 return NULL;
504
505         bmap = add_constraints(s, v, bmap);
506         bmap = isl_basic_map_simplify(bmap);
507         bmap = isl_basic_map_finalize(bmap);
508         return bmap;
509 }
510
511 static struct isl_map *read_disjuncts(struct isl_stream *s,
512         struct vars **v, __isl_take isl_dim *dim)
513 {
514         struct isl_token *tok;
515         struct isl_map *map;
516
517         tok = isl_stream_next_token(s);
518         if (!tok) {
519                 isl_stream_error(s, NULL, "unexpected EOF");
520                 goto error;
521         }
522         if (tok->type == '}') {
523                 isl_stream_push_token(s, tok);
524                 return isl_map_universe(dim);
525         }
526         isl_stream_push_token(s, tok);
527
528         map = isl_map_empty(isl_dim_copy(dim));
529         for (;;) {
530                 struct isl_basic_map *bmap;
531
532                 bmap = read_disjunct(s, v, isl_dim_copy(dim));
533                 map = isl_map_union(map, isl_map_from_basic_map(bmap));
534
535                 tok = isl_stream_next_token(s);
536                 if (!tok || tok->type != ISL_TOKEN_OR)
537                         break;
538                 isl_token_free(tok);
539         }
540         if (tok)
541                 isl_stream_push_token(s, tok);
542
543         isl_dim_free(dim);
544         return map;
545 error:
546         isl_dim_free(dim);
547         return NULL;
548 }
549
550 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
551 {
552         if (pos < isl_basic_map_dim(bmap, isl_dim_out))
553                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
554                            isl_basic_map_dim(bmap, isl_dim_in) + pos;
555         pos -= isl_basic_map_dim(bmap, isl_dim_out);
556
557         if (pos < isl_basic_map_dim(bmap, isl_dim_in))
558                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
559         pos -= isl_basic_map_dim(bmap, isl_dim_in);
560
561         if (pos < isl_basic_map_dim(bmap, isl_dim_div))
562                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
563                            isl_basic_map_dim(bmap, isl_dim_in) +
564                            isl_basic_map_dim(bmap, isl_dim_out) + pos;
565         pos -= isl_basic_map_dim(bmap, isl_dim_div);
566
567         if (pos < isl_basic_map_dim(bmap, isl_dim_param))
568                 return 1 + pos;
569
570         return 0;
571 }
572
573 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
574         struct isl_stream *s, __isl_take isl_basic_map *bmap)
575 {
576         int j;
577         struct isl_token *tok;
578         int type;
579         int k;
580         isl_int *c;
581         unsigned nparam;
582         unsigned dim;
583
584         if (!bmap)
585                 return NULL;
586
587         nparam = isl_basic_map_dim(bmap, isl_dim_param);
588         dim = isl_basic_map_dim(bmap, isl_dim_out);
589
590         tok = isl_stream_next_token(s);
591         if (!tok || tok->type != ISL_TOKEN_VALUE) {
592                 isl_stream_error(s, tok, "expecting coefficient");
593                 if (tok)
594                         isl_stream_push_token(s, tok);
595                 goto error;
596         }
597         if (!tok->on_new_line) {
598                 isl_stream_error(s, tok, "coefficient should appear on new line");
599                 isl_stream_push_token(s, tok);
600                 goto error;
601         }
602
603         type = isl_int_get_si(tok->u.v);
604         isl_token_free(tok);
605
606         isl_assert(s->ctx, type == 0 || type == 1, goto error);
607         if (type == 0) {
608                 k = isl_basic_map_alloc_equality(bmap);
609                 c = bmap->eq[k];
610         } else {
611                 k = isl_basic_map_alloc_inequality(bmap);
612                 c = bmap->ineq[k];
613         }
614         if (k < 0)
615                 goto error;
616
617         for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
618                 int pos;
619                 tok = isl_stream_next_token(s);
620                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
621                         isl_stream_error(s, tok, "expecting coefficient");
622                         if (tok)
623                                 isl_stream_push_token(s, tok);
624                         goto error;
625                 }
626                 if (tok->on_new_line) {
627                         isl_stream_error(s, tok,
628                                 "coefficient should not appear on new line");
629                         isl_stream_push_token(s, tok);
630                         goto error;
631                 }
632                 pos = polylib_pos_to_isl_pos(bmap, j);
633                 isl_int_set(c[pos], tok->u.v);
634                 isl_token_free(tok);
635         }
636
637         return bmap;
638 error:
639         isl_basic_map_free(bmap);
640         return NULL;
641 }
642
643 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
644         int nparam)
645 {
646         int i;
647         struct isl_token *tok;
648         struct isl_token *tok2;
649         int n_row, n_col;
650         int on_new_line;
651         unsigned dim;
652         struct isl_basic_map *bmap = NULL;
653
654         if (nparam < 0)
655                 nparam = 0;
656
657         tok = isl_stream_next_token(s);
658         if (!tok) {
659                 isl_stream_error(s, NULL, "unexpected EOF");
660                 return NULL;
661         }
662         tok2 = isl_stream_next_token(s);
663         if (!tok2) {
664                 isl_token_free(tok);
665                 isl_stream_error(s, NULL, "unexpected EOF");
666                 return NULL;
667         }
668         n_row = isl_int_get_si(tok->u.v);
669         n_col = isl_int_get_si(tok2->u.v);
670         on_new_line = tok2->on_new_line;
671         isl_token_free(tok2);
672         isl_token_free(tok);
673         isl_assert(s->ctx, !on_new_line, return NULL);
674         isl_assert(s->ctx, n_row >= 0, return NULL);
675         isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
676         dim = n_col - 2 - nparam;
677         bmap = isl_basic_map_alloc(s->ctx, nparam, 0, dim, 0, n_row, n_row);
678         if (!bmap)
679                 return NULL;
680
681         for (i = 0; i < n_row; ++i)
682                 bmap = basic_map_read_polylib_constraint(s, bmap);
683
684         bmap = isl_basic_map_simplify(bmap);
685         bmap = isl_basic_map_finalize(bmap);
686         return bmap;
687 }
688
689 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
690 {
691         struct isl_token *tok;
692         struct isl_token *tok2;
693         int i, n;
694         struct isl_map *map;
695
696         tok = isl_stream_next_token(s);
697         if (!tok) {
698                 isl_stream_error(s, NULL, "unexpected EOF");
699                 return NULL;
700         }
701         tok2 = isl_stream_next_token(s);
702         if (!tok2) {
703                 isl_token_free(tok);
704                 isl_stream_error(s, NULL, "unexpected EOF");
705                 return NULL;
706         }
707         if (!tok2->on_new_line) {
708                 isl_stream_push_token(s, tok2);
709                 isl_stream_push_token(s, tok);
710                 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
711         }
712         isl_stream_push_token(s, tok2);
713         n = isl_int_get_si(tok->u.v);
714         isl_token_free(tok);
715
716         isl_assert(s->ctx, n >= 1, return NULL);
717
718         map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
719
720         for (i = 1; i < n; ++i)
721                 map = isl_map_union(map,
722                         isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
723
724         return map;
725 }
726
727 static struct isl_dim *set_names(struct isl_dim *dim, struct vars *vars,
728         enum isl_dim_type type, int offset, int n)
729 {
730         int i;
731         struct variable *v;
732
733         for (i = 0, v = vars->v; i < offset; ++i, v = v->next)
734                 ;
735         for (i = n - 1; i >= 0; --i, v = v->next)
736                 dim = isl_dim_set_name(dim, type, i, v->name);
737
738         return dim;
739 }
740
741 static struct isl_dim *dim_from_vars(struct vars *vars,
742         int nparam, int n_in, int n_out)
743 {
744         struct isl_dim *dim;
745
746         dim = isl_dim_alloc(vars->ctx, nparam, n_in, n_out);
747         dim = set_names(dim, vars, isl_dim_param, n_out + n_in, nparam);
748         dim = set_names(dim, vars, isl_dim_in, n_out, n_in);
749         dim = set_names(dim, vars, isl_dim_out, 0, n_out);
750
751         return dim;
752 }
753
754 static struct isl_map *map_read(struct isl_stream *s, int nparam)
755 {
756         struct isl_dim *dim = NULL;
757         struct isl_map *map = NULL;
758         struct isl_token *tok;
759         struct vars *v = NULL;
760         int n1;
761         int n2;
762
763         tok = isl_stream_next_token(s);
764         if (!tok) {
765                 isl_stream_error(s, NULL, "unexpected EOF");
766                 goto error;
767         }
768         if (tok->type == ISL_TOKEN_VALUE) {
769                 isl_stream_push_token(s, tok);
770                 return map_read_polylib(s, nparam);
771         }
772         v = vars_new(s->ctx);
773         if (tok->type == '[') {
774                 isl_stream_push_token(s, tok);
775                 v = read_tuple(s, v);
776                 if (!v)
777                         return NULL;
778                 if (nparam >= 0)
779                         isl_assert(s->ctx, nparam == v->n, goto error);
780                 nparam = v->n;
781                 tok = isl_stream_next_token(s);
782                 if (!tok || tok->type != ISL_TOKEN_TO) {
783                         isl_stream_error(s, tok, "expecting '->'");
784                         if (tok)
785                                 isl_stream_push_token(s, tok);
786                         goto error;
787                 }
788                 isl_token_free(tok);
789                 tok = isl_stream_next_token(s);
790         }
791         if (nparam < 0)
792                 nparam = 0;
793         if (!tok || tok->type != '{') {
794                 isl_stream_error(s, tok, "expecting '{'");
795                 if (tok)
796                         isl_stream_push_token(s, tok);
797                 goto error;
798         }
799         isl_token_free(tok);
800         v = read_tuple(s, v);
801         if (!v)
802                 return NULL;
803         n1 = v->n - nparam;
804         tok = isl_stream_next_token(s);
805         if (tok && tok->type == ISL_TOKEN_TO) {
806                 isl_token_free(tok);
807                 v = read_tuple(s, v);
808                 if (!v)
809                         return NULL;
810                 n2 = v->n - n1 - nparam;
811         } else {
812                 if (tok)
813                         isl_stream_push_token(s, tok);
814                 n2 = n1;
815                 n1 = 0;
816         }
817         dim = dim_from_vars(v, nparam, n1, n2);
818         tok = isl_stream_next_token(s);
819         if (!tok) {
820                 isl_stream_error(s, NULL, "unexpected EOF");
821                 goto error;
822         }
823         if (tok->type == ':') {
824                 isl_token_free(tok);
825                 map = read_disjuncts(s, &v, isl_dim_copy(dim));
826                 tok = isl_stream_next_token(s);
827         } else
828                 map = isl_map_universe(isl_dim_copy(dim));
829         if (tok && tok->type == '}') {
830                 isl_token_free(tok);
831         } else {
832                 isl_stream_error(s, tok, "unexpected isl_token");
833                 if (tok)
834                         isl_token_free(tok);
835                 goto error;
836         }
837         vars_free(v);
838         isl_dim_free(dim);
839
840         return map;
841 error:
842         isl_dim_free(dim);
843         isl_map_free(map);
844         if (v)
845                 vars_free(v);
846         return NULL;
847 }
848
849 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
850 {
851         struct isl_map *map;
852         struct isl_basic_map *bmap;
853
854         map = map_read(s, nparam);
855         if (!map)
856                 return NULL;
857
858         isl_assert(map->ctx, map->n <= 1, goto error);
859
860         if (map->n == 0)
861                 bmap = isl_basic_map_empty_like_map(map);
862         else
863                 bmap = isl_basic_map_copy(map->p[0]);
864
865         isl_map_free(map);
866
867         return bmap;
868 error:
869         isl_map_free(map);
870         return NULL;
871 }
872
873 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
874                 FILE *input, int nparam)
875 {
876         struct isl_basic_map *bmap;
877         struct isl_stream *s = isl_stream_new_file(ctx, input);
878         if (!s)
879                 return NULL;
880         bmap = basic_map_read(s, nparam);
881         isl_stream_free(s);
882         return bmap;
883 }
884
885 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
886                 FILE *input, int nparam)
887 {
888         struct isl_basic_map *bmap;
889         bmap = isl_basic_map_read_from_file(ctx, input, nparam);
890         if (!bmap)
891                 return NULL;
892         isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
893         return (struct isl_basic_set *)bmap;
894 error:
895         isl_basic_map_free(bmap);
896         return NULL;
897 }
898
899 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
900                 const char *str, int nparam)
901 {
902         struct isl_basic_map *bmap;
903         struct isl_stream *s = isl_stream_new_str(ctx, str);
904         if (!s)
905                 return NULL;
906         bmap = basic_map_read(s, nparam);
907         isl_stream_free(s);
908         return bmap;
909 }
910
911 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
912                 const char *str, int nparam)
913 {
914         struct isl_basic_map *bmap;
915         bmap = isl_basic_map_read_from_str(ctx, str, nparam);
916         if (!bmap)
917                 return NULL;
918         isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
919         return (struct isl_basic_set *)bmap;
920 error:
921         isl_basic_map_free(bmap);
922         return NULL;
923 }
924
925 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
926                 FILE *input, int nparam)
927 {
928         struct isl_map *map;
929         struct isl_stream *s = isl_stream_new_file(ctx, input);
930         if (!s)
931                 return NULL;
932         map = map_read(s, nparam);
933         isl_stream_free(s);
934         return map;
935 }
936
937 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
938                 const char *str, int nparam)
939 {
940         struct isl_map *map;
941         struct isl_stream *s = isl_stream_new_str(ctx, str);
942         if (!s)
943                 return NULL;
944         map = map_read(s, nparam);
945         isl_stream_free(s);
946         return map;
947 }
948
949 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
950                 FILE *input, int nparam)
951 {
952         struct isl_map *map;
953         map = isl_map_read_from_file(ctx, input, nparam);
954         if (!map)
955                 return NULL;
956         isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
957         return (struct isl_set *)map;
958 error:
959         isl_map_free(map);
960         return NULL;
961 }
962
963 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
964                 const char *str, int nparam)
965 {
966         struct isl_map *map;
967         map = isl_map_read_from_str(ctx, str, nparam);
968         if (!map)
969                 return NULL;
970         isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
971         return (struct isl_set *)map;
972 error:
973         isl_map_free(map);
974         return NULL;
975 }
976
977 static char *next_line(FILE *input, char *line, unsigned len)
978 {
979         char *p;
980
981         do {
982                 if (!(p = fgets(line, len, input)))
983                         return NULL;
984                 while (isspace(*p) && *p != '\n')
985                         ++p;
986         } while (*p == '#' || *p == '\n');
987
988         return p;
989 }
990
991 static struct isl_vec *isl_vec_read_from_file_polylib(struct isl_ctx *ctx,
992                 FILE *input)
993 {
994         struct isl_vec *vec = NULL;
995         char line[1024];
996         char val[1024];
997         char *p;
998         unsigned size;
999         int j;
1000         int n;
1001         int offset;
1002
1003         isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
1004         isl_assert(ctx, sscanf(line, "%u", &size) == 1, return NULL);
1005
1006         vec = isl_vec_alloc(ctx, size);
1007
1008         p = next_line(input, line, sizeof(line));
1009         isl_assert(ctx, p, goto error);
1010
1011         for (j = 0; j < size; ++j) {
1012                 n = sscanf(p, "%s%n", val, &offset);
1013                 isl_assert(ctx, n != 0, goto error);
1014                 isl_int_read(vec->el[j], val);
1015                 p += offset;
1016         }
1017
1018         return vec;
1019 error:
1020         isl_vec_free(vec);
1021         return NULL;
1022 }
1023
1024 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
1025                 FILE *input, unsigned input_format)
1026 {
1027         if (input_format == ISL_FORMAT_POLYLIB)
1028                 return isl_vec_read_from_file_polylib(ctx, input);
1029         else
1030                 isl_assert(ctx, 0, return NULL);
1031 }