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

@@ -174,7 +174,6 @@ static scc_lir_cond_t map_cmp_cond(scc_hir_op_type_t op, cbool is_float) {
Panic("invalid float cmp");
}
} else {
// 默认为有符号比较 (无符号需额外处理类型)
switch (op) {
case SCC_HIR_OP_EQ:
return SCC_LIR_COND_EQ;
@@ -188,6 +187,14 @@ static scc_lir_cond_t map_cmp_cond(scc_hir_op_type_t op, cbool is_float) {
return SCC_LIR_COND_SGT;
case SCC_HIR_OP_GE:
return SCC_LIR_COND_SGE;
case SCC_HIR_OP_ULT:
return SCC_LIR_COND_ULT;
case SCC_HIR_OP_ULE:
return SCC_LIR_COND_ULE;
case SCC_HIR_OP_UGT:
return SCC_LIR_COND_UGT;
case SCC_HIR_OP_UGE:
return SCC_LIR_COND_UGE;
default:
Panic("invalid int cmp");
}
@@ -196,6 +203,54 @@ static scc_lir_cond_t map_cmp_cond(scc_hir_op_type_t op, cbool is_float) {
}
static scc_lir_op_t map_binop(scc_hir_op_type_t op, cbool is_float) {
switch (op) {
case SCC_HIR_OP_EMPTY:
return SCC_LIR_NOP;
/// Not equal to.
case SCC_HIR_OP_NEQ:
/// Equal to.
case SCC_HIR_OP_EQ:
/// Greater than.
case SCC_HIR_OP_GT:
/// Less than.
case SCC_HIR_OP_LT:
/// Greater than or equal to.
case SCC_HIR_OP_GE:
/// Less than or equal to.
case SCC_HIR_OP_LE:
/// Unsigned greater than.
case SCC_HIR_OP_UGT:
/// Unsigned less than.
case SCC_HIR_OP_ULT:
/// Unsigned greater than or equal to.
case SCC_HIR_OP_UGE:
/// Unsigned less than or equal to.
case SCC_HIR_OP_ULE:
return SCC_LIR_CMP;
/// Bitwise AND.
case SCC_HIR_OP_AND:
return SCC_LIR_AND;
/// Bitwise OR.
case SCC_HIR_OP_OR:
return SCC_LIR_OR;
/// Bitwise XOR.
case SCC_HIR_OP_XOR:
return SCC_LIR_XOR;
/// Bitwise NOT.
case SCC_HIR_OP_NOT:
return SCC_LIR_NOT;
/// Shift left logical.
case SCC_HIR_OP_SHL:
return SCC_LIR_SHL;
/// Shift right logical.
case SCC_HIR_OP_SHR:
return SCC_LIR_SHR;
/// Shift right arithmetic.
case SCC_HIR_OP_SAR:
return SCC_LIR_SAR;
default:
break;
}
if (is_float) {
switch (op) {
case SCC_HIR_OP_ADD:
@@ -206,6 +261,9 @@ static scc_lir_op_t map_binop(scc_hir_op_type_t op, cbool is_float) {
return SCC_LIR_FMUL;
case SCC_HIR_OP_DIV:
return SCC_LIR_FDIV;
case SCC_HIR_OP_MOD:
TODO();
return SCC_LIR_FNEG;
default:
Panic("unsupported float binop");
}
@@ -221,20 +279,8 @@ static scc_lir_op_t map_binop(scc_hir_op_type_t op, cbool is_float) {
return SCC_LIR_DIV_S;
case SCC_HIR_OP_MOD:
return SCC_LIR_REM_S;
case SCC_HIR_OP_AND:
return SCC_LIR_AND;
case SCC_HIR_OP_OR:
return SCC_LIR_OR;
case SCC_HIR_OP_XOR:
return SCC_LIR_XOR;
case SCC_HIR_OP_SHL:
return SCC_LIR_SHL;
case SCC_HIR_OP_SHR:
return SCC_LIR_SHR;
case SCC_HIR_OP_SAR:
return SCC_LIR_SAR;
default:
return SCC_LIR_CMP;
Panic("unsupported binop");
}
}
return SCC_LIR_NOP;
@@ -277,7 +323,10 @@ static void translate_hir_value(ir2lir_ctx_t *ctx, scc_hir_value_t *value,
switch (value->tag) {
case SCC_HIR_VALUE_TAG_OP: {
scc_lir_val_t lhs = ir_value_to_lir_operand(ctx, value->data.op.lhs);
scc_lir_val_t rhs = ir_value_to_lir_operand(ctx, value->data.op.rhs);
scc_lir_val_t rhs = SCC_LIR_NONE();
if (value->data.op.rhs != SCC_HIR_REF_nullptr) {
rhs = ir_value_to_lir_operand(ctx, value->data.op.rhs);
}
scc_lir_op_t op = map_binop(value->data.op.op, is_float);
scc_lir_instr_t instr = {
@@ -308,9 +357,18 @@ static void translate_hir_value(ir2lir_ctx_t *ctx, scc_hir_value_t *value,
ir_value_to_lir_operand(ctx, value->data.store.value);
scc_lir_val_t addr =
ir_value_to_lir_operand(ctx, value->data.store.target);
ty = scc_hir_module_get_type_by_value(ctx->hir_module,
value->data.store.value);
ir_type_to_lir_size_ext(ty, &size, &ext);
/* 修复: 使用目标指针的元素类型确定 store 宽度而非源值类型 */
scc_hir_type_t *ptr_type = scc_hir_module_get_type_by_value(
ctx->hir_module, value->data.store.target);
if (ptr_type && ptr_type->tag == SCC_HIR_TYPE_PTR) {
scc_hir_type_t *elem_type = scc_hir_module_get_type(
ctx->hir_module, ptr_type->data.pointer.base);
ir_type_to_lir_size_ext(elem_type, &size, &ext);
} else {
ty = scc_hir_module_get_type_by_value(ctx->hir_module,
value->data.store.value);
ir_type_to_lir_size_ext(ty, &size, &ext);
}
scc_lir_instr_t instr = {.op = SCC_LIR_STORE,
.ext = ext,
.size = size,
@@ -446,10 +504,9 @@ static void translate_hir_value(ir2lir_ctx_t *ctx, scc_hir_value_t *value,
} else {
// 计算源类型宽度
scc_hir_type_t *src_type = scc_hir_module_get_type(
ctx->hir_module,
scc_hir_module_get_value(ctx->hir_module,
value->data.conv.operand)
->type);
ctx->hir_module, scc_hir_module_get_value(
ctx->hir_module, value->data.conv.operand)
->type);
int from_size = scc_hir_module_type_size(ctx->hir_module, src_type);
scc_lir_instr_t instr = {.op = SCC_LIR_EXTEND,
.size = size,