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:
@@ -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;
|
||||
};
|
||||
@@ -1853,6 +1856,26 @@ use os;
|
||||
use mem;
|
||||
use types;
|
||||
|
||||
// Local wrappers around os.writeall's tagged return — collapse the
|
||||
// (i64 | oserror) back to a boolean / int sentinel for the
|
||||
// length-checked / fire-and-forget write patterns below.
|
||||
fn wrn(fd: i32, p: *u8, n: u64, want: i64) bool = {
|
||||
let r: (i64 | os.oserror) = os.writeall(fd, p, n);
|
||||
match (r) {
|
||||
case let v: i64 => return v == want;
|
||||
case let e: os.oserror => return false;
|
||||
};
|
||||
return false;
|
||||
};
|
||||
|
||||
fn wrdrop(fd: i32, p: *u8, n: u64) void = {
|
||||
let r: (i64 | os.oserror) = os.writeall(fd, p, n);
|
||||
match (r) {
|
||||
case let v: i64 => { };
|
||||
case let e: os.oserror => { };
|
||||
};
|
||||
};
|
||||
|
||||
// ---- ELF constants ----------------------------------------------------
|
||||
def ELFCLASS64: u8 = 2u8;
|
||||
def ELFDATA2LSB: u8 = 1u8;
|
||||
@@ -2043,27 +2066,27 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
||||
wru16(eh.ptr, 60u64, NSECT); // e_shnum
|
||||
wru16(eh.ptr, 62u64, 5u16); // e_shstrndx
|
||||
|
||||
if (os.writeall(fd, eh.ptr, 64u64) != 64i64) { return -1; };
|
||||
if (!wrn(fd, eh.ptr, 64u64, 64i64)) { return -1; };
|
||||
if (a.textlen > 0u64) {
|
||||
if (os.writeall(fd, a.text, a.textlen) != a.textlen: i64) { return -1; };
|
||||
if (!wrn(fd, a.text, a.textlen, a.textlen: i64)) { return -1; };
|
||||
};
|
||||
if (rela.n > 0u64) {
|
||||
if (os.writeall(fd, rela.p, rela.n) != rela.n: i64) { return -1; };
|
||||
if (!wrn(fd, rela.p, rela.n, rela.n: i64)) { return -1; };
|
||||
};
|
||||
if (sym.n > 0u64) {
|
||||
if (os.writeall(fd, sym.p, sym.n) != sym.n: i64) { return -1; };
|
||||
if (!wrn(fd, sym.p, sym.n, sym.n: i64)) { return -1; };
|
||||
};
|
||||
if (str_.n > 0u64) {
|
||||
if (os.writeall(fd, str_.p, str_.n) != str_.n: i64) { return -1; };
|
||||
if (!wrn(fd, str_.p, str_.n, str_.n: i64)) { return -1; };
|
||||
};
|
||||
if (shstr.n > 0u64) {
|
||||
if (os.writeall(fd, shstr.p, shstr.n) != shstr.n: i64) { return -1; };
|
||||
if (!wrn(fd, shstr.p, shstr.n, shstr.n: i64)) { return -1; };
|
||||
};
|
||||
|
||||
// Pad to 8 before shdrs.
|
||||
let written: u64 = EHDR_SZ + a.textlen + rela.n + sym.n + str_.n + shstr.n;
|
||||
for ((written & 7u64) != 0u64) {
|
||||
os.writeall(fd, &zero, 1u64);
|
||||
wrdrop(fd, &zero, 1u64);
|
||||
written += 1u64;
|
||||
};
|
||||
|
||||
@@ -2072,7 +2095,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
||||
// SHT_NULL
|
||||
let sn: i32 = 0;
|
||||
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
||||
os.writeall(fd, shbuf.ptr, 64u64);
|
||||
wrdrop(fd, shbuf.ptr, 64u64);
|
||||
// .text
|
||||
sn = 0;
|
||||
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
||||
@@ -2082,7 +2105,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
||||
wru64(shbuf.ptr, 24u64, offtext);
|
||||
wru64(shbuf.ptr, 32u64, a.textlen);
|
||||
wru64(shbuf.ptr, 48u64, 1u64); // sh_addralign
|
||||
os.writeall(fd, shbuf.ptr, 64u64);
|
||||
wrdrop(fd, shbuf.ptr, 64u64);
|
||||
// .rela.text
|
||||
sn = 0;
|
||||
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
||||
@@ -2095,7 +2118,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
||||
wru32(shbuf.ptr, 44u64, 1u32); // sh_info = .text idx
|
||||
wru64(shbuf.ptr, 48u64, 8u64);
|
||||
wru64(shbuf.ptr, 56u64, RELA_SZ);
|
||||
os.writeall(fd, shbuf.ptr, 64u64);
|
||||
wrdrop(fd, shbuf.ptr, 64u64);
|
||||
// .symtab
|
||||
sn = 0;
|
||||
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
||||
@@ -2107,7 +2130,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
||||
wru32(shbuf.ptr, 44u64, 1u32); // sh_info = one local (STN_UNDEF)
|
||||
wru64(shbuf.ptr, 48u64, 8u64);
|
||||
wru64(shbuf.ptr, 56u64, SYM_SZ);
|
||||
os.writeall(fd, shbuf.ptr, 64u64);
|
||||
wrdrop(fd, shbuf.ptr, 64u64);
|
||||
// .strtab
|
||||
sn = 0;
|
||||
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
||||
@@ -2116,7 +2139,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
||||
wru64(shbuf.ptr, 24u64, offstr);
|
||||
wru64(shbuf.ptr, 32u64, str_.n);
|
||||
wru64(shbuf.ptr, 48u64, 1u64);
|
||||
os.writeall(fd, shbuf.ptr, 64u64);
|
||||
wrdrop(fd, shbuf.ptr, 64u64);
|
||||
// .shstrtab
|
||||
sn = 0;
|
||||
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
||||
@@ -2125,7 +2148,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
||||
wru64(shbuf.ptr, 24u64, offshstr);
|
||||
wru64(shbuf.ptr, 32u64, shstr.n);
|
||||
wru64(shbuf.ptr, 48u64, 1u64);
|
||||
os.writeall(fd, shbuf.ptr, 64u64);
|
||||
wrdrop(fd, shbuf.ptr, 64u64);
|
||||
|
||||
return 0;
|
||||
};
|
||||
@@ -2167,12 +2190,21 @@ fn cstrlen(p: *u8) u64 = {
|
||||
fn slurp(path: *u8) (*u8, u64) = {
|
||||
let fd: i32 = os.open(path, 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 szr: (i64 | os.oserror) = os.filesize(fd);
|
||||
let n: i64 = 0i64;
|
||||
match (szr) {
|
||||
case let v: i64 => n = v;
|
||||
case let e: os.oserror => { os.close(fd); return nil, 0u64; };
|
||||
};
|
||||
let nz: u64 = n: u64;
|
||||
let buf: *u8 = os.alloc(nz + 1u64): *u8;
|
||||
let got: i64 = os.readall(fd, buf, nz);
|
||||
let rr: (i64 | os.oserror) = os.readall(fd, buf, nz);
|
||||
os.close(fd);
|
||||
let got: i64 = 0i64;
|
||||
match (rr) {
|
||||
case let v: i64 => got = v;
|
||||
case let e: os.oserror => return nil, 0u64;
|
||||
};
|
||||
if (got != n) { return nil, 0u64; };
|
||||
buf[nz] = 0u8;
|
||||
return buf, nz;
|
||||
|
||||
Reference in New Issue
Block a user