- 将scc_ast_def.h中的attr_of从union改为struct以修复结构定义问题 - 添加type_abi依赖到ast2ir模块的cbuild.toml配置文件中 - 重命名scc_ast2ir.h中的abi字段为type_abi,并更新相关初始化函数签名 - 移除废弃的scc_abi_type.h和相关平台ABI头文件 - 添加辅助函数is_variadic_marker和fixed_param_count用于处理可变参数 - 添加数组和聚合类型初始化的辅助函数
125 lines
4.9 KiB
C
125 lines
4.9 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
|
||
const scc_type_abi_t *type_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_type_abi_t *type_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);
|
||
|
||
// Utils
|
||
|
||
// 判断 AST 参数是否为 ... 生成的假 VA_LIST 参数
|
||
static cbool is_variadic_marker(const scc_ast_decl_t *decl_param) {
|
||
return decl_param->name == nullptr &&
|
||
decl_param->param.type->base.type == SCC_AST_TYPE_BUILTIN &&
|
||
scc_ast_canon_type(decl_param->param.type)->builtin.type ==
|
||
SCC_AST_BUILTIN_TYPE_VA_LIST;
|
||
}
|
||
|
||
// 计算函数类型中的固定参数个数(去掉尾部的 ... 标记)
|
||
static int fixed_param_count(const scc_ast_canon_type_t *canon) {
|
||
int n = (int)scc_vec_size(canon->function.params);
|
||
if (n > 0) {
|
||
const scc_ast_decl_t *last =
|
||
scc_vec_at(canon->function.params, (usize)(n - 1));
|
||
if (is_variadic_marker(last))
|
||
return n - 1;
|
||
}
|
||
return n;
|
||
}
|
||
|
||
void emit_array_initialization(scc_ast2ir_ctx_t *ctx,
|
||
scc_hir_value_ref_t array_ptr,
|
||
const scc_hir_type_t *array_type,
|
||
const scc_ast_expr_t *init_expr);
|
||
|
||
void emit_aggregate_initialization(scc_ast2ir_ctx_t *ctx,
|
||
scc_hir_value_ref_t base_ptr,
|
||
const scc_hir_type_t *type,
|
||
const scc_ast_expr_t *init_expr);
|
||
|
||
// ====== 类型提升(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__ */
|