r8asm_rev 测试线独立成仓:断言 harness + gen_macros 生成器

This commit is contained in:
hermes committed 2026-08-26 11:47:43 +00:00
commit 8afc74da34
6 files changed
+323

No files matched your search

+1
View File
@@ -0,0 +1 @@
build/
+37
View File
@@ -0,0 +1,37 @@
# r8asm-rev-tests
r8asm_rev(ROT-8 汇编器重构版)的独立测试线。核心代码在
[starcells/r8asm_rev](https://gitea.starcells.ltd/starcells/r8asm_rev),本仓库只装测试和生成器,
互不干扰——测试怎么改都不碰核心仓,核心仓的 pull/push 也不会被本地测试文件挡路。
## 依赖
- **r8asm_rev**(必需):编译时需要它的 `include/` 和 `src/r8asm.cc`
- **R8ASM**(可选):`asm/lib.r8asm` 是 `macros_gen.inc` 的输入源,存在时构建脚本会重新生成
## 构建与运行
```bash
# 默认找同级的 ../r8asm_rev;布局不同就显式传路径
./build.sh
./build.sh /path/to/r8asm_rev
```
构建脚本会:重新生成 `tests/macros_gen.inc`(若 R8ASM 在)→ 编译 → 跑断言。
## 结构
- `tests/r8_test.cc` — 断言线:11 个宏展开(set_0/copy/clb_bit_mov…) + 11 个表达式求值,
每条都校验指令数和 dataptr 守恒。宏展开用 `a.macros[...]` 直接注入(见下)
- `tests/macros_gen.inc` — 生成器产物(已提交,保证无 R8ASM 也能构建)
- `tools/gen_macros.py` — 把旧 R8ASM 的 `asm/lib.r8asm` 转成 C++ ast_node 初始化代码
```bash
python3 tools/gen_macros.py # 默认 ../R8ASM/asm/lib.r8asm
python3 tools/gen_macros.py /path/lib.r8asm # 显式指定
```
## 历史战绩
这套线曾在 30 分钟内抓住 4 个 bug:REPEAT fallthrough、一元操作符越界两次、
`expand_node_to_ins` 改签名后 ROT8 节点整体消失。改核心之前先跑它,比读代码快。
Executable
+26
View File
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
# r8asm-rev-tests 构建脚本
# 用法: ./build.sh [r8asm_rev 路径]
# 默认取本仓库同级的 ../r8asm_rev(即 /opt/data/r8asm_rev 这种布局)
# 若 R8ASM/asm/lib.r8asm 存在则重新生成 macros_gen.inc,否则用仓库里已生成的
set -e
HERE="$(cd "$(dirname "$0")" && pwd)"
REV_DIR="${1:-$(dirname "$HERE")/r8asm_rev}"
if [ ! -f "$REV_DIR/include/r8asm/r8asm.hh" ]; then
echo "ERROR: 找不到 r8asm_rev 核心仓: $REV_DIR" >&2
echo "用法: ./build.sh [r8asm_rev 路径]" >&2
exit 1
fi
mkdir -p "$HERE/build"
if [ -f "$HERE/../R8ASM/asm/lib.r8asm" ]; then
echo "[gen] 重新生成 macros_gen.inc (来自 ../R8ASM/asm/lib.r8asm)"
python3 "$HERE/tools/gen_macros.py" > "$HERE/tests/macros_gen.inc"
fi
g++ -std=c++20 -I"$REV_DIR/include" \
"$REV_DIR/src/r8asm.cc" \
"$HERE/tests/r8_test.cc" \
-o "$HERE/build/r8asm_test"
"$HERE/build/r8asm_test"
+71
View File
@@ -0,0 +1,71 @@
a.macros["step"] = ast_node{ast_node_t::CODEBLOCK, "", {
ast_node{ast_node_t::REPEAT, "", {ast_node{ast_node_t::PARAMETER, "1", {}}, ast_node{ast_node_t::CODEBLOCK, "", {ast_node{ast_node_t::ROT8, "rot8_stp", {}}}}}},
ast_node{ast_node_t::REPEAT, "", {ast_node{ast_node_t::PARAMETER, "1", {}}, ast_node{ast_node_t::CODEBLOCK, "", {ast_node{ast_node_t::ROT8, "rot8_stp", {}}}}}}
}};
a.macros["back"] = ast_node{ast_node_t::CODEBLOCK, "", {
ast_node{ast_node_t::REPEAT, "", {ast_node{ast_node_t::PARAMETER, "1", {}}, ast_node{ast_node_t::CODEBLOCK, "", {ast_node{ast_node_t::ROT8, "rot8_btp", {}}}}}},
ast_node{ast_node_t::REPEAT, "", {ast_node{ast_node_t::PARAMETER, "1", {}}, ast_node{ast_node_t::CODEBLOCK, "", {ast_node{ast_node_t::ROT8, "rot8_btp", {}}}}}}
}};
a.macros["set_0"] = ast_node{ast_node_t::CODEBLOCK, "", {
ast_node{ast_node_t::ROT8, "rot8_biz", {}},
ast_node{ast_node_t::ROT8, "rot8_flb", {}},
ast_node{ast_node_t::ROT8, "rot8_rnz", {}}
}};
a.macros["set_1"] = ast_node{ast_node_t::CODEBLOCK, "", {
ast_node{ast_node_t::MACROCALL, "set_0", {}},
ast_node{ast_node_t::ROT8, "rot8_flb", {}}
}};
a.macros["erase"] = ast_node{ast_node_t::CODEBLOCK, "", {
ast_node{ast_node_t::REPEAT, "", {ast_node{ast_node_t::VARIABLE, "_CELL_SIZE", {}}, ast_node{ast_node_t::CODEBLOCK, "", {ast_node{ast_node_t::MACROCALL, "set_0", {}}, ast_node{ast_node_t::ROT8, "rot8_ror", {}}}}}}
}};
a.macros["rol"] = ast_node{ast_node_t::CODEBLOCK, "", {
ast_node{ast_node_t::REPEAT, "", {ast_node{ast_node_t::VARIABLE, "_ROL_STEP", {}}, ast_node{ast_node_t::CODEBLOCK, "", {ast_node{ast_node_t::ROT8, "rot8_ror", {}}}}}}
}};
a.macros["clb_bit_mov"] = ast_node{ast_node_t::CODEBLOCK, "", {
ast_node{ast_node_t::BUILTIN, "builtin_anchor", {ast_node{ast_node_t::VARIABLE, "before_clb_bit_mov", {}}}},
ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::PARAMETER, "1", {}}}},
ast_node{ast_node_t::MACROCALL, "set_0", {}},
ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::VARIABLE, "before_clb_bit_mov", {}}}},
ast_node{ast_node_t::ROT8, "rot8_biz", {}},
ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::PARAMETER, "1", {}}}},
ast_node{ast_node_t::ROT8, "rot8_flb", {}},
ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::VARIABLE, "before_clb_bit_mov", {}}}},
ast_node{ast_node_t::ROT8, "rot8_flb", {}},
ast_node{ast_node_t::ROT8, "rot8_rnz", {}},
ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::VARIABLE, "before_clb_bit_mov", {}}}}
}};
a.macros["bit_copy"] = ast_node{ast_node_t::CODEBLOCK, "", {
ast_node{ast_node_t::BUILTIN, "builtin_anchor", {ast_node{ast_node_t::VARIABLE, "before_bit_copy", {}}}},
ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::PARAMETER, "1", {}}}},
ast_node{ast_node_t::MACROCALL, "set_0", {}},
ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::PARAMETER, "2", {}}}},
ast_node{ast_node_t::MACROCALL, "set_0", {}},
ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::VARIABLE, "before_bit_copy", {}}}},
ast_node{ast_node_t::ROT8, "rot8_biz", {}},
ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::PARAMETER, "1", {}}}},
ast_node{ast_node_t::ROT8, "rot8_flb", {}},
ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::PARAMETER, "2", {}}}},
ast_node{ast_node_t::ROT8, "rot8_flb", {}},
ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::VARIABLE, "before_bit_copy", {}}}},
ast_node{ast_node_t::ROT8, "rot8_flb", {}},
ast_node{ast_node_t::ROT8, "rot8_rnz", {}},
ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::PARAMETER, "2", {}}}},
ast_node{ast_node_t::MACROCALL, "clb_bit_mov", {ast_node{ast_node_t::VARIABLE, "before_bit_copy", {}}}},
ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::VARIABLE, "before_bit_copy", {}}}}
}};
a.macros["copy"] = ast_node{ast_node_t::CODEBLOCK, "", {
ast_node{ast_node_t::BUILTIN, "builtin_anchor", {ast_node{ast_node_t::VARIABLE, "before_copy", {}}}},
ast_node{ast_node_t::REPEAT, "", {ast_node{ast_node_t::VARIABLE, "_CELL_SIZE", {}}, ast_node{ast_node_t::CODEBLOCK, "", {ast_node{ast_node_t::MACROCALL, "bit_copy", {ast_node{ast_node_t::PARAMETER, "1", {}}, ast_node{ast_node_t::PARAMETER, "2", {}}}}, ast_node{ast_node_t::ROT8, "rot8_ror", {}}, ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::PARAMETER, "1", {}}}}, ast_node{ast_node_t::ROT8, "rot8_ror", {}}, ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::PARAMETER, "2", {}}}}, ast_node{ast_node_t::ROT8, "rot8_ror", {}}, ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::VARIABLE, "before_copy", {}}}}}}}}
}};
a.macros["bit_copy_to_buffer"] = ast_node{ast_node_t::CODEBLOCK, "", {
ast_node{ast_node_t::ROT8, "rot8_stp", {}},
ast_node{ast_node_t::BUILTIN, "builtin_anchor", {ast_node{ast_node_t::VARIABLE, "bit_copy_to_buffer_buf_1", {}}}},
ast_node{ast_node_t::ROT8, "rot8_btp", {}},
ast_node{ast_node_t::MACROCALL, "bit_copy", {ast_node{ast_node_t::VARIABLE, "bit_copy_to_buffer_buf_1", {}}, ast_node{ast_node_t::PARAMETER, "1", {}}}}
}};
a.macros["copy_to_buffer"] = ast_node{ast_node_t::CODEBLOCK, "", {
ast_node{ast_node_t::ROT8, "rot8_stp", {}},
ast_node{ast_node_t::BUILTIN, "builtin_anchor", {ast_node{ast_node_t::VARIABLE, "copy_to_buffer_buf_1", {}}}},
ast_node{ast_node_t::ROT8, "rot8_btp", {}},
ast_node{ast_node_t::MACROCALL, "copy", {ast_node{ast_node_t::VARIABLE, "copy_to_buffer_buf_1", {}}, ast_node{ast_node_t::PARAMETER, "1", {}}}}
}};
+93
View File
@@ -0,0 +1,93 @@
#include "r8asm/r8asm.hh"
#include <cstdint>
#include <exception>
#include <iostream>
#include <string>
#include <vector>
using ast = r8asm::ast;
using ast_node = r8asm::ast_node;
using ast_node_t = r8asm::ast_node_t;
static ast_node mcall(const std::string &name, std::vector<ast_node> args) {
return ast_node{ast_node_t::MACROCALL, name, std::move(args)};
}
static ast_node cst(int v) {
return ast_node{ast_node_t::CONSTANT, std::to_string(v), {}};
}
int main() {
ast a;
a.scopes[0]["_CELL_SIZE"] = 8; // language constants for %rep counts
a.scopes[0]["_ROL_STEP"] = 7;
#include "macros_gen.inc"
int fail = 0;
auto test = [&](const char *name, ast_node call, size_t expect_len,
bool conserve, int32_t expect_dp_delta = 0) {
uint32_t dp0 = a.dataptr;
try {
auto out = a.expand_node_to_ins(call);
size_t n = out.children.size();
bool len_ok = (expect_len == 0) ? (n > 0) : (n == expect_len);
bool dp_ok = conserve ? (a.dataptr == dp0)
: (a.dataptr == dp0 + expect_dp_delta);
std::cout << name << ": " << n << " ins, dataptr " << dp0 << " -> "
<< a.dataptr << (len_ok ? "" : " FAIL(len)");
if (!dp_ok)
std::cout << " FAIL(dp)";
std::cout << "\n";
if (!len_ok || !dp_ok)
fail++;
} catch (const std::exception &e) {
std::cout << name << ": EXCEPTION: " << e.what() << "\n";
fail++;
}
};
test("set_0", mcall("set_0", {}), 3, true);
test("set_1", mcall("set_1", {}), 4, true);
test("rol", mcall("rol", {}), 7, true);
test("erase", mcall("erase", {}), 32, true);
test("step 2", mcall("step", {cst(2)}), 4, false, 4);
test("back 2", mcall("back", {cst(2)}), 4, false, -4);
test("clb_bit_mov 5", mcall("clb_bit_mov", {cst(5)}), 27, true);
// nested macros: assert non-empty + dataptr conserved, print actual length
test("bit_copy 3 5", mcall("bit_copy", {cst(3), cst(5)}), 0, true);
test("copy 3 5", mcall("copy", {cst(3), cst(5)}), 0, true);
test("bit_copy_to_buffer 5", mcall("bit_copy_to_buffer", {cst(5)}), 0, true);
test("copy_to_buffer 5", mcall("copy_to_buffer", {cst(5)}), 0, true);
// expand_node_to_data: expression evaluation with op_ prefixed operators
auto data_test = [&](const char *name, ast_node node, int32_t expect) {
try {
int32_t v = a.expand_node_to_data(node);
bool ok = (v == expect);
std::cout << "expr " << name << ": " << v << (ok ? "" : " FAIL")
<< " (expect " << expect << ")\n";
if (!ok)
fail++;
} catch (const std::exception &e) {
std::cout << "expr " << name << ": EXCEPTION: " << e.what() << "\n";
fail++;
}
};
auto op = [](const std::string &name, std::vector<ast_node> args) {
return ast_node{ast_node_t::OPERATOR, name, std::move(args)};
};
data_test("1+2", op("op_add", {cst(1), cst(2)}), 3);
data_test("3-2", op("op_sub", {cst(3), cst(2)}), 1);
data_test("2*3", op("op_mul", {cst(2), cst(3)}), 6);
data_test("6/2", op("op_div", {cst(6), cst(2)}), 3);
data_test("5^3", op("op_xor", {cst(5), cst(3)}), 6);
data_test("5&3", op("op_and", {cst(5), cst(3)}), 1);
data_test("5|3", op("op_or", {cst(5), cst(3)}), 7);
data_test("~0", op("op_not", {cst(0)}), -1);
data_test("-5", op("op_neg", {cst(5)}), -5);
data_test("3--2", op("op_sub", {cst(3), op("op_neg", {cst(2)})}), 5);
data_test("1+2*3", op("op_add", {cst(1), op("op_mul", {cst(2), cst(3)})}), 7);
std::cout << (fail ? "FAILED: " : "ALL OK: ") << fail << " failures\n";
return fail ? 1 : 0;
}
+95
View File
@@ -0,0 +1,95 @@
#!/usr/bin/env python3
"""lib.r8asm -> C++ ast_node aggregate-init code (one-shot test harness generator).
New-arch mapping:
%rep N ... %endrep -> REPEAT{count, CODEBLOCK{body}}
`stp %1` (param on ins) -> REPEAT{count=%1, CODEBLOCK{[stp]}} (ins are param-less now)
_CELL_SIZE/_ROL_STEP -> VARIABLE (resolved via global frame, preset in test)
ROT8 value -> "rot8_<name>" (prefixed canonical names, see operator_map)
BUILTIN value -> "builtin_<name>"
"""
import os
import re
import sys
ROT8 = {"stp", "btp", "ror", "flb", "biz", "rnz", "out", "inp"}
# lib.r8asm 路径:默认取本仓库旁 R8ASM/asm/lib.r8asm,可用参数覆盖:
# python3 tools/gen_macros.py [path/to/lib.r8asm]
HERE = os.path.dirname(os.path.abspath(__file__))
DEFAULT_LIB = os.path.normpath(os.path.join(HERE, "../../R8ASM/asm/lib.r8asm"))
LIB_PATH = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_LIB
def expr(tok):
"""Single-token -> ast_node initializer."""
if re.fullmatch(r"-?\d+", tok):
return f'ast_node{{ast_node_t::CONSTANT, "{tok}", {{}}}}'
if tok.startswith("%"):
return f'ast_node{{ast_node_t::PARAMETER, "{tok[1:]}", {{}}}}'
return f'ast_node{{ast_node_t::VARIABLE, "{tok}", {{}}}}'
def line_to_node(tokens):
head = tokens[0]
args = ", ".join(expr(t) for t in tokens[1:])
if head.startswith("."):
return f'ast_node{{ast_node_t::BUILTIN, "builtin_{head[1:]}", {{{args}}}}}'
if head in ROT8:
if len(tokens) > 1:
body = (f'ast_node{{ast_node_t::CODEBLOCK, "", '
f'{{ast_node{{ast_node_t::ROT8, "rot8_{head}", {{}}}}}}}}')
return f'ast_node{{ast_node_t::REPEAT, "", {{{expr(tokens[1])}, {body}}}}}'
return f'ast_node{{ast_node_t::ROT8, "rot8_{head}", {{}}}}'
return f'ast_node{{ast_node_t::MACROCALL, "{head}", {{{args}}}}}'
def parse_body(lines):
"""Lines of a macro body -> list of node initializer strings (%rep handled)."""
nodes = []
i = 0
while i < len(lines):
line = lines[i]
m = re.match(r"^\s*%rep\s+(\S+)", line)
if m:
count = m.group(1)
j = i + 1
body = []
while j < len(lines) and not re.match(r"^\s*%endrep", lines[j]):
body.append(lines[j]); j += 1
inner = ", ".join(parse_body(body))
nodes.append(
f'ast_node{{ast_node_t::REPEAT, "", {{{expr(count)}, '
f'ast_node{{ast_node_t::CODEBLOCK, "", {{{inner}}}}}}}}}')
i = j + 1
continue
if re.match(r"^\s*%endrep", line):
i += 1
continue
nodes.append(line_to_node(line.split()))
i += 1
return nodes
def main():
raw = open(LIB_PATH).read()
lines = [l for l in raw.splitlines()
if l.strip() and not l.strip().startswith(";")]
macros = {}
cur = None
body = []
for l in lines:
m = re.match(r"^\s*%macro\s+(\S+)\s+(\d+)", l)
if m:
cur = m.group(1); body = []
continue
if re.match(r"^\s*%endmacro", l):
macros[cur] = body
cur = None
continue
if cur:
body.append(l)
for name, b in macros.items():
stmts = ",\n ".join(parse_body(b))
print(f' a.macros["{name}"] = ast_node{{ast_node_t::CODEBLOCK, "", {{')
print(f' {stmts}')
print(' }};')
if __name__ == "__main__":
main()