feat(compiler): 启用 ir2mcode 和 sccf2target 库并实现 x86_64 代码生成

- 在 cbuild.toml 中启用 ir2mcode 和 sccf2target 依赖库
- 修改 justfile 中的构建命令,使用 release 模式并更新 tokei 统计排除 mcode 目录
- 重构 LIR 中的地址操作数类型,将 SCC_LIR_INSTR_KIND_ADDR 重命名为 SCC_LIR_INSTR_KIND_MEM
- 实现完整的 MIR 到 x86_64 机器码转换,包括:
  - 添加 move、compare、binary operation 等指令发射函数
  - 实现条件分支和跳转指令生成
  - 支持算术、逻辑、移位等基本操作
  - 添加调用和返回指令处理
  - 实现栈分配和寄存器分配功能
- 完善 ir2mcode 模块,将 MIR 指令转换为机器码
- 更新 ir2sccf 模块,集成机器码生成功能
- 添加 mcode 库的架构支持和内存管理功能
- 修复 PE 文件生成中的空指针检查问题
This commit is contained in:
zzy
2026-05-05 15:59:31 +08:00
parent 676f3ec82c
commit aa8a1ff8ce
14 changed files with 842 additions and 399 deletions

View File

@@ -45,6 +45,11 @@ void scc_ir2sccf(sccf_builder_t *builder, scc_mir_module_t *mir_module) {
scc_cfg_symbol_t *symbol =
&scc_vec_at(mir_module->cfg_module.symbols, i);
if (symbol->name == nullptr) {
LOG_ERROR("Symbol name is null");
continue;
}
sccf_sym_t sym = (sccf_sym_t){
.sccf_sect_offset = scc_vec_size(sect_data),
.sccf_sym_size =
@@ -58,19 +63,33 @@ void scc_ir2sccf(sccf_builder_t *builder, scc_mir_module_t *mir_module) {
Assert(sym_idx != 0);
}
// scc_vec_foreach(ctx->cprog->func_defs, i) {
// scc_ir_value_ref_t func_ref = scc_vec_at(ctx->cprog->func_defs, i);
// scc_ir_func_t *func = scc_ir_module_get_func(GET_MODULE(ctx),
// func_ref); if (!func) {
// LOG_ERROR("invalid function reference");
// return;
// }
// sccf_sym_t *sym =
// sccf_builder_get_symbol_unsafe(ctx->builder, func->name);
// Assert(sym != nullptr);
// sym->sccf_sect_offset = scc_vec_size(ctx->sect_mcode.mcode);
// parse_function(ctx, func);
// }
sccf_sect_data_t sect_code = {0};
scc_vec_init(sect_code);
scc_mcode_t mcode = {0};
scc_mcode_init(&mcode, SCC_MCODE_ARCH_X86_64);
scc_vec_foreach(mir_module->cfg_module.funcs, i) {
if (i == 0)
continue;
scc_mir_func_t *func = &scc_vec_at(mir_module->cfg_module.funcs, i);
sccf_sym_t *sym = sccf_builder_get_symbol_unsafe(builder, func->name);
Assert(sym != nullptr);
sym->sccf_sect_offset = scc_vec_size(mcode.mcode);
scc_vec_foreach(func->bblocks, i) {
scc_cfg_bblock_id_t id = scc_vec_at(func->bblocks, i);
const scc_cfg_bblock_t *bb =
scc_cfg_module_unsafe_get_bblock(&mir_module->cfg_module, id);
scc_mir_instr_vec_t *instrs = SCC_MIR_BBLOCK_VALUES(bb);
scc_vec_foreach(*instrs, i) {
const scc_mir_instr_t *ins = &scc_vec_at(*instrs, i);
// FIXME reloc symbol needed
scc_ir2mcode_emit_instr(&mcode, ins);
}
}
}
scc_vec_unsafe_from_buffer(sect_code, scc_vec_unsafe_get_data(mcode.mcode),
scc_vec_size(mcode.mcode));
// u8 *buf = scc_vec_unsafe_get_data(ctx->sect_mcode.mcode);
// scc_vec_foreach(ctx->builder->relocs, i) {
@@ -95,11 +114,6 @@ void scc_ir2sccf(sccf_builder_t *builder, scc_mir_module_t *mir_module) {
// }
// }
// sccf_sect_data_t text_section;
// scc_vec_unsafe_from_buffer(text_section,
// scc_vec_unsafe_get_data(ctx->sect_mcode.mcode),
// scc_vec_size(ctx->sect_mcode.mcode));
// sccf_builder_add_text_section(ctx->builder, &text_section);
sccf_builder_add_text_section(builder, &sect_code);
sccf_builder_add_data_section(builder, &sect_data);
}