w6c+wwstage: agree on mixed-scalar tuple sret layout (#240)
An over-cap tuple mixing a scalar with slices/str (e.g. (int,[]u8,str),
56B) laid out differently in the two stages — gate-blind, since no
bootstrap path returns such a tuple. Two silent cs!=ww bugs, one per
ABI side:
- callee SEND (cstage cgen.c N_RETURN over-cap-tuple arm): foff
advanced by the LITERAL expression's type size. A bare int literal
element is stamped TY_UNTYPED_INT (size 0), so `e->type->size`
added 0 for a leading scalar — the next element clobbered it at
offset 0 and every trailing element packed 8 bytes low. wwstage
already sized from the return-type tuple (c.fnret.list), so the
callee frames diverged. Fix: size foff from cg_ret_type's tuple
params (rule-13 type table), aligning cstage to wwstage and to the
t.N reader's f->offset.
- caller RECEIVE (wwstage cgenstmt.ww cglet N_TTUPLE arm): the
in-cap register tuple-receive branch had no capacity gate, so a
56B over-cap tuple was received via AX/DX/CX/R8 (+ R8 fill)
instead of from the sret dest the callee wrote. cstage gates the
twin branch on `sz == 16 || sz == 32` and falls over-cap tuples
through to the sret receive. Fix: add the same size gate to
wwstage, aligning it to cstage.
Both stages now emit byte-identical asm and the value round-trips.
Regen w6c + wwdump combined.ww (cgenstmt embeds in both).
New 940_mixed_scalar_tuple_sret_run: leading/trailing/middle scalar
shapes, annotated + inferred let, each self-asserting every element
(scalar direct, slice/str via len) — both drivers exit 0 + cs==ww
byte-id (12/12).
This commit is contained in:
10
Makefile
10
Makefile
@@ -290,6 +290,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
|
||||
$(BIN)/test_deref_slice_store_run \
|
||||
$(BIN)/test_tuple_nary_destructure_run \
|
||||
$(BIN)/test_overcap_tuple_field_store_run \
|
||||
$(BIN)/test_mixed_scalar_tuple_sret_run \
|
||||
$(BIN)/test_tuple_elem_slice_len_run \
|
||||
$(BIN)/test_str_forrange_loopvar_run \
|
||||
$(BIN)/test_composite_call_arg \
|
||||
@@ -1177,6 +1178,15 @@ $(BIN)/test_tuple_elem_slice_len_run: test/wcc/903_tuple_elem_slice_len_run.c \
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
# #240: mixed-scalar tuple (e.g. (int,[]u8,str)) sret layout agrees in both
|
||||
# stages — callee SEND foff via the return-type element size, caller RECEIVE
|
||||
# falls an over-cap tuple through to the sret receive (size-gated).
|
||||
$(BIN)/test_mixed_scalar_tuple_sret_run: test/wcc/940_mixed_scalar_tuple_sret_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 $@ $<
|
||||
|
||||
@@ -8839,11 +8839,27 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame)
|
||||
* wide element's cgexpr clobbers AX/BX/CX. Then reuse
|
||||
* the struct-sret epilogue. The CALL/receive side
|
||||
* stays loud-stopped (#10 Fold B). */
|
||||
/* #240: foff advances by the DECLARED return-type
|
||||
* element size (cg_ret_type tuple params), NOT the
|
||||
* literal expression's type. A bare int literal
|
||||
* element is stamped TY_UNTYPED_INT (size 0), so
|
||||
* `e->type->size` collapsed foff to 0 for a leading
|
||||
* scalar — the next element then clobbered it at
|
||||
* offset 0 and every trailing element packed 8 bytes
|
||||
* low, diverging from the t.N reader (f->offset) and
|
||||
* from wwstage (cgenstmt.ww walks c.fnret.list). */
|
||||
Type *rtt = type_chase_named(cg_ret_type);
|
||||
Tparam *pp = (rtt && rtt->kind == TY_TUPLE)
|
||||
? rtt->params : NULL;
|
||||
int foff = 0;
|
||||
for (Node *e = n->lhs->list; e; e = e->next) {
|
||||
int isflt = fld_isfloat(e->type, &f32);
|
||||
int wide = node_isstr(e) || node_isslice(e);
|
||||
int esz = e->type ? (int)e->type->size : 8;
|
||||
int esz = 8;
|
||||
if (pp && pp->type)
|
||||
esz = (int)pp->type->size;
|
||||
else if (e->type)
|
||||
esz = (int)e->type->size;
|
||||
cgexpr(c, e, *locals);
|
||||
ins2(c, A_MOVQ,
|
||||
amem(D_BP, cg_sret_arg_off), areg(D_DX));
|
||||
@@ -8861,6 +8877,7 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame)
|
||||
ins2(c, fldstoreop(e->type, esz),
|
||||
areg(D_AX), amem(D_DX, foff));
|
||||
foff += esz;
|
||||
if (pp) pp = pp->next;
|
||||
}
|
||||
ins2(c, A_MOVQ, amem(D_BP, cg_sret_arg_off),
|
||||
areg(D_AX));
|
||||
|
||||
@@ -27296,6 +27296,14 @@ fn cglet(c: *cgen, n: *node) void = {
|
||||
// positional slot (eoff steps by the element's slot size: a
|
||||
// slice/str takes its 24B header). str IS []u8 (24B) → 32B tuple
|
||||
// (#1/Phase 3, task #5). Mirror of the cstage unified branch.
|
||||
//
|
||||
// #240: cap-gate on sz (16/32 = in-cap, AX/DX/CX/R8). Without it
|
||||
// an OVER-cap mixed-scalar tuple (e.g. (int,[]u8,str), 56B) was
|
||||
// received here via the register cursor (4 GP regs + R8 fill)
|
||||
// instead of from the sret dest the callee actually wrote — a
|
||||
// silent cs!=ww divergence (cstage gates the twin branch on
|
||||
// `sz == 16 || sz == 32`, cgen.c N_LET, and falls an over-cap
|
||||
// tuple through to the sret receive below).
|
||||
if (n.lhs != nil) {
|
||||
if (n.lhs.kind == nkind.N_TTUPLE) {
|
||||
let p0: *node = n.lhs.list;
|
||||
@@ -27311,7 +27319,7 @@ fn cglet(c: *cgen, n: *node) void = {
|
||||
|| isslicetype(c, p1t);
|
||||
if (p0 != nil) {
|
||||
if (p1 != nil) {
|
||||
if (s0_is_str != s1_is_str) {
|
||||
if ((s0_is_str != s1_is_str) && (sz == 16 || sz == 32)) {
|
||||
cgexpr(c, rhs);
|
||||
let gpcur: i32 = 0;
|
||||
let ssecur: i32 = 0;
|
||||
|
||||
@@ -1084,6 +1084,14 @@ fn cglet(c: *cgen, n: *node) void = {
|
||||
// positional slot (eoff steps by the element's slot size: a
|
||||
// slice/str takes its 24B header). str IS []u8 (24B) → 32B tuple
|
||||
// (#1/Phase 3, task #5). Mirror of the cstage unified branch.
|
||||
//
|
||||
// #240: cap-gate on sz (16/32 = in-cap, AX/DX/CX/R8). Without it
|
||||
// an OVER-cap mixed-scalar tuple (e.g. (int,[]u8,str), 56B) was
|
||||
// received here via the register cursor (4 GP regs + R8 fill)
|
||||
// instead of from the sret dest the callee actually wrote — a
|
||||
// silent cs!=ww divergence (cstage gates the twin branch on
|
||||
// `sz == 16 || sz == 32`, cgen.c N_LET, and falls an over-cap
|
||||
// tuple through to the sret receive below).
|
||||
if (n.lhs != nil) {
|
||||
if (n.lhs.kind == nkind.N_TTUPLE) {
|
||||
let p0: *node = n.lhs.list;
|
||||
@@ -1099,7 +1107,7 @@ fn cglet(c: *cgen, n: *node) void = {
|
||||
|| isslicetype(c, p1t);
|
||||
if (p0 != nil) {
|
||||
if (p1 != nil) {
|
||||
if (s0_is_str != s1_is_str) {
|
||||
if ((s0_is_str != s1_is_str) && (sz == 16 || sz == 32)) {
|
||||
cgexpr(c, rhs);
|
||||
let gpcur: i32 = 0;
|
||||
let ssecur: i32 = 0;
|
||||
|
||||
@@ -27296,6 +27296,14 @@ fn cglet(c: *cgen, n: *node) void = {
|
||||
// positional slot (eoff steps by the element's slot size: a
|
||||
// slice/str takes its 24B header). str IS []u8 (24B) → 32B tuple
|
||||
// (#1/Phase 3, task #5). Mirror of the cstage unified branch.
|
||||
//
|
||||
// #240: cap-gate on sz (16/32 = in-cap, AX/DX/CX/R8). Without it
|
||||
// an OVER-cap mixed-scalar tuple (e.g. (int,[]u8,str), 56B) was
|
||||
// received here via the register cursor (4 GP regs + R8 fill)
|
||||
// instead of from the sret dest the callee actually wrote — a
|
||||
// silent cs!=ww divergence (cstage gates the twin branch on
|
||||
// `sz == 16 || sz == 32`, cgen.c N_LET, and falls an over-cap
|
||||
// tuple through to the sret receive below).
|
||||
if (n.lhs != nil) {
|
||||
if (n.lhs.kind == nkind.N_TTUPLE) {
|
||||
let p0: *node = n.lhs.list;
|
||||
@@ -27311,7 +27319,7 @@ fn cglet(c: *cgen, n: *node) void = {
|
||||
|| isslicetype(c, p1t);
|
||||
if (p0 != nil) {
|
||||
if (p1 != nil) {
|
||||
if (s0_is_str != s1_is_str) {
|
||||
if ((s0_is_str != s1_is_str) && (sz == 16 || sz == 32)) {
|
||||
cgexpr(c, rhs);
|
||||
let gpcur: i32 = 0;
|
||||
let ssecur: i32 = 0;
|
||||
|
||||
262
test/wcc/940_mixed_scalar_tuple_sret_run.c
Normal file
262
test/wcc/940_mixed_scalar_tuple_sret_run.c
Normal file
@@ -0,0 +1,262 @@
|
||||
/*
|
||||
* 940_mixed_scalar_tuple_sret_run — project #240: an over-cap tuple whose
|
||||
* elements mix a scalar with slices/str (e.g. `(int, []u8, str)`) must lay
|
||||
* out IDENTICALLY in both stages and round-trip through an sret return.
|
||||
*
|
||||
* Two SILENT, gate-blind (no bootstrap mixed-scalar tuple) cs!=ww
|
||||
* divergences met here:
|
||||
*
|
||||
* (a) callee SEND (cstage cgen.c N_RETURN over-cap-tuple arm): the packed
|
||||
* element offset advanced by the LITERAL expression's type size, not
|
||||
* the declared return-type element size. A bare int literal element is
|
||||
* stamped TY_UNTYPED_INT (size 0), so `foff += e->type->size` added 0
|
||||
* for a leading scalar — the next element clobbered it at offset 0 and
|
||||
* every trailing element packed 8 bytes low. wwstage already walked the
|
||||
* return-type tuple (c.fnret.list) for the size, so the stages diverged
|
||||
* on the callee frame's store offsets. Fix: size foff from cg_ret_type's
|
||||
* tuple params (rule-13 type table), mirroring wwstage.
|
||||
*
|
||||
* (b) caller RECEIVE (wwstage cgenstmt.ww cglet N_TTUPLE arm): the register
|
||||
* tuple-receive branch (designed for in-cap 16/32B tuples) had NO
|
||||
* capacity gate, so a 56B over-cap tuple was received via AX/DX/CX/R8
|
||||
* (+ R8 fill) instead of from the sret dest the callee actually wrote.
|
||||
* cstage gates that branch on `sz == 16 || sz == 32` and falls an
|
||||
* over-cap tuple through to the sret receive. Fix: add the same size
|
||||
* gate to wwstage.
|
||||
*
|
||||
* Each program assigns DISTINCT, checkable values to every element (a scalar
|
||||
* read directly, a slice/str via len()), and returns 0 only when all read
|
||||
* back correctly — so a wrong offset (either bug) yields a nonzero exit.
|
||||
* Both an annotated and an inferred `let t` are covered (same receive path).
|
||||
*
|
||||
* All K_RUN: build+run exit 0 on BOTH drivers AND cs==ww byte-identical.
|
||||
* NNN<950, self-contained (/tmp, no imports), so rule-14's selfhost-sibling
|
||||
* race does not apply (903/940/945 precedent).
|
||||
*/
|
||||
#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;
|
||||
}
|
||||
|
||||
struct row { const char *label; const char *src; int want; };
|
||||
|
||||
static const struct row rows[] = {
|
||||
/* leading scalar (the canonical #240 case), annotated let. */
|
||||
{ "int_slice_str",
|
||||
"package main;\n"
|
||||
"fn mk() (int, []u8, str) = {\n"
|
||||
" let b: []u8; b.len = 5; b.cap = 9;\n"
|
||||
" let s: str = \"abcd\";\n"
|
||||
" return (42, b, s);\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let t: (int, []u8, str) = mk();\n"
|
||||
" if (t.0 != 42) { return 1; };\n"
|
||||
" if (len(t.1) != 5) { return 2; };\n"
|
||||
" if (len(t.2) != 4) { return 3; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0 },
|
||||
/* same shape, inferred `let t = mk();` — exercises the receive
|
||||
* path that picks up the type from the callee return. */
|
||||
{ "int_slice_str_inferred",
|
||||
"package main;\n"
|
||||
"fn mk() (int, []u8, str) = {\n"
|
||||
" let b: []u8; b.len = 6; b.cap = 8;\n"
|
||||
" let s: str = \"xyz\";\n"
|
||||
" return (17, b, s);\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let t = mk();\n"
|
||||
" if (t.0 != 17) { return 1; };\n"
|
||||
" if (len(t.1) != 6) { return 2; };\n"
|
||||
" if (len(t.2) != 3) { return 3; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0 },
|
||||
/* trailing scalar: foff must reach the last element's offset. */
|
||||
{ "slice_str_int",
|
||||
"package main;\n"
|
||||
"fn mk() ([]u8, str, int) = {\n"
|
||||
" let b: []u8; b.len = 7; b.cap = 9;\n"
|
||||
" let s: str = \"hello\";\n"
|
||||
" return (b, s, 99);\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let t: ([]u8, str, int) = mk();\n"
|
||||
" if (len(t.0) != 7) { return 1; };\n"
|
||||
" if (len(t.1) != 5) { return 2; };\n"
|
||||
" if (t.2 != 99) { return 3; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0 },
|
||||
/* scalar in the middle. */
|
||||
{ "str_int_slice",
|
||||
"package main;\n"
|
||||
"fn mk() (str, int, []u8) = {\n"
|
||||
" let s: str = \"abcde\";\n"
|
||||
" let b: []u8; b.len = 3; b.cap = 4;\n"
|
||||
" return (s, 77, b);\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let t: (str, int, []u8) = mk();\n"
|
||||
" if (len(t.0) != 5) { return 1; };\n"
|
||||
" if (t.1 != 77) { return 2; };\n"
|
||||
" if (len(t.2) != 3) { return 3; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0 },
|
||||
};
|
||||
|
||||
/* 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/mxst_%d_%d.ww", getpid(), i);
|
||||
snprintf(tmpdir, sizeof tmpdir, "/tmp/mxst_%d_d_%d", getpid(), i);
|
||||
snprintf(errf, sizeof errf, "/tmp/mxst_%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 (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
|
||||
byteid(const char *w6c, const char *w6c_ww, const struct row *r, int i)
|
||||
{
|
||||
char src[96], cs_s[96], ws_s[96], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/mxst_bi_%d_%d.ww", getpid(), i);
|
||||
snprintf(cs_s, sizeof cs_s, "/tmp/mxst_bi_%d_%d_cs.s", getpid(), i);
|
||||
snprintf(ws_s, sizeof ws_s, "/tmp/mxst_bi_%d_%d_ww.s", getpid(), i);
|
||||
|
||||
FILE *f = fopen(src, "wb");
|
||||
if (!f) return -1;
|
||||
fputs(r->src, f);
|
||||
fclose(f);
|
||||
|
||||
int rc = 0;
|
||||
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", r->label); rc = 1; }
|
||||
else {
|
||||
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", r->label); rc = 1; }
|
||||
else if (slurp_eq(cs_s, ws_s) != 0) {
|
||||
fprintf(stderr, "row[%s]: cstage/wwstage .s DIFFER "
|
||||
"(#240 mixed-scalar tuple sret-layout regression)\n",
|
||||
r->label);
|
||||
rc = 1;
|
||||
}
|
||||
}
|
||||
unlink(src); unlink(cs_s); unlink(ws_s);
|
||||
return rc;
|
||||
}
|
||||
|
||||
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], wdrv[640], w6c[640], w6c_ww[640];
|
||||
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
||||
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
|
||||
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
|
||||
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
|
||||
|
||||
struct { const char *name; const char *path; int gated; }
|
||||
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 && access(drivers[d].path, X_OK) != 0) {
|
||||
fprintf(stderr, "mixed_scalar_tuple_sret: skip %s (no %s)\n",
|
||||
drivers[d].name, drivers[d].path);
|
||||
continue;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
total++;
|
||||
if (run_driver(drivers[d].path, &rows[i], i) != 0) fail++;
|
||||
}
|
||||
}
|
||||
|
||||
if (access(w6c_ww, X_OK) == 0) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
total++;
|
||||
if (byteid(w6c, w6c_ww, &rows[i], i) != 0) fail++;
|
||||
}
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
fprintf(stderr, "mixed_scalar_tuple_sret: %d/%d checks failed\n",
|
||||
fail, total);
|
||||
return 1;
|
||||
}
|
||||
printf("mixed_scalar_tuple_sret: %d/%d ok\n", total, total);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user