- 在AST定义中移除函数调用结构体中的冗余name字段 - 实现完整的函数声明和定义处理流程,支持符号表查找 - 添加函数参数引用节点类型,支持参数传递和访问 - 实现函数调用的IR生成,包括参数处理和符号解析 - 添加断言确保节点有效性,提升代码健壮性 fix(ast2ir): 优化类型转换处理逻辑 - 移除多余的注释说明 - 简化参数为空检查逻辑,提高代码简洁性 - 修复函数调用时的符号表查找机制 refactor(ir): 改进IR构建器接口设计 - 修改函数构建相关API,使接口更加清晰 - 添加函数声明集合管理 - 重构内置类型缓存机制 feat(ir2mcode): 完善AMD64代码生成 - 实现函数参数到寄存器的映射 - 添加函数调用约定支持(最多4个参数) - 实现函数符号和重定位处理 - 添加栈帧管理机制 - 修正栈偏移计算 chore(ir): 清理和优化IR dump输出 - 更新节点类型描述信息 - 改进函数声明和定义的输出格式 - 修正格式化输出中的符号显示问题 style: 代码格式化和命名规范化 - 统一重定位类型枚举命名 - 优化函数参数验证和错误处理
226 lines
5.7 KiB
C
226 lines
5.7 KiB
C
#ifndef __SCC_IR_DEF_H__
|
|
#define __SCC_IR_DEF_H__
|
|
|
|
#include <scc_core.h>
|
|
|
|
typedef unsigned int ir_handle_t;
|
|
typedef const char *scc_ir_label_t;
|
|
|
|
typedef struct scc_ir_node scc_ir_node_t;
|
|
typedef ir_handle_t scc_ir_node_ref_t;
|
|
typedef SCC_VEC(scc_ir_node_ref_t) scc_ir_node_ref_vec_t;
|
|
|
|
typedef struct scc_ir_type scc_ir_type_t;
|
|
typedef ir_handle_t scc_ir_type_ref_t;
|
|
typedef SCC_VEC(scc_ir_type_ref_t) scc_ir_type_ref_vec_t;
|
|
|
|
typedef struct scc_ir_bblock scc_ir_bblock_t;
|
|
typedef ir_handle_t scc_ir_bblock_ref_t;
|
|
typedef SCC_VEC(scc_ir_bblock_ref_t) scc_ir_bblock_ref_vec_t;
|
|
|
|
typedef struct scc_ir_func scc_ir_func_t;
|
|
typedef ir_handle_t scc_ir_func_ref_t;
|
|
typedef SCC_VEC(scc_ir_func_ref_t) scc_ir_func_ref_vec_t;
|
|
|
|
typedef enum scc_ir_type_tag {
|
|
SCC_IR_TYPE_UNKNOWN,
|
|
SCC_IR_TYPE_VOID,
|
|
|
|
SCC_IR_TYPE_I8,
|
|
SCC_IR_TYPE_I16,
|
|
SCC_IR_TYPE_I32,
|
|
SCC_IR_TYPE_I64,
|
|
SCC_IR_TYPE_I128,
|
|
|
|
SCC_IR_TYPE_U8,
|
|
SCC_IR_TYPE_U16,
|
|
SCC_IR_TYPE_U32,
|
|
SCC_IR_TYPE_U64,
|
|
SCC_IR_TYPE_U128,
|
|
|
|
SCC_IR_TYPE_F16,
|
|
SCC_IR_TYPE_F32,
|
|
SCC_IR_TYPE_F64,
|
|
SCC_IR_TYPE_F128,
|
|
|
|
SCC_IR_TYPE_PTR,
|
|
SCC_IR_TYPE_ARRAY,
|
|
SCC_IR_TYPE_FUNC,
|
|
SCC_IR_TYPE_STRUCT,
|
|
SCC_IR_TYPE_VECTOR,
|
|
} scc_ir_type_tag_t;
|
|
|
|
struct scc_ir_type {
|
|
scc_ir_type_tag_t tag;
|
|
int size; // 字节大小
|
|
int align; // 对齐要求
|
|
union {
|
|
struct {
|
|
scc_ir_type_ref_t base;
|
|
usize len; // TODO usize is target dependent
|
|
} array;
|
|
struct {
|
|
scc_ir_type_ref_t base;
|
|
} pointer;
|
|
struct {
|
|
scc_ir_type_ref_vec_t params;
|
|
scc_ir_type_ref_t ret_type;
|
|
} function;
|
|
} data;
|
|
};
|
|
|
|
struct scc_ir_bblock {
|
|
scc_ir_label_t label;
|
|
scc_ir_node_ref_vec_t instrs;
|
|
// ir_arr_t used_by;
|
|
}; // basic block
|
|
|
|
struct scc_ir_func {
|
|
scc_ir_label_t name;
|
|
scc_ir_type_ref_t type;
|
|
scc_ir_node_ref_vec_t params;
|
|
scc_ir_bblock_ref_vec_t bblocks;
|
|
};
|
|
|
|
typedef enum scc_ir_node_tag {
|
|
SCC_IR_NODE_NULL,
|
|
SCC_IR_NODE_CONST_INT,
|
|
SCC_IR_NODE_CONST_UINT,
|
|
SCC_IR_NODE_CONST_FLOAT,
|
|
SCC_IR_NODE_CONV, ///< 类型转换
|
|
SCC_IR_NODE_FUNC_ARG_REF, ///< 函数参数引用
|
|
SCC_IR_NODE_BLOCK_ARG_REF, ///< 基本块参数引用
|
|
SCC_IR_NODE_ALLOC, ///< 分配内存(stack)
|
|
SCC_IR_NODE_GLOBAL_ALLOC, ///< 全局分配(bss)
|
|
SCC_IR_NODE_LOAD, ///< 加载数据
|
|
SCC_IR_NODE_STORE, ///< 存储数据
|
|
SCC_IR_NODE_GET_PTR, ///< 获取指针
|
|
SCC_IR_NODE_GET_ELEM_PTR, ///< 获取元素指针(used by array)
|
|
SCC_IR_NODE_OP, ///< 二元运算
|
|
SCC_IR_NODE_BRANCH, ///< 有条件分支
|
|
SCC_IR_NODE_JUMP, ///< 无条件跳转
|
|
SCC_IR_NODE_CALL, ///< 调用函数
|
|
SCC_IR_NODE_RET, ///< 函数返回
|
|
} scc_ir_node_tag_t;
|
|
|
|
typedef enum {
|
|
/// Empty op for init or nop
|
|
SCC_IR_OP_EMPTY,
|
|
/// Not equal to.
|
|
SCC_IR_OP_NEQ,
|
|
/// Equal to.
|
|
SCC_IR_OP_EQ,
|
|
/// Greater than.
|
|
SCC_IR_OP_GT,
|
|
/// Less than.
|
|
SCC_IR_OP_LT,
|
|
/// Greater than or equal to.
|
|
SCC_IR_OP_GE,
|
|
/// Less than or equal to.
|
|
SCC_IR_OP_LE,
|
|
/// Addition.
|
|
SCC_IR_OP_ADD,
|
|
/// Subtraction.
|
|
SCC_IR_OP_SUB,
|
|
/// Multiplication.
|
|
SCC_IR_OP_MUL,
|
|
/// Division.
|
|
SCC_IR_OP_DIV,
|
|
/// Modulo.
|
|
SCC_IR_OP_MOD,
|
|
/// Bitwise AND.
|
|
SCC_IR_OP_AND,
|
|
/// Bitwise OR.
|
|
SCC_IR_OP_OR,
|
|
/// Bitwise XOR.
|
|
SCC_IR_OP_XOR,
|
|
/// Bitwise NOT.
|
|
SCC_IR_OP_NOT,
|
|
/// Shift left logical.
|
|
SCC_IR_OP_SHL,
|
|
/// Shift right logical.
|
|
SCC_IR_OP_SHR,
|
|
/// Shift right arithmetic.
|
|
SCC_IR_OP_SAR,
|
|
} scc_ir_op_type_t;
|
|
|
|
struct scc_ir_node {
|
|
scc_ir_type_ref_t type;
|
|
scc_ir_label_t name;
|
|
scc_ir_node_ref_vec_t used_by;
|
|
scc_ir_node_tag_t tag;
|
|
union {
|
|
union {
|
|
i8 int8;
|
|
i16 int16;
|
|
i32 int32;
|
|
i64 int64;
|
|
// TODO int128
|
|
i8 int_any[16];
|
|
} const_int;
|
|
union {
|
|
u8 uint8;
|
|
u16 uint16;
|
|
u32 uint32;
|
|
u64 uint64;
|
|
// TODO uint128;
|
|
u8 uint_any[16];
|
|
} const_uint;
|
|
// aggregate;
|
|
struct {
|
|
usize idx;
|
|
} arg_ref;
|
|
struct {
|
|
scc_ir_node_ref_vec_t elements;
|
|
} global_alloc;
|
|
struct {
|
|
scc_ir_node_ref_t operand;
|
|
scc_ir_type_ref_t target_type; // 目标类型
|
|
enum { CONV_SEXT, CONV_ZEXT, CONV_TRUNC } conv_type;
|
|
} conv;
|
|
struct {
|
|
scc_ir_node_ref_t target;
|
|
} load;
|
|
struct {
|
|
scc_ir_node_ref_t target;
|
|
scc_ir_node_ref_t value;
|
|
} store;
|
|
struct {
|
|
scc_ir_node_ref_t src_addr;
|
|
scc_ir_node_ref_t index;
|
|
} get_ptr;
|
|
struct {
|
|
scc_ir_node_ref_t src_addr;
|
|
scc_ir_node_ref_t index;
|
|
} get_elem_ptr;
|
|
struct {
|
|
scc_ir_op_type_t op;
|
|
scc_ir_node_ref_t lhs;
|
|
scc_ir_node_ref_t rhs;
|
|
} op;
|
|
struct {
|
|
scc_ir_node_ref_t cond;
|
|
scc_ir_bblock_ref_t true_bblock;
|
|
scc_ir_bblock_ref_t false_bblock;
|
|
} branch;
|
|
struct {
|
|
scc_ir_bblock_ref_t target_bblock;
|
|
} jump;
|
|
struct {
|
|
scc_ir_func_ref_t callee; // TODO function pointer call
|
|
scc_ir_node_ref_vec_t args;
|
|
} call;
|
|
struct {
|
|
scc_ir_node_ref_t ret_val;
|
|
} ret;
|
|
} data;
|
|
};
|
|
|
|
typedef struct scc_ir_cprog {
|
|
scc_ir_node_ref_vec_t global_vals; /* 全局变量 */
|
|
scc_ir_func_ref_vec_t func_defs; /* 所有函数定义 */
|
|
scc_ir_func_ref_vec_t func_decls; /* 所有函数包括定义的声明 */
|
|
} scc_ir_cprog_t;
|
|
|
|
#endif /* __SCC_IR_DEF_H__ */
|