From 8afc74da34c672acffa1d630ff80332c1e890ac6 Mon Sep 17 00:00:00 2001 From: hermes Date: Wed, 26 Aug 2026 11:47:43 +0000 Subject: [PATCH] =?UTF-8?q?r8asm=5Frev=20=E6=B5=8B=E8=AF=95=E7=BA=BF?= =?UTF-8?q?=E7=8B=AC=E7=AB=8B=E6=88=90=E4=BB=93:=E6=96=AD=E8=A8=80=20harne?= =?UTF-8?q?ss=20+=20gen=5Fmacros=20=E7=94=9F=E6=88=90=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + README.md | 37 +++++++++++++++++ build.sh | 26 ++++++++++++ tests/macros_gen.inc | 71 +++++++++++++++++++++++++++++++++ tests/r8_test.cc | 93 +++++++++++++++++++++++++++++++++++++++++++ tools/gen_macros.py | 95 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 323 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 build.sh create mode 100644 tests/macros_gen.inc create mode 100644 tests/r8_test.cc create mode 100644 tools/gen_macros.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..585de58 --- /dev/null +++ b/README.md @@ -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 节点整体消失。改核心之前先跑它,比读代码快。 diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..6d8b580 --- /dev/null +++ b/build.sh @@ -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" diff --git a/tests/macros_gen.inc b/tests/macros_gen.inc new file mode 100644 index 0000000..f0cd2d7 --- /dev/null +++ b/tests/macros_gen.inc @@ -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", {}}}} + }}; diff --git a/tests/r8_test.cc b/tests/r8_test.cc new file mode 100644 index 0000000..59eda2e --- /dev/null +++ b/tests/r8_test.cc @@ -0,0 +1,93 @@ +#include "r8asm/r8asm.hh" + +#include +#include +#include +#include +#include + +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 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 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; +} diff --git a/tools/gen_macros.py b/tools/gen_macros.py new file mode 100644 index 0000000..b088e5c --- /dev/null +++ b/tools/gen_macros.py @@ -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_" (prefixed canonical names, see operator_map) + BUILTIN value -> "builtin_" +""" +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()