feat(ast): 重构AST转储功能并创建AST到IR转换器

- 重构ast_dump.c中的宏定义,简化PRINT_VALUE、PRINT_NODE、
  PRINT_QUOTED_VALUE等宏的实现
- 移除冗余的PRINT_COLORED宏定义
- 统一使用SCC_TREE_DUMP_PRINT_PURE和SCC_TREE_DUMP_PRINT_AROUND
  宏进行转储输出
- 在dump_stmt_impl函数中优化节点转储逻辑,统一使用end_node_dump
  替代手动换行
- 添加对未知节点类型的警告日志输出
- 创建新的ast2ir模块,包含AST到IR的基本转换功能
- 实现ast_type_to_ir_type、ast_expr_to_ir、ast_stmt_to_ir、
  ast_decl_to_ir等核心转换函数
- 更新IR库依赖配置,添加scc_utils和tree_dump依赖
- 新增IR基础接口定义文件ir_base.h和IR构建器接口ir_builder.h
This commit is contained in:
zzy
2026-01-30 23:01:55 +08:00
parent c8bf98525d
commit 2a90e165a5
17 changed files with 2341 additions and 172 deletions

View File

@@ -3,52 +3,75 @@
#include <scc_core.h>
typedef unsigned int ir_handle_t;
typedef const char *scc_ir_label_t;
typedef struct scc_ir_node scc_ir_node_t;
typedef SCC_VEC(scc_ir_node_t) scc_ir_node_vec_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 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_VOID,
SCC_IR_TYPE_I1,
SCC_IR_TYPE_I8,
SCC_IR_TYPE_I16,
SCC_IR_TYPE_I32,
SCC_IR_TYPE_I64,
SCC_IR_TYPE_I128,
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_tag_t;
typedef struct scc_ir_type scc_ir_type_t;
typedef SCC_VEC(scc_ir_type_t) scc_ir_type_vec_t;
struct scc_ir_type {
scc_ir_type_tag_t tag;
// int size; // 字节大小
// int align; // 对齐要求
union {
struct {
const scc_ir_type_t *base;
usize len;
scc_ir_type_ref_t base;
usize len; // TODO usize is target dependent
} array;
struct {
const scc_ir_type_t *base;
scc_ir_type_ref_t base;
} pointer;
struct {
scc_ir_type_vec_t params;
const scc_ir_type_t *ret_type;
scc_ir_type_ref_vec_t params;
scc_ir_type_ref_t ret_type;
} function;
} data;
};
typedef struct scc_ir_bblock {
const char *label;
scc_ir_node_vec_t instrs;
struct scc_ir_bblock {
scc_ir_label_t label;
scc_ir_node_ref_vec_t instrs;
// ir_arr_t used_by;
} scc_ir_bblock_t; // basic block
typedef SCC_VEC(scc_ir_bblock_t) scc_ir_bblock_vec_t;
}; // basic block
typedef struct {
const char *name;
scc_ir_type_t *type;
scc_ir_node_vec_t params;
scc_ir_bblock_vec_t bblocks;
} scc_ir_func_t;
typedef SCC_VEC(scc_ir_func_t) scc_ir_func_vec_t;
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_node_tag {
SCC_IR_NODE_NULL,
@@ -106,9 +129,9 @@ typedef enum {
} scc_ir_op_type_t;
struct scc_ir_node {
const scc_ir_type_t *type;
const char *name;
scc_ir_node_vec_t used_by;
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 {
@@ -117,7 +140,7 @@ struct scc_ir_node {
i32 int32;
i64 int64;
// TODO int128
i8 int_any[4];
i8 int_any[16];
} const_int;
union {
u8 uint8;
@@ -125,54 +148,54 @@ struct scc_ir_node {
u32 uint32;
u64 uint64;
// TODO uint128;
u8 uint_any[4];
u8 uint_any[16];
} const_uint;
// aggregate;
// func_arg_ref;
// block_arg_ref;
// global_alloc;
struct {
scc_ir_node_t *target;
scc_ir_node_ref_t target;
} load;
struct {
scc_ir_node_t *target;
scc_ir_node_t *value;
scc_ir_node_ref_t target;
scc_ir_node_ref_t value;
} store;
struct {
scc_ir_node_t *src_addr;
scc_ir_node_t *index;
scc_ir_node_ref_t src_addr;
scc_ir_node_ref_t index;
} get_ptr;
struct {
scc_ir_node_t *src_addr;
scc_ir_node_t *index;
scc_ir_node_ref_t src_addr;
scc_ir_node_ref_t index;
} get_elem_ptr;
struct {
scc_ir_op_type_t op;
scc_ir_node_t *lhs;
scc_ir_node_t *rhs;
scc_ir_node_ref_t lhs;
scc_ir_node_ref_t rhs;
} op;
struct {
scc_ir_node_t *cond;
scc_ir_bblock_t *true_bblock;
scc_ir_bblock_t *false_bblock;
scc_ir_node_ref_t cond;
scc_ir_bblock_ref_t true_bblock;
scc_ir_bblock_ref_t false_bblock;
} branch;
struct {
scc_ir_bblock_t *target_bblock;
scc_ir_bblock_ref_t target_bblock;
} jump;
struct {
scc_ir_func_t *callee;
scc_ir_node_vec_t args;
scc_ir_func_ref_t callee;
scc_ir_node_ref_vec_t args;
} call;
struct {
scc_ir_node_t *ret_val;
scc_ir_node_ref_t ret_val;
} ret;
} data;
};
typedef struct {
scc_ir_node_vec_t global_vals;
scc_ir_func_vec_t funcs;
scc_ir_func_vec_t extern_funcs;
} ir_cprog_t;
typedef struct scc_ir_cprog {
scc_ir_node_ref_vec_t global_vals;
scc_ir_func_ref_vec_t func_defs;
scc_ir_node_ref_vec_t func_decls;
} scc_ir_cprog_t;
#endif /* __SCC_IR_DEF_H__ */