X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=isl_ast.c;h=5885e41328f6ec270c406a0c8cd1d68f3efc7437;hb=32aabe2c05cbaa2df5907aee48d1874936266a29;hp=dbf390441abb632edc622905952cb41decc88676;hpb=5888ac1c9fb2c2c603f8b7acea8be0196754bead;p=platform%2Fupstream%2Fisl.git diff --git a/isl_ast.c b/isl_ast.c index dbf3904..5885e41 100644 --- a/isl_ast.c +++ b/isl_ast.c @@ -1,5 +1,5 @@ #include -#include +#include #undef BASE #define BASE ast_expr @@ -11,13 +11,81 @@ #include +isl_ctx *isl_ast_print_options_get_ctx( + __isl_keep isl_ast_print_options *options) +{ + return options ? options->ctx : NULL; +} + __isl_give isl_ast_print_options *isl_ast_print_options_alloc(isl_ctx *ctx) { - return isl_calloc_type(ctx, isl_ast_print_options); + isl_ast_print_options *options; + + options = isl_calloc_type(ctx, isl_ast_print_options); + if (!options) + return NULL; + + options->ctx = ctx; + isl_ctx_ref(ctx); + options->ref = 1; + + return options; +} + +__isl_give isl_ast_print_options *isl_ast_print_options_dup( + __isl_keep isl_ast_print_options *options) +{ + isl_ctx *ctx; + isl_ast_print_options *dup; + + if (!options) + return NULL; + + ctx = isl_ast_print_options_get_ctx(options); + dup = isl_ast_print_options_alloc(ctx); + if (!dup) + return NULL; + + dup->print_for = options->print_for; + dup->print_for_user = options->print_for_user; + dup->print_user = options->print_user; + dup->print_user_user = options->print_user_user; + + return dup; +} + +__isl_give isl_ast_print_options *isl_ast_print_options_cow( + __isl_take isl_ast_print_options *options) +{ + if (!options) + return NULL; + + if (options->ref == 1) + return options; + options->ref--; + return isl_ast_print_options_dup(options); +} + +__isl_give isl_ast_print_options *isl_ast_print_options_copy( + __isl_keep isl_ast_print_options *options) +{ + if (!options) + return NULL; + + options->ref++; + return options; } void *isl_ast_print_options_free(__isl_take isl_ast_print_options *options) { + if (!options) + return NULL; + + if (--options->ref > 0) + return NULL; + + isl_ctx_deref(options->ctx); + free(options); return NULL; } @@ -30,9 +98,11 @@ void *isl_ast_print_options_free(__isl_take isl_ast_print_options *options) __isl_give isl_ast_print_options *isl_ast_print_options_set_print_user( __isl_take isl_ast_print_options *options, __isl_give isl_printer *(*print_user)(__isl_take isl_printer *p, + __isl_take isl_ast_print_options *options, __isl_keep isl_ast_node *node, void *user), void *user) { + options = isl_ast_print_options_cow(options); if (!options) return NULL; @@ -49,9 +119,11 @@ __isl_give isl_ast_print_options *isl_ast_print_options_set_print_user( __isl_give isl_ast_print_options *isl_ast_print_options_set_print_for( __isl_take isl_ast_print_options *options, __isl_give isl_printer *(*print_for)(__isl_take isl_printer *p, + __isl_take isl_ast_print_options *options, __isl_keep isl_ast_node *node, void *user), void *user) { + options = isl_ast_print_options_cow(options); if (!options) return NULL; @@ -82,7 +154,7 @@ __isl_give isl_ast_expr *isl_ast_expr_dup(__isl_keep isl_ast_expr *expr) ctx = isl_ast_expr_get_ctx(expr); switch (expr->type) { case isl_ast_expr_int: - dup = isl_ast_expr_alloc_int(ctx, expr->u.i); + dup = isl_ast_expr_from_val(isl_val_copy(expr->u.v)); break; case isl_ast_expr_id: dup = isl_ast_expr_from_id(isl_id_copy(expr->u.id)); @@ -131,7 +203,7 @@ void *isl_ast_expr_free(__isl_take isl_ast_expr *expr) switch (expr->type) { case isl_ast_expr_int: - isl_int_clear(expr->u.i); + isl_val_free(expr->u.v); break; case isl_ast_expr_id: isl_id_free(expr->u.id); @@ -166,8 +238,19 @@ int isl_ast_expr_get_int(__isl_keep isl_ast_expr *expr, isl_int *v) if (expr->type != isl_ast_expr_int) isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid, "expression not an int", return -1); - isl_int_set(*v, expr->u.i); - return 0; + return isl_val_get_num_isl_int(expr->u.v, v); +} + +/* Return the integer value represented by "expr". + */ +__isl_give isl_val *isl_ast_expr_get_val(__isl_keep isl_ast_expr *expr) +{ + if (!expr) + return NULL; + if (expr->type != isl_ast_expr_int) + isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid, + "expression not an int", return NULL); + return isl_val_copy(expr->u.v); } __isl_give isl_id *isl_ast_expr_get_id(__isl_keep isl_ast_expr *expr) @@ -304,30 +387,36 @@ __isl_give isl_ast_expr *isl_ast_expr_alloc_int_si(isl_ctx *ctx, int i) isl_ctx_ref(ctx); expr->ref = 1; expr->type = isl_ast_expr_int; - - isl_int_init(expr->u.i); - isl_int_set_si(expr->u.i, i); + expr->u.v = isl_val_int_from_si(ctx, i); + if (!expr->u.v) + return isl_ast_expr_free(expr); return expr; } -/* Create a new integer expression representing "i". +/* Create a new integer expression representing "v". */ -__isl_give isl_ast_expr *isl_ast_expr_alloc_int(isl_ctx *ctx, isl_int i) +__isl_give isl_ast_expr *isl_ast_expr_from_val(__isl_take isl_val *v) { + isl_ctx *ctx; isl_ast_expr *expr; + if (!v) + return NULL; + if (!isl_val_is_int(v)) + isl_die(isl_val_get_ctx(v), isl_error_invalid, + "expecting integer value", return isl_val_free(v)); + + ctx = isl_val_get_ctx(v); expr = isl_calloc_type(ctx, isl_ast_expr); if (!expr) - return NULL; + return isl_val_free(v); expr->ctx = ctx; isl_ctx_ref(ctx); expr->ref = 1; expr->type = isl_ast_expr_int; - - isl_int_init(expr->u.i); - isl_int_set(expr->u.i, i); + expr->u.v = v; return expr; } @@ -918,7 +1007,9 @@ static char *op_str[] = { [isl_ast_op_div] = "/", [isl_ast_op_eq] = "==", [isl_ast_op_le] = "<=", - [isl_ast_op_ge] = ">=" + [isl_ast_op_ge] = ">=", + [isl_ast_op_lt] = "<", + [isl_ast_op_gt] = ">" }; /* Precedence in C of the various operators. @@ -945,6 +1036,8 @@ static int op_prec[] = { [isl_ast_op_eq] = 9, [isl_ast_op_le] = 8, [isl_ast_op_ge] = 8, + [isl_ast_op_lt] = 8, + [isl_ast_op_gt] = 8, [isl_ast_op_call] = 2 }; @@ -970,6 +1063,8 @@ static int op_left[] = { [isl_ast_op_eq] = 1, [isl_ast_op_le] = 1, [isl_ast_op_ge] = 1, + [isl_ast_op_lt] = 1, + [isl_ast_op_gt] = 1, [isl_ast_op_call] = 1 }; @@ -1154,7 +1249,7 @@ __isl_give isl_printer *isl_printer_print_ast_expr(__isl_take isl_printer *p, p = isl_printer_print_str(p, isl_id_get_name(expr->u.id)); break; case isl_ast_expr_int: - p = isl_printer_print_isl_int(p, expr->u.i); + p = isl_printer_print_val(p, expr->u.v); break; case isl_ast_expr_error: break; @@ -1237,7 +1332,7 @@ static int need_block(__isl_keep isl_ast_node *node) static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p, __isl_keep isl_ast_node *node, - __isl_keep isl_ast_print_options *options, int in_block); + __isl_keep isl_ast_print_options *options, int in_block, int in_list); static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p, __isl_keep isl_ast_node *node, __isl_keep isl_ast_print_options *options, int new_line); @@ -1268,7 +1363,8 @@ static __isl_give isl_printer *print_body_c(__isl_take isl_printer *p, if (!else_node && !need_block(node)) { p = isl_printer_end_line(p); p = isl_printer_indent(p, 2); - p = isl_ast_node_print(node, p, options); + p = isl_ast_node_print(node, p, + isl_ast_print_options_copy(options)); p = isl_printer_indent(p, -2); return p; } @@ -1276,7 +1372,7 @@ static __isl_give isl_printer *print_body_c(__isl_take isl_printer *p, p = isl_printer_print_str(p, " {"); p = isl_printer_end_line(p); p = isl_printer_indent(p, 2); - p = print_ast_node_c(p, node, options, 1); + p = print_ast_node_c(p, node, options, 1, 0); p = isl_printer_indent(p, -2); p = isl_printer_start_line(p); p = isl_printer_print_str(p, "}"); @@ -1294,6 +1390,30 @@ static __isl_give isl_printer *print_body_c(__isl_take isl_printer *p, return p; } +/* Print the start of a compound statement. + */ +static __isl_give isl_printer *start_block(__isl_take isl_printer *p) +{ + p = isl_printer_start_line(p); + p = isl_printer_print_str(p, "{"); + p = isl_printer_end_line(p); + p = isl_printer_indent(p, 2); + + return p; +} + +/* Print the end of a compound statement. + */ +static __isl_give isl_printer *end_block(__isl_take isl_printer *p) +{ + p = isl_printer_indent(p, -2); + p = isl_printer_start_line(p); + p = isl_printer_print_str(p, "}"); + p = isl_printer_end_line(p); + + return p; +} + /* Print the for node "node". * * If the for node is degenerate, it is printed as @@ -1307,12 +1427,15 @@ static __isl_give isl_printer *print_body_c(__isl_take isl_printer *p, * body * * "in_block" is set if we are currently inside a block. - * We simply pass it along to print_ast_node_c in case of a degenerate - * for loop. + * "in_list" is set if the current node is not alone in the block. + * If we are not in a block or if the current not is not alone in the block + * then we print a block around a degenerate for loop such that the variable + * declaration will not conflict with any potential other declaration + * of the same variable. */ static __isl_give isl_printer *print_for_c(__isl_take isl_printer *p, __isl_keep isl_ast_node *node, - __isl_keep isl_ast_print_options *options, int in_block) + __isl_keep isl_ast_print_options *options, int in_block, int in_list) { isl_id *id; const char *name; @@ -1342,6 +1465,8 @@ static __isl_give isl_printer *print_for_c(__isl_take isl_printer *p, id = isl_ast_expr_get_id(node->u.f.iterator); name = isl_id_get_name(id); isl_id_free(id); + if (!in_block || in_list) + p = start_block(p); p = isl_printer_start_line(p); p = isl_printer_print_str(p, type); p = isl_printer_print_str(p, " "); @@ -1350,7 +1475,9 @@ static __isl_give isl_printer *print_for_c(__isl_take isl_printer *p, p = isl_printer_print_ast_expr(p, node->u.f.init); p = isl_printer_print_str(p, ";"); p = isl_printer_end_line(p); - p = print_ast_node_c(p, node->u.f.body, options, in_block); + p = print_ast_node_c(p, node->u.f.body, options, 1, 0); + if (!in_block || in_list) + p = end_block(p); } return p; @@ -1379,40 +1506,36 @@ static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p, * If so, we do not print a block around the children of a block node. * We do this to avoid an extra block around the body of a degenerate * for node. + * + * "in_list" is set if the current node is not alone in the block. */ static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p, __isl_keep isl_ast_node *node, - __isl_keep isl_ast_print_options *options, int in_block) + __isl_keep isl_ast_print_options *options, int in_block, int in_list) { switch (node->type) { case isl_ast_node_for: if (options->print_for) - return options->print_for(p, node, - options->print_for_user); - p = print_for_c(p, node, options, in_block); + return options->print_for(p, + isl_ast_print_options_copy(options), + node, options->print_for_user); + p = print_for_c(p, node, options, in_block, in_list); break; case isl_ast_node_if: p = print_if_c(p, node, options, 1); break; case isl_ast_node_block: - if (!in_block) { - p = isl_printer_start_line(p); - p = isl_printer_print_str(p, "{"); - p = isl_printer_end_line(p); - p = isl_printer_indent(p, 2); - } + if (!in_block) + p = start_block(p); p = isl_ast_node_list_print(node->u.b.children, p, options); - if (!in_block) { - p = isl_printer_indent(p, -2); - p = isl_printer_start_line(p); - p = isl_printer_print_str(p, "}"); - p = isl_printer_end_line(p); - } + if (!in_block) + p = end_block(p); break; case isl_ast_node_user: if (options->print_user) - return options->print_user(p, node, - options->print_user_user); + return options->print_user(p, + isl_ast_print_options_copy(options), + node, options->print_user_user); p = isl_printer_start_line(p); p = isl_printer_print_ast_expr(p, node->u.e.expr); p = isl_printer_print_str(p, ";"); @@ -1427,15 +1550,18 @@ static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p, /* Print the for node "node" to "p". */ __isl_give isl_printer *isl_ast_node_for_print(__isl_keep isl_ast_node *node, - __isl_take isl_printer *p, __isl_keep isl_ast_print_options *options) + __isl_take isl_printer *p, __isl_take isl_ast_print_options *options) { if (!node || !options) goto error; if (node->type != isl_ast_node_for) isl_die(isl_ast_node_get_ctx(node), isl_error_invalid, "not a for node", goto error); - return print_for_c(p, node, options, 0); + p = print_for_c(p, node, options, 0, 0); + isl_ast_print_options_free(options); + return p; error: + isl_ast_print_options_free(options); isl_printer_free(p); return NULL; } @@ -1443,15 +1569,18 @@ error: /* Print the if node "node" to "p". */ __isl_give isl_printer *isl_ast_node_if_print(__isl_keep isl_ast_node *node, - __isl_take isl_printer *p, __isl_keep isl_ast_print_options *options) + __isl_take isl_printer *p, __isl_take isl_ast_print_options *options) { if (!node || !options) goto error; if (node->type != isl_ast_node_if) isl_die(isl_ast_node_get_ctx(node), isl_error_invalid, "not an if node", goto error); - return print_if_c(p, node, options, 1); + p = print_if_c(p, node, options, 1); + isl_ast_print_options_free(options); + return p; error: + isl_ast_print_options_free(options); isl_printer_free(p); return NULL; } @@ -1459,12 +1588,15 @@ error: /* Print "node" to "p". */ __isl_give isl_printer *isl_ast_node_print(__isl_keep isl_ast_node *node, - __isl_take isl_printer *p, __isl_keep isl_ast_print_options *options) + __isl_take isl_printer *p, __isl_take isl_ast_print_options *options) { if (!options || !node) goto error; - return print_ast_node_c(p, node, options, 0); + p = print_ast_node_c(p, node, options, 0, 0); + isl_ast_print_options_free(options); + return p; error: + isl_ast_print_options_free(options); isl_printer_free(p); return NULL; } @@ -1488,7 +1620,6 @@ __isl_give isl_printer *isl_printer_print_ast_node(__isl_take isl_printer *p, case ISL_FORMAT_C: options = isl_ast_print_options_alloc(isl_printer_get_ctx(p)); p = isl_ast_node_print(node, p, options); - isl_ast_print_options_free(options); break; default: isl_die(isl_printer_get_ctx(p), isl_error_unsupported, @@ -1511,7 +1642,7 @@ __isl_give isl_printer *isl_ast_node_list_print( return isl_printer_free(p); for (i = 0; i < list->n; ++i) - p = print_ast_node_c(p, list->p[i], options, 1); + p = print_ast_node_c(p, list->p[i], options, 1, 1); return p; }