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

@@ -288,8 +288,15 @@ CONTINUE:
scc_ast_expr_lvalue_init(lvalue, decl->var.type, decl->base.loc);
decl->var.init = scc_parse_initializer(parser, lvalue);
} else if (tok_ptr->type == SCC_TOK_L_BRACE) {
scc_parse_decl_sema(parser, decl);
parser->sema_callbacks.on_decl(parser->sema_callbacks.context,
scc_ast_decl_t_BEGIN, null);
scc_vec_foreach(decl->func.type->function.params, i) {
scc_ast_decl_t *param =
scc_vec_at(decl->func.type->function.params, i);
// Add params to decl
scc_parse_decl_sema(parser, param);
}
scc_ast_stmt_t *body = scc_parse_statement(parser);
parser->sema_callbacks.on_decl(parser->sema_callbacks.context,
scc_ast_decl_t_END, null);

View File

@@ -269,15 +269,15 @@ static scc_ast_stmt_t *parse_for_statement(scc_parser_t *parser,
"Expected '(' before like `( expression )` .");
}
scc_ast_type_t *init = null;
scc_ast_node_t *init = null;
scc_ast_expr_t *cond = null;
scc_ast_expr_t *incr = null;
scc_ast_stmt_t *body = null;
// TODO use decl or expr
init = (scc_ast_type_t *)scc_parse_declaration(parser);
init = (scc_ast_node_t *)scc_parse_declaration(parser);
if (init == null) {
init = (scc_ast_type_t *)scc_parse_expression(parser);
init = (scc_ast_node_t *)scc_parse_expression(parser);
if (!scc_parser_consume_if(parser, SCC_TOK_SEMICOLON)) {
SCC_ERROR(scc_parser_got_current_pos(parser),
"Expected semicolon in for statement.");

View File

@@ -508,22 +508,25 @@ static scc_ast_type_t *build_type_from_info(type_spec_info_t *info,
if (info->is_unsigned)
builtin = SCC_AST_BUILTIN_TYPE_UNSIGNED_CHAR;
else if (!info->is_signed)
builtin = SCC_AST_BUILTIN_TYPE_CHAR; // plain char (可能区别于
// signed char)
builtin = SCC_AST_BUILTIN_TYPE_CHAR;
else
builtin = SCC_AST_BUILTIN_TYPE_SIGNED_CHAR;
} else if (info->is_short) {
builtin = info->is_unsigned ? SCC_AST_BUILTIN_TYPE_UNSIGNED_SHORT
: info->is_signed ? SCC_AST_BUILTIN_TYPE_SIGNED_SHORT
: SCC_AST_BUILTIN_TYPE_SHORT;
} else if (info->is_long_long) {
builtin = info->is_unsigned
? SCC_AST_BUILTIN_TYPE_UNSIGNED_LONG_LONG
: SCC_AST_BUILTIN_TYPE_LONG_LONG;
: info->is_signed ? SCC_AST_BUILTIN_TYPE_SIGNED_LONG_LONG
: SCC_AST_BUILTIN_TYPE_LONG_LONG;
} else if (info->is_long) {
builtin = info->is_unsigned ? SCC_AST_BUILTIN_TYPE_UNSIGNED_LONG
: info->is_signed ? SCC_AST_BUILTIN_TYPE_SIGNED_LONG
: SCC_AST_BUILTIN_TYPE_LONG;
} else { // int
} else {
builtin = info->is_unsigned ? SCC_AST_BUILTIN_TYPE_UNSIGNED_INT
: info->is_signed ? SCC_AST_BUILTIN_TYPE_SIGNED_INT
: SCC_AST_BUILTIN_TYPE_INT;
}
}
@@ -571,6 +574,10 @@ static scc_ast_type_t *parse_record_type(scc_parser_t *parser,
scc_lexer_tok_drop(&tok);
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
if (tok_ptr == null) {
SCC_ERROR(scc_parser_got_current_pos(parser), "Unexpected EOF");
return null;
}
const char *name = null;
scc_ast_decl_t *decl = null;
scc_ast_decl_vec_t member;
@@ -609,7 +616,7 @@ static scc_ast_type_t *parse_record_type(scc_parser_t *parser,
break;
} else {
SCC_ERROR(scc_parser_got_current_pos(parser),
"Unexpected token in enum specifier");
"Unexpected token in struct/union specifier");
scc_parser_next_consume(parser, null);
break;
}
@@ -663,9 +670,14 @@ static scc_ast_type_t *parse_enum_type(scc_parser_t *parser) {
scc_lexer_tok_drop(&tok);
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
if (tok_ptr == null) {
SCC_ERROR(scc_parser_got_current_pos(parser), "Unexpected EOF");
return null;
}
scc_ast_type_t *type = ast_type_alloc();
const char *name = null;
scc_ast_decl_t *decl = null;
scc_ast_expr_vec_t member;
scc_ast_decl_vec_t member;
scc_vec_init(member);
if (tok_ptr->type == SCC_TOK_IDENT) {
@@ -684,20 +696,42 @@ static scc_ast_type_t *parse_enum_type(scc_parser_t *parser) {
break;
}
scc_ast_expr_t *expr = scc_parse_assignment_expression(parser);
if (expr != null) {
scc_vec_push(member, expr);
continue;
scc_parser_next_consume(parser, &tok);
if (tok.type != SCC_TOK_IDENT) {
SCC_ERROR(scc_parser_got_current_pos(parser),
"Expected identifier in enum specifier");
break;
}
if (tok_ptr->type == SCC_TOK_COMMA) {
// FIXME check comma
scc_ast_expr_t *enum_item_init = null;
if (scc_parser_consume_if(parser, SCC_TOK_ASSIGN)) {
enum_item_init = scc_parser_constant_expression(parser);
}
scc_ast_decl_t *enum_item_decl = scc_malloc(sizeof(scc_ast_decl_t));
Assert(enum_item_decl != null);
scc_ast_decl_val_init(enum_item_decl, type,
scc_cstring_as_cstr(&tok.lexeme),
enum_item_init, tok.loc);
scc_vec_push(member, enum_item_decl);
tok_ptr = scc_parser_peek(parser);
cbool got_comma = false;
if (tok_ptr && tok_ptr->type == SCC_TOK_COMMA) {
scc_parser_next_consume(parser, null);
} else if (tok_ptr->type == SCC_TOK_R_BRACE) {
tok_ptr = scc_parser_peek(parser);
got_comma = true;
}
if (tok_ptr && tok_ptr->type == SCC_TOK_R_BRACE) {
scc_parser_next_consume(parser, null);
break;
} else {
SCC_ERROR(scc_parser_got_current_pos(parser),
if (got_comma) {
continue;
}
SCC_ERROR(tok_ptr != null ? tok_ptr->loc
: scc_parser_got_current_pos(parser),
"Unexpected token in enum specifier");
scc_parser_next_consume(parser, null);
break;
@@ -716,7 +750,6 @@ static scc_ast_type_t *parse_enum_type(scc_parser_t *parser) {
}
}
scc_ast_type_t *type = ast_type_alloc();
scc_ast_type_enum_init(type, name, decl, pos);
return type;
}
@@ -1071,7 +1104,10 @@ parse_direct_declarator(scc_parser_t *parser, scc_ast_type_t *base,
tok_ident);
} else {
ret = parse_declarator(parser, null, &delay_pointee, tok_ident);
Assert(ret != null);
if (ret == null) {
SCC_ERROR(tok_ident->loc, "parse_declarator failed");
Panic("parse_declarator failed ret == null");
}
if (!scc_parser_consume_if(parser, SCC_TOK_R_PAREN)) {
SCC_ERROR(scc_parser_got_current_pos(parser), "expect ')'");
}
@@ -1199,31 +1235,25 @@ scc_ast_decl_t *scc_parse_declarator(scc_parser_t *parser,
if (decl_name_tok.type != SCC_TOK_IDENT) {
if (decl_type->base.type == SCC_AST_TYPE_STRUCT ||
decl_type->base.type == SCC_AST_TYPE_UNION) {
decl_type->base.type == SCC_AST_TYPE_UNION ||
decl_type->base.type == SCC_AST_TYPE_ENUM) {
if (decl_type->record.decl == null) {
decl = scc_malloc(sizeof(scc_ast_decl_t));
Assert(decl != null);
if (decl_type->base.type == SCC_AST_TYPE_STRUCT) {
scc_ast_decl_struct_init(decl, decl_type->record.name, null,
decl_type->base.loc);
} else {
} else if (decl_type->base.type == SCC_AST_TYPE_UNION) {
scc_ast_decl_union_init(decl, decl_type->record.name, null,
decl_type->base.loc);
} else {
scc_ast_decl_enum_init(decl, type->record.name, null,
decl_type->base.loc);
}
} else {
decl = decl_type->record.decl;
scc_free(decl_type); // FIXME
}
} else if (decl_type->base.type == SCC_AST_TYPE_ENUM) {
if (decl_type->enumeration.decl == null) {
decl = scc_malloc(sizeof(scc_ast_decl_t));
Assert(decl != null);
scc_ast_decl_enum_init(decl, type->enumeration.name, null,
decl_type->base.loc);
} else {
decl = type->enumeration.decl;
scc_free(decl_type); // FIXME
}
} else {
decl = scc_malloc(sizeof(scc_ast_decl_t));
scc_ast_decl_unsafe_val_init(decl, type, null, null,

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) {}