feat(ast): 添加汇编器模块并改进AST定义和IR转换

- 在README.md中添加asm汇编器模块说明
- 更新ast_def.h中的枚举注释,添加sema相关信息以明确语义分析作用域
- 重命名函数参数param_types为params,使命名更清晰
- 移除call表达式中的_target字段,简化结构
- 为member、identifier等字段添加///< fill by sema注释说明填充时机
- 为jump语句添加_target字段用于语义分析
- 更新所有AST初始化函数,接受位置信息参数以改进错误定位
- 修复alignof表达式的类型应为ALIGN_OF而非SIZE_OF的问题
- 重构ast2ir.h,引入scc_ast2ir_ctx_t上下文结构体统一管理转换状态
- 添加符号表、节点到IR映射等必要的转换上下文信息
This commit is contained in:
zzy
2026-03-17 20:29:40 +08:00
parent cabd1710ed
commit 2e5e98868d
29 changed files with 1289 additions and 1000 deletions

View File

@@ -1,3 +1,4 @@
#include <scc_pos_log.h>
#include <scc_sema.h>
#include <sema_symtab.h>
@@ -10,12 +11,55 @@ static void type_callback(void *context, scc_ast_node_type_t node_type,
return;
}
static void decl_callback(void *context, scc_ast_node_type_t node_type,
static void expr_callback(void *context, scc_ast_node_type_t node_type,
void *node) {
scc_sema_symtab_t *sema_symtab = context;
if (node_type == SCC_AST_UNKNOWN || node == null) {
return;
}
scc_ast_expr_t *decl = SCC_AST_CAST_TO(scc_ast_expr_t, node);
if (node_type == SCC_AST_EXPR_IDENTIFIER) {
scc_ast_node_t *node =
scc_sema_symtab_lookup_symbol(sema_symtab, decl->identifier.name);
if (node == null) {
SCC_ERROR(decl->base.loc, "Identifier '%s' not found",
decl->identifier.name);
}
}
return;
}
static void stmt_callback(void *context, scc_ast_node_type_t node_type,
void *node) {
scc_sema_symtab_t *sema_symtab = context;
if (node_type == scc_ast_stmt_t_BEGIN) {
scc_sema_symtab_enter_scope(sema_symtab);
return;
} else if (node_type == scc_ast_stmt_t_END) {
scc_sema_symtab_leave_scope(sema_symtab);
return;
}
if (node_type == SCC_AST_UNKNOWN || node == null) {
return;
}
return;
}
static void decl_callback(void *context, scc_ast_node_type_t node_type,
void *node) {
scc_sema_symtab_t *sema_symtab = context;
if (node_type == scc_ast_decl_t_BEGIN) {
scc_sema_symtab_enter_scope(sema_symtab);
return;
} else if (node_type == scc_ast_decl_t_END) {
scc_sema_symtab_leave_scope(sema_symtab);
return;
}
if (node_type == SCC_AST_UNKNOWN || node == null) {
return;
}
scc_ast_decl_t *decl = SCC_AST_CAST_TO(scc_ast_decl_t, node);
scc_ast_type_t *type = scc_malloc(sizeof(scc_ast_type_t));
@@ -24,25 +68,25 @@ static void decl_callback(void *context, scc_ast_node_type_t node_type,
return;
}
if (decl->base.type == SCC_AST_DECL_STRUCT) {
scc_ast_type_struct_init(type, decl->name, decl);
scc_ast_type_struct_init(type, decl->name, decl, decl->base.loc);
scc_cstring_t name = scc_cstring_from_cstr("$S_");
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 (decl->base.type == SCC_AST_DECL_UNION) {
scc_ast_type_union_init(type, decl->name, decl);
scc_ast_type_union_init(type, decl->name, decl, decl->base.loc);
scc_cstring_t name = scc_cstring_from_cstr("$U_");
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 (decl->base.type == SCC_AST_DECL_ENUM) {
scc_ast_type_enum_init(type, decl->name, decl);
scc_ast_type_enum_init(type, decl->name, decl, decl->base.loc);
scc_cstring_t name = scc_cstring_from_cstr("$E_");
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 (decl->base.type == SCC_AST_DECL_TYPEDEF) {
scc_ast_type_typedef_init(type, decl->name, decl);
scc_ast_type_typedef_init(type, decl->name, decl, decl->base.loc);
scc_sema_symtab_add_symbol(sema_symtab, decl->name, &type->base);
}
return;
@@ -67,8 +111,8 @@ void scc_sema_init(scc_sema_callbacks_t *callbacks) {
}
callbacks->context = sema_symtab;
callbacks->on_decl = decl_callback;
callbacks->on_expr = null;
callbacks->on_stmt = null;
callbacks->on_expr = expr_callback;
callbacks->on_stmt = stmt_callback;
callbacks->on_type = type_callback;
callbacks->got_type = got_type_callback;