cgen: fix global-ptr field READ, load ptr value via SB before offset (#15)
Reading gp.f through a module-global pointer miscompiled in BOTH stages, differently: cstage classified gp as a local at boff 0 and derefed BP (MOVQ (BP),BX), wwstage collapsed gp.f to an undefined global symbol f (MOVQ f(SB)). Both now load the pointer value from the global's data slot before the field offset, converging on MOVQ gp(SB),BX; MOVQ off(BX),AX. cstage mirrors the #6 store decline; wwstage gains a global-ptr arm and shares a cgptrfieldload helper with the local arm. Fused, not split: the two stages must emit byte-identical asm, so a one-stage commit would fail the byte-id gate. Sibling byte-divergences filed: #16 (chained-spine gp.x.y), #17 (>32B tagged word-order). Test: table-driven 689_globptr_field_read_run (24 rows, runtime + byte-id).
This commit is contained in:
231
test/wcc/689_globptr_field_read_run.c
Normal file
231
test/wcc/689_globptr_field_read_run.c
Normal file
@@ -0,0 +1,231 @@
|
||||
/*
|
||||
* 689_globptr_field_read_run (#15) — reading a field through a module-
|
||||
* GLOBAL `*struct` pointer base (`gp.f`, single-dot) loads correctly AND
|
||||
* cstage / wwstage agree byte-for-byte.
|
||||
*
|
||||
* THE BUG (both stages, two different wrong ways — no byte-id oracle until
|
||||
* one side was fixed, but they DID diverge): a global `*struct` ptr base
|
||||
* carries via_ptr=1 but has NO local slot (localfind/localfindnode = 0),
|
||||
* so the pointer-to-struct field-READ arm never loaded the pointer VALUE
|
||||
* from the global's data slot before applying the field offset:
|
||||
* cstage : `MOVQ (BP),BX; MOVQ 8(BX),AX` — derefed the saved BP → SEGV.
|
||||
* wwstage: `MOVQ f(SB),AX` — collapsed gp.f to a bare
|
||||
* undefined symbol `f`.
|
||||
* Read twin of the #6 STORE fix (689_globptr_field_store_run).
|
||||
*
|
||||
* THE FIX (both stages converge): load the pointer value via SB first —
|
||||
* `MOVQ gp(SB),BX; MOVQ off(BX),AX`
|
||||
* cstage: base load is now `off==0 && let_islet → MOVQ mafn(gp)(SB),BX`.
|
||||
* wwstage: a dedicated global-ptr arm (lc==nil + isletvar + N_TPTR) emits
|
||||
* `MOVQ name(SB),BX` and shares the field tail (cgptrfieldload).
|
||||
*
|
||||
* shape | want
|
||||
* -------------------------------+------
|
||||
* gp.f (scalar, offset 8) | 170
|
||||
* gp.f+gp.g (scalar, offset 0/8) | 14
|
||||
* let x=gp.sf; x.len (str field) | 3
|
||||
* gp.d : i32 (f64 field) | 7
|
||||
* match gp.t (tagged field, 16B) | 5
|
||||
* gp.len (*[]u8 pseudo-field) | 3
|
||||
* lp.f (LOCAL ptr control) | 42
|
||||
* bump param p.f (param control) | 10
|
||||
* Each row also asserts w6c .s == w6c_ww .s byte-identical.
|
||||
* Pre-fix: cstage SEGV(139) on every global-ptr row; wwstage ran wrong;
|
||||
* cs vs ww .s differed.
|
||||
*
|
||||
* SCOPE: single-dot field reads only. Chained-through-global-ptr-root
|
||||
* (`gp.x.y`, `gp.sf.len` inline) is a sibling — both stages run correct
|
||||
* after this fix, but the chained-N_DOT spines still diverge in idiom
|
||||
* (cstage offset-folds; wwstage inner-loads + shuffles), filed separately.
|
||||
*/
|
||||
#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[] = {
|
||||
{ "scalar_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;"
|
||||
" s.f = 170; return gp.f: i32; };\n", 170 },
|
||||
{ "scalar_off0_off8",
|
||||
"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;"
|
||||
" s.f = 5; s.g = 9; return (gp.f + gp.g): i32; };\n", 14 },
|
||||
{ "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=\"abc\"}; gp = &s;"
|
||||
" let x: str = gp.sf; return x.len: i32; };\n", 3 },
|
||||
{ "float_field",
|
||||
"package main;\n"
|
||||
"type S = struct { a: i64, d: f64 };\n"
|
||||
"let gp: *S = nil;\n"
|
||||
"export fn main() i32 = { let s: S = S{a=0,d=0.0}; gp = &s;"
|
||||
" s.d = 7.0; return (gp.d): i32; };\n", 7 },
|
||||
{ "tagged_field",
|
||||
"package main;\n"
|
||||
"type S = struct { a: i64, t: (i64 | i32) };\n"
|
||||
"let gp: *S = nil;\n"
|
||||
"export fn main() i32 = { let s: S = S{a=0,t=5i32}; gp = &s;"
|
||||
" let v: i32 = 0; match (gp.t) { case let x: i32 => v = x;"
|
||||
" case let y: i64 => v = y: i32; }; return v; };\n", 5 },
|
||||
{ "pslice_len",
|
||||
"package main;\n"
|
||||
"let gp: *[]u8 = nil;\n"
|
||||
"export fn main() i32 = { let b: []u8 = [1u8,2u8,3u8]; gp = &b;"
|
||||
" return (gp.len): i32; };\n", 3 },
|
||||
{ "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;"
|
||||
" s.f = 42; return lp.f: i32; };\n", 42 },
|
||||
{ "param_ctrl",
|
||||
"package main;\n"
|
||||
"type S = struct { f: i64, g: i64 };\n"
|
||||
"fn rd(p: *S) i32 = { return (p.f + p.g): i32; };\n"
|
||||
"export fn main() i32 = { let s: S = S{f=4,g=6}; return rd(&s); };\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/gpfr_%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/gpfr_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_read[%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_read[%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/gpfr_cs_%d_%d.s", getpid(), i);
|
||||
snprintf(wwf, sizeof wwf, "/tmp/gpfr_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_read[%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_read_run: %d/%d checks failed\n",
|
||||
fail, total);
|
||||
return 1;
|
||||
}
|
||||
printf("globptr_field_read_run: %d/%d ok\n", total, total);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user