stb 头字段偏移常量表:消除 vm/compiler 两侧裸魔法数

- vm/Image.cpp:匿名空间 constexpr 偏移常量(kOffMagic..kOffModelId + 段内行偏移),
  from/model_id/model_matches/func_row/const_entry 全部替换
- compiler/Stb.h:同值 inline constexpr 表(Codegen 写侧 + Stb 读侧共用)
- compiler/Codegen.cpp assemble_image:put_* 头字段/行内偏移替换
- compiler/Stb.cpp StbView:读侧偏移替换
- 两侧 static_assert:头大小 = 72 + 型号标识、字段偏移单调、行内布局自洽
This commit is contained in:
2026-08-24 08:55:57 +08:00
parent d51dce4ae3
commit 7aa2bbefcf
4 changed files with 138 additions and 66 deletions
+23 -23
View File
@@ -847,16 +847,16 @@ namespace {
std::vector<uint8_t>& b = *image_;
b.assign(off_end, 0);
put_le32(b, 0, kMagic);
put_le32(b, 4, kVersion);
put_le32(b, 8, proj_.cycle_limit);
put_le32(b, 12, proj_.dt_ms);
put_le32(b, kOffMagic, kMagic);
put_le32(b, kOffVersion, kVersion);
put_le32(b, kOffCycleLimit, proj_.cycle_limit);
put_le32(b, kOffDtMs, proj_.dt_ms);
uint64_t hash = kFnvBasis;
if (!compute_project_hash(proj_, &hash, err_)) {
return false;
}
put_le64(b, 16, hash);
put_le64(b, kOffProjectHash, hash);
uint32_t entry = 0;
for (size_t i = 0; i < funcs_.size(); ++i) {
@@ -864,38 +864,38 @@ namespace {
entry = static_cast<uint32_t>(i);
}
}
put_le32(b, 24, entry);
put_le32(b, 28, static_cast<uint32_t>(link_.globals.size()));
put_le32(b, 32, 0);
put_le32(b, 36, 0);
put_le32(b, 40, 0);
put_le32(b, 44, static_cast<uint32_t>(consts_.size()));
put_le32(b, 48, static_cast<uint32_t>(funcs_.size()));
put_le32(b, 52, off_const);
put_le32(b, 56, off_funcs);
put_le32(b, 60, off_code);
put_le32(b, 64, off_data);
put_le32(b, 68, off_data);
put_le32(b, kOffEntryFnId, entry);
put_le32(b, kOffNGlobals, static_cast<uint32_t>(link_.globals.size()));
put_le32(b, kOffNI, 0);
put_le32(b, kOffNQ, 0);
put_le32(b, kOffNM, 0);
put_le32(b, kOffNConsts, static_cast<uint32_t>(consts_.size()));
put_le32(b, kOffNFuncs, static_cast<uint32_t>(funcs_.size()));
put_le32(b, kOffConst, off_const);
put_le32(b, kOffFuncs, off_funcs);
put_le32(b, kOffCode, off_code);
put_le32(b, kOffFb, off_data);
put_le32(b, kOffData, off_data);
// 型号标识[32](偏移 7212.13 修订)
char mid[kModelIdSize];
fill_model_id(cfg_.model_name(), cfg_.version(), mid);
for (size_t i = 0; i < kModelIdSize; ++i) {
b[72 + i] = static_cast<uint8_t>(mid[i]);
b[kOffModelId + i] = static_cast<uint8_t>(mid[i]);
}
for (size_t i = 0; i < consts_.size(); ++i) {
const size_t o = off_const + i * kConstEntrySize;
put_le32(b, o, consts_[i].tag);
put_le64(b, o + 4, consts_[i].value);
put_le32(b, o + kConstTagOff, consts_[i].tag);
put_le64(b, o + kConstValueOff, consts_[i].value);
}
uint32_t c = off_code;
for (size_t i = 0; i < funcs_.size(); ++i) {
const FuncCtx& f = funcs_[i];
const size_t o = off_funcs + i * kFuncRowSize;
put_le32(b, o, f.nregs);
put_le32(b, o + 8, static_cast<uint32_t>(f.code.size()));
put_le32(b, o + kFuncNregsOff, f.nregs);
put_le32(b, o + kFuncLenOff, static_cast<uint32_t>(f.code.size()));
for (const Instr in : f.code) {
put_le32(b, c, in);
c += 4;
@@ -904,7 +904,7 @@ namespace {
uint32_t acc = 0;
for (size_t i = 0; i < funcs_.size(); ++i) {
const size_t o = off_funcs + i * kFuncRowSize;
put_le32(b, o + 4, acc);
put_le32(b, o + kFuncCodeOff, acc);
acc += static_cast<uint32_t>(funcs_[i].code.size()) * 4;
}