From 1b9a7a48275783e0788eb977cbdaff93a3a282fc Mon Sep 17 00:00:00 2001 From: Lixuanwang Date: Mon, 4 Aug 2025 01:57:34 +0800 Subject: [PATCH] =?UTF-8?q?[backend]=E4=BF=AE=E5=A4=8D=E4=BA=86=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=BA=8F=E8=A8=80=E5=A4=84=E7=90=86=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E4=B8=94=E4=B8=8E=E5=B0=BE=E5=A3=B0=E6=A0=88=E4=B8=8D=E5=8C=B9?= =?UTF-8?q?=E9=85=8D=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RISCv64/Handler/PrologueEpilogueInsertion.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/backend/RISCv64/Handler/PrologueEpilogueInsertion.cpp b/src/backend/RISCv64/Handler/PrologueEpilogueInsertion.cpp index 0eef863..ab91660 100644 --- a/src/backend/RISCv64/Handler/PrologueEpilogueInsertion.cpp +++ b/src/backend/RISCv64/Handler/PrologueEpilogueInsertion.cpp @@ -95,16 +95,17 @@ void PrologueEpilogueInsertionPass::runOnMachineFunction(MachineFunction* mfunc) // 4.4. 保存所有使用到的被调用者保存寄存器 int next_available_offset = -(16 + frame_info.locals_size + frame_info.spill_size); for (const auto& reg : frame_info.callee_saved_regs_to_store) { - // 采用“先使用,后更新”逻辑 + // 改为“先更新,后使用”逻辑 + next_available_offset -= 8; // 先为当前寄存器分配下一个可用槽位 RVOpcodes store_op = isFPR(reg) ? RVOpcodes::FSD : RVOpcodes::SD; auto save_cs_reg = std::make_unique(store_op); save_cs_reg->addOperand(std::make_unique(reg)); save_cs_reg->addOperand(std::make_unique( std::make_unique(PhysicalReg::S0), - std::make_unique(next_available_offset) // 使用当前偏移 + std::make_unique(next_available_offset) // 使用新计算出的正确偏移 )); prologue_instrs.push_back(std::move(save_cs_reg)); - next_available_offset -= 8; // 为下一个寄存器准备偏移 + // 不再需要在循环末尾递减 } // 4.5. 将所有生成的序言指令一次性插入到函数入口 @@ -121,6 +122,7 @@ void PrologueEpilogueInsertionPass::runOnMachineFunction(MachineFunction* mfunc) // 5.1. 恢复被调用者保存寄存器 int next_available_offset_restore = -(16 + frame_info.locals_size + frame_info.spill_size); for (const auto& reg : frame_info.callee_saved_regs_to_store) { + next_available_offset_restore -= 8; // 为下一个寄存器准备偏移 RVOpcodes load_op = isFPR(reg) ? RVOpcodes::FLD : RVOpcodes::LD; auto restore_cs_reg = std::make_unique(load_op); restore_cs_reg->addOperand(std::make_unique(reg)); @@ -129,7 +131,6 @@ void PrologueEpilogueInsertionPass::runOnMachineFunction(MachineFunction* mfunc) std::make_unique(next_available_offset_restore) // 使用当前偏移 )); epilogue_instrs.push_back(std::move(restore_cs_reg)); - next_available_offset_restore -= 8; // 为下一个寄存器准备偏移 } // 5.2. 恢复 ra 和 s0