diff --git a/libs/ast2ir/src/scc_ast2ir.c b/libs/ast2ir/src/scc_ast2ir.c index 6e2feb2..0108964 100644 --- a/libs/ast2ir/src/scc_ast2ir.c +++ b/libs/ast2ir/src/scc_ast2ir.c @@ -508,7 +508,6 @@ void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, const scc_ast_decl_t *decl, } alloc_val_node = scc_hir_builder_global_alloca( &ctx->builder, final_type_ref, init_val); - scc_vec_push(ctx->builder.cprog->global_vals, alloc_val_node); } else { alloc_val_node = scc_hir_builder_alloca(&ctx->builder, final_type_ref, decl->name); diff --git a/libs/ast2ir/src/scc_ast2ir_expr.c b/libs/ast2ir/src/scc_ast2ir_expr.c index c7521f7..5c3330f 100644 --- a/libs/ast2ir/src/scc_ast2ir_expr.c +++ b/libs/ast2ir/src/scc_ast2ir_expr.c @@ -647,9 +647,9 @@ scc_hir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_hir_value_ref_t index = scc_ast2ir_expr(ctx, expr->subscript.index, false); // 保证 index 至少 64 位,防止后端寄存器分配时宽度错误 - scc_hir_type_t *idx_type = - scc_hir_module_get_type_by_value(scc_ast2ir_mir_module(ctx), index); - if (idx_type != nullptr && + scc_hir_type_ref_t idx_type = + scc_hir_module_get_value(scc_ast2ir_mir_module(ctx), index)->type; + if (idx_type != SCC_CFG_ID_nullptr && scc_hir_module_type_size(scc_ast2ir_mir_module(ctx), idx_type) < sizeof(void *) * 8) { scc_hir_type_t ptrw_desc; @@ -720,14 +720,10 @@ scc_hir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, // 这里用简单的启发式:根据大小变化决定 scc_hir_type_t *src_type = scc_hir_module_get_type_by_value( scc_ast2ir_mir_module(ctx), operand); - scc_hir_type_t *dst_type = - scc_hir_module_get_type(scc_ast2ir_mir_module(ctx), target_type); - - usize src_size = scc_hir_module_type_size( - scc_ast2ir_mir_module(ctx), - src_type); // 你可能有这个函数,否则自行计算 + usize src_size = + scc_hir_module_type_size(scc_ast2ir_mir_module(ctx), operand); usize dst_size = - scc_hir_module_type_size(scc_ast2ir_mir_module(ctx), dst_type); + scc_hir_module_type_size(scc_ast2ir_mir_module(ctx), target_type); int conv_kind; if (dst_size > src_size) { @@ -776,10 +772,8 @@ scc_hir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, } // 2. 计算大小 - const scc_hir_type_t *type = - scc_hir_module_get_type(scc_ast2ir_mir_module(ctx), hir_type); usize dst_size = - scc_hir_module_type_size(scc_ast2ir_mir_module(ctx), type); + scc_hir_module_type_size(scc_ast2ir_mir_module(ctx), hir_type); scc_ap_t val; val.data.digit = dst_size / 8; diff --git a/libs/ir/hir/include/scc_hir_layout.h b/libs/ir/hir/include/scc_hir_layout.h index d74a832..1d85d13 100644 --- a/libs/ir/hir/include/scc_hir_layout.h +++ b/libs/ir/hir/include/scc_hir_layout.h @@ -1,10 +1,10 @@ #ifndef __SCC_HIR_LAYOUT_H__ #define __SCC_HIR_LAYOUT_H__ -#include +#include "scc_hir_module.h" #include -#define SCC_ALIGN_UP(x, align) (((x) + (align)-1) & ~((align)-1)) +#define SCC_ALIGN_UP(x, align) (((x) + (align) - 1) & ~((align) - 1)) /// 结构体/联合体字段布局 typedef struct scc_hir_field_layout { @@ -30,9 +30,9 @@ int scc_hir_type_align(scc_hir_module_t *mod, scc_hir_type_ref_t type, int scc_hir_field_offset(scc_hir_module_t *mod, scc_hir_type_ref_t type, int field_idx, const scc_type_abi_t *abi); -scc_hir_aggregate_layout_t * -scc_hir_aggregate_layout(scc_hir_module_t *mod, scc_hir_type_ref_t type, - const scc_type_abi_t *abi); +scc_hir_aggregate_layout_t *scc_hir_aggregate_layout(scc_hir_module_t *mod, + scc_hir_type_ref_t type, + const scc_type_abi_t *abi); void scc_hir_aggregate_layout_free(scc_hir_aggregate_layout_t *layout); diff --git a/libs/ir/hir/include/scc_hir_module.h b/libs/ir/hir/include/scc_hir_module.h index c41a1ed..c3e567b 100644 --- a/libs/ir/hir/include/scc_hir_module.h +++ b/libs/ir/hir/include/scc_hir_module.h @@ -4,9 +4,11 @@ #include "scc_hir_def.h" #include #include +#include typedef struct { scc_cfg_module_t cfg_module; + const scc_type_abi_t *abi; scc_cfg_id_t value_uid; scc_cfg_id_t type_uid; @@ -20,7 +22,7 @@ typedef struct { SCC_VEC(scc_hir_func_meta_t *) funcs_meta; } scc_hir_module_t; -void scc_hir_module_init(scc_hir_module_t *ctx); +void scc_hir_module_init(scc_hir_module_t *ctx, const scc_type_abi_t *abi); void scc_hir_module_drop(scc_hir_module_t *ctx); scc_hir_type_ref_t scc_hir_module_add_type(scc_hir_module_t *ctx, const scc_hir_type_t *type); @@ -38,7 +40,7 @@ scc_hir_bblock_t *scc_hir_module_get_bblock(scc_hir_module_t *ctx, scc_hir_bblock_ref_t ref); scc_hir_func_t *scc_hir_module_get_func(scc_hir_module_t *ctx, scc_hir_func_ref_t ref); -usize scc_hir_module_type_size(scc_hir_module_t *ctx, scc_hir_type_t *type); +usize scc_hir_module_type_size(scc_hir_module_t *ctx, scc_hir_type_ref_t ref); static inline scc_hir_type_t * scc_hir_module_get_type_by_value(scc_hir_module_t *ctx, diff --git a/libs/ir/hir/include/scc_hir_prog.h b/libs/ir/hir/include/scc_hir_prog.h index 95fbeb2..659e07d 100644 --- a/libs/ir/hir/include/scc_hir_prog.h +++ b/libs/ir/hir/include/scc_hir_prog.h @@ -1,7 +1,6 @@ #ifndef __SCC_HIR_PROG_H__ #define __SCC_HIR_PROG_H__ -#include "scc_hir_def.h" #include "scc_hir_module.h" typedef struct scc_hir_cprog { @@ -11,7 +10,7 @@ typedef struct scc_hir_cprog { scc_hir_func_ref_vec_t func_decls; /* 所有函数包括定义的声明 */ } scc_hir_cprog_t; -void scc_hir_cprog_init(scc_hir_cprog_t *in); +void scc_hir_cprog_init(scc_hir_cprog_t *in, const scc_type_abi_t *abi); void scc_hir_cprog_drop(scc_hir_cprog_t *in); #endif /* __SCC_HIR_PROG_H__ */ diff --git a/libs/ir/hir/src/scc_hir_builder.c b/libs/ir/hir/src/scc_hir_builder.c index 379c8a6..466dabc 100644 --- a/libs/ir/hir/src/scc_hir_builder.c +++ b/libs/ir/hir/src/scc_hir_builder.c @@ -285,21 +285,7 @@ scc_hir_type_ref_t scc_hir_builder_type(scc_hir_builder_t *builder, scc_hir_value_ref_t scc_hir_builder_const_string(scc_hir_builder_t *builder, const char *str, usize len) { SCC_HIR_BUILDER_CHECK_NO_BORROW(builder); - scc_hir_type_ref_t u8_type = scc_hir_builder_type_u8(builder); - scc_hir_type_t array_type = { - .tag = SCC_HIR_TYPE_ARRAY, - .data.array.base = u8_type, - .data.array.len = len - 1, // 包含 nullptr 结尾 - }; - scc_hir_type_ref_t array_type_ref = - scc_hir_builder_type(builder, &array_type); - // 5. 创建聚合节点 - scc_hir_value_t const_array_value = { - .tag = SCC_HIR_VALUE_TAG_ARRAY, - .type = array_type_ref, - .data.const_array.base_type = u8_type, - }; scc_str_t buf; scc_str_init(&buf); // FIXME content to real string @@ -348,8 +334,23 @@ scc_hir_value_ref_t scc_hir_builder_const_string(scc_hir_builder_t *builder, } } } - scc_str_append_ch(&buf, '\0'); - usize buf_len = scc_str_len(&buf); + usize buf_len = scc_str_len(&buf) + 1; + + scc_hir_type_ref_t u8_type = scc_hir_builder_type_u8(builder); + scc_hir_type_t array_type = { + .tag = SCC_HIR_TYPE_ARRAY, + .data.array.base = u8_type, + .data.array.len = buf_len, // 包含 '\0' 结尾 + }; + + scc_hir_type_ref_t array_type_ref = + scc_hir_builder_type(builder, &array_type); + scc_hir_value_t const_array_value = { + .tag = SCC_HIR_VALUE_TAG_ARRAY, + .type = array_type_ref, + .data.const_array.base_type = u8_type, + }; + scc_vec_unsafe_from_buffer(const_array_value.data.const_array.fields, (u8 *)scc_str_move_cstr(&buf), buf_len); scc_hir_value_ref_t const_array_ref = diff --git a/libs/ir/hir/src/scc_hir_dump.c b/libs/ir/hir/src/scc_hir_dump.c index 01c1ac2..eccdeff 100644 --- a/libs/ir/hir/src/scc_hir_dump.c +++ b/libs/ir/hir/src/scc_hir_dump.c @@ -327,7 +327,11 @@ void scc_hir_dump_value_linear(scc_hir_dump_t *ctx, case SCC_HIR_VALUE_TAG_GLOBAL_ALLOC: scc_tree_dump_append_fmt(ctx->dump_ctx, "global %s", value->name); scc_tree_dump_begin_line(ctx->dump_ctx); - scc_hir_dump_value_linear(ctx, value->data.global_alloc.value); + if (value->data.global_alloc.value) { + scc_hir_dump_value_linear(ctx, value->data.global_alloc.value); + } else { + scc_tree_dump_append(ctx->dump_ctx, ""); + } break; case SCC_HIR_VALUE_TAG_ARRAY: scc_tree_dump_append(ctx->dump_ctx, "const_array "); diff --git a/libs/ir/hir/src/scc_hir_module.c b/libs/ir/hir/src/scc_hir_module.c index 87d2a19..2e713aa 100644 --- a/libs/ir/hir/src/scc_hir_module.c +++ b/libs/ir/hir/src/scc_hir_module.c @@ -1,7 +1,10 @@ +#include #include -void scc_hir_module_init(scc_hir_module_t *ctx) { +void scc_hir_module_init(scc_hir_module_t *ctx, const scc_type_abi_t *abi) { scc_cfg_module_init(&ctx->cfg_module); + Assert(abi != nullptr); + ctx->abi = abi; scc_vec_init(ctx->values); scc_vec_init(ctx->types); @@ -126,45 +129,7 @@ scc_hir_func_t *scc_hir_module_get_func(scc_hir_module_t *ctx, return scc_cfg_module_unsafe_get_func(&ctx->cfg_module, ref); } -usize scc_hir_module_type_size(scc_hir_module_t *ctx, scc_hir_type_t *type) { - Assert(type != nullptr); - switch (type->tag) { - case SCC_HIR_TYPE_unknown: - case SCC_HIR_TYPE_void: - return 0; - case SCC_HIR_TYPE_i8: - case SCC_HIR_TYPE_u8: - return 8; - case SCC_HIR_TYPE_i16: - case SCC_HIR_TYPE_u16: - return 16; - case SCC_HIR_TYPE_f32: - case SCC_HIR_TYPE_i32: - case SCC_HIR_TYPE_u32: - return 32; - case SCC_HIR_TYPE_f64: - case SCC_HIR_TYPE_i64: - case SCC_HIR_TYPE_u64: - return 64; - case SCC_HIR_TYPE_FUNC: - case SCC_HIR_TYPE_PTR: { - // TODO - // 目标指针大小,可以定义为 64(64位)或从 ABI 获取 - // 假设你的目标架构是 64 位 - return 64; - } - case SCC_HIR_TYPE_ARRAY: { - usize elem_size = scc_hir_module_type_size( - ctx, scc_hir_module_get_type(ctx, type->data.array.base)); - return elem_size * type->data.array.len; - } - case SCC_HIR_TYPE_STRUCT: - case SCC_HIR_TYPE_UNION: - // 暂时无法计算,保守返回 0 或报错 - LOG_ERROR("Cannot compute size of struct/union without layout info"); - return 0; - default: - LOG_ERROR("Unknown type tag %d", type->tag); - return 0; - } +usize scc_hir_module_type_size(scc_hir_module_t *ctx, scc_hir_type_ref_t ref) { + Assert(ref != 0); + return scc_hir_type_size(ctx, ref, ctx->abi); } diff --git a/libs/ir/hir/src/scc_hir_prog.c b/libs/ir/hir/src/scc_hir_prog.c index 3d1273b..18f2cde 100644 --- a/libs/ir/hir/src/scc_hir_prog.c +++ b/libs/ir/hir/src/scc_hir_prog.c @@ -1,10 +1,11 @@ #include -void scc_hir_cprog_init(scc_hir_cprog_t *in) { +void scc_hir_cprog_init(scc_hir_cprog_t *in, const scc_type_abi_t *abi) { + Assert(abi != nullptr); scc_vec_init(in->func_decls); scc_vec_init(in->func_defs); scc_vec_init(in->global_vals); - scc_hir_module_init(&in->module); + scc_hir_module_init(&in->module, abi); } void scc_hir_cprog_drop(scc_hir_cprog_t *in) { diff --git a/libs/ir/lir/src/scc_hir2lir.c b/libs/ir/lir/src/scc_hir2lir.c index 43c135f..cec0ae6 100644 --- a/libs/ir/lir/src/scc_hir2lir.c +++ b/libs/ir/lir/src/scc_hir2lir.c @@ -5,6 +5,7 @@ #include #include +#include /* ---------- 转换上下文 ---------- */ typedef struct { @@ -90,7 +91,7 @@ static void ir_type_to_lir_size_ext(scc_hir_type_t *type, u8 *out_size, *out_ext = SCC_LIR_EXT_NONE; break; default: - Panic("unsupported IR type in lowering"); + Panic("unsupported IR type in lowering at %d", type->tag); } } @@ -386,14 +387,12 @@ static void translate_hir_value(ir2lir_ctx_t *ctx, scc_hir_value_t *value, // 2. 获取元素类型和大小 scc_hir_type_t *ptr_type = scc_hir_module_get_type(ctx->hir_module, value->type); - scc_hir_type_t *pointee = NULL; + scc_hir_type_ref_t pointee = SCC_CFG_ID_nullptr; if (ptr_type->tag == SCC_HIR_TYPE_PTR) { - pointee = scc_hir_module_get_type(ctx->hir_module, - ptr_type->data.pointer.base); + pointee = ptr_type->data.pointer.base; } else if (ptr_type->tag == SCC_HIR_TYPE_ARRAY) { // 数组名退化为指针,元素类型为数组的元素类型 - pointee = scc_hir_module_get_type(ctx->hir_module, - ptr_type->data.array.base); + pointee = ptr_type->data.array.base; } else { Panic("GET_ELEM_PTR on non-pointer/array type"); } @@ -414,10 +413,8 @@ static void translate_hir_value(ir2lir_ctx_t *ctx, scc_hir_value_t *value, } break; case SCC_HIR_VALUE_TAG_ALLOC: { Assert(ty != nullptr); - scc_hir_type_t *alloc_ty = - scc_hir_module_get_type(ctx->hir_module, ty->data.pointer.base); int alloc_size_bits = - scc_hir_module_type_size(ctx->hir_module, alloc_ty); + scc_hir_module_type_size(ctx->hir_module, ty->data.pointer.base); scc_lir_instr_t instr = {.op = SCC_LIR_ALLOCA, .size_bits = SCC_LIR_SIZE_64, .to = SCC_LIR_VREG(dst_vreg), @@ -506,12 +503,10 @@ static void translate_hir_value(ir2lir_ctx_t *ctx, scc_hir_value_t *value, scc_lir_builder_add_instr(ctx, &instr); } else { // 计算源类型宽度 - scc_hir_type_t *src_type = scc_hir_module_get_type( + int from_size_bits = scc_hir_module_type_size( ctx->hir_module, scc_hir_module_get_value( ctx->hir_module, value->data.conv.operand) ->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_bits = size_bits, .ext = conv_ext, @@ -671,13 +666,27 @@ void scc_hir2lir(scc_lir_module_t *module, scc_hir_cprog_t *cprog) { scc_hir_value_ref_t gv_ref = scc_vec_at(cprog->global_vals, i); scc_hir_value_t *galloc = scc_hir_module_get_value(&cprog->module, gv_ref); - Assert(galloc->tag == SCC_HIR_VALUE_TAG_GLOBAL_ALLOC); + Assert(galloc != nullptr && + galloc->tag == SCC_HIR_VALUE_TAG_GLOBAL_ALLOC); scc_hir_value_t *val = scc_hir_module_get_value( &cprog->module, galloc->data.global_alloc.value); - scc_hir_buffer_t *data = &val->data.const_array.fields; - scc_lir_symbol_id_t id = scc_lir_module_add_data( - module, galloc->name, SCC_CFG_SYMBOL_KIND_DATA, - scc_vec_unsafe_get_data(*data), scc_vec_size(*data), 0); + scc_lir_symbol_id_t id = SCC_CFG_ID_nullptr; + + int size = scc_hir_module_type_size(&cprog->module, val->type); + Assert(size > 0); + if (val == nullptr) { + // TODO char == 8 bit + id = scc_lir_module_add_data(module, galloc->name, + SCC_CFG_SYMBOL_KIND_DATA, nullptr, + size / 8, 0); + } else { + scc_hir_buffer_t *data = &val->data.const_array.fields; + Assert(scc_vec_size(*data) * 8 == size); + // TODO char == 8 bit + id = scc_lir_module_add_data( + module, galloc->name, SCC_CFG_SYMBOL_KIND_DATA, + scc_vec_unsafe_get_data(*data), size / 8, 0); + } Assert(id != SCC_CFG_ID_nullptr); } diff --git a/libs/mcode/include/x86/scc_x86_encode.h b/libs/mcode/include/x86/scc_x86_encode.h index f432669..fe9b487 100644 --- a/libs/mcode/include/x86/scc_x86_encode.h +++ b/libs/mcode/include/x86/scc_x86_encode.h @@ -50,6 +50,7 @@ typedef struct { static inline scc_x86_operand_value_t scc_x86_op_preg(scc_x86_reg_t reg, u8 size_bits) { + Assert(size_bits % 8 == 0); scc_x86_operand_value_t o = { .kind = SCC_X86_OPR_REG, .reg = reg, @@ -61,6 +62,7 @@ static inline scc_x86_reg_t scc_x86_op_vreg_reg(int vreg) { return (int)SCC_X86_REG_COUNT + vreg; } static inline scc_x86_operand_value_t scc_x86_op_vreg(int vreg, u8 size_bits) { + Assert(size_bits % 8 == 0); scc_x86_operand_value_t o = { .kind = SCC_X86_OPR_REG, .reg = scc_x86_op_vreg_reg(vreg), @@ -77,6 +79,7 @@ static inline scc_x86_operand_value_t scc_x86_op_relbr(i32 rel) { return o; } static inline scc_x86_operand_value_t scc_x86_op_imm(i64 imm, u8 size_bits) { + Assert(size_bits % 8 == 0); scc_x86_operand_value_t o = { .kind = SCC_X86_OPR_IMM, .simm0 = imm, @@ -86,6 +89,7 @@ static inline scc_x86_operand_value_t scc_x86_op_imm(i64 imm, u8 size_bits) { } static inline scc_x86_operand_value_t scc_x86_op_mem(scc_x86_mem_t mem, u8 size_bits) { + Assert(size_bits % 8 == 0); scc_x86_operand_value_t o = { .kind = SCC_X86_OPR_MEM, .mem = mem, diff --git a/src/main.c b/src/main.c index 197cfec..ce5244d 100644 --- a/src/main.c +++ b/src/main.c @@ -244,7 +244,7 @@ sstream_drop: scc_ast2ir_ctx_t ast2ir_ctx; scc_hir_cprog_t cprog; - scc_hir_cprog_init(&cprog); + scc_hir_cprog_init(&cprog, &SCC_TYPE_ABI_WIN_X64); scc_ast2ir_ctx_init(&ast2ir_ctx, &SCC_TYPE_ABI_WIN_X64, &ast_module, &cprog); scc_ast2ir_run(&ast2ir_ctx, translation_unit);