阶段 B 步骤 4:compiler 配置驱动化,去除 isa 依赖。

- Project.h 自带 IoBinding;Stb.h/cpp:compiler 侧 .stb 规范(常量/ConstEntry/FNV/StbView/读写/sidecar)
- Codec.h/cpp:指令字 pack/字段/配置驱动 disasm(format 表驱动)
- Codegen:opcode 全查 MachineConfig(E_* 发射器)、常量 tag 查配置类型、初值按基元宽度写、
  映像拼装用 Stb 常量;codegen_project 增加 cfg 参数
- main.cpp:--machine <path>(编译/反汇编必填),--disasm 用 StbView+Codec
- CMake:compiler 不再链接 isa(仅 toml++ 私有);测试补链 isa(自身断言用)
- 验证:20 用例字节不变(hash 0xc3fe4ae74ad45900 相同)、ctest 12/12、缺 --machine 报错退出
This commit is contained in:
2026-08-21 22:14:29 +08:00
parent 44b5ecd46d
commit 447a8d0119
15 changed files with 739 additions and 390 deletions
+65
View File
@@ -0,0 +1,65 @@
/**
* @file Codec.cpp
* @brief 指令字编解码 + 配置驱动反汇编(编译器侧)
* @author
* @date 2026-08-21
*/
#include "compiler/Codec.h"
#include <cstdio>
namespace compiler {
Instr pack(uint8_t opcode, uint8_t rd, uint8_t a, uint8_t b) {
return static_cast<uint32_t>(opcode)
| (static_cast<uint32_t>(rd) << 8)
| (static_cast<uint32_t>(a) << 16)
| (static_cast<uint32_t>(b) << 24);
}
uint8_t op_of(Instr w) { return static_cast<uint8_t>(w & 0xFFu); }
uint8_t rd_of(Instr w) { return static_cast<uint8_t>((w >> 8) & 0xFFu); }
uint8_t a_of(Instr w) { return static_cast<uint8_t>((w >> 16) & 0xFFu); }
uint8_t b_of(Instr w) { return static_cast<uint8_t>((w >> 24) & 0xFFu); }
uint16_t imm16_of(Instr w) {
return static_cast<uint16_t>(a_of(w) | (static_cast<uint16_t>(b_of(w)) << 8));
}
int16_t off16_of(Instr w) { return static_cast<int16_t>(imm16_of(w)); }
void disasm(const MachineConfig& cfg, Instr w, char* out, size_t cap) {
if (cap == 0) {
return;
}
out[0] = '\0';
const ConfigOp* op = cfg.find_op_by_code(op_of(w));
if (op == nullptr) {
snprintf(out, cap, "??? 0x%08x", static_cast<unsigned>(w));
return;
}
const uint8_t rd = rd_of(w);
const std::string& f = op->format;
if (f == "RR") {
snprintf(out, cap, "%s r%u, r%u", op->name.c_str(), static_cast<unsigned>(rd),
static_cast<unsigned>(a_of(w)));
} else if (f == "RRR") {
snprintf(out, cap, "%s r%u, r%u, r%u", op->name.c_str(), static_cast<unsigned>(rd),
static_cast<unsigned>(a_of(w)), static_cast<unsigned>(b_of(w)));
} else if (f == "IMM" || f == "SLOT") {
snprintf(out, cap, "%s r%u, %u", op->name.c_str(), static_cast<unsigned>(rd),
static_cast<unsigned>(imm16_of(w)));
} else if (f == "JMP") {
snprintf(out, cap, "%s %+d", op->name.c_str(), static_cast<int>(off16_of(w)));
} else if (f == "JC") {
snprintf(out, cap, "%s r%u, %+d", op->name.c_str(), static_cast<unsigned>(rd),
static_cast<int>(off16_of(w)));
} else if (f == "CALL" || f == "CAL") {
snprintf(out, cap, "%s %u", op->name.c_str(), static_cast<unsigned>(imm16_of(w)));
} else { // NONE
snprintf(out, cap, "%s", op->name.c_str());
}
}
} // namespace compiler