wcc/ww: def-dim array .len field-reads resolve the dimension from the type table

buf.len on a [MAX]u8 returned 0 — the cgdot len arms (local/global/
def) and letemitsize only read an N_INTLIT dimension. Route a
non-literal dimension through tichase().alen (the #21 fix mirrored
into the field-read consumers; .ptr arms are dim-independent).
Review-era task #56.
This commit is contained in:
2026-06-13 01:52:42 +09:00
parent faf1a2908b
commit b97be7301f
6 changed files with 308 additions and 30 deletions

View File

@@ -257,6 +257,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_defdim_slice_run \
$(BIN)/test_trystr_run \
$(BIN)/test_allocalias_run \
$(BIN)/test_defdim_field_run \
$(BIN)/test_gunsigned_run \
$(BIN)/test_taggedidx_run \
$(BIN)/test_fnptrcollide_run \
@@ -757,6 +758,17 @@ $(BIN)/test_allocalias_run: test/wcc/989_allocalias_run.c \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
# 989_defdim_field_run (#56): `.len` field-read on a `def`-dimensioned array
# (`[MAX]T`) resolves the length from the type table in the local/global/def
# cgdot arms + letemitsize (cgdot sibling of #21). Builds+runs on BOTH driver
# twins (rule-10).
$(BIN)/test_defdim_field_run: test/wcc/989_defdim_field_run.c \
$(BIN)/ww $(BIN)/ww_ww \
$(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
$(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
# 989_gunsigned_run (F7-c5, #25): a module-global unsigned ident on the
# divide/shift/relational path must pick the unsigned opcode. Builds+runs on
# BOTH driver twins (rule-10). CLASS-M — see the test header.

View File

@@ -26664,8 +26664,19 @@ fn cgdot(c: *cgen, n: *node) void = {
if (streq(fld, "len")) {
let lenn: *node = tn.rhs;
let alen: i64 = 0i64;
if (lenn != nil) {
if (lenn.kind == nkind.N_INTLIT) { alen = lenn.uval: i64; };
if (lenn != nil && lenn.kind == nkind.N_INTLIT) {
alen = lenn.uval: i64;
} else {
// #56: def/const dim — resolve from the
// stamped array tinfo (rule-13), the field-
// read twin of the #21 cgslice fix. The dim
// node is an N_IDENT(def), not N_INTLIT, so
// the literal read above defaults 0; cstage
// reads the resolved bu->alen.
let abt: *tinfo = tichase(tn.type_: *tinfo);
if (abt != nil && abt.kind == tykind.TY_ARRAY) {
alen = abt.alen: i64;
};
};
emitline("\tMOVQ\t$");
emitint(alen);
@@ -26899,9 +26910,15 @@ fn cgdot(c: *cgen, n: *node) void = {
if (streq(fld, "len")) {
let lenn: *node = gtn.rhs;
let alen: i64 = 0i64;
if (lenn != nil) {
if (lenn.kind == nkind.N_INTLIT) {
alen = lenn.uval: i64;
if (lenn != nil && lenn.kind == nkind.N_INTLIT) {
alen = lenn.uval: i64;
} else {
// #56: def/const dim on a let-global array —
// resolve from the stamped array tinfo
// (rule-13), twin of the local arm above.
let abt: *tinfo = tichase(gtn.type_: *tinfo);
if (abt != nil && abt.kind == tykind.TY_ARRAY) {
alen = abt.alen: i64;
};
};
emitline("\tMOVQ\t$");
@@ -26943,9 +26960,15 @@ fn cgdot(c: *cgen, n: *node) void = {
if (dtn.kind == nkind.N_TARRAY) {
let lenn: *node = dtn.rhs;
let alen: i64 = 0i64;
if (lenn != nil) {
if (lenn.kind == nkind.N_INTLIT) {
alen = lenn.uval: i64;
if (lenn != nil && lenn.kind == nkind.N_INTLIT) {
alen = lenn.uval: i64;
} else {
// #56: def/const dim on a def array —
// resolve from the stamped array tinfo
// (rule-13), twin of the let-global arm above.
let abt: *tinfo = tichase(dtn.type_: *tinfo);
if (abt != nil && abt.kind == tykind.TY_ARRAY) {
alen = abt.alen: i64;
};
};
emitline("\tMOVQ\t$");
@@ -41524,8 +41547,17 @@ fn letemitsize(c: *cgen, d: *node) i32 = {
let lenn: *node = t.rhs;
let elemn: *node = t.lhs;
let alen: i32 = 1;
if (lenn != nil) {
if (lenn.kind == nkind.N_INTLIT) { alen = lenn.uval: i32; };
if (lenn != nil && lenn.kind == nkind.N_INTLIT) {
alen = lenn.uval: i32;
} else {
// #56: def/const dim — resolve from the stamped array
// tinfo (rule-13), the letemitsize twin of the cgdot
// .len fix. Pre-fix a non-N_INTLIT dim defaulted alen=1
// → array global mis-sized (one element's worth).
let abt: *tinfo = tichase(t.type_: *tinfo);
if (abt != nil && abt.kind == tykind.TY_ARRAY) {
alen = abt.alen: i32;
};
};
let esz: i32 = 8;
if (elemn != nil) {

View File

@@ -1004,8 +1004,17 @@ fn letemitsize(c: *cgen, d: *node) i32 = {
let lenn: *node = t.rhs;
let elemn: *node = t.lhs;
let alen: i32 = 1;
if (lenn != nil) {
if (lenn.kind == nkind.N_INTLIT) { alen = lenn.uval: i32; };
if (lenn != nil && lenn.kind == nkind.N_INTLIT) {
alen = lenn.uval: i32;
} else {
// #56: def/const dim — resolve from the stamped array
// tinfo (rule-13), the letemitsize twin of the cgdot
// .len fix. Pre-fix a non-N_INTLIT dim defaulted alen=1
// → array global mis-sized (one element's worth).
let abt: *tinfo = tichase(t.type_: *tinfo);
if (abt != nil && abt.kind == tykind.TY_ARRAY) {
alen = abt.alen: i32;
};
};
let esz: i32 = 8;
if (elemn != nil) {

View File

@@ -3444,8 +3444,19 @@ fn cgdot(c: *cgen, n: *node) void = {
if (streq(fld, "len")) {
let lenn: *node = tn.rhs;
let alen: i64 = 0i64;
if (lenn != nil) {
if (lenn.kind == nkind.N_INTLIT) { alen = lenn.uval: i64; };
if (lenn != nil && lenn.kind == nkind.N_INTLIT) {
alen = lenn.uval: i64;
} else {
// #56: def/const dim — resolve from the
// stamped array tinfo (rule-13), the field-
// read twin of the #21 cgslice fix. The dim
// node is an N_IDENT(def), not N_INTLIT, so
// the literal read above defaults 0; cstage
// reads the resolved bu->alen.
let abt: *tinfo = tichase(tn.type_: *tinfo);
if (abt != nil && abt.kind == tykind.TY_ARRAY) {
alen = abt.alen: i64;
};
};
emitline("\tMOVQ\t$");
emitint(alen);
@@ -3679,9 +3690,15 @@ fn cgdot(c: *cgen, n: *node) void = {
if (streq(fld, "len")) {
let lenn: *node = gtn.rhs;
let alen: i64 = 0i64;
if (lenn != nil) {
if (lenn.kind == nkind.N_INTLIT) {
alen = lenn.uval: i64;
if (lenn != nil && lenn.kind == nkind.N_INTLIT) {
alen = lenn.uval: i64;
} else {
// #56: def/const dim on a let-global array —
// resolve from the stamped array tinfo
// (rule-13), twin of the local arm above.
let abt: *tinfo = tichase(gtn.type_: *tinfo);
if (abt != nil && abt.kind == tykind.TY_ARRAY) {
alen = abt.alen: i64;
};
};
emitline("\tMOVQ\t$");
@@ -3723,9 +3740,15 @@ fn cgdot(c: *cgen, n: *node) void = {
if (dtn.kind == nkind.N_TARRAY) {
let lenn: *node = dtn.rhs;
let alen: i64 = 0i64;
if (lenn != nil) {
if (lenn.kind == nkind.N_INTLIT) {
alen = lenn.uval: i64;
if (lenn != nil && lenn.kind == nkind.N_INTLIT) {
alen = lenn.uval: i64;
} else {
// #56: def/const dim on a def array —
// resolve from the stamped array tinfo
// (rule-13), twin of the let-global arm above.
let abt: *tinfo = tichase(dtn.type_: *tinfo);
if (abt != nil && abt.kind == tykind.TY_ARRAY) {
alen = abt.alen: i64;
};
};
emitline("\tMOVQ\t$");

View File

@@ -26664,8 +26664,19 @@ fn cgdot(c: *cgen, n: *node) void = {
if (streq(fld, "len")) {
let lenn: *node = tn.rhs;
let alen: i64 = 0i64;
if (lenn != nil) {
if (lenn.kind == nkind.N_INTLIT) { alen = lenn.uval: i64; };
if (lenn != nil && lenn.kind == nkind.N_INTLIT) {
alen = lenn.uval: i64;
} else {
// #56: def/const dim — resolve from the
// stamped array tinfo (rule-13), the field-
// read twin of the #21 cgslice fix. The dim
// node is an N_IDENT(def), not N_INTLIT, so
// the literal read above defaults 0; cstage
// reads the resolved bu->alen.
let abt: *tinfo = tichase(tn.type_: *tinfo);
if (abt != nil && abt.kind == tykind.TY_ARRAY) {
alen = abt.alen: i64;
};
};
emitline("\tMOVQ\t$");
emitint(alen);
@@ -26899,9 +26910,15 @@ fn cgdot(c: *cgen, n: *node) void = {
if (streq(fld, "len")) {
let lenn: *node = gtn.rhs;
let alen: i64 = 0i64;
if (lenn != nil) {
if (lenn.kind == nkind.N_INTLIT) {
alen = lenn.uval: i64;
if (lenn != nil && lenn.kind == nkind.N_INTLIT) {
alen = lenn.uval: i64;
} else {
// #56: def/const dim on a let-global array —
// resolve from the stamped array tinfo
// (rule-13), twin of the local arm above.
let abt: *tinfo = tichase(gtn.type_: *tinfo);
if (abt != nil && abt.kind == tykind.TY_ARRAY) {
alen = abt.alen: i64;
};
};
emitline("\tMOVQ\t$");
@@ -26943,9 +26960,15 @@ fn cgdot(c: *cgen, n: *node) void = {
if (dtn.kind == nkind.N_TARRAY) {
let lenn: *node = dtn.rhs;
let alen: i64 = 0i64;
if (lenn != nil) {
if (lenn.kind == nkind.N_INTLIT) {
alen = lenn.uval: i64;
if (lenn != nil && lenn.kind == nkind.N_INTLIT) {
alen = lenn.uval: i64;
} else {
// #56: def/const dim on a def array —
// resolve from the stamped array tinfo
// (rule-13), twin of the let-global arm above.
let abt: *tinfo = tichase(dtn.type_: *tinfo);
if (abt != nil && abt.kind == tykind.TY_ARRAY) {
alen = abt.alen: i64;
};
};
emitline("\tMOVQ\t$");
@@ -41524,8 +41547,17 @@ fn letemitsize(c: *cgen, d: *node) i32 = {
let lenn: *node = t.rhs;
let elemn: *node = t.lhs;
let alen: i32 = 1;
if (lenn != nil) {
if (lenn.kind == nkind.N_INTLIT) { alen = lenn.uval: i32; };
if (lenn != nil && lenn.kind == nkind.N_INTLIT) {
alen = lenn.uval: i32;
} else {
// #56: def/const dim — resolve from the stamped array
// tinfo (rule-13), the letemitsize twin of the cgdot
// .len fix. Pre-fix a non-N_INTLIT dim defaulted alen=1
// → array global mis-sized (one element's worth).
let abt: *tinfo = tichase(t.type_: *tinfo);
if (abt != nil && abt.kind == tykind.TY_ARRAY) {
alen = abt.alen: i32;
};
};
let esz: i32 = 8;
if (elemn != nil) {

View File

@@ -0,0 +1,170 @@
/*
* 989_defdim_field_run (#56) — `.len` field-read on an array whose dimension
* is a `def` constant (`[MAX]T`) must resolve the length from the type table,
* the cgdot sibling of the #21 cgslice fix.
*
* THE BUG (wwstage only): the cgdot `.len` arms (local / let-global / def) and
* letemitsize read the array dimension straight off the AST tnode and only
* handled an N_INTLIT dim. A `def MAX` dim arrives as a non-literal node, so
* `.len` folded to MOVQ $0 (ww returned 0 where cstage returns the resolved
* length). cstage reads the resolved bu->alen. THE FIX: when the dim is not an
* N_INTLIT node, read the length from the stamped array tinfo (rule-13), in
* each of the local/global/def cgdot arms + letemitsize.
*
* row | shape | exit (cs==ww)
* --------+----------------------------------------+--------------
* local | def MAX=5; let buf:[MAX]u8; buf.len | 5 (was ww 0)
* global | def MAX=5; let g:[MAX]u8; g.len | 5 (was ww 0)
* defarr | def MAX=5; def A:[MAX]u8; A.len | 5 (was ww 0)
* sum | local + global + def .len (one fn) | 15
*/
#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[] = {
{ "local",
"package main;\n"
"def MAX: i32 = 5;\n"
"export fn main() i32 = {\n"
" let buf: [MAX]u8 = [1, 2, 3, 4, 5];\n"
" return buf.len: i32;\n"
"};\n",
5 },
{ "global",
"package main;\n"
"def MAX: i32 = 5;\n"
"let g: [MAX]u8 = [1, 2, 3, 4, 5];\n"
"export fn main() i32 = {\n"
" return g.len: i32;\n"
"};\n",
5 },
{ "defarr",
"package main;\n"
"def MAX: i32 = 5;\n"
"def A: [MAX]u8 = [1, 2, 3, 4, 5];\n"
"export fn main() i32 = {\n"
" return A.len: i32;\n"
"};\n",
5 },
{ "sum",
"package main;\n"
"def MAX: i32 = 5;\n"
"let g: [MAX]u8 = [1, 2, 3, 4, 5];\n"
"def A: [MAX]u8 = [1, 2, 3, 4, 5];\n"
"export fn main() i32 = {\n"
" let buf: [MAX]u8 = [1, 2, 3, 4, 5];\n"
" return (buf.len + g.len + A.len): i32;\n"
"};\n",
15 },
};
static int
run_build(const char *driver, const struct row *r, int i)
{
char src[64], tmpdir[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/ddf_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/ddf_%d_d_%d", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -2;
fputs(r->src, f);
fclose(f);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null",
tmpdir, driver, src);
int brc = runwait(cmd);
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
char outbin[128];
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
int got = -1;
if (brc == 0) got = runwait(outbin);
unlink(src); unlink(outbin); rmdir(tmpdir);
return brc == 0 ? got : -1;
}
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 n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int i = 0; i < n; i++) {
total++;
int gc = run_build(cdrv, &rows[i], i);
if (gc < 0) {
fprintf(stderr, "defdim_field[cstage][%s]: build/run failed "
"(got %d)\n", rows[i].label, gc);
fail++;
continue;
}
if (gc != rows[i].want_exit) {
fprintf(stderr, "defdim_field[cstage][%s]: exit=%d want=%d\n",
rows[i].label, gc, rows[i].want_exit);
fail++;
}
if (!have_ww) {
fprintf(stderr, "defdim_field: skip wwstage (no %s)\n", wdrv);
continue;
}
int gw = run_build(wdrv, &rows[i], i);
if (gw != gc) {
fprintf(stderr, "defdim_field[%s]: cs=%d != ww=%d "
"(def-dim .len field-read divergence — #56)\n",
rows[i].label, gc, gw);
fail++;
}
if (gw != rows[i].want_exit) {
fprintf(stderr, "defdim_field[wwstage][%s]: exit=%d want=%d\n",
rows[i].label, gw, rows[i].want_exit);
fail++;
}
}
if (fail) {
fprintf(stderr, "defdim_field_run: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("defdim_field_run: %d/%d ok\n", total, total);
return 0;
}