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:
@@ -4,7 +4,10 @@ version = "0.1.0"
|
||||
authors = []
|
||||
description = ""
|
||||
|
||||
dependencies = [{ name = "scc_core", path = "../../runtime/scc_core" }]
|
||||
dependencies = [
|
||||
{ name = "scc_utils", path = "../../runtime/scc_utils" },
|
||||
{ name = "tree_dump", path = "../tree_dump" },
|
||||
]
|
||||
# dependencies = []
|
||||
# features = {}
|
||||
# default_features = []
|
||||
|
||||
17
libs/ir/include/ir_base.h
Normal file
17
libs/ir/include/ir_base.h
Normal 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__ */
|
||||
174
libs/ir/include/ir_builder.h
Normal file
174
libs/ir/include/ir_builder.h
Normal 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
200
libs/ir/include/ir_ctx.h
Normal 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__ */
|
||||
@@ -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__ */
|
||||
|
||||
@@ -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__ */
|
||||
|
||||
@@ -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__ */
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
#include <scc_ir.h>
|
||||
#include <ir_base.h>
|
||||
|
||||
void scc_ir_type_init(scc_ir_type_t *in, scc_ir_type_tag_t tag) {
|
||||
Assert(in != null);
|
||||
in->tag = tag;
|
||||
switch (tag) {
|
||||
case SCC_IR_TYPE_ARRAY:
|
||||
in->data.array.base = null;
|
||||
in->data.array.base = 0;
|
||||
in->data.array.len = 0;
|
||||
break;
|
||||
case SCC_IR_TYPE_PTR:
|
||||
in->data.pointer.base = null;
|
||||
in->data.pointer.base = 0;
|
||||
break;
|
||||
case SCC_IR_TYPE_FUNC:
|
||||
scc_vec_init(in->data.function.params);
|
||||
in->data.function.ret_type = null;
|
||||
in->data.function.ret_type = 0;
|
||||
break;
|
||||
case SCC_IR_TYPE_VOID:
|
||||
case SCC_IR_TYPE_I32:
|
||||
@@ -35,7 +35,7 @@ void scc_ir_func_init(scc_ir_func_t *in, const char *name) {
|
||||
Assert(in != null);
|
||||
Assert(name != null);
|
||||
in->name = name;
|
||||
in->type = null;
|
||||
in->type = 0;
|
||||
scc_vec_init(in->bblocks);
|
||||
scc_vec_init(in->params);
|
||||
}
|
||||
@@ -46,7 +46,7 @@ void scc_ir_node_init(scc_ir_node_t *in, const char *name,
|
||||
in->name = name;
|
||||
in->tag = tag;
|
||||
scc_vec_init(in->used_by);
|
||||
in->type = null;
|
||||
in->type = 0;
|
||||
|
||||
switch (tag) {
|
||||
case SCC_IR_NODE_NULL:
|
||||
@@ -56,38 +56,38 @@ void scc_ir_node_init(scc_ir_node_t *in, const char *name,
|
||||
in->data.const_int.int32 = 0;
|
||||
break;
|
||||
case SCC_IR_NODE_ALLOC:
|
||||
TODO();
|
||||
// TODO();
|
||||
break;
|
||||
case SCC_IR_NODE_LOAD:
|
||||
in->data.load.target = null;
|
||||
in->data.load.target = 0;
|
||||
break;
|
||||
case SCC_IR_NODE_STORE:
|
||||
in->data.store.target = null;
|
||||
in->data.store.value = null;
|
||||
in->data.store.target = 0;
|
||||
in->data.store.value = 0;
|
||||
break;
|
||||
case SCC_IR_NODE_GET_PTR:
|
||||
in->data.get_ptr.src_addr = null;
|
||||
in->data.get_ptr.index = null;
|
||||
in->data.get_ptr.src_addr = 0;
|
||||
in->data.get_ptr.index = 0;
|
||||
break;
|
||||
case SCC_IR_NODE_OP:
|
||||
in->data.op.op = IR_OP_EMPTY;
|
||||
in->data.op.lhs = null;
|
||||
in->data.op.rhs = null;
|
||||
in->data.op.lhs = 0;
|
||||
in->data.op.rhs = 0;
|
||||
break;
|
||||
case SCC_IR_NODE_BRANCH:
|
||||
in->data.branch.cond = null;
|
||||
in->data.branch.true_bblock = null;
|
||||
in->data.branch.false_bblock = null;
|
||||
in->data.branch.cond = 0;
|
||||
in->data.branch.true_bblock = 0;
|
||||
in->data.branch.false_bblock = 0;
|
||||
break;
|
||||
case SCC_IR_NODE_JUMP:
|
||||
in->data.jump.target_bblock = null;
|
||||
in->data.jump.target_bblock = 0;
|
||||
break;
|
||||
case SCC_IR_NODE_CALL:
|
||||
scc_vec_init(in->data.call.args);
|
||||
in->data.call.callee = null;
|
||||
in->data.call.callee = 0;
|
||||
break;
|
||||
case SCC_IR_NODE_RET:
|
||||
in->data.ret.ret_val = null;
|
||||
in->data.ret.ret_val = 0;
|
||||
break;
|
||||
default:
|
||||
UNREACHABLE();
|
||||
@@ -95,34 +95,14 @@ void scc_ir_node_init(scc_ir_node_t *in, const char *name,
|
||||
}
|
||||
}
|
||||
|
||||
void scc_ir_cprog_init(ir_cprog_t *in) {
|
||||
Assert(in != null);
|
||||
scc_vec_init(in->extern_funcs);
|
||||
scc_vec_init(in->funcs);
|
||||
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_type_t *scc_ir_type_alloc(scc_ir_type_tag_t tag) {
|
||||
scc_ir_type_t *ret = scc_malloc(sizeof(scc_ir_type_t));
|
||||
Assert(ret != null);
|
||||
scc_ir_type_init(ret, tag);
|
||||
return ret;
|
||||
}
|
||||
scc_ir_bblock_t *scc_ir_bblock_alloc(const char *label) {
|
||||
scc_ir_bblock_t *ret = scc_malloc(sizeof(scc_ir_bblock_t));
|
||||
Assert(ret != null);
|
||||
scc_ir_bblock_init(ret, label);
|
||||
return ret;
|
||||
}
|
||||
scc_ir_func_t *scc_ir_func_alloc(const char *name) {
|
||||
scc_ir_func_t *ret = scc_malloc(sizeof(scc_ir_func_t));
|
||||
Assert(ret != null);
|
||||
scc_ir_func_init(ret, name);
|
||||
return ret;
|
||||
}
|
||||
scc_ir_node_t *scc_ir_node_alloc(const char *name, scc_ir_node_tag_t tag) {
|
||||
scc_ir_node_t *ret = scc_malloc(sizeof(scc_ir_node_t));
|
||||
Assert(ret != null);
|
||||
scc_ir_node_init(ret, name, tag);
|
||||
return ret;
|
||||
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);
|
||||
}
|
||||
351
libs/ir/src/ir_builder.c
Normal file
351
libs/ir/src/ir_builder.c
Normal file
@@ -0,0 +1,351 @@
|
||||
#include <ir_builder.h>
|
||||
#include <ir_builtin.h>
|
||||
|
||||
void scc_ir_builder_init(scc_ir_builder_t *builder) {
|
||||
builder->current_bblock = SCC_IR_REF_NULL;
|
||||
builder->current_func = SCC_IR_REF_NULL;
|
||||
|
||||
scc_ir_cprog_init(&builder->cprog);
|
||||
scc_ir_ctx_init(&builder->ctx);
|
||||
}
|
||||
|
||||
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_begin_func(scc_ir_builder_t *builder,
|
||||
const char *name,
|
||||
scc_ir_type_ref_t type,
|
||||
const char **param_names) {
|
||||
scc_ir_func_t func = {0};
|
||||
|
||||
// 初始化参数和基本块向量
|
||||
func.name = name;
|
||||
func.type = type;
|
||||
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);
|
||||
builder->current_func = func_ref;
|
||||
|
||||
// 如果提供了参数名称,则创建参数节点
|
||||
if (param_names == null) {
|
||||
return func_ref;
|
||||
}
|
||||
|
||||
scc_ir_func_t *func_ptr = scc_ir_ctx_get_func(&builder->ctx, func_ref);
|
||||
scc_ir_type_t *func_type = scc_ir_ctx_get_type(&builder->ctx, type);
|
||||
|
||||
if (func_type == null || func_type->tag != SCC_IR_TYPE_FUNC) {
|
||||
LOG_ERROR("Invalid function type");
|
||||
return func_ref;
|
||||
}
|
||||
|
||||
scc_vec_foreach(func_type->data.function.params, i) {
|
||||
scc_ir_type_ref_t param_type =
|
||||
scc_vec_at(func_type->data.function.params, i);
|
||||
|
||||
scc_ir_node_t param_node = {0};
|
||||
param_node.tag = SCC_IR_NODE_NULL; // 参数节点标记
|
||||
param_node.type = param_type;
|
||||
param_node.name = param_names[i];
|
||||
scc_vec_init(param_node.used_by);
|
||||
|
||||
scc_ir_node_ref_t param_ref =
|
||||
scc_ir_ctx_new_node(&builder->ctx, ¶m_node);
|
||||
scc_vec_push(func_ptr->params, param_ref);
|
||||
}
|
||||
|
||||
return func_ref;
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
} else {
|
||||
scc_vec_push(builder->cprog.func_defs, builder->current_func);
|
||||
}
|
||||
builder->current_func = 0;
|
||||
}
|
||||
|
||||
scc_ir_func_ref_t scc_ir_builder_current_func(scc_ir_builder_t *builder) {
|
||||
return builder->current_func;
|
||||
}
|
||||
|
||||
scc_ir_bblock_ref_t scc_ir_builder_begin_bblock(scc_ir_builder_t *builder,
|
||||
const char *label) {
|
||||
scc_ir_bblock_t bblock = {0};
|
||||
|
||||
if (label) {
|
||||
bblock.label = label;
|
||||
} else {
|
||||
// TODO 自动生成标签
|
||||
char *auto_label = scc_malloc(sizeof(char) * 64);
|
||||
if (auto_label)
|
||||
LOG_FATAL("allocate memory failed");
|
||||
static int auto_counter = 0;
|
||||
scc_snprintf(auto_label, sizeof(auto_label), ".BB%d", auto_counter++);
|
||||
bblock.label = auto_label;
|
||||
}
|
||||
|
||||
scc_vec_init(bblock.instrs);
|
||||
|
||||
scc_ir_bblock_ref_t bblock_ref =
|
||||
scc_ir_ctx_new_bblock(&builder->ctx, &bblock);
|
||||
builder->current_bblock = bblock_ref;
|
||||
|
||||
// 将基本块添加到当前函数
|
||||
scc_ir_func_t *current_func =
|
||||
scc_ir_ctx_get_func(&builder->ctx, builder->current_func);
|
||||
if (current_func) {
|
||||
scc_vec_push(current_func->bblocks, bblock_ref);
|
||||
}
|
||||
|
||||
return bblock_ref;
|
||||
}
|
||||
|
||||
void scc_ir_builder_end_bblock(scc_ir_builder_t *builder) {
|
||||
builder->current_bblock = 0;
|
||||
}
|
||||
|
||||
void scc_ir_builder_set_current_bblock(scc_ir_builder_t *builder,
|
||||
scc_ir_bblock_ref_t bblock) {
|
||||
builder->current_bblock = bblock;
|
||||
}
|
||||
|
||||
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);
|
||||
if (current_bblock) {
|
||||
scc_vec_push(current_bblock->instrs, node);
|
||||
} else {
|
||||
LOG_ERROR("Current basic block is not set");
|
||||
}
|
||||
}
|
||||
|
||||
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_node_t alloc_node = {0};
|
||||
alloc_node.tag = SCC_IR_NODE_ALLOC;
|
||||
alloc_node.type = scc_ir_ctx_new_type(
|
||||
&builder->ctx,
|
||||
&(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_builder_add_instr(builder, node_ref);
|
||||
|
||||
return node_ref;
|
||||
}
|
||||
|
||||
scc_ir_node_ref_t scc_ir_builder_load(scc_ir_builder_t *builder,
|
||||
scc_ir_node_ref_t ptr) {
|
||||
scc_ir_node_t load_node = {0};
|
||||
load_node.tag = SCC_IR_NODE_LOAD;
|
||||
load_node.data.load.target = ptr;
|
||||
|
||||
// 设置类型为指针指向的类型
|
||||
scc_ir_node_t *ptr_node = scc_ir_ctx_get_node(&builder->ctx, ptr);
|
||||
if (ptr_node) {
|
||||
scc_ir_type_t *ptr_type =
|
||||
scc_ir_ctx_get_type(&builder->ctx, 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_builder_add_instr(builder, node_ref);
|
||||
|
||||
return node_ref;
|
||||
}
|
||||
|
||||
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_node_t store_node = {0};
|
||||
store_node.tag = SCC_IR_NODE_STORE;
|
||||
store_node.data.store.target = ptr;
|
||||
store_node.data.store.value = value;
|
||||
|
||||
scc_ir_node_ref_t node_ref =
|
||||
scc_ir_ctx_new_node(&builder->ctx, &store_node);
|
||||
|
||||
// 添加到当前基本块
|
||||
scc_ir_builder_add_instr(builder, node_ref);
|
||||
|
||||
return node_ref;
|
||||
}
|
||||
|
||||
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_node_t get_ptr_node = {0};
|
||||
get_ptr_node.tag = SCC_IR_NODE_GET_PTR;
|
||||
get_ptr_node.data.get_ptr.src_addr = ptr;
|
||||
get_ptr_node.data.get_ptr.index = index;
|
||||
|
||||
// 类型应与源地址相同(都是指针)
|
||||
scc_ir_node_t *src_node = scc_ir_ctx_get_node(&builder->ctx, ptr);
|
||||
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_builder_add_instr(builder, node_ref);
|
||||
|
||||
return node_ref;
|
||||
}
|
||||
|
||||
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_node_t binop_node = {0};
|
||||
binop_node.tag = SCC_IR_NODE_OP;
|
||||
binop_node.data.op.op = op;
|
||||
binop_node.data.op.lhs = lhs;
|
||||
binop_node.data.op.rhs = rhs;
|
||||
|
||||
// 类型通常与操作数相同(对于算术运算)
|
||||
scc_ir_node_t *lhs_node = scc_ir_ctx_get_node(&builder->ctx, 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_builder_add_instr(builder, node_ref);
|
||||
|
||||
return node_ref;
|
||||
}
|
||||
|
||||
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_node_t cmp_node = {0};
|
||||
cmp_node.tag = SCC_IR_NODE_OP;
|
||||
cmp_node.data.op.op = op;
|
||||
cmp_node.data.op.lhs = lhs;
|
||||
cmp_node.data.op.rhs = rhs;
|
||||
|
||||
// 比较操作的结果通常是布尔值
|
||||
cmp_node.type = scc_ir_ctx_get_builtin_i32(&builder->ctx);
|
||||
|
||||
scc_ir_node_ref_t node_ref = scc_ir_ctx_new_node(&builder->ctx, &cmp_node);
|
||||
|
||||
// 添加到当前基本块
|
||||
scc_ir_builder_add_instr(builder, node_ref);
|
||||
|
||||
return node_ref;
|
||||
}
|
||||
|
||||
scc_ir_node_ref_t scc_ir_builder_jump(scc_ir_builder_t *builder,
|
||||
scc_ir_bblock_ref_t target) {
|
||||
scc_ir_node_t jump_node = {0};
|
||||
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_builder_add_instr(builder, node_ref);
|
||||
|
||||
return node_ref;
|
||||
}
|
||||
|
||||
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_node_t branch_node = {0};
|
||||
branch_node.tag = SCC_IR_NODE_BRANCH;
|
||||
branch_node.data.branch.cond = cond;
|
||||
branch_node.data.branch.true_bblock = true_target;
|
||||
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_builder_add_instr(builder, node_ref);
|
||||
|
||||
return node_ref;
|
||||
}
|
||||
|
||||
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_node_t call_node = {0};
|
||||
call_node.tag = SCC_IR_NODE_CALL;
|
||||
call_node.data.call.callee = callee;
|
||||
|
||||
scc_vec_init(call_node.data.call.args);
|
||||
for (usize i = 0; i < arg_count; i++) {
|
||||
scc_vec_push(call_node.data.call.args, args[i]);
|
||||
}
|
||||
|
||||
// 设置返回类型为被调用函数的返回类型
|
||||
scc_ir_func_t *callee_func = scc_ir_ctx_get_func(&builder->ctx, callee);
|
||||
if (callee_func) {
|
||||
scc_ir_type_t *func_type =
|
||||
scc_ir_ctx_get_type(&builder->ctx, 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_builder_add_instr(builder, node_ref);
|
||||
|
||||
return node_ref;
|
||||
}
|
||||
|
||||
scc_ir_node_ref_t scc_ir_builder_ret(scc_ir_builder_t *builder,
|
||||
scc_ir_node_ref_t value) {
|
||||
scc_ir_node_t ret_node = {0};
|
||||
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_builder_add_instr(builder, node_ref);
|
||||
|
||||
return node_ref;
|
||||
}
|
||||
|
||||
scc_ir_node_ref_t scc_ir_builder_ret_void(scc_ir_builder_t *builder) {
|
||||
scc_ir_node_t ret_node = {0};
|
||||
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_builder_add_instr(builder, node_ref);
|
||||
return node_ref;
|
||||
}
|
||||
345
libs/ir/src/ir_ctx.c
Normal file
345
libs/ir/src/ir_ctx.c
Normal file
@@ -0,0 +1,345 @@
|
||||
#include "ir_ctx.h"
|
||||
#include "ir_builtin.h"
|
||||
|
||||
/**
|
||||
* @brief 哈希混合函数(类似Rust的hash combine)
|
||||
*
|
||||
* Rust的默认哈希实现(SipHash 1-3)使用旋转和异或,
|
||||
* 这里使用简单的FNV-1a变体,类似于Rust的`#[derive(Hash)]`
|
||||
*/
|
||||
static inline u32 scc_hash_mix(u32 seed, u32 value) {
|
||||
// FNV-1a风格混合
|
||||
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) {
|
||||
// 初始哈希:tag
|
||||
u32 hash = (u32)key->tag;
|
||||
|
||||
switch (key->tag) {
|
||||
case SCC_IR_TYPE_VOID:
|
||||
case SCC_IR_TYPE_I1:
|
||||
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;
|
||||
|
||||
case SCC_IR_TYPE_PTR:
|
||||
hash = scc_hash_mix(hash, (u32)key->data.array.base);
|
||||
break;
|
||||
|
||||
case SCC_IR_TYPE_ARRAY:
|
||||
hash = scc_hash_mix(hash, (u32)key->data.array.base);
|
||||
hash = scc_hash_mix(hash, (u32)key->data.array.len);
|
||||
break;
|
||||
|
||||
case SCC_IR_TYPE_FUNC:
|
||||
// 注意:这里需要递归哈希参数类型
|
||||
hash = scc_hash_mix(hash, (u32)key->data.function.ret_type);
|
||||
for (usize i = 0; i < key->data.function.params.size; i++) {
|
||||
hash = scc_hash_mix(hash, (u32)key->data.function.params.data[i]);
|
||||
}
|
||||
hash = scc_hash_mix(hash, (u32)key->data.function.params.size);
|
||||
break;
|
||||
|
||||
case SCC_IR_TYPE_STRUCT:
|
||||
case SCC_IR_TYPE_VECTOR:
|
||||
default:
|
||||
UNREACHABLE();
|
||||
return 0;
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
static int cmp_type(const scc_ir_type_t *key1, const scc_ir_type_t *key2) {
|
||||
// tag不同
|
||||
if (key1->tag != key2->tag) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
switch (key1->tag) {
|
||||
case SCC_IR_TYPE_VOID:
|
||||
case SCC_IR_TYPE_I1:
|
||||
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;
|
||||
|
||||
case SCC_IR_TYPE_ARRAY:
|
||||
return (key1->data.array.base != key2->data.array.base) ||
|
||||
(key1->data.array.len != key2->data.array.len);
|
||||
|
||||
case SCC_IR_TYPE_FUNC: {
|
||||
if (key1->data.function.ret_type != key2->data.function.ret_type) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (key1->data.function.params.size !=
|
||||
key2->data.function.params.size) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (usize i = 0; i < key1->data.function.params.size; i++) {
|
||||
if (key1->data.function.params.data[i] !=
|
||||
key2->data.function.params.data[i]) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
default:
|
||||
UNREACHABLE();
|
||||
return 1;
|
||||
}
|
||||
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);
|
||||
|
||||
// 设置哈希函数
|
||||
ctx->uid2nodes.hash_func = hash_key;
|
||||
ctx->uid2nodes.key_cmp = cmp_key;
|
||||
ctx->uid2types.hash_func = hash_key;
|
||||
ctx->uid2types.key_cmp = cmp_key;
|
||||
ctx->uid2bblocks.hash_func = hash_key;
|
||||
ctx->uid2bblocks.key_cmp = cmp_key;
|
||||
ctx->uid2funcs.hash_func = hash_key;
|
||||
ctx->uid2funcs.key_cmp = cmp_key;
|
||||
// 初始化哈希表
|
||||
scc_hashtable_init(&ctx->uid2nodes);
|
||||
scc_hashtable_init(&ctx->uid2types);
|
||||
scc_hashtable_init(&ctx->uid2bblocks);
|
||||
scc_hashtable_init(&ctx->uid2funcs);
|
||||
|
||||
ctx->type_uniquing.hash_func = (void *)hash_type;
|
||||
ctx->type_uniquing.key_cmp = (void *)cmp_type;
|
||||
scc_hashtable_init(&ctx->type_uniquing);
|
||||
|
||||
// 预留UID 0 作为无效引用
|
||||
ctx->node_uid = 1;
|
||||
ctx->type_uid = 1;
|
||||
ctx->bblock_uid = 1;
|
||||
ctx->func_uid = 1;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// 创建新类型
|
||||
unsigned new_uid = ctx->type_uid++;
|
||||
scc_vec_push(ctx->types, *type);
|
||||
|
||||
// 添加到UID映射
|
||||
usize idx = ctx->types.size - 1;
|
||||
scc_hashtable_set(&ctx->uid2types, &new_uid, (void *)idx);
|
||||
|
||||
// 添加到去重表
|
||||
scc_hashtable_set(&ctx->type_uniquing,
|
||||
&ctx->types.data[idx], // 使用向量中的地址作为键
|
||||
(void *)(uintptr_t)new_uid);
|
||||
|
||||
return new_uid;
|
||||
}
|
||||
|
||||
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) {
|
||||
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) {
|
||||
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 = "";
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -1,13 +1,522 @@
|
||||
#include <ir_ctx.h>
|
||||
#include <ir_dump.h>
|
||||
|
||||
void scc_ir_type_dump(scc_ir_dump_ctx_t *ctx, const scc_ir_type_t *in) {
|
||||
return;
|
||||
#define PRINT_VALUE(ctx, fmt, value) \
|
||||
SCC_TREE_DUMP_PRINT_PURE(ctx, ctx->value_color, fmt, value)
|
||||
|
||||
#define PRINT_NODE(ctx, name) \
|
||||
SCC_TREE_DUMP_PRINT_PURE(ctx, ctx->node_color, "%s", name)
|
||||
|
||||
#define PRINT_QUOTED_VALUE(ctx, str) \
|
||||
SCC_TREE_DUMP_PRINT_AROUND(ctx, ctx->value_color, "'", "%s", str)
|
||||
|
||||
// 获取IR节点类型的字符串表示
|
||||
static const char *get_node_type_str(scc_ir_node_tag_t tag) {
|
||||
static const char *node_types[] = {
|
||||
[SCC_IR_NODE_NULL] = "Null", [SCC_IR_NODE_CONST_INT] = "ConstInt",
|
||||
[SCC_IR_NODE_ALLOC] = "Alloc", [SCC_IR_NODE_LOAD] = "Load",
|
||||
[SCC_IR_NODE_STORE] = "Store", [SCC_IR_NODE_GET_PTR] = "GetElementPtr",
|
||||
[SCC_IR_NODE_OP] = "Op", [SCC_IR_NODE_BRANCH] = "Branch",
|
||||
[SCC_IR_NODE_JUMP] = "Jump", [SCC_IR_NODE_CALL] = "Call",
|
||||
[SCC_IR_NODE_RET] = "Return",
|
||||
};
|
||||
|
||||
if (tag >= 0 && tag < sizeof(node_types) / sizeof(node_types[0]) &&
|
||||
node_types[tag] != NULL) {
|
||||
return node_types[tag];
|
||||
}
|
||||
return "UnknownIRNode";
|
||||
}
|
||||
void scc_ir_bblock_dump(scc_ir_dump_ctx_t *ctx, const scc_ir_bblock_t *in) {
|
||||
return;
|
||||
|
||||
// 获取操作符字符串
|
||||
static const char *get_op_str(scc_ir_op_type_t op) {
|
||||
static const char *ops[] = {
|
||||
[IR_OP_EMPTY] = "empty", [IR_OP_NEQ] = "!=", [IR_OP_EQ] = "==",
|
||||
[IR_OP_GT] = ">", [IR_OP_LT] = "<", [IR_OP_GE] = ">=",
|
||||
[IR_OP_LE] = "<=", [IR_OP_ADD] = "+", [IR_OP_SUB] = "-",
|
||||
[IR_OP_MUL] = "*", [IR_OP_DIV] = "/", [IR_OP_MOD] = "%",
|
||||
[IR_OP_AND] = "&", [IR_OP_OR] = "|", [IR_OP_XOR] = "^",
|
||||
[IR_OP_NOT] = "~", [IR_OP_SHL] = "<<", [IR_OP_SHR] = ">>",
|
||||
[IR_OP_SAR] = ">>a", // Arithmetic shift right
|
||||
};
|
||||
|
||||
if (op >= 0 && op < sizeof(ops) / sizeof(ops[0]) && ops[op] != NULL) {
|
||||
return ops[op];
|
||||
}
|
||||
return "<unknown_op>";
|
||||
}
|
||||
void scc_ir_func_dump(scc_ir_dump_ctx_t *ctx, const scc_ir_func_t *in) {
|
||||
return;
|
||||
|
||||
// 获取类型标签字符串
|
||||
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_I1] = "i1",
|
||||
[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",
|
||||
};
|
||||
|
||||
if (tag >= 0 && tag < sizeof(type_tags) / sizeof(type_tags[0]) &&
|
||||
type_tags[tag] != NULL) {
|
||||
return type_tags[tag];
|
||||
}
|
||||
return "<unknown_type>";
|
||||
}
|
||||
|
||||
// 递归转储辅助函数(使用引用)
|
||||
static inline void dump_child_node_ref(scc_ir_dump_ctx_t *ctx,
|
||||
scc_ir_node_ref_t child_ref,
|
||||
cbool is_last) {
|
||||
if (!child_ref)
|
||||
return;
|
||||
scc_tree_dump_push_level(ctx->dump_ctx, is_last);
|
||||
scc_ir_dump_node(ctx, child_ref);
|
||||
scc_tree_dump_pop_level(ctx->dump_ctx);
|
||||
}
|
||||
|
||||
// 转储常量整数节点
|
||||
static void dump_const_int_node(scc_ir_dump_ctx_t *ctx,
|
||||
const scc_ir_node_t *node) {
|
||||
scc_tree_dump_push_level(ctx->dump_ctx, true);
|
||||
scc_tree_print_indent(ctx->dump_ctx);
|
||||
scc_printf("%d\n", node->data.const_int.int32);
|
||||
scc_tree_dump_pop_level(ctx->dump_ctx);
|
||||
}
|
||||
|
||||
// 转储操作节点
|
||||
static void dump_op_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) {
|
||||
// 打印操作符
|
||||
scc_tree_dump_push_level(ctx->dump_ctx, false);
|
||||
scc_tree_print_indent(ctx->dump_ctx);
|
||||
SCC_TREE_DUMP_PRINT_PURE(ctx->dump_ctx, ctx->dump_ctx->node_color,
|
||||
"op: %s\n", get_op_str(node->data.op.op));
|
||||
scc_tree_dump_pop_level(ctx->dump_ctx);
|
||||
|
||||
// 左操作数
|
||||
if (node->data.op.lhs) {
|
||||
dump_child_node_ref(ctx, node->data.op.lhs,
|
||||
node->data.op.rhs ? false : true);
|
||||
}
|
||||
|
||||
// 右操作数
|
||||
if (node->data.op.rhs) {
|
||||
dump_child_node_ref(ctx, node->data.op.rhs, true);
|
||||
}
|
||||
}
|
||||
|
||||
// 转储加载节点
|
||||
static void dump_load_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) {
|
||||
// PRINT_NODE(ctx->dump_ctx, "load");
|
||||
if (node->data.load.target) {
|
||||
dump_child_node_ref(ctx, node->data.load.target, true);
|
||||
}
|
||||
}
|
||||
|
||||
// 转储存储节点
|
||||
static void dump_store_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) {
|
||||
// PRINT_NODE(ctx->dump_ctx, "store");
|
||||
|
||||
// 输出存储位置
|
||||
if (node->data.store.target) {
|
||||
dump_child_node_ref(ctx, node->data.store.target, false);
|
||||
}
|
||||
|
||||
// 输出存储的值
|
||||
if (node->data.store.value) {
|
||||
dump_child_node_ref(ctx, node->data.store.value, true);
|
||||
}
|
||||
}
|
||||
|
||||
// 转储获取指针节点
|
||||
static void dump_get_ptr_node(scc_ir_dump_ctx_t *ctx,
|
||||
const scc_ir_node_t *node) {
|
||||
// PRINT_NODE(ctx->dump_ctx, "get_ptr");
|
||||
// 输出源地址
|
||||
if (node->data.get_ptr.src_addr) {
|
||||
dump_child_node_ref(ctx, node->data.get_ptr.src_addr, false);
|
||||
}
|
||||
|
||||
// 输出索引
|
||||
if (node->data.get_ptr.index) {
|
||||
dump_child_node_ref(ctx, node->data.get_ptr.index, true);
|
||||
}
|
||||
}
|
||||
|
||||
// 转储分支节点
|
||||
static void dump_branch_node(scc_ir_dump_ctx_t *ctx,
|
||||
const scc_ir_node_t *node) {
|
||||
// PRINT_NODE(ctx->dump_ctx, "branch");
|
||||
|
||||
// 输出条件
|
||||
if (node->data.branch.cond) {
|
||||
dump_child_node_ref(ctx, node->data.branch.cond, false);
|
||||
}
|
||||
|
||||
// 输出真分支块
|
||||
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);
|
||||
if (true_bblock) {
|
||||
scc_tree_print_indent(ctx->dump_ctx);
|
||||
PRINT_NODE(ctx->dump_ctx, "TrueBlock: ");
|
||||
PRINT_QUOTED_VALUE(ctx->dump_ctx, true_bblock->label
|
||||
? true_bblock->label
|
||||
: "<unnamed>");
|
||||
}
|
||||
}
|
||||
|
||||
// 输出假分支块
|
||||
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);
|
||||
if (false_bblock) {
|
||||
scc_tree_print_indent(ctx->dump_ctx);
|
||||
PRINT_NODE(ctx->dump_ctx, "FalseBlock: ");
|
||||
PRINT_QUOTED_VALUE(ctx->dump_ctx, false_bblock->label
|
||||
? false_bblock->label
|
||||
: "<unnamed>");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 转储跳转节点
|
||||
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);
|
||||
if (target_bblock) {
|
||||
scc_printf("to '%s'", target_bblock->label ? target_bblock->label
|
||||
: "<unnamed>");
|
||||
} else {
|
||||
scc_printf("to invalid block");
|
||||
}
|
||||
} else {
|
||||
scc_printf("to NULL");
|
||||
}
|
||||
}
|
||||
|
||||
// 转储调用节点
|
||||
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);
|
||||
if (callee) {
|
||||
scc_printf("func='%s'", callee->name ? callee->name : "<unnamed>");
|
||||
} else {
|
||||
scc_printf("func=<invalid>");
|
||||
}
|
||||
} else {
|
||||
scc_printf("func=NULL");
|
||||
}
|
||||
|
||||
if (scc_vec_size(node->data.call.args) > 0) {
|
||||
scc_printf("\n");
|
||||
}
|
||||
|
||||
// 输出参数
|
||||
for (usize i = 0; i < scc_vec_size(node->data.call.args); i++) {
|
||||
cbool is_last = (i + 1 == scc_vec_size(node->data.call.args));
|
||||
scc_tree_dump_push_level(ctx->dump_ctx, is_last);
|
||||
|
||||
scc_ir_node_ref_t arg_ref = scc_vec_at(node->data.call.args, i);
|
||||
dump_child_node_ref(ctx, arg_ref, is_last);
|
||||
|
||||
scc_tree_dump_pop_level(ctx->dump_ctx);
|
||||
}
|
||||
}
|
||||
|
||||
// 转储返回节点
|
||||
static void dump_ret_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) {
|
||||
// PRINT_NODE(ctx->dump_ctx, "ret");
|
||||
if (node->data.ret.ret_val) {
|
||||
dump_child_node_ref(ctx, node->data.ret.ret_val, true);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
if (!node) {
|
||||
LOG_ERROR("Invalid node ref");
|
||||
return;
|
||||
}
|
||||
|
||||
scc_tree_print_indent(ctx->dump_ctx);
|
||||
PRINT_NODE(ctx->dump_ctx, get_node_type_str(node->tag));
|
||||
|
||||
if (node->name && node->name[0]) {
|
||||
PRINT_VALUE(ctx->dump_ctx, " [%s]", node->name);
|
||||
}
|
||||
|
||||
if (node->type) {
|
||||
scc_printf(" : ");
|
||||
scc_ir_type_t *type = scc_ir_ctx_get_type(ctx->ir_ctx, node->type);
|
||||
if (type) {
|
||||
PRINT_VALUE(ctx->dump_ctx, "%s", get_type_tag_str(type->tag));
|
||||
}
|
||||
}
|
||||
|
||||
scc_printf("\n");
|
||||
|
||||
// 根据节点类型输出特定信息
|
||||
switch (node->tag) {
|
||||
case SCC_IR_NODE_NULL:
|
||||
break;
|
||||
case SCC_IR_NODE_CONST_INT:
|
||||
dump_const_int_node(ctx, node);
|
||||
break;
|
||||
case SCC_IR_NODE_ALLOC:
|
||||
break;
|
||||
case SCC_IR_NODE_LOAD:
|
||||
dump_load_node(ctx, node);
|
||||
break;
|
||||
case SCC_IR_NODE_STORE:
|
||||
dump_store_node(ctx, node);
|
||||
break;
|
||||
case SCC_IR_NODE_GET_PTR:
|
||||
dump_get_ptr_node(ctx, node);
|
||||
break;
|
||||
case SCC_IR_NODE_OP:
|
||||
dump_op_node(ctx, node);
|
||||
break;
|
||||
case SCC_IR_NODE_BRANCH:
|
||||
dump_branch_node(ctx, node);
|
||||
break;
|
||||
case SCC_IR_NODE_JUMP:
|
||||
dump_jump_node(ctx, node);
|
||||
break;
|
||||
case SCC_IR_NODE_CALL:
|
||||
dump_call_node(ctx, node);
|
||||
break;
|
||||
case SCC_IR_NODE_RET:
|
||||
dump_ret_node(ctx, node);
|
||||
break;
|
||||
default:
|
||||
PRINT_QUOTED_VALUE(ctx->dump_ctx, "unknown");
|
||||
scc_printf("tag(%d)", node->tag);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 转储类型信息
|
||||
void scc_ir_dump_type(scc_ir_dump_ctx_t *ctx, scc_ir_type_ref_t type_ref) {
|
||||
// impl fmt::Display for TypeKind {
|
||||
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
// match self {
|
||||
// TypeKind::Int32 => write!(f, "i32"),
|
||||
// TypeKind::Unit => write!(f, "unit"),
|
||||
// TypeKind::Array(t, len) => write!(f, "[{}, {}]", t, len),
|
||||
// TypeKind::Pointer(t) => write!(f, "*{}", t),
|
||||
// TypeKind::Function(params, ret) => {
|
||||
// write!(f, "(")?;
|
||||
// let mut first = true;
|
||||
// for param in params {
|
||||
// if !first {
|
||||
// write!(f, ", ")?;
|
||||
// }
|
||||
// write!(f, "{}", param)?;
|
||||
// first = false;
|
||||
// }
|
||||
// if !ret.is_unit() {
|
||||
// write!(f, "): {}", ret)
|
||||
// } else {
|
||||
// write!(f, ")")
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
if (!ctx || !type_ref) {
|
||||
LOG_ERROR("invalid parameter");
|
||||
return;
|
||||
}
|
||||
|
||||
scc_ir_type_t *type = scc_ir_ctx_get_type(ctx->ir_ctx, type_ref);
|
||||
if (!type) {
|
||||
LOG_ERROR("invalid type ref");
|
||||
return;
|
||||
}
|
||||
scc_tree_print_indent(ctx->dump_ctx);
|
||||
PRINT_NODE(ctx->dump_ctx, "Type: ");
|
||||
PRINT_QUOTED_VALUE(ctx->dump_ctx, get_type_tag_str(type->tag));
|
||||
scc_printf("\n");
|
||||
|
||||
// 递归转储子类型
|
||||
switch (type->tag) {
|
||||
case SCC_IR_TYPE_PTR:
|
||||
if (type->data.pointer.base) {
|
||||
scc_tree_dump_push_level(ctx->dump_ctx, true);
|
||||
scc_ir_dump_type(ctx, type->data.pointer.base);
|
||||
scc_tree_dump_pop_level(ctx->dump_ctx);
|
||||
}
|
||||
break;
|
||||
case SCC_IR_TYPE_ARRAY:
|
||||
if (type->data.array.len > 0) {
|
||||
scc_tree_print_indent(ctx->dump_ctx);
|
||||
SCC_TREE_DUMP_PRINT_PURE(ctx->dump_ctx, ctx->dump_ctx->node_color,
|
||||
"Array Length: %zu\n",
|
||||
type->data.array.len);
|
||||
}
|
||||
if (type->data.array.base) {
|
||||
scc_tree_dump_push_level(ctx->dump_ctx, true);
|
||||
scc_ir_dump_type(ctx, type->data.array.base);
|
||||
scc_tree_dump_pop_level(ctx->dump_ctx);
|
||||
}
|
||||
break;
|
||||
case SCC_IR_TYPE_FUNC:
|
||||
// 先输出参数类型
|
||||
for (usize i = 0; i < scc_vec_size(type->data.function.params); i++) {
|
||||
cbool is_last = (i + 1 == scc_vec_size(type->data.function.params));
|
||||
scc_tree_dump_push_level(ctx->dump_ctx, is_last);
|
||||
|
||||
scc_ir_type_ref_t param_type_ref =
|
||||
scc_vec_at(type->data.function.params, i);
|
||||
scc_ir_dump_type(ctx, param_type_ref);
|
||||
|
||||
scc_tree_dump_pop_level(ctx->dump_ctx);
|
||||
}
|
||||
|
||||
// 再输出返回类型
|
||||
if (type->data.function.ret_type) {
|
||||
scc_tree_dump_push_level(ctx->dump_ctx, true);
|
||||
scc_ir_dump_type(ctx, type->data.function.ret_type);
|
||||
scc_tree_dump_pop_level(ctx->dump_ctx);
|
||||
}
|
||||
break;
|
||||
case SCC_IR_TYPE_STRUCT:
|
||||
// 结构体处理
|
||||
break;
|
||||
case SCC_IR_TYPE_VECTOR:
|
||||
// 向量处理
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 转储基本块
|
||||
void scc_ir_dump_bblock(scc_ir_dump_ctx_t *ctx,
|
||||
scc_ir_bblock_ref_t bblock_ref) {
|
||||
if (!ctx || !bblock_ref) {
|
||||
LOG_ERROR("invalid parameter");
|
||||
return;
|
||||
}
|
||||
|
||||
scc_ir_bblock_t *bblock = scc_ir_ctx_get_bblock(ctx->ir_ctx, bblock_ref);
|
||||
if (!bblock) {
|
||||
LOG_ERROR("invalid bblock ref");
|
||||
return;
|
||||
}
|
||||
|
||||
scc_tree_print_indent(ctx->dump_ctx);
|
||||
PRINT_NODE(ctx->dump_ctx, "BasicBlock: ");
|
||||
PRINT_QUOTED_VALUE(ctx->dump_ctx,
|
||||
bblock->label ? bblock->label : "<unnamed>");
|
||||
scc_printf("\n");
|
||||
|
||||
// 转储基本块中的指令
|
||||
for (usize i = 0; i < scc_vec_size(bblock->instrs); i++) {
|
||||
cbool is_last = (i + 1 == scc_vec_size(bblock->instrs));
|
||||
scc_tree_dump_push_level(ctx->dump_ctx, is_last);
|
||||
|
||||
scc_ir_node_ref_t instr_ref = scc_vec_at(bblock->instrs, i);
|
||||
scc_ir_dump_node(ctx, instr_ref);
|
||||
scc_tree_dump_pop_level(ctx->dump_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);
|
||||
if (!ctx || !func) {
|
||||
LOG_ERROR("invalid parameter");
|
||||
return;
|
||||
}
|
||||
|
||||
scc_tree_print_indent(ctx->dump_ctx);
|
||||
PRINT_NODE(ctx->dump_ctx, "Function: ");
|
||||
PRINT_QUOTED_VALUE(ctx->dump_ctx, func->name ? func->name : "<unnamed>");
|
||||
scc_printf("\n");
|
||||
|
||||
// 输出函数类型
|
||||
if (func->type) {
|
||||
scc_tree_dump_push_level(ctx->dump_ctx, false);
|
||||
scc_ir_dump_type(ctx, func->type);
|
||||
scc_tree_dump_pop_level(ctx->dump_ctx);
|
||||
}
|
||||
|
||||
// 输出函数参数
|
||||
for (usize i = 0; i < scc_vec_size(func->params); i++) {
|
||||
cbool is_last = (i + 1 == scc_vec_size(func->params));
|
||||
scc_tree_dump_push_level(ctx->dump_ctx, is_last);
|
||||
|
||||
scc_ir_node_ref_t param_ref = scc_vec_at(func->params, i);
|
||||
scc_ir_dump_node(ctx, param_ref);
|
||||
scc_tree_dump_pop_level(ctx->dump_ctx);
|
||||
}
|
||||
|
||||
// 输出基本块
|
||||
for (usize i = 0; i < scc_vec_size(func->bblocks); i++) {
|
||||
cbool is_last = (i + 1 == scc_vec_size(func->bblocks));
|
||||
scc_tree_dump_push_level(ctx->dump_ctx, is_last);
|
||||
|
||||
scc_ir_bblock_ref_t bblock_ref = scc_vec_at(func->bblocks, i);
|
||||
scc_ir_dump_bblock(ctx, bblock_ref);
|
||||
|
||||
scc_tree_dump_pop_level(ctx->dump_ctx);
|
||||
}
|
||||
}
|
||||
|
||||
// 转储整个程序
|
||||
void scc_ir_dump_cprog(scc_ir_dump_ctx_t *ctx) {
|
||||
// 输出全局值
|
||||
|
||||
// if (scc_vec_size(program->global_vals) > 0) {
|
||||
// print_indented(ctx, "Global Values:", "");
|
||||
|
||||
// for (usize i = 0; i < scc_vec_size(program->global_vals); i++) {
|
||||
// cbool is_last = (i + 1 ==
|
||||
// scc_vec_size(program->global_vals));
|
||||
// scc_tree_dump_push_level(ctx->dump_ctx, is_last);
|
||||
|
||||
// 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);
|
||||
// if (global_node) {
|
||||
// scc_ir_dump_node(ctx, ir_ctx, global_node);
|
||||
// }
|
||||
|
||||
// scc_tree_dump_pop_level(ctx->dump_ctx);
|
||||
// }
|
||||
// }
|
||||
|
||||
// // 输出函数定义
|
||||
// if (scc_vec_size(ir_ctx->funcs) > 0) {
|
||||
// print_indented(ctx, "Functions:", "");
|
||||
|
||||
// for (usize i = 0; i < scc_vec_size(ir_ctx->funcs); i++) {
|
||||
// cbool is_last = (i + 1 == scc_vec_size(ir_ctx->funcs));
|
||||
// scc_tree_dump_push_level(ctx->dump_ctx, is_last);
|
||||
|
||||
// scc_ir_func_t func_ref = scc_vec_at(ir_ctx->funcs, i);
|
||||
// if (func_ref.type != 0) {
|
||||
// // TODO hack it
|
||||
// dump_func_ref(ir_ctx, ctx, i + 1);
|
||||
// }
|
||||
|
||||
// scc_tree_dump_pop_level(ctx->dump_ctx);
|
||||
// }
|
||||
// }
|
||||
PRINT_NODE(ctx->dump_ctx, "Func defs:\n");
|
||||
scc_vec_foreach(ctx->cprog->func_defs, i) {
|
||||
cbool is_last = (i + 1 == scc_vec_size(ctx->cprog->func_defs));
|
||||
scc_tree_dump_push_level(ctx->dump_ctx, is_last);
|
||||
scc_ir_dump_func(ctx, scc_vec_at(ctx->cprog->func_defs, i));
|
||||
scc_tree_dump_pop_level(ctx->dump_ctx);
|
||||
}
|
||||
}
|
||||
void scc_ir_node_dump(scc_ir_dump_ctx_t *ctx, scc_ir_node_t *in) { return; }
|
||||
void scc_ir_cprog_dump(scc_ir_dump_ctx_t *ctx, ir_cprog_t *in) { return; }
|
||||
Reference in New Issue
Block a user