refactor(ast2ir): 移除全局值向量的直接推送

移除了在全局变量声明处理中对全局值向量的直接推送操作,
以优化内存管理和值引用的一致性。

fix(hir): 修复表达式索引类型检查中的空指针访问

修正了表达式处理中索引类型的获取方式,从使用类型指针改为使用类型引用,
并更新了空值检查条件以避免潜在的空指针解引用问题。

perf(hir): 优化类型大小计算性能

将类型大小计算逻辑从模块内部实现替换为使用HIR布局系统提供的统一接口,
提高计算效率和代码复用性。

refactor(hir): 统一字符串常量构建流程

重构了字符串常量的创建过程,简化了类型定义步骤并确保包含正确的空终止符。

fix(dump): 改进全局分配值转储的健壮性

添加了对空初始化值的检查,当全局分配没有初始值时显示零初始化器,
避免访问空指针导致的程序崩溃。

refactor(x86): 增强操作数编码的安全性

在x86指令编码中添加了对操作数字节对齐的断言检查,确保所有操作数都符合
字节边界对齐要求。

chore(build): 更新头文件包含路径和初始化参数

调整了头文件包含路径格式,并更新了HIR程序和模块的初始化函数签名,
传入ABI参数以支持更准确的目标平台特性。
This commit is contained in:
zzy
2026-06-02 14:47:05 +08:00
parent 31d7e91ef1
commit be33eb3942
12 changed files with 80 additions and 102 deletions

View File

@@ -5,6 +5,7 @@
#include <scc_hashtable.h>
#include <scc_hir2lir.h>
#include <scc_hir_layout.h>
/* ---------- 转换上下文 ---------- */
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);
}