diff --git a/Makefile b/Makefile index 85469f9b..5901f87c 100644 --- a/Makefile +++ b/Makefile @@ -263,6 +263,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_globstructret_run \ $(BIN)/test_globstructwiden_run \ $(BIN)/test_globtagwiden_run \ + $(BIN)/test_globfloatstructarg_run \ $(BIN)/test_arr_ptr_global \ $(BIN)/test_def_arr_infer_len \ $(BIN)/test_def_arr_len \ @@ -804,6 +805,16 @@ $(BIN)/test_globtagwiden_run: test/wcc/989_globtagwiden_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +# 989_globfloatstructarg_run (F8-c9, #31, the EXCEPTION): a module-global +# float-bearing struct passed by value must drain its float eightbytes into +# the SSE arg regs. align-UP — structfloatclass keyed off the global tnode. +$(BIN)/test_globfloatstructarg_run: test/wcc/989_globfloatstructarg_run.c \ + $(BIN)/ww $(BIN)/ww_ww \ + $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ + $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_let_global: test/wcc/630_let_global.c $(BIN)/ww $(BIN)/w6c \ $(BIN)/w6a $(BIN)/w6l $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 0801f1d4..8279700c 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -30607,7 +30607,17 @@ fn cgcall(c: *cgen, n: *node) void = { let stfc: i32 = 0; if (a.kind == nkind.N_IDENT) { let lc: *local = localfindnode(c, a.str); - if (lc != nil) { stfc = structfloatclass(c, lc.tnode); }; + if (lc != nil) { stfc = structfloatclass(c, lc.tnode); } + else { + // #31 EXCEPTION (align-UP): a module-global float-bearing + // struct arg — lc is nil, so the local-only stfc stayed 0 and + // the drain fell to all-GP (X0 never loaded). Key the float- + // class off the global's declared tnode (letvartnode), the same + // SysV classification the local path uses. cstage keys + // structfloatclass off the operand TYPE, not a local slot. + let gtn: *node = letvartnode(c, a.str); + if (gtn != nil) { stfc = structfloatclass(c, gtn); }; + }; }; if (stfc != 0) { // #165: float-bearing struct arg — drain by SysV diff --git a/selfhost/cmd/wcc/cgenexpr.ww b/selfhost/cmd/wcc/cgenexpr.ww index 3ba94c28..8a8dc8f2 100644 --- a/selfhost/cmd/wcc/cgenexpr.ww +++ b/selfhost/cmd/wcc/cgenexpr.ww @@ -7750,7 +7750,17 @@ fn cgcall(c: *cgen, n: *node) void = { let stfc: i32 = 0; if (a.kind == nkind.N_IDENT) { let lc: *local = localfindnode(c, a.str); - if (lc != nil) { stfc = structfloatclass(c, lc.tnode); }; + if (lc != nil) { stfc = structfloatclass(c, lc.tnode); } + else { + // #31 EXCEPTION (align-UP): a module-global float-bearing + // struct arg — lc is nil, so the local-only stfc stayed 0 and + // the drain fell to all-GP (X0 never loaded). Key the float- + // class off the global's declared tnode (letvartnode), the same + // SysV classification the local path uses. cstage keys + // structfloatclass off the operand TYPE, not a local slot. + let gtn: *node = letvartnode(c, a.str); + if (gtn != nil) { stfc = structfloatclass(c, gtn); }; + }; }; if (stfc != 0) { // #165: float-bearing struct arg — drain by SysV diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 2a0a16d4..98bf1f78 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -30607,7 +30607,17 @@ fn cgcall(c: *cgen, n: *node) void = { let stfc: i32 = 0; if (a.kind == nkind.N_IDENT) { let lc: *local = localfindnode(c, a.str); - if (lc != nil) { stfc = structfloatclass(c, lc.tnode); }; + if (lc != nil) { stfc = structfloatclass(c, lc.tnode); } + else { + // #31 EXCEPTION (align-UP): a module-global float-bearing + // struct arg — lc is nil, so the local-only stfc stayed 0 and + // the drain fell to all-GP (X0 never loaded). Key the float- + // class off the global's declared tnode (letvartnode), the same + // SysV classification the local path uses. cstage keys + // structfloatclass off the operand TYPE, not a local slot. + let gtn: *node = letvartnode(c, a.str); + if (gtn != nil) { stfc = structfloatclass(c, gtn); }; + }; }; if (stfc != 0) { // #165: float-bearing struct arg — drain by SysV diff --git a/test/wcc/989_globfloatstructarg_run.c b/test/wcc/989_globfloatstructarg_run.c new file mode 100644 index 00000000..6bd02de4 --- /dev/null +++ b/test/wcc/989_globfloatstructarg_run.c @@ -0,0 +1,161 @@ +/* + * 989_globfloatstructarg_run — F8-c9 (report-item #31, the EXCEPTION): + * passing a module-GLOBAL float-bearing struct by value must drain its float + * eightbytes into the SSE arg regs (X0..), not all-GP. + * + * THE BUG (cat-A silent miscompile, align-UP): cgcall's per-arg drain decides + * the SysV eightbyte class via structfloatclass, but read it from the local + * slot's tnode ONLY (`if (lc != nil) stfc = structfloatclass(lc.tnode)`). A + * module-global struct arg (lc==nil) kept stfc=0, so the float eightbytes + * drained as integers (POPQ into DI/SI) and X0 was never loaded — the callee + * read its f64 fields from the wrong registers. cstage classifies off the + * operand TYPE, so it loaded X0 and ran correct — the cat-A divergence. + * + * THIS IS THE EXCEPTION among the F8 members: the bug is a float-CLASS + * decision keyed to a local-only slot, NOT an address-routing fallback — an + * F7-flavoured stamp/type-keying fix. THE FIX: for a global ident, key + * structfloatclass off the global's declared tnode (letvartnode), the same + * classification the local path uses. align ww UP; the .s is byte-identical. + * + * Rows (build+run on cstage `ww` and wwstage `ww_ww`; rule-10 — agree+hit; + * each take() returns 0 iff every field round-trips through the call): + * row | shape | want + * ----------------+----------------------------------+----- + * float_first | gp:{f:f64,i:i64}={1.5,9}; take | 0 [#31: f→X0] + * int_first | gp:{i:i64,f:f64}={9,2.5}; take | 0 [int eb→GP, f→X0] + * both_float | gp:{a:f64,b:f64}={1.5,2.5}; take | 0 [two SSE eightbytes] + */ +#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_exit; +}; + +static const struct row rows[] = { + { "float_first", + "package main;\n" + "type pair = struct { f: f64, i: i64 };\n" + "let gp: pair = pair { f = 1.5, i = 9 };\n" + "fn take(p: pair) int = { if (p.f == 1.5 && p.i == 9) { return 0; }; return 2; };\n" + "export fn main() int = { return take(gp); };\n", + 0 }, + + { "int_first", + "package main;\n" + "type pair = struct { i: i64, f: f64 };\n" + "let gp: pair = pair { i = 9, f = 2.5 };\n" + "fn take(p: pair) int = { if (p.i == 9 && p.f == 2.5) { return 0; }; return 2; };\n" + "export fn main() int = { return take(gp); };\n", + 0 }, + + { "both_float", + "package main;\n" + "type pp = struct { a: f64, b: f64 };\n" + "let gp: pp = pp { a = 1.5, b = 2.5 };\n" + "fn take(p: pp) int = { if (p.a == 1.5 && p.b == 2.5) { return 0; }; return 2; };\n" + "export fn main() int = { return take(gp); };\n", + 0 }, +}; + +/* run_build — build+run `src` via `driver`; returns the binary's exit + * code, or -1 on a build failure. */ +static int +run_build(const char *driver, const struct row *r, int i) +{ + char src[64], tmpdir[64], cmd[1024]; + snprintf(src, sizeof src, "/tmp/gfsa_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/gfsa_%d_d_%d", getpid(), i); + + FILE *f = fopen(src, "wb"); + if (!f) return -2; + fputs(r->src, f); + fclose(f); + + mkdir(tmpdir, 0755); + snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null", + tmpdir, driver, src); + int brc = runwait(cmd); + + 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 = -1; + if (brc == 0) got = runwait(outbin); + + unlink(src); unlink(outbin); rmdir(tmpdir); + return brc == 0 ? got : -1; +} + +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], wdrv[1024]; + snprintf(cdrv, sizeof cdrv, "%s/ww", bin); + snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); + + struct { const char *name; const char *drv; int gated; } + 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 && access(drivers[d].drv, X_OK) != 0) { + fprintf(stderr, "globfloatstructarg_run: skip %s (no %s)\n", + drivers[d].name, drivers[d].drv); + continue; + } + for (int i = 0; i < n; i++) { + total++; + int got = run_build(drivers[d].drv, &rows[i], i); + if (got != rows[i].want_exit) { + fprintf(stderr, "globfloatstructarg_run[%s][%s]: " + "exit=%d want=%d\n", drivers[d].name, + rows[i].label, got, rows[i].want_exit); + fail++; + } + } + } + + if (fail) { + fprintf(stderr, "globfloatstructarg_run: %d/%d fixtures failed\n", + fail, total); + return 1; + } + printf("globfloatstructarg_run: %d/%d ok\n", total, total); + return 0; +}