From dee206f197b32e18f0f43e6770f1e59e5c25af75 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Fri, 10 Jan 2020 23:35:39 -0500 Subject: made #size/alignof unary ops --- cgen.c | 12 ++++-------- copy.c | 6 ------ eval.c | 8 ++++---- package.c | 13 ++++++++----- parse.c | 26 ++++++++------------------ test.toc | 18 ++++++++++++++++-- types.c | 42 ++++++++++++++++++++++-------------------- types.h | 12 +++--------- 8 files changed, 65 insertions(+), 72 deletions(-) diff --git a/cgen.c b/cgen.c index 384cb0b..e2fc85c 100644 --- a/cgen.c +++ b/cgen.c @@ -55,8 +55,6 @@ static bool cgen_defs_decl(CGenerator *g, Declaration *d); case EXPR_TYPE: \ case EXPR_VAL: \ case EXPR_C: \ - case EXPR_DSIZEOF: \ - case EXPR_DALIGNOF: \ case EXPR_IDENT: \ case EXPR_LITERAL_BOOL: \ case EXPR_LITERAL_INT: \ @@ -737,8 +735,6 @@ static bool cgen_set_tuple(CGenerator *g, Expression *exprs, Identifier *idents, case EXPR_CAST: case EXPR_NEW: case EXPR_C: - case EXPR_DSIZEOF: - case EXPR_DALIGNOF: case EXPR_TYPE: case EXPR_PKG: assert(0); @@ -1148,8 +1144,6 @@ static bool cgen_expr_pre(CGenerator *g, Expression *e) { case EXPR_IDENT: case EXPR_FN: case EXPR_C: - case EXPR_DSIZEOF: - case EXPR_DALIGNOF: case EXPR_TYPE: case EXPR_PKG: break; @@ -1340,6 +1334,10 @@ static bool cgen_expr(CGenerator *g, Expression *e) { } handled = true; } break; + case UNARY_DSIZEOF: + case UNARY_DALIGNOF: + assert(0); + return false; } if (handled) break; cgen_write(g, "("); @@ -1443,8 +1441,6 @@ static bool cgen_expr(CGenerator *g, Expression *e) { cgen_write(g, ")"); } } break; - case EXPR_DSIZEOF: - case EXPR_DALIGNOF: /* handled by types.c */ case EXPR_TUPLE: /* the only time this should happen is if you're stating a tuple, e.g. 3, 5;, but we've errored about that before diff --git a/copy.c b/copy.c index 58ba390..4ac56e3 100644 --- a/copy.c +++ b/copy.c @@ -247,12 +247,6 @@ static void copy_expr(Copier *c, Expression *out, Expression *in) { case EXPR_C: copy_expr(c, out->c.code = allocr_malloc(a, sizeof *out->c.code), in->c.code); break; - case EXPR_DSIZEOF: - copy_expr(c, out->dsizeof.of = allocr_malloc(a, sizeof *out->dsizeof.of), in->dsizeof.of); - break; - case EXPR_DALIGNOF: - copy_expr(c, out->dalignof.of = allocr_malloc(a, sizeof *out->dalignof.of), in->dalignof.of); - break; case EXPR_SLICE: { SliceExpr *sin = &in->slice; SliceExpr *sout = &out->slice; diff --git a/eval.c b/eval.c index 0eab329..4215aa1 100644 --- a/eval.c +++ b/eval.c @@ -1074,6 +1074,10 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) { default: assert(0); break; } break; + case UNARY_DSIZEOF: + case UNARY_DALIGNOF: + assert(0); + return false; } } break; case EXPR_BINARY_OP: { @@ -1522,10 +1526,6 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) { case EXPR_PKG: v->pkg = e->pkg.name_ident->pkg; break; - case EXPR_DSIZEOF: - case EXPR_DALIGNOF: - assert(0); - return false; } return true; } diff --git a/package.c b/package.c index 221591c..7f396b8 100644 --- a/package.c +++ b/package.c @@ -317,7 +317,7 @@ static bool export_type(Exporter *ex, Type *type, Location where) { struc->export.id = (U32)nexported_structs; } - export_len(ex, (size_t)struc->export.id); + export_vlq(ex, (U64)struc->export.id); } break; case TYPE_EXPR: if (!export_expr(ex, type->expr)) @@ -378,6 +378,13 @@ static void import_type(Importer *im, Type *type) { type->fn.constness[i] = import_u8(im); } else type->fn.constness = NULL; } break; + case TYPE_STRUCT: { + U64 struct_id = import_vlq(im); + type->struc = &im->structs[struct_id]; + } break; + case TYPE_EXPR: + import_expr(im, type->expr = imptr_new_expr(im)); + break; } } @@ -655,10 +662,6 @@ static bool export_expr(Exporter *ex, Expression *e) { if (!export_block(ex, &ea->body)) return false; } break; - case EXPR_DSIZEOF: - case EXPR_DALIGNOF: - assert(0); - break; } return true; } diff --git a/parse.c b/parse.c index 9a636b4..cfc66c9 100644 --- a/parse.c +++ b/parse.c @@ -36,8 +36,6 @@ static const char *expr_kind_to_str(ExprKind k) { case EXPR_EACH: return "each expression"; case EXPR_CALL: return "function call"; case EXPR_C: return "c code"; - case EXPR_DSIZEOF: return "#sizeof"; - case EXPR_DALIGNOF: return "#alignof"; case EXPR_NEW: return "new expression"; case EXPR_CAST: return "cast expression"; case EXPR_UNARY_OP: return "unary operator"; @@ -63,6 +61,8 @@ static const char *unary_op_to_str(UnaryOp u) { case UNARY_NOT: return "!"; case UNARY_DEL: return "del"; case UNARY_LEN: return "len"; + case UNARY_DSIZEOF: return "#sizeof"; + case UNARY_DALIGNOF: return "#alignof"; } assert(0); return ""; @@ -1667,12 +1667,14 @@ static bool parse_expr(Parser *p, Expression *e, Token *end) { single_arg = e->c.code = parser_new_expr(p); break; case DIRECT_SIZEOF: - e->kind = EXPR_DSIZEOF; - single_arg = e->dsizeof.of = parser_new_expr(p); + e->kind = EXPR_UNARY_OP; + e->unary.op = UNARY_DSIZEOF; + single_arg = e->unary.of = parser_new_expr(p); break; case DIRECT_ALIGNOF: - e->kind = EXPR_DALIGNOF; - single_arg = e->dalignof.of = parser_new_expr(p); + e->kind = EXPR_UNARY_OP; + e->unary.op = UNARY_DALIGNOF; + single_arg = e->unary.of = parser_new_expr(p); break; case DIRECT_EXPORT: tokr_err(t, "Unrecognized expression."); @@ -2222,16 +2224,6 @@ static void fprint_expr(FILE *out, Expression *e) { fprint_expr(out, e->c.code); fprintf(out, ")"); break; - case EXPR_DSIZEOF: - fprintf(out, "#sizeof("); - fprint_expr(out, e->dsizeof.of); - fprintf(out, ")"); - break; - case EXPR_DALIGNOF: - fprintf(out, "#alignof("); - fprint_expr(out, e->dalignof.of); - fprintf(out, ")"); - break; case EXPR_SLICE: { SliceExpr *s = &e->slice; fprint_expr(out, s->of); @@ -2349,8 +2341,6 @@ static bool expr_is_definitely_const(Expression *e) { case EXPR_LITERAL_CHAR: case EXPR_LITERAL_STR: case EXPR_LITERAL_BOOL: - case EXPR_DSIZEOF: - case EXPR_DALIGNOF: case EXPR_TYPE: case EXPR_VAL: case EXPR_PKG: diff --git a/test.toc b/test.toc index c835a0b..165b41f 100644 --- a/test.toc +++ b/test.toc @@ -9,6 +9,20 @@ putf ::= fn(x: float) { -point ::= pkg "point"; +// point ::= pkg "point"; + + main ::= fn() { -}; \ No newline at end of file + s ::= struct { x,y,z:int; f:f32; }; + puti(#alignof(s)); + puti(#sizeof(s)); + +}; +/* +something's wrong (should be too few opening parentheses!) +main ::= fn() { + puti(#alignof(struct { x,y,z:int; f:f64; }); + puti(#sizeof(struct { x,y,z:int; f:f64; }); + +}; +*/ \ No newline at end of file diff --git a/types.c b/types.c index 06bb21b..48be1d7 100644 --- a/types.c +++ b/types.c @@ -168,8 +168,6 @@ static bool expr_must_lval(Expression *e) { case EXPR_EACH: case EXPR_CALL: case EXPR_C: - case EXPR_DALIGNOF: - case EXPR_DSIZEOF: case EXPR_BLOCK: case EXPR_SLICE: case EXPR_TYPE: @@ -1645,23 +1643,6 @@ static bool types_expr(Typer *tr, Expression *e) { code->kind = EXPR_VAL; t->kind = TYPE_UNKNOWN; } break; - case EXPR_DSIZEOF: - case EXPR_DALIGNOF: { - Expression *of = e->kind == EXPR_DSIZEOF ? e->dsizeof.of : e->dalignof.of; - if (!types_expr(tr, of)) - return false; - if (type_is_builtin(&of->type, BUILTIN_TYPE)) { - Value val; - if (!eval_expr(tr->evalr, of, &val)) - return false; - e->val.i64 = (I64)(e->kind == EXPR_DSIZEOF ? compiler_sizeof : compiler_alignof)(val.type); - } else { - e->val.i64 = (I64)(e->kind == EXPR_DSIZEOF ? compiler_sizeof : compiler_alignof)(&of->type); - } - e->kind = EXPR_VAL; - t->kind = TYPE_BUILTIN; - t->builtin = BUILTIN_I64; - } break; case EXPR_UNARY_OP: { Expression *of = e->unary.of; Type *of_type = &of->type; @@ -1741,7 +1722,28 @@ static bool types_expr(Typer *tr, Expression *e) { return false; } break; - } + case UNARY_DSIZEOF: + case UNARY_DALIGNOF: { + if (!types_expr(tr, of)) + return false; + Type *queried_type; + if (type_is_builtin(&of->type, BUILTIN_TYPE)) { + Value val; + if (!eval_expr(tr->evalr, of, &val)) + return false; + queried_type = val.type; + } else { + queried_type = &of->type; + } + if (e->unary.op == UNARY_DSIZEOF) + e->val.i64 = (I64)compiler_sizeof(queried_type); + else + e->val.i64 = (I64)compiler_alignof(queried_type); + e->kind = EXPR_VAL; + t->kind = TYPE_BUILTIN; + t->builtin = BUILTIN_I64; + } break; + } } break; case EXPR_BINARY_OP: { Expression *lhs = e->binary.lhs; diff --git a/types.h b/types.h index f9520a2..b994f48 100644 --- a/types.h +++ b/types.h @@ -476,8 +476,6 @@ typedef enum { EXPR_BLOCK, EXPR_TUPLE, EXPR_C, - EXPR_DSIZEOF, - EXPR_DALIGNOF, EXPR_SLICE, EXPR_TYPE, EXPR_PKG, @@ -495,7 +493,9 @@ typedef enum { UNARY_DEREF, /* *x */ UNARY_NOT, /* !x */ UNARY_DEL, /* del x */ - UNARY_LEN /* x.len ; replaces BINARY_DOT len when typing */ + UNARY_LEN, /* x.len ; replaces BINARY_DOT len when typing */ + UNARY_DSIZEOF, + UNARY_DALIGNOF } UnaryOp; typedef enum { @@ -655,12 +655,6 @@ typedef struct Expression { struct { struct Expression *code; } c; - struct { - struct Expression *of; - } dsizeof; /* #sizeof directive */ - struct { - struct Expression *of; - } dalignof; /* #alignof directive */ Identifier ident; NewExpr new; struct { -- cgit v1.2.3