wcc: TYPE-based forward-tagged predicate in wwstage cgreturn (#201)

cgreturn's forwardtagged detection was keyed on the CALLEE NAME
(N_IDENT/N_DOT only via fnretlookupmod), so any other callee shape
fell through to the variant-tag synthesis path — clobbering the
just-returned AX/DX/CX/R8 tagged-ABI words. The deref-call case
`(*r)(...)` (impl-e1-resume STOP, 994 w6c_ww byte-id red) was the
proximate trigger.

Replace with a TYPE-BASED predicate over the checker-stamped tinfos
(rhs.type_ vs c.fnret.type_), mirroring cstage cgen.c:8007 passthrough.
Peel TY_NAMED on both sides then identity-check the underlying
TY_TAGGED — sufficient for the NAMED case because tinfocache memoizes
per typedecl (#191 lineage). Variant-pointer fallback walks the
params chain when identity fails so anonymous unions like the
cross-module (i32 | void) shared between strings.byteindex and
bytes.index still forward correctly; full recursive tinfo
structural-eq is gated by #178 (typeeqast's TY_TAGGED arm
conservatively returns false today).

Probe 770_return_tagged_forward covers 6 rows — IDENT forward, widen
non-matching, deref-call (the bug), scalar (sanity), nested call,
cross-module forward — each gated on cstage runtime + wwstage runtime
+ cs.s == ww.s byte-identity.
This commit is contained in:
2026-05-29 01:00:02 +09:00
parent b2ac8cbf81
commit fb53b4798b
5 changed files with 477 additions and 72 deletions

View File

@@ -0,0 +1,368 @@
/*
* 770_return_tagged_forward — project #201: wwstage cgreturn forwarding
* of a matching tagged-union call result. cstage cgen.c:8007-8014
* detects passthrough via Type-based `istagged && (vu == rt ||
* type_eq(vt, cg_ret_type))`, so any callee shape (IDENT, DOT, deref-
* call) that returns the same tagged type triggers a direct AX/DX/CX/R8
* forward. Wwstage's cgreturn was instead detecting forwardtagged by
* walking the CALLEE NAME — only N_IDENT and N_DOT callees were
* inspected; an N_UN(TK_STAR) deref-call fell through to the variant-
* tag synthesis path, which clobbered AX→DX and zeroed CX/R8/AX before
* RET, wiping the just-returned tagged-ABI words.
*
* Trigger (impl-e1-resume STOP / 994_w6c_ww byte-id red):
* fn st_read(s: vstream, ...) (size | io.eof | io.error) =
* { return (*r)(s, buf); };
*
* Fix: selfhost/cmd/wcc/cgenstmt.ww cgreturn — replace the IDENT/DOT
* name-keyed lookup with a TYPE-BASED predicate on the checker-stamped
* tinfo: peel TY_NAMED from both rhs.type_ and c.fnret.type_, and
* forward when the peeled rhs tinfo is TY_TAGGED and pointer-identical
* to the peeled fnret tinfo. Identity is sufficient because
* tinfofornode memoizes per typedecl (check.ww:1566-1568 "every TNAME
* resolving to the same decl yields the SAME tinfo pointer"); the
* structural fallback in cstage's type_eq is gated by #178 (no tinfo-
* level structural-eq helper today, and typeeqast's TY_TAGGED arm
* conservatively returns false).
*
* Coverage (6 rows):
* 1. forward_named_match — IDENT callee, both fns return the same
* NAMED tagged alias. Pre-fix: forwarded
* via the old name-keyed path. Post-fix:
* same forward via the new TYPE path.
* Asm regression gate.
* 2. widen_subset — IDENT callee returns a concrete variant
* (i32), fnret is the tagged union. Must
* KEEP the widen-shuffle (AX→DX, zero
* CX/R8/AX). Asserts the new predicate
* correctly rejects non-matching shapes.
* 3. deref_call_match — `(*r)()` callee, both fns return the
* same NAMED tagged alias. THE BUG row.
* Pre-fix wwstage: spurious tag-synth
* shuffle after CALL AX. Post-fix:
* direct CALL AX + RET.
* 4. scalar_return — fnret is i32 (not tagged); cgreturn
* skips the istaggedtype gate entirely.
* Sanity that the fix doesn't perturb
* the unrelated scalar path.
* 5. nested_call_forward — outer call wraps inner scalar call,
* both fns return the same NAMED tagged
* alias. Forward fires on the outer
* N_CALL despite the nested arg.
* 6. cross_module_forward — caller is in `main`, callee+type live
* in an imported module. drew-add: gates
* cross-module TY_NAMED identity (every
* reference to `mtag770.result` resolves
* to the SAME NAMED tinfo via aliassym +
* sym.type_ cache; #191 lineage). If a
* regressor splits NAMED tinfo per
* reference site, this row reds first.
*
* Per-row gates: cstage runtime exit, wwstage runtime exit, cs.s ==
* ww.s byte-identical (rule-10 stage symmetry).
*
* GATE POLARITY: must stay GREEN. A red on row 3/5/6 means the
* TYPE-based forward predicate dropped a callee shape; row 2 reds if
* the predicate fires too aggressively (non-matching rhs incorrectly
* forwarded); row 1/4 red means structural regression in adjacent
* cgreturn arms.
*/
#include <stdio.h>
#include <stdlib.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;
}
#define STAGE_CS 1
#define STAGE_WW 2
struct row {
const char *label;
const char *src;
/* Optional secondary-module source (row 6 cross_module). */
const char *modname;
const char *modsrc;
int expected_exit;
int stage_mask;
};
static const struct row rows[] = {
{ "forward_named_match",
"package main;\n"
"type result = (i32 | str);\n"
"fn ra(x: i32) result = { return x + 1: i32; };\n"
"fn cw(x: i32) result = { return ra(x); };\n"
"export fn main() i32 = {\n"
" let r: result = cw(40);\n"
" match (r) {\n"
" case let i: i32 => return i;\n"
" case let s: str => return 0;\n"
" };\n"
"};\n",
NULL, NULL,
41,
STAGE_CS | STAGE_WW },
{ "widen_subset",
"package main;\n"
"type result = (i32 | str);\n"
"fn ri(x: i32) i32 = { return x + 1; };\n"
"fn cw(x: i32) result = { return ri(x); };\n"
"export fn main() i32 = {\n"
" let r: result = cw(40);\n"
" match (r) {\n"
" case let i: i32 => return i;\n"
" case let s: str => return 0;\n"
" };\n"
"};\n",
NULL, NULL,
41,
STAGE_CS | STAGE_WW },
{ "deref_call_match",
"package main;\n"
"type result = (i32 | str);\n"
"fn ra(x: i32) result = { return x + 1: i32; };\n"
"fn cw(r: *fn(x: i32) result, x: i32) result = { return (*r)(x); };\n"
"export fn main() i32 = {\n"
" let p: *fn(x: i32) result = &ra;\n"
" let v: result = cw(p, 40);\n"
" match (v) {\n"
" case let i: i32 => return i;\n"
" case let s: str => return 0;\n"
" };\n"
"};\n",
NULL, NULL,
41,
STAGE_CS | STAGE_WW },
{ "scalar_return",
"package main;\n"
"fn ri(x: i32) i32 = { return x + 1; };\n"
"fn cw(x: i32) i32 = { return ri(x); };\n"
"export fn main() i32 = { return cw(40); };\n",
NULL, NULL,
41,
STAGE_CS | STAGE_WW },
{ "nested_call_forward",
"package main;\n"
"type result = (i32 | str);\n"
"fn ri(x: i32) i32 = { return x + 1; };\n"
"fn wrap(x: i32) result = { return x: i32; };\n"
"fn cw(x: i32) result = { return wrap(ri(x)); };\n"
"export fn main() i32 = {\n"
" let r: result = cw(39);\n"
" match (r) {\n"
" case let i: i32 => return i;\n"
" case let s: str => return 0;\n"
" };\n"
"};\n",
NULL, NULL,
40,
STAGE_CS | STAGE_WW },
{ "cross_module_forward",
"package main;\n"
"import mtag770;\n"
"fn cw(x: i32) mtag770.result = { return mtag770.ra(x); };\n"
"export fn main() i32 = {\n"
" let r: mtag770.result = cw(40);\n"
" match (r) {\n"
" case let i: i32 => return i;\n"
" case let s: str => return 0;\n"
" };\n"
"};\n",
"mtag770",
"package mtag770;\n"
"export type result = (i32 | str);\n"
"export fn ra(x: i32) result = { return x + 1: i32; };\n",
41,
STAGE_CS | STAGE_WW },
};
static int
write_source(const char *path, const char *src)
{
FILE *f = fopen(path, "wb");
if (!f) return -1;
fputs(src, f);
fclose(f);
return 0;
}
static int
write_sources(const struct row *r, const char *tmpdir, const char *src)
{
if (r->modname != NULL) {
char moddir[256], modfile[512];
snprintf(moddir, sizeof moddir, "%s/%s", tmpdir, r->modname);
snprintf(modfile, sizeof modfile, "%s/%s.ww",
moddir, r->modname);
mkdir(moddir, 0755);
if (write_source(modfile, r->modsrc) != 0) return -1;
}
return write_source(src, r->src);
}
static void
cleanup_tmp(const struct row *r, const char *tmpdir, const char *base)
{
char p[512];
snprintf(p, sizeof p, "%s/%s.ww", tmpdir, base); unlink(p);
snprintf(p, sizeof p, "%s/%s.s", tmpdir, base); unlink(p);
snprintf(p, sizeof p, "%s/%s.o", tmpdir, base); unlink(p);
snprintf(p, sizeof p, "%s/%s.combined.ww", tmpdir, base); unlink(p);
snprintf(p, sizeof p, "%s/%s", tmpdir, base); unlink(p);
if (r->modname != NULL) {
char moddir[256];
snprintf(moddir, sizeof moddir, "%s/%s", tmpdir, r->modname);
snprintf(p, sizeof p, "%s/%s.ww", moddir, r->modname); unlink(p);
snprintf(p, sizeof p, "%s/%s.s", moddir, r->modname); unlink(p);
snprintf(p, sizeof p, "%s/%s.o", moddir, r->modname); unlink(p);
snprintf(p, sizeof p, "%s/%s.combined.ww", moddir, r->modname); unlink(p);
rmdir(moddir);
}
rmdir(tmpdir);
}
static int
build_via_driver(const char *driver, const char *tmpdir, const char *src)
{
char cmd[1024];
snprintf(cmd, sizeof cmd, "cd %s && timeout 180 %s build %s 2>/dev/null",
tmpdir, driver, src);
return runwait(cmd);
}
static int
run_row(const char *driver, const struct row *r, int seq)
{
char tmpdir[256], src[512], base[64], outbin[768];
snprintf(tmpdir, sizeof tmpdir, "/tmp/rtf_%d_d_%d", getpid(), seq);
snprintf(src, sizeof src, "%s/main770.ww", tmpdir);
snprintf(base, sizeof base, "main770");
mkdir(tmpdir, 0755);
if (write_sources(r, tmpdir, src) != 0) {
cleanup_tmp(r, tmpdir, base);
return -1;
}
int rc = -1;
if (build_via_driver(driver, tmpdir, src) == 0) {
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
rc = runwait(outbin);
}
cleanup_tmp(r, tmpdir, base);
return rc;
}
/* asm_byte_identical — diff cstage vs wwstage .s. Parallel trees so
* ww_ww writing intermediates next to the source doesn't clobber the
* cstage .s (CLAUDE.md rule 14 phase split). */
static int
asm_byte_identical(const char *cdrv, const char *wdrv,
const struct row *r, int seq)
{
char src[512], tdc[256], tdw[256], base[64], cs[512], ws[512];
snprintf(tdc, sizeof tdc, "/tmp/rtf_%d_c_%d", getpid(), seq);
snprintf(tdw, sizeof tdw, "/tmp/rtf_%d_w_%d", getpid(), seq);
snprintf(base, sizeof base, "main770");
mkdir(tdc, 0755);
mkdir(tdw, 0755);
snprintf(src, sizeof src, "%s/main770.ww", tdc);
if (write_sources(r, tdc, src) != 0) { cleanup_tmp(r, tdc, base); cleanup_tmp(r, tdw, base); return -1; }
int rc = -1;
if (build_via_driver(cdrv, tdc, src) != 0) goto out;
snprintf(cs, sizeof cs, "%s/%s.s", tdc, base);
snprintf(src, sizeof src, "%s/main770.ww", tdw);
if (write_sources(r, tdw, src) != 0) goto out;
if (build_via_driver(wdrv, tdw, src) != 0) goto out;
snprintf(ws, sizeof ws, "%s/%s.s", tdw, base);
FILE *fc = fopen(cs, "rb");
FILE *fw = fopen(ws, "rb");
if (fc && fw) {
rc = 0;
for (;;) {
int a = fgetc(fc);
int b = fgetc(fw);
if (a != b) { rc = -1; break; }
if (a == EOF) break;
}
}
if (fc) fclose(fc);
if (fw) fclose(fw);
out:
cleanup_tmp(r, tdc, base);
cleanup_tmp(r, tdw, base);
return rc;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[512];
if (bin[0] != '/') {
char cwd[256];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
char cdrv[640], wdrv[640];
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;
int wwpresent = (access(wdrv, X_OK) == 0);
int seq = 0;
for (int i = 0; i < n; i++) {
if (rows[i].stage_mask & STAGE_CS) {
total++;
int got = run_row(cdrv, &rows[i], seq++);
if (got != rows[i].expected_exit) {
fprintf(stderr,
"return_tagged_forward[cstage run][%s]: exit=%d want=%d\n",
rows[i].label, got, rows[i].expected_exit);
fail++;
}
}
if (wwpresent && (rows[i].stage_mask & STAGE_WW)) {
total++;
int got = run_row(wdrv, &rows[i], seq++);
if (got != rows[i].expected_exit) {
fprintf(stderr,
"return_tagged_forward[wwstage run][%s]: exit=%d want=%d\n",
rows[i].label, got, rows[i].expected_exit);
fail++;
}
total++;
if (asm_byte_identical(cdrv, wdrv, &rows[i], seq++) != 0) {
fprintf(stderr,
"return_tagged_forward[byte-id][%s]: cstage vs wwstage asm differs\n",
rows[i].label);
fail++;
}
}
}
if (!wwpresent)
fprintf(stderr, "return_tagged_forward: skip wwstage (no %s)\n", wdrv);
if (fail) {
fprintf(stderr, "return_tagged_forward: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("return_tagged_forward: %d/%d ok\n", total, total);
return 0;
}