w6c+selfhost+lib: cgen quality batch + lib Hare-shape graduation

Six fixes across the toolchain, surfaced by lib/lisp porting work.

  1. f64 compound assigns (`acc += d`, `-=`, `*=`, `/=`). Both stages
     load slot → X1, OP X0 into X1, store back (ADDSD/SUBSD/MULSD/
     DIVSD are reg-reg only). Previous MOVSD-overwrite dropped the
     OP. Locals and top-level lets.

  2. Top-level `[N]u8` arrays + `&arr[i]`. let_emit_size grows a
     TY_ARRAY branch so zero-init DATAW lands; cgindex / N_INDEX
     store / `&base[i]` all detect a global array base and use
     LEAQ name(SB) instead of LEAQ (BP). TK_AMP no longer pre-
     evaluates the operand as a value-load — `&base[i]` computes
     base + i*esz directly. Unblocks Hare's static-buffer pattern:
     strconv.{u64,i64,f64}tos graduate to module-level `*_buf`
     arrays and return owned views.

  3. Cross-module `pkg.Enum.MEMBER`. Nested N_DOT chains that
     don't fold to a known shape now emit `MOVQ <leaf>(SB), AX`
     (mirrors the bare-IDENT unresolved fallback), so isolation
     probes — and the test 990 cgen-match floor — stay consistent
     across stages. strconv exposes `base` as a real `enum i32`;
     callers updated. The `main` exemption (linker entry-point
     keeps bare name even when not exported) mirrors C-side
     collectmods into selfhost cgendecl.

  4. Sum-typed parameter ABI. lib/bytes.{index,rindex} take
     `(u8 | []u8)` needle; lib/strings.byteindex / rbyteindex take
     `(str | rune)` needle (Hare-shaped; the byte-wise misnomer
     `index` is dropped). tagged_arg_size cap bumps to 48 (6 int
     regs), with a new partial-fit branch on the callee: when an
     N-word tagged arg overflows remaining regs, fill what fits and
     stitch the rest from positive BP offsets. scanlocals MCASE
     handles slice binds (24B) and walks each arm with a saved /
     restored seenmark set so two arms naming the same local each
     get their own slot — matches cstage's per-arm scope reset.

  5. 4-reg tagged-return ABI (AX=tag, DX=word0, CX=word1, R8=word2),
     up from 3 regs. Slice-payload variants (`([]T | E)`, slot 32B)
     round-trip ptr/len/cap end-to-end. Every receive site updates:
     let-init via cgwidentaggedstore, match scrutinee spill, cgindex
     tagged-element load (both N_IDENT and fallback bases),
     pushargsrev tagged-ident arg (reads word count from slot size),
     cgreturn slice variant in the shuffle path.

  6. `expr: TaggedAlias` is a widening, not a re-interpret. C cgen +
     selfhost cgwidentaggedstore peel an N_CAST whose destination IS
     the union — so cgexpr's natural shape (str: AX=ptr, BX=len;
     slice: AX=ptr, BX=len, CX=cap) is consumed by the matching
     concrete-variant branch instead of being misread as a tagged
     AX/DX/CX triple. Inner casts to a concrete variant (`7: i32`)
     keep their type for proper tag lookup. `[N]Alias` arrays
     resolve element size via slotsize + aliaslookup, and aliaslookup
     strips a `pkg.` prefix so cross-module references work.

lib/fmt grows `formattable = (i64 | str | bool | rune)` plus
`printv` / `printlnv` taking an explicit `[]formattable` slice (the
receive side of Hare's `args: formattable...`). Call-site variadic
gather isn't wired — callers either hand-build the slice or compose
strconv.i64tos + strings.concat.

700_e2e: 114 → 123 rows (f64 compound, top-level u8 arrays + `&buf[i]`,
pkg.Enum.MEMBER, sum-typed (str|rune) and (u8|[]u8) params, 4-reg
slice-return ABI, formattable array). 26/26 tests, bootstrap stable
through ww4.
This commit is contained in:
2026-05-13 08:05:01 +09:00
parent 6fd0160c0f
commit 46edb8db4a
23 changed files with 2665 additions and 915 deletions

View File

@@ -1,11 +1,10 @@
// strconv — number↔string conversions.
//
// Mirrors Hare's strconv:: surface. The *tos functions return a fresh
// owned `str`; release via os.free(r.ptr, r.len: u64) when done.
// Hare returns `const str` into a static buffer; ww allocates per
// call because the wwstage cgen doesn't currently support mutating a
// module-level `*u8` (so a lazy-init shared buffer isn't expressible
// today). Graduate to the static-buffer shape once that lands.
// Mirrors Hare's strconv:: surface. The *tos functions return a
// `const str` view into a module-level buffer that is overwritten on
// the next call to the same function; callers must copy the bytes if
// they need to outlive the next invocation. See [[strings.dup]] to
// duplicate. Matches Hare's strconv::*tos semantics.
use os;
use strings;
@@ -22,43 +21,44 @@ export type overflow = !void;
// error — any error from a strconv call. Mirrors Hare's strconv::error.
export type error = !(invalid | overflow);
// base — numeric base for parsing/formatting. Plain i32 (not a named
// enum) because cross-module `strconv.base.DEC` chains miscompile in
// the cstage cgen — it emits a memory load through `base(SB)` rather
// than inlining the enum value. Hare names them as `strconv::base`
// enum values; we expose them as module-level `def`s so callers say
// `strconv.DEC` and the cgen inlines the immediate.
// base — numeric base for parsing/formatting. Mirrors Hare's
// `strconv::base` (Hare uses `enum uint`; we pick `enum i32` since
// the underlying parse/format loops index with i32).
//
// HEX is HEX_UPPER; HEX_LOWER is a separate pseudo-base that produces
// lowercase a-f digits.
export def DEFAULT: i32 = 0;
export def BIN: i32 = 2;
export def OCT: i32 = 8;
export def DEC: i32 = 10;
export def HEX_UPPER: i32 = 16;
export def HEX: i32 = 16;
export def HEX_LOWER: i32 = 17;
// HEX is an alias for HEX_UPPER; HEX_LOWER is a pseudo-base that
// produces lowercase a-f digits.
export type base = enum i32 {
DEFAULT = 0,
BIN = 2,
OCT = 8,
DEC = 10,
HEX_UPPER = 16,
HEX = 16,
HEX_LOWER = 17,
};
fn basenum(b: i32) i64 = {
if (b == BIN) { return 2; };
if (b == OCT) { return 8; };
if (b == HEX) { return 16; };
if (b == HEX_UPPER) { return 16; };
if (b == HEX_LOWER) { return 16; };
fn basenum(b: base) i64 = {
if (b == base.BIN) { return 2; };
if (b == base.OCT) { return 8; };
if (b == base.HEX) { return 16; };
if (b == base.HEX_UPPER) { return 16; };
if (b == base.HEX_LOWER) { return 16; };
return 10; // DEC and DEFAULT
};
fn basedigit(d: i64, b: i32) u8 = {
fn basedigit(d: i64, b: base) u8 = {
if (d < 10) { return (d + 48): u8; };
let off: i64 = d - 10;
if (b == HEX_LOWER) { return (off + 97): u8; };
if (b == base.HEX_LOWER) { return (off + 97): u8; };
return (off + 65): u8;
};
// u64tos — convert v to a base-b numeric string. Returns owned str;
// release via os.free(r.ptr, r.len: u64). Mirrors Hare's
// strconv::u64tos (Hare returns const str into a static buffer).
export fn u64tos(v: u64, b: i32) str = {
// u64tos — convert v to a base-b numeric string. Returns a view into
// `u64tos_buf` which is overwritten on the next call. Matches Hare's
// strconv::u64tos.
let u64tos_buf: [65]u8;
export fn u64tos(v: u64, b: base) str = {
let nb: u64 = basenum(b): u64;
let tmp: [65]u8;
let i: i32 = 0;
@@ -70,22 +70,25 @@ export fn u64tos(v: u64, b: i32) str = {
n = n / nb;
i += 1;
};
let buf: *u8 = os.alloc(i: u64): *u8;
let out: i32 = 0;
for (i > 0) {
i -= 1;
buf[out] = tmp[i];
u64tos_buf[out] = tmp[i];
out += 1;
};
let r: str;
r.ptr = buf;
r.ptr = &u64tos_buf[0];
r.len = out;
return r;
};
// i64tos — convert v to a base-b numeric string. Returns owned str;
// release via os.free. Mirrors Hare's strconv::i64tos.
export fn i64tos(v: i64, b: i32) str = {
// i64tos — convert v to a base-b numeric string. Returns a view into
// `i64tos_buf` which is overwritten on the next call. Independent
// buffer from u64tos so i64tos's own call to u64tos doesn't clobber
// the in-flight result. Matches Hare's strconv::i64tos.
let i64tos_buf: [66]u8;
export fn i64tos(v: i64, b: base) str = {
let neg: bool = false;
let n: i64 = v;
if (n < 0) { neg = true; n = -n; };
@@ -99,37 +102,33 @@ export fn i64tos(v: i64, b: i32) str = {
n = n / nb;
i += 1;
};
let extra: i32 = 0;
if (neg) { extra = 1; };
let total: i32 = i + extra;
let buf: *u8 = os.alloc(total: u64): *u8;
let out: i32 = 0;
if (neg) { buf[out] = 45u8; out += 1; }; // '-'
if (neg) { i64tos_buf[out] = 45u8; out += 1; }; // '-'
for (i > 0) {
i -= 1;
buf[out] = tmp[i];
i64tos_buf[out] = tmp[i];
out += 1;
};
let r: str;
r.ptr = buf;
r.ptr = &i64tos_buf[0];
r.len = out;
return r;
};
export fn i32tos(v: i32, b: i32) str = { return i64tos(v: i64, b); };
export fn i16tos(v: i16, b: i32) str = { return i64tos(v: i64, b); };
export fn i8tos(v: i8, b: i32) str = { return i64tos(v: i64, b); };
export fn i32tos(v: i32, b: base) str = { return i64tos(v: i64, b); };
export fn i16tos(v: i16, b: base) str = { return i64tos(v: i64, b); };
export fn i8tos(v: i8, b: base) str = { return i64tos(v: i64, b); };
export fn u32tos(v: u32, b: i32) str = { return u64tos(v: u64, b); };
export fn u16tos(v: u16, b: i32) str = { return u64tos(v: u64, b); };
export fn u8tos(v: u8, b: i32) str = { return u64tos(v: u64, b); };
export fn u32tos(v: u32, b: base) str = { return u64tos(v: u64, b); };
export fn u16tos(v: u16, b: base) str = { return u64tos(v: u64, b); };
export fn u8tos(v: u8, b: base) str = { return u64tos(v: u64, b); };
// digval — value of digit byte `c` under base `b`, or -1 if not a
// valid digit. Letters are accepted case-insensitively under HEX /
// HEX_UPPER; only lowercase under HEX_LOWER.
fn digval(c: u8, b: i32) i32 = {
fn digval(c: u8, b: base) i32 = {
if (c >= 48u8) { if (c <= 57u8) { return (c - 48u8): i32; }; };
if (b == HEX_LOWER) {
if (b == base.HEX_LOWER) {
if (c >= 97u8) { if (c <= 102u8) { return ((c - 97u8) + 10u8): i32; }; };
return -1;
};
@@ -142,7 +141,7 @@ fn digval(c: u8, b: i32) i32 = {
// No locale, no whitespace, no underscores: optional leading '-' then
// digits. Returns invalid with the offending index or overflow on
// out-of-range.
export fn stoi64(s: str, b: i32) (i64 | invalid | overflow) = {
export fn stoi64(s: str, b: base) (i64 | invalid | overflow) = {
if (s.len == 0) { return 0: invalid; };
let i: i32 = 0;
let neg: bool = false;
@@ -163,7 +162,7 @@ export fn stoi64(s: str, b: i32) (i64 | invalid | overflow) = {
};
// stou64 — parse unsigned base-b number. Mirrors Hare's strconv::stou64.
export fn stou64(s: str, b: i32) (u64 | invalid | overflow) = {
export fn stou64(s: str, b: base) (u64 | invalid | overflow) = {
if (s.len == 0) { return 0: invalid; };
let nb: u64 = basenum(b): u64;
let v: u64 = 0u64;
@@ -179,7 +178,7 @@ export fn stou64(s: str, b: i32) (u64 | invalid | overflow) = {
return v;
};
export fn stoi32(s: str, b: i32) (i32 | invalid | overflow) = {
export fn stoi32(s: str, b: base) (i32 | invalid | overflow) = {
let r = stoi64(s, b);
match (r) {
case let v: i64 => {
@@ -193,7 +192,7 @@ export fn stoi32(s: str, b: i32) (i32 | invalid | overflow) = {
return 0: invalid; // unreachable; appeases the path-cov checker
};
export fn stoi16(s: str, b: i32) (i16 | invalid | overflow) = {
export fn stoi16(s: str, b: base) (i16 | invalid | overflow) = {
let r = stoi64(s, b);
match (r) {
case let v: i64 => {
@@ -207,7 +206,7 @@ export fn stoi16(s: str, b: i32) (i16 | invalid | overflow) = {
return 0: invalid;
};
export fn stoi8(s: str, b: i32) (i8 | invalid | overflow) = {
export fn stoi8(s: str, b: base) (i8 | invalid | overflow) = {
let r = stoi64(s, b);
match (r) {
case let v: i64 => {
@@ -221,7 +220,7 @@ export fn stoi8(s: str, b: i32) (i8 | invalid | overflow) = {
return 0: invalid;
};
export fn stou32(s: str, b: i32) (u32 | invalid | overflow) = {
export fn stou32(s: str, b: base) (u32 | invalid | overflow) = {
let r = stou64(s, b);
match (r) {
case let v: u64 => {
@@ -234,7 +233,7 @@ export fn stou32(s: str, b: i32) (u32 | invalid | overflow) = {
return 0: invalid;
};
export fn stou16(s: str, b: i32) (u16 | invalid | overflow) = {
export fn stou16(s: str, b: base) (u16 | invalid | overflow) = {
let r = stou64(s, b);
match (r) {
case let v: u64 => {
@@ -247,7 +246,7 @@ export fn stou16(s: str, b: i32) (u16 | invalid | overflow) = {
return 0: invalid;
};
export fn stou8(s: str, b: i32) (u8 | invalid | overflow) = {
export fn stou8(s: str, b: base) (u8 | invalid | overflow) = {
let r = stou64(s, b);
match (r) {
case let v: u64 => {
@@ -283,13 +282,14 @@ export fn stou8(s: str, b: i32) (u8 | invalid | overflow) = {
// 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.
let f64tos_buf: [64]u8;
export fn f64tos(v: f64) str = {
let tmp: [64]u8;
let out: i32 = 0;
let f: f64 = v;
let zero: f64 = 0: f64;
if (f < zero) {
tmp[out] = 45u8; // '-'
f64tos_buf[out] = 45u8; // '-'
out += 1;
f = -f;
};
@@ -299,12 +299,9 @@ export fn f64tos(v: f64) str = {
if (f >= cap) {
let s: str = "huge";
let k: i32 = 0;
for (k < s.len) { tmp[out] = s[k]; out += 1; k += 1; };
let buf: *u8 = os.alloc(out: u64): *u8;
let q: i32 = 0;
for (q < out) { buf[q] = tmp[q]; q += 1; };
for (k < s.len) { f64tos_buf[out] = s[k]; out += 1; k += 1; };
let r: str;
r.ptr = buf;
r.ptr = &f64tos_buf[0];
r.len = out;
return r;
};
@@ -323,32 +320,27 @@ export fn f64tos(v: f64) str = {
ip += 1;
fp = 0;
};
let intstr: str = i64tos(ip, DEC);
let intstr: str = i64tos(ip, base.DEC);
let k: i32 = 0;
for (k < intstr.len) { tmp[out] = intstr.ptr[k]; out += 1; k += 1; };
os.free(intstr.ptr: *void, intstr.len: u64);
for (k < intstr.len) { f64tos_buf[out] = intstr.ptr[k]; out += 1; k += 1; };
if (fp != 0) {
tmp[out] = 46u8; // '.'
f64tos_buf[out] = 46u8; // '.'
out += 1;
let fracstr: str = u64tos(fp: u64, DEC);
let fracstr: str = u64tos(fp: u64, base.DEC);
// Pad fractional to 6 digits with leading zeros (e.g. 0.05 →
// fp=50000, fracstr="50000", pad one '0' before).
let z: i32 = 6 - fracstr.len;
for (z > 0) { tmp[out] = 48u8; out += 1; z -= 1; };
for (z > 0) { f64tos_buf[out] = 48u8; out += 1; z -= 1; };
k = 0;
for (k < fracstr.len) { tmp[out] = fracstr.ptr[k]; out += 1; k += 1; };
os.free(fracstr.ptr: *void, fracstr.len: u64);
for (k < fracstr.len) { f64tos_buf[out] = fracstr.ptr[k]; out += 1; k += 1; };
// Trim trailing zeros in the fractional part.
for (out > 0) {
if (tmp[out - 1] != 48u8) { break; };
if (f64tos_buf[out - 1] != 48u8) { break; };
out -= 1;
};
};
let buf: *u8 = os.alloc(out: u64): *u8;
let q: i32 = 0;
for (q < out) { buf[q] = tmp[q]; q += 1; };
let r: str;
r.ptr = buf;
r.ptr = &f64tos_buf[0];
r.len = out;
return r;
};