Files
ww/test/wcc/941_tuple_slot_layout_run.c
Hojun-Cho fdfc2ce318 wcc+w6c+w6c_ww: tuple slot layout SSoT — checker size = cgen slot stride (C-t0)
The checker computed TY_TUPLE size as the packed element-size sum
((u32,u32) = 8B) while every cgen cursor-transport site strode 8B
slots (16B). 16B tuples were blind to the split (slot == packed);
packed tuples hit it everywhere: cstage let-receive keyed on sz 16/32
missed sz 8 and dropped word 1, the cgfn param receive spilled
8B/element into a packed-sized local (saved-BP clobber, SIGSEGV), and
mixed (u32,f64)/(u32,str) shapes missed the receive arms entirely.

Slot layout is now the SSoT (user-ratified): the flip lives in the two
checkers' N_TTUPLE size computation only (check.c, check.ww
tupleelemslot + stamp); cgen's packed-keyed walks (t.N read, #235 len
arm, over-cap sret send/receive pair) align onto the slot stride, and
the wwstage t.N read gains the natural-width load (tnodeloadop) to
byte-id with cstage's fldloadop. ttupleelem.offset re-stamped
slot-cumulative (no consumers yet). The #242/#243 eightbyte-share
loud-stop dissolves by construction (no two narrows ever share an
eightbyte) — 940's eightbyte_share row graduates to a runtime
round-trip. Hare-layout divergence documented at both checker sites;
re-alignment is task #60. #32 send skew and #33 wwstage literal-let
receive are separate commits on this base.

941_tuple_slot_layout_run pins the matrix: 4 packed rows fail at the
parent (8/21 checks), 3 neutral anchors prove 16B/32B emission
untouched.
2026-06-04 19:06:54 +09:00

288 lines
8.5 KiB
C

/*
* 941_tuple_slot_layout_run — tuple arc C-t0: SLOT layout is the tuple
* SSoT (user-ratified). The checker's TY_TUPLE size counts the stride
* cgen's cursor transport actually writes — a str/slice its 24B header,
* everything else (narrow scalars included) one 8B eightbyte — and the
* t.N read walks stride the same slots.
*
* Pre-C-t0 there were TWO answers: the checker summed PACKED element
* sizes ((u32,u32) = 8B) while every cursor transport site strode 8B
* slots (16B). The 16B shapes were blind to the split (slot == packed);
* the packed shapes hit it everywhere:
* - `let t: (u32,u32) = f()` — cstage's receive keys on the checker
* size (16/32), so sz=8 fell to the generic single-word store
* (word 1 DROPPED) and the packed read walk then read the wrong
* offsets. wwstage (slot-keyed throughout) was runtime-CORRECT
* here — inverted polarity vs the 16B #33 map, gate-blind.
* - a (u32,u32) by-value param: cgfn's receive walk spilled
* 8B/element into a local sized from the PACKED checker size (8B)
* — word 1 landed on the saved-BP slot, frame corruption, SIGSEGV.
* - a mixed (u32,f64)/(u32,str) tuple: packed sz (12/28) missed the
* 16/32 receive arms entirely — silent drop on both stages.
*
* The send-side ABI skew (#32, C-t2) and the wwstage literal-let
* receive (#33, C-t1) are SEPARATE bugs pinned by their own commits;
* this test pins the layout unification only:
*
* row | shape | want
* ---------------------+--------------------------------------+-----
* letcall_packed | let t:(u32,u32)=f(); t.0/t.1 | 0
* letcall_float_packed | (u32,f64) from call, X0 element | 0
* letcall_signed_packed| (i32,i32) negatives — MOVSXD reads | 0
* letcall_mixed_packed | (u32,str): 28B packed missed the 32B |
* | arm; len(t.1) strides a full slot | 0
* len_elem_packed | len(t.1) of (u32,str) — #235 arm's |
* | slot stride (was BP+4+8) | (in mixed row)
* letcall_16_neutral | (i64,i64) — slot==packed, anchor | 0
* letcall_32_neutral | (i64,str) — 32B arm, anchor | 0
* mlet_packed_neutral | let (a,b) = f() packed — per-element |
* | locals, layout-independent anchor | 0
*
* Every row also asserts cstage/wwstage asm byte-id.
* NNN<950, self-contained (/tmp, no imports) — 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[] = {
{ "letcall_packed",
"package main;\n"
"fn f() (u32, u32) = {\n"
" return (3, 4);\n"
"};\n"
"export fn main() i32 = {\n"
" let t: (u32, u32) = f();\n"
" if (t.0 != 3) { return 1; };\n"
" if (t.1 != 4) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "letcall_float_packed",
"package main;\n"
"fn f() (u32, f64) = {\n"
" let a: u32 = 9;\n"
" let x: f64 = 2.5;\n"
" return (a, x);\n"
"};\n"
"export fn main() i32 = {\n"
" let t: (u32, f64) = f();\n"
" if (t.0 != 9) { return 1; };\n"
" if (t.1 != 2.5) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "letcall_signed_packed",
"package main;\n"
"fn f() (i32, i32) = {\n"
" let a: i32 = -5;\n"
" let b: i32 = -6;\n"
" return (a, b);\n"
"};\n"
"export fn main() i32 = {\n"
" let t: (i32, i32) = f();\n"
" if (t.0 != -5) { return 1; };\n"
" if (t.1 != -6) { return 2; };\n"
" if (t.0 + t.1 != -11) { return 3; };\n"
" return 0;\n"
"};\n", 0 },
{ "letcall_mixed_packed",
"package main;\n"
"fn f() (u32, str) = {\n"
" let a: u32 = 7;\n"
" let s: str = \"hello\";\n"
" return (a, s);\n"
"};\n"
"export fn main() i32 = {\n"
" let t: (u32, str) = f();\n"
" if (t.0 != 7) { return 1; };\n"
" if (len(t.1) != 5) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "letcall_16_neutral",
"package main;\n"
"fn f() (i64, i64) = {\n"
" return (41, 17);\n"
"};\n"
"export fn main() i32 = {\n"
" let t: (i64, i64) = f();\n"
" if (t.0 != 41) { return 1; };\n"
" if (t.1 != 17) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "letcall_32_neutral",
"package main;\n"
"fn f() (i64, str) = {\n"
" let a: i64 = 12;\n"
" let s: str = \"wxyz\";\n"
" return (a, s);\n"
"};\n"
"export fn main() i32 = {\n"
" let t: (i64, str) = f();\n"
" if (t.0 != 12) { return 1; };\n"
" if (len(t.1) != 4) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "mlet_packed_neutral",
"package main;\n"
"fn f() (u32, u32) = {\n"
" return (3, 4);\n"
"};\n"
"export fn main() i32 = {\n"
" let (a, b) = f();\n"
" if (a != 3) { return 1; };\n"
" if (b != 4) { return 2; };\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/tsl_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/tsl_%d_d_%d", getpid(), i);
snprintf(errf, sizeof errf, "/tmp/tsl_%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
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/tsl_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/tsl_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/tsl_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++) {
total++;
if (asm_byte_identical(bin, &rows[i], i) != 0) fail++;
}
}
if (fail) {
fprintf(stderr, "tuple_slot_layout: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("tuple_slot_layout: %d/%d ok\n", total, total);
return 0;
}