isl_stream_read_map: properly read nested divs
[platform/upstream/isl.git] / isl_input.c
index ed21140..607732e 100644 (file)
@@ -14,6 +14,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <strings.h>
+#include <isl_ctx_private.h>
 #include <isl_map_private.h>
 #include <isl/set.h>
 #include <isl/seq.h>
@@ -160,11 +161,40 @@ static __isl_give isl_basic_map *set_name(__isl_take isl_basic_map *bmap,
        return bmap;
 }
 
+/* Obtain next token, with some preprocessing.
+ * In particular, evaluate expressions of the form x^y,
+ * with x and y values.
+ */
+static struct isl_token *next_token(struct isl_stream *s)
+{
+       struct isl_token *tok, *tok2;
+
+       tok = isl_stream_next_token(s);
+       if (!tok || tok->type != ISL_TOKEN_VALUE)
+               return tok;
+       if (!isl_stream_eat_if_available(s, '^'))
+               return tok;
+       tok2 = isl_stream_next_token(s);
+       if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
+               isl_stream_error(s, tok2, "expecting constant value");
+               goto error;
+       }
+
+       isl_int_pow_ui(tok->u.v, tok->u.v, isl_int_get_ui(tok2->u.v));
+
+       isl_token_free(tok2);
+       return tok;
+error:
+       isl_token_free(tok);
+       isl_token_free(tok2);
+       return NULL;
+}
+
 static int accept_cst_factor(struct isl_stream *s, isl_int *f)
 {
        struct isl_token *tok;
 
-       tok = isl_stream_next_token(s);
+       tok = next_token(s);
        if (!tok || tok->type != ISL_TOKEN_VALUE) {
                isl_stream_error(s, tok, "expecting constant value");
                goto error;
@@ -197,7 +227,7 @@ static __isl_give isl_vec *affine_mod(struct isl_stream *s,
        struct variable *var;
        isl_vec *mod;
 
-       tok = isl_stream_next_token(s);
+       tok = next_token(s);
        if (!tok || tok->type != ISL_TOKEN_VALUE) {
                isl_stream_error(s, tok, "expecting constant value");
                goto error;
@@ -240,7 +270,7 @@ static __isl_give isl_vec *accept_affine_factor(struct isl_stream *s,
        struct isl_token *tok = NULL;
        isl_vec *aff = NULL;
 
-       tok = isl_stream_next_token(s);
+       tok = next_token(s);
        if (!tok) {
                isl_stream_error(s, NULL, "unexpected EOF");
                goto error;
@@ -293,6 +323,7 @@ static __isl_give isl_vec *accept_affine_factor(struct isl_stream *s,
                tok = NULL;
                if (read_div_definition(s, v) < 0)
                        goto error;
+               aff = isl_vec_zero_extend(aff, 1 + v->n);
        } else {
                isl_stream_error(s, tok, "expecting factor");
                goto error;
@@ -331,7 +362,7 @@ static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v)
        isl_seq_clr(aff->el, aff->size);
 
        for (;;) {
-               tok = isl_stream_next_token(s);
+               tok = next_token(s);
                if (!tok) {
                        isl_stream_error(s, NULL, "unexpected EOF");
                        goto error;
@@ -378,7 +409,7 @@ static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v)
                }
                isl_token_free(tok);
 
-               tok = isl_stream_next_token(s);
+               tok = next_token(s);
                if (tok && tok->type == '-') {
                        sign = -sign;
                        isl_token_free(tok);
@@ -489,7 +520,7 @@ static __isl_give isl_basic_map *read_var_list(struct isl_stream *s,
        int i = 0;
        struct isl_token *tok;
 
-       while ((tok = isl_stream_next_token(s)) != NULL) {
+       while ((tok = next_token(s)) != NULL) {
                int new_name = 0;
 
                if (tok->type == ISL_TOKEN_IDENT) {
@@ -614,7 +645,7 @@ static int read_div_definition(struct isl_stream *s, struct vars *v)
        if (isl_stream_eat(s, '/'))
                return -1;
 
-       tok = isl_stream_next_token(s);
+       tok = next_token(s);
        if (!tok)
                return -1;
        if (tok->type != ISL_TOKEN_VALUE) {
@@ -867,6 +898,40 @@ static int is_comparator(struct isl_token *tok)
        }
 }
 
+/* Add any variables in the variable list "v" that are not already in "bmap"
+ * as output variables in "bmap".
+ */
+static __isl_give isl_basic_map *add_lifted_divs(__isl_take isl_basic_map *bmap,
+       struct vars *v)
+{
+       int i;
+       int extra;
+       struct variable *var;
+
+       extra = v->n - isl_basic_map_total_dim(bmap);
+
+       if (extra == 0)
+               return bmap;
+
+       bmap = isl_basic_map_add(bmap, isl_dim_out, extra);
+       bmap = isl_basic_map_extend_dim(bmap, isl_basic_map_get_dim(bmap),
+                                       0, 0, 2 * extra);
+
+       for (i = 0, var = v->v; i < extra; ++i, var = var->next) {
+               var->def = isl_vec_zero_extend(var->def, 2 + v->n);
+               if (!var->def)
+                       goto error;
+               if (isl_basic_map_add_div_constraints_var(bmap, var->pos,
+                                                         var->def->el) < 0)
+                       goto error;
+       }
+
+       return bmap;
+error:
+       isl_basic_map_free(bmap);
+       return NULL;
+}
+
 static struct isl_basic_map *add_constraint(struct isl_stream *s,
        struct vars *v, struct isl_basic_map *bmap)
 {
@@ -895,7 +960,7 @@ static struct isl_basic_map *add_constraint(struct isl_stream *s,
                aff1 = isl_mat_add_zero_cols(aff1, aff2->n_col - aff1->n_col);
                if (!aff1)
                        goto error;
-               bmap = add_divs(bmap, v);
+               bmap = add_lifted_divs(bmap, v);
                bmap = isl_basic_map_extend_constraints(bmap, 0,
                                                aff1->n_row * aff2->n_row);
                for (i = 0; i < aff1->n_row; ++i)
@@ -933,16 +998,17 @@ static isl_map *read_constraint(struct isl_stream *s,
        if (!bmap)
                return NULL;
 
+       bmap = isl_basic_set_unwrap(isl_basic_set_lift(isl_basic_map_wrap(bmap)));
+
        bmap = add_constraint(s, v, bmap);
        bmap = isl_basic_map_simplify(bmap);
        bmap = isl_basic_map_finalize(bmap);
 
+       bmap = isl_basic_set_unwrap(isl_basic_map_domain(bmap));
+
        vars_drop(v, v->n - n);
 
        return isl_map_from_basic_map(bmap);
-error:
-       isl_basic_map_free(bmap);
-       return NULL;
 }
 
 static struct isl_map *read_disjuncts(struct isl_stream *s,
@@ -1367,7 +1433,7 @@ static __isl_give isl_qpolynomial *read_factor(struct isl_stream *s,
        struct isl_qpolynomial *qp;
        struct isl_token *tok;
 
-       tok = isl_stream_next_token(s);
+       tok = next_token(s);
        if (!tok) {
                isl_stream_error(s, NULL, "unexpected EOF");
                return NULL;
@@ -1388,7 +1454,7 @@ static __isl_give isl_qpolynomial *read_factor(struct isl_stream *s,
                tok2 = isl_stream_next_token(s);
                if (tok2 && tok2->type == '/') {
                        isl_token_free(tok2);
-                       tok2 = isl_stream_next_token(s);
+                       tok2 = next_token(s);
                        if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
                                isl_stream_error(s, tok2, "expected denominator");
                                isl_token_free(tok);
@@ -1472,7 +1538,7 @@ static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
        qp = read_factor(s, bmap, v);
 
        for (;;) {
-               tok = isl_stream_next_token(s);
+               tok = next_token(s);
                if (!tok)
                        return qp;
 
@@ -1744,7 +1810,7 @@ static struct isl_obj obj_read(struct isl_stream *s, int nparam)
        struct vars *v = NULL;
        struct isl_obj obj = { isl_obj_set, NULL };
 
-       tok = isl_stream_next_token(s);
+       tok = next_token(s);
        if (!tok) {
                isl_stream_error(s, NULL, "unexpected EOF");
                goto error;
@@ -2079,6 +2145,18 @@ error:
        return NULL;
 }
 
+__isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
+       FILE *input)
+{
+       isl_union_map *umap;
+       struct isl_stream *s = isl_stream_new_file(ctx, input);
+       if (!s)
+               return NULL;
+       umap = isl_stream_read_union_map(s);
+       isl_stream_free(s);
+       return umap;
+}
+
 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
                const char *str)
 {
@@ -2091,6 +2169,18 @@ __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
        return umap;
 }
 
+__isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
+       FILE *input)
+{
+       isl_union_set *uset;
+       struct isl_stream *s = isl_stream_new_file(ctx, input);
+       if (!s)
+               return NULL;
+       uset = isl_stream_read_union_set(s);
+       isl_stream_free(s);
+       return uset;
+}
+
 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
                const char *str)
 {
@@ -2138,22 +2228,18 @@ error:
        return NULL;
 }
 
-static __isl_give isl_vec *vec_read(struct isl_stream *s,
-       unsigned input_format)
+static __isl_give isl_vec *vec_read(struct isl_stream *s)
 {
-       if (input_format == ISL_FORMAT_POLYLIB)
-               return isl_vec_read_polylib(s);
-       isl_assert(s->ctx, 0, return NULL);
+       return isl_vec_read_polylib(s);
 }
 
-struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
-               FILE *input, unsigned input_format)
+__isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
 {
        isl_vec *v;
        struct isl_stream *s = isl_stream_new_file(ctx, input);
        if (!s)
                return NULL;
-       v = vec_read(s, input_format);
+       v = vec_read(s);
        isl_stream_free(s);
        return v;
 }