阶段 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;
-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;
}
+12 -13
View File
@@ -15,7 +15,6 @@
#include "compiler/Project.h"
#include "compiler/Typecheck.h"
#include "isa/Encode.h"
#include "isa/Image.h"
#include "isa/Instr.h"
#include "isa/Op.h"
#include "vm/Machine.h"
@@ -93,7 +92,7 @@ struct HFunc {
uint32_t nregs = 8;
};
static std::vector<uint8_t> hand_image(const std::vector<isa::ConstEntry>& consts,
static std::vector<uint8_t> hand_image(const std::vector<vm::ConstEntry>& consts,
const std::vector<HFunc>& funcs,
uint32_t entry = 0,
uint32_t cycle_limit = 100000,
@@ -102,11 +101,11 @@ static std::vector<uint8_t> hand_image(const std::vector<isa::ConstEntry>& const
for (const HFunc& f : funcs) {
code_total += f.code.size() * 4;
}
const uint32_t off_const = 72;
const uint32_t off_const = 104;
const uint32_t off_funcs = off_const + static_cast<uint32_t>(consts.size()) * 12;
const uint32_t off_code = off_funcs + static_cast<uint32_t>(funcs.size()) * 12;
const uint32_t off_data = off_code + static_cast<uint32_t>(code_total);
const uint32_t off_end = off_data + static_cast<uint32_t>(data.size());
const uint32_t off_end = off_data + static_cast<uint32_t>(data.size()) + 32; // SHA 尾占位
std::vector<uint8_t> b(off_end, 0);
auto put32 = [&](size_t o, uint32_t v) {
@@ -120,8 +119,8 @@ static std::vector<uint8_t> hand_image(const std::vector<isa::ConstEntry>& const
b[o + i] = static_cast<uint8_t>((v >> (8 * i)) & 0xFFu);
}
};
put32(0, isa::kMagic);
put32(4, isa::kVersion);
put32(0, 0x43545353u);
put32(4, 1u);
put32(8, cycle_limit);
put32(12, 10); // dt_ms
put32(24, entry);
@@ -163,10 +162,10 @@ static std::vector<uint8_t> hand_image(const std::vector<isa::ConstEntry>& const
static bool test_hand_scalar() {
// r8 := 5r9 := r8 + 3;槽 0 ← r9;槽 1 → r10;JT 跳过一条
const std::vector<isa::ConstEntry> consts = {
{isa::types::Int, 5},
{isa::types::Int, 3},
{isa::types::Int, 1},
const std::vector<vm::ConstEntry> consts = {
{1, 5},
{1, 3},
{1, 1},
};
const std::vector<HFunc> funcs = {{
{
@@ -201,9 +200,9 @@ static bool test_hand_scalar() {
static bool test_hand_call() {
// fn0(入口):r8=5、r2=7、实参 r1←r8CALL 1;结果 r0 → r8
// fn1r0 = r1 + r2r1/r2 来自调用约定区复制)
const std::vector<isa::ConstEntry> consts = {
{isa::types::Int, 5},
{isa::types::Int, 7},
const std::vector<vm::ConstEntry> consts = {
{1, 5},
{1, 7},
};
const std::vector<HFunc> funcs = {
{{