Files
ww/test/wcc/951_defdim_struct_run.c
Hojun-Cho f1dcd4ecae wcc/check: #141 def-dim array as struct field — fold def in dim, shared arrayelen across 3 ww readers (both stages)
A def-dimensioned array [MAX]u8 used as a struct field was BOTH-WRONG: cstage
loud-rejected ("array length must be an integer literal"); wwstage silently
sized the dim to 0, so the next field overlapped it (frame-smash). The
reference is neither stage — it is Hare: accept + fold the def.

cstage: fold the def into the dim via eval_def_const. The fold needs def NAMES
visible when resolve_typedecl walks struct bodies, so a stub loop binds
def-name stubs (type=NULL, filled in place by the existing def loop) before
resolve_typedecl — this extends check_file's existing names-first USE+TYPEDECL
pass to DEFs; def-TYPE resolution stays in its original order, and the
kind-filtered type lookup (#225) keeps the SK_DEF stub out of type position.

wwstage: one shared arrayelen(c, rhs) (INTLIT -> uval; else evaldefconst;
else 0) routed through astsize / tinfofornode / checkarrlitfits.

Closes #13's def-dim cstage-reject half (the slice-repeat clause stays open).
Pin test/wcc/951 (5 rows incl a cross-module os.PATH_MAX dim + a ~4KB shape;
teeth = cstage loud-reject + ww frame-smash). cgen-first blocker for the
path::buffer arc (type buffer = struct{[MAX]u8, ...}).
2026-06-08 01:00:29 +09:00

255 lines
8.1 KiB
C

/*
* 951_defdim_struct_run — runtime + byte-id net for #141: a struct
* field whose array dimension is a `def` (`[MAX]u8`, MAX a same-module
* def), not an integer literal. BOTH stages were wrong, oppositely:
*
* cstage LOUD-rejected ("array length must be an integer literal").
* resolve_type's N_TARRAY arm folded only N_INTLIT dims; a def-ref
* fell through to the err. Root was a phase-ordering trap: def names
* weren't bound in scope when resolve_typedecl walked the struct
* body, so eval_def_const couldn't see MAX. Fix binds def-name stubs
* before type-body resolution, then folds the dim via eval_def_const.
*
* wwstage SILENTLY mis-laid-out the struct. astsize sized the def-dim
* field to 0 (only N_INTLIT dims were read), so the N_TSTRUCT
* `off += astsize(field)` loop gave the NEXT field the array's offset
* — the trailing scalar overlapped the array, reading/writing the
* wrong bytes (ken pdefM4: end != written). Fix routes every N_TARRAY
* length reader through a shared arrayelen() that folds the def.
*
* The TEETH row is the trailing scalar after the def-dim array: pre-fix
* ww read a corrupted `end`; post-fix it round-trips and cs==ww byte-id.
* Closes #13's def-dim half (the slice-repeat clause stays open).
*
* cstage `ww build` + run for exit code; w6c vs w6c_ww `.s` cmp for the
* rule-10 byte-id gate. The byte-id leg compiles the `ww build`-produced
* .combined.ww, not the raw src: w6c does no import resolution, so the
* cross-module row (C1, `import os`) only folds once concatenated, and a
* same-module row combines to itself. The last row is the ACTUAL
* path::buffer shape — a cross-module `os.PATH_MAX` def dimension (the
* N_DOT fold arm). u8 array elements + i32 trailing scalars only (the
* i32 return is MOVL/MOVSXD byte-id; see 949_dotbase_arr_run).
*/
#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[] = {
/* TEETH: def-dim array FOLLOWED by a scalar; write both, read the
* scalar back. Pre-fix ww laid `end` at the array's offset (array
* sized 0) → end read garbage. Post-fix end at offset 4 → 42. */
{ "m4_end",
"package main;\n"
"def M: i32 = 4;\n"
"type t = struct { b: [M]u8, end: i32 };\n"
"export fn main() i32 = {\n"
" let s: t;\n"
" s.b[3] = 9u8;\n"
" s.end = 42;\n"
" return s.end;\n"
"};\n", 42 },
/* the array element itself must survive too — read the last byte
* (pre-fix the overlapping `end` write would clobber it). */
{ "m4_belem",
"package main;\n"
"def M: i32 = 4;\n"
"type t = struct { b: [M]u8, end: i32 };\n"
"export fn main() i32 = {\n"
" let s: t;\n"
" s.b[3] = 200u8;\n"
" s.end = 7;\n"
" return s.b[3]: i32;\n"
"};\n", 200 },
/* the ~4KB path::buffer shape — def MAX=4095, read the trailing
* scalar after a 4095-byte array. */
{ "m4095_end",
"package main;\n"
"def MAX: i32 = 4095;\n"
"type t = struct { b: [MAX]u8, end: i32 };\n"
"export fn main() i32 = {\n"
" let s: t;\n"
" s.b[4094] = 5u8;\n"
" s.end = 99;\n"
" return s.end;\n"
"};\n", 99 },
/* two scalars after the def-dim array — pins the cumulative offset
* (a+c read their written values: 11+22=33). */
{ "twoafter",
"package main;\n"
"def M: i32 = 4;\n"
"type t = struct { b: [M]u8, a: i32, c: i32 };\n"
"export fn main() i32 = {\n"
" let s: t;\n"
" s.a = 11;\n"
" s.c = 22;\n"
" return s.a + s.c;\n"
"};\n", 33 },
/* C1 (rob-mandatory): the ACTUAL path::buffer shape — a CROSS-
* MODULE def dimension. `def MAX = os.PATH_MAX - 1` folds an N_DOT
* (os.PATH_MAX) const-ref through eval_def_const's cross-module arm,
* which the same-module rows don't exercise. os.PATH_MAX is 4096, so
* MAX is 4095. Byte-id runs on the ww-build combined.ww (w6c alone
* does no import resolution, so it can't compile the raw `import os`
* source). */
{ "xmod_pathmax",
"package main;\n"
"import os;\n"
"def MAX: i32 = os.PATH_MAX - 1;\n"
"type t = struct { b: [MAX]u8, end: i32 };\n"
"export fn main() i32 = {\n"
" let s: t;\n"
" s.b[4094] = 7u8;\n"
" s.end = 77;\n"
" return s.end;\n"
"};\n", 77 },
{ 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, "defdim_struct: 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/wwdds_%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/wwdds_%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 base[64];
const char *b = strrchr(src, '/');
b = b ? b + 1 : src;
snprintf(base, sizeof base, "%s", b);
char *dot = strrchr(base, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
char outbin[160], combined[96], arto[96], arts[96];
/* `ww build` writes the runnable binary into the build cwd
* (tmpdir) but its intermediates — the concatenated module
* source .combined.ww, plus .o/.s — next to the SOURCE. The
* byte-id leg compiles that combined form, not the raw src:
* w6c does no import resolution, so the cross-module row's
* `import os` only folds once concatenated. A same-module row
* combines to itself, so the one path serves every row. */
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
snprintf(combined, sizeof combined,
"/tmp/wwdds_%d_%d.combined.ww", getpid(), i);
snprintf(arto, sizeof arto, "/tmp/wwdds_%d_%d.o", getpid(), i);
snprintf(arts, sizeof arts, "/tmp/wwdds_%d_%d.s", getpid(), i);
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++;
}
char cs_s[64], ws_s[64];
snprintf(cs_s, sizeof cs_s, "/tmp/wwdds_%d_%d_cs.s",
getpid(), i);
snprintf(ws_s, sizeof ws_s, "/tmp/wwdds_%d_%d_ww.s",
getpid(), i);
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
w6c, cs_s, combined);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c failed\n", rows[i].label);
fail++;
} else {
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
w6c_ww, ws_s, combined);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c_ww failed\n",
rows[i].label);
fail++;
} else 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(outbin); rmdir(tmpdir);
unlink(combined); unlink(arto); unlink(arts);
unlink(src); unlink(cs_s); unlink(ws_s);
}
if (fail) {
fprintf(stderr, "%d/%d defdim-struct tests failed\n",
fail, n);
return 1;
}
printf("defdim_struct: %d/%d ok (cstage run + cs==ww byte-id)\n",
n, n);
return 0;
}