fix(abi): 修复void类型的ABI计算缺少break语句
在scc_type_abi.c文件中,void类型的case分支缺少break语句, 导致执行流程错误地进入下一个case分支。 feat(ast): 为参数声明添加索引字段 在ast_def.h头文件中为参数声明结构体添加param_idx字段, 用于跟踪参数在函数参数列表中的位置索引。 feat(ast): 更新参数初始化函数以支持索引参数 修改scc_ast.h中的scc_ast_decl_param_init函数签名, 添加参数索引idx参数,并将该值存储到参数声明结构体中。 feat(ast2ir): 添加IR转换上下文的值使用提示选项 在ast2ir.h中为scc_ast2ir_ctx_t结构体添加hint_using_value字段, 控制参数转换时是使用值还是分配内存的方式。 fix(ast2ir): 正确处理void类型到IR的转换 当遇到大小为0的类型(如void)时,直接返回void类型, 而不是尝试匹配其他大小分支。 refactor(ast2ir): 统一基本块引用类型为value_ref 将逻辑表达式、条件语句、循环语句中的基本块引用类型 从bblock_ref_t改为value_ref_t,保持类型一致性。 fix(ast2ir): 修正函数引用空值检查 使用SCC_IR_REF_nullptr常量替代0进行函数引用的空值检查, 提高代码的可读性和正确性。 refactor(ast2ir): 简化参数处理逻辑 移除不必要的函数参数获取和命名设置逻辑, 通过递归调用scc_ast2ir_decl来处理参数声明。 feat(ast2ir): 实现参数声明到IR的转换 为参数声明添加完整的IR转换逻辑,包括类型转换、 参数引用创建和内存分配处理。 refactor(ast2ir): 更新哈希表初始化接口 适配新的哈希表初始化函数签名,添加userdata参数支持, 并初始化hint_using_value字段为false。 refactor(ir): 移除函数参数的预分配逻辑 删除IR构建器中函数参数的预分配和循环添加逻辑, 简化函数开始构建的处理流程。 refactor(ir): 更新类型哈希表键值处理 修改类型哈希表的哈希和比较函数以接受模块参数, 正确处理空引用情况并支持新的键值传递方式。 fix(ir): 修复IR转储中的字符串格式 移除IR函数转储时多余的换行符,确保输出格式正确。 refactor(ir): 更新模块哈希表初始化 适配哈希表初始化接口变更,添加userdata参数, 并为各种向量预留UID 0作为无效引用。 fix(ir): 修复模块清理中的循环起始索引 将模块清理循环的起始索引从0改为1,跳过预留的 无效引用项,避免访问空指针。 refactor(ir): 调整向量和哈希表操作顺序 调整模块中向量push和哈希表set的操作顺序, 确保数据一致性和正确的UID分配。 chore(build): 移除ir2mcode模块相关文件 移除ir2mcode相关的头文件和源文件,这些组件 将在后续重构中重新设计或替换。
This commit is contained in:
@@ -120,40 +120,9 @@ void scc_ir_builder_begin_func(scc_ir_builder_t *builder,
|
||||
return;
|
||||
}
|
||||
|
||||
scc_ir_type_ref_vec_t params = func_type->data.function.params;
|
||||
// 释放借用,因为下面要调用 add_value(可能 realloc)
|
||||
SCC_IR_BUILDER_END_BORROW(builder); // func_type
|
||||
SCC_IR_BUILDER_END_BORROW(builder); // func_ptr
|
||||
|
||||
// 预先分配所有参数值(临时数组,避免在循环中 push 到 func_ptr->params 时
|
||||
// func_ptr 失效)
|
||||
usize param_count = scc_vec_size(params);
|
||||
scc_ir_value_ref_t *param_refs =
|
||||
scc_malloc(sizeof(scc_ir_value_ref_t) * param_count);
|
||||
|
||||
for (usize i = 0; i < param_count; i++) {
|
||||
scc_ir_type_ref_t param_type = scc_vec_at(params, i);
|
||||
scc_ir_value_t param_node = {0};
|
||||
param_node.tag = SCC_IR_VALUE_TAG_FUNC_ARG_REF;
|
||||
param_node.type = scc_ir_module_add_type(
|
||||
GET_MODULE(builder),
|
||||
&(scc_ir_type_t){.tag = SCC_IR_TYPE_PTR,
|
||||
.data.pointer.base = param_type});
|
||||
param_node.name = param_names ? param_names[i] : nullptr;
|
||||
param_node.data.arg_ref.idx = i;
|
||||
scc_vec_init(param_node.used_by);
|
||||
param_refs[i] =
|
||||
scc_ir_module_add_value(GET_MODULE(builder), ¶m_node);
|
||||
}
|
||||
|
||||
// 重新借用 func_ptr 以添加参数到函数
|
||||
SCC_IR_BUILDER_BEGIN_BORROW(
|
||||
builder, func_ptr,
|
||||
scc_ir_module_get_func(GET_MODULE(builder), func_ref));
|
||||
for (usize i = 0; i < param_count; i++) {
|
||||
scc_vec_push(func_ptr->params, param_refs[i]);
|
||||
}
|
||||
SCC_IR_BUILDER_END_BORROW(builder); // func_ptr
|
||||
}
|
||||
|
||||
void scc_ir_builder_end_func(scc_ir_builder_t *builder) {
|
||||
|
||||
@@ -11,8 +11,12 @@ static inline u32 scc_hash_mix(u32 seed, u32 value) {
|
||||
return (seed ^ value) * 16777619u;
|
||||
}
|
||||
|
||||
static u32 hash_type(const void *_key) {
|
||||
const scc_ir_type_t *key = _key;
|
||||
static u32 hash_type(const void *_key, void *userdata) {
|
||||
scc_ir_module_t *module = userdata;
|
||||
const scc_ir_type_t *key =
|
||||
_key == SCC_IR_REF_nullptr
|
||||
? &scc_vec_at(module->types, 0)
|
||||
: scc_ir_module_get_type(module, (usize)_key);
|
||||
// 初始哈希:tag
|
||||
u32 hash = (u32)key->tag;
|
||||
|
||||
@@ -65,8 +69,16 @@ static u32 hash_type(const void *_key) {
|
||||
return hash;
|
||||
}
|
||||
|
||||
static int cmp_type(const void *_key1, const void *_key2) {
|
||||
const scc_ir_type_t *key1 = _key1, *key2 = _key2;
|
||||
static int cmp_type(const void *_key1, const void *_key2, void *userdata) {
|
||||
scc_ir_module_t *module = userdata;
|
||||
const scc_ir_type_t *key1 =
|
||||
_key1 == SCC_IR_REF_nullptr
|
||||
? &scc_vec_at(module->types, 0)
|
||||
: scc_ir_module_get_type(module, (usize)_key1),
|
||||
*key2 =
|
||||
_key2 == SCC_IR_REF_nullptr
|
||||
? &scc_vec_at(module->types, 0)
|
||||
: scc_ir_module_get_type(module, (usize)_key2);
|
||||
Assert(key1 != nullptr && key2 != nullptr);
|
||||
if (key1->tag == SCC_IR_TYPE_unknown || key2->tag == SCC_IR_TYPE_unknown) {
|
||||
return 1;
|
||||
@@ -123,12 +135,10 @@ static int cmp_type(const void *_key1, const void *_key2) {
|
||||
|
||||
void scc_ir_ctx_init(scc_ir_ctx_t *ctx, scc_ir_module_t *module) {
|
||||
ctx->module = module;
|
||||
scc_hashtable_init(&ctx->type_uniquing, hash_type, cmp_type);
|
||||
scc_hashtable_init(&ctx->type_uniquing, hash_type, cmp_type, module);
|
||||
// scc_hashtable_init(&ctx->const_pool, /* 常量哈希函数 */,
|
||||
// /* 常量比较函数 */);
|
||||
scc_hashtable_init(&ctx->func_decl_set,
|
||||
(scc_hashtable_hash_func_t)scc_strhash32,
|
||||
(scc_hashtable_equal_func_t)scc_strcmp);
|
||||
scc_hashtable_cstr_init(&ctx->func_decl_set);
|
||||
}
|
||||
|
||||
void scc_ir_ctx_drop(scc_ir_ctx_t *ctx) {
|
||||
@@ -140,15 +150,17 @@ void scc_ir_ctx_drop(scc_ir_ctx_t *ctx) {
|
||||
scc_ir_type_ref_t scc_ir_ctx_get_type(scc_ir_ctx_t *ctx,
|
||||
const scc_ir_type_t *type_desc) {
|
||||
Assert(type_desc->tag != SCC_IR_TYPE_unknown);
|
||||
|
||||
scc_vec_at(ctx->module->types, 0) = *type_desc;
|
||||
// 先查哈希表
|
||||
void *found = scc_hashtable_get(&ctx->type_uniquing, (void *)type_desc);
|
||||
void *found = scc_hashtable_get(&ctx->type_uniquing, (void *)(usize)0);
|
||||
if (found) {
|
||||
return (scc_ir_type_ref_t)(usize)found;
|
||||
}
|
||||
|
||||
// 不存在,添加新类型
|
||||
scc_ir_type_ref_t new_ref = scc_ir_module_add_type(ctx->module, type_desc);
|
||||
scc_hashtable_set(&ctx->type_uniquing, (void *)type_desc,
|
||||
scc_hashtable_set(&ctx->type_uniquing, (void *)(usize)new_ref,
|
||||
(void *)(usize)new_ref);
|
||||
return new_ref;
|
||||
}
|
||||
|
||||
@@ -658,7 +658,7 @@ void scc_ir_dump_func_linear(scc_ir_dump_ctx_t *ctx, scc_ir_func_ref_t func_ref,
|
||||
scc_ir_func_t *func = scc_ir_module_get_func(GET_MODULE(ctx), func_ref);
|
||||
scc_tree_dump_begin_line(ctx->dump_ctx);
|
||||
if (!func) {
|
||||
scc_tree_dump_append(ctx->dump_ctx, "<invalid function>\n");
|
||||
scc_tree_dump_append(ctx->dump_ctx, "<invalid function>");
|
||||
return;
|
||||
}
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, "func @%s",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include <ir_module.h>
|
||||
|
||||
static u32 hash_key(const void *key) { return (u32)(usize)key; }
|
||||
static int cmp_key(const void *key1, const void *key2) {
|
||||
static u32 hash_key(const void *key, void *userdata) { return (u32)(usize)key; }
|
||||
static int cmp_key(const void *key1, const void *key2, void *userdata) {
|
||||
return (u32)(usize)key1 != (u32)(usize)key2;
|
||||
}
|
||||
|
||||
@@ -10,11 +10,15 @@ void scc_ir_module_init(scc_ir_module_t *ctx) {
|
||||
scc_vec_init(ctx->types);
|
||||
scc_vec_init(ctx->bblocks);
|
||||
scc_vec_init(ctx->funcs);
|
||||
scc_hashtable_init(&ctx->uid2value, hash_key, cmp_key);
|
||||
scc_hashtable_init(&ctx->uid2type, hash_key, cmp_key);
|
||||
scc_hashtable_init(&ctx->uid2bblock, hash_key, cmp_key);
|
||||
scc_hashtable_init(&ctx->uid2func, hash_key, cmp_key);
|
||||
scc_hashtable_init(&ctx->uid2value, hash_key, cmp_key, nullptr);
|
||||
scc_hashtable_init(&ctx->uid2type, hash_key, cmp_key, nullptr);
|
||||
scc_hashtable_init(&ctx->uid2bblock, hash_key, cmp_key, nullptr);
|
||||
scc_hashtable_init(&ctx->uid2func, hash_key, cmp_key, nullptr);
|
||||
// 预留UID 0 作为无效引用
|
||||
scc_vec_push(ctx->values, (scc_ir_value_t){0});
|
||||
scc_vec_push(ctx->types, (scc_ir_type_t){0});
|
||||
scc_vec_push(ctx->bblocks, (scc_ir_bblock_t){0});
|
||||
scc_vec_push(ctx->funcs, (scc_ir_func_t){0});
|
||||
ctx->value_uid = 1;
|
||||
ctx->type_uid = 1;
|
||||
ctx->bblock_uid = 1;
|
||||
@@ -23,7 +27,7 @@ void scc_ir_module_init(scc_ir_module_t *ctx) {
|
||||
|
||||
void scc_ir_module_drop(scc_ir_module_t *ctx) {
|
||||
// 释放所有实体的内部内存
|
||||
for (usize i = 0; i < ctx->values.size; i++) {
|
||||
for (usize i = 1; i < ctx->values.size; i++) {
|
||||
scc_ir_value_t *node = &ctx->values.data[i];
|
||||
scc_vec_free(node->used_by);
|
||||
if (node->tag == SCC_IR_VALUE_TAG_CALL) {
|
||||
@@ -31,19 +35,19 @@ void scc_ir_module_drop(scc_ir_module_t *ctx) {
|
||||
}
|
||||
}
|
||||
|
||||
for (usize i = 0; i < ctx->types.size; i++) {
|
||||
for (usize i = 1; 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++) {
|
||||
for (usize i = 1; 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++) {
|
||||
for (usize i = 1; i < ctx->funcs.size; i++) {
|
||||
scc_ir_func_t *func = &ctx->funcs.data[i];
|
||||
scc_vec_free(func->params);
|
||||
scc_vec_free(func->bblocks);
|
||||
@@ -64,11 +68,11 @@ void scc_ir_module_drop(scc_ir_module_t *ctx) {
|
||||
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)); \
|
||||
(void *)(usize)(scc_vec_size(vec))); \
|
||||
/* 添加到向量 */ \
|
||||
scc_vec_push((vec), *(data)); \
|
||||
return new_uid; \
|
||||
} while (0)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user