From be4bd7b281c821b7a57bb2b33761c85b161d97e2 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Tue, 22 Oct 2019 13:22:25 -0400 Subject: improved new slice (removed pre) --- cgen.c | 35 +++++++++++++---------------------- out.c | 11 ++++++----- test.toc | 2 ++ types.h | 3 --- 4 files changed, 21 insertions(+), 30 deletions(-) diff --git a/cgen.c b/cgen.c index c635d9c..59e2598 100644 --- a/cgen.c +++ b/cgen.c @@ -643,27 +643,6 @@ static bool cgen_expr_pre(CGenerator *g, Expression *e) { case EXPR_CAST: if (!cgen_expr_pre(g, e->cast.expr)) return false; break; - case EXPR_NEW: - if (e->new.n) { - if (!cgen_expr_pre(g, e->new.n)) - return false; - IdentID n_id = e->new.c.id = g->ident_counter++; - cgen_write(g, "slice_ "); - cgen_ident_id(g, n_id); - cgen_write(g, "; "); - cgen_ident_id(g, n_id); - cgen_write(g, ".data = calloc("); - if (!cgen_expr(g, e->new.n)) return false; - cgen_write(g, ", sizeof("); - if (!cgen_type_pre(g, &e->new.type, e->where)) return false; - if (!cgen_type_post(g, &e->new.type, e->where)) return false; - cgen_write(g, ")); "); - cgen_ident_id(g, n_id); - cgen_write(g, ".n = "); - if (!cgen_expr(g, e->new.n)) return false; - cgen_write(g, ";"); - } - break; case EXPR_SLICE: { SliceExpr *s = &e->slice; IdentID s_id = e->slice.c.id = g->ident_counter++; @@ -716,6 +695,10 @@ static bool cgen_expr_pre(CGenerator *g, Expression *e) { cgen_write(g, "; }"); cgen_nl(g); } break; + case EXPR_NEW: + if (e->new.n && !cgen_expr_pre(g, e->new.n)) + return false; + break; case EXPR_LITERAL_INT: case EXPR_LITERAL_FLOAT: case EXPR_LITERAL_BOOL: @@ -859,7 +842,14 @@ static bool cgen_expr(CGenerator *g, Expression *e) { } break; case EXPR_NEW: { if (e->new.n) { - cgen_ident_id(g, e->new.c.id); + cgen_write(g, "mkslice_(calloc("); + if (!cgen_expr(g, e->new.n)) return false; + cgen_write(g, ", sizeof("); + if (!cgen_type_pre(g, &e->new.type, e->where)) return false; + if (!cgen_type_post(g, &e->new.type, e->where)) return false; + cgen_write(g, ")), "); + if (!cgen_expr(g, e->new.n)) return false; + cgen_write(g, ")"); } else { Type *t = &e->new.type; cgen_write(g, "(("); @@ -1162,6 +1152,7 @@ static bool cgen_file(CGenerator *g, ParsedFile *f) { "typedef double f64;\n" "typedef unsigned char bool;\n" "typedef struct { void *data; u64 n; } slice_;\n" + "static slice_ mkslice_(void *data, u64 n) { slice_ ret; ret.data = data; ret.n = n; return ret; }\n" "#define false ((bool)0)\n" "#define true ((bool)1)\n\n\n"); if (!cgen_decls_file(g, f)) diff --git a/out.c b/out.c index 9994c0d..e733d42 100644 --- a/out.c +++ b/out.c @@ -12,6 +12,7 @@ typedef float f32; typedef double f64; typedef unsigned char bool; typedef struct { void *data; u64 n; } slice_; +static slice_ mkslice_(void *data, u64 n) { slice_ ret; ret.data = data; ret.n = n; return ret; } #define false ((bool)0) #define true ((bool)1) @@ -39,17 +40,17 @@ i64 foo(void) { i64 N; { i64 expr__; expr__ = 10;N = expr__;} slice_ numbers; { - slice_ expr__; slice_ a0_; a0_.data = calloc(N, sizeof(i64)); a0_.n = N;expr__ = a0_;numbers = expr__;} + slice_ expr__; expr__ = mkslice_(calloc(N, sizeof(i64)), N);numbers = expr__;} i64 i; { i64 expr__; expr__ = 0;i = expr__;} while ((i