aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlemon <lsof@mailbox.org>2022-08-10 04:12:33 +0200
committerlemon <lsof@mailbox.org>2022-08-10 04:12:33 +0200
commitb33ee6afa74ab1e83554d1b535d81c7df0b3fca5 (patch)
treed5fb52a1529bf35658468adae796757e664bc792
parent769aa95cf3374117c86ec652117dcbab97497eec (diff)
many bugfix
-rwxr-xr-xbootstrap/bootstrap.sh2
-rw-r--r--bootstrap/cgen.c8
-rw-r--r--bootstrap/env.c8
-rw-r--r--bootstrap/parse.c11
-rw-r--r--src/all.hff51
-rw-r--r--src/common.hff12
-rw-r--r--src/libc.hff7
-rw-r--r--src/main.cff12
-rw-r--r--src/parse.cff2
-rw-r--r--src/parse.hff27
-rw-r--r--src/util.cff14
-rw-r--r--src/vec.hff38
12 files changed, 137 insertions, 55 deletions
diff --git a/bootstrap/bootstrap.sh b/bootstrap/bootstrap.sh
index 1cb9f42..7ae3c09 100755
--- a/bootstrap/bootstrap.sh
+++ b/bootstrap/bootstrap.sh
@@ -2,7 +2,7 @@
cd $(dirname "$0")
-./build.sh
+./build.sh || exit 1
if [ x"$CC" == x ]; then
export CC=cc
diff --git a/bootstrap/cgen.c b/bootstrap/cgen.c
index 62a808d..4a80bfb 100644
--- a/bootstrap/cgen.c
+++ b/bootstrap/cgen.c
@@ -634,7 +634,11 @@ gendecl(struct decl *decl, bool toplevel) {
case Dlet:
assert(!toplevel);
break;
- case Ddef: case Dtype: case Dtepl: case Dmacro: case Dlabel:
+ case Dtype:
+ if (decl->ty->t == TYstruct || decl->ty->t == TYunion)
+ for (int i = 0; i < decl->ty->agg.decls.n; ++i)
+ liftdecl(decl->ty->agg.decls.d[i]);
+ case Ddef: case Dtepl: case Dmacro: case Dlabel:
break;
}
}
@@ -716,8 +720,6 @@ defctype(const struct type *ty, void *_) {
pri("_Static_assert(__alignof__(%s) == %U, \"__alignof__(%t) == %U\");\n",
*cname, (u64)ty->align, ty, (u64)ty->align);
}
- for (int i = 0; i < ty->agg.decls.n; ++i)
- liftdecl(ty->agg.decls.d[i]);
break;
case TYeunion:
if (ty->konst) {
diff --git a/bootstrap/env.c b/bootstrap/env.c
index 601b446..2c5e80f 100644
--- a/bootstrap/env.c
+++ b/bootstrap/env.c
@@ -32,13 +32,19 @@ envput(struct env *env, const struct decl *decl) {
static int age;
if ((d0 = envfind(&env_noparent, INT_MAX, decl->name))) {
+ if (decl == d0 || !memcmp(d0, decl, sizeof *d0))
+ return d0;
// modify existing forward declarations?
+ if (d0->t != decl->t)
+ return NULL;
+ if (d0->t == Dmacro && !memcmp(&decl->macro, &d0->macro, sizeof decl->macro))
+ return d0;
for (int kind = TYstruct; kind <= TYunion; ++kind) {
if (d0->t == Dtype && d0->ty->t == kind
&& decl->t == Dtype && decl->ty->t == kind && d0->ty->agg.fwd) {
*(size_t *)&d0->ty->size = decl->ty->size;
*(size_t *)&d0->ty->align = decl->ty->align;
- *(bool *)&d0->ty->agg.fwd = 0;
+ *(bool *)&d0->ty->agg.fwd = decl->ty->agg.fwd;
memcpy((void *)&d0->ty->agg.flds, &decl->ty->agg.flds,
sizeof d0->ty->agg.flds);
memcpy((void *)&d0->ty->agg.decls, &decl->ty->agg.decls,
diff --git a/bootstrap/parse.c b/bootstrap/parse.c
index 8d6fe2c..49f01b1 100644
--- a/bootstrap/parse.c
+++ b/bootstrap/parse.c
@@ -2781,7 +2781,7 @@ getbasedir(const char *path) {
static struct comfile
doimport(struct parser *P, const char *path) {
- static vec_t(const char *) seen;
+ static vec_t(struct entry { const char *s; struct comfile cf; }) seen;
char path2[PATH_MAX], _rpath[PATH_MAX], *rpath;
struct comfile cf = {0};
struct parser P2;
@@ -2791,15 +2791,15 @@ doimport(struct parser *P, const char *path) {
if (!(rpath = realpath(path2, _rpath)))
fatal(P, P->tokspan, "import %q: %s", path, strerror(errno));
for (int i = 0; i < seen.length; ++i)
- if (!strcmp(seen.data[i], rpath))
- return cf;
+ if (!strcmp(seen.data[i].s, rpath))
+ return seen.data[i].cf;
rpath = xstrdup(rpath);
- vec_push(&seen, rpath);
// P2.primenv = P->primenv;
initparser(&P2, xstrdup(path2));
P2.is_header = 1;
parse(&cf, &P2);
+ vec_push(&seen, ((struct entry) { rpath, cf }));
return cf;
}
@@ -3003,7 +3003,7 @@ parsedecl(decl_yielder_t yield, void *yarg, struct parser *P, bool toplevel) {
const char *path;
struct comfile cf;
assert(toplevel);
- path = lexexpect(P, TKstrlit).str;
+ path = (tok = lexexpect(P, TKstrlit)).str;
cf = doimport(P, path);
free((void *)path);
for (int i = 0; i < cf.decls.n; ++i) {
@@ -3011,7 +3011,6 @@ parsedecl(decl_yielder_t yield, void *yarg, struct parser *P, bool toplevel) {
if (yield)
yield(decl, yarg);
}
- free(cf.decls.d);
lexexpect(P, ';');
return;
} else if (lexmatch(P, &tok, TKident)) {
diff --git a/src/all.hff b/src/all.hff
new file mode 100644
index 0000000..89f2b71
--- /dev/null
+++ b/src/all.hff
@@ -0,0 +1,51 @@
+import "libc.hff";
+
+/// Macros
+
+defmacro assert {
+(ex, s) [
+ (do
+ if not (ex) {
+ fprintf(stderr, "%s:%d: assertion failed: ", #FILE, #LINE);
+ fprintf(stderr, "`%s'", (s));
+ fprintf(stderr, "\n");
+ abort();
+ }
+ )
+]
+}
+
+/// Types
+
+struct Loc {
+ fileid u16,
+ idx isize,
+ col int,
+ line int,
+}
+
+#[lax]
+enum TokT {
+ kw_or
+}
+
+struct Tok {
+ t int,
+ u union {
+ i i64
+ },
+}
+
+struct Decl {
+}
+
+struct Parser {
+
+}
+
+// parse.cff
+extern fn parse(P *Parser, path *const u8) [#]Decl;
+
+// util.cff
+extern fn xmalloc(n usize) *void;
+extern fn xrealloc(p *void, n usize) *void;
diff --git a/src/common.hff b/src/common.hff
deleted file mode 100644
index 71e3be0..0000000
--- a/src/common.hff
+++ /dev/null
@@ -1,12 +0,0 @@
-import "libc.hff";
-
-defmacro assert {
-(ex,s) [ (do
- if not (ex) {
- fprintf(stderr, "%s:%d: assertion failed: ", #FILE, #LINE);
- fprintf(stderr, "`%s'", (s));
- fprintf(stderr, "\n");
- abort();
- }
-) ]
-}
diff --git a/src/libc.hff b/src/libc.hff
index f06f4dd..d686905 100644
--- a/src/libc.hff
+++ b/src/libc.hff
@@ -1,4 +1,4 @@
-! stdio.h
+// stdio.h
struct FILE;
extern static stdin *FILE,
stdout *FILE,
@@ -6,6 +6,9 @@ extern static stdin *FILE,
extern fn printf(fmt *const u8, ...) void;
extern fn fprintf(fp *FILE, fmt *const u8, ...) void;
-! stdlib.h
+// stdlib.h
extern fn abort() void;
extern fn exit(c int) void;
+extern fn malloc(n usize) *void;
+extern fn realloc(p *void, n usize) *void;
+extern fn free(p *void) void;
diff --git a/src/main.cff b/src/main.cff
index 9392a7a..4315ce1 100644
--- a/src/main.cff
+++ b/src/main.cff
@@ -1,11 +1,19 @@
+import "all.hff";
import "libc.hff";
-import "common.hff";
-import "parse.hff";
+import "vec.hff";
extern fn main(argc int, argv **u8) int {
assert(argc > 1, "args?");
let p = Parser {};
parse(&p, argv[1]);
+
+ let args Vec<*u8> = {};
+ for let i = 1; i < argc; ++i {
+ args->push(argv[i]);
+ }
+ vec_each(s, i, args,
+ printf("%d: %s\n", i, s);
+ )
}
diff --git a/src/parse.cff b/src/parse.cff
index 35cccdb..089d647 100644
--- a/src/parse.cff
+++ b/src/parse.cff
@@ -1,4 +1,4 @@
-import "parse.hff";
+import "all.hff";
extern fn parse(P *Parser, path *const u8) [#]Decl {
return {};
diff --git a/src/parse.hff b/src/parse.hff
deleted file mode 100644
index 9976df3..0000000
--- a/src/parse.hff
+++ /dev/null
@@ -1,27 +0,0 @@
-struct Loc {
- fileid u16,
- idx isize,
- col int,
- line int,
-}
-
-#[lax]
-enum TokT {
- kw_or
-}
-
-struct Tok {
- t int,
- u union {
- i i64
- },
-}
-
-struct Decl {
-}
-
-struct Parser {
-
-}
-
-extern fn parse(P *Parser, path *const u8) [#]Decl;
diff --git a/src/util.cff b/src/util.cff
new file mode 100644
index 0000000..36a0c13
--- /dev/null
+++ b/src/util.cff
@@ -0,0 +1,14 @@
+import "all.hff";
+
+extern fn xmalloc(n usize) *void {
+ let p = malloc(n);
+ assert(p != #null, "malloc");
+ return p;
+}
+
+extern fn xrealloc(p *void, n usize) *void {
+ let p = realloc(p, n);
+ assert(p != #null, "realloc");
+ return p;
+}
+
diff --git a/src/vec.hff b/src/vec.hff
new file mode 100644
index 0000000..8f794c9
--- /dev/null
+++ b/src/vec.hff
@@ -0,0 +1,38 @@
+import "all.hff";
+
+extern fn malloc(n usize) *void;
+struct Vec<T> {
+ dat *T,
+ len uint,
+ cap uint,
+
+ fn push(vec *Vec, x T) void {
+ if vec.len >= vec.cap {
+ vec.cap = (vec.len + 1) > 8 ? (vec.len + 1) * 2 : 8;
+ if vec.dat == #null {
+ vec.dat = malloc(vec.cap * sizeof T);
+ assert(vec.dat != #null, "malloc");
+ } else {
+ vec.dat = realloc(vec.dat, vec.cap * sizeof T);
+ assert(vec.dat != #null, "realloc");
+ }
+ }
+ vec.dat[vec.len++] = x;
+ }
+
+ fn clear(vec *Vec) void {
+ free(vec.dat);
+ vec.len = 0;
+ vec.cap = 0;
+ }
+}
+
+defmacro vec_each(x, i, v, ...body) [
+ {
+ let $v = v;
+ for let i = 0; i < $v.len; ++i {
+ let x = v.dat[i];
+ { body }
+ }
+ }
+]