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

@@ -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;