refactor(ast2ir): 移除废弃的ABI依赖并优化类型转换处理
移除了对scc_abi包的依赖,将相关头文件从libs/abi移动到libs/ast2ir目录下。 重构了基本类型解析功能,将parse_base_type函数提取为独立的 scc_ast2ir_parse_base_type实现,并支持有符号/无符号类型区分。 feat(ast2ir): 实现整数常量表达式求值器 新增了完整的整数常量表达式求值功能,支持C11标准中的常量表达式规则, 包括字面量、标识符、sizeof/_Alignof、一元/二元运算、条件表达式和 类型转换等操作。该功能用于数组大小和枚举值的编译期计算验证。 refactor(ast2ir): 完善类型提升和算术转换机制 改进了整数提升和寻常算术转换的实现,修复了移位操作的符号处理问题, 添加了无符号比较操作的支持,增强了类型安全检查,统一了错误处理流程。 fix(ast2ir): 修复赋值表达式返回值和数组大小计算问题 修正了赋值表达式的返回值处理,确保返回右侧值而不是存储指令引用。 使用新的常量表达式求值器替代原有的硬编码数组大小计算,提高了 数组声明的正确性。
This commit is contained in:
@@ -7,7 +7,6 @@ description = ""
|
||||
dependencies = [
|
||||
{ name = "scc_ast", path = "../ast" },
|
||||
{ name = "scc_hir", path = "../ir/hir" },
|
||||
{ name = "scc_abi", path = "../abi" },
|
||||
]
|
||||
# features = {}
|
||||
# default_features = []
|
||||
|
||||
@@ -38,6 +38,10 @@ void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, const scc_ast_stmt_t *stmt);
|
||||
scc_hir_type_ref_t scc_ast2ir_type(scc_ast2ir_ctx_t *ctx,
|
||||
const scc_ast_qual_type_t *ast_type);
|
||||
|
||||
scc_hir_type_ref_t
|
||||
scc_ast2ir_parse_base_type(scc_ast2ir_ctx_t *ctx,
|
||||
const scc_ast_qual_type_t *ast_type);
|
||||
|
||||
// ====== 类型提升(Type Promotion)接口 ======
|
||||
|
||||
/**
|
||||
@@ -47,16 +51,17 @@ scc_hir_type_ref_t scc_ast2ir_type(scc_ast2ir_ctx_t *ctx,
|
||||
* @return 提升后的类型的引用,若无需提升则返回原始类型
|
||||
*/
|
||||
scc_hir_type_ref_t scc_ast2ir_integer_promotion(scc_ast2ir_ctx_t *ctx,
|
||||
scc_hir_type_ref_t type_ref);
|
||||
scc_hir_type_ref_t type_ref);
|
||||
|
||||
/**
|
||||
* @brief 寻常算术转换(C11 6.3.1.8)
|
||||
* 对二元算术操作的两个操作数类型找到公共类型
|
||||
* @return 公共类型的引用
|
||||
*/
|
||||
scc_hir_type_ref_t scc_ast2ir_usual_arithmetic_conversion(
|
||||
scc_ast2ir_ctx_t *ctx, scc_hir_type_ref_t t1_ref,
|
||||
scc_hir_type_ref_t t2_ref);
|
||||
scc_hir_type_ref_t
|
||||
scc_ast2ir_usual_arithmetic_conversion(scc_ast2ir_ctx_t *ctx,
|
||||
scc_hir_type_ref_t t1_ref,
|
||||
scc_hir_type_ref_t t2_ref);
|
||||
|
||||
/**
|
||||
* @brief 插入类型转换指令
|
||||
@@ -64,7 +69,25 @@ scc_hir_type_ref_t scc_ast2ir_usual_arithmetic_conversion(
|
||||
* @return 转换后的值引用
|
||||
*/
|
||||
scc_hir_value_ref_t scc_ast2ir_emit_conversion(scc_ast2ir_ctx_t *ctx,
|
||||
scc_hir_value_ref_t value,
|
||||
scc_hir_type_ref_t target_type);
|
||||
scc_hir_value_ref_t value,
|
||||
scc_hir_type_ref_t target_type);
|
||||
|
||||
// ====== 常量表达式求值 ======
|
||||
|
||||
/**
|
||||
* @brief 对整数常量表达式求值(C11 6.6)
|
||||
*
|
||||
* 递归计算 AST 表达式,得到一个编译期可确定的整数值。
|
||||
* 支持:整数字面量、字符字面量、枚举常量、sizeof / _Alignof、
|
||||
* 一元运算(+ - ~ !)、二元运算(算术/位/移位/关系/逻辑)、
|
||||
* 条件表达式(?:)、强制类型转换。
|
||||
*
|
||||
* @param ctx ast2ir 上下文
|
||||
* @param result 输出参数,存放求值结果
|
||||
* @param expr 待求值的 AST 表达式
|
||||
* @return 成功返回 true;若表达式不是合法的常量表达式则返回 false
|
||||
*/
|
||||
cbool scc_ast2ir_eval_constant_int(scc_ast2ir_ctx_t *ctx, scc_ap_t *result,
|
||||
const scc_ast_expr_t *expr);
|
||||
|
||||
#endif /* __SCC_AST2IR_H__ */
|
||||
|
||||
106
libs/ast2ir/include/scc_type_abi.h
Normal file
106
libs/ast2ir/include/scc_type_abi.h
Normal file
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* @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__ */
|
||||
4
libs/ast2ir/include/target/scc_abi_dummy.h
Normal file
4
libs/ast2ir/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__ */
|
||||
49
libs/ast2ir/include/target/scc_abi_win_x64_pc.h
Normal file
49
libs/ast2ir/include/target/scc_abi_win_x64_pc.h
Normal file
@@ -0,0 +1,49 @@
|
||||
#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__ */
|
||||
@@ -24,29 +24,6 @@ static int fixed_param_count(const scc_ast_canon_type_t *canon) {
|
||||
return n;
|
||||
}
|
||||
|
||||
static scc_hir_type_ref_t parse_base_type(scc_ast2ir_ctx_t *ctx,
|
||||
const scc_ast_qual_type_t *ast_type) {
|
||||
scc_abi_type_layout_t layout;
|
||||
// 映射内置类型
|
||||
ctx->abi->compute_type_layout(ctx->abi, (void *)ast_type, &layout);
|
||||
switch (layout.size) {
|
||||
case 0:
|
||||
return scc_hir_builder_type_void(&ctx->builder);
|
||||
case 1:
|
||||
return scc_hir_builder_type_i8(&ctx->builder);
|
||||
case 2:
|
||||
return scc_hir_builder_type_i16(&ctx->builder);
|
||||
case 4:
|
||||
return scc_hir_builder_type_i32(&ctx->builder);
|
||||
case 8:
|
||||
return scc_hir_builder_type_i64(&ctx->builder);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return SCC_HIR_REF_nullptr;
|
||||
}
|
||||
|
||||
static inline bool scc_hir_type_is_signed(scc_hir_type_tag_t tag) {
|
||||
switch (tag) {
|
||||
case SCC_HIR_TYPE_i8:
|
||||
@@ -69,6 +46,7 @@ static inline bool scc_hir_type_is_signed(scc_hir_type_tag_t tag) {
|
||||
|
||||
// 判断是否为算术类型(整数或浮点)
|
||||
static cbool is_arithmetic_type(const scc_hir_type_t *type) {
|
||||
Assert(type != nullptr);
|
||||
switch (type->tag) {
|
||||
case SCC_HIR_TYPE_i8:
|
||||
case SCC_HIR_TYPE_i16:
|
||||
@@ -235,7 +213,7 @@ scc_hir_type_ref_t scc_ast2ir_type(scc_ast2ir_ctx_t *ctx,
|
||||
|
||||
switch (ast_type->base.type) {
|
||||
case SCC_AST_TYPE_BUILTIN: {
|
||||
return parse_base_type(ctx, ast_type);
|
||||
return scc_ast2ir_parse_base_type(ctx, ast_type);
|
||||
}
|
||||
case SCC_AST_TYPE_POINTER: {
|
||||
scc_hir_type_init(&ir_type, SCC_HIR_TYPE_PTR);
|
||||
@@ -253,16 +231,11 @@ scc_hir_type_ref_t scc_ast2ir_type(scc_ast2ir_ctx_t *ctx,
|
||||
ir_type.data.array.base = element_type;
|
||||
ir_type.data.array.len = 0;
|
||||
if (scc_ast_canon_type(ast_type)->array.size) {
|
||||
// TODO constant expression
|
||||
if (scc_ast_canon_type(ast_type)->array.size->base.type !=
|
||||
SCC_AST_EXPR_INT_LITERAL) {
|
||||
Panic("TODO: array size expression");
|
||||
}
|
||||
scc_ap_t value;
|
||||
scc_ap_from_cstr(
|
||||
&value, scc_ast_canon_type(ast_type)->array.size->literal.lexme,
|
||||
10);
|
||||
// FIXME hack
|
||||
if (!scc_ast2ir_eval_constant_int(
|
||||
ctx, &value, scc_ast_canon_type(ast_type)->array.size)) {
|
||||
Panic("array size is not a constant expression");
|
||||
}
|
||||
ir_type.data.array.len = value.data.digit;
|
||||
}
|
||||
} break;
|
||||
@@ -359,7 +332,7 @@ scc_hir_type_ref_t scc_ast2ir_type(scc_ast2ir_ctx_t *ctx,
|
||||
.base.type = SCC_AST_TYPE_BUILTIN,
|
||||
.type = int_canon_type,
|
||||
};
|
||||
return parse_base_type(ctx, &int_type);
|
||||
return scc_ast2ir_parse_base_type(ctx, &int_type);
|
||||
case SCC_AST_TYPE_TYPEDEF:
|
||||
// TODO maybe using cache
|
||||
return scc_ast2ir_type(
|
||||
@@ -569,7 +542,8 @@ scc_hir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx,
|
||||
rhs = scc_ast2ir_emit_conversion(
|
||||
ctx, rhs, lhs_ptr_type->data.pointer.base);
|
||||
}
|
||||
return scc_hir_builder_store(&ctx->builder, lhs, rhs);
|
||||
scc_hir_builder_store(&ctx->builder, lhs, rhs);
|
||||
return rhs;
|
||||
}
|
||||
|
||||
if (expr->binary.op == SCC_AST_OP_LOGICAL_AND ||
|
||||
@@ -577,30 +551,36 @@ scc_hir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx,
|
||||
// TODO 类型提升
|
||||
return scc_ast2ir_logical_expr(ctx, expr);
|
||||
}
|
||||
rhs = scc_ast2ir_expr(ctx, expr->binary.rhs, false);
|
||||
lhs = scc_ast2ir_expr(ctx, expr->binary.lhs, false);
|
||||
if (expr->binary.op == SCC_AST_OP_COMMA) {
|
||||
lhs = scc_ast2ir_expr(ctx, expr->binary.lhs, false);
|
||||
rhs = scc_ast2ir_expr(ctx, expr->binary.rhs, false);
|
||||
} else {
|
||||
rhs = scc_ast2ir_expr(ctx, expr->binary.rhs, false);
|
||||
lhs = scc_ast2ir_expr(ctx, expr->binary.lhs, false);
|
||||
}
|
||||
|
||||
// 类型提升:对算术类型二元操作应用寻常算术转换(C11 6.3.1.8)
|
||||
// 移位操作符不适用(仅对每个操作数单独整数提升)
|
||||
cbool is_shift = (expr->binary.op == SCC_AST_OP_LEFT_SHIFT ||
|
||||
expr->binary.op == SCC_AST_OP_RIGHT_SHIFT);
|
||||
scc_hir_type_ref_t common_type = 0;
|
||||
if (!is_shift && expr->binary.op != SCC_AST_OP_COMMA) {
|
||||
scc_hir_module_t *module = scc_ast2ir_mir_module(ctx);
|
||||
scc_hir_type_ref_t lhs_type_ref =
|
||||
scc_hir_module_get_value(module, lhs)->type;
|
||||
scc_hir_type_ref_t rhs_type_ref =
|
||||
scc_hir_module_get_value(module, rhs)->type;
|
||||
Assert(lhs_type_ref != 0 && rhs_type_ref != 0);
|
||||
scc_hir_type_t *lhs_type =
|
||||
scc_hir_module_get_type(module, lhs_type_ref);
|
||||
scc_hir_type_t *rhs_type =
|
||||
scc_hir_module_get_type(module, rhs_type_ref);
|
||||
if (is_arithmetic_type(lhs_type) && is_arithmetic_type(rhs_type)) {
|
||||
scc_hir_type_ref_t common =
|
||||
scc_ast2ir_usual_arithmetic_conversion(ctx, lhs_type_ref,
|
||||
rhs_type_ref);
|
||||
if (common != 0) {
|
||||
lhs = scc_ast2ir_emit_conversion(ctx, lhs, common);
|
||||
rhs = scc_ast2ir_emit_conversion(ctx, rhs, common);
|
||||
common_type = scc_ast2ir_usual_arithmetic_conversion(
|
||||
ctx, lhs_type_ref, rhs_type_ref);
|
||||
if (common_type != 0) {
|
||||
lhs = scc_ast2ir_emit_conversion(ctx, lhs, common_type);
|
||||
rhs = scc_ast2ir_emit_conversion(ctx, rhs, common_type);
|
||||
}
|
||||
}
|
||||
} else if (is_shift) {
|
||||
@@ -628,16 +608,42 @@ scc_hir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx,
|
||||
case SCC_AST_OP_BITWISE_XOR: op = SCC_HIR_OP_XOR; break;
|
||||
case SCC_AST_OP_LEFT_SHIFT: op = SCC_HIR_OP_SHL; break;
|
||||
case SCC_AST_OP_RIGHT_SHIFT: {
|
||||
op = SCC_HIR_OP_SHR;
|
||||
// FIXME op = SCC_HIR_OP_SAR;
|
||||
scc_hir_module_t *module = scc_ast2ir_mir_module(ctx);
|
||||
scc_hir_type_ref_t type_ref = scc_hir_module_get_value(module, lhs)->type;
|
||||
scc_hir_type_t *type = scc_hir_module_get_type(module, type_ref);
|
||||
op = scc_hir_type_is_signed(type->tag) ? SCC_HIR_OP_SAR : SCC_HIR_OP_SHR;
|
||||
break;
|
||||
}
|
||||
case SCC_AST_OP_EQUAL: op = SCC_HIR_OP_EQ; break;
|
||||
case SCC_AST_OP_NOT_EQUAL: op = SCC_HIR_OP_NEQ; break;
|
||||
case SCC_AST_OP_LESS: op = SCC_HIR_OP_LT; break;
|
||||
case SCC_AST_OP_LESS_EQUAL: op = SCC_HIR_OP_LE; break;
|
||||
case SCC_AST_OP_GREATER: op = SCC_HIR_OP_GT; break;
|
||||
case SCC_AST_OP_GREATER_EQUAL: op = SCC_HIR_OP_GE; break;
|
||||
case SCC_AST_OP_LESS: {
|
||||
scc_hir_module_t *module = scc_ast2ir_mir_module(ctx);
|
||||
scc_hir_type_ref_t tr = common_type ? common_type : scc_hir_module_get_value(module, lhs)->type;
|
||||
scc_hir_type_t *t = scc_hir_module_get_type(module, tr);
|
||||
op = scc_hir_type_is_signed(t->tag) ? SCC_HIR_OP_LT : SCC_HIR_OP_ULT;
|
||||
break;
|
||||
}
|
||||
case SCC_AST_OP_LESS_EQUAL: {
|
||||
scc_hir_module_t *module = scc_ast2ir_mir_module(ctx);
|
||||
scc_hir_type_ref_t tr = common_type ? common_type : scc_hir_module_get_value(module, lhs)->type;
|
||||
scc_hir_type_t *t = scc_hir_module_get_type(module, tr);
|
||||
op = scc_hir_type_is_signed(t->tag) ? SCC_HIR_OP_LE : SCC_HIR_OP_ULE;
|
||||
break;
|
||||
}
|
||||
case SCC_AST_OP_GREATER: {
|
||||
scc_hir_module_t *module = scc_ast2ir_mir_module(ctx);
|
||||
scc_hir_type_ref_t tr = common_type ? common_type : scc_hir_module_get_value(module, lhs)->type;
|
||||
scc_hir_type_t *t = scc_hir_module_get_type(module, tr);
|
||||
op = scc_hir_type_is_signed(t->tag) ? SCC_HIR_OP_GT : SCC_HIR_OP_UGT;
|
||||
break;
|
||||
}
|
||||
case SCC_AST_OP_GREATER_EQUAL: {
|
||||
scc_hir_module_t *module = scc_ast2ir_mir_module(ctx);
|
||||
scc_hir_type_ref_t tr = common_type ? common_type : scc_hir_module_get_value(module, lhs)->type;
|
||||
scc_hir_type_t *t = scc_hir_module_get_type(module, tr);
|
||||
op = scc_hir_type_is_signed(t->tag) ? SCC_HIR_OP_GE : SCC_HIR_OP_UGE;
|
||||
break;
|
||||
}
|
||||
case SCC_AST_OP_COMMA: {
|
||||
// 逗号运算符:计算左表达式,丢弃结果,返回右表达式
|
||||
return rhs;
|
||||
@@ -854,28 +860,24 @@ scc_hir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx,
|
||||
scc_hir_value_ref_vec_t args;
|
||||
scc_vec_init(args);
|
||||
|
||||
// 获取函数类型信息用于参数转换。注意:不能长期持有指向 types 向量
|
||||
// 内部的指针(如 ft->data.function.params),因为递归的
|
||||
// scc_ast2ir_expr 可能触发 scc_hir_module_add_type 导致 realloc。
|
||||
// 改为通过 func_type_ref(稳定的索引)每次需要时重新获取指针。
|
||||
// 获取函数类型信息用于参数转换。
|
||||
scc_hir_func_ref_t callee_func =
|
||||
(scc_hir_func_ref_t)(usize)scc_hashtable_get(
|
||||
&ctx->symtab, expr->call.callee->identifier._target->name);
|
||||
scc_hir_type_ref_t func_type_ref = SCC_HIR_REF_nullptr;
|
||||
cbool is_variadic = false;
|
||||
int fixed_count = 0;
|
||||
if (callee_func != SCC_HIR_REF_nullptr) {
|
||||
scc_hir_func_t *hir_func = scc_hir_module_get_func(
|
||||
scc_ast2ir_mir_module(ctx), callee_func);
|
||||
scc_hir_func_meta_t *meta = SCC_HIR_FUNC_META(hir_func);
|
||||
is_variadic = meta->is_variadic;
|
||||
func_type_ref = meta->type;
|
||||
if (func_type_ref != SCC_HIR_REF_nullptr) {
|
||||
const scc_hir_type_t *ft = scc_hir_module_get_type(
|
||||
scc_ast2ir_mir_module(ctx), func_type_ref);
|
||||
if (ft != nullptr && ft->tag == SCC_HIR_TYPE_FUNC)
|
||||
fixed_count = (int)ft->data.function.params.size;
|
||||
}
|
||||
Assert(callee_func != SCC_HIR_REF_nullptr);
|
||||
scc_hir_func_t *hir_func =
|
||||
scc_hir_module_get_func(scc_ast2ir_mir_module(ctx), callee_func);
|
||||
scc_hir_func_meta_t *meta = SCC_HIR_FUNC_META(hir_func);
|
||||
is_variadic = meta->is_variadic;
|
||||
func_type_ref = meta->type;
|
||||
if (func_type_ref != SCC_HIR_REF_nullptr) {
|
||||
const scc_hir_type_t *ft = scc_hir_module_get_type(
|
||||
scc_ast2ir_mir_module(ctx), func_type_ref);
|
||||
if (ft != nullptr && ft->tag == SCC_HIR_TYPE_FUNC)
|
||||
fixed_count = (int)ft->data.function.params.size;
|
||||
}
|
||||
|
||||
scc_vec_foreach(expr->call.args, i) {
|
||||
@@ -897,8 +899,11 @@ scc_hir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx,
|
||||
// 可变参数:只应用默认参数提升(C11 6.5.2.2)
|
||||
// 整数提升:char/short → int
|
||||
scc_hir_module_t *module = scc_ast2ir_mir_module(ctx);
|
||||
scc_hir_type_ref_t arg_type_ref =
|
||||
scc_hir_module_get_value(module, arg_node)->type;
|
||||
|
||||
scc_hir_value_t *arg_value =
|
||||
scc_hir_module_get_value(module, arg_node);
|
||||
Assert(arg_value != nullptr);
|
||||
scc_hir_type_ref_t arg_type_ref = arg_value->type;
|
||||
Assert(arg_type_ref != 0);
|
||||
scc_hir_type_tag_t arg_tag =
|
||||
scc_hir_module_get_type(module, arg_type_ref)->tag;
|
||||
@@ -1136,7 +1141,7 @@ scc_hir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx,
|
||||
// FIXME maybe using some array to int;
|
||||
scc_hir_type_ref_t type_ref = scc_hir_builder_type_i32(&ctx->builder);
|
||||
scc_ap_t value;
|
||||
scc_ap_from_cstr(&value, expr->literal.lexme, 10);
|
||||
scc_ap_from_cstr(&value, expr->literal.lexme, 0);
|
||||
return scc_hir_builder_integer(&ctx->builder, type_ref, &value);
|
||||
}
|
||||
// SCC_AST_EXPR_FLOAT_LITERAL, // 浮点字面量
|
||||
@@ -1655,24 +1660,23 @@ void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, const scc_ast_decl_t *decl,
|
||||
} break;
|
||||
case SCC_AST_DECL_ENUM: {
|
||||
scc_ap_t val;
|
||||
int idx = 0;
|
||||
scc_ap_set_int(&val, idx);
|
||||
scc_ap_set_int(&val, 0);
|
||||
scc_vec_foreach(decl->record.fields, i) {
|
||||
scc_ast_decl_t *item = scc_vec_at(decl->record.fields, i);
|
||||
Assert(item->base.type == SCC_AST_DECL_VAR);
|
||||
if (item->var.init) {
|
||||
Assert(item->var.init->base.type == SCC_AST_EXPR_INT_LITERAL);
|
||||
scc_ap_from_cstr(&val, item->var.init->literal.lexme, 10);
|
||||
if (!scc_ast2ir_eval_constant_int(ctx, &val, item->var.init)) {
|
||||
Panic("enum value is not a constant expression");
|
||||
}
|
||||
}
|
||||
scc_hir_value_ref_t item_val_ref = scc_hir_builder_integer(
|
||||
&ctx->builder, scc_hir_builder_type_i32(&ctx->builder), &val);
|
||||
scc_hashtable_set(&ctx->ast2ir_cache, item,
|
||||
(void *)(usize)item_val_ref);
|
||||
// FIXME hack ap
|
||||
idx = val.data.digit;
|
||||
idx += 1;
|
||||
// scc_ap_add 1 ();
|
||||
scc_ap_set_int(&val, idx);
|
||||
/* next = val + 1 */
|
||||
scc_ap_t one;
|
||||
scc_ap_set_int(&one, 1);
|
||||
scc_ap_add(&val, &val, &one);
|
||||
}
|
||||
} break;
|
||||
case SCC_AST_DECL_TYPEDEF:
|
||||
|
||||
381
libs/ast2ir/src/scc_ast2ir_const.c
Normal file
381
libs/ast2ir/src/scc_ast2ir_const.c
Normal file
@@ -0,0 +1,381 @@
|
||||
#include <scc_ast2ir.h>
|
||||
#include <scc_ast_def.h>
|
||||
#include <scc_ast_utils.h>
|
||||
|
||||
/* ---------- internal helpers ---------- */
|
||||
|
||||
static cbool is_builtin_signed(scc_ast_builtin_type_t t) {
|
||||
switch (t) {
|
||||
case SCC_AST_BUILTIN_TYPE_BOOL:
|
||||
case SCC_AST_BUILTIN_TYPE_CHAR: /* implementation-defined, treat as signed */
|
||||
case SCC_AST_BUILTIN_TYPE_SHORT:
|
||||
case SCC_AST_BUILTIN_TYPE_INT:
|
||||
case SCC_AST_BUILTIN_TYPE_LONG:
|
||||
case SCC_AST_BUILTIN_TYPE_LONG_LONG:
|
||||
case SCC_AST_BUILTIN_TYPE_SIGNED_CHAR:
|
||||
case SCC_AST_BUILTIN_TYPE_SIGNED_SHORT:
|
||||
case SCC_AST_BUILTIN_TYPE_SIGNED_INT:
|
||||
case SCC_AST_BUILTIN_TYPE_SIGNED_LONG:
|
||||
case SCC_AST_BUILTIN_TYPE_SIGNED_LONG_LONG:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static cbool is_builtin_unsigned(scc_ast_builtin_type_t t) {
|
||||
switch (t) {
|
||||
case SCC_AST_BUILTIN_TYPE_UNSIGNED_CHAR:
|
||||
case SCC_AST_BUILTIN_TYPE_UNSIGNED_SHORT:
|
||||
case SCC_AST_BUILTIN_TYPE_UNSIGNED_INT:
|
||||
case SCC_AST_BUILTIN_TYPE_UNSIGNED_LONG:
|
||||
case SCC_AST_BUILTIN_TYPE_UNSIGNED_LONG_LONG:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/* Check whether an AST qual-type is an integer type (not pointer/float/struct) */
|
||||
static cbool is_ast_integer_type(const scc_ast_qual_type_t *type) {
|
||||
if (type->base.type != SCC_AST_TYPE_BUILTIN)
|
||||
return false;
|
||||
scc_ast_builtin_type_t bt = scc_ast_canon_type(type)->builtin.type;
|
||||
return bt >= SCC_AST_BUILTIN_TYPE_BOOL &&
|
||||
bt <= SCC_AST_BUILTIN_TYPE_UNSIGNED_LONG_LONG;
|
||||
}
|
||||
|
||||
/*
|
||||
* Truncate / sign-extend an AP value to match a target integer type width.
|
||||
* Uses the ABI calculator to determine the type's size in bytes.
|
||||
*/
|
||||
static cbool apply_integer_truncation(scc_ast2ir_ctx_t *ctx, scc_ap_t *val,
|
||||
const scc_ast_qual_type_t *target_type) {
|
||||
if (!is_ast_integer_type(target_type))
|
||||
return false;
|
||||
|
||||
scc_abi_type_layout_t layout;
|
||||
ctx->abi->compute_type_layout(ctx->abi, (void *)target_type, &layout);
|
||||
int bits = layout.size * 8;
|
||||
if (bits >= (int)sizeof(scc_ap_digit) * 8)
|
||||
return true; /* no truncation needed */
|
||||
|
||||
/* Extract magnitude */
|
||||
uint64_t mag = val->data.digit;
|
||||
uint64_t mask = ((uint64_t)1 << bits) - 1;
|
||||
uint64_t truncated = mag & mask;
|
||||
|
||||
/* For signed types, sign-extend if the MSB is set */
|
||||
scc_ast_builtin_type_t bt = scc_ast_canon_type(target_type)->builtin.type;
|
||||
if (is_builtin_signed(bt) && (truncated >> (bits - 1)))
|
||||
truncated |= ~mask; /* set all bits above bits-1 */
|
||||
|
||||
val->data.digit = truncated;
|
||||
val->len = (val->len < 0 && truncated == 0) ? 1 : val->len;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ---------- unary expression ---------- */
|
||||
|
||||
static cbool eval_unary(scc_ast2ir_ctx_t *ctx, scc_ap_t *result,
|
||||
const scc_ast_expr_t *expr) {
|
||||
scc_ap_t val;
|
||||
scc_ap_init(&val);
|
||||
|
||||
if (!scc_ast2ir_eval_constant_int(ctx, &val, expr->unary.operand))
|
||||
return false;
|
||||
|
||||
switch (expr->unary.op) {
|
||||
case SCC_AST_OP_UNARY_PLUS:
|
||||
result->capacity = -1;
|
||||
result->len = val.len;
|
||||
result->data.digit = val.data.digit;
|
||||
return true;
|
||||
case SCC_AST_OP_UNARY_MINUS:
|
||||
scc_ap_neg(result, &val);
|
||||
return true;
|
||||
case SCC_AST_OP_BITWISE_NOT:
|
||||
scc_ap_not(result, &val);
|
||||
return true;
|
||||
case SCC_AST_OP_LOGICAL_NOT:
|
||||
scc_ap_set_int(result, scc_ap_is_zero(&val));
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------- binary expression ---------- */
|
||||
|
||||
static cbool eval_binary(scc_ast2ir_ctx_t *ctx, scc_ap_t *result,
|
||||
const scc_ast_expr_t *expr) {
|
||||
scc_ap_t lhs_val, rhs_val;
|
||||
scc_ap_init(&lhs_val);
|
||||
scc_ap_init(&rhs_val);
|
||||
|
||||
if (!scc_ast2ir_eval_constant_int(ctx, &lhs_val, expr->binary.lhs))
|
||||
return false;
|
||||
if (!scc_ast2ir_eval_constant_int(ctx, &rhs_val, expr->binary.rhs))
|
||||
return false;
|
||||
|
||||
switch (expr->binary.op) {
|
||||
/* arithmetic */
|
||||
case SCC_AST_OP_ADD:
|
||||
scc_ap_add(result, &lhs_val, &rhs_val);
|
||||
return true;
|
||||
case SCC_AST_OP_SUB:
|
||||
scc_ap_sub(result, &lhs_val, &rhs_val);
|
||||
return true;
|
||||
case SCC_AST_OP_MUL:
|
||||
scc_ap_mul(result, &lhs_val, &rhs_val);
|
||||
return true;
|
||||
case SCC_AST_OP_DIV:
|
||||
scc_ap_div(result, &lhs_val, &rhs_val);
|
||||
return true;
|
||||
case SCC_AST_OP_MOD:
|
||||
scc_ap_mod(result, &lhs_val, &rhs_val);
|
||||
return true;
|
||||
/* bitwise */
|
||||
case SCC_AST_OP_BITWISE_AND:
|
||||
scc_ap_and(result, &lhs_val, &rhs_val);
|
||||
return true;
|
||||
case SCC_AST_OP_BITWISE_OR:
|
||||
scc_ap_or(result, &lhs_val, &rhs_val);
|
||||
return true;
|
||||
case SCC_AST_OP_BITWISE_XOR:
|
||||
scc_ap_xor(result, &lhs_val, &rhs_val);
|
||||
return true;
|
||||
/* shift */
|
||||
case SCC_AST_OP_LEFT_SHIFT:
|
||||
scc_ap_shl(result, &lhs_val, (unsigned)rhs_val.data.digit);
|
||||
return true;
|
||||
case SCC_AST_OP_RIGHT_SHIFT:
|
||||
scc_ap_shr(result, &lhs_val, (unsigned)rhs_val.data.digit);
|
||||
return true;
|
||||
/* relational */
|
||||
case SCC_AST_OP_EQUAL:
|
||||
scc_ap_set_int(result, scc_ap_cmp(&lhs_val, &rhs_val) == 0);
|
||||
return true;
|
||||
case SCC_AST_OP_NOT_EQUAL:
|
||||
scc_ap_set_int(result, scc_ap_cmp(&lhs_val, &rhs_val) != 0);
|
||||
return true;
|
||||
case SCC_AST_OP_LESS:
|
||||
scc_ap_set_int(result, scc_ap_cmp(&lhs_val, &rhs_val) < 0);
|
||||
return true;
|
||||
case SCC_AST_OP_GREATER:
|
||||
scc_ap_set_int(result, scc_ap_cmp(&lhs_val, &rhs_val) > 0);
|
||||
return true;
|
||||
case SCC_AST_OP_LESS_EQUAL:
|
||||
scc_ap_set_int(result, scc_ap_cmp(&lhs_val, &rhs_val) <= 0);
|
||||
return true;
|
||||
case SCC_AST_OP_GREATER_EQUAL:
|
||||
scc_ap_set_int(result, scc_ap_cmp(&lhs_val, &rhs_val) >= 0);
|
||||
return true;
|
||||
/* logical */
|
||||
case SCC_AST_OP_LOGICAL_AND:
|
||||
if (scc_ap_is_zero(&lhs_val)) {
|
||||
scc_ap_set_int(result, 0);
|
||||
} else {
|
||||
scc_ap_set_int(result, !scc_ap_is_zero(&rhs_val));
|
||||
}
|
||||
return true;
|
||||
case SCC_AST_OP_LOGICAL_OR:
|
||||
if (!scc_ap_is_zero(&lhs_val)) {
|
||||
scc_ap_set_int(result, 1);
|
||||
} else {
|
||||
scc_ap_set_int(result, !scc_ap_is_zero(&rhs_val));
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------- identifier (enum constant lookup) ---------- */
|
||||
|
||||
static cbool eval_identifier(scc_ast2ir_ctx_t *ctx, scc_ap_t *result,
|
||||
const scc_ast_expr_t *expr) {
|
||||
if (expr->identifier._target == nullptr)
|
||||
return false;
|
||||
|
||||
/* Try the ast2ir_cache — enum values are stored there as HIR integer
|
||||
* constants during scc_ast2ir_decl(SCC_AST_DECL_ENUM) */
|
||||
scc_hir_value_ref_t cached = (scc_hir_value_ref_t)(usize)scc_hashtable_get(
|
||||
&ctx->ast2ir_cache, expr->identifier._target);
|
||||
if (cached != 0) {
|
||||
scc_hir_value_t *hir_val =
|
||||
scc_hir_module_get_value(scc_ast2ir_mir_module(ctx), cached);
|
||||
if (hir_val != nullptr && hir_val->tag == SCC_HIR_VALUE_TAG_INTEGER) {
|
||||
/* shallow copy — digit mode has no heap allocation */
|
||||
result->capacity = -1;
|
||||
result->len = hir_val->data.integer.len;
|
||||
result->data.digit = hir_val->data.integer.data.digit;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/* If not cached yet, evaluate the var init recursively.
|
||||
* This handles enum-internal forward references within a single enum decl:
|
||||
* enum { A = 1, B = A + 1 }; — when evaluating B, A may not be cached
|
||||
* yet, but we can look at A's init expression. */
|
||||
if (expr->identifier._target->base.type == SCC_AST_DECL_VAR &&
|
||||
expr->identifier._target->var.init != nullptr) {
|
||||
return scc_ast2ir_eval_constant_int(ctx, result,
|
||||
expr->identifier._target->var.init);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/* ---------- character literal evaluation ---------- */
|
||||
|
||||
static int eval_char_literal(const scc_ast_expr_t *expr) {
|
||||
const char *lexme = expr->literal.lexme;
|
||||
Assert(lexme[0] == '\'');
|
||||
|
||||
if (lexme[1] != '\\')
|
||||
return (unsigned char)lexme[1];
|
||||
|
||||
switch (lexme[2]) {
|
||||
case 'a':
|
||||
return '\a';
|
||||
case 'b':
|
||||
return '\b';
|
||||
case 'f':
|
||||
return '\f';
|
||||
case 'n':
|
||||
return '\n';
|
||||
case 'r':
|
||||
return '\r';
|
||||
case 't':
|
||||
return '\t';
|
||||
case 'v':
|
||||
return '\v';
|
||||
case '\\':
|
||||
return '\\';
|
||||
case '\'':
|
||||
return '\'';
|
||||
case '"':
|
||||
return '"';
|
||||
case '0':
|
||||
return '\0';
|
||||
case 'x':
|
||||
/* hex escape: \xNN — parse up to 2 hex digits */
|
||||
{
|
||||
int val = 0;
|
||||
int j = 3;
|
||||
while (1) {
|
||||
char c = lexme[j];
|
||||
unsigned d;
|
||||
if (c >= '0' && c <= '9')
|
||||
d = c - '0';
|
||||
else if (c >= 'a' && c <= 'f')
|
||||
d = c - 'a' + 10;
|
||||
else if (c >= 'A' && c <= 'F')
|
||||
d = c - 'A' + 10;
|
||||
else
|
||||
break;
|
||||
val = val * 16 + d;
|
||||
j++;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
default:
|
||||
/* octal: \nnn */
|
||||
if (lexme[2] >= '0' && lexme[2] <= '7') {
|
||||
int val = 0;
|
||||
int j = 2;
|
||||
while (j < 5 && lexme[j] >= '0' && lexme[j] <= '7') {
|
||||
val = val * 8 + (lexme[j] - '0');
|
||||
j++;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
return (unsigned char)lexme[2];
|
||||
}
|
||||
}
|
||||
|
||||
/* ===================================================================
|
||||
* Public API
|
||||
* =================================================================== */
|
||||
|
||||
cbool scc_ast2ir_eval_constant_int(scc_ast2ir_ctx_t *ctx, scc_ap_t *result,
|
||||
const scc_ast_expr_t *expr) {
|
||||
if (ctx == nullptr || result == nullptr || expr == nullptr)
|
||||
return false;
|
||||
|
||||
scc_ap_init(result);
|
||||
|
||||
switch (expr->base.type) {
|
||||
case SCC_AST_EXPR_INT_LITERAL:
|
||||
scc_ap_from_cstr(result, expr->literal.lexme, 0);
|
||||
return true;
|
||||
|
||||
case SCC_AST_EXPR_CHAR_LITERAL:
|
||||
scc_ap_set_int(result, eval_char_literal(expr));
|
||||
return true;
|
||||
|
||||
case SCC_AST_EXPR_BINARY:
|
||||
return eval_binary(ctx, result, expr);
|
||||
|
||||
case SCC_AST_EXPR_UNARY:
|
||||
return eval_unary(ctx, result, expr);
|
||||
|
||||
case SCC_AST_EXPR_COND: {
|
||||
scc_ap_t cond_val;
|
||||
scc_ap_init(&cond_val);
|
||||
if (!scc_ast2ir_eval_constant_int(ctx, &cond_val, expr->cond.cond))
|
||||
return false;
|
||||
return scc_ast2ir_eval_constant_int(ctx, result,
|
||||
scc_ap_is_zero(&cond_val)
|
||||
? expr->cond.else_expr
|
||||
: expr->cond.then_expr);
|
||||
}
|
||||
|
||||
case SCC_AST_EXPR_CAST: {
|
||||
if (!scc_ast2ir_eval_constant_int(ctx, result, expr->cast.expr))
|
||||
return false;
|
||||
/* Truncate / sign-extend to the target type width */
|
||||
if (!apply_integer_truncation(ctx, result, expr->cast.type))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
case SCC_AST_EXPR_SIZE_OF: {
|
||||
scc_abi_type_layout_t layout;
|
||||
if (expr->attr_of.type) {
|
||||
ctx->abi->compute_type_layout(ctx->abi, (void *)expr->attr_of.type,
|
||||
&layout);
|
||||
} else if (expr->attr_of.expr) {
|
||||
/* sizeof expression: try to get the type through the ABI */
|
||||
/* For constant expressions, sema should have annotated the type */
|
||||
/* TODO: handle sizeof(expr) where expr is not a type */
|
||||
return false;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
scc_ap_set_int(result, layout.size);
|
||||
return true;
|
||||
}
|
||||
|
||||
case SCC_AST_EXPR_ALIGN_OF: {
|
||||
scc_abi_type_layout_t layout;
|
||||
if (expr->attr_of.type) {
|
||||
ctx->abi->compute_type_layout(ctx->abi, (void *)expr->attr_of.type,
|
||||
&layout);
|
||||
} else if (expr->attr_of.expr) {
|
||||
return false;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
scc_ap_set_int(result, layout.alignment);
|
||||
return true;
|
||||
}
|
||||
|
||||
case SCC_AST_EXPR_IDENTIFIER:
|
||||
return eval_identifier(ctx, result, expr);
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -179,9 +179,10 @@ scc_hir_type_ref_t scc_ast2ir_integer_promotion(scc_ast2ir_ctx_t *ctx,
|
||||
* d. 如果有符号类型可以表示无符号类型的所有值,无符号转换为有符号
|
||||
* e. 否则,两个都转换为有符号类型的无符号版本
|
||||
*/
|
||||
scc_hir_type_ref_t scc_ast2ir_usual_arithmetic_conversion(
|
||||
scc_ast2ir_ctx_t *ctx, scc_hir_type_ref_t t1_ref,
|
||||
scc_hir_type_ref_t t2_ref) {
|
||||
scc_hir_type_ref_t
|
||||
scc_ast2ir_usual_arithmetic_conversion(scc_ast2ir_ctx_t *ctx,
|
||||
scc_hir_type_ref_t t1_ref,
|
||||
scc_hir_type_ref_t t2_ref) {
|
||||
scc_hir_module_t *module = scc_ast2ir_mir_module(ctx);
|
||||
scc_hir_type_t *t1 = scc_hir_module_get_type(module, t1_ref);
|
||||
scc_hir_type_t *t2 = scc_hir_module_get_type(module, t2_ref);
|
||||
@@ -200,13 +201,9 @@ scc_hir_type_ref_t scc_ast2ir_usual_arithmetic_conversion(
|
||||
scc_hir_type_ref_t p2_ref = scc_ast2ir_integer_promotion(ctx, t2_ref);
|
||||
|
||||
scc_hir_type_tag_t tag1 =
|
||||
(p1_ref != 0)
|
||||
? scc_hir_module_get_type(module, p1_ref)->tag
|
||||
: t1->tag;
|
||||
(p1_ref != 0) ? scc_hir_module_get_type(module, p1_ref)->tag : t1->tag;
|
||||
scc_hir_type_tag_t tag2 =
|
||||
(p2_ref != 0)
|
||||
? scc_hir_module_get_type(module, p2_ref)->tag
|
||||
: t2->tag;
|
||||
(p2_ref != 0) ? scc_hir_module_get_type(module, p2_ref)->tag : t2->tag;
|
||||
|
||||
// 如果提升后相同,直接返回
|
||||
if (tag1 == tag2)
|
||||
@@ -252,6 +249,7 @@ scc_hir_type_ref_t scc_ast2ir_usual_arithmetic_conversion(
|
||||
* - 返回 -1 表示无需转换(同类型同大小)
|
||||
*/
|
||||
static int determine_conv_kind(scc_hir_type_tag_t src, scc_hir_type_tag_t dst) {
|
||||
Assert(SCC_HIR_TYPE_VALID(src) && SCC_HIR_TYPE_VALID(dst));
|
||||
if (src == dst)
|
||||
return -1;
|
||||
|
||||
@@ -302,10 +300,8 @@ scc_hir_value_ref_t scc_ast2ir_emit_conversion(scc_ast2ir_ctx_t *ctx,
|
||||
scc_hir_value_ref_t value,
|
||||
scc_hir_type_ref_t target_type) {
|
||||
scc_hir_module_t *module = scc_ast2ir_mir_module(ctx);
|
||||
scc_hir_type_t *src_type =
|
||||
scc_hir_module_get_type_by_value(module, value);
|
||||
scc_hir_type_t *dst_type =
|
||||
scc_hir_module_get_type(module, target_type);
|
||||
scc_hir_type_t *src_type = scc_hir_module_get_type_by_value(module, value);
|
||||
scc_hir_type_t *dst_type = scc_hir_module_get_type(module, target_type);
|
||||
|
||||
if (src_type == nullptr || dst_type == nullptr)
|
||||
return value;
|
||||
|
||||
76
libs/ast2ir/src/scc_ast2ir_type.c
Normal file
76
libs/ast2ir/src/scc_ast2ir_type.c
Normal file
@@ -0,0 +1,76 @@
|
||||
#include <scc_ast2ir.h>
|
||||
|
||||
scc_hir_type_ref_t
|
||||
scc_ast2ir_parse_base_type(scc_ast2ir_ctx_t *ctx,
|
||||
const scc_ast_qual_type_t *ast_type) {
|
||||
scc_abi_type_layout_t layout;
|
||||
// 映射内置类型
|
||||
ctx->abi->compute_type_layout(ctx->abi, (void *)ast_type, &layout);
|
||||
switch (scc_ast_canon_type(ast_type)->builtin.type) {
|
||||
// FIXME it not a void
|
||||
case SCC_AST_BUILTIN_TYPE_VA_LIST:
|
||||
case SCC_AST_BUILTIN_TYPE_VOID:
|
||||
return scc_hir_builder_type_void(&ctx->builder);
|
||||
case SCC_AST_BUILTIN_TYPE_BOOL:
|
||||
case SCC_AST_BUILTIN_TYPE_SIGNED_CHAR:
|
||||
case SCC_AST_BUILTIN_TYPE_SHORT:
|
||||
case SCC_AST_BUILTIN_TYPE_SIGNED_SHORT:
|
||||
case SCC_AST_BUILTIN_TYPE_INT:
|
||||
case SCC_AST_BUILTIN_TYPE_SIGNED_INT:
|
||||
case SCC_AST_BUILTIN_TYPE_LONG:
|
||||
case SCC_AST_BUILTIN_TYPE_SIGNED_LONG:
|
||||
case SCC_AST_BUILTIN_TYPE_LONG_LONG:
|
||||
case SCC_AST_BUILTIN_TYPE_SIGNED_LONG_LONG:
|
||||
switch (layout.size) {
|
||||
case 1:
|
||||
return scc_hir_builder_type_i8(&ctx->builder);
|
||||
case 2:
|
||||
return scc_hir_builder_type_i16(&ctx->builder);
|
||||
case 4:
|
||||
return scc_hir_builder_type_i32(&ctx->builder);
|
||||
case 8:
|
||||
return scc_hir_builder_type_i64(&ctx->builder);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case SCC_AST_BUILTIN_TYPE_CHAR:
|
||||
// TODO CHAR TO BE UNSIGNED CHAR
|
||||
case SCC_AST_BUILTIN_TYPE_UNSIGNED_CHAR:
|
||||
case SCC_AST_BUILTIN_TYPE_UNSIGNED_SHORT:
|
||||
case SCC_AST_BUILTIN_TYPE_UNSIGNED_INT:
|
||||
case SCC_AST_BUILTIN_TYPE_UNSIGNED_LONG:
|
||||
case SCC_AST_BUILTIN_TYPE_UNSIGNED_LONG_LONG:
|
||||
switch (layout.size) {
|
||||
case 1:
|
||||
return scc_hir_builder_type_u8(&ctx->builder);
|
||||
case 2:
|
||||
return scc_hir_builder_type_u16(&ctx->builder);
|
||||
case 4:
|
||||
return scc_hir_builder_type_u32(&ctx->builder);
|
||||
case 8:
|
||||
return scc_hir_builder_type_u64(&ctx->builder);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case SCC_AST_BUILTIN_TYPE_FLOAT:
|
||||
case SCC_AST_BUILTIN_TYPE_DOUBLE:
|
||||
switch (layout.size) {
|
||||
case 4:
|
||||
return scc_hir_builder_type_f32(&ctx->builder);
|
||||
case 8:
|
||||
return scc_hir_builder_type_f64(&ctx->builder);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
default:
|
||||
Panic("Unsupported AST type: %d",
|
||||
scc_ast_canon_type(ast_type)->builtin.type);
|
||||
break;
|
||||
}
|
||||
return SCC_HIR_REF_nullptr;
|
||||
}
|
||||
76
libs/ast2ir/src/scc_type_abi.c
Normal file
76
libs/ast2ir/src/scc_type_abi.c
Normal file
@@ -0,0 +1,76 @@
|
||||
#include <scc_ast.h>
|
||||
#include <scc_hir.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_qual_type_t *ast_type = type;
|
||||
scc_abi_base_type_kind_t kind = SCC_ABI_TYPE_VOID;
|
||||
switch (scc_ast_canon_type(ast_type)->builtin.type) {
|
||||
case SCC_AST_BUILTIN_TYPE_VA_LIST:
|
||||
kind = SCC_ABI_TYPE_VA_LIST;
|
||||
break;
|
||||
case SCC_AST_BUILTIN_TYPE_BOOL:
|
||||
kind = SCC_ABI_TYPE_I_INT;
|
||||
break;
|
||||
case SCC_AST_BUILTIN_TYPE_VOID:
|
||||
kind = SCC_ABI_TYPE_VOID;
|
||||
break;
|
||||
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;
|
||||
case SCC_AST_BUILTIN_TYPE_FLOAT:
|
||||
kind = SCC_ABI_TYPE_FLOAT;
|
||||
break;
|
||||
case SCC_AST_BUILTIN_TYPE_DOUBLE:
|
||||
kind = SCC_ABI_TYPE_DOUBLE;
|
||||
break;
|
||||
default:
|
||||
Panic("Unsupported AST type: %d",
|
||||
scc_ast_canon_type(ast_type)->builtin.type);
|
||||
break;
|
||||
}
|
||||
scc_abi_get_base_type_layout(ctx->impls, kind, layout);
|
||||
}
|
||||
Reference in New Issue
Block a user