690 lines
21 KiB
Plaintext
690 lines
21 KiB
Plaintext
// MODULE: os
|
|
// 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: nr) i64;
|
|
@symbol("rt_syscall") fn syscall1(num: nr, a: i64) i64;
|
|
@symbol("rt_syscall") fn syscall2(num: nr, a: i64, b: i64) i64;
|
|
@symbol("rt_syscall") fn syscall3(num: nr, a: i64, b: i64, c: i64) i64;
|
|
@symbol("rt_syscall") fn syscall4(num: nr, 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); };
|
|
};
|
|
|
|
// Linux amd64 syscall numbers. Internal to this module — passed as
|
|
// the first arg of syscall0..4 via libwwrt's rt_syscall trampoline.
|
|
// `nr` is the type so the call sites can't accidentally pass an
|
|
// arbitrary i64 (`syscall1(0i64, ...)` no longer typechecks).
|
|
type nr = enum i64 {
|
|
READ = 0,
|
|
WRITE = 1,
|
|
OPEN = 2,
|
|
CLOSE = 3,
|
|
LSEEK = 8,
|
|
ACCESS = 21,
|
|
DUP2 = 33,
|
|
GETPID = 39,
|
|
FORK = 57,
|
|
EXECVE = 59,
|
|
EXIT = 60,
|
|
WAIT4 = 61,
|
|
UNLINK = 87,
|
|
GETCWD = 79,
|
|
GETDENTS64 = 217,
|
|
};
|
|
|
|
// open(2) flags. Linux values, matching <fcntl.h>. Hare names them
|
|
// `fs::flag::RDONLY` etc; we use the same leaf names so callers say
|
|
// `os.flag.RDONLY` and `os.flag.WRONLY | os.flag.CREATE`.
|
|
export type flag = enum i32 {
|
|
RDONLY = 0,
|
|
WRONLY = 1,
|
|
RDWR = 2,
|
|
CREATE = 64, // 0x40
|
|
TRUNC = 512, // 0x200
|
|
};
|
|
|
|
// lseek(2) whence. Hare names it `io::whence`.
|
|
export type whence = enum i32 {
|
|
SET = 0,
|
|
CUR = 1,
|
|
END = 2,
|
|
};
|
|
|
|
export fn exit(code: i32) void = {
|
|
syscall1(nr.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(nr.WRITE, fd: i64, buf: i64, n: i64);
|
|
};
|
|
|
|
export fn read(fd: i32, buf: *u8, n: u64) i64 = {
|
|
return syscall3(nr.READ, fd: i64, buf: i64, n: i64);
|
|
};
|
|
|
|
export fn close(fd: i32) i32 = {
|
|
return syscall1(nr.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 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(nr.DUP2, oldfd: i64, newfd: i64): i32;
|
|
};
|
|
|
|
// Fallible wrappers. The error variant is `oserror` (an i64 carrying
|
|
// -errno). The sum type makes success/failure explicit and lets
|
|
// callers `?` the result up the stack.
|
|
export fn tryread(fd: i32, buf: *u8, n: u64) (i64 | oserror) = {
|
|
let r: i64 = read(fd, buf, n);
|
|
if (r < 0) { return r: oserror; };
|
|
return r;
|
|
};
|
|
|
|
export fn trywrite(fd: i32, buf: *u8, n: u64) (i64 | oserror) = {
|
|
let r: i64 = write(fd, buf, n);
|
|
if (r < 0) { return r: oserror; };
|
|
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: flag, mode: i32) i32 = {
|
|
return syscall3(nr.OPEN, path: i64, (flags as i32): i64, mode: i64): i32;
|
|
};
|
|
|
|
export fn tryopen(path: *u8, flags: flag, mode: i32) (i32 | oserror) = {
|
|
let fd: i32 = open(path, flags, mode);
|
|
if (fd < 0) { return fd: i64: oserror; };
|
|
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, w: whence) i64 = {
|
|
return syscall3(nr.LSEEK, fd: i64, off, (w as i32): i64);
|
|
};
|
|
|
|
// oserror — the underlying errno from a failed syscall, as a
|
|
// negative i64 (Linux's int convention; e.g. -2 = ENOENT). The
|
|
// `!`-flagged alias makes ?-propagation pick this variant as the
|
|
// error half of any (T | oserror) shape. Hare's analogue is
|
|
// errors::errno carried inside io::error.
|
|
export type oserror = !i64;
|
|
|
|
// filesize — byte length of an open fd via lseek-to-end-and-back.
|
|
export fn filesize(fd: i32) (i64 | oserror) = {
|
|
let end: i64 = lseek(fd, 0i64, whence.END);
|
|
if (end < 0) { return end: oserror; };
|
|
let r: i64 = lseek(fd, 0i64, whence.SET);
|
|
if (r < 0) { return r: oserror; };
|
|
return end;
|
|
};
|
|
|
|
// readall — keep reading until `n` bytes have arrived or the fd
|
|
// closes early. Hare name (io::readall); the buffer is caller-
|
|
// supplied, matching the Plan 9 subset convention.
|
|
export fn readall(fd: i32, buf: *u8, n: u64) (i64 | oserror) = {
|
|
let got: u64 = 0u64;
|
|
for (got < n) {
|
|
let r: i64 = read(fd, buf + got, n - got);
|
|
if (r < 0) { return r: oserror; };
|
|
if (r == 0) { return got: i64; }; // short read: caller decides
|
|
got += r: u64;
|
|
};
|
|
return got: i64;
|
|
};
|
|
|
|
// writeall — keep writing until `n` bytes have been accepted or the
|
|
// fd refuses progress. Hare name (io::writeall).
|
|
export fn writeall(fd: i32, buf: *u8, n: u64) (i64 | oserror) = {
|
|
let sent: u64 = 0u64;
|
|
for (sent < n) {
|
|
let r: i64 = write(fd, buf + sent, n - sent);
|
|
if (r < 0) { return r: oserror; };
|
|
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(nr.ACCESS, path: i64, mode: i64): i32;
|
|
};
|
|
|
|
// remove — unlink(2). Hare name; the underlying syscall is unlink(2).
|
|
export fn remove(path: *u8) i32 = {
|
|
return syscall1(nr.UNLINK, path: i64): i32;
|
|
};
|
|
|
|
// getpid(2). Used by the driver to mint unique scratch paths.
|
|
export fn getpid() i32 = {
|
|
return syscall0(nr.GETPID): i32;
|
|
};
|
|
|
|
// fork(2): 0 in the child, child pid in the parent, negative errno
|
|
// on failure.
|
|
export fn fork() i32 = {
|
|
return syscall0(nr.FORK): i32;
|
|
};
|
|
|
|
// execve(2): on success, does not return.
|
|
export fn execve(path: *u8, argv: **u8, envp: **u8) i32 = {
|
|
return syscall3(nr.EXECVE, path: i64, argv: i64, envp: i64): i32;
|
|
};
|
|
|
|
// wait4(2): wait for `pid` (or any child if -1), store status in
|
|
// `*status`, return the pid that ended (or negative errno).
|
|
export fn wait4(pid: i32, status: *i32, options: i32, rusage: *void) i32 = {
|
|
return syscall4(nr.WAIT4, pid: i64, status: i64,
|
|
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(nr.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(nr.GETDENTS64, fd: i64, buf: i64, n: i64);
|
|
};
|
|
|
|
// MODULE: strconv
|
|
// strconv — number↔string conversions. Decimal i64 to/from a fixed
|
|
// buffer. Error shapes mirror Hare's strconv types: (T | invalid |
|
|
// overflow) where each error is a named alias over a payload type
|
|
// (Hare uses !size / !void; ww uses i32 / void without the `!` mark).
|
|
|
|
// invalid — input wasn't a valid number in the requested format.
|
|
// Payload is the byte index of the first offending position. Mirrors
|
|
// Hare's strconv::invalid = !size (we use i32 instead of size).
|
|
export type invalid = !i32;
|
|
|
|
// overflow — input was valid but doesn't fit the target type. No
|
|
// payload (a single yes/no signal). Mirrors Hare's !void shape.
|
|
export type overflow = !void;
|
|
|
|
// u64tos — write `v` in decimal into `buf` and return the byte count.
|
|
// Hare name; the buffer-in shape is the sanctioned Plan 9 subset of
|
|
// Hare's `u64tos(u, base) const str`. Unsigned-only so callers don't
|
|
// have to think about wraparound when printing a u64 with the high
|
|
// bit set.
|
|
export fn u64tos(buf: []u8, v: u64) i32 = {
|
|
let tmp: [32]u8;
|
|
let i: i32 = 0;
|
|
let n: u64 = v;
|
|
for (n > 0u64) {
|
|
tmp[i] = ((n % 10u64) + 48u64): u8;
|
|
n = n / 10u64;
|
|
i += 1;
|
|
};
|
|
if (i == 0) {
|
|
tmp[0] = 48u8;
|
|
i = 1;
|
|
};
|
|
let out: i32 = 0;
|
|
for (i > 0) {
|
|
i -= 1;
|
|
buf[out] = tmp[i];
|
|
out += 1;
|
|
};
|
|
return out;
|
|
};
|
|
|
|
export fn i64tos(buf: []u8, v: i64) i32 = {
|
|
let neg: bool = false;
|
|
let n: i64 = v;
|
|
if (n < 0) {
|
|
neg = true;
|
|
n = -n;
|
|
};
|
|
let tmp: [32]u8;
|
|
let i: i32 = 0;
|
|
for (n > 0) {
|
|
tmp[i] = ((n % 10) + 48): u8;
|
|
n = n / 10;
|
|
i += 1;
|
|
};
|
|
if (i == 0) {
|
|
tmp[0] = 48u8;
|
|
i = 1;
|
|
};
|
|
let out: i32 = 0;
|
|
if (neg) {
|
|
buf[out] = 45u8; // '-'
|
|
out += 1;
|
|
};
|
|
for (i > 0) {
|
|
i -= 1;
|
|
buf[out] = tmp[i];
|
|
out += 1;
|
|
};
|
|
return out;
|
|
};
|
|
|
|
// stoi64 — Hare-style fallible signed decimal parser. No locale, no
|
|
// whitespace, no underscores: a leading '-' is the only non-digit
|
|
// accepted, and only at position 0.
|
|
export fn stoi64(s: str) (i64 | invalid | overflow) = {
|
|
if (s.len == 0) { return 0: invalid; };
|
|
let i: i32 = 0;
|
|
let neg: bool = false;
|
|
if (s[0] == 45u8) { neg = true; i = 1; };
|
|
if (i >= s.len) { return i: invalid; };
|
|
let v: i64 = 0;
|
|
for (i < s.len) {
|
|
let c: u8 = s[i];
|
|
if (c < 48u8) { return i: invalid; };
|
|
if (c > 57u8) { return i: invalid; };
|
|
v = v * 10 + ((c: i64) - 48);
|
|
i += 1;
|
|
};
|
|
if (neg) { v = -v; };
|
|
return v;
|
|
};
|
|
|
|
// stou64 — fallible unsigned decimal parser. No leading sign.
|
|
export fn stou64(s: str) (u64 | invalid | overflow) = {
|
|
if (s.len == 0) { return 0: invalid; };
|
|
let v: u64 = 0u64;
|
|
let i: i32 = 0;
|
|
for (i < s.len) {
|
|
let c: u8 = s[i];
|
|
if (c < 48u8) { return i: invalid; };
|
|
if (c > 57u8) { return i: invalid; };
|
|
v = v * 10u64 + ((c: u64) - 48u64);
|
|
i += 1;
|
|
};
|
|
return v;
|
|
};
|
|
|
|
// f64tos — write `v` in decimal into `buf` and return the byte count.
|
|
// Hare name; this is the buffer-in Plan 9 subset of Hare's
|
|
// `f64tos(n) const str`. Today's surface:
|
|
//
|
|
// - finite values only. NaN/±Inf detection needs an f64→u64 bit
|
|
// reinterpret cast that the cgen doesn't expose yet.
|
|
// - fixed-point only, up to 6 fractional digits. Trailing zeros
|
|
// after the decimal point are trimmed. Trailing '.' is dropped.
|
|
// - magnitudes ≥ 9e18 (overflows i64 in the integer-part cast)
|
|
// fall back to the literal token "huge". Hare would print these
|
|
// in scientific notation via Ryū; we will graduate when the
|
|
// compiler grows the bit-reinterpret cast.
|
|
//
|
|
// Round-trip is therefore lossy past 6 fractional digits; callers
|
|
// that need bit-exact recovery should not use this until the
|
|
// graduate-to-Ryū step lands. `f64tos(buf, 1.0)` writes "1" (no
|
|
// decimal point), `f64tos(buf, 1.5)` writes "1.5", `f64tos(buf,
|
|
// 0.1)` writes "0.1".
|
|
//
|
|
// No float literals in the body — 990's wwdump diff requires this
|
|
// file's TK_FLOAT count to match between C and ww front-ends, and
|
|
// the ww-side wwdump currently skips TK_FLOAT.fval while the C side
|
|
// %g-formats it. Same trick lib/ww/lex/lex.ww's parsef64 uses:
|
|
// build f64 constants via int-to-f64 casts.
|
|
export fn f64tos(buf: []u8, v: f64) i32 = {
|
|
let out: i32 = 0;
|
|
let f: f64 = v;
|
|
let zero: f64 = 0: f64;
|
|
if (f < zero) {
|
|
buf[out] = 45u8; // '-'
|
|
out += 1;
|
|
f = -f;
|
|
};
|
|
// 9e18 is comfortably under I64_MAX (9.22e18). Past this the
|
|
// `f: i64` cast wraps and the integer part comes back as garbage.
|
|
let cap: f64 = 9000000000000000000i64: f64;
|
|
if (f >= cap) {
|
|
let s: str = "huge";
|
|
let k: i32 = 0;
|
|
for (k < s.len) { buf[out] = s[k]; out += 1; k += 1; };
|
|
return out;
|
|
};
|
|
let ip: i64 = f: i64;
|
|
// Fractional part scaled to 6 decimal digits, with round-to-
|
|
// nearest via +0.5. (f64 compound assigns mis-lower in cgen —
|
|
// use the explicit form, as the rest of lib does.)
|
|
let frac: f64 = f - (ip: f64);
|
|
let scale: f64 = 1000000: f64;
|
|
frac = frac * scale;
|
|
let half: f64 = (1: f64) / (2: f64);
|
|
let fp: i64 = (frac + half): i64;
|
|
// Carry: e.g. 0.9999996 rounds fp up to 1000000 and the integer
|
|
// part needs to advance.
|
|
if (fp >= 1000000) {
|
|
ip += 1;
|
|
fp = 0;
|
|
};
|
|
let itmp: [32]u8;
|
|
let in: i32 = i64tos(itmp[0:32], ip);
|
|
let k: i32 = 0;
|
|
for (k < in) { buf[out] = itmp[k]; out += 1; k += 1; };
|
|
if (fp == 0) { return out; };
|
|
buf[out] = 46u8; // '.'
|
|
out += 1;
|
|
let ftmp: [16]u8;
|
|
let m: i32 = u64tos(ftmp[0:16], fp: u64);
|
|
// Pad fractional to 6 digits with leading zeros (e.g. 0.05 →
|
|
// fp=50000, m=5, pad one '0' before "50000").
|
|
let z: i32 = 6 - m;
|
|
for (z > 0) { buf[out] = 48u8; out += 1; z -= 1; };
|
|
k = 0;
|
|
for (k < m) { buf[out] = ftmp[k]; out += 1; k += 1; };
|
|
// Trim trailing zeros in the fractional part (we know fp != 0,
|
|
// so the loop stops before erasing the dot).
|
|
for (out > 0) {
|
|
if (buf[out - 1] != 48u8) { break; };
|
|
out -= 1;
|
|
};
|
|
return out;
|
|
};
|
|
|
|
// MODULE: ascii
|
|
// ascii — rune-class predicates and case folding for the ASCII range.
|
|
// Matches Hare's ascii::isdigit family (rune-taking signature). Runes
|
|
// outside 0..127 always answer `false`. The lexer hot path uses these
|
|
// inline; they are expected to inline to a couple of compares.
|
|
|
|
export fn isdigit(c: rune) bool = {
|
|
if (c < 48) { return false; };
|
|
if (c > 57) { return false; };
|
|
return true;
|
|
};
|
|
|
|
export fn isupper(c: rune) bool = {
|
|
if (c < 65) { return false; };
|
|
if (c > 90) { return false; };
|
|
return true;
|
|
};
|
|
|
|
export fn islower(c: rune) bool = {
|
|
if (c < 97) { return false; };
|
|
if (c > 122) { return false; };
|
|
return true;
|
|
};
|
|
|
|
export fn isalpha(c: rune) bool = {
|
|
if (isupper(c)) { return true; };
|
|
return islower(c);
|
|
};
|
|
|
|
export fn isalnum(c: rune) bool = {
|
|
if (isalpha(c)) { return true; };
|
|
return isdigit(c);
|
|
};
|
|
|
|
// isspace — the C/Hare set: space, tab, NL, VT, FF, CR.
|
|
export fn isspace(c: rune) bool = {
|
|
if (c == 32) { return true; }; // ' '
|
|
if (c == 9) { return true; }; // '\t'
|
|
if (c == 10) { return true; }; // '\n'
|
|
if (c == 11) { return true; }; // '\v'
|
|
if (c == 12) { return true; }; // '\f'
|
|
if (c == 13) { return true; }; // '\r'
|
|
return false;
|
|
};
|
|
|
|
export fn isxdigit(c: rune) bool = {
|
|
if (isdigit(c)) { return true; };
|
|
if (c >= 65) {
|
|
if (c <= 70) { return true; }; // 'A'..'F'
|
|
};
|
|
if (c >= 97) {
|
|
if (c <= 102) { return true; }; // 'a'..'f'
|
|
};
|
|
return false;
|
|
};
|
|
|
|
// digitval — value of `c` as a hex/decimal digit. void variant means
|
|
// `c` isn't a hex digit. Useful when scanning numeric literals.
|
|
export fn digitval(c: rune) (i32 | void) = {
|
|
if (isdigit(c)) { return (c - 48): i32; };
|
|
if (c >= 65) {
|
|
if (c <= 70) { return ((c - 65) + 10): i32; };
|
|
};
|
|
if (c >= 97) {
|
|
if (c <= 102) { return ((c - 97) + 10): i32; };
|
|
};
|
|
return;
|
|
};
|
|
|
|
// isidstart / isidpart — identifier classes used by the lexer.
|
|
// Alpha or '_' starts; alnum or '_' continues.
|
|
export fn isidstart(c: rune) bool = {
|
|
if (isalpha(c)) { return true; };
|
|
if (c == 95) { return true; }; // '_'
|
|
return false;
|
|
};
|
|
|
|
export fn isidpart(c: rune) bool = {
|
|
if (isalnum(c)) { return true; };
|
|
if (c == 95) { return true; };
|
|
return false;
|
|
};
|
|
|
|
// tolower / toupper — fold ASCII case. Non-letters pass through.
|
|
export fn tolower(c: rune) rune = {
|
|
if (isupper(c)) { return c + 32; };
|
|
return c;
|
|
};
|
|
|
|
export fn toupper(c: rune) rune = {
|
|
if (islower(c)) { return c - 32; };
|
|
return c;
|
|
};
|
|
|
|
// MODULE: test
|
|
// selfhost/test/smoke.ww — end-to-end smoke for the selfhost path.
|
|
//
|
|
// Exercises the patterns the real ww-side compiler port will use:
|
|
// - bump arena allocator (mem.ww shape)
|
|
// - error idiom (T | str)
|
|
// - struct of fn pointers + ctx pointer (the io.stream-style
|
|
// polymorphism we use instead of interfaces)
|
|
// - byte-level scanning that mirrors the hot path inside lex.ww
|
|
// - strconv round-trip via the real stdlib
|
|
//
|
|
// `main` returns 42 when every check passes, 1..N on failure
|
|
// 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 w6l, which is a separate
|
|
// task; until then we exercise polymorphism via ctx pointers, which
|
|
// is what the real port wants anyway.
|
|
|
|
use os;
|
|
use strconv;
|
|
use ascii;
|
|
|
|
// --- bump arena ---------------------------------------------------------
|
|
|
|
type arena = struct {
|
|
buf: *u8,
|
|
off: u64,
|
|
cap: u64,
|
|
};
|
|
|
|
// In-place init. Returning a 24-byte struct by value isn't yet
|
|
// 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 = {
|
|
a.buf = buf;
|
|
a.off = 0u64;
|
|
a.cap = cap;
|
|
};
|
|
|
|
fn arena_alloc(a: *arena, n: u64) *u8 = {
|
|
if (n > a.cap - a.off) { return nil; };
|
|
let p: *u8 = a.buf + a.off;
|
|
a.off += n;
|
|
return p;
|
|
};
|
|
|
|
// --- (i32 | str) error idiom -------------------------------------------
|
|
|
|
fn checked_div(num: i32, den: i32) (i32 | str) = {
|
|
if (den == 0) { return "div by zero"; };
|
|
return num / den;
|
|
};
|
|
|
|
// --- struct-of-fn-pointer polymorphism ---------------------------------
|
|
//
|
|
// A trivial "writer" abstraction: a function pointer plus a context.
|
|
// This mirrors how io.stream / Plan 9 Bio work. The ctx pointer lets
|
|
// the implementation own its own state without a global.
|
|
|
|
type counter = struct {
|
|
n: i32,
|
|
};
|
|
|
|
type writer = struct {
|
|
ctx: *void,
|
|
emit: fn(ctx: *void, b: u8) void,
|
|
};
|
|
|
|
fn count_emit(ctx: *void, b: u8) void = {
|
|
let c: *counter = ctx: *counter;
|
|
c.n += 1;
|
|
};
|
|
|
|
// --- byte scanner like lex.ww's hot path -------------------------------
|
|
|
|
fn count_digits(s: str) i32 = {
|
|
let i: i32 = 0;
|
|
let n: i32 = 0;
|
|
for (i < s.len) {
|
|
let c: u8 = s[i];
|
|
if (c >= 48u8) {
|
|
if (c <= 57u8) { n += 1; };
|
|
};
|
|
i += 1;
|
|
};
|
|
return n;
|
|
};
|
|
|
|
// --- entry --------------------------------------------------------------
|
|
|
|
export fn main() i32 = {
|
|
// Probe 1 — arena hands out distinct pointers, refuses oversize.
|
|
let buf: [256]u8;
|
|
let a: arena;
|
|
arena_init(&a, buf.ptr, 256u64);
|
|
let p1: *u8 = arena_alloc(&a, 32u64);
|
|
let p2: *u8 = arena_alloc(&a, 32u64);
|
|
if (p1 == nil) { return 1; };
|
|
if (p2 == nil) { return 2; };
|
|
if (p1 == p2) { return 3; };
|
|
let p3: *u8 = arena_alloc(&a, 1024u64);
|
|
if (p3 != nil) { return 4; };
|
|
|
|
// Probe 2 — error union both ways.
|
|
let r_ok: (i32 | str) = checked_div(84, 2);
|
|
let r_bad: (i32 | str) = checked_div(1, 0);
|
|
let acc: i32 = 0;
|
|
match (r_ok) {
|
|
case let v: i32 => acc = v;
|
|
case let e: str => return 5;
|
|
};
|
|
if (acc != 42) { return 6; };
|
|
match (r_bad) {
|
|
case let v: i32 => return 7;
|
|
case let e: str => acc = e.len: i32;
|
|
};
|
|
if (acc != 11) { return 8; }; // len("div by zero") == 11
|
|
|
|
// Probe 3 — struct-of-fn-pointer dispatch via ctx pointer.
|
|
let c: counter = counter { n = 0 };
|
|
let w: writer = writer { ctx = (&c): *void, emit = count_emit };
|
|
w.emit(w.ctx, 65u8);
|
|
w.emit(w.ctx, 66u8);
|
|
w.emit(w.ctx, 67u8);
|
|
if (c.n != 3) { return 9; };
|
|
|
|
// Probe 4 — byte scan over a literal.
|
|
let dn: i32 = count_digits("ww123abc");
|
|
if (dn != 3) { return 10; };
|
|
|
|
// Probe 5 — strconv round-trip via the real stdlib.
|
|
let outbuf: [32]u8;
|
|
let nb: i32 = strconv.i64tos(outbuf[0:32], 4242i64);
|
|
if (nb != 4) { return 11; };
|
|
if (outbuf[0] != 52u8) { return 12; }; // '4'
|
|
if (outbuf[3] != 50u8) { return 13; }; // '2'
|
|
|
|
// Probe 6 — ascii classifications (rune-taking, Hare-shaped).
|
|
if (!ascii.isdigit(53)) { return 14; }; // '5'
|
|
if (ascii.isdigit(65)) { return 15; }; // 'A' is not a digit
|
|
if (!ascii.isalpha(122)) { return 16; }; // 'z'
|
|
if (!ascii.isidstart(95)) { return 17; }; // '_'
|
|
if (!ascii.isidpart(48)) { return 18; }; // '0' is part
|
|
let dv: (i32 | void) = ascii.digitval(70);
|
|
match (dv) {
|
|
case let v: i32 => { if (v != 15) { return 19; }; }; // 'F' = 15
|
|
case void => { return 19; };
|
|
};
|
|
if (ascii.tolower(65) != 97) { return 20; }; // 'A' -> 'a'
|
|
|
|
// Probe 7 — file open/read via the new os APIs. /proc/self/cmdline
|
|
// always exists on Linux, no write side, and is non-empty.
|
|
let path: str = "/proc/self/cmdline";
|
|
// Use raw os.open here (returns i32 with -errno) for the same
|
|
// reason as os.read below: probe 6 in 990_selfhost compiles
|
|
// smoke.ww standalone (no `use` expansion), so cross-module type
|
|
// references like `os.oserror` and `os.flag` don't resolve at
|
|
// that step. RDONLY is 0; passing the literal keeps the call
|
|
// site standalone-compilable to byte-identical asm on both
|
|
// compilers.
|
|
let fd: i32 = os.open(path.ptr, 0, 0i32);
|
|
if (fd < 0) { return 21; };
|
|
let rbuf: [128]u8;
|
|
// Use raw os.read here (single syscall, plain i64) instead of
|
|
// os.readall: the 990 cgen-match probe compiles smoke.ww
|
|
// standalone without `use os;` expansion, so cross-module type
|
|
// references like `os.oserror` can't be resolved.
|
|
let n: i64 = os.read(fd, rbuf.ptr, 128u64);
|
|
os.close(fd);
|
|
if (n <= 0i64) { return 22; };
|
|
|
|
return 42;
|
|
};
|
|
|