阶段 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
+78
View File
@@ -0,0 +1,78 @@
/**
* @file Image.h
* @brief vm 自带的 .stb 只读视图(执行器侧;compiler 各有实现)
* @author
* @date 2026-08-21
*
* @details 格式契约见 Doc/isa/指令与映像.md12.13 修订:头 104 = 72 + 型号标识[32]
* 文件尾 SHA-256[32])。步骤 7:解析头与段;步骤 8:补型号匹配与 SHA-256 校验。
*/
#pragma once
#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>
namespace vm {
// 映像头(字段与 Doc/isa/指令与映像.md 一致)
struct ImageHeader {
uint32_t cycle_limit = 0;
uint32_t dt_ms = 0;
uint64_t project_hash = 0;
uint32_t entry_fn_id = 0;
uint32_t n_globals = 0;
uint32_t n_i = 0;
uint32_t n_q = 0;
uint32_t n_m = 0;
uint32_t n_consts = 0;
uint32_t n_funcs = 0;
uint32_t offset_const = 0;
uint32_t offset_funcs = 0;
uint32_t offset_code = 0;
uint32_t offset_fb = 0;
uint32_t offset_data = 0;
};
struct FuncRow {
uint32_t nregs = 0;
uint32_t code_offset = 0; // 相对字节码段起点(字节)
uint32_t code_len = 0; // 指令条数
};
struct ConstEntry {
uint32_t tag = 0;
uint64_t value = 0;
};
// 只读视图:校验过的映像(指向外部缓冲)
class Image {
public:
static Image from(const uint8_t* buf, size_t len);
static Image from(const std::vector<uint8_t>& buf);
bool ok() const { return ok_; }
const std::string& error() const { return err_; }
const ImageHeader& header() const { return hdr_; }
FuncRow func_row(size_t i) const;
ConstEntry const_entry(size_t i) const;
const uint8_t* code_bytes() const; // 字节码段起点
const uint8_t* data_bytes() const; // 数据段起点
size_t data_len() const; // 字节数
public:
// 默认构造为无效态(ok() == false
Image() : buf_(nullptr), len_(0) {}
private:
const uint8_t* buf_;
size_t len_;
bool ok_ = false;
std::string err_;
ImageHeader hdr_;
};
}
+3 -3
View File
@@ -19,7 +19,7 @@
#include <string>
#include <vector>
#include "isa/Image.h"
#include "vm/Image.h"
#include "isa/Instr.h"
namespace vm {
@@ -67,7 +67,7 @@ namespace vm {
bool ended() const;
/** @brief 映像头(dt_ms / cycle_limit 等) */
const isa::ImageHeader& header() const;
const vm::ImageHeader& header() const;
/** @brief 当前指令下标(相对当前函数字节码段) */
uint32_t pc() const;
@@ -105,7 +105,7 @@ namespace vm {
uint32_t ret_fn_id = 0; // 返回目标函数
};
isa::ImageView image_; // 映像只读视图
vm::Image image_; // 映像只读视图vm 自实现)
std::vector<uint8_t> data_; // 数据区工作副本(8 字节定宽槽)
std::vector<Frame> frames_; // 调用栈([0] = MAIN,跨周期保留)
// 边沿检测上次输入(每槽 2 字节:[slot*2] 第一边沿、[slot*2+1] 第二边沿;