- 重构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
201 lines
5.9 KiB
C
201 lines
5.9 KiB
C
#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__ */
|