- 添加 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 等
85 lines
2.9 KiB
C
85 lines
2.9 KiB
C
#include <arch/scc_x86_mir.h>
|
|
#include <arch/scc_x86_reg_alloc.h>
|
|
#include <target/scc_win64.h>
|
|
|
|
// #include <core_pass/scc_frame_layout.h>
|
|
// #include <core_pass/scc_prolog_epilog.h>
|
|
// #include <core_pass/scc_reg_alloc.h>
|
|
|
|
#include <scc_mir_module.h>
|
|
#include <scc_mir_pass.h>
|
|
|
|
void scc_frame_layout(scc_frame_layout_t *ctx, scc_mir_module_t *module) {
|
|
Assert(ctx && ctx->impl_fn && module);
|
|
scc_vec_foreach(module->cfg_module.funcs, i) {
|
|
if (i == 0)
|
|
continue;
|
|
ctx->impl_fn(ctx, module, &scc_vec_at(module->cfg_module.funcs, i));
|
|
}
|
|
}
|
|
|
|
void scc_prolog_epilog(scc_prolog_epilog_t *ctx, scc_mir_module_t *module) {
|
|
Assert(ctx && ctx->epilog && ctx->prolog && module);
|
|
scc_vec_foreach(module->cfg_module.funcs, i) {
|
|
if (i == 0)
|
|
continue;
|
|
scc_mir_func_t *func = &scc_vec_at(module->cfg_module.funcs, i);
|
|
scc_vec_foreach(func->bblocks, i) {
|
|
scc_cfg_bblock_id_t bb_id = scc_vec_at(func->bblocks, i);
|
|
scc_cfg_bblock_t *bb =
|
|
scc_cfg_module_unsafe_get_bblock(&module->cfg_module, bb_id);
|
|
Assert(bb != nullptr);
|
|
scc_mir_instr_vec_t *old_instrs = SCC_MIR_BBLOCK_VALUES_PTR(bb);
|
|
scc_mir_instr_vec_t new_instrs;
|
|
scc_vec_init(new_instrs);
|
|
|
|
if (i == 0) {
|
|
ctx->prolog(&new_instrs, func);
|
|
}
|
|
|
|
scc_vec_foreach(*old_instrs, i) {
|
|
scc_mir_instr_t *ins =
|
|
scc_vec_sized_at_ptr(*old_instrs, module->instr_size, i);
|
|
if (ins->opcode < 0) {
|
|
continue;
|
|
}
|
|
if (ctx->need_epilog(ins)) {
|
|
ctx->epilog(&new_instrs, func);
|
|
}
|
|
scc_vec_sized_push(new_instrs, module->instr_size, ins,
|
|
module->instr_size);
|
|
}
|
|
scc_vec_free(*old_instrs);
|
|
*old_instrs = new_instrs;
|
|
}
|
|
}
|
|
}
|
|
|
|
void scc_mir_pass(scc_mir_module_t *mir_module, scc_mir_pass_stage_t stage) {
|
|
scc_reg_alloc_ctx_t reg_alloc_ctx = {.func = nullptr, .ops = {0}};
|
|
scc_reg_alloc_fill_arch_x86(®_alloc_ctx.ops);
|
|
scc_win_pc_x64_reg_alloc_fill(®_alloc_ctx.ops);
|
|
|
|
scc_reg_alloc(®_alloc_ctx, mir_module);
|
|
if (stage == SCC_MIR_STAGE_REGALLOC) {
|
|
return;
|
|
}
|
|
|
|
void scc_x86_peephole_optimize(scc_mir_module_t * module);
|
|
// scc_x86_peephole_optimize(mir_module);
|
|
|
|
scc_frame_layout_t frame_layout_ctx = {0};
|
|
scc_win_pc_x64_frame_alloc_init(&frame_layout_ctx);
|
|
scc_frame_layout(&frame_layout_ctx, mir_module);
|
|
if (stage == SCC_MIR_STAGE_FRAME_LAYOUT) {
|
|
return;
|
|
}
|
|
|
|
scc_prolog_epilog_t prolog_epilog_ctx = {0};
|
|
scc_win_pc_x64_prolog_epilog_init(&prolog_epilog_ctx);
|
|
scc_prolog_epilog(&prolog_epilog_ctx, mir_module);
|
|
if (stage == SCC_MIR_STAGE_PROLOGUE_EPILOGUE) {
|
|
return;
|
|
}
|
|
}
|