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

@@ -9,6 +9,12 @@ typedef enum {
SCC_REG_ALLOC_OP_ACCESS_READWRITE = 2,
} scc_reg_op_access_t;
typedef struct {
const scc_mir_instr_t *instr;
int op_idx;
int op_sub_idx;
} scc_reg_alloc_iter_t;
// 后端回调表 —— 框架通过回调获取/修改指令,不感知具体布局
typedef struct scc_reg_alloc_op {
// ── 寄存器池 ──
@@ -18,25 +24,21 @@ typedef struct scc_reg_alloc_op {
void (*clean_mark_regs)(void *ctx);
// ── 指令信息 ──
int (*instr_opcode)(const void *instr);
int (*instr_num_operands)(const void *instr);
bool (*op_is_vreg)(const void *instr, int idx);
int (*op_get_vreg)(const void *instr, int idx);
void (*op_set_preg)(void *instr, int idx, int preg);
void (*op_set_slot)(void *instr, int idx, int slot);
scc_reg_alloc_iter_t iter;
void (*alloc_iter_begin)(scc_reg_alloc_iter_t *iter);
cbool (*alloc_iter_next)(scc_reg_alloc_iter_t *iter, int *out_vreg,
int *out_size, scc_reg_op_access_t *out_access);
void (*alloc_iter_replace_preg)(scc_reg_alloc_iter_t *iter, int preg);
void (*alloc_iter_replace_slot)(scc_reg_alloc_iter_t *iter, int slot);
void (*alloc_iter_end)(scc_reg_alloc_iter_t *iter);
// 读写属性与隐式寄存器
scc_reg_op_access_t (*get_operand_access)(void *ctx, int opcode,
int op_idx);
void (*get_implicit_regs)(void *ctx, int opcode, const int **out_uses,
const int **out_defs);
// ── 伪指令处理 ──
bool (*is_pseudo)(const void *instr);
void (*handle_pseudo)(scc_mir_func_t *func, void *instr, void *out);
// ── 溢出/重载(写入 out 向量) ──
void (*emit_spill)(void *out, int preg, int slot);
void (*emit_reload)(void *out, int preg, int slot);
void (*emit_spill)(void *out, int preg, int slot, int size);
void (*emit_reload)(void *out, int preg, int slot, int size);
void (*emit_copy)(void *out, int dst_preg, int src_preg, int size);
} scc_reg_alloc_op_t;