diff options
-rw-r--r-- | allocator.c | 2 | ||||
-rw-r--r-- | cgen.c | 14 | ||||
-rw-r--r-- | eval.c | 15 | ||||
-rw-r--r-- | main.c | 3 | ||||
-rw-r--r-- | types.c | 23 |
5 files changed, 34 insertions, 23 deletions
diff --git a/allocator.c b/allocator.c index 98005ef..6573914 100644 --- a/allocator.c +++ b/allocator.c @@ -25,7 +25,7 @@ static void *err_malloc(size_t bytes); static void *err_calloc(size_t n, size_t sz); static void *err_realloc(void *prev, size_t new_size); #ifdef TOC_DEBUG -#define NO_ALLOCATOR 1 /* useful for debugging; valgrind checks writing past the end of a malloc, but that won't work with an allocator */ +#define NO_ALLOCATOR 0 /* useful for debugging; valgrind checks writing past the end of a malloc, but that won't work with an allocator */ #endif /* number of bytes a page hold, not including the header */ #define PAGE_BYTES (16384 - sizeof(Page)) @@ -1342,8 +1342,9 @@ static void cgen_fn(CGenerator *g, FnExpr *f) { return; FnExpr *prev_fn = g->fn; Block *prev_block = g->block; + Block *body = &f->body; g->fn = f; - g->block = &f->body; + g->block = body; cgen_fn_header(g, f); cgen_write(g, " {"); cgen_nl(g); @@ -1394,8 +1395,9 @@ static void cgen_fn(CGenerator *g, FnExpr *f) { cgen_decl(g, d); } - cgen_block(g, &f->body, CGEN_BLOCK_NOBRACES); - cgen_deferred_from_block(g, &f->body); + cgen_block(g, body, CGEN_BLOCK_NOBRACES); + cgen_deferred_from_block(g, body); + arr_clear(body->deferred); if (f->ret_decls) cgen_ret(g, NULL, NULL); /* for example, if function is fn() x: int, we need to generate return x; at the end */ cgen_writeln(g, "}"); g->block = prev_block; @@ -1661,6 +1663,7 @@ static void cgen_stmt(CGenerator *g, Statement *s) { } break; case STMT_FOR: { For *fo = s->for_; + Block *body = &fo->body; ForFlags flags = fo->flags; U32 is_range = flags & FOR_IS_RANGE; Declaration *header_decl = &fo->header; @@ -1843,8 +1846,9 @@ static void cgen_stmt(CGenerator *g, Statement *s) { } } } - cgen_block(g, &fo->body, CGEN_BLOCK_NOBRACES); - cgen_deferred_from_block(g, &fo->body); + cgen_block(g, body, CGEN_BLOCK_NOBRACES); + cgen_deferred_from_block(g, body); + arr_clear(body->deferred); cgen_write(g, "}}"); if (fo->body.c.break_lbl) { cgen_lbl(g, fo->body.c.break_lbl); @@ -735,7 +735,7 @@ static Status eval_ptr_to_struct_field(Evaluator *ev, Expression *dot_expr, void if (is_ptr) { struc_data = struc.ptr; if (struc_data == NULL) { - err_print(dot_expr->where, "Attempt to dereference NULL pointer."); + err_print(dot_expr->where, "Attempt to dereference null pointer."); return false; } } else { @@ -986,14 +986,14 @@ static void eval_numerical_bin_op(Value lhs, Type *lhs_type, BinaryOp op, Value } } -static Value val_zero(Type *t) { +static Value val_zero(Allocator *a, Type *t) { Value val = {0}; switch (t->kind) { case TYPE_STRUCT: - val.struc = err_calloc(1, compiler_sizeof(t)); + val.struc = allocr_calloc(a, 1, compiler_sizeof(t)); break; case TYPE_ARR: - val.arr = err_calloc((size_t)t->arr.n, compiler_sizeof(t->arr.of)); + val.arr = allocr_calloc(a, (size_t)t->arr.n, compiler_sizeof(t->arr.of)); break; default: break; @@ -1095,7 +1095,8 @@ static Status eval_expr(Evaluator *ev, Expression *e, Value *v) { switch (e->kind) { case EXPR_UNARY_OP: { Value of; - if (!eval_expr(ev, e->unary.of, &of)) return false; + if (e->unary.op != UNARY_ADDRESS) + if (!eval_expr(ev, e->unary.of, &of)) return false; switch (e->unary.op) { case UNARY_ADDRESS: { Expression *o = e->unary.of; @@ -1334,7 +1335,7 @@ static Status eval_expr(Evaluator *ev, Expression *e, Value *v) { if (d->flags & DECL_HAS_EXPR) { *ival = d->type.kind == TYPE_TUPLE ? ret_decl_val.tuple[idx] : ret_decl_val; } else { - *ival = val_zero(type); + *ival = val_zero(NULL, type); } ++idx; } @@ -1476,7 +1477,7 @@ static Status eval_decl(Evaluator *ev, Declaration *d) { *ival = v; } } else { - *ival = val_zero(type); + *ival = val_zero(NULL, type); } } ++index; @@ -8,6 +8,9 @@ /* @TODO: +make slice a pointer field of Value +make arr a pointer field of Type +- start passing Types instead of pointers to Type figure out how printf is gonna work if we do #include "foo.toc", bar; and foo.toc fails, bar should be declared as TYPE_UNKNOWN (right now it's undeclared) fix #foreign not at global scope - right now the cgen'd definition doesn't use the proper type @@ -12,7 +12,7 @@ static void val_cast(Value vin, Type *from, Value *vout, Type *to); static U64 val_to_u64(Value v, BuiltinType v_type); static I64 val_to_i64(Value v, BuiltinType v_type); static bool val_truthiness(Value v, Type *t); -static Value val_zero(Type *t); +static Value val_zero(Allocator *a, Type *t); static Status eval_stmt(Evaluator *ev, Statement *stmt); static Status struct_resolve(Typer *tr, StructDef *s); static Status expr_must_usable(Typer *tr, Expression *e); @@ -1750,11 +1750,12 @@ static Status types_expr(Typer *tr, Expression *e) { e->flags = 0; e->binary.op = BINARY_DOT; e->binary.lhs = used; - e->binary.rhs = typer_calloc(tr, 1, sizeof *e->binary.rhs); - e->binary.rhs->kind = EXPR_IDENT; - e->binary.rhs->flags = 0; - e->binary.rhs->ident_str.str = i_str; - e->binary.rhs->ident_str.len = i_len; + Expression *rhs = e->binary.rhs = typer_calloc(tr, 1, sizeof *e->binary.rhs); + rhs->where = e->where; + rhs->kind = EXPR_IDENT; + rhs->flags = 0; + rhs->ident_str.str = i_str; + rhs->ident_str.len = i_len; /* re-type */ if (!types_expr(tr, e)) return false; @@ -2366,7 +2367,7 @@ static Status types_expr(Typer *tr, Expression *e) { copy_val(tr->allocr, &varg->val, arg->val, varg->type); } else { /* use zero value everywhere */ - varg->val = val_zero(varg->type); + varg->val = val_zero(tr->allocr, varg->type); } } } else if (is_const) { @@ -2845,6 +2846,7 @@ static Status types_expr(Typer *tr, Expression *e) { /* foo.baz => (foo.bar).baz */ Expression *old_lhs = lhs; lhs = e->binary.lhs = typer_malloc(tr, sizeof *lhs); + lhs->where = old_lhs->where; lhs->kind = EXPR_BINARY_OP; lhs->flags = 0; lhs->binary.op = BINARY_DOT; @@ -2852,6 +2854,7 @@ static Status types_expr(Typer *tr, Expression *e) { Expression *middle = lhs->binary.rhs = typer_calloc(tr, 1, sizeof *lhs->binary.rhs); middle->kind = EXPR_IDENT; middle->flags = 0; + middle->where = e->where; assert(arr_len(uf->use_decl->idents) == 1); middle->ident_str = ident_to_string(uf->use_decl->idents[0]); e->flags &= (ExprFlags)~(ExprFlags)EXPR_FOUND_TYPE; @@ -3146,13 +3149,13 @@ static Status types_decl(Typer *tr, Declaration *d) { } else if (!tr->block || tr->block->kind == BLOCK_NMS) { /* give global variables without initializers a value stack */ Value *val = typer_malloc(tr, sizeof *val); - arr_add(d->val_stack, val); + arr_adda(d->val_stack, val, tr->allocr); /* arr_adda because this will never be freed */ if (n_idents > 1 && dtype->kind != TYPE_TUPLE) { Value *tuple = val->tuple = typer_malloc(tr, n_idents * sizeof *tuple); for (size_t i = 0; i < n_idents; ++i) - tuple[i] = val_zero(dtype); + tuple[i] = val_zero(tr->allocr, dtype); } else { - *val = val_zero(dtype); + *val = val_zero(tr->allocr, dtype); } } |