ww: rename toolchain to w-prefix + hare-style build/run/test driver

Plan 9-style w-prefix on the per-arch tools, disambiguating from the
real Plan 9 6c/6a/6l in ref/plan9front/:

    cmd/wwc/      → cmd/wcc/        libwwc.a → libwcc.a
    cmd/6{c,a,l}  → cmd/w6{c,a,l}   binary names too
    test/wwc/     → test/wcc/       6 test files w/ w6 prefix
    selfhost/cmd  mirror in lockstep
    bootstrap/amd64/{w6c,w6a,w6l}   snapshot binaries (gitignored)
    WW_6{C,A,L}   → WW_W6{C,A,L}    env-var overrides

Plan 9 source-tree refs ("Plan 9 6c shape", ref/plan9front/, etc.)
preserved. Hare-style driver, both C and ww sides:

    ww test [path]   discover *_test.ww in a directory module, run
                     each; single-file mode for `ww test foo.ww`
    Module-by-name   `ww build foo` resolves to foo.ww or foo/foo.ww
                     via search path (cwd : -I dirs : $WW_LIB)
    Default-to-cwd   `ww build` / `ww test` build the cwd module
    Run pass-through `ww run path arg1 arg2` reaches the program

lib/os: getcwd (79) and getdents64 (217) syscalls power `.` resolution
and directory enumeration on the ww side.

Makefile: wwstage tool deps now include lib/os/os.ww (+ lib/strconv
for wwdump_ww) so lib/* edits force their rebuild instead of leaving
stale binaries — surfaced when test 995 first failed against a stale
w6c_ww built before the lib/os additions.

Test 993 byte-identical parity gate (C-side ww vs ww-side ww_ww on a
build corpus) stays green; all 19 tests pass.
This commit is contained in:
2026-05-11 13:49:27 +09:00
parent e217cd32d1
commit 2c33228b7e
96 changed files with 2129 additions and 973 deletions

View File

@@ -1,10 +1,10 @@
// selfhost/cmd/6a/asm.ww — port of cmd/6a/asm.c.
// selfhost/cmd/w6a/asm.ww — port of cmd/w6a/asm.c.
//
// Encode the parsed aprog list into amd64 machine bytes, appending to
// asm_.text. Relocations for CALL/branch targets that resolve to
// externals are queued in asm_.relocs.
//
// Encoding subset matches what 6c emits — see cmd/6a/asm.c for the
// Encoding subset matches what w6c emits — see cmd/w6a/asm.c for the
// authoritative list. Helpers (rcode/rhi/modrm/emit_rex etc.) are
// fully ported; a_encode itself is still a stub pending the full
// switch over A_*.
@@ -354,7 +354,7 @@ export fn a_encode(a: *asm_) i32 = {
a_addreloc(a, reloff, 2, s, -4i64);
p = p.link; continue;
};};
os.write(2, "6a: unsupported MOVQ shape\n".ptr, 27u64);
os.write(2, "w6a: unsupported MOVQ shape\n".ptr, 27u64);
a.errs += 1;
p = p.link; continue;
};
@@ -374,7 +374,7 @@ export fn a_encode(a: *asm_) i32 = {
emit_modrm_mem(a, rcode(tt), p.from.reg, p.from.offset);
p = p.link; continue;
};};
os.write(2, "6a: unsupported MOVB shape\n".ptr, 27u64);
os.write(2, "w6a: unsupported MOVB shape\n".ptr, 27u64);
a.errs += 1;
p = p.link; continue;
};
@@ -389,7 +389,7 @@ export fn a_encode(a: *asm_) i32 = {
emit_modrm_mem(a, rcode(tt), p.from.reg, p.from.offset);
p = p.link; continue;
};};
os.write(2, "6a: unsupported MOVZBQ shape\n".ptr, 29u64);
os.write(2, "w6a: unsupported MOVZBQ shape\n".ptr, 29u64);
a.errs += 1;
p = p.link; continue;
};
@@ -415,7 +415,7 @@ export fn a_encode(a: *asm_) i32 = {
a_emit_byte(a, modrm_byte(3, rcode(ft), rcode(tt)));
p = p.link; continue;
};};
os.write(2, "6a: unsupported MOVL shape\n".ptr, 27u64);
os.write(2, "w6a: unsupported MOVL shape\n".ptr, 27u64);
a.errs += 1;
p = p.link; continue;
};
@@ -429,7 +429,7 @@ export fn a_encode(a: *asm_) i32 = {
emit_modrm_mem(a, rcode(tt), p.from.reg, p.from.offset);
p = p.link; continue;
};};
os.write(2, "6a: unsupported MOVSXD shape\n".ptr, 29u64);
os.write(2, "w6a: unsupported MOVSXD shape\n".ptr, 29u64);
a.errs += 1;
p = p.link; continue;
};
@@ -449,7 +449,7 @@ export fn a_encode(a: *asm_) i32 = {
sse_mr_load(a, 242u8, 17u8, ft, p.to.reg, p.to.offset);
p = p.link; continue;
};};
os.write(2, "6a: unsupported MOVSD shape\n".ptr, 28u64);
os.write(2, "w6a: unsupported MOVSD shape\n".ptr, 28u64);
a.errs += 1;
p = p.link; continue;
};
@@ -476,7 +476,7 @@ export fn a_encode(a: *asm_) i32 = {
sse_mr_load(a, 243u8, 17u8, ft, p.to.reg, p.to.offset);
p = p.link; continue;
};};
os.write(2, "6a: unsupported MOVSS shape\n".ptr, 28u64);
os.write(2, "w6a: unsupported MOVSS shape\n".ptr, 28u64);
a.errs += 1;
p = p.link; continue;
};
@@ -651,7 +651,7 @@ export fn a_encode(a: *asm_) i32 = {
p = p.link; continue;
};
os.write(2, "6a: unsupported opcode\n".ptr, 23u64);
os.write(2, "w6a: unsupported opcode\n".ptr, 23u64);
a.errs += 1;
p = p.link;
};
@@ -660,7 +660,7 @@ export fn a_encode(a: *asm_) i32 = {
let f: *afixup = a.fixups;
for (f != nil) {
if (!label_defined(a, f.label)) {
os.write(2, "6a: undefined label '".ptr, 21u64);
os.write(2, "w6a: undefined label '".ptr, 21u64);
let lbl: str = f.label;
os.write(2, lbl.ptr, lbl.len: u64);
os.write(2, "'\n".ptr, 2u64);

View File

@@ -1,6 +1,6 @@
// selfhost/cmd/6a/lex.ww — port of cmd/6a/lex.c.
// selfhost/cmd/w6a/lex.ww — port of cmd/w6a/lex.c.
//
// Character-level helpers for 6a's line-oriented parser. The parser
// Character-level helpers for w6a's line-oriented parser. The parser
// itself is in parse.ww; here we keep tokenisers for identifiers and
// numbers so parse.ww stays focused on syntax.

View File

@@ -18,19 +18,21 @@ export fn assert(cond: bool, msg: str) void = {
if (!cond) { abort(msg); };
};
def SYS_READ: i64 = 0;
def SYS_WRITE: i64 = 1;
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;
def SYS_EXIT: i64 = 60;
def SYS_WAIT4: i64 = 61;
def SYS_UNLINK: i64 = 87;
def SYS_READ: i64 = 0;
def SYS_WRITE: i64 = 1;
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;
def SYS_EXIT: i64 = 60;
def SYS_WAIT4: i64 = 61;
def SYS_UNLINK: i64 = 87;
def SYS_GETCWD: i64 = 79;
def SYS_GETDENTS64: i64 = 217;
// open(2) flags. Linux values, matching <fcntl.h>.
def O_RDONLY: i32 = 0;
@@ -65,7 +67,7 @@ export fn close(fd: i32) 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
// negative errno. Used by w6c_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;
@@ -179,7 +181,33 @@ export fn wait4(pid: i32, status_out: *i32, options: i32, rusage: *void) i32 = {
options: i64, rusage: i64): i32;
};
// selfhost/cmd/wwc/mem.ww — port of cmd/wwc/mem.c.
// getcwd(2) — Linux flavour. Writes the NUL-terminated cwd into `buf`
// and returns the number of bytes written (including the NUL), or a
// negative errno. The driver uses it to expand `.` to the cwd's
// basename for `ww build` / `ww test`.
export fn getcwd(buf: *u8, n: u64) i64 = {
return syscall2(SYS_GETCWD, buf: i64, n: i64);
};
// getdents64(2) — Linux directory enumeration. The fd must be opened
// with O_RDONLY on a directory. `buf` receives a packed sequence of
// linux_dirent64 records:
//
// struct linux_dirent64 {
// u64 d_ino; // 0..7
// i64 d_off; // 8..15
// u16 d_reclen; // 16..17 — total bytes for this record
// u8 d_type; // 18 — DT_REG/DT_DIR/...
// u8 d_name[]; // 19.. — NUL-terminated name + padding
// };
//
// Returns bytes written into `buf` (advance by d_reclen to walk),
// 0 at end-of-directory, or a negative errno.
export fn getdents64(fd: i32, buf: *u8, n: u64) i64 = {
return syscall3(SYS_GETDENTS64, fd: i64, buf: i64, n: i64);
};
// selfhost/cmd/wcc/mem.ww — port of cmd/wcc/mem.c.
//
// Bump arena allocator. Backed by the runtime page allocator
// (rt_alloc / rt_free), no libc. Each chunk is mmap'd; when the
@@ -187,7 +215,7 @@ export fn wait4(pid: i32, status_out: *i32, options: i32, rusage: *void) i32 = {
// unmaps the chain.
//
// Memory handed out is 16-byte aligned. The C version under
// cmd/wwc/ is retained until the three-stage bootstrap diffs clean.
// cmd/wcc/ is retained until the three-stage bootstrap diffs clean.
use os;
@@ -286,15 +314,15 @@ export fn freearena(a: *arena) void = {
};
};
// selfhost/cmd/6a/types.ww — types + constants shared across the
// 6a port. Mirrors cmd/6a/a.h and cmd/6c/6.out.h.
// selfhost/cmd/w6a/types.ww — types + constants shared across the
// w6a port. Mirrors cmd/w6a/a.h and cmd/w6c/6.out.h.
use mem;
// ---- registers + operand kinds (from 6.out.h) -------------------------
// These must stay numerically aligned with the C enum so that ww-cgen
// output (which reads them via `D_AX(SB)` etc.) lands on the same
// integers when read by ww-6a.
// integers when read by ww-w6a.
def D_NONE: i32 = 0;
def D_AX: i32 = 1;
@@ -409,7 +437,7 @@ def A_JNZ: i32 = 58;
def A_SYSCALL: i32 = 59;
// ---- structs (mirror cmd/6a/a.h) --------------------------------------
// ---- structs (mirror cmd/w6a/a.h) --------------------------------------
type aoperand = struct {
atype: i32, // D_NONE / D_AX..D_R15 / D_CONST / D_INDIR / D_EXTERN / D_BRANCH
@@ -480,9 +508,9 @@ type asm_ = struct {
errs: i32,
};
// selfhost/cmd/6a/lex.ww — port of cmd/6a/lex.c.
// selfhost/cmd/w6a/lex.ww — port of cmd/w6a/lex.c.
//
// Character-level helpers for 6a's line-oriented parser. The parser
// Character-level helpers for w6a's line-oriented parser. The parser
// itself is in parse.ww; here we keep tokenisers for identifiers and
// numbers so parse.ww stays focused on syntax.
@@ -547,9 +575,9 @@ export fn a_parsenum(p: *u8, n: u64) (i64, u64) = {
return v, i;
};
// selfhost/cmd/6a/parse.ww — port of cmd/6a/parse.c.
// selfhost/cmd/w6a/parse.ww — port of cmd/w6a/parse.c.
//
// Line-oriented parser for the asm subset emitted by 6c.
// Line-oriented parser for the asm subset emitted by w6c.
// Grammar:
// line := blank | comment | label | text | instr
// blank := /^\s*$/
@@ -719,7 +747,7 @@ export fn a_intern(a: *asm_, name: str) *asym = {
};
fn perr(a: *asm_, msg: str) void = {
os.write(2, "6a: ".ptr, 4u64);
os.write(2, "w6a: ".ptr, 4u64);
let f: str = a.file;
os.write(2, f.ptr, f.len: u64);
os.write(2, ": ".ptr, 2u64);
@@ -1131,13 +1159,13 @@ export fn a_parse(a: *asm_) i32 = {
return a.errs;
};
// selfhost/cmd/6a/asm.ww — port of cmd/6a/asm.c.
// selfhost/cmd/w6a/asm.ww — port of cmd/w6a/asm.c.
//
// Encode the parsed aprog list into amd64 machine bytes, appending to
// asm_.text. Relocations for CALL/branch targets that resolve to
// externals are queued in asm_.relocs.
//
// Encoding subset matches what 6c emits — see cmd/6a/asm.c for the
// Encoding subset matches what w6c emits — see cmd/w6a/asm.c for the
// authoritative list. Helpers (rcode/rhi/modrm/emit_rex etc.) are
// fully ported; a_encode itself is still a stub pending the full
// switch over A_*.
@@ -1487,7 +1515,7 @@ export fn a_encode(a: *asm_) i32 = {
a_addreloc(a, reloff, 2, s, -4i64);
p = p.link; continue;
};};
os.write(2, "6a: unsupported MOVQ shape\n".ptr, 27u64);
os.write(2, "w6a: unsupported MOVQ shape\n".ptr, 27u64);
a.errs += 1;
p = p.link; continue;
};
@@ -1507,7 +1535,7 @@ export fn a_encode(a: *asm_) i32 = {
emit_modrm_mem(a, rcode(tt), p.from.reg, p.from.offset);
p = p.link; continue;
};};
os.write(2, "6a: unsupported MOVB shape\n".ptr, 27u64);
os.write(2, "w6a: unsupported MOVB shape\n".ptr, 27u64);
a.errs += 1;
p = p.link; continue;
};
@@ -1522,7 +1550,7 @@ export fn a_encode(a: *asm_) i32 = {
emit_modrm_mem(a, rcode(tt), p.from.reg, p.from.offset);
p = p.link; continue;
};};
os.write(2, "6a: unsupported MOVZBQ shape\n".ptr, 29u64);
os.write(2, "w6a: unsupported MOVZBQ shape\n".ptr, 29u64);
a.errs += 1;
p = p.link; continue;
};
@@ -1548,7 +1576,7 @@ export fn a_encode(a: *asm_) i32 = {
a_emit_byte(a, modrm_byte(3, rcode(ft), rcode(tt)));
p = p.link; continue;
};};
os.write(2, "6a: unsupported MOVL shape\n".ptr, 27u64);
os.write(2, "w6a: unsupported MOVL shape\n".ptr, 27u64);
a.errs += 1;
p = p.link; continue;
};
@@ -1562,7 +1590,7 @@ export fn a_encode(a: *asm_) i32 = {
emit_modrm_mem(a, rcode(tt), p.from.reg, p.from.offset);
p = p.link; continue;
};};
os.write(2, "6a: unsupported MOVSXD shape\n".ptr, 29u64);
os.write(2, "w6a: unsupported MOVSXD shape\n".ptr, 29u64);
a.errs += 1;
p = p.link; continue;
};
@@ -1582,7 +1610,7 @@ export fn a_encode(a: *asm_) i32 = {
sse_mr_load(a, 242u8, 17u8, ft, p.to.reg, p.to.offset);
p = p.link; continue;
};};
os.write(2, "6a: unsupported MOVSD shape\n".ptr, 28u64);
os.write(2, "w6a: unsupported MOVSD shape\n".ptr, 28u64);
a.errs += 1;
p = p.link; continue;
};
@@ -1609,7 +1637,7 @@ export fn a_encode(a: *asm_) i32 = {
sse_mr_load(a, 243u8, 17u8, ft, p.to.reg, p.to.offset);
p = p.link; continue;
};};
os.write(2, "6a: unsupported MOVSS shape\n".ptr, 28u64);
os.write(2, "w6a: unsupported MOVSS shape\n".ptr, 28u64);
a.errs += 1;
p = p.link; continue;
};
@@ -1784,7 +1812,7 @@ export fn a_encode(a: *asm_) i32 = {
p = p.link; continue;
};
os.write(2, "6a: unsupported opcode\n".ptr, 23u64);
os.write(2, "w6a: unsupported opcode\n".ptr, 23u64);
a.errs += 1;
p = p.link;
};
@@ -1793,7 +1821,7 @@ export fn a_encode(a: *asm_) i32 = {
let f: *afixup = a.fixups;
for (f != nil) {
if (!label_defined(a, f.label)) {
os.write(2, "6a: undefined label '".ptr, 21u64);
os.write(2, "w6a: undefined label '".ptr, 21u64);
let lbl: str = f.label;
os.write(2, lbl.ptr, lbl.len: u64);
os.write(2, "'\n".ptr, 2u64);
@@ -1813,7 +1841,7 @@ export fn a_encode(a: *asm_) i32 = {
return a.errs;
};
// selfhost/cmd/6a/obj.ww — port of cmd/6a/obj.c.
// selfhost/cmd/w6a/obj.ww — port of cmd/w6a/obj.c.
//
// Emit a tiny ELF64 relocatable object. Layout (in file order):
// [0] ELF header
@@ -2107,11 +2135,11 @@ export fn a_emit_elf(a: *asm_, fd: i32) i32 = {
return 0;
};
// selfhost/cmd/6a/main.ww — port of cmd/6a/main.c.
// selfhost/cmd/w6a/main.ww — port of cmd/w6a/main.c.
//
// 6a = amd64 assembler. Read .s, parse, encode, emit ELF .o.
// w6a = amd64 assembler. Read .s, parse, encode, emit ELF .o.
//
// 6a_ww -o file.o file.s
// w6a_ww -o file.o file.s
use os;
use mem;
@@ -2164,16 +2192,16 @@ export fn main(argc: i32, argv: **u8) i32 = {
if (streq_cs(a, "-o")) {
i += 1;
if (i >= argc) {
os.write(2, "6a: -o requires arg\n".ptr, 20u64);
os.write(2, "w6a: -o requires arg\n".ptr, 20u64);
return 2;
};
out_cs = argv[i];
} else { if (a[0u64] == 45u8) {
os.write(2, "6a: unknown flag\n".ptr, 17u64);
os.write(2, "w6a: unknown flag\n".ptr, 17u64);
return 2;
} else {
if (src_cs != nil) {
os.write(2, "6a: only one input\n".ptr, 19u64);
os.write(2, "w6a: only one input\n".ptr, 19u64);
return 2;
};
src_cs = a;
@@ -2182,11 +2210,11 @@ export fn main(argc: i32, argv: **u8) i32 = {
};
if (src_cs == nil) {
os.write(2, "usage: 6a_ww -o file.o file.s\n".ptr, 30u64);
os.write(2, "usage: w6a_ww -o file.o file.s\n".ptr, 30u64);
return 2;
};
if (out_cs == nil) {
os.write(2, "6a: missing -o\n".ptr, 15u64);
os.write(2, "w6a: missing -o\n".ptr, 15u64);
return 2;
};
@@ -2194,7 +2222,7 @@ export fn main(argc: i32, argv: **u8) i32 = {
let blen: u64;
buf, blen = slurp(src_cs);
if (buf == nil) {
os.write(2, "6a: cannot read input\n".ptr, 22u64);
os.write(2, "w6a: cannot read input\n".ptr, 22u64);
return 1;
};
@@ -2210,7 +2238,7 @@ export fn main(argc: i32, argv: **u8) i32 = {
// Open output for write.
let fd: i32 = os.open(out_cs, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 420i32); // 0o644
if (fd < 0) {
os.write(2, "6a: cannot open output\n".ptr, 23u64);
os.write(2, "w6a: cannot open output\n".ptr, 23u64);
return 1;
};
let rc: i32 = a_emit_elf(&asm, fd);

View File

@@ -1,8 +1,8 @@
// selfhost/cmd/6a/main.ww — port of cmd/6a/main.c.
// selfhost/cmd/w6a/main.ww — port of cmd/w6a/main.c.
//
// 6a = amd64 assembler. Read .s, parse, encode, emit ELF .o.
// w6a = amd64 assembler. Read .s, parse, encode, emit ELF .o.
//
// 6a_ww -o file.o file.s
// w6a_ww -o file.o file.s
use os;
use mem;
@@ -55,16 +55,16 @@ export fn main(argc: i32, argv: **u8) i32 = {
if (streq_cs(a, "-o")) {
i += 1;
if (i >= argc) {
os.write(2, "6a: -o requires arg\n".ptr, 20u64);
os.write(2, "w6a: -o requires arg\n".ptr, 20u64);
return 2;
};
out_cs = argv[i];
} else { if (a[0u64] == 45u8) {
os.write(2, "6a: unknown flag\n".ptr, 17u64);
os.write(2, "w6a: unknown flag\n".ptr, 17u64);
return 2;
} else {
if (src_cs != nil) {
os.write(2, "6a: only one input\n".ptr, 19u64);
os.write(2, "w6a: only one input\n".ptr, 19u64);
return 2;
};
src_cs = a;
@@ -73,11 +73,11 @@ export fn main(argc: i32, argv: **u8) i32 = {
};
if (src_cs == nil) {
os.write(2, "usage: 6a_ww -o file.o file.s\n".ptr, 30u64);
os.write(2, "usage: w6a_ww -o file.o file.s\n".ptr, 30u64);
return 2;
};
if (out_cs == nil) {
os.write(2, "6a: missing -o\n".ptr, 15u64);
os.write(2, "w6a: missing -o\n".ptr, 15u64);
return 2;
};
@@ -85,7 +85,7 @@ export fn main(argc: i32, argv: **u8) i32 = {
let blen: u64;
buf, blen = slurp(src_cs);
if (buf == nil) {
os.write(2, "6a: cannot read input\n".ptr, 22u64);
os.write(2, "w6a: cannot read input\n".ptr, 22u64);
return 1;
};
@@ -101,7 +101,7 @@ export fn main(argc: i32, argv: **u8) i32 = {
// Open output for write.
let fd: i32 = os.open(out_cs, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 420i32); // 0o644
if (fd < 0) {
os.write(2, "6a: cannot open output\n".ptr, 23u64);
os.write(2, "w6a: cannot open output\n".ptr, 23u64);
return 1;
};
let rc: i32 = a_emit_elf(&asm, fd);

View File

@@ -1,4 +1,4 @@
// selfhost/cmd/6a/obj.ww — port of cmd/6a/obj.c.
// selfhost/cmd/w6a/obj.ww — port of cmd/w6a/obj.c.
//
// Emit a tiny ELF64 relocatable object. Layout (in file order):
// [0] ELF header

View File

@@ -1,6 +1,6 @@
// selfhost/cmd/6a/parse.ww — port of cmd/6a/parse.c.
// selfhost/cmd/w6a/parse.ww — port of cmd/w6a/parse.c.
//
// Line-oriented parser for the asm subset emitted by 6c.
// Line-oriented parser for the asm subset emitted by w6c.
// Grammar:
// line := blank | comment | label | text | instr
// blank := /^\s*$/
@@ -170,7 +170,7 @@ export fn a_intern(a: *asm_, name: str) *asym = {
};
fn perr(a: *asm_, msg: str) void = {
os.write(2, "6a: ".ptr, 4u64);
os.write(2, "w6a: ".ptr, 4u64);
let f: str = a.file;
os.write(2, f.ptr, f.len: u64);
os.write(2, ": ".ptr, 2u64);

View File

@@ -1,12 +1,12 @@
// selfhost/cmd/6a/types.ww — types + constants shared across the
// 6a port. Mirrors cmd/6a/a.h and cmd/6c/6.out.h.
// selfhost/cmd/w6a/types.ww — types + constants shared across the
// w6a port. Mirrors cmd/w6a/a.h and cmd/w6c/6.out.h.
use mem;
// ---- registers + operand kinds (from 6.out.h) -------------------------
// These must stay numerically aligned with the C enum so that ww-cgen
// output (which reads them via `D_AX(SB)` etc.) lands on the same
// integers when read by ww-6a.
// integers when read by ww-w6a.
def D_NONE: i32 = 0;
def D_AX: i32 = 1;
@@ -121,7 +121,7 @@ def A_JNZ: i32 = 58;
def A_SYSCALL: i32 = 59;
// ---- structs (mirror cmd/6a/a.h) --------------------------------------
// ---- structs (mirror cmd/w6a/a.h) --------------------------------------
type aoperand = struct {
atype: i32, // D_NONE / D_AX..D_R15 / D_CONST / D_INDIR / D_EXTERN / D_BRANCH

View File

@@ -18,19 +18,21 @@ export fn assert(cond: bool, msg: str) void = {
if (!cond) { abort(msg); };
};
def SYS_READ: i64 = 0;
def SYS_WRITE: i64 = 1;
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;
def SYS_EXIT: i64 = 60;
def SYS_WAIT4: i64 = 61;
def SYS_UNLINK: i64 = 87;
def SYS_READ: i64 = 0;
def SYS_WRITE: i64 = 1;
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;
def SYS_EXIT: i64 = 60;
def SYS_WAIT4: i64 = 61;
def SYS_UNLINK: i64 = 87;
def SYS_GETCWD: i64 = 79;
def SYS_GETDENTS64: i64 = 217;
// open(2) flags. Linux values, matching <fcntl.h>.
def O_RDONLY: i32 = 0;
@@ -65,7 +67,7 @@ export fn close(fd: i32) 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
// negative errno. Used by w6c_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;
@@ -179,7 +181,33 @@ export fn wait4(pid: i32, status_out: *i32, options: i32, rusage: *void) i32 = {
options: i64, rusage: i64): i32;
};
// selfhost/cmd/wwc/mem.ww — port of cmd/wwc/mem.c.
// getcwd(2) — Linux flavour. Writes the NUL-terminated cwd into `buf`
// and returns the number of bytes written (including the NUL), or a
// negative errno. The driver uses it to expand `.` to the cwd's
// basename for `ww build` / `ww test`.
export fn getcwd(buf: *u8, n: u64) i64 = {
return syscall2(SYS_GETCWD, buf: i64, n: i64);
};
// getdents64(2) — Linux directory enumeration. The fd must be opened
// with O_RDONLY on a directory. `buf` receives a packed sequence of
// linux_dirent64 records:
//
// struct linux_dirent64 {
// u64 d_ino; // 0..7
// i64 d_off; // 8..15
// u16 d_reclen; // 16..17 — total bytes for this record
// u8 d_type; // 18 — DT_REG/DT_DIR/...
// u8 d_name[]; // 19.. — NUL-terminated name + padding
// };
//
// Returns bytes written into `buf` (advance by d_reclen to walk),
// 0 at end-of-directory, or a negative errno.
export fn getdents64(fd: i32, buf: *u8, n: u64) i64 = {
return syscall3(SYS_GETDENTS64, fd: i64, buf: i64, n: i64);
};
// selfhost/cmd/wcc/mem.ww — port of cmd/wcc/mem.c.
//
// Bump arena allocator. Backed by the runtime page allocator
// (rt_alloc / rt_free), no libc. Each chunk is mmap'd; when the
@@ -187,7 +215,7 @@ export fn wait4(pid: i32, status_out: *i32, options: i32, rusage: *void) i32 = {
// unmaps the chain.
//
// Memory handed out is 16-byte aligned. The C version under
// cmd/wwc/ is retained until the three-stage bootstrap diffs clean.
// cmd/wcc/ is retained until the three-stage bootstrap diffs clean.
use os;
@@ -406,8 +434,8 @@ export fn parseu64(s: str) (u64 | str) = {
return v;
};
// selfhost/cmd/wwc/tok.ww — port of cmd/wwc/tok.c plus the Tkind /
// Tok / Pos shapes from cmd/wwc/ww.h.
// selfhost/cmd/wcc/tok.ww — port of cmd/wcc/tok.c plus the Tkind /
// Tok / Pos shapes from cmd/wcc/ww.h.
//
// Token kind values must stay numerically equal to the C side: the
// 990_selfhost test diffs ww-side wwdump output against C-side
@@ -415,13 +443,13 @@ export fn parseu64(s: str) (u64 | str) = {
// integers and breaks the diff.
//
// Bottom of file: tokprint, which emits one token per line in a
// format identical to cmd/wwc/tok.c:tokprint().
// format identical to cmd/wcc/tok.c:tokprint().
use os;
use strconv;
// ---- Tkind ------------------------------------------------------------
// Mirror of the C enum in cmd/wwc/ww.h. Don't reorder.
// Mirror of the C enum in cmd/wcc/ww.h. Don't reorder.
def TK_NONE: i32 = 0;
def TK_EOF: i32 = 1;
@@ -516,7 +544,7 @@ def TK_LAST: i32 = 80;
// ---- Pos / Tok --------------------------------------------------------
//
// `pos` is used at error-reporting boundaries; we always pass it via
// *pos so the value never gets struct-copied (6c can't yet copy a
// *pos so the value never gets struct-copied (w6c can't yet copy a
// 24-byte struct).
//
// `tok` is flat — file/line/col live directly on the token rather than
@@ -554,7 +582,7 @@ fn streq_n(a: *u8, b: str, n: i32) bool = {
// kwlookup — returns the matching TK_* keyword kind for a byte run,
// or TK_NONE if it's an ordinary identifier. Linear search over a
// small alphabetised list, matching cmd/wwc/tok.c.
// small alphabetised list, matching cmd/wcc/tok.c.
export fn kwlookup(p: *u8, n: i32) i32 = {
if (streq_n(p, "as", n)) { return TK_AS; };
if (streq_n(p, "break", n)) { return TK_BREAK; };
@@ -683,7 +711,7 @@ export fn tokname(k: i32) str = {
// ---- writer for tokprint ----------------------------------------------
//
// fputq mirrors cmd/wwc/tok.c:fputq — quote the string with C-style
// fputq mirrors cmd/wcc/tok.c:fputq — quote the string with C-style
// escapes for \, ", \n, \t, \r and \xNN for other non-printables.
fn fputc_byte(fd: i32, b: u8) void = {
@@ -750,14 +778,14 @@ fn fputq(fd: i32, p: *u8, n: i32) void = {
};
// tokprint — write one token line to fd. Format must match
// cmd/wwc/tok.c:tokprint() byte-for-byte: that's the diff anchor.
// cmd/wcc/tok.c:tokprint() byte-for-byte: that's the diff anchor.
// "<file>:<line>:<col> <kindname>[ <value>]\n"
//
// Takes `t` by pointer because 6c can't yet pass a >16-byte struct
// Takes `t` by pointer because w6c can't yet pass a >16-byte struct
// by value; the C version takes Tok by value.
export fn tokprint(fd: i32, t: *tok) void = {
// Chained-dot field reads (`t.x.y`) on str sub-fields aren't yet
// reduced by 6c — `t.x.y` returns the whole str. Lift the str
// reduced by w6c — `t.x.y` returns the whole str. Lift the str
// fields into locals so we can use the str pseudo-field path.
let tfile: str = t.file;
let ttext: str = t.text;
@@ -894,14 +922,14 @@ export fn toupper(c: u8) u8 = {
return c;
};
// selfhost/cmd/wwc/lex.ww — port of cmd/wwc/lex.c.
// selfhost/cmd/wcc/lex.ww — port of cmd/wcc/lex.c.
//
// The DFA, the helpers, and the order of decisions all mirror the C
// version exactly. The 990_selfhost test diffs the resulting token
// stream against the C-side wwdump byte-for-byte; any divergence is
// a port bug.
//
// Calling-convention note: 6c can't yet pass or return structs >16
// Calling-convention note: w6c can't yet pass or return structs >16
// bytes by value, so `tok` and `pos` are passed by pointer (out
// params). The C version passes `Tok` by value; we differ here only
// in shape, not in observable behaviour. Token kind values stay
@@ -1551,7 +1579,7 @@ export fn lexnext(l: *lex, out: *tok) void = {
out.text = astrndup(l.a, one.ptr, 1u64);
};
// selfhost/cmd/wwc/ast.ww — port of cmd/wwc/ast.c (Node defs + printer).
// selfhost/cmd/wcc/ast.ww — port of cmd/wcc/ast.c (Node defs + printer).
//
// Status: AST printer is fully ported. Constructor `newnode` is here.
// The parser (parse.ww) is currently minimal — see its file header.
@@ -1567,7 +1595,7 @@ use tok;
// ---- Nkind ------------------------------------------------------------
//
// Mirror of cmd/wwc/ww.h Nkind. Values must stay numerically equal so
// Mirror of cmd/wcc/ww.h Nkind. Values must stay numerically equal so
// the AST diff probe in 990_selfhost works.
def N_NONE: i32 = 0;
@@ -1887,7 +1915,7 @@ export fn astprint(fd: i32, n: *node) void = {
pr(fd, n, 0);
};
// selfhost/cmd/wwc/parse.ww — port of cmd/wwc/parse.c.
// selfhost/cmd/wcc/parse.ww — port of cmd/wcc/parse.c.
//
// Status: GROWING stub. Currently handles top-level `use IDENT;`,
// `def NAME: TYPE = LIT;`, `type NAME = TYPE;`, and `fn NAME(params)
@@ -1900,7 +1928,7 @@ export fn astprint(fd: i32, n: *node) void = {
// surface form lands here gradually so the AST diff in 990_selfhost
// grows toward whole-language coverage one increment at a time.
//
// Calling-convention shim: 6c can't yet pass a sub-struct field
// Calling-convention shim: w6c can't yet pass a sub-struct field
// (e.g. p.cur.line where p.cur is a `tok` of size 76). The parser
// stores the current token as flat primitive fields rather than a
// nested `tok` struct; `refill` copies a freshly lexed token in.
@@ -2635,7 +2663,7 @@ fn parsestmt(p: *parser) *node = {
};
// expression statement, or tuple-destructure multi-assign:
// a, b = expr;
// Mirrors cmd/wwc/parse.c:1015-1031. We parse the first lvalue
// Mirrors cmd/wcc/parse.c:1015-1031. We parse the first lvalue
// with parseexpr (matches the C side); subsequent lvalues go
// through parsebin(parseunary, 1) so the `=` stays for us to
// consume — parseexpr would absorb it.
@@ -2870,7 +2898,7 @@ export fn parsefile(p: *parser) *node = {
return f;
};
// selfhost/cmd/wwc/type.ww — port of cmd/wwc/type.c.
// selfhost/cmd/wcc/type.ww — port of cmd/wcc/type.c.
//
// Status: full structural port. The C version uses module-globals for
// the primitive types (ty_void, ty_i32, …); ww doesn't have writable
@@ -2882,7 +2910,7 @@ use os;
use mem;
// ---- TypeKind ---------------------------------------------------------
// Numeric values must stay aligned with cmd/wwc/ww.h TypeKind so the
// Numeric values must stay aligned with cmd/wcc/ww.h TypeKind so the
// next diff signal (typed-AST printer / cgen) can compare across the
// two implementations.
@@ -3200,7 +3228,7 @@ export fn type_eq(a: *tinfo, b: *tinfo) bool = {
return true; // primitives match by kind alone
};
// selfhost/cmd/wwc/sym.ww — port of cmd/wwc/sym.c.
// selfhost/cmd/wcc/sym.ww — port of cmd/wcc/sym.c.
//
// Per-scope hashtable, chained to the parent. Lookup walks up.
// Plan 9 / Hare flavoured. Duplicate definitions in the same scope
@@ -3210,7 +3238,7 @@ use mem;
use typ;
use ast;
// Symbol kinds — must stay numerically aligned with cmd/wwc/ww.h Skind.
// Symbol kinds — must stay numerically aligned with cmd/wcc/ww.h Skind.
def SK_NONE: i32 = 0;
def SK_VAR: i32 = 1;
def SK_PARAM: i32 = 2;
@@ -3314,11 +3342,11 @@ export fn scope_define(s: *scope, name: str, k: i32, t: *tinfo, decl: *node) *sy
return sy;
};
// selfhost/cmd/wwc/check.ww — minimal port of cmd/wwc/check.c.
// selfhost/cmd/wcc/check.ww — minimal port of cmd/wcc/check.c.
//
// Status: name-resolution + primitive-type seeding only. Full type
// inference, conversion rules, tagged-union dispatch typing, return-
// type checking, etc. all live in cmd/wwc/check.c (937 lines) and
// type checking, etc. all live in cmd/wcc/check.c (937 lines) and
// will land here in subsequent commits.
//
// What this version does:
@@ -3562,10 +3590,10 @@ export fn check_file(c: *checker, file: *node) void = {
};
};
// selfhost/cmd/wwc/cgen.ww — port of cmd/6c/cgen.c.
// selfhost/cmd/wcc/cgen.ww — port of cmd/w6c/cgen.c.
//
// Status: GROWING. Each subsystem we add is verified by `wwdump_ww -c`
// producing byte-identical output to C-side `6c` for the same source,
// producing byte-identical output to C-side `w6c` for the same source,
// then by assembling + linking + running the result.
//
// Current coverage:
@@ -3760,7 +3788,7 @@ fn local_alloc(c: *cgen, name: str, sz: i32, tnode: *node) i32 = {
fn local_add(c: *cgen, name: str, sz: i32, tnode: *node) i32 = {
// Name-based slot reuse for N_LETs and params: if `name` is
// already declared in this function, return its existing
// offset. Mirrors C cgen (cmd/6c/cgen.c:localoff). Two
// offset. Mirrors C cgen (cmd/w6c/cgen.c:localoff). Two
// disjoint scopes that declare the same name share one slot —
// so `escape` in wwdump (three `let cp: pos;` across separate
// branches) reserves one slot, not three. scan_locals does
@@ -4097,7 +4125,7 @@ fn fnret_lookup(c: *cgen, name: str) *node = {
// ---- def-constant registry ------------------------------------------
//
// `def NAME: T = LIT;` becomes a DATA symbol the C-side 6c emits; an
// `def NAME: T = LIT;` becomes a DATA symbol the C-side w6c emits; an
// ident reference loads it via `MOVQ NAME(SB), AX`. We collect them at
// file load and consult on N_IDENT lookup.
@@ -4725,7 +4753,7 @@ fn node_isunsigned(c: *cgen, n: *node) bool = {
// Walks the base local's declared type and pulls the element
// out — *u8 → u8, [N]u32 → u32, []u64 → u64. Without this the
// compare-codegen for `p[i] >= 48u8` falls back to signed JGE
// instead of JAE, diverging from C 6c on byte indexing.
// instead of JAE, diverging from C w6c on byte indexing.
if (k == N_INDEX) {
let base: *node = n.lhs;
if (base != nil) {
@@ -5198,7 +5226,7 @@ fn cgexpr(c: *cgen, n: *node) void = {
// path the cgen falls through and AX retains whatever the
// inner expression left there — typically the *struct pointer
// itself, so reads silently get the pointer value instead of
// the field. (Showed up porting 6l/pass.ww.)
// the field. (Showed up porting w6l/pass.ww.)
if (lhs != nil) {
if (lhs.kind == N_DOT) {
let inner_t: *node = dot_inner_struct_ptr(c, lhs);
@@ -5487,7 +5515,7 @@ fn cgexpr(c: *cgen, n: *node) void = {
};
cgexpr(c, n.rhs);
// Push order matches C cgen
// (cmd/6c/cgen.c:1033-1041): PUSHQ AX
// (cmd/w6c/cgen.c:1033-1041): PUSHQ AX
// (ptr) first, then PUSHQ BX (len) if
// str, so the pop sequence is POP CX
// (len) → POP AX (ptr) → MOVQ AX,
@@ -6259,7 +6287,7 @@ fn cgstmt(c: *cgen, n: *node) void = {
};
} else {
// Bare `let x: T;` with no initializer. C cgen
// (cmd/6c/cgen.c:2181-2183) zero-inits only when
// (cmd/w6c/cgen.c:2181-2183) zero-inits only when
// the underlying type's natural size is 8 — pointers,
// i64/u64, function pointers, ints. Structs/arrays/
// slices/strings/tagged/tuples are left for per-field
@@ -6330,7 +6358,7 @@ fn cgstmt(c: *cgen, n: *node) void = {
// Tuple-destructure assign: `a, b = call();`. The call's tuple
// return lands in (AX, DX); push DX to free it, store AX into
// the first lvalue, then pop DX into the second. Mirrors
// cmd/6c/cgen.c:2424-2440. Lvalues beyond two are dropped (same
// cmd/w6c/cgen.c:2424-2440. Lvalues beyond two are dropped (same
// as C — no fixture uses >2 today).
if (k == N_MASSIGN) {
if (n.rhs != nil) { cgexpr(c, n.rhs); };
@@ -6645,14 +6673,14 @@ export fn cg_file(c: *cgen, file: *node) void = {
emit_def_constants(c, file);
};
// selfhost/cmd/6c/main.ww — port of cmd/6c/main.c.
// selfhost/cmd/w6c/main.ww — port of cmd/w6c/main.c.
//
// 6c = amd64 compiler. Read .ww, parse, codegen, emit Plan 9 amd64
// w6c = 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
// w6c_ww -o file.s file.ww
//
// The cgen routines in selfhost/cmd/wwc/cgen.ww write directly to
// The cgen routines in selfhost/cmd/wcc/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.
@@ -6710,16 +6738,16 @@ export fn main(argc: i32, argv: **u8) i32 = {
if (streq_cs(a, "-o")) {
i += 1;
if (i >= argc) {
os.write(2, "6c: -o requires arg\n".ptr, 20u64);
os.write(2, "w6c: -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);
os.write(2, "w6c: unknown flag\n".ptr, 17u64);
return 2;
} else {
if (src_cs != nil) {
os.write(2, "6c: only one input\n".ptr, 19u64);
os.write(2, "w6c: only one input\n".ptr, 19u64);
return 2;
};
src_cs = a;
@@ -6728,7 +6756,7 @@ export fn main(argc: i32, argv: **u8) i32 = {
};
if (src_cs == nil) {
os.write(2, "usage: 6c_ww [-o out.s] file.ww\n".ptr, 32u64);
os.write(2, "usage: w6c_ww [-o out.s] file.ww\n".ptr, 32u64);
return 2;
};
@@ -6736,7 +6764,7 @@ export fn main(argc: i32, argv: **u8) i32 = {
let blen: u64;
buf, blen = slurp(src_cs);
if (buf == nil) {
os.write(2, "6c: cannot read input\n".ptr, 22u64);
os.write(2, "w6c: cannot read input\n".ptr, 22u64);
return 1;
};
@@ -6747,11 +6775,11 @@ export fn main(argc: i32, argv: **u8) i32 = {
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);
os.write(2, "w6c: cannot open output\n".ptr, 23u64);
return 1;
};
if (os.dup2(ofd, 1i32) < 0) {
os.write(2, "6c: dup2 failed\n".ptr, 16u64);
os.write(2, "w6c: dup2 failed\n".ptr, 16u64);
os.close(ofd);
return 1;
};

View File

@@ -1,11 +1,11 @@
// selfhost/cmd/6c/main.ww — port of cmd/6c/main.c.
// selfhost/cmd/w6c/main.ww — port of cmd/w6c/main.c.
//
// 6c = amd64 compiler. Read .ww, parse, codegen, emit Plan 9 amd64
// w6c = 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
// w6c_ww -o file.s file.ww
//
// The cgen routines in selfhost/cmd/wwc/cgen.ww write directly to
// The cgen routines in selfhost/cmd/wcc/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.
@@ -63,16 +63,16 @@ export fn main(argc: i32, argv: **u8) i32 = {
if (streq_cs(a, "-o")) {
i += 1;
if (i >= argc) {
os.write(2, "6c: -o requires arg\n".ptr, 20u64);
os.write(2, "w6c: -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);
os.write(2, "w6c: unknown flag\n".ptr, 17u64);
return 2;
} else {
if (src_cs != nil) {
os.write(2, "6c: only one input\n".ptr, 19u64);
os.write(2, "w6c: only one input\n".ptr, 19u64);
return 2;
};
src_cs = a;
@@ -81,7 +81,7 @@ export fn main(argc: i32, argv: **u8) i32 = {
};
if (src_cs == nil) {
os.write(2, "usage: 6c_ww [-o out.s] file.ww\n".ptr, 32u64);
os.write(2, "usage: w6c_ww [-o out.s] file.ww\n".ptr, 32u64);
return 2;
};
@@ -89,7 +89,7 @@ export fn main(argc: i32, argv: **u8) i32 = {
let blen: u64;
buf, blen = slurp(src_cs);
if (buf == nil) {
os.write(2, "6c: cannot read input\n".ptr, 22u64);
os.write(2, "w6c: cannot read input\n".ptr, 22u64);
return 1;
};
@@ -100,11 +100,11 @@ export fn main(argc: i32, argv: **u8) i32 = {
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);
os.write(2, "w6c: cannot open output\n".ptr, 23u64);
return 1;
};
if (os.dup2(ofd, 1i32) < 0) {
os.write(2, "6c: dup2 failed\n".ptr, 16u64);
os.write(2, "w6c: dup2 failed\n".ptr, 16u64);
os.close(ofd);
return 1;
};

View File

@@ -1,4 +1,4 @@
// selfhost/cmd/6l/dyn.ww — port of cmd/6l/dyn.c.
// selfhost/cmd/w6l/dyn.ww — port of cmd/w6l/dyn.c.
//
// Load a shared object (ET_DYN) so the linker knows which symbols it
// exports and which DT_NEEDED entry to record. We do not pull bytes
@@ -167,11 +167,11 @@ export fn l_load_so(l: *lnk, path_cs: *u8) i32 = {
let blen: u64;
buf, blen = read_all_so(path_cs);
if (buf == nil) {
os.write(2, "6l: cannot read .so\n".ptr, 20u64);
os.write(2, "w6l: cannot read .so\n".ptr, 20u64);
return -1;
};
if (blen < 64u64) {
os.write(2, "6l: short ELF\n".ptr, 14u64);
os.write(2, "w6l: short ELF\n".ptr, 14u64);
return -1;
};
if (buf[0u64] != 127u8) { return so_err("not ELF"); };
@@ -359,7 +359,7 @@ export fn l_load_so(l: *lnk, path_cs: *u8) i32 = {
};
fn so_err(msg: str) i32 = {
os.write(2, "6l: ".ptr, 4u64);
os.write(2, "w6l: ".ptr, 4u64);
os.write(2, msg.ptr, msg.len: u64);
os.write(2, "\n".ptr, 1u64);
return -1;

View File

@@ -1,4 +1,4 @@
// selfhost/cmd/6l/dynout.ww — port of cmd/6l/dynout.c.
// selfhost/cmd/w6l/dynout.ww — port of cmd/w6l/dynout.c.
//
// Emit a dynamic-linked ELF executable. The shape is the simplest
// valid one: PT_INTERP + PT_DYNAMIC + DT_BIND_NOW so the loader
@@ -196,7 +196,7 @@ export fn l_emit_dyn_elf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
let i: i32 = 0;
for (i < n) {
if (dynsyms[i] == nil) {
os.write(2, "6l: dynout: no sym for plt_idx\n".ptr, 31u64);
os.write(2, "w6l: dynout: no sym for plt_idx\n".ptr, 31u64);
return 1;
};
i += 1;
@@ -621,7 +621,7 @@ export fn l_emit_dyn_elf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
};
d_wri64(dynamic_buf, dk * 16u64, DT_NULL); d_wr64(dynamic_buf, dk * 16u64 + 8u64, 0u64); dk += 1u64;
if (dk != ndyn) {
os.write(2, "6l: dynamic entry count mismatch\n".ptr, 33u64);
os.write(2, "w6l: dynamic entry count mismatch\n".ptr, 33u64);
return 1;
};
@@ -633,7 +633,7 @@ export fn l_emit_dyn_elf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
if (rsym.is_dyn != 0) {
if (r.kind != R_X86_64_PC32_D) {
if (r.kind != R_X86_64_PLT32_D) {
os.write(2, "6l: dynamic reloc kind unsupported\n".ptr, 35u64);
os.write(2, "w6l: dynamic reloc kind unsupported\n".ptr, 35u64);
return 1;
};
};
@@ -649,7 +649,7 @@ export fn l_emit_dyn_elf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
// ---- assemble file buffer ----
let file_buf: *u8 = os.alloc(file_end): *u8;
if (file_buf == nil) {
os.write(2, "6l: out of memory\n".ptr, 18u64);
os.write(2, "w6l: out of memory\n".ptr, 18u64);
return 1;
};

View File

@@ -18,19 +18,21 @@ export fn assert(cond: bool, msg: str) void = {
if (!cond) { abort(msg); };
};
def SYS_READ: i64 = 0;
def SYS_WRITE: i64 = 1;
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;
def SYS_EXIT: i64 = 60;
def SYS_WAIT4: i64 = 61;
def SYS_UNLINK: i64 = 87;
def SYS_READ: i64 = 0;
def SYS_WRITE: i64 = 1;
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;
def SYS_EXIT: i64 = 60;
def SYS_WAIT4: i64 = 61;
def SYS_UNLINK: i64 = 87;
def SYS_GETCWD: i64 = 79;
def SYS_GETDENTS64: i64 = 217;
// open(2) flags. Linux values, matching <fcntl.h>.
def O_RDONLY: i32 = 0;
@@ -65,7 +67,7 @@ export fn close(fd: i32) 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
// negative errno. Used by w6c_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;
@@ -179,7 +181,33 @@ export fn wait4(pid: i32, status_out: *i32, options: i32, rusage: *void) i32 = {
options: i64, rusage: i64): i32;
};
// selfhost/cmd/wwc/mem.ww — port of cmd/wwc/mem.c.
// getcwd(2) — Linux flavour. Writes the NUL-terminated cwd into `buf`
// and returns the number of bytes written (including the NUL), or a
// negative errno. The driver uses it to expand `.` to the cwd's
// basename for `ww build` / `ww test`.
export fn getcwd(buf: *u8, n: u64) i64 = {
return syscall2(SYS_GETCWD, buf: i64, n: i64);
};
// getdents64(2) — Linux directory enumeration. The fd must be opened
// with O_RDONLY on a directory. `buf` receives a packed sequence of
// linux_dirent64 records:
//
// struct linux_dirent64 {
// u64 d_ino; // 0..7
// i64 d_off; // 8..15
// u16 d_reclen; // 16..17 — total bytes for this record
// u8 d_type; // 18 — DT_REG/DT_DIR/...
// u8 d_name[]; // 19.. — NUL-terminated name + padding
// };
//
// Returns bytes written into `buf` (advance by d_reclen to walk),
// 0 at end-of-directory, or a negative errno.
export fn getdents64(fd: i32, buf: *u8, n: u64) i64 = {
return syscall3(SYS_GETDENTS64, fd: i64, buf: i64, n: i64);
};
// selfhost/cmd/wcc/mem.ww — port of cmd/wcc/mem.c.
//
// Bump arena allocator. Backed by the runtime page allocator
// (rt_alloc / rt_free), no libc. Each chunk is mmap'd; when the
@@ -187,7 +215,7 @@ export fn wait4(pid: i32, status_out: *i32, options: i32, rusage: *void) i32 = {
// unmaps the chain.
//
// Memory handed out is 16-byte aligned. The C version under
// cmd/wwc/ is retained until the three-stage bootstrap diffs clean.
// cmd/wcc/ is retained until the three-stage bootstrap diffs clean.
use os;
@@ -286,7 +314,7 @@ export fn freearena(a: *arena) void = {
};
};
// selfhost/cmd/6l/sym.ww — port of cmd/6l/sym.c.
// selfhost/cmd/w6l/sym.ww — port of cmd/w6l/sym.c.
//
// Linker symbol table. Singly-linked list, usually a few hundred
// entries; hashing isn't worth it yet.
@@ -388,9 +416,9 @@ export fn l_lookup(l: *lnk, name: str) *lsym = {
return nil;
};
// selfhost/cmd/6l/obj.ww — port of cmd/6l/obj.c.
// selfhost/cmd/w6l/obj.ww — port of cmd/w6l/obj.c.
//
// Loads relocatable ELF64 .o files emitted by 6a, appends .text to
// Loads relocatable ELF64 .o files emitted by w6a, appends .text to
// the combined image, and pulls in symbols + relocations with
// offsets adjusted to the combined section.
//
@@ -410,7 +438,7 @@ def SHT_STRTAB: i32 = 3;
def SHT_RELA: i32 = 4;
// ---- little-endian byte readers ----------------------------------------
// 6a/6l use straight LE on amd64. Reading via byte offsets keeps us off
// w6a/w6l use straight LE on amd64. Reading via byte offsets keeps us off
// the cgen's u16 field-load story for now (MOVZBQ exists; MOVZWQ doesn't).
fn rd_u16(p: *u8, off: u64) u16 = {
@@ -663,7 +691,7 @@ fn member_defines_undef(l: *lnk, m: *armember) bool = {
return false;
};
// load_archive — port of cmd/6l/obj.c:load_archive.
// load_archive — port of cmd/w6l/obj.c:load_archive.
//
// Pass 1 indexes every regular member. Pass 2 iteratively pulls in any
// member that supplies a currently-undefined symbol; each pull may
@@ -726,7 +754,7 @@ export fn l_load(l: *lnk, path_cs: *u8) i32 = {
let buflen: u64;
bufp, buflen = read_all(path_cs);
if (bufp == nil) {
os.write(2, "6l: cannot read object\n".ptr, 23u64);
os.write(2, "w6l: cannot read object\n".ptr, 23u64);
return -1;
};
if (is_archive(bufp, buflen)) {
@@ -773,11 +801,11 @@ fn load_image(l: *lnk, path_cs: *u8, buf: *u8, len: u64) i32 = {
i += 1u32;
};
if (idx_text < 0) {
os.write(2, "6l: missing .text\n".ptr, 18u64);
os.write(2, "w6l: missing .text\n".ptr, 18u64);
return -1;
};
if (idx_symtab < 0) {
os.write(2, "6l: missing .symtab\n".ptr, 20u64);
os.write(2, "w6l: missing .symtab\n".ptr, 20u64);
return -1;
};
@@ -824,7 +852,7 @@ fn load_image(l: *lnk, path_cs: *u8, buf: *u8, len: u64) i32 = {
if (st_shndx != 0u16) {
if ((st_shndx: i32) == idx_text) {
if (gs.defined != 0) {
os.write(2, "6l: duplicate symbol\n".ptr, 21u64);
os.write(2, "w6l: duplicate symbol\n".ptr, 21u64);
l.errs += 1;
} else {
gs.defined = 1;
@@ -875,7 +903,7 @@ fn load_image(l: *lnk, path_cs: *u8, buf: *u8, len: u64) i32 = {
return 0;
};
// selfhost/cmd/6l/dyn.ww — port of cmd/6l/dyn.c.
// selfhost/cmd/w6l/dyn.ww — port of cmd/w6l/dyn.c.
//
// Load a shared object (ET_DYN) so the linker knows which symbols it
// exports and which DT_NEEDED entry to record. We do not pull bytes
@@ -1044,11 +1072,11 @@ export fn l_load_so(l: *lnk, path_cs: *u8) i32 = {
let blen: u64;
buf, blen = read_all_so(path_cs);
if (buf == nil) {
os.write(2, "6l: cannot read .so\n".ptr, 20u64);
os.write(2, "w6l: cannot read .so\n".ptr, 20u64);
return -1;
};
if (blen < 64u64) {
os.write(2, "6l: short ELF\n".ptr, 14u64);
os.write(2, "w6l: short ELF\n".ptr, 14u64);
return -1;
};
if (buf[0u64] != 127u8) { return so_err("not ELF"); };
@@ -1236,7 +1264,7 @@ export fn l_load_so(l: *lnk, path_cs: *u8) i32 = {
};
fn so_err(msg: str) i32 = {
os.write(2, "6l: ".ptr, 4u64);
os.write(2, "w6l: ".ptr, 4u64);
os.write(2, msg.ptr, msg.len: u64);
os.write(2, "\n".ptr, 1u64);
return -1;
@@ -1282,7 +1310,7 @@ fn s_eq(a: str, b: str) bool = {
return true;
};
// selfhost/cmd/6l/pass.ww — port of cmd/6l/pass.c.
// selfhost/cmd/w6l/pass.ww — port of cmd/w6l/pass.c.
//
// Resolution + relocation. l_resolve flags every undefined symbol
// referenced by a relocation, and promotes those provided by some
@@ -1345,7 +1373,7 @@ export fn l_resolve(l: *lnk) i32 = {
if (r2.sym != nil) {
if (r2.sym.defined == 0) {
if (r2.sym.is_dyn == 0) {
os.write(2, "6l: undefined reference to '".ptr, 28u64);
os.write(2, "w6l: undefined reference to '".ptr, 28u64);
let nm: str = r2.sym.name;
os.write(2, nm.ptr, nm.len: u64);
os.write(2, "'\n".ptr, 2u64);
@@ -1388,7 +1416,7 @@ export fn l_relocate(l: *lnk, base: u64) i32 = {
let rel: i64 = (target - site: i64) + r.addend;
patch_u32(l.text + r.off, rel: u32);
} else {
os.write(2, "6l: unsupported reloc kind\n".ptr, 27u64);
os.write(2, "w6l: unsupported reloc kind\n".ptr, 27u64);
l.errs += 1;
};};
};
@@ -1398,7 +1426,7 @@ export fn l_relocate(l: *lnk, base: u64) i32 = {
return l.errs;
};
// selfhost/cmd/6l/dynout.ww — port of cmd/6l/dynout.c.
// selfhost/cmd/w6l/dynout.ww — port of cmd/w6l/dynout.c.
//
// Emit a dynamic-linked ELF executable. The shape is the simplest
// valid one: PT_INTERP + PT_DYNAMIC + DT_BIND_NOW so the loader
@@ -1596,7 +1624,7 @@ export fn l_emit_dyn_elf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
let i: i32 = 0;
for (i < n) {
if (dynsyms[i] == nil) {
os.write(2, "6l: dynout: no sym for plt_idx\n".ptr, 31u64);
os.write(2, "w6l: dynout: no sym for plt_idx\n".ptr, 31u64);
return 1;
};
i += 1;
@@ -2021,7 +2049,7 @@ export fn l_emit_dyn_elf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
};
d_wri64(dynamic_buf, dk * 16u64, DT_NULL); d_wr64(dynamic_buf, dk * 16u64 + 8u64, 0u64); dk += 1u64;
if (dk != ndyn) {
os.write(2, "6l: dynamic entry count mismatch\n".ptr, 33u64);
os.write(2, "w6l: dynamic entry count mismatch\n".ptr, 33u64);
return 1;
};
@@ -2033,7 +2061,7 @@ export fn l_emit_dyn_elf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
if (rsym.is_dyn != 0) {
if (r.kind != R_X86_64_PC32_D) {
if (r.kind != R_X86_64_PLT32_D) {
os.write(2, "6l: dynamic reloc kind unsupported\n".ptr, 35u64);
os.write(2, "w6l: dynamic reloc kind unsupported\n".ptr, 35u64);
return 1;
};
};
@@ -2049,7 +2077,7 @@ export fn l_emit_dyn_elf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
// ---- assemble file buffer ----
let file_buf: *u8 = os.alloc(file_end): *u8;
if (file_buf == nil) {
os.write(2, "6l: out of memory\n".ptr, 18u64);
os.write(2, "w6l: out of memory\n".ptr, 18u64);
return 1;
};
@@ -2142,7 +2170,7 @@ export fn l_emit_dyn_elf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
return 0;
};
// selfhost/cmd/6l/out.ww — port of cmd/6l/out.c.
// selfhost/cmd/w6l/out.ww — port of cmd/w6l/out.c.
//
// Emit a static ELF64 executable. File layout (per the C original):
// [0..64) Ehdr
@@ -2243,13 +2271,13 @@ export fn l_emit_elf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
return 0;
};
// selfhost/cmd/6l/main.ww — port of cmd/6l/main.c.
// selfhost/cmd/w6l/main.ww — port of cmd/w6l/main.c.
//
// 6l = amd64 linker. Reads relocatable ELF .o files, SysV `ar`
// w6l = amd64 linker. Reads relocatable ELF .o files, SysV `ar`
// archives, and shared objects (ET_DYN). Resolves symbols, applies
// relocations, writes a static or dynamic-linked ELF executable.
//
// 6l_ww -o out [-L<dir>...] [-l<name>...] file1.o file2.o ...
// w6l_ww -o out [-L<dir>...] [-l<name>...] file1.o file2.o ...
use os;
use mem;
@@ -2471,14 +2499,14 @@ export fn main(argc: i32, argv: **u8) i32 = {
if (streq_cs(a, "-o")) {
i += 1;
if (i >= argc) {
os.write(2, "6l: -o requires argument\n".ptr, 25u64);
os.write(2, "w6l: -o requires argument\n".ptr, 25u64);
return 2;
};
out_path = argv[i];
} else { if (streq_cs(a, "-L")) {
i += 1;
if (i >= argc) {
os.write(2, "6l: -L requires argument\n".ptr, 25u64);
os.write(2, "w6l: -L requires argument\n".ptr, 25u64);
return 2;
};
libdirs[n_libdirs] = argv[i];
@@ -2486,7 +2514,7 @@ export fn main(argc: i32, argv: **u8) i32 = {
} else { if (streq_cs(a, "-l")) {
i += 1;
if (i >= argc) {
os.write(2, "6l: -l requires argument\n".ptr, 25u64);
os.write(2, "w6l: -l requires argument\n".ptr, 25u64);
return 2;
};
lflags[n_lflags] = argv[i];
@@ -2498,7 +2526,7 @@ export fn main(argc: i32, argv: **u8) i32 = {
libdirs[n_libdirs] = a + 2u64;
n_libdirs += 1;
} else {
os.write(2, "6l: bare -L\n".ptr, 12u64);
os.write(2, "w6l: bare -L\n".ptr, 12u64);
return 2;
};
} else { if (a[1u64] == 108u8) {
@@ -2506,16 +2534,16 @@ export fn main(argc: i32, argv: **u8) i32 = {
lflags[n_lflags] = a + 2u64;
n_lflags += 1;
} else {
os.write(2, "6l: bare -l\n".ptr, 12u64);
os.write(2, "w6l: bare -l\n".ptr, 12u64);
return 2;
};
} else {
os.write(2, "6l: unknown flag\n".ptr, 17u64);
os.write(2, "w6l: unknown flag\n".ptr, 17u64);
return 2;
};};
} else {
if (ninputs >= max_inputs) {
os.write(2, "6l: too many inputs\n".ptr, 20u64);
os.write(2, "w6l: too many inputs\n".ptr, 20u64);
return 2;
};
inputs[ninputs] = a;
@@ -2525,11 +2553,11 @@ export fn main(argc: i32, argv: **u8) i32 = {
};
if (out_path == nil) {
os.write(2, "usage: 6l_ww -o exe [-L<dir>...] [-l<name>...] file1.o [file2.o...]\n".ptr, 68u64);
os.write(2, "usage: w6l_ww -o exe [-L<dir>...] [-l<name>...] file1.o [file2.o...]\n".ptr, 68u64);
return 2;
};
if (ninputs == 0) {
os.write(2, "6l: no inputs\n".ptr, 14u64);
os.write(2, "w6l: no inputs\n".ptr, 14u64);
return 2;
};
@@ -2554,7 +2582,7 @@ export fn main(argc: i32, argv: **u8) i32 = {
for (lf < n_lflags) {
let p: *u8 = resolve_lib(a, lflags[lf], libdirs, n_libdirs);
if (p == nil) {
os.write(2, "6l: cannot find -l".ptr, 18u64);
os.write(2, "w6l: cannot find -l".ptr, 18u64);
let nm: *u8 = lflags[lf];
os.write(2, nm, cstrlen_l(nm));
os.write(2, "\n".ptr, 1u64);
@@ -2575,18 +2603,18 @@ export fn main(argc: i32, argv: **u8) i32 = {
if (entry_sym == nil) { entry_sym = l_lookup(l, "main"); }
else { if (entry_sym.defined == 0) { entry_sym = l_lookup(l, "main"); }; };
if (entry_sym == nil) {
os.write(2, "6l: no _start or main symbol\n".ptr, 29u64);
os.write(2, "w6l: no _start or main symbol\n".ptr, 29u64);
return 1;
};
if (entry_sym.defined == 0) {
os.write(2, "6l: no _start or main symbol\n".ptr, 29u64);
os.write(2, "w6l: no _start or main symbol\n".ptr, 29u64);
return 1;
};
let flags: i32 = os.O_WRONLY | os.O_CREAT | os.O_TRUNC;
let fd: i32 = os.open(out_path, flags, 493i32); // 0o755
if (fd < 0) {
os.write(2, "6l: cannot open output\n".ptr, 23u64);
os.write(2, "w6l: cannot open output\n".ptr, 23u64);
return 1;
};

View File

@@ -1,10 +1,10 @@
// selfhost/cmd/6l/main.ww — port of cmd/6l/main.c.
// selfhost/cmd/w6l/main.ww — port of cmd/w6l/main.c.
//
// 6l = amd64 linker. Reads relocatable ELF .o files, SysV `ar`
// w6l = amd64 linker. Reads relocatable ELF .o files, SysV `ar`
// archives, and shared objects (ET_DYN). Resolves symbols, applies
// relocations, writes a static or dynamic-linked ELF executable.
//
// 6l_ww -o out [-L<dir>...] [-l<name>...] file1.o file2.o ...
// w6l_ww -o out [-L<dir>...] [-l<name>...] file1.o file2.o ...
use os;
use mem;
@@ -226,14 +226,14 @@ export fn main(argc: i32, argv: **u8) i32 = {
if (streq_cs(a, "-o")) {
i += 1;
if (i >= argc) {
os.write(2, "6l: -o requires argument\n".ptr, 25u64);
os.write(2, "w6l: -o requires argument\n".ptr, 25u64);
return 2;
};
out_path = argv[i];
} else { if (streq_cs(a, "-L")) {
i += 1;
if (i >= argc) {
os.write(2, "6l: -L requires argument\n".ptr, 25u64);
os.write(2, "w6l: -L requires argument\n".ptr, 25u64);
return 2;
};
libdirs[n_libdirs] = argv[i];
@@ -241,7 +241,7 @@ export fn main(argc: i32, argv: **u8) i32 = {
} else { if (streq_cs(a, "-l")) {
i += 1;
if (i >= argc) {
os.write(2, "6l: -l requires argument\n".ptr, 25u64);
os.write(2, "w6l: -l requires argument\n".ptr, 25u64);
return 2;
};
lflags[n_lflags] = argv[i];
@@ -253,7 +253,7 @@ export fn main(argc: i32, argv: **u8) i32 = {
libdirs[n_libdirs] = a + 2u64;
n_libdirs += 1;
} else {
os.write(2, "6l: bare -L\n".ptr, 12u64);
os.write(2, "w6l: bare -L\n".ptr, 12u64);
return 2;
};
} else { if (a[1u64] == 108u8) {
@@ -261,16 +261,16 @@ export fn main(argc: i32, argv: **u8) i32 = {
lflags[n_lflags] = a + 2u64;
n_lflags += 1;
} else {
os.write(2, "6l: bare -l\n".ptr, 12u64);
os.write(2, "w6l: bare -l\n".ptr, 12u64);
return 2;
};
} else {
os.write(2, "6l: unknown flag\n".ptr, 17u64);
os.write(2, "w6l: unknown flag\n".ptr, 17u64);
return 2;
};};
} else {
if (ninputs >= max_inputs) {
os.write(2, "6l: too many inputs\n".ptr, 20u64);
os.write(2, "w6l: too many inputs\n".ptr, 20u64);
return 2;
};
inputs[ninputs] = a;
@@ -280,11 +280,11 @@ export fn main(argc: i32, argv: **u8) i32 = {
};
if (out_path == nil) {
os.write(2, "usage: 6l_ww -o exe [-L<dir>...] [-l<name>...] file1.o [file2.o...]\n".ptr, 68u64);
os.write(2, "usage: w6l_ww -o exe [-L<dir>...] [-l<name>...] file1.o [file2.o...]\n".ptr, 68u64);
return 2;
};
if (ninputs == 0) {
os.write(2, "6l: no inputs\n".ptr, 14u64);
os.write(2, "w6l: no inputs\n".ptr, 14u64);
return 2;
};
@@ -309,7 +309,7 @@ export fn main(argc: i32, argv: **u8) i32 = {
for (lf < n_lflags) {
let p: *u8 = resolve_lib(a, lflags[lf], libdirs, n_libdirs);
if (p == nil) {
os.write(2, "6l: cannot find -l".ptr, 18u64);
os.write(2, "w6l: cannot find -l".ptr, 18u64);
let nm: *u8 = lflags[lf];
os.write(2, nm, cstrlen_l(nm));
os.write(2, "\n".ptr, 1u64);
@@ -330,18 +330,18 @@ export fn main(argc: i32, argv: **u8) i32 = {
if (entry_sym == nil) { entry_sym = l_lookup(l, "main"); }
else { if (entry_sym.defined == 0) { entry_sym = l_lookup(l, "main"); }; };
if (entry_sym == nil) {
os.write(2, "6l: no _start or main symbol\n".ptr, 29u64);
os.write(2, "w6l: no _start or main symbol\n".ptr, 29u64);
return 1;
};
if (entry_sym.defined == 0) {
os.write(2, "6l: no _start or main symbol\n".ptr, 29u64);
os.write(2, "w6l: no _start or main symbol\n".ptr, 29u64);
return 1;
};
let flags: i32 = os.O_WRONLY | os.O_CREAT | os.O_TRUNC;
let fd: i32 = os.open(out_path, flags, 493i32); // 0o755
if (fd < 0) {
os.write(2, "6l: cannot open output\n".ptr, 23u64);
os.write(2, "w6l: cannot open output\n".ptr, 23u64);
return 1;
};

View File

@@ -1,6 +1,6 @@
// selfhost/cmd/6l/obj.ww — port of cmd/6l/obj.c.
// selfhost/cmd/w6l/obj.ww — port of cmd/w6l/obj.c.
//
// Loads relocatable ELF64 .o files emitted by 6a, appends .text to
// Loads relocatable ELF64 .o files emitted by w6a, appends .text to
// the combined image, and pulls in symbols + relocations with
// offsets adjusted to the combined section.
//
@@ -20,7 +20,7 @@ def SHT_STRTAB: i32 = 3;
def SHT_RELA: i32 = 4;
// ---- little-endian byte readers ----------------------------------------
// 6a/6l use straight LE on amd64. Reading via byte offsets keeps us off
// w6a/w6l use straight LE on amd64. Reading via byte offsets keeps us off
// the cgen's u16 field-load story for now (MOVZBQ exists; MOVZWQ doesn't).
fn rd_u16(p: *u8, off: u64) u16 = {
@@ -273,7 +273,7 @@ fn member_defines_undef(l: *lnk, m: *armember) bool = {
return false;
};
// load_archive — port of cmd/6l/obj.c:load_archive.
// load_archive — port of cmd/w6l/obj.c:load_archive.
//
// Pass 1 indexes every regular member. Pass 2 iteratively pulls in any
// member that supplies a currently-undefined symbol; each pull may
@@ -336,7 +336,7 @@ export fn l_load(l: *lnk, path_cs: *u8) i32 = {
let buflen: u64;
bufp, buflen = read_all(path_cs);
if (bufp == nil) {
os.write(2, "6l: cannot read object\n".ptr, 23u64);
os.write(2, "w6l: cannot read object\n".ptr, 23u64);
return -1;
};
if (is_archive(bufp, buflen)) {
@@ -383,11 +383,11 @@ fn load_image(l: *lnk, path_cs: *u8, buf: *u8, len: u64) i32 = {
i += 1u32;
};
if (idx_text < 0) {
os.write(2, "6l: missing .text\n".ptr, 18u64);
os.write(2, "w6l: missing .text\n".ptr, 18u64);
return -1;
};
if (idx_symtab < 0) {
os.write(2, "6l: missing .symtab\n".ptr, 20u64);
os.write(2, "w6l: missing .symtab\n".ptr, 20u64);
return -1;
};
@@ -434,7 +434,7 @@ fn load_image(l: *lnk, path_cs: *u8, buf: *u8, len: u64) i32 = {
if (st_shndx != 0u16) {
if ((st_shndx: i32) == idx_text) {
if (gs.defined != 0) {
os.write(2, "6l: duplicate symbol\n".ptr, 21u64);
os.write(2, "w6l: duplicate symbol\n".ptr, 21u64);
l.errs += 1;
} else {
gs.defined = 1;

View File

@@ -1,4 +1,4 @@
// selfhost/cmd/6l/out.ww — port of cmd/6l/out.c.
// selfhost/cmd/w6l/out.ww — port of cmd/w6l/out.c.
//
// Emit a static ELF64 executable. File layout (per the C original):
// [0..64) Ehdr

View File

@@ -1,4 +1,4 @@
// selfhost/cmd/6l/pass.ww — port of cmd/6l/pass.c.
// selfhost/cmd/w6l/pass.ww — port of cmd/w6l/pass.c.
//
// Resolution + relocation. l_resolve flags every undefined symbol
// referenced by a relocation, and promotes those provided by some
@@ -61,7 +61,7 @@ export fn l_resolve(l: *lnk) i32 = {
if (r2.sym != nil) {
if (r2.sym.defined == 0) {
if (r2.sym.is_dyn == 0) {
os.write(2, "6l: undefined reference to '".ptr, 28u64);
os.write(2, "w6l: undefined reference to '".ptr, 28u64);
let nm: str = r2.sym.name;
os.write(2, nm.ptr, nm.len: u64);
os.write(2, "'\n".ptr, 2u64);
@@ -104,7 +104,7 @@ export fn l_relocate(l: *lnk, base: u64) i32 = {
let rel: i64 = (target - site: i64) + r.addend;
patch_u32(l.text + r.off, rel: u32);
} else {
os.write(2, "6l: unsupported reloc kind\n".ptr, 27u64);
os.write(2, "w6l: unsupported reloc kind\n".ptr, 27u64);
l.errs += 1;
};};
};

View File

@@ -1,4 +1,4 @@
// selfhost/cmd/6l/sym.ww — port of cmd/6l/sym.c.
// selfhost/cmd/w6l/sym.ww — port of cmd/w6l/sym.c.
//
// Linker symbol table. Singly-linked list, usually a few hundred
// entries; hashing isn't worth it yet.

View File

@@ -1,4 +1,4 @@
// selfhost/cmd/wwc/ast.ww — port of cmd/wwc/ast.c (Node defs + printer).
// selfhost/cmd/wcc/ast.ww — port of cmd/wcc/ast.c (Node defs + printer).
//
// Status: AST printer is fully ported. Constructor `newnode` is here.
// The parser (parse.ww) is currently minimal — see its file header.
@@ -14,7 +14,7 @@ use tok;
// ---- Nkind ------------------------------------------------------------
//
// Mirror of cmd/wwc/ww.h Nkind. Values must stay numerically equal so
// Mirror of cmd/wcc/ww.h Nkind. Values must stay numerically equal so
// the AST diff probe in 990_selfhost works.
def N_NONE: i32 = 0;

View File

@@ -1,7 +1,7 @@
// selfhost/cmd/wwc/cgen.ww — port of cmd/6c/cgen.c.
// selfhost/cmd/wcc/cgen.ww — port of cmd/w6c/cgen.c.
//
// Status: GROWING. Each subsystem we add is verified by `wwdump_ww -c`
// producing byte-identical output to C-side `6c` for the same source,
// producing byte-identical output to C-side `w6c` for the same source,
// then by assembling + linking + running the result.
//
// Current coverage:
@@ -196,7 +196,7 @@ fn local_alloc(c: *cgen, name: str, sz: i32, tnode: *node) i32 = {
fn local_add(c: *cgen, name: str, sz: i32, tnode: *node) i32 = {
// Name-based slot reuse for N_LETs and params: if `name` is
// already declared in this function, return its existing
// offset. Mirrors C cgen (cmd/6c/cgen.c:localoff). Two
// offset. Mirrors C cgen (cmd/w6c/cgen.c:localoff). Two
// disjoint scopes that declare the same name share one slot —
// so `escape` in wwdump (three `let cp: pos;` across separate
// branches) reserves one slot, not three. scan_locals does
@@ -533,7 +533,7 @@ fn fnret_lookup(c: *cgen, name: str) *node = {
// ---- def-constant registry ------------------------------------------
//
// `def NAME: T = LIT;` becomes a DATA symbol the C-side 6c emits; an
// `def NAME: T = LIT;` becomes a DATA symbol the C-side w6c emits; an
// ident reference loads it via `MOVQ NAME(SB), AX`. We collect them at
// file load and consult on N_IDENT lookup.
@@ -1161,7 +1161,7 @@ fn node_isunsigned(c: *cgen, n: *node) bool = {
// Walks the base local's declared type and pulls the element
// out — *u8 → u8, [N]u32 → u32, []u64 → u64. Without this the
// compare-codegen for `p[i] >= 48u8` falls back to signed JGE
// instead of JAE, diverging from C 6c on byte indexing.
// instead of JAE, diverging from C w6c on byte indexing.
if (k == N_INDEX) {
let base: *node = n.lhs;
if (base != nil) {
@@ -1634,7 +1634,7 @@ fn cgexpr(c: *cgen, n: *node) void = {
// path the cgen falls through and AX retains whatever the
// inner expression left there — typically the *struct pointer
// itself, so reads silently get the pointer value instead of
// the field. (Showed up porting 6l/pass.ww.)
// the field. (Showed up porting w6l/pass.ww.)
if (lhs != nil) {
if (lhs.kind == N_DOT) {
let inner_t: *node = dot_inner_struct_ptr(c, lhs);
@@ -1923,7 +1923,7 @@ fn cgexpr(c: *cgen, n: *node) void = {
};
cgexpr(c, n.rhs);
// Push order matches C cgen
// (cmd/6c/cgen.c:1033-1041): PUSHQ AX
// (cmd/w6c/cgen.c:1033-1041): PUSHQ AX
// (ptr) first, then PUSHQ BX (len) if
// str, so the pop sequence is POP CX
// (len) → POP AX (ptr) → MOVQ AX,
@@ -2695,7 +2695,7 @@ fn cgstmt(c: *cgen, n: *node) void = {
};
} else {
// Bare `let x: T;` with no initializer. C cgen
// (cmd/6c/cgen.c:2181-2183) zero-inits only when
// (cmd/w6c/cgen.c:2181-2183) zero-inits only when
// the underlying type's natural size is 8 — pointers,
// i64/u64, function pointers, ints. Structs/arrays/
// slices/strings/tagged/tuples are left for per-field
@@ -2766,7 +2766,7 @@ fn cgstmt(c: *cgen, n: *node) void = {
// Tuple-destructure assign: `a, b = call();`. The call's tuple
// return lands in (AX, DX); push DX to free it, store AX into
// the first lvalue, then pop DX into the second. Mirrors
// cmd/6c/cgen.c:2424-2440. Lvalues beyond two are dropped (same
// cmd/w6c/cgen.c:2424-2440. Lvalues beyond two are dropped (same
// as C — no fixture uses >2 today).
if (k == N_MASSIGN) {
if (n.rhs != nil) { cgexpr(c, n.rhs); };

View File

@@ -1,8 +1,8 @@
// selfhost/cmd/wwc/check.ww — minimal port of cmd/wwc/check.c.
// selfhost/cmd/wcc/check.ww — minimal port of cmd/wcc/check.c.
//
// Status: name-resolution + primitive-type seeding only. Full type
// inference, conversion rules, tagged-union dispatch typing, return-
// type checking, etc. all live in cmd/wwc/check.c (937 lines) and
// type checking, etc. all live in cmd/wcc/check.c (937 lines) and
// will land here in subsequent commits.
//
// What this version does:

View File

@@ -1,4 +1,4 @@
// selfhost/cmd/wwc/err.ww — port of cmd/wwc/err.c.
// selfhost/cmd/wcc/err.ww — port of cmd/wcc/err.c.
//
// Diagnostics. Plan 9 style: short, no levels beyond fatal/error/warn.
// Output goes through os.write so we don't pull in libc stdio.

View File

@@ -1,11 +1,11 @@
// selfhost/cmd/wwc/lex.ww — port of cmd/wwc/lex.c.
// selfhost/cmd/wcc/lex.ww — port of cmd/wcc/lex.c.
//
// The DFA, the helpers, and the order of decisions all mirror the C
// version exactly. The 990_selfhost test diffs the resulting token
// stream against the C-side wwdump byte-for-byte; any divergence is
// a port bug.
//
// Calling-convention note: 6c can't yet pass or return structs >16
// Calling-convention note: w6c can't yet pass or return structs >16
// bytes by value, so `tok` and `pos` are passed by pointer (out
// params). The C version passes `Tok` by value; we differ here only
// in shape, not in observable behaviour. Token kind values stay

View File

@@ -1,4 +1,4 @@
// selfhost/cmd/wwc/mem.ww — port of cmd/wwc/mem.c.
// selfhost/cmd/wcc/mem.ww — port of cmd/wcc/mem.c.
//
// Bump arena allocator. Backed by the runtime page allocator
// (rt_alloc / rt_free), no libc. Each chunk is mmap'd; when the
@@ -6,7 +6,7 @@
// unmaps the chain.
//
// Memory handed out is 16-byte aligned. The C version under
// cmd/wwc/ is retained until the three-stage bootstrap diffs clean.
// cmd/wcc/ is retained until the three-stage bootstrap diffs clean.
use os;

View File

@@ -1,4 +1,4 @@
// selfhost/cmd/wwc/parse.ww — port of cmd/wwc/parse.c.
// selfhost/cmd/wcc/parse.ww — port of cmd/wcc/parse.c.
//
// Status: GROWING stub. Currently handles top-level `use IDENT;`,
// `def NAME: TYPE = LIT;`, `type NAME = TYPE;`, and `fn NAME(params)
@@ -11,7 +11,7 @@
// surface form lands here gradually so the AST diff in 990_selfhost
// grows toward whole-language coverage one increment at a time.
//
// Calling-convention shim: 6c can't yet pass a sub-struct field
// Calling-convention shim: w6c can't yet pass a sub-struct field
// (e.g. p.cur.line where p.cur is a `tok` of size 76). The parser
// stores the current token as flat primitive fields rather than a
// nested `tok` struct; `refill` copies a freshly lexed token in.
@@ -746,7 +746,7 @@ fn parsestmt(p: *parser) *node = {
};
// expression statement, or tuple-destructure multi-assign:
// a, b = expr;
// Mirrors cmd/wwc/parse.c:1015-1031. We parse the first lvalue
// Mirrors cmd/wcc/parse.c:1015-1031. We parse the first lvalue
// with parseexpr (matches the C side); subsequent lvalues go
// through parsebin(parseunary, 1) so the `=` stays for us to
// consume — parseexpr would absorb it.

View File

@@ -1,4 +1,4 @@
// selfhost/cmd/wwc/sym.ww — port of cmd/wwc/sym.c.
// selfhost/cmd/wcc/sym.ww — port of cmd/wcc/sym.c.
//
// Per-scope hashtable, chained to the parent. Lookup walks up.
// Plan 9 / Hare flavoured. Duplicate definitions in the same scope
@@ -8,7 +8,7 @@ use mem;
use typ;
use ast;
// Symbol kinds — must stay numerically aligned with cmd/wwc/ww.h Skind.
// Symbol kinds — must stay numerically aligned with cmd/wcc/ww.h Skind.
def SK_NONE: i32 = 0;
def SK_VAR: i32 = 1;
def SK_PARAM: i32 = 2;

View File

@@ -1,5 +1,5 @@
// selfhost/cmd/wwc/tok.ww — port of cmd/wwc/tok.c plus the Tkind /
// Tok / Pos shapes from cmd/wwc/ww.h.
// selfhost/cmd/wcc/tok.ww — port of cmd/wcc/tok.c plus the Tkind /
// Tok / Pos shapes from cmd/wcc/ww.h.
//
// Token kind values must stay numerically equal to the C side: the
// 990_selfhost test diffs ww-side wwdump output against C-side
@@ -7,13 +7,13 @@
// integers and breaks the diff.
//
// Bottom of file: tokprint, which emits one token per line in a
// format identical to cmd/wwc/tok.c:tokprint().
// format identical to cmd/wcc/tok.c:tokprint().
use os;
use strconv;
// ---- Tkind ------------------------------------------------------------
// Mirror of the C enum in cmd/wwc/ww.h. Don't reorder.
// Mirror of the C enum in cmd/wcc/ww.h. Don't reorder.
def TK_NONE: i32 = 0;
def TK_EOF: i32 = 1;
@@ -108,7 +108,7 @@ def TK_LAST: i32 = 80;
// ---- Pos / Tok --------------------------------------------------------
//
// `pos` is used at error-reporting boundaries; we always pass it via
// *pos so the value never gets struct-copied (6c can't yet copy a
// *pos so the value never gets struct-copied (w6c can't yet copy a
// 24-byte struct).
//
// `tok` is flat — file/line/col live directly on the token rather than
@@ -146,7 +146,7 @@ fn streq_n(a: *u8, b: str, n: i32) bool = {
// kwlookup — returns the matching TK_* keyword kind for a byte run,
// or TK_NONE if it's an ordinary identifier. Linear search over a
// small alphabetised list, matching cmd/wwc/tok.c.
// small alphabetised list, matching cmd/wcc/tok.c.
export fn kwlookup(p: *u8, n: i32) i32 = {
if (streq_n(p, "as", n)) { return TK_AS; };
if (streq_n(p, "break", n)) { return TK_BREAK; };
@@ -275,7 +275,7 @@ export fn tokname(k: i32) str = {
// ---- writer for tokprint ----------------------------------------------
//
// fputq mirrors cmd/wwc/tok.c:fputq — quote the string with C-style
// fputq mirrors cmd/wcc/tok.c:fputq — quote the string with C-style
// escapes for \, ", \n, \t, \r and \xNN for other non-printables.
fn fputc_byte(fd: i32, b: u8) void = {
@@ -342,14 +342,14 @@ fn fputq(fd: i32, p: *u8, n: i32) void = {
};
// tokprint — write one token line to fd. Format must match
// cmd/wwc/tok.c:tokprint() byte-for-byte: that's the diff anchor.
// cmd/wcc/tok.c:tokprint() byte-for-byte: that's the diff anchor.
// "<file>:<line>:<col> <kindname>[ <value>]\n"
//
// Takes `t` by pointer because 6c can't yet pass a >16-byte struct
// Takes `t` by pointer because w6c can't yet pass a >16-byte struct
// by value; the C version takes Tok by value.
export fn tokprint(fd: i32, t: *tok) void = {
// Chained-dot field reads (`t.x.y`) on str sub-fields aren't yet
// reduced by 6c — `t.x.y` returns the whole str. Lift the str
// reduced by w6c — `t.x.y` returns the whole str. Lift the str
// fields into locals so we can use the str pseudo-field path.
let tfile: str = t.file;
let ttext: str = t.text;

View File

@@ -1,4 +1,4 @@
// selfhost/cmd/wwc/type.ww — port of cmd/wwc/type.c.
// selfhost/cmd/wcc/type.ww — port of cmd/wcc/type.c.
//
// Status: full structural port. The C version uses module-globals for
// the primitive types (ty_void, ty_i32, …); ww doesn't have writable
@@ -10,7 +10,7 @@ use os;
use mem;
// ---- TypeKind ---------------------------------------------------------
// Numeric values must stay aligned with cmd/wwc/ww.h TypeKind so the
// Numeric values must stay aligned with cmd/wcc/ww.h TypeKind so the
// next diff signal (typed-AST printer / cgen) can compare across the
// two implementations.

View File

@@ -18,19 +18,21 @@ export fn assert(cond: bool, msg: str) void = {
if (!cond) { abort(msg); };
};
def SYS_READ: i64 = 0;
def SYS_WRITE: i64 = 1;
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;
def SYS_EXIT: i64 = 60;
def SYS_WAIT4: i64 = 61;
def SYS_UNLINK: i64 = 87;
def SYS_READ: i64 = 0;
def SYS_WRITE: i64 = 1;
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;
def SYS_EXIT: i64 = 60;
def SYS_WAIT4: i64 = 61;
def SYS_UNLINK: i64 = 87;
def SYS_GETCWD: i64 = 79;
def SYS_GETDENTS64: i64 = 217;
// open(2) flags. Linux values, matching <fcntl.h>.
def O_RDONLY: i32 = 0;
@@ -65,7 +67,7 @@ export fn close(fd: i32) 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
// negative errno. Used by w6c_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;
@@ -179,7 +181,33 @@ export fn wait4(pid: i32, status_out: *i32, options: i32, rusage: *void) i32 = {
options: i64, rusage: i64): i32;
};
// selfhost/cmd/wwc/mem.ww — port of cmd/wwc/mem.c.
// getcwd(2) — Linux flavour. Writes the NUL-terminated cwd into `buf`
// and returns the number of bytes written (including the NUL), or a
// negative errno. The driver uses it to expand `.` to the cwd's
// basename for `ww build` / `ww test`.
export fn getcwd(buf: *u8, n: u64) i64 = {
return syscall2(SYS_GETCWD, buf: i64, n: i64);
};
// getdents64(2) — Linux directory enumeration. The fd must be opened
// with O_RDONLY on a directory. `buf` receives a packed sequence of
// linux_dirent64 records:
//
// struct linux_dirent64 {
// u64 d_ino; // 0..7
// i64 d_off; // 8..15
// u16 d_reclen; // 16..17 — total bytes for this record
// u8 d_type; // 18 — DT_REG/DT_DIR/...
// u8 d_name[]; // 19.. — NUL-terminated name + padding
// };
//
// Returns bytes written into `buf` (advance by d_reclen to walk),
// 0 at end-of-directory, or a negative errno.
export fn getdents64(fd: i32, buf: *u8, n: u64) i64 = {
return syscall3(SYS_GETDENTS64, fd: i64, buf: i64, n: i64);
};
// selfhost/cmd/wcc/mem.ww — port of cmd/wcc/mem.c.
//
// Bump arena allocator. Backed by the runtime page allocator
// (rt_alloc / rt_free), no libc. Each chunk is mmap'd; when the
@@ -187,7 +215,7 @@ export fn wait4(pid: i32, status_out: *i32, options: i32, rusage: *void) i32 = {
// unmaps the chain.
//
// Memory handed out is 16-byte aligned. The C version under
// cmd/wwc/ is retained until the three-stage bootstrap diffs clean.
// cmd/wcc/ is retained until the three-stage bootstrap diffs clean.
use os;
@@ -290,13 +318,13 @@ export fn freearena(a: *arena) void = {
//
// The user-facing driver. Plan 9 cc(1) / Hare hare(1) analogue:
//
// ww build foo.ww → 6c foo.ww > foo.s ; 6a foo.s > foo.o ;
// 6l -o foo foo.o libwwrt.a
// ww build foo.ww → w6c foo.ww > foo.s ; w6a foo.s > foo.o ;
// w6l -o foo foo.o libwwrt.a
// ww run foo.ww → build then exec
// ww version → print version
//
// Tool paths default to siblings of $0 so a fresh build runs out of
// out/bin/. Env-var overrides (WW_6C / WW_6A / WW_6L / WW_LIB) are
// out/bin/. Env-var overrides (WW_W6C / WW_W6A / WW_W6L / WW_LIB) are
// not yet supported in this port; the bootstrap doesn't need them.
use os;
@@ -696,7 +724,7 @@ fn append_lit(stem: *u8, suffix: str) *u8 = {
};
// linker flags bundled as a struct so build_one stays at the wwstage
// 6c's 6-argument calling-convention limit.
// w6c's 6-argument calling-convention limit.
type lflags = struct {
libdirs: **u8,
n_libdirs: i32,
@@ -706,7 +734,7 @@ type lflags = struct {
// build_one — compile `src` into the executable named `out`.
// self_dir: NUL-terminated dir containing this driver and the
// wwstage tools (6c_ww/6a_ww/6l_ww)
// wwstage tools (w6c_ww/w6a_ww/w6l_ww)
// src: NUL-terminated path to the .ww file
// out: NUL-terminated desired output path
// incs: NUL-terminated colon-list of -I dirs (may be empty)
@@ -714,14 +742,14 @@ type lflags = struct {
//
// The ww-side driver shells to the ww-side tools so a `ww_ww build`
// touches no C-built code at runtime. The C `ww` driver in cmd/ww/
// still drives the C-built 6c/6a/6l. Test 993 pins the two
// still drives the C-built w6c/w6a/w6l. Test 993 pins the two
// pipelines to byte-identical output on a corpus.
fn build_one(self_dir: *u8, src: *u8, out: *u8, incs: *u8, lf: *lflags) i32 = {
let a: *arena = newarena();
let c6: *u8 = join_path_lit(self_dir, "6c_ww");
let a6: *u8 = join_path_lit(self_dir, "6a_ww");
let l6: *u8 = join_path_lit(self_dir, "6l_ww");
let c6: *u8 = join_path_lit(self_dir, "w6c_ww");
let a6: *u8 = join_path_lit(self_dir, "w6a_ww");
let l6: *u8 = join_path_lit(self_dir, "w6l_ww");
// Default lib search path: <self_dir>/../../lib
let dotdot_lib: *u8 = os.alloc(PATH_MAX): *u8;
@@ -774,35 +802,35 @@ fn build_one(self_dir: *u8, src: *u8, out: *u8, incs: *u8, lf: *lflags) i32 = {
};
os.close(cf);
// Step 2: 6c -o <stem>.s <stem>.combined.ww
// Step 2: w6c -o <stem>.s <stem>.combined.ww
{
let argv: **u8 = os.alloc(40u64): **u8;
argv[0] = "6c\0".ptr;
argv[0] = "w6c\0".ptr;
argv[1] = "-o\0".ptr;
argv[2] = asmf;
argv[3] = combined;
argv[4] = nil;
if (proc_run(c6, argv) != 0) {
os.write(2, "ww: 6c failed\n".ptr, 14u64);
os.write(2, "ww: w6c failed\n".ptr, 15u64);
return 1;
};
};
// Step 3: 6a -o <stem>.o <stem>.s
// Step 3: w6a -o <stem>.o <stem>.s
{
let argv: **u8 = os.alloc(40u64): **u8;
argv[0] = "6a\0".ptr;
argv[0] = "w6a\0".ptr;
argv[1] = "-o\0".ptr;
argv[2] = objf;
argv[3] = asmf;
argv[4] = nil;
if (proc_run(a6, argv) != 0) {
os.write(2, "ww: 6a failed\n".ptr, 14u64);
os.write(2, "ww: w6a failed\n".ptr, 15u64);
return 1;
};
};
// Step 4: 6l -o <out> <stem>.o libwwrt.a [-L<dir>...] [-l<name>...]
// Step 4: w6l -o <out> <stem>.o libwwrt.a [-L<dir>...] [-l<name>...]
{
let nl_dirs: i32 = 0;
let nl_libs: i32 = 0;
@@ -814,13 +842,13 @@ fn build_one(self_dir: *u8, src: *u8, out: *u8, incs: *u8, lf: *lflags) i32 = {
l_dirs = lf.libdirs;
l_libs = lf.libs;
};
// argv slots: 5 fixed (6l, -o, out, objf, libwwrt)
// argv slots: 5 fixed (w6l, -o, out, objf, libwwrt)
// + 2 * n_libdirs (-L, dir)
// + 2 * n_libs (-l, name)
// + 1 nil terminator.
let total: i32 = 5 + 2 * nl_dirs + 2 * nl_libs + 1;
let argv: **u8 = os.alloc((total: u64) * 8u64): **u8;
argv[0] = "6l\0".ptr;
argv[0] = "w6l\0".ptr;
argv[1] = "-o\0".ptr;
argv[2] = out;
argv[3] = objf;
@@ -842,17 +870,139 @@ fn build_one(self_dir: *u8, src: *u8, out: *u8, incs: *u8, lf: *lflags) i32 = {
};
argv[pos] = nil;
if (proc_run(l6, argv) != 0) {
os.write(2, "ww: 6l failed\n".ptr, 14u64);
os.write(2, "ww: w6l failed\n".ptr, 15u64);
return 1;
};
};
return 0;
};
// ---- Module-by-name resolution ----------------------------------------
//
// Mirrors cmd/ww/main.c:resolve_module. Maps a name like "foo", "lib/foo",
// "foo.ww", or "." to a concrete .ww file path:
// 1. literal <name>.ww that exists → use as-is
// 2. "." → <cwd>/<basename(cwd)>.ww → that, if it exists
// 3. <name>/<basename(name)>.ww → that, if it exists
// 4. walk search path (cwd:incs:<self_dir>/../../lib):
// <dir>/<name>.ww or <dir>/<name>/<name>.ww
fn cstr_endswith_lit(p: *u8, lit: str) bool = {
let plen: u64 = cstrlen(p);
let slen: u64 = lit.len: u64;
if (plen < slen) { return false; };
let off: u64 = plen - slen;
let i: i32 = 0;
for (i < lit.len) {
let iu: u64 = i: u64;
if (p[off + iu] != lit[i]) { return false; };
i += 1;
};
return true;
};
// basename_off — return the offset of the last path segment within `p`
// (i.e. one past the final '/'). Returns 0 if there's no slash.
fn basename_off(p: *u8, plen: u64) u64 = {
let start: u64 = 0u64;
let i: u64 = 0u64;
for (i < plen) {
if (p[i] == 47u8) { start = i + 1u64; }; // '/'
i += 1u64;
};
return start;
};
// arena_dup_cstr — copy `plen` bytes from `src` into a fresh NUL-sealed
// arena buffer.
fn arena_dup_cstr(a: *arena, src: *u8, plen: u64) *u8 = {
let buf: *u8 = amalloc(a, plen + 1u64): *u8;
let i: u64 = 0u64;
for (i < plen) { buf[i] = src[i]; i += 1u64; };
buf[plen] = 0u8;
return buf;
};
// build_dir_module_path — alloc <dir>/<base>.ww, NUL-terminated, in `a`.
fn build_dir_module_path(a: *arena, dir: *u8, dlen: u64,
base: *u8, blen: u64) *u8 = {
let need: u64 = dlen + 1u64 + blen + 3u64 + 1u64;
let buf: *u8 = amalloc(a, need): *u8;
let off: u64 = 0u64;
let i: u64 = 0u64;
for (i < dlen) { buf[off] = dir[i]; off += 1u64; i += 1u64; };
buf[off] = 47u8; off += 1u64; // '/'
i = 0u64;
for (i < blen) { buf[off] = base[i]; off += 1u64; i += 1u64; };
buf[off] = 46u8; off += 1u64; // '.'
buf[off] = 119u8; off += 1u64; // 'w'
buf[off] = 119u8; off += 1u64; // 'w'
buf[off] = 0u8;
return buf;
};
// build_search_path — compose the colon-separated lookup path used by
// resolve_module's case (4). Order: "." : <incs> : <self_dir>/../../lib
fn build_search_path(a: *arena, self_dir: *u8, incs: *u8) *u8 = {
let buf: *u8 = amalloc(a, PATH_MAX * 2u64): *u8;
let off: u64 = 0u64;
buf[off] = 46u8; off += 1u64; // '.'
if (incs != nil) {
if (incs[0u64] != 0u8) {
buf[off] = 58u8; off += 1u64; // ':'
off = cstr_into(buf, off, incs);
};
};
buf[off] = 58u8; off += 1u64;
off = cstr_into(buf, off, self_dir);
off = str_into(buf, off, "/../../lib");
cstr_seal(buf, off);
return buf;
};
fn resolve_module(a: *arena, self_dir: *u8, name: *u8, incs: *u8) *u8 = {
let nlen: u64 = cstrlen(name);
// (1) literal .ww file that exists
if (cstr_endswith_lit(name, ".ww")) {
if (os.access(name, 0i32) == 0) {
return arena_dup_cstr(a, name, nlen);
};
};
// (2) "." → cwd's <basename>.ww
if (nlen == 1u64) {
if (name[0u64] == 46u8) { // '.'
let cwd: *u8 = amalloc(a, PATH_MAX): *u8;
let r: i64 = os.getcwd(cwd, PATH_MAX);
if (r <= 0i64) { return nil; };
let cwdlen: u64 = (r: u64) - 1u64; // strip trailing NUL
let bo: u64 = basename_off(cwd, cwdlen);
let blen: u64 = cwdlen - bo;
let dot: *u8 = amalloc(a, 2u64): *u8;
dot[0] = 46u8; dot[1] = 0u8;
let probe: *u8 = build_dir_module_path(a, dot, 1u64,
cwd + bo, blen);
if (os.access(probe, 0i32) == 0) { return probe; };
return nil;
};
};
// (3) <name>/<basename(name)>.ww — directory-as-module
let bo: u64 = basename_off(name, nlen);
let probe: *u8 = build_dir_module_path(a, name, nlen,
name + bo, nlen - bo);
if (os.access(probe, 0i32) == 0) { return probe; };
// (4) search path lookup
let search: *u8 = build_search_path(a, self_dir, incs);
return locate_import(a, search, name, nlen);
};
// ---- Subcommand handlers ----------------------------------------------
fn write_usage(fd: i32) void = {
let s: str = "usage: ww [-V] <subcommand> [args...]\n -V print version and exit\n build <path> compile module to a static binary\n run <path> build then exec\n version print version and exit\n";
let s: str = "usage: ww [-V] <subcommand> [args...]\n -V print version and exit\n build [path] compile module to a static binary (path defaults to cwd)\n run [path] ... build then exec, passing extra args to the program\n test [path] build and run *_test.ww in the module (path defaults to cwd)\n version print version and exit\n\n path forms:\n foo.ww literal file\n foo search cwd, -I dirs, then $WW_LIB-equiv for foo.ww or foo/foo.ww\n lib/foo directory: build lib/foo/foo.ww\n . build the cwd's <basename>.ww\n";
os.write(fd, s.ptr, s.len: u64);
};
@@ -894,6 +1044,7 @@ fn default_out_path(src: *u8) *u8 = {
};
fn do_build(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
let a: *arena = newarena();
let src: *u8 = nil;
let incs: *u8 = os.alloc(PATH_MAX * 2u64): *u8;
let inc_off: u64 = 0u64;
@@ -974,16 +1125,23 @@ fn do_build(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
};
if (src == nil) {
os.write(2, "ww build: missing source\n".ptr, 25u64);
return 2;
// default to cwd module
let dot: *u8 = amalloc(a, 2u64): *u8;
dot[0] = 46u8; dot[1] = 0u8;
src = dot;
};
let out: *u8 = default_out_path(src);
let resolved: *u8 = resolve_module(a, self_dir, src, incs);
if (resolved == nil) {
os.write(2, "ww build: cannot find module\n".ptr, 29u64);
return 1;
};
let out: *u8 = default_out_path(resolved);
let lf: lflags;
lf.libdirs = libdirs;
lf.n_libdirs = n_libdirs;
lf.libs = libs;
lf.n_libs = n_libs;
return build_one(self_dir, src, out, incs, &lf);
return build_one(self_dir, resolved, out, incs, &lf);
};
// Format the scratch path /tmp/ww_run_<pid> into buf. Returns NUL-
@@ -1017,13 +1175,149 @@ fn make_run_tmp(buf: *u8) void = {
};
fn do_run(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
if (start >= argc) {
os.write(2, "ww run: missing source\n".ptr, 23u64);
return 2;
let a: *arena = newarena();
let src: *u8 = nil;
let pass_start: i32 = -1; // first argv idx to pass through to program
let incs: *u8 = os.alloc(PATH_MAX * 2u64): *u8;
let inc_off: u64 = 0u64;
cstr_seal(incs, 0u64);
let max_lflags: i32 = 32;
let libdirs: **u8 = os.alloc((max_lflags: u64) * 8u64): **u8;
let n_libdirs: i32 = 0;
let libs: **u8 = os.alloc((max_lflags: u64) * 8u64): **u8;
let n_libs: i32 = 0;
let i: i32 = start;
for (i < argc) {
if (pass_start >= 0) { i = argc; } // stop, leave rest for exec
else {
let p: *u8 = argv[i];
if (p[0u64] == 45u8) {
if (p[1u64] == 73u8) {
let dir: *u8 = nil;
if (p[2u64] != 0u8) {
dir = p + 2u64;
} else {
if (i + 1 >= argc) {
os.write(2, "ww run: -I needs an argument\n".ptr, 29u64);
return 2;
};
i += 1;
dir = argv[i];
};
if (inc_off > 0u64) {
incs[inc_off] = 58u8;
inc_off += 1u64;
};
inc_off = cstr_into(incs, inc_off, dir);
cstr_seal(incs, inc_off);
} else { if (p[1u64] == 76u8) {
let dir: *u8 = nil;
if (p[2u64] != 0u8) {
dir = p + 2u64;
} else {
if (i + 1 >= argc) {
os.write(2, "ww run: -L needs an argument\n".ptr, 29u64);
return 2;
};
i += 1;
dir = argv[i];
};
if (n_libdirs >= max_lflags) {
os.write(2, "ww run: too many -L\n".ptr, 20u64);
return 2;
};
libdirs[n_libdirs] = dir;
n_libdirs += 1;
} else { if (p[1u64] == 108u8) {
let nm: *u8 = nil;
if (p[2u64] != 0u8) {
nm = p + 2u64;
} else {
if (i + 1 >= argc) {
os.write(2, "ww run: -l needs an argument\n".ptr, 29u64);
return 2;
};
i += 1;
nm = argv[i];
};
if (n_libs >= max_lflags) {
os.write(2, "ww run: too many -l\n".ptr, 20u64);
return 2;
};
libs[n_libs] = nm;
n_libs += 1;
} else {
os.write(2, "ww run: unknown flag\n".ptr, 21u64);
return 2;
}; }; };
i += 1;
} else {
if (src == nil) {
src = p;
i += 1;
} else {
pass_start = i; // remaining args go to the program
};
};
};
};
if (src == nil) {
let dot: *u8 = amalloc(a, 2u64): *u8;
dot[0] = 46u8; dot[1] = 0u8;
src = dot;
};
let resolved: *u8 = resolve_module(a, self_dir, src, incs);
if (resolved == nil) {
os.write(2, "ww run: cannot find module\n".ptr, 27u64);
return 1;
};
let tmp: *u8 = os.alloc(PATH_MAX): *u8;
make_run_tmp(tmp);
if (build_one(self_dir, argv[start], tmp, "\0".ptr, nil) != 0) { return 1; };
let lf: lflags;
lf.libdirs = libdirs;
lf.n_libdirs = n_libdirs;
lf.libs = libs;
lf.n_libs = n_libs;
if (build_one(self_dir, resolved, tmp, incs, &lf) != 0) {
os.unlink(tmp);
return 1;
};
// exec with [tmp, argv[pass_start..argc), nil]
let n_extra: i32 = 0;
if (pass_start >= 0) { n_extra = argc - pass_start; };
let total: i32 = n_extra + 2;
let exec_argv: **u8 = os.alloc((total: u64) * 8u64): **u8;
exec_argv[0] = tmp;
let k: i32 = 0;
for (k < n_extra) {
exec_argv[k + 1] = argv[pass_start + k];
k += 1;
};
exec_argv[n_extra + 1] = nil;
let rc: i32 = proc_run(tmp, exec_argv);
os.unlink(tmp);
return rc;
};
// ---- ww test ----------------------------------------------------------
//
// Mirrors cmd/ww/main.c:do_test. Two modes:
// single-file: build+run a literal *.ww file, return its exit code
// directory: open the dir, getdents64, build+run each *_test.ww,
// report ok/FAIL per file, return 0 iff all pass.
fn run_single_test(self_dir: *u8, src: *u8) i32 = {
let tmp: *u8 = os.alloc(PATH_MAX): *u8;
make_run_tmp(tmp);
if (build_one(self_dir, src, tmp, "\0".ptr, nil) != 0) {
os.unlink(tmp);
return 1;
};
let exec_argv: **u8 = os.alloc(16u64): **u8;
exec_argv[0] = tmp;
exec_argv[1] = nil;
@@ -1032,6 +1326,100 @@ fn do_run(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
return rc;
};
fn run_dir_tests(self_dir: *u8, dir: *u8) i32 = {
let fd: i32 = os.open(dir, os.O_RDONLY, 0i32);
if (fd < 0) {
os.write(2, "ww test: cannot open directory\n".ptr, 31u64);
return 1;
};
let pass: i32 = 0;
let fail: i32 = 0;
let buf: *u8 = os.alloc(8192u64): *u8;
let dirlen: u64 = cstrlen(dir);
let n: i64 = os.getdents64(fd, buf, 8192u64);
for (n > 0i64) {
let off: u64 = 0u64;
let nu: u64 = n: u64;
for (off < nu) {
// d_reclen at offset+16 (u16 LE), d_name at offset+19 (cstr)
let blo: u64 = (buf[off + 16u64]): u64;
let bhi: u64 = (buf[off + 17u64]): u64;
let reclen: u64 = blo + (bhi * 256u64);
let name: *u8 = buf + off + 19u64;
if (cstr_endswith_lit(name, "_test.ww")) {
let nlen: u64 = cstrlen(name);
// path = <dir>/<name>
let path: *u8 = os.alloc(PATH_MAX): *u8;
let p_off: u64 = cstr_into(path, 0u64, dir);
path[p_off] = 47u8; p_off += 1u64;
let i: u64 = 0u64;
for (i < nlen) { path[p_off + i] = name[i]; i += 1u64; };
p_off += nlen;
cstr_seal(path, p_off);
// incs = <dir> so test files can `use ` siblings
let tincs: *u8 = os.alloc(PATH_MAX): *u8;
let ic: u64 = cstr_into(tincs, 0u64, dir);
cstr_seal(tincs, ic);
let tmp: *u8 = os.alloc(PATH_MAX): *u8;
make_run_tmp(tmp);
let bres: i32 = build_one(self_dir, path, tmp, tincs, nil);
if (bres != 0) {
fail += 1;
os.write(2, "FAIL ".ptr, 5u64);
os.write(2, name, nlen);
os.write(2, " (build)\n".ptr, 9u64);
} else {
let exec_argv: **u8 = os.alloc(16u64): **u8;
exec_argv[0] = tmp;
exec_argv[1] = nil;
let rc: i32 = proc_run(tmp, exec_argv);
if (rc == 0) {
pass += 1;
os.write(1, "ok ".ptr, 5u64);
os.write(1, name, nlen);
os.write(1, "\n".ptr, 1u64);
} else {
fail += 1;
os.write(2, "FAIL ".ptr, 5u64);
os.write(2, name, nlen);
os.write(2, "\n".ptr, 1u64);
};
};
os.unlink(tmp);
};
off += reclen;
};
n = os.getdents64(fd, buf, 8192u64);
};
os.close(fd);
if (fail == 0) { return 0; };
return 1;
};
fn do_test(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
let a: *arena = newarena();
let target: *u8;
if (start >= argc) {
let dot: *u8 = amalloc(a, 2u64): *u8;
dot[0] = 46u8; dot[1] = 0u8;
target = dot;
} else {
target = argv[start];
};
// single-file mode: literal *.ww that exists
if (cstr_endswith_lit(target, ".ww")) {
if (os.access(target, 0i32) == 0) {
return run_single_test(self_dir, target);
};
};
// otherwise treat target as a directory; enumerate *_test.ww
return run_dir_tests(self_dir, target);
};
// ---- Entry -------------------------------------------------------------
export fn main(argc: i32, argv: **u8) i32 = {
@@ -1065,6 +1453,9 @@ export fn main(argc: i32, argv: **u8) i32 = {
if (cstreq_lit(cmd, "run")) {
return do_run(self_dir, argv, argc, 2);
};
if (cstreq_lit(cmd, "test")) {
return do_test(self_dir, argv, argc, 2);
};
os.write(2, "ww: unknown subcommand\n".ptr, 23u64);
write_usage(2);
return 2;

View File

@@ -2,13 +2,13 @@
//
// The user-facing driver. Plan 9 cc(1) / Hare hare(1) analogue:
//
// ww build foo.ww → 6c foo.ww > foo.s ; 6a foo.s > foo.o ;
// 6l -o foo foo.o libwwrt.a
// ww build foo.ww → w6c foo.ww > foo.s ; w6a foo.s > foo.o ;
// w6l -o foo foo.o libwwrt.a
// ww run foo.ww → build then exec
// ww version → print version
//
// Tool paths default to siblings of $0 so a fresh build runs out of
// out/bin/. Env-var overrides (WW_6C / WW_6A / WW_6L / WW_LIB) are
// out/bin/. Env-var overrides (WW_W6C / WW_W6A / WW_W6L / WW_LIB) are
// not yet supported in this port; the bootstrap doesn't need them.
use os;
@@ -408,7 +408,7 @@ fn append_lit(stem: *u8, suffix: str) *u8 = {
};
// linker flags bundled as a struct so build_one stays at the wwstage
// 6c's 6-argument calling-convention limit.
// w6c's 6-argument calling-convention limit.
type lflags = struct {
libdirs: **u8,
n_libdirs: i32,
@@ -418,7 +418,7 @@ type lflags = struct {
// build_one — compile `src` into the executable named `out`.
// self_dir: NUL-terminated dir containing this driver and the
// wwstage tools (6c_ww/6a_ww/6l_ww)
// wwstage tools (w6c_ww/w6a_ww/w6l_ww)
// src: NUL-terminated path to the .ww file
// out: NUL-terminated desired output path
// incs: NUL-terminated colon-list of -I dirs (may be empty)
@@ -426,14 +426,14 @@ type lflags = struct {
//
// The ww-side driver shells to the ww-side tools so a `ww_ww build`
// touches no C-built code at runtime. The C `ww` driver in cmd/ww/
// still drives the C-built 6c/6a/6l. Test 993 pins the two
// still drives the C-built w6c/w6a/w6l. Test 993 pins the two
// pipelines to byte-identical output on a corpus.
fn build_one(self_dir: *u8, src: *u8, out: *u8, incs: *u8, lf: *lflags) i32 = {
let a: *arena = newarena();
let c6: *u8 = join_path_lit(self_dir, "6c_ww");
let a6: *u8 = join_path_lit(self_dir, "6a_ww");
let l6: *u8 = join_path_lit(self_dir, "6l_ww");
let c6: *u8 = join_path_lit(self_dir, "w6c_ww");
let a6: *u8 = join_path_lit(self_dir, "w6a_ww");
let l6: *u8 = join_path_lit(self_dir, "w6l_ww");
// Default lib search path: <self_dir>/../../lib
let dotdot_lib: *u8 = os.alloc(PATH_MAX): *u8;
@@ -486,35 +486,35 @@ fn build_one(self_dir: *u8, src: *u8, out: *u8, incs: *u8, lf: *lflags) i32 = {
};
os.close(cf);
// Step 2: 6c -o <stem>.s <stem>.combined.ww
// Step 2: w6c -o <stem>.s <stem>.combined.ww
{
let argv: **u8 = os.alloc(40u64): **u8;
argv[0] = "6c\0".ptr;
argv[0] = "w6c\0".ptr;
argv[1] = "-o\0".ptr;
argv[2] = asmf;
argv[3] = combined;
argv[4] = nil;
if (proc_run(c6, argv) != 0) {
os.write(2, "ww: 6c failed\n".ptr, 14u64);
os.write(2, "ww: w6c failed\n".ptr, 15u64);
return 1;
};
};
// Step 3: 6a -o <stem>.o <stem>.s
// Step 3: w6a -o <stem>.o <stem>.s
{
let argv: **u8 = os.alloc(40u64): **u8;
argv[0] = "6a\0".ptr;
argv[0] = "w6a\0".ptr;
argv[1] = "-o\0".ptr;
argv[2] = objf;
argv[3] = asmf;
argv[4] = nil;
if (proc_run(a6, argv) != 0) {
os.write(2, "ww: 6a failed\n".ptr, 14u64);
os.write(2, "ww: w6a failed\n".ptr, 15u64);
return 1;
};
};
// Step 4: 6l -o <out> <stem>.o libwwrt.a [-L<dir>...] [-l<name>...]
// Step 4: w6l -o <out> <stem>.o libwwrt.a [-L<dir>...] [-l<name>...]
{
let nl_dirs: i32 = 0;
let nl_libs: i32 = 0;
@@ -526,13 +526,13 @@ fn build_one(self_dir: *u8, src: *u8, out: *u8, incs: *u8, lf: *lflags) i32 = {
l_dirs = lf.libdirs;
l_libs = lf.libs;
};
// argv slots: 5 fixed (6l, -o, out, objf, libwwrt)
// argv slots: 5 fixed (w6l, -o, out, objf, libwwrt)
// + 2 * n_libdirs (-L, dir)
// + 2 * n_libs (-l, name)
// + 1 nil terminator.
let total: i32 = 5 + 2 * nl_dirs + 2 * nl_libs + 1;
let argv: **u8 = os.alloc((total: u64) * 8u64): **u8;
argv[0] = "6l\0".ptr;
argv[0] = "w6l\0".ptr;
argv[1] = "-o\0".ptr;
argv[2] = out;
argv[3] = objf;
@@ -554,17 +554,139 @@ fn build_one(self_dir: *u8, src: *u8, out: *u8, incs: *u8, lf: *lflags) i32 = {
};
argv[pos] = nil;
if (proc_run(l6, argv) != 0) {
os.write(2, "ww: 6l failed\n".ptr, 14u64);
os.write(2, "ww: w6l failed\n".ptr, 15u64);
return 1;
};
};
return 0;
};
// ---- Module-by-name resolution ----------------------------------------
//
// Mirrors cmd/ww/main.c:resolve_module. Maps a name like "foo", "lib/foo",
// "foo.ww", or "." to a concrete .ww file path:
// 1. literal <name>.ww that exists → use as-is
// 2. "." → <cwd>/<basename(cwd)>.ww → that, if it exists
// 3. <name>/<basename(name)>.ww → that, if it exists
// 4. walk search path (cwd:incs:<self_dir>/../../lib):
// <dir>/<name>.ww or <dir>/<name>/<name>.ww
fn cstr_endswith_lit(p: *u8, lit: str) bool = {
let plen: u64 = cstrlen(p);
let slen: u64 = lit.len: u64;
if (plen < slen) { return false; };
let off: u64 = plen - slen;
let i: i32 = 0;
for (i < lit.len) {
let iu: u64 = i: u64;
if (p[off + iu] != lit[i]) { return false; };
i += 1;
};
return true;
};
// basename_off — return the offset of the last path segment within `p`
// (i.e. one past the final '/'). Returns 0 if there's no slash.
fn basename_off(p: *u8, plen: u64) u64 = {
let start: u64 = 0u64;
let i: u64 = 0u64;
for (i < plen) {
if (p[i] == 47u8) { start = i + 1u64; }; // '/'
i += 1u64;
};
return start;
};
// arena_dup_cstr — copy `plen` bytes from `src` into a fresh NUL-sealed
// arena buffer.
fn arena_dup_cstr(a: *arena, src: *u8, plen: u64) *u8 = {
let buf: *u8 = amalloc(a, plen + 1u64): *u8;
let i: u64 = 0u64;
for (i < plen) { buf[i] = src[i]; i += 1u64; };
buf[plen] = 0u8;
return buf;
};
// build_dir_module_path — alloc <dir>/<base>.ww, NUL-terminated, in `a`.
fn build_dir_module_path(a: *arena, dir: *u8, dlen: u64,
base: *u8, blen: u64) *u8 = {
let need: u64 = dlen + 1u64 + blen + 3u64 + 1u64;
let buf: *u8 = amalloc(a, need): *u8;
let off: u64 = 0u64;
let i: u64 = 0u64;
for (i < dlen) { buf[off] = dir[i]; off += 1u64; i += 1u64; };
buf[off] = 47u8; off += 1u64; // '/'
i = 0u64;
for (i < blen) { buf[off] = base[i]; off += 1u64; i += 1u64; };
buf[off] = 46u8; off += 1u64; // '.'
buf[off] = 119u8; off += 1u64; // 'w'
buf[off] = 119u8; off += 1u64; // 'w'
buf[off] = 0u8;
return buf;
};
// build_search_path — compose the colon-separated lookup path used by
// resolve_module's case (4). Order: "." : <incs> : <self_dir>/../../lib
fn build_search_path(a: *arena, self_dir: *u8, incs: *u8) *u8 = {
let buf: *u8 = amalloc(a, PATH_MAX * 2u64): *u8;
let off: u64 = 0u64;
buf[off] = 46u8; off += 1u64; // '.'
if (incs != nil) {
if (incs[0u64] != 0u8) {
buf[off] = 58u8; off += 1u64; // ':'
off = cstr_into(buf, off, incs);
};
};
buf[off] = 58u8; off += 1u64;
off = cstr_into(buf, off, self_dir);
off = str_into(buf, off, "/../../lib");
cstr_seal(buf, off);
return buf;
};
fn resolve_module(a: *arena, self_dir: *u8, name: *u8, incs: *u8) *u8 = {
let nlen: u64 = cstrlen(name);
// (1) literal .ww file that exists
if (cstr_endswith_lit(name, ".ww")) {
if (os.access(name, 0i32) == 0) {
return arena_dup_cstr(a, name, nlen);
};
};
// (2) "." → cwd's <basename>.ww
if (nlen == 1u64) {
if (name[0u64] == 46u8) { // '.'
let cwd: *u8 = amalloc(a, PATH_MAX): *u8;
let r: i64 = os.getcwd(cwd, PATH_MAX);
if (r <= 0i64) { return nil; };
let cwdlen: u64 = (r: u64) - 1u64; // strip trailing NUL
let bo: u64 = basename_off(cwd, cwdlen);
let blen: u64 = cwdlen - bo;
let dot: *u8 = amalloc(a, 2u64): *u8;
dot[0] = 46u8; dot[1] = 0u8;
let probe: *u8 = build_dir_module_path(a, dot, 1u64,
cwd + bo, blen);
if (os.access(probe, 0i32) == 0) { return probe; };
return nil;
};
};
// (3) <name>/<basename(name)>.ww — directory-as-module
let bo: u64 = basename_off(name, nlen);
let probe: *u8 = build_dir_module_path(a, name, nlen,
name + bo, nlen - bo);
if (os.access(probe, 0i32) == 0) { return probe; };
// (4) search path lookup
let search: *u8 = build_search_path(a, self_dir, incs);
return locate_import(a, search, name, nlen);
};
// ---- Subcommand handlers ----------------------------------------------
fn write_usage(fd: i32) void = {
let s: str = "usage: ww [-V] <subcommand> [args...]\n -V print version and exit\n build <path> compile module to a static binary\n run <path> build then exec\n version print version and exit\n";
let s: str = "usage: ww [-V] <subcommand> [args...]\n -V print version and exit\n build [path] compile module to a static binary (path defaults to cwd)\n run [path] ... build then exec, passing extra args to the program\n test [path] build and run *_test.ww in the module (path defaults to cwd)\n version print version and exit\n\n path forms:\n foo.ww literal file\n foo search cwd, -I dirs, then $WW_LIB-equiv for foo.ww or foo/foo.ww\n lib/foo directory: build lib/foo/foo.ww\n . build the cwd's <basename>.ww\n";
os.write(fd, s.ptr, s.len: u64);
};
@@ -606,6 +728,7 @@ fn default_out_path(src: *u8) *u8 = {
};
fn do_build(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
let a: *arena = newarena();
let src: *u8 = nil;
let incs: *u8 = os.alloc(PATH_MAX * 2u64): *u8;
let inc_off: u64 = 0u64;
@@ -686,16 +809,23 @@ fn do_build(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
};
if (src == nil) {
os.write(2, "ww build: missing source\n".ptr, 25u64);
return 2;
// default to cwd module
let dot: *u8 = amalloc(a, 2u64): *u8;
dot[0] = 46u8; dot[1] = 0u8;
src = dot;
};
let out: *u8 = default_out_path(src);
let resolved: *u8 = resolve_module(a, self_dir, src, incs);
if (resolved == nil) {
os.write(2, "ww build: cannot find module\n".ptr, 29u64);
return 1;
};
let out: *u8 = default_out_path(resolved);
let lf: lflags;
lf.libdirs = libdirs;
lf.n_libdirs = n_libdirs;
lf.libs = libs;
lf.n_libs = n_libs;
return build_one(self_dir, src, out, incs, &lf);
return build_one(self_dir, resolved, out, incs, &lf);
};
// Format the scratch path /tmp/ww_run_<pid> into buf. Returns NUL-
@@ -729,13 +859,149 @@ fn make_run_tmp(buf: *u8) void = {
};
fn do_run(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
if (start >= argc) {
os.write(2, "ww run: missing source\n".ptr, 23u64);
return 2;
let a: *arena = newarena();
let src: *u8 = nil;
let pass_start: i32 = -1; // first argv idx to pass through to program
let incs: *u8 = os.alloc(PATH_MAX * 2u64): *u8;
let inc_off: u64 = 0u64;
cstr_seal(incs, 0u64);
let max_lflags: i32 = 32;
let libdirs: **u8 = os.alloc((max_lflags: u64) * 8u64): **u8;
let n_libdirs: i32 = 0;
let libs: **u8 = os.alloc((max_lflags: u64) * 8u64): **u8;
let n_libs: i32 = 0;
let i: i32 = start;
for (i < argc) {
if (pass_start >= 0) { i = argc; } // stop, leave rest for exec
else {
let p: *u8 = argv[i];
if (p[0u64] == 45u8) {
if (p[1u64] == 73u8) {
let dir: *u8 = nil;
if (p[2u64] != 0u8) {
dir = p + 2u64;
} else {
if (i + 1 >= argc) {
os.write(2, "ww run: -I needs an argument\n".ptr, 29u64);
return 2;
};
i += 1;
dir = argv[i];
};
if (inc_off > 0u64) {
incs[inc_off] = 58u8;
inc_off += 1u64;
};
inc_off = cstr_into(incs, inc_off, dir);
cstr_seal(incs, inc_off);
} else { if (p[1u64] == 76u8) {
let dir: *u8 = nil;
if (p[2u64] != 0u8) {
dir = p + 2u64;
} else {
if (i + 1 >= argc) {
os.write(2, "ww run: -L needs an argument\n".ptr, 29u64);
return 2;
};
i += 1;
dir = argv[i];
};
if (n_libdirs >= max_lflags) {
os.write(2, "ww run: too many -L\n".ptr, 20u64);
return 2;
};
libdirs[n_libdirs] = dir;
n_libdirs += 1;
} else { if (p[1u64] == 108u8) {
let nm: *u8 = nil;
if (p[2u64] != 0u8) {
nm = p + 2u64;
} else {
if (i + 1 >= argc) {
os.write(2, "ww run: -l needs an argument\n".ptr, 29u64);
return 2;
};
i += 1;
nm = argv[i];
};
if (n_libs >= max_lflags) {
os.write(2, "ww run: too many -l\n".ptr, 20u64);
return 2;
};
libs[n_libs] = nm;
n_libs += 1;
} else {
os.write(2, "ww run: unknown flag\n".ptr, 21u64);
return 2;
}; }; };
i += 1;
} else {
if (src == nil) {
src = p;
i += 1;
} else {
pass_start = i; // remaining args go to the program
};
};
};
};
if (src == nil) {
let dot: *u8 = amalloc(a, 2u64): *u8;
dot[0] = 46u8; dot[1] = 0u8;
src = dot;
};
let resolved: *u8 = resolve_module(a, self_dir, src, incs);
if (resolved == nil) {
os.write(2, "ww run: cannot find module\n".ptr, 27u64);
return 1;
};
let tmp: *u8 = os.alloc(PATH_MAX): *u8;
make_run_tmp(tmp);
if (build_one(self_dir, argv[start], tmp, "\0".ptr, nil) != 0) { return 1; };
let lf: lflags;
lf.libdirs = libdirs;
lf.n_libdirs = n_libdirs;
lf.libs = libs;
lf.n_libs = n_libs;
if (build_one(self_dir, resolved, tmp, incs, &lf) != 0) {
os.unlink(tmp);
return 1;
};
// exec with [tmp, argv[pass_start..argc), nil]
let n_extra: i32 = 0;
if (pass_start >= 0) { n_extra = argc - pass_start; };
let total: i32 = n_extra + 2;
let exec_argv: **u8 = os.alloc((total: u64) * 8u64): **u8;
exec_argv[0] = tmp;
let k: i32 = 0;
for (k < n_extra) {
exec_argv[k + 1] = argv[pass_start + k];
k += 1;
};
exec_argv[n_extra + 1] = nil;
let rc: i32 = proc_run(tmp, exec_argv);
os.unlink(tmp);
return rc;
};
// ---- ww test ----------------------------------------------------------
//
// Mirrors cmd/ww/main.c:do_test. Two modes:
// single-file: build+run a literal *.ww file, return its exit code
// directory: open the dir, getdents64, build+run each *_test.ww,
// report ok/FAIL per file, return 0 iff all pass.
fn run_single_test(self_dir: *u8, src: *u8) i32 = {
let tmp: *u8 = os.alloc(PATH_MAX): *u8;
make_run_tmp(tmp);
if (build_one(self_dir, src, tmp, "\0".ptr, nil) != 0) {
os.unlink(tmp);
return 1;
};
let exec_argv: **u8 = os.alloc(16u64): **u8;
exec_argv[0] = tmp;
exec_argv[1] = nil;
@@ -744,6 +1010,100 @@ fn do_run(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
return rc;
};
fn run_dir_tests(self_dir: *u8, dir: *u8) i32 = {
let fd: i32 = os.open(dir, os.O_RDONLY, 0i32);
if (fd < 0) {
os.write(2, "ww test: cannot open directory\n".ptr, 31u64);
return 1;
};
let pass: i32 = 0;
let fail: i32 = 0;
let buf: *u8 = os.alloc(8192u64): *u8;
let dirlen: u64 = cstrlen(dir);
let n: i64 = os.getdents64(fd, buf, 8192u64);
for (n > 0i64) {
let off: u64 = 0u64;
let nu: u64 = n: u64;
for (off < nu) {
// d_reclen at offset+16 (u16 LE), d_name at offset+19 (cstr)
let blo: u64 = (buf[off + 16u64]): u64;
let bhi: u64 = (buf[off + 17u64]): u64;
let reclen: u64 = blo + (bhi * 256u64);
let name: *u8 = buf + off + 19u64;
if (cstr_endswith_lit(name, "_test.ww")) {
let nlen: u64 = cstrlen(name);
// path = <dir>/<name>
let path: *u8 = os.alloc(PATH_MAX): *u8;
let p_off: u64 = cstr_into(path, 0u64, dir);
path[p_off] = 47u8; p_off += 1u64;
let i: u64 = 0u64;
for (i < nlen) { path[p_off + i] = name[i]; i += 1u64; };
p_off += nlen;
cstr_seal(path, p_off);
// incs = <dir> so test files can `use ` siblings
let tincs: *u8 = os.alloc(PATH_MAX): *u8;
let ic: u64 = cstr_into(tincs, 0u64, dir);
cstr_seal(tincs, ic);
let tmp: *u8 = os.alloc(PATH_MAX): *u8;
make_run_tmp(tmp);
let bres: i32 = build_one(self_dir, path, tmp, tincs, nil);
if (bres != 0) {
fail += 1;
os.write(2, "FAIL ".ptr, 5u64);
os.write(2, name, nlen);
os.write(2, " (build)\n".ptr, 9u64);
} else {
let exec_argv: **u8 = os.alloc(16u64): **u8;
exec_argv[0] = tmp;
exec_argv[1] = nil;
let rc: i32 = proc_run(tmp, exec_argv);
if (rc == 0) {
pass += 1;
os.write(1, "ok ".ptr, 5u64);
os.write(1, name, nlen);
os.write(1, "\n".ptr, 1u64);
} else {
fail += 1;
os.write(2, "FAIL ".ptr, 5u64);
os.write(2, name, nlen);
os.write(2, "\n".ptr, 1u64);
};
};
os.unlink(tmp);
};
off += reclen;
};
n = os.getdents64(fd, buf, 8192u64);
};
os.close(fd);
if (fail == 0) { return 0; };
return 1;
};
fn do_test(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
let a: *arena = newarena();
let target: *u8;
if (start >= argc) {
let dot: *u8 = amalloc(a, 2u64): *u8;
dot[0] = 46u8; dot[1] = 0u8;
target = dot;
} else {
target = argv[start];
};
// single-file mode: literal *.ww that exists
if (cstr_endswith_lit(target, ".ww")) {
if (os.access(target, 0i32) == 0) {
return run_single_test(self_dir, target);
};
};
// otherwise treat target as a directory; enumerate *_test.ww
return run_dir_tests(self_dir, target);
};
// ---- Entry -------------------------------------------------------------
export fn main(argc: i32, argv: **u8) i32 = {
@@ -777,6 +1137,9 @@ export fn main(argc: i32, argv: **u8) i32 = {
if (cstreq_lit(cmd, "run")) {
return do_run(self_dir, argv, argc, 2);
};
if (cstreq_lit(cmd, "test")) {
return do_test(self_dir, argv, argc, 2);
};
os.write(2, "ww: unknown subcommand\n".ptr, 23u64);
write_usage(2);
return 2;

View File

@@ -18,19 +18,21 @@ export fn assert(cond: bool, msg: str) void = {
if (!cond) { abort(msg); };
};
def SYS_READ: i64 = 0;
def SYS_WRITE: i64 = 1;
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;
def SYS_EXIT: i64 = 60;
def SYS_WAIT4: i64 = 61;
def SYS_UNLINK: i64 = 87;
def SYS_READ: i64 = 0;
def SYS_WRITE: i64 = 1;
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;
def SYS_EXIT: i64 = 60;
def SYS_WAIT4: i64 = 61;
def SYS_UNLINK: i64 = 87;
def SYS_GETCWD: i64 = 79;
def SYS_GETDENTS64: i64 = 217;
// open(2) flags. Linux values, matching <fcntl.h>.
def O_RDONLY: i32 = 0;
@@ -65,7 +67,7 @@ export fn close(fd: i32) 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
// negative errno. Used by w6c_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;
@@ -179,7 +181,33 @@ export fn wait4(pid: i32, status_out: *i32, options: i32, rusage: *void) i32 = {
options: i64, rusage: i64): i32;
};
// selfhost/cmd/wwc/mem.ww — port of cmd/wwc/mem.c.
// getcwd(2) — Linux flavour. Writes the NUL-terminated cwd into `buf`
// and returns the number of bytes written (including the NUL), or a
// negative errno. The driver uses it to expand `.` to the cwd's
// basename for `ww build` / `ww test`.
export fn getcwd(buf: *u8, n: u64) i64 = {
return syscall2(SYS_GETCWD, buf: i64, n: i64);
};
// getdents64(2) — Linux directory enumeration. The fd must be opened
// with O_RDONLY on a directory. `buf` receives a packed sequence of
// linux_dirent64 records:
//
// struct linux_dirent64 {
// u64 d_ino; // 0..7
// i64 d_off; // 8..15
// u16 d_reclen; // 16..17 — total bytes for this record
// u8 d_type; // 18 — DT_REG/DT_DIR/...
// u8 d_name[]; // 19.. — NUL-terminated name + padding
// };
//
// Returns bytes written into `buf` (advance by d_reclen to walk),
// 0 at end-of-directory, or a negative errno.
export fn getdents64(fd: i32, buf: *u8, n: u64) i64 = {
return syscall3(SYS_GETDENTS64, fd: i64, buf: i64, n: i64);
};
// selfhost/cmd/wcc/mem.ww — port of cmd/wcc/mem.c.
//
// Bump arena allocator. Backed by the runtime page allocator
// (rt_alloc / rt_free), no libc. Each chunk is mmap'd; when the
@@ -187,7 +215,7 @@ export fn wait4(pid: i32, status_out: *i32, options: i32, rusage: *void) i32 = {
// unmaps the chain.
//
// Memory handed out is 16-byte aligned. The C version under
// cmd/wwc/ is retained until the three-stage bootstrap diffs clean.
// cmd/wcc/ is retained until the three-stage bootstrap diffs clean.
use os;
@@ -406,8 +434,8 @@ export fn parseu64(s: str) (u64 | str) = {
return v;
};
// selfhost/cmd/wwc/tok.ww — port of cmd/wwc/tok.c plus the Tkind /
// Tok / Pos shapes from cmd/wwc/ww.h.
// selfhost/cmd/wcc/tok.ww — port of cmd/wcc/tok.c plus the Tkind /
// Tok / Pos shapes from cmd/wcc/ww.h.
//
// Token kind values must stay numerically equal to the C side: the
// 990_selfhost test diffs ww-side wwdump output against C-side
@@ -415,13 +443,13 @@ export fn parseu64(s: str) (u64 | str) = {
// integers and breaks the diff.
//
// Bottom of file: tokprint, which emits one token per line in a
// format identical to cmd/wwc/tok.c:tokprint().
// format identical to cmd/wcc/tok.c:tokprint().
use os;
use strconv;
// ---- Tkind ------------------------------------------------------------
// Mirror of the C enum in cmd/wwc/ww.h. Don't reorder.
// Mirror of the C enum in cmd/wcc/ww.h. Don't reorder.
def TK_NONE: i32 = 0;
def TK_EOF: i32 = 1;
@@ -516,7 +544,7 @@ def TK_LAST: i32 = 80;
// ---- Pos / Tok --------------------------------------------------------
//
// `pos` is used at error-reporting boundaries; we always pass it via
// *pos so the value never gets struct-copied (6c can't yet copy a
// *pos so the value never gets struct-copied (w6c can't yet copy a
// 24-byte struct).
//
// `tok` is flat — file/line/col live directly on the token rather than
@@ -554,7 +582,7 @@ fn streq_n(a: *u8, b: str, n: i32) bool = {
// kwlookup — returns the matching TK_* keyword kind for a byte run,
// or TK_NONE if it's an ordinary identifier. Linear search over a
// small alphabetised list, matching cmd/wwc/tok.c.
// small alphabetised list, matching cmd/wcc/tok.c.
export fn kwlookup(p: *u8, n: i32) i32 = {
if (streq_n(p, "as", n)) { return TK_AS; };
if (streq_n(p, "break", n)) { return TK_BREAK; };
@@ -683,7 +711,7 @@ export fn tokname(k: i32) str = {
// ---- writer for tokprint ----------------------------------------------
//
// fputq mirrors cmd/wwc/tok.c:fputq — quote the string with C-style
// fputq mirrors cmd/wcc/tok.c:fputq — quote the string with C-style
// escapes for \, ", \n, \t, \r and \xNN for other non-printables.
fn fputc_byte(fd: i32, b: u8) void = {
@@ -750,14 +778,14 @@ fn fputq(fd: i32, p: *u8, n: i32) void = {
};
// tokprint — write one token line to fd. Format must match
// cmd/wwc/tok.c:tokprint() byte-for-byte: that's the diff anchor.
// cmd/wcc/tok.c:tokprint() byte-for-byte: that's the diff anchor.
// "<file>:<line>:<col> <kindname>[ <value>]\n"
//
// Takes `t` by pointer because 6c can't yet pass a >16-byte struct
// Takes `t` by pointer because w6c can't yet pass a >16-byte struct
// by value; the C version takes Tok by value.
export fn tokprint(fd: i32, t: *tok) void = {
// Chained-dot field reads (`t.x.y`) on str sub-fields aren't yet
// reduced by 6c — `t.x.y` returns the whole str. Lift the str
// reduced by w6c — `t.x.y` returns the whole str. Lift the str
// fields into locals so we can use the str pseudo-field path.
let tfile: str = t.file;
let ttext: str = t.text;
@@ -894,14 +922,14 @@ export fn toupper(c: u8) u8 = {
return c;
};
// selfhost/cmd/wwc/lex.ww — port of cmd/wwc/lex.c.
// selfhost/cmd/wcc/lex.ww — port of cmd/wcc/lex.c.
//
// The DFA, the helpers, and the order of decisions all mirror the C
// version exactly. The 990_selfhost test diffs the resulting token
// stream against the C-side wwdump byte-for-byte; any divergence is
// a port bug.
//
// Calling-convention note: 6c can't yet pass or return structs >16
// Calling-convention note: w6c can't yet pass or return structs >16
// bytes by value, so `tok` and `pos` are passed by pointer (out
// params). The C version passes `Tok` by value; we differ here only
// in shape, not in observable behaviour. Token kind values stay
@@ -1551,7 +1579,7 @@ export fn lexnext(l: *lex, out: *tok) void = {
out.text = astrndup(l.a, one.ptr, 1u64);
};
// selfhost/cmd/wwc/ast.ww — port of cmd/wwc/ast.c (Node defs + printer).
// selfhost/cmd/wcc/ast.ww — port of cmd/wcc/ast.c (Node defs + printer).
//
// Status: AST printer is fully ported. Constructor `newnode` is here.
// The parser (parse.ww) is currently minimal — see its file header.
@@ -1567,7 +1595,7 @@ use tok;
// ---- Nkind ------------------------------------------------------------
//
// Mirror of cmd/wwc/ww.h Nkind. Values must stay numerically equal so
// Mirror of cmd/wcc/ww.h Nkind. Values must stay numerically equal so
// the AST diff probe in 990_selfhost works.
def N_NONE: i32 = 0;
@@ -1887,7 +1915,7 @@ export fn astprint(fd: i32, n: *node) void = {
pr(fd, n, 0);
};
// selfhost/cmd/wwc/parse.ww — port of cmd/wwc/parse.c.
// selfhost/cmd/wcc/parse.ww — port of cmd/wcc/parse.c.
//
// Status: GROWING stub. Currently handles top-level `use IDENT;`,
// `def NAME: TYPE = LIT;`, `type NAME = TYPE;`, and `fn NAME(params)
@@ -1900,7 +1928,7 @@ export fn astprint(fd: i32, n: *node) void = {
// surface form lands here gradually so the AST diff in 990_selfhost
// grows toward whole-language coverage one increment at a time.
//
// Calling-convention shim: 6c can't yet pass a sub-struct field
// Calling-convention shim: w6c can't yet pass a sub-struct field
// (e.g. p.cur.line where p.cur is a `tok` of size 76). The parser
// stores the current token as flat primitive fields rather than a
// nested `tok` struct; `refill` copies a freshly lexed token in.
@@ -2635,7 +2663,7 @@ fn parsestmt(p: *parser) *node = {
};
// expression statement, or tuple-destructure multi-assign:
// a, b = expr;
// Mirrors cmd/wwc/parse.c:1015-1031. We parse the first lvalue
// Mirrors cmd/wcc/parse.c:1015-1031. We parse the first lvalue
// with parseexpr (matches the C side); subsequent lvalues go
// through parsebin(parseunary, 1) so the `=` stays for us to
// consume — parseexpr would absorb it.
@@ -2870,7 +2898,7 @@ export fn parsefile(p: *parser) *node = {
return f;
};
// selfhost/cmd/wwc/type.ww — port of cmd/wwc/type.c.
// selfhost/cmd/wcc/type.ww — port of cmd/wcc/type.c.
//
// Status: full structural port. The C version uses module-globals for
// the primitive types (ty_void, ty_i32, …); ww doesn't have writable
@@ -2882,7 +2910,7 @@ use os;
use mem;
// ---- TypeKind ---------------------------------------------------------
// Numeric values must stay aligned with cmd/wwc/ww.h TypeKind so the
// Numeric values must stay aligned with cmd/wcc/ww.h TypeKind so the
// next diff signal (typed-AST printer / cgen) can compare across the
// two implementations.
@@ -3200,7 +3228,7 @@ export fn type_eq(a: *tinfo, b: *tinfo) bool = {
return true; // primitives match by kind alone
};
// selfhost/cmd/wwc/sym.ww — port of cmd/wwc/sym.c.
// selfhost/cmd/wcc/sym.ww — port of cmd/wcc/sym.c.
//
// Per-scope hashtable, chained to the parent. Lookup walks up.
// Plan 9 / Hare flavoured. Duplicate definitions in the same scope
@@ -3210,7 +3238,7 @@ use mem;
use typ;
use ast;
// Symbol kinds — must stay numerically aligned with cmd/wwc/ww.h Skind.
// Symbol kinds — must stay numerically aligned with cmd/wcc/ww.h Skind.
def SK_NONE: i32 = 0;
def SK_VAR: i32 = 1;
def SK_PARAM: i32 = 2;
@@ -3314,11 +3342,11 @@ export fn scope_define(s: *scope, name: str, k: i32, t: *tinfo, decl: *node) *sy
return sy;
};
// selfhost/cmd/wwc/check.ww — minimal port of cmd/wwc/check.c.
// selfhost/cmd/wcc/check.ww — minimal port of cmd/wcc/check.c.
//
// Status: name-resolution + primitive-type seeding only. Full type
// inference, conversion rules, tagged-union dispatch typing, return-
// type checking, etc. all live in cmd/wwc/check.c (937 lines) and
// type checking, etc. all live in cmd/wcc/check.c (937 lines) and
// will land here in subsequent commits.
//
// What this version does:
@@ -3562,10 +3590,10 @@ export fn check_file(c: *checker, file: *node) void = {
};
};
// selfhost/cmd/wwc/cgen.ww — port of cmd/6c/cgen.c.
// selfhost/cmd/wcc/cgen.ww — port of cmd/w6c/cgen.c.
//
// Status: GROWING. Each subsystem we add is verified by `wwdump_ww -c`
// producing byte-identical output to C-side `6c` for the same source,
// producing byte-identical output to C-side `w6c` for the same source,
// then by assembling + linking + running the result.
//
// Current coverage:
@@ -3760,7 +3788,7 @@ fn local_alloc(c: *cgen, name: str, sz: i32, tnode: *node) i32 = {
fn local_add(c: *cgen, name: str, sz: i32, tnode: *node) i32 = {
// Name-based slot reuse for N_LETs and params: if `name` is
// already declared in this function, return its existing
// offset. Mirrors C cgen (cmd/6c/cgen.c:localoff). Two
// offset. Mirrors C cgen (cmd/w6c/cgen.c:localoff). Two
// disjoint scopes that declare the same name share one slot —
// so `escape` in wwdump (three `let cp: pos;` across separate
// branches) reserves one slot, not three. scan_locals does
@@ -4097,7 +4125,7 @@ fn fnret_lookup(c: *cgen, name: str) *node = {
// ---- def-constant registry ------------------------------------------
//
// `def NAME: T = LIT;` becomes a DATA symbol the C-side 6c emits; an
// `def NAME: T = LIT;` becomes a DATA symbol the C-side w6c emits; an
// ident reference loads it via `MOVQ NAME(SB), AX`. We collect them at
// file load and consult on N_IDENT lookup.
@@ -4725,7 +4753,7 @@ fn node_isunsigned(c: *cgen, n: *node) bool = {
// Walks the base local's declared type and pulls the element
// out — *u8 → u8, [N]u32 → u32, []u64 → u64. Without this the
// compare-codegen for `p[i] >= 48u8` falls back to signed JGE
// instead of JAE, diverging from C 6c on byte indexing.
// instead of JAE, diverging from C w6c on byte indexing.
if (k == N_INDEX) {
let base: *node = n.lhs;
if (base != nil) {
@@ -5198,7 +5226,7 @@ fn cgexpr(c: *cgen, n: *node) void = {
// path the cgen falls through and AX retains whatever the
// inner expression left there — typically the *struct pointer
// itself, so reads silently get the pointer value instead of
// the field. (Showed up porting 6l/pass.ww.)
// the field. (Showed up porting w6l/pass.ww.)
if (lhs != nil) {
if (lhs.kind == N_DOT) {
let inner_t: *node = dot_inner_struct_ptr(c, lhs);
@@ -5487,7 +5515,7 @@ fn cgexpr(c: *cgen, n: *node) void = {
};
cgexpr(c, n.rhs);
// Push order matches C cgen
// (cmd/6c/cgen.c:1033-1041): PUSHQ AX
// (cmd/w6c/cgen.c:1033-1041): PUSHQ AX
// (ptr) first, then PUSHQ BX (len) if
// str, so the pop sequence is POP CX
// (len) → POP AX (ptr) → MOVQ AX,
@@ -6259,7 +6287,7 @@ fn cgstmt(c: *cgen, n: *node) void = {
};
} else {
// Bare `let x: T;` with no initializer. C cgen
// (cmd/6c/cgen.c:2181-2183) zero-inits only when
// (cmd/w6c/cgen.c:2181-2183) zero-inits only when
// the underlying type's natural size is 8 — pointers,
// i64/u64, function pointers, ints. Structs/arrays/
// slices/strings/tagged/tuples are left for per-field
@@ -6330,7 +6358,7 @@ fn cgstmt(c: *cgen, n: *node) void = {
// Tuple-destructure assign: `a, b = call();`. The call's tuple
// return lands in (AX, DX); push DX to free it, store AX into
// the first lvalue, then pop DX into the second. Mirrors
// cmd/6c/cgen.c:2424-2440. Lvalues beyond two are dropped (same
// cmd/w6c/cgen.c:2424-2440. Lvalues beyond two are dropped (same
// as C — no fixture uses >2 today).
if (k == N_MASSIGN) {
if (n.rhs != nil) { cgexpr(c, n.rhs); };

View File

@@ -18,19 +18,21 @@ export fn assert(cond: bool, msg: str) void = {
if (!cond) { abort(msg); };
};
def SYS_READ: i64 = 0;
def SYS_WRITE: i64 = 1;
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;
def SYS_EXIT: i64 = 60;
def SYS_WAIT4: i64 = 61;
def SYS_UNLINK: i64 = 87;
def SYS_READ: i64 = 0;
def SYS_WRITE: i64 = 1;
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;
def SYS_EXIT: i64 = 60;
def SYS_WAIT4: i64 = 61;
def SYS_UNLINK: i64 = 87;
def SYS_GETCWD: i64 = 79;
def SYS_GETDENTS64: i64 = 217;
// open(2) flags. Linux values, matching <fcntl.h>.
def O_RDONLY: i32 = 0;
@@ -65,7 +67,7 @@ export fn close(fd: i32) 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
// negative errno. Used by w6c_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;
@@ -179,6 +181,32 @@ export fn wait4(pid: i32, status_out: *i32, options: i32, rusage: *void) i32 = {
options: i64, rusage: i64): i32;
};
// getcwd(2) — Linux flavour. Writes the NUL-terminated cwd into `buf`
// and returns the number of bytes written (including the NUL), or a
// negative errno. The driver uses it to expand `.` to the cwd's
// basename for `ww build` / `ww test`.
export fn getcwd(buf: *u8, n: u64) i64 = {
return syscall2(SYS_GETCWD, buf: i64, n: i64);
};
// getdents64(2) — Linux directory enumeration. The fd must be opened
// with O_RDONLY on a directory. `buf` receives a packed sequence of
// linux_dirent64 records:
//
// struct linux_dirent64 {
// u64 d_ino; // 0..7
// i64 d_off; // 8..15
// u16 d_reclen; // 16..17 — total bytes for this record
// u8 d_type; // 18 — DT_REG/DT_DIR/...
// u8 d_name[]; // 19.. — NUL-terminated name + padding
// };
//
// Returns bytes written into `buf` (advance by d_reclen to walk),
// 0 at end-of-directory, or a negative errno.
export fn getdents64(fd: i32, buf: *u8, n: u64) i64 = {
return syscall3(SYS_GETDENTS64, fd: i64, buf: i64, n: i64);
};
// strconv — number↔string conversions. Decimal i64 to/from a fixed
// buffer. Two error idioms ship side by side:
// - Plan 9 style (atoi64): tuple `(value, ok)`. Pre-dates the
@@ -406,7 +434,7 @@ export fn toupper(c: u8) u8 = {
// indicating which probe broke. The 990_selfhost test asserts 42.
//
// Note: only stack-local mutable state. Top-level `let` mutation
// requires a writable .data segment in 6l, which is a separate
// requires a writable .data segment in w6l, which is a separate
// task; until then we exercise polymorphism via ctx pointers, which
// is what the real port wants anyway.
@@ -423,7 +451,7 @@ type arena = struct {
};
// In-place init. Returning a 24-byte struct by value isn't yet
// supported in 6c (SysV requires a hidden return-slot pointer for
// supported in w6c (SysV requires a hidden return-slot pointer for
// structs >16 bytes), so we initialize through a pointer like the
// real compiler does today.
fn arena_init(a: *arena, buf: *u8, cap: u64) void = {

View File

@@ -12,7 +12,7 @@
// indicating which probe broke. The 990_selfhost test asserts 42.
//
// Note: only stack-local mutable state. Top-level `let` mutation
// requires a writable .data segment in 6l, which is a separate
// requires a writable .data segment in w6l, which is a separate
// task; until then we exercise polymorphism via ctx pointers, which
// is what the real port wants anyway.
@@ -29,7 +29,7 @@ type arena = struct {
};
// In-place init. Returning a 24-byte struct by value isn't yet
// supported in 6c (SysV requires a hidden return-slot pointer for
// supported in w6c (SysV requires a hidden return-slot pointer for
// structs >16 bytes), so we initialize through a pointer like the
// real compiler does today.
fn arena_init(a: *arena, buf: *u8, cap: u64) void = {

View File

@@ -3,7 +3,7 @@
// `type NAME = TYPE;` (alias + struct), top-level `let NAME: TYPE = LIT;`.
//
// Function declarations are still recovered past — the body parser
// is the next major chunk. See selfhost/cmd/wwc/parse.ww header.
// is the next major chunk. See selfhost/cmd/wcc/parse.ww header.
use os;
use mem;