Files
ww/test/asm/chain_test.ww

444 lines
15 KiB
Plaintext

package chain_test;
// Direct-w6c asm-window gate over chained-index element dispatch.
// Port of the retired native carriers test/wcc/739_chained_index.c,
// 740_chained_write.c and 741_dotbase_chained.c; every assertion
// preserved. test/lang/chainidx_test.ww owns ARRAY chains only — the
// **T pointer-chain narrow width, the exact stride-scale pair counts
// and the N_DOT-base recursion have no other owner; 740/741 are the
// sole exercisers of their shapes.
//
// 739 (#24, read) / 740 (#27, write): inside [TEXT main.probe ..
// first RET line) the element access must use the narrow MOV
// mnemonic (MOVZBQ/MOVSXD load, MOVB/MOVL store), the u8 write row
// anti-checks the pre-fix 8-byte `MOVQ AX, (BX)`, and the
// `MOVQ $N, CX` .. `IMULQ CX, AX` scale pairs must count EXACTLY
// inner=1 for **u8 (a stray outer scale was the pre-fix wwstage
// emit) and inner=1 outer=1 for **i32. The pair scan is the
// carriers' greedy two-strstr walk, ported positionally.
//
// 741 (#28 + #30): the N_DOT-base chained read must land MOVZBQ
// (esz recursion through dotfieldtnode) and the tagged-array write
// must scale by the 32B element stride; the write row's
// scalar-overwrite divergence net is the byte-id leg, exactly the
// carrier's design (its anti-needle machinery was never armed).
//
// Byte-id legs ride every row as in the C. Dropped C machinery, not
// assertions: w6c_ww-absent skip gates, getpid()-keyed /tmp names,
// slurp caps. The `package main;` prefix reproduces wwtest_fputs.
import os;
import os.exec;
import strings;
import testenv;
import time;
fn fail(label: str, why: str) void = {
let m: str = strings.concat("chain FAIL: ", label, " -- ", why,
"\n");
os.write(2, m.ptr, m.len: u64);
assert(false);
};
fn tmo() time.duration = {
return (180i64 * (time.second: i64)): time.duration;
};
fn emitstage(td: str, label: str, stage: str, drv: str,
outname: str) void = {
let av: []str = [];
append(av, drv);
append(av, "-o");
append(av, outname);
append(av, "src.ww");
let co: testenv.commandout;
testenv.runcommand(td, td, stage, av, tmo(), &co);
let ok: bool = co.termination == exec.termination.EXIT && co.code == 0;
if (!ok) { fail(label, strings.concat(stage, " compile failed")); };
};
fn posafter(s: str, start: i32, needle: str) i32 = {
let p: i32 = testenv.pos(strings.sub(s, start, s.len), needle);
if (p < 0) { return -1; };
return start + p;
};
// [TEXT main.probe .. first `\tRET\n`), widened to EOF when the RET
// needle is missing — the carriers' probe window.
fn probewindow(label: str, stage: str, s: str) str = {
let fp: i32 = testenv.pos(s, "TEXT main.probe");
if (fp < 0) {
fail(label, strings.concat(stage, ": no TEXT probe in .s"));
};
let tail: str = strings.sub(s, fp, s.len);
let rp: i32 = testenv.pos(tail, "\tRET\n");
if (rp < 0) { return tail; };
return strings.sub(tail, 0, rp);
};
// Greedy `movq` .. `IMULQ CX, AX` pair count (the carriers'
// two-strstr walk: an unpaired movq re-scans from movq+1, a paired
// one advances past its IMULQ).
fn paircount(w: str, movq: str) i32 = {
let n: i32 = 0;
let p: i32 = 0;
for (p < w.len) {
let ip: i32 = posafter(w, p, movq);
if (ip < 0) { break; };
let nx: i32 = posafter(w, ip, "\tIMULQ\tCX, AX\n");
if (nx < 0) { p = ip + 1; continue; };
n += 1;
p = nx + 1;
};
return n;
};
// outerscale 1 -> exactly 1 inner $8 pair and NO outer scale;
// outerscale 4 -> 1 inner $8 pair AND 1 outer $4 pair.
fn scalecheck(label: str, stage: str, w: str, outerscale: i32) void = {
let inner: i32 = paircount(w, "\tMOVQ\t$8, CX\n");
if (outerscale == 1) {
if (inner != 1) {
fail(label, strings.concat(stage,
": expected exactly 1 inner `MOVQ $8, CX; IMULQ` ",
"pair (no outer scaling for u8)"));
};
} else {
let outer: i32 = paircount(w, "\tMOVQ\t$4, CX\n");
if (inner != 1 || outer != 1) {
fail(label, strings.concat(stage,
": expected 1 inner `MOVQ $8` + 1 outer `MOVQ $4` ",
"IMULQ pair"));
};
};
};
// One 739 row: narrow load mnemonic + scale-pair counts + byte-id.
fn chainreadrow(label: str, src: str, loadmov: str,
outerscale: i32) void = {
let td: str = testenv.fresh();
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"));
let needle: str = strings.concat("\t", loadmov, "\t(AX), AX\n");
let cw: str = probewindow(label, "cstage", cs);
let ww: str = probewindow(label, "wwstage", ws);
if (!testenv.has(cw, needle)) {
fail(label, strings.concat("cstage: expected `", loadmov,
" (AX), AX` in probe body"));
};
if (!testenv.has(ww, needle)) {
fail(label, strings.concat("wwstage: expected `", loadmov,
" (AX), AX` in probe body"));
};
scalecheck(label, "cstage", cw, outerscale);
scalecheck(label, "wwstage", ww, outerscale);
if (!testenv.same(cs, ws)) {
fail(label, "cstage vs wwstage asm differs");
};
testenv.clean(td);
};
// 739: **T chained read. esz of the outer index must come from the
// inner index's value type, not the default 8.
@test fn chainedindex() void = {
chainreadrow("chained_u8", strings.concat(
"package main;\n",
"fn probe(names: **u8) u8 = {\n",
" let i: i32 = 0;\n",
" let k: u64 = 0u64;\n",
" return names[i][k];\n",
"};\n",
"export fn main() i32 = { return 0; };\n"),
"MOVZBQ", 1);
chainreadrow("chained_i32", strings.concat(
"package main;\n",
"fn probe(mat: **i32) i32 = {\n",
" let i: i32 = 0;\n",
" let k: u64 = 0u64;\n",
" return mat[i][k];\n",
"};\n",
"export fn main() i32 = { return 0; };\n"),
"MOVSXD", 4);
};
// One 740 row: narrow store mnemonic, the u8 row's wrong-width-store
// anti-needle, scale-pair counts, byte-id.
fn chainwriterow(label: str, src: str, storemov: str,
outerscale: i32) void = {
let td: str = testenv.fresh();
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"));
let needle: str = strings.concat("\t", storemov, "\tAX, (BX)\n");
let cw: str = probewindow(label, "cstage", cs);
let ww: str = probewindow(label, "wwstage", ws);
if (!testenv.has(cw, needle)) {
fail(label, strings.concat("cstage: expected `", storemov,
" AX, (BX)` in probe body"));
};
if (!testenv.has(ww, needle)) {
fail(label, strings.concat("wwstage: expected `", storemov,
" AX, (BX)` in probe body"));
};
if (outerscale == 1) {
if (testenv.has(cw, "\tMOVQ\tAX, (BX)\n")
|| testenv.has(ww, "\tMOVQ\tAX, (BX)\n")) {
fail(label, strings.concat(
"stray 8-byte `MOVQ AX, (BX)` in probe body -- ",
"pre-#27 wrong-width-store regression"));
};
};
scalecheck(label, "cstage", cw, outerscale);
scalecheck(label, "wwstage", ww, outerscale);
if (!testenv.same(cs, ws)) {
fail(label, "cstage vs wwstage asm differs");
};
testenv.clean(td);
};
// 740: **T chained write, sister of 739 on the cgassign side — the
// wrong-width store corrupts memory adjacent to the u8 slot.
@test fn chainedwrite() void = {
chainwriterow("chained_u8", strings.concat(
"package main;\n",
"fn probe(names: **u8) void = {\n",
" let i: i32 = 0;\n",
" let k: u64 = 0u64;\n",
" names[i][k] = 65u8;\n",
"};\n",
"export fn main() i32 = { return 0; };\n"),
"MOVB", 1);
chainwriterow("chained_i32", strings.concat(
"package main;\n",
"fn probe(mat: **i32) void = {\n",
" let i: i32 = 0;\n",
" let k: u64 = 0u64;\n",
" mat[i][k] = 42i32;\n",
"};\n",
"export fn main() i32 = { return 0; };\n"),
"MOVL", 4);
};
// One 741 row: dispatch-defining needle in the probe window plus
// byte-id.
fn dotbaserow(label: str, src: str, needle: str) void = {
let td: str = testenv.fresh();
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"));
if (!testenv.has(probewindow(label, "cstage", cs), needle)) {
fail(label, strings.concat("cstage: expected `", needle,
"` in probe body"));
};
if (!testenv.has(probewindow(label, "wwstage", ws), needle)) {
fail(label, strings.concat("wwstage: expected `", needle,
"` in probe body"));
};
if (!testenv.same(cs, ws)) {
fail(label, "cstage vs wwstage asm differs");
};
testenv.clean(td);
};
// 741: N_DOT-base chains. The (i64|str) element is 8(tag)+24(str
// payload)=32B, so the write row's stride needle is $32.
@test fn dotbasechained() void = {
dotbaserow("dotbase_chained_read", strings.concat(
"package main;\n",
"type S = struct{ pad: i64, mat: **u8 };\n",
"fn probe(s: *S) u8 = {\n",
" let i: i32 = 0;\n",
" let k: u64 = 0u64;\n",
" return s.mat[i][k];\n",
"};\n",
"export fn main() i32 = { return 0; };\n"),
"\tMOVZBQ\t(AX), AX\n");
dotbaserow("dotbase_array_tagged_write", strings.concat(
"package main;\n",
"type T = (i64 | str);\n",
"type S = struct{ pad: i64, arr: [4]T };\n",
"fn probe(s: *S) void = {\n",
" let i: i32 = 0;\n",
" s.arr[i] = 42i64;\n",
"};\n",
"export fn main() i32 = { return 0; };\n"),
"\tMOVQ\t$32, CX\n");
};
fn unwrapaligncheck(label: str, stage: str, s: str) void = {
let w: str = probewindow(label, stage, s);
let call: i32 = testenv.pos(w, "\tCALL\tmain.mkpair(SB)\n");
if (call < 0) {
fail(label, strings.concat(stage, ": no CALL main.mkpair"));
};
let pre: str = strings.sub(w, 0, call);
let post: str = strings.sub(w, call, w.len);
if (testenv.has(pre, "\tPUSHQ\tBX\n")) {
fail(label, strings.concat(stage,
": destination PUSHQ remains outstanding at CALL"));
};
if (!testenv.has(pre, "\tMOVQ\tBX, -")) {
fail(label, strings.concat(stage,
": destination address is not spilled BP-relative"));
};
if (!testenv.has(post, "\tMOVQ\t-")
|| !testenv.has(post, "(BP), BX\n")) {
fail(label, strings.concat(stage,
": destination address is not reloaded after CALL"));
};
if (!testenv.has(w, "TEXT main.probe,$16")) {
fail(label, strings.concat(stage,
": dedicated address spill is absent from the frame"));
};
};
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);
if (co.termination != exec.termination.EXIT) { return -1; };
return co.code;
};
@test fn unwrapcallalign() void = {
let label: str = "global_unwrap_call_alignment";
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",
"fn mkpair(x: f64) (pair | e) = {\n",
" return pair { a = 111i64, b = 222i64 };\n",
"};\n",
"let gb: box = box {\n",
" f = pair { a = 9i64, b = 9i64 }, guard = 7i64\n",
"};\n",
"fn probe() i32 = {\n",
" gb.f = mkpair(1.25f64)!;\n",
" if (gb.f.a != 111i64 || gb.f.b != 222i64",
" || gb.guard != 7i64) { return 91; };\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"));
unwrapaligncheck(label, "cstage", cs);
unwrapaligncheck(label, "wwstage", ws);
if (!testenv.same(cs, ws)) {
fail(label, "cstage vs wwstage asm differs");
};
let out: str = strings.concat(td, "/unwrapalign");
let bav: []str = [testenv.driver("ww"), "build", "-o", out,
"src.ww"];
if (commandexit(td, "build", bav) != 0) {
fail(label, "runtime pin build failed");
};
let rav: []str = [out];
if (commandexit(td, "run", rav) != 37) {
fail(label, "payload/neighbor runtime exit != 37");
};
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);
};