refactor(ast2ir): 更新IR构建器接口并重构类型映射

- 将IR构建器初始化函数修改为接受cprog参数
- 添加scc_ast2ir_ctx_drop函数用于资源清理
- 更新类型标识符命名规范,从大写改为小写形式
- 替换scc_ir_ctx_get_*函数调用为scc_ir_module_get_*函数
- 移除对ir_builtin.h的依赖,改用ir_builder.h中的构建器函数
- 为整数常量创建添加专门的构建器辅助函数

fix(ir): 重构IR上下文和模块管理结构

- 将原有的scc_ir_cprog_ctx_t拆分为scc_ir_module_t和scc_ir_ctx_t
- 添加scc_ir_module_t结构用于统一管理IR对象存储
- 更新IR类型枚举名称格式,从SCC_IR_TYPE_XXX改为SCC_IR_TYPE_xxx
- 添加整数、无符号整数和浮点数常量联合体定义
- 移除ir_base.h和ir_builtin.h头文件,整合到scc_ir.h中

feat(ir_builder): 添加类型构建器函数和常量创建功能

- 为各种基础类型添加scc_ir_builder_type_*内联函数
- 实现scc_ir_builder_const_int函数用于创建整数常量
- 修改构建器初始化函数签名以接受cprog参数
- 更新构建器内部结构,使用指向cprog的指针而非嵌入式结构
This commit is contained in:
zzy
2026-03-25 11:59:27 +08:00
parent d167a8ba96
commit 8c7af571c2
24 changed files with 779 additions and 792 deletions

View File

@@ -3,6 +3,8 @@
#include <scc_core.h>
#define SCC_IR_REF_NULL 0
typedef unsigned int ir_handle_t;
typedef const char *scc_ir_label_t;
@@ -23,37 +25,34 @@ typedef ir_handle_t scc_ir_func_ref_t;
typedef SCC_VEC(scc_ir_func_ref_t) scc_ir_func_ref_vec_t;
typedef enum scc_ir_type_tag {
SCC_IR_TYPE_UNKNOWN,
SCC_IR_TYPE_VOID,
SCC_IR_TYPE_I8,
SCC_IR_TYPE_I16,
SCC_IR_TYPE_I32,
SCC_IR_TYPE_I64,
SCC_IR_TYPE_I128,
SCC_IR_TYPE_U8,
SCC_IR_TYPE_U16,
SCC_IR_TYPE_U32,
SCC_IR_TYPE_U64,
SCC_IR_TYPE_U128,
SCC_IR_TYPE_F16,
SCC_IR_TYPE_F32,
SCC_IR_TYPE_F64,
SCC_IR_TYPE_F128,
SCC_IR_TYPE_unknown,
SCC_IR_TYPE_void,
SCC_IR_TYPE_i8,
SCC_IR_TYPE_i16,
SCC_IR_TYPE_i32,
SCC_IR_TYPE_i64,
SCC_IR_TYPE_i128,
SCC_IR_TYPE_u8,
SCC_IR_TYPE_u16,
SCC_IR_TYPE_u32,
SCC_IR_TYPE_u64,
SCC_IR_TYPE_u128,
SCC_IR_TYPE_f16,
SCC_IR_TYPE_f32,
SCC_IR_TYPE_f64,
SCC_IR_TYPE_f128,
SCC_IR_TYPE_PTR,
SCC_IR_TYPE_ARRAY,
SCC_IR_TYPE_FUNC,
SCC_IR_TYPE_STRUCT,
SCC_IR_TYPE_VECTOR,
SCC_IR_TYPE_UNION,
SCC_IR_TYPE_VECTOR, // TODO SIMD
} scc_ir_type_tag_t;
struct scc_ir_type {
scc_ir_type_tag_t tag;
int size; // 字节大小
int align; // 对齐要求
const char *name; // For Debug
union {
struct {
scc_ir_type_ref_t base;
@@ -62,6 +61,9 @@ struct scc_ir_type {
struct {
scc_ir_type_ref_t base;
} pointer;
struct {
scc_ir_type_ref_vec_t elements;
} aggregate;
struct {
scc_ir_type_ref_vec_t params;
scc_ir_type_ref_t ret_type;
@@ -144,28 +146,41 @@ typedef enum {
SCC_IR_OP_SAR,
} scc_ir_op_type_t;
typedef union {
i8 int8;
i16 int16;
i32 int32;
i64 int64;
// TODO int128
i8 int_any[16];
} scc_ir_const_int_t;
typedef union {
u8 uint8;
u16 uint16;
u32 uint32;
u64 uint64;
// TODO uint128;
u8 uint_any[16];
} scc_ir_const_uint_t;
typedef union {
// f16 float16;
f32 float32;
f64 float64;
// TODO float128;
u8 float_any[16];
} scc_ir_const_float_t;
struct scc_ir_node {
scc_ir_type_ref_t type;
scc_ir_label_t name;
scc_ir_node_ref_vec_t used_by;
scc_ir_node_tag_t tag;
union {
union {
i8 int8;
i16 int16;
i32 int32;
i64 int64;
// TODO int128
i8 int_any[16];
} const_int;
union {
u8 uint8;
u16 uint16;
u32 uint32;
u64 uint64;
// TODO uint128;
u8 uint_any[16];
} const_uint;
scc_ir_const_int_t const_int;
scc_ir_const_uint_t const_uint;
scc_ir_const_float_t const_float;
// aggregate;
struct {
usize idx;
@@ -216,10 +231,4 @@ struct scc_ir_node {
} data;
};
typedef struct scc_ir_cprog {
scc_ir_node_ref_vec_t global_vals; /* 全局变量 */
scc_ir_func_ref_vec_t func_defs; /* 所有函数定义 */
scc_ir_func_ref_vec_t func_decls; /* 所有函数包括定义的声明 */
} scc_ir_cprog_t;
#endif /* __SCC_IR_DEF_H__ */