- tools/stdmac_matrix.py: raw_add/and/or/xor/inc 全量对拍(真链路:probe_dump 汇编 -> ROT-8 VM -> 读 out) - tools/stdmac_xor_fix_probe.py + tests/xor_fix_probe.r8asm.frag: 候选体 = OR 体删末块 %3 set_1,121/121 通过 - tools/stdmac_inc_fix_probe.py + tests/inc_fix_probe.r8asm.frag: 最小修法 = 对调 bit_raw_inc 第 3/4 实参,22/22 + 链式通过 - tools/stdmac_preserve.py: 五个 raw_ 宏均保留输入(A/B/Ci) - tests/probe_dump.cc: 任意 .r8asm -> tape 的驱动(须在 r8asm_rev 根目录跑,include 是相对 cwd)
145 lines
4.9 KiB
Python
145 lines
4.9 KiB
Python
#!/usr/bin/env python3
|
|
"""stdmac_matrix.py — r8asm_rev 标准宏真值矩阵压测。
|
|
|
|
对 raw_add / raw_and / raw_or / raw_xor / raw_inc 逐用例生成一段 .r8asm,
|
|
走真实链路(probe_dump 汇编 -> ROT-8 VM 运行 -> 读 out 字节),与 Python 参考语义对拍。
|
|
|
|
用法:
|
|
python3 tools/stdmac_matrix.py [r8asm_rev 路径]
|
|
|
|
依赖: ../r8asm_rev(核心仓)、../ROT-8(VM 仓),二者都会用 g++/gcc 现场编译探针。
|
|
"""
|
|
import itertools
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
HERE = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
REV = os.path.abspath(sys.argv[1]) if len(sys.argv) > 1 else os.path.join(os.path.dirname(HERE), "r8asm_rev")
|
|
ROT8 = os.path.join(os.path.dirname(HERE), "ROT-8")
|
|
BUILD = os.path.join(HERE, "build")
|
|
|
|
PROBE = os.path.join(BUILD, "probe_dump")
|
|
VM = os.path.join(BUILD, "rot8_vm")
|
|
|
|
|
|
def build_tools():
|
|
os.makedirs(BUILD, exist_ok=True)
|
|
subprocess.run(
|
|
["g++", "-std=c++20", "-I", os.path.join(REV, "include"),
|
|
os.path.join(REV, "src", "r8asm.cc"), os.path.join(HERE, "tests", "probe_dump.cc"),
|
|
"-o", PROBE], check=True)
|
|
subprocess.run(
|
|
["gcc", "-std=c11", "-I", os.path.join(ROT8, "include"),
|
|
*[os.path.join(ROT8, "src", f) for f in os.listdir(os.path.join(ROT8, "src")) if f.endswith(".c")],
|
|
os.path.join(ROT8, "tests", "vm_test.c"), "-o", VM], check=True)
|
|
|
|
|
|
PRELUDE = """%include "asm/lib.r8asm"
|
|
%include "asm/raw_add.r8asm"
|
|
%include "asm/raw_and.r8asm"
|
|
%include "asm/raw_or.r8asm"
|
|
%include "asm/raw_xor.r8asm"
|
|
%include "asm/raw_inc.r8asm"
|
|
|
|
.var A 0
|
|
.var B 2
|
|
.var Ci 4
|
|
.var S 6
|
|
.var Co 8
|
|
.var tmp 1
|
|
"""
|
|
|
|
|
|
def set_cell(name, value):
|
|
return ".tp {n}\n.xor {v}\n".format(n=name, v=value)
|
|
|
|
|
|
def run_case(src):
|
|
"""汇编 + 跑 VM,返回 out 吐出的整数列表。"""
|
|
with tempfile.NamedTemporaryFile("w", suffix=".r8asm", delete=False, dir=REV) as f:
|
|
f.write(src)
|
|
path = f.name
|
|
tape = tempfile.NamedTemporaryFile("w", suffix=".rot8", delete=False)
|
|
tape.close()
|
|
try:
|
|
asm = subprocess.run([PROBE, path], cwd=REV, capture_output=True, text=True)
|
|
if asm.returncode != 0:
|
|
raise RuntimeError("汇编失败: " + asm.stderr.strip()[-300:])
|
|
with open(tape.name, "w") as fh:
|
|
fh.write(asm.stdout)
|
|
vm = subprocess.run([VM, tape.name], capture_output=True, text=True)
|
|
if vm.returncode != 0:
|
|
raise RuntimeError("VM 失败: " + (vm.stderr.strip() or "rc=%d" % vm.returncode)[-300:])
|
|
return [int(line) for line in vm.stdout.split() if line.strip()]
|
|
finally:
|
|
os.unlink(path)
|
|
os.unlink(tape.name)
|
|
|
|
|
|
def cmp(name, got, want, case, fails):
|
|
ok = got == want
|
|
if not ok:
|
|
fails.append((name, case, want, got))
|
|
return ok
|
|
|
|
|
|
def main():
|
|
build_tools()
|
|
fails = []
|
|
fails_exec = []
|
|
|
|
def safe(name, src, want, case):
|
|
try:
|
|
got = run_case(src)
|
|
except RuntimeError as e:
|
|
fails_exec.append((name, case, str(e)))
|
|
return
|
|
cmp(name, got, want, case, fails)
|
|
|
|
VALS = [0, 1, 2, 3, 5, 126, 127, 128, 170, 254, 255]
|
|
|
|
# ---- raw_add: S = (a+b+cin) & 0xFF, Co = carry out ----
|
|
for a, b, cin in itertools.product(VALS, VALS, [0, 1]):
|
|
src = PRELUDE + set_cell("A", a) + set_cell("B", b) + set_cell("Ci", cin)
|
|
src += "raw_add A B Ci S Co tmp\noutput S\noutput Co\n"
|
|
tot = a + b + cin
|
|
safe("raw_add", src, [tot & 0xFF, (tot >> 8) & 1], (a, b, cin))
|
|
|
|
# ---- raw_and / raw_or / raw_xor ----
|
|
for op, py in (("raw_and", lambda x, y: x & y), ("raw_or", lambda x, y: x | y), ("raw_xor", lambda x, y: x ^ y)):
|
|
for a, b in itertools.product(VALS, VALS):
|
|
src = PRELUDE + set_cell("A", a) + set_cell("B", b)
|
|
src += "%s A B S tmp\noutput S\n" % op
|
|
safe(op, src, [py(a, b)], (a, b))
|
|
|
|
# ---- raw_inc: S = (a+cin) & 0xFF, Co = carry out ----
|
|
for a, cin in itertools.product(VALS, [0, 1]):
|
|
src = PRELUDE + set_cell("A", a) + set_cell("Ci", cin)
|
|
src += "raw_inc A Ci S Co tmp\noutput S\noutput Co\n"
|
|
tot = a + cin
|
|
safe("raw_inc", src, [tot & 0xFF, (tot >> 8) & 1], (a, cin))
|
|
|
|
for name in ("raw_add", "raw_and", "raw_or", "raw_xor", "raw_inc"):
|
|
n_all = len([f for f in fails if f[0] == name])
|
|
n_err = len([f for f in fails_exec if f[0] == name])
|
|
print("%-9s 语义错 %-4d 展开/运行炸 %-4d" % (name, n_all, n_err))
|
|
|
|
if fails:
|
|
print("\n--- 语义错样例(最多 12 条) ---")
|
|
for name, case, want, got in fails[:12]:
|
|
print(" %-9s %-14s want=%-12s got=%s" % (name, case, want, got))
|
|
if fails_exec:
|
|
print("\n--- 展开/运行炸(最多 5 条) ---")
|
|
for name, case, err in fails_exec[:5]:
|
|
print(" %-9s %-14s %s" % (name, case, err))
|
|
|
|
total = len(fails) + len(fails_exec)
|
|
print("\n总失败 %d 例" % total)
|
|
return 1 if total else 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|