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:
@@ -7,6 +7,7 @@ description = ""
|
||||
dependencies = [
|
||||
{ name = "scc_ast", path = "../ast" },
|
||||
{ name = "scc_hir", path = "../ir/hir" },
|
||||
{ name = "type_abi", path = "../target/type_abi" },
|
||||
]
|
||||
# features = {}
|
||||
# default_features = []
|
||||
|
||||
@@ -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)接口 ======
|
||||
|
||||
/**
|
||||
|
||||
@@ -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__ */
|
||||
@@ -1,4 +0,0 @@
|
||||
#ifndef __SCC_ABI_DUMMY_H__
|
||||
#define __SCC_ABI_DUMMY_H__
|
||||
|
||||
#endif /* __SCC_ABI_DUMMY_H__ */
|
||||
@@ -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__ */
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,7 @@
|
||||
#include <scc_ast2ir.h>
|
||||
#include <scc_ast_def.h>
|
||||
#include <scc_ast_utils.h>
|
||||
#include <scc_hir_layout.h>
|
||||
|
||||
/* ---------- internal helpers ---------- */
|
||||
|
||||
@@ -54,9 +55,8 @@ static cbool apply_integer_truncation(scc_ast2ir_ctx_t *ctx, scc_ap_t *val,
|
||||
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;
|
||||
scc_hir_type_ref_t tref = scc_ast2ir_type(ctx, target_type);
|
||||
int bits = scc_hir_type_size(scc_ast2ir_mir_module(ctx), tref, ctx->type_abi);
|
||||
if (bits >= (int)sizeof(scc_ap_digit) * 8)
|
||||
return true; /* no truncation needed */
|
||||
|
||||
@@ -342,34 +342,31 @@ cbool scc_ast2ir_eval_constant_int(scc_ast2ir_ctx_t *ctx, scc_ap_t *result,
|
||||
}
|
||||
|
||||
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);
|
||||
scc_hir_type_ref_t tref = scc_ast2ir_type(ctx, expr->attr_of.type);
|
||||
int size = scc_hir_type_size(scc_ast2ir_mir_module(ctx), tref,
|
||||
ctx->type_abi);
|
||||
scc_ap_set_int(result, size / 8);
|
||||
return true;
|
||||
} 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);
|
||||
scc_hir_type_ref_t tref = scc_ast2ir_type(ctx, expr->attr_of.type);
|
||||
int align = scc_hir_type_align(scc_ast2ir_mir_module(ctx), tref,
|
||||
ctx->type_abi);
|
||||
scc_ap_set_int(result, align / 8);
|
||||
return true;
|
||||
} 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:
|
||||
|
||||
925
libs/ast2ir/src/scc_ast2ir_expr.c
Normal file
925
libs/ast2ir/src/scc_ast2ir_expr.c
Normal file
@@ -0,0 +1,925 @@
|
||||
#include <scc_ast2ir.h>
|
||||
#include <scc_ast_def.h>
|
||||
#include <scc_ast_utils.h>
|
||||
#include <scc_hir_builder.h>
|
||||
#include <scc_hir_def.h>
|
||||
|
||||
// 判断是否为算术类型(整数或浮点)
|
||||
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:
|
||||
case SCC_HIR_TYPE_i32:
|
||||
case SCC_HIR_TYPE_i64:
|
||||
case SCC_HIR_TYPE_i128:
|
||||
case SCC_HIR_TYPE_u8:
|
||||
case SCC_HIR_TYPE_u16:
|
||||
case SCC_HIR_TYPE_u32:
|
||||
case SCC_HIR_TYPE_u64:
|
||||
case SCC_HIR_TYPE_u128:
|
||||
case SCC_HIR_TYPE_f16:
|
||||
case SCC_HIR_TYPE_f32:
|
||||
case SCC_HIR_TYPE_f64:
|
||||
case SCC_HIR_TYPE_f128:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static inline bool scc_hir_type_is_signed(scc_hir_type_tag_t tag) {
|
||||
switch (tag) {
|
||||
case SCC_HIR_TYPE_i8:
|
||||
case SCC_HIR_TYPE_i16:
|
||||
case SCC_HIR_TYPE_i32:
|
||||
case SCC_HIR_TYPE_i64:
|
||||
case SCC_HIR_TYPE_i128:
|
||||
return true;
|
||||
case SCC_HIR_TYPE_u8:
|
||||
case SCC_HIR_TYPE_u16:
|
||||
case SCC_HIR_TYPE_u32:
|
||||
case SCC_HIR_TYPE_u64:
|
||||
case SCC_HIR_TYPE_u128:
|
||||
return false;
|
||||
default:
|
||||
// 指针可以视作无符号整数
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
scc_hir_value_ref_t scc_ast2ir_logical_expr(scc_ast2ir_ctx_t *ctx,
|
||||
const scc_ast_expr_t *expr) {
|
||||
// scc_hir_bblock_ref_t start_block =
|
||||
// scc_hir_builder_current_bblock(&ctx->builder);
|
||||
|
||||
scc_hir_value_ref_t right_block =
|
||||
scc_hir_builder_create_bblock(&ctx->builder, "logic_right");
|
||||
scc_hir_value_ref_t end_block =
|
||||
scc_hir_builder_create_bblock(&ctx->builder, "logic_end");
|
||||
|
||||
// 为结果创建临时存储空间
|
||||
scc_hir_type_ref_t int32_type = scc_hir_builder_type_i32(&ctx->builder);
|
||||
scc_hir_value_ref_t result_var =
|
||||
scc_hir_builder_alloca(&ctx->builder, int32_type, "logic_result");
|
||||
|
||||
// 计算左操作数
|
||||
scc_hir_value_ref_t left_val =
|
||||
scc_ast2ir_expr(ctx, expr->binary.lhs, false);
|
||||
|
||||
scc_ap_t zero_ap;
|
||||
scc_ap_set_int(&zero_ap, 0);
|
||||
scc_ap_t one_ap;
|
||||
scc_ap_set_int(&one_ap, 1);
|
||||
scc_hir_value_ref_t zero_val =
|
||||
scc_hir_builder_integer(&ctx->builder, int32_type, &zero_ap);
|
||||
scc_hir_value_ref_t one_val =
|
||||
scc_hir_builder_integer(&ctx->builder, int32_type, &one_ap);
|
||||
|
||||
if (expr->binary.op == SCC_AST_OP_LOGICAL_AND) {
|
||||
// a && b
|
||||
scc_hir_value_ref_t false_block =
|
||||
scc_hir_builder_create_bblock(&ctx->builder, "and_false");
|
||||
|
||||
// 如果左操作数为0,结果为0(短路)
|
||||
scc_hir_value_ref_t is_left_zero = scc_hir_builder_binop(
|
||||
&ctx->builder, SCC_HIR_OP_EQ, left_val, zero_val);
|
||||
scc_hir_builder_branch(&ctx->builder, is_left_zero, false_block,
|
||||
right_block);
|
||||
|
||||
// false_block: 左边为0,结果为0
|
||||
scc_hir_builder_append_bblock(&ctx->builder, false_block);
|
||||
scc_hir_builder_set_current_bblock(&ctx->builder, false_block);
|
||||
scc_hir_builder_store(&ctx->builder, result_var, zero_val);
|
||||
scc_hir_builder_jump(&ctx->builder, end_block);
|
||||
|
||||
// right_block: 左边非0,计算右边
|
||||
scc_hir_builder_append_bblock(&ctx->builder, right_block);
|
||||
scc_hir_builder_set_current_bblock(&ctx->builder, right_block);
|
||||
scc_hir_value_ref_t right_val =
|
||||
scc_ast2ir_expr(ctx, expr->binary.rhs, false);
|
||||
scc_hir_value_ref_t is_right_zero = scc_hir_builder_binop(
|
||||
&ctx->builder, SCC_HIR_OP_EQ, right_val, zero_val);
|
||||
scc_hir_value_ref_t result =
|
||||
scc_hir_builder_binop(&ctx->builder, SCC_HIR_OP_XOR, is_right_zero,
|
||||
one_val); // !right == 0 ? 1 : 0
|
||||
scc_hir_builder_store(&ctx->builder, result_var, result);
|
||||
scc_hir_builder_jump(&ctx->builder, end_block);
|
||||
|
||||
} else { // SCC_AST_OP_LOGICAL_OR
|
||||
// a || b
|
||||
scc_hir_value_ref_t true_block =
|
||||
scc_hir_builder_create_bblock(&ctx->builder, "or_true");
|
||||
|
||||
// 如果左操作数非0,结果为1(短路)
|
||||
scc_hir_value_ref_t is_left_nonzero = scc_hir_builder_binop(
|
||||
&ctx->builder, SCC_HIR_OP_NEQ, left_val, zero_val);
|
||||
scc_hir_builder_branch(&ctx->builder, is_left_nonzero, true_block,
|
||||
right_block);
|
||||
|
||||
// true_block: 左边非0,结果为1
|
||||
scc_hir_builder_append_bblock(&ctx->builder, true_block);
|
||||
scc_hir_builder_set_current_bblock(&ctx->builder, true_block);
|
||||
scc_hir_builder_store(&ctx->builder, result_var, one_val);
|
||||
scc_hir_builder_jump(&ctx->builder, end_block);
|
||||
|
||||
// right_block: 左边为0,计算右边
|
||||
scc_hir_builder_append_bblock(&ctx->builder, right_block);
|
||||
scc_hir_builder_set_current_bblock(&ctx->builder, right_block);
|
||||
scc_hir_value_ref_t right_val =
|
||||
scc_ast2ir_expr(ctx, expr->binary.rhs, false);
|
||||
scc_hir_value_ref_t is_right_zero = scc_hir_builder_binop(
|
||||
&ctx->builder, SCC_HIR_OP_EQ, right_val, zero_val);
|
||||
scc_hir_value_ref_t result =
|
||||
scc_hir_builder_binop(&ctx->builder, SCC_HIR_OP_XOR, is_right_zero,
|
||||
one_val); // !right == 0 ? 1 : 0
|
||||
scc_hir_builder_store(&ctx->builder, result_var, result);
|
||||
scc_hir_builder_jump(&ctx->builder, end_block);
|
||||
}
|
||||
|
||||
// 设置结束块为当前块,并返回结果
|
||||
scc_hir_builder_append_bblock(&ctx->builder, end_block);
|
||||
scc_hir_builder_set_current_bblock(&ctx->builder, end_block);
|
||||
return scc_hir_builder_load(&ctx->builder, result_var);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
* @param ctx
|
||||
* @param expr
|
||||
* @return scc_hir_value_ref_t
|
||||
*/
|
||||
scc_hir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx,
|
||||
const scc_ast_expr_t *expr,
|
||||
cbool is_lvalue) {
|
||||
if (ctx == nullptr || expr == nullptr) {
|
||||
LOG_ERROR("args is nullptr");
|
||||
return 0;
|
||||
}
|
||||
|
||||
cbool is_assign = true;
|
||||
switch (expr->base.type) {
|
||||
case SCC_AST_EXPR_BINARY: {
|
||||
scc_ast_expr_t tmp_expr;
|
||||
scc_hir_value_ref_t lhs = SCC_HIR_REF_nullptr,
|
||||
rhs = SCC_HIR_REF_nullptr;
|
||||
switch (expr->binary.op) {
|
||||
case SCC_AST_OP_ASSIGN: // =
|
||||
rhs = scc_ast2ir_expr(ctx, expr->binary.rhs, false);
|
||||
break;
|
||||
case SCC_AST_OP_ASSIGN_ADD: // +=
|
||||
scc_ast_expr_binary_init(&tmp_expr, SCC_AST_OP_ADD,
|
||||
expr->binary.lhs, expr->binary.rhs,
|
||||
expr->base.loc);
|
||||
rhs = scc_ast2ir_expr(ctx, &tmp_expr, false);
|
||||
break;
|
||||
case SCC_AST_OP_ASSIGN_SUB: // -=
|
||||
scc_ast_expr_binary_init(&tmp_expr, SCC_AST_OP_SUB,
|
||||
expr->binary.lhs, expr->binary.rhs,
|
||||
expr->base.loc);
|
||||
rhs = scc_ast2ir_expr(ctx, &tmp_expr, false);
|
||||
break;
|
||||
case SCC_AST_OP_ASSIGN_MUL: // *=
|
||||
scc_ast_expr_binary_init(&tmp_expr, SCC_AST_OP_MUL,
|
||||
expr->binary.lhs, expr->binary.rhs,
|
||||
expr->base.loc);
|
||||
rhs = scc_ast2ir_expr(ctx, &tmp_expr, false);
|
||||
break;
|
||||
case SCC_AST_OP_ASSIGN_DIV: // /=
|
||||
scc_ast_expr_binary_init(&tmp_expr, SCC_AST_OP_DIV,
|
||||
expr->binary.lhs, expr->binary.rhs,
|
||||
expr->base.loc);
|
||||
rhs = scc_ast2ir_expr(ctx, &tmp_expr, false);
|
||||
break;
|
||||
case SCC_AST_OP_ASSIGN_MOD: // %=
|
||||
scc_ast_expr_binary_init(&tmp_expr, SCC_AST_OP_MOD,
|
||||
expr->binary.lhs, expr->binary.rhs,
|
||||
expr->base.loc);
|
||||
rhs = scc_ast2ir_expr(ctx, &tmp_expr, false);
|
||||
break;
|
||||
case SCC_AST_OP_ASSIGN_AND: // &=
|
||||
scc_ast_expr_binary_init(&tmp_expr, SCC_AST_OP_BITWISE_AND,
|
||||
expr->binary.lhs, expr->binary.rhs,
|
||||
expr->base.loc);
|
||||
rhs = scc_ast2ir_expr(ctx, &tmp_expr, false);
|
||||
break;
|
||||
case SCC_AST_OP_ASSIGN_XOR: // ^=
|
||||
scc_ast_expr_binary_init(&tmp_expr, SCC_AST_OP_BITWISE_XOR,
|
||||
expr->binary.lhs, expr->binary.rhs,
|
||||
expr->base.loc);
|
||||
rhs = scc_ast2ir_expr(ctx, &tmp_expr, false);
|
||||
break;
|
||||
case SCC_AST_OP_ASSIGN_OR: // |=
|
||||
scc_ast_expr_binary_init(&tmp_expr, SCC_AST_OP_BITWISE_OR,
|
||||
expr->binary.lhs, expr->binary.rhs,
|
||||
expr->base.loc);
|
||||
rhs = scc_ast2ir_expr(ctx, &tmp_expr, false);
|
||||
break;
|
||||
case SCC_AST_OP_ASSIGN_LSHIFT: // <<=
|
||||
scc_ast_expr_binary_init(&tmp_expr, SCC_AST_OP_LEFT_SHIFT,
|
||||
expr->binary.lhs, expr->binary.rhs,
|
||||
expr->base.loc);
|
||||
rhs = scc_ast2ir_expr(ctx, &tmp_expr, false);
|
||||
break;
|
||||
case SCC_AST_OP_ASSIGN_RSHIFT: // >>=
|
||||
scc_ast_expr_binary_init(&tmp_expr, SCC_AST_OP_RIGHT_SHIFT,
|
||||
expr->binary.lhs, expr->binary.rhs,
|
||||
expr->base.loc);
|
||||
rhs = scc_ast2ir_expr(ctx, &tmp_expr, false);
|
||||
break;
|
||||
default:
|
||||
is_assign = false;
|
||||
break;
|
||||
}
|
||||
if (is_assign) {
|
||||
Assert(rhs != SCC_HIR_REF_nullptr);
|
||||
lhs = scc_ast2ir_expr(ctx, expr->binary.lhs, true);
|
||||
// 将 RHS 转换为 LHS 指向的类型(C11 6.5.16.1 简单赋值)
|
||||
scc_hir_type_t *lhs_ptr_type = scc_hir_module_get_type_by_value(
|
||||
scc_ast2ir_mir_module(ctx), lhs);
|
||||
if (lhs_ptr_type->tag == SCC_HIR_TYPE_PTR &&
|
||||
is_arithmetic_type(
|
||||
scc_hir_module_get_type(scc_ast2ir_mir_module(ctx),
|
||||
lhs_ptr_type->data.pointer.base))) {
|
||||
rhs = scc_ast2ir_emit_conversion(
|
||||
ctx, rhs, lhs_ptr_type->data.pointer.base);
|
||||
}
|
||||
scc_hir_builder_store(&ctx->builder, lhs, rhs);
|
||||
return rhs;
|
||||
}
|
||||
|
||||
if (expr->binary.op == SCC_AST_OP_LOGICAL_AND ||
|
||||
expr->binary.op == SCC_AST_OP_LOGICAL_OR) {
|
||||
// TODO 类型提升
|
||||
return scc_ast2ir_logical_expr(ctx, expr);
|
||||
}
|
||||
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)) {
|
||||
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) {
|
||||
// 移位操作符:只进行整数提升(C11 6.5.7)
|
||||
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 promoted =
|
||||
scc_ast2ir_integer_promotion(ctx, lhs_type_ref);
|
||||
if (promoted != 0)
|
||||
lhs = scc_ast2ir_emit_conversion(ctx, lhs, promoted);
|
||||
}
|
||||
|
||||
// 映射操作符
|
||||
scc_hir_op_type_t op;
|
||||
switch (expr->binary.op) {
|
||||
/* clang-format off */
|
||||
case SCC_AST_OP_ADD: op = SCC_HIR_OP_ADD; break;
|
||||
case SCC_AST_OP_SUB: op = SCC_HIR_OP_SUB; break;
|
||||
case SCC_AST_OP_MUL: op = SCC_HIR_OP_MUL; break;
|
||||
case SCC_AST_OP_DIV: op = SCC_HIR_OP_DIV; break;
|
||||
case SCC_AST_OP_MOD: op = SCC_HIR_OP_MOD; break;
|
||||
case SCC_AST_OP_BITWISE_AND: op = SCC_HIR_OP_AND; break;
|
||||
case SCC_AST_OP_BITWISE_OR: op = SCC_HIR_OP_OR; break;
|
||||
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: {
|
||||
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: {
|
||||
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;
|
||||
}
|
||||
/* 逻辑操作符 */
|
||||
case SCC_AST_OP_LOGICAL_OR: // ||
|
||||
case SCC_AST_OP_LOGICAL_AND: // &&
|
||||
UNREACHABLE();
|
||||
return 0;
|
||||
/* clang-format on */
|
||||
default:
|
||||
LOG_FATAL("Unsupported binary operator: %d", expr->binary.op);
|
||||
return 0;
|
||||
}
|
||||
// 创建操作节点
|
||||
return scc_hir_builder_binop(&ctx->builder, op, lhs, rhs);
|
||||
} break;
|
||||
case SCC_AST_EXPR_UNARY: {
|
||||
if (expr->unary.op == SCC_AST_OP_ADDRESS_OF) {
|
||||
return scc_ast2ir_expr(ctx, expr->unary.operand, true);
|
||||
} else if (expr->unary.op == SCC_AST_OP_INDIRECTION) {
|
||||
// 从地址取值
|
||||
scc_hir_value_ref_t ptr =
|
||||
scc_ast2ir_expr(ctx, expr->unary.operand, false);
|
||||
if (is_lvalue) {
|
||||
// 作为左值使用(如 *ptr = ...),直接返回指针值(地址)
|
||||
return ptr;
|
||||
} else {
|
||||
// 作为右值使用(如 x = *ptr),加载指针指向的值
|
||||
return scc_hir_builder_load(&ctx->builder, ptr);
|
||||
}
|
||||
}
|
||||
scc_hir_value_ref_t operand =
|
||||
scc_ast2ir_expr(ctx, expr->unary.operand, is_lvalue);
|
||||
switch (expr->unary.op) {
|
||||
case SCC_AST_OP_UNARY_PLUS:
|
||||
/* just pass */
|
||||
return operand;
|
||||
case SCC_AST_OP_UNARY_MINUS: {
|
||||
// 负号
|
||||
// 实现为0 - operand
|
||||
scc_hir_type_ref_t type_ref =
|
||||
scc_hir_builder_type_i32(&ctx->builder);
|
||||
scc_ap_t value;
|
||||
scc_ap_set_int(&value, 0);
|
||||
scc_hir_value_ref_t zero_ref =
|
||||
scc_hir_builder_integer(&ctx->builder, type_ref, &value);
|
||||
return scc_hir_builder_binop(&ctx->builder, SCC_HIR_OP_SUB,
|
||||
zero_ref, operand);
|
||||
}
|
||||
case SCC_AST_OP_ADDRESS_OF:
|
||||
case SCC_AST_OP_INDIRECTION:
|
||||
UNREACHABLE();
|
||||
break;
|
||||
case SCC_AST_OP_BITWISE_NOT:
|
||||
// 按位取反
|
||||
return scc_hir_builder_binop(&ctx->builder, SCC_HIR_OP_NOT, operand,
|
||||
0);
|
||||
case SCC_AST_OP_LOGICAL_NOT: {
|
||||
// 逻辑非
|
||||
// 实现为与0比较
|
||||
scc_hir_type_ref_t type_ref =
|
||||
scc_hir_builder_type_i32(&ctx->builder);
|
||||
scc_ap_t value;
|
||||
scc_ap_set_int(&value, 0);
|
||||
scc_hir_value_ref_t zero_ref =
|
||||
scc_hir_builder_integer(&ctx->builder, type_ref, &value);
|
||||
return scc_hir_builder_binop(&ctx->builder, SCC_HIR_OP_EQ, zero_ref,
|
||||
operand);
|
||||
}
|
||||
case SCC_AST_OP_PREFIX_INCREMENT:
|
||||
case SCC_AST_OP_PREFIX_DECREMENT: {
|
||||
// 获取左值地址
|
||||
scc_hir_value_ref_t addr =
|
||||
scc_ast2ir_expr(ctx, expr->unary.operand, true);
|
||||
scc_hir_value_ref_t old_val =
|
||||
scc_hir_builder_load(&ctx->builder, addr);
|
||||
scc_hir_type_ref_t old_type =
|
||||
scc_hir_module_get_value(
|
||||
scc_hir_builder_get_module(&ctx->builder), old_val)
|
||||
->type;
|
||||
|
||||
// 整数提升:C11 6.3.1.1,对 short/char 等类型提升为 int
|
||||
scc_hir_type_ref_t promoted_type_ref =
|
||||
scc_ast2ir_integer_promotion(ctx, old_type);
|
||||
scc_hir_value_ref_t promoted = old_val;
|
||||
if (promoted_type_ref != 0) {
|
||||
promoted =
|
||||
scc_ast2ir_emit_conversion(ctx, old_val, promoted_type_ref);
|
||||
} else {
|
||||
promoted_type_ref = old_type;
|
||||
}
|
||||
|
||||
scc_hir_value_ref_t one = scc_hir_builder_integer(
|
||||
&ctx->builder, promoted_type_ref,
|
||||
&(scc_ap_t){.data.digit = 1, .capacity = -1});
|
||||
scc_hir_op_type_t op =
|
||||
(expr->unary.op == SCC_AST_OP_PREFIX_INCREMENT)
|
||||
? SCC_HIR_OP_ADD
|
||||
: SCC_HIR_OP_SUB;
|
||||
scc_hir_value_ref_t new_val =
|
||||
scc_hir_builder_binop(&ctx->builder, op, promoted, one);
|
||||
|
||||
// 如果原类型不是提升后的类型,需要截断回原类型再存储
|
||||
scc_hir_value_ref_t stored_val = new_val;
|
||||
if (promoted_type_ref != old_type) {
|
||||
stored_val = scc_ast2ir_emit_conversion(ctx, new_val, old_type);
|
||||
}
|
||||
scc_hir_builder_store(&ctx->builder, addr, stored_val);
|
||||
Assert(new_val != SCC_HIR_REF_nullptr);
|
||||
return new_val; // 表达式的值是提升后的新值(符合 C 标准)
|
||||
} break;
|
||||
case SCC_AST_OP_POSTFIX_INCREMENT:
|
||||
case SCC_AST_OP_POSTFIX_DECREMENT: {
|
||||
// 获取左值地址
|
||||
scc_hir_value_ref_t addr =
|
||||
scc_ast2ir_expr(ctx, expr->unary.operand, true);
|
||||
scc_hir_value_ref_t old_val =
|
||||
scc_hir_builder_load(&ctx->builder, addr);
|
||||
scc_hir_type_ref_t old_type =
|
||||
scc_hir_module_get_value(
|
||||
scc_hir_builder_get_module(&ctx->builder), old_val)
|
||||
->type;
|
||||
|
||||
// 整数提升(C11 6.3.1.1)
|
||||
scc_hir_type_ref_t promoted_type_ref =
|
||||
scc_ast2ir_integer_promotion(ctx, old_type);
|
||||
scc_hir_value_ref_t promoted = old_val;
|
||||
if (promoted_type_ref != 0) {
|
||||
promoted =
|
||||
scc_ast2ir_emit_conversion(ctx, old_val, promoted_type_ref);
|
||||
} else {
|
||||
promoted_type_ref = old_type;
|
||||
}
|
||||
|
||||
scc_hir_value_ref_t one = scc_hir_builder_integer(
|
||||
&ctx->builder, promoted_type_ref,
|
||||
&(scc_ap_t){.data.digit = 1, .capacity = -1});
|
||||
scc_hir_op_type_t op =
|
||||
(expr->unary.op == SCC_AST_OP_POSTFIX_INCREMENT)
|
||||
? SCC_HIR_OP_ADD
|
||||
: SCC_HIR_OP_SUB;
|
||||
scc_hir_value_ref_t new_val =
|
||||
scc_hir_builder_binop(&ctx->builder, op, promoted, one);
|
||||
|
||||
scc_hir_value_ref_t stored_val = new_val;
|
||||
if (promoted_type_ref != old_type) {
|
||||
stored_val = scc_ast2ir_emit_conversion(ctx, new_val, old_type);
|
||||
}
|
||||
scc_hir_builder_store(&ctx->builder, addr, stored_val);
|
||||
|
||||
// 后缀返回旧值的提升后版本(C11 6.5.2.4)
|
||||
return promoted;
|
||||
} break;
|
||||
default:
|
||||
LOG_FATAL("Unsupported unary operator: %d", expr->unary.op);
|
||||
return 0;
|
||||
}
|
||||
UNREACHABLE();
|
||||
} break;
|
||||
case SCC_AST_EXPR_COND: {
|
||||
scc_hir_type_ref_t true_type = SCC_HIR_REF_nullptr;
|
||||
scc_hir_type_ref_t false_type = SCC_HIR_REF_nullptr;
|
||||
|
||||
scc_hir_value_ref_t true_block =
|
||||
scc_hir_builder_bblock(&ctx->builder, "cond_true");
|
||||
scc_hir_value_ref_t false_block =
|
||||
scc_hir_builder_bblock(&ctx->builder, "cond_false");
|
||||
scc_hir_value_ref_t merge_block =
|
||||
scc_hir_builder_bblock(&ctx->builder, "cond_merge");
|
||||
|
||||
scc_hir_value_ref_t cond_node =
|
||||
scc_ast2ir_expr(ctx, expr->cond.cond, false);
|
||||
scc_hir_builder_branch(&ctx->builder, cond_node, true_block,
|
||||
false_block);
|
||||
|
||||
// 生成true分支
|
||||
scc_hir_builder_set_current_bblock(&ctx->builder, true_block);
|
||||
scc_hir_value_ref_t true_val =
|
||||
scc_ast2ir_expr(ctx, expr->cond.then_expr, false);
|
||||
|
||||
true_type = scc_hir_module_get_value(
|
||||
scc_hir_builder_get_module(&ctx->builder), true_val)
|
||||
->type;
|
||||
Assert(true_type != SCC_HIR_REF_nullptr);
|
||||
scc_hir_value_ref_t result_slot =
|
||||
scc_hir_builder_alloca(&ctx->builder, true_type, "cond_result");
|
||||
|
||||
scc_hir_builder_store(&ctx->builder, result_slot, true_val);
|
||||
scc_hir_builder_jump(&ctx->builder, merge_block);
|
||||
|
||||
// 生成false分支
|
||||
scc_hir_builder_set_current_bblock(&ctx->builder, false_block);
|
||||
scc_hir_value_ref_t false_val =
|
||||
scc_ast2ir_expr(ctx, expr->cond.else_expr, false);
|
||||
false_type = scc_hir_module_get_value(
|
||||
scc_hir_builder_get_module(&ctx->builder), false_val)
|
||||
->type;
|
||||
Assert(false_type != SCC_HIR_REF_nullptr);
|
||||
|
||||
scc_hir_builder_store(&ctx->builder, result_slot, false_val);
|
||||
scc_hir_builder_jump(&ctx->builder, merge_block);
|
||||
|
||||
// 合并并返回
|
||||
scc_hir_builder_set_current_bblock(&ctx->builder, merge_block);
|
||||
if (true_type != false_type) {
|
||||
LOG_ERROR("Type mismatch in conditional expression");
|
||||
// FIXME need panic
|
||||
}
|
||||
return scc_hir_builder_load(&ctx->builder, result_slot);
|
||||
} break;
|
||||
case SCC_AST_EXPR_CALL: {
|
||||
// 转换参数
|
||||
scc_hir_value_ref_vec_t args;
|
||||
scc_vec_init(args);
|
||||
|
||||
// 获取函数类型信息用于参数转换。
|
||||
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;
|
||||
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) {
|
||||
scc_ast_expr_t *arg_expr = scc_vec_at(expr->call.args, i);
|
||||
scc_hir_value_ref_t arg_node;
|
||||
arg_node = scc_ast2ir_expr(ctx, arg_expr, false);
|
||||
if ((int)i < fixed_count) {
|
||||
// 固定参数:转换为形参类型
|
||||
// 每次迭代重新获取指针(scc_ast2ir_expr 可能触发 types 向量
|
||||
// realloc)
|
||||
scc_hir_module_t *module = scc_ast2ir_mir_module(ctx);
|
||||
const scc_hir_type_t *ft =
|
||||
scc_hir_module_get_type(module, func_type_ref);
|
||||
scc_hir_type_ref_t param_type =
|
||||
scc_vec_at(ft->data.function.params, i);
|
||||
arg_node =
|
||||
scc_ast2ir_emit_conversion(ctx, arg_node, param_type);
|
||||
} else if (is_variadic) {
|
||||
// 可变参数:只应用默认参数提升(C11 6.5.2.2)
|
||||
// 整数提升:char/short → int
|
||||
scc_hir_module_t *module = scc_ast2ir_mir_module(ctx);
|
||||
|
||||
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;
|
||||
scc_hir_type_ref_t promoted =
|
||||
scc_ast2ir_integer_promotion(ctx, arg_type_ref);
|
||||
if (promoted != 0) {
|
||||
arg_node =
|
||||
scc_ast2ir_emit_conversion(ctx, arg_node, promoted);
|
||||
} else if (arg_tag == SCC_HIR_TYPE_f32) {
|
||||
// float → double(默认参数提升)
|
||||
scc_hir_type_t f64_desc;
|
||||
scc_hir_type_init(&f64_desc, SCC_HIR_TYPE_f64);
|
||||
scc_hir_type_ref_t f64_ref =
|
||||
scc_hir_builder_type(&ctx->builder, &f64_desc);
|
||||
arg_node =
|
||||
scc_ast2ir_emit_conversion(ctx, arg_node, f64_ref);
|
||||
}
|
||||
// 指针、i32/i64 等保持不变
|
||||
}
|
||||
scc_vec_push(args, arg_node);
|
||||
}
|
||||
|
||||
// 创建调用节点(需要查找函数定义)
|
||||
if (!callee_func) {
|
||||
LOG_ERROR("Function %s not found",
|
||||
expr->call.callee->identifier._target->name);
|
||||
}
|
||||
scc_hir_value_ref_t node = scc_hir_builder_call(
|
||||
&ctx->builder, callee_func, args.data, args.size);
|
||||
scc_vec_free(args);
|
||||
return node;
|
||||
}
|
||||
case SCC_AST_EXPR_ARRAY_SUBSCRIPT: {
|
||||
// 1. 计算数组/指针基址(右值,得到地址)
|
||||
scc_hir_value_ref_t base_ptr =
|
||||
scc_ast2ir_expr(ctx, expr->subscript.array, true);
|
||||
// 2. 计算下标值
|
||||
scc_hir_value_ref_t index =
|
||||
scc_ast2ir_expr(ctx, expr->subscript.index, false);
|
||||
// 保证 index 至少 64 位,防止后端寄存器分配时宽度错误
|
||||
scc_hir_type_t *idx_type =
|
||||
scc_hir_module_get_type_by_value(scc_ast2ir_mir_module(ctx), index);
|
||||
if (idx_type != nullptr &&
|
||||
scc_hir_module_type_size(scc_ast2ir_mir_module(ctx), idx_type) <
|
||||
sizeof(void *) * 8) {
|
||||
scc_hir_type_t ptrw_desc;
|
||||
scc_hir_type_init(&ptrw_desc, SCC_HIR_TYPE_u64);
|
||||
scc_hir_type_ref_t ptrw_ref =
|
||||
scc_hir_builder_type(&ctx->builder, &ptrw_desc);
|
||||
index = scc_ast2ir_emit_conversion(ctx, index, ptrw_ref);
|
||||
}
|
||||
// 3. 生成 getptr(GEP)
|
||||
scc_hir_value_ref_t elem_ptr =
|
||||
scc_hir_builder_get_elem_ptr(&ctx->builder, base_ptr, index);
|
||||
// 4. 根据左值/右值返回
|
||||
if (is_lvalue) {
|
||||
return elem_ptr; // 作为左值:返回地址
|
||||
} else {
|
||||
return scc_hir_builder_load(&ctx->builder,
|
||||
elem_ptr); // 作为右值:加载值
|
||||
}
|
||||
}
|
||||
case SCC_AST_EXPR_MEMBER: {
|
||||
// 1. 获取基对象的左值(地址)
|
||||
scc_hir_value_ref_t base_ptr =
|
||||
scc_ast2ir_expr(ctx, expr->member.base, true);
|
||||
// 2. 通过偏移生成字段地址
|
||||
// 需要基对象类型来计算索引:假设 sema 填好了 _target_idx
|
||||
usize field_idx = expr->member._target_idx;
|
||||
scc_ap_t idx_ap;
|
||||
scc_ap_set_int(&idx_ap, field_idx);
|
||||
scc_hir_value_ref_t idx_val = scc_hir_builder_integer(
|
||||
&ctx->builder, scc_hir_builder_type_u64(&ctx->builder), &idx_ap);
|
||||
scc_hir_value_ref_t field_ptr =
|
||||
scc_hir_builder_get_elem_ptr(&ctx->builder, base_ptr, idx_val);
|
||||
if (is_lvalue)
|
||||
return field_ptr;
|
||||
else
|
||||
return scc_hir_builder_load(&ctx->builder, field_ptr);
|
||||
}
|
||||
case SCC_AST_EXPR_PTR_MEMBER: {
|
||||
// 1. 计算指针值(obj->field 等价于 (*obj).field)
|
||||
scc_hir_value_ref_t obj_ptr =
|
||||
scc_ast2ir_expr(ctx, expr->member.base, false);
|
||||
// 2. 解引用得到对象地址,再访问成员
|
||||
// 但更简单:先 load 出对象值?不对:obj
|
||||
// 是指向结构体的指针,我们需要指针的地址? 实际上,obj->field 相当于
|
||||
// (*(obj)).field,所以可以直接用 obj 作为基地址进行 get_elem_ptr
|
||||
// 因为 get_elem_ptr 接收一个指针,obj 本身就是指向结构体的指针。
|
||||
usize field_idx = expr->member._target_idx;
|
||||
scc_ap_t idx_ap;
|
||||
scc_ap_set_int(&idx_ap, field_idx);
|
||||
scc_hir_value_ref_t idx_val = scc_hir_builder_integer(
|
||||
&ctx->builder, scc_hir_builder_type_u64(&ctx->builder), &idx_ap);
|
||||
scc_hir_value_ref_t field_ptr =
|
||||
scc_hir_builder_get_elem_ptr(&ctx->builder, obj_ptr, idx_val);
|
||||
if (is_lvalue)
|
||||
return field_ptr;
|
||||
else
|
||||
return scc_hir_builder_load(&ctx->builder, field_ptr);
|
||||
}
|
||||
case SCC_AST_EXPR_CAST: {
|
||||
// 1. 转换操作数(右值)
|
||||
scc_hir_value_ref_t operand =
|
||||
scc_ast2ir_expr(ctx, expr->cast.expr, false);
|
||||
|
||||
// 2. 转换目标类型为 IR 类型
|
||||
scc_hir_type_ref_t target_type = scc_ast2ir_type(ctx, expr->cast.type);
|
||||
|
||||
// 3. 确定转换模式(SEXT / ZEXT / TRUNC)
|
||||
// 这里用简单的启发式:根据大小变化决定
|
||||
scc_hir_type_t *src_type = scc_hir_module_get_type_by_value(
|
||||
scc_ast2ir_mir_module(ctx), operand);
|
||||
scc_hir_type_t *dst_type =
|
||||
scc_hir_module_get_type(scc_ast2ir_mir_module(ctx), target_type);
|
||||
|
||||
usize src_size = scc_hir_module_type_size(
|
||||
scc_ast2ir_mir_module(ctx),
|
||||
src_type); // 你可能有这个函数,否则自行计算
|
||||
usize dst_size =
|
||||
scc_hir_module_type_size(scc_ast2ir_mir_module(ctx), dst_type);
|
||||
|
||||
int conv_kind;
|
||||
if (dst_size > src_size) {
|
||||
// 目标更大,需要扩展。根据源类型有无符号决定符号扩展还是零扩展
|
||||
conv_kind = scc_hir_type_is_signed(src_type->tag)
|
||||
? SCC_HIR_CONV_SEXT
|
||||
: SCC_HIR_CONV_ZEXT;
|
||||
} else if (dst_size < src_size) {
|
||||
conv_kind = SCC_HIR_CONV_TRUNC;
|
||||
} else {
|
||||
// 同大小,可以视为 NOP 转换,或者直接返回操作数
|
||||
return operand; // 或创建一个 CONV_SEXT 也没问题
|
||||
}
|
||||
|
||||
// 4. 构造转换节点
|
||||
scc_hir_value_t conv_node = {
|
||||
.tag = SCC_HIR_VALUE_TAG_CONV,
|
||||
.type = target_type,
|
||||
.data.conv.operand = operand,
|
||||
.data.conv.target_type = target_type,
|
||||
.data.conv.conv_type = conv_kind,
|
||||
};
|
||||
return scc_hir_module_add_value(scc_ast2ir_mir_module(ctx), &conv_node);
|
||||
}
|
||||
case SCC_AST_EXPR_SIZE_OF: {
|
||||
// 1. 将 sizeof 的操作数(类型或表达式)转换为 IR 类型
|
||||
scc_hir_type_ref_t hir_type;
|
||||
if (expr->attr_of.type) {
|
||||
// sizeof(type)
|
||||
hir_type = scc_ast2ir_type(ctx, expr->attr_of.type);
|
||||
} else if (expr->attr_of.expr) {
|
||||
// sizeof expression:计算表达式类型
|
||||
// 注意:sizeof
|
||||
// 不对表达式求值,只需类型,这里假设语义分析已经标注了类型
|
||||
scc_hir_value_ref_t dummy =
|
||||
scc_ast2ir_expr(ctx, expr->attr_of.expr, false);
|
||||
hir_type =
|
||||
scc_hir_module_get_value(scc_ast2ir_mir_module(ctx), dummy)
|
||||
->type;
|
||||
} else {
|
||||
scc_ap_t val;
|
||||
val.data.digit = 0;
|
||||
LOG_ERROR("[ast2ir] unsupported");
|
||||
return scc_hir_builder_integer(
|
||||
&ctx->builder, scc_hir_builder_type_u64(&ctx->builder), &val);
|
||||
}
|
||||
|
||||
// 2. 计算大小
|
||||
const scc_hir_type_t *type =
|
||||
scc_hir_module_get_type(scc_ast2ir_mir_module(ctx), hir_type);
|
||||
usize dst_size =
|
||||
scc_hir_module_type_size(scc_ast2ir_mir_module(ctx), type);
|
||||
|
||||
scc_ap_t val;
|
||||
val.data.digit = dst_size / 8;
|
||||
return scc_hir_builder_integer(
|
||||
&ctx->builder, scc_hir_builder_type_u64(&ctx->builder), &val);
|
||||
}
|
||||
case SCC_AST_EXPR_ALIGN_OF: {
|
||||
TODO();
|
||||
// return scc_hir_builder_integer(
|
||||
// &ctx->builder, scc_hir_builder_type_u64(&ctx->builder), val);
|
||||
}
|
||||
case SCC_AST_EXPR_COMPOUND: {
|
||||
// 1. 从 base 子节点取得类型(base 一定是 SCC_AST_EXPR_LVALUE)
|
||||
Assert(expr->compound.base != nullptr);
|
||||
Assert(expr->compound.base->base.type == SCC_AST_EXPR_LVALUE);
|
||||
scc_ast_qual_type_t *ast_type = expr->compound.base->lvalue.type;
|
||||
scc_hir_type_ref_t type_ref = scc_ast2ir_type(ctx, ast_type);
|
||||
const scc_hir_type_t *type =
|
||||
scc_hir_module_get_type(scc_ast2ir_mir_module(ctx), type_ref);
|
||||
|
||||
// 2. 分配栈上临时存储
|
||||
scc_hir_value_ref_t storage =
|
||||
scc_hir_builder_alloca(&ctx->builder, type_ref, "compound_literal");
|
||||
|
||||
// 3. 根据类型调用对应的初始化函数
|
||||
if (type->tag == SCC_HIR_TYPE_ARRAY) {
|
||||
emit_array_initialization(ctx, storage, type, expr);
|
||||
} else if (type->tag == SCC_HIR_TYPE_STRUCT ||
|
||||
type->tag == SCC_HIR_TYPE_UNION) {
|
||||
emit_aggregate_initialization(ctx, storage, type, expr);
|
||||
} else {
|
||||
// 标量:取初始化列表的第一个值
|
||||
Assert(expr->compound.rhs_exprs.size > 0);
|
||||
scc_ast_expr_t *init_expr = scc_vec_at(expr->compound.rhs_exprs, 0);
|
||||
scc_hir_value_ref_t val = scc_ast2ir_expr(ctx, init_expr, false);
|
||||
scc_hir_builder_store(&ctx->builder, storage, val);
|
||||
}
|
||||
|
||||
// 4. 根据左值 / 右值返回
|
||||
if (is_lvalue) {
|
||||
return storage; // 左值
|
||||
} else {
|
||||
// 右值:数组退化为首元素指针,结构体暂时返回地址
|
||||
if (type->tag == SCC_HIR_TYPE_ARRAY) {
|
||||
return scc_hir_builder_get_elem_ptr(&ctx->builder, storage,
|
||||
SCC_HIR_REF_nullptr);
|
||||
} else if (type->tag == SCC_HIR_TYPE_STRUCT ||
|
||||
type->tag == SCC_HIR_TYPE_UNION) {
|
||||
return storage; // 调用者会 memcpy
|
||||
} else {
|
||||
return scc_hir_builder_load(&ctx->builder, storage);
|
||||
}
|
||||
}
|
||||
} break;
|
||||
case SCC_AST_EXPR_LVALUE: {
|
||||
UNREACHABLE(); // should only appear as compound.base
|
||||
} break;
|
||||
case SCC_AST_EXPR_BUILTIN: {
|
||||
TODO();
|
||||
} break;
|
||||
case SCC_AST_EXPR_INT_LITERAL: {
|
||||
// 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, 0);
|
||||
return scc_hir_builder_integer(&ctx->builder, type_ref, &value);
|
||||
}
|
||||
// SCC_AST_EXPR_FLOAT_LITERAL, // 浮点字面量
|
||||
case SCC_AST_EXPR_CHAR_LITERAL: {
|
||||
// FIXME just 'a' '\n'
|
||||
const char *lexme = expr->literal.lexme;
|
||||
Assert(lexme[0] == '\'');
|
||||
i8 int_lit = 0;
|
||||
if (lexme[1] == '\\') {
|
||||
switch (lexme[2]) {
|
||||
case 'a':
|
||||
int_lit = '\a';
|
||||
break;
|
||||
case 'b':
|
||||
int_lit = '\b';
|
||||
break;
|
||||
case 'f':
|
||||
int_lit = '\f';
|
||||
break;
|
||||
case 'n':
|
||||
int_lit = '\n';
|
||||
break;
|
||||
case 'r':
|
||||
int_lit = '\r';
|
||||
}
|
||||
} else {
|
||||
int_lit = lexme[1];
|
||||
}
|
||||
scc_ap_t value;
|
||||
// FIXME i32 maybe false, we need i8 or u8
|
||||
scc_hir_type_ref_t type_ref = scc_hir_builder_type_i32(&ctx->builder);
|
||||
scc_ap_set_int(&value, int_lit);
|
||||
return scc_hir_builder_integer(&ctx->builder, type_ref, &value);
|
||||
}
|
||||
case SCC_AST_EXPR_STRING_LITERAL: {
|
||||
// const_string 返回指向常量区字符串的指针,左右值都返回此指针
|
||||
scc_hir_value_ref_t value =
|
||||
scc_hir_builder_const_string(&ctx->builder, expr->literal.lexme,
|
||||
scc_strlen(expr->literal.lexme));
|
||||
return value;
|
||||
}
|
||||
case SCC_AST_EXPR_IDENTIFIER: {
|
||||
if (expr->identifier._target == nullptr) {
|
||||
LOG_ERROR("unknown identifier %s", expr->identifier.name);
|
||||
}
|
||||
// FIXME hack hashtable
|
||||
scc_hir_value_ref_t in = (scc_hir_value_ref_t)(usize)scc_hashtable_get(
|
||||
&ctx->ast2ir_cache, expr->identifier._target);
|
||||
Assert(in != 0);
|
||||
if (is_lvalue) {
|
||||
return in;
|
||||
} else {
|
||||
// 右值:如果是数组类型,退化为指针(返回地址)
|
||||
scc_hir_type_t *ir_type = scc_hir_module_get_type_by_value(
|
||||
scc_ast2ir_mir_module(ctx), in);
|
||||
if (ir_type->tag == SCC_HIR_TYPE_PTR) {
|
||||
scc_hir_type_t *target_type = scc_hir_module_get_type(
|
||||
scc_ast2ir_mir_module(ctx), ir_type->data.pointer.base);
|
||||
if (target_type->tag == SCC_HIR_TYPE_ARRAY) {
|
||||
// 生成 getptr 获取数组首地址
|
||||
return scc_hir_builder_get_elem_ptr(&ctx->builder, in,
|
||||
SCC_HIR_REF_nullptr);
|
||||
} else {
|
||||
// 标量类型:加载值
|
||||
return scc_hir_builder_load(&ctx->builder, in);
|
||||
}
|
||||
} else {
|
||||
return in;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
LOG_FATAL("Unsupported expression type: %d", expr->base.type);
|
||||
return 0;
|
||||
}
|
||||
UNREACHABLE();
|
||||
}
|
||||
@@ -1,76 +1,106 @@
|
||||
#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:
|
||||
static int ast_base_type_size(const scc_type_abi_t *abi,
|
||||
scc_ast_builtin_type_t bt) {
|
||||
switch (bt) {
|
||||
case SCC_AST_BUILTIN_TYPE_VOID:
|
||||
return scc_hir_builder_type_void(&ctx->builder);
|
||||
return 0;
|
||||
case SCC_AST_BUILTIN_TYPE_VA_LIST:
|
||||
return abi->va_list_size;
|
||||
case SCC_AST_BUILTIN_TYPE_BOOL:
|
||||
return 1;
|
||||
case SCC_AST_BUILTIN_TYPE_CHAR:
|
||||
case SCC_AST_BUILTIN_TYPE_SIGNED_CHAR:
|
||||
case SCC_AST_BUILTIN_TYPE_UNSIGNED_CHAR:
|
||||
return abi->char_size;
|
||||
case SCC_AST_BUILTIN_TYPE_SHORT:
|
||||
case SCC_AST_BUILTIN_TYPE_SIGNED_SHORT:
|
||||
case SCC_AST_BUILTIN_TYPE_UNSIGNED_SHORT:
|
||||
return abi->short_size;
|
||||
case SCC_AST_BUILTIN_TYPE_INT:
|
||||
case SCC_AST_BUILTIN_TYPE_SIGNED_INT:
|
||||
case SCC_AST_BUILTIN_TYPE_UNSIGNED_INT:
|
||||
return abi->int_size;
|
||||
case SCC_AST_BUILTIN_TYPE_LONG:
|
||||
case SCC_AST_BUILTIN_TYPE_SIGNED_LONG:
|
||||
case SCC_AST_BUILTIN_TYPE_UNSIGNED_LONG:
|
||||
return abi->long_size;
|
||||
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_LONG_LONG:
|
||||
return abi->long_long_size;
|
||||
case SCC_AST_BUILTIN_TYPE_FLOAT:
|
||||
return abi->float_size;
|
||||
case SCC_AST_BUILTIN_TYPE_DOUBLE:
|
||||
return abi->double_size;
|
||||
case SCC_AST_BUILTIN_TYPE_LONG_DOUBLE:
|
||||
return abi->long_double_size;
|
||||
default:
|
||||
Panic("Unsupported AST builtin type: %d", bt);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static cbool is_signed_builtin(scc_ast_builtin_type_t bt) {
|
||||
switch (bt) {
|
||||
case SCC_AST_BUILTIN_TYPE_BOOL:
|
||||
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;
|
||||
return false;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
scc_hir_type_ref_t
|
||||
scc_ast2ir_parse_base_type(scc_ast2ir_ctx_t *ctx,
|
||||
const scc_ast_qual_type_t *ast_type) {
|
||||
scc_ast_builtin_type_t bt = scc_ast_canon_type(ast_type)->builtin.type;
|
||||
int size = ast_base_type_size(ctx->type_abi, bt);
|
||||
|
||||
if (bt == SCC_AST_BUILTIN_TYPE_VOID ||
|
||||
bt == SCC_AST_BUILTIN_TYPE_VA_LIST) {
|
||||
return scc_hir_builder_type_void(&ctx->builder);
|
||||
}
|
||||
|
||||
cbool is_signed = is_signed_builtin(bt);
|
||||
|
||||
// 浮点类型
|
||||
switch (bt) {
|
||||
case SCC_AST_BUILTIN_TYPE_FLOAT:
|
||||
case SCC_AST_BUILTIN_TYPE_DOUBLE:
|
||||
switch (layout.size) {
|
||||
case SCC_AST_BUILTIN_TYPE_LONG_DOUBLE:
|
||||
switch (size) {
|
||||
case 4:
|
||||
return scc_hir_builder_type_f32(&ctx->builder);
|
||||
case 8:
|
||||
return scc_hir_builder_type_f64(&ctx->builder);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
Panic("Unsupported float size: %d", size);
|
||||
}
|
||||
default:
|
||||
Panic("Unsupported AST type: %d",
|
||||
scc_ast_canon_type(ast_type)->builtin.type);
|
||||
break;
|
||||
}
|
||||
return SCC_HIR_REF_nullptr;
|
||||
|
||||
// 整数类型
|
||||
switch (size) {
|
||||
case 1:
|
||||
return is_signed ? scc_hir_builder_type_i8(&ctx->builder)
|
||||
: scc_hir_builder_type_u8(&ctx->builder);
|
||||
case 2:
|
||||
return is_signed ? scc_hir_builder_type_i16(&ctx->builder)
|
||||
: scc_hir_builder_type_u16(&ctx->builder);
|
||||
case 4:
|
||||
return is_signed ? scc_hir_builder_type_i32(&ctx->builder)
|
||||
: scc_hir_builder_type_u32(&ctx->builder);
|
||||
case 8:
|
||||
return is_signed ? scc_hir_builder_type_i64(&ctx->builder)
|
||||
: scc_hir_builder_type_u64(&ctx->builder);
|
||||
default:
|
||||
Panic("Unsupported integer size: %d", size);
|
||||
return SCC_HIR_REF_nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
102
libs/ast2ir/src/scc_ast2ir_utils.c
Normal file
102
libs/ast2ir/src/scc_ast2ir_utils.c
Normal file
@@ -0,0 +1,102 @@
|
||||
#include <scc_ast2ir.h>
|
||||
|
||||
// 辅助函数:生成数组初始化代码
|
||||
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) {
|
||||
Assert(array_type->tag == SCC_HIR_TYPE_ARRAY);
|
||||
scc_hir_type_ref_t elem_type_ref = array_type->data.array.base;
|
||||
const scc_hir_type_t *elem_type =
|
||||
scc_hir_module_get_type(scc_ast2ir_mir_module(ctx), elem_type_ref);
|
||||
usize array_len = array_type->data.array.len;
|
||||
|
||||
// 字符串字面量初始化:直接 memcpy
|
||||
if (init_expr->base.type == SCC_AST_EXPR_STRING_LITERAL) {
|
||||
scc_hir_value_ref_t str_val = scc_ast2ir_expr(ctx, init_expr, false);
|
||||
scc_ap_t len_ap;
|
||||
scc_ap_set_int(&len_ap, array_len);
|
||||
scc_hir_value_ref_t len_ref = scc_hir_builder_integer(
|
||||
&ctx->builder, scc_hir_builder_type_u64(&ctx->builder), &len_ap);
|
||||
scc_hir_builder_builtin_memcpy(&ctx->builder, array_ptr, str_val,
|
||||
len_ref);
|
||||
return;
|
||||
}
|
||||
|
||||
// 复合初始化:逐个元素 store
|
||||
if (init_expr->base.type != SCC_AST_EXPR_COMPOUND) {
|
||||
Panic("unsupported initializer for array");
|
||||
}
|
||||
|
||||
// 遍历初始化列表
|
||||
usize idx = 0;
|
||||
scc_vec_foreach(init_expr->compound.rhs_exprs, i) {
|
||||
scc_ast_expr_t *elem_expr =
|
||||
scc_vec_at(init_expr->compound.rhs_exprs, i);
|
||||
Assert(elem_expr != nullptr);
|
||||
if (idx >= array_len)
|
||||
break; // 防止溢出
|
||||
|
||||
// 生成元素地址:array_ptr + idx * elem_size
|
||||
scc_ap_t offset_ap;
|
||||
scc_ap_set_int(&offset_ap, idx);
|
||||
scc_hir_value_ref_t idx_val = scc_hir_builder_integer(
|
||||
&ctx->builder, scc_hir_builder_type_u64(&ctx->builder), &offset_ap);
|
||||
scc_hir_value_ref_t elem_ptr =
|
||||
scc_hir_builder_get_elem_ptr(&ctx->builder, array_ptr, idx_val);
|
||||
|
||||
// 递归处理:如果元素本身是数组且初始化表达式是复合初始化,需要嵌套处理
|
||||
if (elem_type->tag == SCC_HIR_TYPE_ARRAY &&
|
||||
elem_expr->base.type == SCC_AST_EXPR_COMPOUND) {
|
||||
// 递归调用自身,但注意 elem_ptr 已经是子数组的首地址
|
||||
emit_array_initialization(ctx, elem_ptr, elem_type, elem_expr);
|
||||
} else {
|
||||
// 标量元素:计算右值并 store
|
||||
scc_hir_value_ref_t val = scc_ast2ir_expr(ctx, elem_expr, false);
|
||||
scc_hir_builder_store(&ctx->builder, elem_ptr, val);
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
// 如果初始化列表元素少于数组长度,剩余元素默认零初始化(C 标准要求)
|
||||
// 这里简单忽略,实际可生成 memset 循环,但为简化暂不处理
|
||||
}
|
||||
|
||||
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) {
|
||||
Assert(type->tag == SCC_HIR_TYPE_STRUCT || type->tag == SCC_HIR_TYPE_UNION);
|
||||
if (init_expr->base.type != SCC_AST_EXPR_COMPOUND) {
|
||||
Panic("expected compound initializer");
|
||||
}
|
||||
usize idx = 0;
|
||||
scc_vec_foreach(init_expr->compound.rhs_exprs, i) {
|
||||
if (idx >= type->data.aggregate.fields.size)
|
||||
break;
|
||||
scc_ast_expr_t *field_init =
|
||||
scc_vec_at(init_expr->compound.rhs_exprs, i);
|
||||
scc_hir_type_ref_t field_type_ref =
|
||||
scc_vec_at(type->data.aggregate.fields, idx);
|
||||
const scc_hir_type_t *field_type =
|
||||
scc_hir_module_get_type(scc_ast2ir_mir_module(ctx), field_type_ref);
|
||||
|
||||
scc_ap_t idx_ap;
|
||||
scc_ap_set_int(&idx_ap, idx);
|
||||
scc_hir_value_ref_t idx_val = scc_hir_builder_integer(
|
||||
&ctx->builder, scc_hir_builder_type_u64(&ctx->builder), &idx_ap);
|
||||
scc_hir_value_ref_t field_ptr =
|
||||
scc_hir_builder_get_elem_ptr(&ctx->builder, base_ptr, idx_val);
|
||||
|
||||
if (field_type->tag == SCC_HIR_TYPE_ARRAY) {
|
||||
emit_array_initialization(ctx, field_ptr, field_type, field_init);
|
||||
} else if (field_type->tag == SCC_HIR_TYPE_STRUCT ||
|
||||
field_type->tag == SCC_HIR_TYPE_UNION) {
|
||||
emit_aggregate_initialization(ctx, field_ptr, field_type,
|
||||
field_init);
|
||||
} else {
|
||||
scc_hir_value_ref_t val = scc_ast2ir_expr(ctx, field_init, false);
|
||||
scc_hir_builder_store(&ctx->builder, field_ptr, val);
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
#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