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).
263 lines
8.1 KiB
C
263 lines
8.1 KiB
C
/*
|
|
* 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;
|
|
}
|