isl_tab_pip.c: fix typos in comments
[platform/upstream/isl.git] / isl_input.c
index 7ff7fa2..3fcf5e0 100644 (file)
 #include <stdio.h>
 #include <string.h>
 #include <strings.h>
-#include <isl_set.h>
-#include <isl_seq.h>
-#include <isl_div.h>
-#include "isl_stream.h"
+#include <isl/set.h>
+#include <isl/seq.h>
+#include <isl/div.h>
+#include <isl/stream.h>
 #include "isl_map_private.h"
-#include "isl_obj.h"
+#include <isl/obj.h>
 #include "isl_polynomial_private.h"
-#include <isl_union_map.h>
+#include <isl/union_map.h>
 #include <isl_mat_private.h>
 
 struct variable {
@@ -376,8 +376,9 @@ static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
        for (i = 0, var = v->v; i < extra; ++i, var = var->next) {
                int k = bmap->n_div - 1 - i;
 
-               isl_seq_cpy(bmap->div[k], var->def->el, 2 + var->pos);
-               isl_seq_clr(bmap->div[k] + 2 + var->pos, v->n - var->pos);
+               isl_seq_cpy(bmap->div[k], var->def->el, var->def->size);
+               isl_seq_clr(bmap->div[k] + var->def->size,
+                           2 + v->n - var->def->size);
 
                if (isl_basic_map_add_div_constraints(bmap, k) < 0)
                        goto error;
@@ -392,6 +393,8 @@ error:
 static __isl_give isl_basic_map *read_var_def(struct isl_stream *s,
        __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
 {
+       isl_dim *dim;
+       isl_basic_map *def = NULL;
        struct isl_vec *vec;
        int k;
        int n;
@@ -404,12 +407,14 @@ static __isl_give isl_basic_map *read_var_def(struct isl_stream *s,
        if (!vec)
                goto error;
 
-       bmap = add_divs(bmap, v);
-       bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
-       k = isl_basic_map_alloc_equality(bmap);
+       dim = isl_basic_map_get_dim(bmap);
+       def = isl_basic_map_universe(dim);
+       def = add_divs(def, v);
+       def = isl_basic_map_extend_constraints(def, 1, 0);
+       k = isl_basic_map_alloc_equality(def);
        if (k >= 0) {
-               isl_seq_cpy(bmap->eq[k], vec->el, vec->size);
-               isl_int_set_si(bmap->eq[k][1 + n - 1], -1);
+               isl_seq_cpy(def->eq[k], vec->el, vec->size);
+               isl_int_set_si(def->eq[k][1 + n - 1], -1);
        }
        isl_vec_free(vec);
        if (k < 0)
@@ -417,9 +422,13 @@ static __isl_give isl_basic_map *read_var_def(struct isl_stream *s,
 
        vars_drop(v, v->n - n);
 
+       def = isl_basic_map_simplify(def);
+       def = isl_basic_map_finalize(def);
+       bmap = isl_basic_map_intersect(bmap, def);
        return bmap;
 error:
        isl_basic_map_free(bmap);
+       isl_basic_map_free(def);
        return NULL;
 }
 
@@ -1137,6 +1146,13 @@ static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
                isl_stream_error(s, NULL, "unexpected EOF");
                return NULL;
        }
+       if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
+               isl_stream_push_token(s, tok2);
+               isl_stream_push_token(s, tok);
+               isl_stream_error(s, NULL,
+                                "expecting constraint matrix dimensions");
+               return NULL;
+       }
        n_row = isl_int_get_si(tok->u.v);
        n_col = isl_int_get_si(tok2->u.v);
        on_new_line = tok2->on_new_line;
@@ -1237,11 +1253,17 @@ static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
                return NULL;
        }
        tok2 = isl_stream_next_token_on_same_line(s);
-       if (tok2) {
+       if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
                isl_stream_push_token(s, tok2);
                isl_stream_push_token(s, tok);
                return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
        }
+       if (tok2) {
+               isl_stream_error(s, tok2, "unexpected token");
+               isl_stream_push_token(s, tok2);
+               isl_stream_push_token(s, tok);
+               return NULL;
+       }
        n = isl_int_get_si(tok->u.v);
        isl_token_free(tok);
 
@@ -1249,7 +1271,7 @@ static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
 
        map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
 
-       for (i = 1; i < n; ++i)
+       for (i = 1; map && i < n; ++i)
                map = isl_map_union(map,
                        isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
 
@@ -1282,26 +1304,25 @@ static int optional_power(struct isl_stream *s)
 }
 
 static __isl_give isl_div *read_div(struct isl_stream *s,
-       __isl_keep isl_basic_map *bmap, struct vars *v)
+       __isl_take isl_dim *dim, struct vars *v)
 {
-       unsigned total = isl_basic_map_total_dim(bmap);
-       int k;
-
-       bmap = isl_basic_map_copy(bmap);
-       bmap = isl_basic_map_cow(bmap);
-       bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
-                                       1, 0, 2);
+       int n;
+       isl_basic_map *bmap;
 
-       if ((k = isl_basic_map_alloc_div(bmap)) < 0)
-               goto error;
-       isl_seq_clr(bmap->div[k], 1 + 1 + total);
+       n = v->n;
+       bmap = isl_basic_map_universe(dim);
 
        if (vars_add_anon(v) < 0)
                goto error;
-       bmap = add_div_definition(s, v, bmap, k);
-       vars_drop(v, 1);
+       if (read_div_definition(s, v) < 0)
+               goto error;
+       bmap = add_divs(bmap, v);
+       bmap = isl_basic_map_order_divs(bmap);
+       if (!bmap)
+               goto error;
+       vars_drop(v, v->n - n);
 
-       return isl_basic_map_div(bmap, k);
+       return isl_basic_map_div(bmap, bmap->n_div - 1);
 error:
        isl_basic_map_free(bmap);
        return NULL;
@@ -1322,12 +1343,16 @@ static __isl_give isl_qpolynomial *read_factor(struct isl_stream *s,
                return NULL;
        }
        if (tok->type == '(') {
+               int pow;
+
                isl_token_free(tok);
                qp = read_term(s, bmap, v);
                if (!qp)
                        return NULL;
                if (isl_stream_eat(s, ')'))
                        goto error;
+               pow = optional_power(s);
+               qp = isl_qpolynomial_pow(qp, pow);
        } else if (tok->type == ISL_TOKEN_VALUE) {
                struct isl_token *tok2;
                tok2 = isl_stream_next_token(s);
@@ -1370,13 +1395,13 @@ static __isl_give isl_qpolynomial *read_factor(struct isl_stream *s,
                }
                isl_token_free(tok);
                pow = optional_power(s);
-               qp = isl_qpolynomial_pow(isl_basic_map_get_dim(bmap), pos, pow);
+               qp = isl_qpolynomial_var_pow(isl_basic_map_get_dim(bmap), pos, pow);
        } else if (tok->type == '[') {
                isl_div *div;
                int pow;
 
                isl_stream_push_token(s, tok);
-               div = read_div(s, bmap, v);
+               div = read_div(s, isl_basic_map_get_dim(bmap), v);
                pow = optional_power(s);
                qp = isl_qpolynomial_div_pow(div, pow);
        } else if (tok->type == '-') {
@@ -1811,6 +1836,49 @@ error:
        return NULL;
 }
 
+__isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
+{
+       struct isl_obj obj;
+       isl_union_map *umap;
+
+       obj = obj_read(s, -1);
+       if (obj.type == isl_obj_map) {
+               obj.type = isl_obj_union_map;
+               obj.v = isl_union_map_from_map(obj.v);
+       }
+       if (obj.type == isl_obj_set) {
+               obj.type = isl_obj_union_set;
+               obj.v = isl_union_set_from_set(obj.v);
+       }
+       if (obj.v)
+               isl_assert(s->ctx, obj.type == isl_obj_union_map ||
+                                  obj.type == isl_obj_union_set, goto error);
+
+       return obj.v;
+error:
+       obj.type->free(obj.v);
+       return NULL;
+}
+
+__isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
+{
+       struct isl_obj obj;
+       isl_union_set *uset;
+
+       obj = obj_read(s, -1);
+       if (obj.type == isl_obj_set) {
+               obj.type = isl_obj_union_set;
+               obj.v = isl_union_set_from_set(obj.v);
+       }
+       if (obj.v)
+               isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
+
+       return obj.v;
+error:
+       obj.type->free(obj.v);
+       return NULL;
+}
+
 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
 {
        struct isl_obj obj;
@@ -1941,60 +2009,83 @@ error:
        return NULL;
 }
 
-static char *next_line(FILE *input, char *line, unsigned len)
+__isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
+               const char *str)
 {
-       char *p;
-
-       do {
-               if (!(p = fgets(line, len, input)))
-                       return NULL;
-               while (isspace(*p) && *p != '\n')
-                       ++p;
-       } while (*p == '#' || *p == '\n');
+       isl_union_map *umap;
+       struct isl_stream *s = isl_stream_new_str(ctx, str);
+       if (!s)
+               return NULL;
+       umap = isl_stream_read_union_map(s);
+       isl_stream_free(s);
+       return umap;
+}
 
-       return p;
+__isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
+               const char *str)
+{
+       isl_union_set *uset;
+       struct isl_stream *s = isl_stream_new_str(ctx, str);
+       if (!s)
+               return NULL;
+       uset = isl_stream_read_union_set(s);
+       isl_stream_free(s);
+       return uset;
 }
 
-static struct isl_vec *isl_vec_read_from_file_polylib(struct isl_ctx *ctx,
-               FILE *input)
+static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
 {
        struct isl_vec *vec = NULL;
-       char line[1024];
-       char val[1024];
-       char *p;
+       struct isl_token *tok;
        unsigned size;
        int j;
-       int n;
-       int offset;
 
-       isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
-       isl_assert(ctx, sscanf(line, "%u", &size) == 1, return NULL);
+       tok = isl_stream_next_token(s);
+       if (!tok || tok->type != ISL_TOKEN_VALUE) {
+               isl_stream_error(s, tok, "expecting vector length");
+               goto error;
+       }
 
-       vec = isl_vec_alloc(ctx, size);
+       size = isl_int_get_si(tok->u.v);
+       isl_token_free(tok);
 
-       p = next_line(input, line, sizeof(line));
-       isl_assert(ctx, p, goto error);
+       vec = isl_vec_alloc(s->ctx, size);
 
        for (j = 0; j < size; ++j) {
-               n = sscanf(p, "%s%n", val, &offset);
-               isl_assert(ctx, n != 0, goto error);
-               isl_int_read(vec->el[j], val);
-               p += offset;
+               tok = isl_stream_next_token(s);
+               if (!tok || tok->type != ISL_TOKEN_VALUE) {
+                       isl_stream_error(s, tok, "expecting constant value");
+                       goto error;
+               }
+               isl_int_set(vec->el[j], tok->u.v);
+               isl_token_free(tok);
        }
 
        return vec;
 error:
+       isl_token_free(tok);
        isl_vec_free(vec);
        return NULL;
 }
 
+static __isl_give isl_vec *vec_read(struct isl_stream *s,
+       unsigned input_format)
+{
+       if (input_format == ISL_FORMAT_POLYLIB)
+               return isl_vec_read_polylib(s);
+       isl_assert(s->ctx, 0, return NULL);
+}
+
 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
                FILE *input, unsigned input_format)
 {
-       if (input_format == ISL_FORMAT_POLYLIB)
-               return isl_vec_read_from_file_polylib(ctx, input);
-       else
-               isl_assert(ctx, 0, return NULL);
+       isl_vec *v;
+       struct isl_stream *s = isl_stream_new_file(ctx, input);
+       if (!s)
+               return NULL;
+       v = vec_read(s, input_format);
+       isl_stream_free(s);
+       return v;
 }
 
 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(