阶段 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
+1 -1
View File
@@ -72,7 +72,7 @@ add_test(NAME typecheck_types
add_executable(codegen_test
./src/codegen_test.cpp)
target_link_libraries(codegen_test PRIVATE compiler)
target_link_libraries(codegen_test PRIVATE compiler isa)
target_compile_definitions(codegen_test PRIVATE
REPO_ROOT="${CMAKE_SOURCE_DIR}")
+6 -1
View File
@@ -119,8 +119,13 @@ namespace {
std::printf("FAIL %s type: %s\n", dir, err.c_str());
return false;
}
MachineConfig cfg;
if (!cfg.load(std::string(REPO_ROOT) + "/compiler/machine.toml", &err)) {
std::printf("FAIL %s machine load\n", dir);
return false;
}
std::vector<uint8_t> img;
if (!codegen_project(p, units, link, &img, &err)) {
if (!codegen_project(p, units, link, cfg, &img, &err)) {
if (want == Outcome::CompileError) {
++g_checks;
return true;
+18 -3
View File
@@ -34,8 +34,19 @@ static int g_checks = 0;
} while (0)
// 从用例目录走完整编译管线(解析 → 链接 → 类型检查 → 代码生成)
static bool load_cfg(compiler::MachineConfig* cfg) {
std::string err;
if (!cfg->load(std::string(REPO_ROOT) + "/compiler/machine.toml", &err)) {
std::printf("FAIL load machine.toml: %s\n", err.c_str());
return false;
}
return true;
}
static bool compile_case(const char* dir, std::vector<uint8_t>* image, std::string* err) {
using namespace compiler;
compiler::MachineConfig cfg;
if (!load_cfg(&cfg)) return false;
const std::string toml = std::string(REPO_ROOT) + "/tests/cases/" + dir + "/project.toml";
Project p;
if (!parse_project(toml, &p, err)) {
@@ -56,7 +67,7 @@ static bool compile_case(const char* dir, std::vector<uint8_t>* image, std::stri
if (!check_project(p, units, link, err)) {
return false;
}
return codegen_project(p, units, link, image, err);
return codegen_project(p, units, link, cfg, image, err);
}
// 从临时工程(globals.st + main.st + 可含 io 绑定)走完整管线
@@ -64,6 +75,8 @@ static bool compile_src(const char* name, const char* io_extra, const char* glob
const char* main_st, std::vector<uint8_t>* image,
std::string* err) {
using namespace compiler;
compiler::MachineConfig cfg;
if (!load_cfg(&cfg)) return false;
const std::string dir = std::string(REPO_ROOT) + "/build/cg_tmp_" + name;
std::filesystem::remove_all(dir);
std::filesystem::create_directories(dir);
@@ -100,7 +113,7 @@ static bool compile_src(const char* name, const char* io_extra, const char* glob
if (!check_project(p, units, link, err)) {
return false;
}
return codegen_project(p, units, link, image, err);
return codegen_project(p, units, link, cfg, image, err);
}
// ---- 1. 用例 01:空 MAIN + RET ----
@@ -653,7 +666,9 @@ static bool test_line1() {
LinkResult link;
CHECK(link_project(p, units, &link, &err));
CHECK(check_project(p, units, link, &err));
CHECK(codegen_project(p, units, link, &img, &err));
compiler::MachineConfig cfg;
CHECK(load_cfg(&cfg));
CHECK(codegen_project(p, units, link, cfg, &img, &err));
const isa::ImageView v = isa::ImageView::from(img);
CHECK(v.ok());
+5 -1
View File
@@ -55,6 +55,10 @@ static void wslot(vm::Machine& m, int s, int64_t v) {
static bool make_machine(const char* dir, vm::Machine* m, std::string* err) {
using namespace compiler;
MachineConfig cfg;
if (!cfg.load(std::string(REPO_ROOT) + "/compiler/machine.toml", err)) {
return false;
}
const std::string toml = std::string(REPO_ROOT) + "/" + dir + "/project.toml";
Project p;
if (!parse_project(toml, &p, err)) {
@@ -76,7 +80,7 @@ static bool make_machine(const char* dir, vm::Machine* m, std::string* err) {
return false;
}
std::vector<uint8_t> img;
if (!codegen_project(p, units, link, &img, err)) {
if (!codegen_project(p, units, link, cfg, &img, err)) {
return false;
}
return vm::Machine::create(img, m, err);