feat(ir): 添加IR库的基础结构和定义
- 创建IR库的cbuild.toml配置文件,添加对scc_core的依赖 - 新增ir_def.h头文件,定义IR类型、节点、基本块和函数的数据结构 - 添加ir_builtin.h和ir_builtin.c,提供内置的i32类型和零值常量 - 实现ir_dump.h和ir_dump.c,提供IR转储功能的接口 - 创建scc_ir.h和scc_ir.c,实现IR对象的初始化和分配功能 - 添加测试文件test_ir.c用于验证IR库功能 - 定义了完整的IR节点类型枚举和操作类型枚举
This commit is contained in:
10
libs/ir/src/ir_builtin.c
Normal file
10
libs/ir/src/ir_builtin.c
Normal file
@@ -0,0 +1,10 @@
|
||||
#include <ir_builtin.h>
|
||||
|
||||
scc_ir_type_t scc_ir_builtin_i32 = {
|
||||
.tag = SCC_IR_TYPE_I32,
|
||||
};
|
||||
|
||||
scc_ir_node_t scc_ir_builtin_zero = {
|
||||
.tag = SCC_IR_NODE_CONST_INT,
|
||||
.data.const_int.int_any = {0},
|
||||
};
|
||||
13
libs/ir/src/ir_dump.c
Normal file
13
libs/ir/src/ir_dump.c
Normal file
@@ -0,0 +1,13 @@
|
||||
#include <ir_dump.h>
|
||||
|
||||
void scc_ir_type_dump(scc_ir_dump_ctx_t *ctx, const scc_ir_type_t *in) {
|
||||
return;
|
||||
}
|
||||
void scc_ir_bblock_dump(scc_ir_dump_ctx_t *ctx, const scc_ir_bblock_t *in) {
|
||||
return;
|
||||
}
|
||||
void scc_ir_func_dump(scc_ir_dump_ctx_t *ctx, const scc_ir_func_t *in) {
|
||||
return;
|
||||
}
|
||||
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; }
|
||||
128
libs/ir/src/scc_ir.c
Normal file
128
libs/ir/src/scc_ir.c
Normal file
@@ -0,0 +1,128 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user