summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cgen.c35
-rw-r--r--out.c11
-rw-r--r--test.toc2
-rw-r--r--types.h3
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<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]));
}
diff --git a/test.toc b/test.toc
index bf98789..3e017db 100644
--- a/test.toc
+++ b/test.toc
@@ -17,6 +17,8 @@ foo @= fn() int {
};
main @= fn() {
+ // N @= 5;
+ // puti(N);
x : [foo()]int;
puti(foo());
};
diff --git a/types.h b/types.h
index f684c18..e3a251e 100644
--- a/types.h
+++ b/types.h
@@ -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 {