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

@@ -27,10 +27,18 @@ static inline void emit_direct_call(scc_x86_64_isel_t *isel,
const char *callee) { const char *callee) {
(void)callee; (void)callee;
scc_mir_x86_instr_t instr = {0}; scc_mir_x86_instr_t instr = {0};
scc_mir_x86_instr_1(&instr, SCC_X86_IFORM_CALL_NEAR_GPRV, scc_mir_x86_instr_1(&instr, SCC_X86_IFORM_CALL_NEAR_RELBRZ,
scc_x86_op_relbr(0), scc_pos_create()); scc_x86_op_relbr(0), scc_pos_create());
scc_vec_push(isel->instrs, instr); scc_vec_push(isel->instrs, instr);
} }
static inline void emit_indirect_call(scc_x86_64_isel_t *isel,
scc_x86_operand_value_t reg) {
scc_mir_x86_instr_t instr = {0};
scc_mir_x86_instr_1(&instr, SCC_X86_IFORM_CALL_NEAR_GPRV, reg,
scc_pos_create());
}
static inline void emit_ret(scc_x86_64_isel_t *isel) { static inline void emit_ret(scc_x86_64_isel_t *isel) {
scc_mir_x86_instr_t instr = {0}; scc_mir_x86_instr_t instr = {0};
scc_mir_x86_instr_0(&instr, SCC_X86_IFORM_RET_NEAR, scc_pos_create()); scc_mir_x86_instr_0(&instr, SCC_X86_IFORM_RET_NEAR, scc_pos_create());

View File

@@ -4,12 +4,14 @@
#include "../scc_mir_module.h" #include "../scc_mir_module.h"
#include <scc_lir_module.h> #include <scc_lir_module.h>
typedef void (*scc_prolog_epilog_fn)(void *userdata, typedef void (*scc_prolog_epilog_fn)(scc_mir_instr_vec_t *userdata,
const scc_mir_func_t *func); const scc_mir_func_t *func);
typedef int (*scc_need_epilog_fn)(const scc_mir_instr_t *instr);
typedef struct scc_prolog_epilog { typedef struct scc_prolog_epilog {
scc_prolog_epilog_fn prolog; scc_prolog_epilog_fn prolog;
scc_prolog_epilog_fn epilog; scc_prolog_epilog_fn epilog;
scc_need_epilog_fn need_epilog;
} scc_prolog_epilog_t; } scc_prolog_epilog_t;
#endif /* __SCC_PROLOG_EPILOG_H__ */ #endif /* __SCC_PROLOG_EPILOG_H__ */

View File

@@ -37,6 +37,7 @@ typedef scc_cfg_bblock_t scc_mir_bblock_t;
// 函数元数据 —— 不包含任何指令结构,由各后端自行定义指令布局 // 函数元数据 —— 不包含任何指令结构,由各后端自行定义指令布局
typedef scc_cfg_func_t scc_mir_func_t; typedef scc_cfg_func_t scc_mir_func_t;
typedef struct scc_mir_func_meta { typedef struct scc_mir_func_meta {
int need_va_args;
int frame_size; int frame_size;
int stack_alignment; int stack_alignment;
int vregs_count; int vregs_count;

View File

@@ -6,6 +6,9 @@
typedef SCC_VEC(char) scc_mir_instr_vec_t; typedef SCC_VEC(char) scc_mir_instr_vec_t;
typedef struct {
} scc_mir_symbol_t;
typedef scc_lir_symbol_meta_vec_t scc_mir_symbol_meta_vec_t; typedef scc_lir_symbol_meta_vec_t scc_mir_symbol_meta_vec_t;
typedef SCC_VEC(scc_mir_func_meta_t *) scc_mir_func_meta_vec_t; typedef SCC_VEC(scc_mir_func_meta_t *) scc_mir_func_meta_vec_t;

View File

@@ -1,6 +1,7 @@
#include <scc_mir.h> #include <scc_mir.h>
void scc_mir_func_meta_init(scc_mir_func_meta_t *func_meta) { void scc_mir_func_meta_init(scc_mir_func_meta_t *func_meta) {
func_meta->need_va_args = false;
func_meta->frame_size = 0; func_meta->frame_size = 0;
func_meta->stack_alignment = 0; func_meta->stack_alignment = 0;
func_meta->vregs_count = 0; func_meta->vregs_count = 0;

View File

@@ -34,18 +34,18 @@ void scc_prolog_epilog(scc_prolog_epilog_t *ctx, scc_mir_module_t *module) {
scc_vec_init(new_instrs); scc_vec_init(new_instrs);
if (i == 0) { if (i == 0) {
ctx->prolog(&new_instrs, func);
} else if (i == scc_vec_size(func->bblocks) - 1) {
// FIXME epilog it maybe used in ret instr
} }
scc_vec_foreach(*old_instrs, i) { scc_vec_foreach(*old_instrs, i) {
scc_mir_instr_t *ins = scc_mir_instr_t *ins =
scc_vec_sized_at_ptr(*old_instrs, module->instr_size, i); scc_vec_sized_at_ptr(*old_instrs, module->instr_size, i);
if (ins->opcode == SCC_MIR_PSEUDO_ALLOCA) { if (ins->opcode < 0) {
continue; continue;
} }
// FIXME error if (ctx->need_epilog(ins)) {
ctx->epilog(&new_instrs, func);
}
scc_vec_sized_push(new_instrs, module->instr_size, ins, scc_vec_sized_push(new_instrs, module->instr_size, ins,
module->instr_size); module->instr_size);
} }

View File

@@ -50,8 +50,10 @@ void scc_win_pc_x64_frame_alloc_init(scc_frame_layout_t *ctx) {
windows x86 prolog epilog windows x86 prolog epilog
https://learn.microsoft.com/zh-cn/cpp/build/prolog-and-epilog?view=msvc-180 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) { static void prologue(scc_mir_instr_vec_t *userdata,
scc_x86_64_isel_t *isel = 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 mov [RSP + 8], RCX
push R15 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); 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 // va_arg
add_instr_1(isel, SCC_X86_IFORM_PUSH_GPRV_50, if (meta->need_va_args) {
scc_x86_op_preg(SCC_X86_REG_RBP)); /// FILL to shadow space
}
// 2. mov rbp, rsp // sub rsp, frame_size
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
if (frame_size > 0) { if (frame_size > 0) {
scc_x86_operand_value_t imm = scc_x86_op_imm(frame_size); scc_mir_x86_instr_2(&instr, SCC_X86_IFORM_SUB_GPRV_IMMZ,
add_instr_2(isel, SCC_X86_IFORM_SUB_GPRV_IMMZ, scc_x86_op_preg(SCC_X86_REG_RSP),
scc_x86_op_preg(SCC_X86_REG_RSP), imm); 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) { static void epilogue(scc_mir_instr_vec_t *userdata,
scc_x86_64_isel_t *isel = 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 add RSP, fixed-allocation-size
pop R13 pop R13
@@ -90,19 +91,36 @@ static void epilogue(void *userdata, const scc_mir_func_t *func) {
pop R15 pop R15
ret ret
*/ */
// 1. mov rsp, rbp scc_mir_func_meta_t *meta = SCC_MIR_FUNC_META(func);
add_instr_2(isel, SCC_X86_IFORM_MOV_GPRV_GPRV_8B, int frame_size = meta->frame_size;
scc_x86_op_preg(SCC_X86_REG_RSP),
scc_x86_op_preg(SCC_X86_REG_RBP));
// 2. pop rbp // add rsp, frame_size
add_instr_1(isel, SCC_X86_IFORM_POP_GPRV_8F, if (frame_size > 0) {
scc_x86_op_preg(SCC_X86_REG_RBP)); 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) { void scc_win_pc_x64_prolog_epilog_init(scc_prolog_epilog_t *ctx) {
ctx->prolog = prologue; ctx->prolog = prologue;
ctx->epilog = epilogue; ctx->epilog = epilogue;
ctx->need_epilog = need_epilog;
} }
static void lower_call(void *userdata, const scc_lir_instr_t *instr) { static void lower_call(void *userdata, const scc_lir_instr_t *instr) {