From 6c74a1de4fdb7e8daef0afb14bd05dc671b2d73f Mon Sep 17 00:00:00 2001 From: hermes Date: Wed, 26 Aug 2026 12:04:58 +0000 Subject: [PATCH] =?UTF-8?q?flat-ROT8=20=E4=B8=8D=E5=8F=98=E9=87=8F=20+=20m?= =?UTF-8?q?ixed=20codeblock=20=E4=BD=8D=E7=BD=AE=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=E9=87=91=E4=B8=9D=E9=9B=80(=E6=9A=B4=E9=9C=B2=20.anchor=20?= =?UTF-8?q?=E9=94=AE=E5=86=99=E9=94=99=20bug,=E5=BE=85=E6=A0=B8=E5=BF=83?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=90=8E=E9=87=8D=E6=A0=87=E6=95=B0=E5=AD=97?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/r8_test.cc | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/tests/r8_test.cc b/tests/r8_test.cc index 59eda2e..98fafbf 100644 --- a/tests/r8_test.cc +++ b/tests/r8_test.cc @@ -24,6 +24,22 @@ int main() { #include "macros_gen.inc" int fail = 0; + // 不变量:expand_node_to_ins 的输出必须是纯 ROT8 平铺指令流 + // (CODEBLOCK 的 children 里每个节点都必须是叶子 ROT8,不能有嵌套结构) + auto check_flat_rot8 = [&](const ast_node &cb, const char *name) -> int { + int bad = 0; + for (const auto &n : cb.children) { + if (n.type != ast_node_t::ROT8) { + std::cout << " " << name << ": NON-ROT8 child (type " + << static_cast(n.type) << ")\n"; + bad++; + } else if (!n.children.empty()) { + std::cout << " " << name << ": ROT8 leaf with children\n"; + bad++; + } + } + return bad; + }; 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; @@ -33,12 +49,15 @@ int main() { bool len_ok = (expect_len == 0) ? (n > 0) : (n == expect_len); bool dp_ok = conserve ? (a.dataptr == dp0) : (a.dataptr == dp0 + expect_dp_delta); + int flat_bad = check_flat_rot8(out, name); std::cout << name << ": " << n << " ins, dataptr " << dp0 << " -> " << a.dataptr << (len_ok ? "" : " FAIL(len)"); if (!dp_ok) std::cout << " FAIL(dp)"; + if (flat_bad) + std::cout << " FAIL(flat)"; std::cout << "\n"; - if (!len_ok || !dp_ok) + if (!len_ok || !dp_ok || flat_bad) fail++; } catch (const std::exception &e) { std::cout << name << ": EXCEPTION: " << e.what() << "\n"; @@ -53,6 +72,16 @@ int main() { 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); + // 顶层 CODEBLOCK 混合展开:ROT8 + MACROCALL + REPEAT 一起,验证平铺 + test("mixed codeblock", + ast_node{ast_node_t::CODEBLOCK, "", + {{ast_node_t::ROT8, "rot8_stp", {}}, + mcall("set_0", {}), + {ast_node_t::REPEAT, "", + {cst(2), + {ast_node_t::CODEBLOCK, "", + {{ast_node_t::ROT8, "rot8_stp", {}}}}}}}}, + 6, false, 3); // 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);