适配 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
This commit is contained in:
starcells committed 2026-09-14 13:06:41 +00:00
1 parent d452e5cda1
commit 8be05b5866
3 files changed
+61 -3

No files matched your search

+2 -1
View File
@@ -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);
}
+4 -2
View File
@@ -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);
+55
View File
@@ -0,0 +1,55 @@
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#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;
}