cgen: ww str for-range loop-var narrows to MOVZBQ -- Phase 2 step-3 Fold 1 (align-up)
Ranging a str (for (let b .. = s)) and reading the loop var back emitted MOVZBQ on cstage (correct u8 zero-extend) but MOVQ on wwstage (the missed case, #14). Align wwstage UP. ww cgforrange derived the element-type node only for slice/array; for a str scrutinee it left elemt=nil, so the loop var registered with no type and localloadop short-circuited to MOVQ. Fix: for a str scrutinee, synthesize a u8 element node (type_ = str.sub = u8, from F1) as elemt, so localadd hands the loop var a u8 tnode and the GENERIC narrow-load fires (MOVZBQ) -- consuming str.sub as F1 intended, mirroring how []u8 supplies its element node. NOT an if-str special-case. cstage already correct, untouched (ww-only). str's own type stays nominal. GATE is the ASM SHAPE byte-id (cstage==wwstage at the loop-var read), NOT a runtime probe: the divergence is runtime-benign (MOVQ and MOVZBQ read the same zero-extended byte) so a runtime test passes both ways and cannot distinguish -- it was a byte-id-INVISIBLE divergence (990-997 green despite cstage!=ww, since no bootstrap input exercises a narrow-read str loop var). Verified fail-pre (the cstage-MOVZBQ vs wwstage-MOVQ 1-line diff) / pass-post (.s byte-identical). []u8/slice/array for-range emission unchanged. test/wcc/940 carries the fixture (runtime corpus coverage, both drivers). main.combined.ww regenerated via the canonical make path.
This commit is contained in:
7
Makefile
7
Makefile
@@ -262,6 +262,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
|
||||
$(BIN)/test_str_arrfield_store_cap_run \
|
||||
$(BIN)/test_str_chainfield_store_cap_run \
|
||||
$(BIN)/test_str_massign_store_cap_run \
|
||||
$(BIN)/test_str_forrange_loopvar_run \
|
||||
$(BIN)/test_composite_call_arg \
|
||||
$(BIN)/test_composite_call_arg_run \
|
||||
$(BIN)/test_letdecl_zeroinit \
|
||||
@@ -676,6 +677,12 @@ $(BIN)/test_str_massign_store_cap_run: test/wcc/939_str_massign_store_cap_run.c
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
$(BIN)/test_str_forrange_loopvar_run: test/wcc/940_str_forrange_loopvar_run.c \
|
||||
$(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
|
||||
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
$(BIN)/test_composite_call_arg: test/wcc/723_composite_call_arg.c \
|
||||
$(BIN)/w6c $(BIN)/w6c_ww | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
@@ -21356,6 +21356,27 @@ fn cgforrange(c: *cgen, n: *node) void = {
|
||||
let sk: nkind = slctn.kind;
|
||||
if (sk == nkind.N_TSLICE) { elemt = slctn.lhs; };
|
||||
if (sk == nkind.N_TARRAY) { elemt = slctn.lhs; };
|
||||
// str IS []u8 (F1: tystr.sub = tyu8). []u8 hands cgen a real
|
||||
// u8 element node (slctn.lhs); a str scrutinee has none, so the
|
||||
// loop var would register tnode=nil and read back as a wide
|
||||
// MOVQ. Synthesise the u8 element off str.sub so the loop-var
|
||||
// registration carries a u8 tnode and localloadop narrows the
|
||||
// read-back to MOVZBQ on its own — aligning wwstage up to
|
||||
// cstage, whose checker stamps the binding u8. Kind-gated so
|
||||
// str's own type stays nominal.
|
||||
if (sk == nkind.N_TNAME) {
|
||||
if (streq(slctn.str, "str")) {
|
||||
let sti: *tinfo = slctn.type_: *tinfo;
|
||||
if (sti != nil) {
|
||||
if (sti.sub != nil) {
|
||||
let u8n: *node = newnode(nkind.N_TNAME, slctn.file, slctn.line, slctn.col);
|
||||
u8n.str = "u8";
|
||||
u8n.type_ = sti.sub: *void;
|
||||
elemt = u8n;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
// esz: raw elem byte size. For tuple-element slices `[](T0, T1)`,
|
||||
// C cgen reads the resolved tuple's size (sum of raw param sizes,
|
||||
|
||||
@@ -1504,6 +1504,27 @@ fn cgforrange(c: *cgen, n: *node) void = {
|
||||
let sk: nkind = slctn.kind;
|
||||
if (sk == nkind.N_TSLICE) { elemt = slctn.lhs; };
|
||||
if (sk == nkind.N_TARRAY) { elemt = slctn.lhs; };
|
||||
// str IS []u8 (F1: tystr.sub = tyu8). []u8 hands cgen a real
|
||||
// u8 element node (slctn.lhs); a str scrutinee has none, so the
|
||||
// loop var would register tnode=nil and read back as a wide
|
||||
// MOVQ. Synthesise the u8 element off str.sub so the loop-var
|
||||
// registration carries a u8 tnode and localloadop narrows the
|
||||
// read-back to MOVZBQ on its own — aligning wwstage up to
|
||||
// cstage, whose checker stamps the binding u8. Kind-gated so
|
||||
// str's own type stays nominal.
|
||||
if (sk == nkind.N_TNAME) {
|
||||
if (streq(slctn.str, "str")) {
|
||||
let sti: *tinfo = slctn.type_: *tinfo;
|
||||
if (sti != nil) {
|
||||
if (sti.sub != nil) {
|
||||
let u8n: *node = newnode(nkind.N_TNAME, slctn.file, slctn.line, slctn.col);
|
||||
u8n.str = "u8";
|
||||
u8n.type_ = sti.sub: *void;
|
||||
elemt = u8n;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
// esz: raw elem byte size. For tuple-element slices `[](T0, T1)`,
|
||||
// C cgen reads the resolved tuple's size (sum of raw param sizes,
|
||||
|
||||
@@ -21356,6 +21356,27 @@ fn cgforrange(c: *cgen, n: *node) void = {
|
||||
let sk: nkind = slctn.kind;
|
||||
if (sk == nkind.N_TSLICE) { elemt = slctn.lhs; };
|
||||
if (sk == nkind.N_TARRAY) { elemt = slctn.lhs; };
|
||||
// str IS []u8 (F1: tystr.sub = tyu8). []u8 hands cgen a real
|
||||
// u8 element node (slctn.lhs); a str scrutinee has none, so the
|
||||
// loop var would register tnode=nil and read back as a wide
|
||||
// MOVQ. Synthesise the u8 element off str.sub so the loop-var
|
||||
// registration carries a u8 tnode and localloadop narrows the
|
||||
// read-back to MOVZBQ on its own — aligning wwstage up to
|
||||
// cstage, whose checker stamps the binding u8. Kind-gated so
|
||||
// str's own type stays nominal.
|
||||
if (sk == nkind.N_TNAME) {
|
||||
if (streq(slctn.str, "str")) {
|
||||
let sti: *tinfo = slctn.type_: *tinfo;
|
||||
if (sti != nil) {
|
||||
if (sti.sub != nil) {
|
||||
let u8n: *node = newnode(nkind.N_TNAME, slctn.file, slctn.line, slctn.col);
|
||||
u8n.str = "u8";
|
||||
u8n.type_ = sti.sub: *void;
|
||||
elemt = u8n;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
// esz: raw elem byte size. For tuple-element slices `[](T0, T1)`,
|
||||
// C cgen reads the resolved tuple's size (sum of raw param sizes,
|
||||
|
||||
169
test/wcc/940_str_forrange_loopvar_run.c
Normal file
169
test/wcc/940_str_forrange_loopvar_run.c
Normal file
@@ -0,0 +1,169 @@
|
||||
/*
|
||||
* 940_str_forrange_loopvar_run — corpus coverage for the step-3 Fold 1
|
||||
* fold: ranging a `str` value-form (`for (let b .. s)`) and reading the
|
||||
* loop var `b` back must narrow to MOVZBQ (u8 zero-extend), matching
|
||||
* cstage. Pre-fold the wwstage cgen registered the loop var with a nil
|
||||
* tnode (str had no element node the way []u8 does), so the read-back
|
||||
* fell through localloadop to a wide MOVQ; cstage's checker stamps the
|
||||
* binding u8, so it narrowed. The fold synthesises the u8 element off
|
||||
* str.sub (Phase 2 F1) so the loop var carries a u8 tnode and the
|
||||
* existing narrow-load logic fires on its own.
|
||||
*
|
||||
* REAL GATE IS SHAPE BYTE-ID, NOT RUNTIME. The MOVQ-vs-MOVZBQ
|
||||
* divergence is runtime-benign: the slot is always written by a
|
||||
* MOVZBQ-into-AX then MOVQ-AX-into-slot, so the upper bytes are already
|
||||
* zero — a MOVQ read-back yields the same value a MOVZBQ would. A
|
||||
* runtime probe therefore passes on BOTH the buggy and the fixed code
|
||||
* and CANNOT distinguish them. The load-bearing gate is diffing the
|
||||
* cstage (w6c) and wwstage (w6c_ww) .s at the loop-var read:
|
||||
* pre-fold: cstage MOVZBQ vs wwstage MOVQ (1-line diff)
|
||||
* post-fold: byte-identical
|
||||
* This fixture exists for corpus presence — it confirms the path lowers
|
||||
* and runs correctly through both drivers; it does not by itself prove
|
||||
* the narrow.
|
||||
*
|
||||
* Loop-var read-back is exercised two ways the fold touches:
|
||||
* - Site A: pass `b` to a fn (`use1(b)` → cgident read into an arg).
|
||||
* - Site B: use `b` in an arithmetic expression (`sum += b: i32`).
|
||||
*/
|
||||
#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; };
|
||||
|
||||
static const struct row rows[] = {
|
||||
/* Site A — read-back into a fn arg. "AB" is 0x41,0x42; the callee
|
||||
* checks each byte zero-extends to its exact value, so a botched
|
||||
* wide read (had the slot held garbage) would mismatch. */
|
||||
{ "sitea_arg_readback",
|
||||
"fn check(x: u8) i32 = {\n"
|
||||
" if (x: i32 < 65) { return 1; };\n"
|
||||
" if (x: i32 > 66) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let s: str = \"AB\";\n"
|
||||
" for (let b .. s) {\n"
|
||||
" if (check(b) != 0) { return 1; };\n"
|
||||
" };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
0 },
|
||||
/* Site B — read-back in an arithmetic expression. Sum the bytes of
|
||||
* "AB" (0x41 + 0x42 = 131) and confirm. */
|
||||
{ "siteb_expr_readback",
|
||||
"export fn main() i32 = {\n"
|
||||
" let s: str = \"AB\";\n"
|
||||
" let sum: i32 = 0;\n"
|
||||
" for (let b .. s) {\n"
|
||||
" sum += b: i32;\n"
|
||||
" };\n"
|
||||
" if (sum != 131) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
0 },
|
||||
};
|
||||
|
||||
static int
|
||||
run_driver(const char *driver, const struct row *r, int i)
|
||||
{
|
||||
char src[96], tmpdir[96], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/strforrange_%d_%d.ww", getpid(), i);
|
||||
snprintf(tmpdir, sizeof tmpdir, "/tmp/strforrange_%d_d_%d", getpid(), i);
|
||||
|
||||
FILE *f = fopen(src, "wb");
|
||||
if (!f) return -1;
|
||||
fputs(r->src, f);
|
||||
fclose(f);
|
||||
|
||||
mkdir(tmpdir, 0755);
|
||||
snprintf(cmd, sizeof cmd, "cd %s && %s build %s",
|
||||
tmpdir, driver, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: build via %s failed\n",
|
||||
r->label, driver);
|
||||
unlink(src); rmdir(tmpdir);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *base = strrchr(src, '/');
|
||||
base = base ? base + 1 : src;
|
||||
char outbin[160];
|
||||
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
|
||||
char *dot = strrchr(outbin, '.');
|
||||
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
|
||||
int got = runwait(outbin);
|
||||
|
||||
unlink(src); unlink(outbin); rmdir(tmpdir);
|
||||
return got;
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const char *bin = getenv("BIN");
|
||||
if (!bin) bin = "out/bin";
|
||||
char absbin[512];
|
||||
if (bin[0] != '/') {
|
||||
char cwd[256];
|
||||
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
|
||||
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
|
||||
bin = absbin;
|
||||
}
|
||||
|
||||
char cdrv[640];
|
||||
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
||||
char wdrv[640];
|
||||
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
|
||||
|
||||
struct { const char *name; const char *path; int gated_on_existence; }
|
||||
drivers[] = {
|
||||
{ "cstage", cdrv, 0 },
|
||||
{ "wwstage", wdrv, 1 },
|
||||
{ NULL, NULL, 0 },
|
||||
};
|
||||
|
||||
int n = (int)(sizeof rows / sizeof rows[0]);
|
||||
int total = 0, fail = 0;
|
||||
for (int d = 0; drivers[d].name; d++) {
|
||||
if (drivers[d].gated_on_existence
|
||||
&& access(drivers[d].path, X_OK) != 0) {
|
||||
fprintf(stderr,
|
||||
"str_forrange_loopvar_run: skip %s (no %s)\n",
|
||||
drivers[d].name, drivers[d].path);
|
||||
continue;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
int got = run_driver(drivers[d].path, &rows[i], i);
|
||||
total++;
|
||||
if (got != rows[i].want) {
|
||||
fprintf(stderr,
|
||||
"str_forrange_loopvar_run[%s][%s]: exit=%d want=%d\n",
|
||||
drivers[d].name, rows[i].label,
|
||||
got, rows[i].want);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
fprintf(stderr, "str_forrange_loopvar_run: %d/%d fixtures failed\n",
|
||||
fail, total);
|
||||
return 1;
|
||||
}
|
||||
printf("str_forrange_loopvar_run: %d/%d ok\n", total, total);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user