From c490ed3ec11c8d7086f8ecd0cdd218a035fa41a0 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Wed, 3 Jun 2026 23:51:28 +0900 Subject: [PATCH] w6c+w6c_ww: store full slice header for struct-literal slice fields (fix #24) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cg_structlit_fill / cgstructlitfill had a TY_STR arm that stored all three header words (ptr@+0, len@+8, cap@+16) but no TY_SLICE arm, so a slice field in a struct literal `cl{ items = b, n = .. }` fell through to the generic scalar tail and stored only the ptr word — the field's .len and .cap read 0. str fields (the same 24B {ptr,len,cap} shape) worked; slice fields silently dropped two words. Both stages emitted IDENTICAL wrong asm, so the 990-997 byte-id gate was green on both-wrong; runtime readback is the only correctness net. Same is_str/is_slice discrimination gap as #10 part-b, here in the struct-literal field-init path. A slice is the same 24B header shape as str, so widen the str arm's guard to TY_STR || TY_SLICE (cstage) / isstrtype || isslicetype (wwstage) and let a slice ride the already-correct 3-word store. The TAGGED arm stays ordered before it, so a nullable/tagged slice (TY_TAGGED) still routes to the widener, not the 3-word store. Test 689 (table-driven, runtime readback + dual-stage asm byte-id): slice .len/.cap/.ptr, a scalar field beside/before the slice, a slice at a non-zero field offset, two slice fields, and a str field beside a slice (str-arm regression pin). 33/33 ok. --- Makefile | 7 + cmd/w6c/cgen.c | 6 +- selfhost/cmd/w6c/main.combined.ww | 7 +- selfhost/cmd/wcc/cgenutil.ww | 7 +- selfhost/cmd/wwdump/main.combined.ww | 7 +- test/wcc/689_structlit_slice_field.c | 348 +++++++++++++++++++++++++++ 6 files changed, 374 insertions(+), 8 deletions(-) create mode 100644 test/wcc/689_structlit_slice_field.c diff --git a/Makefile b/Makefile index 7801efd8..bfbcf582 100644 --- a/Makefile +++ b/Makefile @@ -246,6 +246,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_slice_str_global_zero \ $(BIN)/test_slice_literal_global \ $(BIN)/test_global_arr_elem_field \ + $(BIN)/test_structlit_slice_field \ $(BIN)/test_dot_str_chained_arg \ $(BIN)/test_dot_slice_arg \ $(BIN)/test_dot_tagged_source \ @@ -597,6 +598,12 @@ $(BIN)/test_global_arr_elem_field: test/wcc/688_global_arr_elem_field.c $(BIN)/w $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_structlit_slice_field: test/wcc/689_structlit_slice_field.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_dot_str_chained_arg: test/wcc/692_dot_str_chained_arg.c $(BIN)/ww \ $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ diff --git a/cmd/w6c/cgen.c b/cmd/w6c/cgen.c index a30373e0..6953c2a5 100644 --- a/cmd/w6c/cgen.c +++ b/cmd/w6c/cgen.c @@ -2690,8 +2690,10 @@ cg_structlit_fill(Cg *c, Local **locals_p, Type *lu, Node *lit, /* str IS []u8: 3-word field (ptr,len,cap). cgexpr leaves * AX/BX/CX; for non-BP modes the dst base goes in DX to dodge * BX=len / CX=cap (the generic store below reloads BX, which - * would clobber len) (#1/Phase 3). */ - if (fu && fu->kind == TY_STR) { + * would clobber len) (#1/Phase 3). A slice is the same 24B + * {ptr,len,cap} shape, so it rides this arm; without it the + * generic scalar tail stored only the ptr word (#24). */ + if (fu && (fu->kind == TY_STR || fu->kind == TY_SLICE)) { cgexpr(c, f->lhs, *locals_p); if (mode == DST_BP) { ins2(c, A_MOVQ, areg(D_AX), diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index e5812fa7..cb680d77 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -19519,12 +19519,15 @@ fn cgstructlitfill(c: *cgen, si: *structinfo, lit: *node, fi = nil; } else if (callwhole) { fi = nil; - } else if (isstrtype(c, fi.tnode)) { + } else if (isstrtype(c, fi.tnode) || isslicetype(c, fi.tnode)) { // str IS []u8: 3-word field (ptr,len,cap). // cgexpr leaves AX/BX/CX; for non-BP modes // the dst base goes in DX to dodge BX=len / // CX=cap (the generic store reloads BX, which - // would clobber len) (#1/Phase 3). + // would clobber len) (#1/Phase 3). A slice is + // the same 24B {ptr,len,cap} shape, so it rides + // this arm; without it the generic scalar tail + // stored only the ptr word (#24). cgexpr(c, fieldnode.lhs); if (mode == 0) { emitline("\tMOVQ\tAX, "); diff --git a/selfhost/cmd/wcc/cgenutil.ww b/selfhost/cmd/wcc/cgenutil.ww index 4a9a881e..dd1080e4 100644 --- a/selfhost/cmd/wcc/cgenutil.ww +++ b/selfhost/cmd/wcc/cgenutil.ww @@ -3853,12 +3853,15 @@ fn cgstructlitfill(c: *cgen, si: *structinfo, lit: *node, fi = nil; } else if (callwhole) { fi = nil; - } else if (isstrtype(c, fi.tnode)) { + } else if (isstrtype(c, fi.tnode) || isslicetype(c, fi.tnode)) { // str IS []u8: 3-word field (ptr,len,cap). // cgexpr leaves AX/BX/CX; for non-BP modes // the dst base goes in DX to dodge BX=len / // CX=cap (the generic store reloads BX, which - // would clobber len) (#1/Phase 3). + // would clobber len) (#1/Phase 3). A slice is + // the same 24B {ptr,len,cap} shape, so it rides + // this arm; without it the generic scalar tail + // stored only the ptr word (#24). cgexpr(c, fieldnode.lhs); if (mode == 0) { emitline("\tMOVQ\tAX, "); diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 59b5218c..908fcce0 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -19519,12 +19519,15 @@ fn cgstructlitfill(c: *cgen, si: *structinfo, lit: *node, fi = nil; } else if (callwhole) { fi = nil; - } else if (isstrtype(c, fi.tnode)) { + } else if (isstrtype(c, fi.tnode) || isslicetype(c, fi.tnode)) { // str IS []u8: 3-word field (ptr,len,cap). // cgexpr leaves AX/BX/CX; for non-BP modes // the dst base goes in DX to dodge BX=len / // CX=cap (the generic store reloads BX, which - // would clobber len) (#1/Phase 3). + // would clobber len) (#1/Phase 3). A slice is + // the same 24B {ptr,len,cap} shape, so it rides + // this arm; without it the generic scalar tail + // stored only the ptr word (#24). cgexpr(c, fieldnode.lhs); if (mode == 0) { emitline("\tMOVQ\tAX, "); diff --git a/test/wcc/689_structlit_slice_field.c b/test/wcc/689_structlit_slice_field.c new file mode 100644 index 00000000..32f2a39d --- /dev/null +++ b/test/wcc/689_structlit_slice_field.c @@ -0,0 +1,348 @@ +/* + * 689_structlit_slice_field — cstage and wwstage agree, byte-for-byte + * and at runtime, that a struct-LITERAL initializer `cl{ items = b, n = + * .. }` with a SLICE field copies the FULL 24B { ptr, len, cap } header + * of that field (task #24). + * + * The bug: cg_structlit_fill / cgstructlitfill had a TY_STR arm that + * stored all three header words (ptr@+0, len@+8, cap@+16), but NO + * TY_SLICE arm — a slice field fell through to the generic scalar tail + * which stored only the ptr word, so the stored .len and .cap read 0. + * str fields (same 24B shape) worked; slice fields silently dropped + * two words. Both stages emitted IDENTICAL wrong asm (the 990-997 + * byte-id gate was green on both-wrong), so RUNTIME readback is the + * correctness net. Same is_str/is_slice discrimination gap as #10 + * part-b, here in the struct-literal field-init path. + * + * The fix (BOTH stages, converged byte-identical): the str arm's guard + * widens to `TY_STR || TY_SLICE` (cstage) / `isstrtype || isslicetype` + * (wwstage) — a slice rides the existing, already-correct 3-word store. + * The TAGGED arm stays ordered BEFORE it, so a nullable/tagged slice + * (TY_TAGGED) still routes to the widener, not the 3-word store. + * + * Mutation-sanity: the .len and .cap rows fail if the store drops + * either word (the pre-fix 1-word slice store), and the scalar-field + * rows (y.n, y.head) pin that the field beside/before the slice is + * untouched. The str_* rows are a regression pin on the untouched str + * arm. + * + * row | shape | want + * ---------------+------------------------------------------+------ + * slice_len | cl{items=b,n=9}; b.len=5; y.items.len | 5 + * slice_cap | cl{items=b,n=9}; b.cap=8; y.items.cap | 8 + * slice_n | cl{items=b,n=9}; y.n (scalar beside slice)| 9 + * slice_ptr | hb[0]=4; cl{items=b,..}; y.items[0] | 4 + * off_len | struct{head:i64,s:[]u8}; s @foff 8; | 7 + * | s.len=7; y.s.len (pins disp+foff+8 hdr) | + * off_cap | same; s.cap=8; y.s.cap (disp+foff+16) | 8 + * off_head | same; head=1; y.head (scalar before slice)| 1 + * two_b_len | struct{a:[]u8,b:[]u8}; b @foff 24; | 6 + * | b.len=6; y.b.len (2nd slice, non-0 foff) | + * two_b_cap | same; b.cap=8; y.b.cap | 8 + * str_name_len | struct{s:[]u8,name:str}; name="hello"; | 5 + * | y.name.len (str arm regression pin) | + * str_s_len | same; s.len=3; y.s.len (slice beside str) | 3 + * + * The asm-byte-id rows pin the symmetric fix (cstage == wwstage); a + * non-empty diff means one stage missed the new slice arm. + */ +#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[] = { + { "slice_len", + "package main;\n" + "type cl = struct { items: []u8, n: i64 };\n" + "export fn main() i32 = {\n" + "\tlet hb: [8]u8;\n" + "\tlet b: []u8; b.ptr = &hb[0]; b.len = 5; b.cap = 8;\n" + "\tlet y: cl = cl { items = b, n = 9i64 };\n" + "\treturn y.items.len: i32;\n" + "};\n", + 5 }, + + { "slice_cap", + "package main;\n" + "type cl = struct { items: []u8, n: i64 };\n" + "export fn main() i32 = {\n" + "\tlet hb: [8]u8;\n" + "\tlet b: []u8; b.ptr = &hb[0]; b.len = 5; b.cap = 8;\n" + "\tlet y: cl = cl { items = b, n = 9i64 };\n" + "\treturn y.items.cap: i32;\n" + "};\n", + 8 }, + + { "slice_n", + "package main;\n" + "type cl = struct { items: []u8, n: i64 };\n" + "export fn main() i32 = {\n" + "\tlet hb: [8]u8;\n" + "\tlet b: []u8; b.ptr = &hb[0]; b.len = 5; b.cap = 8;\n" + "\tlet y: cl = cl { items = b, n = 9i64 };\n" + "\treturn y.n: i32;\n" + "};\n", + 9 }, + + { "slice_ptr", + "package main;\n" + "type cl = struct { items: []u8, n: i64 };\n" + "export fn main() i32 = {\n" + "\tlet hb: [8]u8; hb[0] = 4u8;\n" + "\tlet b: []u8; b.ptr = &hb[0]; b.len = 5; b.cap = 8;\n" + "\tlet y: cl = cl { items = b, n = 9i64 };\n" + "\treturn y.items[0]: i32;\n" + "};\n", + 4 }, + + /* slice field at a NON-ZERO field offset (head: i64 @0, s @8). + * Pins disp+foff+8/+16 in the stored header. */ + { "off_len", + "package main;\n" + "type hs = struct { head: i64, s: []u8 };\n" + "export fn main() i32 = {\n" + "\tlet hb: [8]u8;\n" + "\tlet b: []u8; b.ptr = &hb[0]; b.len = 7; b.cap = 8;\n" + "\tlet y: hs = hs { head = 1i64, s = b };\n" + "\treturn y.s.len: i32;\n" + "};\n", + 7 }, + + { "off_cap", + "package main;\n" + "type hs = struct { head: i64, s: []u8 };\n" + "export fn main() i32 = {\n" + "\tlet hb: [8]u8;\n" + "\tlet b: []u8; b.ptr = &hb[0]; b.len = 7; b.cap = 8;\n" + "\tlet y: hs = hs { head = 1i64, s = b };\n" + "\treturn y.s.cap: i32;\n" + "};\n", + 8 }, + + { "off_head", + "package main;\n" + "type hs = struct { head: i64, s: []u8 };\n" + "export fn main() i32 = {\n" + "\tlet hb: [8]u8;\n" + "\tlet b: []u8; b.ptr = &hb[0]; b.len = 7; b.cap = 8;\n" + "\tlet y: hs = hs { head = 1i64, s = b };\n" + "\treturn y.head: i32;\n" + "};\n", + 1 }, + + /* two slice fields: a @0, b @24. Pins the 2nd slice's header at a + * non-zero field offset (the 24B stride). */ + { "two_b_len", + "package main;\n" + "type tt = struct { a: []u8, b: []u8 };\n" + "export fn main() i32 = {\n" + "\tlet hb: [8]u8;\n" + "\tlet p: []u8; p.ptr = &hb[0]; p.len = 3; p.cap = 8;\n" + "\tlet q: []u8; q.ptr = &hb[0]; q.len = 6; q.cap = 8;\n" + "\tlet y: tt = tt { a = p, b = q };\n" + "\treturn y.b.len: i32;\n" + "};\n", + 6 }, + + { "two_b_cap", + "package main;\n" + "type tt = struct { a: []u8, b: []u8 };\n" + "export fn main() i32 = {\n" + "\tlet hb: [8]u8;\n" + "\tlet p: []u8; p.ptr = &hb[0]; p.len = 3; p.cap = 8;\n" + "\tlet q: []u8; q.ptr = &hb[0]; q.len = 6; q.cap = 8;\n" + "\tlet y: tt = tt { a = p, b = q };\n" + "\treturn y.b.cap: i32;\n" + "};\n", + 8 }, + + /* str field beside a slice field — the untouched str arm must still + * store its full header (regression pin). */ + { "str_name_len", + "package main;\n" + "type sn = struct { s: []u8, name: str };\n" + "export fn main() i32 = {\n" + "\tlet hb: [8]u8;\n" + "\tlet b: []u8; b.ptr = &hb[0]; b.len = 3; b.cap = 8;\n" + "\tlet y: sn = sn { s = b, name = \"hello\" };\n" + "\treturn y.name.len: i32;\n" + "};\n", + 5 }, + + { "str_s_len", + "package main;\n" + "type sn = struct { s: []u8, name: str };\n" + "export fn main() i32 = {\n" + "\tlet hb: [8]u8;\n" + "\tlet b: []u8; b.ptr = &hb[0]; b.len = 3; b.cap = 8;\n" + "\tlet y: sn = sn { s = b, name = \"hello\" };\n" + "\treturn y.s.len: i32;\n" + "};\n", + 3 }, +}; + +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/slf_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/slf_%d_d_%d", getpid(), i); + + FILE *f = fopen(src, "wb"); + if (!f) return -1; + fputs(r->src, f); + fclose(f); + + mkdir(tmpdir, 0755); + snprintf(cmd, sizeof cmd, "cd %s && %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 — w6c vs w6c_ww .s for the same source must match. + * Pre-fix BOTH stages dropped len+cap identically (byte-id green on + * both-wrong); the symmetric fix keeps them byte-identical and correct. */ +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/slf_asm_%d_%d.ww", getpid(), i); + snprintf(cs, sizeof cs, "/tmp/slf_asm_%d_%d_c.s", getpid(), i); + snprintf(ws, sizeof ws, "/tmp/slf_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, "%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, "%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, "structlit_slice_field: 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, + "structlit_slice_field[%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, + "structlit_slice_field: %d/%d fixtures failed\n", + fail, total); + return 1; + } + printf("structlit_slice_field: %d/%d ok\n", total, total); + return 0; +}