From 2a90e165a51e3773cd888e57980fc9095ba35cf2 Mon Sep 17 00:00:00 2001 From: zzy <2450266535@qq.com> Date: Fri, 30 Jan 2026 23:01:55 +0800 Subject: [PATCH] =?UTF-8?q?feat(ast):=20=E9=87=8D=E6=9E=84AST=E8=BD=AC?= =?UTF-8?q?=E5=82=A8=E5=8A=9F=E8=83=BD=E5=B9=B6=E5=88=9B=E5=BB=BAAST?= =?UTF-8?q?=E5=88=B0IR=E8=BD=AC=E6=8D=A2=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 重构ast_dump.c中的宏定义,简化PRINT_VALUE、PRINT_NODE、 PRINT_QUOTED_VALUE等宏的实现 - 移除冗余的PRINT_COLORED宏定义 - 统一使用SCC_TREE_DUMP_PRINT_PURE和SCC_TREE_DUMP_PRINT_AROUND 宏进行转储输出 - 在dump_stmt_impl函数中优化节点转储逻辑,统一使用end_node_dump 替代手动换行 - 添加对未知节点类型的警告日志输出 - 创建新的ast2ir模块,包含AST到IR的基本转换功能 - 实现ast_type_to_ir_type、ast_expr_to_ir、ast_stmt_to_ir、 ast_decl_to_ir等核心转换函数 - 更新IR库依赖配置,添加scc_utils和tree_dump依赖 - 新增IR基础接口定义文件ir_base.h和IR构建器接口ir_builder.h --- libs/ast/src/ast_dump.c | 67 ++-- libs/ast2ir/cbuild.toml | 9 + libs/ast2ir/include/scc_ast2ir.h | 10 + libs/ast2ir/src/ast2ir.c | 553 ++++++++++++++++++++++++++++ libs/ast2ir/tests/test_.c | 10 + libs/ir/cbuild.toml | 5 +- libs/ir/include/ir_base.h | 17 + libs/ir/include/ir_builder.h | 174 +++++++++ libs/ir/include/ir_ctx.h | 200 ++++++++++ libs/ir/include/ir_def.h | 119 +++--- libs/ir/include/ir_dump.h | 17 +- libs/ir/include/scc_ir.h | 24 +- libs/ir/src/{scc_ir.c => ir_base.c} | 74 ++-- libs/ir/src/ir_builder.c | 351 ++++++++++++++++++ libs/ir/src/ir_ctx.c | 345 +++++++++++++++++ libs/ir/src/ir_dump.c | 525 +++++++++++++++++++++++++- libs/tree_dump/include/tree_dump.h | 13 + 17 files changed, 2341 insertions(+), 172 deletions(-) create mode 100644 libs/ast2ir/cbuild.toml create mode 100644 libs/ast2ir/include/scc_ast2ir.h create mode 100644 libs/ast2ir/src/ast2ir.c create mode 100644 libs/ast2ir/tests/test_.c create mode 100644 libs/ir/include/ir_base.h create mode 100644 libs/ir/include/ir_builder.h create mode 100644 libs/ir/include/ir_ctx.h rename libs/ir/src/{scc_ir.c => ir_base.c} (51%) create mode 100644 libs/ir/src/ir_builder.c create mode 100644 libs/ir/src/ir_ctx.c diff --git a/libs/ast/src/ast_dump.c b/libs/ast/src/ast_dump.c index 1ed9754..702d935 100644 --- a/libs/ast/src/ast_dump.c +++ b/libs/ast/src/ast_dump.c @@ -5,27 +5,14 @@ #include -// 通用宏定义 -#define PRINT_COLORED(ctx, color_field, fmt, ...) \ - do { \ - if (ctx->use_color) { \ - scc_printf("%s" fmt "%s", ctx->color_field, ##__VA_ARGS__, \ - ctx->reset_color); \ - } else { \ - scc_printf(fmt, ##__VA_ARGS__); \ - } \ - } while (0) +#define PRINT_VALUE(ctx, fmt, value) \ + SCC_TREE_DUMP_PRINT_PURE(ctx, ctx->value_color, fmt, value) -#define PRINT_VALUE(ctx, fmt, ...) \ - PRINT_COLORED(ctx, value_color, fmt, ##__VA_ARGS__) +#define PRINT_NODE(ctx, name, value) \ + SCC_TREE_DUMP_PRINT_PURE(ctx, ctx->node_color, "%s", name) -#define PRINT_NODE_TYPE(ctx, node) \ - PRINT_COLORED(ctx, node_color, "%s", get_node_type_str(node->type)) - -#define PRINT_QUOTED_VALUE(ctx, value) \ - do { \ - PRINT_VALUE(ctx, "'%s'", value); \ - } while (0) +#define PRINT_QUOTED_VALUE(ctx, str) \ + SCC_TREE_DUMP_PRINT_AROUND(ctx, ctx->value_color, "'", "%s", str) // 获取节点类型的字符串表示 static const char *get_node_type_str(scc_ast_node_type_t type) { @@ -267,7 +254,8 @@ static const char *get_op_str(scc_ast_expr_op_t op) { static inline void start_node_dump(scc_ast_node_t *node, scc_tree_dump_ctx_t *ctx) { scc_tree_print_indent(ctx); - PRINT_NODE_TYPE(ctx, node); + SCC_TREE_DUMP_PRINT_PURE(ctx, ctx->node_color, "%s", + get_node_type_str(node->type)); } // 通用的结束节点打印函数 @@ -455,8 +443,7 @@ static void dump_expr_impl(scc_ast_expr_t *expr, scc_tree_dump_ctx_t *ctx) { dump_child_node((scc_ast_node_t *)expr->member.base, ctx, false); // 打印成员访问信息 scc_tree_print_indent(ctx); - PRINT_COLORED(ctx, node_color, "Member [\"%s\"]", - expr->member.member_name); + PRINT_NODE(ctx, "Member [\"%s\"]", expr->member.member_name); scc_printf("\n"); break; @@ -496,11 +483,10 @@ static void dump_stmt_impl(scc_ast_stmt_t *stmt, scc_tree_dump_ctx_t *ctx) { return; start_node_dump(&stmt->base, ctx); - // 根据语句类型输出特定信息 switch (stmt->base.type) { case SCC_AST_STMT_IF: - scc_printf("\n"); // if语句总是换行显示子节点 + end_node_dump(ctx); dump_child_node((scc_ast_node_t *)stmt->if_stmt.cond, ctx, false); dump_child_node((scc_ast_node_t *)stmt->if_stmt.then_stmt, ctx, !stmt->if_stmt.opt_else_stmt); @@ -510,22 +496,22 @@ static void dump_stmt_impl(scc_ast_stmt_t *stmt, scc_tree_dump_ctx_t *ctx) { } return; case SCC_AST_STMT_WHILE: - scc_printf("\n"); // 循环和switch语句换行显示子节点 + end_node_dump(ctx); dump_child_node((scc_ast_node_t *)stmt->while_stmt.cond, ctx, false); dump_child_node((scc_ast_node_t *)stmt->while_stmt.body, ctx, true); return; case SCC_AST_STMT_DO_WHILE: - scc_printf("\n"); // 循环和switch语句换行显示子节点 + end_node_dump(ctx); dump_child_node((scc_ast_node_t *)stmt->do_while_stmt.body, ctx, false); dump_child_node((scc_ast_node_t *)stmt->do_while_stmt.cond, ctx, true); return; case SCC_AST_STMT_SWITCH: - scc_printf("\n"); // 循环和switch语句换行显示子节点 + end_node_dump(ctx); dump_child_node((scc_ast_node_t *)stmt->switch_stmt.cond, ctx, false); dump_child_node((scc_ast_node_t *)stmt->switch_stmt.body, ctx, true); return; case SCC_AST_STMT_FOR: - scc_printf("\n"); // for语句换行显示子节点 + end_node_dump(ctx); if (stmt->for_stmt.init) { dump_child_node((scc_ast_node_t *)stmt->for_stmt.init, ctx, false); } @@ -539,31 +525,27 @@ static void dump_stmt_impl(scc_ast_stmt_t *stmt, scc_tree_dump_ctx_t *ctx) { return; case SCC_AST_STMT_RETURN: if (stmt->return_stmt.expr) { - scc_printf("\n"); + end_node_dump(ctx); dump_child_node((scc_ast_node_t *)stmt->return_stmt.expr, ctx, true); - return; + } else { + end_node_dump(ctx); } break; case SCC_AST_STMT_GOTO: if (stmt->goto_stmt.label) { PRINT_VALUE(ctx, " Label: %s", stmt->goto_stmt.label); } + end_node_dump(ctx); break; case SCC_AST_STMT_LABEL: if (stmt->label_stmt.label) { PRINT_VALUE(ctx, " %s", stmt->label_stmt.label); } + end_node_dump(ctx); break; - default: - break; - } - - end_node_dump(ctx); - - // 递归转储其他子节点 - switch (stmt->base.type) { case SCC_AST_STMT_COMPOUND: + end_node_dump(ctx); for (size_t i = 0; i < stmt->compound.block_items.size; i++) { scc_ast_node_t *item = (scc_ast_node_t *)stmt->compound.block_items.data[i]; @@ -573,21 +555,25 @@ static void dump_stmt_impl(scc_ast_stmt_t *stmt, scc_tree_dump_ctx_t *ctx) { break; case SCC_AST_STMT_EXPR: + end_node_dump(ctx); if (stmt->expr.expr) { dump_child_node((scc_ast_node_t *)stmt->expr.expr, ctx, true); } break; case SCC_AST_STMT_CASE: + end_node_dump(ctx); dump_child_node((scc_ast_node_t *)stmt->case_stmt.expr, ctx, false); dump_child_node((scc_ast_node_t *)stmt->case_stmt.stmt, ctx, true); break; case SCC_AST_STMT_DEFAULT: + end_node_dump(ctx); dump_child_node((scc_ast_node_t *)stmt->default_stmt.stmt, ctx, true); break; default: + LOG_WARN("unknown node type %d", stmt->base.type); break; } } @@ -705,8 +691,7 @@ static void dump_unit_impl(scc_ast_translation_unit_t *unit, return; start_node_dump(&unit->base, ctx); - scc_printf("\n"); - + end_node_dump(ctx); for (size_t i = 0; i < unit->declarations.size; i++) { dump_child_node((scc_ast_node_t *)unit->declarations.data[i], ctx, i == unit->declarations.size - 1); @@ -728,4 +713,4 @@ void scc_ast_dump_node(scc_tree_dump_ctx_t *ctx, const scc_ast_node_t *node) { } else if (SCC_AST_IS_A(scc_ast_translation_unit_t, node)) { dump_unit_impl(SCC_AST_CAST_TO(scc_ast_translation_unit_t, node), ctx); } -} \ No newline at end of file +} diff --git a/libs/ast2ir/cbuild.toml b/libs/ast2ir/cbuild.toml new file mode 100644 index 0000000..2fc8343 --- /dev/null +++ b/libs/ast2ir/cbuild.toml @@ -0,0 +1,9 @@ +[package] +name = "" +version = "0.1.0" +authors = [] +description = "" + +# dependencies = [] +# features = {} +# default_features = [] diff --git a/libs/ast2ir/include/scc_ast2ir.h b/libs/ast2ir/include/scc_ast2ir.h new file mode 100644 index 0000000..f8c211a --- /dev/null +++ b/libs/ast2ir/include/scc_ast2ir.h @@ -0,0 +1,10 @@ +#ifndef __SCC_AST2IR_H__ +#define __SCC_AST2IR_H__ + +#include +#include +#include + +void scc_ast2ir(scc_ast_translation_unit_t *tu, scc_ir_builder_t *builder); + +#endif /* __SCC_AST2IR_H__ */ diff --git a/libs/ast2ir/src/ast2ir.c b/libs/ast2ir/src/ast2ir.c new file mode 100644 index 0000000..7e92679 --- /dev/null +++ b/libs/ast2ir/src/ast2ir.c @@ -0,0 +1,553 @@ +#include +#include + +static scc_ir_node_ref_t ast_expr_to_ir(scc_ir_builder_t *ctx, + scc_ast_expr_t *expr); +static void ast_stmt_to_ir(scc_ir_builder_t *ctx, scc_ast_stmt_t *stmt); +static void ast_decl_to_ir(scc_ir_builder_t *ctx, scc_ast_decl_t *decl); +static scc_ir_type_ref_t ast_type_to_ir_type(scc_ir_builder_t *ctx, + scc_ast_type_t *ast_type); + +// 转换AST类型为IR类型 +static scc_ir_type_ref_t ast_type_to_ir_type(scc_ir_builder_t *ctx, + scc_ast_type_t *ast_type) { + if (ctx == null || ast_type == null) { + LOG_ERROR("args is null"); + return 0; + } + scc_ir_type_t ir_type; + + switch (ast_type->base.type) { + case SCC_AST_TYPE_BUILTIN: { + // 映射内置类型 + scc_ir_type_init(&ir_type, SCC_IR_TYPE_I32); + // TODO: 根据具体内置类型设置 + break; + } + + case SCC_AST_TYPE_POINTER: { + scc_ir_type_init(&ir_type, SCC_IR_TYPE_PTR); + + // 创建指向类型并添加到程序类型列表 + scc_ir_type_ref_t pointee_type = + ast_type_to_ir_type(ctx, ast_type->pointer.pointee); + + // 注意:我们需要找到一种合适的方式来存储类型信息 + // 目前的IR设计中类型信息应该直接存储在类型结构中 + ir_type.data.pointer.base = pointee_type; + break; + } + + case SCC_AST_TYPE_ARRAY: { + scc_ir_type_init(&ir_type, SCC_IR_TYPE_ARRAY); + + // 创建元素类型并添加到程序类型列表 + scc_ir_type_ref_t element_type = + ast_type_to_ir_type(ctx, ast_type->array.element); + + // 将类型添加到程序的类型容器中 + ir_type.data.array.base = element_type; + // TODO: 处理数组大小表达式 + ir_type.data.array.len = 0; // 暂时设为0 + break; + } + + case SCC_AST_TYPE_FUNCTION: { + scc_ir_type_init(&ir_type, SCC_IR_TYPE_FUNC); + + // 处理返回类型 + scc_ir_type_ref_t ret_type = + ast_type_to_ir_type(ctx, ast_type->function.return_type); + + // 将返回类型添加到程序的类型容器中 + ir_type.data.function.ret_type = ret_type; + + // 转换参数类型 + scc_ir_type_ref_vec_t params; + scc_vec_init(params); + scc_vec_foreach(ast_type->function.param_types, i) { + scc_ast_type_t *param_type = + scc_vec_at(ast_type->function.param_types, i); + scc_ir_type_ref_t tmp_type = ast_type_to_ir_type(ctx, param_type); + scc_vec_push(params, tmp_type); + } + ir_type.data.function.params = params; + break; + } + + default: + LOG_FATAL("Unsupported AST type: %d", ast_type->base.type); + return 0; + } + return scc_ir_ctx_new_type(&ctx->ctx, &ir_type); +} + +// 转换AST表达式为IR节点 +static scc_ir_node_ref_t ast_expr_to_ir(scc_ir_builder_t *ctx, + scc_ast_expr_t *expr) { + if (ctx == null || expr == null) { + LOG_ERROR("args is null"); + return 0; + } + + switch (expr->base.type) { + case SCC_AST_EXPR_IDENTIFIER: { + // TODO maybe error or need symtab + scc_ir_node_t in; + scc_ir_node_init(&in, expr->identifier.name, SCC_IR_NODE_LOAD); + return scc_ir_ctx_new_node(&ctx->ctx, &in); + } + + case SCC_AST_EXPR_INT_LITERAL: { + return scc_ir_ctx_get_i32_const(&ctx->ctx, expr->literal.value.i); + } + + case SCC_AST_EXPR_BINARY: { + // 转换左右操作数 + scc_ir_node_ref_t lhs, rhs; + lhs = ast_expr_to_ir(ctx, expr->binary.lhs); + rhs = ast_expr_to_ir(ctx, expr->binary.rhs); + + // 映射操作符 + scc_ir_op_type_t op; + switch (expr->binary.op) { + /* clang-format off */ + case SCC_AST_OP_ADD: op = IR_OP_ADD; break; + case SCC_AST_OP_SUB: op = IR_OP_SUB; break; + case SCC_AST_OP_MUL: op = IR_OP_MUL; break; + case SCC_AST_OP_DIV: op = IR_OP_DIV; break; + case SCC_AST_OP_MOD: op = IR_OP_MOD; break; + case SCC_AST_OP_LOGICAL_AND: op = IR_OP_AND; break; + case SCC_AST_OP_LOGICAL_OR: op = IR_OP_OR; break; + case SCC_AST_OP_BITWISE_XOR: op = IR_OP_XOR; break; + case SCC_AST_OP_LEFT_SHIFT: op = IR_OP_SHL; break; + case SCC_AST_OP_RIGHT_SHIFT: op = IR_OP_SHR; break; + case SCC_AST_OP_EQUAL: op = IR_OP_EQ; break; + case SCC_AST_OP_NOT_EQUAL: op = IR_OP_NEQ; break; + case SCC_AST_OP_LESS: op = IR_OP_LT; break; + case SCC_AST_OP_LESS_EQUAL: op = IR_OP_LE; break; + case SCC_AST_OP_GREATER: op = IR_OP_GT; break; + case SCC_AST_OP_GREATER_EQUAL: op = IR_OP_GE; break; + case SCC_AST_OP_ASSIGN: { + // 赋值表达式:存储右值到左值位置 + return scc_ir_builder_store(ctx, lhs, rhs); + + } + case SCC_AST_OP_ASSIGN_ADD: + TODO(); + /* clang-format on */ + default: + LOG_WARN("Unsupported binary operator: %d", expr->binary.op); + op = IR_OP_ADD; // 默认 + } + + // 创建操作节点 + return scc_ir_builder_binop(ctx, op, lhs, rhs); + } + + case SCC_AST_EXPR_UNARY: { + // 转换操作数 + scc_ir_node_ref_t operand = ast_expr_to_ir(ctx, expr->unary.operand); + + // 映射一元操作符 + switch (expr->unary.op) { + case SCC_TOK_SUB: + // 负号 + // 实现为0 - operand + return scc_ir_builder_binop(ctx, IR_OP_SUB, + scc_ir_ctx_get_builtin_zero(&ctx->ctx), + operand); + case SCC_TOK_BIT_NOT: + // 按位取反 + return scc_ir_builder_binop(ctx, IR_OP_NOT, operand, 0); + case SCC_TOK_NOT: + // 逻辑非 + // 实现为与0比较 + return scc_ir_builder_binop( + ctx, IR_OP_EQ, scc_ir_ctx_get_builtin_zero(&ctx->ctx), operand); + default: + LOG_WARN("Unsupported unary operator: %d", expr->unary.op); + return 0; + } + } + + case SCC_AST_EXPR_COND: { + TODO(); + break; + } + + case SCC_AST_EXPR_CALL: { + // 转换参数 + scc_ir_node_ref_vec_t args; + scc_vec_init(args); + + // 检查参数是否为空 + if (expr->call.args.data != null) { + scc_vec_foreach(expr->call.args, i) { + scc_ast_expr_t *arg_expr = scc_vec_at(expr->call.args, i); + scc_ir_node_ref_t arg_node; + arg_node = ast_expr_to_ir(ctx, arg_expr); + scc_vec_push(args, arg_node); + } + } + + // 创建调用节点(需要查找函数定义) + // TODO: 需要符号表查找函数 + scc_ir_node_ref_t func = + scc_ir_builder_call(ctx, 0, args.data, args.size); + scc_vec_free(args); + return func; + } + + default: + LOG_WARN("Unsupported expression type: %d", expr->base.type); + return 0; + } +} + +// 转换AST语句为IR +static void ast_stmt_to_ir(scc_ir_builder_t *ctx, scc_ast_stmt_t *stmt) { + if (stmt == null) { + return; + } + + switch (stmt->base.type) { + case SCC_AST_STMT_COMPOUND: { + // 进入新的作用域 + scc_vec_foreach(stmt->compound.block_items, i) { + scc_ast_node_t *child_stmt = + scc_vec_at(stmt->compound.block_items, i); + if (SCC_AST_IS_A(scc_ast_stmt_t, child_stmt)) { + ast_stmt_to_ir(ctx, + SCC_AST_CAST_TO(scc_ast_stmt_t, child_stmt)); + } else if (SCC_AST_IS_A(scc_ast_decl_t, child_stmt)) { + ast_decl_to_ir(ctx, + SCC_AST_CAST_TO(scc_ast_decl_t, child_stmt)); + } else { + UNREACHABLE(); + } + } + break; + } + + case SCC_AST_STMT_EXPR: { + ast_expr_to_ir(ctx, stmt->expr.expr); + break; + } + + // case SCC_AST_STMT_IF: { + // // 创建基本块 - 统一使用栈上分配 + // scc_ir_bblock_t true_block; + // scc_ir_bblock_init(&true_block, "if_true"); + + // scc_ir_bblock_t false_block; + // scc_ir_bblock_init(&false_block, "if_false"); + + // scc_ir_bblock_t merge_block; + // scc_ir_bblock_init(&merge_block, "if_merge"); + + // // 转换条件 + // scc_ir_node_ref_t cond_node = ast_expr_to_ir(ctx, + // stmt->if_stmt.cond); + + // // 创建分支指令 + // scc_ir_builder_branch(); + // scc_ir_node_t br_node; + // init_branch_node(&br_node, cond, &true_block, &false_block); + // emit_instruction(ctx, &br_node); + + // // 保存当前块 + // ast2ir_ctx_t saved = ctx->current_ctx; + + // // 生成true分支 + // emit_basicblock(ctx, &true_block); + // ast_stmt_to_ir(ctx, stmt->if_stmt.then_stmt); + + // // 跳转到合并块 + // scc_ir_node_t jump_node; + // init_jump_node(&jump_node, &merge_block); + // emit_instruction(ctx, &jump_node); + + // // 生成false分支(如果有) + // emit_basicblock(ctx, &false_block); + // if (stmt->if_stmt.opt_else_stmt) { + // ast_stmt_to_ir(ctx, stmt->if_stmt.opt_else_stmt); + // } + + // // 跳转到合并块 + // init_jump_node(&jump_node, &merge_block); + // emit_instruction(ctx, &jump_node); + + // // 恢复上下文并设置当前块为合并块 + // ctx->current_ctx = saved; + // emit_basicblock(ctx, &merge_block); + // break; + // } + + // case SCC_AST_STMT_WHILE: { + // // 创建基本块 - 统一使用栈上分配 + // scc_ir_bblock_t cond_block; + // scc_ir_bblock_init(&cond_block, "while_cond"); + + // scc_ir_bblock_t body_block; + // scc_ir_bblock_init(&body_block, "while_body"); + + // scc_ir_bblock_t exit_block; + // scc_ir_bblock_init(&exit_block, "while_exit"); + + // // 跳转到条件块 + // scc_ir_node_t jump_node; + // init_jump_node(&jump_node, &cond_block); + // emit_instruction(ctx, &jump_node); + + // // 保存当前块 + // ast2ir_ctx_t saved = ctx->current_ctx; + + // // 生成条件块 + // emit_basicblock(ctx, &cond_block); + // scc_ir_node_t cond_node; + // ast_expr_to_ir(ctx, stmt->while_stmt.cond, &cond_node); + + // // 将条件节点添加到程序节点列表 + // scc_vec_push(ctx->program->global_vals, cond_node); + // scc_ir_node_t *cond = + // &scc_vec_at(ctx->program->global_vals, + // scc_vec_size(ctx->program->global_vals) - 1); + + // // 创建分支 + // scc_ir_node_t br_node; + // init_branch_node(&br_node, cond, &body_block, &exit_block); + // emit_instruction(ctx, &br_node); + + // // 生成循环体 + // emit_basicblock(ctx, &body_block); + // ast_stmt_to_ir(ctx, stmt->while_stmt.body); + + // // 跳转回条件块 + // init_jump_node(&jump_node, &cond_block); + // emit_instruction(ctx, &jump_node); + + // // 恢复上下文并设置当前块为退出块 + // ctx->current_ctx = saved; + // emit_basicblock(ctx, &exit_block); + // break; + // } + + // case SCC_AST_STMT_DO_WHILE: { + // // 创建基本块 - 统一使用栈上分配 + // scc_ir_bblock_t cond_block; + // scc_ir_bblock_init(&cond_block, "do_while_cond"); + + // scc_ir_bblock_t body_block; + // scc_ir_bblock_init(&body_block, "do_while_body"); + + // scc_ir_bblock_t exit_block; + // scc_ir_bblock_init(&exit_block, "do_while_exit"); + + // // 跳转到循环体块 + // scc_ir_node_t jump_node; + // init_jump_node(&jump_node, &body_block); + // emit_instruction(ctx, &jump_node); + + // // 保存当前块 + // ast2ir_ctx_t saved = ctx->current_ctx; + + // // 生成循环体 + // emit_basicblock(ctx, &body_block); + // ast_stmt_to_ir(ctx, stmt->do_while_stmt.body); + + // // 跳转到条件块 + // init_jump_node(&jump_node, &cond_block); + // emit_instruction(ctx, &jump_node); + + // // 生成条件块 + // emit_basicblock(ctx, &cond_block); + // scc_ir_node_t cond_node; + // ast_expr_to_ir(ctx, stmt->do_while_stmt.cond, &cond_node); + + // // 将条件节点添加到程序节点列表 + // scc_vec_push(ctx->program->global_vals, cond_node); + // scc_ir_node_t *cond = + // &scc_vec_at(ctx->program->global_vals, + // scc_vec_size(ctx->program->global_vals) - 1); + + // // 创建分支 + // scc_ir_node_t br_node; + // init_branch_node(&br_node, cond, &body_block, &exit_block); + // emit_instruction(ctx, &br_node); + + // // 恢复上下文并设置当前块为退出块 + // ctx->current_ctx = saved; + // emit_basicblock(ctx, &exit_block); + // break; + // } + + // case SCC_AST_STMT_FOR: { + // // 初始化部分 + // if (SCC_AST_IS_A(scc_ast_expr_t, stmt->for_stmt.init)) { + // scc_ir_node_t dummy_node; + // ast_expr_to_ir(ctx, + // SCC_AST_CAST_TO(scc_ast_expr_t, + // stmt->for_stmt.init), &dummy_node); + // } else if (SCC_AST_IS_A(scc_ast_decl_t, stmt->for_stmt.init)) { + // ast_decl_to_ir( + // ctx, SCC_AST_CAST_TO(scc_ast_decl_t, + // stmt->for_stmt.init)); + // } else { + // UNREACHABLE(); + // } + + // // 创建基本块 - 统一使用栈上分配 + // scc_ir_bblock_t cond_block; + // scc_ir_bblock_init(&cond_block, "for_cond"); + + // scc_ir_bblock_t body_block; + // scc_ir_bblock_init(&body_block, "for_body"); + + // scc_ir_bblock_t exit_block; + // scc_ir_bblock_init(&exit_block, "for_exit"); + + // // 跳转到条件块 + // scc_ir_node_t jump_node; + // init_jump_node(&jump_node, &cond_block); + // emit_instruction(ctx, &jump_node); + + // // 保存当前块 + // ast2ir_ctx_t saved = ctx->current_ctx; + + // // 生成条件块 + // emit_basicblock(ctx, &cond_block); + + // scc_ir_node_t *cond = null; + // if (stmt->for_stmt.cond) { + // scc_ir_node_t cond_node; + // ast_expr_to_ir(ctx, stmt->for_stmt.cond, &cond_node); + + // // 将条件节点添加到程序节点列表 + // scc_vec_push(ctx->program->global_vals, cond_node); + // cond = &scc_vec_at(ctx->program->global_vals, + // scc_vec_size(ctx->program->global_vals) - + // 1); + // } else { + // // 无条件循环,条件为真 + // scc_ir_node_t true_node; + // init_const_int_node(&true_node, 1, null); + + // // 将true节点添加到程序节点列表 + // scc_vec_push(ctx->program->global_vals, true_node); + // cond = &scc_vec_at(ctx->program->global_vals, + // scc_vec_size(ctx->program->global_vals) - + // 1); + // } + + // // 创建分支 + // scc_ir_node_t br_node; + // init_branch_node(&br_node, cond, &body_block, &exit_block); + // emit_instruction(ctx, &br_node); + + // // 生成循环体 + // emit_basicblock(ctx, &body_block); + // ast_stmt_to_ir(ctx, stmt->for_stmt.body); + + // // 执行迭代表达式(如果存在) + // if (stmt->for_stmt.iter) { + // scc_ir_node_t dummy_node; + // ast_expr_to_ir(ctx, stmt->for_stmt.iter, &dummy_node); + // } + + // // 跳转回条件块 + // init_jump_node(&jump_node, &cond_block); + // emit_instruction(ctx, &jump_node); + + // // 恢复上下文并设置当前块为退出块 + // ctx->current_ctx = saved; + // emit_basicblock(ctx, &exit_block); + // break; + // } + + case SCC_AST_STMT_RETURN: { + if (stmt->return_stmt.expr) { + scc_ir_node_ref_t ret_val_node = + ast_expr_to_ir(ctx, stmt->return_stmt.expr); + + scc_ir_builder_ret(ctx, ret_val_node); + } else { + scc_ir_builder_ret_void(ctx); + } + break; + } + + default: + LOG_WARN("Unsupported statement type: %d", stmt->base.type); + break; + } +} + +// 转换AST声明为IR +static void ast_decl_to_ir(scc_ir_builder_t *ctx, scc_ast_decl_t *decl) { + if (ctx == null || decl == null) { + LOG_ERROR("Invalid argument"); + return; + } + + switch (decl->base.type) { + case SCC_AST_DECL_VAR: { + // 转换类型 + scc_ir_type_ref_t ir_type = ast_type_to_ir_type(ctx, decl->var.type); + + // 创建分配节点 + scc_ir_node_ref_t alloc_val_node = + scc_ir_builder_alloca(ctx, ir_type, decl->var.name); + + // 如果有初始化表达式 + if (!decl->var.init) { + break; + } + scc_ir_node_ref_t init_val_node = ast_expr_to_ir(ctx, decl->var.init); + + // 将初始化值节点添加到程序节点列表 + // scc_vec_push(ctx->program->global_vals, init_val_node); + // scc_ir_node_t *init_val = + // &scc_vec_at(ctx->program->global_vals, + // scc_vec_size(ctx->program->global_vals) - 1); + + scc_ir_builder_store(ctx, alloc_val_node, init_val_node); + break; + } + + case SCC_AST_DECL_FUNC: { + // TODO params name + scc_ir_type_ref_t func_type = ast_type_to_ir_type(ctx, decl->func.type); + scc_ir_builder_begin_func(ctx, decl->func.name, func_type, null); + // 处理函数体(如果有) + if (decl->func.body) { + scc_ir_builder_begin_bblock(ctx, "entry"); + ast_stmt_to_ir(ctx, decl->func.body); + scc_ir_builder_end_bblock(ctx); + } + scc_ir_builder_end_func(ctx); + break; + } + + default: + LOG_WARN("Unsupported declaration type: %d", decl->base.type); + break; + } +} + +// 主转换函数:将AST翻译单元转换为IR程序 +void scc_ast2ir(scc_ast_translation_unit_t *tu, scc_ir_builder_t *builder) { + if (tu == null || builder == null) { + LOG_ERROR("Invalid argument"); + return; + } + + // 初始化上下文 + scc_ir_builder_init(builder); + + // 转换所有声明 + scc_vec_foreach(tu->declarations, i) { + scc_ast_decl_t *decl = scc_vec_at(tu->declarations, i); + ast_decl_to_ir(builder, decl); + } +} diff --git a/libs/ast2ir/tests/test_.c b/libs/ast2ir/tests/test_.c new file mode 100644 index 0000000..1616237 --- /dev/null +++ b/libs/ast2ir/tests/test_.c @@ -0,0 +1,10 @@ +#include + +void test_example() { + printf("Test passed!\n"); +} + +int main() { + test_example(); + return 0; +} diff --git a/libs/ir/cbuild.toml b/libs/ir/cbuild.toml index 42a7cbe..6ef6640 100644 --- a/libs/ir/cbuild.toml +++ b/libs/ir/cbuild.toml @@ -4,7 +4,10 @@ version = "0.1.0" authors = [] description = "" -dependencies = [{ name = "scc_core", path = "../../runtime/scc_core" }] +dependencies = [ + { name = "scc_utils", path = "../../runtime/scc_utils" }, + { name = "tree_dump", path = "../tree_dump" }, +] # dependencies = [] # features = {} # default_features = [] diff --git a/libs/ir/include/ir_base.h b/libs/ir/include/ir_base.h new file mode 100644 index 0000000..c8ff72e --- /dev/null +++ b/libs/ir/include/ir_base.h @@ -0,0 +1,17 @@ +#ifndef __SCC_IR_BASE_H__ +#define __SCC_IR_BASE_H__ + +#include "ir_def.h" + +void scc_ir_type_init(scc_ir_type_t *in, scc_ir_type_tag_t tag); +void scc_ir_bblock_init(scc_ir_bblock_t *in, const char *label); +void scc_ir_func_init(scc_ir_func_t *in, const char *name); + +// node name can be null ptr +void scc_ir_node_init(scc_ir_node_t *in, const char *name, + scc_ir_node_tag_t tag); + +void scc_ir_cprog_init(scc_ir_cprog_t *in); +void scc_ir_cprog_drop(scc_ir_cprog_t *in); + +#endif /* __SCC_IR_BASE_H__ */ diff --git a/libs/ir/include/ir_builder.h b/libs/ir/include/ir_builder.h new file mode 100644 index 0000000..dfcab6d --- /dev/null +++ b/libs/ir/include/ir_builder.h @@ -0,0 +1,174 @@ +#ifndef __SCC_IR_BUILDER_H__ +#define __SCC_IR_BUILDER_H__ + +#include "ir_ctx.h" +#include "ir_def.h" +#include + +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_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); + +/** + * @brief 开始构建函数 + * @param name 函数名 + * @param type 函数类型 + * @param param_names 参数名列表(可为NULL) + * @return 函数引用 + */ +scc_ir_func_ref_t scc_ir_builder_begin_func(scc_ir_builder_t *builder, + const char *name, + scc_ir_type_ref_t type, + 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 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); + +/** + * @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__ */ diff --git a/libs/ir/include/ir_ctx.h b/libs/ir/include/ir_ctx.h new file mode 100644 index 0000000..6518858 --- /dev/null +++ b/libs/ir/include/ir_ctx.h @@ -0,0 +1,200 @@ +#ifndef __SCC_IR_CTX_H__ +#define __SCC_IR_CTX_H__ + +#include "ir_base.h" +#include "ir_def.h" +#include + +#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_type_ref_t builtin_i32; + scc_ir_type_ref_t builtin_i1; + scc_ir_type_ref_t builtin_void; + scc_ir_node_ref_t builtin_zero_i32; +} 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__ */ diff --git a/libs/ir/include/ir_def.h b/libs/ir/include/ir_def.h index 9bda495..5d0aa6e 100644 --- a/libs/ir/include/ir_def.h +++ b/libs/ir/include/ir_def.h @@ -3,52 +3,75 @@ #include +typedef unsigned int ir_handle_t; +typedef const char *scc_ir_label_t; + typedef struct scc_ir_node scc_ir_node_t; -typedef SCC_VEC(scc_ir_node_t) scc_ir_node_vec_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_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_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; -typedef struct scc_ir_type scc_ir_type_t; -typedef SCC_VEC(scc_ir_type_t) scc_ir_type_vec_t; - struct scc_ir_type { scc_ir_type_tag_t tag; - + // int size; // 字节大小 + // int align; // 对齐要求 union { struct { - const scc_ir_type_t *base; - usize len; + scc_ir_type_ref_t base; + usize len; // TODO usize is target dependent } array; struct { - const scc_ir_type_t *base; + scc_ir_type_ref_t base; } pointer; struct { - scc_ir_type_vec_t params; - const scc_ir_type_t *ret_type; + scc_ir_type_ref_vec_t params; + scc_ir_type_ref_t ret_type; } function; } data; }; -typedef struct scc_ir_bblock { - const char *label; - scc_ir_node_vec_t instrs; +struct scc_ir_bblock { + scc_ir_label_t label; + scc_ir_node_ref_vec_t instrs; // ir_arr_t used_by; -} scc_ir_bblock_t; // basic block -typedef SCC_VEC(scc_ir_bblock_t) scc_ir_bblock_vec_t; +}; // basic block -typedef struct { - const char *name; - scc_ir_type_t *type; - scc_ir_node_vec_t params; - scc_ir_bblock_vec_t bblocks; -} scc_ir_func_t; -typedef SCC_VEC(scc_ir_func_t) scc_ir_func_vec_t; +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, @@ -106,9 +129,9 @@ typedef enum { } scc_ir_op_type_t; struct scc_ir_node { - const scc_ir_type_t *type; - const char *name; - scc_ir_node_vec_t used_by; + 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 { @@ -117,7 +140,7 @@ struct scc_ir_node { i32 int32; i64 int64; // TODO int128 - i8 int_any[4]; + i8 int_any[16]; } const_int; union { u8 uint8; @@ -125,54 +148,54 @@ struct scc_ir_node { u32 uint32; u64 uint64; // TODO uint128; - u8 uint_any[4]; + u8 uint_any[16]; } const_uint; // aggregate; // func_arg_ref; // block_arg_ref; // global_alloc; struct { - scc_ir_node_t *target; + scc_ir_node_ref_t target; } load; struct { - scc_ir_node_t *target; - scc_ir_node_t *value; + scc_ir_node_ref_t target; + scc_ir_node_ref_t value; } store; struct { - scc_ir_node_t *src_addr; - scc_ir_node_t *index; + scc_ir_node_ref_t src_addr; + scc_ir_node_ref_t index; } get_ptr; struct { - scc_ir_node_t *src_addr; - scc_ir_node_t *index; + 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_t *lhs; - scc_ir_node_t *rhs; + scc_ir_node_ref_t lhs; + scc_ir_node_ref_t rhs; } op; struct { - scc_ir_node_t *cond; - scc_ir_bblock_t *true_bblock; - scc_ir_bblock_t *false_bblock; + 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_t *target_bblock; + scc_ir_bblock_ref_t target_bblock; } jump; struct { - scc_ir_func_t *callee; - scc_ir_node_vec_t args; + scc_ir_func_ref_t callee; + scc_ir_node_ref_vec_t args; } call; struct { - scc_ir_node_t *ret_val; + scc_ir_node_ref_t ret_val; } ret; } data; }; -typedef struct { - scc_ir_node_vec_t global_vals; - scc_ir_func_vec_t funcs; - scc_ir_func_vec_t extern_funcs; -} ir_cprog_t; +typedef struct scc_ir_cprog { + scc_ir_node_ref_vec_t global_vals; + scc_ir_func_ref_vec_t func_defs; + scc_ir_node_ref_vec_t func_decls; +} scc_ir_cprog_t; #endif /* __SCC_IR_DEF_H__ */ diff --git a/libs/ir/include/ir_dump.h b/libs/ir/include/ir_dump.h index aa4c0bd..08f2881 100644 --- a/libs/ir/include/ir_dump.h +++ b/libs/ir/include/ir_dump.h @@ -1,16 +1,21 @@ #ifndef __SCC_IR_DUMP_H__ #define __SCC_IR_DUMP_H__ +#include "ir_ctx.h" #include "ir_def.h" +#include typedef struct { - + scc_ir_cprog_ctx_t *ir_ctx; + scc_ir_cprog_t *cprog; + scc_tree_dump_ctx_t *dump_ctx; } scc_ir_dump_ctx_t; +void scc_ir_dump_ctx_init(); -void scc_ir_type_dump(scc_ir_dump_ctx_t *ctx, const scc_ir_type_t *in); -void scc_ir_bblock_dump(scc_ir_dump_ctx_t *ctx, const scc_ir_bblock_t *in); -void scc_ir_func_dump(scc_ir_dump_ctx_t *ctx, const scc_ir_func_t *in); -void scc_ir_node_dump(scc_ir_dump_ctx_t *ctx, scc_ir_node_t *in); -void scc_ir_cprog_dump(scc_ir_dump_ctx_t *ctx, ir_cprog_t *in); +void scc_ir_dump_node(scc_ir_dump_ctx_t *ctx, scc_ir_node_ref_t node_ref); +void scc_ir_dump_type(scc_ir_dump_ctx_t *ctx, scc_ir_type_ref_t type_ref); +void scc_ir_dump_bblock(scc_ir_dump_ctx_t *ctx, scc_ir_bblock_ref_t bblock_ref); +void scc_ir_dump_func(scc_ir_dump_ctx_t *ctx, scc_ir_func_ref_t func_ref); +void scc_ir_dump_cprog(scc_ir_dump_ctx_t *ctx); #endif /* __SCC_IR_DUMP_H__ */ diff --git a/libs/ir/include/scc_ir.h b/libs/ir/include/scc_ir.h index f543118..4680e40 100644 --- a/libs/ir/include/scc_ir.h +++ b/libs/ir/include/scc_ir.h @@ -1,27 +1,9 @@ #ifndef __SCC_IR_H__ #define __SCC_IR_H__ +#include "ir_builder.h" +#include "ir_ctx.h" #include "ir_def.h" - -void scc_ir_type_init(scc_ir_type_t *in, scc_ir_type_tag_t tag); -void scc_ir_bblock_init(scc_ir_bblock_t *in, const char *label); -void scc_ir_func_init(scc_ir_func_t *in, const char *name); - -// node name can be null ptr -void scc_ir_node_init(scc_ir_node_t *in, const char *name, - scc_ir_node_tag_t tag); - -void scc_ir_cprog_init(ir_cprog_t *in); - -scc_ir_type_t *scc_ir_type_alloc(scc_ir_type_tag_t tag); -scc_ir_bblock_t *scc_ir_bblock_alloc(const char *label); -scc_ir_func_t *scc_ir_func_alloc(const char *name); -scc_ir_node_t *scc_ir_node_alloc(const char *name, scc_ir_node_tag_t tag); - -// scc_ir_type_t *scc_ir_type_alloc_with_ctx(scc_ir_type_tag_t tag); -// scc_ir_bblock_t *scc_ir_bblock_alloc_with_ctx(const char *label); -// scc_ir_func_t *scc_ir_func_alloc_with_ctx(const char *name); -// scc_ir_node_t *scc_ir_node_alloc_with_ctx(const char *name, -// scc_ir_node_tag_t tag); +#include #endif /* __SCC_IR_H__ */ diff --git a/libs/ir/src/scc_ir.c b/libs/ir/src/ir_base.c similarity index 51% rename from libs/ir/src/scc_ir.c rename to libs/ir/src/ir_base.c index a3848b2..88bec41 100644 --- a/libs/ir/src/scc_ir.c +++ b/libs/ir/src/ir_base.c @@ -1,19 +1,19 @@ -#include +#include void scc_ir_type_init(scc_ir_type_t *in, scc_ir_type_tag_t tag) { Assert(in != null); in->tag = tag; switch (tag) { case SCC_IR_TYPE_ARRAY: - in->data.array.base = null; + in->data.array.base = 0; in->data.array.len = 0; break; case SCC_IR_TYPE_PTR: - in->data.pointer.base = null; + in->data.pointer.base = 0; break; case SCC_IR_TYPE_FUNC: scc_vec_init(in->data.function.params); - in->data.function.ret_type = null; + in->data.function.ret_type = 0; break; case SCC_IR_TYPE_VOID: case SCC_IR_TYPE_I32: @@ -35,7 +35,7 @@ void scc_ir_func_init(scc_ir_func_t *in, const char *name) { Assert(in != null); Assert(name != null); in->name = name; - in->type = null; + in->type = 0; scc_vec_init(in->bblocks); scc_vec_init(in->params); } @@ -46,7 +46,7 @@ void scc_ir_node_init(scc_ir_node_t *in, const char *name, in->name = name; in->tag = tag; scc_vec_init(in->used_by); - in->type = null; + in->type = 0; switch (tag) { case SCC_IR_NODE_NULL: @@ -56,38 +56,38 @@ void scc_ir_node_init(scc_ir_node_t *in, const char *name, in->data.const_int.int32 = 0; break; case SCC_IR_NODE_ALLOC: - TODO(); + // TODO(); break; case SCC_IR_NODE_LOAD: - in->data.load.target = null; + in->data.load.target = 0; break; case SCC_IR_NODE_STORE: - in->data.store.target = null; - in->data.store.value = null; + in->data.store.target = 0; + in->data.store.value = 0; break; case SCC_IR_NODE_GET_PTR: - in->data.get_ptr.src_addr = null; - in->data.get_ptr.index = null; + in->data.get_ptr.src_addr = 0; + in->data.get_ptr.index = 0; break; case SCC_IR_NODE_OP: in->data.op.op = IR_OP_EMPTY; - in->data.op.lhs = null; - in->data.op.rhs = null; + in->data.op.lhs = 0; + in->data.op.rhs = 0; break; case SCC_IR_NODE_BRANCH: - in->data.branch.cond = null; - in->data.branch.true_bblock = null; - in->data.branch.false_bblock = null; + in->data.branch.cond = 0; + in->data.branch.true_bblock = 0; + in->data.branch.false_bblock = 0; break; case SCC_IR_NODE_JUMP: - in->data.jump.target_bblock = null; + in->data.jump.target_bblock = 0; break; case SCC_IR_NODE_CALL: scc_vec_init(in->data.call.args); - in->data.call.callee = null; + in->data.call.callee = 0; break; case SCC_IR_NODE_RET: - in->data.ret.ret_val = null; + in->data.ret.ret_val = 0; break; default: UNREACHABLE(); @@ -95,34 +95,14 @@ void scc_ir_node_init(scc_ir_node_t *in, const char *name, } } -void scc_ir_cprog_init(ir_cprog_t *in) { - Assert(in != null); - scc_vec_init(in->extern_funcs); - scc_vec_init(in->funcs); +void scc_ir_cprog_init(scc_ir_cprog_t *in) { + scc_vec_init(in->func_decls); + scc_vec_init(in->func_defs); scc_vec_init(in->global_vals); } -scc_ir_type_t *scc_ir_type_alloc(scc_ir_type_tag_t tag) { - scc_ir_type_t *ret = scc_malloc(sizeof(scc_ir_type_t)); - Assert(ret != null); - scc_ir_type_init(ret, tag); - return ret; -} -scc_ir_bblock_t *scc_ir_bblock_alloc(const char *label) { - scc_ir_bblock_t *ret = scc_malloc(sizeof(scc_ir_bblock_t)); - Assert(ret != null); - scc_ir_bblock_init(ret, label); - return ret; -} -scc_ir_func_t *scc_ir_func_alloc(const char *name) { - scc_ir_func_t *ret = scc_malloc(sizeof(scc_ir_func_t)); - Assert(ret != null); - scc_ir_func_init(ret, name); - return ret; -} -scc_ir_node_t *scc_ir_node_alloc(const char *name, scc_ir_node_tag_t tag) { - scc_ir_node_t *ret = scc_malloc(sizeof(scc_ir_node_t)); - Assert(ret != null); - scc_ir_node_init(ret, name, tag); - return ret; +void scc_ir_cprog_drop(scc_ir_cprog_t *in) { + scc_vec_free(in->func_decls); + scc_vec_free(in->func_defs); + scc_vec_free(in->global_vals); } diff --git a/libs/ir/src/ir_builder.c b/libs/ir/src/ir_builder.c new file mode 100644 index 0000000..f1e6b3f --- /dev/null +++ b/libs/ir/src/ir_builder.c @@ -0,0 +1,351 @@ +#include +#include + +void scc_ir_builder_init(scc_ir_builder_t *builder) { + builder->current_bblock = SCC_IR_REF_NULL; + builder->current_func = SCC_IR_REF_NULL; + + scc_ir_cprog_init(&builder->cprog); + scc_ir_ctx_init(&builder->ctx); +} + +void scc_ir_builder_drop(scc_ir_builder_t *builder) { + scc_ir_cprog_drop(&builder->cprog); + scc_ir_ctx_drop(&builder->ctx); +} + +scc_ir_func_ref_t scc_ir_builder_begin_func(scc_ir_builder_t *builder, + const char *name, + scc_ir_type_ref_t type, + const char **param_names) { + scc_ir_func_t func = {0}; + + // 初始化参数和基本块向量 + func.name = name; + func.type = type; + scc_vec_init(func.params); + scc_vec_init(func.bblocks); + + // 创建函数并设置为当前函数 + scc_ir_func_ref_t func_ref = scc_ir_ctx_new_func(&builder->ctx, &func); + builder->current_func = func_ref; + + // 如果提供了参数名称,则创建参数节点 + if (param_names == null) { + return func_ref; + } + + scc_ir_func_t *func_ptr = scc_ir_ctx_get_func(&builder->ctx, func_ref); + scc_ir_type_t *func_type = scc_ir_ctx_get_type(&builder->ctx, type); + + if (func_type == null || func_type->tag != SCC_IR_TYPE_FUNC) { + LOG_ERROR("Invalid function type"); + return func_ref; + } + + scc_vec_foreach(func_type->data.function.params, i) { + scc_ir_type_ref_t param_type = + scc_vec_at(func_type->data.function.params, i); + + scc_ir_node_t param_node = {0}; + param_node.tag = SCC_IR_NODE_NULL; // 参数节点标记 + param_node.type = param_type; + param_node.name = param_names[i]; + scc_vec_init(param_node.used_by); + + scc_ir_node_ref_t param_ref = + scc_ir_ctx_new_node(&builder->ctx, ¶m_node); + scc_vec_push(func_ptr->params, param_ref); + } + + return func_ref; +} + +void scc_ir_builder_end_func(scc_ir_builder_t *builder) { + scc_ir_func_t *func_ptr = + scc_ir_ctx_get_func(&builder->ctx, builder->current_func); + if (func_ptr == null) { + LOG_FATAL("Invalid function reference"); + return; + } + if (scc_vec_size(func_ptr->bblocks) == 0) { + scc_vec_push(builder->cprog.func_decls, builder->current_func); + } else { + scc_vec_push(builder->cprog.func_defs, builder->current_func); + } + builder->current_func = 0; +} + +scc_ir_func_ref_t scc_ir_builder_current_func(scc_ir_builder_t *builder) { + return builder->current_func; +} + +scc_ir_bblock_ref_t scc_ir_builder_begin_bblock(scc_ir_builder_t *builder, + const char *label) { + scc_ir_bblock_t bblock = {0}; + + if (label) { + bblock.label = label; + } else { + // TODO 自动生成标签 + char *auto_label = scc_malloc(sizeof(char) * 64); + if (auto_label) + LOG_FATAL("allocate memory failed"); + static int auto_counter = 0; + scc_snprintf(auto_label, sizeof(auto_label), ".BB%d", auto_counter++); + bblock.label = auto_label; + } + + scc_vec_init(bblock.instrs); + + scc_ir_bblock_ref_t bblock_ref = + scc_ir_ctx_new_bblock(&builder->ctx, &bblock); + builder->current_bblock = bblock_ref; + + // 将基本块添加到当前函数 + scc_ir_func_t *current_func = + scc_ir_ctx_get_func(&builder->ctx, builder->current_func); + if (current_func) { + scc_vec_push(current_func->bblocks, bblock_ref); + } + + return bblock_ref; +} + +void scc_ir_builder_end_bblock(scc_ir_builder_t *builder) { + builder->current_bblock = 0; +} + +void scc_ir_builder_set_current_bblock(scc_ir_builder_t *builder, + scc_ir_bblock_ref_t bblock) { + builder->current_bblock = bblock; +} + +static void scc_ir_builder_add_instr(scc_ir_builder_t *builder, + scc_ir_node_ref_t node) { + scc_ir_bblock_t *current_bblock = + scc_ir_ctx_get_bblock(&builder->ctx, builder->current_bblock); + if (current_bblock) { + scc_vec_push(current_bblock->instrs, node); + } else { + LOG_ERROR("Current basic block is not set"); + } +} + +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_t alloc_node = {0}; + alloc_node.tag = SCC_IR_NODE_ALLOC; + alloc_node.type = scc_ir_ctx_new_type( + &builder->ctx, + &(scc_ir_type_t){.tag = SCC_IR_TYPE_PTR, .data.pointer.base = type}); + alloc_node.name = name; + + scc_ir_node_ref_t node_ref = + scc_ir_ctx_new_node(&builder->ctx, &alloc_node); + + // 添加到当前基本块 + scc_ir_builder_add_instr(builder, node_ref); + + return node_ref; +} + +scc_ir_node_ref_t scc_ir_builder_load(scc_ir_builder_t *builder, + scc_ir_node_ref_t ptr) { + scc_ir_node_t load_node = {0}; + load_node.tag = SCC_IR_NODE_LOAD; + load_node.data.load.target = ptr; + + // 设置类型为指针指向的类型 + scc_ir_node_t *ptr_node = scc_ir_ctx_get_node(&builder->ctx, ptr); + if (ptr_node) { + scc_ir_type_t *ptr_type = + scc_ir_ctx_get_type(&builder->ctx, ptr_node->type); + if (ptr_type && ptr_type->tag == SCC_IR_TYPE_PTR) { + load_node.type = ptr_type->data.pointer.base; + } + } + + scc_ir_node_ref_t node_ref = scc_ir_ctx_new_node(&builder->ctx, &load_node); + + // 添加到当前基本块 + scc_ir_builder_add_instr(builder, node_ref); + + return node_ref; +} + +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) { + scc_ir_node_t store_node = {0}; + store_node.tag = SCC_IR_NODE_STORE; + store_node.data.store.target = ptr; + store_node.data.store.value = value; + + scc_ir_node_ref_t node_ref = + scc_ir_ctx_new_node(&builder->ctx, &store_node); + + // 添加到当前基本块 + scc_ir_builder_add_instr(builder, node_ref); + + return node_ref; +} + +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) { + scc_ir_node_t get_ptr_node = {0}; + get_ptr_node.tag = SCC_IR_NODE_GET_PTR; + get_ptr_node.data.get_ptr.src_addr = ptr; + get_ptr_node.data.get_ptr.index = index; + + // 类型应与源地址相同(都是指针) + scc_ir_node_t *src_node = scc_ir_ctx_get_node(&builder->ctx, ptr); + if (src_node) { + get_ptr_node.type = src_node->type; + } + + scc_ir_node_ref_t node_ref = + scc_ir_ctx_new_node(&builder->ctx, &get_ptr_node); + + // 添加到当前基本块 + scc_ir_builder_add_instr(builder, node_ref); + + return node_ref; +} + +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) { + scc_ir_node_t binop_node = {0}; + binop_node.tag = SCC_IR_NODE_OP; + binop_node.data.op.op = op; + binop_node.data.op.lhs = lhs; + binop_node.data.op.rhs = rhs; + + // 类型通常与操作数相同(对于算术运算) + scc_ir_node_t *lhs_node = scc_ir_ctx_get_node(&builder->ctx, lhs); + if (lhs_node) { + binop_node.type = lhs_node->type; + } + + scc_ir_node_ref_t node_ref = + scc_ir_ctx_new_node(&builder->ctx, &binop_node); + + // 添加到当前基本块 + scc_ir_builder_add_instr(builder, node_ref); + + return node_ref; +} + +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) { + scc_ir_node_t cmp_node = {0}; + cmp_node.tag = SCC_IR_NODE_OP; + cmp_node.data.op.op = op; + cmp_node.data.op.lhs = lhs; + cmp_node.data.op.rhs = rhs; + + // 比较操作的结果通常是布尔值 + cmp_node.type = scc_ir_ctx_get_builtin_i32(&builder->ctx); + + scc_ir_node_ref_t node_ref = scc_ir_ctx_new_node(&builder->ctx, &cmp_node); + + // 添加到当前基本块 + scc_ir_builder_add_instr(builder, node_ref); + + return node_ref; +} + +scc_ir_node_ref_t scc_ir_builder_jump(scc_ir_builder_t *builder, + scc_ir_bblock_ref_t target) { + scc_ir_node_t jump_node = {0}; + jump_node.tag = SCC_IR_NODE_JUMP; + jump_node.data.jump.target_bblock = target; + + scc_ir_node_ref_t node_ref = scc_ir_ctx_new_node(&builder->ctx, &jump_node); + + // 添加到当前基本块 + scc_ir_builder_add_instr(builder, node_ref); + + return node_ref; +} + +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) { + scc_ir_node_t branch_node = {0}; + branch_node.tag = SCC_IR_NODE_BRANCH; + branch_node.data.branch.cond = cond; + branch_node.data.branch.true_bblock = true_target; + branch_node.data.branch.false_bblock = false_target; + + scc_ir_node_ref_t node_ref = + scc_ir_ctx_new_node(&builder->ctx, &branch_node); + + // 添加到当前基本块 + scc_ir_builder_add_instr(builder, node_ref); + + return node_ref; +} + +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) { + scc_ir_node_t call_node = {0}; + call_node.tag = SCC_IR_NODE_CALL; + call_node.data.call.callee = callee; + + scc_vec_init(call_node.data.call.args); + for (usize i = 0; i < arg_count; i++) { + scc_vec_push(call_node.data.call.args, args[i]); + } + + // 设置返回类型为被调用函数的返回类型 + scc_ir_func_t *callee_func = scc_ir_ctx_get_func(&builder->ctx, callee); + if (callee_func) { + scc_ir_type_t *func_type = + scc_ir_ctx_get_type(&builder->ctx, callee_func->type); + if (func_type && func_type->tag == SCC_IR_TYPE_FUNC) { + call_node.type = func_type->data.function.ret_type; + } + } + + scc_ir_node_ref_t node_ref = scc_ir_ctx_new_node(&builder->ctx, &call_node); + + // 添加到当前基本块 + scc_ir_builder_add_instr(builder, node_ref); + + return node_ref; +} + +scc_ir_node_ref_t scc_ir_builder_ret(scc_ir_builder_t *builder, + scc_ir_node_ref_t value) { + scc_ir_node_t ret_node = {0}; + ret_node.tag = SCC_IR_NODE_RET; + ret_node.data.ret.ret_val = value; + + scc_ir_node_ref_t node_ref = scc_ir_ctx_new_node(&builder->ctx, &ret_node); + + // 添加到当前基本块 + scc_ir_builder_add_instr(builder, node_ref); + + return node_ref; +} + +scc_ir_node_ref_t scc_ir_builder_ret_void(scc_ir_builder_t *builder) { + scc_ir_node_t ret_node = {0}; + ret_node.tag = SCC_IR_NODE_RET; + ret_node.data.ret.ret_val = 0; // 无返回值 + + scc_ir_node_ref_t node_ref = scc_ir_ctx_new_node(&builder->ctx, &ret_node); + + // 添加到当前基本块 + scc_ir_builder_add_instr(builder, node_ref); + return node_ref; +} diff --git a/libs/ir/src/ir_ctx.c b/libs/ir/src/ir_ctx.c new file mode 100644 index 0000000..b9cd6ce --- /dev/null +++ b/libs/ir/src/ir_ctx.c @@ -0,0 +1,345 @@ +#include "ir_ctx.h" +#include "ir_builtin.h" + +/** + * @brief 哈希混合函数(类似Rust的hash combine) + * + * Rust的默认哈希实现(SipHash 1-3)使用旋转和异或, + * 这里使用简单的FNV-1a变体,类似于Rust的`#[derive(Hash)]` + */ +static inline u32 scc_hash_mix(u32 seed, u32 value) { + // FNV-1a风格混合 + return (seed ^ value) * 16777619u; +} + +static u32 hash_key(const void *key) { return (u32)(usize)key; } +static int cmp_key(const void *key1, const void *key2) { + return (u32)(usize)key1 != (u32)(usize)key2; +} +static u32 hash_type(const scc_ir_type_t *key) { + // 初始哈希:tag + u32 hash = (u32)key->tag; + + switch (key->tag) { + case SCC_IR_TYPE_VOID: + case SCC_IR_TYPE_I1: + case SCC_IR_TYPE_I8: + case SCC_IR_TYPE_I16: + case SCC_IR_TYPE_I32: + case SCC_IR_TYPE_I64: + case SCC_IR_TYPE_I128: + case SCC_IR_TYPE_F16: + case SCC_IR_TYPE_F32: + case SCC_IR_TYPE_F64: + case SCC_IR_TYPE_F128: + // 基本类型,只有tag + break; + + case SCC_IR_TYPE_PTR: + hash = scc_hash_mix(hash, (u32)key->data.array.base); + break; + + case SCC_IR_TYPE_ARRAY: + hash = scc_hash_mix(hash, (u32)key->data.array.base); + hash = scc_hash_mix(hash, (u32)key->data.array.len); + break; + + case SCC_IR_TYPE_FUNC: + // 注意:这里需要递归哈希参数类型 + hash = scc_hash_mix(hash, (u32)key->data.function.ret_type); + for (usize i = 0; i < key->data.function.params.size; i++) { + hash = scc_hash_mix(hash, (u32)key->data.function.params.data[i]); + } + hash = scc_hash_mix(hash, (u32)key->data.function.params.size); + break; + + case SCC_IR_TYPE_STRUCT: + case SCC_IR_TYPE_VECTOR: + default: + UNREACHABLE(); + return 0; + } + + return hash; +} +static int cmp_type(const scc_ir_type_t *key1, const scc_ir_type_t *key2) { + // tag不同 + if (key1->tag != key2->tag) { + return 1; + } + + switch (key1->tag) { + case SCC_IR_TYPE_VOID: + case SCC_IR_TYPE_I1: + case SCC_IR_TYPE_I8: + case SCC_IR_TYPE_I16: + case SCC_IR_TYPE_I32: + case SCC_IR_TYPE_I64: + case SCC_IR_TYPE_I128: + case SCC_IR_TYPE_F16: + case SCC_IR_TYPE_F32: + case SCC_IR_TYPE_F64: + case SCC_IR_TYPE_F128: + return 0; // 基本类型,tag相同即可 + case SCC_IR_TYPE_PTR: + return key1->data.pointer.base != key2->data.pointer.base; + + case SCC_IR_TYPE_ARRAY: + return (key1->data.array.base != key2->data.array.base) || + (key1->data.array.len != key2->data.array.len); + + case SCC_IR_TYPE_FUNC: { + if (key1->data.function.ret_type != key2->data.function.ret_type) { + return 1; + } + + if (key1->data.function.params.size != + key2->data.function.params.size) { + return 1; + } + + for (usize i = 0; i < key1->data.function.params.size; i++) { + if (key1->data.function.params.data[i] != + key2->data.function.params.data[i]) { + return 1; + } + } + return 0; + } + default: + UNREACHABLE(); + return 1; + } + return 1; +} + +void scc_ir_ctx_init(scc_ir_cprog_ctx_t *ctx) { + // 初始化向量 + scc_vec_init(ctx->nodes); + scc_vec_init(ctx->types); + scc_vec_init(ctx->bblocks); + scc_vec_init(ctx->funcs); + + // 设置哈希函数 + ctx->uid2nodes.hash_func = hash_key; + ctx->uid2nodes.key_cmp = cmp_key; + ctx->uid2types.hash_func = hash_key; + ctx->uid2types.key_cmp = cmp_key; + ctx->uid2bblocks.hash_func = hash_key; + ctx->uid2bblocks.key_cmp = cmp_key; + ctx->uid2funcs.hash_func = hash_key; + ctx->uid2funcs.key_cmp = cmp_key; + // 初始化哈希表 + scc_hashtable_init(&ctx->uid2nodes); + scc_hashtable_init(&ctx->uid2types); + scc_hashtable_init(&ctx->uid2bblocks); + scc_hashtable_init(&ctx->uid2funcs); + + ctx->type_uniquing.hash_func = (void *)hash_type; + ctx->type_uniquing.key_cmp = (void *)cmp_type; + scc_hashtable_init(&ctx->type_uniquing); + + // 预留UID 0 作为无效引用 + ctx->node_uid = 1; + ctx->type_uid = 1; + ctx->bblock_uid = 1; + ctx->func_uid = 1; +} + +void scc_ir_ctx_drop(scc_ir_cprog_ctx_t *ctx) { + // 释放所有实体的内部内存 + for (usize i = 0; i < ctx->nodes.size; i++) { + scc_ir_node_t *node = &ctx->nodes.data[i]; + scc_vec_free(node->used_by); + if (node->tag == SCC_IR_NODE_CALL) { + scc_vec_free(node->data.call.args); + } + } + + for (usize i = 0; i < ctx->types.size; i++) { + scc_ir_type_t *type = &ctx->types.data[i]; + if (type->tag == SCC_IR_TYPE_FUNC) { + scc_vec_free(type->data.function.params); + } + } + + for (usize i = 0; i < ctx->bblocks.size; i++) { + scc_ir_bblock_t *bblock = &ctx->bblocks.data[i]; + scc_vec_free(bblock->instrs); + } + + for (usize i = 0; i < ctx->funcs.size; i++) { + scc_ir_func_t *func = &ctx->funcs.data[i]; + scc_vec_free(func->params); + scc_vec_free(func->bblocks); + } + + // 释放向量 + scc_vec_free(ctx->nodes); + scc_vec_free(ctx->types); + scc_vec_free(ctx->bblocks); + scc_vec_free(ctx->funcs); + + // 释放哈希表 + scc_hashtable_drop(&ctx->uid2nodes); + scc_hashtable_drop(&ctx->uid2types); + scc_hashtable_drop(&ctx->uid2bblocks); + scc_hashtable_drop(&ctx->uid2funcs); +} + +void scc_ir_ctx_reset(scc_ir_cprog_ctx_t *ctx) { + // 先销毁再重新初始化 + scc_ir_ctx_drop(ctx); + scc_ir_ctx_init(ctx); +} + +// 辅助宏:创建实体并添加到哈希表 +#define CREATE_ENTITY(ctx, vec, uid, data, hashtable) \ + do { \ + /* 分配新UID */ \ + unsigned new_uid = (ctx)->uid++; \ + /* 添加到向量 */ \ + scc_vec_push((vec), *(data)); \ + /* 添加到哈希表 */ \ + scc_hashtable_set(&(ctx)->hashtable, (const void *)(usize)new_uid, \ + (void *)(usize)(scc_vec_size(vec) - 1)); \ + return new_uid; \ + } while (0) + +scc_ir_type_ref_t scc_ir_ctx_new_type(scc_ir_cprog_ctx_t *ctx, + const scc_ir_type_t *type) { + // 首先检查去重表 + void *existing = scc_hashtable_get(&ctx->type_uniquing, type); + if (existing) { + return (scc_ir_type_ref_t)(uintptr_t)existing; + } + + // 创建新类型 + unsigned new_uid = ctx->type_uid++; + scc_vec_push(ctx->types, *type); + + // 添加到UID映射 + usize idx = ctx->types.size - 1; + scc_hashtable_set(&ctx->uid2types, &new_uid, (void *)idx); + + // 添加到去重表 + scc_hashtable_set(&ctx->type_uniquing, + &ctx->types.data[idx], // 使用向量中的地址作为键 + (void *)(uintptr_t)new_uid); + + return new_uid; +} + +scc_ir_node_ref_t scc_ir_ctx_new_node(scc_ir_cprog_ctx_t *ctx, + const scc_ir_node_t *node) { + CREATE_ENTITY(ctx, ctx->nodes, node_uid, node, uid2nodes); +} + +scc_ir_bblock_ref_t scc_ir_ctx_new_bblock(scc_ir_cprog_ctx_t *ctx, + const scc_ir_bblock_t *bblock) { + CREATE_ENTITY(ctx, ctx->bblocks, bblock_uid, bblock, uid2bblocks); +} + +scc_ir_func_ref_t scc_ir_ctx_new_func(scc_ir_cprog_ctx_t *ctx, + const scc_ir_func_t *func) { + CREATE_ENTITY(ctx, ctx->funcs, func_uid, func, uid2funcs); +} + +#undef CREATE_ENTITY + +// 辅助宏:从哈希表获取索引 +#define GET_ENTITY_INDEX(ctx, ref, hashtable) \ + ((usize)scc_hashtable_get(&(ctx)->hashtable, (void *)(usize)ref)) + +scc_ir_type_t *scc_ir_ctx_get_type(scc_ir_cprog_ctx_t *ctx, + scc_ir_type_ref_t ref) { + if (ref == 0) + return null; + usize idx = GET_ENTITY_INDEX(ctx, ref, uid2types); + if (idx >= ctx->types.size) + return null; + return &ctx->types.data[idx]; +} + +scc_ir_node_t *scc_ir_ctx_get_node(scc_ir_cprog_ctx_t *ctx, + scc_ir_node_ref_t ref) { + if (ref == 0) + return null; + usize idx = GET_ENTITY_INDEX(ctx, ref, uid2nodes); + if (idx >= ctx->nodes.size) + return null; + return &ctx->nodes.data[idx]; +} + +scc_ir_bblock_t *scc_ir_ctx_get_bblock(scc_ir_cprog_ctx_t *ctx, + scc_ir_bblock_ref_t ref) { + if (ref == 0) + return null; + usize idx = GET_ENTITY_INDEX(ctx, ref, uid2bblocks); + if (idx >= ctx->bblocks.size) + return null; + return &ctx->bblocks.data[idx]; +} + +scc_ir_func_t *scc_ir_ctx_get_func(scc_ir_cprog_ctx_t *ctx, + scc_ir_func_ref_t ref) { + if (ref == 0) + return null; + usize idx = GET_ENTITY_INDEX(ctx, ref, uid2funcs); + if (idx >= ctx->funcs.size) + return null; + return &ctx->funcs.data[idx]; +} + +#undef GET_ENTITY_INDEX + +// 内置类型和常量缓存 +static scc_ir_type_ref_t cached_i32_type = 0; +static scc_ir_node_ref_t cached_zero_const = 0; + +scc_ir_type_ref_t scc_ir_ctx_get_builtin_i32(scc_ir_cprog_ctx_t *ctx) { + if (cached_i32_type == 0) { + scc_ir_type_t i32_type = {.tag = SCC_IR_TYPE_I32}; + cached_i32_type = scc_ir_ctx_new_type(ctx, &i32_type); + } + return cached_i32_type; +} + +scc_ir_node_ref_t scc_ir_ctx_get_builtin_zero(scc_ir_cprog_ctx_t *ctx) { + if (cached_zero_const == 0) { + scc_ir_node_t zero_node = {.tag = SCC_IR_NODE_CONST_INT, + .type = scc_ir_ctx_get_builtin_i32(ctx), + .name = "zero", + .data.const_int.int32 = 0}; + cached_zero_const = scc_ir_ctx_new_node(ctx, &zero_node); + } + return cached_zero_const; +} + +// 常量池(简化版) +typedef struct { + i32 value; + scc_ir_node_ref_t ref; +} i32_const_entry_t; + +scc_ir_node_ref_t scc_ir_ctx_get_i32_const(scc_ir_cprog_ctx_t *ctx, i32 value) { + // 如果是0,使用内置zero + if (value == 0) { + return scc_ir_ctx_get_builtin_zero(ctx); + } + + // TODO: 实现常量池哈希表 + // 这里简化实现,每次都创建新常量 + scc_ir_node_t const_node = {.tag = SCC_IR_NODE_CONST_INT, + .type = scc_ir_ctx_get_builtin_i32(ctx), + .data.const_int.int32 = value}; + + const_node.name = ""; + + return scc_ir_ctx_new_node(ctx, &const_node); +} + +scc_ir_node_ref_t scc_ir_ctx_get_null_const(scc_ir_cprog_ctx_t *ctx, + scc_ir_type_ref_t ptr_type) { + return 1; +} diff --git a/libs/ir/src/ir_dump.c b/libs/ir/src/ir_dump.c index 9cd61c4..7062fab 100644 --- a/libs/ir/src/ir_dump.c +++ b/libs/ir/src/ir_dump.c @@ -1,13 +1,522 @@ +#include #include -void scc_ir_type_dump(scc_ir_dump_ctx_t *ctx, const scc_ir_type_t *in) { - return; +#define PRINT_VALUE(ctx, fmt, value) \ + SCC_TREE_DUMP_PRINT_PURE(ctx, ctx->value_color, fmt, value) + +#define PRINT_NODE(ctx, name) \ + SCC_TREE_DUMP_PRINT_PURE(ctx, ctx->node_color, "%s", name) + +#define PRINT_QUOTED_VALUE(ctx, str) \ + SCC_TREE_DUMP_PRINT_AROUND(ctx, ctx->value_color, "'", "%s", str) + +// 获取IR节点类型的字符串表示 +static const char *get_node_type_str(scc_ir_node_tag_t tag) { + static const char *node_types[] = { + [SCC_IR_NODE_NULL] = "Null", [SCC_IR_NODE_CONST_INT] = "ConstInt", + [SCC_IR_NODE_ALLOC] = "Alloc", [SCC_IR_NODE_LOAD] = "Load", + [SCC_IR_NODE_STORE] = "Store", [SCC_IR_NODE_GET_PTR] = "GetElementPtr", + [SCC_IR_NODE_OP] = "Op", [SCC_IR_NODE_BRANCH] = "Branch", + [SCC_IR_NODE_JUMP] = "Jump", [SCC_IR_NODE_CALL] = "Call", + [SCC_IR_NODE_RET] = "Return", + }; + + if (tag >= 0 && tag < sizeof(node_types) / sizeof(node_types[0]) && + node_types[tag] != NULL) { + return node_types[tag]; + } + return "UnknownIRNode"; } -void scc_ir_bblock_dump(scc_ir_dump_ctx_t *ctx, const scc_ir_bblock_t *in) { - return; + +// 获取操作符字符串 +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 + }; + + if (op >= 0 && op < sizeof(ops) / sizeof(ops[0]) && ops[op] != NULL) { + return ops[op]; + } + return ""; } -void scc_ir_func_dump(scc_ir_dump_ctx_t *ctx, const scc_ir_func_t *in) { - return; + +// 获取类型标签字符串 +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_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", + [SCC_IR_TYPE_F32] = "f32", [SCC_IR_TYPE_F64] = "f64", + [SCC_IR_TYPE_F128] = "f128", [SCC_IR_TYPE_PTR] = "ptr", + [SCC_IR_TYPE_ARRAY] = "array", [SCC_IR_TYPE_FUNC] = "func", + [SCC_IR_TYPE_STRUCT] = "struct", [SCC_IR_TYPE_VECTOR] = "vector", + }; + + if (tag >= 0 && tag < sizeof(type_tags) / sizeof(type_tags[0]) && + type_tags[tag] != NULL) { + return type_tags[tag]; + } + return ""; +} + +// 递归转储辅助函数(使用引用) +static inline void dump_child_node_ref(scc_ir_dump_ctx_t *ctx, + scc_ir_node_ref_t child_ref, + cbool is_last) { + if (!child_ref) + return; + scc_tree_dump_push_level(ctx->dump_ctx, is_last); + scc_ir_dump_node(ctx, child_ref); + scc_tree_dump_pop_level(ctx->dump_ctx); +} + +// 转储常量整数节点 +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_pop_level(ctx->dump_ctx); +} + +// 转储操作节点 +static void dump_op_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) { + // 打印操作符 + scc_tree_dump_push_level(ctx->dump_ctx, false); + scc_tree_print_indent(ctx->dump_ctx); + SCC_TREE_DUMP_PRINT_PURE(ctx->dump_ctx, ctx->dump_ctx->node_color, + "op: %s\n", get_op_str(node->data.op.op)); + scc_tree_dump_pop_level(ctx->dump_ctx); + + // 左操作数 + if (node->data.op.lhs) { + dump_child_node_ref(ctx, node->data.op.lhs, + node->data.op.rhs ? false : true); + } + + // 右操作数 + if (node->data.op.rhs) { + dump_child_node_ref(ctx, node->data.op.rhs, true); + } +} + +// 转储加载节点 +static void dump_load_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) { + // PRINT_NODE(ctx->dump_ctx, "load"); + if (node->data.load.target) { + dump_child_node_ref(ctx, node->data.load.target, true); + } +} + +// 转储存储节点 +static void dump_store_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) { + // PRINT_NODE(ctx->dump_ctx, "store"); + + // 输出存储位置 + if (node->data.store.target) { + dump_child_node_ref(ctx, node->data.store.target, false); + } + + // 输出存储的值 + if (node->data.store.value) { + dump_child_node_ref(ctx, node->data.store.value, true); + } +} + +// 转储获取指针节点 +static void dump_get_ptr_node(scc_ir_dump_ctx_t *ctx, + const scc_ir_node_t *node) { + // PRINT_NODE(ctx->dump_ctx, "get_ptr"); + // 输出源地址 + if (node->data.get_ptr.src_addr) { + dump_child_node_ref(ctx, node->data.get_ptr.src_addr, false); + } + + // 输出索引 + if (node->data.get_ptr.index) { + dump_child_node_ref(ctx, node->data.get_ptr.index, true); + } +} + +// 转储分支节点 +static void dump_branch_node(scc_ir_dump_ctx_t *ctx, + const scc_ir_node_t *node) { + // PRINT_NODE(ctx->dump_ctx, "branch"); + + // 输出条件 + if (node->data.branch.cond) { + dump_child_node_ref(ctx, node->data.branch.cond, false); + } + + // 输出真分支块 + if (node->data.branch.true_bblock) { + scc_ir_bblock_t *true_bblock = + scc_ir_ctx_get_bblock(ctx->ir_ctx, node->data.branch.true_bblock); + if (true_bblock) { + scc_tree_print_indent(ctx->dump_ctx); + PRINT_NODE(ctx->dump_ctx, "TrueBlock: "); + PRINT_QUOTED_VALUE(ctx->dump_ctx, true_bblock->label + ? true_bblock->label + : ""); + } + } + + // 输出假分支块 + if (node->data.branch.false_bblock) { + scc_ir_bblock_t *false_bblock = + scc_ir_ctx_get_bblock(ctx->ir_ctx, node->data.branch.false_bblock); + if (false_bblock) { + scc_tree_print_indent(ctx->dump_ctx); + PRINT_NODE(ctx->dump_ctx, "FalseBlock: "); + PRINT_QUOTED_VALUE(ctx->dump_ctx, false_bblock->label + ? false_bblock->label + : ""); + } + } +} + +// 转储跳转节点 +static void dump_jump_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) { + // PRINT_NODE(ctx->dump_ctx, "jump"); + if (node->data.jump.target_bblock) { + 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 + : ""); + } else { + scc_printf("to invalid block"); + } + } else { + scc_printf("to NULL"); + } +} + +// 转储调用节点 +static void dump_call_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) { + // PRINT_NODE(ctx->dump_ctx, "call"); + if (node->data.call.callee) { + 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 : ""); + } else { + scc_printf("func="); + } + } else { + scc_printf("func=NULL"); + } + + if (scc_vec_size(node->data.call.args) > 0) { + scc_printf("\n"); + } + + // 输出参数 + for (usize i = 0; i < scc_vec_size(node->data.call.args); i++) { + cbool is_last = (i + 1 == scc_vec_size(node->data.call.args)); + scc_tree_dump_push_level(ctx->dump_ctx, is_last); + + scc_ir_node_ref_t arg_ref = scc_vec_at(node->data.call.args, i); + dump_child_node_ref(ctx, arg_ref, is_last); + + scc_tree_dump_pop_level(ctx->dump_ctx); + } +} + +// 转储返回节点 +static void dump_ret_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) { + // PRINT_NODE(ctx->dump_ctx, "ret"); + if (node->data.ret.ret_val) { + dump_child_node_ref(ctx, node->data.ret.ret_val, true); + } +} + +void scc_ir_dump_node(scc_ir_dump_ctx_t *ctx, scc_ir_node_ref_t node_ref) { + scc_ir_node_t *node = scc_ir_ctx_get_node(ctx->ir_ctx, node_ref); + if (!node) { + LOG_ERROR("Invalid node ref"); + return; + } + + scc_tree_print_indent(ctx->dump_ctx); + PRINT_NODE(ctx->dump_ctx, get_node_type_str(node->tag)); + + if (node->name && node->name[0]) { + PRINT_VALUE(ctx->dump_ctx, " [%s]", node->name); + } + + if (node->type) { + scc_printf(" : "); + 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"); + + // 根据节点类型输出特定信息 + switch (node->tag) { + case SCC_IR_NODE_NULL: + break; + case SCC_IR_NODE_CONST_INT: + dump_const_int_node(ctx, node); + break; + case SCC_IR_NODE_ALLOC: + break; + case SCC_IR_NODE_LOAD: + dump_load_node(ctx, node); + break; + case SCC_IR_NODE_STORE: + dump_store_node(ctx, node); + break; + case SCC_IR_NODE_GET_PTR: + dump_get_ptr_node(ctx, node); + break; + case SCC_IR_NODE_OP: + dump_op_node(ctx, node); + break; + case SCC_IR_NODE_BRANCH: + dump_branch_node(ctx, node); + break; + case SCC_IR_NODE_JUMP: + dump_jump_node(ctx, node); + break; + case SCC_IR_NODE_CALL: + dump_call_node(ctx, node); + break; + case SCC_IR_NODE_RET: + dump_ret_node(ctx, node); + break; + default: + PRINT_QUOTED_VALUE(ctx->dump_ctx, "unknown"); + scc_printf("tag(%d)", node->tag); + break; + } +} + +// 转储类型信息 +void scc_ir_dump_type(scc_ir_dump_ctx_t *ctx, scc_ir_type_ref_t type_ref) { + // impl fmt::Display for TypeKind { + // fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + // match self { + // TypeKind::Int32 => write!(f, "i32"), + // TypeKind::Unit => write!(f, "unit"), + // TypeKind::Array(t, len) => write!(f, "[{}, {}]", t, len), + // TypeKind::Pointer(t) => write!(f, "*{}", t), + // TypeKind::Function(params, ret) => { + // write!(f, "(")?; + // let mut first = true; + // for param in params { + // if !first { + // write!(f, ", ")?; + // } + // write!(f, "{}", param)?; + // first = false; + // } + // if !ret.is_unit() { + // write!(f, "): {}", ret) + // } else { + // write!(f, ")") + // } + // } + // } + // } + // } + if (!ctx || !type_ref) { + LOG_ERROR("invalid parameter"); + return; + } + + scc_ir_type_t *type = scc_ir_ctx_get_type(ctx->ir_ctx, type_ref); + if (!type) { + LOG_ERROR("invalid type ref"); + return; + } + 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"); + + // 递归转储子类型 + switch (type->tag) { + case SCC_IR_TYPE_PTR: + if (type->data.pointer.base) { + scc_tree_dump_push_level(ctx->dump_ctx, true); + scc_ir_dump_type(ctx, type->data.pointer.base); + scc_tree_dump_pop_level(ctx->dump_ctx); + } + break; + case SCC_IR_TYPE_ARRAY: + if (type->data.array.len > 0) { + scc_tree_print_indent(ctx->dump_ctx); + SCC_TREE_DUMP_PRINT_PURE(ctx->dump_ctx, ctx->dump_ctx->node_color, + "Array Length: %zu\n", + type->data.array.len); + } + if (type->data.array.base) { + scc_tree_dump_push_level(ctx->dump_ctx, true); + scc_ir_dump_type(ctx, type->data.array.base); + scc_tree_dump_pop_level(ctx->dump_ctx); + } + break; + case SCC_IR_TYPE_FUNC: + // 先输出参数类型 + for (usize i = 0; i < scc_vec_size(type->data.function.params); i++) { + cbool is_last = (i + 1 == scc_vec_size(type->data.function.params)); + scc_tree_dump_push_level(ctx->dump_ctx, is_last); + + scc_ir_type_ref_t param_type_ref = + scc_vec_at(type->data.function.params, i); + scc_ir_dump_type(ctx, param_type_ref); + + scc_tree_dump_pop_level(ctx->dump_ctx); + } + + // 再输出返回类型 + if (type->data.function.ret_type) { + scc_tree_dump_push_level(ctx->dump_ctx, true); + scc_ir_dump_type(ctx, type->data.function.ret_type); + scc_tree_dump_pop_level(ctx->dump_ctx); + } + break; + case SCC_IR_TYPE_STRUCT: + // 结构体处理 + break; + case SCC_IR_TYPE_VECTOR: + // 向量处理 + break; + default: + break; + } +} + +// 转储基本块 +void scc_ir_dump_bblock(scc_ir_dump_ctx_t *ctx, + scc_ir_bblock_ref_t bblock_ref) { + if (!ctx || !bblock_ref) { + LOG_ERROR("invalid parameter"); + return; + } + + scc_ir_bblock_t *bblock = scc_ir_ctx_get_bblock(ctx->ir_ctx, bblock_ref); + if (!bblock) { + LOG_ERROR("invalid bblock ref"); + return; + } + + scc_tree_print_indent(ctx->dump_ctx); + PRINT_NODE(ctx->dump_ctx, "BasicBlock: "); + PRINT_QUOTED_VALUE(ctx->dump_ctx, + bblock->label ? bblock->label : ""); + scc_printf("\n"); + + // 转储基本块中的指令 + for (usize i = 0; i < scc_vec_size(bblock->instrs); i++) { + cbool is_last = (i + 1 == scc_vec_size(bblock->instrs)); + scc_tree_dump_push_level(ctx->dump_ctx, is_last); + + scc_ir_node_ref_t instr_ref = scc_vec_at(bblock->instrs, i); + scc_ir_dump_node(ctx, instr_ref); + scc_tree_dump_pop_level(ctx->dump_ctx); + } +} + +// 转储函数 +void scc_ir_dump_func(scc_ir_dump_ctx_t *ctx, scc_ir_func_ref_t func_ref) { + scc_ir_func_t *func = scc_ir_ctx_get_func(ctx->ir_ctx, func_ref); + if (!ctx || !func) { + LOG_ERROR("invalid parameter"); + return; + } + + scc_tree_print_indent(ctx->dump_ctx); + PRINT_NODE(ctx->dump_ctx, "Function: "); + PRINT_QUOTED_VALUE(ctx->dump_ctx, func->name ? func->name : ""); + scc_printf("\n"); + + // 输出函数类型 + if (func->type) { + scc_tree_dump_push_level(ctx->dump_ctx, false); + scc_ir_dump_type(ctx, func->type); + scc_tree_dump_pop_level(ctx->dump_ctx); + } + + // 输出函数参数 + for (usize i = 0; i < scc_vec_size(func->params); i++) { + cbool is_last = (i + 1 == scc_vec_size(func->params)); + scc_tree_dump_push_level(ctx->dump_ctx, is_last); + + scc_ir_node_ref_t param_ref = scc_vec_at(func->params, i); + scc_ir_dump_node(ctx, param_ref); + scc_tree_dump_pop_level(ctx->dump_ctx); + } + + // 输出基本块 + for (usize i = 0; i < scc_vec_size(func->bblocks); i++) { + cbool is_last = (i + 1 == scc_vec_size(func->bblocks)); + scc_tree_dump_push_level(ctx->dump_ctx, is_last); + + scc_ir_bblock_ref_t bblock_ref = scc_vec_at(func->bblocks, i); + scc_ir_dump_bblock(ctx, bblock_ref); + + scc_tree_dump_pop_level(ctx->dump_ctx); + } +} + +// 转储整个程序 +void scc_ir_dump_cprog(scc_ir_dump_ctx_t *ctx) { + // 输出全局值 + + // if (scc_vec_size(program->global_vals) > 0) { + // print_indented(ctx, "Global Values:", ""); + + // for (usize i = 0; i < scc_vec_size(program->global_vals); i++) { + // cbool is_last = (i + 1 == + // scc_vec_size(program->global_vals)); + // scc_tree_dump_push_level(ctx->dump_ctx, is_last); + + // scc_ir_node_ref_t global_ref = + // scc_vec_at(program->global_vals, i); scc_ir_node_t + // *global_node = + // scc_ir_ctx_get_node(ir_ctx, global_ref); + // if (global_node) { + // scc_ir_dump_node(ctx, ir_ctx, global_node); + // } + + // scc_tree_dump_pop_level(ctx->dump_ctx); + // } + // } + + // // 输出函数定义 + // if (scc_vec_size(ir_ctx->funcs) > 0) { + // print_indented(ctx, "Functions:", ""); + + // for (usize i = 0; i < scc_vec_size(ir_ctx->funcs); i++) { + // cbool is_last = (i + 1 == scc_vec_size(ir_ctx->funcs)); + // scc_tree_dump_push_level(ctx->dump_ctx, is_last); + + // scc_ir_func_t func_ref = scc_vec_at(ir_ctx->funcs, i); + // if (func_ref.type != 0) { + // // TODO hack it + // dump_func_ref(ir_ctx, ctx, i + 1); + // } + + // scc_tree_dump_pop_level(ctx->dump_ctx); + // } + // } + PRINT_NODE(ctx->dump_ctx, "Func defs:\n"); + scc_vec_foreach(ctx->cprog->func_defs, i) { + cbool is_last = (i + 1 == scc_vec_size(ctx->cprog->func_defs)); + scc_tree_dump_push_level(ctx->dump_ctx, is_last); + scc_ir_dump_func(ctx, scc_vec_at(ctx->cprog->func_defs, i)); + scc_tree_dump_pop_level(ctx->dump_ctx); + } } -void scc_ir_node_dump(scc_ir_dump_ctx_t *ctx, scc_ir_node_t *in) { return; } -void scc_ir_cprog_dump(scc_ir_dump_ctx_t *ctx, ir_cprog_t *in) { return; } \ No newline at end of file diff --git a/libs/tree_dump/include/tree_dump.h b/libs/tree_dump/include/tree_dump.h index d6392be..ca9c0dd 100644 --- a/libs/tree_dump/include/tree_dump.h +++ b/libs/tree_dump/include/tree_dump.h @@ -15,6 +15,19 @@ // #define ANSI_FMT +#define SCC_TREE_DUMP_PRINT_COLORED(ctx, color, before_str, fmt, after_str, \ + ...) \ + scc_printf(before_str "%s" fmt "%s" after_str, \ + ctx->use_color ? color : "", ##__VA_ARGS__, \ + ctx->use_color ? ANSI_NONE : ""); + +#define SCC_TREE_DUMP_PRINT_AROUND(ctx, color, around_str, fmt, ...) \ + SCC_TREE_DUMP_PRINT_COLORED(ctx, color, around_str, fmt, around_str, \ + ##__VA_ARGS__) + +#define SCC_TREE_DUMP_PRINT_PURE(ctx, color, fmt, ...) \ + SCC_TREE_DUMP_PRINT_COLORED(ctx, color, "", fmt, "", ##__VA_ARGS__) + typedef SCC_VEC(cbool) scc_ast_dump_stack_t; typedef struct {