diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2019-10-22 13:22:25 -0400 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2019-10-22 13:22:25 -0400 |
commit | be4bd7b281c821b7a57bb2b33761c85b161d97e2 (patch) | |
tree | ed7a28127fd2d016e3840538812bc47b035a3bf6 | |
parent | d28b38f18575ae9fe65936fc71ddcb7ea91131cd (diff) |
improved new slice (removed pre)
-rw-r--r-- | cgen.c | 35 | ||||
-rw-r--r-- | out.c | 11 | ||||
-rw-r--r-- | test.toc | 2 | ||||
-rw-r--r-- | types.h | 3 |
4 files changed, 21 insertions, 30 deletions
@@ -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)) @@ -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<N)) { (((i64(*))(numbers.data))[i]) = i;; i = (i+1);; }; - slice_ a2_; { slice_ of__ = numbers; u64 a3_ = 5; a2_.data = (i64(*))(of__.data) + a3_; a2_.n = 7 - a3_; } - slice_ a4_; { slice_ of__ = numbers; u64 a5_ = 2; a4_.data = (i64(*))(of__.data) + a5_; a4_.n = of__.n - 1 - a5_; } - slice_ a6_; { slice_ of__ = numbers; u64 a7_ = 0; a6_.data = (i64(*))(of__.data) + a7_; a6_.n = 6 - a7_; } - return (((((i64(*))(a2_.data))[1])+(((i64(*))(a4_.data))[0]))+(((i64(*))(a6_.data))[3])); + slice_ a1_; { slice_ of__ = numbers; u64 a2_ = 5; a1_.data = (i64(*))(of__.data) + a2_; a1_.n = 7 - a2_; } + slice_ a3_; { slice_ of__ = numbers; u64 a4_ = 2; a3_.data = (i64(*))(of__.data) + a4_; a3_.n = of__.n - 1 - a4_; } + slice_ a5_; { slice_ of__ = numbers; u64 a6_ = 0; a5_.data = (i64(*))(of__.data) + a6_; a5_.n = 6 - a6_; } + return (((((i64(*))(a1_.data))[1])+(((i64(*))(a3_.data))[0]))+(((i64(*))(a5_.data))[3])); } @@ -17,6 +17,8 @@ foo @= fn() int { }; main @= fn() { + // N @= 5; + // puti(N); x : [foo()]int; puti(foo()); }; @@ -386,9 +386,6 @@ typedef struct { typedef struct { Type type; struct Expression *n; /* e.g. for new(int, 5) */ - struct { - IdentID id; - } c; } NewExpr; typedef struct { |