阶段 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
+67 -67
View File
@@ -16,7 +16,7 @@
#include "compiler/Project.h"
#include "compiler/Typecheck.h"
#include "isa/Encode.h"
#include "isa/Image.h"
#include "compiler/Stb.h"
#ifndef REPO_ROOT
#define REPO_ROOT "."
@@ -123,17 +123,17 @@ static bool test_case01() {
std::string err;
CHECK(compile_case("01_empty_main", &img, &err));
const isa::ImageView v = isa::ImageView::from(img);
const compiler::StbView v = compiler::StbView::from(img);
CHECK(v.ok());
CHECK(v.header().cycle_limit == 1000);
CHECK(v.header().dt_ms == 10);
CHECK(v.header().entry_fn_id == 0);
CHECK(v.header().n_funcs == 1);
CHECK(v.header().n_consts == 0);
CHECK(v.header().n_globals == 0);
CHECK(v.header().project_hash != isa::kFnvBasis);
CHECK(v.cycle_limit() == 1000);
CHECK(v.dt_ms() == 10);
CHECK(v.entry_fn_id() == 0);
CHECK(v.n_funcs() == 1);
CHECK(v.n_consts() == 0);
CHECK(v.n_globals() == 0);
CHECK(v.project_hash() != compiler::kFnvBasis);
const isa::FuncRow r = v.func_row(0);
const compiler::StbView::FuncRow r = v.func_row(0);
CHECK(r.nregs == 8); // 帧基址(调用约定区)
CHECK(r.code_len == 1);
@@ -150,12 +150,12 @@ static bool test_case02() {
std::string err;
CHECK(compile_case("02_bool_assign", &img, &err));
const isa::ImageView v = isa::ImageView::from(img);
const compiler::StbView v = compiler::StbView::from(img);
CHECK(v.ok());
CHECK(v.header().n_funcs == 1);
CHECK(v.header().n_consts == 2); // TRUE 与 FALSE
CHECK(v.n_funcs() == 1);
CHECK(v.n_consts() == 2); // TRUE 与 FALSE
const isa::FuncRow r = v.func_row(0);
const compiler::StbView::FuncRow r = v.func_row(0);
CHECK(r.nregs == 10); // a, br8 起)
CHECK(r.code_len == 3);
@@ -170,10 +170,10 @@ static bool test_case02() {
CHECK(std::strcmp(buf, "RET") == 0);
// 常量表:0 = BOOL 11 = BOOL 0
const isa::ConstEntry c0 = v.const_entry(0);
CHECK(c0.tag == isa::types::Bool && c0.value == 1);
const isa::ConstEntry c1 = v.const_entry(1);
CHECK(c1.tag == isa::types::Bool && c1.value == 0);
const compiler::ConstEntry c0 = v.const_entry(0);
CHECK(c0.tag == 0 && c0.value == 1);
const compiler::ConstEntry c1 = v.const_entry(1);
CHECK(c1.tag == 0 && c1.value == 0);
return true;
}
@@ -184,14 +184,14 @@ static bool test_case09() {
std::string err;
CHECK(compile_case("09_gvl_external", &img, &err));
const isa::ImageView v = isa::ImageView::from(img);
const compiler::StbView v = compiler::StbView::from(img);
CHECK(v.ok());
CHECK(v.header().n_globals == 1);
CHECK(v.data_len() == 8 + 32); // 8 字节定宽槽 + SHA-256
CHECK(v.n_globals() == 1);
CHECK(v.data_len() == 8); // 8 字节定宽槽(不含 SHA 尾
CHECK(v.data_bytes()[0] == 5); // G1 初值 5(小端低位)
CHECK(v.data_bytes()[1] == 0);
const isa::FuncRow r = v.func_row(0);
const compiler::StbView::FuncRow r = v.func_row(0);
CHECK(r.nregs == 9); // r8 起:仅局部 x
CHECK(r.code_len == 2);
@@ -221,11 +221,11 @@ static bool test_io_ops() {
"END_PROGRAM\n";
CHECK(compile_src("io", io_extra, globals_st, main_st, &img, &err));
const isa::ImageView v = isa::ImageView::from(img);
const compiler::StbView v = compiler::StbView::from(img);
CHECK(v.ok());
CHECK(v.header().n_globals == 2);
CHECK(v.n_globals() == 2);
const isa::FuncRow r = v.func_row(0);
const compiler::StbView::FuncRow r = v.func_row(0);
CHECK(r.nregs == 10); // r8 起:q + 1 临时
char buf[64];
@@ -258,10 +258,10 @@ static bool test_layout() {
"END_PROGRAM\n";
CHECK(compile_src("layout", "", globals_st, main_st, &img, &err));
const isa::ImageView v = isa::ImageView::from(img);
const compiler::StbView v = compiler::StbView::from(img);
CHECK(v.ok());
CHECK(v.header().n_globals == 3);
CHECK(v.data_len() == 24 + 32); // 3 槽 × 8 字节定宽 + SHA-256 尾
CHECK(v.n_globals() == 3);
CHECK(v.data_len() == 24); // 3 槽 × 8 字节定宽
CHECK(v.data_bytes()[0] == 0); // B 无初值
CHECK(v.data_bytes()[8] == 0x2C && v.data_bytes()[9] == 0x01); // I = 300(槽 1
CHECK(v.data_bytes()[16] == 10); // T = 10ms(槽 2
@@ -299,9 +299,9 @@ static bool test_case03() {
std::string err;
CHECK(compile_case("03_short_circuit", &img, &err));
const isa::ImageView v = isa::ImageView::from(img);
const compiler::StbView v = compiler::StbView::from(img);
CHECK(v.ok());
const isa::FuncRow r = v.func_row(0);
const compiler::StbView::FuncRow r = v.func_row(0);
CHECK(r.nregs == 12); // r8 起:a b x + 1 临时
CHECK(r.code_len == 9);
@@ -342,9 +342,9 @@ static bool test_not() {
"END_PROGRAM\n";
CHECK(compile_src("not", "", "", main_st, &img, &err));
const isa::ImageView v = isa::ImageView::from(img);
const compiler::StbView v = compiler::StbView::from(img);
CHECK(v.ok());
const isa::FuncRow r = v.func_row(0);
const compiler::StbView::FuncRow r = v.func_row(0);
CHECK(r.nregs == 11); // r8 起:a x + 1 临时
CHECK(r.code_len == 3);
@@ -367,9 +367,9 @@ static bool test_case04() {
std::string err;
CHECK(compile_case("04_if_elsif_else", &img, &err));
const isa::ImageView v = isa::ImageView::from(img);
const compiler::StbView v = compiler::StbView::from(img);
CHECK(v.ok());
const isa::FuncRow r = v.func_row(0);
const compiler::StbView::FuncRow r = v.func_row(0);
CHECK(r.nregs == 13); // r8 起:sel out + 3 临时
CHECK(r.code_len == 14);
@@ -417,9 +417,9 @@ static bool test_case05() {
std::string err;
CHECK(compile_case("05_while_normal", &img, &err));
const isa::ImageView v = isa::ImageView::from(img);
const compiler::StbView v = compiler::StbView::from(img);
CHECK(v.ok());
const isa::FuncRow r = v.func_row(0);
const compiler::StbView::FuncRow r = v.func_row(0);
CHECK(r.nregs == 12); // r8 起:n + 3 临时(条件结果 r9 + 两操作数)
CHECK(r.code_len == 10);
@@ -458,9 +458,9 @@ static bool test_case07() {
std::string err;
CHECK(compile_case("07_int_arith", &img, &err));
const isa::ImageView v = isa::ImageView::from(img);
const compiler::StbView v = compiler::StbView::from(img);
CHECK(v.ok());
const isa::FuncRow r = v.func_row(0);
const compiler::StbView::FuncRow r = v.func_row(0);
CHECK(r.nregs == 14); // r8 起:a b c eq + 2 临时
char buf[64];
@@ -485,15 +485,15 @@ static bool test_case08() {
std::string err;
CHECK(compile_case("08_time_literal", &img, &err));
const isa::ImageView v = isa::ImageView::from(img);
const compiler::StbView v = compiler::StbView::from(img);
CHECK(v.ok());
CHECK(v.header().n_consts == 2);
const isa::ConstEntry c0 = v.const_entry(0);
CHECK(c0.tag == isa::types::Time && c0.value == 10); // T#10ms
const isa::ConstEntry c1 = v.const_entry(1);
CHECK(c1.tag == isa::types::Time && c1.value == 1250); // T#1s250ms
CHECK(v.n_consts() == 2);
const compiler::ConstEntry c0 = v.const_entry(0);
CHECK(c0.tag == 2 && c0.value == 10); // T#10ms
const compiler::ConstEntry c1 = v.const_entry(1);
CHECK(c1.tag == 2 && c1.value == 1250); // T#1s250ms
const isa::FuncRow r = v.func_row(0);
const compiler::StbView::FuncRow r = v.func_row(0);
CHECK(r.nregs == 10); // r8 起
char buf[64];
const uint32_t* ins = reinterpret_cast<const uint32_t*>(v.code_bytes());
@@ -511,15 +511,15 @@ static bool test_case13() {
std::string err;
CHECK(compile_case("13_function_call", &img, &err));
const isa::ImageView v = isa::ImageView::from(img);
const compiler::StbView v = compiler::StbView::from(img);
CHECK(v.ok());
CHECK(v.header().n_funcs == 2);
CHECK(v.header().entry_fn_id == 1); // MAIN 是第二个 POU
CHECK(v.n_funcs() == 2);
CHECK(v.entry_fn_id() == 1); // MAIN 是第二个 POU
char buf[64];
const uint8_t* base = v.code_bytes();
// Addfn_id 0):结果 r0、输入 r1(a) r2(b)、临时 r8/r9
const isa::FuncRow fa = v.func_row(0);
const compiler::StbView::FuncRow fa = v.func_row(0);
CHECK(fa.nregs == 10);
CHECK(fa.code_len == 4);
const uint32_t* ia = reinterpret_cast<const uint32_t*>(base);
@@ -533,7 +533,7 @@ static bool test_case13() {
CHECK(std::strcmp(buf, "RET") == 0);
// MAINfn_id 1):x 在 r8,实参临时 r9/r10
const isa::FuncRow fm = v.func_row(1);
const compiler::StbView::FuncRow fm = v.func_row(1);
CHECK(fm.nregs == 11);
CHECK(fm.code_len == 7);
const uint32_t* im = reinterpret_cast<const uint32_t*>(base) + fm.code_offset / 4;
@@ -561,13 +561,13 @@ static bool test_case15() {
std::string err;
CHECK(compile_case("15_fb_instance", &img, &err));
const isa::ImageView v = isa::ImageView::from(img);
const compiler::StbView v = compiler::StbView::from(img);
CHECK(v.ok());
CHECK(v.header().n_funcs == 2); // FB 占位 + MAIN
CHECK(v.header().entry_fn_id == 1);
CHECK(v.data_len() == 24 + 32); // 实例 3 槽 × 8 字节定宽 + SHA-256 尾
CHECK(v.n_funcs() == 2); // FB 占位 + MAIN
CHECK(v.entry_fn_id() == 1);
CHECK(v.data_len() == 24); // 实例 3 槽 × 8 字节定宽
const isa::FuncRow fm = v.func_row(1);
const compiler::StbView::FuncRow fm = v.func_row(1);
CHECK(fm.nregs == 12);
CHECK(fm.code_len == 12);
const uint32_t* im = reinterpret_cast<const uint32_t*>(v.code_bytes()) + fm.code_offset / 4;
@@ -601,10 +601,10 @@ static bool test_case17() {
std::string err;
CHECK(compile_case("17_ton", &img, &err));
const isa::ImageView v = isa::ImageView::from(img);
const compiler::StbView v = compiler::StbView::from(img);
CHECK(v.ok());
CHECK(v.data_len() == 32 + 32); // in@0 pt@8 q@16 et@24 + SHA-256 尾
const isa::FuncRow r = v.func_row(0);
CHECK(v.data_len() == 32); // in@0 pt@8 q@16 et@24
const compiler::StbView::FuncRow r = v.func_row(0);
CHECK(r.nregs == 11);
const uint32_t* ins = reinterpret_cast<const uint32_t*>(v.code_bytes());
char buf[64];
@@ -630,10 +630,10 @@ static bool test_case18() {
std::string err;
CHECK(compile_case("18_tof_ctu", &img, &err));
const isa::ImageView v = isa::ImageView::from(img);
const compiler::StbView v = compiler::StbView::from(img);
CHECK(v.ok());
CHECK(v.data_len() == 72 + 32); // tf 4 槽 + c 5 槽 = 9 槽 × 8 + SHA-256 尾
const isa::FuncRow r = v.func_row(0);
CHECK(v.data_len() == 72); // tf 4 槽 + c 5 槽 = 9 槽 × 8
const compiler::StbView::FuncRow r = v.func_row(0);
CHECK(r.nregs == 14);
const uint32_t* ins = reinterpret_cast<const uint32_t*>(v.code_bytes());
char buf[64];
@@ -670,14 +670,14 @@ static bool test_line1() {
CHECK(load_cfg(&cfg));
CHECK(codegen_project(p, units, link, cfg, &img, &err));
const isa::ImageView v = isa::ImageView::from(img);
const compiler::StbView v = compiler::StbView::from(img);
CHECK(v.ok());
CHECK(v.header().n_globals == 4);
CHECK(v.header().n_funcs == 2);
CHECK(v.header().entry_fn_id == 1);
CHECK(v.data_len() == 56 + 32); // 7 槽 × 8 字节定宽 + SHA-256 尾
CHECK(v.n_globals() == 4);
CHECK(v.n_funcs() == 2);
CHECK(v.entry_fn_id() == 1);
CHECK(v.data_len() == 56); // 7 槽 × 8 字节定宽
const isa::FuncRow fm = v.func_row(1);
const compiler::StbView::FuncRow fm = v.func_row(1);
CHECK(fm.nregs == 13);
CHECK(fm.code_len == 17);
const uint32_t* im = reinterpret_cast<const uint32_t*>(v.code_bytes()) + fm.code_offset / 4;