feat(abi): 新增ABI类型布局描述接口和Windows x64实现
- 新增scc_abi包,包含基础类型布局描述接口 - 实现Windows x64 ABI类型布局计算功能 - 定义基本类型枚举和布局信息结构体 - 提供类型布局计算的核心接口函数 refactor(ast2ir): 使用新的ABI接口替换旧的类型转换实现 - 将旧的scc_type_abi_t替换为新的scc_abi_type_calc_t - 更新AST到IR的类型转换逻辑,使用新的ABI计算接口 - 修改上下文初始化和类型解析相关代码 - 移除废弃的头文件和相关实现 refactor(ir): 统一IR节点引用类型命名并完善构建器功能 - 将scc_ir_node_ref_vec_t重命名为scc_ir_value_ref_vec_t保持一致性 - 更新聚合类型的字段名称从elements到fields - 添加全局变量分配构建器函数scc_ir_builder_global_alloca - 清理构建器中多余的注释和代码
This commit is contained in:
9
libs/abi/cbuild.toml
Normal file
9
libs/abi/cbuild.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "scc_abi"
|
||||
version = "0.1.0"
|
||||
authors = []
|
||||
description = ""
|
||||
|
||||
# dependencies = []
|
||||
# features = {}
|
||||
# default_features = []
|
||||
105
libs/abi/include/scc_type_abi.h
Normal file
105
libs/abi/include/scc_type_abi.h
Normal file
@@ -0,0 +1,105 @@
|
||||
/**
|
||||
* @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_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__ */
|
||||
4
libs/abi/include/target/scc_abi_dummy.h
Normal file
4
libs/abi/include/target/scc_abi_dummy.h
Normal file
@@ -0,0 +1,4 @@
|
||||
#ifndef __SCC_ABI_DUMMY_H__
|
||||
#define __SCC_ABI_DUMMY_H__
|
||||
|
||||
#endif /* __SCC_ABI_DUMMY_H__ */
|
||||
40
libs/abi/include/target/scc_abi_win_x64_pc.h
Normal file
40
libs/abi/include/target/scc_abi_win_x64_pc.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#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[] = {
|
||||
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__ */
|
||||
62
libs/abi/src/scc_type_abi.c
Normal file
62
libs/abi/src/scc_type_abi.c
Normal file
@@ -0,0 +1,62 @@
|
||||
#include <scc_ast.h>
|
||||
#include <scc_ir.h>
|
||||
#include <scc_type_abi.h>
|
||||
|
||||
void scc_abi_compute_ast_type_layout(const scc_abi_type_calc_t *ctx, void *type,
|
||||
scc_abi_type_layout_t *layout) {
|
||||
scc_ast_type_t *ast_type = type;
|
||||
scc_abi_base_type_kind_t kind = SCC_ABI_TYPE_VOID;
|
||||
switch (ast_type->builtin.type) {
|
||||
case SCC_AST_BUILTIN_TYPE_VOID:
|
||||
kind = SCC_ABI_TYPE_VOID;
|
||||
case SCC_AST_BUILTIN_TYPE_CHAR:
|
||||
kind = SCC_ABI_TYPE_CHAR;
|
||||
break;
|
||||
case SCC_AST_BUILTIN_TYPE_UNSIGNED_CHAR:
|
||||
kind = SCC_ABI_TYPE_U_CHAR;
|
||||
break;
|
||||
case SCC_AST_BUILTIN_TYPE_SIGNED_CHAR:
|
||||
kind = SCC_ABI_TYPE_I_CHAR;
|
||||
break;
|
||||
case SCC_AST_BUILTIN_TYPE_SHORT:
|
||||
kind = SCC_ABI_TYPE_I_SHORT;
|
||||
break;
|
||||
case SCC_AST_BUILTIN_TYPE_SIGNED_SHORT:
|
||||
kind = SCC_ABI_TYPE_I_SHORT;
|
||||
break;
|
||||
case SCC_AST_BUILTIN_TYPE_UNSIGNED_SHORT:
|
||||
kind = SCC_ABI_TYPE_U_SHORT;
|
||||
break;
|
||||
case SCC_AST_BUILTIN_TYPE_INT:
|
||||
kind = SCC_ABI_TYPE_I_INT;
|
||||
break;
|
||||
case SCC_AST_BUILTIN_TYPE_SIGNED_INT:
|
||||
kind = SCC_ABI_TYPE_I_INT;
|
||||
break;
|
||||
case SCC_AST_BUILTIN_TYPE_UNSIGNED_INT:
|
||||
kind = SCC_ABI_TYPE_U_INT;
|
||||
break;
|
||||
case SCC_AST_BUILTIN_TYPE_LONG:
|
||||
kind = SCC_ABI_TYPE_I_LONG;
|
||||
break;
|
||||
case SCC_AST_BUILTIN_TYPE_SIGNED_LONG:
|
||||
kind = SCC_ABI_TYPE_I_LONG;
|
||||
break;
|
||||
case SCC_AST_BUILTIN_TYPE_UNSIGNED_LONG:
|
||||
kind = SCC_ABI_TYPE_U_LONG;
|
||||
break;
|
||||
case SCC_AST_BUILTIN_TYPE_LONG_LONG:
|
||||
kind = SCC_ABI_TYPE_I_LONG_LONG;
|
||||
break;
|
||||
case SCC_AST_BUILTIN_TYPE_SIGNED_LONG_LONG:
|
||||
kind = SCC_ABI_TYPE_I_LONG_LONG;
|
||||
break;
|
||||
case SCC_AST_BUILTIN_TYPE_UNSIGNED_LONG_LONG:
|
||||
kind = SCC_ABI_TYPE_U_LONG_LONG;
|
||||
break;
|
||||
default:
|
||||
Panic("Unsupported AST type: %d", ast_type->builtin.type);
|
||||
break;
|
||||
}
|
||||
scc_abi_get_base_type_layout(ctx->impls, kind, layout);
|
||||
}
|
||||
Reference in New Issue
Block a user