Files
starcells 8be05b5866 适配 bef8310 新 API:import_file 带 cell_size / set_cell_size 播种
- probe_dump.cc: import_file(argv[1], cell_size),cell_size 可走 argv[2],默认 8
- r8_test.cc: 两处直接写 scopes[0][...] 改成 set_cell_size(8)。bef8310 起
  scopes 默认是空 vector(不再预置全局帧),旧写法是空 vector 上的 scopes[0],
  实测 segfault;tape 用例块里新建的 r8asm 也要自己播种(.xor 展开读
  scopes[0]["_CELL_SIZE"],否则 map::at 抛)
- 新增 tools/cellsize_probe.cc: 测 CELL_SIZE 的播种时序(未播种时 CELL_SIZE
  是 indeterminate、scopes 为空)

验收:build.sh 后端 29 断言全绿;stdmac_matrix.py 627 例 0 失败;asm/test.r8asm
tape 与 2469c3a 版本 md5 一致(264296f5…)、VM 输出 127 11 1 139 0 / 127 11 11 /
127 11 127 / 127 11 116 / 127 1 128 0
2026-09-14 13:06:41 +00:00

205 lines
8.3 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 测试需要直接读写 ast 内部状态(scopes/macros/dataptr),生产代码保持 private。
// 仅在测试 TU 里临时放开访问权限;r8asm.hh 的依赖头必须先包含完,
// 防止 #define 污染标准库内部。
#include <cstddef>
#include <cstdint>
#include <exception>
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#define private public
#include "r8asm/r8asm.hh"
#undef private
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;
// 语言常量(_CELL_SIZE/_CELL_MAX/_ROL_STEP)由 set_cell_size 播种;
// 注入树测试不走 import_file,必须自己调用,否则 scopes 是空 vector。
a.set_cell_size(8);
#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<int>(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;
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);
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 || flat_bad)
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);
// 顶层 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);
// 顺序即状态流:dataptr 在用例间传递(mixed codeblock 之后锚点宏从 dp=3 起步)。
// 锚点宏的展开指令数依赖起始 dataptr,所以精确计数只在当前顺序下有效——
// 重排用例需重标;conserve 检查(回到起点)是不依赖顺序的真正不变量。
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 已 private,走公开路径:通过 .var 展开表达式,
// 再从作用域读结果(顺带覆盖 STRING 名字参数 + VAR 写入路径)
auto data_test = [&](const char *name, ast_node expr, int32_t expect) {
static int seq = 0;
std::string vn = "t" + std::to_string(++seq);
try {
a.expand_node_to_ins(
{ast_node_t::BUILTIN, "builtin_var",
{{ast_node_t::STRING, vn, {}}, expr}});
auto it = a.scopes[0].find(vn);
bool ok = (it != a.scopes[0].end()) && (it->second == expect);
std::cout << "expr " << name << ": "
<< (it != a.scopes[0].end() ? std::to_string(it->second)
: "未写入")
<< (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);
// NEG 已删(2026-08-28): 负数字面量写 0-N,`-` 恒为二元 op_sub
data_test("0-5", op("op_sub", {cst(0), cst(5)}), -5);
data_test("3-(0-2)", op("op_sub", {cst(3), op("op_sub", {cst(0), cst(2)})}),
5);
data_test("1+2*3", op("op_add", {cst(1), op("op_mul", {cst(2), cst(3)})}), 7);
// export_rot8:AST → ROT8 字节码字符串(字符表映射首次被测试)
{
r8asm r;
r.main_tree.set_cell_size(8); // .xor 展开要读 scopes[0]["_CELL_SIZE"]
r.main_tree.macros = a.macros; // 复用已注册的库宏
auto tape = [&](ast_node root) -> std::string {
r.main_tree.root = std::move(root);
r.main_tree.dataptr = 0; // 每个用例从 dp=0 起跑,保持确定性
return r.export_rot8();
};
auto expect_tape = [&](const char *name, ast_node root,
const std::string &expect) {
try {
std::string got = tape(std::move(root));
bool ok = (got == expect);
std::cout << "tape " << name << ": \"" << got << "\""
<< (ok ? "" : " FAIL") << " (expect \"" << expect << "\")\n";
if (!ok)
fail++;
} catch (const std::exception &e) {
std::cout << "tape " << name << ": EXCEPTION: " << e.what() << "\n";
fail++;
}
};
auto rot8 = [](const char *name) {
return ast_node{ast_node_t::ROT8, std::string("rot8_") + name, {}};
};
// 全 opcode 字符表:rot8_t 枚举序 vs 字符表一致(表错位是最经典的 bug)
expect_tape("all opcodes",
{ast_node_t::CODEBLOCK, "",
{rot8("stp"), rot8("btp"), rot8("ror"), rot8("flb"),
rot8("biz"), rot8("rnz"), rot8("out"), rot8("inp")}},
"><+-[].,");
// 宏路径:set_0 = biz flb rnz
expect_tape("set_0", {ast_node_t::CODEBLOCK, "",
{{ast_node_t::MACROCALL, "set_0", {}}}},
"[-]");
// REPEAT 平铺
expect_tape("repeat 2x stp",
{ast_node_t::CODEBLOCK, "",
{{ast_node_t::REPEAT, "",
{ast_node{ast_node_t::CONSTANT, "2", {}},
{ast_node_t::CODEBLOCK, "", {rot8("stp")}}}}}},
">>");
// .tp 2:dp 0 -> 2
expect_tape(".tp 2",
{ast_node_t::CODEBLOCK, "",
{{ast_node_t::BUILTIN, "builtin_tp",
{{ast_node_t::CONSTANT, "2", {}}}}}},
">>");
// .xor 5:0b101 → flb,ror,ror,flb,ror×6 → "-++-++++++"
expect_tape(".xor 5",
{ast_node_t::CODEBLOCK, "",
{{ast_node_t::BUILTIN, "builtin_xor",
{{ast_node_t::CONSTANT, "5", {}}}}}},
"-++-++++++");
// 空 root
expect_tape("empty", {ast_node_t::CODEBLOCK, "", {}}, "");
}
std::cout << (fail ? "FAILED: " : "ALL OK: ") << fail << " failures\n";
return fail ? 1 : 0;
}