wcc_ww/cgen: #63 alias-named struct-lit fill via structlookupchain (let-init + sret-return)

cglet's N_STRUCTLIT init arm resolved the struct by a bare
structlookup(c, sname). For an alias-NAMED literal
(`type rep2 = rep; let r = rep2{id=6}`) the type ref carries the
alias name "rep2" but only the base `rep` is registered, so the
lookup returned nil and the field-fill never fired. The nil then
split by slot size into two symptoms of one root:
  - <=8B: the small-let scalar default zeroed the slot and DROPPED
    the literal (SILENT wrong — the field read 0), and
  - >8B: no fill arm matched, falling to the cglet "unhandled rhs
    shape" LOUD (task #7/rule-7).

Route the arm through structlookupchain (the #92/W2 SSoT already
adopted at cgenstmt:1974/:2687), which chases the alias chain to the
base struct. trefn (rhs.lhs) is already the N_IDENT/N_TNAME type ref
structlookupchain accepts, so the bare sname extraction is dropped.
cstage operates on the resolved Type* via type_chase_named and was
always correct: ww-only align-UP, cs UNTOUCHED.

ROUTED (the two reachable silent sites, one class):
  :2421  local N_STRUCTLIT let-init — the #63 repro hits it for
         both the <=8B silent-zero and the >24B loud symptoms.
  :1250  >24B sret RETURN twin (reviewer-63). sretretsize chases
         the alias for the size GATE so this sret arm fires, but
         the fill used the same bare structlookup(sname) — for an
         alias-named >24B literal it returned nil and the fill was
         SKIPPED, so the callee returned an uninitialised sret
         buffer (SILENT wrong, runtime-0; cs correct). Same root,
         same symptom, sibling site → folded by construction.
DECLINED (traced, not blind-routed; rule-11 + the #101 precedent):
  :2625  N_IDENT struct-copy — also bare-structlookup but the copy
         falls through to a generic path byte-identical with cstage;
         both stages run correct. The post-copy field-READ diverges
         (cs direct-offset vs ww LEAQ-indirect) = the #81/#65 alias
         field-read class, out of #63 scope.
  :2511  N_CALL struct-recv — blocked UPSTREAM by the aggregate-
         return shape (#272/#277); ww louds at the sender.
  :1363  <=24B register RETURN — alias case louds via the same
         scalar-default catch (#277), not silently wrong.
The 2 already-chasing sites (1974/2687) untouched.

CONVERGENCE: m3_letinit_typed + m3_letinit_untyped (ww silent-zero ->
6/6 byte-id) + m6_letlit_alias (ww loud -> 7 byte-id) + sret_return_-
alias32 (ww silent-0 -> 10 byte-id), plus a non-alias control
(no-regress). Bootstrap byte-id NEUTRAL (selfhost has no
alias-struct-litinit/return; all 4 selfhost tools cs==ww confirmed).

Test: 944_alias_structlit_init_run (5 rows x cs-run + ww-run +
cs==ww byte-id = 15 checks), Makefile-wired.
This commit is contained in:
2026-06-06 10:36:16 +09:00
parent 45f5415209
commit 3546673756
5 changed files with 329 additions and 42 deletions

View File

@@ -0,0 +1,253 @@
/*
* 944_alias_structlit_init_run — #63: let-init from an alias-NAMED
* struct LITERAL. wwstage's cglet N_STRUCTLIT arm resolved the struct
* by a BARE structlookup(c, sname): for an alias `type rep2 = rep`
* only the base `rep` is registered, so the lookup returned nil and
* the field-fill never fired. The nil then split by slot size —
* • <=8B: the small-let scalar default zeroed the slot and DROPPED
* the literal (SILENT wrong: `let r = rep2{id=6}` read 0), and
* • >8B: no fill arm matched -> the cglet "unhandled rhs shape"
* LOUD (task #7/rule-7).
* Same root, two symptoms. The fix routes the arm through
* structlookupchain (the #92/W2 SSoT already used at cgenstmt:1974 /
* :2687), which chases the alias chain to the base struct. cstage
* operates on the resolved Type* via type_chase_named and was always
* correct, so this is ww-only align-UP; cs UNTOUCHED. Oracle: ken
* .ai/ken-63-oracle.md.
*
* The N_IDENT-copy (cgenstmt:2625) and N_CALL-recv (:2511) sibling
* arms are also bare-structlookup but are NOT in scope: the copy
* falls through to a generic path byte-identical with cstage (both
* stages run correct), and the recv is blocked upstream by the
* aggregate-return shape (#272/#277), and the ≤24B return (:1363)
* likewise louds via the scalar-default catch. The >24B sret RETURN
* (:1250) had NO such catch — it was silently wrong (returned an
* uninitialised sret buffer); reviewer-63 routed it through
* structlookupchain in the same commit (see sret_return_alias32).
* The N_IDENT struct-copy (:2625) falls through to a generic path
* both stages run correct (asm divergence is the #81/#65 alias
* field-READ class). See the #63 ledger.
*
* row | shape | want
* -------------------+---------------------------------------+-----
* letinit_typed | type rep2=rep(8B); let r: rep2 = |
* | rep2{id=6}; read r.id (was silent 0) | 6
* letinit_untyped | inferred: let r = rep2{id=6}; r.id | 6
* letlit_alias16 | type row=st(16B); let a: row = |
* | row{a=3,b=4}; read a.a+a.b (was LOUD) | 7
* control_direct | non-alias pt2{...} 16B let-init — the |
* | no-regress control (alias hop is the |
* | trigger), byte-id both stages | 7
* sret_return_alias32| type biga=big(32B); mk() returns |
* | biga{...}; the >24B sret RETURN twin |
* | (:1250, was silent 0) | 10
*
* Every row asserts cstage/wwstage asm byte-id (rule 10). NNN<950,
* self-contained (/tmp, no imports) — rule-14's selfhost-sibling race
* does not apply (941/944 precedent).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
static int
runwait(const char *cmd)
{
int rc = system(cmd);
if (rc == -1) return -1;
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
return -1;
}
static int
slurp_eq(const char *a, const char *b)
{
FILE *fa = fopen(a, "rb");
FILE *fb = fopen(b, "rb");
if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; }
int rc = 0;
for (;;) {
int ca = fgetc(fa), cb = fgetc(fb);
if (ca != cb) { rc = -1; break; }
if (ca == EOF) break;
}
fclose(fa); fclose(fb);
return rc;
}
struct row { const char *label; const char *src; int want; };
static const struct row rows[] = {
{ "letinit_typed",
"package main;\n"
"type rep = struct { id: size };\n"
"type rep2 = rep;\n"
"export fn main() i32 = {\n"
" let r: rep2 = rep2{id=6};\n"
" return r.id: i32;\n"
"};\n", 6 },
{ "letinit_untyped",
"package main;\n"
"type rep = struct { id: size };\n"
"type rep2 = rep;\n"
"export fn main() i32 = {\n"
" let r = rep2{id=6};\n"
" return r.id: i32;\n"
"};\n", 6 },
{ "letlit_alias16",
"package main;\n"
"type st = struct { a: size, b: size };\n"
"type row = st;\n"
"export fn main() i32 = {\n"
" let a: row = row{a=3, b=4};\n"
" return (a.a + a.b): i32;\n"
"};\n", 7 },
{ "control_direct",
"package main;\n"
"type pt2 = struct { a: size, b: size };\n"
"export fn main() i32 = {\n"
" let x: pt2 = pt2{a=3, b=4};\n"
" return (x.a + x.b): i32;\n"
"};\n", 7 },
/* reviewer-63 survivor leg: the >24B sret-RETURN twin of the
* let-init site (cgenstmt:1250). sretretsize chases the alias for
* the size gate so the sret arm fires, but the field-fill used a
* bare structlookup(sname) that missed the alias name -> the fill
* was SKIPPED and the callee returned an uninitialised sret buffer
* (ww silent 0; cs correct 10). The impl ledger wrongly declined
* :1250 as "already chases"; routed through structlookupchain. */
{ "sret_return_alias32",
"package main;\n"
"type big = struct { a: size, b: size, c: size, d: size };\n"
"type biga = big;\n"
"fn mk() biga = { return biga{a=1, b=2, c=3, d=4}; };\n"
"export fn main() i32 = {\n"
" let r: biga = mk();\n"
" return (r.a + r.b + r.c + r.d): i32;\n"
"};\n", 10 },
};
/* build+run via a driver (ww / ww_ww); returns 0 pass, nonzero fail. */
static int
run_driver(const char *driver, const struct row *r, int i)
{
char src[96], tmpdir[96], errf[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/asli_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/asli_%d_d_%d", getpid(), i);
snprintf(errf, sizeof errf, "/tmp/asli_%d_e_%d", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd,
"cd %s && timeout 20 %s build %s >/dev/null 2>%s",
tmpdir, driver, src, errf);
int brc = runwait(cmd);
if (brc != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
unlink(src); unlink(errf); rmdir(tmpdir);
return -1;
}
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
char outbin[256];
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
int got = runwait(outbin);
unlink(src); unlink(outbin); unlink(errf); rmdir(tmpdir);
if (got != r->want) {
fprintf(stderr, "row[%s]: %s exit %d, want %d\n",
r->label, driver, got, r->want);
return 1;
}
return 0;
}
/* cs==ww .s byte-id (rule 10). */
static int
asm_byte_identical(const char *bin, const struct row *r, int i)
{
char src[96], cs[96], ws[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/asli_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/asli_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/asli_asm_%d_%d_w.s", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c errored\n", r->label);
unlink(src);
return -1;
}
snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null",
bin, ws, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c_ww errored\n", r->label);
unlink(src); unlink(cs);
return -1;
}
int rc = slurp_eq(cs, ws);
if (rc != 0)
fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n",
r->label);
unlink(src); unlink(cs); unlink(ws);
return rc;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[2080];
if (bin[0] != '/') {
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
char cdrv[2120], wdrv[2120];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int i = 0; i < n; i++) {
total++;
if (run_driver(cdrv, &rows[i], i) != 0) fail++;
}
if (access(wdrv, X_OK) == 0) {
for (int i = 0; i < n; i++) {
total++;
if (run_driver(wdrv, &rows[i], i) != 0) fail++;
}
for (int i = 0; i < n; i++) {
total++;
if (asm_byte_identical(bin, &rows[i], i) != 0) fail++;
}
}
if (fail) {
fprintf(stderr, "alias_structlit_init: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("alias_structlit_init: %d/%d ok\n", total, total);
return 0;
}