/*
* Copyright 2008-2009 Katholieke Universiteit Leuven
* Copyright 2010 INRIA Saclay
+ * Copyright 2012 Ecole Normale Superieure
*
* Use of this software is governed by the MIT license
*
* Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
* and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
* ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
+ * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
*/
#include <ctype.h>
return 0;
}
-static __isl_give isl_map *set_name(__isl_take isl_map *map,
- enum isl_dim_type type, unsigned pos, char *name)
-{
- char *prime;
-
- if (!map)
- return NULL;
- if (!name)
- return map;
-
- prime = strchr(name, '\'');
- if (prime)
- *prime = '\0';
- map = isl_map_set_dim_name(map, type, pos, name);
- if (prime)
- *prime = '\'';
-
- return map;
-}
-
/* Obtain next token, with some preprocessing.
* In particular, evaluate expressions of the form x^y,
* with x and y values.
if (pos < 0)
goto error;
if (pos >= n) {
+ vars_drop(v, v->n - n);
isl_stream_error(s, tok, "unknown identifier");
goto error;
}
}
static struct isl_map *read_disjuncts(struct isl_stream *s,
- struct vars *v, __isl_take isl_map *map);
+ struct vars *v, __isl_take isl_map *map, int rational);
static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
- __isl_take isl_space *dim, struct vars *v);
+ __isl_take isl_space *dim, struct vars *v, int rational);
/* Accept a ternary operator, given the first argument.
*/
static __isl_give isl_pw_aff *accept_ternary(struct isl_stream *s,
- __isl_take isl_map *cond, struct vars *v)
+ __isl_take isl_map *cond, struct vars *v, int rational)
{
isl_space *dim;
isl_pw_aff *pwaff1 = NULL, *pwaff2 = NULL, *pa_cond;
goto error;
dim = isl_space_wrap(isl_map_get_space(cond));
- pwaff1 = accept_extended_affine(s, dim, v);
+ pwaff1 = accept_extended_affine(s, dim, v, rational);
if (!pwaff1)
goto error;
goto error;
dim = isl_pw_aff_get_domain_space(pwaff1);
- pwaff2 = accept_extended_affine(s, dim, v);
+ pwaff2 = accept_extended_affine(s, dim, v, rational);
if (!pwaff1)
goto error;
* argument of a ternary operator and try to parse that.
*/
static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
- __isl_take isl_space *dim, struct vars *v)
+ __isl_take isl_space *dim, struct vars *v, int rational)
{
isl_space *space;
isl_map *cond;
}
pwaff = accept_affine(s, dim, v);
+ if (rational)
+ pwaff = isl_pw_aff_set_rational(pwaff);
if (!pwaff)
return NULL;
isl_stream_push_token(s, tok);
- cond = read_disjuncts(s, v, cond);
+ cond = read_disjuncts(s, v, cond, rational);
- return accept_ternary(s, cond, v);
+ return accept_ternary(s, cond, v, rational);
}
static __isl_give isl_map *read_var_def(struct isl_stream *s,
- __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
+ __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
+ int rational)
{
isl_pw_aff *def;
int pos;
}
--pos;
- def = accept_extended_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
+ def = accept_extended_affine(s, isl_space_wrap(isl_map_get_space(map)),
+ v, rational);
def_map = isl_map_from_pw_aff(def);
def_map = isl_map_equate(def_map, type, pos, isl_dim_out, 0);
def_map = isl_set_unwrap(isl_map_domain(def_map));
return map;
}
-static __isl_give isl_map *read_var_list(struct isl_stream *s,
- __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
-{
- int i = 0;
- struct isl_token *tok;
-
- if (isl_stream_next_token_is(s, ']'))
- return isl_map_add_dims(map, type, 0);
-
- while ((tok = 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) {
- map = isl_map_add_dims(map, type, 1);
- map = set_name(map, type, i, v->v->name);
- isl_token_free(tok);
- if (isl_stream_eat_if_available(s, '='))
- map = read_var_def(s, map, type, v);
- } else {
- if (type == isl_dim_param) {
- isl_stream_error(s, tok,
- "expecting unique identifier");
- goto error;
- }
- isl_stream_push_token(s, tok);
- tok = NULL;
- if (vars_add_anon(v) < 0)
- goto error;
- map = isl_map_add_dims(map, type, 1);
- map = read_var_def(s, map, type, v);
- }
-
- tok = isl_stream_next_token(s);
- if (tok && tok->type == ']' &&
- isl_stream_next_token_is(s, '[')) {
- isl_token_free(tok);
- tok = isl_stream_next_token(s);
- } else if (!tok || tok->type != ',')
- break;
-
- isl_token_free(tok);
- i++;
- }
- if (tok)
- isl_stream_push_token(s, tok);
-
- return map;
-error:
- isl_token_free(tok);
- isl_map_free(map);
- return NULL;
-}
-
static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
__isl_take isl_space *dim, struct vars *v)
{
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);
}
static __isl_give isl_map *read_defined_var_list(struct isl_stream *s,
- struct vars *v, __isl_take isl_map *map)
+ struct vars *v, __isl_take isl_map *map, int rational)
{
struct isl_token *tok;
tok = isl_stream_next_token(s);
if (tok && tok->type == '=') {
isl_token_free(tok);
- map = read_var_def(s, map, isl_dim_out, v);
+ map = read_var_def(s, map, isl_dim_out, v, rational);
tok = isl_stream_next_token(s);
}
return is_tuple;
}
-static __isl_give isl_map *read_tuple(struct isl_stream *s,
- __isl_take isl_map *map, enum isl_dim_type type, struct vars *v);
+/* Allocate an initial tuple with zero dimensions and an anonymous,
+ * unstructured space.
+ * A tuple is represented as an isl_multi_pw_aff.
+ * The range space is the space of the tuple.
+ * The domain space is an anonymous space
+ * with a dimension for each variable in the set of variables in "v".
+ * If a given dimension is not defined in terms of earlier dimensions in
+ * the input, then the corresponding isl_pw_aff is set equal to one time
+ * the variable corresponding to the dimension being defined.
+ */
+static __isl_give isl_multi_pw_aff *tuple_alloc(struct vars *v)
+{
+ return isl_multi_pw_aff_alloc(isl_space_alloc(v->ctx, 0, v->n, 0));
+}
-static __isl_give isl_set *read_nested_tuple(struct isl_stream *s,
- __isl_take isl_map *map, struct vars *v)
+/* Is "pa" an expression in term of earlier dimensions?
+ * The alternative is that the dimension is defined to be equal to itself,
+ * meaning that it has a universe domain and an expression that depends
+ * on itself. "i" is the position of the expression in a sequence
+ * of "n" expressions. The final dimensions of "pa" correspond to
+ * these "n" expressions.
+ */
+static int pw_aff_is_expr(__isl_keep isl_pw_aff *pa, int i, int n)
{
- map = read_tuple(s, map, isl_dim_in, v);
- if (isl_stream_eat(s, ISL_TOKEN_TO))
- goto error;
- map = read_tuple(s, map, isl_dim_out, v);
- return isl_map_wrap(map);
+ isl_aff *aff;
+
+ if (!pa)
+ return -1;
+ if (pa->n != 1)
+ return 1;
+ if (!isl_set_plain_is_universe(pa->p[0].set))
+ return 1;
+
+ aff = pa->p[0].aff;
+ if (isl_int_is_zero(aff->v->el[aff->v->size - n + i]))
+ return 1;
+ return 0;
+}
+
+/* Does the tuple contain any dimensions that are defined
+ * in terms of earlier dimensions?
+ */
+static int tuple_has_expr(__isl_keep isl_multi_pw_aff *tuple)
+{
+ int i, n;
+ int has_expr = 0;
+ isl_pw_aff *pa;
+
+ if (!tuple)
+ return -1;
+ n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
+ for (i = 0; i < n; ++i) {
+ pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
+ has_expr = pw_aff_is_expr(pa, i, n);
+ isl_pw_aff_free(pa);
+ if (has_expr < 0 || has_expr)
+ break;
+ }
+
+ return has_expr;
+}
+
+/* Add a dimension to the given tuple.
+ * The dimension is initially undefined, so it is encoded
+ * as one times itself.
+ */
+static __isl_give isl_multi_pw_aff *tuple_add_dim(
+ __isl_take isl_multi_pw_aff *tuple, struct vars *v)
+{
+ isl_space *space;
+ isl_aff *aff;
+ isl_pw_aff *pa;
+
+ tuple = isl_multi_pw_aff_add_dims(tuple, isl_dim_in, 1);
+ space = isl_multi_pw_aff_get_domain_space(tuple);
+ aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
+ aff = isl_aff_add_coefficient_si(aff, isl_dim_in, v->n, 1);
+ pa = isl_pw_aff_from_aff(aff);
+ tuple = isl_multi_pw_aff_flat_range_product(tuple,
+ isl_multi_pw_aff_from_pw_aff(pa));
+
+ return tuple;
+}
+
+/* Set the name of dimension "pos" in "tuple" to "name".
+ * During printing, we add primes if the same name appears more than once
+ * to distinguish the occurrences. Here, we remove those primes from "name"
+ * before setting the name of the dimension.
+ */
+static __isl_give isl_multi_pw_aff *tuple_set_dim_name(
+ __isl_take isl_multi_pw_aff *tuple, int pos, char *name)
+{
+ char *prime;
+
+ if (!name)
+ return tuple;
+
+ prime = strchr(name, '\'');
+ if (prime)
+ *prime = '\0';
+ tuple = isl_multi_pw_aff_set_dim_name(tuple, isl_dim_set, pos, name);
+ if (prime)
+ *prime = '\'';
+
+ return tuple;
+}
+
+/* Read an affine expression from "s" and replace the definition
+ * of dimension "pos" in "tuple" by this expression.
+ *
+ * accept_extended_affine requires a wrapped space as input.
+ * The domain space of "tuple", on the other hand is an anonymous space,
+ * so we have to adjust the space of the isl_pw_aff before adding it
+ * to "tuple".
+ */
+static __isl_give isl_multi_pw_aff *read_tuple_var_def(struct isl_stream *s,
+ __isl_take isl_multi_pw_aff *tuple, int pos, struct vars *v,
+ int rational)
+{
+ isl_space *space;
+ isl_pw_aff *def;
+
+ space = isl_space_wrap(isl_space_alloc(s->ctx, 0, v->n, 0));
+ def = accept_extended_affine(s, space, v, rational);
+ space = isl_space_set_alloc(s->ctx, 0, v->n);
+ def = isl_pw_aff_reset_domain_space(def, space);
+ tuple = isl_multi_pw_aff_set_pw_aff(tuple, pos, def);
+
+ return tuple;
+}
+
+/* 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, int comma)
+{
+ int i = 0;
+ struct isl_token *tok;
+ isl_multi_pw_aff *res;
+
+ res = tuple_alloc(v);
+
+ if (isl_stream_next_token_is(s, ']'))
+ return res;
+
+ while ((tok = next_token(s)) != NULL) {
+ int new_name = 0;
+
+ res = tuple_add_dim(res, v);
+
+ 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 (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, '='))
+ res = read_tuple_var_def(s, res, i, v,
+ rational);
+ } else {
+ isl_stream_push_token(s, tok);
+ tok = NULL;
+ if (vars_add_anon(v) < 0)
+ goto error;
+ res = read_tuple_var_def(s, res, i, v, rational);
+ }
+
+ tok = isl_stream_next_token(s);
+ if (!comma && tok && tok->type == ']' &&
+ isl_stream_next_token_is(s, '[')) {
+ isl_token_free(tok);
+ tok = isl_stream_next_token(s);
+ } else if (!tok || tok->type != ',')
+ break;
+
+ isl_token_free(tok);
+ i++;
+ }
+ if (tok)
+ isl_stream_push_token(s, tok);
+
+ return res;
error:
- isl_map_free(map);
- return NULL;
+ isl_token_free(tok);
+ return isl_multi_pw_aff_free(res);
}
-static __isl_give isl_map *read_tuple(struct isl_stream *s,
- __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
+/* 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, int comma)
{
struct isl_token *tok;
char *name = NULL;
+ isl_multi_pw_aff *res = NULL;
tok = isl_stream_next_token(s);
- if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
+ if (!tok)
+ goto error;
+ if (tok->type == ISL_TOKEN_IDENT || tok->is_keyword) {
name = strdup(tok->u.s);
+ isl_token_free(tok);
if (!name)
goto error;
- isl_token_free(tok);
- tok = isl_stream_next_token(s);
- }
- if (!tok || tok->type != '[') {
- isl_stream_error(s, tok, "expecting '['");
+ } else
+ isl_stream_push_token(s, tok);
+ if (isl_stream_eat(s, '['))
goto error;
- }
- isl_token_free(tok);
- if (type != isl_dim_param && next_is_tuple(s)) {
- isl_space *dim = isl_map_get_space(map);
- int nparam = isl_space_dim(dim, isl_dim_param);
- int n_in = isl_space_dim(dim, isl_dim_in);
- isl_set *nested;
- if (type == isl_dim_out) {
- dim = isl_space_move_dims(dim, isl_dim_param, nparam,
- isl_dim_in, 0, n_in);
- dim = isl_space_params(dim);
- }
- nested = read_nested_tuple(s, isl_map_universe(dim), v);
- if (type == isl_dim_in) {
- nested = isl_map_reverse(nested);
- map = isl_map_intersect_params(nested, map);
- } else {
- isl_set *set;
- dim = isl_set_get_space(nested);
- dim = isl_space_drop_dims(dim, isl_dim_param, nparam, n_in);
- dim = isl_space_join(isl_map_get_space(map), dim);
- set = isl_map_domain(map);
- nested = isl_map_reset_space(nested, dim);
- map = isl_map_intersect_domain(nested, set);
- }
+ if (next_is_tuple(s)) {
+ isl_multi_pw_aff *out;
+ int n;
+ res = read_tuple(s, v, rational, comma);
+ if (isl_stream_eat(s, ISL_TOKEN_TO))
+ goto error;
+ 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
- map = read_var_list(s, map, type, v);
- tok = isl_stream_next_token(s);
- if (!tok || tok->type != ']') {
- isl_stream_error(s, tok, "expecting ']'");
+ res = read_tuple_var_list(s, v, rational, comma);
+ if (isl_stream_eat(s, ']'))
goto error;
- }
- isl_token_free(tok);
if (name) {
- map = isl_map_set_tuple_name(map, type, name);
+ res = isl_multi_pw_aff_set_tuple_name(res, isl_dim_out, name);
free(name);
}
+ return res;
+error:
+ free(name);
+ return isl_multi_pw_aff_free(res);
+}
+
+/* Read a tuple from "s" and add it to "map".
+ * The tuple is initially represented as an isl_multi_pw_aff.
+ * We first create the appropriate space in "map" based on the range
+ * space of this isl_multi_pw_aff. Then, we add equalities based
+ * on the affine expressions. These live in an anonymous space,
+ * however, so we first need to reset the space to that of "map".
+ */
+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 comma)
+{
+ int i, n;
+ isl_multi_pw_aff *tuple;
+ isl_space *space = NULL;
+
+ 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) ||
+ isl_space_is_wrapping(space)) {
+ isl_die(s->ctx, isl_error_invalid,
+ "parameter tuples cannot be named or nested",
+ goto error);
+ }
+ map = isl_map_add_dims(map, type, n);
+ for (i = 0; i < n; ++i) {
+ isl_id *id;
+ if (!isl_space_has_dim_name(space, isl_dim_set, i))
+ isl_die(s->ctx, isl_error_invalid,
+ "parameters must be named",
+ goto error);
+ id = isl_space_get_dim_id(space, isl_dim_set, i);
+ map = isl_map_set_dim_id(map, isl_dim_param, i, id);
+ }
+ } else if (type == isl_dim_in) {
+ isl_set *set;
+
+ set = isl_set_universe(isl_space_copy(space));
+ if (rational)
+ set = isl_set_set_rational(set);
+ set = isl_set_intersect_params(set, isl_map_params(map));
+ map = isl_map_from_domain(set);
+ } else {
+ isl_set *set;
+
+ set = isl_set_universe(isl_space_copy(space));
+ if (rational)
+ set = isl_set_set_rational(set);
+ map = isl_map_from_domain_and_range(isl_map_domain(map), set);
+ }
+
+ for (i = 0; i < n; ++i) {
+ isl_pw_aff *pa;
+ isl_space *space;
+ isl_aff *aff;
+ isl_set *set;
+ isl_map *map_i;
+
+ pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
+ space = isl_pw_aff_get_domain_space(pa);
+ aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
+ aff = isl_aff_add_coefficient_si(aff,
+ isl_dim_in, v->n - n + i, -1);
+ pa = isl_pw_aff_add(pa, isl_pw_aff_from_aff(aff));
+ if (rational)
+ pa = isl_pw_aff_set_rational(pa);
+ set = isl_pw_aff_zero_set(pa);
+ map_i = isl_map_from_range(set);
+ map_i = isl_map_reset_space(map_i, isl_map_get_space(map));
+ map = isl_map_intersect(map, map_i);
+ }
+
+ isl_space_free(space);
+ isl_multi_pw_aff_free(tuple);
return map;
error:
- if (tok)
- isl_token_free(tok);
+ isl_space_free(space);
+ isl_multi_pw_aff_free(tuple);
isl_map_free(map);
return NULL;
}
static __isl_give isl_set *construct_constraints(
__isl_take isl_set *set, int type,
- __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right)
+ __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right,
+ int rational)
{
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);
}
static __isl_give isl_map *add_constraint(struct isl_stream *s,
- struct vars *v, __isl_take isl_map *map)
+ struct vars *v, __isl_take isl_map *map, int rational)
{
struct isl_token *tok = NULL;
isl_pw_aff_list *list1 = NULL, *list2 = NULL;
if (!list2)
goto error;
- set = construct_constraints(set, tok->type, list1, list2);
+ set = construct_constraints(set, tok->type, list1, list2,
+ rational);
isl_token_free(tok);
isl_pw_aff_list_free(list1);
list1 = list2;
}
static __isl_give isl_map *read_exists(struct isl_stream *s,
- struct vars *v, __isl_take isl_map *map)
+ struct vars *v, __isl_take isl_map *map, int rational)
{
int n = v->n;
int seen_paren = isl_stream_eat_if_available(s, '(');
map = isl_map_from_domain(isl_map_wrap(map));
- map = read_defined_var_list(s, v, map);
+ map = read_defined_var_list(s, v, map, rational);
if (isl_stream_eat(s, ':'))
goto error;
- map = read_disjuncts(s, v, map);
+ map = read_disjuncts(s, v, map, rational);
map = isl_set_unwrap(isl_map_domain(map));
vars_drop(v, v->n - n);
* start of a condition and continue parsing.
*/
static int resolve_paren_expr(struct isl_stream *s,
- struct vars *v, __isl_take isl_map *map)
+ struct vars *v, __isl_take isl_map *map, int rational)
{
struct isl_token *tok, *tok2;
int line, col;
goto error;
if (isl_stream_next_token_is(s, '('))
- if (resolve_paren_expr(s, v, isl_map_copy(map)))
+ if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
goto error;
if (isl_stream_next_token_is(s, ISL_TOKEN_EXISTS) ||
isl_stream_next_token_is(s, ISL_TOKEN_TRUE) ||
isl_stream_next_token_is(s, ISL_TOKEN_FALSE) ||
isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
- map = read_disjuncts(s, v, map);
+ map = read_disjuncts(s, v, map, rational);
if (isl_stream_eat(s, ')'))
goto error;
tok->type = ISL_TOKEN_MAP;
isl_stream_push_token(s, tok2);
- map = read_disjuncts(s, v, map);
+ map = read_disjuncts(s, v, map, rational);
if (isl_stream_eat(s, ')'))
goto error;
}
static __isl_give isl_map *read_conjunct(struct isl_stream *s,
- struct vars *v, __isl_take isl_map *map)
+ struct vars *v, __isl_take isl_map *map, int rational)
{
if (isl_stream_next_token_is(s, '('))
- if (resolve_paren_expr(s, v, isl_map_copy(map)))
+ if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
goto error;
if (isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
}
if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
- return read_exists(s, v, map);
+ return read_exists(s, v, map, rational);
if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
return map;
return isl_map_empty(dim);
}
- return add_constraint(s, v, map);
+ return add_constraint(s, v, map, rational);
error:
isl_map_free(map);
return NULL;
}
static __isl_give isl_map *read_conjuncts(struct isl_stream *s,
- struct vars *v, __isl_take isl_map *map)
+ struct vars *v, __isl_take isl_map *map, int rational)
{
isl_map *res;
int negate;
negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
- res = read_conjunct(s, v, isl_map_copy(map));
+ res = read_conjunct(s, v, isl_map_copy(map), rational);
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);
- res_i = read_conjunct(s, v, isl_map_copy(map));
+ res_i = read_conjunct(s, v, isl_map_copy(map), rational);
if (negate)
res = isl_map_subtract(res, res_i);
else
}
static struct isl_map *read_disjuncts(struct isl_stream *s,
- struct vars *v, __isl_take isl_map *map)
+ struct vars *v, __isl_take isl_map *map, int rational)
{
isl_map *res;
return isl_map_universe(dim);
}
- res = read_conjuncts(s, v, isl_map_copy(map));
+ res = read_conjuncts(s, v, isl_map_copy(map), rational);
while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
isl_map *res_i;
- res_i = read_conjuncts(s, v, isl_map_copy(map));
+ res_i = read_conjuncts(s, v, isl_map_copy(map), rational);
res = isl_map_union(res, res_i);
}
}
static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
- __isl_take isl_map *map, struct vars *v)
+ __isl_take isl_map *map, struct vars *v, int rational)
{
struct isl_token *tok;
if (tok->type == ':' ||
(tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
isl_token_free(tok);
- map = read_disjuncts(s, v, map);
+ map = read_disjuncts(s, v, map, rational);
} else
isl_stream_push_token(s, tok);
struct isl_set *set;
pwqp = read_term(s, map, v);
- map = read_optional_disjuncts(s, map, v);
+ map = read_optional_disjuncts(s, map, v, 0);
set = isl_map_range(map);
pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
if (isl_stream_eat(s, ')'))
goto error;
- set = read_optional_disjuncts(s, set, v);
+ set = read_optional_disjuncts(s, set, v, 0);
pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
vars_drop(v, v->n - n);
struct isl_token *tok;
struct isl_obj obj = { isl_obj_set, NULL };
int n = v->n;
+ int rational;
- if (is_rational(s))
+ rational = is_rational(s);
+ if (rational)
map = isl_map_set_rational(map);
if (isl_stream_next_token_is(s, ':')) {
obj.type = isl_obj_set;
- obj.v = read_optional_disjuncts(s, map, v);
+ obj.v = read_optional_disjuncts(s, map, v, rational);
return obj;
}
if (!next_is_tuple(s))
return obj_read_poly_or_fold(s, map, v, n);
- map = read_tuple(s, map, isl_dim_in, v);
+ map = read_map_tuple(s, map, isl_dim_in, v, rational, 0);
if (!map)
goto error;
tok = isl_stream_next_token(s);
isl_set *set = isl_map_domain(map);
return obj_read_poly_or_fold(s, set, v, n);
}
- map = read_tuple(s, map, isl_dim_out, v);
+ map = read_map_tuple(s, map, isl_dim_out, v, rational, 0);
if (!map)
goto error;
} else {
- map = isl_map_reverse(map);
+ map = isl_map_domain(map);
isl_stream_push_token(s, tok);
}
- map = read_optional_disjuncts(s, map, v);
+ map = read_optional_disjuncts(s, map, v, rational);
vars_drop(v, v->n - n);
map = isl_map_universe(isl_space_params_alloc(s->ctx, 0));
if (tok->type == '[') {
isl_stream_push_token(s, tok);
- map = read_tuple(s, map, isl_dim_param, v);
+ map = read_map_tuple(s, map, isl_dim_param, v, 0, 0);
if (!map)
goto error;
tok = isl_stream_next_token(s);
isl_token_free(tok);
if (isl_stream_eat(s, '='))
goto error;
- map = read_tuple(s, map, isl_dim_param, v);
+ map = read_map_tuple(s, map, isl_dim_param, v, 0, 1);
if (!map)
goto error;
} else if (tok->type == '}') {
return pwqp;
}
-/* Read an affine expression from "s" with domain (space) "dom".
- * We call accept_affine to parse a possibly piecewise affine expression
- * and then check that the result is a single affine expression on
- * a universe domain.
- */
-static __isl_give isl_aff *read_aff_with_dom(struct isl_stream *s,
- __isl_take isl_set *dom, struct vars *v)
-{
- isl_aff *aff = NULL;
- isl_pw_aff *pwaff = NULL;
-
- if (!isl_set_plain_is_universe(dom))
- isl_die(s->ctx, isl_error_invalid,
- "expecting universe domain", goto error);
-
- if (!isl_set_is_params(dom) && isl_stream_eat(s, ISL_TOKEN_TO))
- goto error;
-
- if (isl_stream_eat(s, '['))
- goto error;
-
- pwaff = accept_affine(s, isl_set_get_space(dom), v);
-
- if (isl_stream_eat(s, ']'))
- goto error;
- if (isl_stream_eat(s, '}'))
- goto error;
-
- if (!pwaff)
- goto error;
-
- if (pwaff->n != 1)
- isl_die(s->ctx, isl_error_invalid,
- "expecting single affine expression", goto error);
- if (!isl_set_plain_is_universe(pwaff->p[0].set))
- isl_die(s->ctx, isl_error_invalid,
- "expecting universe domain", goto error);
-
- aff = isl_aff_copy(pwaff->p[0].aff);
-
- vars_free(v);
- isl_pw_aff_free(pwaff);
- isl_set_free(dom);
- return aff;
-error:
- vars_free(v);
- isl_pw_aff_free(pwaff);
- isl_set_free(dom);
- return NULL;
-}
-
/* Is the next token an identifer not in "v"?
*/
static int next_is_fresh_ident(struct isl_stream *s, struct vars *v)
tok = isl_stream_next_token(s);
if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
isl_stream_push_token(s, tok);
- return read_tuple(s, dom, isl_dim_set, v);
+ return read_map_tuple(s, dom, isl_dim_set, v, 1, 0);
}
if (!tok || tok->type != '[') {
isl_stream_error(s, tok, "expecting '['");
}
if (next_is_tuple(s) || next_is_fresh_ident(s, v)) {
isl_stream_push_token(s, tok);
- dom = read_tuple(s, dom, isl_dim_set, v);
+ dom = read_map_tuple(s, dom, isl_dim_set, v, 1, 0);
} else
isl_stream_push_token(s, tok);
error:
if (tok)
isl_stream_push_token(s, tok);
- vars_free(v);
isl_set_free(dom);
return NULL;
}
/* Read an affine expression from "s".
- * We first read the domain of the affine expression, which may be
- * a parameter space or a set, and then call read_aff_with_dom.
*/
__isl_give isl_aff *isl_stream_read_aff(struct isl_stream *s)
{
- struct vars *v;
- isl_set *dom = NULL;
+ isl_aff *aff;
+ isl_multi_aff *ma;
- v = vars_new(s->ctx);
- if (!v)
+ ma = isl_stream_read_multi_aff(s);
+ if (!ma)
return NULL;
+ if (isl_multi_aff_dim(ma, isl_dim_out) != 1)
+ isl_die(s->ctx, isl_error_invalid,
+ "expecting single affine expression",
+ goto error);
- dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
- if (next_is_tuple(s)) {
- dom = read_tuple(s, dom, isl_dim_param, v);
- if (isl_stream_eat(s, ISL_TOKEN_TO))
- goto error;
- }
- if (isl_stream_eat(s, '{'))
- goto error;
-
- dom = read_aff_domain(s, dom, v);
- return read_aff_with_dom(s, dom, v);
+ aff = isl_multi_aff_get_aff(ma, 0);
+ isl_multi_aff_free(ma);
+ return aff;
error:
- vars_free(v);
- isl_set_free(dom);
+ isl_multi_aff_free(ma);
return NULL;
}
if (isl_stream_eat(s, ']'))
goto error;
- dom = read_optional_disjuncts(s, dom, v);
+ dom = read_optional_disjuncts(s, dom, v, 0);
pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
return pwaff;
dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
if (next_is_tuple(s)) {
- dom = read_tuple(s, dom, isl_dim_param, v);
+ dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
if (isl_stream_eat(s, ISL_TOKEN_TO))
goto error;
}
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.
+ */
+static __isl_give isl_aff *aff_from_pw_aff(__isl_take isl_pw_aff *pa)
+{
+ isl_aff *aff;
+
+ if (!pa)
+ return NULL;
+ if (pa->n != 1)
+ isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
+ "expecting single affine expression",
+ goto error);
+ if (!isl_set_plain_is_universe(pa->p[0].set))
+ isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
+ "expecting universe domain",
+ goto error);
+
+ aff = isl_aff_copy(pa->p[0].aff);
+ isl_pw_aff_free(pa);
+ return aff;
+error:
+ isl_pw_aff_free(pa);
+ return NULL;
+}
+
/* Read a multi-affine expression from "s".
- * We call isl_stream_read_pw_multi_aff to parse a possibly piecewise
- * multi-affine expression and then check that the result is
- * a single multi-affine expression on a universe domain.
+ * If the multi-affine expression has a domain, then then tuple
+ * representing this domain cannot involve any affine expressions.
+ * The tuple representing the actual expressions needs to consist
+ * of only affine expressions. Moreover, these expressions can
+ * only depend on parameters and input dimensions and not on other
+ * output dimensions.
*/
__isl_give isl_multi_aff *isl_stream_read_multi_aff(struct isl_stream *s)
{
- isl_pw_multi_aff *pma;
- isl_multi_aff *maff;
+ struct vars *v;
+ isl_set *dom = NULL;
+ isl_multi_pw_aff *tuple = NULL;
+ int dim, i, n;
+ isl_space *space, *dom_space;
+ isl_multi_aff *ma = NULL;
- pma = isl_stream_read_pw_multi_aff(s);
- if (!pma)
+ v = vars_new(s->ctx);
+ if (!v)
return NULL;
- if (pma->n != 1)
+
+ 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, 0);
+ if (isl_stream_eat(s, ISL_TOKEN_TO))
+ goto error;
+ }
+ if (!isl_set_plain_is_universe(dom))
isl_die(s->ctx, isl_error_invalid,
- "expecting single list of affine expressions",
- return isl_pw_multi_aff_free(pma));
- if (!isl_set_plain_is_universe(pma->p[0].set))
- isl_die(s->ctx, isl_error_invalid, "expecting universe domain",
- return isl_pw_multi_aff_free(pma));
- maff = isl_multi_aff_copy(pma->p[0].maff);
- isl_pw_multi_aff_free(pma);
- return maff;
+ "expecting universe parameter domain", goto error);
+ if (isl_stream_eat(s, '{'))
+ goto error;
+
+ tuple = read_tuple(s, v, 0, 0);
+ if (!tuple)
+ goto error;
+ if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
+ isl_set *set;
+ isl_space *space;
+ int has_expr;
+
+ has_expr = tuple_has_expr(tuple);
+ if (has_expr < 0)
+ goto error;
+ if (has_expr)
+ isl_die(s->ctx, isl_error_invalid,
+ "expecting universe domain", goto error);
+ space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
+ set = isl_set_universe(space);
+ dom = isl_set_intersect_params(set, dom);
+ isl_multi_pw_aff_free(tuple);
+ tuple = read_tuple(s, v, 0, 0);
+ if (!tuple)
+ goto error;
+ }
+
+ if (isl_stream_eat(s, '}'))
+ goto error;
+
+ n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
+ dim = isl_set_dim(dom, isl_dim_all);
+ dom_space = isl_set_get_space(dom);
+ space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
+ space = isl_space_align_params(space, isl_space_copy(dom_space));
+ if (!isl_space_is_params(dom_space))
+ space = isl_space_map_from_domain_and_range(
+ isl_space_copy(dom_space), space);
+ isl_space_free(dom_space);
+ ma = isl_multi_aff_alloc(space);
+
+ for (i = 0; i < n; ++i) {
+ isl_pw_aff *pa;
+ isl_aff *aff;
+ pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
+ aff = aff_from_pw_aff(pa);
+ if (!aff)
+ goto error;
+ if (isl_aff_involves_dims(aff, isl_dim_in, dim, i + 1)) {
+ isl_aff_free(aff);
+ isl_die(s->ctx, isl_error_invalid,
+ "not an affine expression", goto error);
+ }
+ aff = isl_aff_drop_dims(aff, isl_dim_in, dim, n);
+ space = isl_multi_aff_get_domain_space(ma);
+ aff = isl_aff_reset_domain_space(aff, space);
+ ma = isl_multi_aff_set_aff(ma, i, aff);
+ }
+
+ isl_multi_pw_aff_free(tuple);
+ vars_free(v);
+ isl_set_free(dom);
+ return ma;
+error:
+ isl_multi_pw_aff_free(tuple);
+ vars_free(v);
+ isl_set_free(dom);
+ isl_multi_aff_free(ma);
+ return NULL;
}
__isl_give isl_multi_aff *isl_multi_aff_read_from_str(isl_ctx *ctx,