From 36fb0ba973cfe9d7f01d3f8a4c7a7b2dc3e53588 Mon Sep 17 00:00:00 2001 From: Claude Betz Date: Mon, 25 Mar 2024 11:07:30 +0000 Subject: [PATCH] feat: converge abstract and body syntax issue: https://github.com/xr0-org/xr0/issues/33 --- editors/vim/syntax.vim | 2 +- include/ast.h | 32 ++- include/state.h | 2 +- include/storage.h | 2 - libx/stdio.h | 2 +- libx/stdlib.h | 4 +- src/0v/ast/expr/expr.c | 94 +++++++++ src/0v/ast/expr/expr.h | 11 ++ src/0v/ast/expr/verify.c | 184 +++++++++++++++++- src/0v/ast/function/function.c | 2 +- src/0v/ast/gram.y | 29 ++- src/0v/ast/lex.l | 6 +- src/0v/ast/stmt/stmt.c | 103 ---------- src/0v/ast/stmt/stmt.h | 6 - src/0v/ast/stmt/verify.c | 152 ++------------- src/0v/state/stack.c | 8 +- src/0v/state/state.c | 4 +- tests/0-basic/030-alloc.x | 2 +- tests/0-basic/041-FAIL-false-alloc-void.x | 2 +- tests/0-basic/050-two-variables.x | 2 +- tests/0-basic/060-FAIL-two-allocs.x | 2 +- tests/0-basic/160-one-free-one-alloc.x | 2 +- tests/0-basic/180-FAIL-alloc-freed.x | 2 +- tests/0-basic/200-free-param.x | 4 +- tests/1-branches/0100-body-trivial.x | 2 +- .../1-branches/0200-conditional-allocation.x | 4 +- .../0300-conditional-allocation-body.x | 2 +- .../0310-FAIL-conditional-allocation-body.x | 2 +- .../0320-FAIL-conditional-allocation-body.x | 2 +- .../0400-strcmp-conditional-allocation.x | 4 +- .../0500-strcmp-conditional-allocation-body.x | 2 +- tests/1-branches/0600-indirect.x | 4 +- tests/1-branches/0700-indirect.x | 4 +- tests/1-branches/0800-indirect.x | 4 +- tests/1-branches/0900-subsequent.x | 4 +- tests/1-branches/1000-chaining-functions.x | 10 +- tests/3-topological/001-valid-sort-matrix.x | 38 ++-- tests/3-topological/002-valid-sort-parse.x | 154 +++++++++------ .../000-internal-prototype-abstract.x | 2 +- .../010-internal-two-matching-abstracts.x | 4 +- .../020-FAIL-internal-mismatch-abstracts.x | 4 +- .../030-FAIL-internal-missing-definition.x | 2 +- .../4-linking/040-FAIL-multiple-definitions.x | 2 +- tests/4-linking/050-multiple-prototype.x | 2 +- tests/5-pass-by-ptr/000-basic.x | 2 +- .../100-FAIL-need-alloced-lval.x | 2 +- .../101-FAIL-need-alloced-rval.x | 4 +- .../102-FAIL-need-clump-lval-freed-ptr.x | 2 +- .../103-FAIL-need-clump-lval-assigned-ptr.x | 2 +- tests/7-use-after-free/000-read-and-write.x | 10 +- .../001-read-and-write-arbitrary.x | 10 +- tests/7-use-after-free/002-pass-in-lvalue.x | 2 +- tests/7-use-after-free/003-pass-in-rvalue.x | 4 +- .../004-mix-multiple-levels.x | 8 +- tests/7-use-after-free/005-conditions.x | 4 +- .../112-FAIL-freed-ptr-dangling-false-claim.x | 2 +- ...120-FAIL-freed-ptr-maybe-dangling-return.x | 6 +- ...freed-ptr-maybe-dangling-return.x.EXPECTED | 2 +- ...210-FAIL-stack-frame-pop-dangling-assign.x | 2 +- .../300-FAIL-struct-free-ptr-dangling.x | 12 +- ...0-FAIL-struct-free-ptr-dangling.x.EXPECTED | 2 +- .../400-FAIL-conditions-in-setup.x | 2 +- .../004-FAIL-uninitialised-memory-rval.x | 2 +- tests/99-program/000-matrix.x | 41 ++-- tests/99-program/100-lex/parse.x | 52 ++--- 65 files changed, 605 insertions(+), 479 deletions(-) diff --git a/editors/vim/syntax.vim b/editors/vim/syntax.vim index 3fd40d9..282067d 100644 --- a/editors/vim/syntax.vim +++ b/editors/vim/syntax.vim @@ -12,7 +12,7 @@ syntax keyword Keyword const exit syntax keyword Type unsigned int char double void bool size_t FILE syntax keyword Structure struct enum union syntax keyword Conditional if else for some while switch assume pre -syntax keyword Include axiom alloc dealloc realloc clump +syntax keyword Include axiom malloc free realloc clump syntax keyword Label case default syntax keyword Boolean true false syntax keyword Exception return result undefined diff --git a/include/ast.h b/include/ast.h index 189fe0a..3e41955 100644 --- a/include/ast.h +++ b/include/ast.h @@ -25,6 +25,9 @@ ast_expr_as_constant(struct ast_expr *expr); char * ast_expr_as_literal(struct ast_expr *); +struct ast_expr * +ast_expr_as_allocation(struct ast_expr *); + struct ast_expr * ast_expr_literal_create(char *); @@ -144,6 +147,27 @@ ast_expr_isisdereferencable(struct ast_expr *expr); struct ast_expr * ast_expr_arbarg_create(); +struct ast_expr * +ast_expr_alloc_create(struct ast_expr *); + +struct ast_expr * +ast_expr_dealloc_create(struct ast_expr *); + +struct ast_expr * +ast_expr_clump_create(struct ast_expr *); + +struct ast_expr * +ast_expr_alloc_arg(struct ast_expr *); + +enum ast_alloc_kind +ast_expr_alloc_kind(struct ast_expr *); + +struct state; + +struct error * +ast_expr_alloc_rangeprocess(struct ast_expr *expr, struct ast_expr *lw, + struct ast_expr *up, struct state *); + void ast_expr_destroy(struct ast_expr *); @@ -161,8 +185,6 @@ struct math_state; bool ast_expr_matheval(struct ast_expr *e); -struct state; - bool ast_expr_decide(struct ast_expr *, struct state *); @@ -345,12 +367,6 @@ ast_stmt_create_dealloc(struct lexememarker *, struct ast_expr *arg); struct ast_stmt * ast_stmt_create_clump(struct lexememarker *, struct ast_expr *arg); -struct ast_expr * -ast_stmt_alloc_arg(struct ast_stmt *); - -enum ast_alloc_kind -ast_stmt_alloc_kind(struct ast_stmt *); - void ast_stmt_destroy(struct ast_stmt *); diff --git a/include/state.h b/include/state.h index a97bd1b..ecb0ed0 100644 --- a/include/state.h +++ b/include/state.h @@ -3,7 +3,7 @@ #include -#define KEYWORD_RESULT "result" +#define KEYWORD_RETURN "return" /* ast */ struct ast_type; diff --git a/include/storage.h b/include/storage.h index 92e43f9..e61cd96 100644 --- a/include/storage.h +++ b/include/storage.h @@ -95,6 +95,4 @@ state_alloc(struct state *state); char * state_str(struct state *state); -#define KEYWORD_RESULT "result" - #endif diff --git a/libx/stdio.h b/libx/stdio.h index 83f9c8b..28c6280 100644 --- a/libx/stdio.h +++ b/libx/stdio.h @@ -15,7 +15,7 @@ axiom int fputs(char *s, FILE *stream); axiom int -puts(char *s) ~ [ pre: .clump s; ]; +puts(char *s) ~ [ pre: s = .clump(1); ]; axiom FILE * fopen(char *pathname, char *mode); diff --git a/libx/stdlib.h b/libx/stdlib.h index 622ee12..e77f6da 100644 --- a/libx/stdlib.h +++ b/libx/stdlib.h @@ -4,10 +4,10 @@ #include axiom void * -malloc(int size) ~ [ .alloc result; ]; +malloc(int size) ~ [ return .malloc(size); ]; axiom void -free(void *ptr) ~ [ .dealloc ptr; ]; +free(void *ptr) ~ [ .free(ptr); ]; axiom void exit(int status); diff --git a/src/0v/ast/expr/expr.c b/src/0v/ast/expr/expr.c index 55f999c..c9c2b46 100644 --- a/src/0v/ast/expr/expr.c +++ b/src/0v/ast/expr/expr.c @@ -656,6 +656,91 @@ ast_expr_arbarg_create() return expr; } +struct ast_expr * +ast_expr_alloc_create(struct ast_expr *arg) +{ + struct ast_expr *expr = ast_expr_create(); + expr->kind = EXPR_ALLOCATION; + expr->u.alloc.kind = ALLOC; + expr->u.alloc.arg = arg; + return expr; +} + +struct ast_expr * +ast_expr_dealloc_create(struct ast_expr *arg) +{ + struct ast_expr *expr = ast_expr_create(); + expr->kind = EXPR_ALLOCATION; + expr->u.alloc.kind = DEALLOC; + expr->u.alloc.arg = arg; + return expr; +} + +struct ast_expr * +ast_expr_clump_create(struct ast_expr *arg) +{ + struct ast_expr *expr = ast_expr_create(); + expr->kind = EXPR_ALLOCATION; + expr->u.alloc.kind= CLUMP; + expr->u.alloc.arg = arg; + return expr; +} + +static struct ast_expr * +ast_expr_alloc_copy(struct ast_expr *expr) +{ + struct ast_expr *arg = ast_expr_copy(expr->u.alloc.arg); + switch (expr->u.alloc.kind) { + case ALLOC: + return ast_expr_alloc_create(arg); + case DEALLOC: + return ast_expr_dealloc_create(arg); + case CLUMP: + return ast_expr_clump_create(arg); + default: + assert(false); + } +} + +static void +ast_expr_alloc_str_build(struct ast_expr *expr, struct strbuilder *b) +{ + assert(expr->kind == EXPR_ALLOCATION); + + char *arg = ast_expr_str(expr->u.alloc.arg); + + switch (expr->u.alloc.kind) { + case ALLOC: + strbuilder_printf(b, ".%s %s;", "malloc", arg); + break; + case DEALLOC: + strbuilder_printf(b, ".%s %s;", "free", arg); + break; + case CLUMP: + strbuilder_printf(b, ".%s %s;", "clump", arg); + break; + default: + assert(false); + } + free(arg); +} + +struct ast_expr * +ast_expr_alloc_arg(struct ast_expr *expr) +{ + assert(expr->kind == EXPR_ALLOCATION); + return expr->u.alloc.arg; +} + +enum ast_alloc_kind +ast_expr_alloc_kind(struct ast_expr *expr) +{ + assert(expr->kind == EXPR_ALLOCATION); + return expr->u.alloc.kind; +} + + + void ast_expr_destroy(struct ast_expr *expr) { @@ -697,6 +782,9 @@ ast_expr_destroy(struct ast_expr *expr) break; case EXPR_ARBARG: break; + case EXPR_ALLOCATION: + ast_expr_destroy(expr->u.alloc.arg); + break; default: assert(false); } @@ -747,6 +835,9 @@ ast_expr_str(struct ast_expr *expr) case EXPR_ARBARG: strbuilder_putc(b, '$'); break; + case EXPR_ALLOCATION: + ast_expr_alloc_str_build(expr, b); + break; default: assert(false); } @@ -806,6 +897,8 @@ ast_expr_copy(struct ast_expr *expr) ); case EXPR_ARBARG: return ast_expr_arbarg_create(); + case EXPR_ALLOCATION: + return ast_expr_alloc_copy(expr); default: assert(false); } @@ -1014,6 +1107,7 @@ ast_expr_splits(struct ast_expr *e, struct state *s) case EXPR_STRING_LITERAL: case EXPR_ARBARG: case EXPR_ISDEREFERENCABLE: + case EXPR_ALLOCATION: return (struct ast_stmt_splits) { .n = 0, .cond = NULL, .err = NULL }; default: assert(false); diff --git a/src/0v/ast/expr/expr.h b/src/0v/ast/expr/expr.h index 9bc1190..e8a7615 100644 --- a/src/0v/ast/expr/expr.h +++ b/src/0v/ast/expr/expr.h @@ -3,6 +3,12 @@ #include "util.h" +enum ast_alloc_kind { + ALLOC = 1 << 0, + DEALLOC = 1 << 1, + CLUMP = 1 << 2, +}; + struct ast_expr { enum ast_expr_kind { EXPR_IDENTIFIER = 1 << 0, @@ -24,6 +30,7 @@ struct ast_expr { EXPR_ISDEALLOCAND = 1 << 11, EXPR_ISDEREFERENCABLE = 1 << 12, EXPR_ARBARG = 1 << 13, + EXPR_ALLOCATION = 1 << 14, } kind; struct ast_expr *root; union { @@ -63,6 +70,10 @@ struct ast_expr { struct ast_expr *e1, *e2; } binary; struct ast_expr *assignment_value; + struct { + enum ast_alloc_kind kind; + struct ast_expr *arg; + } alloc; } u; }; diff --git a/src/0v/ast/expr/verify.c b/src/0v/ast/expr/verify.c index edb4521..f5a11fe 100644 --- a/src/0v/ast/expr/verify.c +++ b/src/0v/ast/expr/verify.c @@ -119,6 +119,100 @@ expr_isdeallocand_rangedecide(struct ast_expr *expr, struct ast_expr *lw, return state_range_aredeallocands(state, obj, lw, up); } +static struct error * +rangeprocess_alloc(struct ast_expr *, struct ast_expr *lw, struct ast_expr *up, struct state *); + +static struct error * +rangeprocess_dealloc(struct ast_expr *, struct ast_expr *lw, struct ast_expr *up, struct state *); + +static struct object * +hack_base_object_from_alloc(struct ast_expr *, struct state *); + +struct error * +ast_expr_alloc_rangeprocess(struct ast_expr *alloc, struct ast_expr *lw, struct ast_expr *up, + struct state *state) +{ + struct error *err; + + struct result *result_lw = ast_expr_eval(lw, state), + *result_up = ast_expr_eval(up, state); + + if (result_iserror(result_lw)) { + return result_as_error(result_lw); + } + if (result_iserror(result_up)) { + return result_as_error(result_up); + } + + /*printf("stmt: %s\n", ast_stmt_str(stmt));*/ + /*printf("state: %s\n", state_str(state));*/ + + struct ast_expr *res_lw = value_to_expr(result_as_value(result_lw)), + *res_up = value_to_expr(result_as_value(result_up)); + + result_destroy(result_up); + result_destroy(result_lw); + + switch (alloc->kind) { + case EXPR_ASSIGNMENT: + err = rangeprocess_alloc(alloc, res_lw, res_up, state); + break; + case EXPR_ALLOCATION: + err = rangeprocess_dealloc(alloc, res_lw, res_up, state); + break; + default: + assert(false); + } + + ast_expr_destroy(res_up); + ast_expr_destroy(res_lw); + + if (err) { + return err; + } + return NULL; +} + +static struct error * +rangeprocess_alloc(struct ast_expr *expr, struct ast_expr *lw, struct ast_expr *up, + struct state *state) +{ + struct ast_expr *lval = ast_expr_assignment_lval(expr), + *rval = ast_expr_assignment_rval(expr); + assert(ast_expr_kind(rval) == EXPR_ALLOCATION); + assert(ast_expr_alloc_kind(rval) != DEALLOC); + struct object *obj = hack_base_object_from_alloc(lval, state); + return state_range_alloc(state, obj, lw, up); +} + +static struct error * +rangeprocess_dealloc(struct ast_expr *dealloc, struct ast_expr *lw, struct ast_expr *up, + struct state *state) +{ + struct object *obj = hack_base_object_from_alloc(ast_expr_alloc_arg(dealloc), state); + return state_range_dealloc(state, obj, lw, up); +} + +static struct object * +hack_base_object_from_alloc(struct ast_expr *expr, struct state *state) +{ + /* we're currently discarding analysis of `offset` and relying on the + * bounds (lower, upper beneath) alone. We passed in `*(arr+offset)` */ + struct ast_expr *inner = ast_expr_unary_operand(expr); /* `arr+offset` */ + struct ast_expr *i = ast_expr_identifier_create(dynamic_str("i")); + assert(ast_expr_equal(ast_expr_binary_e2(inner), i)); + ast_expr_destroy(i); + struct lvalue_res res = ast_expr_lvalue(ast_expr_binary_e1(inner), state); + if (res.err) { + assert(false); + } + struct object *obj = lvalue_object(res.lval); + assert(obj); + return obj; +} + + + struct error * ast_expr_exec(struct ast_expr *expr, struct state *state) { @@ -160,7 +254,7 @@ expr_identifier_lvalue(struct ast_expr *expr, struct state *state) char *id = ast_expr_as_identifier(expr); return (struct lvalue_res) { - .lval = lvalue_create( state_getobjecttype(state, id), state_getobject(state, id)), + .lval = lvalue_create(state_getobjecttype(state, id), state_getobject(state, id)), .err = NULL }; } @@ -268,9 +362,6 @@ expr_isdeallocand_decide(struct ast_expr *expr, struct state *state) return isdeallocand; } -struct result * -ast_expr_eval(struct ast_expr *expr, struct state *state); - static bool value_compare(struct value *, enum ast_binary_operator, struct value *); @@ -942,30 +1033,105 @@ arbarg_eval(struct ast_expr *expr, struct state *state) } static struct result * -assign_absexec(struct ast_expr *expr, struct state *state); +assign_absexec(struct ast_expr *, struct state *); + +static struct result * +isdereferencable_absexec(struct ast_expr *, struct state *); static struct result * -isdereferencable_absexec(struct ast_expr *expr, struct state *state); +alloc_absexec(struct ast_expr *, struct state *); struct result * ast_expr_absexec(struct ast_expr *expr, struct state *state) { switch (ast_expr_kind(expr)) { - case EXPR_CALL: - return expr_call_eval(expr, state); case EXPR_ASSIGNMENT: return assign_absexec(expr, state); case EXPR_ISDEREFERENCABLE: return isdereferencable_absexec(expr, state); + case EXPR_ALLOCATION: + return alloc_absexec(expr, state); + case EXPR_IDENTIFIER: + case EXPR_CONSTANT: + case EXPR_UNARY: + case EXPR_CALL: + case EXPR_STRUCTMEMBER: + case EXPR_ARBARG: + return ast_expr_eval(expr, state); default: assert(false); } } +static struct result * +dealloc_process(struct ast_expr *, struct state *); + +/* operates at location level. It either creates an object on the heap and returns + * a location or gets the location pointed to by an lvalue and attempts to free + * possibly returning an error + * */ +static struct result * +alloc_absexec(struct ast_expr *expr, struct state *state) +{ + switch (ast_expr_alloc_kind(expr)) { + case ALLOC: + /* TODO: size needs to be passed in here when added to .alloc */ + return result_value_create(state_alloc(state)); + case DEALLOC: + return dealloc_process(expr, state); + case CLUMP: + return result_value_create(state_clump(state)); + default: + assert(false); + } +} + +static struct result * +dealloc_process(struct ast_expr *expr, struct state *state) +{ + struct ast_expr *arg = ast_expr_alloc_arg(expr); + /* arg is pointing at the heap location we want to free, so we want its + * value rather than location */ + struct result *res = ast_expr_eval(arg, state); + if (result_iserror(res)) { + return res; + } + struct value *val = result_as_value(res); + assert(val); + struct error *err = state_dealloc(state, val); + if (err) { + return result_error_create(err); + } + value_destroy(val); + return result_value_create(NULL); +} + static struct result * assign_absexec(struct ast_expr *expr, struct state *state) { - return expr_assign_eval(expr, state); + struct ast_expr *lval = ast_expr_assignment_lval(expr), + *rval = ast_expr_assignment_rval(expr); + + struct result *res = ast_expr_absexec(rval, state); + if (result_iserror(res)) { + return res; + } + if (!result_hasvalue(res)) { + assert(false); + return result_error_create(error_create("undefined indirection (rvalue)")); + } + struct lvalue_res lval_res = ast_expr_lvalue(lval, state); + if (lval_res.err) { + return result_error_create(lval_res.err); + } + struct object *obj = lvalue_object(lval_res.lval); + if (!obj) { + return result_error_create(error_create("undefined indirection (lvalue)")); + } + + object_assign(obj, value_copy(result_as_value(res))); + + return res; } static struct result * diff --git a/src/0v/ast/function/function.c b/src/0v/ast/function/function.c index d5c21c2..7ac8739 100644 --- a/src/0v/ast/function/function.c +++ b/src/0v/ast/function/function.c @@ -336,7 +336,7 @@ inititalise_param(struct ast_variable *param, struct state *state) assert(obj); if (object_hasvalue(obj)) { /* must on the clump or heap */ - struct value *val = object_as_value(obj); + //struct value *val = object_as_value(obj); //struct location *loc = value_as_location(val); //assert( // location_type(loc) == LOCATION_DEREFERENCABLE || diff --git a/src/0v/ast/gram.y b/src/0v/ast/gram.y index 6235430..d2891cb 100644 --- a/src/0v/ast/gram.y +++ b/src/0v/ast/gram.y @@ -62,8 +62,7 @@ expr_array_create(struct ast_expr *v) } struct expr_array -expr_array_append(struct expr_array *arr, struct ast_expr *v) -{ +expr_array_append(struct expr_array *arr, struct ast_expr *v) { arr->expr = realloc(arr->expr, sizeof(struct ast_expr *) * ++arr->n); arr->expr[arr->n-1] = v; return *arr; @@ -99,7 +98,7 @@ variable_array_create(struct ast_variable *v) %token ARB_ARG %token CASE DEFAULT IF ELSE SWITCH WHILE DO FOR SOME GOTO CONTINUE BREAK RETURN -%token ALLOC DEALLOC CLUMP +%token TK_ALLOC TK_DEALLOC TK_CLUMP %start translation_unit @@ -167,7 +166,7 @@ variable_array_create(struct ast_variable *v) %type exclusive_or_expression and_expression equality_expression %type relational_expression shift_expression additive_expression %type multiplicative_expression cast_expression -%type isdeallocand_expression +%type allocation_expression isdeallocand_expression %type argument_expression_list @@ -177,7 +176,7 @@ variable_array_create(struct ast_variable *v) %type pointer %type statement expression_statement selection_statement jump_statement %type labelled_statement iteration_statement for_iteration_statement -%type iteration_effect_statement allocation_statement +%type iteration_effect_statement %type statement_list %type identifier direct_declarator %type declaration_specifiers type_specifier struct_or_union_specifier @@ -203,6 +202,15 @@ primary_expression { $$ = ast_expr_bracketed_create($2); } ; +allocation_expression + : TK_ALLOC '(' expression ')' + { $$ = ast_expr_alloc_create($3); } + | TK_DEALLOC '(' expression ')' + { $$ = ast_expr_dealloc_create($3); } + | TK_CLUMP '(' expression ')' + { $$ = ast_expr_clump_create($3); } + ; + postfix_expression : primary_expression | postfix_expression '[' expression ']' { @@ -234,6 +242,7 @@ postfix_expression { $$ = ast_expr_incdec_create($1, true, false); } | postfix_expression DEC_OP { $$ = ast_expr_incdec_create($1, false, false); } + | allocation_expression ; argument_expression_list @@ -672,7 +681,6 @@ statement { $$ = ast_stmt_create_iter_e($1); } | compound_verification_statement { $$ = ast_stmt_create_compound_v(lexloc(), $1); } - | allocation_statement ; labelled_statement @@ -714,15 +722,6 @@ compound_verification_statement | '~' '[' block ']' { $$ = $3; } ; -allocation_statement - : ALLOC postfix_expression ';' - { $$ = ast_stmt_create_alloc(lexloc(), $2); } - | DEALLOC postfix_expression ';' - { $$ = ast_stmt_create_dealloc(lexloc(), $2); } - | CLUMP postfix_expression ';' - { $$ = ast_stmt_create_clump(lexloc(), $2); } - ; - declaration_list : declaration { $$ = variable_array_create(ast_variable_create($1.name, $1.t)); } diff --git a/src/0v/ast/lex.l b/src/0v/ast/lex.l index ac18875..59eb06b 100644 --- a/src/0v/ast/lex.l +++ b/src/0v/ast/lex.l @@ -30,9 +30,9 @@ check_type(); %% "#" { preproc(); } "/*" { comment(); } -".alloc" { count(); return(ALLOC); } -".dealloc" { count(); return(DEALLOC); } -".clump" { count(); return(CLUMP); } +".malloc" { count(); return(TK_ALLOC); } +".free" { count(); return(TK_DEALLOC); } +".clump" { count(); return(TK_CLUMP); } "auto" { count(); return(AUTO); } "axiom" { count(); return(AXIOM); } diff --git a/src/0v/ast/stmt/stmt.c b/src/0v/ast/stmt/stmt.c index 625788d..331e192 100644 --- a/src/0v/ast/stmt/stmt.c +++ b/src/0v/ast/stmt/stmt.c @@ -238,98 +238,6 @@ ast_stmt_destroy_jump(struct ast_stmt *stmt) ast_expr_destroy(rv); } -struct ast_stmt * -ast_stmt_create_alloc(struct lexememarker *loc, struct ast_expr *arg) -{ - struct ast_stmt *stmt = ast_stmt_create(loc); - stmt->kind = STMT_ALLOCATION; - stmt->u.alloc.kind = ALLOC; - stmt->u.alloc.arg = arg; - return stmt; -} - -struct ast_stmt * -ast_stmt_create_dealloc(struct lexememarker *loc, struct ast_expr *arg) -{ - struct ast_stmt *stmt = ast_stmt_create(loc); - stmt->kind = STMT_ALLOCATION; - stmt->u.alloc.kind = DEALLOC; - stmt->u.alloc.arg = arg; - return stmt; -} - -struct ast_stmt * -ast_stmt_create_clump(struct lexememarker *loc, struct ast_expr *arg) -{ - struct ast_stmt *stmt = ast_stmt_create(loc); - stmt->kind = STMT_ALLOCATION; - stmt->u.alloc.kind= CLUMP; - stmt->u.alloc.arg = arg; - return stmt; -} - -static struct ast_stmt * -ast_stmt_copy_alloc(struct lexememarker *loc, struct ast_stmt *stmt) -{ - struct ast_expr *arg = ast_expr_copy(stmt->u.alloc.arg); - switch (stmt->u.alloc.kind) { - case ALLOC: - return ast_stmt_create_alloc(loc, arg); - case DEALLOC: - return ast_stmt_create_dealloc(loc, arg); - case CLUMP: - return ast_stmt_create_clump(loc, arg); - default: - assert(false); - } -} - -static void -ast_stmt_destroy_alloc(struct ast_stmt *stmt) -{ - assert(stmt->kind == STMT_ALLOCATION); - - ast_expr_destroy(stmt->u.alloc.arg); -} - -static void -ast_stmt_alloc_sprint(struct ast_stmt *stmt, struct strbuilder *b) -{ - assert(stmt->kind == STMT_ALLOCATION); - - char *arg = ast_expr_str(stmt->u.alloc.arg); - - switch (stmt->u.alloc.kind) { - case ALLOC: - strbuilder_printf(b, ".%s %s;", "alloc", arg); - break; - case DEALLOC: - strbuilder_printf(b, ".%s %s;", "dealloc", arg); - break; - case CLUMP: - strbuilder_printf(b, ".%s %s;", "clump", arg); - break; - default: - assert(false); - } - free(arg); -} - -struct ast_expr * -ast_stmt_alloc_arg(struct ast_stmt *stmt) -{ - assert(stmt->kind == STMT_ALLOCATION); - return stmt->u.alloc.arg; -} - -enum ast_alloc_kind -ast_stmt_alloc_kind(struct ast_stmt *stmt) -{ - assert(stmt->kind == STMT_ALLOCATION); - return stmt->u.alloc.kind; -} - - struct ast_stmt * ast_stmt_create_sel(struct lexememarker *loc, bool isswitch, struct ast_expr *cond, struct ast_stmt *body, struct ast_stmt *nest) @@ -570,9 +478,6 @@ ast_stmt_destroy(struct ast_stmt *stmt) case STMT_JUMP: ast_stmt_destroy_jump(stmt); break; - case STMT_ALLOCATION: - ast_stmt_destroy_alloc(stmt); - break; default: assert(false); break; @@ -634,8 +539,6 @@ ast_stmt_copy(struct ast_stmt *stmt) loc, stmt->u.jump.kind, ast_expr_copy_ifnotnull(stmt->u.jump.rv) ); - case STMT_ALLOCATION: - return ast_stmt_copy_alloc(loc, stmt); default: assert(false); } @@ -674,9 +577,6 @@ ast_stmt_str(struct ast_stmt *stmt) case STMT_JUMP: ast_stmt_jump_sprint(stmt, b); break; - case STMT_ALLOCATION: - ast_stmt_alloc_sprint(stmt, b); - break; default: assert(false); } @@ -752,8 +652,6 @@ ast_stmt_getfuncs(struct ast_stmt *stmt) return ast_stmt_iteration_getfuncs(stmt); case STMT_JUMP: return ast_expr_getfuncs(stmt->u.jump.rv); - case STMT_ALLOCATION: - return ast_expr_getfuncs(stmt->u.alloc.arg); default: assert(false); } @@ -780,7 +678,6 @@ ast_stmt_splits(struct ast_stmt *stmt, struct state *s) return (struct ast_stmt_splits) { .n = 0, .cond = NULL }; case STMT_LABELLED: return ast_stmt_splits(stmt->u.labelled.stmt, s); - case STMT_ALLOCATION: case STMT_ITERATION: case STMT_COMPOUND: case STMT_COMPOUND_V: diff --git a/src/0v/ast/stmt/stmt.h b/src/0v/ast/stmt/stmt.h index c855833..413f0c5 100644 --- a/src/0v/ast/stmt/stmt.h +++ b/src/0v/ast/stmt/stmt.h @@ -3,12 +3,6 @@ #include "util.h" -enum ast_alloc_kind { - ALLOC = 1 << 0, - DEALLOC = 1 << 1, - CLUMP = 1 << 2, -}; - enum ast_stmt_kind { STMT_NOP = 1 << 0, STMT_LABELLED = 1 << 1, diff --git a/src/0v/ast/stmt/verify.c b/src/0v/ast/stmt/verify.c index 3cf1532..b9ca2e8 100644 --- a/src/0v/ast/stmt/verify.c +++ b/src/0v/ast/stmt/verify.c @@ -313,7 +313,7 @@ static struct result * comp_absexec(struct ast_stmt *stmt, struct state *state, bool should_setup); static struct result * -alloc_absexec(struct ast_stmt *stmt, struct state *state); +jump_absexec(struct ast_stmt *, struct state *); struct result * ast_stmt_absexec(struct ast_stmt *stmt, struct state *state, bool should_setup) @@ -331,8 +331,8 @@ ast_stmt_absexec(struct ast_stmt *stmt, struct state *state, bool should_setup) return iter_absexec(stmt, state); case STMT_COMPOUND: return comp_absexec(stmt, state, should_setup); - case STMT_ALLOCATION: - return alloc_absexec(stmt, state); + case STMT_JUMP: + return jump_absexec(stmt, state); default: assert(false); } @@ -372,8 +372,6 @@ sel_absexec(struct ast_stmt *stmt, struct state *state, bool should_setup) struct decision sel_decide(struct ast_expr *control, struct state *state) { - /*printf("(sel_decide) state: %s\n", state_str(state));*/ - /*printf("(sel_decide) control: %s\n", ast_expr_str(control));*/ struct result *res = ast_expr_pf_reduce(control, state); if (result_iserror(res)) { return (struct decision) { .err = result_as_error(res) }; @@ -381,11 +379,8 @@ sel_decide(struct ast_expr *control, struct state *state) assert(result_hasvalue(res)); /* TODO: user error */ struct value *v = result_as_value(res); - /*printf("(sel_decide) value: %s\n", value_str(v));*/ if (value_issync(v)) { struct ast_expr *sync = value_as_sync(v); - /*printf("state: %s\n", state_str(state));*/ - /*printf("sync: %s\n", ast_expr_str(sync));*/ struct props *p = state_getprops(state); if (props_get(p, sync)) { return (struct decision) { .decision = true, .err = NULL }; @@ -423,91 +418,32 @@ sel_decide(struct ast_expr *control, struct state *state) return (struct decision) { .decision = nonzero, .err = NULL }; } -static struct ast_stmt * +static struct ast_expr * hack_alloc_from_neteffect(struct ast_stmt *); -static struct object * -hack_base_object_from_alloc(struct ast_stmt *, struct state *); - static struct result * iter_absexec(struct ast_stmt *stmt, struct state *state) { struct error *err; - struct ast_stmt *alloc = hack_alloc_from_neteffect(stmt); - - struct object *obj = hack_base_object_from_alloc(alloc, state); - - struct ast_expr *lw = ast_stmt_iter_lower_bound(stmt), + struct ast_expr *alloc = hack_alloc_from_neteffect(stmt), + *lw = ast_stmt_iter_lower_bound(stmt), *up = ast_stmt_iter_upper_bound(stmt); - struct result *result_lw = ast_expr_eval(lw, state), - *result_up = ast_expr_eval(up, state); - - if (result_iserror(result_lw)) { - return result_lw; - } - if (result_iserror(result_up)) { - return result_up; - } - - /*printf("stmt: %s\n", ast_stmt_str(stmt));*/ - /*printf("state: %s\n", state_str(state));*/ - - struct ast_expr *res_lw = value_to_expr(result_as_value(result_lw)), - *res_up = value_to_expr(result_as_value(result_up)); - - result_destroy(result_up); - result_destroy(result_lw); - - switch (ast_stmt_alloc_kind(alloc)) { - case ALLOC: - err = state_range_alloc(state, obj, res_lw, res_up); - break; - case DEALLOC: - err = state_range_dealloc(state, obj, res_lw, res_up); - break; - default: - assert(false); - } - - ast_expr_destroy(res_up); - ast_expr_destroy(res_lw); - - if (err) { + if ((err = ast_expr_alloc_rangeprocess(alloc, lw, up, state))) { return result_error_create(err); } - return result_value_create(NULL); } -static struct ast_stmt * +static struct ast_expr * hack_alloc_from_neteffect(struct ast_stmt *stmt) { struct ast_stmt *body = ast_stmt_iter_body(stmt); assert(ast_stmt_kind(body) == STMT_COMPOUND); struct ast_block *block = ast_stmt_as_block(body); assert(ast_block_ndecls(block) == 0 && ast_block_nstmts(block) == 1); - return ast_block_stmts(block)[0]; -} - -static struct object * -hack_base_object_from_alloc(struct ast_stmt *alloc, struct state *state) -{ - /* we're currently discarding analysis of `offset` and relying on the - * bounds (lower, upper beneath) alone */ - struct ast_expr *acc = ast_stmt_alloc_arg(alloc); /* `*(arr+offset)` */ - struct ast_expr *inner = ast_expr_unary_operand(acc); /* `arr+offset` */ - struct ast_expr *i = ast_expr_identifier_create(dynamic_str("i")); - assert(ast_expr_equal(ast_expr_binary_e2(inner), i)); - ast_expr_destroy(i); - struct lvalue_res res = ast_expr_lvalue(ast_expr_binary_e1(inner), state); - if (res.err) { - assert(false); - } - struct object *obj = lvalue_object(res.lval); - assert(obj); - return obj; + return ast_stmt_as_expr(ast_block_stmts(block)[0]); } static struct result * @@ -525,68 +461,15 @@ comp_absexec(struct ast_stmt *stmt, struct state *state, bool should_setup) } static struct result * -alloc_process(struct ast_stmt *, struct state *); - -static struct result * -alloc_absexec(struct ast_stmt *alloc, struct state *state) +jump_absexec(struct ast_stmt *stmt, struct state *state) { - struct result *res = alloc_process(alloc, state); - if (result_iserror(res)) { - return res; - } - if (result_hasvalue(res)) { - struct lvalue_res lval_res = ast_expr_lvalue(ast_stmt_alloc_arg(alloc), state); - if (lval_res.err) { - return result_error_create(lval_res.err); - } - struct object *obj = lvalue_object(lval_res.lval); - assert(obj); - object_assign(obj, value_copy(result_as_value(res))); - } - return res; -} - -static struct result * -dealloc_process(struct ast_stmt *, struct state *); - -/* operates at location level. It either creates an object on the heap and returns - * a location or gets the location pointed to by an lvalue and attempts to free - * possibly returning an error - * */ -static struct result * -alloc_process(struct ast_stmt *stmt, struct state *state) -{ - switch (ast_stmt_alloc_kind(stmt)) { - case ALLOC: - /* TODO: size needs to be passed in here when added to .alloc */ - return result_value_create(state_alloc(state)); - case DEALLOC: - return dealloc_process(stmt, state); - case CLUMP: - return result_value_create(state_clump(state)); - default: - assert(false); - } -} - -static struct result * -dealloc_process(struct ast_stmt *stmt, struct state *state) -{ - struct ast_expr *arg = ast_stmt_alloc_arg(stmt); - /* arg is pointing at the heap location we want to free, so we want its - * value rather than location */ - struct result *res = ast_expr_eval(arg, state); - if (result_iserror(res)) { - return res; - } - struct value *val = result_as_value(res); - assert(val); - struct error *err = state_dealloc(state, val); - if (err) { - return result_error_create(err); - } - value_destroy(val); - return result_value_create(NULL); + return ast_expr_absexec( + ast_expr_assignment_create( + ast_expr_identifier_create("return"), + ast_stmt_jump_rv(stmt) + ), + state + ); } static struct error * @@ -616,6 +499,7 @@ stmt_setupabsexec(struct ast_stmt *stmt, struct state *state) switch (ast_stmt_kind(stmt)) { case STMT_EXPR: case STMT_ALLOCATION: + case STMT_JUMP: return NULL; case STMT_LABELLED: return labelled_setupabsexec(stmt, state); diff --git a/src/0v/state/stack.c b/src/0v/state/stack.c index a6730ed..fc03073 100644 --- a/src/0v/state/stack.c +++ b/src/0v/state/stack.c @@ -35,7 +35,7 @@ stack_newblock(struct stack *stack) } struct stack * -stack_create(char *name, struct stack *prev, struct ast_type *result_type) +stack_create(char *name, struct stack *prev, struct ast_type *return_type) { struct stack *stack = calloc(1, sizeof(struct stack)); assert(stack); @@ -48,7 +48,7 @@ stack_create(char *name, struct stack *prev, struct ast_type *result_type) stack->prev = prev; stack->id = prev ? prev->id + 1 : 0; - stack->result = variable_create(result_type, stack, false); + stack->result = variable_create(return_type, stack, false); return stack; } @@ -144,7 +144,7 @@ stack_str(struct stack *stack, struct state *state) strbuilder_putc(b, '\n'); } char *result = variable_str(stack->result, stack, state); - strbuilder_printf(b, "\tresult: %s\n", result); + strbuilder_printf(b, "\treturn: %s\n", result); free(result); strbuilder_printf(b, "\t"); /* TODO: fix length of line */ @@ -213,7 +213,7 @@ stack_getvarmap(struct stack *s) struct variable * stack_getvariable(struct stack *s, char *id) { - assert(strcmp(id, KEYWORD_RESULT) != 0); + assert(strcmp(id, KEYWORD_RETURN) != 0); return map_get(s->varmap, id); } diff --git a/src/0v/state/state.c b/src/0v/state/state.c index 4fcba0a..67de036 100644 --- a/src/0v/state/state.c +++ b/src/0v/state/state.c @@ -333,7 +333,7 @@ state_getresulttype(struct state *state) struct ast_type * state_getobjecttype(struct state *state, char *id) { - if (strcmp(id, KEYWORD_RESULT) == 0) { + if (strcmp(id, KEYWORD_RETURN) == 0) { return state_getresulttype(state); } @@ -355,7 +355,7 @@ state_getloc(struct state *state, char *id) struct object * state_getobject(struct state *state, char *id) { - if (strcmp(id, KEYWORD_RESULT) == 0) { + if (strcmp(id, KEYWORD_RETURN) == 0) { return state_getresult(state); } diff --git a/tests/0-basic/030-alloc.x b/tests/0-basic/030-alloc.x index b5a140a..aaba6a5 100644 --- a/tests/0-basic/030-alloc.x +++ b/tests/0-basic/030-alloc.x @@ -1,7 +1,7 @@ #include void * -unit() ~ [ .alloc result; ] +unit() ~ [ return .malloc(1); ] { void *p; diff --git a/tests/0-basic/041-FAIL-false-alloc-void.x b/tests/0-basic/041-FAIL-false-alloc-void.x index 54ae322..f6c40d4 100644 --- a/tests/0-basic/041-FAIL-false-alloc-void.x +++ b/tests/0-basic/041-FAIL-false-alloc-void.x @@ -1,6 +1,6 @@ #include void * -unit() ~ [ .alloc result; ] +unit() ~ [ return .malloc(1); ] { } diff --git a/tests/0-basic/050-two-variables.x b/tests/0-basic/050-two-variables.x index d3758a0..f1af67f 100644 --- a/tests/0-basic/050-two-variables.x +++ b/tests/0-basic/050-two-variables.x @@ -1,7 +1,7 @@ #include void * -unit() ~ [ .alloc result; ] +unit() ~ [ return .malloc(1); ] { void *p; void *q; diff --git a/tests/0-basic/060-FAIL-two-allocs.x b/tests/0-basic/060-FAIL-two-allocs.x index f1c0164..2e7282e 100644 --- a/tests/0-basic/060-FAIL-two-allocs.x +++ b/tests/0-basic/060-FAIL-two-allocs.x @@ -1,7 +1,7 @@ #include void * -leak() ~ [ .alloc result; ] +leak() ~ [ return .malloc(1); ] { void *p; void *q; diff --git a/tests/0-basic/160-one-free-one-alloc.x b/tests/0-basic/160-one-free-one-alloc.x index 937a121..8080ab3 100644 --- a/tests/0-basic/160-one-free-one-alloc.x +++ b/tests/0-basic/160-one-free-one-alloc.x @@ -1,7 +1,7 @@ #include void * -unit() ~ [ .alloc result; ] +unit() ~ [ return .malloc(1); ] { void *p; void *q; diff --git a/tests/0-basic/180-FAIL-alloc-freed.x b/tests/0-basic/180-FAIL-alloc-freed.x index eb9adc4..abbe22f 100644 --- a/tests/0-basic/180-FAIL-alloc-freed.x +++ b/tests/0-basic/180-FAIL-alloc-freed.x @@ -1,7 +1,7 @@ #include void * -unit() ~ [ .alloc result; ] +unit() ~ [ return .malloc(1); ] { void *p; diff --git a/tests/0-basic/200-free-param.x b/tests/0-basic/200-free-param.x index 3529b6a..5857db8 100644 --- a/tests/0-basic/200-free-param.x +++ b/tests/0-basic/200-free-param.x @@ -2,9 +2,9 @@ void unit(void *p) ~ [ - pre: .alloc p; + pre: p = .malloc(1); - .dealloc p; + .free(p); ]{ free(p); } diff --git a/tests/1-branches/0100-body-trivial.x b/tests/1-branches/0100-body-trivial.x index 05e60b9..59d1014 100644 --- a/tests/1-branches/0100-body-trivial.x +++ b/tests/1-branches/0100-body-trivial.x @@ -1,7 +1,7 @@ #include void * -test(int x) ~ [ .alloc result; ] +test(int x) ~ [ return .malloc(1); ] { if (x) { return malloc(1); diff --git a/tests/1-branches/0200-conditional-allocation.x b/tests/1-branches/0200-conditional-allocation.x index 552989b..602d99c 100644 --- a/tests/1-branches/0200-conditional-allocation.x +++ b/tests/1-branches/0200-conditional-allocation.x @@ -3,10 +3,10 @@ void * test(int x) ~ [ if (x) { - .alloc result; + return .malloc(1); } if (!x) { - .alloc result; + return .malloc(1); } ]{ return malloc(1); diff --git a/tests/1-branches/0300-conditional-allocation-body.x b/tests/1-branches/0300-conditional-allocation-body.x index d809dfa..609b056 100644 --- a/tests/1-branches/0300-conditional-allocation-body.x +++ b/tests/1-branches/0300-conditional-allocation-body.x @@ -3,7 +3,7 @@ void * test(int x) ~ [ if (x) { - .alloc result; + return .malloc(1); } ]{ if (x) { diff --git a/tests/1-branches/0310-FAIL-conditional-allocation-body.x b/tests/1-branches/0310-FAIL-conditional-allocation-body.x index 2954f64..e720d94 100644 --- a/tests/1-branches/0310-FAIL-conditional-allocation-body.x +++ b/tests/1-branches/0310-FAIL-conditional-allocation-body.x @@ -3,7 +3,7 @@ void * test(int x) ~ [ if (x) { - .alloc result; + return .malloc(1); } ]{ return NULL; diff --git a/tests/1-branches/0320-FAIL-conditional-allocation-body.x b/tests/1-branches/0320-FAIL-conditional-allocation-body.x index 3a1ce9d..43b49bb 100644 --- a/tests/1-branches/0320-FAIL-conditional-allocation-body.x +++ b/tests/1-branches/0320-FAIL-conditional-allocation-body.x @@ -3,7 +3,7 @@ void * test(int x) ~ [ if (x) { - .alloc result; + return .malloc(1); } ]{ return malloc(1); diff --git a/tests/1-branches/0400-strcmp-conditional-allocation.x b/tests/1-branches/0400-strcmp-conditional-allocation.x index 133a60a..e660b5b 100644 --- a/tests/1-branches/0400-strcmp-conditional-allocation.x +++ b/tests/1-branches/0400-strcmp-conditional-allocation.x @@ -4,10 +4,10 @@ void * test(char *s) ~ [ if (strcmp(s, "yes") == 0) { - .alloc result; + return .malloc(1); } if (!(strcmp(s, "yes") == 0)) { - .alloc result; + return .malloc(1); } ]{ return malloc(1); diff --git a/tests/1-branches/0500-strcmp-conditional-allocation-body.x b/tests/1-branches/0500-strcmp-conditional-allocation-body.x index 015f8ef..a2e5c24 100644 --- a/tests/1-branches/0500-strcmp-conditional-allocation-body.x +++ b/tests/1-branches/0500-strcmp-conditional-allocation-body.x @@ -4,7 +4,7 @@ void * test(char *s) ~ [ if (strcmp(s, "yes") == 0) { - .alloc result; + return .malloc(1); } ]{ if (strcmp(s, "yes") == 0) { diff --git a/tests/1-branches/0600-indirect.x b/tests/1-branches/0600-indirect.x index fa9696d..a9f8f9f 100644 --- a/tests/1-branches/0600-indirect.x +++ b/tests/1-branches/0600-indirect.x @@ -3,7 +3,7 @@ void * alloc_if(int num) ~ [ if (num) { - .alloc result; + return .malloc(1); } ]{ if (num) { @@ -15,7 +15,7 @@ alloc_if(int num) ~ [ void * test(int x) ~ [ if (x) { - .alloc result; + return .malloc(1); } ]{ void *p; diff --git a/tests/1-branches/0700-indirect.x b/tests/1-branches/0700-indirect.x index cff2bb4..e8196eb 100644 --- a/tests/1-branches/0700-indirect.x +++ b/tests/1-branches/0700-indirect.x @@ -6,14 +6,14 @@ number(); void * alloc_if(int num) ~ [ if (num) { - .alloc result; + return .malloc(1); } ]; void * test() ~ [ if (number()) { - .alloc result; + return .malloc(1); } ]{ int num; diff --git a/tests/1-branches/0800-indirect.x b/tests/1-branches/0800-indirect.x index 18996ad..90f8e81 100644 --- a/tests/1-branches/0800-indirect.x +++ b/tests/1-branches/0800-indirect.x @@ -6,14 +6,14 @@ number(int param); void * alloc_if(int num) ~ [ if (num) { - .alloc result; + return .malloc(1); } ]; void * test(int x) ~ [ if (number(x)) { - .alloc result; + return .malloc(1); } ]{ int n; diff --git a/tests/1-branches/0900-subsequent.x b/tests/1-branches/0900-subsequent.x index c05d1bd..3111486 100644 --- a/tests/1-branches/0900-subsequent.x +++ b/tests/1-branches/0900-subsequent.x @@ -9,14 +9,14 @@ g(int param); void * alloc_if(int num) ~ [ if (num) { - .alloc result; + return .malloc(1); } ]; void * test(int x) ~ [ if (f(g(x))) { - .alloc result; + return .malloc(1); } ]{ int m; diff --git a/tests/1-branches/1000-chaining-functions.x b/tests/1-branches/1000-chaining-functions.x index ef5f5d4..ca015e0 100644 --- a/tests/1-branches/1000-chaining-functions.x +++ b/tests/1-branches/1000-chaining-functions.x @@ -4,15 +4,17 @@ struct tuple { int x; int y; }; struct tuple tuple_create() ~ [ - result.x = $; - result.y = $; + struct tuple t; + t.x = $; + t.y = $; + return t; ]; void * -conditional_alloc(int x) ~ [ if (x) .alloc result; ]; +conditional_alloc(int x) ~ [ if (x) { return .malloc(1); } ]; void * -test() ~ [ if (tuple_create().x) .alloc result; ] +test() ~ [ if (tuple_create().x) { return .malloc(1); } ] { struct tuple t; diff --git a/tests/3-topological/001-valid-sort-matrix.x b/tests/3-topological/001-valid-sort-matrix.x index b30ebe9..d9170ab 100644 --- a/tests/3-topological/001-valid-sort-matrix.x +++ b/tests/3-topological/001-valid-sort-matrix.x @@ -9,14 +9,16 @@ struct matrix { struct matrix * matrix_create(int rows, int cols) ~ [ int i; + struct matrix *m; - .alloc result; - .alloc result->data; - result->rows = rows; - result->cols = cols; - for (i = 0; i < result->rows; i++) { - .alloc result->data[i]; + m = .malloc(sizeof(struct matrix)); + m->rows = rows; + m->cols = cols; + m->data = .malloc(sizeof(int *) * rows); + for (i = 0; i < m->rows; i++) { + m->data[i] = .malloc(1); } + return m; ]; struct matrix * @@ -31,7 +33,7 @@ matrix_create(int rows, int cols) m->cols = cols; m->data = malloc(sizeof(int *) * rows); - for (i = 0; i < rows; i++) ~ [ .alloc m->data[i]; ] { + for (i = 0; i < m->rows; i++) ~ [ m->data[i] = .malloc(1); ] { m->data[i] = malloc(sizeof(int) * cols); } @@ -45,14 +47,14 @@ matrix_destroy(struct matrix *m) ~ [ pre: m = matrix_create($, $); for (i = 0; i < m->rows; i++) { - .dealloc m->data[i]; + .free(m->data[i]); } - .dealloc m->data; - .dealloc m; + .free(m->data); + .free(m); ]{ int i; - for (i = 0; i < m->rows; i++) ~ [ .dealloc m->data[i]; ] { + for (i = 0; i < m->rows; i++) ~ [ .free(m->data[i]); ] { free(m->data[i]); } free(m->data); @@ -62,19 +64,21 @@ matrix_destroy(struct matrix *m) ~ [ struct matrix * matrix_add(struct matrix *m1, struct matrix *m2) ~ [ int i; + struct matrix *sum; pre: { m1 = matrix_create($, $); m2 = matrix_create($, $); } - .alloc result; - .alloc result->data; - result->rows = m1->rows; - result->cols = m1->cols; - for (i = 0; i < result->rows; i++) { - .alloc result->data[i]; + sum = .malloc(sizeof(struct matrix)); + sum->rows = m1->rows; + sum->cols = m1->cols; + sum->data = .malloc(sizeof(int *) * rows); + for (i = 0; i < sum->rows; i++) { + sum->data[i] = .malloc(1); } + return sum; ]{ int i; int j; struct matrix *res; diff --git a/tests/3-topological/002-valid-sort-parse.x b/tests/3-topological/002-valid-sort-parse.x index 2092f4d..cfeba50 100644 --- a/tests/3-topological/002-valid-sort-parse.x +++ b/tests/3-topological/002-valid-sort-parse.x @@ -7,15 +7,23 @@ #define true 1 char * -read_file(char *path) ~ [ .alloc result; ]; +read_file(char *path) ~ [ return .malloc(1); ]; struct lexer; struct lexer * -parse(char *input); +parse(char *input) ~ [ + struct lexer *l; + l = lexer_create( + $, $, + $, malloc(1), + $, malloc(1) + ); + return l; +]; void -lexer_destroy(struct lexer *) ~ [ +lexer_destroy(struct lexer *l) ~ [ pre: { l = lexer_create( $, $, @@ -23,19 +31,18 @@ lexer_destroy(struct lexer *) ~ [ $, malloc(1) ); } - - .dealloc l->pattern; - .dealloc l->token; - .dealloc l; + .free(l->pattern); + .free(l->token); + .free(l); ]; void -lexer_print(struct lexer *) ~ [ +lexer_print(struct lexer *l) ~ [ pre: { l = lexer_create( - $, $, - $, malloc(1), - $, malloc(1) + .clump(1), .clump(1), + $, .malloc(1), + $, .malloc(1) ); } ]; @@ -101,11 +108,13 @@ struct lexer { struct lexer * lexer_create(char *pre, char *post, int npat, struct pattern *pattern, int ntok, struct token *token) ~ [ - .alloc result; - result->pre = pre; - result->post = post; - result->pattern = pattern; - result->token = token; + struct lexer *l; + l = .malloc(sizeof(struct lexer)); + l->pre = pre; + l->post = post; + l->pattern = pattern; + l->token = token; + return l; ]{ struct lexer *l; @@ -174,12 +183,14 @@ count_patterns(char *pos); struct defsresult parse_defs(char *input) ~ [ + struct defsresult res; if (beginsdefs(skipws(input))) { - .alloc result.pre; + res.pre = .malloc(1); } if (count_patterns(input)) { - .alloc result.pattern; + res.pattern = .malloc(1); } + return res; ]; int @@ -196,15 +207,17 @@ struct rulesresult { struct rulesresult parse_rules(char *pos) ~ [ - result.token = $; /* TODO: put in else block */ + struct rulesresult res; + res.token = $; /* TODO: put in else block */ if (count_tokens(pos)) { - .alloc result.token; + res.token = .malloc(sizeof(struct token)); } - result.pos = $; + res.pos = $; + return res; ]; char * -parse_toeof(char *input) ~ [ .alloc result; ]; +parse_toeof(char *input) ~ [ return .malloc(1); ]; struct lexer * parse(char *pos) @@ -233,7 +246,7 @@ parse(char *pos) } char * -substr(char *s, int n) ~ [ .alloc result; ] +substr(char *s, int n) ~ [ return .malloc(1); ] { int len; char *ss; @@ -260,7 +273,7 @@ skiplinespace(char *s) } char * -parse_id(char *input) ~ [ .alloc result; ]; +parse_id(char *input) ~ [ return .malloc(1); ]; char * skipoptions(char *pos) @@ -299,7 +312,7 @@ parse_id(char *input) } char * -parse_tonewline(char *input) ~ [ .alloc result; ] +parse_tonewline(char *input) ~ [ return .malloc(1); ] { char *s; s = input; /* must be here because not seen with skip loop hack */ @@ -316,11 +329,14 @@ struct stringresult { struct stringresult parse_defsraw(char *input) ~ [ - result.s = $; + struct stringresult res; + + res.s = $; if (beginsdefs(input)) { - .alloc result.s; + res.s = .malloc(1); } - result.pos = $; + res.pos = $; + return res; ]; struct patternet { @@ -331,12 +347,14 @@ struct patternet { struct patternet parse_defsproper(char *input) ~ [ - result.pattern = $; /* TODO: put in else block */ + struct patternet res; + res.pattern = $; /* TODO: put in else block */ if (count_patterns(input)) { - .alloc result.pattern; + res.pattern = .malloc(1); } - result.npat = $; - result.pos = $; + res.npat = $; + res.pos = $; + return res; ]; struct defsresult @@ -386,9 +404,11 @@ parse_defsraw(char *input) struct pattern * pattern_create(char *name, char *pattern) ~ [ - .alloc result; - result->name = name; - result->pattern = pattern; + struct pattern p; + p = .malloc(sizeof(struct pattern)); + p->name = name; + p->pattern = pattern; + return p; ]{ struct pattern *p; @@ -421,11 +441,13 @@ struct patternpos { struct patternpos parse_defs_n(char *pos, int npat) ~ [ - result.p = $; /* TODO: put in else block */ + struct patternpos res; + res.p = $; /* TODO: put in else block */ if (npat) { - .alloc result.p; + res.p = .malloc(1); } - result.pos = $; + res.pos = $; + return res; ]; struct patternet @@ -504,10 +526,12 @@ parse_defs_n(char *pos, int npat) struct token * token_create(int isliteral, char *name, char *action) ~ [ - .alloc result; - result->isliteral = isliteral; - result->name = name; - result->action = action; + struct token *tk; + tk = .malloc(sizeof(struct token)); + tk->isliteral = isliteral; + tk->name = name; + tk->action = action; + return tk; ]{ struct token *tk; @@ -538,11 +562,13 @@ struct tokenpos { struct tokenpos parse_rules_n(char *pos, int ntok) ~ [ - result.t = $; /* TODO: put in else block */ + struct tokenpos tpos; + tpos.t = $; /* TODO: put in else block */ if (ntok) { - .alloc result.t; + tpos.t = .malloc(1); } - result.pos = $; + tpos.pos = $; + return tpos; ]; struct rulesresult @@ -565,8 +591,10 @@ struct tokenresult { struct tokenresult parse_token(char *pos) ~ [ - result.tk = token_create($, malloc($), malloc($)); - result.pos = $; + struct tokenresult tres; + tres.tk = token_create($, malloc($), malloc($)); + tres.pos = $; + return tres; ]; int @@ -616,15 +644,19 @@ struct tknameresult { struct tknameresult parse_name(char *pos) ~ [ - .alloc result.name; - result.isliteral = $; - result.pos = $; + struct tknameresult res; + res.name = .malloc(1); + res.isliteral = $; + res.pos = $; + return res; ]; struct stringresult parse_action(char *input) ~ [ - .alloc result.s; - result.pos = $; + struct stringresult res; + res.s = .malloc(1); + res.pos = $; + return res; ]; struct tokenresult @@ -643,13 +675,25 @@ parse_token(char *pos) } struct tknameresult -parse_token_id(char *pos) ~ [ .alloc result.name; ]; +parse_token_id(char *pos) ~ [ + struct tknameresult res; + res.name = .malloc(1); + return res; +]; struct tknameresult -parse_token_literal(char *input) ~ [ .alloc result.name; ]; +parse_token_literal(char *input) ~ [ + struct tknameresult res; + res.name = .malloc(1); + return res; +]; struct tknameresult -parse_token_pattern(char *pos) ~ [ .alloc result.name; ]; +parse_token_pattern(char *pos) ~ [ + struct tknameresult res; + res.name = .malloc(1); + return res; +]; struct tknameresult parse_name(char *pos) diff --git a/tests/4-linking/000-internal-prototype-abstract.x b/tests/4-linking/000-internal-prototype-abstract.x index e240674..051e16b 100644 --- a/tests/4-linking/000-internal-prototype-abstract.x +++ b/tests/4-linking/000-internal-prototype-abstract.x @@ -1,7 +1,7 @@ #include void * -allocating() ~ [ .alloc result; ]; +allocating() ~ [ return .malloc(1); ]; int main() diff --git a/tests/4-linking/010-internal-two-matching-abstracts.x b/tests/4-linking/010-internal-two-matching-abstracts.x index 182ebb7..dc58a5f 100644 --- a/tests/4-linking/010-internal-two-matching-abstracts.x +++ b/tests/4-linking/010-internal-two-matching-abstracts.x @@ -1,7 +1,7 @@ #include void * -allocating() ~ [ .alloc result; ]; +allocating() ~ [ return .malloc(1); ]; int main() @@ -13,7 +13,7 @@ main() } void * -allocating() ~ [ .alloc result; ] +allocating() ~ [ return .malloc(1); ] { void *p; diff --git a/tests/4-linking/020-FAIL-internal-mismatch-abstracts.x b/tests/4-linking/020-FAIL-internal-mismatch-abstracts.x index a17ec0d..4a54c05 100644 --- a/tests/4-linking/020-FAIL-internal-mismatch-abstracts.x +++ b/tests/4-linking/020-FAIL-internal-mismatch-abstracts.x @@ -1,7 +1,7 @@ #include void * -allocating() ~ [ .dealloc result; ]; +allocating() ~ [ .free(result); ]; int main() @@ -13,7 +13,7 @@ main() } void * -allocating() ~ [ .alloc result; ] +allocating() ~ [ return .malloc(1); ] { void *p; diff --git a/tests/4-linking/030-FAIL-internal-missing-definition.x b/tests/4-linking/030-FAIL-internal-missing-definition.x index 07e2fb4..d6195f4 100644 --- a/tests/4-linking/030-FAIL-internal-missing-definition.x +++ b/tests/4-linking/030-FAIL-internal-missing-definition.x @@ -1,4 +1,4 @@ #include static void * -allocating() ~ [ .alloc result; ]; +allocating() ~ [ return .malloc(1); ]; diff --git a/tests/4-linking/040-FAIL-multiple-definitions.x b/tests/4-linking/040-FAIL-multiple-definitions.x index de0afc9..8df71d7 100644 --- a/tests/4-linking/040-FAIL-multiple-definitions.x +++ b/tests/4-linking/040-FAIL-multiple-definitions.x @@ -1,7 +1,7 @@ #include void * -allocating() ~ [ .alloc result; ]; +allocating() ~ [ return .malloc(1); ]; void * allocating() { } diff --git a/tests/4-linking/050-multiple-prototype.x b/tests/4-linking/050-multiple-prototype.x index 666a047..c980df2 100644 --- a/tests/4-linking/050-multiple-prototype.x +++ b/tests/4-linking/050-multiple-prototype.x @@ -1,7 +1,7 @@ #include void * -allocating() ~ [ .alloc result; ]; +allocating() ~ [ return .malloc(1); ]; void * nonallocating(); diff --git a/tests/5-pass-by-ptr/000-basic.x b/tests/5-pass-by-ptr/000-basic.x index ae1286a..d7f4269 100644 --- a/tests/5-pass-by-ptr/000-basic.x +++ b/tests/5-pass-by-ptr/000-basic.x @@ -2,7 +2,7 @@ void * assign(void *p) ~ [ - pre: .clump p; + pre: p = .clump(1); *p = 1; ] { /* TODO: internal verification */ diff --git a/tests/6-preconditions/100-FAIL-need-alloced-lval.x b/tests/6-preconditions/100-FAIL-need-alloced-lval.x index 8acb288..15dbdce 100644 --- a/tests/6-preconditions/100-FAIL-need-alloced-lval.x +++ b/tests/6-preconditions/100-FAIL-need-alloced-lval.x @@ -2,7 +2,7 @@ int func(int *x) ~ [ - pre: .alloc x; + pre: x = .malloc(1); *x = 5; ]{ *x = 5; diff --git a/tests/6-preconditions/101-FAIL-need-alloced-rval.x b/tests/6-preconditions/101-FAIL-need-alloced-rval.x index 70940dc..7d2dcb5 100644 --- a/tests/6-preconditions/101-FAIL-need-alloced-rval.x +++ b/tests/6-preconditions/101-FAIL-need-alloced-rval.x @@ -4,10 +4,10 @@ int func(int *x) ~ [ int i; pre: { - .alloc x; + x = .malloc(1); *x = $; } - result = *x; + return *x; ]{ return *x; } diff --git a/tests/6-preconditions/102-FAIL-need-clump-lval-freed-ptr.x b/tests/6-preconditions/102-FAIL-need-clump-lval-freed-ptr.x index 18d1838..2452545 100644 --- a/tests/6-preconditions/102-FAIL-need-clump-lval-freed-ptr.x +++ b/tests/6-preconditions/102-FAIL-need-clump-lval-freed-ptr.x @@ -2,7 +2,7 @@ int func(int *x) ~ [ - pre: .clump x; + pre: x = .clump(1); ]{ } diff --git a/tests/6-preconditions/103-FAIL-need-clump-lval-assigned-ptr.x b/tests/6-preconditions/103-FAIL-need-clump-lval-assigned-ptr.x index 43c2bbb..1f51feb 100644 --- a/tests/6-preconditions/103-FAIL-need-clump-lval-assigned-ptr.x +++ b/tests/6-preconditions/103-FAIL-need-clump-lval-assigned-ptr.x @@ -2,7 +2,7 @@ int func(int *x) ~ [ - pre: .clump x; + pre: x = .clump(1); ]{ } diff --git a/tests/7-use-after-free/000-read-and-write.x b/tests/7-use-after-free/000-read-and-write.x index 123a21d..e967999 100644 --- a/tests/7-use-after-free/000-read-and-write.x +++ b/tests/7-use-after-free/000-read-and-write.x @@ -1,10 +1,14 @@ #include int * -read_and_write_definite() ~ [ .alloc result; *result = 1; ] -{ +read_and_write_definite() ~ [ + int *p; + p = .malloc(sizeof(int)); + *p = 1; + return p; +] { int *p; - p = malloc(1); + p = malloc(sizeof(int)); *p = 1; return p; } diff --git a/tests/7-use-after-free/001-read-and-write-arbitrary.x b/tests/7-use-after-free/001-read-and-write-arbitrary.x index 96faf78..c2b423f 100644 --- a/tests/7-use-after-free/001-read-and-write-arbitrary.x +++ b/tests/7-use-after-free/001-read-and-write-arbitrary.x @@ -1,10 +1,14 @@ #include int * -read_and_write_arbitrary() ~ [ .alloc result; *result = $; ] -{ +read_and_write_arbitrary() ~ [ + int *p; + p = .malloc(sizeof(int)); + *p = 1; + return p; +] { int *p; - p = malloc(1); + p = malloc(sizeof(int)); *p = 1; return p; } diff --git a/tests/7-use-after-free/002-pass-in-lvalue.x b/tests/7-use-after-free/002-pass-in-lvalue.x index 1bc842d..44f24a6 100644 --- a/tests/7-use-after-free/002-pass-in-lvalue.x +++ b/tests/7-use-after-free/002-pass-in-lvalue.x @@ -1,7 +1,7 @@ void modify(int *q) ~ [ pre: { - .clump q; + q = .clump(sizeof(int)); } *q = 2; ] { diff --git a/tests/7-use-after-free/003-pass-in-rvalue.x b/tests/7-use-after-free/003-pass-in-rvalue.x index 7cb4379..935e5c4 100644 --- a/tests/7-use-after-free/003-pass-in-rvalue.x +++ b/tests/7-use-after-free/003-pass-in-rvalue.x @@ -1,7 +1,7 @@ -int +void assign(int *q) ~ [ pre: { - .clump q; /* no side effect */ + q = .clump(sizeof(int)); *q = $; } ] { diff --git a/tests/7-use-after-free/004-mix-multiple-levels.x b/tests/7-use-after-free/004-mix-multiple-levels.x index 20df13c..527f93d 100644 --- a/tests/7-use-after-free/004-mix-multiple-levels.x +++ b/tests/7-use-after-free/004-mix-multiple-levels.x @@ -1,10 +1,10 @@ int snapshot_and_change(int *arg) ~ [ pre: { - .clump arg; + arg = .clump(sizeof(int)); *arg = $; } - result = *arg; + return *arg; *arg = 3; ] { int j; @@ -17,9 +17,9 @@ void modify(int *p, int *q) ~ [ int i; pre: { - .clump p; + p = .clump(sizeof(int)); *p = $; - .clump q; + q = .clump(sizeof(int)); }; *q = 2; *p = 3; diff --git a/tests/7-use-after-free/005-conditions.x b/tests/7-use-after-free/005-conditions.x index 1fe3b5f..1a55a90 100644 --- a/tests/7-use-after-free/005-conditions.x +++ b/tests/7-use-after-free/005-conditions.x @@ -1,6 +1,6 @@ void modify0(int *q, int x) ~ [ - pre: .clump q; + pre: q = .clump(1); if (x) { *q = 1; } @@ -13,7 +13,7 @@ modify0(int *q, int x) ~ [ void modify1(int *q, int x) ~ [ if (x) { - pre: .clump q; + pre: q = .clump(1); *q = 2; } ] { diff --git a/tests/7-use-after-free/112-FAIL-freed-ptr-dangling-false-claim.x b/tests/7-use-after-free/112-FAIL-freed-ptr-dangling-false-claim.x index 6dafc62..1ac8886 100644 --- a/tests/7-use-after-free/112-FAIL-freed-ptr-dangling-false-claim.x +++ b/tests/7-use-after-free/112-FAIL-freed-ptr-dangling-false-claim.x @@ -1,7 +1,7 @@ #include int * -func() ~ [ result = $; ] /* ERROR: */ +func() ~ [ return $; ] /* ERROR: */ { int *p; p = malloc(1); diff --git a/tests/7-use-after-free/120-FAIL-freed-ptr-maybe-dangling-return.x b/tests/7-use-after-free/120-FAIL-freed-ptr-maybe-dangling-return.x index 2e46031..d7c0d68 100644 --- a/tests/7-use-after-free/120-FAIL-freed-ptr-maybe-dangling-return.x +++ b/tests/7-use-after-free/120-FAIL-freed-ptr-maybe-dangling-return.x @@ -2,10 +2,12 @@ int * func(int x) ~ [ - .alloc result; + int *p; + p = .malloc(sizeof(int)); if (x) { - .dealloc result; + .free(p); } + return p; ] { int *p; p = malloc(1); diff --git a/tests/7-use-after-free/120-FAIL-freed-ptr-maybe-dangling-return.x.EXPECTED b/tests/7-use-after-free/120-FAIL-freed-ptr-maybe-dangling-return.x.EXPECTED index dd8878a..cac5dfb 100644 --- a/tests/7-use-after-free/120-FAIL-freed-ptr-maybe-dangling-return.x.EXPECTED +++ b/tests/7-use-after-free/120-FAIL-freed-ptr-maybe-dangling-return.x.EXPECTED @@ -1 +1 @@ -7-use-after-free/120-FAIL-freed-ptr-maybe-dangling-return.x:32:8: cannot exec statement: undefined indirection (lvalue) +7-use-after-free/120-FAIL-freed-ptr-maybe-dangling-return.x:34:8: cannot exec statement: undefined indirection (lvalue) diff --git a/tests/7-use-after-free/210-FAIL-stack-frame-pop-dangling-assign.x b/tests/7-use-after-free/210-FAIL-stack-frame-pop-dangling-assign.x index 17f7270..bec3b25 100644 --- a/tests/7-use-after-free/210-FAIL-stack-frame-pop-dangling-assign.x +++ b/tests/7-use-after-free/210-FAIL-stack-frame-pop-dangling-assign.x @@ -4,7 +4,7 @@ void dangling_assign(int **i) ~ [ int j; - pre: { .clump i; } + pre: i = .clump(sizeof(int *)); j = 5; *i = &j; diff --git a/tests/7-use-after-free/300-FAIL-struct-free-ptr-dangling.x b/tests/7-use-after-free/300-FAIL-struct-free-ptr-dangling.x index fa2e9bd..980d6d4 100644 --- a/tests/7-use-after-free/300-FAIL-struct-free-ptr-dangling.x +++ b/tests/7-use-after-free/300-FAIL-struct-free-ptr-dangling.x @@ -13,10 +13,14 @@ struct score { struct score * create_score(char *subject, int grade) ~ [ - pre: .alloc subject; - .alloc result; - result->subject = subject; - result->grade = grade; + struct score *s; + + pre: subject = .malloc(sizeof(char *) * 100); + + s = .malloc(sizeof(struct score)); + s->subject = subject; + s->grade = grade; + return s; ] { struct score *s; s = malloc(sizeof(struct score)); diff --git a/tests/7-use-after-free/300-FAIL-struct-free-ptr-dangling.x.EXPECTED b/tests/7-use-after-free/300-FAIL-struct-free-ptr-dangling.x.EXPECTED index 7094401..57596c4 100644 --- a/tests/7-use-after-free/300-FAIL-struct-free-ptr-dangling.x.EXPECTED +++ b/tests/7-use-after-free/300-FAIL-struct-free-ptr-dangling.x.EXPECTED @@ -1 +1 @@ -7-use-after-free/300-FAIL-struct-free-ptr-dangling.x:41:18: cannot exec statement: parameter 's' must be lvalue +7-use-after-free/300-FAIL-struct-free-ptr-dangling.x:45:18: cannot exec statement: parameter 's' must be lvalue diff --git a/tests/7-use-after-free/400-FAIL-conditions-in-setup.x b/tests/7-use-after-free/400-FAIL-conditions-in-setup.x index bf759c4..8210488 100644 --- a/tests/7-use-after-free/400-FAIL-conditions-in-setup.x +++ b/tests/7-use-after-free/400-FAIL-conditions-in-setup.x @@ -1,6 +1,6 @@ void modify2(int *q, int x) ~ [ - pre: if (x) { .clump q; } /* ERROR: setup must be decidable */ + pre: if (x) { q = .clump(1); } /* ERROR: setup must be decidable */ if (x) { *q = 2; } diff --git a/tests/8-uninitialised-memory/004-FAIL-uninitialised-memory-rval.x b/tests/8-uninitialised-memory/004-FAIL-uninitialised-memory-rval.x index 746e9f8..05735c4 100644 --- a/tests/8-uninitialised-memory/004-FAIL-uninitialised-memory-rval.x +++ b/tests/8-uninitialised-memory/004-FAIL-uninitialised-memory-rval.x @@ -1,6 +1,6 @@ int func(int i) ~ [ - result = i; + return i; ]{ return i; } diff --git a/tests/99-program/000-matrix.x b/tests/99-program/000-matrix.x index d281f70..25349b8 100644 --- a/tests/99-program/000-matrix.x +++ b/tests/99-program/000-matrix.x @@ -8,15 +8,17 @@ struct matrix { struct matrix * matrix_create(int rows, int cold) ~ [ + struct matrix *m; int i; - .alloc result; - .alloc result->data; - result->rows = rows; - result->cols = cols; - for (i = 0; i < result->rows; i++) { - .alloc result->data[i]; + m = .malloc(sizeof(struct matrix)); + m->data = .malloc(sizeof(int *) * rows); + m->rows = rows; + m->cols = cols; + for (i = 0; i < m->rows; i++) { + m->data[i] = .malloc(sizeof(int)); } + return m; ]; struct matrix * @@ -31,10 +33,9 @@ matrix_create(int rows, int cols) m->cols = cols; m->data = malloc(sizeof(int *) * rows); - for (i = 0; i < rows; i++) ~ [ .alloc m->data[i]; ] { - m->data[i] = malloc(sizeof(int) * cols); + for (i = 0; i < rows; i++) ~ [ m->data[i] = .malloc(sizeof(int *) * cols); ] { + m->data[i] = malloc(sizeof(int *) * cols); } - return m; } @@ -45,14 +46,14 @@ matrix_destroy(struct matrix *m) ~ [ pre: m = matrix_create($, $); for (i = 0; i < m->rows; i++) { - .dealloc m->data[i]; + .free(m->data[i]); } - .dealloc m->data; - .dealloc m; + .free(m->data); + .free(m); ]{ int i; - for (i = 0; i < m->rows; i++) ~ [ .dealloc m->data[i]; ] { + for (i = 0; i < m->rows; i++) ~ [ .free(m->data[i]); ] { free(m->data[i]); } free(m->data); @@ -62,19 +63,21 @@ matrix_destroy(struct matrix *m) ~ [ struct matrix * matrix_add(struct matrix *m1, struct matrix *m2) ~ [ int i; + struct matrix *sum; pre: { m1 = matrix_create($, $); m2 = matrix_create($, $); } - .alloc result; - .alloc result->data; - result->rows = m1->rows; - result->cols = m1->cols; - for (i = 0; i < result->rows; i++) { - .alloc result->data[i]; + sum = .malloc(sizeof(struct matrix *)); + sum->data = .malloc(sizeof(int *) * m1->rows); + sum->rows = m1->rows; + sum->cols = m1->cols; + for (i = 0; i < sum->rows; i++) { + sum->data[i] = .malloc(sizeof(int)); } + return sum; ]{ int i; int j; struct matrix *res; diff --git a/tests/99-program/100-lex/parse.x b/tests/99-program/100-lex/parse.x index de9eaef..ef7f250 100644 --- a/tests/99-program/100-lex/parse.x +++ b/tests/99-program/100-lex/parse.x @@ -8,7 +8,7 @@ #define true 1 char * -read_file(char *path) ~ [ .alloc result; ]; +read_file(char *path) ~ [ .malloc result; ]; struct lexer { char *pre; char *post; @@ -19,7 +19,7 @@ struct lexer { struct lexer * lexer_create(char *pre, char *post, int npat, struct pattern *pattern, int ntok, struct token *token) ~ [ - .alloc result; + .malloc result; result->pre = pre; result->post = post; result->pattern = pattern; @@ -40,7 +40,7 @@ struct stringresult { struct stringresult parse_defsraw(char *input) ~ [ - .alloc result.s; + .malloc result.s; result.pos = $; ]; @@ -57,7 +57,7 @@ parse(char *pos) ~ [ pattern = malloc(1); token = malloc(1); post = malloc(1); - result = lexer_create(pre, post, $, pattern, $, token); + return .exer_create(pre, post, $, pattern, $, token); ]; void @@ -70,11 +70,11 @@ lexer_destroy(struct lexer *l) ~ [ ); } - .dealloc l->pre; - .dealloc l->post; - .dealloc l->pattern; - .dealloc l->token; - .dealloc l; + .free l->pre; + .free l->post; + .free l->pattern; + .free l->token; + .free l; ]; void @@ -219,8 +219,8 @@ count_patterns(char *pos); struct defsresult parse_defs(char *pos) ~ [ - .alloc result.pre; - .alloc result.pattern; + .malloc result.pre; + .malloc result.pattern; result.pos = $; result.npat = $; ]; @@ -242,13 +242,13 @@ count_tokens(char *pos); struct rulesresult parse_rules(char *pos) ~ [ - .alloc result.token; + .malloc result.token; result.pos = $; result.ntok = $; ]; char * -parse_toeof(char *input) ~ [ .alloc result; ]; +parse_toeof(char *input) ~ [ .malloc result; ]; struct lexer * parse(char *pos) @@ -278,7 +278,7 @@ isboundary(char *s) } char * -substr(char *s, int n) ~ [ .alloc result; ] +substr(char *s, int n) ~ [ .malloc result; ] { int len; char *ss; @@ -305,7 +305,7 @@ skiplinespace(char *s) } char * -parse_id(char *input) ~ [ .alloc result; ]; +parse_id(char *input) ~ [ .malloc result; ]; char * skipoptions(char *pos) @@ -345,7 +345,7 @@ parse_id(char *input) } char * -parse_tonewline(char *input) ~ [ .alloc result; ] +parse_tonewline(char *input) ~ [ .malloc result; ] { char *s; s = input; /* must be here because not seen with skip loop hack */ @@ -363,7 +363,7 @@ struct patternet { struct patternet parse_defsproper(char *input) ~ [ - .alloc result.pattern; + .malloc result.pattern; result.npat = $; result.pos = $; ]; @@ -414,7 +414,7 @@ parse_defsraw(char *input) struct pattern * pattern_create(char *name, char *pattern) ~ [ - .alloc result; + .malloc result; result->name = name; result->pattern = pattern; ]{ @@ -449,7 +449,7 @@ struct patternpos { struct patternpos parse_defs_n(char *pos, int npat) ~ [ - .alloc result.p; + .malloc result.p; result.pos = $; ]; @@ -534,7 +534,7 @@ parse_defs_n(char *pos, int npat) struct token * token_create(int isliteral, char *name, char *action) ~ [ - .alloc result; + .malloc result; result->isliteral = isliteral; result->name = name; result->action = action; @@ -565,7 +565,7 @@ struct tokenpos { struct tokenpos parse_rules_n(char *pos, int ntok) ~ [ - .alloc result.t; + .malloc result.t; result.pos = $; ]; @@ -645,14 +645,14 @@ struct tknameresult { struct tknameresult parse_name(char *pos) ~ [ - .alloc result.name; + .malloc result.name; result.isliteral = $; result.pos = $; ]; struct stringresult parse_action(char *input) ~ [ - .alloc result.s; + .malloc result.s; result.pos = $; ]; @@ -672,13 +672,13 @@ parse_token(char *pos) } struct tknameresult -parse_token_id(char *pos) ~ [ .alloc result.name; ]; +parse_token_id(char *pos) ~ [ .malloc result.name; ]; struct tknameresult -parse_token_literal(char *input) ~ [ .alloc result.name; ]; +parse_token_literal(char *input) ~ [ .malloc result.name; ]; struct tknameresult -parse_token_pattern(char *pos) ~ [ .alloc result.name; ]; +parse_token_pattern(char *pos) ~ [ .malloc result.name; ]; struct tknameresult parse_name(char *pos) -- 2.45.2