阶段 C 步骤 7:isa 拆出 Image,vm 依赖收窄。

- 删 isa/Image.h + Image.cpp(ImageView/FNV/写文件/sidecar 不再属于 isa)
- vm 自实现只读视图 vm/Image.h/cpp:头 104(12.13)/段校验/缺 SHA 尾报错
  (型号匹配与 SHA 校验留步骤 8);Machine 改用 vm::Image + vm::ImageHeader
- executor:本地读文件(替换 isa::read_stb_file)
- 测试:isa_test 删映像/FNV 部分;codegen_test/vm_test 改用 compiler::StbView /
  vm::ConstEntry;vm_test 手拼映像适配新格式(头 104 + 尾 32 占位)
- 验证:line1 编译 288 字节 → BytecodeExecutor 回放 Q 正确;ctest 12/12
This commit is contained in:
2026-08-21 22:24:57 +08:00
parent a308ae713d
commit 5fccbadaa8
12 changed files with 334 additions and 647 deletions
-104
View File
@@ -12,7 +12,6 @@
#include <vector>
#include "isa/Encode.h"
#include "isa/Image.h"
#include "isa/Instr.h"
#include "isa/Op.h"
#include "isa/Types.h"
@@ -127,114 +126,11 @@ static bool test_encode() {
return true;
}
// ---- 5. FNV-1a 64 ----
static bool test_fnv() {
using namespace isa;
CHECK(fnv1a64(0, 0) == kFnvBasis);
const uint8_t a1[] = {'a'};
CHECK(fnv1a64(a1, 1) == 0xaf63dc4c8601ec8cull);
return true;
}
// ---- 6. 映像:write_ret_main → ImageView ----
static bool test_image() {
using namespace isa;
const uint64_t hash = 0x123456789abcdef0ull;
std::vector<uint8_t> img = write_ret_main(100000, 10, hash);
ImageView v = ImageView::from(img);
CHECK(v.ok());
CHECK(v.error().empty());
CHECK(v.header().cycle_limit == 100000);
CHECK(v.header().dt_ms == 10);
CHECK(v.header().project_hash == hash);
CHECK(v.header().entry_fn_id == 0);
CHECK(v.header().n_funcs == 1);
CHECK(v.header().n_globals == 0);
CHECK(v.header().n_i == 0);
CHECK(v.header().n_q == 0);
CHECK(v.header().n_m == 0);
CHECK(v.header().offset_const == kHeaderSize);
CHECK(v.header().offset_data == kHeaderSize + kFuncRowSize + 4);
CHECK(v.code_len() == 4);
CHECK(v.data_len() == 0);
const FuncRow r = v.func_row(0);
CHECK(r.nregs == 0);
CHECK(r.code_offset == 0);
CHECK(r.code_len == 1);
char buf[32];
disasm(v.code_bytes()[0], buf, sizeof buf);
CHECK(std::strcmp(buf, "RET") == 0);
// 损坏映像必须拒绝
std::vector<uint8_t> bad = img;
bad[0] = 'X';
CHECK(!ImageView::from(bad).ok());
std::vector<uint8_t> bad2 = img;
bad2[60] = 255; // offset_code 越界
CHECK(!ImageView::from(bad2).ok());
std::vector<uint8_t> bad3 = img;
bad3[48] = 2; // n_funcs=2 但表只有 1 行
CHECK(!ImageView::from(bad3).ok());
std::vector<uint8_t> bad4 = img;
bad4[24] = 1; // entry_fn_id 越界
CHECK(!ImageView::from(bad4).ok());
std::vector<uint8_t> short_img = img;
short_img.resize(kHeaderSize - 1);
CHECK(!ImageView::from(short_img).ok());
return true;
}
// ---- 7. .stb 文件与 sidecar ----
static bool test_files() {
using namespace isa;
std::vector<uint8_t> img = write_ret_main(100000, 10, 0);
const char* stb_path = "isa_test_tmp.stb";
std::string err;
CHECK(write_stb_file(stb_path, img, &err));
std::vector<uint8_t> back;
CHECK(read_stb_file(stb_path, &back, &err));
CHECK(back == img);
CHECK(ImageView::from(back).ok());
std::remove(stb_path);
std::vector<IoBinding> bs;
IoBinding b1;
b1.var = "I0_0"; b1.slot = 1; b1.channel = 0; b1.bit = 0; b1.is_input = true;
IoBinding b2;
b2.var = "Q0_0"; b2.slot = 3; b2.channel = 0; b2.bit = 0; b2.is_input = false;
bs.push_back(b1);
bs.push_back(b2);
const std::string sc = make_sidecar(bs);
CHECK(sc.find("[[io.input]]") == 0);
CHECK(sc.find("var = \"I0_0\"") != std::string::npos);
CHECK(sc.find("slot = 1") != std::string::npos);
CHECK(sc.find("channel = 0") != std::string::npos);
CHECK(sc.find("[[io.output]]") != std::string::npos);
CHECK(sc.find("var = \"Q0_0\"") != std::string::npos);
CHECK(sc.find("slot = 3") != std::string::npos);
const char* sc_path = "isa_test_tmp.runtime.toml";
CHECK(write_sidecar_file(sc_path, bs, &err));
std::remove(sc_path);
return true;
}
int main() {
if (!test_sat()) return 1;
if (!test_op()) return 1;
if (!test_instr()) return 1;
if (!test_encode()) return 1;
if (!test_fnv()) return 1;
if (!test_image()) return 1;
if (!test_files()) return 1;
std::printf("isa_test: %d checks passed\n", g_checks);
return 0;
}