diff --git a/Makefile b/Makefile index 718e557c..8b289f96 100644 --- a/Makefile +++ b/Makefile @@ -311,6 +311,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_cast_enum_movl \ $(BIN)/test_check_enum_fold \ $(BIN)/test_def_widen_const \ + $(BIN)/test_inferred_struct_arg_push \ $(BIN)/test_use_promote_alias \ $(BIN)/test_field_signed $(BIN)/test_frame_argcount \ $(BIN)/test_selfhost $(BIN)/test_w6a_ww $(BIN)/test_w6l_ww \ @@ -584,6 +585,12 @@ $(BIN)/test_def_widen_const: test/wcc/760_def_widen_const.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_inferred_struct_arg_push: test/wcc/761_inferred_struct_arg_push.c \ + $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ + $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_arrlit_str_full: test/wcc/711_arrlit_str_full.c \ $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ diff --git a/test/wcc/761_inferred_struct_arg_push.c b/test/wcc/761_inferred_struct_arg_push.c new file mode 100644 index 00000000..d294d22b --- /dev/null +++ b/test/wcc/761_inferred_struct_arg_push.c @@ -0,0 +1,355 @@ +/* + * 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 +#include +#include +#include +#include +#include + +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 src[64], tmpdir[64], cmd[1024]; + snprintf(src, sizeof src, "/tmp/wcisp_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/wcisp_%d_d_%d", getpid(), i); + + FILE *f = fopen(src, "wb"); + if (!f) return -1; + fputs(r->src, f); + fclose(f); + + mkdir(tmpdir, 0755); + /* 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, "cd %s && timeout 180 %s build %s 2>/dev/null", + tmpdir, driver, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: build via %s failed\n", + r->label, driver); + unlink(src); rmdir(tmpdir); + return -1; + } + + const char *base = strrchr(src, '/'); + base = base ? base + 1 : src; + char outbin[128]; + 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); rmdir(tmpdir); + 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; +}