cgexpr could not produce a tuple VALUE, so a destructure / let bind of an
RVALUE tuple read garbage past the first element (cstage) or left an untyped
binder aborting wwstage's asserttyped gate — a DANGEROUS gate-blind cs!=ww,
and the strconv-int blocker (Hare's stoi64/stou64 require
`let (sign, u) = parseint(s, base)?`). Three feeders, all routed at the same
SysV register-return cursor the cgmlet/cgmassign consumers already read:
- an N_TUPLE literal fell to the `cgexpr_int(0)` / `MOVQ $0, AX` default;
- a tuple-typed IDENT loaded only word0 into AX (`yield t`, `return t`,
`let q = t`), leaving DX/CX stale;
- the `?`/`!` unwrap of a tuple-in-union payload lifted only word0->AX,
stranding word1 in CX (the scalar/str success ABI).
Fix (both stages, byte-identical per rule 10):
- cgexpr packs an N_TUPLE literal into the cursor (cg_tuple_lit_to_cursor /
cgtuplelittocursor — a byte-identical reuse of cgreturn's in-register
N_TUPLE arm) and a tuple IDENT from its slot at the register-ABI stride
(cg_tuple_slot_to_cursor / cgtupleslottocursor);
- the ?/! unwrap shifts a tuple success payload down one integer reg past
the tag (cg_tagged_tuple_payload_shift / cgtaggedtuplepayloadshift),
loud-stopping a float/slice/str payload element (the SysV per-eightbyte
tagged-tuple-payload classification is #243);
- wwstage's checker recovers the popped match-arm binder type for a
`yield <binder>` operand (matchyieldtype's scope-free fallback to the
arm's declared type), so the destructured binders stamp — cstage reads
the operand's already-stamped ->type, wwstage caches only a tinfo.
Over-cap rvalue-tuple materialisation (no slot to sret a bare expression
value into) loud-stops both stages — the #10 follow-up.
NOT closed (distinct root, deferred to #238/task #6): single-var
`let q = (true, 9u64)` then `q.N` — the N_LET tuple-init sz==16||32 gate
drops a narrow-first mixed tuple, and the N_DOT tuple-field PACKED-offset
reader disagrees with tuple_store's 8B stride. Not the rvalue-into-cursor
fix and not a strconv blocker (strconv destructures); documented at the test
header.
Test 945_rvalue_tuple_destructure_run: literal destructure, match-yield
destructure, and the ?-call strconv shape, each run + cs==ww byte-id on both
drivers (9 checks). Embedded w6c/wwdump combined.ww regenerated.
242 lines
7.4 KiB
C
242 lines
7.4 KiB
C
/*
|
|
* 945_rvalue_tuple_destructure_run — project #241: materialise an RVALUE
|
|
* tuple into the register cursor before a destructure / let bind.
|
|
*
|
|
* cgexpr could not produce a tuple VALUE: a literal `(a, b)` fell to the
|
|
* `cgexpr_int(0)` / `MOVQ $0, AX` default, a tuple-typed IDENT loaded only
|
|
* word0 into AX, and the `?`/`!` unwrap of a tuple-in-union payload lifted
|
|
* only word0->AX (stranding word1 in CX). So `let (x, y) = <rvalue tuple>`
|
|
* read GARBAGE past the first element (cstage), and the un-typed binder left
|
|
* wwstage's checker aborting (asserttyped). DANGEROUS gate-blind cs!=ww — the
|
|
* strconv blocker (`let (sign, u) = parseint(s, base)?`).
|
|
*
|
|
* Fix (both stages, byte-identical per rule 10): cgexpr packs an N_TUPLE
|
|
* literal and a tuple IDENT into the SysV register-return cursor (the SAME
|
|
* ABI a tuple-returning call leaves, which the cgmlet/cgmassign consumers
|
|
* already read); the ?/! unwrap shifts a tuple success payload down one
|
|
* integer reg past the tag. wwstage's checker recovers the popped match-arm
|
|
* binder type for a `yield <binder>` (scope-free, via the arm's declared
|
|
* type) so the destructured binders stamp.
|
|
*
|
|
* Rows assert BOTH elements on BOTH drivers AND cs==ww byte-identical:
|
|
* literal_destructure `let (a, b) = (true, 11u64)`
|
|
* match_yield `let (sg, u) = match (mk()) { case let t => yield t }`
|
|
* trycall_strconv `let (sign, u) = parseint(base)?` (the strconv shape)
|
|
*
|
|
* NOT covered (separate root, deferred): single-var `let q = (true, 9u64)`
|
|
* then `q.N` rides #238 — the N_LET tuple-init sz==16||32 gate + the N_DOT
|
|
* tuple-field PACKED-offset reader vs tuple_store's 8B stride disagree for a
|
|
* narrow-first tuple. Out of scope for #241 (the rvalue-into-cursor fix); see
|
|
* task #6 / proj #238.
|
|
*
|
|
* 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[] = {
|
|
{ "literal_destructure",
|
|
"package main;\n"
|
|
"export fn main() i32 = {\n"
|
|
" let (a, b) = (true, 11u64);\n"
|
|
" if (b != 11u64) { return 1; };\n"
|
|
" if (!a) { return 2; };\n"
|
|
" return 0;\n"
|
|
"};\n", 0 },
|
|
{ "match_yield",
|
|
"package main;\n"
|
|
"type myerr = !void;\n"
|
|
"fn mk(x: u64, s: bool) ((bool, u64) | myerr) = { return (s, x); };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let (sg, u) = match (mk(6u64, true)) {\n"
|
|
" case let t: (bool, u64) => yield t;\n"
|
|
" case myerr => { return 9; };\n"
|
|
" };\n"
|
|
" if (u != 6u64) { return 1; };\n"
|
|
" if (!sg) { return 2; };\n"
|
|
" return 0;\n"
|
|
"};\n", 0 },
|
|
{ "trycall_strconv",
|
|
"package main;\n"
|
|
"type invalid = !void;\n"
|
|
"fn parseint(base: int) ((bool, u64) | invalid) = {\n"
|
|
" let sg: bool = true;\n"
|
|
" let n: u64 = 42u64;\n"
|
|
" return (sg, n);\n"
|
|
"};\n"
|
|
"fn stoi(base: int) (u64 | invalid) = {\n"
|
|
" let (sign, u) = parseint(base)?;\n"
|
|
" if (!sign) { return 0u64; };\n"
|
|
" return u;\n"
|
|
"};\n"
|
|
"export fn main() i32 = {\n"
|
|
" match (stoi(10)) {\n"
|
|
" case let v: u64 => { if (v != 42u64) { return 1; }; };\n"
|
|
" case invalid => { return 2; };\n"
|
|
" };\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/rvtd_%d_%d.ww", getpid(), i);
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/rvtd_%d_d_%d", getpid(), i);
|
|
snprintf(errf, sizeof errf, "/tmp/rvtd_%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/rvtd_bi_%d_%d.ww", getpid(), i);
|
|
snprintf(cs_s, sizeof cs_s, "/tmp/rvtd_bi_%d_%d_cs.s", getpid(), i);
|
|
snprintf(ws_s, sizeof ws_s, "/tmp/rvtd_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 "
|
|
"(rule-10 byte-id)\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, "rvalue_tuple_destructure: 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, "rvalue_tuple_destructure: %d/%d checks failed\n",
|
|
fail, total);
|
|
return 1;
|
|
}
|
|
printf("rvalue_tuple_destructure: %d/%d ok\n", total, total);
|
|
return 0;
|
|
}
|