feat(ir): 添加MIR中间表示层并重构LIR相关代码

- 新增mir库模块,包含scc_mir、scc_mir_module、scc_mir_dump等组件
- 添加LIR到MIR的转换接口scc_lir2mir
- 定义MIR操作数类型和指令结构,支持虚拟寄存器、物理寄存器、立即数等
- 实现x86_64指令选择器,将LIR指令转换为MIR指令
- 重构LIR模块中函数参数命名,统一使用lir_module参数名
- 移除LIR中物理寄存器相关的代码和注释
- 更新cbuild.toml依赖配置,添加mir模块依赖
This commit is contained in:
zzy
2026-04-26 17:38:30 +08:00
parent e850b5c981
commit f57ba9bd31
21 changed files with 830 additions and 54 deletions

View File

@@ -14,7 +14,6 @@ typedef struct {
scc_lir_func_t *current_func;
scc_lir_instr_vec_t instrs;
scc_hashtable_t bb_map; // ir_bblock_ref_t -> scc_lir_bblock_id_t
scc_hashtable_t value_to_vreg; // ir_value_ref_t -> unsigned int vreg
scc_hashtable_t func_decl_map; // ir_func_ref_t -> const char* (用于调用)
} ir2lir_ctx_t;
@@ -23,13 +22,11 @@ static void ir2lir_ctx_init(ir2lir_ctx_t *ctx, scc_lir_module_t *lir_module,
scc_hir_module_t *hir_module) {
ctx->hir_module = hir_module;
ctx->lir_module = lir_module;
scc_hashtable_usize_init(&ctx->bb_map);
scc_hashtable_usize_init(&ctx->value_to_vreg);
scc_hashtable_usize_init(&ctx->func_decl_map);
}
static void ir2lir_ctx_drop(ir2lir_ctx_t *ctx) {
scc_hashtable_drop(&ctx->bb_map);
scc_hashtable_drop(&ctx->value_to_vreg);
scc_hashtable_drop(&ctx->func_decl_map);
}
@@ -107,7 +104,8 @@ static int get_vreg_for_value(ir2lir_ctx_t *ctx, scc_hir_value_ref_t val_ref) {
if (found)
return (int)(usize)found;
int vreg = SCC_LIR_FUNC_META(ctx->current_func)->vregs_count++;
int vreg = ++(SCC_LIR_FUNC_META(ctx->current_func)->vregs_count);
Assert(vreg != 0);
scc_hashtable_set(&ctx->value_to_vreg, (void *)(usize)val_ref,
(void *)(usize)vreg);
return vreg;
@@ -386,25 +384,18 @@ static void translate_hir_value(ir2lir_ctx_t *ctx, scc_hir_value_t *value,
case SCC_HIR_VALUE_TAG_BRANCH: {
scc_lir_val_t cond =
ir_value_to_lir_operand(ctx, value->data.branch.cond);
scc_lir_bblock_id_t true_bb =
(scc_lir_bblock_id_t)(uintptr_t)scc_hashtable_get(
&ctx->bb_map,
(void *)(uintptr_t)value->data.branch.true_bblock);
scc_lir_bblock_id_t false_bb =
(scc_lir_bblock_id_t)(uintptr_t)scc_hashtable_get(
&ctx->bb_map,
(void *)(uintptr_t)value->data.branch.false_bblock);
scc_lir_instr_t instr = {
.op = SCC_LIR_BR, .arg0 = cond, .metadata.br = {true_bb, false_bb}};
.op = SCC_LIR_BR,
.arg0 = cond,
.metadata.br = {value->data.branch.true_bblock,
value->data.branch.false_bblock}};
scc_lir_builder_add_instr(ctx, &instr);
break;
}
case SCC_HIR_VALUE_TAG_JUMP: {
scc_lir_bblock_id_t target =
(scc_lir_bblock_id_t)(usize)scc_hashtable_get(
&ctx->bb_map, (void *)(usize)value->data.jump.target_bblock);
scc_lir_instr_t instr = {.op = SCC_LIR_JMP,
.metadata.jmp_target = target};
.metadata.jmp_target =
value->data.jump.target_bblock};
scc_lir_builder_add_instr(ctx, &instr);
break;
}
@@ -487,7 +478,6 @@ static void translate_func(ir2lir_ctx_t *ctx, scc_hir_func_t *ir_func) {
}
// 清理本次函数翻译的临时映射
scc_hashtable_drop(&ctx->bb_map);
scc_hashtable_drop(&ctx->value_to_vreg);
}