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

@@ -38,6 +38,10 @@ void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, const scc_ast_stmt_t *stmt);
scc_hir_type_ref_t scc_ast2ir_type(scc_ast2ir_ctx_t *ctx,
const scc_ast_qual_type_t *ast_type);
scc_hir_type_ref_t
scc_ast2ir_parse_base_type(scc_ast2ir_ctx_t *ctx,
const scc_ast_qual_type_t *ast_type);
// ====== 类型提升Type Promotion接口 ======
/**
@@ -47,16 +51,17 @@ scc_hir_type_ref_t scc_ast2ir_type(scc_ast2ir_ctx_t *ctx,
* @return 提升后的类型的引用,若无需提升则返回原始类型
*/
scc_hir_type_ref_t scc_ast2ir_integer_promotion(scc_ast2ir_ctx_t *ctx,
scc_hir_type_ref_t type_ref);
scc_hir_type_ref_t type_ref);
/**
* @brief 寻常算术转换C11 6.3.1.8
* 对二元算术操作的两个操作数类型找到公共类型
* @return 公共类型的引用
*/
scc_hir_type_ref_t scc_ast2ir_usual_arithmetic_conversion(
scc_ast2ir_ctx_t *ctx, scc_hir_type_ref_t t1_ref,
scc_hir_type_ref_t t2_ref);
scc_hir_type_ref_t
scc_ast2ir_usual_arithmetic_conversion(scc_ast2ir_ctx_t *ctx,
scc_hir_type_ref_t t1_ref,
scc_hir_type_ref_t t2_ref);
/**
* @brief 插入类型转换指令
@@ -64,7 +69,25 @@ scc_hir_type_ref_t scc_ast2ir_usual_arithmetic_conversion(
* @return 转换后的值引用
*/
scc_hir_value_ref_t scc_ast2ir_emit_conversion(scc_ast2ir_ctx_t *ctx,
scc_hir_value_ref_t value,
scc_hir_type_ref_t target_type);
scc_hir_value_ref_t value,
scc_hir_type_ref_t target_type);
// ====== 常量表达式求值 ======
/**
* @brief 对整数常量表达式求值C11 6.6
*
* 递归计算 AST 表达式,得到一个编译期可确定的整数值。
* 支持整数字面量、字符字面量、枚举常量、sizeof / _Alignof、
* 一元运算(+ - ~ !)、二元运算(算术/位/移位/关系/逻辑)、
* 条件表达式(?:)、强制类型转换。
*
* @param ctx ast2ir 上下文
* @param result 输出参数,存放求值结果
* @param expr 待求值的 AST 表达式
* @return 成功返回 true若表达式不是合法的常量表达式则返回 false
*/
cbool scc_ast2ir_eval_constant_int(scc_ast2ir_ctx_t *ctx, scc_ap_t *result,
const scc_ast_expr_t *expr);
#endif /* __SCC_AST2IR_H__ */

View File

@@ -0,0 +1,106 @@
/**
* @file scc_abi_type.h
* @brief 目标无关的类型布局描述接口
* @note 本模块仅定义接口。
*/
#ifndef __SCC_ABI_TYPE_H__
#define __SCC_ABI_TYPE_H__
#include <scc_core.h>
/**
* @brief ABI 基础类型类别枚举。
*
* 用于快速查询目标预定义的基本类型属性。复杂类型(指针、数组、结构体)
* 通过组合与递归计算。
*/
typedef enum scc_abi_base_type_kind {
SCC_ABI_TYPE_VOID,
SCC_ABI_TYPE_VA_LIST,
SCC_ABI_TYPE_CHAR,
SCC_ABI_TYPE_I_CHAR,
SCC_ABI_TYPE_U_CHAR,
SCC_ABI_TYPE_I_SHORT,
SCC_ABI_TYPE_U_SHORT,
SCC_ABI_TYPE_I_INT,
SCC_ABI_TYPE_U_INT,
SCC_ABI_TYPE_I_LONG,
SCC_ABI_TYPE_U_LONG,
SCC_ABI_TYPE_I_LONG_LONG,
SCC_ABI_TYPE_U_LONG_LONG,
SCC_ABI_TYPE_PTR,
SCC_ABI_TYPE_FLOAT,
SCC_ABI_TYPE_DOUBLE,
SCC_ABI_TYPE_USIZE,
SCC_ABI_TYPE_ISIZE,
/* 可扩展I128, F16, F128, VECTOR, ... */
} scc_abi_base_type_kind_t;
/**
* @brief 单个类型的布局信息。
*/
typedef struct scc_abi_type_layout {
int size; /**< 类型占用的字节数 */
int alignment; /**< 类型的对齐要求(字节边界) */
} scc_abi_type_layout_t;
typedef struct {
scc_abi_base_type_kind_t kind;
scc_abi_type_layout_t layout;
} scc_abi_base_type_impl_t;
#define SCC_ABI_BASE_TYPE_IMPL(type, bytes_size, alians) \
[type] = { \
.kind = type, \
.layout.size = bytes_size, \
.layout.alignment = alians, \
}
/**
* @brief 单个结构体字段的布局信息。
* 由目标布局算法填充,用于 IR 的 getelementptr 常量索引计算。
*/
typedef struct scc_abi_field_layout {
int offset; /**< 字段相对于结构体基址的字节偏移 */
int size; /**< 字段自身大小 */
int alignment; /**< 字段的对齐要求 */
} scc_abi_field_layout_t;
typedef SCC_VEC(scc_abi_field_layout_t) scc_abi_field_layout_vec_t;
/**
* @brief 获取基本类型的布局信息。
*
* 目标必须实现此函数,为每个 scc_abi_base_type_kind_t 返回正确的
* size/alignment。
*
* @param kind 基本类型类别
* @param layout 输出参数,存放布局信息
* @return 成功返回 0若 kind 不支持则返回 -1
*/
static inline void
scc_abi_get_base_type_layout(const scc_abi_base_type_impl_t *impls,
scc_abi_base_type_kind_t kind,
scc_abi_type_layout_t *layout) {
if (impls[kind].kind != kind)
Panic("invalid base type kind");
*layout = impls[kind].layout;
}
typedef struct scc_abi_type_calc scc_abi_type_calc_t;
void scc_abi_compute_ast_type_layout(const scc_abi_type_calc_t *ctx, void *type,
scc_abi_type_layout_t *layout);
typedef struct scc_abi_type_calc {
void *ctx;
const scc_abi_base_type_impl_t *impls;
/// @brief 可以是系统内置的结构,比如 struct int128_t
void (*compute_type_layout)(const scc_abi_type_calc_t *ctx, void *type,
scc_abi_type_layout_t *layout);
/// @brief
void (*compute_field_layout)(const scc_abi_type_calc_t *ctx, void *type,
scc_abi_field_layout_vec_t *field_layouts);
} scc_abi_type_calc_t;
#endif /* __SCC_ABI_TYPE_H__ */

View File

@@ -0,0 +1,4 @@
#ifndef __SCC_ABI_DUMMY_H__
#define __SCC_ABI_DUMMY_H__
#endif /* __SCC_ABI_DUMMY_H__ */

View File

@@ -0,0 +1,49 @@
#ifndef __SCC_ABI_WIN_X64_PC_H__
#define __SCC_ABI_WIN_X64_PC_H__
/**
* @brief Windows x64 ABI Type
* @details
* https://learn.microsoft.com/zh-cn/cpp/build/x64-software-conventions?view=msvc-180
*/
#include "../scc_type_abi.h"
static const scc_abi_base_type_impl_t scc_abi_base_type_impls[] = {
/**
* @brief
* https://learn.microsoft.com/zh-cn/cpp/build/x64-calling-convention?view=msvc-180#varargs
* 如果通过 vararg (例如省略号参数) 传递参数,则需遵守常规寄存器参数传递约定.
* 该约定规定了将第 5个及后面的参数溢出到堆栈中.
* 被调用方负责转储带有其地址的参数.
* (仅适用于浮点值)如果被调用方希望在整数寄存器中使用浮点值,则整数寄存器和浮点数寄存器都必须包含该值.
*/
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_VA_LIST, 4, 4),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_VOID, 0, 0),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_CHAR, 1, 1),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_I_CHAR, 1, 1),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_U_CHAR, 1, 1),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_I_SHORT, 2, 2),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_U_SHORT, 2, 2),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_I_INT, 4, 4),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_U_INT, 4, 4),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_I_LONG, 4, 4),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_U_LONG, 4, 4),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_I_LONG_LONG, 8, 8),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_U_LONG_LONG, 8, 8),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_PTR, 8, 8),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_FLOAT, 4, 4),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_DOUBLE, 8, 8),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_USIZE, 8, 8),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_ISIZE, 8, 8),
};
static const scc_abi_type_calc_t scc_ast_abi_impl = {
.impls = scc_abi_base_type_impls,
.ctx = nullptr,
.compute_type_layout = scc_abi_compute_ast_type_layout,
.compute_field_layout = nullptr,
};
#ifdef SCC_ABI_IMPLIMENT
#endif
#endif /* __SCC_ABI_WIN_X64_PC_H__ */