feat(argparse): 添加枚举类型选项支持
添加了对命令行参数枚举类型的支持,允许用户从预定义的选项中进行选择, 并在解析时进行验证。同时修复了空值检查问题。 refactor(mir): 重构pass管理系统 将原有的pass管理器系统简化为直接的pass执行函数,移除了复杂的 依赖管理和流水线构建机制,使系统更加简洁明了。 chore(ir): 添加Windows x64 ABI实现 实现了Windows x64平台的ABI约定,包括参数传递、寄存器使用和 栈帧布局的处理逻辑。 feat(config): 统一编译阶段控制配置 将多个独立的布尔类型编译阶段控制改为统一的枚举类型,便于 管理和扩展新的编译阶段。
This commit is contained in:
@@ -36,6 +36,9 @@ void scc_x86_instr_dump(scc_tree_dump_t *td, const scc_mir_instr_t *instr) {
|
||||
case SCC_MIR_OP_SYMBOL:
|
||||
scc_tree_dump_append_fmt(td, "@%s", op->symbol);
|
||||
break;
|
||||
case SCC_MIR_OP_MEM:
|
||||
scc_tree_dump_append_fmt(td, "[%d]", op->stack_slot);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -14,8 +14,11 @@ void scc_mir_dump_instr(scc_mir_dump_ctx_t *ctx, const scc_mir_instr_t *ins) {
|
||||
case SCC_MIR_PSUEDO_ALLOCA:
|
||||
scc_tree_dump_node(td, " @alloca");
|
||||
scc_tree_dump_append_fmt(td, "(%d)", ins->operands[1].imm);
|
||||
Assert(ins->operands[0].kind == SCC_MIR_OP_VREG);
|
||||
scc_tree_dump_append_fmt(td, " %%%d", ins->operands[0].vreg);
|
||||
if (ins->operands[0].kind == SCC_MIR_OP_VREG) {
|
||||
scc_tree_dump_append_fmt(td, " %%%d", ins->operands[0].vreg);
|
||||
} else {
|
||||
scc_tree_dump_append(td, "<alloced>");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
@@ -1,2 +1,8 @@
|
||||
#include <core_pass/scc_reg_alloc.h>
|
||||
#include <scc_mir_module.h>
|
||||
void scc_mir_pass(scc_mir_module_t *mir_module) {}
|
||||
|
||||
extern scc_reg_alloc_t x86_reg_alloc_hooks;
|
||||
|
||||
void scc_mir_pass(scc_mir_module_t *mir_module) {
|
||||
scc_reg_alloc(&x86_reg_alloc_hooks, mir_module);
|
||||
}
|
||||
|
||||
205
libs/ir/mir/src/target/win64_abi.c
Normal file
205
libs/ir/mir/src/target/win64_abi.c
Normal file
@@ -0,0 +1,205 @@
|
||||
#include <scc_mir_module.h>
|
||||
#include <x86/scc_x86_iform.h>
|
||||
|
||||
#include <arch/x86_64_isel.h>
|
||||
#include <core_pass/scc_abi_lowering.h>
|
||||
#include <core_pass/scc_prolog_epilog.h>
|
||||
|
||||
static const int WIN64_DEFAULT_ALIGN = 8;
|
||||
static const int WIN64_STACK_ALIGN = 16;
|
||||
|
||||
typedef struct {
|
||||
scc_hashtable_t *vreg2slot;
|
||||
int offset;
|
||||
} frame_layout_ctx_t;
|
||||
|
||||
static void transit_vreg(scc_mir_func_meta_t *func, scc_mir_operand_t *op) {
|
||||
Assert(op->kind == SCC_MIR_OP_VREG);
|
||||
}
|
||||
|
||||
static void frame_alloc_impl(frame_layout_ctx_t *ctx,
|
||||
scc_mir_module_t *mir_module,
|
||||
scc_mir_func_t *mir_func) {
|
||||
/*
|
||||
WIN ABI
|
||||
*/
|
||||
ctx->offset = 8; // called ret address
|
||||
|
||||
scc_mir_func_meta_t *func_meta = SCC_MIR_FUNC_META(mir_func);
|
||||
scc_vec_foreach(mir_func->bblocks, i) {
|
||||
scc_cfg_bblock_id_t id = scc_vec_at(mir_func->bblocks, i);
|
||||
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, j) {
|
||||
scc_mir_instr_t *ins = &scc_vec_at(*instrs, j);
|
||||
for (int k = 0; k < ins->num_operands; k += 1) {
|
||||
if (ins->operands[k].kind == SCC_MIR_OP_VREG) {
|
||||
transit_vreg(func_meta, &ins->operands[k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Windows shadow space
|
||||
int total_size = ctx->offset + 32; // 加上影子空间
|
||||
// 16 字节栈对齐
|
||||
ctx->offset =
|
||||
(total_size + WIN64_STACK_ALIGN - 1) & ~(WIN64_STACK_ALIGN - 1);
|
||||
}
|
||||
|
||||
/*
|
||||
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;
|
||||
/*
|
||||
mov [RSP + 8], RCX
|
||||
push R15
|
||||
push R14
|
||||
push R13
|
||||
sub RSP, fixed-allocation-size
|
||||
lea R13, 128[RSP]
|
||||
...
|
||||
*/
|
||||
scc_mir_func_meta_t *meta = SCC_MIR_FUNC_META(func);
|
||||
int frame_size = meta->frame_size; // 之前计算好的
|
||||
|
||||
// 1. push rbp
|
||||
add_instr_1(isel, SCC_X86_IFORM_PUSH_GPRV_50, reg_operand(SCC_X86_REG_RBP));
|
||||
|
||||
// 2. mov rbp, rsp
|
||||
add_instr_2(isel, SCC_X86_IFORM_MOV_GPRV_GPRV_89,
|
||||
reg_operand(SCC_X86_REG_RBP), reg_operand(SCC_X86_REG_RSP));
|
||||
|
||||
// 3. sub rsp, frame_size
|
||||
if (frame_size > 0) {
|
||||
scc_mir_operand_t imm = {.kind = SCC_MIR_OP_IMM, .imm = frame_size};
|
||||
add_instr_2(isel, SCC_X86_IFORM_SUB_GPRV_IMMZ,
|
||||
reg_operand(SCC_X86_REG_RSP), imm);
|
||||
}
|
||||
}
|
||||
|
||||
static void epilogue(void *userdata, const scc_mir_func_t *func) {
|
||||
scc_x86_64_isel_t *isel = userdata;
|
||||
/*
|
||||
add RSP, fixed-allocation-size
|
||||
pop R13
|
||||
pop R14
|
||||
pop R15
|
||||
ret
|
||||
*/
|
||||
// 1. mov rsp, rbp
|
||||
add_instr_2(isel, SCC_X86_IFORM_MOV_GPRV_GPRV_89,
|
||||
reg_operand(SCC_X86_REG_RSP), reg_operand(SCC_X86_REG_RBP));
|
||||
|
||||
// 2. pop rbp
|
||||
add_instr_1(isel, SCC_X86_IFORM_POP_GPRV_58, reg_operand(SCC_X86_REG_RBP));
|
||||
}
|
||||
|
||||
static void lower_call(void *userdata, const scc_lir_instr_t *instr) {
|
||||
scc_x86_64_isel_t *isel = userdata;
|
||||
/*
|
||||
Windows x64 parameter passing
|
||||
https://learn.microsoft.com/zh-cn/cpp/build/x64-calling-convention?view=msvc-180#parameter-passing
|
||||
|
||||
eg.
|
||||
func1(int a, int b, int c, int d, int e, int f);
|
||||
// a in RCX, b in RDX, c in R8, d in R9, f then e passed on stack
|
||||
|
||||
func2(float a, double b, float c, double d, float e, float f);
|
||||
// a in XMM0, b in XMM1, c in XMM2, d in XMM3, f then e passed on stack
|
||||
|
||||
func3(int a, double b, int c, float d, int e, float f);
|
||||
// a in RCX, b in XMM1, c in R8, d in XMM3, f then e passed on stack
|
||||
|
||||
func4(__m64 a, __m128 b, struct c, float d, __m128 e, __m128 f);
|
||||
// a in RCX, ptr to b in RDX, ptr to c in R8, d in XMM3,
|
||||
// ptr to f passed on stack, then ptr to e passed on stack
|
||||
*/
|
||||
|
||||
for (int i = instr->metadata.call.arg_count - 1; i >= 0; i -= 1) {
|
||||
scc_lir_val_t *args = &instr->metadata.call.args[i];
|
||||
switch (i) {
|
||||
case 0:
|
||||
scc_x86_emit_move(isel, reg_operand(SCC_X86_REG_RCX),
|
||||
scc_x86_lir_val_to_mir_op(isel, args), 8);
|
||||
break;
|
||||
case 1:
|
||||
scc_x86_emit_move(isel, reg_operand(SCC_X86_REG_RDX),
|
||||
scc_x86_lir_val_to_mir_op(isel, args), 8);
|
||||
break;
|
||||
case 2:
|
||||
scc_x86_emit_move(isel, reg_operand(SCC_X86_REG_R8),
|
||||
scc_x86_lir_val_to_mir_op(isel, args), 8);
|
||||
break;
|
||||
case 3:
|
||||
scc_x86_emit_move(isel, reg_operand(SCC_X86_REG_R9),
|
||||
scc_x86_lir_val_to_mir_op(isel, args), 8);
|
||||
break;
|
||||
default:
|
||||
// Using stack
|
||||
scc_mir_operand_t op = scc_x86_lir_val_to_mir_op(isel, args);
|
||||
add_instr_1(isel,
|
||||
op.kind == SCC_MIR_OP_PREG || op.kind == SCC_MIR_OP_VREG
|
||||
? SCC_X86_IFORM_PUSH_GPRV_50
|
||||
: SCC_X86_IFORM_PUSH_IMMZ,
|
||||
op);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
emit_call(isel, instr->metadata.call.callee);
|
||||
|
||||
scc_mir_operand_t ret_reg = scc_x86_lir_val_to_mir_op(isel, &instr->to);
|
||||
if (ret_reg.kind == SCC_MIR_OP_VREG) {
|
||||
scc_x86_emit_move(isel, ret_reg, reg_operand(SCC_X86_REG_RAX), 8);
|
||||
} else {
|
||||
TODO();
|
||||
}
|
||||
}
|
||||
|
||||
static scc_mir_operand_t lower_param(void *userdata, const scc_lir_val_t *val) {
|
||||
scc_x86_64_isel_t *isel = userdata;
|
||||
Assert(val->kind == SCC_LIR_INSTR_KIND_ARG);
|
||||
switch (val->data.arg) {
|
||||
case 0:
|
||||
return reg_operand(SCC_X86_REG_RCX);
|
||||
case 1:
|
||||
return reg_operand(SCC_X86_REG_RDX);
|
||||
case 2:
|
||||
return reg_operand(SCC_X86_REG_R8);
|
||||
case 3:
|
||||
return reg_operand(SCC_X86_REG_R9);
|
||||
default:
|
||||
return (scc_mir_operand_t){.kind = SCC_MIR_OP_MEM,
|
||||
.stack_slot = -val->data.arg};
|
||||
}
|
||||
}
|
||||
|
||||
static void lower_ret(void *userdata, const scc_lir_instr_t *instr) {
|
||||
scc_x86_64_isel_t *isel = userdata;
|
||||
scc_lir_val_t ret_val = instr->metadata.ret_val;
|
||||
if (ret_val.kind != SCC_LIR_INSTR_KIND_NONE) {
|
||||
scc_x86_emit_move(isel, reg_operand(SCC_X86_REG_RAX),
|
||||
scc_x86_lir_val_to_mir_op(isel, &ret_val), 8);
|
||||
}
|
||||
emit_ret(isel);
|
||||
}
|
||||
|
||||
void scc_win_pc_x64_abi_lowering(scc_abi_lowering_t *abi_lowering) {
|
||||
abi_lowering->lower_call = lower_call;
|
||||
abi_lowering->lower_ret = lower_ret;
|
||||
abi_lowering->lower_param = lower_param;
|
||||
|
||||
abi_lowering->lower_va_start = nullptr;
|
||||
abi_lowering->lower_va_arg = nullptr;
|
||||
abi_lowering->lower_va_copy = nullptr;
|
||||
abi_lowering->lower_va_end = nullptr;
|
||||
}
|
||||
|
||||
void scc_win_pc_x64_prolog_epilog(scc_prolog_epilog_t *prolog_epilog) {
|
||||
prolog_epilog->prolog = prologue;
|
||||
prolog_epilog->epilog = epilogue;
|
||||
}
|
||||
Reference in New Issue
Block a user