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

@@ -186,10 +186,6 @@ struct scc_ast_type {
const char *name;
scc_ast_decl_t *decl; // can be null
} record;
struct {
const char *name;
scc_ast_decl_t *decl; // can be null
} enumeration;
struct {
const char *name;
scc_ast_decl_t *decl;
@@ -255,9 +251,6 @@ typedef enum scc_ast_expr_op {
SCC_AST_OP_PREFIX_DECREMENT, // -- (前缀)
SCC_AST_OP_POSTFIX_INCREMENT, // ++ (后缀)
SCC_AST_OP_POSTFIX_DECREMENT, // -- (后缀)
/* 成员访问 */
SCC_AST_OP_MEMBER_ACCESS, // .
SCC_AST_OP_PTR_MEMBER_ACCESS, // ->
} scc_ast_expr_op_t;
typedef enum {
@@ -371,7 +364,7 @@ struct scc_ast_stmt {
} while_stmt;
// for 语句
struct {
scc_ast_type_t *init; // expr or decl or null
scc_ast_node_t *init; // expr or decl or null
scc_ast_expr_t *cond; // 可为 null
scc_ast_expr_t *incr; // 可为 null
scc_ast_stmt_t *body;
@@ -435,14 +428,10 @@ struct scc_ast_decl {
struct {
scc_ast_type_t *type;
} param;
// 结构体/联合声明
// 结构体/联合/枚举声明
struct {
scc_ast_decl_vec_t fields;
} record;
// 枚举声明
struct {
scc_ast_expr_vec_t enumerators;
} enumeration;
// typedef 声明
struct {
scc_ast_type_t *type;

View File

@@ -82,14 +82,14 @@ static inline void scc_ast_decl_param_init(scc_ast_decl_t *decl,
decl->param.type = type;
}
// name and fields can be null
static inline void scc_ast_decl_struct_init(scc_ast_decl_t *decl,
const char *name,
scc_ast_decl_vec_t *fields_move,
scc_pos_t loc) {
static inline void _scc_ast_decl_record_init(scc_ast_decl_t *decl,
scc_ast_node_type_t type,
const char *name,
scc_ast_decl_vec_t *fields_move,
scc_pos_t loc) {
Assert(decl != null);
decl->base.loc = loc;
decl->base.type = SCC_AST_DECL_STRUCT;
decl->base.type = type;
decl->name = name;
if (fields_move == null) {
scc_vec_init(decl->record.fields);
@@ -99,38 +99,29 @@ static inline void scc_ast_decl_struct_init(scc_ast_decl_t *decl,
}
}
// name and fields can be null
static inline void scc_ast_decl_struct_init(scc_ast_decl_t *decl,
const char *name,
scc_ast_decl_vec_t *fields_move,
scc_pos_t loc) {
_scc_ast_decl_record_init(decl, SCC_AST_DECL_STRUCT, name, fields_move,
loc);
}
// name and fields can be null
static inline void scc_ast_decl_union_init(scc_ast_decl_t *decl,
const char *name,
scc_ast_decl_vec_t *fields_move,
scc_pos_t loc) {
Assert(decl != null);
decl->base.loc = loc;
decl->base.type = SCC_AST_DECL_UNION;
decl->name = name;
if (fields_move == null) {
scc_vec_init(decl->record.fields);
} else {
decl->record.fields = *fields_move;
scc_vec_init(*fields_move);
}
_scc_ast_decl_record_init(decl, SCC_AST_DECL_UNION, name, fields_move, loc);
}
// name and fields can be null
static inline void scc_ast_decl_enum_init(scc_ast_decl_t *decl,
const char *name,
scc_ast_expr_vec_t *fields_move,
scc_ast_decl_vec_t *fields_move,
scc_pos_t loc) {
Assert(decl != null);
decl->base.loc = loc;
decl->base.type = SCC_AST_DECL_ENUM;
decl->name = name;
if (fields_move == null) {
scc_vec_init(decl->enumeration.enumerators);
} else {
decl->enumeration.enumerators = *fields_move;
scc_vec_init(*fields_move);
}
_scc_ast_decl_record_init(decl, SCC_AST_DECL_ENUM, name, fields_move, loc);
}
static inline void scc_ast_decl_typedef_init(scc_ast_decl_t *decl,
@@ -205,7 +196,7 @@ static inline void scc_ast_stmt_do_while_init(scc_ast_stmt_t *stmt,
// FIXME
static inline void scc_ast_stmt_for_init(scc_ast_stmt_t *stmt,
scc_ast_type_t *init,
scc_ast_node_t *init,
scc_ast_expr_t *cond,
scc_ast_expr_t *incr,
scc_ast_stmt_t *body, scc_pos_t loc) {
@@ -591,12 +582,7 @@ static inline void scc_ast_type_union_init(scc_ast_type_t *type,
static inline void scc_ast_type_enum_init(scc_ast_type_t *type,
const char *name,
scc_ast_decl_t *decl, scc_pos_t loc) {
Assert(type != null);
type->base.loc = loc;
type->base.type = SCC_AST_TYPE_ENUM;
type->quals = (scc_ast_decl_specifier_t){0}; // FIXME
type->enumeration.name = name;
type->enumeration.decl = decl;
_scc_ast_type_record_init(type, SCC_AST_TYPE_ENUM, name, decl, loc);
}
static inline void scc_ast_type_typedef_init(scc_ast_type_t *type,

View File

@@ -195,10 +195,6 @@ static const char *get_op_str(scc_ast_expr_op_t op) {
return "++";
case SCC_AST_OP_POSTFIX_DECREMENT:
return "--";
case SCC_AST_OP_MEMBER_ACCESS:
return ".";
case SCC_AST_OP_PTR_MEMBER_ACCESS:
return "->";
default:
return "<op>";
}
@@ -300,8 +296,8 @@ static void dump_type_impl(scc_ast_type_t *type, scc_tree_dump_ctx_t *ctx) {
}
break;
case SCC_AST_TYPE_ENUM:
if (type->enumeration.name) {
BUILD_TYPE_NAME(ctx, "enum ", type->enumeration.name);
if (type->record.name) {
BUILD_TYPE_NAME(ctx, "enum ", type->record.name);
} else {
PRINT_QUOTED_VALUE(ctx, "anonymous enum");
}
@@ -327,17 +323,6 @@ static void dump_type_impl(scc_ast_type_t *type, scc_tree_dump_ctx_t *ctx) {
dump_child_node((scc_ast_node_t *)type->array.size, ctx, true);
}
break;
case SCC_AST_TYPE_STRUCT:
if (type->enumeration.decl) {
dump_child_node((scc_ast_node_t *)type->enumeration.decl, ctx,
true);
}
break;
case SCC_AST_TYPE_ENUM:
if (type->record.decl) {
dump_child_node((scc_ast_node_t *)type->record.decl, ctx, true);
}
break;
case SCC_AST_TYPE_FUNCTION:
scc_vec_push(ctx->stack, false);
scc_tree_print_indent(ctx);
@@ -585,6 +570,7 @@ static void dump_stmt_impl(scc_ast_stmt_t *stmt, scc_tree_dump_ctx_t *ctx) {
case SCC_AST_STMT_BREAK:
case SCC_AST_STMT_CONTINUE:
end_node_dump(ctx);
break;
case SCC_AST_STMT_DEFAULT:
@@ -645,6 +631,7 @@ static void dump_decl_impl(scc_ast_decl_t *decl, scc_tree_dump_ctx_t *ctx) {
case SCC_AST_DECL_STRUCT:
case SCC_AST_DECL_UNION:
case SCC_AST_DECL_ENUM:
scc_vec_foreach(decl->record.fields, i) {
dump_child_node(
(scc_ast_node_t *)scc_vec_at(decl->record.fields, i), ctx,
@@ -652,14 +639,6 @@ static void dump_decl_impl(scc_ast_decl_t *decl, scc_tree_dump_ctx_t *ctx) {
}
break;
case SCC_AST_DECL_ENUM:
scc_vec_foreach(decl->enumeration.enumerators, i) {
dump_child_node(
(scc_ast_node_t *)scc_vec_at(decl->enumeration.enumerators, i),
ctx, i + 1 == scc_vec_size(decl->enumeration.enumerators));
}
break;
case SCC_AST_DECL_TYPEDEF:
if (decl->typedef_decl.type) {
dump_child_node((scc_ast_node_t *)decl->typedef_decl.type, ctx,