Files
scc/libs/ir/include/ir_def.h
zzy 78e7c800ba 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。
2026-03-31 11:42:14 +08:00

244 lines
6.1 KiB
C

#ifndef __SCC_IR_DEF_H__
#define __SCC_IR_DEF_H__
#include <scc_core.h>
#define SCC_IR_REF_NULL 0
typedef unsigned int ir_handle_t;
typedef const char *scc_ir_label_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;
typedef SCC_VEC(scc_ir_type_ref_t) scc_ir_type_ref_vec_t;
typedef struct scc_ir_bblock scc_ir_bblock_t;
typedef ir_handle_t scc_ir_bblock_ref_t;
typedef SCC_VEC(scc_ir_bblock_ref_t) scc_ir_bblock_ref_vec_t;
typedef struct scc_ir_func scc_ir_func_t;
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_PTR,
SCC_IR_TYPE_ARRAY,
SCC_IR_TYPE_FUNC,
SCC_IR_TYPE_STRUCT,
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;
const char *name; // For Debug
union {
struct {
scc_ir_type_ref_t base;
usize len; // TODO usize is target dependent
} array;
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;
} function;
} data;
};
struct scc_ir_bblock {
scc_ir_label_t label;
scc_ir_node_ref_vec_t instrs;
// ir_arr_t used_by;
}; // basic block
struct scc_ir_func {
scc_ir_label_t name;
scc_ir_type_ref_t type;
scc_ir_node_ref_vec_t params;
scc_ir_bblock_ref_vec_t bblocks;
};
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
SCC_IR_OP_EMPTY,
/// Not equal to.
SCC_IR_OP_NEQ,
/// Equal to.
SCC_IR_OP_EQ,
/// Greater than.
SCC_IR_OP_GT,
/// Less than.
SCC_IR_OP_LT,
/// Greater than or equal to.
SCC_IR_OP_GE,
/// Less than or equal to.
SCC_IR_OP_LE,
/// Addition.
SCC_IR_OP_ADD,
/// Subtraction.
SCC_IR_OP_SUB,
/// Multiplication.
SCC_IR_OP_MUL,
/// Division.
SCC_IR_OP_DIV,
/// Modulo.
SCC_IR_OP_MOD,
/// Bitwise AND.
SCC_IR_OP_AND,
/// Bitwise OR.
SCC_IR_OP_OR,
/// Bitwise XOR.
SCC_IR_OP_XOR,
/// Bitwise NOT.
SCC_IR_OP_NOT,
/// Shift left logical.
SCC_IR_OP_SHL,
/// Shift right logical.
SCC_IR_OP_SHR,
/// Shift right arithmetic.
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_value {
scc_ir_type_ref_t type;
scc_ir_label_t name;
scc_ir_node_ref_vec_t used_by;
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;
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_value_ref_t value;
} global_alloc;
struct {
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_value_ref_t target;
} load;
struct {
scc_ir_value_ref_t target;
scc_ir_value_ref_t value;
} store;
struct {
scc_ir_value_ref_t src_addr;
scc_ir_value_ref_t index;
} get_ptr;
struct {
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_value_ref_t lhs;
scc_ir_value_ref_t rhs;
} op;
struct {
scc_ir_value_ref_t cond;
scc_ir_bblock_ref_t true_bblock;
scc_ir_bblock_ref_t false_bblock;
} branch;
struct {
scc_ir_bblock_ref_t target_bblock;
} jump;
struct {
scc_ir_func_ref_t callee; // TODO function pointer call
scc_ir_node_ref_vec_t args;
} call;
struct {
scc_ir_value_ref_t ret_val;
} ret;
} data;
};
#endif /* __SCC_IR_DEF_H__ */