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

@@ -1,17 +0,0 @@
#ifndef __SCC_IR_BASE_H__
#define __SCC_IR_BASE_H__
#include "ir_def.h"
void scc_ir_type_init(scc_ir_type_t *in, scc_ir_type_tag_t tag);
void scc_ir_bblock_init(scc_ir_bblock_t *in, const char *label);
void scc_ir_func_init(scc_ir_func_t *in, const char *name);
// node name can be null ptr
void scc_ir_node_init(scc_ir_node_t *in, const char *name,
scc_ir_node_tag_t tag);
void scc_ir_cprog_init(scc_ir_cprog_t *in);
void scc_ir_cprog_drop(scc_ir_cprog_t *in);
#endif /* __SCC_IR_BASE_H__ */

View File

@@ -2,8 +2,7 @@
#define __SCC_IR_BUILDER_H__
#include "ir_ctx.h"
#include "ir_def.h"
#include <scc_core.h>
#include "scc_ir.h"
typedef struct scc_ir_builder scc_ir_builder_t;
@@ -17,18 +16,16 @@ typedef struct scc_ir_builder scc_ir_builder_t;
* - 当前构建位置(函数、基本块)
*/
struct scc_ir_builder {
scc_ir_cprog_ctx_t ctx; /**< 核心上下文 */
scc_ir_cprog_t cprog;
scc_hashtable_t func_decl_set;
// 当前构建位置
scc_ir_func_ref_t current_func; /**< 当前正在构建的函数 */
scc_ir_bblock_ref_t current_bblock; /**< 当前基本块 */
scc_ir_cprog_t *cprog;
scc_ir_ctx_t ctx; ///< 核心上下文
scc_ir_func_ref_t current_func; ///< 当前正在构建的函数
scc_ir_bblock_ref_t current_bblock; ///< 当前基本块
};
/**
* @brief 初始化 IR 构建器
*/
void scc_ir_builder_init(scc_ir_builder_t *builder);
void scc_ir_builder_init(scc_ir_builder_t *builder, scc_ir_cprog_t *cprog);
/**
* @brief 销毁 IR 构建器及其所有资源
@@ -39,6 +36,46 @@ scc_ir_func_ref_t scc_ir_builder_func(scc_ir_builder_t *builder,
scc_ir_type_ref_t type_ref,
const char *name);
scc_ir_type_ref_t scc_ir_builder_type(scc_ir_builder_t *builder,
const scc_ir_type_t *type_desc);
// TODO
static inline scc_ir_node_ref_t
scc_ir_builder_const_int(scc_ir_builder_t *builder, scc_ir_type_ref_t type,
scc_ir_const_int_t value) {
scc_ir_node_t node;
scc_ir_node_init(&node, null, SCC_IR_NODE_CONST_INT);
node.data.const_int = value;
node.type = type;
return scc_ir_module_add_node(&builder->cprog->module, &node);
}
#define SCC_IR_BUILDER_TYPE_FUNC(scc_type) \
[[maybe_unused]] static inline scc_ir_type_ref_t \
scc_ir_builder_type_##scc_type(scc_ir_builder_t *builder) { \
scc_ir_type_t type_desc; \
scc_ir_type_init(&type_desc, SCC_IR_TYPE_##scc_type); \
return scc_ir_ctx_get_type(&builder->ctx, &type_desc); \
}
SCC_IR_BUILDER_TYPE_FUNC(unknown)
SCC_IR_BUILDER_TYPE_FUNC(void)
SCC_IR_BUILDER_TYPE_FUNC(i8)
SCC_IR_BUILDER_TYPE_FUNC(i16)
SCC_IR_BUILDER_TYPE_FUNC(i32)
SCC_IR_BUILDER_TYPE_FUNC(i64)
SCC_IR_BUILDER_TYPE_FUNC(i128)
SCC_IR_BUILDER_TYPE_FUNC(u8)
SCC_IR_BUILDER_TYPE_FUNC(u16)
SCC_IR_BUILDER_TYPE_FUNC(u32)
SCC_IR_BUILDER_TYPE_FUNC(u64)
SCC_IR_BUILDER_TYPE_FUNC(u128)
// SCC_IR_BUILDER_TYPE_FUNC(f8)
SCC_IR_BUILDER_TYPE_FUNC(f16)
SCC_IR_BUILDER_TYPE_FUNC(f32)
SCC_IR_BUILDER_TYPE_FUNC(f64)
SCC_IR_BUILDER_TYPE_FUNC(f128)
/**
* @brief 开始构建函数
* @param func_ref 函数引用

View File

@@ -1,9 +0,0 @@
#ifndef __SCC_IR_BUILTIN_H__
#define __SCC_IR_BUILTIN_H__
#include "ir_def.h"
extern scc_ir_type_t scc_ir_builtin_i32;
extern scc_ir_node_t scc_ir_builtin_zero;
#endif /* __SCC_IR_BUILTIN_H__ */

View File

@@ -1,194 +1,32 @@
#ifndef __SCC_IR_CTX_H__
#define __SCC_IR_CTX_H__
#include "ir_base.h"
#include "ir_def.h"
#include "ir_prog.h"
#include <scc_hashtable.h>
#define SCC_IR_REF_NULL 0
typedef struct {
unsigned int node_uid;
unsigned int type_uid;
unsigned int bblock_uid;
unsigned int func_uid;
scc_ir_module_t *module; // 关联的模块(用于实际存储)
scc_hashtable_t type_uniquing; // 类型哈希表hash -> type_ref
scc_hashtable_t const_pool; // 常量哈希表hash -> node_ref
scc_hashtable_t func_decl_set; // 函数声明集合name -> func_ref
} scc_ir_ctx_t;
SCC_VEC(scc_ir_node_t) nodes;
SCC_VEC(scc_ir_type_t) types;
SCC_VEC(scc_ir_bblock_t) bblocks;
SCC_VEC(scc_ir_func_t) funcs;
void scc_ir_ctx_init(scc_ir_ctx_t *ctx, scc_ir_module_t *module);
void scc_ir_ctx_drop(scc_ir_ctx_t *ctx);
// UID -> 索引 映射
scc_hashtable_t uid2nodes;
scc_hashtable_t uid2types;
scc_hashtable_t uid2bblocks;
scc_hashtable_t uid2funcs;
// 获取唯一类型,若不存在则创建并返回新引用
scc_ir_type_ref_t scc_ir_ctx_get_type(scc_ir_ctx_t *ctx,
const scc_ir_type_t *type_desc);
// 类型去重表(类型键 -> 类型引用
scc_hashtable_t type_uniquing;
// 获取唯一常量(例如整数常量
// scc_ir_node_ref_t scc_ir_ctx_get_const_int(scc_ir_ctx_t *ctx,
// scc_ir_type_ref_t type, i64
// value);
// 常量池(常量键 -> 节点引用
scc_hashtable_t const_pool;
} scc_ir_cprog_ctx_t;
/**
* @brief 初始化IR上下文
* @param ctx 上下文指针
*/
void scc_ir_ctx_init(scc_ir_cprog_ctx_t *ctx);
/**
* @brief 销毁IR上下文及其所有资源
* @param ctx 上下文指针
*/
void scc_ir_ctx_drop(scc_ir_cprog_ctx_t *ctx);
/**
* @brief 重置IR上下文清空所有数据但保留内存
* @param ctx 上下文指针
*/
void scc_ir_ctx_reset(scc_ir_cprog_ctx_t *ctx);
/**
* @brief 创建新的类型
* @param ctx 上下文指针
* @param type 类型数据(会被拷贝)
* @return 类型引用0表示失败
*/
scc_ir_type_ref_t scc_ir_ctx_new_type(scc_ir_cprog_ctx_t *ctx,
const scc_ir_type_t *type);
/**
* @brief 创建新的节点
* @param ctx 上下文指针
* @param node 节点数据(会被拷贝)
* @return 节点引用0表示失败
*/
scc_ir_node_ref_t scc_ir_ctx_new_node(scc_ir_cprog_ctx_t *ctx,
const scc_ir_node_t *node);
/**
* @brief 创建新的基本块
* @param ctx 上下文指针
* @param bblock 基本块数据(会被拷贝)
* @return 基本块引用0表示失败
*/
scc_ir_bblock_ref_t scc_ir_ctx_new_bblock(scc_ir_cprog_ctx_t *ctx,
const scc_ir_bblock_t *bblock);
/**
* @brief 创建新的函数
* @param ctx 上下文指针
* @param func 函数数据(会被拷贝)
* @return 函数引用0表示失败
*/
scc_ir_func_ref_t scc_ir_ctx_new_func(scc_ir_cprog_ctx_t *ctx,
const scc_ir_func_t *func);
/**
* @brief 根据引用获取类型
* @param ctx 上下文指针
* @param ref 类型引用
* @return 类型指针NULL表示无效引用
*/
scc_ir_type_t *scc_ir_ctx_get_type(scc_ir_cprog_ctx_t *ctx,
scc_ir_type_ref_t ref);
/**
* @brief 根据引用获取节点
* @param ctx 上下文指针
* @param ref 节点引用
* @return 节点指针NULL表示无效引用
*/
scc_ir_node_t *scc_ir_ctx_get_node(scc_ir_cprog_ctx_t *ctx,
scc_ir_node_ref_t ref);
/**
* @brief 根据引用获取基本块
* @param ctx 上下文指针
* @param ref 基本块引用
* @return 基本块指针NULL表示无效引用
*/
scc_ir_bblock_t *scc_ir_ctx_get_bblock(scc_ir_cprog_ctx_t *ctx,
scc_ir_bblock_ref_t ref);
/**
* @brief 根据引用获取函数
* @param ctx 上下文指针
* @param ref 函数引用
* @return 函数指针NULL表示无效引用
*/
scc_ir_func_t *scc_ir_ctx_get_func(scc_ir_cprog_ctx_t *ctx,
scc_ir_func_ref_t ref);
// /**
// * @brief 遍历所有类型
// * @param ctx 上下文指针
// * @param callback 回调函数
// * @param userdata 用户数据
// */
// void scc_ir_ctx_foreach_type(scc_ir_cprog_ctx_t *ctx,
// void (*callback)(scc_ir_type_ref_t ref,
// scc_ir_type_t *type,
// void *userdata),
// void *userdata);
// /**
// * @brief 遍历所有节点
// */
// void scc_ir_ctx_foreach_node(scc_ir_cprog_ctx_t *ctx,
// void (*callback)(scc_ir_node_ref_t ref,
// scc_ir_node_t *node,
// void *userdata),
// void *userdata);
// /**
// * @brief 遍历所有基本块
// */
// void scc_ir_ctx_foreach_bblock(scc_ir_cprog_ctx_t *ctx,
// void (*callback)(scc_ir_bblock_ref_t ref,
// scc_ir_bblock_t *bblock,
// void *userdata),
// void *userdata);
// /**
// * @brief 遍历所有函数
// */
// void scc_ir_ctx_foreach_func(scc_ir_cprog_ctx_t *ctx,
// void (*callback)(scc_ir_func_ref_t ref,
// scc_ir_func_t *func,
// void *userdata),
// void *userdata);
/**
* @brief 获取内置i32类型
* @param ctx 上下文指针
* @return i32类型引用
*/
scc_ir_type_ref_t scc_ir_ctx_get_builtin_i32(scc_ir_cprog_ctx_t *ctx);
/**
* @brief 获取内置零常量
* @param ctx 上下文指针
* @return 零常量节点引用
*/
scc_ir_node_ref_t scc_ir_ctx_get_builtin_zero(scc_ir_cprog_ctx_t *ctx);
/**
* @brief 创建或获取i32常量
* @param ctx 上下文指针
* @param value 常量值
* @return 常量节点引用
*/
scc_ir_node_ref_t scc_ir_ctx_get_i32_const(scc_ir_cprog_ctx_t *ctx, i32 value);
/**
* @brief 创建或获取null常量
* @param ctx 上下文指针
* @param ptr_type 指针类型引用
* @return null常量节点引用
*/
scc_ir_node_ref_t scc_ir_ctx_get_null_const(scc_ir_cprog_ctx_t *ctx,
scc_ir_type_ref_t ptr_type);
// 注册函数声明,若已存在则返回已有引用
scc_ir_func_ref_t scc_ir_ctx_declare_func(scc_ir_ctx_t *ctx,
scc_ir_type_ref_t type,
const char *name);
#endif /* __SCC_IR_CTX_H__ */

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__ */

View File

@@ -1,20 +1,17 @@
#ifndef __SCC_IR_DUMP_H__
#define __SCC_IR_DUMP_H__
#include "ir_ctx.h"
#include "ir_def.h"
#include "ir_prog.h"
#include <tree_dump.h>
typedef struct {
scc_ir_cprog_ctx_t *ir_ctx;
scc_ir_cprog_t *cprog;
scc_tree_dump_ctx_t *dump_ctx;
} scc_ir_dump_ctx_t;
void scc_ir_dump_ctx_init(scc_ir_dump_ctx_t *ctx,
scc_tree_dump_ctx_t *tree_dump, scc_ir_cprog_t *cprog,
scc_ir_cprog_ctx_t *ir_ctx);
scc_tree_dump_ctx_t *tree_dump,
scc_ir_cprog_t *cprog);
void scc_ir_dump_node(scc_ir_dump_ctx_t *ctx, scc_ir_node_ref_t node_ref);
void scc_ir_dump_type(scc_ir_dump_ctx_t *ctx, scc_ir_type_ref_t type_ref);
void scc_ir_dump_bblock(scc_ir_dump_ctx_t *ctx, scc_ir_bblock_ref_t bblock_ref);

52
libs/ir/include/ir_prog.h Normal file
View File

@@ -0,0 +1,52 @@
#ifndef __SCC_IR_PROG_H__
#define __SCC_IR_PROG_H__
#include "ir_def.h"
#include <scc_utils.h>
typedef struct {
unsigned int node_uid;
unsigned int type_uid;
unsigned int bblock_uid;
unsigned int func_uid;
SCC_VEC(scc_ir_node_t) nodes;
SCC_VEC(scc_ir_type_t) types;
SCC_VEC(scc_ir_bblock_t) bblocks;
SCC_VEC(scc_ir_func_t) funcs;
// UID -> ref index
scc_hashtable_t uid2nodes;
scc_hashtable_t uid2types;
scc_hashtable_t uid2bblocks;
scc_hashtable_t uid2funcs;
} scc_ir_module_t;
void scc_ir_module_init(scc_ir_module_t *ctx);
void scc_ir_module_drop(scc_ir_module_t *ctx);
scc_ir_type_ref_t scc_ir_module_add_type(scc_ir_module_t *ctx,
const scc_ir_type_t *type);
scc_ir_node_ref_t scc_ir_module_add_node(scc_ir_module_t *ctx,
const scc_ir_node_t *node);
scc_ir_bblock_ref_t scc_ir_module_add_bblock(scc_ir_module_t *ctx,
const scc_ir_bblock_t *bblock);
scc_ir_func_ref_t scc_ir_module_add_func(scc_ir_module_t *ctx,
const scc_ir_func_t *func);
scc_ir_type_t *scc_ir_module_get_type(scc_ir_module_t *ctx,
scc_ir_type_ref_t ref);
scc_ir_node_t *scc_ir_module_get_node(scc_ir_module_t *ctx,
scc_ir_node_ref_t ref);
scc_ir_bblock_t *scc_ir_module_get_bblock(scc_ir_module_t *ctx,
scc_ir_bblock_ref_t ref);
scc_ir_func_t *scc_ir_module_get_func(scc_ir_module_t *ctx,
scc_ir_func_ref_t ref);
typedef struct scc_ir_cprog {
scc_ir_module_t module;
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;
void scc_ir_cprog_init(scc_ir_cprog_t *in);
void scc_ir_cprog_drop(scc_ir_cprog_t *in);
#endif /* __SCC_IR_PROG_H__ */

View File

@@ -1,9 +1,15 @@
#ifndef __SCC_IR_H__
#define __SCC_IR_H__
#include "ir_builder.h"
#include "ir_ctx.h"
#include "ir_def.h"
#include <scc_utils.h>
#include "ir_prog.h"
void scc_ir_type_init(scc_ir_type_t *in, scc_ir_type_tag_t tag);
void scc_ir_bblock_init(scc_ir_bblock_t *in, const char *label);
void scc_ir_func_init(scc_ir_func_t *in, const char *name);
// node name can be null ptr
void scc_ir_node_init(scc_ir_node_t *in, const char *name,
scc_ir_node_tag_t tag);
#endif /* __SCC_IR_H__ */