refactor(ir): 将IR节点重构为值引用并添加字符串字面量支持

- 将scc_ir_node_ref_t重命名为scc_ir_value_ref_t,并更新所有相关API
- 修改IR定义中的节点标签枚举为值标签枚举(scc_ir_node_tag_t -> scc_ir_value_tag_t)
- 更新AST到IR转换器中所有节点引用的类型声明
- 添加对内置类型(VOID, CHAR, INT等)的完整映射实现
- 实现字符串字面量的常量数组创建功能
- 更新项目名称从"Simple Modual C Compiler"为"Simple C Compiler"
- 在Doxyfile中排除external目录的文档生成
- 移除未使用的strpool字段并重命名decl到IR的映射表

BREAKING CHANGE: IR节点引用类型已更改,所有使用scc_ir_node_ref_t的地方需
替换为scc_ir_value_ref_t。
This commit is contained in:
zzy
2026-03-31 11:42:14 +08:00
parent 4ddad7b456
commit 78e7c800ba
22 changed files with 992 additions and 727 deletions

View File

@@ -39,16 +39,8 @@ scc_ir_func_ref_t scc_ir_builder_func(scc_ir_builder_t *builder,
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);
}
void scc_ir_builder_add_instr(scc_ir_builder_t *builder,
scc_ir_value_ref_t instr);
#define SCC_IR_BUILDER_TYPE_FUNC(scc_type) \
[[maybe_unused]] static inline scc_ir_type_ref_t \
@@ -76,6 +68,78 @@ SCC_IR_BUILDER_TYPE_FUNC(f32)
SCC_IR_BUILDER_TYPE_FUNC(f64)
SCC_IR_BUILDER_TYPE_FUNC(f128)
// TODO
static inline scc_ir_value_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_value_t node;
scc_ir_node_init(&node, null, SCC_IR_VALUE_TAG_CONST_INT);
node.data.const_int = value;
node.type = type;
return scc_ir_module_add_value(&builder->cprog->module, &node);
}
static inline scc_ir_value_ref_t
scc_ir_builder_const_string(scc_ir_builder_t *builder, const char *str,
usize len) {
scc_ir_type_ref_t u8_type = scc_ir_builder_type_u8(builder);
scc_ir_type_t array_type = {
.tag = SCC_IR_TYPE_ARRAY,
.data.array.base = u8_type,
.data.array.len = len + 1, // 包含 null 结尾
};
scc_ir_type_ref_t array_type_ref =
scc_ir_ctx_get_type(&builder->ctx, &array_type);
// 2. 创建指针类型:指向 array_type
scc_ir_type_t ptr_type = {.tag = SCC_IR_TYPE_PTR,
.data.pointer.base = u8_type};
scc_ir_type_ref_t ptr_type_ref =
scc_ir_ctx_get_type(&builder->ctx, &ptr_type);
// 5. 创建聚合节点
scc_ir_value_t const_array_value = {
.tag = SCC_IR_VALUE_TAG_CONST_ARRAY,
.type = array_type_ref,
.data.const_array.base_type = u8_type,
};
char *buff = scc_malloc(len + 1);
Assert(buff);
for (usize i = 0; i < len; i++) {
buff[i] = str[i];
}
buff[len] = '\0';
scc_vec_unsafe_from_buffer(const_array_value.data.const_array.elements,
buff, len + 1);
scc_ir_value_ref_t const_array_ref =
scc_ir_module_add_value(builder->ctx.module, &const_array_value);
Assert(const_array_ref != SCC_IR_REF_NULL);
// 3. 创建全局变量节点,类型为指针,初始值指向常量数组
char *name = scc_malloc(32);
// FIXME MAYBE MEMORY LEAK
scc_ir_value_ref_t global_value_ref = scc_ir_module_add_value(
builder->ctx.module, &(scc_ir_value_t){
.name = name,
.tag = SCC_IR_VALUE_TAG_GLOBAL_ALLOC,
.type = ptr_type_ref,
.data.global_alloc.value = const_array_ref,
});
scc_snprintf(name, 32, "$G%u", global_value_ref);
scc_vec_push(builder->cprog->global_vals, global_value_ref);
// scc_hashtable_insert(builder);
scc_ir_value_ref_t pointer_to_global_value = scc_ir_module_add_value(
builder->ctx.module, &(scc_ir_value_t){
.tag = SCC_IR_VALUE_TAG_GET_PTR,
.data.get_ptr.src_addr = global_value_ref,
.data.get_ptr.index = SCC_IR_VALUE_TAG_NULL,
});
scc_ir_builder_add_instr(builder, pointer_to_global_value);
return pointer_to_global_value;
}
/**
* @brief 开始构建函数
* @param func_ref 函数引用
@@ -129,38 +193,38 @@ void scc_ir_builder_set_current_bblock(scc_ir_builder_t *builder,
* @param type 分配的类型
* @param name 变量名可为NULL
*/
scc_ir_node_ref_t scc_ir_builder_alloca(scc_ir_builder_t *builder,
scc_ir_type_ref_t type,
const char *name);
scc_ir_value_ref_t scc_ir_builder_alloca(scc_ir_builder_t *builder,
scc_ir_type_ref_t type,
const char *name);
scc_ir_node_ref_t scc_ir_builder_func_arg_ref(scc_ir_builder_t *builder,
scc_ir_type_ref_t type,
const char *name, usize arg_idx);
scc_ir_value_ref_t scc_ir_builder_func_arg_ref(scc_ir_builder_t *builder,
scc_ir_type_ref_t type,
const char *name, usize arg_idx);
/**
* @brief 创建load指令
* @param ptr 指针操作数
*/
scc_ir_node_ref_t scc_ir_builder_load(scc_ir_builder_t *builder,
scc_ir_node_ref_t ptr);
scc_ir_value_ref_t scc_ir_builder_load(scc_ir_builder_t *builder,
scc_ir_value_ref_t ptr);
/**
* @brief 创建store指令
* @param ptr 目标指针
* @param value 要存储的值
*/
scc_ir_node_ref_t scc_ir_builder_store(scc_ir_builder_t *builder,
scc_ir_node_ref_t ptr,
scc_ir_node_ref_t value);
scc_ir_value_ref_t scc_ir_builder_store(scc_ir_builder_t *builder,
scc_ir_value_ref_t ptr,
scc_ir_value_ref_t value);
/**
* @brief 创建getptr指令指针运算
* @param ptr 基础指针
* @param index 索引值
*/
scc_ir_node_ref_t scc_ir_builder_get_ptr(scc_ir_builder_t *builder,
scc_ir_node_ref_t ptr,
scc_ir_node_ref_t index);
scc_ir_value_ref_t scc_ir_builder_get_ptr(scc_ir_builder_t *builder,
scc_ir_value_ref_t ptr,
scc_ir_value_ref_t index);
/**
* @brief 创建二元运算指令
@@ -168,10 +232,10 @@ scc_ir_node_ref_t scc_ir_builder_get_ptr(scc_ir_builder_t *builder,
* @param lhs 左操作数
* @param rhs 右操作数
*/
scc_ir_node_ref_t scc_ir_builder_binop(scc_ir_builder_t *builder,
scc_ir_op_type_t op,
scc_ir_node_ref_t lhs,
scc_ir_node_ref_t rhs);
scc_ir_value_ref_t scc_ir_builder_binop(scc_ir_builder_t *builder,
scc_ir_op_type_t op,
scc_ir_value_ref_t lhs,
scc_ir_value_ref_t rhs);
/**
* @brief 创建比较指令
@@ -179,16 +243,17 @@ scc_ir_node_ref_t scc_ir_builder_binop(scc_ir_builder_t *builder,
* @param lhs 左操作数
* @param rhs 右操作数
*/
scc_ir_node_ref_t scc_ir_builder_cmp(scc_ir_builder_t *builder,
scc_ir_op_type_t op, scc_ir_node_ref_t lhs,
scc_ir_node_ref_t rhs);
scc_ir_value_ref_t scc_ir_builder_cmp(scc_ir_builder_t *builder,
scc_ir_op_type_t op,
scc_ir_value_ref_t lhs,
scc_ir_value_ref_t rhs);
/**
* @brief 创建跳转指令(无条件)
* @param target 目标基本块
*/
scc_ir_node_ref_t scc_ir_builder_jump(scc_ir_builder_t *builder,
scc_ir_bblock_ref_t target);
scc_ir_value_ref_t scc_ir_builder_jump(scc_ir_builder_t *builder,
scc_ir_bblock_ref_t target);
/**
* @brief 创建条件分支指令
@@ -196,10 +261,10 @@ scc_ir_node_ref_t scc_ir_builder_jump(scc_ir_builder_t *builder,
* @param true_target 条件为真时的目标
* @param false_target 条件为假时的目标
*/
scc_ir_node_ref_t scc_ir_builder_branch(scc_ir_builder_t *builder,
scc_ir_node_ref_t cond,
scc_ir_bblock_ref_t true_target,
scc_ir_bblock_ref_t false_target);
scc_ir_value_ref_t scc_ir_builder_branch(scc_ir_builder_t *builder,
scc_ir_value_ref_t cond,
scc_ir_bblock_ref_t true_target,
scc_ir_bblock_ref_t false_target);
/**
* @brief 创建函数调用指令
@@ -207,21 +272,21 @@ scc_ir_node_ref_t scc_ir_builder_branch(scc_ir_builder_t *builder,
* @param args 参数列表
* @param arg_count 参数数量
*/
scc_ir_node_ref_t scc_ir_builder_call(scc_ir_builder_t *builder,
scc_ir_func_ref_t callee,
const scc_ir_node_ref_t *args,
usize arg_count);
scc_ir_value_ref_t scc_ir_builder_call(scc_ir_builder_t *builder,
scc_ir_func_ref_t callee,
const scc_ir_value_ref_t *args,
usize arg_count);
/**
* @brief 创建返回指令(带返回值)
* @param value 返回值
*/
scc_ir_node_ref_t scc_ir_builder_ret(scc_ir_builder_t *builder,
scc_ir_node_ref_t value);
scc_ir_value_ref_t scc_ir_builder_ret(scc_ir_builder_t *builder,
scc_ir_value_ref_t value);
/**
* @brief 创建返回指令void返回
*/
scc_ir_node_ref_t scc_ir_builder_ret_void(scc_ir_builder_t *builder);
scc_ir_value_ref_t scc_ir_builder_ret_void(scc_ir_builder_t *builder);
#endif /* __SCC_IR_BUILDER_H__ */

View File

@@ -19,11 +19,6 @@ void scc_ir_ctx_drop(scc_ir_ctx_t *ctx);
scc_ir_type_ref_t scc_ir_ctx_get_type(scc_ir_ctx_t *ctx,
const scc_ir_type_t *type_desc);
// 获取唯一常量(例如整数常量)
// 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_ir_func_ref_t scc_ir_ctx_declare_func(scc_ir_ctx_t *ctx,
scc_ir_type_ref_t type,

View File

@@ -8,9 +8,11 @@
typedef unsigned int ir_handle_t;
typedef const char *scc_ir_label_t;
typedef struct scc_ir_node scc_ir_node_t;
typedef ir_handle_t scc_ir_node_ref_t;
typedef SCC_VEC(scc_ir_node_ref_t) scc_ir_node_ref_vec_t;
typedef SCC_VEC(u8) scc_ir_buffer_t;
typedef struct scc_ir_value scc_ir_value_t;
typedef ir_handle_t scc_ir_value_ref_t;
typedef SCC_VEC(scc_ir_value_ref_t) scc_ir_node_ref_vec_t;
typedef struct scc_ir_type scc_ir_type_t;
typedef ir_handle_t scc_ir_type_ref_t;
@@ -41,7 +43,6 @@ typedef enum scc_ir_type_tag {
SCC_IR_TYPE_f32,
SCC_IR_TYPE_f64,
SCC_IR_TYPE_f128,
SCC_IR_TYPE_PTR,
SCC_IR_TYPE_ARRAY,
SCC_IR_TYPE_FUNC,
@@ -84,26 +85,28 @@ struct scc_ir_func {
scc_ir_bblock_ref_vec_t bblocks;
};
typedef enum scc_ir_node_tag {
SCC_IR_NODE_NULL,
SCC_IR_NODE_CONST_INT,
SCC_IR_NODE_CONST_UINT,
SCC_IR_NODE_CONST_FLOAT,
SCC_IR_NODE_CONV, ///< 类型转换
SCC_IR_NODE_FUNC_ARG_REF, ///< 函数参数引用
SCC_IR_NODE_BLOCK_ARG_REF, ///< 基本块参数引用
SCC_IR_NODE_ALLOC, ///< 分配内存(stack)
SCC_IR_NODE_GLOBAL_ALLOC, ///< 全局分配(bss)
SCC_IR_NODE_LOAD, ///< 加载数据
SCC_IR_NODE_STORE, ///< 存储数据
SCC_IR_NODE_GET_PTR, ///< 获取指针
SCC_IR_NODE_GET_ELEM_PTR, ///< 获取元素指针(used by array)
SCC_IR_NODE_OP, ///< 二元运算
SCC_IR_NODE_BRANCH, ///< 有条件分支
SCC_IR_NODE_JUMP, ///< 无条件跳转
SCC_IR_NODE_CALL, ///< 调用函数
SCC_IR_NODE_RET, ///< 函数返回
} scc_ir_node_tag_t;
typedef enum scc_ir_value_tag {
SCC_IR_VALUE_TAG_NULL,
SCC_IR_VALUE_TAG_CONST_INT,
SCC_IR_VALUE_TAG_CONST_UINT,
SCC_IR_VALUE_TAG_CONST_FLOAT,
SCC_IR_VALUE_TAG_CONST_ARRAY,
SCC_IR_VALUE_TAG_AGGREGATE, ///< 聚合值
SCC_IR_VALUE_TAG_CONV, ///< 类型转换
SCC_IR_VALUE_TAG_FUNC_ARG_REF, ///< 函数参数引用
SCC_IR_VALUE_TAG_BLOCK_ARG_REF, ///< 基本块参数引用
SCC_IR_VALUE_TAG_ALLOC, ///< 分配内存
SCC_IR_VALUE_TAG_GLOBAL_ALLOC, ///< 全局分配
SCC_IR_VALUE_TAG_LOAD, ///< 加载数据
SCC_IR_VALUE_TAG_STORE, ///< 存储数据
SCC_IR_VALUE_TAG_GET_PTR, ///< 获取指针
SCC_IR_VALUE_TAG_GET_ELEM_PTR, ///< 获取元素指针
SCC_IR_VALUE_TAG_OP, ///< 二元运算
SCC_IR_VALUE_TAG_BRANCH, ///< 有条件分支
SCC_IR_VALUE_TAG_JUMP, ///< 无条件跳转
SCC_IR_VALUE_TAG_CALL, ///< 调用函数
SCC_IR_VALUE_TAG_RET, ///< 函数返回
} scc_ir_value_tag_t;
typedef enum {
/// Empty op for init or nop
@@ -172,49 +175,55 @@ typedef union {
u8 float_any[16];
} scc_ir_const_float_t;
struct scc_ir_node {
struct scc_ir_value {
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;
scc_ir_value_tag_t tag;
union {
scc_ir_const_int_t const_int;
scc_ir_const_uint_t const_uint;
scc_ir_const_float_t const_float;
// aggregate;
struct {
scc_ir_value_ref_t base_type;
scc_ir_buffer_t elements;
} const_array;
struct {
scc_ir_node_ref_vec_t elements;
} aggregate;
struct {
usize idx;
} arg_ref;
struct {
scc_ir_node_ref_vec_t elements;
scc_ir_value_ref_t value;
} global_alloc;
struct {
scc_ir_node_ref_t operand;
scc_ir_value_ref_t operand;
scc_ir_type_ref_t target_type; // 目标类型
enum { CONV_SEXT, CONV_ZEXT, CONV_TRUNC } conv_type;
} conv;
struct {
scc_ir_node_ref_t target;
scc_ir_value_ref_t target;
} load;
struct {
scc_ir_node_ref_t target;
scc_ir_node_ref_t value;
scc_ir_value_ref_t target;
scc_ir_value_ref_t value;
} store;
struct {
scc_ir_node_ref_t src_addr;
scc_ir_node_ref_t index;
scc_ir_value_ref_t src_addr;
scc_ir_value_ref_t index;
} get_ptr;
struct {
scc_ir_node_ref_t src_addr;
scc_ir_node_ref_t index;
scc_ir_value_ref_t src_addr;
scc_ir_value_ref_t index;
} get_elem_ptr;
struct {
scc_ir_op_type_t op;
scc_ir_node_ref_t lhs;
scc_ir_node_ref_t rhs;
scc_ir_value_ref_t lhs;
scc_ir_value_ref_t rhs;
} op;
struct {
scc_ir_node_ref_t cond;
scc_ir_value_ref_t cond;
scc_ir_bblock_ref_t true_bblock;
scc_ir_bblock_ref_t false_bblock;
} branch;
@@ -226,7 +235,7 @@ struct scc_ir_node {
scc_ir_node_ref_vec_t args;
} call;
struct {
scc_ir_node_ref_t ret_val;
scc_ir_value_ref_t ret_val;
} ret;
} data;
};

View File

@@ -12,7 +12,7 @@ typedef struct {
void scc_ir_dump_ctx_init(scc_ir_dump_ctx_t *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_value(scc_ir_dump_ctx_t *ctx, scc_ir_value_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);
void scc_ir_dump_func(scc_ir_dump_ctx_t *ctx, scc_ir_func_ref_t func_ref);

View File

@@ -0,0 +1,42 @@
#ifndef __SCC_IR_MODULE_H__
#define __SCC_IR_MODULE_H__
#include "ir_def.h"
#include <scc_hashtable.h>
typedef struct {
unsigned int value_uid;
unsigned int type_uid;
unsigned int bblock_uid;
unsigned int func_uid;
SCC_VEC(scc_ir_value_t) values;
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 uid2value;
scc_hashtable_t uid2type;
scc_hashtable_t uid2bblock;
scc_hashtable_t uid2func;
} 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_value_ref_t scc_ir_module_add_value(scc_ir_module_t *ctx,
const scc_ir_value_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_value_t *scc_ir_module_get_value(scc_ir_module_t *ctx,
scc_ir_value_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);
#endif /* __SCC_IR_MODULE_H__ */

View File

@@ -2,42 +2,7 @@
#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);
#include "ir_module.h"
typedef struct scc_ir_cprog {
scc_ir_module_t module;

View File

@@ -9,7 +9,7 @@ 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_node_init(scc_ir_value_t *in, const char *name,
scc_ir_value_tag_t tag);
#endif /* __SCC_IR_H__ */