w6c: fix global-ptr field-store SEGV via decline-to-resolver (#6)
A module-global pointer's field store/compound (`let gp:*S=nil; gp.f += 5`,
`gp.in = Inner{...}`) SEGV'd in cstage: the enumerated N_DOT-lhs arms load
the base pointer with `MOVQ boff(BP),BX`, valid only for a LOCAL ptr slot,
but a module-global ptr has no local slot (localfind=0) so it dereferenced
the saved BP. wwstage was correct -- it routes these through its F6
cgplaceaddr resolver (its dedicated arm is scalar-`=`-only by design,
#60/#61). The byte-id gate was blind (no global-ptr compound in the
bootstrap corpus) and the deferral note was stale: this is a live cs!=ww
divergence with wwstage as the oracle.
cstage already has an equivalent assign-resolver (cgen.c ~7488) that emits
byte-identically to wwstage's F6 route, but the enumerated arms intercepted
the global case first. Fix (align cstage UP, cstage-only): two precondition
entry-guards decline a module-global `*struct` base for the compound +
non-scalar-field cases so they fall through to the resolver. Plain-scalar
`=` stays in the enumerated arm (its #47 fix already matches wwstage). The
decline and resolver accept-sets exactly partition the global-base
N_DOT-lhs space (no gap, no overlap); tagged/float field stores now both
loud-stop symmetrically (were SEGV'ing). The discriminant keys on
localfind-presence + let_islet, so a param at offset 0 stays local.
New both-stage + byte-id test 689_globptr_field_store_run covers offset-0/8,
compound, struct/str field, chained gp.x.y, indexed gp.a[i].f, with local +
offset-0-param controls. The field-READ path is independently broken in
both stages (filed #15). make clean && make test: all 403 passed, byte-id
990-996 green.
This commit is contained in:
227
test/wcc/689_globptr_field_store_run.c
Normal file
227
test/wcc/689_globptr_field_store_run.c
Normal file
@@ -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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user