diff --git a/Makefile b/Makefile index 75872347..3fc0ba8b 100644 --- a/Makefile +++ b/Makefile @@ -325,6 +325,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_global_arr_elem_field \ $(BIN)/test_structlit_slice_field \ $(BIN)/test_arrglob_nodup_run \ + $(BIN)/test_globptr_field_store_run \ $(BIN)/test_dot_str_chained_arg \ $(BIN)/test_dot_slice_arg \ $(BIN)/test_dot_tagged_source \ @@ -1448,6 +1449,15 @@ $(BIN)/test_arrglob_nodup_run: test/wcc/689_arrglob_nodup_run.c $(BIN)/ww \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +# 689_globptr_field_store_run (#6): a module-global *struct ptr base field +# store (compound / non-scalar / chained / indexed) no longer SEGVs in cstage +# and is byte-identical to wwstage (cstage aligned UP via decline-to-resolver). +$(BIN)/test_globptr_field_store_run: test/wcc/689_globptr_field_store_run.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 19b8ce5b..b7e7348a 100644 --- a/cmd/w6c/cgen.c +++ b/cmd/w6c/cgen.c @@ -2406,6 +2406,74 @@ cgplaceaddr(Cg *c, Node *n, int dst_reg, Local *locals) return 0; } +/* #6 — a module-GLOBAL `*struct` pointer base (`gp.f = v`, `gp.f += v`) + * carries via_ptr=1 but has NO local slot: localfind returns 0, so the + * enumerated via_ptr field-store arms emit `MOVQ (BP),BX` (deref the + * saved BP = garbage → SEGV). wwstage routes every such case EXCEPT + * plain-scalar-`=` through its F6 cgplaceaddr resolver (the scalar-only + * dedicated arm, selfhost cgenexpr.ww; compound + non-scalar = the + * deferred #60/#61). cstage's own F6 assign-resolver (cgen.c N_ASSIGN, + * via cgplaceaddr) emits byte-identically. So decline those cases at the + * enumerated arm's dispatch — they fall to the resolver, aligning cstage + * UP to wwstage and restoring byte-id (handle where the resolver handles; + * loud-stop, same message, where it loud-stops on tagged/float). The + * plain-scalar-`=` struct-field case STAYS in the enumerated arm — its + * #47 fix already matches wwstage's dedicated arm. Discriminant is + * localfind-ABSENCE + let_islet (storage class), NEVER a boff==0 + * sentinel: a param at offset 0 has localfind != 0 and stays local. */ +static int +global_ptr_field_decline(Node *lhs, int op, Local *locals) +{ + if (lhs == NULL || lhs->kind != N_DOT || lhs->lhs == NULL) + return 0; + Node *base = lhs->lhs; + if (base->kind == N_UN && base->op == TK_STAR) base = base->lhs; + if (base == NULL || base->kind != N_IDENT) return 0; + if (localfind(locals, base->str) != 0 || !let_islet(base->str)) + return 0; + Type *bu = type_chase_named(base->type); + if (bu == NULL || bu->kind != TY_PTR) return 0; + Type *su = type_chase_named(bu->sub); + if (su == NULL) return 0; + /* `(*gp).ptr/.len/.cap` pseudo-field: no struct field, always + * resolver (wwstage loud-stops "unsupported assign target shape"). */ + if (su->kind == TY_SLICE || su->kind == TY_STR) return 1; + if (su->kind != TY_STRUCT) return 0; + if (op != TK_ASSIGN) return 1; + for (Tfield *fl = su->fields; fl; fl = fl->next) + if (strcmp(fl->name, lhs->str) == 0) { + Type *fu = type_chase_named(fl->type); + int isf32 = 0; + if (fld_isfloat(fl->type, &isf32)) return 1; + if (fu && (fu->kind == TY_STRUCT + || fu->kind == TY_ARRAY || fu->kind == TY_TUPLE + || fu->kind == TY_STR || fu->kind == TY_SLICE + || fu->kind == TY_TAGGED)) + return 1; + return 0; + } + return 0; +} + +/* #6 (chained twin) — a multi-dot LHS (`gp.x.y = v`) rooted at a module- + * GLOBAL pointer the walk dereferences: the chained value-struct walker + * (cgen.c N_ASSIGN) conflates is_global with ptr_root and emits the same + * `MOVQ (BP),CX` SEGV for plain `=`. Decline at entry → the F6 resolver + * (byte-id with wwstage). A global VALUE-struct root (`gv.x.y`, root type + * TY_STRUCT) and a LOCAL pointer root (localfind != 0) both stay in the + * walker — they are already correct. */ +static int +global_ptr_chain_root_decline(Node *lhs, Local *locals) +{ + Node *cur = lhs; + while (cur && cur->kind == N_DOT) cur = cur->lhs; + if (cur == NULL || cur->kind != N_IDENT) return 0; + if (localfind(locals, cur->str) != 0 || !let_islet(cur->str)) + return 0; + Type *u = type_chase_named(cur->type); + return (u != NULL && u->kind == TY_PTR); +} + /* FA1 (#15): append() header-place access, cgplaceaddr's append * consumer. direct = ident-local header in the frame (BP-disp — the * legacy emission, kept byte-identical); indirect = header address @@ -5078,7 +5146,8 @@ cgexpr(Cg *c, Node *n, Local *locals) * `p.f = v`. v1 scope: bare-IDENT inner only; (*expr).f * (non-IDENT inner) falls through to the existing drop * behaviour pending follow-up task. */ - if (n->lhs && n->lhs->kind == N_DOT && n->lhs->lhs && + if (!global_ptr_field_decline(n->lhs, n->op, locals) + && n->lhs && n->lhs->kind == N_DOT && n->lhs->lhs && (n->lhs->lhs->kind == N_IDENT || (n->lhs->lhs->kind == N_UN && n->lhs->lhs->op == TK_STAR && n->lhs->lhs->lhs @@ -6080,7 +6149,8 @@ cgexpr(Cg *c, Node *n, Local *locals) * (the existing 1-deep branch only fires for `ident.field = …`). * Only plain `=` is wired — compound on a chained value-struct * field is rare and stays unhandled. */ - if (n->lhs && n->lhs->kind == N_DOT && n->lhs->lhs + if (!global_ptr_chain_root_decline(n->lhs, locals) + && n->lhs && n->lhs->kind == N_DOT && n->lhs->lhs && n->lhs->lhs->kind == N_DOT && n->op == TK_ASSIGN) { struct { Type *pu; const char *name; } steps[16]; int nsteps = 0; diff --git a/test/wcc/689_globptr_field_store_run.c b/test/wcc/689_globptr_field_store_run.c new file mode 100644 index 00000000..c670c4bf --- /dev/null +++ b/test/wcc/689_globptr_field_store_run.c @@ -0,0 +1,227 @@ +/* + * 689_globptr_field_store_run (#6) — a module-GLOBAL `*struct` pointer base + * (`gp.f = v` / `gp.f += v` / `gp.x.y = v` / `gp.a[i].f = v`) stores + * correctly AND cstage / wwstage agree byte-for-byte. + * + * THE BUG (cstage only): a global `*struct` ptr base carries via_ptr=1 but + * has no local slot — localfind returns 0, so the enumerated via_ptr field- + * store arms emitted `MOVQ (BP),BX` (deref the saved BP = garbage → SEGV). + * The plain-scalar-`=` STORE half was already correct (the #47/#8 fix, LEAQ + * gp(SB)), so load/store were asymmetric. wwstage routes every case EXCEPT + * plain-scalar-`=` through its F6 cgplaceaddr resolver and was correct. + * + * THE FIX (align cstage UP): decline the global-`*struct`-ptr base at the + * enumerated arm's dispatch for compound + non-scalar (and a chained + * `gp.x.y` root), so cstage falls to its OWN F6 assign-resolver — byte- + * identical to wwstage. Plain-scalar-`=` stays in the enumerated arm (its + * #47 byte-id is undisturbed); a local ptr and a param at offset 0 + * (localfind != 0) also stay local. + * + * shape | want + * ------------------------------+------ + * gp.f += 5 (compound off 8) | 5 + * gp.f += 7 (compound off 0) | 7 + * gp.in = Inner{7,9} (struct) | 16 + * gp.sf = "hi" (str field) | 2 + * gp.x.y=11; gp.x.z+=7 (chain) | 18 + * gp.a[1].f=5; gp.a[1].g+=8 | 13 + * lp.f += 5 (LOCAL ptr ctrl) | 5 + * bump(&s): p.f/p.g (param ctrl)| 10 + * Each row also asserts w6c .s == w6c_ww .s byte-identical. + * Pre-fix: cstage SEGV(139) on every global-ptr row; cs vs ww .s differed. + */ +#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 *name; + const char *src; + int want; +}; + +static const struct row ROWS[] = { +{ "compound_off8", + "package main;\n" + "type S = struct { a: i64, f: i64 };\n" + "let gp: *S = nil;\n" + "export fn main() i32 = { let s: S = S{a=0,f=0}; gp = &s;" + " gp.f += 5; return s.f: i32; };\n", 5 }, +{ "compound_off0", + "package main;\n" + "type S = struct { f: i64, g: i64 };\n" + "let gp: *S = nil;\n" + "export fn main() i32 = { let s: S = S{f=0,g=0}; gp = &s;" + " gp.f += 7; return s.f: i32; };\n", 7 }, +{ "struct_field", + "package main;\n" + "type I = struct { x: i64, y: i64 };\n" + "type S = struct { a: i64, in: I };\n" + "let gp: *S = nil;\n" + "export fn main() i32 = { let s: S = S{a=0,in=I{x=0,y=0}}; gp = &s;" + " gp.in = I{x=7,y=9}; return (s.in.x+s.in.y): i32; };\n", 16 }, +{ "str_field", + "package main;\n" + "type S = struct { a: i64, sf: str };\n" + "let gp: *S = nil;\n" + "export fn main() i32 = { let s: S = S{a=0,sf=\"\"}; gp = &s;" + " gp.sf = \"hi\"; return (s.sf.len): i32; };\n", 2 }, +{ "chained", + "package main;\n" + "type I = struct { y: i64, z: i64 };\n" + "type S = struct { a: i64, x: I };\n" + "let gp: *S = nil;\n" + "export fn main() i32 = { let s: S = S{a=0,x=I{y=0,z=0}}; gp = &s;" + " gp.x.y = 11; gp.x.z += 7; return (s.x.y+s.x.z): i32; };\n", 18 }, +{ "indexed", + "package main;\n" + "type E = struct { f: i64, g: i64 };\n" + "type S = struct { n: i64, a: [3]E };\n" + "let gp: *S = nil;\n" + "export fn main() i32 = { let s: S; gp = &s;" + " gp.a[1].f = 5; gp.a[1].g += 8; return (s.a[1].f+s.a[1].g): i32; };\n", 13 }, +{ "local_ptr_ctrl", + "package main;\n" + "type S = struct { a: i64, f: i64 };\n" + "export fn main() i32 = { let s: S = S{a=0,f=0}; let lp: *S = &s;" + " lp.f += 5; return s.f: i32; };\n", 5 }, +{ "param_off0_ctrl", + "package main;\n" + "type S = struct { f: i64, g: i64 };\n" + "fn bump(p: *S) void = { p.f += 3; p.g += 4; };\n" + "export fn main() i32 = { let s: S = S{f=1,g=2}; bump(&s);" + " return (s.f+s.g): i32; };\n", 10 }, +}; + +#define NROWS ((int)(sizeof ROWS / sizeof ROWS[0])) + +/* build+run one row through `driver`; return exit code or -1. */ +static int +run_driver(const char *driver, const char *src, int idx) +{ + char tmpdir[64], srcf[128], outbin[160], rmcmd[192], cmd[2400]; + snprintf(tmpdir, sizeof tmpdir, "/tmp/gpfs_%d_%d_d", getpid(), idx); + mkdir(tmpdir, 0755); + snprintf(srcf, sizeof srcf, "%s/t.ww", tmpdir); + snprintf(outbin, sizeof outbin, "%s/t", tmpdir); + snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir); + + FILE *f = fopen(srcf, "wb"); + if (!f) { runwait(rmcmd); return -1; } + fputs(src, f); + fclose(f); + + snprintf(cmd, sizeof cmd, "%s build -o %s %s 2>/dev/null", + driver, outbin, srcf); + if (runwait(cmd) != 0) { runwait(rmcmd); return -1; } + int got = runwait(outbin); + runwait(rmcmd); + return got; +} + +/* emit .s for `tool` (w6c / w6c_ww) into asmf; 0 ok, -1 on compile fail. */ +static int +emit_asm(const char *bin, const char *tool, const char *src, int idx, + const char *asmf) +{ + char srcf[80], cmd[2400]; + snprintf(srcf, sizeof srcf, "/tmp/gpfs_asm_%d_%d.ww", getpid(), idx); + FILE *f = fopen(srcf, "wb"); + if (!f) return -1; + fputs(src, f); + fclose(f); + snprintf(cmd, sizeof cmd, "%s/%s -o %s %s 2>/dev/null", + bin, tool, asmf, srcf); + int rc = runwait(cmd); + unlink(srcf); + return rc == 0 ? 0 : -1; +} + +static int +files_identical(const char *a, const char *b) +{ + FILE *fa = fopen(a, "rb"), *fb = fopen(b, "rb"); + if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return 0; } + int ca, cb, same = 1; + do { + ca = fgetc(fa); cb = fgetc(fb); + if (ca != cb) { same = 0; break; } + } while (ca != EOF); + fclose(fa); fclose(fb); + return same; +} + +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); + int have_ww = (access(wdrv, X_OK) == 0); + + int fail = 0, total = 0; + for (int i = 0; i < NROWS; i++) { + const struct row *r = &ROWS[i]; + + total++; + int gc = run_driver(cdrv, r->src, i); + if (gc != r->want) { + fprintf(stderr, "globptr_field_store[%s,cstage]: run=%d want=%d\n", + r->name, gc, r->want); + fail++; + } + if (have_ww) { + total++; + int gw = run_driver(wdrv, r->src, i); + if (gw != r->want) { + fprintf(stderr, "globptr_field_store[%s,wwstage]: run=%d want=%d\n", + r->name, gw, r->want); + fail++; + } + } + + /* byte-id: w6c .s == w6c_ww .s */ + if (have_ww) { + char csf[80], wwf[80]; + snprintf(csf, sizeof csf, "/tmp/gpfs_cs_%d_%d.s", getpid(), i); + snprintf(wwf, sizeof wwf, "/tmp/gpfs_ww_%d_%d.s", getpid(), i); + total++; + int ec = emit_asm(bin, "w6c", r->src, i, csf); + int ew = emit_asm(bin, "w6c_ww", r->src, i, wwf); + if (ec != 0 || ew != 0 || !files_identical(csf, wwf)) { + fprintf(stderr, "globptr_field_store[%s]: cs/ww .s NOT " + "byte-identical (ec=%d ew=%d)\n", r->name, ec, ew); + fail++; + } + unlink(csf); unlink(wwf); + } + } + + if (fail) { + fprintf(stderr, "globptr_field_store_run: %d/%d checks failed\n", + fail, total); + return 1; + } + printf("globptr_field_store_run: %d/%d ok\n", total, total); + return 0; +}