os: graduate filesize/readall/writeall to (i64 | oserror)

`type oserror = i64` carries -errno (Hare's errors::errno-shaped
named-i64). The three convenience wrappers move off the i64 = -1
sentinel and onto the tagged-union surface.

Callers updated across the selfhost (wwdump, w6c, w6a, w6l, ww
driver). The slurp paths in w6c/w6a/w6l/wwdump now match on the
filesize and readall results; the ELF-emitting writeall sites in
w6a/obj.ww are wrapped through two small local helpers (`wrn` for
"wrote N bytes ok?", `wrdrop` for fire-and-forget) so the existing
11-callsite write loop stays readable.

selfhost/test/smoke.ww kept using raw os.read instead of
os.readall: the 990 cgen-match probe compiles smoke.ww standalone
(no `use` expansion), and cross-module type references like
`os.oserror` can't be resolved in that mode.

Two selfhost-side gaps surfaced and got plugged:
- lib/ww/parse/parse.ww parsetype now collapses dotted type names
  (`pkg.Type` → single N_TNAME with the joined string), mirroring C
  parsetype's dotted-path loop. Local `joindotted` helper because
  there's no arena-based string-concat in the selfhost lib yet.
- selfhost/cmd/wcc/check.ww name-resolver applies the dotted-prefix
  rule from cmd/wcc/check.c's resolve_typename: split at the last
  dot, look up the head as a `use` import, then the leaf as a type.
This commit is contained in:
2026-05-12 02:25:40 +09:00
parent 5c20c40724
commit fd45aedf6c
19 changed files with 533 additions and 181 deletions

View File

@@ -110,25 +110,29 @@ 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 = {
// oserror — the underlying errno from a failed syscall, as a
// negative i64 (Linux's int convention; e.g. -2 = ENOENT). The
// NAMED-i64 alias makes it a distinct variant tag from a "good"
// i64 byte count. Hare's analogue is errors::errno.
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, SEEK_END);
if (end < 0) { return -1i64; };
if (end < 0) { return end: oserror; };
let r: i64 = lseek(fd, 0i64, SEEK_SET);
if (r < 0) { return -1i64; };
if (r < 0) { return r: oserror; };
return end;
};
// readall — keep reading until `n` bytes have arrived or the fd
// closes early. Returns bytes read (0..=n) or -1 on read error.
// 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 = {
// 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 -1i64; };
if (r < 0) { return r: oserror; };
if (r == 0) { return got: i64; }; // short read: caller decides
got += r: u64;
};
@@ -136,13 +140,12 @@ export fn readall(fd: i32, buf: *u8, n: u64) i64 = {
};
// writeall — keep writing until `n` bytes have been accepted or the
// fd refuses progress. Returns bytes written or -1. Hare name
// (io::writeall).
export fn writeall(fd: i32, buf: *u8, n: u64) i64 = {
// 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 -1i64; };
if (r < 0) { return r: oserror; };
if (r == 0) { return sent: i64; };
sent += r: u64;
};
@@ -2954,6 +2957,26 @@ fn expectbindname(p: *parser, into: *str) bool = {
// Other forms (slice, array, struct, fn, chan, tuple, tagged) will
// land in subsequent commits.
// joindotted — arena-build "head.tail" for dotted type-name path
// collapse. Mirrors aprintf in C parser; pulled local to avoid a
// cross-module dependency.
fn joindotted(a: *arena, head: str, tail: str) str = {
let n: u64 = head.len: u64 + 1u64 + tail.len: u64;
let p: *u8 = amalloc(a, n + 1u64): *u8;
let i: u64 = 0u64;
let j: i32 = 0;
for (j < head.len) { p[i] = head[j]; i += 1u64; j += 1; };
p[i] = 46u8; // '.'
i += 1u64;
j = 0;
for (j < tail.len) { p[i] = tail[j]; i += 1u64; j += 1; };
p[i] = 0u8;
let r: str;
r.ptr = p;
r.len = n: i32;
return r;
};
fn parsetype(p: *parser) *node = {
let pf: str = p.curfile;
let pl: i32 = p.curline;
@@ -3025,10 +3048,17 @@ fn parsetype(p: *parser) *node = {
if (p.curkind == TK_IDENT) {
let n: *node = newnode(p.a, N_TNAME, pf, pl, pc);
n.str = p.curtext;
let acc: str = p.curtext;
advance(p);
// Dotted path collapse (pkg.Type) deferred — fixtures don't
// need it yet.
// Dotted path collapse: pkg.Type → single TNAME with the
// joined string. Mirrors C parsetype's loop.
for (p.curkind == TK_DOT) {
advance(p);
if (p.curkind != TK_IDENT) { break; };
acc = joindotted(p.a, acc, p.curtext);
advance(p);
};
n.str = acc;
return n;
};
@@ -3758,6 +3788,28 @@ fn resolvewalk(c: *checker, n: *node) void = {
let nm: str = n.str;
if (nm.len > 0) {
let s: *sym = scopelookup(c.cur, nm);
// `pkg.Type` — strip the last dot prefix and look up
// the leaf if `pkg` is a use-imported name. Mirrors
// cmd/wcc/check.c resolve_typename.
if (s == nil) {
let dot: i32 = nm.len - 1;
for (dot >= 0) {
if (nm[dot] == 46u8) { break; };
dot -= 1;
};
if (dot > 0) {
let head: str;
head.ptr = nm.ptr;
head.len = dot;
let m: *sym = scopelookup(c.cur, head);
if (m != nil) {
let leaf: str;
leaf.ptr = nm.ptr + (dot + 1): u64;
leaf.len = nm.len - (dot + 1);
s = scopelookup(c.cur, leaf);
};
};
};
if (s == nil) {
c.nunresolved += 1;
if (c.verbose != 0) {
@@ -8041,17 +8093,29 @@ export fn main(argc: i32, argv: **u8) i32 = {
};
};
let sz: i64 = os.filesize(fd);
if (sz < 0i64) {
let szr: (i64 | os.oserror) = os.filesize(fd);
let sz: i64 = 0i64;
match (szr) {
case let v: i64 => sz = v;
case let e: os.oserror => {
os.write(2, "wwdump: filesize failed\n".ptr, 24u64);
os.close(fd);
return 1;
};
};
let a: *arena = newarena();
let buf: *u8 = amalloc(a, sz: u64): *u8;
let r: i64 = os.readall(fd, buf, sz: u64);
let rr: (i64 | os.oserror) = os.readall(fd, buf, sz: u64);
os.close(fd);
let r: i64 = 0i64;
match (rr) {
case let v: i64 => r = v;
case let e: os.oserror => {
os.write(2, "wwdump: read failed\n".ptr, 20u64);
return 1;
};
};
if (r != sz) {
os.write(2, "wwdump: short read\n".ptr, 19u64);
return 1;

View File

@@ -86,17 +86,29 @@ export fn main(argc: i32, argv: **u8) i32 = {
};
};
let sz: i64 = os.filesize(fd);
if (sz < 0i64) {
let szr: (i64 | os.oserror) = os.filesize(fd);
let sz: i64 = 0i64;
match (szr) {
case let v: i64 => sz = v;
case let e: os.oserror => {
os.write(2, "wwdump: filesize failed\n".ptr, 24u64);
os.close(fd);
return 1;
};
};
let a: *arena = newarena();
let buf: *u8 = amalloc(a, sz: u64): *u8;
let r: i64 = os.readall(fd, buf, sz: u64);
let rr: (i64 | os.oserror) = os.readall(fd, buf, sz: u64);
os.close(fd);
let r: i64 = 0i64;
match (rr) {
case let v: i64 => r = v;
case let e: os.oserror => {
os.write(2, "wwdump: read failed\n".ptr, 20u64);
return 1;
};
};
if (r != sz) {
os.write(2, "wwdump: short read\n".ptr, 19u64);
return 1;