- 在AST定义中移除函数调用结构体中的冗余name字段 - 实现完整的函数声明和定义处理流程,支持符号表查找 - 添加函数参数引用节点类型,支持参数传递和访问 - 实现函数调用的IR生成,包括参数处理和符号解析 - 添加断言确保节点有效性,提升代码健壮性 fix(ast2ir): 优化类型转换处理逻辑 - 移除多余的注释说明 - 简化参数为空检查逻辑,提高代码简洁性 - 修复函数调用时的符号表查找机制 refactor(ir): 改进IR构建器接口设计 - 修改函数构建相关API,使接口更加清晰 - 添加函数声明集合管理 - 重构内置类型缓存机制 feat(ir2mcode): 完善AMD64代码生成 - 实现函数参数到寄存器的映射 - 添加函数调用约定支持(最多4个参数) - 实现函数符号和重定位处理 - 添加栈帧管理机制 - 修正栈偏移计算 chore(ir): 清理和优化IR dump输出 - 更新节点类型描述信息 - 改进函数声明和定义的输出格式 - 修正格式化输出中的符号显示问题 style: 代码格式化和命名规范化 - 统一重定位类型枚举命名 - 优化函数参数验证和错误处理
195 lines
5.7 KiB
C
195 lines
5.7 KiB
C
#ifndef __SCC_IR_CTX_H__
|
||
#define __SCC_IR_CTX_H__
|
||
|
||
#include "ir_base.h"
|
||
#include "ir_def.h"
|
||
#include <scc_hashtable.h>
|
||
|
||
#define SCC_IR_REF_NULL 0
|
||
|
||
typedef struct {
|
||
unsigned int node_uid;
|
||
unsigned int type_uid;
|
||
unsigned int bblock_uid;
|
||
unsigned int func_uid;
|
||
|
||
SCC_VEC(scc_ir_node_t) nodes;
|
||
SCC_VEC(scc_ir_type_t) types;
|
||
SCC_VEC(scc_ir_bblock_t) bblocks;
|
||
SCC_VEC(scc_ir_func_t) funcs;
|
||
|
||
// UID -> 索引 映射
|
||
scc_hashtable_t uid2nodes;
|
||
scc_hashtable_t uid2types;
|
||
scc_hashtable_t uid2bblocks;
|
||
scc_hashtable_t uid2funcs;
|
||
|
||
// 类型去重表(类型键 -> 类型引用)
|
||
scc_hashtable_t type_uniquing;
|
||
|
||
// 常量池(常量键 -> 节点引用)
|
||
scc_hashtable_t const_pool;
|
||
} scc_ir_cprog_ctx_t;
|
||
|
||
/**
|
||
* @brief 初始化IR上下文
|
||
* @param ctx 上下文指针
|
||
*/
|
||
void scc_ir_ctx_init(scc_ir_cprog_ctx_t *ctx);
|
||
|
||
/**
|
||
* @brief 销毁IR上下文及其所有资源
|
||
* @param ctx 上下文指针
|
||
*/
|
||
void scc_ir_ctx_drop(scc_ir_cprog_ctx_t *ctx);
|
||
|
||
/**
|
||
* @brief 重置IR上下文(清空所有数据但保留内存)
|
||
* @param ctx 上下文指针
|
||
*/
|
||
void scc_ir_ctx_reset(scc_ir_cprog_ctx_t *ctx);
|
||
|
||
/**
|
||
* @brief 创建新的类型
|
||
* @param ctx 上下文指针
|
||
* @param type 类型数据(会被拷贝)
|
||
* @return 类型引用(0表示失败)
|
||
*/
|
||
scc_ir_type_ref_t scc_ir_ctx_new_type(scc_ir_cprog_ctx_t *ctx,
|
||
const scc_ir_type_t *type);
|
||
|
||
/**
|
||
* @brief 创建新的节点
|
||
* @param ctx 上下文指针
|
||
* @param node 节点数据(会被拷贝)
|
||
* @return 节点引用(0表示失败)
|
||
*/
|
||
scc_ir_node_ref_t scc_ir_ctx_new_node(scc_ir_cprog_ctx_t *ctx,
|
||
const scc_ir_node_t *node);
|
||
|
||
/**
|
||
* @brief 创建新的基本块
|
||
* @param ctx 上下文指针
|
||
* @param bblock 基本块数据(会被拷贝)
|
||
* @return 基本块引用(0表示失败)
|
||
*/
|
||
scc_ir_bblock_ref_t scc_ir_ctx_new_bblock(scc_ir_cprog_ctx_t *ctx,
|
||
const scc_ir_bblock_t *bblock);
|
||
|
||
/**
|
||
* @brief 创建新的函数
|
||
* @param ctx 上下文指针
|
||
* @param func 函数数据(会被拷贝)
|
||
* @return 函数引用(0表示失败)
|
||
*/
|
||
scc_ir_func_ref_t scc_ir_ctx_new_func(scc_ir_cprog_ctx_t *ctx,
|
||
const scc_ir_func_t *func);
|
||
|
||
/**
|
||
* @brief 根据引用获取类型
|
||
* @param ctx 上下文指针
|
||
* @param ref 类型引用
|
||
* @return 类型指针(NULL表示无效引用)
|
||
*/
|
||
scc_ir_type_t *scc_ir_ctx_get_type(scc_ir_cprog_ctx_t *ctx,
|
||
scc_ir_type_ref_t ref);
|
||
|
||
/**
|
||
* @brief 根据引用获取节点
|
||
* @param ctx 上下文指针
|
||
* @param ref 节点引用
|
||
* @return 节点指针(NULL表示无效引用)
|
||
*/
|
||
scc_ir_node_t *scc_ir_ctx_get_node(scc_ir_cprog_ctx_t *ctx,
|
||
scc_ir_node_ref_t ref);
|
||
|
||
/**
|
||
* @brief 根据引用获取基本块
|
||
* @param ctx 上下文指针
|
||
* @param ref 基本块引用
|
||
* @return 基本块指针(NULL表示无效引用)
|
||
*/
|
||
scc_ir_bblock_t *scc_ir_ctx_get_bblock(scc_ir_cprog_ctx_t *ctx,
|
||
scc_ir_bblock_ref_t ref);
|
||
|
||
/**
|
||
* @brief 根据引用获取函数
|
||
* @param ctx 上下文指针
|
||
* @param ref 函数引用
|
||
* @return 函数指针(NULL表示无效引用)
|
||
*/
|
||
scc_ir_func_t *scc_ir_ctx_get_func(scc_ir_cprog_ctx_t *ctx,
|
||
scc_ir_func_ref_t ref);
|
||
|
||
// /**
|
||
// * @brief 遍历所有类型
|
||
// * @param ctx 上下文指针
|
||
// * @param callback 回调函数
|
||
// * @param userdata 用户数据
|
||
// */
|
||
// void scc_ir_ctx_foreach_type(scc_ir_cprog_ctx_t *ctx,
|
||
// void (*callback)(scc_ir_type_ref_t ref,
|
||
// scc_ir_type_t *type,
|
||
// void *userdata),
|
||
// void *userdata);
|
||
|
||
// /**
|
||
// * @brief 遍历所有节点
|
||
// */
|
||
// void scc_ir_ctx_foreach_node(scc_ir_cprog_ctx_t *ctx,
|
||
// void (*callback)(scc_ir_node_ref_t ref,
|
||
// scc_ir_node_t *node,
|
||
// void *userdata),
|
||
// void *userdata);
|
||
|
||
// /**
|
||
// * @brief 遍历所有基本块
|
||
// */
|
||
// void scc_ir_ctx_foreach_bblock(scc_ir_cprog_ctx_t *ctx,
|
||
// void (*callback)(scc_ir_bblock_ref_t ref,
|
||
// scc_ir_bblock_t *bblock,
|
||
// void *userdata),
|
||
// void *userdata);
|
||
|
||
// /**
|
||
// * @brief 遍历所有函数
|
||
// */
|
||
// void scc_ir_ctx_foreach_func(scc_ir_cprog_ctx_t *ctx,
|
||
// void (*callback)(scc_ir_func_ref_t ref,
|
||
// scc_ir_func_t *func,
|
||
// void *userdata),
|
||
// void *userdata);
|
||
|
||
/**
|
||
* @brief 获取内置i32类型
|
||
* @param ctx 上下文指针
|
||
* @return i32类型引用
|
||
*/
|
||
scc_ir_type_ref_t scc_ir_ctx_get_builtin_i32(scc_ir_cprog_ctx_t *ctx);
|
||
|
||
/**
|
||
* @brief 获取内置零常量
|
||
* @param ctx 上下文指针
|
||
* @return 零常量节点引用
|
||
*/
|
||
scc_ir_node_ref_t scc_ir_ctx_get_builtin_zero(scc_ir_cprog_ctx_t *ctx);
|
||
|
||
/**
|
||
* @brief 创建或获取i32常量
|
||
* @param ctx 上下文指针
|
||
* @param value 常量值
|
||
* @return 常量节点引用
|
||
*/
|
||
scc_ir_node_ref_t scc_ir_ctx_get_i32_const(scc_ir_cprog_ctx_t *ctx, i32 value);
|
||
|
||
/**
|
||
* @brief 创建或获取null常量
|
||
* @param ctx 上下文指针
|
||
* @param ptr_type 指针类型引用
|
||
* @return null常量节点引用
|
||
*/
|
||
scc_ir_node_ref_t scc_ir_ctx_get_null_const(scc_ir_cprog_ctx_t *ctx,
|
||
scc_ir_type_ref_t ptr_type);
|
||
|
||
#endif /* __SCC_IR_CTX_H__ */
|