Files
ww/test/asm/chain_test.ww
Hojun-Cho ff36d94be4 test: port the chained-index asm observers to ww
739/740/741 -> test/asm/chain_test.ww. Narrow load/store mnemonics,
the u8 write anti-needle, the greedy MOVQ-$N/IMULQ scale-pair
exact counts and per-row byte-id preserved; 741's never-armed
anti-needle machinery dropped as machinery, not assertion.
2026-08-08 14:45:02 +09:00

274 lines
9.4 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");
};