refactor(ast): 统一记录类型结构并移除成员访问操作符

- 移除了枚举类型的独立结构定义,统一使用record结构
- 移除了成员访问操作符SCC_AST_OP_MEMBER_ACCESS和SCC_AST_OP_PTR_MEMBER_ACCESS
- 更新了for循环语句中init字段的类型从scc_ast_type_t*到scc_ast_node_t*
- 修改了声明初始化函数以支持统一的记录类型处理

fix(ast2ir): 完善二元表达式处理和for循环代码生成

- 重构了赋值操作符的处理逻辑,通过临时表达式实现复合赋值
- 添加了对for循环语句的完整IR代码生成功能
- 修复了if-else语句中错误的基本块跳转问题
- 改进了标识符未找到时的错误提示信息

chore: 清理代码和修复潜在问题

- 移除了未使用的自动标签生成功能
- 统一了IR基本块标签格式化输出
- 修复了机器码生成中的寄存器存储控制流问题
- 改进了词法分析器中十六进制数字的处理逻辑
This commit is contained in:
zzy
2026-03-21 10:33:17 +08:00
parent a64e1f8330
commit 35a704a1cb
25 changed files with 1335 additions and 2095 deletions

View File

@@ -23,10 +23,11 @@ static void expr_callback(void *context, scc_ast_node_type_t node_type,
scc_ast_node_t *node =
scc_sema_symtab_lookup_symbol(sema_symtab, expr->identifier.name);
if (node == null) {
SCC_ERROR(expr->base.loc, "Identifier '%s' not found",
SCC_ERROR(expr->base.loc, "sema error: Identifier '%s' not found",
expr->identifier.name);
} else if (!SCC_AST_IS_A(scc_ast_decl_t, node)) {
SCC_ERROR(expr->base.loc, "Identifier '%s' is not a variable",
SCC_ERROR(expr->base.loc,
"sema error: Identifier '%s' is not a variable",
expr->identifier.name);
} else {
expr->identifier._target = SCC_AST_CAST_TO(scc_ast_decl_t, node);
@@ -72,34 +73,71 @@ static void decl_callback(void *context, scc_ast_node_type_t node_type,
scc_ast_type_t *type = scc_malloc(sizeof(scc_ast_type_t));
Assert(type != null);
if (decl->name == null) {
return;
}
if (node_type == SCC_AST_DECL_STRUCT) {
scc_ast_type_struct_init(type, decl->name, decl, decl->base.loc);
// FIXME memory leak
scc_cstring_t name = scc_cstring_from_cstr("$S_");
if (decl->name == null) {
decl->name = "<anonymous struct>";
}
scc_cstring_append_cstr(&name, decl->name, scc_strlen(decl->name));
scc_sema_symtab_add_symbol(sema_symtab, scc_cstring_as_cstr(&name),
&type->base);
} else if (node_type == SCC_AST_DECL_UNION) {
scc_ast_type_union_init(type, decl->name, decl, decl->base.loc);
scc_cstring_t name = scc_cstring_from_cstr("$U_");
if (decl->name == null) {
decl->name = "<anonymous union>";
}
scc_cstring_append_cstr(&name, decl->name, scc_strlen(decl->name));
scc_sema_symtab_add_symbol(sema_symtab, scc_cstring_as_cstr(&name),
&type->base);
} else if (node_type == SCC_AST_DECL_ENUM) {
scc_ast_type_enum_init(type, decl->name, decl, decl->base.loc);
scc_cstring_t name = scc_cstring_from_cstr("$E_");
if (decl->name == null) {
decl->name = "<anonymous enum>";
}
scc_cstring_append_cstr(&name, decl->name, scc_strlen(decl->name));
scc_sema_symtab_add_symbol(sema_symtab, scc_cstring_as_cstr(&name),
&type->base);
scc_vec_foreach(decl->record.fields, i) {
scc_ast_decl_t *enum_decl = scc_vec_at(decl->record.fields, i);
scc_sema_symtab_add_symbol(sema_symtab, enum_decl->name,
&enum_decl->base);
// LOG_INFO("enum added %s", enum_decl->name);
}
} else if (node_type == SCC_AST_DECL_TYPEDEF) {
if (decl->name == null) {
SCC_ERROR(decl->base.loc, "typedef without name");
return;
}
scc_ast_type_typedef_init(type, decl->name, decl, decl->base.loc);
scc_sema_symtab_add_symbol(sema_symtab, decl->name, &type->base);
} else if (node_type == SCC_AST_DECL_VAR) {
scc_sema_symtab_add_symbol(sema_symtab, decl->name, &decl->base);
} else if (node_type == SCC_AST_DECL_PARAM) {
if (decl->name == null) {
if (decl->param.type->base.type == SCC_AST_TYPE_BUILTIN &&
(decl->param.type->builtin.type ==
SCC_AST_BUILTIN_TYPE_VA_LIST ||
decl->param.type->builtin.type == SCC_AST_BUILTIN_TYPE_VOID)) {
return;
}
SCC_ERROR(decl->base.loc, "sema error: Parameter must have a name");
return;
}
scc_sema_symtab_add_symbol(sema_symtab, decl->name, &decl->base);
} else if (node_type == SCC_AST_DECL_FUNC) {
if (decl->name == null) {
SCC_ERROR(decl->base.loc, "sema error: Function must have a name");
} else {
// FIXME 重名函数...
scc_sema_symtab_add_symbol(sema_symtab, decl->name, &decl->base);
}
}
return;
}
@@ -136,6 +174,25 @@ void scc_sema_init(scc_sema_callbacks_t *callbacks) {
scc_pos_create());
scc_sema_symtab_add_symbol(sema_symtab, "__scc_builtin_va_list",
&type->base);
scc_ast_decl_t *decl = scc_malloc(sizeof(scc_ast_decl_t));
scc_ast_decl_val_init(decl, type, "__scc_builtin__", null,
scc_pos_create());
scc_sema_symtab_add_symbol(sema_symtab, "__func__", &decl->base);
scc_ast_type_t *built_func_type = scc_malloc(sizeof(scc_ast_type_t));
scc_ast_type_function_init(built_func_type, null, null, scc_pos_create());
scc_ast_decl_t *builin_func = scc_malloc(sizeof(scc_ast_decl_t));
scc_ast_decl_func_init(builin_func, built_func_type, "__scc_builtin_func",
null, scc_pos_create());
scc_sema_symtab_add_symbol(sema_symtab, "__scc_builtin_va_start",
&builin_func->base);
scc_sema_symtab_add_symbol(sema_symtab, "__scc_builtin_va_end",
&builin_func->base);
scc_sema_symtab_add_symbol(sema_symtab, "__scc_builtin_va_arg",
&builin_func->base);
scc_sema_symtab_add_symbol(sema_symtab, "__scc_builtin_va_copy",
&builin_func->base);
}
void scc_sema_drop(scc_sema_callbacks_t *callbacks) {}