- 将scc_ast_ctx重命名为scc_ast_module以更好地反映其功能 - 添加scc_strpool_t用于统一管理AST节点名称字符串的生命周期 - 实现scc_ast_module_intern函数用于字符串驻留 - 更新所有相关的初始化、销毁和访问函数命名 - 修改内存分配宏以使用新的模块结构 refactor(parser): 更新解析器以使用AST模块和字符串池 - 将解析器中的ast_ctx字段替换为ast_module - 在创建AST节点时使用新的ast_module参数 - 使用scc_ast_module_intern函数处理标识符和字符串字面量 - 确保所有字符串都被正确驻留到模块的字符串池中 refactor(sema): 更新语义分析器使用AST模块 - 将sema_ctx中的ast_ctx字段替换为ast_module - 更新语义分析器初始化函数参数 refactor(ast2ir): 更新AST到IR转换器使用AST模块 - 将ast_ctx字段替换为ast_module - 更新上下文初始化函数参数和实现 fix(cfg): 修复CFG模块中的符号查找错误 - 修正scc_cfg_module_unsafe_get_symbol函数中的边界检查条件 perf(ir): 完善各IR层模块的内存清理机制 - 为HIR模块添加函数和基本块元数据的释放逻辑 - 为MIR和LIR模块完善完整的资源清理和内存释放 - 确保所有分配的元数据结构都能被正确释放 chore(deps): 添加scc_utils依赖到AST库 - 在libs/ast/cbuild.toml中添加对scc_utils的依赖
94 lines
3.7 KiB
C
94 lines
3.7 KiB
C
#ifndef __SCC_AST2IR_H__
|
||
#define __SCC_AST2IR_H__
|
||
|
||
#include <scc_ast.h>
|
||
#include <scc_hir_builder.h>
|
||
#include <scc_type_abi.h>
|
||
|
||
typedef struct {
|
||
scc_hir_builder_t builder;
|
||
scc_hashtable_t ast2ir_cache; ///< ast node to ir ref cache
|
||
scc_hashtable_t break_cache; ///< break cache
|
||
scc_hashtable_t continue_cache; ///< continue cache
|
||
scc_hashtable_t symtab; ///< symbol to ir_ref
|
||
scc_hashtable_t type_cache; ///< scc_ast_canon_type_t* -> scc_hir_type_ref_t
|
||
// scc_strpool_t strpool; ///< string pool
|
||
const scc_abi_type_calc_t *abi;
|
||
cbool hint_using_value; // 转换时尽可能使用value而不是alloc
|
||
|
||
scc_ast_module_t *ast_module;
|
||
} scc_ast2ir_ctx_t;
|
||
|
||
static inline scc_hir_module_t *scc_ast2ir_mir_module(scc_ast2ir_ctx_t *ctx) {
|
||
return &ctx->builder.cprog->module;
|
||
}
|
||
|
||
void scc_ast2ir_ctx_init(scc_ast2ir_ctx_t *ctx, const scc_abi_type_calc_t *abi,
|
||
scc_ast_module_t *ast_module, scc_hir_cprog_t *cprog);
|
||
void scc_ast2ir_ctx_drop(scc_ast2ir_ctx_t *ctx);
|
||
|
||
void scc_ast2ir_run(scc_ast2ir_ctx_t *ctx,
|
||
const scc_ast_translation_unit_t *tu);
|
||
void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, const scc_ast_decl_t *decl,
|
||
cbool is_global);
|
||
scc_hir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx,
|
||
const scc_ast_expr_t *expr,
|
||
cbool is_lvalue);
|
||
void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, const scc_ast_stmt_t *stmt);
|
||
scc_hir_type_ref_t scc_ast2ir_type(scc_ast2ir_ctx_t *ctx,
|
||
const scc_ast_qual_type_t *ast_type);
|
||
|
||
scc_hir_type_ref_t
|
||
scc_ast2ir_parse_base_type(scc_ast2ir_ctx_t *ctx,
|
||
const scc_ast_qual_type_t *ast_type);
|
||
|
||
// ====== 类型提升(Type Promotion)接口 ======
|
||
|
||
/**
|
||
* @brief 整数提升(C11 6.3.1.1)
|
||
* 对 _Bool、char、short 等秩低于 int 的类型提升为 int/unsigned int
|
||
* @param type_ref 原始类型引用
|
||
* @return 提升后的类型的引用,若无需提升则返回原始类型
|
||
*/
|
||
scc_hir_type_ref_t scc_ast2ir_integer_promotion(scc_ast2ir_ctx_t *ctx,
|
||
scc_hir_type_ref_t type_ref);
|
||
|
||
/**
|
||
* @brief 寻常算术转换(C11 6.3.1.8)
|
||
* 对二元算术操作的两个操作数类型找到公共类型
|
||
* @return 公共类型的引用
|
||
*/
|
||
scc_hir_type_ref_t
|
||
scc_ast2ir_usual_arithmetic_conversion(scc_ast2ir_ctx_t *ctx,
|
||
scc_hir_type_ref_t t1_ref,
|
||
scc_hir_type_ref_t t2_ref);
|
||
|
||
/**
|
||
* @brief 插入类型转换指令
|
||
* 根据源类型和目标类型自动选择 SEXT/ZEXT/TRUNC/F2I/I2F/F2F
|
||
* @return 转换后的值引用
|
||
*/
|
||
scc_hir_value_ref_t scc_ast2ir_emit_conversion(scc_ast2ir_ctx_t *ctx,
|
||
scc_hir_value_ref_t value,
|
||
scc_hir_type_ref_t target_type);
|
||
|
||
// ====== 常量表达式求值 ======
|
||
|
||
/**
|
||
* @brief 对整数常量表达式求值(C11 6.6)
|
||
*
|
||
* 递归计算 AST 表达式,得到一个编译期可确定的整数值。
|
||
* 支持:整数字面量、字符字面量、枚举常量、sizeof / _Alignof、
|
||
* 一元运算(+ - ~ !)、二元运算(算术/位/移位/关系/逻辑)、
|
||
* 条件表达式(?:)、强制类型转换。
|
||
*
|
||
* @param ctx ast2ir 上下文
|
||
* @param result 输出参数,存放求值结果
|
||
* @param expr 待求值的 AST 表达式
|
||
* @return 成功返回 true;若表达式不是合法的常量表达式则返回 false
|
||
*/
|
||
cbool scc_ast2ir_eval_constant_int(scc_ast2ir_ctx_t *ctx, scc_ap_t *result,
|
||
const scc_ast_expr_t *expr);
|
||
|
||
#endif /* __SCC_AST2IR_H__ */
|