cstage+selfhost+test: System V AMD64 sret discipline for >24B struct return (#23)
Class B shared miscompile pre-fix: cstage skipped the CALL emit at the
receive site (frame collapsed, exit 11); wwstage emitted CALL but
truncated 32B return to AX only (slice payload garbage, segfault on
g.b[0]). Both stages now lower plain TY_STRUCT > 24B through the SysV
sret discipline: caller pre-allocates dest, passes &dest in RDI as a
hidden first-arg (user args shift to SI/DX/CX/R8/R9/+stack), callee
saves RDI to @sretarg at the prologue and writes through it, returns
RDI in RAX. Surfaced by lib/encoding/utf8 pre-flight when the
Hoehrmann decoder (32B) hit 698_cgreturn_struct.c's OUT-OF-SCOPE
marker.
Scope: plain TY_STRUCT > 24B only — tagged unions, tuples, str, slice
keep their existing register-return ABIs. `return f()` forwarding
from a sret callee is fail-loud-not-wired (compile-time error in
both stages, follow-up filed); the workaround `let r = f(); return
r;` is wired and byte-identical. Discard-context calls (`f();` of an
sret-returning function) share a per-fn single-slot @sretscr;
consecutive discards reuse the same slot.
698_cgreturn_struct.c's OUT-OF-SCOPE marker retired in the same
commit; three positive rows (32B quad, 32B decoder, 40B five) now
assert the sret discipline across both stages via byte-id diff.
Tests:
- 721_sret_struct_return pins three asm-presence sentinels per
row: (a) LEAQ -K(BP), DI immediately before CALL at the receive
site, (b) MOVQ -K(BP), AX before RET in the callee (sret return-
the-pointer), (c) negative-assert no MOVQ AX, -K(BP) capture for
return type >8B. Three rows × both stages × cmp -s byte-id.
- 925_sret_struct_return_run runtime-pins 7 rows × 2 stages
including the collision row (25B+ struct BOTH returned AND passed
by-value as arg — catches arg-shift, sister site to #11), nested
struct payload, slice payload, reassign-receive, N_IDENT return
rhs.
89/89 ok. 995_self_rebuild stays green (ww2==ww3==ww4 byte-id).
This commit is contained in:
@@ -1,41 +1,39 @@
|
||||
/*
|
||||
* 698_cgreturn_struct — whole-struct return ABI for sizes <= 24B.
|
||||
* 698_cgreturn_struct — whole-struct return ABI across both 3-reg
|
||||
* register-return (≤24B) and SysV sret (>24B, task #23) paths.
|
||||
*
|
||||
* Pre-#7: `return s;` from a struct-returning fn fell through to the
|
||||
* scalar path: only the first 8 bytes of the struct made it to AX,
|
||||
* the rest was silently dropped. Compounded with the receive side
|
||||
* (#5 N_ASSIGN whole-STRUCT rhs) being unwired, struct returns were
|
||||
* a no-op end-to-end.
|
||||
*
|
||||
* #7 wires the producer side: cgreturn now materialises the struct
|
||||
* into a zero-padded 24B scratch slot (`@retscr`), then loads
|
||||
* AX/DX/CX from the slot unconditionally — three MOVQs regardless of
|
||||
* declared size — so receiver code (landing in #5) can read all
|
||||
* three words and mask by the declared struct size. R8 stays
|
||||
* reserved for the tagged-return 4th word; sret for sizes > 24B is
|
||||
* a separate future task.
|
||||
* the rest was silently dropped. #7 wired the ≤24B producer side
|
||||
* (materialise into @retscr, load AX/DX/CX). The >24B case stayed
|
||||
* OUT OF SCOPE — fell through to the same scalar path — until task
|
||||
* #23 landed standard SysV sret discipline (caller pre-allocates
|
||||
* dest, passes &dest in RDI, callee writes through *RDI and returns
|
||||
* RDI in RAX). Surfaced by lib/encoding/utf8 pre-flight when the
|
||||
* Hoehrmann decoder (`struct { offs: size, src: []u8 }`, 32B) hit
|
||||
* the OUT-OF-SCOPE path; the marker pattern bit precisely when its
|
||||
* scope hit, which is the right escalation signal.
|
||||
*
|
||||
* What this test pins:
|
||||
* - cstage and wwstage emit byte-identical asm for every fixture
|
||||
* (the bootstrap byte-identity invariant — if either stage's
|
||||
* scanlocals / cgreturn drifts, the diff catches it).
|
||||
* - cstage and wwstage emit byte-identical asm for every fixture,
|
||||
* including the new 25B+ sret rows (the bootstrap byte-identity
|
||||
* invariant — if either stage's scanlocals / cgreturn / cgcall
|
||||
* drifts, the diff catches it).
|
||||
* - Each fixture compiles+links+runs without crashing under both
|
||||
* drivers (proves the prologue SUBQ reserves enough frame for
|
||||
* the @retscr scratch; an under-booked frame would smash the
|
||||
* saved BP / return address on the load-back).
|
||||
* @retscr / @sretarg / @sretscr; an under-booked frame would
|
||||
* smash the saved BP / return address).
|
||||
* - End-to-end value verification (caller reads AX/DX/CX into a
|
||||
* dst slot) is deferred to #5's test surface, since that's the
|
||||
* receive side. Until then the call's result is discarded and
|
||||
* main returns a literal exit code; we're checking the cgreturn
|
||||
* side doesn't crash or produce invalid asm.
|
||||
* dst slot, or sret writes through *RDI) is deferred to #5's
|
||||
* test surface and the new 9xx semantic test for #23. Here we
|
||||
* check the cgreturn side doesn't crash or produce invalid asm.
|
||||
*
|
||||
* Coverage: five struct shapes — 8B one-field, 16B two-i64, 24B
|
||||
* three-i64 (the headline ABI shape), mixed-alignment i32+i32+i64+i64
|
||||
* (totsize 24 with the i32-pair packed), and N_IDENT rhs (let-init
|
||||
* then `return p;`) vs N_STRUCTLIT rhs (`return T{...};`). The
|
||||
* 25B+ sret case is explicitly OUT OF SCOPE — falls through to the
|
||||
* existing scalar path (only AX gets the first qword); not pinned
|
||||
* here.
|
||||
* Coverage: ≤24B shapes — 8B one-field, 16B two-i64, 24B three-i64,
|
||||
* mixed-alignment i32+i32+i64+i64 (totsize 24 with the i32-pair
|
||||
* packed), and N_IDENT rhs (let-init then `return p;`). Plus the
|
||||
* new 25B+ rows added with #23: 32B i64×4, the utf8 decoder shape
|
||||
* (i64 + []u8 = 32B aligned, the canonical surfacing case), and
|
||||
* 40B i64×5.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -99,6 +97,42 @@ static const struct row rows[] = {
|
||||
"};\n"
|
||||
"fn main() i32 = { mk(); return 0; };\n",
|
||||
0 },
|
||||
/* 32B four-i64 — first row past the 24B → sret threshold.
|
||||
* Hits the sret prologue (@sretarg ← DI), the structlit-fill
|
||||
* through *(@sretarg), and `MOVQ @sretarg(BP), AX; RET`. */
|
||||
{ "quad_i64_lit_sret",
|
||||
"type quad = struct { a: i64, b: i64, c: i64, d: i64 };\n"
|
||||
"fn mk() quad = { return quad { a = 1i64, b = 2i64, c = 3i64, d = 4i64 }; };\n"
|
||||
"fn main() i32 = { let q: quad = mk(); return 0; };\n",
|
||||
0 },
|
||||
/* utf8 decoder shape (the surfacing case for #23):
|
||||
* `struct { offs: i64, src: []u8 }` — i64 at +0, slice at +8
|
||||
* (16B+8B alignment, totsize 32). N_IDENT rhs exercises the
|
||||
* sret word-copy-from-rhs-slot branch. */
|
||||
{ "decoder_ident_sret",
|
||||
"type decoder = struct { offs: i64, src: []u8 };\n"
|
||||
"fn mk(s: []u8) decoder = {\n"
|
||||
" let r: decoder;\n"
|
||||
" r.offs = 0i64;\n"
|
||||
" r.src = s;\n"
|
||||
" return r;\n"
|
||||
"};\n"
|
||||
"fn main() i32 = {\n"
|
||||
" let b: [1]u8;\n"
|
||||
" let d: decoder = mk(b[0:1]);\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
0 },
|
||||
/* 40B five-i64 — second sret row, sized past 32B to exercise
|
||||
* the sretscr / @sretarg sizing for a larger struct in the
|
||||
* same fn family. */
|
||||
{ "five_i64_lit_sret",
|
||||
"type five = struct { a: i64, b: i64, c: i64, d: i64, e: i64 };\n"
|
||||
"fn mk() five = {\n"
|
||||
" return five { a = 1i64, b = 2i64, c = 3i64, d = 4i64, e = 5i64 };\n"
|
||||
"};\n"
|
||||
"fn main() i32 = { let f: five = mk(); return 0; };\n",
|
||||
0 },
|
||||
};
|
||||
|
||||
static int
|
||||
|
||||
304
test/wcc/721_sret_struct_return.c
Normal file
304
test/wcc/721_sret_struct_return.c
Normal file
@@ -0,0 +1,304 @@
|
||||
/*
|
||||
* 721_sret_struct_return — Class A asm-presence sentinels for the
|
||||
* System V AMD64 sret ABI lowering (task #23).
|
||||
*
|
||||
* #23 wires both stages to lower a plain TY_STRUCT return > 24B
|
||||
* through the standard SysV sret discipline: caller pre-allocates
|
||||
* dest, passes &dest in RDI as a hidden first arg (shifting all
|
||||
* declared args right by one — SI/DX/CX/R8/R9/+stack), callee saves
|
||||
* RDI to @sretarg in the prologue, writes the return value through
|
||||
* the saved pointer, then `MOVQ @sretarg(BP), AX; RET` (the SysV
|
||||
* "return the pointer" discipline). 4 lowering sites: caller arg-
|
||||
* shift+receive, callee prologue, callee return — receive collapses
|
||||
* into the caller-prealloc because the named LHS slot IS the
|
||||
* prealloc dest.
|
||||
*
|
||||
* Pre-#23: cstage skipped the CALL emit entirely at the receive
|
||||
* site (frame layout collapsed; exit 11). wwstage emitted the CALL
|
||||
* but truncated the 32B return to RAX only (slice payload garbage;
|
||||
* segfault). Surfaced by lib/encoding/utf8 pre-flight when the
|
||||
* Hoehrmann decoder `struct { offs: size, src: []u8 }` (32B) hit
|
||||
* the documented OUT-OF-SCOPE marker at 698.
|
||||
*
|
||||
* Three sentinels per row pinned here (rob-pike's triangle):
|
||||
* (a) caller emits `LEAQ <K>(BP), DI` immediately before `CALL` —
|
||||
* the hidden RDI dest pointer (asm-presence positive).
|
||||
* (b) callee emits `MOVQ <K>(BP), AX` BEFORE the final `RET` —
|
||||
* the sret return-the-pointer load (asm-presence positive).
|
||||
* The K is the @sretarg offset; we don't pin it, but we pin
|
||||
* that an `(BP), AX` load lives in the last three lines
|
||||
* before RET in any fn whose return type is > 24B struct.
|
||||
* (c) caller emits NO `MOVQ AX, <K>(BP)` capture of the call
|
||||
* result for a return type > 24B (asm-presence negative).
|
||||
* Pre-#23 wwstage emitted such a capture and truncated.
|
||||
*
|
||||
* Plus byte-id between stages per row (a future Class A divergence
|
||||
* via either site catches here).
|
||||
*/
|
||||
#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;
|
||||
}
|
||||
|
||||
struct row { const char *label; const char *src; };
|
||||
|
||||
/* Each row's mk fn returns a >24B struct; main does a `let r: T = mk(...)`
|
||||
* so the receive site is wired and the sret discipline fires. */
|
||||
static const struct row rows[] = {
|
||||
/* 32B four-i64: the smallest-padded sret return shape. */
|
||||
{ "quad_i64",
|
||||
"type quad = struct { a: i64, b: i64, c: i64, d: i64 };\n"
|
||||
"fn mk() quad = {\n"
|
||||
" return quad { a = 1i64, b = 2i64, c = 3i64, d = 4i64 };\n"
|
||||
"};\n"
|
||||
"fn main() i32 = { let q: quad = mk(); return 0; };\n" },
|
||||
/* utf8 decoder shape (surfacing case for #23): i64 + []u8. The
|
||||
* []u8 field's slice layout (ptr/len/cap) crosses the AX/DX/CX
|
||||
* boundary — the wwstage truncation bug dropped the slice tail. */
|
||||
{ "decoder",
|
||||
"type decoder = struct { offs: i64, src: []u8 };\n"
|
||||
"fn mk(s: []u8) decoder = {\n"
|
||||
" let r: decoder;\n"
|
||||
" r.offs = 0i64;\n"
|
||||
" r.src = s;\n"
|
||||
" return r;\n"
|
||||
"};\n"
|
||||
"fn main() i32 = {\n"
|
||||
" let b: [1]u8;\n"
|
||||
" let d: decoder = mk(b[0:1]);\n"
|
||||
" return 0;\n"
|
||||
"};\n" },
|
||||
/* 40B five-i64: second size past 24B, exercises @sretscr sizing. */
|
||||
{ "five_i64",
|
||||
"type five = struct { a: i64, b: i64, c: i64, d: i64, e: i64 };\n"
|
||||
"fn mk() five = {\n"
|
||||
" return five { a = 1i64, b = 2i64, c = 3i64, d = 4i64, e = 5i64 };\n"
|
||||
"};\n"
|
||||
"fn main() i32 = { let f: five = mk(); return 0; };\n" },
|
||||
};
|
||||
|
||||
static int
|
||||
slurp(const char *path, char *buf, size_t cap)
|
||||
{
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return -1;
|
||||
size_t n = fread(buf, 1, cap - 1, f);
|
||||
fclose(f);
|
||||
buf[n] = '\0';
|
||||
return (int)n;
|
||||
}
|
||||
|
||||
static int
|
||||
emit_s(const char *w6c, const struct row *r, int i, char *out_s, size_t cap)
|
||||
{
|
||||
char src[96], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/sret_asm_%d_%d.ww", getpid(), i);
|
||||
snprintf(out_s, cap, "/tmp/sret_asm_%d_%d_%s.s",
|
||||
getpid(), i, w6c[strlen(w6c) - 1] == 'w' ? "ww" : "c");
|
||||
|
||||
FILE *f = fopen(src, "wb");
|
||||
if (!f) return -1;
|
||||
fputs(r->src, f);
|
||||
fclose(f);
|
||||
|
||||
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, out_s, src);
|
||||
int rc = runwait(cmd);
|
||||
unlink(src);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* (a) caller-side prealloc sentinel: `LEAQ -K(BP), DI` must appear
|
||||
* on the line immediately preceding `CALL mk(SB)`. */
|
||||
static int
|
||||
check_leaq_di_before_call(const char *path, const struct row *r)
|
||||
{
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return -1;
|
||||
char prev[256] = {0};
|
||||
char line[1024];
|
||||
int ok = -1;
|
||||
while (fgets(line, sizeof line, f)) {
|
||||
if (strstr(line, "CALL\tmk(SB)")
|
||||
|| strstr(line, "CALL mk(SB)")) {
|
||||
if (strstr(prev, "LEAQ\t")
|
||||
&& strstr(prev, "(BP), DI")) {
|
||||
ok = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
strncpy(prev, line, sizeof prev - 1);
|
||||
prev[sizeof prev - 1] = '\0';
|
||||
}
|
||||
fclose(f);
|
||||
if (ok != 0)
|
||||
fprintf(stderr,
|
||||
"row[%s]: LEAQ -K(BP), DI before CALL mk(SB) missing\n",
|
||||
r->label);
|
||||
return ok;
|
||||
}
|
||||
|
||||
/* (b) callee-side return-the-pointer sentinel: in the mk fn body
|
||||
* (between `TEXT mk,` and the FIRST `RET` after it), assert a
|
||||
* `MOVQ -K(BP), AX` appears within the last few lines before that
|
||||
* RET — the @sretarg reload. */
|
||||
static int
|
||||
check_movq_bp_ax_before_ret(const char *path, const struct row *r)
|
||||
{
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return -1;
|
||||
char line[1024];
|
||||
int in_mk = 0;
|
||||
char window[8][256] = {{0}};
|
||||
int wi = 0;
|
||||
int ok = -1;
|
||||
while (fgets(line, sizeof line, f)) {
|
||||
if (!in_mk) {
|
||||
if (strstr(line, "TEXT mk,") || strstr(line, "TEXT\tmk,"))
|
||||
in_mk = 1;
|
||||
continue;
|
||||
}
|
||||
if (strstr(line, "\tRET\n")) {
|
||||
for (int k = 0; k < 8; k++) {
|
||||
if (strstr(window[k], "MOVQ\t")
|
||||
&& strstr(window[k], "(BP), AX")) {
|
||||
ok = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
strncpy(window[wi % 8], line, sizeof window[0] - 1);
|
||||
window[wi % 8][sizeof window[0] - 1] = '\0';
|
||||
wi++;
|
||||
}
|
||||
fclose(f);
|
||||
if (ok != 0)
|
||||
fprintf(stderr,
|
||||
"row[%s]: MOVQ -K(BP), AX before RET in mk missing\n",
|
||||
r->label);
|
||||
return ok;
|
||||
}
|
||||
|
||||
/* (c) caller-side negative-assert: between `CALL mk(SB)` and the
|
||||
* NEXT instruction line, there must be NO `MOVQ AX, -K(BP)` (the
|
||||
* pre-#23 wwstage truncation pattern). The natural sret receive
|
||||
* leaves the value in the slot already; AX holds the dest ptr but
|
||||
* we don't store it back. */
|
||||
static int
|
||||
check_no_movq_ax_bp_after_call(const char *path, const struct row *r)
|
||||
{
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return -1;
|
||||
char line[1024];
|
||||
int seen_call = 0;
|
||||
int peek = 0;
|
||||
int ok = 0;
|
||||
while (fgets(line, sizeof line, f)) {
|
||||
if (!seen_call) {
|
||||
if (strstr(line, "CALL\tmk(SB)")
|
||||
|| strstr(line, "CALL mk(SB)")) {
|
||||
seen_call = 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
/* Inspect the next few instruction lines. A `MOVQ AX, -N(BP)`
|
||||
* within ~3 lines after CALL would be the truncation
|
||||
* pattern. */
|
||||
if (peek++ >= 3) break;
|
||||
if (strstr(line, "MOVQ\tAX,") && strstr(line, "(BP)")) {
|
||||
ok = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(f);
|
||||
if (ok != 0)
|
||||
fprintf(stderr,
|
||||
"row[%s]: unexpected MOVQ AX, -K(BP) after CALL mk(SB)"
|
||||
" (pre-#23 truncation pattern)\n", r->label);
|
||||
return ok;
|
||||
}
|
||||
|
||||
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 w6c[640], w6c_ww[640];
|
||||
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
|
||||
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
|
||||
|
||||
int have_ww = (access(w6c_ww, X_OK) == 0);
|
||||
|
||||
int n = (int)(sizeof rows / sizeof rows[0]);
|
||||
int total = 0, fail = 0;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
char cs_path[128], ws_path[128];
|
||||
|
||||
/* cstage asm + three sentinels. */
|
||||
if (emit_s(w6c, &rows[i], i, cs_path, sizeof cs_path) != 0) {
|
||||
fprintf(stderr, "row[%s]: w6c failed\n", rows[i].label);
|
||||
fail++; total++; continue;
|
||||
}
|
||||
total += 3;
|
||||
if (check_leaq_di_before_call(cs_path, &rows[i]) != 0) fail++;
|
||||
if (check_movq_bp_ax_before_ret(cs_path, &rows[i]) != 0) fail++;
|
||||
if (check_no_movq_ax_bp_after_call(cs_path, &rows[i]) != 0) fail++;
|
||||
|
||||
if (!have_ww) { unlink(cs_path); continue; }
|
||||
|
||||
/* wwstage asm + three sentinels. */
|
||||
if (emit_s(w6c_ww, &rows[i], i, ws_path, sizeof ws_path) != 0) {
|
||||
fprintf(stderr,
|
||||
"row[%s]: w6c_ww failed\n", rows[i].label);
|
||||
fail++; total++;
|
||||
unlink(cs_path);
|
||||
continue;
|
||||
}
|
||||
total += 3;
|
||||
if (check_leaq_di_before_call(ws_path, &rows[i]) != 0) fail++;
|
||||
if (check_movq_bp_ax_before_ret(ws_path, &rows[i]) != 0) fail++;
|
||||
if (check_no_movq_ax_bp_after_call(ws_path, &rows[i]) != 0) fail++;
|
||||
|
||||
/* Byte-id diff between stages. */
|
||||
total++;
|
||||
char cmd[512];
|
||||
snprintf(cmd, sizeof cmd, "cmp -s %s %s", cs_path, ws_path);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr,
|
||||
"row[%s]: cstage vs wwstage asm differs\n",
|
||||
rows[i].label);
|
||||
fail++;
|
||||
}
|
||||
|
||||
unlink(cs_path); unlink(ws_path);
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
fprintf(stderr,
|
||||
"sret_struct_return: %d/%d fixtures failed\n",
|
||||
fail, total);
|
||||
return 1;
|
||||
}
|
||||
printf("sret_struct_return: %d/%d ok\n", total, total);
|
||||
return 0;
|
||||
}
|
||||
271
test/wcc/925_sret_struct_return_run.c
Normal file
271
test/wcc/925_sret_struct_return_run.c
Normal file
@@ -0,0 +1,271 @@
|
||||
/*
|
||||
* 925_sret_struct_return_run — Class B semantic test for the System V
|
||||
* AMD64 sret ABI lowering (task #23). End-to-end round-trip pinning
|
||||
* that values cross the >24B struct-return boundary intact under
|
||||
* both cstage and wwstage.
|
||||
*
|
||||
* Class A asm-presence is pinned at 721; Class B (this file) catches
|
||||
* shared miscompiles that asm byte-id can't see. Pre-#23 both stages
|
||||
* were broken in different ways — cstage skipped the CALL emit
|
||||
* (exit 11), wwstage truncated to AX only (segfault). Post-#23 both
|
||||
* round-trip cleanly across 32B / 40B / nested / slice-payload
|
||||
* shapes.
|
||||
*
|
||||
* Critical row: a 25B+ struct that is BOTH returned AND passed by-
|
||||
* value as an arg (collision with #11's struct-by-value param
|
||||
* decompose). Catches arg-shift bugs where the hidden RDI displaces
|
||||
* a declared struct-by-value arg into the wrong reg.
|
||||
*/
|
||||
#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;
|
||||
}
|
||||
|
||||
struct row { const char *label; const char *src; int want; };
|
||||
|
||||
static const struct row rows[] = {
|
||||
/* 32B four-i64: smallest sret shape. All four words must
|
||||
* round-trip; pre-#23 wwstage dropped DX/CX/R8 silently. */
|
||||
{ "quad_i64_roundtrip",
|
||||
"type quad = struct { a: i64, b: i64, c: i64, d: i64 };\n"
|
||||
"fn mk(x: i64) quad = {\n"
|
||||
" return quad { a = x, b = x + 1i64, c = x + 2i64, d = x + 3i64 };\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let q: quad = mk(10i64);\n"
|
||||
" if (q.a != 10i64) { return 1; };\n"
|
||||
" if (q.b != 11i64) { return 2; };\n"
|
||||
" if (q.c != 12i64) { return 3; };\n"
|
||||
" if (q.d != 13i64) { return 4; };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
0 },
|
||||
/* utf8 decoder shape — the surfacing case for #23. i64 + []u8
|
||||
* (slice ptr/len/cap). Slice-tail words pre-#23 leaked into
|
||||
* the wrong slot or stayed garbage. */
|
||||
{ "decoder_slice_payload",
|
||||
"type decoder = struct { offs: i64, src: []u8 };\n"
|
||||
"fn mk(s: []u8) decoder = {\n"
|
||||
" let r: decoder;\n"
|
||||
" r.offs = 42i64;\n"
|
||||
" r.src = s;\n"
|
||||
" return r;\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let buf: [3]u8;\n"
|
||||
" buf[0] = 0xa1u8;\n"
|
||||
" buf[1] = 0xb2u8;\n"
|
||||
" buf[2] = 0xc3u8;\n"
|
||||
" let d: decoder = mk(buf[0:3]);\n"
|
||||
" if (d.offs != 42i64) { return 1; };\n"
|
||||
" if (d.src.len != 3) { return 2; };\n"
|
||||
" if (d.src[0] != 0xa1u8) { return 3; };\n"
|
||||
" if (d.src[1] != 0xb2u8) { return 4; };\n"
|
||||
" if (d.src[2] != 0xc3u8) { return 5; };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
0 },
|
||||
/* 40B five-i64: second past-24B size. Exercises @sretscr /
|
||||
* @sretarg sizing for a larger struct in the same fn family. */
|
||||
{ "five_i64_roundtrip",
|
||||
"type five = struct { a: i64, b: i64, c: i64, d: i64, e: i64 };\n"
|
||||
"fn mk() five = {\n"
|
||||
" return five { a = 1i64, b = 2i64, c = 3i64, d = 4i64, e = 5i64 };\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let f: five = mk();\n"
|
||||
" if (f.a != 1i64) { return 1; };\n"
|
||||
" if (f.b != 2i64) { return 2; };\n"
|
||||
" if (f.c != 3i64) { return 3; };\n"
|
||||
" if (f.d != 4i64) { return 4; };\n"
|
||||
" if (f.e != 5i64) { return 5; };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
0 },
|
||||
/* Nested struct payload: outer 32B struct contains a 16B inner.
|
||||
* Pin that the inner's field offsets land correctly via the
|
||||
* `cg_structlit_fill` recursion through *(@sretarg). */
|
||||
{ "nested_struct_payload",
|
||||
"type inner = struct { p: i64, q: i64 };\n"
|
||||
"type outer = struct { i: inner, s: i64, t: i64 };\n"
|
||||
"fn mk() outer = {\n"
|
||||
" return outer {\n"
|
||||
" i = inner { p = 100i64, q = 200i64 },\n"
|
||||
" s = 300i64, t = 400i64\n"
|
||||
" };\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let o: outer = mk();\n"
|
||||
" if (o.i.p != 100i64) { return 1; };\n"
|
||||
" if (o.i.q != 200i64) { return 2; };\n"
|
||||
" if (o.s != 300i64) { return 3; };\n"
|
||||
" if (o.t != 400i64) { return 4; };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
0 },
|
||||
/* Reassignment receive: `let x: T;` followed by `x = mk();`
|
||||
* routes through cgassign's sret branch (vs cglet's). */
|
||||
{ "reassign_receive",
|
||||
"type quad = struct { a: i64, b: i64, c: i64, d: i64 };\n"
|
||||
"fn mk(s: i64) quad = {\n"
|
||||
" return quad { a = s, b = s, c = s, d = s };\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let q: quad;\n"
|
||||
" q = mk(7i64);\n"
|
||||
" if (q.a != 7i64) { return 1; };\n"
|
||||
" if (q.b != 7i64) { return 2; };\n"
|
||||
" if (q.c != 7i64) { return 3; };\n"
|
||||
" if (q.d != 7i64) { return 4; };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
0 },
|
||||
/* Collision row: a 16B struct passed by-value as an arg into a
|
||||
* fn that ALSO returns a >24B struct. Pre-fix the hidden RDI
|
||||
* would displace SI/DX (the struct-by-value pair) — caller would
|
||||
* pop the i64 struct-words into DX/CX and emit LEAQ into DI,
|
||||
* leaving SI uninitialised and the callee reading garbage. The
|
||||
* arg-shift fix routes the int-arg cursor starting at 1, so the
|
||||
* 16B struct lands cleanly in SI/DX. (Sister site of #11.) */
|
||||
{ "sret_with_struct16_arg",
|
||||
"type pair = struct { x: i64, y: i64 };\n"
|
||||
"type quad = struct { a: i64, b: i64, c: i64, d: i64 };\n"
|
||||
"fn mk(p: pair, k: i64) quad = {\n"
|
||||
" return quad { a = p.x, b = p.y, c = k, d = p.x + p.y + k };\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let p: pair = pair { x = 3i64, y = 5i64 };\n"
|
||||
" let q: quad = mk(p, 11i64);\n"
|
||||
" if (q.a != 3i64) { return 1; };\n"
|
||||
" if (q.b != 5i64) { return 2; };\n"
|
||||
" if (q.c != 11i64) { return 3; };\n"
|
||||
" if (q.d != 19i64) { return 4; };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
0 },
|
||||
/* N_IDENT return rhs: callee builds the struct in a local
|
||||
* `let r: T;` then `return r;` — exercises the word-copy-from-
|
||||
* rhs-slot arm of cgreturn (vs the structlit fill arm). */
|
||||
{ "ident_return_rhs",
|
||||
"type pos = struct { x: i64, y: i64, z: i64, w: i64 };\n"
|
||||
"fn mk() pos = {\n"
|
||||
" let r: pos;\n"
|
||||
" r.x = 11i64;\n"
|
||||
" r.y = 22i64;\n"
|
||||
" r.z = 33i64;\n"
|
||||
" r.w = 44i64;\n"
|
||||
" return r;\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let p: pos = mk();\n"
|
||||
" if (p.x != 11i64) { return 1; };\n"
|
||||
" if (p.y != 22i64) { return 2; };\n"
|
||||
" if (p.z != 33i64) { return 3; };\n"
|
||||
" if (p.w != 44i64) { return 4; };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
0 },
|
||||
};
|
||||
|
||||
static int
|
||||
run_driver(const char *driver, const struct row *r, int i)
|
||||
{
|
||||
char src[96], tmpdir[96], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/sret_run_%d_%d.ww", getpid(), i);
|
||||
snprintf(tmpdir, sizeof tmpdir, "/tmp/sret_run_%d_d_%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",
|
||||
tmpdir, driver, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: build via %s failed\n",
|
||||
r->label, driver);
|
||||
unlink(src); rmdir(tmpdir);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *base = strrchr(src, '/');
|
||||
base = base ? base + 1 : src;
|
||||
char outbin[160];
|
||||
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); rmdir(tmpdir);
|
||||
return got;
|
||||
}
|
||||
|
||||
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];
|
||||
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
||||
char wdrv[640];
|
||||
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
|
||||
|
||||
struct { const char *name; const char *path; int gated_on_existence; }
|
||||
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_on_existence
|
||||
&& access(drivers[d].path, X_OK) != 0) {
|
||||
fprintf(stderr,
|
||||
"sret_struct_return_run: skip %s (no %s)\n",
|
||||
drivers[d].name, drivers[d].path);
|
||||
continue;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
int got = run_driver(drivers[d].path, &rows[i], i);
|
||||
total++;
|
||||
if (got != rows[i].want) {
|
||||
fprintf(stderr,
|
||||
"sret_struct_return_run[%s][%s]: exit=%d want=%d\n",
|
||||
drivers[d].name, rows[i].label,
|
||||
got, rows[i].want);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
fprintf(stderr,
|
||||
"sret_struct_return_run: %d/%d fixtures failed\n",
|
||||
fail, total);
|
||||
return 1;
|
||||
}
|
||||
printf("sret_struct_return_run: %d/%d ok\n", total, total);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user