w6c: keep nested unwrap destinations distinct

This commit is contained in:
2026-08-09 04:52:43 +09:00
parent 7ab080fe2a
commit a2faa8b071
3 changed files with 103 additions and 15 deletions

View File

@@ -299,6 +299,36 @@ fn unwrapaligncheck(label: str, stage: str, s: str) void = {
};
};
fn unwrapnestedcheck(label: str, stage: str, s: str) void = {
let w: str = probewindow(label, stage, s);
if (!testenv.has(w, "TEXT main.probe,$48")) {
fail(label, strings.concat(stage,
": nested spill frame is not $48"));
};
if (testenv.has(w, "\tPUSHQ\tBX\n")) {
fail(label, strings.concat(stage,
": destination PUSHQ remains in nested window"));
};
let osave: i32 = testenv.pos(w, "\tMOVQ\tBX, -8(BP)\n");
let choice: i32 = posafter(w, osave + 1,
"\tCALL\tmain.choice(SB)\n");
let isave: i32 = posafter(w, choice + 1,
"\tMOVQ\tBX, -48(BP)\n");
let make: i32 = posafter(w, isave + 1,
"\tCALL\tmain.make(SB)\n");
let iload: i32 = posafter(w, make + 1,
"\tMOVQ\t-48(BP), BX\n");
let pass: i32 = posafter(w, iload + 1,
"\tCALL\tmain.pass(SB)\n");
let oload: i32 = posafter(w, pass + 1,
"\tMOVQ\t-8(BP), BX\n");
if (osave < 0 || choice < 0 || isave < 0 || make < 0
|| iload < 0 || pass < 0 || oload < 0) {
fail(label, strings.concat(stage,
": nested destination spills are not distinct and scoped"));
};
};
fn commandexit(dir: str, name: str, av: []str) i32 = {
let co: testenv.commandout;
testenv.runcommand(dir, dir, name, av, tmo(), &co);
@@ -349,3 +379,65 @@ fn commandexit(dir: str, name: str, av: []str) i32 = {
};
testenv.clean(td);
};
// The outer destination stays live while its call argument lowers a
// match arm containing another #16 assignment. A per-fn shared spill
// redirected both stores to inner.f and left outer.f unchanged.
@test fn unwrapnestedlive() void = {
let label: str = "nested_global_unwrap_destination";
let td: str = testenv.fresh();
let src: str = strings.concat(
"package main;\n",
"type e = !i32;\n",
"type pair = struct { a: i64, b: i64 };\n",
"type box = struct { f: pair, guard: i64 };\n",
"let outer: box = box {\n",
" f = pair { a = 1i64, b = 2i64 }, guard = 7i64\n",
"};\n",
"let inner: box = box {\n",
" f = pair { a = 3i64, b = 4i64 }, guard = 8i64\n",
"};\n",
"fn make() (pair | e) = {\n",
" return pair { a = 111i64, b = 222i64 };\n",
"};\n",
"fn choice() (i32 | str) = { return 1i32; };\n",
"fn pass(x: i64) (pair | e) = {\n",
" return pair { a = 111i64 + x, b = 222i64 };\n",
"};\n",
"fn probe() i32 = {\n",
" outer.f = pass(match (choice()) {\n",
" case i32 => {\n",
" inner.f = make()!;\n",
" yield 0i64;\n",
" };\n",
" case str => yield 0i64;\n",
" })!;\n",
" if (outer.f.a != 111i64 || outer.f.b != 222i64",
" || outer.guard != 7i64) { return 91; };\n",
" if (inner.f.a != 111i64 || inner.f.b != 222i64",
" || inner.guard != 8i64) { return 92; };\n",
" return 37;\n",
"};\n",
"export fn main() i32 = { return probe(); };\n");
testenv.writefile(strings.concat(td, "/src.ww"), src);
emitstage(td, label, "cstage", testenv.driver("w6c"), "cs.s");
emitstage(td, label, "wwstage", testenv.driver("w6c_ww"), "ws.s");
let cs: str = testenv.readfile(strings.concat(td, "/cs.s"));
let ws: str = testenv.readfile(strings.concat(td, "/ws.s"));
unwrapnestedcheck(label, "cstage", cs);
unwrapnestedcheck(label, "wwstage", ws);
if (!testenv.same(cs, ws)) {
fail(label, "cstage vs wwstage asm differs");
};
let out: str = strings.concat(td, "/unwrapnested");
let bav: []str = [testenv.driver("ww"), "build", "-o", out,
"src.ww"];
if (commandexit(td, "build", bav) != 0) {
fail(label, "nested runtime pin build failed");
};
let rav: []str = [out];
if (commandexit(td, "run", rav) != 37) {
fail(label, "nested payload/neighbour runtime exit != 37");
};
testenv.clean(td);
};