refactor(ast2ir): 提取模块访问函数并优化类型大小计算

- 添加 scc_ast2ir_mir_module 内联函数统一访问模块
- 替换所有直接访问 ctx->builder.cprog->module 的地方
- 移除重复的 scc_hir_type_size 函数实现
- 添加 scc_hir_module_type_size 函数到模块接口
- 更新所有类型大小计算调用使用新函数

feat(hir): 增强构建器安全性和全局变量处理

- 为 scc_hir_builder_integer 添加空指针检查断言
- 修复 scc_hir_builder_global_alloca 中全局变量类型设置
- 改进 scc_hir_builder_get_elem_ptr 处理空指针索引情况
- 重构字符串常量生成使用 get_elem_ptr 构建器函数

refactor(lir): 简化地址表达式表示并增强内置函数支持

- 移除复杂地址结构体 scc_lir_addr_t
- 简化 scc_lir_instr 结构体中的地址表示
- 移除 STORE_ADDR 操作码
- 添加 memcpy 和 memset 内置函数操作码
- 在符号元数据中使用联合体替代嵌套结构体

feat(hir2lir): 完善 HIR 到 LIR 转换中的内置函数处理

- 添加 ensure_vreg 辅助函数确保虚拟寄存器操作数
- 正确处理全局变量地址符号引用
- 优化 GET_ELEM_PTR 转换使用类型大小计算
- 完整实现所有内置函数(BUILTIN)的 LIR 转换
- 包括 memcpy、memset、va_start、va_arg、va_end、va_copy 等
This commit is contained in:
zzy
2026-05-22 15:15:18 +08:00
parent 41d060d7e7
commit d78b91894e
27 changed files with 1272 additions and 563 deletions

View File

@@ -41,26 +41,14 @@ typedef enum {
SCC_LIR_INSTR_KIND_MEM, // 复杂地址表达式 (base + index*scale + offset)
} scc_lir_instr_kind_t;
/**
* @brief 复杂地址表达式
* base + index * scale + offset
*/
typedef struct scc_lir_addr {
int base; // 基址寄存器,-1 表示无
int index; // 索引寄存器,-1 表示无
int scale; // 比例因子 (1, 2, 4, 8)
i64 offset; // 常量偏移
} scc_lir_addr_t;
typedef struct scc_lir_instr {
scc_lir_instr_kind_t kind;
union {
int arg;
int reg; // VREG 或 PREG 索引
scc_ap_t imm; // 整型立即数
f64 fimm; // 浮点立即数
const char *symbol; // 符号名 (生命周期由前端管理)
scc_lir_addr_t addr; // 复杂地址
int reg; // VREG 或 PREG 索引
scc_ap_t imm; // 整型立即数
f64 fimm; // 浮点立即数
const char *symbol; // 符号名 (生命周期由前端管理)
} data;
} scc_lir_val_t;
@@ -74,8 +62,6 @@ typedef struct scc_lir_instr {
((scc_lir_val_t){.kind = SCC_LIR_INSTR_KIND_FIMM, .data.fimm = (v)})
#define SCC_LIR_SYMBOL(s) \
((scc_lir_val_t){.kind = SCC_LIR_INSTR_KIND_SYMBOL, .data.symbol = (s)})
#define SCC_LIR_ADDR(b, i, s, o) \
((scc_lir_val_t){.kind = SCC_LIR_INSTR_KIND_MEM, .data.addr = {b, i, s, o}})
#define SCC_LIR_ARG(n) \
((scc_lir_val_t){.kind = SCC_LIR_INSTR_KIND_ARG, .data.arg = (n)})
@@ -97,7 +83,6 @@ typedef enum {
SCC_LIR_LOAD,
SCC_LIR_LOAD_ADDR,
SCC_LIR_STORE,
SCC_LIR_STORE_ADDR,
/* 整数算术 */
SCC_LIR_ADD,
@@ -143,6 +128,9 @@ typedef enum {
SCC_LIR_VA_ARG,
SCC_LIR_VA_END,
SCC_LIR_VA_COPY,
/* 内置方法 */
SCC_LIR_MEMCPY,
SCC_LIR_MEMSET,
/* 栈管理 */
SCC_LIR_ALLOCA,
@@ -183,6 +171,12 @@ typedef struct scc_lir_ins {
union {
scc_lir_cond_t cond;
// base + index * scale + offset
struct scc_lir_addr {
int scale; // 比例因子 (1, 2, 4, 8)
i64 offset; // 常量偏移
} addr;
struct scc_lir_br {
scc_lir_bblock_id_t true_target;
scc_lir_bblock_id_t false_target;
@@ -240,6 +234,18 @@ typedef struct scc_lir_ins {
scc_lir_val_t dest;
scc_lir_val_t src;
} va_copy;
struct {
scc_lir_val_t dest;
scc_lir_val_t src;
scc_lir_val_t size;
} memcpy;
struct {
scc_lir_val_t dest;
scc_lir_val_t value;
scc_lir_val_t size;
} memset;
} metadata;
} scc_lir_instr_t;
@@ -259,17 +265,15 @@ typedef struct scc_lir_func_meta {
typedef scc_cfg_symbol_id_t scc_lir_symbol_id_t;
typedef scc_cfg_symbol_t scc_lir_symbol_t;
typedef struct scc_lir_symbol_meta {
union {
struct {
scc_lir_func_t *func; // 指向函数体(若为定义)
} func;
struct {
u8 *init_data; // 初始化数据(若为 NULL 则零初始化
usize size; // 数据大小(字节)
int align; // 对齐要求
} data;
};
typedef union scc_lir_symbol_meta {
struct {
scc_lir_func_t *func; // 指向函数体(若为定义)
} func;
struct {
u8 *init_data; // 初始化数据(若为 NULL 则零初始化)
usize size; // 数据大小(字节
int align; // 对齐要求
} data;
} scc_lir_symbol_meta_t;
#define SCC_LIR_SYMBOL_META(symbol) ((scc_lir_symbol_meta_t *)(symbol)->meta)