w6c: return of a module-global struct ident copies the global's bytes

cstage zeroed the return scratch; mirror the landed wwstage emission
(copy from the g(SB) base). Flips the F8-era residual row to
cs==ww==correct. Task #42.
This commit is contained in:
2026-06-12 22:34:31 +09:00
parent 5ff5f4b4fe
commit 960b1e796d
2 changed files with 25 additions and 25 deletions

View File

@@ -13675,12 +13675,21 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame)
scr + idx * esz)); scr + idx * esz));
idx++; idx++;
} }
} else if (n->lhs->kind == N_IDENT) { } else if (n->lhs->kind == N_IDENT
&& localfind(*locals, n->lhs->str) != 0) {
/* N_IDENT: word-copy rhs slot into /* N_IDENT: word-copy rhs slot into
* scratch. Whole 8B words via MOVQ; * scratch. Whole 8B words via MOVQ;
* trailing partial word via MOVL/MOVB * trailing partial word via MOVL/MOVB
* so we read no further than the * so we read no further than the
* source slot's declared size. */ * source slot's declared size.
*
* #42 (#263): only a LOCAL ident has a
* BP slot. A module-global struct ident
* (localfind==0) falls to the addr-copy
* else below — aggarg_srcaddr lands
* g(SB) in SI. Pre-fix this read frame
* garbage off 0(BP). ww half landed
* F8-c6. */
int rhsoff = localfind(*locals, int rhsoff = localfind(*locals,
n->lhs->str); n->lhs->str);
int k = 0; int k = 0;

View File

@@ -3,28 +3,19 @@
* GLOBAL struct ident by value (`return g;`) must copy g's bytes into the * GLOBAL struct ident by value (`return g;`) must copy g's bytes into the
* return buffer. * return buffer.
* *
* THE BUG (cat-A silent miscompile, #263 BOTH-WRONG): cgenstmt.ww cgreturn's * THE BUG (cat-A silent miscompile, #263 BOTH-WRONG, now CLOSED both stages):
* ≤24B register-struct return path resolves an N_IDENT source via * the ≤24B register-struct return path word-copied an N_IDENT source from its
* localfindnode and word-copies from its BP slot only `if (rl != nil)`. A * BP slot only for a LOCAL ident. A module-global struct ident hit no branch
* module-global struct ident (rl==nil) hit no branch — after the @retscr * (wwstage zero-filled the @retscr; cstage copied frame garbage ((BP)/8(BP)),
* zero-fill it copied NOTHING, so the callee returned a zeroed struct and g * never g(SB)). The ww half landed in F8-c6; the cstage half (route a global
* was never referenced. cstage is ALSO wrong: it copies frame garbage * ident through the aggarg_srcaddr memcpy — LEAQ g(SB),SI) landed in ww-core
* ((BP)/8(BP)), never g(SB). Both wrong (#263); the cstage half is filed as * TASK #42. Both stages now copy g's bytes and the .s is byte-identical.
* task #42.
* *
* THE WWSTAGE FIX (ww-runtime-correct): a global N_IDENT source is marked * Rows assert the field-equality check returns 0 on both stages (cs==ww).
* addrsrc, routing it through the existing memcpy path — aggargsrcaddr emits * row | shape (return g; then check) | exit
* LEAQ g(SB),SI and the loop copies rsz bytes into @retscr. Locals keep the * -----------+-------------------------------------------+-----
* BP word-copy (byte-id preserved). cstage stays wrong → cs≠ww residual. * ret_16 | point{x=3,y=4} (rsz 16); x==3 && y==4 | 0
* * ret_24 | triple{a=5,b=6,c=7} (rsz 24); all three | 0
* Rows assert ww runtime-correct (the field-equality check returns 0) AND
* pin cstage's deterministic residual: garbage fields never satisfy the
* equality, so the cstage binary takes the else and returns 1. (#42 closes
* the cs side → these become cs==ww.)
* row | shape (return g; then check) | cs | ww
* -----------+-------------------------------------------+----+----
* ret_16 | point{x=3,y=4} (rsz 16); x==3 && y==4 | 1 | 0
* ret_24 | triple{a=5,b=6,c=7} (rsz 24); all three | 1 | 0
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -60,7 +51,7 @@ static const struct row rows[] = {
" if (p.x == 3 && p.y == 4) { return 0; };\n" " if (p.x == 3 && p.y == 4) { return 0; };\n"
" return 1;\n" " return 1;\n"
"};\n", "};\n",
1, 0 }, 0, 0 },
{ "ret_24", { "ret_24",
"package main;\n" "package main;\n"
@@ -72,7 +63,7 @@ static const struct row rows[] = {
" if (p.a == 5 && p.b == 6 && p.c == 7) { return 0; };\n" " if (p.a == 5 && p.b == 6 && p.c == 7) { return 0; };\n"
" return 1;\n" " return 1;\n"
"};\n", "};\n",
1, 0 }, 0, 0 },
}; };
/* run_build — build+run `src` via `driver`; returns the binary's exit /* run_build — build+run `src` via `driver`; returns the binary's exit