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:
zzy
2026-06-01 12:14:13 +08:00
parent 8b817da3b6
commit 31d7e91ef1
45 changed files with 1918 additions and 1551 deletions

View File

@@ -0,0 +1,9 @@
[package]
name = "scc_elf"
version = "0.1.0"
authors = []
description = ""
dependencies = [{ name = "scc_utils", path = "../../../runtime/scc_utils" }]
# features = {}
# default_features = []

View File

@@ -23,6 +23,11 @@ static u32 reserve_file(scc_pe_builder_t *builder, u32 len, u32 align) {
builder->file_offset = reserve_align(builder->file_offset, align);
u32 offset = builder->file_offset;
/* TODO overflow check: u32 溢出保护, 设计未定暂用简单检查 */
if (builder->file_offset + len < builder->file_offset) {
LOG_FATAL("reserve_file: u32 overflow (offset=%u len=%u)",
builder->file_offset, len);
}
builder->file_offset += len;
LOG_INFO("reserve_file %u bytes at %u [%u]", len, offset,
builder->file_offset);
@@ -35,6 +40,11 @@ static u32 reserve_virtual(scc_pe_builder_t *builder, u32 len, u32 align) {
}
u32 offset = builder->virtual_offset;
/* TODO overflow check: u32 溢出保护, 设计未定暂用简单检查 */
if (builder->virtual_offset + len < builder->virtual_offset) {
LOG_FATAL("reserve_virtual: u32 overflow (offset=%u len=%u)",
builder->virtual_offset, len);
}
builder->virtual_offset += len;
builder->virtual_offset = reserve_align(builder->virtual_offset, align);
LOG_INFO("reserve_virtual %u bytes at %u [%u]", len, offset,

View File

@@ -63,13 +63,19 @@ void scc_pe_idata_builder_init(scc_pe_idata_builder_t *builder,
}
u32 scc_pe_reserve_idata(scc_pe_idata_builder_t *builder) {
/* TODO overflow check: idata_size 为 u32, 乘法可能溢出,
设计未定暂不插入 stdint 类型, 仅做简单防护 */
u32 idata_size = (scc_vec_size(builder->idata_libs) + 1) *
sizeof(IMAGE_IMPORT_DESCRIPTOR);
scc_vec_foreach(builder->idata_libs, i) {
scc_pe_idata_lib_t *lib = &scc_vec_at(builder->idata_libs, i);
idata_size += (scc_vec_size(lib->symbol_names) + 1) * 2 *
sizeof(IMAGE_THUNK_DATA64);
u32 thunk_size = (scc_vec_size(lib->symbol_names) + 1) * 2 *
sizeof(IMAGE_THUNK_DATA64);
if (idata_size + thunk_size < idata_size) {
LOG_FATAL("scc_pe_reserve_idata: u32 overflow");
}
idata_size += thunk_size;
scc_winpe_hnt_builder_push(&builder->hnt_builder, lib->name, 0);
scc_vec_foreach(lib->symbol_names, j) {
@@ -78,6 +84,9 @@ u32 scc_pe_reserve_idata(scc_pe_idata_builder_t *builder) {
}
}
builder->hnt_builder.section_offset = idata_size;
if (idata_size + scc_vec_size(builder->hnt_builder.data) < idata_size) {
LOG_FATAL("scc_pe_reserve_idata: u32 overflow on hnt data");
}
idata_size += scc_vec_size(builder->hnt_builder.data);
scc_vec_realloc(builder->buffer, idata_size);

View File

@@ -212,8 +212,7 @@ void sccf2pe(scc_pe_builder_t *builder, const sccf_t *sccf) {
scc_mcode_t mcode;
scc_mcode_init(&mcode, SCC_MCODE_ARCH_X86_64);
// FIXME hack
mcode.code = *(scc_mcode_buff_t *)code_data;
scc_mcode_adopt_buf(&mcode, (scc_mcode_buff_t *)code_data);
scc_vec_foreach(relocs, i) {
sccf_reloc_t *reloc = &scc_vec_at(relocs, i);
if (reloc->reloc_type == SCCF_RELOC_TYPE_EMPTY) {
@@ -243,7 +242,7 @@ void sccf2pe(scc_pe_builder_t *builder, const sccf_t *sccf) {
scc_x86_patch(&mcode, SCC_X86_PATCH_PC32, reloc->offset + reloc->addend,
(i64)rva - (i64)code_range.virual_address);
}
*(scc_mcode_buff_t *)code_data = mcode.code;
scc_mcode_disown_buf(&mcode, (scc_mcode_buff_t *)code_data);
scc_pe_write_header(builder, &config);
if (code_data != nullptr) {

View File

@@ -2,6 +2,7 @@
#include <sccf2pe.h>
#include <sccf_builder.h>
#include <stdio.h>
#include <x86/scc_x86_patch.h>
int main() {
char data[] = "Hello, World from SCC PE Builder!\n\0";
@@ -56,13 +57,13 @@ int main() {
.offset = 7,
.sect_type = SCCF_SECT_CODE,
.sym_idx = str_idx,
.reloc_type = SCCF_RELOC_TYPE_REL});
.reloc_type = SCC_X86_OPR_RELBR});
sccf_builder_add_reloc(&builder,
(sccf_reloc_t){.addend = 4,
.offset = 13,
.sect_type = SCCF_SECT_CODE,
.sym_idx = puts_idx,
.reloc_type = SCCF_RELOC_TYPE_REL});
.reloc_type = SCC_X86_OPR_RELBR});
const sccf_t *sccf = sccf_builder_to_sccf(&builder);
scc_pe_builder_t pe_builder;

View File

@@ -0,0 +1,9 @@
[package]
name = "type_abi"
version = "0.1.0"
authors = []
description = ""
dependencies = [
{ name = "scc_core", path = "../../../runtime/scc_core" },
]

View File

@@ -0,0 +1,53 @@
#ifndef __SCC_TYPE_ABI_H__
#define __SCC_TYPE_ABI_H__
#include <scc_core.h>
/// 类型布局的三个本质可变点
typedef struct scc_type_abi scc_type_abi_t;
struct scc_type_abi {
int ptr_size;
int ptr_align;
int endian; // 0 = little, 1 = big
// C 类型大小 (AST → HIR 时需要)
int char_size;
int short_size;
int int_size;
int long_size;
int long_long_size;
int float_size;
int double_size;
int long_double_size;
int va_list_size;
// byte_size → 对齐。NULL = 自然对齐 (return byte_size)
int (*type_align)(int byte_size);
// 字段对齐覆盖。NULL = 用 type_align 的值
int (*field_align)(int field_size, int natural_align);
// 聚合体总对齐。NULL = max 字段 align
int (*aggregate_align)(int max_field_align);
};
static inline int scc_type_abi_get_type_align(const scc_type_abi_t *abi,
int byte_size) {
return abi->type_align ? abi->type_align(byte_size) : byte_size;
}
static inline int scc_type_abi_get_field_align(const scc_type_abi_t *abi,
int field_size,
int natural_align) {
return abi->field_align ? abi->field_align(field_size, natural_align)
: natural_align;
}
static inline int scc_type_abi_get_aggregate_align(const scc_type_abi_t *abi,
int max_field_align) {
return abi->aggregate_align ? abi->aggregate_align(max_field_align)
: max_field_align;
}
#endif /* __SCC_TYPE_ABI_H__ */

View File

@@ -0,0 +1,27 @@
#ifndef __SCC_TYPE_ABI_SYSTEMV_X64_H__
#define __SCC_TYPE_ABI_SYSTEMV_X64_H__
#include "scc_type_abi.h"
/// SystemV x86-64: long = 8, long_double = 16, 全部自然对齐
static const scc_type_abi_t SCC_TYPE_ABI_SYSTEMV_X64 = {
.ptr_size = 8,
.ptr_align = 8,
.endian = 0,
.char_size = 1,
.short_size = 2,
.int_size = 4,
.long_size = 8, // SysV: long = 8 bytes
.long_long_size = 8,
.float_size = 4,
.double_size = 8,
.long_double_size = 16, // SysV x86-64: long double = 80-bit extended precision, padded to 16
.va_list_size = 8,
.type_align = NULL,
.field_align = NULL,
.aggregate_align = NULL,
};
#endif /* __SCC_TYPE_ABI_SYSTEMV_X64_H__ */

View File

@@ -0,0 +1,35 @@
#ifndef __SCC_TYPE_ABI_WIN_X64_H__
#define __SCC_TYPE_ABI_WIN_X64_H__
#include "scc_type_abi.h"
/// Win64: ≥16B 类型在 struct 中对齐到 8 而非 16
int scc_win64_field_align(int field_size, int natural_align);
#ifdef __SCC_TYPE_ABI_WIN_X64_IMPL__
int scc_win64_field_align(int field_size, int natural_align) {
(void)field_size;
return natural_align >= 16 ? 8 : natural_align;
}
#endif
static const scc_type_abi_t SCC_TYPE_ABI_WIN_X64 = {
.ptr_size = 8,
.ptr_align = 8,
.endian = 0,
.char_size = 1,
.short_size = 2,
.int_size = 4,
.long_size = 4, // Win64: long = 4 bytes
.long_long_size = 8,
.float_size = 4,
.double_size = 8,
.long_double_size = 8,
.va_list_size = 4,
.type_align = NULL,
.field_align = scc_win64_field_align,
.aggregate_align = NULL,
};
#endif /* __SCC_TYPE_ABI_WIN_X64_H__ */