Files
ww/selfhost/cmd/ww/main.ww
Hojun-Cho 502b304841 ww: driver shells to wwstage tools
build_one now invokes 6c_ww / 6a_ww / 6l_ww from $self_dir, not
the C-built binaries that share the directory. After this change
`ww_ww build foo.ww` touches no cstage code at runtime — the
fresh-checkout cstage is still needed to bring the wwstage into
existence, but day-to-day work runs on the ww toolchain end to
end. The C `ww` driver in cmd/ww/ still drives the C 6c/6a/6l.

Test 993 (which used to be trivial — both drivers invoked the
same C tools) now meaningfully compares the cstage pipeline
against the wwstage pipeline on hello + wwdump and confirms
byte-identical exes.

The .combined.ww files for 6a/6l/ww/wwdump and smoke are
regenerated by the ww driver's `expand()` step; their diff is
the lib/os dup2 wrapper and the cgen.ww port from the prior two
commits, propagating into the bootstrap inputs.
2026-05-11 11:20:23 +09:00

702 lines
18 KiB
Plaintext

// selfhost/cmd/ww/main.ww — port of cmd/ww/main.c.
//
// 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 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
// not yet supported in this port; the bootstrap doesn't need them.
use os;
use mem;
// All path/string scratch buffers go on the runtime page allocator.
// One page is plenty for any path we build.
def PATH_MAX: u64 = 4096u64;
def CMD_MAX: u64 = 8192u64;
// ---- C-string helpers --------------------------------------------------
fn cstrlen(p: *u8) u64 = {
let n: u64 = 0u64;
for (p[n] != 0u8) { n += 1u64; };
return n;
};
fn cstreq(a: *u8, b: *u8) bool = {
let i: u64 = 0u64;
for (a[i] == b[i]) {
if (a[i] == 0u8) { return true; };
i += 1u64;
};
return false;
};
// cstreq_lit — compare a NUL-terminated *u8 to a ww string literal.
fn cstreq_lit(a: *u8, lit: str) bool = {
let n: i32 = lit.len;
let i: i32 = 0;
for (i < n) {
if (a[i] != lit[i]) { return false; };
i += 1;
};
return a[n] == 0u8;
};
// startswith — does a have b as a prefix?
fn cstr_startswith(a: *u8, b: *u8) bool = {
let i: u64 = 0u64;
for (b[i] != 0u8) {
if (a[i] != b[i]) { return false; };
i += 1u64;
};
return true;
};
// memcpy
fn bytecpy(dst: *u8, src: *u8, n: u64) void = {
let i: u64 = 0u64;
for (i < n) {
dst[i] = src[i];
i += 1u64;
};
};
// Copy a NUL-terminated *u8 into dst starting at off; return the new
// offset (without writing a NUL).
fn cstr_into(dst: *u8, off: u64, src: *u8) u64 = {
let i: u64 = 0u64;
for (src[i] != 0u8) {
dst[off + i] = src[i];
i += 1u64;
};
return off + i;
};
// Same, but for a ww `str` (no NUL on the source side; we copy len bytes).
fn str_into(dst: *u8, off: u64, src: str) u64 = {
let n: i32 = src.len;
let i: i32 = 0;
for (i < n) {
let iu: u64 = i: u64;
dst[off + iu] = src[i];
i += 1;
};
let nu: u64 = n: u64;
return off + nu;
};
// Write a single byte, return new offset.
fn byte_into(dst: *u8, off: u64, c: u8) u64 = {
dst[off] = c;
return off + 1u64;
};
// NUL-terminate at off and return the same off (handy when passing the
// buffer to a syscall that expects a C-string).
fn cstr_seal(dst: *u8, off: u64) void = {
dst[off] = 0u8;
};
// ---- Tool-path resolution ---------------------------------------------
// dirname-equivalent: copy argv[0] up to (but not including) the last
// '/' into dst, NUL-terminated. If no slash, write ".".
fn self_dir_into(dst: *u8, dstsz: u64, argv0: *u8) void = {
let n: u64 = cstrlen(argv0);
let cut: u64 = 0u64;
let i: u64 = 0u64;
for (i < n) {
if (argv0[i] == 47u8) { cut = i; }; // '/'
i += 1u64;
};
if (cut == 0u64) {
dst[0u64] = 46u8; // '.'
dst[1u64] = 0u8;
return;
};
if (cut + 1u64 >= dstsz) { cut = dstsz - 2u64; };
bytecpy(dst, argv0, cut);
dst[cut] = 0u8;
};
// Build "$dir/$name" (NUL-terminated) into a fresh page-sized buffer.
fn join_path(dir: *u8, name: *u8) *u8 = {
let buf: *u8 = os.alloc(PATH_MAX): *u8;
let off: u64 = cstr_into(buf, 0u64, dir);
off = byte_into(buf, off, 47u8);
off = cstr_into(buf, off, name);
cstr_seal(buf, off);
return buf;
};
// Same, but the second component is a ww `str` literal.
fn join_path_lit(dir: *u8, name: str) *u8 = {
let buf: *u8 = os.alloc(PATH_MAX): *u8;
let off: u64 = cstr_into(buf, 0u64, dir);
off = byte_into(buf, off, 47u8);
off = str_into(buf, off, name);
cstr_seal(buf, off);
return buf;
};
// ---- Subprocess plumbing ----------------------------------------------
// proc_run — fork, execve `path` with `argv` (NULL-terminated), wait.
// Returns 0 on clean exit-0, 1 on any non-zero exit or signal kill,
// -1 on fork/wait failure.
fn proc_run(path: *u8, argv: **u8) i32 = {
let pid: i32 = os.fork();
if (pid < 0) {
os.write(2, "ww: fork failed\n".ptr, 16u64);
return -1;
};
if (pid == 0) {
os.execve(path, argv, nil: **u8);
os.write(2, "ww: execve failed\n".ptr, 18u64);
os.exit(127);
};
let status: i32 = 0;
let r: i32 = os.wait4(pid, &status, 0i32, nil: *void);
if (r < 0) {
os.write(2, "ww: wait4 failed\n".ptr, 17u64);
return -1;
};
// Linux wait status: low byte = signal (0 if exited cleanly),
// next byte = exit code.
if ((status & 127i32) != 0) { return 1; };
let code: i32 = (status >> 8i32) & 255i32;
if (code != 0) { return 1; };
return 0;
};
// ---- `use` resolution + source concatenation --------------------------
//
// Recursive expansion: for each `use IDENT;` we find at the top of
// `path`, resolve via the colon-separated `dirs`, expand the imported
// file first, then append our own bytes. Already-visited paths are
// skipped (linear scan; typical builds visit a handful of modules).
type strnode = struct {
s: str,
snext: *strnode,
};
type expctx = struct {
a: *arena, // arena for path strings + the visited list
out: i32, // fd we're writing the combined source to
dirs: *u8, // ":"-separated search path (NUL-terminated)
visit: *strnode,
};
fn visit_seen(c: *expctx, path: str) bool = {
let n: *strnode = c.visit;
for (n != nil) {
if (n.s.len == path.len) {
let i: i32 = 0;
let eq: bool = true;
for (i < path.len) {
if (n.s[i] != path[i]) { eq = false; i = path.len; }
else { i += 1; };
};
if (eq) { return true; };
};
n = n.snext;
};
return false;
};
fn visit_add(c: *expctx, path: str) void = {
let n: *strnode = amalloc(c.a, 32u64): *strnode;
n.s = path;
n.snext = c.visit;
c.visit = n;
};
// Try <dir>/<name>.ww then <dir>/<name>/<name>.ww. Returns NUL-terminated
// arena-resident path if found, else nil.
fn locate_in(a: *arena, dir: *u8, dir_len: u64, name: *u8, name_len: u64) *u8 = {
// candidate 1: <dir>/<name>.ww
let buf: *u8 = amalloc(a, PATH_MAX): *u8;
let off: u64 = 0u64;
let i: u64 = 0u64;
for (i < dir_len) { buf[off + i] = dir[i]; i += 1u64; };
off += dir_len;
buf[off] = 47u8; off += 1u64; // '/'
i = 0u64;
for (i < name_len) { buf[off + i] = name[i]; i += 1u64; };
off += name_len;
buf[off] = 46u8; off += 1u64; // '.'
buf[off] = 119u8; off += 1u64; // 'w'
buf[off] = 119u8; off += 1u64; // 'w'
buf[off] = 0u8;
if (os.access(buf, 0i32) == 0) { return buf; };
// candidate 2: <dir>/<name>/<name>.ww
let buf2: *u8 = amalloc(a, PATH_MAX): *u8;
off = 0u64;
i = 0u64;
for (i < dir_len) { buf2[off + i] = dir[i]; i += 1u64; };
off += dir_len;
buf2[off] = 47u8; off += 1u64;
i = 0u64;
for (i < name_len) { buf2[off + i] = name[i]; i += 1u64; };
off += name_len;
buf2[off] = 47u8; off += 1u64;
i = 0u64;
for (i < name_len) { buf2[off + i] = name[i]; i += 1u64; };
off += name_len;
buf2[off] = 46u8; off += 1u64;
buf2[off] = 119u8; off += 1u64;
buf2[off] = 119u8; off += 1u64;
buf2[off] = 0u8;
if (os.access(buf2, 0i32) == 0) { return buf2; };
return nil;
};
// Walk a colon-separated dirlist, return first hit or nil.
fn locate_import(a: *arena, dirs: *u8, name: *u8, name_len: u64) *u8 = {
let total: u64 = cstrlen(dirs);
let p: u64 = 0u64;
for (p < total) {
let q: u64 = p;
for (q < total) {
if (dirs[q] == 58u8) { break; }; // ':'
q += 1u64;
};
let seg_len: u64 = q - p;
if (seg_len > 0u64) {
let hit: *u8 = locate_in(a, dirs + p, seg_len, name, name_len);
if (hit != nil) { return hit; };
};
p = q + 1u64;
};
return nil;
};
// ---- file slurp -------------------------------------------------------
fn read_all(path_cs: *u8) (*u8, u64) = {
let fd: i32 = os.open(path_cs, os.O_RDONLY, 0i32);
if (fd < 0) { return nil, 0u64; };
let n: i64 = os.filesize(fd);
if (n < 0i64) { os.close(fd); return nil, 0u64; };
let nu: u64 = n: u64;
let buf: *u8 = os.alloc(nu + 1u64): *u8;
let got: i64 = os.readfull(fd, buf, nu);
os.close(fd);
if (got != n) { return nil, 0u64; };
buf[nu] = 0u8;
return buf, nu;
};
fn is_ident_byte(c: u8) bool = {
if (c >= 97u8) { if (c <= 122u8) { return true; }; }; // a..z
if (c >= 65u8) { if (c <= 90u8) { return true; }; }; // A..Z
if (c >= 48u8) { if (c <= 57u8) { return true; }; }; // 0..9
if (c == 95u8) { return true; }; // _
if (c == 46u8) { return true; }; // .
return false;
};
// Scan one `use IDENT;` line out of [start, end). Returns the start of
// the ident and its length, or (nil, 0) if no `use` here. The caller
// passes a slice of the source: src points at the line start.
fn scan_use(src: *u8, len: u64) (*u8, u64) = {
let i: u64 = 0u64;
// skip leading whitespace
for (i < len) {
if (src[i] != 32u8) { if (src[i] != 9u8) { break; }; };
i += 1u64;
};
if (i + 4u64 > len) { return nil, 0u64; };
if (src[i] != 117u8) { return nil, 0u64; }; // 'u'
if (src[i + 1u64] != 115u8) { return nil, 0u64; }; // 's'
if (src[i + 2u64] != 101u8) { return nil, 0u64; }; // 'e'
let sep: u8 = src[i + 3u64];
if (sep != 32u8) { if (sep != 9u8) { return nil, 0u64; }; };
i += 4u64;
for (i < len) {
if (src[i] != 32u8) { if (src[i] != 9u8) { break; }; };
i += 1u64;
};
let id_start: u64 = i;
for (i < len) {
if (!is_ident_byte(src[i])) { break; };
i += 1u64;
};
let id_len: u64 = i - id_start;
if (id_len == 0u64) { return nil, 0u64; };
return src + id_start, id_len;
};
// Recursively expand `path` into c.out. Imported files are emitted
// before their importer; cycles are broken via the visited set.
fn expand(c: *expctx, path_cs: *u8) void = {
let plen: u64 = cstrlen(path_cs);
let path_str: str = astrndup(c.a, path_cs, plen);
if (visit_seen(c, path_str)) { return; };
visit_add(c, path_str);
let bufp: *u8;
let blen: u64;
bufp, blen = read_all(path_cs);
if (bufp == nil) {
os.write(2, "ww: cannot read source\n".ptr, 23u64);
return;
};
// Pass 1: scan top-of-file `use X;` lines, recursively expand.
let i: u64 = 0u64;
for (i < blen) {
// Find the end of the current line.
let j: u64 = i;
for (j < blen) {
if (bufp[j] == 10u8) { break; }; // '\n'
j += 1u64;
};
let id_p: *u8;
let id_n: u64;
id_p, id_n = scan_use(bufp + i, j - i);
if (id_p != nil) {
let ipath: *u8 = locate_import(c.a, c.dirs, id_p, id_n);
if (ipath != nil) {
expand(c, ipath);
};
};
i = j + 1u64;
};
// Pass 2: emit our own bytes, then a trailing newline.
os.writefull(c.out, bufp, blen);
os.writefull(c.out, "\n".ptr, 1u64);
};
// ---- Build pipeline ---------------------------------------------------
// Strip the trailing ".ww" off `src` (a NUL-terminated path) into
// `stem`, NUL-terminated. If there's no .ww, the stem is the whole
// path.
fn make_stem(stem: *u8, src: *u8) void = {
let n: u64 = cstrlen(src);
let stop: u64 = n;
if (n >= 3u64) {
if (src[n - 3u64] == 46u8) { // '.'
if (src[n - 2u64] == 119u8) { // 'w'
if (src[n - 1u64] == 119u8) { // 'w'
stop = n - 3u64;
};
};
};
};
let i: u64 = 0u64;
for (i < stop) { stem[i] = src[i]; i += 1u64; };
stem[stop] = 0u8;
};
// Append a literal suffix to `stem` (which already lives in a buffer).
fn append_lit(stem: *u8, suffix: str) *u8 = {
let buf: *u8 = os.alloc(PATH_MAX): *u8;
let off: u64 = cstr_into(buf, 0u64, stem);
off = str_into(buf, off, suffix);
cstr_seal(buf, off);
return buf;
};
// 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)
// 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)
//
// 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
// pipelines to byte-identical output on a corpus.
fn build_one(self_dir: *u8, src: *u8, out: *u8, incs: *u8) 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");
// Default lib search path: <self_dir>/../../lib
let dotdot_lib: *u8 = os.alloc(PATH_MAX): *u8;
{
let off: u64 = cstr_into(dotdot_lib, 0u64, self_dir);
off = str_into(dotdot_lib, off, "/../../lib");
cstr_seal(dotdot_lib, off);
};
// Compose searchpath: incs + ':' + dotdot_lib (or just dotdot_lib).
let searchpath: *u8 = os.alloc(PATH_MAX * 2u64): *u8;
{
let off: u64 = 0u64;
if (incs[0u64] != 0u8) {
off = cstr_into(searchpath, off, incs);
off = byte_into(searchpath, off, 58u8); // ':'
};
off = cstr_into(searchpath, off, dotdot_lib);
cstr_seal(searchpath, off);
};
// stem, .s, .o, .combined.ww, libwwrt.a
let stem: *u8 = os.alloc(PATH_MAX): *u8;
make_stem(stem, src);
let asmf: *u8 = append_lit(stem, ".s");
let objf: *u8 = append_lit(stem, ".o");
let combined: *u8 = append_lit(stem, ".combined.ww");
// libwwrt.a path: <self_dir>/../lib/libwwrt.a
let libwwrt: *u8 = os.alloc(PATH_MAX): *u8;
{
let off: u64 = cstr_into(libwwrt, 0u64, self_dir);
off = str_into(libwwrt, off, "/../lib/libwwrt.a");
cstr_seal(libwwrt, off);
};
// Step 1: expand `use`s into the combined file.
let cf: i32 = os.open(combined, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 420i32); // 0o644
if (cf < 0) {
os.write(2, "ww: cannot open combined\n".ptr, 25u64);
return 1;
};
{
let c: expctx;
c.a = a;
c.out = cf;
c.dirs = searchpath;
c.visit = nil;
expand(&c, src);
};
os.close(cf);
// Step 2: 6c -o <stem>.s <stem>.combined.ww
{
let argv: **u8 = os.alloc(40u64): **u8;
argv[0] = "6c\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);
return 1;
};
};
// Step 3: 6a -o <stem>.o <stem>.s
{
let argv: **u8 = os.alloc(40u64): **u8;
argv[0] = "6a\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);
return 1;
};
};
// Step 4: 6l -o <out> <stem>.o libwwrt.a
{
let argv: **u8 = os.alloc(48u64): **u8;
argv[0] = "6l\0".ptr;
argv[1] = "-o\0".ptr;
argv[2] = out;
argv[3] = objf;
argv[4] = libwwrt;
argv[5] = nil;
if (proc_run(l6, argv) != 0) {
os.write(2, "ww: 6l failed\n".ptr, 14u64);
return 1;
};
};
return 0;
};
// ---- 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";
os.write(fd, s.ptr, s.len: u64);
};
fn do_version() i32 = {
os.write(1, "ww 0.0\n".ptr, 7u64);
return 0;
};
// Compute the basename of src (without trailing ".ww") into a fresh
// buffer. Used as the default output path for `ww build`.
fn default_out_path(src: *u8) *u8 = {
let n: u64 = cstrlen(src);
let start: u64 = 0u64;
let i: u64 = 0u64;
for (i < n) {
if (src[i] == 47u8) { start = i + 1u64; }; // '/'
i += 1u64;
};
let out: *u8 = os.alloc(PATH_MAX): *u8;
let off: u64 = 0u64;
let j: u64 = start;
for (j < n) {
out[off] = src[j];
off += 1u64;
j += 1u64;
};
// Strip ".ww" if present.
if (off >= 3u64) {
if (out[off - 3u64] == 46u8) {
if (out[off - 2u64] == 119u8) {
if (out[off - 1u64] == 119u8) {
off -= 3u64;
};
};
};
};
cstr_seal(out, off);
return out;
};
fn do_build(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
let src: *u8 = nil;
let incs: *u8 = os.alloc(PATH_MAX * 2u64): *u8;
let inc_off: u64 = 0u64;
cstr_seal(incs, 0u64);
let i: i32 = start;
for (i < argc) {
let p: *u8 = argv[i];
// -I <dir>
if (p[0u64] == 45u8) {
if (p[1u64] == 73u8) { // '-I'
let dir: *u8 = nil;
if (p[2u64] != 0u8) {
dir = p + 2u64;
} else {
if (i + 1 >= argc) {
os.write(2, "ww build: -I needs an argument\n".ptr, 31u64);
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 {
// -lLIB silently ignored for now (driver doesn't yet
// pass extra archives to 6l).
if (p[1u64] != 108u8) {
os.write(2, "ww build: unknown flag\n".ptr, 23u64);
return 2;
};
};
} else {
if (src == nil) { src = p; };
};
i += 1;
};
if (src == nil) {
os.write(2, "ww build: missing source\n".ptr, 25u64);
return 2;
};
let out: *u8 = default_out_path(src);
return build_one(self_dir, src, out, incs);
};
// Format the scratch path /tmp/ww_run_<pid> into buf. Returns NUL-
// terminated buf. Pid is folded in decimal manually since we don't
// import strconv.
fn make_run_tmp(buf: *u8) void = {
let off: u64 = 0u64;
off = str_into(buf, off, "/tmp/ww_run_");
let pid: i32 = os.getpid();
// itoa for non-negative pid
let dig: [16]u8;
let n: i32 = 0;
if (pid <= 0) {
dig[n] = 48u8; // '0'
n += 1;
} else {
let v: i32 = pid;
for (v > 0) {
dig[n] = ((v % 10) + 48): u8;
n += 1;
v = v / 10;
};
};
let k: i32 = n - 1;
for (k >= 0) {
buf[off] = dig[k];
off += 1u64;
k -= 1;
};
cstr_seal(buf, off);
};
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 tmp: *u8 = os.alloc(PATH_MAX): *u8;
make_run_tmp(tmp);
if (build_one(self_dir, argv[start], tmp, "\0".ptr) != 0) { return 1; };
let exec_argv: **u8 = os.alloc(16u64): **u8;
exec_argv[0] = tmp;
exec_argv[1] = nil;
let rc: i32 = proc_run(tmp, exec_argv);
os.unlink(tmp);
return rc;
};
// ---- Entry -------------------------------------------------------------
export fn main(argc: i32, argv: **u8) i32 = {
if (argc < 1) {
write_usage(2);
return 2;
};
// self_dir = dirname(argv[0])
let self_dir: *u8 = os.alloc(PATH_MAX): *u8;
self_dir_into(self_dir, PATH_MAX, argv[0]);
if (argc < 2) {
write_usage(2);
return 2;
};
let cmd: *u8 = argv[1];
if (cstreq_lit(cmd, "-V")) { return do_version(); };
if (cstreq_lit(cmd, "version")) { return do_version(); };
if (cstreq_lit(cmd, "-h")) {
write_usage(1);
return 0;
};
if (cstreq_lit(cmd, "--help")) {
write_usage(1);
return 0;
};
if (cstreq_lit(cmd, "build")) {
return do_build(self_dir, argv, argc, 2);
};
if (cstreq_lit(cmd, "run")) {
return do_run(self_dir, argv, argc, 2);
};
os.write(2, "ww: unknown subcommand\n".ptr, 23u64);
write_usage(2);
return 2;
};