From dc16f7a6d8289f40c31d051343b76c2a7338e266 Mon Sep 17 00:00:00 2001 From: Sven Verdoolaege Date: Wed, 18 Sep 2013 19:54:59 +0200 Subject: [PATCH] add isl_multi_val_read_from_str Signed-off-by: Sven Verdoolaege --- doc/user.pod | 2 ++ include/isl/val.h | 2 ++ isl_input.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ isl_test.c | 19 +++++++++++++ 4 files changed, 103 insertions(+) diff --git a/doc/user.pod b/doc/user.pod index 2a70e11c..4c4aa8a3 100644 --- a/doc/user.pod +++ b/doc/user.pod @@ -3000,6 +3000,8 @@ Objects can be read from input using the following functions. #include __isl_give isl_val *isl_val_read_from_str(isl_ctx *ctx, const char *str); + __isl_give isl_multi_val *isl_multi_val_read_from_str( + isl_ctx *ctx, const char *str); #include __isl_give isl_basic_set *isl_basic_set_read_from_file( diff --git a/include/isl/val.h b/include/isl/val.h index 238f0c28..89da1e90 100644 --- a/include/isl/val.h +++ b/include/isl/val.h @@ -102,6 +102,8 @@ __isl_give isl_multi_val *isl_multi_val_add_val(__isl_take isl_multi_val *mv, __isl_give isl_multi_val *isl_multi_val_mod_val(__isl_take isl_multi_val *mv, __isl_take isl_val *v); +__isl_give isl_multi_val *isl_multi_val_read_from_str(isl_ctx *ctx, + const char *str); __isl_give isl_printer *isl_printer_print_multi_val(__isl_take isl_printer *p, __isl_keep isl_multi_val *mv); void isl_multi_val_dump(__isl_keep isl_multi_val *mv); diff --git a/isl_input.c b/isl_input.c index dbf8f501..771d437a 100644 --- a/isl_input.c +++ b/isl_input.c @@ -3173,6 +3173,86 @@ error: return NULL; } +/* This function is called for each element in a tuple inside + * isl_stream_read_multi_val. + * Read an isl_val from "s" and add it to *list. + */ +static __isl_give isl_space *read_val_el(struct isl_stream *s, + struct vars *v, __isl_take isl_space *space, int rational, void *user) +{ + isl_val_list **list = (isl_val_list **) user; + isl_val *val; + + val = isl_stream_read_val(s); + *list = isl_val_list_add(*list, val); + if (!*list) + return isl_space_free(space); + + return space; +} + +/* Read an isl_multi_val from "s". + * + * We first read a tuple space, collecting the element values in a list. + * Then we create an isl_multi_val from the space and the isl_val_list. + */ +__isl_give isl_multi_val *isl_stream_read_multi_val(struct isl_stream *s) +{ + struct vars *v; + isl_set *dom = NULL; + isl_space *space; + isl_multi_val *mv = NULL; + isl_val_list *list; + + v = vars_new(s->ctx); + if (!v) + return NULL; + + 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 universe parameter domain", goto error); + if (isl_stream_eat(s, '{')) + goto error; + + space = isl_set_get_space(dom); + + list = isl_val_list_alloc(s->ctx, 0); + space = read_tuple_space(s, v, space, 1, 0, &read_val_el, &list); + mv = isl_multi_val_from_val_list(space, list); + + if (isl_stream_eat(s, '}')) + goto error; + + vars_free(v); + isl_set_free(dom); + return mv; +error: + vars_free(v); + isl_set_free(dom); + isl_multi_val_free(mv); + return NULL; +} + +/* Read an isl_multi_val from "str". + */ +__isl_give isl_multi_val *isl_multi_val_read_from_str(isl_ctx *ctx, + const char *str) +{ + isl_multi_val *mv; + struct isl_stream *s = isl_stream_new_str(ctx, str); + if (!s) + return NULL; + mv = isl_stream_read_multi_val(s); + isl_stream_free(s); + return mv; +} + /* Read a multi-affine expression from "s". * If the multi-affine expression has a domain, then the tuple * representing this domain cannot involve any affine expressions. diff --git a/isl_test.c b/isl_test.c index ecbd0022..0a3e079f 100644 --- a/isl_test.c +++ b/isl_test.c @@ -104,11 +104,30 @@ static void test_parse_pwaff(isl_ctx *ctx, const char *str) isl_pw_aff_free(pwaff); } +/* Check that we can read an isl_multi_val from "str" without errors. + */ +static int test_parse_multi_val(isl_ctx *ctx, const char *str) +{ + isl_multi_val *mv; + + mv = isl_multi_val_read_from_str(ctx, str); + isl_multi_val_free(mv); + + return mv ? 0 : -1; +} + int test_parse(struct isl_ctx *ctx) { isl_map *map, *map2; const char *str, *str2; + if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0) + return -1; + if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0) + return -1; + if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0) + return -1; + str = "{ [i] -> [-i] }"; map = isl_map_read_from_str(ctx, str); assert(map); -- 2.11.4.GIT