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:
zzy
2026-04-15 14:52:11 +08:00
parent 8054f20375
commit 5a9f816ccf
38 changed files with 260 additions and 1497 deletions

View File

@@ -12,6 +12,7 @@ typedef struct {
scc_hashtable_t symtab; ///< symbol to ir_ref
// scc_strpool_t strpool; ///< string pool
const scc_abi_type_calc_t *abi;
cbool hint_using_value; // 转换时尽可能使用value而不是alloc
} scc_ast2ir_ctx_t;
void scc_ast2ir_ctx_init(scc_ast2ir_ctx_t *ctx, const scc_abi_type_calc_t *abi,

View File

@@ -10,6 +10,8 @@ static scc_ir_type_ref_t parse_base_type(scc_ast2ir_ctx_t *ctx,
// 映射内置类型
ctx->abi->compute_type_layout(ctx->abi, ast_type, &layout);
switch (layout.size) {
case 0:
return scc_ir_builder_type_void(&ctx->builder);
case 1:
return scc_ir_builder_type_i8(&ctx->builder);
case 2:
@@ -229,9 +231,9 @@ scc_ir_value_ref_t scc_ast2ir_logical_expr(scc_ast2ir_ctx_t *ctx,
// scc_ir_bblock_ref_t start_block =
// scc_ir_builder_current_bblock(&ctx->builder);
scc_ir_bblock_ref_t right_block =
scc_ir_value_ref_t right_block =
scc_ir_builder_bblock(&ctx->builder, "logic_right");
scc_ir_bblock_ref_t end_block =
scc_ir_value_ref_t end_block =
scc_ir_builder_bblock(&ctx->builder, "logic_end");
// 为结果创建临时存储空间
@@ -248,7 +250,7 @@ scc_ir_value_ref_t scc_ast2ir_logical_expr(scc_ast2ir_ctx_t *ctx,
if (expr->binary.op == SCC_AST_OP_LOGICAL_AND) {
// a && b
scc_ir_bblock_ref_t false_block =
scc_ir_value_ref_t false_block =
scc_ir_builder_bblock(&ctx->builder, "and_false");
// 如果左操作数为0结果为0短路
@@ -276,7 +278,7 @@ scc_ir_value_ref_t scc_ast2ir_logical_expr(scc_ast2ir_ctx_t *ctx,
} else { // SCC_AST_OP_LOGICAL_OR
// a || b
scc_ir_bblock_ref_t true_block =
scc_ir_value_ref_t true_block =
scc_ir_builder_bblock(&ctx->builder, "or_true");
// 如果左操作数非0结果为1短路
@@ -708,13 +710,13 @@ void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, const scc_ast_stmt_t *stmt) {
\ /
merge_block
*/
scc_ir_bblock_ref_t true_block =
scc_ir_value_ref_t true_block =
scc_ir_builder_bblock(&ctx->builder, "if_true");
scc_ir_bblock_ref_t false_block =
scc_ir_value_ref_t false_block =
scc_ir_builder_bblock(&ctx->builder, "if_false");
scc_ir_bblock_ref_t merge_block =
scc_ir_value_ref_t merge_block =
scc_ir_builder_bblock(&ctx->builder, "if_merge");
scc_ir_value_ref_t cond_node =
@@ -738,11 +740,11 @@ void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, const scc_ast_stmt_t *stmt) {
break;
}
case SCC_AST_STMT_WHILE: {
scc_ir_bblock_ref_t cond_block =
scc_ir_value_ref_t cond_block =
scc_ir_builder_bblock(&ctx->builder, "while_cond");
scc_ir_bblock_ref_t body_block =
scc_ir_value_ref_t body_block =
scc_ir_builder_bblock(&ctx->builder, "while_body");
scc_ir_bblock_ref_t exit_block =
scc_ir_value_ref_t exit_block =
scc_ir_builder_bblock(&ctx->builder, "while_exit");
scc_ir_builder_jump(&ctx->builder, cond_block);
@@ -760,11 +762,11 @@ void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, const scc_ast_stmt_t *stmt) {
break;
}
case SCC_AST_STMT_DO_WHILE: {
scc_ir_bblock_ref_t cond_block =
scc_ir_value_ref_t cond_block =
scc_ir_builder_bblock(&ctx->builder, "do_while_cond");
scc_ir_bblock_ref_t body_block =
scc_ir_value_ref_t body_block =
scc_ir_builder_bblock(&ctx->builder, "do_while_body");
scc_ir_bblock_ref_t exit_block =
scc_ir_value_ref_t exit_block =
scc_ir_builder_bblock(&ctx->builder, "do_while_exit");
scc_ir_builder_jump(&ctx->builder, body_block);
@@ -782,11 +784,11 @@ void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, const scc_ast_stmt_t *stmt) {
break;
}
case SCC_AST_STMT_FOR: {
scc_ir_bblock_ref_t cond_block =
scc_ir_value_ref_t cond_block =
scc_ir_builder_bblock(&ctx->builder, "for_while_cond");
scc_ir_bblock_ref_t body_block =
scc_ir_value_ref_t body_block =
scc_ir_builder_bblock(&ctx->builder, "for_while_body");
scc_ir_bblock_ref_t exit_block =
scc_ir_value_ref_t exit_block =
scc_ir_builder_bblock(&ctx->builder, "for_while_exit");
if (stmt->for_stmt.init) {
@@ -928,7 +930,7 @@ void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, const scc_ast_decl_t *decl,
scc_ir_type_ref_t func_type_ref = scc_ast2ir_type(ctx, decl->func.type);
scc_ir_func_ref_t func_ref =
(usize)scc_hashtable_get(&ctx->symtab, decl->name);
if (func_ref == 0) {
if (func_ref == SCC_IR_REF_nullptr) {
func_ref =
scc_ir_builder_func(&ctx->builder, func_type_ref, decl->name);
scc_hashtable_set(&ctx->symtab, decl->name,
@@ -941,22 +943,14 @@ void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, const scc_ast_decl_t *decl,
}
scc_ir_builder_begin_func(&ctx->builder, func_ref, nullptr);
scc_ir_func_t *func =
scc_ir_module_get_func(ctx->builder.ctx.module, func_ref);
Assert(func != nullptr);
scc_ir_builder_begin_bblock(&ctx->builder, "entry");
scc_vec_foreach(decl->func.type->function.params, i) {
scc_ast_decl_t *param =
scc_vec_at(decl->func.type->function.params, i);
scc_ir_value_ref_t param_node_ref = scc_vec_at(func->params, i);
scc_ir_value_t *param_node = scc_ir_module_get_value(
ctx->builder.ctx.module, param_node_ref);
Assert(param_node != nullptr);
param_node->name = param->name;
scc_hashtable_set(&ctx->ast2ir_cache, param,
(void *)(usize)param_node_ref);
scc_ast2ir_decl(ctx, param, false);
}
scc_ast2ir_stmt(ctx, decl->func.body);
// FIXME need ret for none return or other default ret
scc_ir_builder_ret_void(&ctx->builder);
@@ -973,6 +967,23 @@ void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, const scc_ast_decl_t *decl,
break;
}
case SCC_AST_DECL_PARAM: {
if (decl->param.type->base.type == SCC_AST_TYPE_BUILTIN &&
decl->param.type->builtin.type == SCC_AST_BUILTIN_TYPE_VOID) {
break;
}
scc_ir_type_ref_t parma_type_ref =
scc_ast2ir_type(ctx, decl->param.type);
scc_ir_value_ref_t param_ref = scc_ir_builder_func_arg_ref(
&ctx->builder, parma_type_ref, decl->name, decl->param.param_idx);
if (!ctx->hint_using_value) {
scc_ir_value_ref_t val_ref = scc_ir_builder_alloca(
&ctx->builder, parma_type_ref, decl->name);
scc_ir_builder_store(&ctx->builder, val_ref, param_ref);
scc_hashtable_set(&ctx->ast2ir_cache, decl, (void *)(usize)val_ref);
} else {
Panic("using value is not supported");
}
break;
}
case SCC_AST_DECL_STRUCT:
@@ -1022,8 +1033,10 @@ void scc_ast2ir_translation_unit(scc_ast2ir_ctx_t *ctx,
}
}
static u32 scc_hash_node(const void *key) { return (u32)(usize)key; }
static int scc_cmp_node(const void *key1, const void *key2) {
static u32 scc_hash_node(const void *key, void *userdata) {
return (u32)(usize)key;
}
static int scc_cmp_node(const void *key1, const void *key2, void *userdata) {
return (u32)(usize)key1 - (u32)(usize)key2;
}
@@ -1032,9 +1045,10 @@ void scc_ast2ir_ctx_init(scc_ast2ir_ctx_t *ctx, const scc_abi_type_calc_t *abi,
Assert(ctx != nullptr);
ctx->abi = abi;
scc_ir_builder_init(&ctx->builder, cprog);
scc_hashtable_init(&ctx->ast2ir_cache, scc_hash_node, scc_cmp_node);
scc_hashtable_init(&ctx->symtab, (scc_hashtable_hash_func_t)scc_strhash32,
(scc_hashtable_equal_func_t)scc_strcmp);
scc_hashtable_init(&ctx->ast2ir_cache, scc_hash_node, scc_cmp_node,
nullptr);
scc_hashtable_cstr_init(&ctx->symtab);
ctx->hint_using_value = false;
}
void scc_ast2ir_ctx_drop(scc_ast2ir_ctx_t *ctx) {