From a4ddca68662f4bc0531763357b4bc00b6c50b456 Mon Sep 17 00:00:00 2001 From: lemon Date: Sat, 13 Aug 2022 13:00:37 +0200 Subject: target --- bootstrap/cgen.c | 10 ++++++++-- bootstrap/env.c | 3 +++ bootstrap/fold.c | 4 ++-- bootstrap/parse.c | 4 +++- src/all.hff | 39 ++++++++++++++++++++++++++++----------- src/main.cff | 3 +++ src/targ.cff | 28 ++++++++++++++++++++++++++++ src/type.cff | 30 ++++++++++++++++++++++++++++++ src/util.cff | 4 ++++ 9 files changed, 109 insertions(+), 16 deletions(-) create mode 100644 src/targ.cff create mode 100644 src/type.cff diff --git a/bootstrap/cgen.c b/bootstrap/cgen.c index 14f36f1..8b7b710 100644 --- a/bootstrap/cgen.c +++ b/bootstrap/cgen.c @@ -146,9 +146,15 @@ geniniex(struct expr *ex) { for (int i = 0; i < ex->ini.args.n; ++i) { struct iniarg *arg = &ex->ini.args.d[i]; if (ex->ty->t == TYarr) - pri("[%I] = %e, ", arg->idx, &arg->ex); + if (arg->ex.t == Eini) + pri("[%I] = %n, ", arg->idx, &arg->ex); + else + pri("[%I] = %e, ", arg->idx, &arg->ex); else - pri(".%s_ = %e, ", arg->fld, &arg->ex); + if (arg->ex.t == Eini) + pri(".%s_ = %n, ", arg->fld, &arg->ex); + else + pri(".%s_ = %e, ", arg->fld, &arg->ex); } pri("}"); diff --git a/bootstrap/env.c b/bootstrap/env.c index ff2929f..40392b9 100644 --- a/bootstrap/env.c +++ b/bootstrap/env.c @@ -33,6 +33,9 @@ envput(struct env *env, const struct decl *decl) { if ((d0 = envfind(&env_noparent, INT_MAX, decl->name))) { // modify existing forward declarations? + if (decl->t != d0->t) + return NULL; + if (decl == d0 || !memcmp(d0, decl, sizeof *d0)) return d0; if (d0->t == Ddef && decl->t == Ddef && !memcmp(&d0->var, &decl->var, sizeof d0->var)) diff --git a/bootstrap/fold.c b/bootstrap/fold.c index efe1f83..cd72415 100644 --- a/bootstrap/fold.c +++ b/bootstrap/fold.c @@ -5,9 +5,9 @@ int fold(struct expr *ex); static void numcast(struct expr *ex, const struct type *to) { - enum typetype t0 = ex->ty->t; enum typetype t1 = to->t; - const struct type *from = ex->ty; + const struct type *from = ex->ty->t == TYenum ? ex->ty->enu.intty : ex->ty; + enum typetype t0 = from->t; const struct type *uto = unconstify(to); const struct type *ufrom = unconstify(from); int size = to->size; diff --git a/bootstrap/parse.c b/bootstrap/parse.c index 197b61b..7a35f09 100644 --- a/bootstrap/parse.c +++ b/bootstrap/parse.c @@ -1360,6 +1360,8 @@ pexpostfix(struct parser *P) { const struct type *ty = ex.ty; if (ty->t == TYptr) ty = ty->child; + bool konst = ty->konst; + ty = unconstify(ty); if (ty->t == TYstruct || ty->t == TYunion || ty->t == TYeunion) { int idx = structfldnam2idx(ty, fnam); struct aggfield *fld = &ty->agg.flds.d[idx]; @@ -1371,7 +1373,7 @@ pexpostfix(struct parser *P) { ex.get.lhs = exprdup(ex); ex.t = Eget; ex.span = tok.span; - ex.ty = ex.ty->konst ? constify(fld->ty) : fld->ty; + ex.ty = konst ? constify(fld->ty) : fld->ty; ex.get.fld = fnam; } else { fatal(P, tok.span, "cannot access `%s': left-hand-side is not an aggregate (%t)", diff --git a/src/all.hff b/src/all.hff index ff74ab7..63ca996 100644 --- a/src/all.hff +++ b/src/all.hff @@ -90,20 +90,17 @@ struct Type { align usize, konst bool, id uint, - t enum union { - void, - bool, - int struct { sgn bool }, - flo, - ptr *Type, - arr struct { child *Type, length i64 }, - slice *Type, + u enum union { + Void, + Bool, + Int struct { sgn bool }, + Flo, + Ptr *Type, + Arr struct { child *Type, length i64 }, + Slice *Type, } } -struct Decl { -} - struct Parser { fp *FILE, curfile *const u8, @@ -115,6 +112,19 @@ struct Parser { } +struct Targ { + name *const u8, + ptrsize u8, + intsize u8, + longsize u8, longalign u8, + llongsize u8, llongalign u8, + sizesize u8, + f64align u8, + valistsize u8, valistalign u8, + charsigned bool, + shortenum bool, +} + // parse.cff extern fn parser_init(*Parser, path *const u8) void; extern fn parse(*Parser) [#]Decl; @@ -127,6 +137,7 @@ extern fn xstrdup(str *const u8) *u8; def FNV1A_INI u32 = 0x811c9dc5; extern fn fnv1a(h u32, [#]const u8) u32; extern fn fnv1a_s(h u32, *const u8) u32; +extern fn fnv1a_i(h u32, i64) u32; extern fn addfilepath(*const u8) int; extern fn fatal(*Parser, Loc, fmt *const u8, ...) void; extern fn internstr(*const u8) *const u8; @@ -137,6 +148,12 @@ extern fn pfmt(proc *fn(u8, *void) void, parg *void, fmt *const u8, ...) void; extern fn vefmt(fmt *const u8, ap va_list) void; extern fn efmt(fmt *const u8, ...) void; +// type.cff + +// targ.cff +extern static g_targ *const Targ; +extern fn targ_ini(name *const u8) bool; + // Inline functions fn bswap32(x u32) u32 { return (x >> 24) diff --git a/src/main.cff b/src/main.cff index cfb6390..596a411 100644 --- a/src/main.cff +++ b/src/main.cff @@ -5,6 +5,9 @@ import "vec.hff"; extern fn main(argc int, argv **u8) int { assert(argc > 1, "args?"); + targ_ini("amd64-sysv"); + assert(g_targ != #null, "target?"); + let p = Parser {}; parser_init(&p, argv[1]); parse(&p); diff --git a/src/targ.cff b/src/targ.cff new file mode 100644 index 0000000..6490dff --- /dev/null +++ b/src/targ.cff @@ -0,0 +1,28 @@ +import "all.hff"; + +static const targs []const Targ = { + { + "amd64-sysv", + .ptrsize: 8, + .intsize: 4, + .longsize: 8, .longalign: 8, + .llongsize: 8, .llongalign: 8, + .sizesize: 8, + .f64align: 8, + .valistsize: 24, .valistalign: 8, + .charsigned: #t, + .shortenum: #f, + } +}; + +extern static g_targ *const Targ = {}; + +extern fn targ_ini(name *const u8) bool { + foreach(targ, i, targs[0::], + if streq(name, targ.name) { + g_targ = &targs[i]; + return #t; + } + ) + return #f; +} diff --git a/src/type.cff b/src/type.cff new file mode 100644 index 0000000..bd56503 --- /dev/null +++ b/src/type.cff @@ -0,0 +1,30 @@ +import "all.hff"; + +fn hashtype(ty *const Type) u32 { + let h = FNV1A_INI; + h = fnv1a_i(h, ty.konst ? 1 : 0); + switch ty.u { + case Void; case Bool; + + case Flo; + h = fnv1a_i(h, ty.size); + h = fnv1a_i(h, ty.align); + + case Int i; + h = fnv1a_i(h, i.sgn ? 1 : 0); + h = fnv1a_i(h, ty.size); + h = fnv1a_i(h, ty.align); + + case Ptr child; + h = fnv1a_i(h, child.id); + + case Arr arr; + h = fnv1a_i(h, arr.child.id); + h = fnv1a_i(h, arr.length); + + case Slice child; + h = fnv1a_i(h, child.id); + + } + return h; +} diff --git a/src/util.cff b/src/util.cff index 9bb5db9..926b1e4 100644 --- a/src/util.cff +++ b/src/util.cff @@ -41,6 +41,10 @@ extern fn fnv1a_s(h u32, str *const u8) u32 { return h; } +extern fn fnv1a_i(h u32, x i64) u32 { + return fnv1a(h, (as(*u8)&x)[0::8]); +} + static filepaths [64]*const u8 = {}; static nfilepaths int = 0; -- cgit v1.2.3