feat(mir): 添加函数调用和返回指令支持

添加了间接调用函数emit_indirect_call用于处理寄存器调用,
修改直接调用函数emit_direct_call使用相对分支形式,
为函数元数据添加need_va_args字段标记变参函数需求。

refactor(prolog-epilog): 重构函数序言和尾声处理逻辑

将prolog/epilog回调函数参数从void*改为scc_mir_instr_vec_t*,
添加need_epilog回调函数用于判断是否需要插入尾声代码,
修改pass处理逻辑以支持动态插入序言和尾声代码。

feat(win64-abi): 实现Windows x64平台序言尾声生成功能

实现prologue函数生成栈帧设置和变参处理代码,
实现epilogue函数生成栈清理和返回指令,
添加need_epilog判断函数识别返回指令类型并触发尾声插入。
This commit is contained in:
zzy
2026-05-20 21:11:48 +08:00
parent c6e3bb2e20
commit aa4292a30e
7 changed files with 64 additions and 31 deletions

View File

@@ -50,8 +50,10 @@ void scc_win_pc_x64_frame_alloc_init(scc_frame_layout_t *ctx) {
windows x86 prolog epilog
https://learn.microsoft.com/zh-cn/cpp/build/prolog-and-epilog?view=msvc-180
*/
static void prologue(void *userdata, const scc_mir_func_t *func) {
scc_x86_64_isel_t *isel = userdata;
static void prologue(scc_mir_instr_vec_t *userdata,
const scc_mir_func_t *func) {
scc_mir_x86_instr_vec_t *instrs = (void *)userdata;
scc_mir_x86_instr_t instr;
/*
mov [RSP + 8], RCX
push R15
@@ -62,27 +64,26 @@ static void prologue(void *userdata, const scc_mir_func_t *func) {
...
*/
scc_mir_func_meta_t *meta = SCC_MIR_FUNC_META(func);
int frame_size = meta->frame_size; // 之前计算好的
int frame_size = meta->frame_size;
// 1. push rbp
add_instr_1(isel, SCC_X86_IFORM_PUSH_GPRV_50,
scc_x86_op_preg(SCC_X86_REG_RBP));
// va_arg
if (meta->need_va_args) {
/// FILL to shadow space
}
// 2. mov rbp, rsp
add_instr_2(isel, SCC_X86_IFORM_MOV_GPRV_GPRV_8B,
scc_x86_op_preg(SCC_X86_REG_RBP),
scc_x86_op_preg(SCC_X86_REG_RSP));
// 3. sub rsp, frame_size
// sub rsp, frame_size
if (frame_size > 0) {
scc_x86_operand_value_t imm = scc_x86_op_imm(frame_size);
add_instr_2(isel, SCC_X86_IFORM_SUB_GPRV_IMMZ,
scc_x86_op_preg(SCC_X86_REG_RSP), imm);
scc_mir_x86_instr_2(&instr, SCC_X86_IFORM_SUB_GPRV_IMMZ,
scc_x86_op_preg(SCC_X86_REG_RSP),
scc_x86_op_imm(frame_size), scc_pos_create());
scc_vec_push(*instrs, instr);
}
}
static void epilogue(void *userdata, const scc_mir_func_t *func) {
scc_x86_64_isel_t *isel = userdata;
static void epilogue(scc_mir_instr_vec_t *userdata,
const scc_mir_func_t *func) {
scc_mir_x86_instr_vec_t *instrs = (void *)userdata;
scc_mir_x86_instr_t instr;
/*
add RSP, fixed-allocation-size
pop R13
@@ -90,19 +91,36 @@ static void epilogue(void *userdata, const scc_mir_func_t *func) {
pop R15
ret
*/
// 1. mov rsp, rbp
add_instr_2(isel, SCC_X86_IFORM_MOV_GPRV_GPRV_8B,
scc_x86_op_preg(SCC_X86_REG_RSP),
scc_x86_op_preg(SCC_X86_REG_RBP));
scc_mir_func_meta_t *meta = SCC_MIR_FUNC_META(func);
int frame_size = meta->frame_size;
// 2. pop rbp
add_instr_1(isel, SCC_X86_IFORM_POP_GPRV_8F,
scc_x86_op_preg(SCC_X86_REG_RBP));
// add rsp, frame_size
if (frame_size > 0) {
scc_mir_x86_instr_2(&instr, SCC_X86_IFORM_ADD_GPRV_IMMZ,
scc_x86_op_preg(SCC_X86_REG_RSP),
scc_x86_op_imm(frame_size), scc_pos_create());
scc_vec_push(*instrs, instr);
}
}
static int need_epilog(const scc_mir_instr_t *instr) {
scc_mir_x86_instr_t *ins = (scc_mir_x86_instr_t *)instr;
switch (ins->x86_instr.opcode) {
case SCC_X86_IFORM_RET_FAR:
case SCC_X86_IFORM_RET_NEAR:
case SCC_X86_IFORM_RET_FAR_IMMW:
case SCC_X86_IFORM_RET_NEAR_IMMW:
return true;
default:
break;
}
return false;
}
void scc_win_pc_x64_prolog_epilog_init(scc_prolog_epilog_t *ctx) {
ctx->prolog = prologue;
ctx->epilog = epilogue;
ctx->need_epilog = need_epilog;
}
static void lower_call(void *userdata, const scc_lir_instr_t *instr) {