selfhost/cmd/6c/main.ww is a thin packaging of the wwc cgen — slurp a .ww file, run lex+parse+cgen, write Plan 9 amd64 asm to the path given by -o. The cgen routines in selfhost/cmd/wwc/cgen.ww write directly to fd 1, so we use dup2 to redirect stdout into the output file rather than thread an fd through every emit helper. Adds the SYS_DUP2=33 wrapper in lib/os. Makefile wires $(BIN)/6c_ww alongside the other wwstage tools and adds $(BIN)/test_6c_ww to the TESTS list. test/wwc/994_6c_ww.c diffs 6c_ww byte-for-byte against `wwdump_ww -c` on five in-source programs plus the four selfhost main.combined.ww files: same cgen reached through two binaries, so any divergence is a packaging bug in selfhost/cmd/6c. We deliberately don't diff against C-side 6c here — 990 probe 5 already covers that on the subset the ww cgen handles today.
181 lines
5.8 KiB
Plaintext
181 lines
5.8 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;
|
|
};
|