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:
zzy
2026-03-15 15:44:50 +08:00
parent 6ebf0c48e3
commit 45c59e0b65
19 changed files with 339 additions and 131 deletions

View File

@@ -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;

View File

@@ -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},
};
};

View File

@@ -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:

View File

@@ -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;