refactor(ast2ir): 更新IR构建器接口并重构类型映射
- 将IR构建器初始化函数修改为接受cprog参数 - 添加scc_ast2ir_ctx_drop函数用于资源清理 - 更新类型标识符命名规范,从大写改为小写形式 - 替换scc_ir_ctx_get_*函数调用为scc_ir_module_get_*函数 - 移除对ir_builtin.h的依赖,改用ir_builder.h中的构建器函数 - 为整数常量创建添加专门的构建器辅助函数 fix(ir): 重构IR上下文和模块管理结构 - 将原有的scc_ir_cprog_ctx_t拆分为scc_ir_module_t和scc_ir_ctx_t - 添加scc_ir_module_t结构用于统一管理IR对象存储 - 更新IR类型枚举名称格式,从SCC_IR_TYPE_XXX改为SCC_IR_TYPE_xxx - 添加整数、无符号整数和浮点数常量联合体定义 - 移除ir_base.h和ir_builtin.h头文件,整合到scc_ir.h中 feat(ir_builder): 添加类型构建器函数和常量创建功能 - 为各种基础类型添加scc_ir_builder_type_*内联函数 - 实现scc_ir_builder_const_int函数用于创建整数常量 - 修改构建器初始化函数签名以接受cprog参数 - 更新构建器内部结构,使用指向cprog的指针而非嵌入式结构
This commit is contained in:
@@ -11,128 +11,128 @@
|
||||
static const scc_type_abi_t scc_win_x64_type_abi[] = {
|
||||
{
|
||||
.ast_type = SCC_AST_BUILTIN_TYPE_UNKNOWN,
|
||||
.ir_type = SCC_IR_TYPE_UNKNOWN,
|
||||
.ir_type = SCC_IR_TYPE_unknown,
|
||||
.size = 0,
|
||||
.alignment = 0,
|
||||
},
|
||||
{
|
||||
.ast_type = SCC_AST_BUILTIN_TYPE_VOID,
|
||||
.ir_type = SCC_IR_TYPE_VOID,
|
||||
.ir_type = SCC_IR_TYPE_void,
|
||||
.size = 0,
|
||||
.alignment = 0,
|
||||
},
|
||||
{
|
||||
.ast_type = SCC_AST_BUILTIN_TYPE_BOOL,
|
||||
.ir_type = SCC_IR_TYPE_U8,
|
||||
.ir_type = SCC_IR_TYPE_u8,
|
||||
.size = 1,
|
||||
.alignment = 1,
|
||||
},
|
||||
{
|
||||
.ast_type = SCC_AST_BUILTIN_TYPE_CHAR,
|
||||
.ir_type = SCC_IR_TYPE_I8,
|
||||
.ir_type = SCC_IR_TYPE_i8,
|
||||
.size = 1,
|
||||
.alignment = 1,
|
||||
},
|
||||
{
|
||||
.ast_type = SCC_AST_BUILTIN_TYPE_SIGNED_CHAR,
|
||||
.ir_type = SCC_IR_TYPE_I8,
|
||||
.ir_type = SCC_IR_TYPE_i8,
|
||||
.size = 1,
|
||||
.alignment = 1,
|
||||
},
|
||||
{
|
||||
.ast_type = SCC_AST_BUILTIN_TYPE_UNSIGNED_CHAR,
|
||||
.ir_type = SCC_IR_TYPE_U8,
|
||||
.ir_type = SCC_IR_TYPE_u8,
|
||||
.size = 1,
|
||||
.alignment = 1,
|
||||
},
|
||||
{
|
||||
.ast_type = SCC_AST_BUILTIN_TYPE_SHORT,
|
||||
.ir_type = SCC_IR_TYPE_I16,
|
||||
.ir_type = SCC_IR_TYPE_i16,
|
||||
.size = 2,
|
||||
.alignment = 2,
|
||||
},
|
||||
{
|
||||
.ast_type = SCC_AST_BUILTIN_TYPE_SIGNED_SHORT,
|
||||
.ir_type = SCC_IR_TYPE_I16,
|
||||
.ir_type = SCC_IR_TYPE_i16,
|
||||
.size = 2,
|
||||
.alignment = 2,
|
||||
},
|
||||
{
|
||||
.ast_type = SCC_AST_BUILTIN_TYPE_UNSIGNED_SHORT,
|
||||
.ir_type = SCC_IR_TYPE_U16,
|
||||
.ir_type = SCC_IR_TYPE_u16,
|
||||
.size = 2,
|
||||
.alignment = 2,
|
||||
},
|
||||
{
|
||||
.ast_type = SCC_AST_BUILTIN_TYPE_INT,
|
||||
.ir_type = SCC_IR_TYPE_I32,
|
||||
.ir_type = SCC_IR_TYPE_i32,
|
||||
.size = 4,
|
||||
.alignment = 4,
|
||||
},
|
||||
{
|
||||
.ast_type = SCC_AST_BUILTIN_TYPE_SIGNED_INT,
|
||||
.ir_type = SCC_IR_TYPE_I32,
|
||||
.ir_type = SCC_IR_TYPE_i32,
|
||||
.size = 4,
|
||||
.alignment = 4,
|
||||
},
|
||||
{
|
||||
.ast_type = SCC_AST_BUILTIN_TYPE_UNSIGNED_INT,
|
||||
.ir_type = SCC_IR_TYPE_U32,
|
||||
.ir_type = SCC_IR_TYPE_u32,
|
||||
.size = 4,
|
||||
.alignment = 4,
|
||||
},
|
||||
{
|
||||
.ast_type = SCC_AST_BUILTIN_TYPE_LONG,
|
||||
.ir_type = SCC_IR_TYPE_I32,
|
||||
.ir_type = SCC_IR_TYPE_i32,
|
||||
.size = 4,
|
||||
.alignment = 4,
|
||||
},
|
||||
{
|
||||
.ast_type = SCC_AST_BUILTIN_TYPE_SIGNED_LONG,
|
||||
.ir_type = SCC_IR_TYPE_I32,
|
||||
.ir_type = SCC_IR_TYPE_i32,
|
||||
.size = 4,
|
||||
.alignment = 4,
|
||||
},
|
||||
{
|
||||
.ast_type = SCC_AST_BUILTIN_TYPE_UNSIGNED_LONG,
|
||||
.ir_type = SCC_IR_TYPE_U32,
|
||||
.ir_type = SCC_IR_TYPE_u32,
|
||||
.size = 4,
|
||||
.alignment = 4,
|
||||
},
|
||||
{
|
||||
.ast_type = SCC_AST_BUILTIN_TYPE_LONG_LONG,
|
||||
.ir_type = SCC_IR_TYPE_I64,
|
||||
.ir_type = SCC_IR_TYPE_i64,
|
||||
.size = 8,
|
||||
.alignment = 8,
|
||||
},
|
||||
{
|
||||
.ast_type = SCC_AST_BUILTIN_TYPE_SIGNED_LONG_LONG,
|
||||
.ir_type = SCC_IR_TYPE_I64,
|
||||
.ir_type = SCC_IR_TYPE_i64,
|
||||
.size = 8,
|
||||
.alignment = 8,
|
||||
},
|
||||
{
|
||||
.ast_type = SCC_AST_BUILTIN_TYPE_UNSIGNED_LONG_LONG,
|
||||
.ir_type = SCC_IR_TYPE_I64,
|
||||
.ir_type = SCC_IR_TYPE_i64,
|
||||
.size = 8,
|
||||
.alignment = 8,
|
||||
},
|
||||
{
|
||||
.ast_type = SCC_AST_BUILTIN_TYPE_FLOAT,
|
||||
.ir_type = SCC_IR_TYPE_F32,
|
||||
.ir_type = SCC_IR_TYPE_f32,
|
||||
.size = 4,
|
||||
.alignment = 4,
|
||||
},
|
||||
{
|
||||
.ast_type = SCC_AST_BUILTIN_TYPE_DOUBLE,
|
||||
.ir_type = SCC_IR_TYPE_F64,
|
||||
.ir_type = SCC_IR_TYPE_f64,
|
||||
.size = 8,
|
||||
.alignment = 8,
|
||||
},
|
||||
{
|
||||
// NULL
|
||||
.ast_type = SCC_AST_BUILTIN_TYPE_UNKNOWN,
|
||||
.ir_type = SCC_IR_TYPE_UNKNOWN,
|
||||
.ir_type = SCC_IR_TYPE_unknown,
|
||||
.size = 0,
|
||||
.alignment = 0,
|
||||
},
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define __SCC_AST2IR_H__
|
||||
|
||||
#include "scc_type_abi.h"
|
||||
#include <ir_builder.h>
|
||||
#include <scc_ast.h>
|
||||
#include <scc_ir.h>
|
||||
|
||||
@@ -13,7 +14,9 @@ typedef struct {
|
||||
const scc_type_abi_t *abi;
|
||||
} scc_ast2ir_ctx_t;
|
||||
|
||||
void scc_ast2ir_ctx_init(scc_ast2ir_ctx_t *ctx, const scc_type_abi_t *abi);
|
||||
void scc_ast2ir_ctx_init(scc_ast2ir_ctx_t *ctx, const scc_type_abi_t *abi,
|
||||
scc_ir_cprog_t *cprog);
|
||||
void scc_ast2ir_ctx_drop(scc_ast2ir_ctx_t *ctx);
|
||||
|
||||
void scc_ast2ir_translation_unit(scc_ast2ir_ctx_t *ctx,
|
||||
scc_ast_translation_unit_t *tu);
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
#include <ir_builtin.h>
|
||||
#include <scc_ast2ir.h>
|
||||
|
||||
scc_ir_type_ref_t scc_ast2ir_type(scc_ast2ir_ctx_t *ctx,
|
||||
@@ -12,7 +11,7 @@ scc_ir_type_ref_t scc_ast2ir_type(scc_ast2ir_ctx_t *ctx,
|
||||
switch (ast_type->base.type) {
|
||||
case SCC_AST_TYPE_BUILTIN: {
|
||||
// 映射内置类型
|
||||
scc_ir_type_init(&ir_type, SCC_IR_TYPE_I32);
|
||||
scc_ir_type_init(&ir_type, SCC_IR_TYPE_i32);
|
||||
// TODO: 根据具体内置类型设置
|
||||
break;
|
||||
}
|
||||
@@ -64,20 +63,17 @@ scc_ir_type_ref_t scc_ast2ir_type(scc_ast2ir_ctx_t *ctx,
|
||||
break;
|
||||
}
|
||||
|
||||
// SCC_AST_TYPE_BUILTIN, // 内置类型
|
||||
// SCC_AST_TYPE_POINTER, // 指针类型
|
||||
// SCC_AST_TYPE_ARRAY, // 数组类型
|
||||
// SCC_AST_TYPE_FUNCTION, // 函数类型
|
||||
// SCC_AST_TYPE_STRUCT, // 结构体类型
|
||||
// SCC_AST_TYPE_UNION, // 联合类型
|
||||
// SCC_AST_TYPE_ENUM, // 枚举类型
|
||||
// SCC_AST_TYPE_TYPEDEF, // typedef 类型
|
||||
case SCC_AST_TYPE_TYPEDEF:
|
||||
break;
|
||||
|
||||
default:
|
||||
LOG_FATAL("Unsupported AST type: %d", ast_type->base.type);
|
||||
return 0;
|
||||
}
|
||||
return scc_ir_ctx_new_type(&ctx->builder.ctx, &ir_type);
|
||||
return scc_ir_builder_type(&ctx->builder, &ir_type);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -230,22 +226,32 @@ scc_ir_node_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
|
||||
// SCC_AST_OP_POSTFIX_INCREMENT, // ++ (后缀)
|
||||
// SCC_AST_OP_POSTFIX_DECREMENT, // -- (后缀)
|
||||
switch (expr->unary.op) {
|
||||
case SCC_AST_OP_UNARY_MINUS:
|
||||
case SCC_AST_OP_UNARY_MINUS: {
|
||||
// 负号
|
||||
// 实现为0 - operand
|
||||
return scc_ir_builder_binop(
|
||||
&ctx->builder, SCC_IR_OP_SUB,
|
||||
scc_ir_ctx_get_builtin_zero(&ctx->builder.ctx), operand);
|
||||
scc_ir_const_int_t value;
|
||||
scc_ir_type_ref_t type_ref = scc_ir_builder_type_i32(&ctx->builder);
|
||||
value.int32 = 0;
|
||||
scc_ir_node_ref_t zero_ref =
|
||||
scc_ir_builder_const_int(&ctx->builder, type_ref, value);
|
||||
return scc_ir_builder_binop(&ctx->builder, SCC_IR_OP_SUB, zero_ref,
|
||||
operand);
|
||||
}
|
||||
case SCC_AST_OP_BITWISE_NOT:
|
||||
// 按位取反
|
||||
return scc_ir_builder_binop(&ctx->builder, SCC_IR_OP_NOT, operand,
|
||||
0);
|
||||
case SCC_AST_OP_LOGICAL_NOT:
|
||||
case SCC_AST_OP_LOGICAL_NOT: {
|
||||
// 逻辑非
|
||||
// 实现为与0比较
|
||||
return scc_ir_builder_binop(
|
||||
&ctx->builder, SCC_IR_OP_EQ,
|
||||
scc_ir_ctx_get_builtin_zero(&ctx->builder.ctx), operand);
|
||||
scc_ir_const_int_t value;
|
||||
scc_ir_type_ref_t type_ref = scc_ir_builder_type_i32(&ctx->builder);
|
||||
value.int32 = 0;
|
||||
scc_ir_node_ref_t zero_ref =
|
||||
scc_ir_builder_const_int(&ctx->builder, type_ref, value);
|
||||
return scc_ir_builder_binop(&ctx->builder, SCC_IR_OP_EQ, zero_ref,
|
||||
operand);
|
||||
}
|
||||
default:
|
||||
LOG_FATAL("Unsupported unary operator: %d", expr->unary.op);
|
||||
return 0;
|
||||
@@ -272,6 +278,10 @@ scc_ir_node_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
|
||||
// 创建调用节点(需要查找函数定义)
|
||||
scc_ir_func_ref_t func = (scc_ir_node_ref_t)(usize)scc_hashtable_get(
|
||||
&ctx->symtab, expr->call.callee->identifier._target->name);
|
||||
if (!func) {
|
||||
LOG_ERROR("Function %s not found",
|
||||
expr->call.callee->identifier._target->name);
|
||||
}
|
||||
scc_ir_node_ref_t node =
|
||||
scc_ir_builder_call(&ctx->builder, func, args.data, args.size);
|
||||
scc_vec_free(args);
|
||||
@@ -294,13 +304,18 @@ scc_ir_node_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
|
||||
for (usize i = 0; i < scc_strlen(expr->literal.lexme); i++) {
|
||||
int_lit = int_lit * 10 + (expr->literal.lexme[i] - '0');
|
||||
}
|
||||
return scc_ir_ctx_get_i32_const(&ctx->builder.ctx, int_lit);
|
||||
scc_ir_type_ref_t type_ref = scc_ir_builder_type_i32(&ctx->builder);
|
||||
scc_ir_const_int_t value;
|
||||
value.int32 = int_lit;
|
||||
return scc_ir_builder_const_int(&ctx->builder, type_ref, value);
|
||||
}
|
||||
|
||||
// SCC_AST_EXPR_INT_LITERAL, // 整数字面量
|
||||
// SCC_AST_EXPR_FLOAT_LITERAL, // 浮点字面量
|
||||
// SCC_AST_EXPR_CHAR_LITERAL, // 字符字面量
|
||||
// SCC_AST_EXPR_STRING_LITERAL, // 字符串字面量
|
||||
// case SCC_AST_EXPR_STRING_LITERAL: {
|
||||
// return scc_ir_builder_store();
|
||||
// }
|
||||
|
||||
case SCC_AST_EXPR_IDENTIFIER: {
|
||||
if (expr->identifier._target == null) {
|
||||
@@ -550,7 +565,6 @@ void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, scc_ast_decl_t *decl) {
|
||||
scc_ir_builder_func(&ctx->builder, func_type_ref, decl->name);
|
||||
scc_hashtable_set(&ctx->symtab, decl->name,
|
||||
(void *)(usize)func_ref);
|
||||
scc_vec_push(ctx->builder.cprog.func_decls, func_ref);
|
||||
}
|
||||
|
||||
if (decl->func.body == null) {
|
||||
@@ -559,7 +573,8 @@ void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, scc_ast_decl_t *decl) {
|
||||
}
|
||||
|
||||
scc_ir_builder_begin_func(&ctx->builder, func_ref, null);
|
||||
scc_ir_func_t *func = scc_ir_ctx_get_func(&ctx->builder.ctx, func_ref);
|
||||
scc_ir_func_t *func =
|
||||
scc_ir_module_get_func(ctx->builder.ctx.module, func_ref);
|
||||
Assert(func != null);
|
||||
scc_ir_builder_begin_bblock(&ctx->builder, "entry");
|
||||
scc_vec_foreach(decl->func.type->function.params, i) {
|
||||
@@ -568,7 +583,7 @@ void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, scc_ast_decl_t *decl) {
|
||||
|
||||
scc_ir_node_ref_t param_node_ref = scc_vec_at(func->params, i);
|
||||
scc_ir_node_t *param_node =
|
||||
scc_ir_ctx_get_node(&ctx->builder.ctx, param_node_ref);
|
||||
scc_ir_module_get_node(ctx->builder.ctx.module, param_node_ref);
|
||||
Assert(param_node != null);
|
||||
param_node->name = param->name;
|
||||
scc_hashtable_set(&ctx->node2ir, param,
|
||||
@@ -620,11 +635,18 @@ static int scc_cmp_node(const void *key1, const void *key2) {
|
||||
return (u32)(usize)key1 - (u32)(usize)key2;
|
||||
}
|
||||
|
||||
void scc_ast2ir_ctx_init(scc_ast2ir_ctx_t *ctx, const scc_type_abi_t *abi) {
|
||||
void scc_ast2ir_ctx_init(scc_ast2ir_ctx_t *ctx, const scc_type_abi_t *abi,
|
||||
scc_ir_cprog_t *cprog) {
|
||||
Assert(ctx != null);
|
||||
ctx->abi = abi;
|
||||
scc_ir_builder_init(&ctx->builder);
|
||||
scc_ir_builder_init(&ctx->builder, cprog);
|
||||
scc_hashtable_init(&ctx->node2ir, 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);
|
||||
}
|
||||
|
||||
void scc_ast2ir_ctx_drop(scc_ast2ir_ctx_t *ctx) {
|
||||
scc_ir_builder_drop(&ctx->builder);
|
||||
scc_hashtable_drop(&ctx->node2ir);
|
||||
scc_hashtable_drop(&ctx->symtab);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user