refactor(ir): 将IR节点重构为值引用并添加字符串字面量支持
- 将scc_ir_node_ref_t重命名为scc_ir_value_ref_t,并更新所有相关API - 修改IR定义中的节点标签枚举为值标签枚举(scc_ir_node_tag_t -> scc_ir_value_tag_t) - 更新AST到IR转换器中所有节点引用的类型声明 - 添加对内置类型(VOID, CHAR, INT等)的完整映射实现 - 实现字符串字面量的常量数组创建功能 - 更新项目名称从"Simple Modual C Compiler"为"Simple C Compiler" - 在Doxyfile中排除external目录的文档生成 - 移除未使用的strpool字段并重命名decl到IR的映射表 BREAKING CHANGE: IR节点引用类型已更改,所有使用scc_ir_node_ref_t的地方需 替换为scc_ir_value_ref_t。
This commit is contained in:
1 parent
4ddad7b456
commit
78e7c800ba
22 files changed
+992
-727
No files matched your search
@@ -13,139 +13,3 @@ void scc_ir_cprog_drop(scc_ir_cprog_t *in) {
|
||||
scc_vec_free(in->global_vals);
|
||||
scc_ir_module_drop(&in->module);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void scc_ir_module_init(scc_ir_module_t *ctx) {
|
||||
scc_vec_init(ctx->nodes);
|
||||
scc_vec_init(ctx->types);
|
||||
scc_vec_init(ctx->bblocks);
|
||||
scc_vec_init(ctx->funcs);
|
||||
scc_hashtable_init(&ctx->uid2nodes, hash_key, cmp_key);
|
||||
scc_hashtable_init(&ctx->uid2types, hash_key, cmp_key);
|
||||
scc_hashtable_init(&ctx->uid2bblocks, hash_key, cmp_key);
|
||||
scc_hashtable_init(&ctx->uid2funcs, hash_key, cmp_key);
|
||||
// 预留UID 0 作为无效引用
|
||||
ctx->node_uid = 1;
|
||||
ctx->type_uid = 1;
|
||||
ctx->bblock_uid = 1;
|
||||
ctx->func_uid = 1;
|
||||
}
|
||||
|
||||
void scc_ir_module_drop(scc_ir_module_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);
|
||||
}
|
||||
|
||||
// 辅助宏:创建实体并添加到哈希表
|
||||
#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_module_add_type(scc_ir_module_t *ctx,
|
||||
const scc_ir_type_t *type) {
|
||||
CREATE_ENTITY(ctx, ctx->types, type_uid, type, uid2types);
|
||||
}
|
||||
|
||||
scc_ir_node_ref_t scc_ir_module_add_node(scc_ir_module_t *ctx,
|
||||
const scc_ir_node_t *node) {
|
||||
CREATE_ENTITY(ctx, ctx->nodes, node_uid, node, uid2nodes);
|
||||
}
|
||||
|
||||
scc_ir_bblock_ref_t scc_ir_module_add_bblock(scc_ir_module_t *ctx,
|
||||
const scc_ir_bblock_t *bblock) {
|
||||
CREATE_ENTITY(ctx, ctx->bblocks, bblock_uid, bblock, uid2bblocks);
|
||||
}
|
||||
|
||||
scc_ir_func_ref_t scc_ir_module_add_func(scc_ir_module_t *ctx,
|
||||
const scc_ir_func_t *func) {
|
||||
CREATE_ENTITY(ctx, ctx->funcs, func_uid, func, uid2funcs);
|
||||
}
|
||||
|
||||
// 辅助宏:从哈希表获取索引
|
||||
#define GET_ENTITY_INDEX(ctx, ref, hashtable) \
|
||||
((usize)scc_hashtable_get(&(ctx)->hashtable, (void *)(usize)ref))
|
||||
|
||||
scc_ir_type_t *scc_ir_module_get_type(scc_ir_module_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_module_get_node(scc_ir_module_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_module_get_bblock(scc_ir_module_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_module_get_func(scc_ir_module_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];
|
||||
}
|
||||
Reference in new issue
Block a user