From 218d8469ffa174b192d656854543335519094586 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Mon, 11 May 2026 11:20:06 +0900 Subject: [PATCH] 6c: ww-side compiler binary, dup2 syscall, test 994 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit selfhost/cmd/6c/main.ww is a thin packaging of the wwc cgen — slurp a .ww file, run lex+parse+cgen, write Plan 9 amd64 asm to the path given by -o. The cgen routines in selfhost/cmd/wwc/cgen.ww write directly to fd 1, so we use dup2 to redirect stdout into the output file rather than thread an fd through every emit helper. Adds the SYS_DUP2=33 wrapper in lib/os. Makefile wires $(BIN)/6c_ww alongside the other wwstage tools and adds $(BIN)/test_6c_ww to the TESTS list. test/wwc/994_6c_ww.c diffs 6c_ww byte-for-byte against `wwdump_ww -c` on five in-source programs plus the four selfhost main.combined.ww files: same cgen reached through two binaries, so any divergence is a packaging bug in selfhost/cmd/6c. We deliberately don't diff against C-side 6c here — 990 probe 5 already covers that on the subset the ww cgen handles today. --- Makefile | 26 +++++- lib/os/os.ww | 9 ++ selfhost/cmd/6c/main.ww | 131 ++++++++++++++++++++++++++++ test/wwc/994_6c_ww.c | 184 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 347 insertions(+), 3 deletions(-) create mode 100644 selfhost/cmd/6c/main.ww create mode 100644 test/wwc/994_6c_ww.c diff --git a/Makefile b/Makefile index 55fe341b..ebc63bed 100644 --- a/Makefile +++ b/Makefile @@ -47,8 +47,8 @@ CSTAGE_BINS = $(BIN)/ww $(BIN)/6c $(BIN)/6a $(BIN)/6l $(BIN)/wwdump # Wwstage: ww-rewritten toolchain. Built by Cstage today; will be the # only toolchain after Phase 10 closeout. Byte-identical to Cstage -# (tests 990-993). -WWSTAGE_BINS = $(BIN)/wwdump_ww $(BIN)/6a_ww $(BIN)/6l_ww $(BIN)/ww_ww +# (tests 990-994). +WWSTAGE_BINS = $(BIN)/wwdump_ww $(BIN)/6c_ww $(BIN)/6a_ww $(BIN)/6l_ww $(BIN)/ww_ww BINS = $(CSTAGE_BINS) $(WWSTAGE_BINS) LIBS = $(LIB)/libwwc.a $(LIB)/libwwrt.a @@ -116,6 +116,21 @@ $(BIN)/wwdump_ww: selfhost/cmd/wwdump/main.ww \ $$PWD/../../selfhost/cmd/wwdump/main.ww mv $(BIN)/main $@ +# ---- ww-side 6c (compiler port, exercised by 994_6c_ww) ---------------- +# Thin driver: parse + cgen, both already living in selfhost/cmd/wwc. +# Build through `ww build` like the other wwstage tools. +$(BIN)/6c_ww: selfhost/cmd/6c/main.ww \ + selfhost/cmd/wwc/lex.ww selfhost/cmd/wwc/tok.ww \ + selfhost/cmd/wwc/mem.ww selfhost/cmd/wwc/ast.ww \ + selfhost/cmd/wwc/parse.ww selfhost/cmd/wwc/typ.ww \ + selfhost/cmd/wwc/sym.ww selfhost/cmd/wwc/check.ww \ + selfhost/cmd/wwc/cgen.ww \ + $(BIN)/ww $(BIN)/6c $(BIN)/6a $(BIN)/6l \ + $(LIB)/libwwrt.a | $(BIN) + cd $(BIN) && ./ww build -I $$PWD/../../selfhost/cmd/wwc \ + $$PWD/../../selfhost/cmd/6c/main.ww + mv $(BIN)/main $@ + # ---- ww-side 6a (assembler port, exercised by 991_6a_ww) --------------- # Built like wwdump_ww. Needs -I selfhost/cmd/6a for the local types/lex/ # parse/asm/obj modules and -I selfhost/cmd/wwc to find `mem`. @@ -172,7 +187,8 @@ $(BIN) $(LIB) $(OBJ)/wwc $(OBJ)/ww $(OBJ)/wwdump $(OBJ)/6c $(OBJ)/6a $(OBJ)/6l $ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_6c $(BIN)/test_6a $(BIN)/test_6l $(BIN)/test_arch \ $(BIN)/test_e2e $(BIN)/test_ffi $(BIN)/test_dyn $(BIN)/test_stdlib \ - $(BIN)/test_selfhost $(BIN)/test_6a_ww $(BIN)/test_6l_ww $(BIN)/test_ww_ww + $(BIN)/test_selfhost $(BIN)/test_6a_ww $(BIN)/test_6l_ww \ + $(BIN)/test_6c_ww $(BIN)/test_ww_ww $(BIN)/test_smoke: test/wwc/000_smoke.c $(LIB)/libwwc.a | $(BIN) $(CC) $(CFLAGS) $(INCS) -o $@ $< -L$(LIB) -lwwc @@ -224,6 +240,9 @@ $(BIN)/test_6l_ww: test/wwc/992_6l_ww.c $(BIN)/6l $(BIN)/6l_ww \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_6c_ww: test/wwc/994_6c_ww.c $(BIN)/6c_ww $(BIN)/wwdump_ww | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_ww_ww: test/wwc/993_ww_ww.c $(BIN)/ww $(BIN)/ww_ww \ $(BIN)/6c $(BIN)/6a $(BIN)/6l $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< @@ -278,6 +297,7 @@ bootstrap: all @echo @echo "Already in selfhost/:" @echo " - cmd/wwc/ : ww-cgen (lex/parse/check/cgen) — bootstrap fixed point" + @echo " - cmd/6c/ : ww-6c (lex/parse/cgen front-end) — matches wwdump_ww -c (test 994)" @echo " - cmd/6a/ : ww-6a (lex/parse/asm/obj) — byte-identical to C 6a (test 991)" @echo " - cmd/6l/ : ww-6l (linker w/ archive support) — byte-identical to C 6l (test 992)" @echo " - cmd/ww/ : ww-driver (build/run/version) — byte-identical to C ww (test 993)" diff --git a/lib/os/os.ww b/lib/os/os.ww index 71dc5150..559e414e 100644 --- a/lib/os/os.ww +++ b/lib/os/os.ww @@ -24,6 +24,7 @@ def SYS_OPEN: i64 = 2; def SYS_CLOSE: i64 = 3; def SYS_LSEEK: i64 = 8; def SYS_ACCESS: i64 = 21; +def SYS_DUP2: i64 = 33; def SYS_GETPID: i64 = 39; def SYS_FORK: i64 = 57; def SYS_EXECVE: i64 = 59; @@ -62,6 +63,14 @@ export fn close(fd: i32) i32 = { return syscall1(SYS_CLOSE, fd: i64): i32; }; +// dup2(2): make `newfd` refer to the same description as `oldfd`, +// closing `newfd` first if open. Returns `newfd` on success or a +// negative errno. Used by 6c_ww to redirect stdout into an output +// file without changing the cgen emit path. +export fn dup2(oldfd: i32, newfd: i32) i32 = { + return syscall2(SYS_DUP2, oldfd: i64, newfd: i64): i32; +}; + // Fallible wrappers. The error variant is a plain str (Plan 9 errstr // model, see lib/errors); the sum type makes success/failure explicit // without overloading length-zero. diff --git a/selfhost/cmd/6c/main.ww b/selfhost/cmd/6c/main.ww new file mode 100644 index 00000000..1aea7044 --- /dev/null +++ b/selfhost/cmd/6c/main.ww @@ -0,0 +1,131 @@ +// selfhost/cmd/6c/main.ww — port of cmd/6c/main.c. +// +// 6c = amd64 compiler. Read .ww, parse, codegen, emit Plan 9 amd64 +// asm to stdout (or the file given by -o). +// +// 6c_ww -o file.s file.ww +// +// The cgen routines in selfhost/cmd/wwc/cgen.ww write directly to +// fd 1 via os.write(1, ...). For -o, we open the output file and +// dup2 it onto fd 1 before invoking cg_file. This is the same trick +// the bootstrap uses with shell redirection, just in-process. + +use os; +use mem; +use tok; +use lex; +use ast; +use parse; +use typ; +use sym; +use check; +use cgen; + +fn streq_cs(a: *u8, lit: str) bool = { + let n: u64 = lit.len: u64; + let i: u64 = 0u64; + for (i < n) { + let li: i32 = i: i32; + if (a[i] != lit[li]) { return false; }; + i += 1u64; + }; + if (a[i] != 0u8) { return false; }; + return true; +}; + +fn cstrlen(p: *u8) u64 = { + let n: u64 = 0u64; + for (p[n] != 0u8) { n += 1u64; }; + return n; +}; + +fn slurp(path_cs: *u8) (*u8, u64) = { + let fd: i32 = os.open(path_cs, os.O_RDONLY, 0i32); + if (fd < 0) { return nil, 0u64; }; + let n: i64 = os.filesize(fd); + if (n < 0i64) { os.close(fd); return nil, 0u64; }; + let nz: u64 = n: u64; + let buf: *u8 = os.alloc(nz + 1u64): *u8; + let got: i64 = os.readfull(fd, buf, nz); + os.close(fd); + if (got != n) { return nil, 0u64; }; + buf[nz] = 0u8; + return buf, nz; +}; + +export fn main(argc: i32, argv: **u8) i32 = { + let src_cs: *u8 = nil; + let out_cs: *u8 = nil; + + let i: i32 = 1; + for (i < argc) { + let a: *u8 = argv[i]; + if (streq_cs(a, "-o")) { + i += 1; + if (i >= argc) { + os.write(2, "6c: -o requires arg\n".ptr, 20u64); + return 2; + }; + out_cs = argv[i]; + } else { if (a[0u64] == 45u8) { + os.write(2, "6c: unknown flag\n".ptr, 17u64); + return 2; + } else { + if (src_cs != nil) { + os.write(2, "6c: only one input\n".ptr, 19u64); + return 2; + }; + src_cs = a; + }; }; + i += 1; + }; + + if (src_cs == nil) { + os.write(2, "usage: 6c_ww [-o out.s] file.ww\n".ptr, 32u64); + return 2; + }; + + let buf: *u8; + let blen: u64; + buf, blen = slurp(src_cs); + if (buf == nil) { + os.write(2, "6c: cannot read input\n".ptr, 22u64); + return 1; + }; + + // Redirect fd 1 to the output file before any cgen emit runs. + // cgen.ww writes directly to fd 1; dup2 lets us reuse it without + // threading a file descriptor through the emit helpers. + if (out_cs != nil) { + let ofd: i32 = os.open(out_cs, + os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 420i32); // 0o644 + if (ofd < 0) { + os.write(2, "6c: cannot open output\n".ptr, 23u64); + return 1; + }; + if (os.dup2(ofd, 1i32) < 0) { + os.write(2, "6c: dup2 failed\n".ptr, 16u64); + os.close(ofd); + return 1; + }; + os.close(ofd); + }; + + let ar: *arena = newarena(); + let nlen: u64 = cstrlen(src_cs); + let fname: str = astrndup(ar, src_cs, nlen); + + let l: lex; + lexinit(&l, ar, fname, buf, blen); + + let ps: parser; + parserinit(&ps, ar, &l); + let f: *node = parsefile(&ps); + + let cg: cgen; + cgen_init(&cg, ar); + cg_file(&cg, f); + + if (l.errs > 0) { return 1; }; + return 0; +}; diff --git a/test/wwc/994_6c_ww.c b/test/wwc/994_6c_ww.c new file mode 100644 index 00000000..14af2138 --- /dev/null +++ b/test/wwc/994_6c_ww.c @@ -0,0 +1,184 @@ +/* + * 994_6c_ww — phase-10 marker for the ww-side 6c port. + * + * 6c_ww is a thin packaging of selfhost/cmd/wwc/cgen.ww: it slurps a + * .ww file, runs lex+parse+cgen, and writes Plan 9 amd64 asm to the + * file given by -o. The same cgen is reached through `wwdump_ww -c`, + * so 6c_ww must produce byte-identical output to wwdump_ww -c on + * every program — this test pins that. + * + * (We do not diff against C-side `6c` here because the C compiler has + * features the ww cgen has not yet ported — float compare, fn-address + * @symbol, indirect call. Test 990 probe 5 covers the C-vs-ww diff on + * the subset that the ww cgen handles today.) + */ +#include +#include +#include +#include +#include + +static int +runwait(const char *cmd) +{ + int rc = system(cmd); + if (rc == -1) return -1; + if (WIFEXITED(rc)) return WEXITSTATUS(rc); + return 1; +} + +static const char * +absbin(void) +{ + const char *b = getenv("BIN"); + if (!b) b = "out/bin"; + if (b[0] == '/') return b; + static char buf[2048]; + char cwd[1024]; + if (getcwd(cwd, sizeof cwd) == NULL) return NULL; + snprintf(buf, sizeof buf, "%s/%s", cwd, b); + return buf; +} + +static int +slurp(const char *path, char **outbuf, size_t *outlen) +{ + FILE *f = fopen(path, "rb"); + if (!f) return -1; + fseek(f, 0, SEEK_END); + long n = ftell(f); + fseek(f, 0, SEEK_SET); + if (n < 0) { fclose(f); return -1; } + char *b = malloc((size_t)n + 1); + if (!b) { fclose(f); return -1; } + if (fread(b, 1, (size_t)n, f) != (size_t)n) { free(b); fclose(f); return -1; } + b[n] = '\0'; + fclose(f); + *outbuf = b; + *outlen = (size_t)n; + return 0; +} + +static int +diff_one(const char *bin, const char *label, const char *src) +{ + char ws[64], cs[64], cmd[2048]; + snprintf(ws, sizeof ws, "/tmp/wwc6_%d_w.s", getpid()); + snprintf(cs, sizeof cs, "/tmp/wwc6_%d_c.s", getpid()); + + snprintf(cmd, sizeof cmd, "%s/wwdump_ww -c %s > %s 2>/dev/null", + bin, src, ws); + if (runwait(cmd) != 0) { + fprintf(stderr, "6c_ww FAIL: wwdump_ww -c errored on %s\n", label); + unlink(ws); + return -1; + } + snprintf(cmd, sizeof cmd, "%s/6c_ww -o %s %s 2>/dev/null", bin, cs, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "6c_ww FAIL: 6c_ww errored on %s\n", label); + unlink(ws); unlink(cs); + return -1; + } + char *bw = NULL, *bc = NULL; + size_t nw = 0, nc = 0; + int rc = 0; + if (slurp(ws, &bw, &nw) < 0 || slurp(cs, &bc, &nc) < 0) { + fprintf(stderr, "6c_ww FAIL: cannot read .s for %s\n", label); + rc = -1; + } else if (nw != nc || memcmp(bw, bc, nw) != 0) { + fprintf(stderr, "6c_ww FAIL: %s — wwdump_ww %zu vs 6c_ww %zu bytes\n", + label, nw, nc); + rc = -1; + } + free(bw); free(bc); + unlink(ws); unlink(cs); + return rc; +} + +static int +write_file(const char *path, const char *content) +{ + FILE *f = fopen(path, "wb"); + if (!f) return -1; + fputs(content, f); + fclose(f); + return 0; +} + +int +main(void) +{ + const char *bin = absbin(); + if (!bin) return 1; + char cwd[1024]; + if (getcwd(cwd, sizeof cwd) == NULL) return 1; + + /* In-source corpus: programs the ww-side cgen handles today. + * Mirrors probe 5 in 990_selfhost without the parts that drift + * against C 6c. */ + struct { const char *label; const char *src; } progs[] = { + { "ret42", + "fn main() i32 = { return 42; };" }, + { "add", + "fn add(a: i32, b: i32) i32 = { return a + b; };\n" + "fn main() i32 = { return add(7, 35); };" }, + { "sum_for", + "fn sum(n: i32) i32 = {\n" + " let s: i32 = 0;\n" + " let i: i32 = 0;\n" + " for (i < n) { s += i; i += 1; };\n" + " return s;\n" + "};\n" + "fn main() i32 = { return sum(10); };" }, + { "if_else", + "fn check(x: i32) i32 = {\n" + " if (x > 0) { return 1; };\n" + " if (x < 0) { return 100; };\n" + " return 0;\n" + "};\n" + "fn main() i32 = { return check(7); };" }, + { "recursion", + "fn fact(n: i32) i32 = {\n" + " if (n <= 1) { return 1; };\n" + " return n * fact(n - 1);\n" + "};\n" + "fn main() i32 = { return fact(5); };" }, + { NULL, NULL }, + }; + + /* Source-tree corpus: the same .combined.ww files the bootstrap + * fixed point chews on. Confirms 6c_ww handles realistic inputs, + * not just hand-tailored ones. */ + const char *combined_rel[] = { + "selfhost/cmd/wwdump/main.combined.ww", + "selfhost/cmd/6a/main.combined.ww", + "selfhost/cmd/6l/main.combined.ww", + "selfhost/cmd/ww/main.combined.ww", + NULL, + }; + + int fail = 0, n = 0; + + for (int i = 0; progs[i].label; i++) { + char src[64]; + snprintf(src, sizeof src, "/tmp/wwc6_%d_%d.ww", getpid(), i); + if (write_file(src, progs[i].src) != 0) { fail++; n++; continue; } + if (diff_one(bin, progs[i].label, src) != 0) fail++; + unlink(src); + n++; + } + for (int i = 0; combined_rel[i]; i++) { + char p[2048]; + snprintf(p, sizeof p, "%s/%s", cwd, combined_rel[i]); + if (diff_one(bin, combined_rel[i], p) != 0) fail++; + n++; + } + + if (fail) { + fprintf(stderr, "6c_ww: %d/%d diff(s) failed\n", fail, n); + return 1; + } + printf("6c_ww: byte-identical to wwdump_ww -c on %d corpus inputs " + "(in-source + selfhost combined.ww)\n", n); + return 0; +}