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

View File

@@ -1,42 +1,44 @@
#include <ir_builder.h>
#include <ir_builtin.h>
#include <ir_prog.h>
void scc_ir_builder_init(scc_ir_builder_t *builder) {
#define GET_MODULE(builder) (&(builder->cprog->module))
void scc_ir_builder_init(scc_ir_builder_t *builder, scc_ir_cprog_t *cprog) {
builder->current_bblock = SCC_IR_REF_NULL;
builder->current_func = SCC_IR_REF_NULL;
builder->cprog = cprog;
scc_ir_cprog_init(&builder->cprog);
scc_ir_ctx_init(&builder->ctx);
scc_ir_ctx_init(&builder->ctx, GET_MODULE(builder));
}
void scc_ir_builder_drop(scc_ir_builder_t *builder) {
scc_ir_cprog_drop(&builder->cprog);
scc_ir_ctx_drop(&builder->ctx);
}
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_func_t func = {
.name = name,
.type = type_ref,
};
scc_vec_init(func.params);
scc_vec_init(func.bblocks);
scc_ir_func_ref_t func_ref = scc_ir_ctx_new_func(&builder->ctx, &func);
scc_ir_func_ref_t func_ref =
scc_ir_ctx_declare_func(&builder->ctx, type_ref, name);
scc_vec_push(builder->cprog->func_decls, func_ref);
return func_ref;
}
scc_ir_type_ref_t scc_ir_builder_type(scc_ir_builder_t *builder,
const scc_ir_type_t *type_desc) {
return scc_ir_ctx_get_type(&builder->ctx, type_desc);
}
void scc_ir_builder_begin_func(scc_ir_builder_t *builder,
scc_ir_func_ref_t func_ref,
const char **param_names) {
// 创建函数并设置为当前函数
builder->current_func = func_ref;
scc_ir_func_t *func_ptr = scc_ir_ctx_get_func(&builder->ctx, func_ref);
scc_ir_func_t *func_ptr =
scc_ir_module_get_func(GET_MODULE(builder), func_ref);
scc_ir_type_t *func_type =
scc_ir_ctx_get_type(&builder->ctx, func_ptr->type);
scc_ir_module_get_type(GET_MODULE(builder), func_ptr->type);
if (func_type == null || func_type->tag != SCC_IR_TYPE_FUNC) {
LOG_ERROR("Invalid function type");
@@ -66,7 +68,7 @@ void scc_ir_builder_begin_func(scc_ir_builder_t *builder,
scc_vec_init(param_node.used_by);
scc_ir_node_ref_t param_ref =
scc_ir_ctx_new_node(&builder->ctx, &param_node);
scc_ir_module_add_node(GET_MODULE(builder), &param_node);
scc_vec_push(func_ptr->params, param_ref);
}
@@ -75,15 +77,16 @@ void scc_ir_builder_begin_func(scc_ir_builder_t *builder,
void scc_ir_builder_end_func(scc_ir_builder_t *builder) {
scc_ir_func_t *func_ptr =
scc_ir_ctx_get_func(&builder->ctx, builder->current_func);
scc_ir_module_get_func(GET_MODULE(builder), builder->current_func);
if (func_ptr == null) {
LOG_FATAL("Invalid function reference");
return;
}
if (scc_vec_size(func_ptr->bblocks) == 0) {
scc_vec_push(builder->cprog.func_decls, builder->current_func);
// FIXME
scc_vec_push(builder->cprog->func_decls, builder->current_func);
} else {
scc_vec_push(builder->cprog.func_defs, builder->current_func);
scc_vec_push(builder->cprog->func_defs, builder->current_func);
}
builder->current_func = 0;
}
@@ -100,10 +103,10 @@ scc_ir_bblock_ref_t scc_ir_builder_bblock(scc_ir_builder_t *builder,
}
scc_vec_init(bblock.instrs);
scc_ir_bblock_ref_t bblock_ref =
scc_ir_ctx_new_bblock(&builder->ctx, &bblock);
scc_ir_module_add_bblock(GET_MODULE(builder), &bblock);
scc_ir_func_t *current_func =
scc_ir_ctx_get_func(&builder->ctx, builder->current_func);
scc_ir_module_get_func(GET_MODULE(builder), builder->current_func);
if (current_func) {
scc_vec_push(current_func->bblocks, bblock_ref);
}
@@ -129,7 +132,7 @@ void scc_ir_builder_set_current_bblock(scc_ir_builder_t *builder,
static void scc_ir_builder_add_instr(scc_ir_builder_t *builder,
scc_ir_node_ref_t node) {
scc_ir_bblock_t *current_bblock =
scc_ir_ctx_get_bblock(&builder->ctx, builder->current_bblock);
scc_ir_module_get_bblock(GET_MODULE(builder), builder->current_bblock);
if (current_bblock) {
scc_vec_push(current_bblock->instrs, node);
} else {
@@ -142,13 +145,13 @@ scc_ir_node_ref_t scc_ir_builder_alloca(scc_ir_builder_t *builder,
const char *name) {
scc_ir_node_t alloc_node = {0};
alloc_node.tag = SCC_IR_NODE_ALLOC;
alloc_node.type = scc_ir_ctx_new_type(
&builder->ctx,
alloc_node.type = scc_ir_module_add_type(
GET_MODULE(builder),
&(scc_ir_type_t){.tag = SCC_IR_TYPE_PTR, .data.pointer.base = type});
alloc_node.name = name;
scc_ir_node_ref_t node_ref =
scc_ir_ctx_new_node(&builder->ctx, &alloc_node);
scc_ir_module_add_node(GET_MODULE(builder), &alloc_node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);
@@ -165,7 +168,8 @@ scc_ir_node_ref_t scc_ir_builder_func_arg_ref(scc_ir_builder_t *builder,
node.name = name;
node.data.arg_ref.idx = arg_idx;
scc_ir_node_ref_t node_ref = scc_ir_ctx_new_node(&builder->ctx, &node);
scc_ir_node_ref_t node_ref =
scc_ir_module_add_node(GET_MODULE(builder), &node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);
return node_ref;
@@ -178,16 +182,18 @@ scc_ir_node_ref_t scc_ir_builder_load(scc_ir_builder_t *builder,
load_node.data.load.target = target;
// 设置类型为指针指向的类型
scc_ir_node_t *ptr_node = scc_ir_ctx_get_node(&builder->ctx, target);
scc_ir_node_t *ptr_node =
scc_ir_module_get_node(GET_MODULE(builder), target);
if (ptr_node) {
scc_ir_type_t *ptr_type =
scc_ir_ctx_get_type(&builder->ctx, ptr_node->type);
scc_ir_module_get_type(GET_MODULE(builder), ptr_node->type);
if (ptr_type && ptr_type->tag == SCC_IR_TYPE_PTR) {
load_node.type = ptr_type->data.pointer.base;
}
}
scc_ir_node_ref_t node_ref = scc_ir_ctx_new_node(&builder->ctx, &load_node);
scc_ir_node_ref_t node_ref =
scc_ir_module_add_node(GET_MODULE(builder), &load_node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);
@@ -204,7 +210,7 @@ scc_ir_node_ref_t scc_ir_builder_store(scc_ir_builder_t *builder,
store_node.data.store.value = value;
scc_ir_node_ref_t node_ref =
scc_ir_ctx_new_node(&builder->ctx, &store_node);
scc_ir_module_add_node(GET_MODULE(builder), &store_node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);
@@ -221,13 +227,14 @@ scc_ir_node_ref_t scc_ir_builder_get_ptr(scc_ir_builder_t *builder,
get_ptr_node.data.get_ptr.index = index;
// 类型应与源地址相同(都是指针)
scc_ir_node_t *src_node = scc_ir_ctx_get_node(&builder->ctx, target);
scc_ir_node_t *src_node =
scc_ir_module_get_node(GET_MODULE(builder), target);
if (src_node) {
get_ptr_node.type = src_node->type;
}
scc_ir_node_ref_t node_ref =
scc_ir_ctx_new_node(&builder->ctx, &get_ptr_node);
scc_ir_module_add_node(GET_MODULE(builder), &get_ptr_node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);
@@ -246,13 +253,13 @@ scc_ir_node_ref_t scc_ir_builder_binop(scc_ir_builder_t *builder,
binop_node.data.op.rhs = rhs;
// 类型通常与操作数相同(对于算术运算)
scc_ir_node_t *lhs_node = scc_ir_ctx_get_node(&builder->ctx, lhs);
scc_ir_node_t *lhs_node = scc_ir_module_get_node(GET_MODULE(builder), lhs);
if (lhs_node) {
binop_node.type = lhs_node->type;
}
scc_ir_node_ref_t node_ref =
scc_ir_ctx_new_node(&builder->ctx, &binop_node);
scc_ir_module_add_node(GET_MODULE(builder), &binop_node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);
@@ -270,9 +277,11 @@ scc_ir_node_ref_t scc_ir_builder_cmp(scc_ir_builder_t *builder,
cmp_node.data.op.rhs = rhs;
// 比较操作的结果通常是布尔值
cmp_node.type = scc_ir_ctx_get_builtin_i32(&builder->ctx);
cmp_node.type =
0; // FIXME scc_ir_module_get_builtin_i32(GET_MODULE(builder));
scc_ir_node_ref_t node_ref = scc_ir_ctx_new_node(&builder->ctx, &cmp_node);
scc_ir_node_ref_t node_ref =
scc_ir_module_add_node(GET_MODULE(builder), &cmp_node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);
@@ -286,7 +295,8 @@ scc_ir_node_ref_t scc_ir_builder_jump(scc_ir_builder_t *builder,
jump_node.tag = SCC_IR_NODE_JUMP;
jump_node.data.jump.target_bblock = target;
scc_ir_node_ref_t node_ref = scc_ir_ctx_new_node(&builder->ctx, &jump_node);
scc_ir_node_ref_t node_ref =
scc_ir_module_add_node(GET_MODULE(builder), &jump_node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);
@@ -305,7 +315,7 @@ scc_ir_node_ref_t scc_ir_builder_branch(scc_ir_builder_t *builder,
branch_node.data.branch.false_bblock = false_target;
scc_ir_node_ref_t node_ref =
scc_ir_ctx_new_node(&builder->ctx, &branch_node);
scc_ir_module_add_node(GET_MODULE(builder), &branch_node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);
@@ -327,16 +337,18 @@ scc_ir_node_ref_t scc_ir_builder_call(scc_ir_builder_t *builder,
}
// 设置返回类型为被调用函数的返回类型
scc_ir_func_t *callee_func = scc_ir_ctx_get_func(&builder->ctx, callee);
scc_ir_func_t *callee_func =
scc_ir_module_get_func(GET_MODULE(builder), callee);
if (callee_func) {
scc_ir_type_t *func_type =
scc_ir_ctx_get_type(&builder->ctx, callee_func->type);
scc_ir_module_get_type(GET_MODULE(builder), callee_func->type);
if (func_type && func_type->tag == SCC_IR_TYPE_FUNC) {
call_node.type = func_type->data.function.ret_type;
}
}
scc_ir_node_ref_t node_ref = scc_ir_ctx_new_node(&builder->ctx, &call_node);
scc_ir_node_ref_t node_ref =
scc_ir_module_add_node(GET_MODULE(builder), &call_node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);
@@ -350,7 +362,8 @@ scc_ir_node_ref_t scc_ir_builder_ret(scc_ir_builder_t *builder,
ret_node.tag = SCC_IR_NODE_RET;
ret_node.data.ret.ret_val = value;
scc_ir_node_ref_t node_ref = scc_ir_ctx_new_node(&builder->ctx, &ret_node);
scc_ir_node_ref_t node_ref =
scc_ir_module_add_node(GET_MODULE(builder), &ret_node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);
@@ -363,7 +376,8 @@ scc_ir_node_ref_t scc_ir_builder_ret_void(scc_ir_builder_t *builder) {
ret_node.tag = SCC_IR_NODE_RET;
ret_node.data.ret.ret_val = 0; // 无返回值
scc_ir_node_ref_t node_ref = scc_ir_ctx_new_node(&builder->ctx, &ret_node);
scc_ir_node_ref_t node_ref =
scc_ir_module_add_node(GET_MODULE(builder), &ret_node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);

View File

@@ -1,10 +0,0 @@
#include <ir_builtin.h>
scc_ir_type_t scc_ir_builtin_i32 = {
.tag = SCC_IR_TYPE_I32,
};
scc_ir_node_t scc_ir_builtin_zero = {
.tag = SCC_IR_NODE_CONST_INT,
.data.const_int.int_any = {0},
};

View File

@@ -1,5 +1,4 @@
#include "ir_ctx.h"
#include "ir_builtin.h"
#include <ir_ctx.h>
/**
* @brief 哈希混合函数类似Rust的hash combine
@@ -12,30 +11,27 @@ static inline u32 scc_hash_mix(u32 seed, u32 value) {
return (seed ^ value) * 16777619u;
}
static u32 hash_key(const void *key) { return (u32)(usize)key; }
static int cmp_key(const void *key1, const void *key2) {
return (u32)(usize)key1 != (u32)(usize)key2;
}
static u32 hash_type(const scc_ir_type_t *key) {
static u32 hash_type(const void *_key) {
const scc_ir_type_t *key = _key;
// 初始哈希tag
u32 hash = (u32)key->tag;
switch (key->tag) {
case SCC_IR_TYPE_VOID:
case SCC_IR_TYPE_U8:
case SCC_IR_TYPE_U16:
case SCC_IR_TYPE_U32:
case SCC_IR_TYPE_U64:
case SCC_IR_TYPE_U128:
case SCC_IR_TYPE_I8:
case SCC_IR_TYPE_I16:
case SCC_IR_TYPE_I32:
case SCC_IR_TYPE_I64:
case SCC_IR_TYPE_I128:
case SCC_IR_TYPE_F16:
case SCC_IR_TYPE_F32:
case SCC_IR_TYPE_F64:
case SCC_IR_TYPE_F128:
case SCC_IR_TYPE_void:
case SCC_IR_TYPE_u8:
case SCC_IR_TYPE_u16:
case SCC_IR_TYPE_u32:
case SCC_IR_TYPE_u64:
case SCC_IR_TYPE_u128:
case SCC_IR_TYPE_i8:
case SCC_IR_TYPE_i16:
case SCC_IR_TYPE_i32:
case SCC_IR_TYPE_i64:
case SCC_IR_TYPE_i128:
case SCC_IR_TYPE_f16:
case SCC_IR_TYPE_f32:
case SCC_IR_TYPE_f64:
case SCC_IR_TYPE_f128:
// 基本类型只有tag
break;
@@ -66,28 +62,29 @@ static u32 hash_type(const scc_ir_type_t *key) {
return hash;
}
static int cmp_type(const scc_ir_type_t *key1, const scc_ir_type_t *key2) {
static int cmp_type(const void *_key1, const void *_key2) {
const scc_ir_type_t *key1 = _key1, *key2 = _key2;
// tag不同
if (key1->tag != key2->tag) {
return 1;
}
switch (key1->tag) {
case SCC_IR_TYPE_VOID:
case SCC_IR_TYPE_U8:
case SCC_IR_TYPE_U16:
case SCC_IR_TYPE_U32:
case SCC_IR_TYPE_U64:
case SCC_IR_TYPE_U128:
case SCC_IR_TYPE_I8:
case SCC_IR_TYPE_I16:
case SCC_IR_TYPE_I32:
case SCC_IR_TYPE_I64:
case SCC_IR_TYPE_I128:
case SCC_IR_TYPE_F16:
case SCC_IR_TYPE_F32:
case SCC_IR_TYPE_F64:
case SCC_IR_TYPE_F128:
case SCC_IR_TYPE_void:
case SCC_IR_TYPE_u8:
case SCC_IR_TYPE_u16:
case SCC_IR_TYPE_u32:
case SCC_IR_TYPE_u64:
case SCC_IR_TYPE_u128:
case SCC_IR_TYPE_i8:
case SCC_IR_TYPE_i16:
case SCC_IR_TYPE_i32:
case SCC_IR_TYPE_i64:
case SCC_IR_TYPE_i128:
case SCC_IR_TYPE_f16:
case SCC_IR_TYPE_f32:
case SCC_IR_TYPE_f64:
case SCC_IR_TYPE_f128:
return 0; // 基本类型tag相同即可
case SCC_IR_TYPE_PTR:
return key1->data.pointer.base != key2->data.pointer.base;
@@ -121,213 +118,61 @@ static int cmp_type(const scc_ir_type_t *key1, const scc_ir_type_t *key2) {
return 1;
}
void scc_ir_ctx_init(scc_ir_cprog_ctx_t *ctx) {
// 初始化向量
scc_vec_init(ctx->nodes);
scc_vec_init(ctx->types);
scc_vec_init(ctx->bblocks);
scc_vec_init(ctx->funcs);
// 初始化哈希表
scc_hashtable_init(&ctx->uid2nodes, hash_key, cmp_key);
scc_hashtable_init(&ctx->uid2types, hash_key, cmp_key);
scc_hashtable_init(&ctx->uid2bblocks, hash_key, cmp_key);
scc_hashtable_init(&ctx->uid2funcs, hash_key, cmp_key);
scc_hashtable_init(&ctx->type_uniquing,
(scc_hashtable_hash_func_t)hash_type,
(scc_hashtable_equal_func_t)cmp_type);
// 预留UID 0 作为无效引用
ctx->node_uid = 1;
ctx->type_uid = 1;
ctx->bblock_uid = 1;
ctx->func_uid = 1;
void scc_ir_ctx_init(scc_ir_ctx_t *ctx, scc_ir_module_t *module) {
ctx->module = module;
scc_hashtable_init(&ctx->type_uniquing, hash_type, cmp_type);
// scc_hashtable_init(&ctx->const_pool, /* 常量哈希函数 */,
// /* 常量比较函数 */);
scc_hashtable_init(&ctx->func_decl_set,
(scc_hashtable_hash_func_t)scc_strhash32,
(scc_hashtable_equal_func_t)scc_strcmp);
}
void scc_ir_ctx_drop(scc_ir_cprog_ctx_t *ctx) {
// 释放所有实体的内部内存
for (usize i = 0; i < ctx->nodes.size; i++) {
scc_ir_node_t *node = &ctx->nodes.data[i];
scc_vec_free(node->used_by);
if (node->tag == SCC_IR_NODE_CALL) {
scc_vec_free(node->data.call.args);
}
void scc_ir_ctx_drop(scc_ir_ctx_t *ctx) {
scc_hashtable_drop(&ctx->type_uniquing);
// scc_hashtable_drop(&ctx->const_pool);
scc_hashtable_drop(&ctx->func_decl_set);
}
scc_ir_type_ref_t scc_ir_ctx_get_type(scc_ir_ctx_t *ctx,
const scc_ir_type_t *type_desc) {
// 先查哈希表
void *found = scc_hashtable_get(&ctx->type_uniquing, (void *)type_desc);
if (found) {
return (scc_ir_type_ref_t)(usize)found;
}
for (usize i = 0; i < ctx->types.size; i++) {
scc_ir_type_t *type = &ctx->types.data[i];
if (type->tag == SCC_IR_TYPE_FUNC) {
scc_vec_free(type->data.function.params);
}
// 不存在,添加新类型
scc_ir_type_ref_t new_ref = scc_ir_module_add_type(ctx->module, type_desc);
scc_hashtable_set(&ctx->type_uniquing, (void *)type_desc,
(void *)(usize)new_ref);
return new_ref;
}
// scc_ir_node_ref_t scc_ir_ctx_get_const_int(scc_ir_ctx_t *ctx,
// scc_ir_type_ref_t type, i64 value)
// {
// return scc_ir_node_ref_t();
// }
scc_ir_func_ref_t scc_ir_ctx_declare_func(scc_ir_ctx_t *ctx,
scc_ir_type_ref_t type,
const char *name) {
// 检查是否已声明
void *found = scc_hashtable_get(&ctx->func_decl_set, (void *)name);
if (found) {
return (scc_ir_func_ref_t)(usize)found;
}
for (usize i = 0; i < ctx->bblocks.size; i++) {
scc_ir_bblock_t *bblock = &ctx->bblocks.data[i];
scc_vec_free(bblock->instrs);
}
for (usize i = 0; i < ctx->funcs.size; i++) {
scc_ir_func_t *func = &ctx->funcs.data[i];
scc_vec_free(func->params);
scc_vec_free(func->bblocks);
}
// 释放向量
scc_vec_free(ctx->nodes);
scc_vec_free(ctx->types);
scc_vec_free(ctx->bblocks);
scc_vec_free(ctx->funcs);
// 释放哈希表
scc_hashtable_drop(&ctx->uid2nodes);
scc_hashtable_drop(&ctx->uid2types);
scc_hashtable_drop(&ctx->uid2bblocks);
scc_hashtable_drop(&ctx->uid2funcs);
}
void scc_ir_ctx_reset(scc_ir_cprog_ctx_t *ctx) {
// 先销毁再重新初始化
scc_ir_ctx_drop(ctx);
scc_ir_ctx_init(ctx);
}
// 辅助宏:创建实体并添加到哈希表
#define CREATE_ENTITY(ctx, vec, uid, data, hashtable) \
do { \
/* 分配新UID */ \
unsigned new_uid = (ctx)->uid++; \
/* 添加到向量 */ \
scc_vec_push((vec), *(data)); \
/* 添加到哈希表 */ \
scc_hashtable_set(&(ctx)->hashtable, (const void *)(usize)new_uid, \
(void *)(usize)(scc_vec_size(vec) - 1)); \
return new_uid; \
} while (0)
scc_ir_type_ref_t scc_ir_ctx_new_type(scc_ir_cprog_ctx_t *ctx,
const scc_ir_type_t *type) {
// 首先检查去重表
void *existing = scc_hashtable_get(&ctx->type_uniquing, type);
if (existing) {
return (scc_ir_type_ref_t)(uintptr_t)existing;
}
CREATE_ENTITY(ctx, ctx->types, type_uid, type, uid2types);
}
scc_ir_node_ref_t scc_ir_ctx_new_node(scc_ir_cprog_ctx_t *ctx,
const scc_ir_node_t *node) {
CREATE_ENTITY(ctx, ctx->nodes, node_uid, node, uid2nodes);
}
scc_ir_bblock_ref_t scc_ir_ctx_new_bblock(scc_ir_cprog_ctx_t *ctx,
const scc_ir_bblock_t *bblock) {
CREATE_ENTITY(ctx, ctx->bblocks, bblock_uid, bblock, uid2bblocks);
}
scc_ir_func_ref_t scc_ir_ctx_new_func(scc_ir_cprog_ctx_t *ctx,
const scc_ir_func_t *func) {
CREATE_ENTITY(ctx, ctx->funcs, func_uid, func, uid2funcs);
}
#undef CREATE_ENTITY
// 辅助宏:从哈希表获取索引
#define GET_ENTITY_INDEX(ctx, ref, hashtable) \
((usize)scc_hashtable_get(&(ctx)->hashtable, (void *)(usize)ref))
scc_ir_type_t *scc_ir_ctx_get_type(scc_ir_cprog_ctx_t *ctx,
scc_ir_type_ref_t ref) {
if (ref == 0)
return null;
usize idx = GET_ENTITY_INDEX(ctx, ref, uid2types);
if (idx >= ctx->types.size)
return null;
return &ctx->types.data[idx];
}
scc_ir_node_t *scc_ir_ctx_get_node(scc_ir_cprog_ctx_t *ctx,
scc_ir_node_ref_t ref) {
if (ref == 0)
return null;
usize idx = GET_ENTITY_INDEX(ctx, ref, uid2nodes);
if (idx >= ctx->nodes.size)
return null;
return &ctx->nodes.data[idx];
}
scc_ir_bblock_t *scc_ir_ctx_get_bblock(scc_ir_cprog_ctx_t *ctx,
scc_ir_bblock_ref_t ref) {
if (ref == 0)
return null;
usize idx = GET_ENTITY_INDEX(ctx, ref, uid2bblocks);
if (idx >= ctx->bblocks.size)
return null;
return &ctx->bblocks.data[idx];
}
scc_ir_func_t *scc_ir_ctx_get_func(scc_ir_cprog_ctx_t *ctx,
scc_ir_func_ref_t ref) {
if (ref == 0)
return null;
usize idx = GET_ENTITY_INDEX(ctx, ref, uid2funcs);
if (idx >= ctx->funcs.size)
return null;
return &ctx->funcs.data[idx];
}
#undef GET_ENTITY_INDEX
// // 内置类型和常量缓存
// static scc_ir_type_ref_t cached_i32_type = 0;
// static scc_ir_node_ref_t cached_zero_const = 0;
scc_ir_type_ref_t scc_ir_ctx_get_builtin_i32(scc_ir_cprog_ctx_t *ctx) {
scc_ir_type_ref_t cached_i32_type = 0;
if (cached_i32_type == 0) {
scc_ir_type_t i32_type = {.tag = SCC_IR_TYPE_I32};
cached_i32_type = scc_ir_ctx_new_type(ctx, &i32_type);
}
return cached_i32_type;
}
scc_ir_node_ref_t scc_ir_ctx_get_builtin_zero(scc_ir_cprog_ctx_t *ctx) {
scc_ir_node_ref_t cached_zero_const = 0;
if (cached_zero_const == 0) {
scc_ir_node_t zero_node = {.tag = SCC_IR_NODE_CONST_INT,
.type = scc_ir_ctx_get_builtin_i32(ctx),
.name = "zero",
.data.const_int.int32 = 0};
cached_zero_const = scc_ir_ctx_new_node(ctx, &zero_node);
}
return cached_zero_const;
}
// 常量池(简化版)
typedef struct {
i32 value;
scc_ir_node_ref_t ref;
} i32_const_entry_t;
scc_ir_node_ref_t scc_ir_ctx_get_i32_const(scc_ir_cprog_ctx_t *ctx, i32 value) {
// 如果是0使用内置zero
if (value == 0) {
return scc_ir_ctx_get_builtin_zero(ctx);
}
// TODO: 实现常量池哈希表
// 这里简化实现,每次都创建新常量
scc_ir_node_t const_node = {.tag = SCC_IR_NODE_CONST_INT,
.type = scc_ir_ctx_get_builtin_i32(ctx),
.data.const_int.int32 = value};
const_node.name = null;
return scc_ir_ctx_new_node(ctx, &const_node);
}
scc_ir_node_ref_t scc_ir_ctx_get_null_const(scc_ir_cprog_ctx_t *ctx,
scc_ir_type_ref_t ptr_type) {
return 1;
// 创建新函数
scc_ir_func_t func = {
.name = name,
.type = type,
};
scc_vec_init(func.params);
scc_vec_init(func.bblocks);
scc_ir_func_ref_t new_ref = scc_ir_module_add_func(ctx->module, &func);
scc_hashtable_set(&ctx->func_decl_set, (void *)name,
(void *)(usize)new_ref);
return new_ref;
}

View File

@@ -1,5 +1,7 @@
#include <ir_ctx.h>
#include <ir_dump.h>
#include <ir_prog.h>
#define GET_MODULE(ctx) (&(ctx->cprog->module))
#define PRINT_VALUE(ctx, fmt, value) \
SCC_TREE_DUMP_PRINT_PURE(ctx, ctx->value_color, fmt, value)
@@ -66,14 +68,14 @@ static const char *get_op_str(scc_ir_op_type_t op) {
// 获取类型标签字符串
static const char *get_type_tag_str(scc_ir_type_tag_t tag) {
static const char *type_tags[] = {
[SCC_IR_TYPE_VOID] = "void", [SCC_IR_TYPE_U8] = "u8",
[SCC_IR_TYPE_U16] = "u16", [SCC_IR_TYPE_U32] = "u32",
[SCC_IR_TYPE_U64] = "u64", [SCC_IR_TYPE_U128] = "u128",
[SCC_IR_TYPE_I8] = "i8", [SCC_IR_TYPE_I16] = "i16",
[SCC_IR_TYPE_I32] = "i32", [SCC_IR_TYPE_I64] = "i64",
[SCC_IR_TYPE_I128] = "i128", [SCC_IR_TYPE_F16] = "f16",
[SCC_IR_TYPE_F32] = "f32", [SCC_IR_TYPE_F64] = "f64",
[SCC_IR_TYPE_F128] = "f128", [SCC_IR_TYPE_PTR] = "ptr",
[SCC_IR_TYPE_void] = "void", [SCC_IR_TYPE_u8] = "u8",
[SCC_IR_TYPE_u16] = "u16", [SCC_IR_TYPE_u32] = "u32",
[SCC_IR_TYPE_u64] = "u64", [SCC_IR_TYPE_u128] = "u128",
[SCC_IR_TYPE_i8] = "i8", [SCC_IR_TYPE_i16] = "i16",
[SCC_IR_TYPE_i32] = "i32", [SCC_IR_TYPE_i64] = "i64",
[SCC_IR_TYPE_i128] = "i128", [SCC_IR_TYPE_f16] = "f16",
[SCC_IR_TYPE_f32] = "f32", [SCC_IR_TYPE_f64] = "f64",
[SCC_IR_TYPE_f128] = "f128", [SCC_IR_TYPE_PTR] = "ptr",
[SCC_IR_TYPE_ARRAY] = "array", [SCC_IR_TYPE_FUNC] = "func",
[SCC_IR_TYPE_STRUCT] = "struct", [SCC_IR_TYPE_VECTOR] = "vector",
};
@@ -176,8 +178,8 @@ static void dump_branch_node(scc_ir_dump_ctx_t *ctx,
// 输出真分支块
if (node->data.branch.true_bblock) {
scc_ir_bblock_t *true_bblock =
scc_ir_ctx_get_bblock(ctx->ir_ctx, node->data.branch.true_bblock);
scc_ir_bblock_t *true_bblock = scc_ir_module_get_bblock(
GET_MODULE(ctx), node->data.branch.true_bblock);
if (true_bblock) {
scc_tree_print_indent(ctx->dump_ctx);
PRINT_NODE(ctx->dump_ctx, "TrueBlock: ");
@@ -189,8 +191,8 @@ static void dump_branch_node(scc_ir_dump_ctx_t *ctx,
// 输出假分支块
if (node->data.branch.false_bblock) {
scc_ir_bblock_t *false_bblock =
scc_ir_ctx_get_bblock(ctx->ir_ctx, node->data.branch.false_bblock);
scc_ir_bblock_t *false_bblock = scc_ir_module_get_bblock(
GET_MODULE(ctx), node->data.branch.false_bblock);
if (false_bblock) {
scc_tree_print_indent(ctx->dump_ctx);
PRINT_NODE(ctx->dump_ctx, "FalseBlock: ");
@@ -205,8 +207,8 @@ static void dump_branch_node(scc_ir_dump_ctx_t *ctx,
static void dump_jump_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) {
// PRINT_NODE(ctx->dump_ctx, "jump");
if (node->data.jump.target_bblock) {
scc_ir_bblock_t *target_bblock =
scc_ir_ctx_get_bblock(ctx->ir_ctx, node->data.jump.target_bblock);
scc_ir_bblock_t *target_bblock = scc_ir_module_get_bblock(
GET_MODULE(ctx), node->data.jump.target_bblock);
if (target_bblock) {
scc_tree_dump_printf(ctx->dump_ctx, "to '%s'",
target_bblock->label ? target_bblock->label
@@ -224,7 +226,7 @@ static void dump_call_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) {
// PRINT_NODE(ctx->dump_ctx, "call");
if (node->data.call.callee) {
scc_ir_func_t *callee =
scc_ir_ctx_get_func(ctx->ir_ctx, node->data.call.callee);
scc_ir_module_get_func(GET_MODULE(ctx), node->data.call.callee);
if (callee) {
scc_tree_dump_printf(ctx->dump_ctx, "func='%s'",
callee->name ? callee->name : "<unnamed>");
@@ -260,15 +262,14 @@ static void dump_ret_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) {
}
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) {
ctx->cprog = cprog;
ctx->dump_ctx = tree_dump;
ctx->ir_ctx = ir_ctx;
}
void scc_ir_dump_node(scc_ir_dump_ctx_t *ctx, scc_ir_node_ref_t node_ref) {
scc_ir_node_t *node = scc_ir_ctx_get_node(ctx->ir_ctx, node_ref);
scc_ir_node_t *node = scc_ir_module_get_node(GET_MODULE(ctx), node_ref);
if (!node) {
LOG_ERROR("Invalid node ref");
return;
@@ -283,7 +284,8 @@ void scc_ir_dump_node(scc_ir_dump_ctx_t *ctx, scc_ir_node_ref_t node_ref) {
if (node->type) {
scc_tree_dump_printf(ctx->dump_ctx, " : ");
scc_ir_type_t *type = scc_ir_ctx_get_type(ctx->ir_ctx, node->type);
scc_ir_type_t *type =
scc_ir_module_get_type(GET_MODULE(ctx), node->type);
if (type) {
PRINT_VALUE(ctx->dump_ctx, "%s", get_type_tag_str(type->tag));
}
@@ -338,7 +340,7 @@ void scc_ir_dump_type(scc_ir_dump_ctx_t *ctx, scc_ir_type_ref_t type_ref) {
return;
}
scc_ir_type_t *type = scc_ir_ctx_get_type(ctx->ir_ctx, type_ref);
scc_ir_type_t *type = scc_ir_module_get_type(GET_MODULE(ctx), type_ref);
if (!type) {
LOG_ERROR("invalid type ref");
return;
@@ -409,7 +411,8 @@ void scc_ir_dump_bblock(scc_ir_dump_ctx_t *ctx,
return;
}
scc_ir_bblock_t *bblock = scc_ir_ctx_get_bblock(ctx->ir_ctx, bblock_ref);
scc_ir_bblock_t *bblock =
scc_ir_module_get_bblock(GET_MODULE(ctx), bblock_ref);
if (!bblock) {
LOG_ERROR("invalid bblock ref");
return;
@@ -434,7 +437,7 @@ void scc_ir_dump_bblock(scc_ir_dump_ctx_t *ctx,
// 转储函数
void scc_ir_dump_func(scc_ir_dump_ctx_t *ctx, scc_ir_func_ref_t func_ref) {
scc_ir_func_t *func = scc_ir_ctx_get_func(ctx->ir_ctx, func_ref);
scc_ir_func_t *func = scc_ir_module_get_func(GET_MODULE(ctx), func_ref);
if (!ctx || !func) {
LOG_ERROR("invalid parameter");
return;
@@ -489,7 +492,7 @@ void scc_ir_dump_cprog(scc_ir_dump_ctx_t *ctx) {
// scc_ir_node_ref_t global_ref =
// scc_vec_at(program->global_vals, i); scc_ir_node_t
// *global_node =
// scc_ir_ctx_get_node(ir_ctx, global_ref);
// scc_ir_module_get_node(ir_ctx, global_ref);
// if (global_node) {
// scc_ir_dump_node(ctx, ir_ctx, global_node);
// }
@@ -526,17 +529,17 @@ void scc_ir_dump_cprog(scc_ir_dump_ctx_t *ctx) {
void scc_ir_dump_type_linear(scc_ir_dump_ctx_t *ctx,
scc_ir_type_ref_t type_ref) {
scc_ir_type_t *type = scc_ir_ctx_get_type(ctx->ir_ctx, type_ref);
scc_ir_type_t *type = scc_ir_module_get_type(GET_MODULE(ctx), type_ref);
if (!ctx || !type) {
LOG_ERROR("invalid parameter");
return;
}
#define PRINT_TYPE(ctx, name) PRINT_VALUE(ctx, "%s", name)
switch (type->tag) {
case SCC_IR_TYPE_I32:
case SCC_IR_TYPE_i32:
PRINT_TYPE(ctx->dump_ctx, "i32");
break;
case SCC_IR_TYPE_VOID:
case SCC_IR_TYPE_void:
PRINT_TYPE(ctx->dump_ctx, "void");
break;
case SCC_IR_TYPE_ARRAY:
@@ -577,7 +580,7 @@ void scc_ir_dump_type_linear(scc_ir_dump_ctx_t *ctx,
// 辅助函数:输出节点引用或值到缓冲区
static usize format_node_ref_or_value(scc_ir_dump_ctx_t *ctx, char *buf,
usize size, scc_ir_node_ref_t node_ref) {
scc_ir_node_t *node = scc_ir_ctx_get_node(ctx->ir_ctx, node_ref);
scc_ir_node_t *node = scc_ir_module_get_node(GET_MODULE(ctx), node_ref);
if (!node) {
return scc_snprintf(buf, size, "%%%u", node_ref);
}
@@ -598,7 +601,7 @@ static usize format_node_ref_or_value(scc_ir_dump_ctx_t *ctx, char *buf,
// 线性输出节点信息SSA IR风格
void scc_ir_dump_node_linear(scc_ir_dump_ctx_t *ctx,
scc_ir_node_ref_t node_ref) {
scc_ir_node_t *node = scc_ir_ctx_get_node(ctx->ir_ctx, node_ref);
scc_ir_node_t *node = scc_ir_module_get_node(GET_MODULE(ctx), node_ref);
if (node == null) {
LOG_ERROR("invalid node ref");
return;
@@ -707,8 +710,8 @@ void scc_ir_dump_node_linear(scc_ir_dump_ctx_t *ctx,
break;
case SCC_IR_NODE_CALL: {
char node_name[256] = "";
char args_buf[256] = "";
char node_name[256] = {0};
char args_buf[256] = {0};
char *args_p = args_buf;
usize args_remaining = sizeof(args_buf);
@@ -727,7 +730,7 @@ void scc_ir_dump_node_linear(scc_ir_dump_ctx_t *ctx,
}
scc_ir_func_t *func =
scc_ir_ctx_get_func(ctx->ir_ctx, node->data.call.callee);
scc_ir_module_get_func(GET_MODULE(ctx), node->data.call.callee);
p += scc_snprintf(p, remaining, "call @%s(%s)",
func ? func->name : null, args_buf);
break;
@@ -762,7 +765,8 @@ void scc_ir_dump_node_linear(scc_ir_dump_ctx_t *ctx,
// 线性输出基本块信息
void scc_ir_dump_bblock_linear(scc_ir_dump_ctx_t *ctx,
scc_ir_bblock_ref_t bblock_ref) {
scc_ir_bblock_t *bblock = scc_ir_ctx_get_bblock(ctx->ir_ctx, bblock_ref);
scc_ir_bblock_t *bblock =
scc_ir_module_get_bblock(GET_MODULE(ctx), bblock_ref);
if (bblock == null) {
PRINT_NODE(ctx->dump_ctx, "<invalid block>\n");
@@ -791,7 +795,7 @@ void scc_ir_dump_bblock_linear(scc_ir_dump_ctx_t *ctx,
// 线性输出函数信息
void scc_ir_dump_func_linear(scc_ir_dump_ctx_t *ctx,
scc_ir_func_ref_t func_ref) {
scc_ir_func_t *func = scc_ir_ctx_get_func(ctx->ir_ctx, func_ref);
scc_ir_func_t *func = scc_ir_module_get_func(GET_MODULE(ctx), func_ref);
if (!func) {
LOG_ERROR("invalid function reference");
return;
@@ -816,7 +820,7 @@ void scc_ir_dump_func_linear(scc_ir_dump_ctx_t *ctx,
scc_ir_node_ref_t param_ref = scc_vec_at(func->params, i);
PRINT_NODE(ctx->dump_ctx, "%");
scc_ir_node_t *param_node =
scc_ir_ctx_get_node(ctx->ir_ctx, param_ref);
scc_ir_module_get_node(GET_MODULE(ctx), param_ref);
if (param_node && param_node->name && param_node->name[0] != '\0') {
scc_snprintf(buff, sizeof(buff), "%u[%s]", param_ref,
param_node->name);
@@ -850,7 +854,8 @@ void scc_ir_dump_func_linear(scc_ir_dump_ctx_t *ctx,
void scc_ir_dump_cprog_linear(scc_ir_dump_ctx_t *ctx) {
scc_vec_foreach(ctx->cprog->func_decls, i) {
scc_ir_func_ref_t func_decl = scc_vec_at(ctx->cprog->func_decls, i);
scc_ir_func_t *func = scc_ir_ctx_get_func(ctx->ir_ctx, func_decl);
scc_ir_func_t *func =
scc_ir_module_get_func(GET_MODULE(ctx), func_decl);
Assert(func != null);
if (scc_vec_size(func->bblocks) == 0)
scc_ir_dump_func_linear(ctx, func_decl);

151
libs/ir/src/ir_prog.c Normal file
View File

@@ -0,0 +1,151 @@
#include <ir_prog.h>
void scc_ir_cprog_init(scc_ir_cprog_t *in) {
scc_vec_init(in->func_decls);
scc_vec_init(in->func_defs);
scc_vec_init(in->global_vals);
scc_ir_module_init(&in->module);
}
void scc_ir_cprog_drop(scc_ir_cprog_t *in) {
scc_vec_free(in->func_decls);
scc_vec_free(in->func_defs);
scc_vec_free(in->global_vals);
scc_ir_module_drop(&in->module);
}
static u32 hash_key(const void *key) { return (u32)(usize)key; }
static int cmp_key(const void *key1, const void *key2) {
return (u32)(usize)key1 != (u32)(usize)key2;
}
void scc_ir_module_init(scc_ir_module_t *ctx) {
scc_vec_init(ctx->nodes);
scc_vec_init(ctx->types);
scc_vec_init(ctx->bblocks);
scc_vec_init(ctx->funcs);
scc_hashtable_init(&ctx->uid2nodes, hash_key, cmp_key);
scc_hashtable_init(&ctx->uid2types, hash_key, cmp_key);
scc_hashtable_init(&ctx->uid2bblocks, hash_key, cmp_key);
scc_hashtable_init(&ctx->uid2funcs, hash_key, cmp_key);
// 预留UID 0 作为无效引用
ctx->node_uid = 1;
ctx->type_uid = 1;
ctx->bblock_uid = 1;
ctx->func_uid = 1;
}
void scc_ir_module_drop(scc_ir_module_t *ctx) {
// 释放所有实体的内部内存
for (usize i = 0; i < ctx->nodes.size; i++) {
scc_ir_node_t *node = &ctx->nodes.data[i];
scc_vec_free(node->used_by);
if (node->tag == SCC_IR_NODE_CALL) {
scc_vec_free(node->data.call.args);
}
}
for (usize i = 0; i < ctx->types.size; i++) {
scc_ir_type_t *type = &ctx->types.data[i];
if (type->tag == SCC_IR_TYPE_FUNC) {
scc_vec_free(type->data.function.params);
}
}
for (usize i = 0; i < ctx->bblocks.size; i++) {
scc_ir_bblock_t *bblock = &ctx->bblocks.data[i];
scc_vec_free(bblock->instrs);
}
for (usize i = 0; i < ctx->funcs.size; i++) {
scc_ir_func_t *func = &ctx->funcs.data[i];
scc_vec_free(func->params);
scc_vec_free(func->bblocks);
}
scc_vec_free(ctx->nodes);
scc_vec_free(ctx->types);
scc_vec_free(ctx->bblocks);
scc_vec_free(ctx->funcs);
scc_hashtable_drop(&ctx->uid2nodes);
scc_hashtable_drop(&ctx->uid2types);
scc_hashtable_drop(&ctx->uid2bblocks);
scc_hashtable_drop(&ctx->uid2funcs);
}
// 辅助宏:创建实体并添加到哈希表
#define CREATE_ENTITY(ctx, vec, uid, data, hashtable) \
do { \
/* 分配新UID */ \
unsigned new_uid = (ctx)->uid++; \
/* 添加到向量 */ \
scc_vec_push((vec), *(data)); \
/* 添加到哈希表 */ \
scc_hashtable_set(&(ctx)->hashtable, (const void *)(usize)new_uid, \
(void *)(usize)(scc_vec_size(vec) - 1)); \
return new_uid; \
} while (0)
scc_ir_type_ref_t scc_ir_module_add_type(scc_ir_module_t *ctx,
const scc_ir_type_t *type) {
CREATE_ENTITY(ctx, ctx->types, type_uid, type, uid2types);
}
scc_ir_node_ref_t scc_ir_module_add_node(scc_ir_module_t *ctx,
const scc_ir_node_t *node) {
CREATE_ENTITY(ctx, ctx->nodes, node_uid, node, uid2nodes);
}
scc_ir_bblock_ref_t scc_ir_module_add_bblock(scc_ir_module_t *ctx,
const scc_ir_bblock_t *bblock) {
CREATE_ENTITY(ctx, ctx->bblocks, bblock_uid, bblock, uid2bblocks);
}
scc_ir_func_ref_t scc_ir_module_add_func(scc_ir_module_t *ctx,
const scc_ir_func_t *func) {
CREATE_ENTITY(ctx, ctx->funcs, func_uid, func, uid2funcs);
}
// 辅助宏:从哈希表获取索引
#define GET_ENTITY_INDEX(ctx, ref, hashtable) \
((usize)scc_hashtable_get(&(ctx)->hashtable, (void *)(usize)ref))
scc_ir_type_t *scc_ir_module_get_type(scc_ir_module_t *ctx,
scc_ir_type_ref_t ref) {
if (ref == 0)
return null;
usize idx = GET_ENTITY_INDEX(ctx, ref, uid2types);
if (idx >= ctx->types.size)
return null;
return &ctx->types.data[idx];
}
scc_ir_node_t *scc_ir_module_get_node(scc_ir_module_t *ctx,
scc_ir_node_ref_t ref) {
if (ref == 0)
return null;
usize idx = GET_ENTITY_INDEX(ctx, ref, uid2nodes);
if (idx >= ctx->nodes.size)
return null;
return &ctx->nodes.data[idx];
}
scc_ir_bblock_t *scc_ir_module_get_bblock(scc_ir_module_t *ctx,
scc_ir_bblock_ref_t ref) {
if (ref == 0)
return null;
usize idx = GET_ENTITY_INDEX(ctx, ref, uid2bblocks);
if (idx >= ctx->bblocks.size)
return null;
return &ctx->bblocks.data[idx];
}
scc_ir_func_t *scc_ir_module_get_func(scc_ir_module_t *ctx,
scc_ir_func_ref_t ref) {
if (ref == 0)
return null;
usize idx = GET_ENTITY_INDEX(ctx, ref, uid2funcs);
if (idx >= ctx->funcs.size)
return null;
return &ctx->funcs.data[idx];
}

View File

@@ -1,9 +1,27 @@
#include <ir_base.h>
#include <scc_ir.h>
void scc_ir_type_init(scc_ir_type_t *in, scc_ir_type_tag_t tag) {
Assert(in != null);
in->tag = tag;
in->name = null;
switch (tag) {
case SCC_IR_TYPE_unknown:
case SCC_IR_TYPE_void:
case SCC_IR_TYPE_i8:
case SCC_IR_TYPE_i16:
case SCC_IR_TYPE_i32:
case SCC_IR_TYPE_i64:
case SCC_IR_TYPE_i128:
case SCC_IR_TYPE_u8:
case SCC_IR_TYPE_u16:
case SCC_IR_TYPE_u32:
case SCC_IR_TYPE_u64:
case SCC_IR_TYPE_u128:
case SCC_IR_TYPE_f16:
case SCC_IR_TYPE_f32:
case SCC_IR_TYPE_f64:
case SCC_IR_TYPE_f128:
break;
case SCC_IR_TYPE_ARRAY:
in->data.array.base = 0;
in->data.array.len = 0;
@@ -15,9 +33,6 @@ void scc_ir_type_init(scc_ir_type_t *in, scc_ir_type_tag_t tag) {
scc_vec_init(in->data.function.params);
in->data.function.ret_type = 0;
break;
case SCC_IR_TYPE_VOID:
case SCC_IR_TYPE_I32:
break;
default:
UNREACHABLE();
break;
@@ -53,7 +68,7 @@ void scc_ir_node_init(scc_ir_node_t *in, const char *name,
break;
case SCC_IR_NODE_CONST_INT:
// TODO
in->data.const_int.int32 = 0;
in->data.const_int.int64 = 0;
break;
case SCC_IR_NODE_ALLOC:
// TODO();
@@ -94,15 +109,3 @@ void scc_ir_node_init(scc_ir_node_t *in, const char *name,
break;
}
}
void scc_ir_cprog_init(scc_ir_cprog_t *in) {
scc_vec_init(in->func_decls);
scc_vec_init(in->func_defs);
scc_vec_init(in->global_vals);
}
void scc_ir_cprog_drop(scc_ir_cprog_t *in) {
scc_vec_free(in->func_decls);
scc_vec_free(in->func_defs);
scc_vec_free(in->global_vals);
}