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

@@ -12,8 +12,7 @@ typedef struct {
scc_hashtable_t continue_cache; ///< continue cache
scc_hashtable_t symtab; ///< symbol to ir_ref
scc_hashtable_t type_cache; ///< scc_ast_canon_type_t* -> scc_hir_type_ref_t
// scc_strpool_t strpool; ///< string pool
const scc_abi_type_calc_t *abi;
const scc_type_abi_t *type_abi;
cbool hint_using_value; // 转换时尽可能使用value而不是alloc
scc_ast_module_t *ast_module;
@@ -23,7 +22,7 @@ static inline scc_hir_module_t *scc_ast2ir_mir_module(scc_ast2ir_ctx_t *ctx) {
return &ctx->builder.cprog->module;
}
void scc_ast2ir_ctx_init(scc_ast2ir_ctx_t *ctx, const scc_abi_type_calc_t *abi,
void scc_ast2ir_ctx_init(scc_ast2ir_ctx_t *ctx, const scc_type_abi_t *type_abi,
scc_ast_module_t *ast_module, scc_hir_cprog_t *cprog);
void scc_ast2ir_ctx_drop(scc_ast2ir_ctx_t *ctx);
@@ -42,6 +41,38 @@ scc_hir_type_ref_t
scc_ast2ir_parse_base_type(scc_ast2ir_ctx_t *ctx,
const scc_ast_qual_type_t *ast_type);
// Utils
// 判断 AST 参数是否为 ... 生成的假 VA_LIST 参数
static cbool is_variadic_marker(const scc_ast_decl_t *decl_param) {
return decl_param->name == nullptr &&
decl_param->param.type->base.type == SCC_AST_TYPE_BUILTIN &&
scc_ast_canon_type(decl_param->param.type)->builtin.type ==
SCC_AST_BUILTIN_TYPE_VA_LIST;
}
// 计算函数类型中的固定参数个数(去掉尾部的 ... 标记)
static int fixed_param_count(const scc_ast_canon_type_t *canon) {
int n = (int)scc_vec_size(canon->function.params);
if (n > 0) {
const scc_ast_decl_t *last =
scc_vec_at(canon->function.params, (usize)(n - 1));
if (is_variadic_marker(last))
return n - 1;
}
return n;
}
void emit_array_initialization(scc_ast2ir_ctx_t *ctx,
scc_hir_value_ref_t array_ptr,
const scc_hir_type_t *array_type,
const scc_ast_expr_t *init_expr);
void emit_aggregate_initialization(scc_ast2ir_ctx_t *ctx,
scc_hir_value_ref_t base_ptr,
const scc_hir_type_t *type,
const scc_ast_expr_t *init_expr);
// ====== 类型提升Type Promotion接口 ======
/**

View File

@@ -1,106 +0,0 @@
/**
* @file scc_abi_type.h
* @brief 目标无关的类型布局描述接口
* @note 本模块仅定义接口。
*/
#ifndef __SCC_ABI_TYPE_H__
#define __SCC_ABI_TYPE_H__
#include <scc_core.h>
/**
* @brief ABI 基础类型类别枚举。
*
* 用于快速查询目标预定义的基本类型属性。复杂类型(指针、数组、结构体)
* 通过组合与递归计算。
*/
typedef enum scc_abi_base_type_kind {
SCC_ABI_TYPE_VOID,
SCC_ABI_TYPE_VA_LIST,
SCC_ABI_TYPE_CHAR,
SCC_ABI_TYPE_I_CHAR,
SCC_ABI_TYPE_U_CHAR,
SCC_ABI_TYPE_I_SHORT,
SCC_ABI_TYPE_U_SHORT,
SCC_ABI_TYPE_I_INT,
SCC_ABI_TYPE_U_INT,
SCC_ABI_TYPE_I_LONG,
SCC_ABI_TYPE_U_LONG,
SCC_ABI_TYPE_I_LONG_LONG,
SCC_ABI_TYPE_U_LONG_LONG,
SCC_ABI_TYPE_PTR,
SCC_ABI_TYPE_FLOAT,
SCC_ABI_TYPE_DOUBLE,
SCC_ABI_TYPE_USIZE,
SCC_ABI_TYPE_ISIZE,
/* 可扩展I128, F16, F128, VECTOR, ... */
} scc_abi_base_type_kind_t;
/**
* @brief 单个类型的布局信息。
*/
typedef struct scc_abi_type_layout {
int size; /**< 类型占用的字节数 */
int alignment; /**< 类型的对齐要求(字节边界) */
} scc_abi_type_layout_t;
typedef struct {
scc_abi_base_type_kind_t kind;
scc_abi_type_layout_t layout;
} scc_abi_base_type_impl_t;
#define SCC_ABI_BASE_TYPE_IMPL(type, bytes_size, alians) \
[type] = { \
.kind = type, \
.layout.size = bytes_size, \
.layout.alignment = alians, \
}
/**
* @brief 单个结构体字段的布局信息。
* 由目标布局算法填充,用于 IR 的 getelementptr 常量索引计算。
*/
typedef struct scc_abi_field_layout {
int offset; /**< 字段相对于结构体基址的字节偏移 */
int size; /**< 字段自身大小 */
int alignment; /**< 字段的对齐要求 */
} scc_abi_field_layout_t;
typedef SCC_VEC(scc_abi_field_layout_t) scc_abi_field_layout_vec_t;
/**
* @brief 获取基本类型的布局信息。
*
* 目标必须实现此函数,为每个 scc_abi_base_type_kind_t 返回正确的
* size/alignment。
*
* @param kind 基本类型类别
* @param layout 输出参数,存放布局信息
* @return 成功返回 0若 kind 不支持则返回 -1
*/
static inline void
scc_abi_get_base_type_layout(const scc_abi_base_type_impl_t *impls,
scc_abi_base_type_kind_t kind,
scc_abi_type_layout_t *layout) {
if (impls[kind].kind != kind)
Panic("invalid base type kind");
*layout = impls[kind].layout;
}
typedef struct scc_abi_type_calc scc_abi_type_calc_t;
void scc_abi_compute_ast_type_layout(const scc_abi_type_calc_t *ctx, void *type,
scc_abi_type_layout_t *layout);
typedef struct scc_abi_type_calc {
void *ctx;
const scc_abi_base_type_impl_t *impls;
/// @brief 可以是系统内置的结构,比如 struct int128_t
void (*compute_type_layout)(const scc_abi_type_calc_t *ctx, void *type,
scc_abi_type_layout_t *layout);
/// @brief
void (*compute_field_layout)(const scc_abi_type_calc_t *ctx, void *type,
scc_abi_field_layout_vec_t *field_layouts);
} scc_abi_type_calc_t;
#endif /* __SCC_ABI_TYPE_H__ */

View File

@@ -1,4 +0,0 @@
#ifndef __SCC_ABI_DUMMY_H__
#define __SCC_ABI_DUMMY_H__
#endif /* __SCC_ABI_DUMMY_H__ */

View File

@@ -1,49 +0,0 @@
#ifndef __SCC_ABI_WIN_X64_PC_H__
#define __SCC_ABI_WIN_X64_PC_H__
/**
* @brief Windows x64 ABI Type
* @details
* https://learn.microsoft.com/zh-cn/cpp/build/x64-software-conventions?view=msvc-180
*/
#include "../scc_type_abi.h"
static const scc_abi_base_type_impl_t scc_abi_base_type_impls[] = {
/**
* @brief
* https://learn.microsoft.com/zh-cn/cpp/build/x64-calling-convention?view=msvc-180#varargs
* 如果通过 vararg (例如省略号参数) 传递参数,则需遵守常规寄存器参数传递约定.
* 该约定规定了将第 5个及后面的参数溢出到堆栈中.
* 被调用方负责转储带有其地址的参数.
* (仅适用于浮点值)如果被调用方希望在整数寄存器中使用浮点值,则整数寄存器和浮点数寄存器都必须包含该值.
*/
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_VA_LIST, 4, 4),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_VOID, 0, 0),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_CHAR, 1, 1),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_I_CHAR, 1, 1),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_U_CHAR, 1, 1),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_I_SHORT, 2, 2),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_U_SHORT, 2, 2),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_I_INT, 4, 4),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_U_INT, 4, 4),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_I_LONG, 4, 4),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_U_LONG, 4, 4),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_I_LONG_LONG, 8, 8),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_U_LONG_LONG, 8, 8),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_PTR, 8, 8),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_FLOAT, 4, 4),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_DOUBLE, 8, 8),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_USIZE, 8, 8),
SCC_ABI_BASE_TYPE_IMPL(SCC_ABI_TYPE_ISIZE, 8, 8),
};
static const scc_abi_type_calc_t scc_ast_abi_impl = {
.impls = scc_abi_base_type_impls,
.ctx = nullptr,
.compute_type_layout = scc_abi_compute_ast_type_layout,
.compute_field_layout = nullptr,
};
#ifdef SCC_ABI_IMPLIMENT
#endif
#endif /* __SCC_ABI_WIN_X64_PC_H__ */