From 8be05b58662d77a99b1526c49eb140d636554584 Mon Sep 17 00:00:00 2001 From: StarCells Date: Mon, 14 Sep 2026 13:06:41 +0000 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8D=20bef8310=20=E6=96=B0=20API:?= =?UTF-8?q?import=5Ffile=20=E5=B8=A6=20cell=5Fsize=20/=20set=5Fcell=5Fsize?= =?UTF-8?q?=20=E6=92=AD=E7=A7=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- tests/probe_dump.cc | 3 ++- tests/r8_test.cc | 6 +++-- tools/cellsize_probe.cc | 55 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 tools/cellsize_probe.cc diff --git a/tests/probe_dump.cc b/tests/probe_dump.cc index 1197e07..3a7c4a5 100644 --- a/tests/probe_dump.cc +++ b/tests/probe_dump.cc @@ -9,7 +9,8 @@ int main(int argc, char **argv) { return (1); } r8asm a; - a.import_file(argv[1]); + size_t cell_size = (argc >= 3) ? std::stoul(argv[2]) : 8; + a.import_file(argv[1], cell_size); std::cout << a.export_rot8(); return (0); } diff --git a/tests/r8_test.cc b/tests/r8_test.cc index 4dc5c5c..98deb0b 100644 --- a/tests/r8_test.cc +++ b/tests/r8_test.cc @@ -28,8 +28,9 @@ static ast_node cst(int v) { int main() { ast a; - a.scopes[0]["_CELL_SIZE"] = 8; // language constants for %rep counts - a.scopes[0]["_ROL_STEP"] = 7; + // 语言常量(_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; @@ -141,6 +142,7 @@ int main() { // 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); diff --git a/tools/cellsize_probe.cc b/tools/cellsize_probe.cc new file mode 100644 index 0000000..fa2afc3 --- /dev/null +++ b/tools/cellsize_probe.cc @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define private public +#include "r8asm/r8asm.hh" +#undef private + +// cellsize_probe — CELL_SIZE 的播种时序探针(bef8310 起 CELL_SIZE 从编译期常量 +// 变成 ast 的运行时成员,由 set_cell_size 播种)。 +// +// 编译与运行(必须在 r8asm_rev 根目录跑,%include 路径相对 cwd): +// g++ -std=c++20 -Iinclude src/r8asm.cc \ +// r8asm-rev-tests/tools/cellsize_probe.cc -o build/cellsize_probe +// ./build/cellsize_probe asm/test.r8asm +// +// 关注点: +// [A]/[B] 未播种时 CELL_SIZE 是 indeterminate(实测 0,但不保证)、scopes 为空 vector +// → 此时 scopes[0] 是越界写,scopes.back() 是越界读 +// [C] import_file 结束后 CELL_SIZE 与 scopes[0] 才成型(播种发生在 parse 之后, +// 所以 %include 里嵌套那次 import_file 读到的是父层当时的 CELL_SIZE) +int main(int argc, char **argv) { + using ast = r8asm::ast; + + // 1) 直接构造 ast: scopes 默认是什么?CELL_SIZE 默认是什么? + { + ast a; + std::cout << "[A] freshly constructed ast: scopes.size()=" << a.scopes.size() + << " CELL_SIZE(未初始化)=" << a.CELL_SIZE << "\n"; + } + + // 2) 构造 r8asm,打印 import_file 之前的 main_tree.CELL_SIZE + r8asm r; + std::cout << "[B] before any import_file: main_tree.CELL_SIZE=" + << r.main_tree.CELL_SIZE + << " scopes.size()=" << r.main_tree.scopes.size() << "\n"; + + // 3) import_file 之后 CELL_SIZE 与 scopes[0] 的形态 + // 先塞哨兵值,确认 import_file 末尾的 set_cell_size 会把它覆盖成实参 + r.main_tree.CELL_SIZE = 0xDEADBEEF; + r.import_file(argv[1], 8); + std::cout << "[C] after import_file(...,8): CELL_SIZE=" << r.main_tree.CELL_SIZE + << " scopes.size()=" << r.main_tree.scopes.size() << "\n"; + if (!r.main_tree.scopes.empty()) { + for (const auto &kv : r.main_tree.scopes[0]) + std::cout << " scopes[0] " << kv.first << " = " << kv.second << "\n"; + } + std::cout << "tape bytes=" << r.export_rot8().size() << "\n"; + return 0; +}