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

17
libs/ir/include/ir_base.h Normal file
View File

@@ -0,0 +1,17 @@
#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

@@ -0,0 +1,174 @@
#ifndef __SCC_IR_BUILDER_H__
#define __SCC_IR_BUILDER_H__
#include "ir_ctx.h"
#include "ir_def.h"
#include <scc_core.h>
typedef struct scc_ir_builder scc_ir_builder_t;
/**
* @brief IR 构建器上下文
*
* 负责管理 IR 构建过程中的所有状态:
* - 类型统一化type uniquing
* - 符号命名分配
* - 内存管理
* - 当前构建位置(函数、基本块)
*/
struct scc_ir_builder {
scc_ir_cprog_ctx_t ctx; /**< 核心上下文 */
scc_ir_cprog_t cprog;
// 当前构建位置
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);
/**
* @brief 销毁 IR 构建器及其所有资源
*/
void scc_ir_builder_drop(scc_ir_builder_t *builder);
/**
* @brief 开始构建函数
* @param name 函数名
* @param type 函数类型
* @param param_names 参数名列表可为NULL
* @return 函数引用
*/
scc_ir_func_ref_t scc_ir_builder_begin_func(scc_ir_builder_t *builder,
const char *name,
scc_ir_type_ref_t type,
const char **param_names);
/**
* @brief 结束当前函数的构建
*/
void scc_ir_builder_end_func(scc_ir_builder_t *builder);
/**
* @brief 获取当前正在构建的函数
*/
scc_ir_func_ref_t scc_ir_builder_current_func(scc_ir_builder_t *builder);
/**
* @brief 开始构建新的基本块
* @param label 基本块标签可为NULL自动生成
* @return 基本块引用
*/
scc_ir_bblock_ref_t scc_ir_builder_begin_bblock(scc_ir_builder_t *builder,
const char *label);
/**
* @brief 结束当前基本块的构建
*/
void scc_ir_builder_end_bblock(scc_ir_builder_t *builder);
/**
* @brief 设置当前基本块
*/
void scc_ir_builder_set_current_bblock(scc_ir_builder_t *builder,
scc_ir_bblock_ref_t bblock);
/**
* @brief 创建alloca指令在当前基本块中
* @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);
/**
* @brief 创建load指令
* @param ptr 指针操作数
*/
scc_ir_node_ref_t scc_ir_builder_load(scc_ir_builder_t *builder,
scc_ir_node_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);
/**
* @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);
/**
* @brief 创建二元运算指令
* @param op 操作符
* @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);
/**
* @brief 创建比较指令
* @param op 比较操作符
* @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);
/**
* @brief 创建跳转指令(无条件)
* @param target 目标基本块
*/
scc_ir_node_ref_t scc_ir_builder_jump(scc_ir_builder_t *builder,
scc_ir_bblock_ref_t target);
/**
* @brief 创建条件分支指令
* @param cond 条件值
* @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);
/**
* @brief 创建函数调用指令
* @param callee 被调用函数
* @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);
/**
* @brief 创建返回指令(带返回值)
* @param value 返回值
*/
scc_ir_node_ref_t scc_ir_builder_ret(scc_ir_builder_t *builder,
scc_ir_node_ref_t value);
/**
* @brief 创建返回指令void返回
*/
scc_ir_node_ref_t scc_ir_builder_ret_void(scc_ir_builder_t *builder);
#endif /* __SCC_IR_BUILDER_H__ */

200
libs/ir/include/ir_ctx.h Normal file
View File

@@ -0,0 +1,200 @@
#ifndef __SCC_IR_CTX_H__
#define __SCC_IR_CTX_H__
#include "ir_base.h"
#include "ir_def.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_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 -> 索引 映射
scc_hashtable_t uid2nodes;
scc_hashtable_t uid2types;
scc_hashtable_t uid2bblocks;
scc_hashtable_t uid2funcs;
// 类型去重表(类型键 -> 类型引用)
scc_hashtable_t type_uniquing;
// 常量池(常量键 -> 节点引用)
scc_hashtable_t const_pool;
// 内置类型缓存
scc_ir_type_ref_t builtin_i32;
scc_ir_type_ref_t builtin_i1;
scc_ir_type_ref_t builtin_void;
scc_ir_node_ref_t builtin_zero_i32;
} 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);
#endif /* __SCC_IR_CTX_H__ */

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

View File

@@ -1,16 +1,21 @@
#ifndef __SCC_IR_DUMP_H__
#define __SCC_IR_DUMP_H__
#include "ir_ctx.h"
#include "ir_def.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();
void scc_ir_type_dump(scc_ir_dump_ctx_t *ctx, const scc_ir_type_t *in);
void scc_ir_bblock_dump(scc_ir_dump_ctx_t *ctx, const scc_ir_bblock_t *in);
void scc_ir_func_dump(scc_ir_dump_ctx_t *ctx, const scc_ir_func_t *in);
void scc_ir_node_dump(scc_ir_dump_ctx_t *ctx, scc_ir_node_t *in);
void scc_ir_cprog_dump(scc_ir_dump_ctx_t *ctx, ir_cprog_t *in);
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);
void scc_ir_dump_func(scc_ir_dump_ctx_t *ctx, scc_ir_func_ref_t func_ref);
void scc_ir_dump_cprog(scc_ir_dump_ctx_t *ctx);
#endif /* __SCC_IR_DUMP_H__ */

View File

@@ -1,27 +1,9 @@
#ifndef __SCC_IR_H__
#define __SCC_IR_H__
#include "ir_builder.h"
#include "ir_ctx.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(ir_cprog_t *in);
scc_ir_type_t *scc_ir_type_alloc(scc_ir_type_tag_t tag);
scc_ir_bblock_t *scc_ir_bblock_alloc(const char *label);
scc_ir_func_t *scc_ir_func_alloc(const char *name);
scc_ir_node_t *scc_ir_node_alloc(const char *name, scc_ir_node_tag_t tag);
// scc_ir_type_t *scc_ir_type_alloc_with_ctx(scc_ir_type_tag_t tag);
// scc_ir_bblock_t *scc_ir_bblock_alloc_with_ctx(const char *label);
// scc_ir_func_t *scc_ir_func_alloc_with_ctx(const char *name);
// scc_ir_node_t *scc_ir_node_alloc_with_ctx(const char *name,
// scc_ir_node_tag_t tag);
#include <scc_utils.h>
#endif /* __SCC_IR_H__ */