Files
scc/libs/ir/include/ir_builder.h
zzy 2e5e98868d feat(ast): 添加汇编器模块并改进AST定义和IR转换
- 在README.md中添加asm汇编器模块说明
- 更新ast_def.h中的枚举注释,添加sema相关信息以明确语义分析作用域
- 重命名函数参数param_types为params,使命名更清晰
- 移除call表达式中的_target字段,简化结构
- 为member、identifier等字段添加///< fill by sema注释说明填充时机
- 为jump语句添加_target字段用于语义分析
- 更新所有AST初始化函数,接受位置信息参数以改进错误定位
- 修复alignof表达式的类型应为ALIGN_OF而非SIZE_OF的问题
- 重构ast2ir.h,引入scc_ast2ir_ctx_t上下文结构体统一管理转换状态
- 添加符号表、节点到IR映射等必要的转换上下文信息
2026-03-17 20:29:40 +08:00

184 lines
5.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#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 builder IR构建器
* @param label 基本块标签(可为 NULL自动生成
* @return 新基本块的引用
*/
scc_ir_bblock_ref_t scc_ir_builder_bblock(scc_ir_builder_t *builder,
const char *label);
/**
* @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__ */