#ifndef __SCC_AST2IR_H__ #define __SCC_AST2IR_H__ #include #include #include 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_ctx_t *ast_ctx; } 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_ctx_t *ast_ctx, 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); // ====== 类型提升(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); #endif /* __SCC_AST2IR_H__ */