From 7d4cb5bb96b061ed8708889b75e4d50757d9b3f2 Mon Sep 17 00:00:00 2001 From: lemon Date: Sat, 13 Aug 2022 10:38:27 +0200 Subject: set template --- bootstrap/cgen.c | 6 +++++- bootstrap/dump.c | 12 ++++++------ bootstrap/parse.c | 11 +++++++++-- src/all.hff | 16 ++++++++++++++++ src/parse.cff | 4 +--- src/set.hff | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/util.cff | 47 +++++++---------------------------------------- 7 files changed, 90 insertions(+), 52 deletions(-) create mode 100644 src/set.hff diff --git a/bootstrap/cgen.c b/bootstrap/cgen.c index b164905..14f36f1 100644 --- a/bootstrap/cgen.c +++ b/bootstrap/cgen.c @@ -554,7 +554,9 @@ liftdecl(struct decl *decl) { case Dfn: if ((decl->fn.body || (decl->container && !decl->externp)) && !*decl->_cname) { if (decl->container) - *decl->_cname = xasprintf("__m%s_%s%d", decl->container->agg.name, decl->fn.name, decl->fn.id); + *decl->_cname = xasprintf("__m%s_%s%d", + decl->container->agg.name ? decl->container->agg.name : "_", + decl->fn.name, decl->fn.id); else *decl->_cname = xasprintf("__f%s_%d", decl->fn.name, decl->fn.id); genfn(decl->externp, *decl->_cname, &decl->fn); @@ -841,6 +843,8 @@ cgen(FILE *fp, const struct comfile *cf) { visittypes(defctype, NULL); for (int i = 0; i < cf->decls.n; ++i) { + if (cf->decls.d[i]->t != Dfn && cf->decls.d[i]->t != Dstatic) + liftdecl(cf->decls.d[i]); gendecl(cf->decls.d[i], 1); } } diff --git a/bootstrap/dump.c b/bootstrap/dump.c index bf63d22..d4fe7e2 100644 --- a/bootstrap/dump.c +++ b/bootstrap/dump.c @@ -144,6 +144,12 @@ tok2str(struct tok tok) { strcat(buf, "'"); } else if (tok.t == TKeof) { return ""; + } else if (tok.t == TKtype) { + return ""; + } else if (tok.t == TKexpr) { + return ""; + } else if (tok.t == TKstrify) { + return "#strify"; } else if (tok.t < NUM_KEYWORDS) { snprintf(buf, sizeof buf - 1, "`%s'", keyword2str[tok.t]); } else if (tok.t > 0xFF) { @@ -157,12 +163,6 @@ tok2str(struct tok tok) { } buf[i] = '\0'; strcat(buf, "'"); - } else if (tok.t == TKtype) { - return ""; - } else if (tok.t == TKexpr) { - return ""; - } else if (tok.t == TKstrify) { - return "#strify"; } else { snprintf(buf, sizeof buf - 1, "`%c'", tok.t); } diff --git a/bootstrap/parse.c b/bootstrap/parse.c index 74ab2e2..874b54d 100644 --- a/bootstrap/parse.c +++ b/bootstrap/parse.c @@ -800,6 +800,9 @@ parsetype(struct parser *P) { struct decl *_decl; return parseagg(P, NULL, TYunion, &_decl); } else if (lexmatch(P, &tok, TKkw_enum)) { + struct decl *_decl; + if (lexmatch(P, NULL, TKkw_union)) + return parseagg(P, NULL, TYeunion, &_decl); return parseenum(P, NULL, (struct attr){0}); } else if (lexmatch(P, &tok, TKkw_typeof)) { const struct type *ty = NULL, *ty2, *ty0; @@ -1027,6 +1030,7 @@ static struct expr pexprimary(struct parser *P) { struct expr ex = {0}; struct tok tok; + const struct type *ty; P->used_targty = 0; @@ -1071,7 +1075,6 @@ pexprimary(struct parser *P) { decl: if (!decl) fatal(P, tok.span, "%T is not defined", tok); - const struct type *ty; if (decl->t == Dtype) { ty = decl->ty; typedecl: @@ -1123,6 +1126,9 @@ pexprimary(struct parser *P) { ex.ty = decl->fn.selfty; } else assert(0); } + } else if (lexmatch(P, &tok, TKtype)) { + ty = tok.ty; + goto typedecl; } else if (lexmatch(P, &tok, '(')) { if (lexpeek(P).t == TKkw_do) { struct blockstmt block; @@ -2789,7 +2795,7 @@ parseagg(struct parser *P, const char *name, int kind, struct decl **retdecl) { const char *fnam = (tok = lexexpect(P, TKident)).str; const struct type *ty = NULL; size_t off = size; - if (kind != TYeunion || ((tok = lexpeek(P)).t != ',' && tok.t != '}' && !isdecltokt(tok.t))) { + if (kind != TYeunion || ((tok = lexpeek(P)).t != ',' && tok.t != '}' && tok.t != TKkw_fn)) { ty = parsetype(P); if (f0align < 0) f0align = ty->align; @@ -2857,6 +2863,7 @@ parseagg(struct parser *P, const char *name, int kind, struct decl **retdecl) { size_t off = f0align < 0 ? 0 : ALIGNUP(enumty.size, f0align); ppty->size += off; align = MAX(align, enumty.align); + ppty->size = ALIGNUP(ppty->size, align); for (int i = 0; i < ppty->agg.flds.n; ++i) ppty->agg.flds.d[i].off += off; diff --git a/src/all.hff b/src/all.hff index f15df13..ff74ab7 100644 --- a/src/all.hff +++ b/src/all.hff @@ -85,6 +85,22 @@ struct Tok { }, } +struct Type { + size usize, + 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, + } +} + struct Decl { } diff --git a/src/parse.cff b/src/parse.cff index 9181e70..ef01da5 100644 --- a/src/parse.cff +++ b/src/parse.cff @@ -175,7 +175,6 @@ fn readnumber(s *const u8) Option { suffix *const u8 = #null; for let i = 0; (c = s[i]) != 0; ++i { - printf("<%c>\n",c); if i == 0 and c == '0' { --nused; } @@ -205,6 +204,7 @@ fn readnumber(s *const u8) Option { } let tok = Tok {}; + assert(suffix == #null,"sufx"); if flt { tok.t = :flo; tok.u.flo = accf; @@ -214,8 +214,6 @@ fn readnumber(s *const u8) Option { tok.u.uint = acc; return :Some tok; } - - return :None; } fn lex(P *Parser) Tok { diff --git a/src/set.hff b/src/set.hff new file mode 100644 index 0000000..acff95b --- /dev/null +++ b/src/set.hff @@ -0,0 +1,46 @@ +import "vec.hff"; +import "all.hff"; + +struct Set { + buf Vec, + set **const T, + N int, + count int, + + fn intern(self *Set, x T) *const T { + if self.set == #null { + self.set = xcalloc(self.N = 16, sizeof *T); + } + + if self.count == self.N / 2 { + free(self.set); + self.set = xcalloc(self.N *= 2, sizeof *T); + vec_each(p, i, self.buf, + let i = Traits:hash(p) & (self.N - 1); + for ;; { + if self.set[i] == #null { + self.set[i] = &self.buf.dat[i]; + break; + } + i = (i + 1) & (self.N - 1); + } + ) + } + + let i0 = Traits:hash(x) & (self.N - 1); + let i int = i0; + do { + if self.set[i] == #null { + ++self.count; + self.buf->push(Traits:dup(x)); + return self.set[i] = &self.buf.dat[self.buf.len - 1]; + } else if Traits:eq(*self.set[i], x) { + fprintf(stderr, "found %s\n", x); + return self.set[i]; + } + i = (i + 1) & (self.N - 1); + } while i != i0; + assert(#f, "unreachable"); + } + +} diff --git a/src/util.cff b/src/util.cff index d1c9dc5..70c80b4 100644 --- a/src/util.cff +++ b/src/util.cff @@ -1,4 +1,4 @@ -import "vec.hff"; +import "set.hff"; import "all.hff"; extern fn xmalloc(n usize) *void { @@ -79,43 +79,10 @@ extern fn fatal(P *Parser, loc Loc, fmt *const u8, ...) void { } extern fn internstr(s *const u8) *const u8 { - static buf Vec<*const u8> = {}; - static set **const u8 = {}; - static N int = {}; - static count int = {}; - - if set == #null { - set = xcalloc(N = 16, sizeof int); - } - - if count == N / 2 { - free(set); - set = xcalloc(N *= 2, sizeof int); - vec_each(s, i, buf, - let i = fnv1a_s(FNV1A_INI, s) & (N - 1); - for ;; { - if set[i] == #null { - set[i] = s; - break; - } - i = (i + 1) & (N - 1); - } - ) - } - - let i0 = fnv1a_s(FNV1A_INI, s) & (N - 1); - let i int = i0; - do { - if set[i] == #null { - ++count; - buf->push(xstrdup(s)); - set[i] = buf.dat[buf.len - 1]; - return set[i]; - } else if streq(set[i], s) { - return set[i]; - } - i = (i + 1) & (N - 1); - } while i != i0; - assert(#f, "unreachable"); - + static set Set<*const u8, struct { + fn hash(s *const u8) u32 { return fnv1a_s(FNV1A_INI, s); } + fn eq(a *const u8, b *const u8) bool { return streq(a, b); } + fn dup(s *const u8) *const u8 { return as(*const u8)xstrdup(s); } + }> = {}; + return *set->intern(s); } -- cgit v1.2.3