w6c+w6c_ww: for-range over a non-ident slice base — bound from len, base ptr spilled (#70)
The N_FORRANGE header's non-ident arm stored cgexpr's AX into the
single bound temp — but a slice-valued cgexpr leaves AX=ptr, BX=len,
CX=cap, so the loop compared i against the DATA POINTER; and the
per-iteration element address had no non-ident base arm at all, so
the bound reload doubled as the base. One slot, two roles, holding
the wrong word. An empty slice coincidentally exited (ptr==0), which
is how regex.finish's `for (let charset .. re.charsets)` — planted
verbatim in fold 1 — stayed latent until fold 4 produced the first
non-empty charsets and SEGV'd. Byte-id both stages (the 989 M_ID
entry held on both-wrong-identical); first-consumer surfacing, the
kwtab/#8 pattern.
Fix mirrors the correct local-base arm: bound = BX (len), base ptr
spilled to a dedicated .rgb slot and reloaded per iteration. Covers
field-chain, indexed-element (the task-#57 shape) and call-result
bases. Two shapes whose cgexpr does NOT deliver the header convention
stay LOUD instead of silently wrong (rule 7): deref bases (*p — the
#11 deref-spine family) and non-ident ARRAY bases.
test/937: field (value+ptr roots), 24B-str-header field (the finish
shape), indexed, call, empty-header, eval-once (header captured at
loop entry, not re-read per iteration) rows + the two reject pins,
per-row cs==ww byte-id; verified failing 14/22 at the #66 parent
bb8a44a.
This commit is contained in:
7
Makefile
7
Makefile
@@ -309,6 +309,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
|
||||
$(BIN)/test_tuple_in_union_run \
|
||||
$(BIN)/test_tuple_slot_layout_run \
|
||||
$(BIN)/test_tagged_tuple_widen_run \
|
||||
$(BIN)/test_forrange_fieldbase_run \
|
||||
$(BIN)/test_errtype_compare \
|
||||
$(BIN)/test_tuple_elem_slice_len_run \
|
||||
$(BIN)/test_str_forrange_loopvar_run \
|
||||
@@ -1428,6 +1429,12 @@ $(BIN)/test_tagged_tuple_widen_run: test/wcc/936_tagged_tuple_widen_run.c \
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
$(BIN)/test_forrange_fieldbase_run: test/wcc/937_forrange_fieldbase_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_errtype_compare: test/wcc/949_errtype_compare.c \
|
||||
$(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
|
||||
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \
|
||||
|
||||
@@ -12012,11 +12012,33 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame)
|
||||
? u->sub->under : (u ? u->sub : NULL);
|
||||
int destruct = (n->list != NULL);
|
||||
|
||||
/* allocate temp slots: _i (8B), _len (8B) */
|
||||
/* allocate temp slots: _i (8B), _len (8B). #70: a NON-IDENT
|
||||
* slice/str base (field chain, indexed element, call) also
|
||||
* needs a _base spill — pre-#70 the init stored cgexpr's AX
|
||||
* (the DATA POINTER — a slice-valued cgexpr leaves AX=ptr,
|
||||
* BX=len, CX=cap) into _len, and the per-iteration code had
|
||||
* no non-ident base arm at all, so the bound-reload BX
|
||||
* doubled as the base: i was compared against the POINTER
|
||||
* and walked off the end (regex.finish, SEGV on the first
|
||||
* non-empty charsets; empty slices coincidentally exited on
|
||||
* ptr==0 — latent since fold 1, byte-id both stages). */
|
||||
char *iname = aprintf(c->a, ".rgi_%d", c->labelseq++);
|
||||
char *lname = aprintf(c->a, ".rgl_%d", c->labelseq++);
|
||||
int ioff = localoff(c, locals, iname, 8, frame);
|
||||
int loff = localoff(c, locals, lname, 8, frame);
|
||||
/* #11: cgexpr on a slice DEREF (*p) does not deliver the
|
||||
* AX/BX/CX header convention the spill below assumes (the
|
||||
* deref-spine load family) — pre-#70 this shape crashed or
|
||||
* mis-summed; keep it LOUD until #11 wires the deref load. */
|
||||
if (slc && slc->kind == N_UN && slc->op == TK_STAR
|
||||
&& u && (u->kind == TY_SLICE || u->kind == TY_STR))
|
||||
fatal("for-range over a deref base unwired (#11)");
|
||||
int baseoff = 0;
|
||||
if (slc && slc->kind != N_IDENT
|
||||
&& !(u && u->kind == TY_ARRAY)) {
|
||||
char *bname = aprintf(c->a, ".rgb_%d", c->labelseq++);
|
||||
baseoff = localoff(c, locals, bname, 8, frame);
|
||||
}
|
||||
|
||||
/* allocate per-name slots */
|
||||
struct { int off, sz, foff; Type *ftype; } binds[8] = {0};
|
||||
@@ -12053,11 +12075,29 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame)
|
||||
ins2(c, A_MOVQ, amem(D_BP, boff + 8), areg(D_AX));
|
||||
ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, loff));
|
||||
} else if (u && u->kind == TY_ARRAY) {
|
||||
/* #70: a non-ident ARRAY base has no base spill and
|
||||
* its cgexpr register shape is not the slice header —
|
||||
* the per-iteration base would be garbage. Loud (rule
|
||||
* 7) until a consumer wires it. */
|
||||
if (slc->kind != N_IDENT)
|
||||
fatal("for-range over a non-ident array base "
|
||||
"unwired (#70)");
|
||||
ins2(c, A_MOVQ, aimm((long long)u->alen),
|
||||
amem(D_BP, loff));
|
||||
} else {
|
||||
cgexpr(c, slc, *locals);
|
||||
ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, loff));
|
||||
if (baseoff != 0) {
|
||||
/* #70: slice/str header from cgexpr is AX=ptr,
|
||||
* BX=len, CX=cap — bound is LEN; spill the base
|
||||
* ptr for the per-iteration element address. */
|
||||
ins2(c, A_MOVQ, areg(D_BX), amem(D_BP, loff));
|
||||
ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, baseoff));
|
||||
} else {
|
||||
/* ident with unresolved type — legacy path,
|
||||
* unchanged (per-iteration base loads the
|
||||
* ident's own slot). */
|
||||
ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, loff));
|
||||
}
|
||||
}
|
||||
|
||||
char *loop = mklabel(c, "rloop");
|
||||
@@ -12092,6 +12132,10 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame)
|
||||
} else if (slc->kind == N_IDENT) {
|
||||
int boff = localfind(*locals, slc->str);
|
||||
ins2(c, A_MOVQ, amem(D_BP, boff), areg(D_BX));
|
||||
} else {
|
||||
/* #70: non-ident slice/str base — reload the spilled
|
||||
* data pointer (pre-#70 BX held the bound reload). */
|
||||
ins2(c, A_MOVQ, amem(D_BP, baseoff), areg(D_BX));
|
||||
}
|
||||
ins2(c, A_ADDQ, areg(D_AX), areg(D_BX));
|
||||
/* load each binding from BX + foff into its slot. C4 (F5/FC0,
|
||||
|
||||
@@ -33747,11 +33747,54 @@ fn cgforrange(c: *cgen, n: *node) void = {
|
||||
};
|
||||
let destruct: bool = (n.list != nil);
|
||||
|
||||
// .rgi (counter) + .rgl (length) scratch slots.
|
||||
// .rgi (counter) + .rgl (length) scratch slots. #70: a NON-IDENT
|
||||
// slice/str base (field chain, indexed element, call) also needs
|
||||
// a .rgb base spill — pre-#70 the init stored cgexpr's AX (the
|
||||
// DATA POINTER — a slice-valued cgexpr leaves AX=ptr, BX=len,
|
||||
// CX=cap) into .rgl, and the per-iteration code had no non-ident
|
||||
// base arm, so the bound-reload BX doubled as the base: i was
|
||||
// compared against the POINTER and walked off the end
|
||||
// (regex.finish, SEGV on the first non-empty charsets; empty
|
||||
// slices coincidentally exited on ptr==0 — latent since fold 1,
|
||||
// byte-id both stages). A non-ident ARRAY base is loud (rule 7):
|
||||
// its cgexpr shape is not the slice header.
|
||||
let iname: str = mkscratchname(c, "rgi");
|
||||
let lname: str = mkscratchname(c, "rgl");
|
||||
let ioff: i32 = localalloc(c, iname, 8, nil);
|
||||
let loff: i32 = localalloc(c, lname, 8, nil);
|
||||
let baseoff: i32 = 0;
|
||||
if (slc != nil) {
|
||||
if (slc.kind != nkind.N_IDENT) {
|
||||
let stu70: *tinfo = slc.type_: *tinfo;
|
||||
for (stu70 != nil && stu70.kind == tykind.TY_NAMED) {
|
||||
stu70 = stu70.under;
|
||||
};
|
||||
let arr70: bool = false;
|
||||
if (stu70 != nil) {
|
||||
if (stu70.kind == tykind.TY_ARRAY) {
|
||||
arr70 = true;
|
||||
};
|
||||
};
|
||||
if (arr70) {
|
||||
let m70: str = "for-range over a non-ident array base unwired (#70)\n";
|
||||
os.write(2, m70.ptr, m70.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
// #11: cgexpr on a slice DEREF (*p) does not deliver
|
||||
// the AX/BX/CX header convention the spill assumes
|
||||
// (the deref-spine load family) — keep it LOUD until
|
||||
// #11 wires the deref load.
|
||||
if (slc.kind == nkind.N_UN) {
|
||||
if (slc.op == tkind.TK_STAR) {
|
||||
let m11: str = "for-range over a deref base unwired (#11)\n";
|
||||
os.write(2, m11.ptr, m11.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
};
|
||||
let bname: str = mkscratchname(c, "rgb");
|
||||
baseoff = localalloc(c, bname, 8, nil);
|
||||
};
|
||||
};
|
||||
|
||||
// Per-binding (up to 8 — matches the C array). Parallel arrays so
|
||||
// we don't depend on local-struct cgen.
|
||||
@@ -33859,9 +33902,22 @@ fn cgforrange(c: *cgen, n: *node) void = {
|
||||
emitline("(BP)\n");
|
||||
} else {
|
||||
cgexpr(c, slc);
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff(loff: i64);
|
||||
emitline("(BP)\n");
|
||||
if (baseoff != 0) {
|
||||
// #70: slice/str header from cgexpr is AX=ptr,
|
||||
// BX=len, CX=cap — bound is LEN; spill the base ptr
|
||||
// for the per-iteration element address.
|
||||
emitline("\tMOVQ\tBX, ");
|
||||
emitoff(loff: i64);
|
||||
emitline("(BP)\n");
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff(baseoff: i64);
|
||||
emitline("(BP)\n");
|
||||
} else {
|
||||
// ident with unresolved type — legacy path, unchanged.
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff(loff: i64);
|
||||
emitline("(BP)\n");
|
||||
};
|
||||
};};
|
||||
|
||||
let loopl: str = mklabel(c, "rloop");
|
||||
@@ -33907,6 +33963,12 @@ fn cgforrange(c: *cgen, n: *node) void = {
|
||||
emitline("(BP), BX\n");
|
||||
};
|
||||
};
|
||||
} else {
|
||||
// #70: non-ident slice/str base — reload the spilled data
|
||||
// pointer (pre-#70 BX held the bound reload).
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(baseoff: i64);
|
||||
emitline("(BP), BX\n");
|
||||
};
|
||||
emitline("\tADDQ\tAX, BX\n");
|
||||
|
||||
|
||||
@@ -3305,11 +3305,54 @@ fn cgforrange(c: *cgen, n: *node) void = {
|
||||
};
|
||||
let destruct: bool = (n.list != nil);
|
||||
|
||||
// .rgi (counter) + .rgl (length) scratch slots.
|
||||
// .rgi (counter) + .rgl (length) scratch slots. #70: a NON-IDENT
|
||||
// slice/str base (field chain, indexed element, call) also needs
|
||||
// a .rgb base spill — pre-#70 the init stored cgexpr's AX (the
|
||||
// DATA POINTER — a slice-valued cgexpr leaves AX=ptr, BX=len,
|
||||
// CX=cap) into .rgl, and the per-iteration code had no non-ident
|
||||
// base arm, so the bound-reload BX doubled as the base: i was
|
||||
// compared against the POINTER and walked off the end
|
||||
// (regex.finish, SEGV on the first non-empty charsets; empty
|
||||
// slices coincidentally exited on ptr==0 — latent since fold 1,
|
||||
// byte-id both stages). A non-ident ARRAY base is loud (rule 7):
|
||||
// its cgexpr shape is not the slice header.
|
||||
let iname: str = mkscratchname(c, "rgi");
|
||||
let lname: str = mkscratchname(c, "rgl");
|
||||
let ioff: i32 = localalloc(c, iname, 8, nil);
|
||||
let loff: i32 = localalloc(c, lname, 8, nil);
|
||||
let baseoff: i32 = 0;
|
||||
if (slc != nil) {
|
||||
if (slc.kind != nkind.N_IDENT) {
|
||||
let stu70: *tinfo = slc.type_: *tinfo;
|
||||
for (stu70 != nil && stu70.kind == tykind.TY_NAMED) {
|
||||
stu70 = stu70.under;
|
||||
};
|
||||
let arr70: bool = false;
|
||||
if (stu70 != nil) {
|
||||
if (stu70.kind == tykind.TY_ARRAY) {
|
||||
arr70 = true;
|
||||
};
|
||||
};
|
||||
if (arr70) {
|
||||
let m70: str = "for-range over a non-ident array base unwired (#70)\n";
|
||||
os.write(2, m70.ptr, m70.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
// #11: cgexpr on a slice DEREF (*p) does not deliver
|
||||
// the AX/BX/CX header convention the spill assumes
|
||||
// (the deref-spine load family) — keep it LOUD until
|
||||
// #11 wires the deref load.
|
||||
if (slc.kind == nkind.N_UN) {
|
||||
if (slc.op == tkind.TK_STAR) {
|
||||
let m11: str = "for-range over a deref base unwired (#11)\n";
|
||||
os.write(2, m11.ptr, m11.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
};
|
||||
let bname: str = mkscratchname(c, "rgb");
|
||||
baseoff = localalloc(c, bname, 8, nil);
|
||||
};
|
||||
};
|
||||
|
||||
// Per-binding (up to 8 — matches the C array). Parallel arrays so
|
||||
// we don't depend on local-struct cgen.
|
||||
@@ -3417,9 +3460,22 @@ fn cgforrange(c: *cgen, n: *node) void = {
|
||||
emitline("(BP)\n");
|
||||
} else {
|
||||
cgexpr(c, slc);
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff(loff: i64);
|
||||
emitline("(BP)\n");
|
||||
if (baseoff != 0) {
|
||||
// #70: slice/str header from cgexpr is AX=ptr,
|
||||
// BX=len, CX=cap — bound is LEN; spill the base ptr
|
||||
// for the per-iteration element address.
|
||||
emitline("\tMOVQ\tBX, ");
|
||||
emitoff(loff: i64);
|
||||
emitline("(BP)\n");
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff(baseoff: i64);
|
||||
emitline("(BP)\n");
|
||||
} else {
|
||||
// ident with unresolved type — legacy path, unchanged.
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff(loff: i64);
|
||||
emitline("(BP)\n");
|
||||
};
|
||||
};};
|
||||
|
||||
let loopl: str = mklabel(c, "rloop");
|
||||
@@ -3465,6 +3521,12 @@ fn cgforrange(c: *cgen, n: *node) void = {
|
||||
emitline("(BP), BX\n");
|
||||
};
|
||||
};
|
||||
} else {
|
||||
// #70: non-ident slice/str base — reload the spilled data
|
||||
// pointer (pre-#70 BX held the bound reload).
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(baseoff: i64);
|
||||
emitline("(BP), BX\n");
|
||||
};
|
||||
emitline("\tADDQ\tAX, BX\n");
|
||||
|
||||
|
||||
@@ -33747,11 +33747,54 @@ fn cgforrange(c: *cgen, n: *node) void = {
|
||||
};
|
||||
let destruct: bool = (n.list != nil);
|
||||
|
||||
// .rgi (counter) + .rgl (length) scratch slots.
|
||||
// .rgi (counter) + .rgl (length) scratch slots. #70: a NON-IDENT
|
||||
// slice/str base (field chain, indexed element, call) also needs
|
||||
// a .rgb base spill — pre-#70 the init stored cgexpr's AX (the
|
||||
// DATA POINTER — a slice-valued cgexpr leaves AX=ptr, BX=len,
|
||||
// CX=cap) into .rgl, and the per-iteration code had no non-ident
|
||||
// base arm, so the bound-reload BX doubled as the base: i was
|
||||
// compared against the POINTER and walked off the end
|
||||
// (regex.finish, SEGV on the first non-empty charsets; empty
|
||||
// slices coincidentally exited on ptr==0 — latent since fold 1,
|
||||
// byte-id both stages). A non-ident ARRAY base is loud (rule 7):
|
||||
// its cgexpr shape is not the slice header.
|
||||
let iname: str = mkscratchname(c, "rgi");
|
||||
let lname: str = mkscratchname(c, "rgl");
|
||||
let ioff: i32 = localalloc(c, iname, 8, nil);
|
||||
let loff: i32 = localalloc(c, lname, 8, nil);
|
||||
let baseoff: i32 = 0;
|
||||
if (slc != nil) {
|
||||
if (slc.kind != nkind.N_IDENT) {
|
||||
let stu70: *tinfo = slc.type_: *tinfo;
|
||||
for (stu70 != nil && stu70.kind == tykind.TY_NAMED) {
|
||||
stu70 = stu70.under;
|
||||
};
|
||||
let arr70: bool = false;
|
||||
if (stu70 != nil) {
|
||||
if (stu70.kind == tykind.TY_ARRAY) {
|
||||
arr70 = true;
|
||||
};
|
||||
};
|
||||
if (arr70) {
|
||||
let m70: str = "for-range over a non-ident array base unwired (#70)\n";
|
||||
os.write(2, m70.ptr, m70.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
// #11: cgexpr on a slice DEREF (*p) does not deliver
|
||||
// the AX/BX/CX header convention the spill assumes
|
||||
// (the deref-spine load family) — keep it LOUD until
|
||||
// #11 wires the deref load.
|
||||
if (slc.kind == nkind.N_UN) {
|
||||
if (slc.op == tkind.TK_STAR) {
|
||||
let m11: str = "for-range over a deref base unwired (#11)\n";
|
||||
os.write(2, m11.ptr, m11.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
};
|
||||
let bname: str = mkscratchname(c, "rgb");
|
||||
baseoff = localalloc(c, bname, 8, nil);
|
||||
};
|
||||
};
|
||||
|
||||
// Per-binding (up to 8 — matches the C array). Parallel arrays so
|
||||
// we don't depend on local-struct cgen.
|
||||
@@ -33859,9 +33902,22 @@ fn cgforrange(c: *cgen, n: *node) void = {
|
||||
emitline("(BP)\n");
|
||||
} else {
|
||||
cgexpr(c, slc);
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff(loff: i64);
|
||||
emitline("(BP)\n");
|
||||
if (baseoff != 0) {
|
||||
// #70: slice/str header from cgexpr is AX=ptr,
|
||||
// BX=len, CX=cap — bound is LEN; spill the base ptr
|
||||
// for the per-iteration element address.
|
||||
emitline("\tMOVQ\tBX, ");
|
||||
emitoff(loff: i64);
|
||||
emitline("(BP)\n");
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff(baseoff: i64);
|
||||
emitline("(BP)\n");
|
||||
} else {
|
||||
// ident with unresolved type — legacy path, unchanged.
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff(loff: i64);
|
||||
emitline("(BP)\n");
|
||||
};
|
||||
};};
|
||||
|
||||
let loopl: str = mklabel(c, "rloop");
|
||||
@@ -33907,6 +33963,12 @@ fn cgforrange(c: *cgen, n: *node) void = {
|
||||
emitline("(BP), BX\n");
|
||||
};
|
||||
};
|
||||
} else {
|
||||
// #70: non-ident slice/str base — reload the spilled data
|
||||
// pointer (pre-#70 BX held the bound reload).
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(baseoff: i64);
|
||||
emitline("(BP), BX\n");
|
||||
};
|
||||
emitline("\tADDQ\tAX, BX\n");
|
||||
|
||||
|
||||
345
test/wcc/937_forrange_fieldbase_run.c
Normal file
345
test/wcc/937_forrange_fieldbase_run.c
Normal file
@@ -0,0 +1,345 @@
|
||||
/*
|
||||
* 937_forrange_fieldbase_run — #70: by-value for-range over a NON-IDENT
|
||||
* slice/str base (field chain, indexed element, call result). Pre-#70
|
||||
* the range header stored cgexpr's AX — the DATA POINTER (slice cgexpr
|
||||
* leaves AX=ptr, BX=len, CX=cap) — into the single bound temp, and the
|
||||
* per-iteration code had no non-ident base arm, so the bound reload
|
||||
* doubled as the base: i was compared against the POINTER and the loop
|
||||
* walked off the end (regex.finish, SEGV on the first non-empty
|
||||
* charsets; empty slices coincidentally exited on ptr==0 — latent since
|
||||
* fold 1, BYTE-ID both stages, so the 989 M_ID gate held on
|
||||
* both-wrong-identical). Now: bound = len, base ptr spilled to its own
|
||||
* .rgb slot. Deref bases (*p — the #11 family, cgexpr does NOT deliver
|
||||
* the header) and non-ident ARRAY bases stay LOUD.
|
||||
*
|
||||
* row | shape | want
|
||||
* -------------------+----------------------------------------+-----
|
||||
* field_base | h.xs ([]i64) value + ptr root | 0
|
||||
* field_base_24B | p.names ([]str) — the finish() shape | 0
|
||||
* indexed_base | m[0] ([][]i64 element) | 0
|
||||
* call_base | mk() ([]i64 call result) | 0
|
||||
* empty_field | zero header field exits cleanly | 0
|
||||
* eval_once | base reassigned INSIDE the loop — |
|
||||
* | header captured once (a per-iteration |
|
||||
* | re-read of the field would mis-sum) | 0
|
||||
* reject_deref | *q base stays loud (#11) | err
|
||||
* reject_arr_field | h.arr ([3]i64 field) stays loud | err
|
||||
*
|
||||
* Every K_RUN row also asserts cstage/wwstage asm byte-id. NNN<950,
|
||||
* self-contained (/tmp, no imports).
|
||||
*/
|
||||
#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;
|
||||
}
|
||||
|
||||
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), cb = fgetc(fb);
|
||||
if (ca != cb) { rc = -1; break; }
|
||||
if (ca == EOF) break;
|
||||
}
|
||||
fclose(fa); fclose(fb);
|
||||
return rc;
|
||||
}
|
||||
|
||||
#define K_RUN 0 /* build+run both drivers, exit==want, + cs==ww byte-id */
|
||||
#define K_BUILDERR 1 /* build must FAIL with experr on BOTH drivers (rule 7) */
|
||||
|
||||
struct row { const char *label; const char *src; int want;
|
||||
int kind; const char *experr; };
|
||||
|
||||
/* errlog_has — a BUILDERR row must fail WITH its diagnostic; any other
|
||||
* failure (parse error, crash) is a vacuous reject (940 precedent). */
|
||||
static int
|
||||
errlog_has(const char *path, const char *needle)
|
||||
{
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return 0;
|
||||
char buf[8192];
|
||||
size_t got = fread(buf, 1, sizeof buf - 1, f);
|
||||
fclose(f);
|
||||
buf[got] = '\0';
|
||||
return strstr(buf, needle) != NULL;
|
||||
}
|
||||
|
||||
static const struct row rows[] = {
|
||||
{ "field_base",
|
||||
"package main;\n"
|
||||
"type holder = struct { xs: []i64, name: str };\n"
|
||||
"fn mk() []i64 = {\n"
|
||||
" let xs: []i64;\n"
|
||||
" append(xs, 7);\n"
|
||||
" append(xs, 8);\n"
|
||||
" return xs;\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let h: holder = holder { xs = mk(), name = \"h\" };\n"
|
||||
" let s1: i64 = 0;\n"
|
||||
" for (let v .. h.xs) { s1 += v; };\n"
|
||||
" if (s1 != 15) { return 1; };\n"
|
||||
" let p: *holder = &h;\n"
|
||||
" let s2: i64 = 0;\n"
|
||||
" for (let v .. p.xs) { s2 += v; };\n"
|
||||
" if (s2 != 15) { return 2; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0, K_RUN, NULL },
|
||||
/* 24B str-header elements through a ptr field — regex.finish's
|
||||
* exact shape (the live SEGV repro). */
|
||||
{ "field_base_24B",
|
||||
"package main;\n"
|
||||
"type bag = struct { names: []str, n: i64 };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let names: []str;\n"
|
||||
" append(names, \"ab\");\n"
|
||||
" append(names, \"cde\");\n"
|
||||
" let b: bag = bag { names = names, n = 2 };\n"
|
||||
" let p: *bag = &b;\n"
|
||||
" let total: i64 = 0;\n"
|
||||
" for (let nm .. p.names) {\n"
|
||||
" total += (len(nm): i64);\n"
|
||||
" };\n"
|
||||
" if (total != 5) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0, K_RUN, NULL },
|
||||
/* indexed element base — the task-#57 shape, graduated by #70 */
|
||||
{ "indexed_base",
|
||||
"package main;\n"
|
||||
"fn mk() []i64 = {\n"
|
||||
" let xs: []i64;\n"
|
||||
" append(xs, 7);\n"
|
||||
" append(xs, 8);\n"
|
||||
" return xs;\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let m: [][]i64;\n"
|
||||
" append(m, mk());\n"
|
||||
" let s: i64 = 0;\n"
|
||||
" for (let v .. m[0]) { s += v; };\n"
|
||||
" if (s != 15) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0, K_RUN, NULL },
|
||||
{ "call_base",
|
||||
"package main;\n"
|
||||
"fn mk() []i64 = {\n"
|
||||
" let xs: []i64;\n"
|
||||
" append(xs, 7);\n"
|
||||
" append(xs, 8);\n"
|
||||
" return xs;\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let s: i64 = 0;\n"
|
||||
" for (let v .. mk()) { s += v; };\n"
|
||||
" if (s != 15) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0, K_RUN, NULL },
|
||||
/* zero header: the pre-#70 code exited only because ptr==0; the
|
||||
* fixed code must exit because len==0. */
|
||||
{ "empty_field",
|
||||
"package main;\n"
|
||||
"type holder = struct { xs: []i64, name: str };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let h: holder = holder { name = \"e\", ... };\n"
|
||||
" for (let v .. h.xs) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0, K_RUN, NULL },
|
||||
/* the range header (base ptr + len) is captured ONCE at loop
|
||||
* entry — Hare evaluates the range operand once. A fix that
|
||||
* re-read the field per iteration would walk b (sum 7+100) and
|
||||
* never see the reassign-then-original discipline. */
|
||||
{ "eval_once",
|
||||
"package main;\n"
|
||||
"type holder = struct { xs: []i64, n: i64 };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let a: []i64;\n"
|
||||
" append(a, 7);\n"
|
||||
" append(a, 8);\n"
|
||||
" let b: []i64;\n"
|
||||
" append(b, 100);\n"
|
||||
" let h: holder = holder { xs = a, n = 0 };\n"
|
||||
" let s: i64 = 0;\n"
|
||||
" for (let v .. h.xs) {\n"
|
||||
" s += v;\n"
|
||||
" h.xs = b;\n"
|
||||
" };\n"
|
||||
" if (s != 15) { return 1; };\n"
|
||||
" if (len(h.xs) != 1) { return 2; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0, K_RUN, NULL },
|
||||
/* deref base: cgexpr on *p does not deliver the AX/BX/CX header
|
||||
* (the #11 deref-spine family) — pre-#70 it crashed or mis-summed
|
||||
* SILENTLY; now loud both stages. */
|
||||
{ "reject_deref",
|
||||
"package main;\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let xs: []i64;\n"
|
||||
" append(xs, 1);\n"
|
||||
" let q: *[]i64 = &xs;\n"
|
||||
" let s: i64 = 0;\n"
|
||||
" for (let v .. *q) { s += v; };\n"
|
||||
" return (s: i32);\n"
|
||||
"};\n", 0,
|
||||
K_BUILDERR, "for-range over a deref base unwired (#11)" },
|
||||
/* non-ident ARRAY base: no base spill and a different cgexpr
|
||||
* register shape — loud (rule 7) until a consumer wires it. */
|
||||
{ "reject_arr_field",
|
||||
"package main;\n"
|
||||
"type holder = struct { arr: [3]i64, n: i64 };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let h: holder = holder { arr = [1, 2, 3], n = 0 };\n"
|
||||
" let s: i64 = 0;\n"
|
||||
" for (let v .. h.arr) { s += v; };\n"
|
||||
" return (s: i32);\n"
|
||||
"};\n", 0,
|
||||
K_BUILDERR, "for-range over a non-ident array base unwired (#70)" },
|
||||
};
|
||||
|
||||
/* build+run via a driver (ww / ww_ww); returns 0 pass, nonzero fail. */
|
||||
static int
|
||||
run_driver(const char *driver, const struct row *r, int i)
|
||||
{
|
||||
char src[96], tmpdir[96], errf[96], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/tfr_%d_%d.ww", getpid(), i);
|
||||
snprintf(tmpdir, sizeof tmpdir, "/tmp/tfr_%d_d_%d", getpid(), i);
|
||||
snprintf(errf, sizeof errf, "/tmp/tfr_%d_e_%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 >/dev/null 2>%s",
|
||||
tmpdir, driver, src, errf);
|
||||
int brc = runwait(cmd);
|
||||
if (r->kind == K_BUILDERR) {
|
||||
int ok = (brc != 0)
|
||||
&& (r->experr == NULL || errlog_has(errf, r->experr));
|
||||
if (!ok)
|
||||
fprintf(stderr, "row[%s]: %s expected loud builderr "
|
||||
"\"%s\" (brc=%d)\n", r->label, driver,
|
||||
r->experr ? r->experr : "", brc);
|
||||
unlink(src); unlink(errf); rmdir(tmpdir);
|
||||
return ok ? 0 : 1;
|
||||
}
|
||||
if (brc != 0) {
|
||||
fprintf(stderr, "row[%s]: build via %s failed\n",
|
||||
r->label, driver);
|
||||
unlink(src); unlink(errf); rmdir(tmpdir);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *base = strrchr(src, '/');
|
||||
base = base ? base + 1 : src;
|
||||
char outbin[256];
|
||||
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); unlink(errf); rmdir(tmpdir);
|
||||
if (got != r->want) {
|
||||
fprintf(stderr, "row[%s]: %s exit %d, want %d\n",
|
||||
r->label, driver, got, r->want);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* cs==ww .s byte-id (rule 10). */
|
||||
static int
|
||||
asm_byte_identical(const char *bin, const struct row *r, int i)
|
||||
{
|
||||
char src[96], cs[96], ws[96], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/tfr_asm_%d_%d.ww", getpid(), i);
|
||||
snprintf(cs, sizeof cs, "/tmp/tfr_asm_%d_%d_c.s", getpid(), i);
|
||||
snprintf(ws, sizeof ws, "/tmp/tfr_asm_%d_%d_w.s", getpid(), i);
|
||||
|
||||
FILE *f = fopen(src, "wb");
|
||||
if (!f) return -1;
|
||||
fputs(r->src, f);
|
||||
fclose(f);
|
||||
|
||||
snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: w6c errored\n", r->label);
|
||||
unlink(src);
|
||||
return -1;
|
||||
}
|
||||
snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null",
|
||||
bin, ws, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: w6c_ww errored\n", r->label);
|
||||
unlink(src); unlink(cs);
|
||||
return -1;
|
||||
}
|
||||
int rc = slurp_eq(cs, ws);
|
||||
if (rc != 0)
|
||||
fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n",
|
||||
r->label);
|
||||
unlink(src); unlink(cs); unlink(ws);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const char *bin = getenv("BIN");
|
||||
if (!bin) bin = "out/bin";
|
||||
char absbin[2080];
|
||||
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[2120], wdrv[2120];
|
||||
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
||||
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
|
||||
|
||||
int n = (int)(sizeof rows / sizeof rows[0]);
|
||||
int total = 0, fail = 0;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
total++;
|
||||
if (run_driver(cdrv, &rows[i], i) != 0) fail++;
|
||||
}
|
||||
if (access(wdrv, X_OK) == 0) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
total++;
|
||||
if (run_driver(wdrv, &rows[i], i) != 0) fail++;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (rows[i].kind == K_BUILDERR)
|
||||
continue;
|
||||
total++;
|
||||
if (asm_byte_identical(bin, &rows[i], i) != 0) fail++;
|
||||
}
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
fprintf(stderr, "forrange_fieldbase: %d/%d checks failed\n",
|
||||
fail, total);
|
||||
return 1;
|
||||
}
|
||||
printf("forrange_fieldbase: %d/%d ok\n", total, total);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user