From e52702261eb4449d172cd50419d4921e39e5e33e Mon Sep 17 00:00:00 2001 From: Sven Verdoolaege Date: Mon, 22 Dec 2008 18:36:22 +0100 Subject: [PATCH] clast: allow clast_term to represent multiple of any clast_expr Before, a clast_term could only be used to represent a constant or a multiple of a (const char *) name. Now it can be used to represent a multiple of any clast_expr, include the new clast_name. This functionality is not currently used, but will be useful for the isl backend. Signed-off-by: Sven Verdoolaege --- include/cloog/clast.h | 18 +++++++++++++++--- source/clast.c | 49 ++++++++++++++++++++++++++++++++++++++++--------- source/pprint.c | 37 ++++++++++++++++++++++++++----------- 3 files changed, 81 insertions(+), 23 deletions(-) diff --git a/include/cloog/clast.h b/include/cloog/clast.h index 48fa4b9..761a91b 100644 --- a/include/cloog/clast.h +++ b/include/cloog/clast.h @@ -6,13 +6,23 @@ extern "C" #endif struct clast_expr { - enum { expr_term, expr_bin, expr_red } type; + enum { expr_name, expr_term, expr_bin, expr_red } type; }; +struct clast_name { + struct clast_expr expr; + const char * name; +}; + +/* Represents the term + * val * var (if var != NULL) + * or + * val (if var == NULL) + */ struct clast_term { struct clast_expr expr; cloog_int_t val; - const char * var; + struct clast_expr *var; }; enum clast_red_type { clast_red_sum, clast_red_min, clast_red_max }; @@ -100,7 +110,8 @@ struct clast_stmt *cloog_clast_create(CloogProgram *program, CloogOptions *options); void cloog_clast_free(struct clast_stmt *s); -struct clast_term *new_clast_term(cloog_int_t c, const char *v); +struct clast_name *new_clast_name(const char *name); +struct clast_term *new_clast_term(cloog_int_t c, struct clast_expr *v); struct clast_binary *new_clast_binary(enum clast_bin_type t, struct clast_expr *lhs, cloog_int_t rhs); struct clast_reduction *new_clast_reduction(enum clast_red_type t, int n); @@ -114,6 +125,7 @@ struct clast_for *new_clast_for(const char *it, struct clast_expr *LB, struct clast_expr *UB, cloog_int_t stride); struct clast_guard *new_clast_guard(int n); +void free_clast_name(struct clast_name *t); void free_clast_term(struct clast_term *t); void free_clast_binary(struct clast_binary *b); void free_clast_reduction(struct clast_reduction *r); diff --git a/source/clast.c b/source/clast.c index 390f8f3..51bc536 100644 --- a/source/clast.c +++ b/source/clast.c @@ -60,7 +60,15 @@ static void insert_loop(CloogLoop * loop, int level, int scalar, struct clast_stmt ***next, CloogInfos *infos); -struct clast_term *new_clast_term(cloog_int_t c, const char *v) +struct clast_name *new_clast_name(const char *name) +{ + struct clast_name *n = malloc(sizeof(struct clast_name)); + n->expr.type = expr_name; + n->name = name; + return n; +} + +struct clast_term *new_clast_term(cloog_int_t c, struct clast_expr *v) { struct clast_term *t = malloc(sizeof(struct clast_term)); t->expr.type = expr_term; @@ -246,9 +254,15 @@ struct clast_guard *new_clast_guard(int n) return g; } +void free_clast_name(struct clast_name *n) +{ + free(n); +} + void free_clast_term(struct clast_term *t) { cloog_int_clear(t->val); + free_clast_expr(t->var); free(t); } @@ -272,6 +286,9 @@ void free_clast_expr(struct clast_expr *e) if (!e) return; switch (e->type) { + case expr_name: + free_clast_name((struct clast_name*) e); + break; case expr_term: free_clast_term((struct clast_term*) e); break; @@ -303,6 +320,11 @@ void cloog_clast_free(struct clast_stmt *s) } } +static int clast_name_cmp(struct clast_name *n1, struct clast_name *n2) +{ + return n1->name == n2->name ? 0 : strcmp(n1->name, n2->name); +} + static int clast_term_cmp(struct clast_term *t1, struct clast_term *t2) { int c; @@ -310,7 +332,8 @@ static int clast_term_cmp(struct clast_term *t1, struct clast_term *t2) return -1; if (t1->var && !t2->var) return 1; - if (t1->var != t2->var && (c = strcmp(t1->var, t2->var))) + c = clast_expr_cmp(t1->var, t2->var); + if (c) return c; return cloog_int_cmp(t1->val, t2->val); } @@ -355,6 +378,9 @@ static int clast_expr_cmp(struct clast_expr *e1, struct clast_expr *e2) if (e1->type != e2->type) return e1->type - e2->type; switch (e1->type) { + case expr_name: + return clast_name_cmp((struct clast_name*) e1, + (struct clast_name*) e2); case expr_term: return clast_term_cmp((struct clast_term*) e1, (struct clast_term*) e2); @@ -536,8 +562,8 @@ static struct clast_stmt * clast_equal_cpp(int level, CloogInfos *infos) cloog_constraint_release(equal_constraint); } else { cloog_int_set_si(one, 1); - e = &new_clast_term(one, - cloog_names_name_at_level(infos->names, i+1))->expr; + e = &new_clast_term(one, &new_clast_name( + cloog_names_name_at_level(infos->names, i+1))->expr)->expr; } *next = &new_clast_assignment(NULL, e)->stmt; next = &(*next)->next; @@ -604,7 +630,8 @@ struct clast_expr *clast_bound_from_constraint(CloogConstraint constraint, else cloog_int_set(temp,line[i]); - r->elts[nb_elts++] = &new_clast_term(temp, name)->expr; + r->elts[nb_elts++] = &new_clast_term(temp, + &new_clast_name(name)->expr)->expr; } /* Next, the parameters. */ @@ -617,7 +644,8 @@ struct clast_expr *clast_bound_from_constraint(CloogConstraint constraint, else cloog_int_set(temp,line[i]); - r->elts[nb_elts++] = &new_clast_term(temp, name)->expr; + r->elts[nb_elts++] = &new_clast_term(temp, + &new_clast_name(name)->expr)->expr; } if (sign == -1) { @@ -829,7 +857,8 @@ static void insert_guard(CloogConstraintSet *constraints, int level, else name = infos->names->parameters[i-(nb_iter+1)] ; - g->eq[nb_and].LHS = &(t = new_clast_term(one, name))->expr; + g->eq[nb_and].LHS = &(t = new_clast_term(one, + &new_clast_name(name)->expr))->expr; if (!level || cloog_constraint_is_equality(j)) { /* put the "denominator" in the LHS */ cloog_constraint_coefficient_get(j, i-1, &t->val); @@ -1106,7 +1135,8 @@ static void insert_modulo_guard(CloogConstraint upper, name = cloog_names_name_at_level(infos->names, i); - r->elts[nb_elts++] = &new_clast_term(line[i], name)->expr; + r->elts[nb_elts++] = &new_clast_term(line[i], + &new_clast_name(name)->expr)->expr; } /* ...the parameters... */ @@ -1115,7 +1145,8 @@ static void insert_modulo_guard(CloogConstraint upper, continue; name = infos->names->parameters[i-nb_iter-1] ; - r->elts[nb_elts++] = &new_clast_term(line[i], name)->expr; + r->elts[nb_elts++] = &new_clast_term(line[i], + &new_clast_name(name)->expr)->expr; } /* ...the constant. */ diff --git a/source/pprint.c b/source/pprint.c index 48b342d..075b20a 100644 --- a/source/pprint.c +++ b/source/pprint.c @@ -56,28 +56,42 @@ +void pprint_expr(struct cloogoptions *i, FILE *dst, struct clast_expr *e); + +void pprint_name(FILE *dst, struct clast_name *n) +{ + fprintf(dst, "%s", n->name); +} + /** * This function returns a string containing the printing of a value (possibly * an iterator or a parameter with its coefficient or a constant). * - val is the coefficient or constant value, * - name is a string containing the name of the iterator or of the parameter, */ -void pprint_term(FILE *dst, struct clast_term *t) +void pprint_term(struct cloogoptions *i, FILE *dst, struct clast_term *t) { if (t->var) { + int group = t->var->type == expr_red && + ((struct clast_reduction*) t->var)->n > 1; if (cloog_int_is_one(t->val)) - fprintf(dst, "%s", t->var); + ; else if (cloog_int_is_neg_one(t->val)) - fprintf(dst, "-%s", t->var); + fprintf(dst, "-"); else { cloog_int_print(dst, t->val); - fprintf(dst, "*%s", t->var); + fprintf(dst, "*"); } + if (group) + fprintf(dst, "("); + pprint_expr(i, dst, t->var); + if (group) + fprintf(dst, ")"); } else cloog_int_print(dst, t->val); } -void pprint_sum(FILE *dst, struct clast_reduction *r) +void pprint_sum(struct cloogoptions *opt, FILE *dst, struct clast_reduction *r) { int i; struct clast_term *t; @@ -85,19 +99,17 @@ void pprint_sum(FILE *dst, struct clast_reduction *r) assert(r->n >= 1); assert(r->elts[0]->type == expr_term); t = (struct clast_term *) r->elts[0]; - pprint_term(dst, t); + pprint_term(opt, dst, t); for (i = 1; i < r->n; ++i) { assert(r->elts[i]->type == expr_term); t = (struct clast_term *) r->elts[i]; if (cloog_int_is_pos(t->val)) fprintf(dst, "+"); - pprint_term(dst, t); + pprint_term(opt, dst, t); } } -void pprint_expr(struct cloogoptions *i, FILE *dst, struct clast_expr *e); - void pprint_binary(struct cloogoptions *i, FILE *dst, struct clast_binary *b) { const char *s1 = NULL, *s2 = NULL, *s3 = NULL; @@ -182,7 +194,7 @@ void pprint_reduction(struct cloogoptions *i, FILE *dst, struct clast_reduction { switch (r->type) { case clast_red_sum: - pprint_sum(dst, r); + pprint_sum(i, dst, r); break; case clast_red_min: case clast_red_max: @@ -205,8 +217,11 @@ void pprint_expr(struct cloogoptions *i, FILE *dst, struct clast_expr *e) if (!e) return; switch (e->type) { + case expr_name: + pprint_name(dst, (struct clast_name*) e); + break; case expr_term: - pprint_term(dst, (struct clast_term*) e); + pprint_term(i, dst, (struct clast_term*) e); break; case expr_red: pprint_reduction(i, dst, (struct clast_reduction*) e); -- 2.11.4.GIT