refactor(ast2ir): 移除全局值向量的直接推送
移除了在全局变量声明处理中对全局值向量的直接推送操作, 以优化内存管理和值引用的一致性。 fix(hir): 修复表达式索引类型检查中的空指针访问 修正了表达式处理中索引类型的获取方式,从使用类型指针改为使用类型引用, 并更新了空值检查条件以避免潜在的空指针解引用问题。 perf(hir): 优化类型大小计算性能 将类型大小计算逻辑从模块内部实现替换为使用HIR布局系统提供的统一接口, 提高计算效率和代码复用性。 refactor(hir): 统一字符串常量构建流程 重构了字符串常量的创建过程,简化了类型定义步骤并确保包含正确的空终止符。 fix(dump): 改进全局分配值转储的健壮性 添加了对空初始化值的检查,当全局分配没有初始值时显示零初始化器, 避免访问空指针导致的程序崩溃。 refactor(x86): 增强操作数编码的安全性 在x86指令编码中添加了对操作数字节对齐的断言检查,确保所有操作数都符合 字节边界对齐要求。 chore(build): 更新头文件包含路径和初始化参数 调整了头文件包含路径格式,并更新了HIR程序和模块的初始化函数签名, 传入ABI参数以支持更准确的目标平台特性。
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
#ifndef __SCC_HIR_LAYOUT_H__
|
||||
#define __SCC_HIR_LAYOUT_H__
|
||||
|
||||
#include <scc_hir_module.h>
|
||||
#include "scc_hir_module.h"
|
||||
#include <scc_type_abi.h>
|
||||
|
||||
#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);
|
||||
|
||||
|
||||
@@ -4,9 +4,11 @@
|
||||
#include "scc_hir_def.h"
|
||||
#include <scc_cfg.h>
|
||||
#include <scc_hashtable.h>
|
||||
#include <scc_type_abi.h>
|
||||
|
||||
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,
|
||||
|
||||
@@ -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__ */
|
||||
|
||||
@@ -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 =
|
||||
|
||||
@@ -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, "<zero initializer>");
|
||||
}
|
||||
break;
|
||||
case SCC_HIR_VALUE_TAG_ARRAY:
|
||||
scc_tree_dump_append(ctx->dump_ctx, "const_array ");
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
#include <scc_hir_layout.h>
|
||||
#include <scc_hir_module.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
#include <scc_hir_prog.h>
|
||||
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user