feat(mir): 实现x86-64架构寄存器分配和指令选择优化

- 修改x86_64_isel.h接口,将func_meta替换为func指针,并添加新的isel函数原型
- 添加x86_64_reg_alloc.h头文件,提供架构特定的寄存器分配操作填充函数
- 更新frame_layout.h定义frame_layout上下文结构
- 重构reg_alloc.h中的寄存器分配操作结构体,将ops从指针改为值类型,
  并将mark_reg_unused重命名为clean_mark_regs
- 扩展scc_mir.h中的函数元数据,添加vregs_count字段和虚拟寄存器管理函数
- 重新定义MIR pass阶段枚举,添加FRAME_LAYOUT和PROLOGUE_EPILOGUE阶段
- 添加win64目标相关头文件和实现,提供Windows x64 ABI降低和寄存器分配填充
- 更新虚拟寄存器表示格式从$到%,修复alloca指令处理
- 重构寄存器分配算法,改进虚拟寄存器到物理寄存器/栈槽的映射机制
- 完善MIR pass调度,支持多阶段处理流程
This commit is contained in:
zzy
2026-05-12 10:55:58 +08:00
parent 4f40f0d5e4
commit 68eac24152
17 changed files with 386 additions and 115 deletions

View File

@@ -12,11 +12,14 @@
typedef struct scc_x86_64_isel {
scc_mir_instr_vec_t instrs;
scc_lir_func_meta_t *func_meta;
scc_mir_func_t *func;
scc_abi_lowering_t abi_lowering;
} scc_x86_64_isel_t;
void scc_isel_x86_64(scc_mir_module_t *mir_module,
const scc_lir_module_t *lir_module,
scc_x86_64_isel_t *isel);
static void add_instr(scc_x86_64_isel_t *isel, const scc_mir_instr_t *instr) {
scc_vec_push(isel->instrs, *instr);
}

View File

@@ -0,0 +1,8 @@
#ifndef __SCC_X86_64_REG_ALLOC_H__
#define __SCC_X86_64_REG_ALLOC_H__
#include "../core_pass/scc_reg_alloc.h"
void scc_reg_alloc_fill_arch_x86(scc_reg_alloc_op_t *ops);
#endif /* __SCC_X86_64_REG_ALLOC_H__ */

View File

@@ -1,4 +1,12 @@
#ifndef __SCC_FRAME_LAYOUT_H__
#define __SCC_FRAME_LAYOUT_H__
#include "../scc_mir_module.h"
typedef struct scc_frame_layout {
scc_mir_func_t *func;
} scc_frame_layout_t;
void scc_frame_layout(scc_frame_layout_t *ctx, scc_mir_module_t *module);
#endif /* __SCC_FRAME_LAYOUT_H__ */

View File

@@ -10,8 +10,6 @@ typedef enum {
} scc_op_access_t;
typedef struct scc_reg_alloc_op {
// ---- 指令生成(纯动作,无状态) ----
// preg → [slot]
void (*emit_spill)(scc_mir_instr_vec_t *ctx, int preg, int slot);
// [slot] → preg
@@ -26,7 +24,7 @@ typedef struct scc_reg_alloc_op {
// 显式标记某个寄存器已占用 / 未占用(用于隐式寄存器、固定分配等)
void (*mark_reg_used)(void *ctx, int preg);
void (*mark_reg_unused)(void *ctx, int preg);
void (*clean_mark_regs)(void *ctx);
// ---- 指令信息查询(只读) ----
scc_op_access_t (*get_operand_access)(void *ctx, int opcode, int op_idx);
@@ -35,8 +33,8 @@ typedef struct scc_reg_alloc_op {
} scc_reg_alloc_op_t;
typedef struct scc_reg_alloc_ctx {
const scc_reg_alloc_op_t *ops;
scc_mir_func_meta_t *func_meta;
scc_reg_alloc_op_t ops;
scc_mir_func_t *func;
scc_mir_instr_vec_t *instrs;
} scc_reg_alloc_ctx_t;

View File

@@ -59,14 +59,24 @@ typedef struct scc_mir_func_meta {
// 栈帧信息 (由 FrameLayout Pass 填充)
int frame_size;
int stack_alignment;
int vregs_count;
// 寄存器分配信息
scc_mir_stack_slot_vec_t stack_slots;
scc_hashtable_t vreg2preg; // vreg -> phys reg-1 表示溢出
scc_hashtable_t vreg2slot; // vreg -> stack slot index
// vreg -> phys reg and stack slot index
// positive means stack slot index
// negative means physic register
scc_hashtable_t vreg2physic;
} scc_mir_func_meta_t;
#define SCC_MIR_FUNC_META(func) ((scc_mir_func_meta_t *)(func)->meta)
void scc_mir_func_meta_init(scc_mir_func_meta_t *func_meta);
int scc_mir_alloc_vreg(scc_mir_func_t *func);
void scc_mir_vreg_op(const scc_mir_func_t *func, int vreg,
scc_mir_operand_t *out);
void scc_mir_vreg_map2preg(scc_mir_func_t *func, int vreg, int preg);
int scc_mir_vreg_map2slot(scc_mir_func_t *func, int vreg, int size, int align);
#endif /* __SCC_MIR_H__ */

View File

@@ -19,10 +19,10 @@ typedef enum {
/** Pass 运行阶段 (用于自动排序) */
typedef enum {
SCC_MIR_STAGE_ANY, // 无特殊阶段要求
SCC_MIR_STAGE_PRE_REGALLOC, // 寄存器分配
SCC_MIR_STAGE_REGALLOC, // 寄存器分配本身
SCC_MIR_STAGE_POST_REGALLOC // 寄存器分配后
SCC_MIR_STAGE_ANY, // 无特殊阶段要求
SCC_MIR_STAGE_REGALLOC, // 寄存器分配本身
SCC_MIR_STAGE_FRAME_LAYOUT, // 帧布局
SCC_MIR_STAGE_PROLOGUE_EPILOGUE, // 函数头尾处理
} scc_mir_pass_stage_t;
void scc_mir_pass(scc_mir_module_t *mir_module, scc_mir_pass_stage_t stage);

View File

@@ -0,0 +1,9 @@
#ifndef __SCC_WIN64_H__
#define __SCC_WIN64_H__
#include "../arch/x86_64_isel.h"
#include "../core_pass/scc_reg_alloc.h"
void scc_win_pc_x64_abi_lowering(scc_abi_lowering_t *abi_lowering);
void scc_reg_alloc_fill_win_x64(scc_reg_alloc_op_t *ops);
#endif