X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=isl_stream.c;h=388c2046661e9e431408714f8a10b9808f596da1;hb=ac1713af559335dbfd09f7218d90999f1652c84d;hp=4c61ee4d2c9a1b400ca193fe5fc269be074cbea5;hpb=5958931f4fffe80ccbf82a99bb2995bca137396d;p=platform%2Fupstream%2Fisl.git diff --git a/isl_stream.c b/isl_stream.c index 4c61ee4..388c204 100644 --- a/isl_stream.c +++ b/isl_stream.c @@ -1,7 +1,7 @@ /* * Copyright 2008-2009 Katholieke Universiteit Leuven * - * Use of this software is governed by the GNU LGPLv2.1 license + * Use of this software is governed by the MIT license * * Written by Sven Verdoolaege, K.U.Leuven, Departement * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium @@ -11,7 +11,10 @@ #include #include #include -#include +#include +#include +#include +#include struct isl_keyword { char *name; @@ -64,7 +67,7 @@ enum isl_token_type isl_stream_register_keyword(struct isl_stream *s, return keyword->type; } -static struct isl_token *isl_token_new(struct isl_ctx *ctx, +struct isl_token *isl_token_new(isl_ctx *ctx, int line, int col, unsigned on_new_line) { struct isl_token *tok = isl_alloc_type(ctx, struct isl_token); @@ -78,12 +81,42 @@ static struct isl_token *isl_token_new(struct isl_ctx *ctx, return tok; } +/* Given a token of type ISL_TOKEN_VALUE, return the value it represents. + */ +__isl_give isl_val *isl_token_get_val(isl_ctx *ctx, struct isl_token *tok) +{ + if (!tok) + return NULL; + if (tok->type != ISL_TOKEN_VALUE) + isl_die(ctx, isl_error_invalid, "not a value token", + return NULL); + + return isl_val_int_from_isl_int(ctx, tok->u.v); +} + +/* Given a token of type ISL_TOKEN_STRING, return the string it represents. + */ +__isl_give char *isl_token_get_str(isl_ctx *ctx, struct isl_token *tok) +{ + if (!tok) + return NULL; + if (tok->type != ISL_TOKEN_STRING) + isl_die(ctx, isl_error_invalid, "not a string token", + return NULL); + + return strdup(tok->u.s); +} + void isl_token_free(struct isl_token *tok) { if (!tok) return; if (tok->type == ISL_TOKEN_VALUE) isl_int_clear(tok->u.v); + else if (tok->type == ISL_TOKEN_MAP) + isl_map_free(tok->u.map); + else if (tok->type == ISL_TOKEN_AFF) + isl_pw_aff_free(tok->u.pwaff); else free(tok->u.s); free(tok); @@ -105,6 +138,20 @@ void isl_stream_error(struct isl_stream *s, struct isl_token *tok, char *msg) fprintf(stderr, "got value '"); isl_int_print(stderr, tok->u.v, 0); fprintf(stderr, "'\n"); + } else if (tok->type == ISL_TOKEN_MAP) { + isl_printer *p; + fprintf(stderr, "got map '"); + p = isl_printer_to_file(s->ctx, stderr); + p = isl_printer_print_map(p, tok->u.map); + isl_printer_free(p); + fprintf(stderr, "'\n"); + } else if (tok->type == ISL_TOKEN_AFF) { + isl_printer *p; + fprintf(stderr, "got affine expression '"); + p = isl_printer_to_file(s->ctx, stderr); + p = isl_printer_print_pw_aff(p, tok->u.pwaff); + isl_printer_free(p); + fprintf(stderr, "'\n"); } else if (tok->u.s) fprintf(stderr, "got token '%s'\n", tok->u.s); else @@ -153,7 +200,10 @@ struct isl_stream* isl_stream_new_file(struct isl_ctx *ctx, FILE *file) struct isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str) { - struct isl_stream *s = isl_stream_new(ctx); + struct isl_stream *s; + if (!str) + return NULL; + s = isl_stream_new(ctx); if (!s) return NULL; s->str = str; @@ -213,10 +263,12 @@ static int isl_stream_getc(struct isl_stream *s) static int isl_stream_push_char(struct isl_stream *s, int c) { if (s->len >= s->size) { + char *buffer; s->size = (3*s->size)/2; - s->buffer = isl_realloc_array(s->ctx, s->buffer, char, s->size); - if (!s->buffer) + buffer = isl_realloc_array(s->ctx, s->buffer, char, s->size); + if (!buffer) return -1; + s->buffer = buffer; } s->buffer[s->len++] = c; return 0; @@ -248,6 +300,8 @@ static enum isl_token_type check_keywords(struct isl_stream *s) return ISL_TOKEN_INFTY; if (!strcasecmp(s->buffer, "NaN")) return ISL_TOKEN_NAN; + if (!strcasecmp(s->buffer, "min")) + return ISL_TOKEN_MIN; if (!strcasecmp(s->buffer, "max")) return ISL_TOKEN_MAX; if (!strcasecmp(s->buffer, "rat")) @@ -256,6 +310,12 @@ static enum isl_token_type check_keywords(struct isl_stream *s) return ISL_TOKEN_TRUE; if (!strcasecmp(s->buffer, "false")) return ISL_TOKEN_FALSE; + if (!strcasecmp(s->buffer, "ceild")) + return ISL_TOKEN_CEILD; + if (!strcasecmp(s->buffer, "floord")) + return ISL_TOKEN_FLOORD; + if (!strcasecmp(s->buffer, "mod")) + return ISL_TOKEN_MOD; if (!s->keywords) return ISL_TOKEN_IDENT; @@ -322,8 +382,8 @@ static struct isl_token *next_token(struct isl_stream *s, int same_line) c == '+' || c == '*' || c == '%' || + c == '?' || c == '^' || - c == '=' || c == '@' || c == '$' || c == ',' || @@ -360,6 +420,7 @@ static struct isl_token *next_token(struct isl_stream *s, int same_line) } } if (c == '-' || isdigit(c)) { + int minus = c == '-'; tok = isl_token_new(s->ctx, line, col, old_line != line); if (!tok) return NULL; @@ -374,6 +435,15 @@ static struct isl_token *next_token(struct isl_stream *s, int same_line) isl_stream_ungetc(s, c); isl_stream_push_char(s, '\0'); isl_int_read(tok->u.v, s->buffer); + if (minus && isl_int_is_zero(tok->u.v)) { + tok->col++; + tok->on_new_line = 0; + isl_stream_push_token(s, tok); + tok = isl_token_new(s->ctx, line, col, old_line != line); + if (!tok) + return NULL; + tok->type = (enum isl_token_type) '-'; + } return tok; } if (isalpha(c) || c == '_') { @@ -415,6 +485,21 @@ static struct isl_token *next_token(struct isl_stream *s, int same_line) tok->u.s = strdup(s->buffer); return tok; } + if (c == '=') { + int c; + tok = isl_token_new(s->ctx, line, col, old_line != line); + if (!tok) + return NULL; + if ((c = isl_stream_getc(s)) == '=') { + tok->u.s = strdup("=="); + tok->type = ISL_TOKEN_EQ_EQ; + return tok; + } + if (c != -1) + isl_stream_ungetc(s, c); + tok->type = (enum isl_token_type) '='; + return tok; + } if (c == ':') { int c; tok = isl_token_new(s->ctx, line, col, old_line != line); @@ -534,8 +619,16 @@ static struct isl_token *next_token(struct isl_stream *s, int same_line) tok = isl_token_new(s->ctx, line, col, old_line != line); if (!tok) return NULL; - tok->type = ISL_TOKEN_NOT; - tok->u.s = strdup("!"); + if ((c = isl_stream_getc(s)) == '=') { + tok->u.s = strdup("!="); + tok->type = ISL_TOKEN_NE; + return tok; + } else { + tok->type = ISL_TOKEN_NOT; + tok->u.s = strdup("!"); + } + if (c != -1) + isl_stream_ungetc(s, c); return tok; }