feat(ast2ir): 添加AST到IR转换功能并完善类型ABI支持
- 添加了ast2ir库用于将AST转换为IR中间表示 - 实现了Windows x64 ABI类型定义文件 - 完善了IR库中的类型定义,添加了无符号整数类型和操作符枚举 - 更新了IR转储功能以支持新的节点类型 - 在主程序中集成了AST到IR的转换流程 - 修改了PE目标文件生成功能以修正节区标志 - 更新了向量实现的日志和错误处理机制 fix(ir): 修复IR库中的操作符枚举和类型处理问题 - 修复了IR操作符枚举的命名空间问题,统一使用SCC_IR前缀 - 添加了无符号整数类型的哈希和比较支持 - 修正了常量整数节点的打印函数调用 - 更新了各种IR节点的转储输出格式 - 删除了测试文件中的冗余代码 refactor: 重构项目依赖配置和构建设置 - 启用了ast2ir和ir库的依赖配置 - 添加了def文件到.gitignore中 - 重命名了部分测试文件
This commit is contained in:
@@ -23,17 +23,26 @@ 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_I1,
|
||||
|
||||
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,
|
||||
@@ -76,56 +85,61 @@ struct scc_ir_func {
|
||||
typedef enum scc_ir_node_tag {
|
||||
SCC_IR_NODE_NULL,
|
||||
SCC_IR_NODE_CONST_INT,
|
||||
SCC_IR_NODE_ALLOC,
|
||||
SCC_IR_NODE_LOAD,
|
||||
SCC_IR_NODE_STORE,
|
||||
SCC_IR_NODE_GET_PTR,
|
||||
SCC_IR_NODE_OP,
|
||||
SCC_IR_NODE_BRANCH,
|
||||
SCC_IR_NODE_JUMP,
|
||||
SCC_IR_NODE_CALL,
|
||||
SCC_IR_NODE_RET,
|
||||
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
|
||||
IR_OP_EMPTY,
|
||||
SCC_IR_OP_EMPTY,
|
||||
/// Not equal to.
|
||||
IR_OP_NEQ,
|
||||
SCC_IR_OP_NEQ,
|
||||
/// Equal to.
|
||||
IR_OP_EQ,
|
||||
SCC_IR_OP_EQ,
|
||||
/// Greater than.
|
||||
IR_OP_GT,
|
||||
SCC_IR_OP_GT,
|
||||
/// Less than.
|
||||
IR_OP_LT,
|
||||
SCC_IR_OP_LT,
|
||||
/// Greater than or equal to.
|
||||
IR_OP_GE,
|
||||
SCC_IR_OP_GE,
|
||||
/// Less than or equal to.
|
||||
IR_OP_LE,
|
||||
SCC_IR_OP_LE,
|
||||
/// Addition.
|
||||
IR_OP_ADD,
|
||||
SCC_IR_OP_ADD,
|
||||
/// Subtraction.
|
||||
IR_OP_SUB,
|
||||
SCC_IR_OP_SUB,
|
||||
/// Multiplication.
|
||||
IR_OP_MUL,
|
||||
SCC_IR_OP_MUL,
|
||||
/// Division.
|
||||
IR_OP_DIV,
|
||||
SCC_IR_OP_DIV,
|
||||
/// Modulo.
|
||||
IR_OP_MOD,
|
||||
SCC_IR_OP_MOD,
|
||||
/// Bitwise AND.
|
||||
IR_OP_AND,
|
||||
SCC_IR_OP_AND,
|
||||
/// Bitwise OR.
|
||||
IR_OP_OR,
|
||||
SCC_IR_OP_OR,
|
||||
/// Bitwise XOR.
|
||||
IR_OP_XOR,
|
||||
SCC_IR_OP_XOR,
|
||||
/// Bitwise NOT.
|
||||
IR_OP_NOT,
|
||||
SCC_IR_OP_NOT,
|
||||
/// Shift left logical.
|
||||
IR_OP_SHL,
|
||||
SCC_IR_OP_SHL,
|
||||
/// Shift right logical.
|
||||
IR_OP_SHR,
|
||||
SCC_IR_OP_SHR,
|
||||
/// Shift right arithmetic.
|
||||
IR_OP_SAR,
|
||||
SCC_IR_OP_SAR,
|
||||
} scc_ir_op_type_t;
|
||||
|
||||
struct scc_ir_node {
|
||||
@@ -151,9 +165,17 @@ struct scc_ir_node {
|
||||
u8 uint_any[16];
|
||||
} const_uint;
|
||||
// aggregate;
|
||||
// func_arg_ref;
|
||||
// block_arg_ref;
|
||||
// global_alloc;
|
||||
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;
|
||||
|
||||
@@ -70,7 +70,7 @@ void scc_ir_node_init(scc_ir_node_t *in, const char *name,
|
||||
in->data.get_ptr.index = 0;
|
||||
break;
|
||||
case SCC_IR_NODE_OP:
|
||||
in->data.op.op = IR_OP_EMPTY;
|
||||
in->data.op.op = SCC_IR_OP_EMPTY;
|
||||
in->data.op.lhs = 0;
|
||||
in->data.op.rhs = 0;
|
||||
break;
|
||||
|
||||
@@ -7,4 +7,4 @@ scc_ir_type_t scc_ir_builtin_i32 = {
|
||||
scc_ir_node_t scc_ir_builtin_zero = {
|
||||
.tag = SCC_IR_NODE_CONST_INT,
|
||||
.data.const_int.int_any = {0},
|
||||
};
|
||||
};
|
||||
|
||||
@@ -22,7 +22,11 @@ static u32 hash_type(const scc_ir_type_t *key) {
|
||||
|
||||
switch (key->tag) {
|
||||
case SCC_IR_TYPE_VOID:
|
||||
case SCC_IR_TYPE_I1:
|
||||
case SCC_IR_TYPE_U8:
|
||||
case SCC_IR_TYPE_U16:
|
||||
case SCC_IR_TYPE_U32:
|
||||
case SCC_IR_TYPE_U64:
|
||||
case SCC_IR_TYPE_U128:
|
||||
case SCC_IR_TYPE_I8:
|
||||
case SCC_IR_TYPE_I16:
|
||||
case SCC_IR_TYPE_I32:
|
||||
@@ -70,7 +74,11 @@ static int cmp_type(const scc_ir_type_t *key1, const scc_ir_type_t *key2) {
|
||||
|
||||
switch (key1->tag) {
|
||||
case SCC_IR_TYPE_VOID:
|
||||
case SCC_IR_TYPE_I1:
|
||||
case SCC_IR_TYPE_U8:
|
||||
case SCC_IR_TYPE_U16:
|
||||
case SCC_IR_TYPE_U32:
|
||||
case SCC_IR_TYPE_U64:
|
||||
case SCC_IR_TYPE_U128:
|
||||
case SCC_IR_TYPE_I8:
|
||||
case SCC_IR_TYPE_I16:
|
||||
case SCC_IR_TYPE_I32:
|
||||
|
||||
@@ -31,13 +31,16 @@ static const char *get_node_type_str(scc_ir_node_tag_t tag) {
|
||||
// 获取操作符字符串
|
||||
static const char *get_op_str(scc_ir_op_type_t op) {
|
||||
static const char *ops[] = {
|
||||
[IR_OP_EMPTY] = "empty", [IR_OP_NEQ] = "!=", [IR_OP_EQ] = "==",
|
||||
[IR_OP_GT] = ">", [IR_OP_LT] = "<", [IR_OP_GE] = ">=",
|
||||
[IR_OP_LE] = "<=", [IR_OP_ADD] = "+", [IR_OP_SUB] = "-",
|
||||
[IR_OP_MUL] = "*", [IR_OP_DIV] = "/", [IR_OP_MOD] = "%",
|
||||
[IR_OP_AND] = "&", [IR_OP_OR] = "|", [IR_OP_XOR] = "^",
|
||||
[IR_OP_NOT] = "~", [IR_OP_SHL] = "<<", [IR_OP_SHR] = ">>",
|
||||
[IR_OP_SAR] = ">>a", // Arithmetic shift right
|
||||
[SCC_IR_OP_EMPTY] = "empty", [SCC_IR_OP_NEQ] = "!=",
|
||||
[SCC_IR_OP_EQ] = "==", [SCC_IR_OP_GT] = ">",
|
||||
[SCC_IR_OP_LT] = "<", [SCC_IR_OP_GE] = ">=",
|
||||
[SCC_IR_OP_LE] = "<=", [SCC_IR_OP_ADD] = "+",
|
||||
[SCC_IR_OP_SUB] = "-", [SCC_IR_OP_MUL] = "*",
|
||||
[SCC_IR_OP_DIV] = "/", [SCC_IR_OP_MOD] = "%",
|
||||
[SCC_IR_OP_AND] = "&", [SCC_IR_OP_OR] = "|",
|
||||
[SCC_IR_OP_XOR] = "^", [SCC_IR_OP_NOT] = "~",
|
||||
[SCC_IR_OP_SHL] = "<<", [SCC_IR_OP_SHR] = ">>",
|
||||
[SCC_IR_OP_SAR] = ">>a", // Arithmetic shift right
|
||||
};
|
||||
|
||||
if (op >= 0 && op < sizeof(ops) / sizeof(ops[0]) && ops[op] != NULL) {
|
||||
@@ -49,7 +52,9 @@ static const char *get_op_str(scc_ir_op_type_t op) {
|
||||
// 获取类型标签字符串
|
||||
static const char *get_type_tag_str(scc_ir_type_tag_t tag) {
|
||||
static const char *type_tags[] = {
|
||||
[SCC_IR_TYPE_VOID] = "void", [SCC_IR_TYPE_I1] = "i1",
|
||||
[SCC_IR_TYPE_VOID] = "void", [SCC_IR_TYPE_U8] = "u8",
|
||||
[SCC_IR_TYPE_U16] = "u16", [SCC_IR_TYPE_U32] = "u32",
|
||||
[SCC_IR_TYPE_U64] = "u64", [SCC_IR_TYPE_U128] = "u128",
|
||||
[SCC_IR_TYPE_I8] = "i8", [SCC_IR_TYPE_I16] = "i16",
|
||||
[SCC_IR_TYPE_I32] = "i32", [SCC_IR_TYPE_I64] = "i64",
|
||||
[SCC_IR_TYPE_I128] = "i128", [SCC_IR_TYPE_F16] = "f16",
|
||||
@@ -82,7 +87,7 @@ static void dump_const_int_node(scc_ir_dump_ctx_t *ctx,
|
||||
const scc_ir_node_t *node) {
|
||||
scc_tree_dump_push_level(ctx->dump_ctx, true);
|
||||
scc_tree_print_indent(ctx->dump_ctx);
|
||||
scc_printf("%d\n", node->data.const_int.int32);
|
||||
scc_tree_dump_printf(ctx->dump_ctx, "%d\n", node->data.const_int.int32);
|
||||
scc_tree_dump_pop_level(ctx->dump_ctx);
|
||||
}
|
||||
|
||||
@@ -189,13 +194,14 @@ static void dump_jump_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) {
|
||||
scc_ir_bblock_t *target_bblock =
|
||||
scc_ir_ctx_get_bblock(ctx->ir_ctx, node->data.jump.target_bblock);
|
||||
if (target_bblock) {
|
||||
scc_printf("to '%s'", target_bblock->label ? target_bblock->label
|
||||
: "<unnamed>");
|
||||
scc_tree_dump_printf(ctx->dump_ctx, "to '%s'",
|
||||
target_bblock->label ? target_bblock->label
|
||||
: "<unnamed>");
|
||||
} else {
|
||||
scc_printf("to invalid block");
|
||||
scc_tree_dump_printf(ctx->dump_ctx, "to invalid block");
|
||||
}
|
||||
} else {
|
||||
scc_printf("to NULL");
|
||||
scc_tree_dump_printf(ctx->dump_ctx, "to NULL");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,16 +212,17 @@ static void dump_call_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) {
|
||||
scc_ir_func_t *callee =
|
||||
scc_ir_ctx_get_func(ctx->ir_ctx, node->data.call.callee);
|
||||
if (callee) {
|
||||
scc_printf("func='%s'", callee->name ? callee->name : "<unnamed>");
|
||||
scc_tree_dump_printf(ctx->dump_ctx, "func='%s'",
|
||||
callee->name ? callee->name : "<unnamed>");
|
||||
} else {
|
||||
scc_printf("func=<invalid>");
|
||||
scc_tree_dump_printf(ctx->dump_ctx, "func=<invalid>");
|
||||
}
|
||||
} else {
|
||||
scc_printf("func=NULL");
|
||||
scc_tree_dump_printf(ctx->dump_ctx, "func=NULL");
|
||||
}
|
||||
|
||||
if (scc_vec_size(node->data.call.args) > 0) {
|
||||
scc_printf("\n");
|
||||
scc_tree_dump_printf(ctx->dump_ctx, "\n");
|
||||
}
|
||||
|
||||
// 输出参数
|
||||
@@ -261,14 +268,14 @@ void scc_ir_dump_node(scc_ir_dump_ctx_t *ctx, scc_ir_node_ref_t node_ref) {
|
||||
}
|
||||
|
||||
if (node->type) {
|
||||
scc_printf(" : ");
|
||||
scc_tree_dump_printf(ctx->dump_ctx, " : ");
|
||||
scc_ir_type_t *type = scc_ir_ctx_get_type(ctx->ir_ctx, node->type);
|
||||
if (type) {
|
||||
PRINT_VALUE(ctx->dump_ctx, "%s", get_type_tag_str(type->tag));
|
||||
}
|
||||
}
|
||||
|
||||
scc_printf("\n");
|
||||
scc_tree_dump_printf(ctx->dump_ctx, "\n");
|
||||
|
||||
// 根据节点类型输出特定信息
|
||||
switch (node->tag) {
|
||||
@@ -305,7 +312,7 @@ void scc_ir_dump_node(scc_ir_dump_ctx_t *ctx, scc_ir_node_ref_t node_ref) {
|
||||
break;
|
||||
default:
|
||||
PRINT_QUOTED_VALUE(ctx->dump_ctx, "unknown");
|
||||
scc_printf("tag(%d)", node->tag);
|
||||
scc_tree_dump_printf(ctx->dump_ctx, "tag(%d)", node->tag);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -325,7 +332,7 @@ void scc_ir_dump_type(scc_ir_dump_ctx_t *ctx, scc_ir_type_ref_t type_ref) {
|
||||
scc_tree_print_indent(ctx->dump_ctx);
|
||||
PRINT_NODE(ctx->dump_ctx, "Type: ");
|
||||
PRINT_QUOTED_VALUE(ctx->dump_ctx, get_type_tag_str(type->tag));
|
||||
scc_printf("\n");
|
||||
scc_tree_dump_printf(ctx->dump_ctx, "\n");
|
||||
|
||||
// 递归转储子类型
|
||||
switch (type->tag) {
|
||||
@@ -398,7 +405,7 @@ void scc_ir_dump_bblock(scc_ir_dump_ctx_t *ctx,
|
||||
PRINT_NODE(ctx->dump_ctx, "BasicBlock: ");
|
||||
PRINT_QUOTED_VALUE(ctx->dump_ctx,
|
||||
bblock->label ? bblock->label : "<unnamed>");
|
||||
scc_printf("\n");
|
||||
scc_tree_dump_printf(ctx->dump_ctx, "\n");
|
||||
|
||||
// 转储基本块中的指令
|
||||
for (usize i = 0; i < scc_vec_size(bblock->instrs); i++) {
|
||||
@@ -422,7 +429,7 @@ void scc_ir_dump_func(scc_ir_dump_ctx_t *ctx, scc_ir_func_ref_t func_ref) {
|
||||
scc_tree_print_indent(ctx->dump_ctx);
|
||||
PRINT_NODE(ctx->dump_ctx, "Function: ");
|
||||
PRINT_QUOTED_VALUE(ctx->dump_ctx, func->name ? func->name : "<unnamed>");
|
||||
scc_printf("\n");
|
||||
scc_tree_dump_printf(ctx->dump_ctx, "\n");
|
||||
|
||||
// 输出函数类型
|
||||
if (func->type) {
|
||||
@@ -546,6 +553,7 @@ void scc_ir_dump_type_linear(scc_ir_dump_ctx_t *ctx,
|
||||
} else {
|
||||
PRINT_TYPE(ctx->dump_ctx, ")");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
LOG_ERROR("invalid type tag");
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user