From bc009f5a471e632470861820c74c7a3bbac06961 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Tue, 10 Dec 2019 10:35:14 -0500 Subject: made Expression smaller --- cgen.c | 19 ++++++++++--------- copy.c | 8 +++++--- decls_cgen.c | 22 +++++++++++----------- eval.c | 4 ++-- parse.c | 8 ++++---- scope.c | 4 ++-- tokenizer.c | 1 + typedefs_cgen.c | 4 ++-- types.c | 20 ++++++++++---------- types.h | 4 ++-- 10 files changed, 49 insertions(+), 45 deletions(-) diff --git a/cgen.c b/cgen.c index f2f8fd4..0cd5536 100644 --- a/cgen.c +++ b/cgen.c @@ -105,7 +105,7 @@ static bool cgen_defs_decl(CGenerator *g, Declaration *d); return false; \ break; \ case EXPR_EACH: { \ - EachExpr *ea = &e->each; \ + EachExpr *ea = e->each; \ if (!each_enter(e)) return false; \ if (ea->flags & EACH_IS_RANGE) { \ if (!f(g, ea->range.from)) \ @@ -133,7 +133,7 @@ static bool cgen_defs_decl(CGenerator *g, Declaration *d); return false; \ break; \ case EXPR_FN: { \ - FnExpr *fn = &e->fn; \ + FnExpr *fn = e->fn; \ if (e->type.fn.constness) { \ Instance **data = fn->instances.data; \ for (U64 i = 0; i < fn->instances.cap; ++i) { \ @@ -692,7 +692,7 @@ static bool cgen_set_tuple(CGenerator *g, Expression *exprs, Identifier *idents, prefix_id = to->while_.c.id; goto prefixed; case EXPR_EACH: - prefix_id = to->each.c.id; + prefix_id = to->each->c.id; goto prefixed; prefixed: for (unsigned long i = 0; i < (unsigned long)arr_len(to->type.tuple); ++i) { @@ -803,7 +803,7 @@ static bool cgen_expr_pre(CGenerator *g, Expression *e) { return false; } break; case EXPR_EACH: { - EachExpr *ea = &e->each; + EachExpr *ea = e->each; int is_range = ea->flags & EACH_IS_RANGE; if (is_range) { if (!cgen_expr_pre(g, ea->range.from)) return false; @@ -1174,7 +1174,8 @@ static bool cgen_expr(CGenerator *g, Expression *e) { Expression fn_expr; fn_expr.kind = EXPR_FN; - fn_expr.fn = *fn; + fn_expr.fn = allocr_malloc(g->allocr, sizeof *fn_expr.fn); + *fn_expr.fn = *fn; fn_expr.flags = EXPR_FOUND_TYPE; fn_expr.type = *decl_type_at_index(d, index); @@ -1363,7 +1364,7 @@ static bool cgen_expr(CGenerator *g, Expression *e) { break; case EXPR_EACH: if (e->type.kind != TYPE_VOID) - cgen_ident_id(g, e->each.c.id); + cgen_ident_id(g, e->each->c.id); break; case EXPR_CALL: if (cgen_uses_ptr(&e->type)) { @@ -1435,7 +1436,7 @@ static bool cgen_expr(CGenerator *g, Expression *e) { assert(0); break; case EXPR_FN: { - FnExpr *f = &e->fn; + FnExpr *f = e->fn; cgen_fn_name(g, f); } break; case EXPR_SLICE: @@ -1870,7 +1871,7 @@ static bool cgen_stmt(CGenerator *g, Statement *s) { static bool cgen_defs_expr(CGenerator *g, Expression *e) { if (e->kind == EXPR_FN) { - FnExpr *f = &e->fn; + FnExpr *f = e->fn; FnType *fn_type = &e->type.fn; bool any_const = false; if (fn_type->constness) { @@ -1892,7 +1893,7 @@ static bool cgen_defs_expr(CGenerator *g, Expression *e) { } } if (!any_const) { - if (!cgen_fn(g, &e->fn, e->where, 0, NULL)) + if (!cgen_fn(g, e->fn, e->where, 0, NULL)) return false; } diff --git a/copy.c b/copy.c index 7ccc0a0..e6e9e9f 100644 --- a/copy.c +++ b/copy.c @@ -185,8 +185,10 @@ static void copy_expr(Copier *c, Expression *out, Expression *in) { copy_block(c, &wout->body, &win->body); } break; case EXPR_EACH: { - EachExpr *ein = &in->each; - EachExpr *eout = &out->each; + EachExpr *ein = in->each; + EachExpr *eout = allocr_malloc(a, sizeof *eout); + out->each = eout; + *eout = *ein; if (ein->flags & EACH_ANNOTATED_TYPE) copy_type(c, &eout->type, &ein->type); if (ein->flags & EACH_IS_RANGE) { @@ -201,7 +203,7 @@ static void copy_expr(Copier *c, Expression *out, Expression *in) { copy_block(c, &eout->body, &ein->body); } break; case EXPR_FN: - copy_fn_expr(c, &out->fn, &in->fn, true); + copy_fn_expr(c, out->fn = allocr_malloc(a, sizeof *out->fn), in->fn, true); break; case EXPR_CAST: { CastExpr *cin = &in->cast; diff --git a/decls_cgen.c b/decls_cgen.c index 7288e70..d0923cc 100644 --- a/decls_cgen.c +++ b/decls_cgen.c @@ -9,7 +9,7 @@ static bool cgen_decls_decl(CGenerator *g, Declaration *d); static bool cgen_decls_fn_instances(CGenerator *g, Expression *e) { assert(e->kind == EXPR_FN); - FnExpr *f = &e->fn; + FnExpr *f = e->fn; assert(e->type.fn.constness); Instance **data = f->instances.data; for (U64 i = 0; i < f->instances.cap; ++i) { @@ -33,7 +33,7 @@ static bool cgen_decls_expr(CGenerator *g, Expression *e) { cgen_recurse_subexprs(g, e, cgen_decls_expr, cgen_decls_block, cgen_decls_decl); switch (e->kind) { case EXPR_FN: { - FnExpr *f = &e->fn; + FnExpr *f = e->fn; f->c.name = NULL; if (!f->c.id) f->c.id = g->ident_counter++; @@ -42,13 +42,13 @@ static bool cgen_decls_expr(CGenerator *g, Expression *e) { if (!cgen_decls_fn_instances(g, e)) return false; } else { - if (cgen_should_gen_fn(&e->fn)) { - fn_enter(&e->fn, 0); - if (!cgen_fn_header(g, &e->fn, e->where, 0, 0)) + if (cgen_should_gen_fn(e->fn)) { + fn_enter(e->fn, 0); + if (!cgen_fn_header(g, e->fn, e->where, 0, 0)) return false; cgen_write(g, ";"); cgen_nl(g); - fn_exit(&e->fn); + fn_exit(e->fn); } } } break; @@ -103,18 +103,18 @@ static bool cgen_decls_block(CGenerator *g, Block *b) { static bool cgen_decls_decl(CGenerator *g, Declaration *d) { if (cgen_fn_is_direct(g, d)) { - d->expr.fn.c.name = d->idents[0]; + d->expr.fn->c.name = d->idents[0]; if (d->expr.type.fn.constness) { if (!cgen_decls_fn_instances(g, &d->expr)) return false; } else { - if (cgen_should_gen_fn(&d->expr.fn)) { - fn_enter(&d->expr.fn, 0); - if (!cgen_fn_header(g, &d->expr.fn, d->where, 0, 0)) + if (cgen_should_gen_fn(d->expr.fn)) { + fn_enter(d->expr.fn, 0); + if (!cgen_fn_header(g, d->expr.fn, d->where, 0, 0)) return false; cgen_write(g, ";"); cgen_nl(g); - fn_exit(&d->expr.fn); + fn_exit(d->expr.fn); } } cgen_recurse_subexprs(g, (&d->expr), cgen_decls_expr, cgen_decls_block, cgen_decls_decl); diff --git a/eval.c b/eval.c index 2f4a8bc..65ada12 100644 --- a/eval.c +++ b/eval.c @@ -1180,7 +1180,7 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) { } } break; case EXPR_EACH: { - EachExpr *ea = &e->each; + EachExpr *ea = e->each; if (ea->flags & EACH_IS_RANGE) { Value from, to; Value stepval; @@ -1316,7 +1316,7 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) { val_cast(&casted, &e->cast.expr->type, v, &e->cast.type); } break; case EXPR_FN: - v->fn = &e->fn; + v->fn = e->fn; break; case EXPR_IDENT: { IdentDecl *idecl = ident_decl(e->ident); diff --git a/parse.c b/parse.c index ef49dc9..1aa8c1a 100644 --- a/parse.c +++ b/parse.c @@ -1015,7 +1015,7 @@ static bool parse_expr(Parser *p, Expression *e, Token *end) { case KW_FN: { /* this is a function */ e->kind = EXPR_FN; - if (!parse_fn_expr(p, &e->fn)) + if (!parse_fn_expr(p, e->fn = parser_malloc(p, sizeof *e->fn))) return false; if (t->token != end) { @@ -1109,7 +1109,7 @@ static bool parse_expr(Parser *p, Expression *e, Token *end) { } case KW_EACH: { e->kind = EXPR_EACH; - EachExpr *ea = &e->each; + EachExpr *ea = e->each = parser_malloc(p, sizeof *ea); ea->flags = 0; ea->value = NULL; ea->index = NULL; @@ -2061,7 +2061,7 @@ static void fprint_expr(FILE *out, Expression *e) { fprintf(out, ")"); break; case EXPR_FN: - fprint_fn_expr(out, &e->fn); + fprint_fn_expr(out, e->fn); break; case EXPR_CAST: fprintf(out, "cast("); @@ -2091,7 +2091,7 @@ static void fprint_expr(FILE *out, Expression *e) { fprint_block(out, &e->while_.body); break; case EXPR_EACH: { - EachExpr *ea = &e->each; + EachExpr *ea = e->each; fprintf(out, "each "); if (ea->index) { fprint_ident(out, ea->index); diff --git a/scope.c b/scope.c index 2fc6041..4fd9df9 100644 --- a/scope.c +++ b/scope.c @@ -98,7 +98,7 @@ static void fn_exit(FnExpr *f) { static bool each_enter(Expression *e) { assert(e->kind == EXPR_EACH); - EachExpr *ea = &e->each; + EachExpr *ea = e->each; if (ea->index && ea->index == ea->value) { err_print(e->where, "The identifier for the index of an each loop must be different from the identifier for the value."); return false; @@ -122,7 +122,7 @@ static bool each_enter(Expression *e) { static void each_exit(Expression *e) { assert(e->kind == EXPR_EACH); - EachExpr *ea = &e->each; + EachExpr *ea = e->each; if (ea->index) { arr_remove_last(&ea->index->decls); } diff --git a/tokenizer.c b/tokenizer.c index a4037ad..90b37d3 100644 --- a/tokenizer.c +++ b/tokenizer.c @@ -218,6 +218,7 @@ static Token *tokr_add(Tokenizer *t) { static bool tokenize_string(Tokenizer *t, char *str) { int has_err = 0; t->s = str; + t->err_ctx->str = str; t->line = 1; while (1) { if (*t->s == 0) break; diff --git a/typedefs_cgen.c b/typedefs_cgen.c index 3896686..cfba024 100644 --- a/typedefs_cgen.c +++ b/typedefs_cgen.c @@ -24,7 +24,7 @@ static bool typedefs_expr(CGenerator *g, Expression *e) { cgen_recurse_subexprs(g, e, typedefs_expr, typedefs_block, typedefs_decl); if (e->kind == EXPR_FN) { /* needs to go before decls_cgen.c... */ - e->fn.c.id = g->ident_counter++; + e->fn->c.id = g->ident_counter++; } if (e->kind == EXPR_TYPE && e->typeval.kind == TYPE_STRUCT) { StructDef *sdef = e->typeval.struc; @@ -44,7 +44,7 @@ static bool typedefs_expr(CGenerator *g, Expression *e) { static bool typedefs_decl(CGenerator *g, Declaration *d) { if (cgen_fn_is_direct(g, d)) { - d->expr.fn.c.name = d->idents[0]; + d->expr.fn->c.name = d->idents[0]; } for (int idx = 0; idx < (int)arr_len(d->idents); ++idx) { Identifier i = d->idents[idx]; diff --git a/types.c b/types.c index fa8c626..1b3d8b2 100644 --- a/types.c +++ b/types.c @@ -385,7 +385,7 @@ static bool type_of_ident(Typer *tr, Location where, Identifier i, Type *t) { } else { if ((d->flags & DECL_HAS_EXPR) && (d->expr.kind == EXPR_FN)) { /* allow using a function before declaring it */ - if (!type_of_fn(tr, &d->expr.fn, d->expr.where, t, 0)) return false; + if (!type_of_fn(tr, d->expr.fn, d->expr.where, t, 0)) return false; return true; } else { if (location_after(d->where, where)) { @@ -417,12 +417,12 @@ static bool type_of_ident(Typer *tr, Location where, Identifier i, Type *t) { switch (e->kind) { case EXPR_EACH: - if (i == e->each.index) { + if (i == e->each->index) { t->kind = TYPE_BUILTIN; t->builtin = BUILTIN_I64; } else { - assert(i == e->each.value); - *t = e->each.type; + assert(i == e->each->value); + *t = e->each->type; } break; default: assert(0); return false; @@ -717,13 +717,13 @@ static bool types_expr(Typer *tr, Expression *e) { e->flags |= EXPR_FOUND_TYPE; /* even if failed, pretend we found the type */ switch (e->kind) { case EXPR_FN: { - if (!type_of_fn(tr, &e->fn, e->where, &e->type, 0)) + if (!type_of_fn(tr, e->fn, e->where, &e->type, 0)) return false; - if (fn_has_any_const_params(&e->fn)) { + if (fn_has_any_const_params(e->fn)) { HashTable z = {0}; - e->fn.instances = z; + e->fn->instances = z; } else { - if (!types_fn(tr, &e->fn, &e->type, e->where, NULL)) + if (!types_fn(tr, e->fn, &e->type, e->where, NULL)) return false; } } break; @@ -757,7 +757,7 @@ static bool types_expr(Typer *tr, Expression *e) { t->flags |= TYPE_IS_RESOLVED; break; case EXPR_EACH: { - EachExpr *ea = &e->each; + EachExpr *ea = e->each; *(Expression **)arr_add(&tr->in_expr_decls) = e; if (!each_enter(e)) return false; if (ea->flags & EACH_IS_RANGE) { @@ -1040,7 +1040,7 @@ static bool types_expr(Typer *tr, Expression *e) { if (decl->decl->flags & DECL_HAS_EXPR) { Expression *expr = &decl->decl->expr; if (expr->kind == EXPR_FN) - fn_decl = &decl->decl->expr.fn; + fn_decl = decl->decl->expr.fn; } } } diff --git a/types.h b/types.h index 7ad5886..4918ba1 100644 --- a/types.h +++ b/types.h @@ -615,8 +615,8 @@ typedef struct Expression { } del; IfExpr if_; WhileExpr while_; - EachExpr each; - FnExpr fn; + EachExpr *each; + FnExpr *fn; CastExpr cast; SliceExpr slice; struct { -- cgit v1.2.3