feat(ast): 重构AST转储功能并创建AST到IR转换器

- 重构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
This commit is contained in:
zzy
2026-01-30 23:01:55 +08:00
parent c8bf98525d
commit 2a90e165a5
17 changed files with 2341 additions and 172 deletions

View File

@@ -1,128 +0,0 @@
#include <scc_ir.h>
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.len = 0;
break;
case SCC_IR_TYPE_PTR:
in->data.pointer.base = null;
break;
case SCC_IR_TYPE_FUNC:
scc_vec_init(in->data.function.params);
in->data.function.ret_type = null;
break;
case SCC_IR_TYPE_VOID:
case SCC_IR_TYPE_I32:
break;
default:
UNREACHABLE();
break;
}
}
void scc_ir_bblock_init(scc_ir_bblock_t *in, const char *label) {
Assert(in != null);
Assert(label != null);
in->label = label;
scc_vec_init(in->instrs);
}
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;
scc_vec_init(in->bblocks);
scc_vec_init(in->params);
}
void scc_ir_node_init(scc_ir_node_t *in, const char *name,
scc_ir_node_tag_t tag) {
Assert(in != null);
in->name = name;
in->tag = tag;
scc_vec_init(in->used_by);
in->type = null;
switch (tag) {
case SCC_IR_NODE_NULL:
break;
case SCC_IR_NODE_CONST_INT:
// TODO
in->data.const_int.int32 = 0;
break;
case SCC_IR_NODE_ALLOC:
TODO();
break;
case SCC_IR_NODE_LOAD:
in->data.load.target = null;
break;
case SCC_IR_NODE_STORE:
in->data.store.target = null;
in->data.store.value = null;
break;
case SCC_IR_NODE_GET_PTR:
in->data.get_ptr.src_addr = null;
in->data.get_ptr.index = null;
break;
case SCC_IR_NODE_OP:
in->data.op.op = IR_OP_EMPTY;
in->data.op.lhs = null;
in->data.op.rhs = null;
break;
case SCC_IR_NODE_BRANCH:
in->data.branch.cond = null;
in->data.branch.true_bblock = null;
in->data.branch.false_bblock = null;
break;
case SCC_IR_NODE_JUMP:
in->data.jump.target_bblock = null;
break;
case SCC_IR_NODE_CALL:
scc_vec_init(in->data.call.args);
in->data.call.callee = null;
break;
case SCC_IR_NODE_RET:
in->data.ret.ret_val = null;
break;
default:
UNREACHABLE();
break;
}
}
void scc_ir_cprog_init(ir_cprog_t *in) {
Assert(in != null);
scc_vec_init(in->extern_funcs);
scc_vec_init(in->funcs);
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;
}