Files
ww/selfhost/cmd/ww/main.combined.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

991 lines
27 KiB
Plaintext

// os — process and filesystem facade. The body of each call lands
// either in libwwrt.a (rt_syscall trampoline) or libc bindings,
// depending on how the program was linked.
@symbol("rt_syscall") fn syscall0(num: i64) i64;
@symbol("rt_syscall") fn syscall1(num: i64, a: i64) i64;
@symbol("rt_syscall") fn syscall2(num: i64, a: i64, b: i64) i64;
@symbol("rt_syscall") fn syscall3(num: i64, a: i64, b: i64, c: i64) i64;
@symbol("rt_syscall") fn syscall4(num: i64, a: i64, b: i64, c: i64, d: i64) i64;
@symbol("rt_alloc") fn alloc(n: u64) *void;
@symbol("rt_free") fn free(p: *void, n: u64) void;
@symbol("rt_abort") fn abort(msg: str) void;
// Hare-style runtime check. Caller passes a message that's printed
// to stderr before exit(1).
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;
// open(2) flags. Linux values, matching <fcntl.h>.
def O_RDONLY: i32 = 0;
def O_WRONLY: i32 = 1;
def O_RDWR: i32 = 2;
def O_CREAT: i32 = 64; // 0x40
def O_TRUNC: i32 = 512; // 0x200
// lseek(2) whence.
def SEEK_SET: i32 = 0;
def SEEK_CUR: i32 = 1;
def SEEK_END: i32 = 2;
export fn exit(code: i32) void = {
syscall1(SYS_EXIT, code: i64);
};
// Raw, non-fallible primitives. These return Linux's int conventions
// (negative = -errno, non-negative = bytes/fd/etc). Callers wanting a
// Hare-style fallible API use the wrappers below.
export fn write(fd: i32, buf: *u8, n: u64) i64 = {
return syscall3(SYS_WRITE, fd: i64, buf: i64, n: i64);
};
export fn read(fd: i32, buf: *u8, n: u64) i64 = {
return syscall3(SYS_READ, fd: i64, buf: i64, n: i64);
};
export fn close(fd: i32) i32 = {
return syscall1(SYS_CLOSE, fd: i64): i32;
};
// dup2(2): make `newfd` refer to the same description as `oldfd`,
// closing `newfd` first if open. Returns `newfd` on success or a
// negative errno. Used by 6c_ww to redirect stdout into an output
// file without changing the cgen emit path.
export fn dup2(oldfd: i32, newfd: i32) i32 = {
return syscall2(SYS_DUP2, oldfd: i64, newfd: i64): i32;
};
// Fallible wrappers. The error variant is a plain str (Plan 9 errstr
// model, see lib/errors); the sum type makes success/failure explicit
// without overloading length-zero.
export fn tryread(fd: i32, buf: *u8, n: u64) (i64 | str) = {
let r: i64 = read(fd, buf, n);
if (r < 0) { return "read failed"; };
return r;
};
export fn trywrite(fd: i32, buf: *u8, n: u64) (i64 | str) = {
let r: i64 = write(fd, buf, n);
if (r < 0) { return "write failed"; };
return r;
};
// open — Linux open(2). Path must be NUL-terminated; callers using ww
// `str` must ensure the bytes are followed by a 0 byte (literals are,
// arena-copied paths usually are by construction). Returns -errno on
// failure, fd otherwise. Higher-level callers prefer `tryopen`.
export fn open(path: *u8, flags: i32, mode: i32) i32 = {
return syscall3(SYS_OPEN, path: i64, flags: i64, mode: i64): i32;
};
export fn tryopen(path: *u8, flags: i32, mode: i32) (i32 | str) = {
let fd: i32 = open(path, flags, mode);
if (fd < 0) { return "open failed"; };
return fd;
};
// lseek — set/inspect the fd's position. Returns the new offset or
// a negative errno. We use this for fstat-free file-size discovery
// (open ⇒ lseek to end ⇒ lseek back).
export fn lseek(fd: i32, off: i64, whence: i32) i64 = {
return syscall3(SYS_LSEEK, fd: i64, off, whence: i64);
};
// filesize — convenience: returns the byte length of an open fd by
// seeking to the end and back. -1 on error.
export fn filesize(fd: i32) i64 = {
let end: i64 = lseek(fd, 0i64, SEEK_END);
if (end < 0) { return -1i64; };
let r: i64 = lseek(fd, 0i64, SEEK_SET);
if (r < 0) { return -1i64; };
return end;
};
// readfull — keep reading until `n` bytes have arrived or the fd
// closes early. Returns bytes read (0..=n) or -1 on read error.
export fn readfull(fd: i32, buf: *u8, n: u64) i64 = {
let got: u64 = 0u64;
for (got < n) {
let r: i64 = read(fd, buf + got, n - got);
if (r < 0) { return -1i64; };
if (r == 0) { return got: i64; }; // short read: caller decides
got += r: u64;
};
return got: i64;
};
// writefull — keep writing until `n` bytes have been accepted or the
// fd refuses progress. Returns bytes written or -1.
export fn writefull(fd: i32, buf: *u8, n: u64) i64 = {
let sent: u64 = 0u64;
for (sent < n) {
let r: i64 = write(fd, buf + sent, n - sent);
if (r < 0) { return -1i64; };
if (r == 0) { return sent: i64; };
sent += r: u64;
};
return sent: i64;
};
// ---- process and filesystem helpers used by the `ww` driver ----------
// access(2): returns 0 if the file is reachable, negative errno
// otherwise. mode is the bitset described in <unistd.h> (F_OK=0).
export fn access(path: *u8, mode: i32) i32 = {
return syscall2(SYS_ACCESS, path: i64, mode: i64): i32;
};
// unlink(2).
export fn unlink(path: *u8) i32 = {
return syscall1(SYS_UNLINK, path: i64): i32;
};
// getpid(2). Used by the driver to mint unique scratch paths.
export fn getpid() i32 = {
return syscall0(SYS_GETPID): i32;
};
// fork(2): 0 in the child, child pid in the parent, negative errno
// on failure.
export fn fork() i32 = {
return syscall0(SYS_FORK): i32;
};
// execve(2): on success, does not return.
export fn execve(path: *u8, argv: **u8, envp: **u8) i32 = {
return syscall3(SYS_EXECVE, path: i64, argv: i64, envp: i64): i32;
};
// wait4(2): wait for `pid` (or any child if -1), store status in
// `*status_out`, return the pid that ended (or negative errno).
export fn wait4(pid: i32, status_out: *i32, options: i32, rusage: *void) i32 = {
return syscall4(SYS_WAIT4, pid: i64, status_out: i64,
options: i64, rusage: i64): i32;
};
// selfhost/cmd/wwc/mem.ww — port of cmd/wwc/mem.c.
//
// Bump arena allocator. Backed by the runtime page allocator
// (rt_alloc / rt_free), no libc. Each chunk is mmap'd; when the
// current chunk runs out we link a fresh one. Freeing the arena
// 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.
use os;
def ALIGN: u64 = 16u64;
def INIT_CHUNK: u64 = 65536u64;
def MAX_CHUNK: u64 = 4194304u64;
def ARENA_SZ: u64 = 48u64; // sizeof(arena), kept in sync below
type arena = struct {
buf: *u8,
off: u64,
cap: u64,
next: *arena,
total: u64,
};
fn roundup(n: u64, a: u64) u64 = {
return (n + a - 1u64) & ~(a - 1u64);
};
export fn newarena() *arena = {
let a: *arena = os.alloc(ARENA_SZ): *arena;
a.buf = os.alloc(INIT_CHUNK): *u8;
a.off = 0u64;
a.cap = INIT_CHUNK;
a.next = nil;
a.total = 0u64;
return a;
};
// Grow: link a fresh chunk in front of the head. We push the old
// chunk into `next` so the head always describes the current bump
// region. Chunk size doubles up to MAX_CHUNK.
fn grow(a: *arena, need: u64) bool = {
let want: u64 = a.cap * 2u64;
if (want < need) { want = need; };
if (want > MAX_CHUNK) { want = MAX_CHUNK; };
if (want < need) { return false; }; // single allocation too big
let old: *arena = os.alloc(ARENA_SZ): *arena;
old.buf = a.buf;
old.off = a.off;
old.cap = a.cap;
old.next = a.next;
old.total = 0u64;
a.buf = os.alloc(want): *u8;
a.off = 0u64;
a.cap = want;
a.next = old;
return true;
};
export fn amalloc(a: *arena, n: u64) *void = {
let need: u64 = roundup(n, ALIGN);
if (need > a.cap - a.off) {
if (!grow(a, need)) { return nil; };
};
let p: *u8 = a.buf + a.off;
a.off += need;
a.total += need;
// Zero the region. Plan 9 amalloc zeroes; we mirror that here so
// the checker can assume freshly allocated nodes start at 0.
let i: u64 = 0u64;
for (i < need) {
p[i] = 0u8;
i += 1u64;
};
return p: *void;
};
// astrndup — copy `n` bytes into the arena and produce a NUL-terminated
// view. Returns a `str` whose ptr is arena-owned and whose len is `n`
// (the trailing NUL is past `len`, so callers reading exactly n bytes
// see no padding). Used by the lexer to capture token text.
export fn astrndup(a: *arena, src: *u8, n: u64) str = {
let p: *u8 = amalloc(a, n + 1u64): *u8;
let i: u64 = 0u64;
for (i < n) {
p[i] = src[i];
i += 1u64;
};
p[n] = 0u8;
let r: str;
r.ptr = p;
r.len = n: i32;
return r;
};
export fn freearena(a: *arena) void = {
for (a != nil) {
let next: *arena = a.next;
os.free(a.buf: *void, a.cap);
os.free(a: *void, ARENA_SZ);
a = next;
};
};
// 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;
};