Files
scc/libs/ir/include/ir_builder.h
zzy 741171dbba feat(ir): 实现函数调用和参数处理功能
- 在AST定义中移除函数调用结构体中的冗余name字段
- 实现完整的函数声明和定义处理流程,支持符号表查找
- 添加函数参数引用节点类型,支持参数传递和访问
- 实现函数调用的IR生成,包括参数处理和符号解析
- 添加断言确保节点有效性,提升代码健壮性

fix(ast2ir): 优化类型转换处理逻辑

- 移除多余的注释说明
- 简化参数为空检查逻辑,提高代码简洁性
- 修复函数调用时的符号表查找机制

refactor(ir): 改进IR构建器接口设计

- 修改函数构建相关API,使接口更加清晰
- 添加函数声明集合管理
- 重构内置类型缓存机制

feat(ir2mcode): 完善AMD64代码生成

- 实现函数参数到寄存器的映射
- 添加函数调用约定支持(最多4个参数)
- 实现函数符号和重定位处理
- 添加栈帧管理机制
- 修正栈偏移计算

chore(ir): 清理和优化IR dump输出

- 更新节点类型描述信息
- 改进函数声明和定义的输出格式
- 修正格式化输出中的符号显示问题

style: 代码格式化和命名规范化

- 统一重定位类型枚举命名
- 优化函数参数验证和错误处理
2026-03-23 16:02:23 +08:00

191 lines
5.9 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_hashtable_t func_decl_set;
// 当前构建位置
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);
scc_ir_func_ref_t scc_ir_builder_func(scc_ir_builder_t *builder,
scc_ir_type_ref_t type_ref,
const char *name);
/**
* @brief 开始构建函数
* @param func_ref 函数引用
* @param param_names 参数名列表可为NULL
* @return void
*/
void scc_ir_builder_begin_func(scc_ir_builder_t *builder,
scc_ir_func_ref_t func_ref,
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);
scc_ir_node_ref_t scc_ir_builder_func_arg_ref(scc_ir_builder_t *builder,
scc_ir_type_ref_t type,
const char *name, usize arg_idx);
/**
* @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__ */