Files
ww/test/wcc/949_structlit_arrfield_run.c
Hojun-Cho 0fb4bae337 w6c+wwstage: store struct-literal array-field init (#249 BUG A)
A struct literal initialising an array-typed field as a local
(`e{ encmap = [..] }`) silently dropped the initializer: cg_structlit_fill
(cstage) / cgstructlitfill (wwstage) had no TY_ARRAY field arm, so the
array field fell to the generic scalar tail — cgexpr the N_ARRLIT (→ AX≈0)
then store one sized word — losing every element. cstage returned 0;
wwstage emitted byte-identical wrong code. (The GLOBAL literal-init path
is unaffected: it goes through emit_struct_lit_bytes, already correct via
#129 A.3.)

Both stages now element-wise store the N_ARRLIT at base+field_off+i*esz,
reusing the proven N_LET array-init shape (cgen.c:8467 / cgenstmt.ww:1393)
for int and float elements plus its `...` repeat fill; esz routes through
the type table (rule 13). str/slice/struct/tagged ELEMENT arrays are the
N_LET path's documented multi-word gap (cgen.c:8462) — converted from the
silent drop to a LOUD rule-7 error in both stages, not left silent.
Symmetric both stages (rule 10), byte-identical .s.

The `...` repeat in a struct-literal array field is checker-unreachable
today (the field type-check rejects `[v...]` length inference — a
separate checker gap); the arm mirrors N_LET's repeat for symmetry.

Test 949_structlit_arrfield_run: +local literal-init reads (idx 0 / last
element), cstage run + cs==ww byte-id.
2026-06-02 01:19:46 +09:00

213 lines
7.1 KiB
C

/*
* 949_structlit_arrfield_run — runtime + byte-id net for #249: struct
* array-field init/read silent miscompiles. Two distinct roots, both
* gate-blind (cs==ww broken identically pre-fix):
*
* BUG B — reading an array field of a module-GLOBAL struct value
* (`G.arr[i]`). The N_INDEX fallback's cg_dotbase_addr / dotbaseaddr
* helper (the #135 sibling) had no module-global-struct base arm:
* cstage emitted `LEAQ (BP)` (read the stack → 0), wwstage fell to
* cgexpr(N_DOT) which loaded the field VALUE as a pointer → SEGFAULT.
* The .data was already correct (emit_struct_lit_bytes #129 A.3); only
* the READ base address was wrong. Fix: a global value-struct base
* emits `LEAQ name(SB) (+ ADDQ field_off)`, mirroring the scalar
* global-field read (cgen.c:7532). const globals are def_isstructdef.
*
* BUG A — initializing an array field from a struct literal
* (`e{ arr = [..] }`) as a local. cg_structlit_fill / cgstructlitfill
* had no TY_ARRAY field arm; the array field fell to the generic
* scalar tail (cgexpr the N_ARRLIT → AX, store one sized word) which
* silently DROPPED every element. Fix: a TY_ARRAY field arm element-
* wise stores the N_ARRLIT at base+field_off+i*esz, reusing the proven
* N_LET array-init shape. The GLOBAL literal-init path is unaffected
* (it goes through emit_struct_lit_bytes, already correct).
*
* cstage `ww build` + run for exit code; w6c vs w6c_ww `.s` cmp for the
* rule-10 byte-id gate. u8 element rows only (a `[N]u8` read cast to i32
* is byte-id; wider widths hit the i32-return MOVL/MOVSXD cs/ww
* divergence — see 949_dotbase_arr_run's deferral note).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.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 *label; const char *src; int want_exit; };
static const struct row rows[] = {
/* BUG B: read [4]u8 field of a module-global `let` struct at idx 0
* (field_off 0). Pre-fix cstage→0, wwstage→SEGFAULT. encmap[0]='A'
* (65). */
{ "global_let_read",
"package main;\n"
"type e = struct { encmap: [4]u8 };\n"
"let g: e = e { encmap = [65u8, 66u8, 67u8, 68u8] };\n"
"export fn main() i32 = { return g.encmap[0]: i32; };\n", 65 },
/* BUG B: read [4]u8 field of a module-global `def` (const) struct
* at idx 2, with a non-zero field offset (a [3]u8 pad ahead of it) —
* exercises def_isstructdef + the ADDQ $field_off arm. The base64
* `const std_encoding` shape. pad ahead, encmap[2]='C' (67). */
{ "global_def_read_off",
"package main;\n"
"type e = struct { pad: [3]u8, encmap: [4]u8 };\n"
"def G: e = e { pad = [9u8, 9u8, 9u8],"
" encmap = [65u8, 66u8, 67u8, 68u8] };\n"
"export fn main() i32 = { return G.encmap[2]: i32; };\n", 67 },
/* BUG A: local struct-literal init of a [4]u8 field, read idx 0.
* Pre-fix the array field fell to the generic scalar tail (cgexpr
* the N_ARRLIT → AX, store one word) and silently dropped every
* element → 0. encmap[0]='A' (65). */
{ "local_lit_read0",
"package main;\n"
"type e = struct { encmap: [4]u8 };\n"
"export fn main() i32 = {\n"
" let g: e = e { encmap = [65u8, 66u8, 67u8, 68u8] };\n"
" return g.encmap[0]: i32;\n"
"};\n", 65 },
/* BUG A: same init, read idx 3 — asserts the LAST element landed
* (the pre-fix single-word store would never reach it). 'D' (68). */
{ "local_lit_read3",
"package main;\n"
"type e = struct { encmap: [4]u8 };\n"
"export fn main() i32 = {\n"
" let g: e = e { encmap = [65u8, 66u8, 67u8, 68u8] };\n"
" return g.encmap[3]: i32;\n"
"};\n", 68 },
/* NB: a trailing `...` repeat in a struct-LITERAL array field
* (`encmap = [7u8...]`) is rejected by the CHECKER ("[1]u8 not
* assignable to [4]u8") — the field type-check doesn't apply the
* repeat-length inference that bare `let a: [N]T = [v...]` gets.
* The cg_structlit_fill array arm mirrors the N_LET `...` handling
* for symmetry, but that path is checker-unreachable today (separate
* checker gap, not #249). No row exercises it. */
{ NULL, NULL, 0 }
};
static int
slurp_eq(const char *a, const char *b)
{
FILE *fa = fopen(a, "rb");
FILE *fb = fopen(b, "rb");
if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; }
int rc = 0;
for (;;) {
int ca = fgetc(fa);
int cb = fgetc(fb);
if (ca != cb) { rc = -1; break; }
if (ca == EOF) break;
}
fclose(fa); fclose(fb);
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 w6c[1100], w6c_ww[1100];
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
if (access(w6c_ww, X_OK) != 0) {
fprintf(stderr, "structlit_arrfield: w6c_ww missing — cannot run "
"the cs==ww byte-id gate (the whole point of this test)\n");
return 1;
}
int n = 0, fail = 0;
for (int i = 0; rows[i].src; i++, n++) {
char src[64];
snprintf(src, sizeof src, "/tmp/wwsaf_%d_%d.ww", getpid(), i);
FILE *f = fopen(src, "wb");
if (f == NULL) { fail++; continue; }
fputs(rows[i].src, f);
fclose(f);
char tmpdir[64];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwsaf_%d_d_%d",
getpid(), i);
mkdir(tmpdir, 0755);
char cmd[2048];
snprintf(cmd, sizeof cmd, "cd %s && %s/ww build %s",
tmpdir, bin, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: cstage build failed\n",
rows[i].label);
fail++;
unlink(src); rmdir(tmpdir);
continue;
}
char outbin[128];
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
int got = runwait(outbin);
if (got != rows[i].want_exit) {
fprintf(stderr, "row[%s]: cstage exit %d, want %d\n",
rows[i].label, got, rows[i].want_exit);
fail++;
}
unlink(outbin); rmdir(tmpdir);
char cs_s[64], ws_s[64];
snprintf(cs_s, sizeof cs_s, "/tmp/wwsaf_%d_%d_cs.s",
getpid(), i);
snprintf(ws_s, sizeof ws_s, "/tmp/wwsaf_%d_%d_ww.s",
getpid(), i);
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
w6c, cs_s, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c failed\n", rows[i].label);
fail++; unlink(src); continue;
}
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
w6c_ww, ws_s, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c_ww failed\n",
rows[i].label);
fail++; unlink(src); unlink(cs_s); continue;
}
if (slurp_eq(cs_s, ws_s) != 0) {
fprintf(stderr,
"row[%s]: cstage/wwstage .s DIFFER (rule-10 "
"byte-id violation)\n", rows[i].label);
fail++;
}
unlink(src); unlink(cs_s); unlink(ws_s);
}
if (fail) {
fprintf(stderr, "%d/%d structlit-arrfield tests failed\n",
fail, n);
return 1;
}
printf("structlit_arrfield: %d/%d ok (cstage run + cs==ww byte-id)\n",
n, n);
return 0;
}