Files
ww/test/wcc/761_inferred_struct_arg_push.c
Hojun-Cho ce3a25a0b4 test: contain sepwork scratch per-driver tmpdir, fix /tmp+in-repo leak (#8)
The wcc test drivers ran `ww build <bare-/tmp src>` with no -o, so the
compiler's <stem>.sepwork scratch landed beside the source and was never
cleaned: unbounded /tmp growth (2195 stale dirs observed) that fills tmpfs
and fabricates phantom test failures + silent harness aborts, and for
in-repo fixture builds leaked .sepwork into the tracked tree.

Each leaking build now writes its source + output inside a per-invocation
tmpdir, passes -o <tmpdir>/<stem> so the .sepwork lands inside it, and
rm -rf's the tmpdir on every exit path -- including fopen-fail and the
expected-fail reject builds (scratch is mkdir'd before the build can fail).
`ww run` and explicit-`-o`/byte-id helpers are left as-is; the 990/993
byte-id comparison logic is byte-for-byte unchanged.

Two items filed separately (this commit holds the no-Makefile / no-main.c
rail):
- #13: a stale <src>.s byte-id readback (749) silently no-ops since
  separate-compile emits .s to <ostem>.sepwork/__root.s; documented inline.
- #14: build-system Makefile recipes build selfhost/cmd/*/main.ww with no
  -o and leak main.sepwork in-tree (bounded, gitignored; own commit).

One concern -- sepwork leak hygiene -- across 228 drivers; uniform
transform applied per-file and two-round reviewed. make test: all 402
passed, zero net-new /tmp scratch, zero test-driven in-repo .sepwork.
2026-06-22 23:29:39 +09:00

351 lines
12 KiB
C

/*
* 761_inferred_struct_arg_push — regression lock for project #16, the
* inferred-let-struct-local → call-arg push width. Closed incidentally
* by ea1579a (SK_USE-gated N_DOT call result), 7198937 (asserttyped bail
* rearm), 39f9267 + 66a91c8 (#169b structabisize convergence cascade).
*
* Each closing commit removed one path that could have left the local's
* type-AST unstamped — together they guarantee cglet's `tn = n.lhs ??
* inferletcalltype(...)` and the localadd'd `lc.tnode` always carry a
* resolvable N_TNAME for an inferred struct-typed let. pushargsrev's
* N_IDENT struct arm (selfhost/cmd/wcc/cgenutil.ww:459) then reaches
* structparamsize > 0 and emits the maxalign-rounded 1-or-2 word push,
* byte-id with cstage's `args[i]->type` → struct_arg_size path
* (cmd/w6c/cgen.c:427, :5452).
*
* The 16-shape impl-16 probe sweep (worktree-impl-16) confirmed
* byte-identity across every reasonable trigger. This file consolidates
* the load-bearing rows so the symmetric behavior cannot regress without
* a green-to-red flip on these probes.
*
* WHAT THIS LOCKS
* - cs==ww .s byte-identity for inferred-let + struct-arg-push
* across struct shapes:
* * {i64,i64} — canonical 16B, maxalign 8
* * {u32,i64} — decf32-shape (ken-flagged
* ftos.ww:411 refactor target), 16B with align-8 tail
* * {i64,i32} — narrow-tail (#169 maxalign-8
* tail-padded ABI)
* * {i32,i32,i32} — maxalign 4, slot-padded 16B
* but ABI 12B (cstage struct_arg_size returns 12, both
* stages still emit 2-word push since stsz>8)
* - cs==ww runtime exit codes for the same shapes
* - Inferral paths covered:
* * `let p = make(...);` (direct N_CALL infer)
* * `let q = make(...); let p = q;` (N_IDENT-rhs infer chain)
* * `let p = make_big(...);` for >24B sret (no register-ABI
* push, but exercises let_isarray=false sret-recv → ident
* push path)
* - Branched callee (per #105 lesson — single-return masks
* reg-routing via coincidence): both `make` and the consumer
* branch on a flag so the GP register routing is exercised
* through both arms.
*
* WHAT THIS DOES NOT COVER (separate filed bugs — do NOT bundle)
* - #173: `let p = call_returning_tagged()!` drops CX/word1 on
* receive (wwstage) and elides the CALL entirely (cstage). Live
* miscompile, separate impl.
* - #176: `let x = w.p` where p is a struct field — wwstage emits
* 1-word memcpy (under-copy of nested-struct field).
* - #175: `let p = arr[0]` — both stages buggy in different ways.
* - #177: aliased struct return (`type alias_p = point;
* fn make() alias_p`) — cstage emits 1-word push (OPPOSITE of
* the #16 stated symptom polarity).
* - #174: `let p = call(): T;` cast in rhs — cstage drops the
* call. Both-stages broken.
*
* GATE POLARITY: this file must stay GREEN. A red here means one of
* the closing commits regressed. Bisect against the 4 cited commits.
*/
#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;
}
struct row { const char *label; const char *src; int want; };
static const struct row rows[] = {
/* 1. Canonical {i64,i64} — `let p = make(0);` inferred, then
* `sum(p)` passes p as the struct arg. Branched callee on flag.
* Returns p.a + p.b = 10 + 20 = 30. */
{ "i64_i64_infer_direct",
"type point = struct { a: i64, b: i64 };\n"
"fn make(flag: i32) point = {\n"
" if (flag == 0) { return point { a = 10i64, b = 20i64 }; };\n"
" return point { a = 100i64, b = 200i64 };\n"
"};\n"
"fn sum(p: point, which: i32) i32 = {\n"
" if (which == 0) { return (p.a + p.b): i32; };\n"
" return (p.a - p.b): i32;\n"
"};\n"
"fn main() i32 = {\n"
" let p = make(0);\n"
" return sum(p, 0);\n"
"};\n",
30 },
/* 2. {u32,i64} — decf32-shape. The narrow-mantissa-first then
* widening-exponent layout is the strconv ftos.ww:411 decf32
* (ken-flagged as one-refactor-away). Returns
* (mantissa: i64) + exponent = 7 + 13 = 20. */
{ "u32_i64_decf_infer",
"type decf = struct { mantissa: u32, exponent: i64 };\n"
"fn make(flag: i32) decf = {\n"
" if (flag == 0) { return decf { mantissa = 7u32, exponent = 13i64 }; };\n"
" return decf { mantissa = 99u32, exponent = -1i64 };\n"
"};\n"
"fn use_(d: decf, which: i32) i32 = {\n"
" if (which == 0) { return ((d.mantissa: i64) + d.exponent): i32; };\n"
" return ((d.mantissa: i64) - d.exponent): i32;\n"
"};\n"
"fn main() i32 = {\n"
" let d = make(0);\n"
" return use_(d, 0);\n"
"};\n",
20 },
/* 3. {i64,i32} narrow-tail — natural 12B, maxalign 8, ABI 16B
* via #169 structabisize round-up. Branched callee on flag.
* Returns p.a + (p.b: i64) = 50 + 7 = 57. */
{ "i64_i32_narrow_tail_infer",
"type ntail = struct { a: i64, b: i32 };\n"
"fn make(flag: i32) ntail = {\n"
" if (flag == 0) { return ntail { a = 50i64, b = 7i32 }; };\n"
" return ntail { a = 500i64, b = 70i32 };\n"
"};\n"
"fn use_(p: ntail, which: i32) i32 = {\n"
" if (which == 0) { return (p.a + (p.b: i64)): i32; };\n"
" return (p.a - (p.b: i64)): i32;\n"
"};\n"
"fn main() i32 = {\n"
" let p = make(0);\n"
" return use_(p, 0);\n"
"};\n",
57 },
/* 4. {i32,i32,i32} — maxalign 4, ABI 12B (cstage); wwstage si
* .totsize 16. Both stages still push 2 words since stsz > 8;
* the 2-word push is THE bug #16 watched for. Returns
* p.a + p.b + p.c = 1 + 2 + 3 = 6. */
{ "i32_x3_maxalign4_infer",
"type three = struct { a: i32, b: i32, c: i32 };\n"
"fn make(flag: i32) three = {\n"
" if (flag == 0) { return three { a = 1i32, b = 2i32, c = 3i32 }; };\n"
" return three { a = 10i32, b = 20i32, c = 30i32 };\n"
"};\n"
"fn use_(p: three, which: i32) i32 = {\n"
" if (which == 0) { return p.a + p.b + p.c; };\n"
" return p.a * p.b * p.c;\n"
"};\n"
"fn main() i32 = {\n"
" let p = make(0);\n"
" return use_(p, 0);\n"
"};\n",
6 },
/* 5. Ident-rhs inference chain — `let p = q;` where q itself
* was inferred from a call. Pre-bail-rearm the checker's
* `n.lhs = src` walk had to propagate q's resolved type through
* exprtype(N_IDENT); a stale path would leave p's lc.tnode nil
* and structparamsize → 0 → 1-word push. Returns
* p.a + p.b = 11 + 22 = 33. */
{ "ident_rhs_chain_infer",
"type point = struct { a: i64, b: i64 };\n"
"fn make(flag: i32) point = {\n"
" if (flag == 0) { return point { a = 11i64, b = 22i64 }; };\n"
" return point { a = 110i64, b = 220i64 };\n"
"};\n"
"fn sum(p: point, which: i32) i32 = {\n"
" if (which == 0) { return (p.a + p.b): i32; };\n"
" return (p.a - p.b): i32;\n"
"};\n"
"fn main() i32 = {\n"
" let q = make(0);\n"
" let p = q;\n"
" return sum(p, 0);\n"
"};\n",
33 },
/* 6. >24B sret RECEIVE — exercises the cglet sret-dest wiring
* (selfhost/cmd/wcc/cgenstmt.ww:1231 callsretsize branch). The
* let's own slot is the hidden RDI dest pointer; the callee
* writes through it. The struct is then passed BY POINTER to
* the consumer (NOT by value) — the by-value >24B param ABI is
* orthogonal to #16's call-arg-push scope and has its own
* filed gaps. Returns
* p.a + p.b + p.c + p.d = 1 + 2 + 3 + 4 = 10. */
{ "big_sret_infer_then_ptr_arg",
"type big = struct { a: i64, b: i64, c: i64, d: i64 };\n"
"fn make(flag: i32) big = {\n"
" if (flag == 0) { return big { a = 1i64, b = 2i64, c = 3i64, d = 4i64 }; };\n"
" return big { a = 10i64, b = 20i64, c = 30i64, d = 40i64 };\n"
"};\n"
"fn use_(p: *big, which: i32) i32 = {\n"
" if (which == 0) { return (p.a + p.b + p.c + p.d): i32; };\n"
" return (p.a * p.b * p.c * p.d): i32;\n"
"};\n"
"fn main() i32 = {\n"
" let p = make(0);\n"
" return use_(&p, 0);\n"
"};\n",
10 },
};
static int
run_driver(const char *driver, const struct row *r, int i)
{
char tmpdir[64], src[128], outbin[128], rmcmd[160], cmd[1024];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wcisp_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/wcisp_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/wcisp_%d_%d", tmpdir, getpid(), i);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
FILE *f = fopen(src, "wb");
if (!f) { runwait(rmcmd); return -1; }
fputs(r->src, f);
fclose(f);
/* timeout 180 per repo convention (cf. 760); test/run does not
* bound individual binaries, so an unguarded hang from a future
* cgen regression would stall make test instead of redding here. */
snprintf(cmd, sizeof cmd, "timeout 180 %s build -o %s %s 2>/dev/null",
driver, outbin, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
runwait(rmcmd);
return -1;
}
int got = runwait(outbin);
runwait(rmcmd);
return got;
}
/* asm_byte_identical — diff w6c vs w6c_ww text output. The closing
* commits (ea1579a + 7198937 + 39f9267 + 66a91c8) ensure both stages
* emit the same struct-arg push width for every shape; a red here
* means one of those reverted (#16 surfaces). */
static int
asm_byte_identical(const char *bin, const struct row *r, int i)
{
char src[64], cs[64], ws[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/wcisp_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/wcisp_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/wcisp_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, "timeout 180 %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, "timeout 180 %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;
}
FILE *fc = fopen(cs, "rb");
FILE *fw = fopen(ws, "rb");
int rc = 0;
if (!fc || !fw) {
rc = -1;
} else {
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);
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[1024];
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[1024];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
char wdrv[1024];
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
struct { const char *name; const char *path; int gated_on_existence; }
drivers[] = {
{ "cstage", cdrv, 0 },
{ "wwstage", wdrv, 1 },
{ NULL, NULL, 0 },
};
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int d = 0; drivers[d].name; d++) {
if (drivers[d].gated_on_existence
&& access(drivers[d].path, X_OK) != 0) {
fprintf(stderr, "inferred_struct_arg_push: skip %s (no %s)\n",
drivers[d].name, drivers[d].path);
continue;
}
for (int i = 0; i < n; i++) {
int got = run_driver(drivers[d].path, &rows[i], i);
total++;
if (got != rows[i].want) {
fprintf(stderr,
"inferred_struct_arg_push[%s][%s]: exit=%d want=%d\n",
drivers[d].name, rows[i].label,
got, rows[i].want);
fail++;
}
}
}
if (access(wdrv, X_OK) == 0) {
for (int i = 0; i < n; i++) {
total++;
if (asm_byte_identical(bin, &rows[i], i) != 0)
fail++;
}
}
if (fail) {
fprintf(stderr,
"inferred_struct_arg_push: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("inferred_struct_arg_push: %d/%d ok\n", total, total);
return 0;
}