refactor(ast2ir): 移除废弃的ABI依赖并优化类型转换处理

移除了对scc_abi包的依赖,将相关头文件从libs/abi移动到libs/ast2ir目录下。
重构了基本类型解析功能,将parse_base_type函数提取为独立的
scc_ast2ir_parse_base_type实现,并支持有符号/无符号类型区分。

feat(ast2ir): 实现整数常量表达式求值器

新增了完整的整数常量表达式求值功能,支持C11标准中的常量表达式规则,
包括字面量、标识符、sizeof/_Alignof、一元/二元运算、条件表达式和
类型转换等操作。该功能用于数组大小和枚举值的编译期计算验证。

refactor(ast2ir): 完善类型提升和算术转换机制

改进了整数提升和寻常算术转换的实现,修复了移位操作的符号处理问题,
添加了无符号比较操作的支持,增强了类型安全检查,统一了错误处理流程。

fix(ast2ir): 修复赋值表达式返回值和数组大小计算问题

修正了赋值表达式的返回值处理,确保返回右侧值而不是存储指令引用。
使用新的常量表达式求值器替代原有的硬编码数组大小计算,提高了
数组声明的正确性。
This commit is contained in:
zzy
2026-05-31 17:30:22 +08:00
parent c4467b8420
commit d2eafa9dc6
27 changed files with 2579 additions and 218 deletions

View File

@@ -3,6 +3,15 @@
#include "../core_pass/scc_reg_alloc.h"
/* 默认位掩码策略R8/R9 被排除ABI 参数寄存器),剩余 6 个通用寄存器 */
void scc_reg_alloc_fill_arch_x86(scc_reg_alloc_op_t *ops);
/*
* 顺序分配策略:光标递增,永不回收。
* 池中包含 R8-R15共 8 个寄存器),每分配一个光标前进一格。
* release_reg 为空操作;寄存器耗尽后 acquire_reg 返回 -1
* 由分配器回退为纯栈操作。
*/
void scc_reg_alloc_fill_seq_x86(scc_reg_alloc_op_t *ops);
#endif /* __SCC_X86_REG_ALLOC_H__ */

View File

@@ -148,6 +148,17 @@ static scc_x86_operand_value_t new_vreg_temp(scc_x86_64_isel_t *isel,
return scc_x86_op_vreg(scc_mir_alloc_vreg(isel->func), size);
}
/* 确保操作数为寄存器: IMM → 加载到临时虚拟寄存器 */
static scc_x86_operand_value_t ensure_reg(scc_x86_64_isel_t *isel,
scc_x86_operand_value_t op) {
if (op.kind == SCC_X86_OPR_IMM) {
scc_x86_operand_value_t tmp = new_vreg_temp(isel, op.size);
scc_x86_emit_move(isel, tmp, op);
return tmp;
}
return op;
}
static scc_x86_iform_t cond_to_setcc(scc_lir_cond_t cond) {
switch (cond) {
case SCC_LIR_COND_EQ:
@@ -343,9 +354,11 @@ static void sel_mir(scc_x86_64_isel_t *isel, const scc_lir_instr_t *instr) {
break;
/* ---- 一元运算 ---- */
case SCC_LIR_NEG:
emit_copy_if_needed(isel, dst, src0);
add_instr_1(isel, SCC_X86_IFORM_NEG_GPRV, dst);
break;
case SCC_LIR_NOT:
emit_copy_if_needed(isel, dst, src0);
add_instr_1(isel, SCC_X86_IFORM_NOT_GPRV, dst);
break;
/* ---- 算术/逻辑二元运算 ---- */
@@ -416,11 +429,18 @@ static void sel_mir(scc_x86_64_isel_t *isel, const scc_lir_instr_t *instr) {
scc_x86_operand_value_t rax = scc_x86_op_preg(SCC_X86_REG_RAX, size);
scc_x86_operand_value_t rdx = scc_x86_op_preg(SCC_X86_REG_RDX, size);
scc_x86_emit_move(isel, rax, src0);
if (instr->op == SCC_LIR_DIV_S || instr->op == SCC_LIR_REM_S) {
if (size < 8) {
// TODO 可能需要在lir进行size对齐
scc_x86_operand_value_t rax64 =
scc_x86_op_preg(SCC_X86_REG_RAX, 8);
add_instr_2(isel, SCC_X86_IFORM_MOVSXD_GPRV_GPRZ, rax64, src0);
} else {
scc_x86_emit_move(isel, rax, src0);
}
add_instr_0(isel, SCC_X86_IFORM_CQO);
} else {
scc_x86_emit_move(isel, rax, src0);
scc_x86_operand_value_t zero = scc_x86_op_imm(0, size);
scc_x86_emit_move(isel, rdx, zero);
}
@@ -429,7 +449,12 @@ static void sel_mir(scc_x86_64_isel_t *isel, const scc_lir_instr_t *instr) {
(instr->op == SCC_LIR_DIV_S || instr->op == SCC_LIR_REM_S)
? SCC_X86_IFORM_IDIV_GPRV
: SCC_X86_IFORM_DIV_GPRV;
add_instr_1(isel, div_if, src1);
scc_x86_operand_value_t divisor = src1;
if (src1.kind == SCC_X86_OPR_IMM) {
divisor = new_vreg_temp(isel, size);
scc_x86_emit_move(isel, divisor, src1);
}
add_instr_1(isel, div_if, divisor);
if (instr->op == SCC_LIR_REM_S || instr->op == SCC_LIR_REM_U)
scc_x86_emit_move(isel, dst, rdx);
@@ -440,6 +465,11 @@ static void sel_mir(scc_x86_64_isel_t *isel, const scc_lir_instr_t *instr) {
/* ---- 比较指令 ---- */
case SCC_LIR_CMP: {
Assert(src0.size == src1.size);
if (src0.kind == SCC_X86_OPR_IMM) {
scc_x86_operand_value_t tmp_reg = new_vreg_temp(isel, src0.size);
scc_x86_emit_move(isel, tmp_reg, src0);
src0 = tmp_reg;
}
if (scc_x86_op_is_vreg(&src0) && src1.kind == SCC_X86_OPR_IMM)
// SCC_X86_IFORM_CMP_GPR8_IMMB_82R7 历史遗留指令在amd64中已不再使用
add_instr_2(isel,
@@ -511,6 +541,7 @@ static void sel_mir(scc_x86_64_isel_t *isel, const scc_lir_instr_t *instr) {
int from_size = instr->metadata.extend.from_size;
scc_x86_operand_value_t ext_src =
scc_x86_lir_val_to_mir_op(isel, &instr->arg0, from_size);
ext_src = ensure_reg(isel, ext_src);
if (instr->ext == SCC_LIR_EXT_ZEXT) {
if (from_size == 4) {

View File

@@ -42,12 +42,27 @@ scc_mir_x86_mov_mem_imm(scc_x86_operand_value_t op0,
: SCC_X86_IFORM_MOV_MEMV_IMMZ;
}
static inline u8 get_op_size(scc_x86_operand_value_t op, cbool get_value) {
if (get_value) {
return op.size;
}
if (op.kind == SCC_X86_OPR_MEM) {
return 8;
}
if (op.kind == SCC_X86_OPR_RELOC && op.reloc.kind == SCC_X86_OPR_MEM) {
return 8;
}
return op.size;
}
void scc_x86_emit_move_to_vec(scc_mir_x86_instr_vec_t *vec,
scc_x86_operand_value_t dst,
scc_x86_operand_value_t src) {
if (dst.size != src.size) {
LOG_WARN("Mismatched register sizes for move %d != %d", dst.size,
src.size);
u8 dst_size = get_op_size(dst, false);
u8 src_size = get_op_size(src, false);
if (dst_size != src_size) {
LOG_FATAL("Mismatched register sizes for move %d != %d", dst_size,
src_size);
}
scc_mir_x86_instr_t ins;
if (dst.kind == SCC_X86_OPR_REG) {
@@ -60,7 +75,7 @@ void scc_x86_emit_move_to_vec(scc_mir_x86_instr_vec_t *vec,
} else if (src.kind == SCC_X86_OPR_MEM ||
(src.kind == SCC_X86_OPR_RELOC &&
src.reloc.target == SCC_X86_RELOC_TARGET_SYMBOL)) {
Assert(dst.size == 8);
Assert(dst_size == 8);
scc_mir_x86_instr_2(&ins, SCC_X86_IFORM_LEA_GPRV_AGEN, dst, src,
scc_pos_create());
} else {
@@ -85,8 +100,11 @@ void scc_x86_emit_move_to_vec(scc_mir_x86_instr_vec_t *vec,
void scc_x86_emit_load_to_vec(scc_mir_x86_instr_vec_t *vec,
scc_x86_operand_value_t dst,
scc_x86_operand_value_t src_addr) {
if (dst.size != src_addr.size) {
LOG_WARN("Mismatched sizes for load %d != %d", dst.size, src_addr.size);
u8 dst_size = get_op_size(dst, false);
u8 src_addr_size = get_op_size(src_addr, true);
if (dst_size != src_addr_size) {
LOG_FATAL("Mismatched sizes for load %d != %d", dst_size,
src_addr_size);
}
Assert(dst.kind == SCC_X86_OPR_REG);
scc_x86_operand_value_t mem_op;
@@ -99,9 +117,9 @@ void scc_x86_emit_load_to_vec(scc_mir_x86_instr_vec_t *vec,
.index = SCC_X86_REG_INVALID,
.scale = 1,
.disp.displacement = 0,
.disp.displacement_bits = src_addr.size * 8,
.disp.displacement_bits = src_addr_size * 8,
},
.size = src_addr.size,
.size = src_addr_size,
};
} else if (src_addr.kind == SCC_X86_OPR_MEM) {
mem_op = src_addr;
@@ -117,9 +135,11 @@ void scc_x86_emit_load_to_vec(scc_mir_x86_instr_vec_t *vec,
void scc_x86_emit_store_to_vec(scc_mir_x86_instr_vec_t *vec,
scc_x86_operand_value_t dst_addr,
scc_x86_operand_value_t src) {
if (dst_addr.size != src.size) {
LOG_WARN("Mismatched sizes for store %d != %d", dst_addr.size,
src.size);
u8 dst_addr_size = get_op_size(dst_addr, true);
u8 src_size = get_op_size(src, false);
if (dst_addr_size != src_size) {
LOG_FATAL("Mismatched sizes for store %d != %d", dst_addr_size,
src_size);
}
scc_x86_operand_value_t mem_op;
if (dst_addr.kind == SCC_X86_OPR_REG) {
@@ -131,9 +151,9 @@ void scc_x86_emit_store_to_vec(scc_mir_x86_instr_vec_t *vec,
.index = SCC_X86_REG_INVALID,
.scale = 1,
.disp.displacement = 0,
.disp.displacement_bits = dst_addr.size * 8,
.disp.displacement_bits = dst_addr_size * 8,
},
.size = dst_addr.size,
.size = dst_addr_size,
};
} else if (dst_addr.kind == SCC_X86_OPR_MEM) {
mem_op = dst_addr;

View File

@@ -73,16 +73,25 @@ static void get_implicit_regs(void *ctx, int opcode, const int **uses,
}
}
/* ========== 寄存器池 ========== */
/* ========== 默认寄存器池(位掩码策略) ========== */
static const int reg_pool[] = {
SCC_X86_REG_R8, SCC_X86_REG_R9, SCC_X86_REG_R10, SCC_X86_REG_R11,
SCC_X86_REG_R12, SCC_X86_REG_R13, SCC_X86_REG_R14, SCC_X86_REG_R15};
/*
* R8/R9 被排除在通用寄存器池之外,因为它们被 Win64 ABI 用作参数传递寄存器
* RCX=arg0, RDX=arg1, R8=arg2, R9=arg3
* 如果把它们放在池中,当 lower_call 发出 MOV R8, vreg 时,
* 分配器可能把 R8 分配给源 vreg导致冗余的 MOV R8, R8。
*
* 可用寄存器: R10, R11, R12, R13, R14, R15
*/
static const int reg_pool[] = {SCC_X86_REG_R10, SCC_X86_REG_R11,
SCC_X86_REG_R12, SCC_X86_REG_R13,
SCC_X86_REG_R14, SCC_X86_REG_R15};
#define REG_POOL_SIZE ((int)SCC_ARRLEN(reg_pool))
static uint32_t reg_mask = 0;
static int acquire_reg(void *ctx) {
(void)ctx;
for (int i = 0; i < SCC_ARRLEN(reg_pool); i++)
for (int i = 0; i < REG_POOL_SIZE; i++)
if (!(reg_mask & (1u << i))) {
reg_mask |= (1u << i);
return reg_pool[i];
@@ -92,7 +101,7 @@ static int acquire_reg(void *ctx) {
static void release_reg(void *ctx, int preg) {
(void)ctx;
for (int i = 0; i < SCC_ARRLEN(reg_pool); i++)
for (int i = 0; i < REG_POOL_SIZE; i++)
if (reg_pool[i] == preg) {
reg_mask &= ~(1u << i);
return;
@@ -101,7 +110,7 @@ static void release_reg(void *ctx, int preg) {
static void mark_reg_used(void *ctx, int preg) {
(void)ctx;
for (int i = 0; i < SCC_ARRLEN(reg_pool); i++)
for (int i = 0; i < REG_POOL_SIZE; i++)
if (reg_pool[i] == preg) {
reg_mask |= (1u << i);
return;
@@ -113,6 +122,57 @@ static void clean_mark_regs(void *ctx) {
reg_mask = 0;
}
/* ========== 顺序分配策略(光标,永不回收) ========== */
/*
* 顺序分配策略按固定顺序分配寄存器R8 → R9 → ... → R15
* 一旦分配就永不回收release_reg 为空操作)。
* 当所有 8 个寄存器耗尽后acquire_reg 返回 -1
* 分配器将 vreg 直接替换为栈槽spill/reload 由内存操作数隐式完成)。
*
* 包含 R8/R9 是安全的,因为顺序分配避免了冲突:
* - 如果 R8 已被分配,参数 MOV R8, vreg 中的 vreg 会分配下一个可用寄存器(如
* R10 然后通过 MOV R8, R10 将值移入 R8。
* - 如果 R8 尚未分配,参数 MOV R8, vreg 的 vreg 会分配到 R8
* 产生 MOV R8, R8空操作这在功能上是正确的。
*/
static const int seq_pool[] = {
SCC_X86_REG_R8, SCC_X86_REG_R9, SCC_X86_REG_R10, SCC_X86_REG_R11,
SCC_X86_REG_R12, SCC_X86_REG_R13, SCC_X86_REG_R14, SCC_X86_REG_R15};
#define SEQ_POOL_SIZE ((int)SCC_ARRLEN(seq_pool))
static int seq_cursor = 0;
static int seq_acquire_reg(void *ctx) {
(void)ctx;
if (seq_cursor < SEQ_POOL_SIZE)
return seq_pool[seq_cursor++];
return -1; /* 寄存器耗尽 */
}
static void seq_release_reg(void *ctx, int preg) {
(void)ctx;
(void)preg;
/* 永不回收:释放为空操作 */
}
static void seq_mark_reg_used(void *ctx, int preg) {
(void)ctx;
for (int i = seq_cursor; i < SEQ_POOL_SIZE; i++)
if (seq_pool[i] == preg) {
/* 将光标移到该寄存器之后(燃烧) */
if (i + 1 > seq_cursor)
seq_cursor = i + 1;
return;
}
}
static void seq_clean_mark_regs(void *ctx) {
(void)ctx;
seq_cursor = 0;
}
/* ========== 指令迭代器(两种策略共享) ========== */
static void x86_alloc_iter_begin(scc_reg_alloc_iter_t *iter) {
iter->op_idx = 0;
iter->op_sub_idx = 0;
@@ -128,8 +188,8 @@ static cbool x86_alloc_iter_next(scc_reg_alloc_iter_t *iter, int *out_vreg,
while (iter->op_idx < num_ops) {
const scc_x86_operand_value_t *op =
&ins->x86_instr.operands[iter->op_idx];
scc_reg_op_access_t base_access = SCC_REG_ALLOC_OP_ACCESS_READWRITE;
// base_access = get_operand_access(opcode, iter->op_idx);
scc_reg_op_access_t base_access =
get_operand_access(opcode, iter->op_idx);
*out_size = op->size;
if (op->kind == SCC_X86_OPR_REG) {
@@ -223,6 +283,8 @@ static void x86_alloc_iter_end(scc_reg_alloc_iter_t *iter) {
(void)iter; // 无需清理
}
/* ========== 回调表填充 ========== */
void scc_reg_alloc_fill_arch_x86(scc_reg_alloc_op_t *ops) {
ops->acquire_reg = acquire_reg;
ops->release_reg = release_reg;
@@ -240,3 +302,21 @@ void scc_reg_alloc_fill_arch_x86(scc_reg_alloc_op_t *ops) {
ops->emit_reload = emit_reload;
ops->emit_copy = emit_copy;
}
void scc_reg_alloc_fill_seq_x86(scc_reg_alloc_op_t *ops) {
ops->acquire_reg = seq_acquire_reg;
ops->release_reg = seq_release_reg;
ops->mark_reg_used = seq_mark_reg_used;
ops->clean_mark_regs = seq_clean_mark_regs;
ops->alloc_iter_begin = x86_alloc_iter_begin;
ops->alloc_iter_next = x86_alloc_iter_next;
ops->alloc_iter_replace_preg = x86_alloc_iter_replace_preg;
ops->alloc_iter_replace_slot = x86_alloc_iter_replace_slot;
ops->alloc_iter_end = x86_alloc_iter_end;
ops->get_implicit_regs = get_implicit_regs;
ops->emit_spill = emit_spill;
ops->emit_reload = emit_reload;
ops->emit_copy = emit_copy;
}

View File

@@ -49,16 +49,21 @@ static void alloc_instr(scc_reg_alloc_ctx_t *ctx,
if (mapping == 0)
slot = scc_mir_vreg_map2slot(ctx->func, vreg, size, 8);
preg = ops->acquire_reg(ctx);
scc_vec_push(allocated, preg);
if (preg >= 0) {
scc_vec_push(allocated, preg);
if (access == SCC_REG_ALLOC_OP_ACCESS_READ ||
access == SCC_REG_ALLOC_OP_ACCESS_READWRITE)
ops->emit_reload(&before, preg, slot, size);
if (access == SCC_REG_ALLOC_OP_ACCESS_WRITE ||
access == SCC_REG_ALLOC_OP_ACCESS_READWRITE)
ops->emit_spill(&after, preg, slot, size);
if (access == SCC_REG_ALLOC_OP_ACCESS_READ ||
access == SCC_REG_ALLOC_OP_ACCESS_READWRITE)
ops->emit_reload(&before, preg, slot, size);
if (access == SCC_REG_ALLOC_OP_ACCESS_WRITE ||
access == SCC_REG_ALLOC_OP_ACCESS_READWRITE)
ops->emit_spill(&after, preg, slot, size);
ops->alloc_iter_replace_preg(&iter, preg, size);
ops->alloc_iter_replace_preg(&iter, preg, size);
} else {
/* 寄存器耗尽顺序策略vreg 直接使用栈槽 */
ops->alloc_iter_replace_slot(&iter, slot, size);
}
}
ops->alloc_iter_end(&iter);

View File

@@ -171,6 +171,10 @@ void scc_win_pc_x64_prolog_epilog_init(scc_prolog_epilog_t *ctx) {
ctx->need_epilog = need_epilog;
}
/*
windows x64 calling convention
https://learn.microsoft.com/zh-cn/cpp/build/x64-calling-convention?view=msvc-180#varargs
*/
static void lower_call(void *userdata, const scc_lir_instr_t *instr) {
scc_x86_64_isel_t *isel = userdata;