add isl_aff_mod_val
[platform/upstream/isl.git] / isl_input.c
index 6bf6929..164a269 100644 (file)
@@ -26,6 +26,7 @@
 #include <isl_mat_private.h>
 #include <isl_aff_private.h>
 #include <isl/list.h>
+#include <isl_val_private.h>
 
 struct variable {
        char                    *name;
@@ -168,6 +169,86 @@ error:
        return NULL;
 }
 
+/* Read an isl_val from "s".
+ *
+ * The following token sequences are recognized
+ *
+ *     "infty"         ->      infty
+ *     "-" "infty"     ->      -infty
+ *     "NaN"           ->      NaN
+ *     n "/" d         ->      n/d
+ *     v               ->      v
+ *
+ * where n, d and v are integer constants.
+ */
+__isl_give isl_val *isl_stream_read_val(struct isl_stream *s)
+{
+       struct isl_token *tok = NULL;
+       struct isl_token *tok2 = NULL;
+       isl_val *val;
+
+       tok = next_token(s);
+       if (!tok) {
+               isl_stream_error(s, NULL, "unexpected EOF");
+               goto error;
+       }
+       if (tok->type == ISL_TOKEN_INFTY) {
+               isl_token_free(tok);
+               return isl_val_infty(s->ctx);
+       }
+       if (tok->type == '-' &&
+           isl_stream_eat_if_available(s, ISL_TOKEN_INFTY)) {
+               isl_token_free(tok);
+               return isl_val_neginfty(s->ctx);
+       }
+       if (tok->type == ISL_TOKEN_NAN) {
+               isl_token_free(tok);
+               return isl_val_nan(s->ctx);
+       }
+       if (tok->type != ISL_TOKEN_VALUE) {
+               isl_stream_error(s, tok, "expecting value");
+               goto error;
+       }
+
+       if (isl_stream_eat_if_available(s, '/')) {
+               tok2 = next_token(s);
+               if (!tok2) {
+                       isl_stream_error(s, NULL, "unexpected EOF");
+                       goto error;
+               }
+               if (tok2->type != ISL_TOKEN_VALUE) {
+                       isl_stream_error(s, tok2, "expecting value");
+                       goto error;
+               }
+               val = isl_val_rat_from_isl_int(s->ctx, tok->u.v, tok2->u.v);
+               val = isl_val_normalize(val);
+       } else {
+               val = isl_val_int_from_isl_int(s->ctx, tok->u.v);
+       }
+
+       isl_token_free(tok);
+       isl_token_free(tok2);
+       return val;
+error:
+       isl_token_free(tok);
+       isl_token_free(tok2);
+       return NULL;
+}
+
+/* Read an isl_val from "str".
+ */
+struct isl_val *isl_val_read_from_str(struct isl_ctx *ctx,
+       const char *str)
+{
+       isl_val *val;
+       struct isl_stream *s = isl_stream_new_str(ctx, str);
+       if (!s)
+               return NULL;
+       val = isl_stream_read_val(s);
+       isl_stream_free(s);
+       return val;
+}
+
 static int accept_cst_factor(struct isl_stream *s, isl_int *f)
 {
        struct isl_token *tok;
@@ -343,6 +424,7 @@ static __isl_give isl_pw_aff *accept_affine_factor(struct isl_stream *s,
                if (pos < 0)
                        goto error;
                if (pos >= n) {
+                       vars_drop(v, v->n - n);
                        isl_stream_error(s, tok, "unknown identifier");
                        goto error;
                }
@@ -700,7 +782,7 @@ static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
                list = isl_pw_aff_list_concat(list,
                                isl_pw_aff_list_from_pw_aff(pwaff));
                if (!list)
-                       return NULL;
+                       goto error;
        }
 
        isl_space_free(dim);
@@ -914,9 +996,10 @@ static __isl_give isl_multi_pw_aff *read_tuple_var_def(struct isl_stream *s,
 /* Read a list of variables and/or affine expressions and return the list
  * as an isl_multi_pw_aff.
  * The elements in the list are separated by either "," or "][".
+ * If "comma" is set then only "," is allowed.
  */
 static __isl_give isl_multi_pw_aff *read_tuple_var_list(struct isl_stream *s,
-       struct vars *v, int rational)
+       struct vars *v, int rational, int comma)
 {
        int i = 0;
        struct isl_token *tok;
@@ -940,7 +1023,11 @@ static __isl_give isl_multi_pw_aff *read_tuple_var_list(struct isl_stream *s,
                        new_name = p >= n;
                }
 
-               if (new_name) {
+               if (tok->type == '*') {
+                       if (vars_add_anon(v) < 0)
+                               goto error;
+                       isl_token_free(tok);
+               } else if (new_name) {
                        res = tuple_set_dim_name(res, i, v->v->name);
                        isl_token_free(tok);
                        if (isl_stream_eat_if_available(s, '='))
@@ -955,7 +1042,7 @@ static __isl_give isl_multi_pw_aff *read_tuple_var_list(struct isl_stream *s,
                }
 
                tok = isl_stream_next_token(s);
-               if (tok && tok->type == ']' &&
+               if (!comma && tok && tok->type == ']' &&
                    isl_stream_next_token_is(s, '[')) {
                        isl_token_free(tok);
                        tok = isl_stream_next_token(s);
@@ -977,7 +1064,7 @@ error:
 /* Read a tuple and represent it as an isl_multi_pw_aff.  See tuple_alloc.
  */
 static __isl_give isl_multi_pw_aff *read_tuple(struct isl_stream *s,
-       struct vars *v, int rational)
+       struct vars *v, int rational, int comma)
 {
        struct isl_token *tok;
        char *name = NULL;
@@ -998,15 +1085,15 @@ static __isl_give isl_multi_pw_aff *read_tuple(struct isl_stream *s,
        if (next_is_tuple(s)) {
                isl_multi_pw_aff *out;
                int n;
-               res = read_tuple(s, v, rational);
+               res = read_tuple(s, v, rational, comma);
                if (isl_stream_eat(s, ISL_TOKEN_TO))
                        goto error;
-               out = read_tuple(s, v, rational);
+               out = read_tuple(s, v, rational, comma);
                n = isl_multi_pw_aff_dim(out, isl_dim_out);
                res = isl_multi_pw_aff_add_dims(res, isl_dim_in, n);
                res = isl_multi_pw_aff_range_product(res, out);
        } else
-               res = read_tuple_var_list(s, v, rational);
+               res = read_tuple_var_list(s, v, rational, comma);
        if (isl_stream_eat(s, ']'))
                goto error;
 
@@ -1030,18 +1117,20 @@ error:
  */
 static __isl_give isl_map *read_map_tuple(struct isl_stream *s,
        __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
-       int rational)
+       int rational, int comma)
 {
        int i, n;
        isl_multi_pw_aff *tuple;
        isl_space *space = NULL;
 
-       tuple = read_tuple(s, v, rational);
+       tuple = read_tuple(s, v, rational, comma);
        if (!tuple)
                goto error;
 
        n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
        space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
+       if (!space)
+               goto error;
 
        if (type == isl_dim_param) {
                if (isl_space_has_tuple_name(space, isl_dim_set) ||
@@ -1115,28 +1204,24 @@ static __isl_give isl_set *construct_constraints(
 {
        isl_set *cond;
 
+       left = isl_pw_aff_list_copy(left);
+       right = isl_pw_aff_list_copy(right);
        if (rational) {
                left = isl_pw_aff_list_set_rational(left);
                right = isl_pw_aff_list_set_rational(right);
        }
        if (type == ISL_TOKEN_LE)
-               cond = isl_pw_aff_list_le_set(isl_pw_aff_list_copy(left),
-                                             isl_pw_aff_list_copy(right));
+               cond = isl_pw_aff_list_le_set(left, right);
        else if (type == ISL_TOKEN_GE)
-               cond = isl_pw_aff_list_ge_set(isl_pw_aff_list_copy(left),
-                                             isl_pw_aff_list_copy(right));
+               cond = isl_pw_aff_list_ge_set(left, right);
        else if (type == ISL_TOKEN_LT)
-               cond = isl_pw_aff_list_lt_set(isl_pw_aff_list_copy(left),
-                                             isl_pw_aff_list_copy(right));
+               cond = isl_pw_aff_list_lt_set(left, right);
        else if (type == ISL_TOKEN_GT)
-               cond = isl_pw_aff_list_gt_set(isl_pw_aff_list_copy(left),
-                                             isl_pw_aff_list_copy(right));
+               cond = isl_pw_aff_list_gt_set(left, right);
        else if (type == ISL_TOKEN_NE)
-               cond = isl_pw_aff_list_ne_set(isl_pw_aff_list_copy(left),
-                                             isl_pw_aff_list_copy(right));
+               cond = isl_pw_aff_list_ne_set(left, right);
        else
-               cond = isl_pw_aff_list_eq_set(isl_pw_aff_list_copy(left),
-                                             isl_pw_aff_list_copy(right));
+               cond = isl_pw_aff_list_eq_set(left, right);
 
        return isl_set_intersect(set, cond);
 }
@@ -1349,7 +1434,7 @@ static __isl_give isl_map *read_conjuncts(struct isl_stream *s,
        if (negate)
                res = isl_map_subtract(isl_map_copy(map), res);
 
-       while (isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
+       while (res && isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
                isl_map *res_i;
 
                negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
@@ -1686,8 +1771,9 @@ static __isl_give isl_pw_qpolynomial *read_factor(struct isl_stream *s,
                pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
        } else if (tok->type == ISL_TOKEN_VALUE) {
                struct isl_token *tok2;
-               tok2 = isl_stream_next_token(s);
                isl_qpolynomial *qp;
+
+               tok2 = isl_stream_next_token(s);
                if (tok2 && tok2->type == '/') {
                        isl_token_free(tok2);
                        tok2 = next_token(s);
@@ -1932,7 +2018,7 @@ static struct isl_obj obj_read_body(struct isl_stream *s,
        if (!next_is_tuple(s))
                return obj_read_poly_or_fold(s, map, v, n);
 
-       map = read_map_tuple(s, map, isl_dim_in, v, rational);
+       map = read_map_tuple(s, map, isl_dim_in, v, rational, 0);
        if (!map)
                goto error;
        tok = isl_stream_next_token(s);
@@ -1945,7 +2031,7 @@ static struct isl_obj obj_read_body(struct isl_stream *s,
                        isl_set *set = isl_map_domain(map);
                        return obj_read_poly_or_fold(s, set, v, n);
                }
-               map = read_map_tuple(s, map, isl_dim_out, v, rational);
+               map = read_map_tuple(s, map, isl_dim_out, v, rational, 0);
                if (!map)
                        goto error;
        } else {
@@ -2087,7 +2173,7 @@ static struct isl_obj obj_read(struct isl_stream *s)
        map = isl_map_universe(isl_space_params_alloc(s->ctx, 0));
        if (tok->type == '[') {
                isl_stream_push_token(s, tok);
-               map = read_map_tuple(s, map, isl_dim_param, v, 0);
+               map = read_map_tuple(s, map, isl_dim_param, v, 0, 0);
                if (!map)
                        goto error;
                tok = isl_stream_next_token(s);
@@ -2115,7 +2201,7 @@ static struct isl_obj obj_read(struct isl_stream *s)
                isl_token_free(tok);
                if (isl_stream_eat(s, '='))
                        goto error;
-               map = read_map_tuple(s, map, isl_dim_param, v, 0);
+               map = read_map_tuple(s, map, isl_dim_param, v, 0, 1);
                if (!map)
                        goto error;
        } else if (tok->type == '}') {
@@ -2569,7 +2655,7 @@ static __isl_give isl_set *read_aff_domain(struct isl_stream *s,
        tok = isl_stream_next_token(s);
        if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
                isl_stream_push_token(s, tok);
-               return read_map_tuple(s, dom, isl_dim_set, v, 1);
+               return read_map_tuple(s, dom, isl_dim_set, v, 1, 0);
        }
        if (!tok || tok->type != '[') {
                isl_stream_error(s, tok, "expecting '['");
@@ -2577,7 +2663,7 @@ static __isl_give isl_set *read_aff_domain(struct isl_stream *s,
        }
        if (next_is_tuple(s) || next_is_fresh_ident(s, v)) {
                isl_stream_push_token(s, tok);
-               dom = read_map_tuple(s, dom, isl_dim_set, v, 1);
+               dom = read_map_tuple(s, dom, isl_dim_set, v, 1, 0);
        } else
                isl_stream_push_token(s, tok);
 
@@ -2585,7 +2671,6 @@ static __isl_give isl_set *read_aff_domain(struct isl_stream *s,
 error:
        if (tok)
                isl_stream_push_token(s, tok);
-       vars_free(v);
        isl_set_free(dom);
        return NULL;
 }
@@ -2655,7 +2740,7 @@ __isl_give isl_pw_aff *isl_stream_read_pw_aff(struct isl_stream *s)
 
        dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
        if (next_is_tuple(s)) {
-               dom = read_map_tuple(s, dom, isl_dim_param, v, 1);
+               dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
                if (isl_stream_eat(s, ISL_TOKEN_TO))
                        goto error;
        }
@@ -2749,6 +2834,47 @@ __isl_give isl_pw_multi_aff *isl_pw_multi_aff_read_from_str(isl_ctx *ctx,
        return pma;
 }
 
+/* Read an isl_union_pw_multi_aff from "s".
+ * We currently read a generic object and if it turns out to be a set or
+ * a map, we convert that to an isl_union_pw_multi_aff.
+ * It would be more efficient if we were to construct
+ * the isl_union_pw_multi_aff directly.
+ */
+__isl_give isl_union_pw_multi_aff *isl_stream_read_union_pw_multi_aff(
+       struct isl_stream *s)
+{
+       struct isl_obj obj;
+
+       obj = obj_read(s);
+       if (!obj.v)
+               return NULL;
+
+       if (obj.type == isl_obj_map || obj.type == isl_obj_set)
+               obj = to_union(s->ctx, obj);
+       if (obj.type == isl_obj_union_map)
+               return isl_union_pw_multi_aff_from_union_map(obj.v);
+       if (obj.type == isl_obj_union_set)
+               return isl_union_pw_multi_aff_from_union_set(obj.v);
+
+       obj.type->free(obj.v);
+       isl_die(s->ctx, isl_error_invalid, "unexpected object type",
+               return NULL);
+}
+
+/* Read an isl_union_pw_multi_aff from "str".
+ */
+__isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_read_from_str(
+       isl_ctx *ctx, const char *str)
+{
+       isl_union_pw_multi_aff *upma;
+       struct isl_stream *s = isl_stream_new_str(ctx, str);
+       if (!s)
+               return NULL;
+       upma = isl_stream_read_union_pw_multi_aff(s);
+       isl_stream_free(s);
+       return upma;
+}
+
 /* Assuming "pa" represents a single affine expression defined on a universe
  * domain, extract this affine expression.
  */
@@ -2798,7 +2924,7 @@ __isl_give isl_multi_aff *isl_stream_read_multi_aff(struct isl_stream *s)
 
        dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
        if (next_is_tuple(s)) {
-               dom = read_map_tuple(s, dom, isl_dim_param, v, 1);
+               dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
                if (isl_stream_eat(s, ISL_TOKEN_TO))
                        goto error;
        }
@@ -2808,7 +2934,7 @@ __isl_give isl_multi_aff *isl_stream_read_multi_aff(struct isl_stream *s)
        if (isl_stream_eat(s, '{'))
                goto error;
 
-       tuple = read_tuple(s, v, 0);
+       tuple = read_tuple(s, v, 0, 0);
        if (!tuple)
                goto error;
        if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
@@ -2826,7 +2952,7 @@ __isl_give isl_multi_aff *isl_stream_read_multi_aff(struct isl_stream *s)
                set = isl_set_universe(space);
                dom = isl_set_intersect_params(set, dom);
                isl_multi_pw_aff_free(tuple);
-               tuple = read_tuple(s, v, 0);
+               tuple = read_tuple(s, v, 0, 0);
                if (!tuple)
                        goto error;
        }