#include <strings.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_polynomial_private.h"
struct variable {
char *name;
free(v);
}
+static void vars_drop(struct vars *v, int n)
+{
+ struct variable *var;
+
+ if (!v)
+ return;
+
+ v->n -= n;
+
+ var = v->v;
+ while (--n >= 0) {
+ struct variable *next = var->next;
+ free(var->name);
+ free(var);
+ var = next;
+ }
+ v->v = var;
+}
+
static struct variable *variable_new(struct vars *v, const char *name, int len,
int pos)
{
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;
+ char *prime;
- while ((tok = isl_stream_next_token(s)) != NULL) {
- int p;
- int n = v->n;
-
- 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;
}
{
struct isl_token *tok = NULL;
struct isl_vec *aff;
+ int sign = 1;
aff = isl_vec_alloc(v->ctx, 1 + v->n);
isl_seq_clr(aff->el, aff->size);
goto error;
if (pos >= n) {
isl_stream_error(s, tok, "unknown identifier");
+ isl_token_free(tok);
goto error;
}
- isl_int_add_ui(aff->el[1 + pos], aff->el[1 + pos], 1);
+ if (sign > 0)
+ isl_int_add_ui(aff->el[1 + pos],
+ aff->el[1 + pos], 1);
+ else
+ isl_int_sub_ui(aff->el[1 + pos],
+ aff->el[1 + pos], 1);
+ sign = 1;
} else if (tok->type == ISL_TOKEN_VALUE) {
struct isl_token *tok2;
int n = v->n;
int pos = -1;
+ if (isl_stream_eat_if_available(s, '*') &&
+ !isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
+ isl_stream_error(s, NULL, "missing identifier");
+ goto error;
+ }
tok2 = isl_stream_next_token(s);
if (tok2 && tok2->type == ISL_TOKEN_IDENT) {
pos = vars_pos(v, tok2->u.s, -1);
if (pos >= n) {
isl_stream_error(s, tok2,
"unknown identifier");
+ isl_token_free(tok);
isl_token_free(tok2);
goto error;
}
isl_token_free(tok2);
} else if (tok2)
isl_stream_push_token(s, tok2);
+ if (sign < 0)
+ isl_int_neg(tok->u.v, tok->u.v);
isl_int_add(aff->el[1 + pos],
aff->el[1 + pos], tok->u.v);
+ sign = 1;
+ } else if (tok->type == '-') {
+ sign = -sign;
} else if (tok->type == '+') {
/* nothing */
} else {
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)
+{
+ struct isl_vec *vec;
+ struct isl_mat *mat;
+ struct isl_token *tok = NULL;
+
+ vec = accept_affine(s, v);
+ mat = isl_mat_from_row_vec(vec);
+ if (!mat)
+ return NULL;
+
+ for (;;) {
+ tok = isl_stream_next_token(s);
+ if (!tok) {
+ isl_stream_error(s, NULL, "unexpected EOF");
+ goto error;
+ }
+ if (tok->type != ',') {
+ isl_stream_push_token(s, tok);
+ break;
+ }
+ isl_token_free(tok);
+
+ vec = accept_affine(s, v);
+ mat = isl_mat_vec_concat(mat, vec);
+ if (!mat)
+ return NULL;
+ }
+
+ return mat;
+error:
+ isl_mat_free(mat);
+ return NULL;
+}
+
static struct isl_basic_map *add_div_definition(struct isl_stream *s,
struct vars *v, struct isl_basic_map *bmap, int k)
{
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;
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 ']'");
}
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;
}
static struct isl_basic_map *add_constraints(struct isl_stream *s,
- struct vars **v, struct isl_basic_map *bmap);
+ struct vars *v, struct isl_basic_map *bmap);
static struct isl_basic_map *add_exists(struct isl_stream *s,
- struct vars **v, struct isl_basic_map *bmap)
+ struct vars *v, struct isl_basic_map *bmap)
{
struct isl_token *tok;
- int n = (*v)->n;
+ int n = v->n;
int extra;
int seen_paren = 0;
int i;
} else
isl_stream_push_token(s, tok);
- bmap = read_defined_var_list(s, *v, bmap);
+ bmap = read_defined_var_list(s, v, bmap);
if (!bmap)
goto error;
return NULL;
}
+static __isl_give isl_basic_map *construct_constraint(
+ __isl_take isl_basic_map *bmap, enum isl_token_type type,
+ isl_int *left, isl_int *right)
+{
+ int k;
+ unsigned len;
+ struct isl_ctx *ctx;
+
+ if (!bmap)
+ return NULL;
+ len = 1 + isl_basic_map_total_dim(bmap);
+ ctx = bmap->ctx;
+
+ k = isl_basic_map_alloc_inequality(bmap);
+ if (k < 0)
+ goto error;
+ if (type == ISL_TOKEN_LE)
+ isl_seq_combine(bmap->ineq[k], ctx->negone, left,
+ ctx->one, right,
+ len);
+ else if (type == ISL_TOKEN_GE)
+ isl_seq_combine(bmap->ineq[k], ctx->one, left,
+ ctx->negone, right,
+ len);
+ else if (type == ISL_TOKEN_LT) {
+ isl_seq_combine(bmap->ineq[k], ctx->negone, left,
+ ctx->one, right,
+ len);
+ isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
+ } else if (type == ISL_TOKEN_GT) {
+ isl_seq_combine(bmap->ineq[k], ctx->one, left,
+ ctx->negone, right,
+ len);
+ isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
+ } else {
+ isl_seq_combine(bmap->ineq[k], ctx->one, left,
+ ctx->negone, right,
+ len);
+ isl_basic_map_inequality_to_equality(bmap, k);
+ }
+
+ return bmap;
+error:
+ isl_basic_map_free(bmap);
+ 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)
+ struct vars *v, struct isl_basic_map *bmap)
{
+ int i, j;
unsigned total = isl_basic_map_total_dim(bmap);
- int k;
struct isl_token *tok = NULL;
- struct isl_vec *aff1 = NULL, *aff2 = NULL;
+ struct isl_mat *aff1 = NULL, *aff2 = NULL;
tok = isl_stream_next_token(s);
if (!tok)
tok = NULL;
bmap = isl_basic_map_cow(bmap);
- bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
- k = isl_basic_map_alloc_inequality(bmap);
- if (k < 0)
- goto error;
- aff1 = accept_affine(s, *v);
+ aff1 = accept_affine_list(s, v);
if (!aff1)
goto error;
tok = isl_stream_next_token(s);
- switch (tok->type) {
- 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(s, *v);
- if (!aff2)
- goto error;
- isl_assert(aff1->ctx, aff1->size == 1 + total, goto error);
- isl_assert(aff2->ctx, aff2->size == 1 + total, goto error);
+ isl_assert(aff1->ctx, aff1->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,
+ aff1->row[i], aff2->row[j]);
+ isl_token_free(tok);
+ isl_mat_free(aff1);
+ aff1 = aff2;
- if (tok->type == ISL_TOKEN_LE)
- isl_seq_combine(bmap->ineq[k], (*v)->ctx->negone, aff1->el,
- (*v)->ctx->one, aff2->el,
- aff1->size);
- else if (tok->type == ISL_TOKEN_GE)
- isl_seq_combine(bmap->ineq[k], (*v)->ctx->one, aff1->el,
- (*v)->ctx->negone, aff2->el,
- aff1->size);
- else {
- isl_seq_combine(bmap->ineq[k], (*v)->ctx->one, aff1->el,
- (*v)->ctx->negone, aff2->el,
- aff1->size);
- isl_basic_map_inequality_to_equality(bmap, k);
+ tok = isl_stream_next_token(s);
+ if (!is_comparator(tok)) {
+ if (tok)
+ isl_stream_push_token(s, tok);
+ break;
+ }
}
- isl_token_free(tok);
- isl_vec_free(aff1);
- isl_vec_free(aff2);
+ isl_mat_free(aff1);
return bmap;
error:
if (tok)
isl_token_free(tok);
- isl_vec_free(aff1);
- isl_vec_free(aff2);
+ isl_mat_free(aff1);
+ isl_mat_free(aff2);
isl_basic_map_free(bmap);
return NULL;
}
static struct isl_basic_map *add_constraints(struct isl_stream *s,
- struct vars **v, struct isl_basic_map *bmap)
+ struct vars *v, struct isl_basic_map *bmap)
{
struct isl_token *tok;
}
static struct isl_basic_map *read_disjunct(struct isl_stream *s,
- struct vars **v, __isl_take isl_dim *dim)
+ 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,
- struct vars **v, __isl_take isl_dim *dim)
+ struct vars *v, __isl_take isl_dim *dim)
{
struct isl_token *tok;
struct isl_map *map;
map = isl_map_empty(isl_dim_copy(dim));
for (;;) {
struct isl_basic_map *bmap;
+ int n = v->n;
bmap = read_disjunct(s, v, isl_dim_copy(dim));
map = isl_map_union(map, isl_map_from_basic_map(bmap));
+ vars_drop(v, v->n - n);
+
tok = isl_stream_next_token(s);
if (!tok || tok->type != ISL_TOKEN_OR)
break;
return NULL;
}
+static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
+{
+ if (pos < isl_basic_map_dim(bmap, isl_dim_out))
+ return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
+ isl_basic_map_dim(bmap, isl_dim_in) + pos;
+ pos -= isl_basic_map_dim(bmap, isl_dim_out);
+
+ if (pos < isl_basic_map_dim(bmap, isl_dim_in))
+ return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
+ pos -= isl_basic_map_dim(bmap, isl_dim_in);
+
+ if (pos < isl_basic_map_dim(bmap, isl_dim_div))
+ return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
+ isl_basic_map_dim(bmap, isl_dim_in) +
+ isl_basic_map_dim(bmap, isl_dim_out) + pos;
+ pos -= isl_basic_map_dim(bmap, isl_dim_div);
+
+ if (pos < isl_basic_map_dim(bmap, isl_dim_param))
+ return 1 + pos;
+
+ return 0;
+}
+
static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
struct isl_stream *s, __isl_take isl_basic_map *bmap)
{
if (k < 0)
goto error;
- for (j = 0; j < dim; ++j) {
+ for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
+ int pos;
tok = isl_stream_next_token(s);
if (!tok || tok->type != ISL_TOKEN_VALUE) {
isl_stream_error(s, tok, "expecting coefficient");
isl_stream_push_token(s, tok);
goto error;
}
- isl_int_set(c[1 + nparam + j], tok->u.v);
- isl_token_free(tok);
- }
- for (j = 0; j < nparam; ++j) {
- tok = isl_stream_next_token(s);
- if (!tok || tok->type != ISL_TOKEN_VALUE) {
- isl_stream_error(s, tok, "expecting coefficient");
- if (tok)
- isl_stream_push_token(s, tok);
+ if (tok->on_new_line) {
+ isl_stream_error(s, tok,
+ "coefficient should not appear on new line");
+ isl_stream_push_token(s, tok);
goto error;
}
- isl_int_set(c[1 + j], tok->u.v);
+ pos = polylib_pos_to_isl_pos(bmap, j);
+ isl_int_set(c[pos], tok->u.v);
isl_token_free(tok);
}
- tok = isl_stream_next_token(s);
- if (!tok || tok->type != ISL_TOKEN_VALUE) {
- isl_stream_error(s, tok, "expecting coefficient");
- if (tok)
- isl_stream_push_token(s, tok);
- goto error;
- }
- isl_int_set(c[0], tok->u.v);
- isl_token_free(tok);
return bmap;
error:
struct isl_token *tok2;
int n_row, n_col;
int on_new_line;
- unsigned dim;
+ unsigned in = 0, out, local = 0;
struct isl_basic_map *bmap = NULL;
if (nparam < 0)
isl_assert(s->ctx, !on_new_line, return NULL);
isl_assert(s->ctx, n_row >= 0, return NULL);
isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
- dim = n_col - 2 - nparam;
- bmap = isl_basic_map_alloc(s->ctx, nparam, 0, dim, 0, n_row, n_row);
+ tok = isl_stream_next_token_on_same_line(s);
+ if (tok) {
+ if (tok->type != ISL_TOKEN_VALUE) {
+ isl_stream_error(s, tok,
+ "expecting number of output dimensions");
+ isl_stream_push_token(s, tok);
+ goto error;
+ }
+ out = isl_int_get_si(tok->u.v);
+ isl_token_free(tok);
+
+ tok = isl_stream_next_token_on_same_line(s);
+ if (!tok || tok->type != ISL_TOKEN_VALUE) {
+ isl_stream_error(s, tok,
+ "expecting number of input dimensions");
+ if (tok)
+ isl_stream_push_token(s, tok);
+ goto error;
+ }
+ in = isl_int_get_si(tok->u.v);
+ isl_token_free(tok);
+
+ tok = isl_stream_next_token_on_same_line(s);
+ if (!tok || tok->type != ISL_TOKEN_VALUE) {
+ isl_stream_error(s, tok,
+ "expecting number of existentials");
+ if (tok)
+ isl_stream_push_token(s, tok);
+ goto error;
+ }
+ local = isl_int_get_si(tok->u.v);
+ isl_token_free(tok);
+
+ tok = isl_stream_next_token_on_same_line(s);
+ if (!tok || tok->type != ISL_TOKEN_VALUE) {
+ isl_stream_error(s, tok,
+ "expecting number of parameters");
+ if (tok)
+ isl_stream_push_token(s, tok);
+ goto error;
+ }
+ nparam = isl_int_get_si(tok->u.v);
+ isl_token_free(tok);
+ if (n_col != 1 + out + in + local + nparam + 1) {
+ isl_stream_error(s, NULL,
+ "dimensions don't match");
+ goto error;
+ }
+ } else
+ out = n_col - 2 - nparam;
+ bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
if (!bmap)
return NULL;
+ for (i = 0; i < local; ++i) {
+ int k = isl_basic_map_alloc_div(bmap);
+ if (k < 0)
+ goto error;
+ }
+
for (i = 0; i < n_row; ++i)
bmap = basic_map_read_polylib_constraint(s, bmap);
+ tok = isl_stream_next_token_on_same_line(s);
+ if (tok) {
+ isl_stream_error(s, tok, "unexpected extra token on line");
+ isl_stream_push_token(s, tok);
+ goto error;
+ }
+
bmap = isl_basic_map_simplify(bmap);
bmap = isl_basic_map_finalize(bmap);
return bmap;
+error:
+ isl_basic_map_free(bmap);
+ return NULL;
}
static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
isl_stream_error(s, NULL, "unexpected EOF");
return NULL;
}
- tok2 = isl_stream_next_token(s);
- if (!tok2) {
- isl_token_free(tok);
- isl_stream_error(s, NULL, "unexpected EOF");
- return NULL;
- }
- if (!tok2->on_new_line) {
+ tok2 = isl_stream_next_token_on_same_line(s);
+ if (tok2) {
isl_stream_push_token(s, tok2);
isl_stream_push_token(s, tok);
return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
}
- isl_stream_push_token(s, tok2);
n = isl_int_get_si(tok->u.v);
isl_token_free(tok);
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 int optional_power(struct isl_stream *s)
{
- int i;
- struct variable *v;
+ int pow;
+ struct isl_token *tok;
+
+ tok = isl_stream_next_token(s);
+ if (!tok)
+ return 1;
+ if (tok->type != '^') {
+ isl_stream_push_token(s, tok);
+ return 1;
+ }
+ isl_token_free(tok);
+ tok = isl_stream_next_token(s);
+ if (!tok || tok->type != ISL_TOKEN_VALUE) {
+ isl_stream_error(s, tok, "expecting exponent");
+ if (tok)
+ isl_stream_push_token(s, tok);
+ return 1;
+ }
+ pow = isl_int_get_si(tok->u.v);
+ isl_token_free(tok);
+ return pow;
+}
+
+static __isl_give isl_div *read_div(struct isl_stream *s,
+ __isl_keep isl_basic_map *bmap, 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);
- 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);
+ if ((k = isl_basic_map_alloc_div(bmap)) < 0)
+ goto error;
+ isl_seq_clr(bmap->div[k], 1 + 1 + total);
+ bmap = add_div_definition(s, v, bmap, k);
- return dim;
+ return isl_basic_map_div(bmap, k);
+error:
+ isl_basic_map_free(bmap);
+ return NULL;
}
-static struct isl_dim *dim_from_vars(struct vars *vars,
- int nparam, int n_in, int n_out)
+static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
+ __isl_keep isl_basic_map *bmap, struct vars *v);
+
+static __isl_give isl_qpolynomial *read_factor(struct isl_stream *s,
+ __isl_keep isl_basic_map *bmap, struct vars *v)
{
- struct isl_dim *dim;
+ struct isl_qpolynomial *qp;
+ struct isl_token *tok;
+
+ tok = isl_stream_next_token(s);
+ if (!tok) {
+ isl_stream_error(s, NULL, "unexpected EOF");
+ return NULL;
+ }
+ if (tok->type == '(') {
+ isl_token_free(tok);
+ qp = read_term(s, bmap, v);
+ if (!qp)
+ return NULL;
+ if (isl_stream_eat(s, ')'))
+ goto error;
+ } else if (tok->type == ISL_TOKEN_VALUE) {
+ struct isl_token *tok2;
+ tok2 = isl_stream_next_token(s);
+ if (tok2 && tok2->type == '/') {
+ isl_token_free(tok2);
+ tok2 = isl_stream_next_token(s);
+ if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
+ isl_stream_error(s, tok2, "expected denominator");
+ isl_token_free(tok);
+ isl_token_free(tok2);
+ return NULL;
+ }
+ qp = isl_qpolynomial_rat_cst(isl_basic_map_get_dim(bmap),
+ tok->u.v, tok2->u.v);
+ isl_token_free(tok2);
+ } else {
+ isl_stream_push_token(s, tok2);
+ qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
+ tok->u.v);
+ }
+ isl_token_free(tok);
+ } else if (tok->type == ISL_TOKEN_INFTY) {
+ isl_token_free(tok);
+ qp = isl_qpolynomial_infty(isl_basic_map_get_dim(bmap));
+ } else if (tok->type == ISL_TOKEN_NAN) {
+ isl_token_free(tok);
+ qp = isl_qpolynomial_nan(isl_basic_map_get_dim(bmap));
+ } else if (tok->type == ISL_TOKEN_IDENT) {
+ int n = v->n;
+ int pos = vars_pos(v, tok->u.s, -1);
+ int pow;
+ isl_token_free(tok);
+ if (pos < 0)
+ goto error;
+ if (pos >= n) {
+ isl_stream_error(s, tok, "unknown identifier");
+ return NULL;
+ }
+ pow = optional_power(s);
+ qp = isl_qpolynomial_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);
+ pow = optional_power(s);
+ qp = isl_qpolynomial_div_pow(div, pow);
+ } else if (tok->type == '-') {
+ struct isl_qpolynomial *qp2;
+
+ isl_token_free(tok);
+ qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
+ s->ctx->negone);
+ qp2 = read_factor(s, bmap, v);
+ qp = isl_qpolynomial_mul(qp, qp2);
+ } else {
+ isl_stream_error(s, tok, "unexpected isl_token");
+ isl_stream_push_token(s, tok);
+ return NULL;
+ }
+
+ if (isl_stream_eat_if_available(s, '*') ||
+ isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
+ struct isl_qpolynomial *qp2;
- 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);
+ qp2 = read_factor(s, bmap, v);
+ qp = isl_qpolynomial_mul(qp, qp2);
+ }
- return dim;
+ return qp;
+error:
+ isl_qpolynomial_free(qp);
+ return NULL;
}
-static struct isl_map *map_read(struct isl_stream *s, int nparam)
+static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
+ __isl_keep isl_basic_map *bmap, struct vars *v)
+{
+ struct isl_token *tok;
+ struct isl_qpolynomial *qp;
+
+ qp = read_factor(s, bmap, v);
+
+ for (;;) {
+ tok = isl_stream_next_token(s);
+ if (!tok)
+ return qp;
+
+ if (tok->type == '+') {
+ struct isl_qpolynomial *qp2;
+
+ isl_token_free(tok);
+ qp2 = read_factor(s, bmap, v);
+ qp = isl_qpolynomial_add(qp, qp2);
+ } else if (tok->type == '-') {
+ struct isl_qpolynomial *qp2;
+
+ isl_token_free(tok);
+ qp2 = read_factor(s, bmap, v);
+ qp = isl_qpolynomial_sub(qp, qp2);
+ } else if (tok->type == ISL_TOKEN_VALUE &&
+ isl_int_is_neg(tok->u.v)) {
+ struct isl_qpolynomial *qp2;
+
+ qp2 = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
+ tok->u.v);
+ isl_token_free(tok);
+ qp = isl_qpolynomial_add(qp, qp2);
+ } else {
+ isl_stream_push_token(s, tok);
+ break;
+ }
+ }
+
+ return qp;
+}
+
+static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
+ __isl_take isl_basic_map *bmap, struct vars *v)
+{
+ struct isl_token *tok;
+ struct isl_map *map;
+
+ 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);
+
+ isl_basic_map_free(bmap);
+
+ return map;
+error:
+ isl_basic_map_free(bmap);
+ return NULL;
+}
+
+static struct isl_obj obj_read_poly(struct isl_stream *s,
+ __isl_take isl_basic_map *bmap, struct vars *v, int n)
+{
+ struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
+ struct isl_pw_qpolynomial *pwqp;
+ struct isl_qpolynomial *qp;
+ struct isl_map *map;
+ struct isl_set *set;
+
+ bmap = isl_basic_map_reverse(bmap);
+
+ qp = read_term(s, bmap, v);
+ map = read_optional_disjuncts(s, bmap, v);
+ set = isl_set_from_map(map);
+
+ pwqp = isl_pw_qpolynomial_alloc(set, qp);
+
+ vars_drop(v, v->n - n);
+
+ obj.v = pwqp;
+ return obj;
+}
+
+static struct isl_obj obj_read_body(struct isl_stream *s,
+ __isl_take isl_basic_map *bmap, struct vars *v, isl_obj_type type)
{
- struct isl_dim *dim = NULL;
struct isl_map *map = NULL;
struct isl_token *tok;
+ struct isl_obj obj = { type, NULL };
+ int n = v->n;
+
+ if (!isl_stream_next_token_is(s, '['))
+ return obj_read_poly(s, bmap, v, n);
+
+ 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) {
+ obj.type = isl_obj_map;
+ isl_token_free(tok);
+ tok = isl_stream_next_token(s);
+ if (tok && tok->type != '[') {
+ isl_stream_push_token(s, tok);
+ return obj_read_poly(s, bmap, v, n);
+ }
+ isl_stream_push_token(s, 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);
+ }
+
+ map = read_optional_disjuncts(s, bmap, v);
+
+ vars_drop(v, v->n - n);
+
+ obj.v = map;
+ return obj;
+error:
+ isl_basic_map_free(bmap);
+ obj.type = isl_obj_none;
+ return obj;
+}
+
+static struct isl_obj obj_add(struct isl_ctx *ctx,
+ struct isl_obj obj1, struct isl_obj obj2)
+{
+ isl_assert(ctx, obj1.type == obj2.type, goto error);
+ obj1.v = obj1.type->add(obj1.v, obj2.v);
+ return obj1;
+error:
+ obj1.type->free(obj1.v);
+ obj2.type->free(obj2.v);
+ obj1.type = isl_obj_none;
+ obj1.v = NULL;
+ return obj1;
+}
+
+static struct isl_obj obj_read(struct isl_stream *s, int nparam)
+{
+ struct isl_basic_map *bmap = NULL;
+ struct isl_token *tok;
struct vars *v = NULL;
- int n1;
- int n2;
+ struct isl_obj obj = { isl_obj_set, NULL };
tok = isl_stream_next_token(s);
if (!tok) {
goto error;
}
if (tok->type == ISL_TOKEN_VALUE) {
+ struct isl_map *map;
isl_stream_push_token(s, tok);
- return map_read_polylib(s, nparam);
+ map = map_read_polylib(s, nparam);
+ if (!map)
+ goto error;
+ if (isl_map_dim(map, isl_dim_in) > 0)
+ obj.type = isl_obj_map;
+ obj.v = map;
+ return obj;
}
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 '->'");
}
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)
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 (;;) {
+ struct isl_obj o;
+ tok = NULL;
+ o = obj_read_body(s, isl_basic_map_copy(bmap), v, obj.type);
+ if (o.type == isl_obj_none)
+ break;
+ if (!obj.v)
+ obj = o;
+ else {
+ obj = obj_add(s->ctx, obj, o);
+ if (obj.type == isl_obj_none)
+ break;
+ }
+ 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 {
goto error;
}
vars_free(v);
- isl_dim_free(dim);
+ isl_basic_map_free(bmap);
- return map;
+ return obj;
error:
- isl_dim_free(dim);
- isl_map_free(map);
+ isl_basic_map_free(bmap);
+ obj.type->free(obj.v);
if (v)
vars_free(v);
+ obj.v = NULL;
+ return obj;
+}
+
+struct isl_obj isl_stream_read_obj(struct isl_stream *s)
+{
+ return obj_read(s, -1);
+}
+
+__isl_give isl_map *isl_stream_read_map(struct isl_stream *s, int nparam)
+{
+ struct isl_obj obj;
+ struct isl_map *map;
+
+ obj = obj_read(s, nparam);
+ if (obj.v)
+ isl_assert(s->ctx, obj.type == isl_obj_map ||
+ obj.type == isl_obj_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;
struct isl_map *map;
struct isl_basic_map *bmap;
- map = map_read(s, nparam);
+ obj = obj_read(s, nparam);
+ map = obj.v;
if (!map)
return NULL;
struct isl_stream *s = isl_stream_new_file(ctx, input);
if (!s)
return NULL;
- map = map_read(s, nparam);
+ map = isl_stream_read_map(s, nparam);
isl_stream_free(s);
return map;
}
struct isl_stream *s = isl_stream_new_str(ctx, str);
if (!s)
return NULL;
- map = map_read(s, nparam);
+ map = isl_stream_read_map(s, nparam);
isl_stream_free(s);
return map;
}