refactor(ast2ir): 重构ABI类型系统并修复union结构问题
- 将scc_ast_def.h中的attr_of从union改为struct以修复结构定义问题 - 添加type_abi依赖到ast2ir模块的cbuild.toml配置文件中 - 重命名scc_ast2ir.h中的abi字段为type_abi,并更新相关初始化函数签名 - 移除废弃的scc_abi_type.h和相关平台ABI头文件 - 添加辅助函数is_variadic_marker和fixed_param_count用于处理可变参数 - 添加数组和聚合类型初始化的辅助函数
This commit is contained in:
@@ -65,10 +65,10 @@ typedef struct scc_lir_instr {
|
||||
#define SCC_LIR_ARG(n) \
|
||||
((scc_lir_val_t){.kind = SCC_LIR_INSTR_KIND_ARG, .data.arg = (n)})
|
||||
|
||||
#define SCC_LIR_SIZE_8 1
|
||||
#define SCC_LIR_SIZE_16 2
|
||||
#define SCC_LIR_SIZE_32 4
|
||||
#define SCC_LIR_SIZE_64 8
|
||||
#define SCC_LIR_SIZE_8 8
|
||||
#define SCC_LIR_SIZE_16 16
|
||||
#define SCC_LIR_SIZE_32 32
|
||||
#define SCC_LIR_SIZE_64 64
|
||||
|
||||
typedef enum {
|
||||
SCC_LIR_EXT_NONE,
|
||||
@@ -163,7 +163,7 @@ typedef enum {
|
||||
typedef scc_cfg_bblock_id_t scc_lir_bblock_id_t;
|
||||
typedef struct scc_lir_ins {
|
||||
scc_lir_op_t op;
|
||||
u8 size;
|
||||
u8 size_bits;
|
||||
scc_lir_ext_t ext;
|
||||
scc_lir_val_t to;
|
||||
scc_lir_val_t arg0;
|
||||
@@ -207,12 +207,12 @@ typedef struct scc_lir_ins {
|
||||
} parallel_copy;
|
||||
|
||||
struct scc_lir_alloca {
|
||||
int size_bytes;
|
||||
int align_bytes;
|
||||
int size_bits;
|
||||
int align_bits;
|
||||
} alloca;
|
||||
|
||||
struct scc_lir_extend {
|
||||
int from_size; // 源类型宽度(字节)
|
||||
int from_size_bits; // 源类型宽度(位)
|
||||
} extend;
|
||||
|
||||
struct {
|
||||
|
||||
@@ -294,7 +294,7 @@ static int ensure_vreg(ir2lir_ctx_t *ctx, scc_lir_val_t *val) {
|
||||
// 分配新的虚拟寄存器
|
||||
int new_vreg = ++(SCC_LIR_FUNC_META(ctx->current_func)->vregs_count);
|
||||
scc_lir_instr_t mov = {.op = SCC_LIR_MOV,
|
||||
.size = SCC_LIR_SIZE_64, // 地址宽度
|
||||
.size_bits = SCC_LIR_SIZE_64, // 地址宽度
|
||||
.to = SCC_LIR_VREG(new_vreg),
|
||||
.arg0 = *val};
|
||||
scc_lir_builder_add_instr(ctx, &mov);
|
||||
@@ -311,10 +311,10 @@ static void translate_hir_value(ir2lir_ctx_t *ctx, scc_hir_value_t *value,
|
||||
return;
|
||||
|
||||
scc_hir_type_t *ty = scc_hir_module_get_type(ctx->hir_module, value->type);
|
||||
u8 size = 0;
|
||||
u8 size_bits = 0;
|
||||
scc_lir_ext_t ext = SCC_LIR_EXT_NONE;
|
||||
if (ty != nullptr) {
|
||||
ir_type_to_lir_size_ext(ty, &size, &ext);
|
||||
ir_type_to_lir_size_ext(ty, &size_bits, &ext);
|
||||
}
|
||||
bool is_float = (ext == SCC_LIR_EXT_FLOAT);
|
||||
|
||||
@@ -331,7 +331,7 @@ static void translate_hir_value(ir2lir_ctx_t *ctx, scc_hir_value_t *value,
|
||||
|
||||
scc_lir_instr_t instr = {
|
||||
.op = op,
|
||||
.size = size,
|
||||
.size_bits = size_bits,
|
||||
.ext = ext,
|
||||
.to = SCC_LIR_VREG(dst_vreg),
|
||||
.arg0 = lhs,
|
||||
@@ -346,7 +346,7 @@ static void translate_hir_value(ir2lir_ctx_t *ctx, scc_hir_value_t *value,
|
||||
scc_lir_val_t addr =
|
||||
ir_value_to_lir_operand(ctx, value->data.load.target);
|
||||
scc_lir_instr_t instr = {.op = SCC_LIR_LOAD,
|
||||
.size = size,
|
||||
.size_bits = size_bits,
|
||||
.ext = ext,
|
||||
.to = SCC_LIR_VREG(dst_vreg),
|
||||
.arg0 = addr};
|
||||
@@ -363,15 +363,15 @@ static void translate_hir_value(ir2lir_ctx_t *ctx, scc_hir_value_t *value,
|
||||
if (ptr_type && ptr_type->tag == SCC_HIR_TYPE_PTR) {
|
||||
scc_hir_type_t *elem_type = scc_hir_module_get_type(
|
||||
ctx->hir_module, ptr_type->data.pointer.base);
|
||||
ir_type_to_lir_size_ext(elem_type, &size, &ext);
|
||||
ir_type_to_lir_size_ext(elem_type, &size_bits, &ext);
|
||||
} else {
|
||||
ty = scc_hir_module_get_type_by_value(ctx->hir_module,
|
||||
value->data.store.value);
|
||||
ir_type_to_lir_size_ext(ty, &size, &ext);
|
||||
ir_type_to_lir_size_ext(ty, &size_bits, &ext);
|
||||
}
|
||||
scc_lir_instr_t instr = {.op = SCC_LIR_STORE,
|
||||
.ext = ext,
|
||||
.size = size,
|
||||
.size_bits = size_bits,
|
||||
.arg0 = data,
|
||||
.arg1 = addr};
|
||||
scc_lir_builder_add_instr(ctx, &instr);
|
||||
@@ -397,16 +397,17 @@ static void translate_hir_value(ir2lir_ctx_t *ctx, scc_hir_value_t *value,
|
||||
} else {
|
||||
Panic("GET_ELEM_PTR on non-pointer/array type");
|
||||
}
|
||||
int elem_size = scc_hir_module_type_size(ctx->hir_module, pointee);
|
||||
int elem_size_bits = scc_hir_module_type_size(ctx->hir_module, pointee);
|
||||
|
||||
int dst_vreg = get_vreg_for_value(ctx, value_ref);
|
||||
scc_lir_instr_t instr = {
|
||||
.op = SCC_LIR_LOAD_ADDR,
|
||||
.size = SCC_LIR_SIZE_64,
|
||||
.size_bits = SCC_LIR_SIZE_64,
|
||||
.to = SCC_LIR_VREG(dst_vreg),
|
||||
.arg0 = base,
|
||||
.arg1 = index,
|
||||
.metadata.addr.scale = elem_size,
|
||||
// FIXME
|
||||
.metadata.addr.scale = elem_size_bits / 8,
|
||||
.metadata.addr.offset = 0,
|
||||
};
|
||||
scc_lir_builder_add_instr(ctx, &instr);
|
||||
@@ -415,11 +416,13 @@ static void translate_hir_value(ir2lir_ctx_t *ctx, scc_hir_value_t *value,
|
||||
Assert(ty != nullptr);
|
||||
scc_hir_type_t *alloc_ty =
|
||||
scc_hir_module_get_type(ctx->hir_module, ty->data.pointer.base);
|
||||
int alloc_size = scc_hir_module_type_size(ctx->hir_module, alloc_ty);
|
||||
int alloc_size_bits =
|
||||
scc_hir_module_type_size(ctx->hir_module, alloc_ty);
|
||||
scc_lir_instr_t instr = {.op = SCC_LIR_ALLOCA,
|
||||
.size = alloc_size,
|
||||
.size_bits = SCC_LIR_SIZE_64,
|
||||
.to = SCC_LIR_VREG(dst_vreg),
|
||||
.metadata.alloca = {alloc_size, 1}};
|
||||
// FIXME
|
||||
.metadata.alloca = {alloc_size_bits, 8}};
|
||||
scc_lir_builder_add_instr(ctx, &instr);
|
||||
} break;
|
||||
case SCC_HIR_VALUE_TAG_CALL: {
|
||||
@@ -440,7 +443,7 @@ static void translate_hir_value(ir2lir_ctx_t *ctx, scc_hir_value_t *value,
|
||||
int is_direct_call = sym->kind == SCC_CFG_SYMBOL_KIND_FUNC;
|
||||
scc_lir_instr_t instr = (scc_lir_instr_t){
|
||||
.op = is_direct_call ? SCC_LIR_CALL : SCC_LIR_CALL_INDIRECT,
|
||||
.size = size,
|
||||
.size_bits = size_bits,
|
||||
.to = SCC_LIR_VREG(dst_vreg),
|
||||
.metadata.call = {.args = lir_args,
|
||||
.arg_count = arg_count,
|
||||
@@ -497,7 +500,7 @@ static void translate_hir_value(ir2lir_ctx_t *ctx, scc_hir_value_t *value,
|
||||
// TRUNC 用 SCC_LIR_MOV 截断
|
||||
if (value->data.conv.conv_type == SCC_HIR_CONV_TRUNC) {
|
||||
scc_lir_instr_t instr = {.op = SCC_LIR_MOV,
|
||||
.size = size,
|
||||
.size_bits = size_bits,
|
||||
.to = SCC_LIR_VREG(dst_vreg),
|
||||
.arg0 = src};
|
||||
scc_lir_builder_add_instr(ctx, &instr);
|
||||
@@ -507,13 +510,15 @@ static void translate_hir_value(ir2lir_ctx_t *ctx, scc_hir_value_t *value,
|
||||
ctx->hir_module, scc_hir_module_get_value(
|
||||
ctx->hir_module, value->data.conv.operand)
|
||||
->type);
|
||||
int from_size = scc_hir_module_type_size(ctx->hir_module, src_type);
|
||||
int from_size_bits =
|
||||
scc_hir_module_type_size(ctx->hir_module, src_type);
|
||||
scc_lir_instr_t instr = {.op = SCC_LIR_EXTEND,
|
||||
.size = size,
|
||||
.size_bits = size_bits,
|
||||
.ext = conv_ext,
|
||||
.to = SCC_LIR_VREG(dst_vreg),
|
||||
.arg0 = src,
|
||||
.metadata.extend.from_size = from_size};
|
||||
.metadata.extend.from_size_bits =
|
||||
from_size_bits};
|
||||
scc_lir_builder_add_instr(ctx, &instr);
|
||||
}
|
||||
} break;
|
||||
|
||||
@@ -135,7 +135,7 @@ void scc_lir_dump_ins(scc_lir_dump_ctx_t *ctx, const scc_lir_instr_t *ins) {
|
||||
scc_tree_dump_node(td, "%s", op_to_string(ins->op));
|
||||
|
||||
// 输出宽度和扩展标志
|
||||
dump_size_ext(ctx, ins->size, ins->ext);
|
||||
dump_size_ext(ctx, ins->size_bits, ins->ext);
|
||||
scc_tree_dump_append(td, " ");
|
||||
|
||||
switch (ins->op) {
|
||||
@@ -165,8 +165,8 @@ void scc_lir_dump_ins(scc_lir_dump_ctx_t *ctx, const scc_lir_instr_t *ins) {
|
||||
break;
|
||||
case SCC_LIR_ALLOCA:
|
||||
scc_tree_dump_append_fmt(td, "(sz=%zd,al=%zd) => ",
|
||||
ins->metadata.alloca.size_bytes,
|
||||
ins->metadata.alloca.align_bytes);
|
||||
ins->metadata.alloca.size_bits,
|
||||
ins->metadata.alloca.align_bits);
|
||||
dump_operand(ctx, &ins->to);
|
||||
break;
|
||||
case SCC_LIR_EXTEND: {
|
||||
@@ -182,8 +182,8 @@ void scc_lir_dump_ins(scc_lir_dump_ctx_t *ctx, const scc_lir_instr_t *ins) {
|
||||
ext_name = "ext";
|
||||
break;
|
||||
}
|
||||
scc_tree_dump_append_fmt(td, "%s(%d<-%d) ", ext_name, ins->size * 8,
|
||||
ins->metadata.extend.from_size * 8);
|
||||
scc_tree_dump_append_fmt(td, "%s(%d<-%d) ", ext_name, ins->size_bits,
|
||||
ins->metadata.extend.from_size_bits);
|
||||
dump_operand(ctx, &ins->to);
|
||||
scc_tree_dump_append(td, " <- ");
|
||||
dump_operand(ctx, &ins->arg0);
|
||||
|
||||
Reference in New Issue
Block a user