feat(argparse): 添加枚举类型选项支持
添加了对命令行参数枚举类型的支持,允许用户从预定义的选项中进行选择, 并在解析时进行验证。同时修复了空值检查问题。 refactor(mir): 重构pass管理系统 将原有的pass管理器系统简化为直接的pass执行函数,移除了复杂的 依赖管理和流水线构建机制,使系统更加简洁明了。 chore(ir): 添加Windows x64 ABI实现 实现了Windows x64平台的ABI约定,包括参数传递、寄存器使用和 栈帧布局的处理逻辑。 feat(config): 统一编译阶段控制配置 将多个独立的布尔类型编译阶段控制改为统一的枚举类型,便于 管理和扩展新的编译阶段。
This commit is contained in:
@@ -218,10 +218,11 @@ static inline void scc_argparse_spec_setup_float(scc_argparse_spec_t *spec,
|
||||
|
||||
static inline void scc_argparse_spec_setup_choices(scc_argparse_spec_t *spec,
|
||||
const char **values,
|
||||
int count) {
|
||||
int count, int *store) {
|
||||
spec->value_type = SCC_ARGPARSE_VAL_TYPE_ENUM;
|
||||
spec->choices.values = values;
|
||||
spec->choices.count = count;
|
||||
spec->store.int_store = store;
|
||||
}
|
||||
|
||||
// 添加设置列表的辅助函数(内联)
|
||||
|
||||
@@ -69,6 +69,10 @@ static inline void prepare_cmd(scc_optparse_t *optparse,
|
||||
min_args = 1;
|
||||
max_args = 65535; // FIXME maybe INT_MAX ?
|
||||
break;
|
||||
case SCC_ARGPARSE_VAL_TYPE_ENUM:
|
||||
min_args = 1;
|
||||
max_args = 1;
|
||||
break;
|
||||
default:
|
||||
min_args = 0;
|
||||
max_args = 0;
|
||||
@@ -136,13 +140,22 @@ static int transite_error(scc_argparse_context_t *ctx) {
|
||||
return error;
|
||||
}
|
||||
|
||||
static int check_choices(const scc_argparse_spec_t *spec, const char *value) {
|
||||
static int check_choices(const scc_argparse_spec_t *spec, const char *value,
|
||||
int *out_index) {
|
||||
if (!spec->choices.values || spec->choices.count == 0) {
|
||||
return SCC_ARGPARSE_ERR_NONE;
|
||||
}
|
||||
|
||||
if (value == nullptr) {
|
||||
return SCC_ARGPARSE_ERR_MISSING_VALUE;
|
||||
}
|
||||
if (out_index)
|
||||
*out_index = -1;
|
||||
|
||||
for (int i = 0; i < spec->choices.count; i += 1) {
|
||||
if (scc_strcmp(value, spec->choices.values[i]) == 0) {
|
||||
if (out_index)
|
||||
*out_index = i;
|
||||
return SCC_ARGPARSE_ERR_NONE;
|
||||
}
|
||||
}
|
||||
@@ -219,6 +232,9 @@ static int handle_option(scc_argparse_context_t *ctx, scc_argparse_t *parser) {
|
||||
|
||||
if (opt->spec.value_type == SCC_ARGPARSE_VAL_TYPE_BOOL) {
|
||||
*opt->spec.store.bool_store = true;
|
||||
} else if (opt->spec.value_type == SCC_ARGPARSE_VAL_TYPE_ENUM) {
|
||||
return check_choices(&opt->spec, ctx->result.value,
|
||||
opt->spec.store.int_store);
|
||||
}
|
||||
|
||||
if (!ctx->result.value) {
|
||||
|
||||
4
libs/ir/mir/include/core_pass/scc_frame_layout.h
Normal file
4
libs/ir/mir/include/core_pass/scc_frame_layout.h
Normal file
@@ -0,0 +1,4 @@
|
||||
#ifndef __SCC_FRAME_LAYOUT_H__
|
||||
#define __SCC_FRAME_LAYOUT_H__
|
||||
|
||||
#endif /* __SCC_FRAME_LAYOUT_H__ */
|
||||
14
libs/ir/mir/include/core_pass/scc_prolog_epilog.h
Normal file
14
libs/ir/mir/include/core_pass/scc_prolog_epilog.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef __SCC_PROLOG_EPILOG_H__
|
||||
#define __SCC_PROLOG_EPILOG_H__
|
||||
|
||||
#include <scc_lir_module.h>
|
||||
#include <scc_mir_module.h>
|
||||
typedef void (*scc_prolog_epilog_fn)(void *userdata,
|
||||
const scc_mir_func_t *func);
|
||||
|
||||
typedef struct scc_prolog_epilog {
|
||||
scc_prolog_epilog_fn prolog;
|
||||
scc_prolog_epilog_fn epilog;
|
||||
} scc_prolog_epilog_t;
|
||||
|
||||
#endif /* __SCC_PROLOG_EPILOG_H__ */
|
||||
@@ -7,7 +7,4 @@
|
||||
void scc_lir2mir(scc_mir_module_t *mir_module,
|
||||
const scc_lir_module_t *lir_module);
|
||||
|
||||
// TODO
|
||||
void scc_mir_pass(scc_mir_module_t *mir_module);
|
||||
|
||||
#endif /* __SCC_LIR2MIR_H__ */
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef __SCC_MIR_PASS_H__
|
||||
#define __SCC_MIR_PASS_H__
|
||||
|
||||
#include "scc_mir.h"
|
||||
#include "scc_mir_module.h"
|
||||
|
||||
/** Pass 的唯一标识符 (枚举) */
|
||||
typedef enum {
|
||||
@@ -25,107 +25,6 @@ typedef enum {
|
||||
SCC_MIR_STAGE_POST_REGALLOC // 寄存器分配后
|
||||
} scc_mir_pass_stage_t;
|
||||
|
||||
/**
|
||||
* @brief Pass 执行函数原型
|
||||
* @param func 输入的 MIR 函数 (Pass 可修改或替换)
|
||||
* @param ctx 可选的上下文指针 (如目标机器信息、调试标志)
|
||||
* @return 执行后的 MIR 函数指针 (若未修改可返回原指针,若需替换可返回新指针)
|
||||
* @warning Pass 应负责释放不再使用的旧函数资源
|
||||
*/
|
||||
typedef scc_mir_func_t *(*scc_mir_pass_fn_t)(scc_mir_func_t *func, void *ctx);
|
||||
|
||||
typedef struct scc_lir_pass_desc {
|
||||
scc_mir_pass_id_t id; // 唯一标识
|
||||
const char *name; // 可读名称
|
||||
scc_mir_pass_stage_t stage; // 所属阶段
|
||||
scc_mir_pass_id_t *deps; // 依赖的 Pass ID 数组 (可为 NULL)
|
||||
usize num_deps; // 依赖数量
|
||||
scc_mir_pass_fn_t run; // 执行函数
|
||||
cbool required; // 是否为核心必需 Pass
|
||||
} scc_mir_pass_desc_t;
|
||||
|
||||
/* --------------------------------------------------------------------------
|
||||
* Pass 管理器
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
typedef struct scc_lir_pass_manager {
|
||||
SCC_VEC(scc_mir_pass_desc_t) passes; // 所有已注册 Pass
|
||||
SCC_VEC(scc_mir_pass_id_t) pipeline; // 当前流水线顺序
|
||||
void *ctx; // 传递给每个 Pass 的上下文
|
||||
} scc_mir_pass_manager_t;
|
||||
|
||||
/**
|
||||
* @brief 初始化 Pass 管理器
|
||||
* @param pm 管理器指针
|
||||
* @param ctx 全局上下文 (如 scc_target_t*)
|
||||
*/
|
||||
void scc_mir_pass_manager_init(scc_mir_pass_manager_t *pm, void *ctx);
|
||||
|
||||
/**
|
||||
* @brief 销毁 Pass 管理器,释放资源
|
||||
*/
|
||||
void scc_mir_pass_manager_drop(scc_mir_pass_manager_t *pm);
|
||||
|
||||
/**
|
||||
* @brief 注册一个 Pass (通常在编译器初始化时调用)
|
||||
* @param pm 管理器
|
||||
* @param desc Pass 描述符
|
||||
*/
|
||||
void scc_mir_pass_register(scc_mir_pass_manager_t *pm,
|
||||
const scc_mir_pass_desc_t *desc);
|
||||
|
||||
/**
|
||||
* @brief 构建默认的核心流水线 (仅包含 required=true 的 Pass)
|
||||
* @param pm 管理器
|
||||
*
|
||||
* 自动按阶段排序并解析依赖。
|
||||
*/
|
||||
void scc_mir_pass_build_core_pipeline(scc_mir_pass_manager_t *pm);
|
||||
|
||||
/**
|
||||
* @brief 向流水线追加一个可选 Pass
|
||||
* @param pm 管理器
|
||||
* @param id 要添加的 Pass ID
|
||||
* @return 成功返回 true,若依赖不满足或 ID 无效返回 false
|
||||
*/
|
||||
cbool scc_mir_pass_add_to_pipeline(scc_mir_pass_manager_t *pm,
|
||||
scc_mir_pass_id_t id);
|
||||
|
||||
/**
|
||||
* @brief 对指定函数运行当前流水线中的所有 Pass
|
||||
* @param pm 管理器
|
||||
* @param func 要处理的 LIR 函数
|
||||
* @return 最终处理后的函数指针
|
||||
*/
|
||||
scc_lir_func_t *scc_mir_pass_run_pipeline(scc_mir_pass_manager_t *pm,
|
||||
scc_lir_func_t *func);
|
||||
|
||||
/* --------------------------------------------------------------------------
|
||||
* 便捷宏:声明并注册一个 Pass
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* @brief 在模块内部声明并注册一个 Pass
|
||||
* @param pm 管理器指针
|
||||
* @param id Pass ID 枚举值
|
||||
* @param name_str Pass 名称字符串
|
||||
* @param stage 运行阶段
|
||||
* @param deps_arr 依赖数组 (可为 NULL)
|
||||
* @param num_deps 依赖数量
|
||||
* @param fn 执行函数
|
||||
* @param req 是否必需
|
||||
*/
|
||||
#define SCC_MIR_REGISTER_PASS(pm, id, name_str, stage, deps_arr, num_deps, fn, \
|
||||
req) \
|
||||
do { \
|
||||
scc_mir_pass_desc_t desc = {.id = (id), \
|
||||
.name = (name_str), \
|
||||
.stage = (stage), \
|
||||
.deps = (deps_arr), \
|
||||
.num_deps = (num_deps), \
|
||||
.run = (fn), \
|
||||
.required = (req)}; \
|
||||
scc_mir_pass_register((pm), &desc); \
|
||||
} while (0)
|
||||
void scc_mir_pass(scc_mir_module_t *mir_module, scc_mir_pass_stage_t stage);
|
||||
|
||||
#endif /* __SCC_LIR_PASS_H__ */
|
||||
|
||||
@@ -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