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

@@ -39,53 +39,46 @@ export fn hassuffix(s: str, suf: str) bool = {
return true;
};
// indexbyte — first byte position of byte `c` in `s`. Mirrors
// Hare's strings::byteindex when the needle is a single ASCII rune,
// renamed to match bytes.indexbyte and to disambiguate from Hare's
// `byteindex(haystack, needle: (str | rune))` which we don't have
// the union-arg ABI for yet.
export fn indexbyte(s: str, c: u8) (i32 | void) = {
let i: i32 = 0;
for (i < s.len) {
if (s[i] == c) { return i; };
i += 1;
};
return;
};
// rindexbyte — last byte position of byte `c` in `s`.
export fn rindexbyte(s: str, c: u8) (i32 | void) = {
let i: i32 = s.len - 1;
for (i >= 0) {
if (s[i] == c) { return i; };
i -= 1;
};
return;
};
// index — first index of `sub` in `s`. Naive scan; fine for short
// patterns and small strings, which dominate config and CLI parsing.
// Empty `sub` matches at 0.
export fn index(s: str, sub: str) (i32 | void) = {
if (sub.len == 0) { return 0; };
if (sub.len > s.len) { return; };
let last: i32 = s.len - sub.len;
let i: i32 = 0;
for (i <= last) {
let j: i32 = 0;
let ok: bool = true;
for (j < sub.len) {
if (s[i + j] != sub[j]) { ok = false; j = sub.len; }
else { j += 1; };
// byteindex — first byte position of `needle` in `s`. Mirrors Hare's
// strings::byteindex: a single-codepoint rune scans for the byte that
// encodes it (ASCII only here — multi-byte UTF-8 awaits utf8 encode),
// a str needle scans for the substring. Returns void if absent.
export fn byteindex(s: str, needle: (str | rune)) (i32 | void) = {
match (needle) {
case let r: rune => {
let c: u8 = r: u8;
let i: i32 = 0;
for (i < s.len) {
if (s[i] == c) { return i; };
i += 1;
};
if (ok) { return i; };
i += 1;
return;
};
case let sub: str => {
if (sub.len == 0) { return 0; };
if (sub.len > s.len) { return; };
let last: i32 = s.len - sub.len;
let i: i32 = 0;
for (i <= last) {
let j: i32 = 0;
let ok: bool = true;
for (j < sub.len) {
if (s[i + j] != sub[j]) { ok = false; j = sub.len; }
else { j += 1; };
};
if (ok) { return i; };
i += 1;
};
return;
};
};
return;
};
// contains — true iff `sub` appears in `s`. Mirrors Hare's
// strings::contains shape (byte-wise on the str-needle case).
export fn contains(s: str, sub: str) bool = {
let r: (i32 | void) = index(s, sub);
let r: (i32 | void) = byteindex(s, sub);
match (r) {
case let i: i32 => return true;
case void => return false;
@@ -129,21 +122,37 @@ export fn dup(s: str) str = {
return r;
};
// rindex — last index of `sub` in `s`. Mirrors Hare's strings::rindex
// (slice case). Empty `sub` matches at s.len.
export fn rindex(s: str, sub: str) (i32 | void) = {
if (sub.len == 0) { return s.len; };
if (sub.len > s.len) { return; };
let i: i32 = s.len - sub.len;
for (i >= 0) {
let j: i32 = 0;
let ok: bool = true;
for (j < sub.len) {
if (s[i + j] != sub[j]) { ok = false; j = sub.len; }
else { j += 1; };
// rbyteindex — last byte position of `needle` in `s`. Mirrors Hare's
// strings::rbyteindex. Rune needle scans for the byte that encodes it
// (ASCII only); str needle scans for the substring. Empty str needle
// matches at s.len.
export fn rbyteindex(s: str, needle: (str | rune)) (i32 | void) = {
match (needle) {
case let r: rune => {
let c: u8 = r: u8;
let i: i32 = s.len - 1;
for (i >= 0) {
if (s[i] == c) { return i; };
i -= 1;
};
if (ok) { return i; };
i -= 1;
return;
};
case let sub: str => {
if (sub.len == 0) { return s.len; };
if (sub.len > s.len) { return; };
let i: i32 = s.len - sub.len;
for (i >= 0) {
let j: i32 = 0;
let ok: bool = true;
for (j < sub.len) {
if (s[i + j] != sub[j]) { ok = false; j = sub.len; }
else { j += 1; };
};
if (ok) { return i; };
i -= 1;
};
return;
};
};
return;
};