isl_map_read: accept list of predicated elements
[platform/upstream/isl.git] / isl_input.c
index 1f48d01..091c7cf 100644 (file)
@@ -120,38 +120,29 @@ static int vars_pos(struct vars *v, const char *s, int len)
        return pos;
 }
 
-static struct vars *read_var_list(struct isl_stream *s, struct vars *v)
+static __isl_give isl_basic_map *set_name(__isl_take isl_basic_map *bmap,
+       enum isl_dim_type type, unsigned pos, char *name)
 {
-       struct isl_token *tok;
-
-       while ((tok = isl_stream_next_token(s)) != NULL) {
-               int p;
-               int n = v->n;
+       char *prime;
 
-               if (tok->type != ISL_TOKEN_IDENT)
-                       break;
+       if (!bmap)
+               return NULL;
+       if (!name)
+               return bmap;
 
-               p = vars_pos(v, tok->u.s, -1);
-               if (p < 0)
-                       goto error;
-               if (p < n) {
-                       isl_stream_error(s, tok, "expecting unique identifier");
-                       goto error;
-               }
-               isl_token_free(tok);
-               tok = isl_stream_next_token(s);
-               if (!tok || tok->type != ',')
-                       break;
+       prime = strchr(name, '\'');
+       if (prime)
+               *prime = '\0';
+       bmap->dim = isl_dim_set_name(bmap->dim, type, pos, name);
+       if (prime)
+               *prime = '\'';
 
-               isl_token_free(tok);
-       }
-       if (tok)
-               isl_stream_push_token(s, tok);
+       if (!bmap->dim)
+               goto error;
 
-       return v;
+       return bmap;
 error:
-       isl_token_free(tok);
-       vars_free(v);
+       isl_basic_map_free(bmap);
        return NULL;
 }
 
@@ -228,6 +219,75 @@ error:
        return NULL;
 }
 
+static __isl_give isl_basic_map *read_var_list(struct isl_stream *s,
+       __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
+{
+       int i = 0;
+       struct isl_token *tok;
+
+       while ((tok = isl_stream_next_token(s)) != NULL) {
+               int new_name = 0;
+
+               if (tok->type == ISL_TOKEN_IDENT) {
+                       int n = v->n;
+                       int p = vars_pos(v, tok->u.s, -1);
+                       if (p < 0)
+                               goto error;
+                       new_name = p >= n;
+               }
+
+               if (new_name) {
+                       bmap = isl_basic_map_add(bmap, type, 1);
+                       bmap = set_name(bmap, type, i, v->v->name);
+                       isl_token_free(tok);
+               } else if (tok->type == ISL_TOKEN_IDENT ||
+                          tok->type == ISL_TOKEN_VALUE) {
+                       struct isl_vec *vec;
+                       int k;
+                       if (type == isl_dim_param) {
+                               isl_stream_error(s, tok,
+                                               "expecting unique identifier");
+                               goto error;
+                       }
+                       isl_stream_push_token(s, tok);
+                       tok = NULL;
+                       vec = accept_affine(s, v);
+                       if (!vec)
+                               goto error;
+                       v->v = variable_new(v, "", 0, v->n);
+                       if (!v->v)
+                               goto error;
+                       v->n++;
+                       bmap = isl_basic_map_add(bmap, type, 1);
+                       bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
+                       k = isl_basic_map_alloc_equality(bmap);
+                       if (k >= 0) {
+                               isl_seq_cpy(bmap->eq[k], vec->el, vec->size);
+                               isl_int_set_si(bmap->eq[k][vec->size], -1);
+                       }
+                       isl_vec_free(vec);
+                       if (k < 0)
+                               goto error;
+               } else
+                       break;
+
+               tok = isl_stream_next_token(s);
+               if (!tok || tok->type != ',')
+                       break;
+
+               isl_token_free(tok);
+               i++;
+       }
+       if (tok)
+               isl_stream_push_token(s, tok);
+
+       return bmap;
+error:
+       isl_token_free(tok);
+       isl_basic_map_free(bmap);
+       return NULL;
+}
+
 static __isl_give isl_mat *accept_affine_list(struct isl_stream *s,
        struct vars *v)
 {
@@ -372,7 +432,8 @@ error:
        return NULL;
 }
 
-static struct vars *read_tuple(struct isl_stream *s, struct vars *v)
+static __isl_give isl_basic_map *read_tuple(struct isl_stream *s,
+       __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
 {
        struct isl_token *tok;
 
@@ -382,7 +443,7 @@ static struct vars *read_tuple(struct isl_stream *s, struct vars *v)
                goto error;
        }
        isl_token_free(tok);
-       v = read_var_list(s, v);
+       bmap = read_var_list(s, bmap, type, v);
        tok = isl_stream_next_token(s);
        if (!tok || tok->type != ']') {
                isl_stream_error(s, tok, "expecting ']'");
@@ -390,11 +451,11 @@ static struct vars *read_tuple(struct isl_stream *s, struct vars *v)
        }
        isl_token_free(tok);
 
-       return v;
+       return bmap;
 error:
        if (tok)
                isl_token_free(tok);
-       vars_free(v);
+       isl_basic_map_free(bmap);
        return NULL;
 }
 
@@ -482,6 +543,23 @@ error:
        return NULL;
 }
 
+static int is_comparator(struct isl_token *tok)
+{
+       if (!tok)
+               return 0;
+
+       switch (tok->type) {
+       case ISL_TOKEN_LT:
+       case ISL_TOKEN_GT:
+       case ISL_TOKEN_LE:
+       case ISL_TOKEN_GE:
+       case '=':
+               return 1;
+       default:
+               return 0;
+       }
+}
+
 static struct isl_basic_map *add_constraint(struct isl_stream *s,
        struct vars *v, struct isl_basic_map *bmap)
 {
@@ -506,33 +584,38 @@ static struct isl_basic_map *add_constraint(struct isl_stream *s,
        if (!aff1)
                goto error;
        tok = isl_stream_next_token(s);
-       switch (tok->type) {
-       case ISL_TOKEN_LT:
-       case ISL_TOKEN_GT:
-       case ISL_TOKEN_LE:
-       case ISL_TOKEN_GE:
-       case '=':
-               break;
-       default:
+       if (!is_comparator(tok)) {
                isl_stream_error(s, tok, "missing operator");
-               isl_stream_push_token(s, tok);
+               if (tok)
+                       isl_stream_push_token(s, tok);
                tok = NULL;
                goto error;
        }
-       aff2 = accept_affine_list(s, v);
-       if (!aff2)
-               goto error;
        isl_assert(aff1->ctx, aff1->n_col == 1 + total, goto error);
-       isl_assert(aff2->ctx, aff2->n_col == 1 + total, goto error);
+       for (;;) {
+               aff2 = accept_affine_list(s, v);
+               if (!aff2)
+                       goto error;
+               isl_assert(aff2->ctx, aff2->n_col == 1 + total, goto error);
 
-       bmap = isl_basic_map_extend_constraints(bmap, 0, aff1->n_row * aff2->n_row);
-       for (i = 0; i < aff1->n_row; ++i)
-               for (j = 0; j < aff2->n_row; ++j)
-                       bmap = construct_constraint(bmap, tok->type,
+               bmap = isl_basic_map_extend_constraints(bmap, 0,
+                                               aff1->n_row * aff2->n_row);
+               for (i = 0; i < aff1->n_row; ++i)
+                       for (j = 0; j < aff2->n_row; ++j)
+                               bmap = construct_constraint(bmap, tok->type,
                                                    aff1->row[i], aff2->row[j]);
-       isl_token_free(tok);
+               isl_token_free(tok);
+               isl_mat_free(aff1);
+               aff1 = aff2;
+
+               tok = isl_stream_next_token(s);
+               if (!is_comparator(tok)) {
+                       if (tok)
+                               isl_stream_push_token(s, tok);
+                       break;
+               }
+       }
        isl_mat_free(aff1);
-       isl_mat_free(aff2);
 
        return bmap;
 error:
@@ -575,16 +658,34 @@ error:
 static struct isl_basic_map *read_disjunct(struct isl_stream *s,
        struct vars *v, __isl_take isl_dim *dim)
 {
+       int seen_paren = 0;
+       struct isl_token *tok;
        struct isl_basic_map *bmap;
 
        bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
        if (!bmap)
                return NULL;
 
+       tok = isl_stream_next_token(s);
+       if (!tok)
+               goto error;
+       if (tok->type == '(') {
+               seen_paren = 1;
+               isl_token_free(tok);
+       } else
+               isl_stream_push_token(s, tok);
+
        bmap = add_constraints(s, v, bmap);
        bmap = isl_basic_map_simplify(bmap);
        bmap = isl_basic_map_finalize(bmap);
+
+       if (seen_paren && isl_stream_eat(s, ')'))
+               goto error;
+
        return bmap;
+error:
+       isl_basic_map_free(bmap);
+       return NULL;
 }
 
 static struct isl_map *read_disjuncts(struct isl_stream *s,
@@ -864,41 +965,55 @@ static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
        return map;
 }
 
-static struct isl_dim *set_names(struct isl_dim *dim, struct vars *vars,
-       enum isl_dim_type type, int offset, int n)
+static struct isl_map *map_read_body(struct isl_stream *s,
+       __isl_take isl_basic_map *bmap, struct vars *v)
 {
-       int i;
-       struct variable *v;
-
-       for (i = 0, v = vars->v; i < offset; ++i, v = v->next)
-               ;
-       for (i = n - 1; i >= 0; --i, v = v->next)
-               dim = isl_dim_set_name(dim, type, i, v->name);
-
-       return dim;
-}
+       struct isl_map *map = NULL;
+       struct isl_token *tok;
+       int n = v->n;
 
-static struct isl_dim *dim_from_vars(struct vars *vars,
-       int nparam, int n_in, int n_out)
-{
-       struct isl_dim *dim;
+       bmap = read_tuple(s, bmap, isl_dim_in, v);
+       if (!bmap)
+               goto error;
+       tok = isl_stream_next_token(s);
+       if (tok && tok->type == ISL_TOKEN_TO) {
+               isl_token_free(tok);
+               bmap = read_tuple(s, bmap, isl_dim_out, v);
+               if (!bmap)
+                       goto error;
+       } else {
+               bmap = isl_basic_map_reverse(bmap);
+               if (tok)
+                       isl_stream_push_token(s, tok);
+       }
+       tok = isl_stream_next_token(s);
+       if (!tok) {
+               isl_stream_error(s, NULL, "unexpected EOF");
+               goto error;
+       }
+       map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
+       if (tok->type == ':') {
+               isl_token_free(tok);
+               map = isl_map_intersect(map,
+                           read_disjuncts(s, v, isl_basic_map_get_dim(bmap)));
+       } else
+               isl_stream_push_token(s, tok);
 
-       dim = isl_dim_alloc(vars->ctx, nparam, n_in, n_out);
-       dim = set_names(dim, vars, isl_dim_param, n_out + n_in, nparam);
-       dim = set_names(dim, vars, isl_dim_in, n_out, n_in);
-       dim = set_names(dim, vars, isl_dim_out, 0, n_out);
+       vars_drop(v, v->n - n);
 
-       return dim;
+       isl_basic_map_free(bmap);
+       return map;
+error:
+       isl_basic_map_free(bmap);
+       return NULL;
 }
 
 static struct isl_map *map_read(struct isl_stream *s, int nparam)
 {
-       struct isl_dim *dim = NULL;
+       struct isl_basic_map *bmap = NULL;
        struct isl_map *map = NULL;
        struct isl_token *tok;
        struct vars *v = NULL;
-       int n1;
-       int n2;
 
        tok = isl_stream_next_token(s);
        if (!tok) {
@@ -910,14 +1025,14 @@ static struct isl_map *map_read(struct isl_stream *s, int nparam)
                return map_read_polylib(s, nparam);
        }
        v = vars_new(s->ctx);
+       bmap = isl_basic_map_alloc(s->ctx, 0, 0, 0, 0, 0, 0);
        if (tok->type == '[') {
                isl_stream_push_token(s, tok);
-               v = read_tuple(s, v);
-               if (!v)
-                       return NULL;
+               bmap = read_tuple(s, bmap, isl_dim_param, v);
+               if (!bmap)
+                       goto error;
                if (nparam >= 0)
                        isl_assert(s->ctx, nparam == v->n, goto error);
-               nparam = v->n;
                tok = isl_stream_next_token(s);
                if (!tok || tok->type != ISL_TOKEN_TO) {
                        isl_stream_error(s, tok, "expecting '->'");
@@ -927,9 +1042,8 @@ static struct isl_map *map_read(struct isl_stream *s, int nparam)
                }
                isl_token_free(tok);
                tok = isl_stream_next_token(s);
-       }
-       if (nparam < 0)
-               nparam = 0;
+       } if (nparam > 0)
+               bmap = isl_basic_map_add(bmap, isl_dim_param, nparam);
        if (!tok || tok->type != '{') {
                isl_stream_error(s, tok, "expecting '{'");
                if (tok)
@@ -937,35 +1051,21 @@ static struct isl_map *map_read(struct isl_stream *s, int nparam)
                goto error;
        }
        isl_token_free(tok);
-       v = read_tuple(s, v);
-       if (!v)
-               return NULL;
-       n1 = v->n - nparam;
-       tok = isl_stream_next_token(s);
-       if (tok && tok->type == ISL_TOKEN_TO) {
+
+       for (;;) {
+               isl_map *m = map_read_body(s, isl_basic_map_copy(bmap), v);
+               if (!m)
+                       break;
+               if (map)
+                       map = isl_map_union(map, m);
+               else
+                       map = m;
+               tok = isl_stream_next_token(s);
+               if (!tok || tok->type != ';')
+                       break;
                isl_token_free(tok);
-               v = read_tuple(s, v);
-               if (!v)
-                       return NULL;
-               n2 = v->n - n1 - nparam;
-       } else {
-               if (tok)
-                       isl_stream_push_token(s, tok);
-               n2 = n1;
-               n1 = 0;
        }
-       dim = dim_from_vars(v, nparam, n1, n2);
-       tok = isl_stream_next_token(s);
-       if (!tok) {
-               isl_stream_error(s, NULL, "unexpected EOF");
-               goto error;
-       }
-       if (tok->type == ':') {
-               isl_token_free(tok);
-               map = read_disjuncts(s, v, isl_dim_copy(dim));
-               tok = isl_stream_next_token(s);
-       } else
-               map = isl_map_universe(isl_dim_copy(dim));
+
        if (tok && tok->type == '}') {
                isl_token_free(tok);
        } else {
@@ -975,11 +1075,11 @@ static struct isl_map *map_read(struct isl_stream *s, int nparam)
                goto error;
        }
        vars_free(v);
-       isl_dim_free(dim);
+       isl_basic_map_free(bmap);
 
        return map;
 error:
-       isl_dim_free(dim);
+       isl_basic_map_free(bmap);
        isl_map_free(map);
        if (v)
                vars_free(v);