From fee84528da854229d12ae9319977222e1489ca93 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Fri, 12 Jun 2026 08:57:04 +0900 Subject: [PATCH] wcc/ww: global-struct slice-field store widens the str arm to slices A slice-typed field of a module-global struct stored only the str-form words; widen the arm to the full slice header via the g(SB) base (mirror cgen.c sibling arm). Review item #35; dual-stage rows red-proven. --- Makefile | 12 ++ selfhost/cmd/w6c/main.combined.ww | 16 ++- selfhost/cmd/wcc/cgenexpr.ww | 16 ++- selfhost/cmd/wwdump/main.combined.ww | 16 ++- test/wcc/989_globslicefield_run.c | 204 +++++++++++++++++++++++++++ 5 files changed, 246 insertions(+), 18 deletions(-) create mode 100644 test/wcc/989_globslicefield_run.c diff --git a/Makefile b/Makefile index 4d947728..69b73bc0 100644 --- a/Makefile +++ b/Makefile @@ -255,6 +255,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_taggedidx_run \ $(BIN)/test_fnptrcollide_run \ $(BIN)/test_tagnorm_run \ + $(BIN)/test_globslicefield_run \ $(BIN)/test_arr_ptr_global \ $(BIN)/test_def_arr_infer_len \ $(BIN)/test_def_arr_len \ @@ -711,6 +712,17 @@ $(BIN)/test_fnptrcollide_run: test/wcc/989_fnptrcollide_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +# 989_globslicefield_run (F8-c1, #35): a SLICE field of a module-global +# struct assigned via `g.f = ` must store the full {ptr,len,cap} +# header. Builds+runs on BOTH driver twins (rule-10); align-UP — see the +# test header. +$(BIN)/test_globslicefield_run: test/wcc/989_globslicefield_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 8bc29570..1e222bff 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -32994,12 +32994,16 @@ fn cgassign(c: *cgen, n: *node) void = { }; if (n.op == tkind.TK_ASSIGN) { cgexpr(c, n.rhs); - if (isstrtype(c, fi.tnode)) { - // str IS []u8: cgexpr left - // (AX=ptr, BX=len, CX=cap). CX - // holds cap, so stage the base - // addr in DX and store all three - // words (#1/Phase 3). + if (isstrtype(c, fi.tnode) || isslicetype(c, fi.tnode)) { + // str OR slice field: str IS []u8, so + // both store the full {ptr,len,cap} + // header (cstage cgen.c:5055 gates + // TY_STR||TY_SLICE the same; without the + // slice arm this dropped to the 1-word + // scalar store below). cgexpr left + // (AX=ptr, BX=len, CX=cap). CX holds cap, + // so stage the base addr in DX and store + // all three words (#1/Phase 3). emitline("\tLEAQ\t"); emitsymname(c, bn); emitline("(SB), DX\n"); diff --git a/selfhost/cmd/wcc/cgenexpr.ww b/selfhost/cmd/wcc/cgenexpr.ww index 7f003f5a..b680dd82 100644 --- a/selfhost/cmd/wcc/cgenexpr.ww +++ b/selfhost/cmd/wcc/cgenexpr.ww @@ -10230,12 +10230,16 @@ fn cgassign(c: *cgen, n: *node) void = { }; if (n.op == tkind.TK_ASSIGN) { cgexpr(c, n.rhs); - if (isstrtype(c, fi.tnode)) { - // str IS []u8: cgexpr left - // (AX=ptr, BX=len, CX=cap). CX - // holds cap, so stage the base - // addr in DX and store all three - // words (#1/Phase 3). + if (isstrtype(c, fi.tnode) || isslicetype(c, fi.tnode)) { + // str OR slice field: str IS []u8, so + // both store the full {ptr,len,cap} + // header (cstage cgen.c:5055 gates + // TY_STR||TY_SLICE the same; without the + // slice arm this dropped to the 1-word + // scalar store below). cgexpr left + // (AX=ptr, BX=len, CX=cap). CX holds cap, + // so stage the base addr in DX and store + // all three words (#1/Phase 3). emitline("\tLEAQ\t"); emitsymname(c, bn); emitline("(SB), DX\n"); diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 2459f399..d5297fbf 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -32994,12 +32994,16 @@ fn cgassign(c: *cgen, n: *node) void = { }; if (n.op == tkind.TK_ASSIGN) { cgexpr(c, n.rhs); - if (isstrtype(c, fi.tnode)) { - // str IS []u8: cgexpr left - // (AX=ptr, BX=len, CX=cap). CX - // holds cap, so stage the base - // addr in DX and store all three - // words (#1/Phase 3). + if (isstrtype(c, fi.tnode) || isslicetype(c, fi.tnode)) { + // str OR slice field: str IS []u8, so + // both store the full {ptr,len,cap} + // header (cstage cgen.c:5055 gates + // TY_STR||TY_SLICE the same; without the + // slice arm this dropped to the 1-word + // scalar store below). cgexpr left + // (AX=ptr, BX=len, CX=cap). CX holds cap, + // so stage the base addr in DX and store + // all three words (#1/Phase 3). emitline("\tLEAQ\t"); emitsymname(c, bn); emitline("(SB), DX\n"); diff --git a/test/wcc/989_globslicefield_run.c b/test/wcc/989_globslicefield_run.c new file mode 100644 index 00000000..aa489685 --- /dev/null +++ b/test/wcc/989_globslicefield_run.c @@ -0,0 +1,204 @@ +/* + * 989_globslicefield_run — F8-c1 (report-item #35): a SLICE field of a + * module-GLOBAL struct, assigned with `g.field = `, must store the + * full {ptr,len,cap} header — both stages, byte-identical and correct. + * + * THE BUG (cat-A silent miscompile, align-UP): in cgenexpr.ww the + * single-dot global-struct field-assign block (`g.f = v`, base is a direct + * global struct ident) had a str arm that staged the base in DX and stored + * all three header words, but it gated on isstrtype ONLY — a non-str slice + * field ([]i64, []int, ...) fell through to the generic 1-word scalar store + * (`LEAQ g(SB),BX; MOVQ AX,(BX)`), silently dropping .len and .cap. cstage + * (cmd/w6c/cgen.c:5055) gates the same arm on TY_STR||TY_SLICE, so it stored + * the full header and ran correct — the cat-A divergence. The bootstrap + * corpus never assigns a non-str slice to a global-struct field, so 990-997 + * stayed green; a runtime row is the net. THE FIX: the wwstage gate widens + * to isstrtype||isslicetype (aligning UP to cstage); a slice rides the + * existing, already-correct 3-word DX store. + * + * Rows (each builds+runs on cstage `ww` and, when present, wwstage `ww_ww`; + * rule-10 — both stages agree AND hit want_exit): + * row | shape | want + * ---------------+---------------------------------------------+----- + * glob_slice_len | g:{sl:[]i64}; g.sl=b[0:3]; len(g.sl) | 3 [#35] + * glob_slice_cap | same; g.sl.cap (b:[4]i64 → cap 4) | 4 [#35] + * glob_off_len | g:{pad:i64,sl:[]i64}; sl@foff 8; len(g.sl) | 2 [#35: + * | pins the DX+foff+8 header store] | + * glob_str_len | g:{name:str}; g.name="hello"; len(g.name) | 5 (str-arm + * | regression pin — the pre-existing path) | + * glob_scalar_n | g:{n:i64}; g.n=7; g.n (scalar field control) | 7 (the + * | 1-word store the slice arm must not steal) | + * + * Pre-fix wwstage: glob_slice_len/cap/off_len all read 0 for the dropped + * words (the 1-word store); cstage and the str/scalar rows were correct. + */ +#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[] = { + { "glob_slice_len", + "package main;\n" + "type box = struct { sl: []i64 };\n" + "let g: box;\n" + "export fn main() int = {\n" + " let b: [4]i64 = [10, 11, 12, 13];\n" + " g.sl = b[0:3];\n" + " return len(g.sl): int;\n" + "};\n", + 3 }, + + { "glob_slice_cap", + "package main;\n" + "type box = struct { sl: []i64 };\n" + "let g: box;\n" + "export fn main() int = {\n" + " let b: [4]i64 = [10, 11, 12, 13];\n" + " g.sl = b[0:3];\n" + " return g.sl.cap: int;\n" + "};\n", + 4 }, + + /* slice field at NON-ZERO field offset (pad: i64 @0, sl @8). Pins + * the DX+foff+8 / +16 header store. */ + { "glob_off_len", + "package main;\n" + "type box = struct { pad: i64, sl: []i64 };\n" + "let g: box;\n" + "export fn main() int = {\n" + " let b: [4]i64 = [10, 11, 12, 13];\n" + " g.pad = 99;\n" + " g.sl = b[0:2];\n" + " return len(g.sl): int;\n" + "};\n", + 2 }, + + /* str field on a global struct — the pre-existing isstrtype arm; a + * regression pin that the widened gate keeps it byte-identical. */ + { "glob_str_len", + "package main;\n" + "type box = struct { name: str };\n" + "let g: box;\n" + "export fn main() int = {\n" + " g.name = \"hello\";\n" + " return len(g.name): int;\n" + "};\n", + 5 }, + + /* scalar field on a global struct — the generic 1-word store the + * slice arm must not divert (control). */ + { "glob_scalar_n", + "package main;\n" + "type box = struct { n: i64 };\n" + "let g: box;\n" + "export fn main() int = {\n" + " g.n = 7;\n" + " return g.n: int;\n" + "};\n", + 7 }, +}; + +/* 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/gslf_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/gslf_%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, "globslicefield_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, "globslicefield_run[%s][%s]: exit=%d " + "want=%d\n", drivers[d].name, rows[i].label, + got, rows[i].want_exit); + fail++; + } + } + } + + if (fail) { + fprintf(stderr, "globslicefield_run: %d/%d fixtures failed\n", + fail, total); + return 1; + } + printf("globslicefield_run: %d/%d ok\n", total, total); + return 0; +}