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

@@ -144,7 +144,7 @@ static const struct row rows[] = {
{ "use os;\n"
"use strconv;\n"
"fn main() i32 = {\n"
" let s: str = strconv.i64tos(12345, strconv.DEC);\n"
" let s: str = strconv.i64tos(12345, strconv.base.DEC);\n"
" os.write(1, s.ptr, s.len: u64);\n"
" os.write(1, \"\\n\".ptr, 1u64);\n"
" return s.len;\n"
@@ -199,8 +199,8 @@ static const struct row rows[] = {
"use strconv;\n"
"fn main() i32 = {\n"
" fmt.println(\"ww\");\n"
" fmt.println(strconv.i64tos(42, strconv.DEC));\n"
" fmt.println(strconv.i64tos(-7, strconv.DEC));\n"
" fmt.println(strconv.i64tos(42, strconv.base.DEC));\n"
" fmt.println(strconv.i64tos(-7, strconv.base.DEC));\n"
" return 0;\n"
"};", 0 },
/* struct with i32 fields: MOVL/MOVSXD avoids clobbering neighbors */
@@ -1178,9 +1178,9 @@ static const struct row rows[] = {
{ "use strconv;\n"
"type r_t = (i64 | strconv.invalid | strconv.overflow);\n"
"fn main() i32 = {\n"
" let r1: r_t = strconv.stoi64(\"42\", strconv.DEC);\n"
" let r2: r_t = strconv.stoi64(\"-7\", strconv.DEC);\n"
" let r3: r_t = strconv.stoi64(\"abc\", strconv.DEC);\n"
" let r1: r_t = strconv.stoi64(\"42\", strconv.base.DEC);\n"
" let r2: r_t = strconv.stoi64(\"-7\", strconv.base.DEC);\n"
" let r3: r_t = strconv.stoi64(\"abc\", strconv.base.DEC);\n"
" let acc: i32 = 0;\n"
" match (r1) {\n"
" case let v: i64 => acc += v: i32;\n"
@@ -1204,8 +1204,8 @@ static const struct row rows[] = {
{ "use strconv;\n"
"type r_t = (u64 | strconv.invalid | strconv.overflow);\n"
"fn main() i32 = {\n"
" let r1: r_t = strconv.stou64(\"123\", strconv.DEC);\n"
" let r2: r_t = strconv.stou64(\"-1\", strconv.DEC);\n"
" let r1: r_t = strconv.stou64(\"123\", strconv.base.DEC);\n"
" let r2: r_t = strconv.stou64(\"-1\", strconv.base.DEC);\n"
" let acc: i32 = 0;\n"
" match (r1) {\n"
" case let v: u64 => acc += v: i32;\n"
@@ -1219,7 +1219,7 @@ static const struct row rows[] = {
" };\n"
" return acc;\n"
"};", 123 }, /* 123 + 0 (invalid at index 0 in \"-1\") */
/* strings.indexbyte and strings.index: now (i32 | void). */
/* strings.byteindex with (str | rune) needle: returns (i32 | void). */
{ "use strings;\n"
"fn pick(r: (i32 | void), miss: i32) i32 = {\n"
" match (r) {\n"
@@ -1230,10 +1230,10 @@ static const struct row rows[] = {
"};\n"
"fn main() i32 = {\n"
" let s: str = \"hello, world\";\n"
" let i1: i32 = pick(strings.indexbyte(s, 44u8), -1);\n"
" let i2: i32 = pick(strings.indexbyte(s, 122u8), -1);\n"
" let i3: i32 = pick(strings.index(s, \"world\"), -1);\n"
" let i4: i32 = pick(strings.index(s, \"nope\"), -1);\n"
" let i1: i32 = pick(strings.byteindex(s, ','), -1);\n"
" let i2: i32 = pick(strings.byteindex(s, 'z'), -1);\n"
" let i3: i32 = pick(strings.byteindex(s, \"world\"), -1);\n"
" let i4: i32 = pick(strings.byteindex(s, \"nope\"), -1);\n"
" return i1 + i2 + i3 + i4;\n"
"};", 10 }, /* 5 + (-1) + 7 + (-1) */
/* bytes.index: substring search over []u8, (i32 | void). */
@@ -1419,6 +1419,145 @@ static const struct row rows[] = {
"// MODULE: main\n"
"use pkg;\n"
"fn main() i32 = { return pkg.dir.SOUTH as i32; };", 1 },
/* f64 compound assigns on local: += -= *= /= each modify in place
* (ADDSD/SUBSD/MULSD/DIVSD load-modify-store, not a plain MOVSD that
* would overwrite). 1.5 + 0.5 = 2.0 → 2.0 - 1.0 = 1.0 → 1.0 * 4.0 =
* 4.0 → 4.0 / 2.0 = 2.0 → return 2. */
{ "fn main() i32 = {\n"
" let a: f64 = 1.5;\n"
" a += 0.5;\n"
" a -= 1.0;\n"
" a *= 4.0;\n"
" a /= 2.0;\n"
" return a: i32;\n"
"};", 2 },
/* f64 compound on a top-level global: LEAQ name(SB), then MOVSD
* load → ADDSD → MOVSD store. 10.0 + 5.0 = 15.0 → 15.0 * 2.0 =
* 30.0 → 30.0 - 20.0 = 10.0 → 10.0 / 5.0 = 2.0. */
{ "let G: f64 = 10.0;\n"
"fn main() i32 = {\n"
" G += 5.0;\n"
" G *= 2.0;\n"
" G -= 20.0;\n"
" G /= 5.0;\n"
" return G: i32;\n"
"};", 2 },
/* Top-level `[N]u8` array: zero-init DATAW slot + LEAQ name(SB)
* addressing for index and address-of. `buf[i] = c` narrows to
* MOVB; reading back roundtrips through MOVZBQ. */
{ "let buf: [4]u8;\n"
"fn main() i32 = {\n"
" buf[0] = 7u8;\n"
" buf[1] = 35u8;\n"
" return (buf[0] + buf[1]): i32;\n"
"};", 42 },
/* `&arr[i]` for a top-level array: TK_AMP must compute the
* address, not the value. Then `*p = c` for *u8 stores 1 byte.
* Drives Hare's static-buffer pattern (strconv.*tos). */
{ "let buf: [4]u8;\n"
"fn main() i32 = {\n"
" let p: *u8 = &buf[0];\n"
" *p = 41u8;\n"
" let q: *u8 = &buf[1];\n"
" *q = 1u8;\n"
" return (buf[0] + buf[1]): i32;\n"
"};", 42 },
/* Cross-module enum member access: `pkg.Enum.MEMBER`. Inner
* N_DOT resolves through SK_USE → SK_TYPE; outer N_DOT folds to
* the member's integer literal. Validates the strconv.base.DEC
* shape that the *tos / sto* signatures now use. */
{ "// MODULE: pkg\n"
"export type base = enum i32 { DEC = 10, HEX = 16 };\n"
"// MODULE: main\n"
"use pkg;\n"
"fn pick(b: pkg.base) i32 = { return b as i32; };\n"
"fn main() i32 = {\n"
" let a: i32 = pick(pkg.base.DEC);\n"
" let b: i32 = pick(pkg.base.HEX);\n"
" return a + b + 16;\n"
"};", 42 },
/* Sum-typed parameter (str | rune): match-dispatch on a tagged
* union arg widened from a concrete variant at the call site.
* Mirrors lib/strings.byteindex's needle parameter. */
{ "fn pick(n: (str | rune)) i32 = {\n"
" match (n) {\n"
" case let s: str => return s.len + 100;\n"
" case let r: rune => return r: i32;\n"
" };\n"
"};\n"
"fn main() i32 = {\n"
" let a: i32 = pick(\"hi\");\n"
" let b: i32 = pick('?');\n"
" return a + b - 123;\n"
"};", 42 }, /* (2+100) + 63 - 123 = 42 */
/* Sum-typed (u8 | []u8): 32B slot exceeds the old 24B cap on
* tagged_arg_size. Param fills 4 reg words; the callee must
* read slot+24 (cap) for the slice variant to round-trip. */
{ "use bytes;\n"
"fn main() i32 = {\n"
" let buf: [4]u8;\n"
" buf[0] = 1u8; buf[1] = 2u8; buf[2] = 3u8; buf[3] = 4u8;\n"
" let needle: [2]u8;\n"
" needle[0] = 3u8; needle[1] = 4u8;\n"
" let r1: (i32 | void) = bytes.index(buf[0:4], 3u8);\n"
" let r2: (i32 | void) = bytes.index(buf[0:4], needle[0:2]);\n"
" let a: i32 = 99;\n"
" let b: i32 = 99;\n"
" match (r1) {\n"
" case let i: i32 => a = i;\n"
" case void => a = -1;\n"
" };\n"
" match (r2) {\n"
" case let i: i32 => b = i;\n"
" case void => b = -1;\n"
" };\n"
" return a * 10 + b + 18;\n" /* 2*10 + 2 + 18 = 40, off-by-2 → 42 */
"};", 40 },
/* Slice-payload tagged return ([]u8 | E), slot 32B. The 4-reg
* return ABI (AX=tag, DX=ptr, CX=len, R8=cap) lets the callee
* forward all 4 words. Before the bump, slice.len was dropped
* because only 3 regs were used. */
{ "type rterr = !str;\n"
"fn build(n: i32) ([]u8 | rterr) = {\n"
" if (n < 0) { return \"bad\": rterr; };\n"
" let buf: [4]u8;\n"
" buf[0] = 10u8; buf[1] = 20u8; buf[2] = 30u8; buf[3] = 40u8;\n"
" return buf[0:n];\n"
"};\n"
"fn main() i32 = {\n"
" let r: ([]u8 | rterr) = build(3);\n"
" match (r) {\n"
" case let xs: []u8 => {\n"
" if (xs.len != 3) { return 100; };\n"
" return (xs[0] + xs[1] + xs[2]): i32;\n"
" };\n"
" case let e: rterr => return -1;\n"
" };\n"
" return 0;\n"
"};", 60 }, /* 10 + 20 + 30 = 60 */
/* `[N]TaggedAlias` array: each element is a 24B tagged slot,
* and `arr[i] = literal: TaggedAlias` widens through the
* cgwidentaggedstore path. Validates the cast-peel for
* `expr: TaggedAlias` (which is a widening, not a re-interpret)
* and the alias-resolving element-size lookup. */
{ "type formattable = (i64 | str | bool);\n"
"fn main() i32 = {\n"
" let args: [3]formattable;\n"
" args[0] = 1i64: formattable;\n"
" args[1] = \"hi\": formattable;\n"
" args[2] = true: formattable;\n"
" let s: i32 = 0;\n"
" let i: i32 = 0;\n"
" for (i < args.len) {\n"
" match (args[i]) {\n"
" case let n: i64 => s += n: i32;\n"
" case let v: str => s += v.len;\n"
" case let b: bool => { if (b) { s += 39; }; };\n"
" };\n"
" i += 1;\n"
" };\n"
" return s;\n"
"};", 42 }, /* 1 + 2 + 39 = 42 */
{ NULL, 0 }
};