[backend]修复了多参数传递的问题
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
#include "CalleeSavedHandler.h"
|
||||
#include <set>
|
||||
#include <vector> //
|
||||
#include <algorithm>
|
||||
#include <iterator> //
|
||||
|
||||
namespace sysy {
|
||||
|
||||
@@ -77,9 +79,10 @@ void CalleeSavedHandler::runOnMachineFunction(MachineFunction* mfunc) {
|
||||
std::sort(sorted_int_regs.begin(), sorted_int_regs.end());
|
||||
std::sort(sorted_fp_regs.begin(), sorted_fp_regs.end());
|
||||
|
||||
std::vector<std::unique_ptr<MachineInstr>> save_instrs;
|
||||
int current_offset = -16; // ra和s0已占用-8和-16,从-24开始分配
|
||||
|
||||
// 插入整数保存指令 (sd)
|
||||
// 准备整数保存指令 (sd)
|
||||
for (PhysicalReg reg : sorted_int_regs) {
|
||||
current_offset -= 8;
|
||||
auto sd = std::make_unique<MachineInstr>(RVOpcodes::SD);
|
||||
@@ -88,10 +91,10 @@ void CalleeSavedHandler::runOnMachineFunction(MachineFunction* mfunc) {
|
||||
std::make_unique<RegOperand>(PhysicalReg::S0), // 基址为帧指针 s0
|
||||
std::make_unique<ImmOperand>(current_offset)
|
||||
));
|
||||
entry_instrs.insert(insert_pos, std::move(sd));
|
||||
save_instrs.push_back(std::move(sd));
|
||||
}
|
||||
|
||||
// 插入浮点保存指令 (fsd)
|
||||
// 准备浮点保存指令 (fsd)
|
||||
for (PhysicalReg reg : sorted_fp_regs) {
|
||||
current_offset -= 8;
|
||||
auto fsd = std::make_unique<MachineInstr>(RVOpcodes::FSD); // 使用浮点保存指令
|
||||
@@ -100,16 +103,24 @@ void CalleeSavedHandler::runOnMachineFunction(MachineFunction* mfunc) {
|
||||
std::make_unique<RegOperand>(PhysicalReg::S0),
|
||||
std::make_unique<ImmOperand>(current_offset)
|
||||
));
|
||||
entry_instrs.insert(insert_pos, std::move(fsd));
|
||||
save_instrs.push_back(std::move(fsd));
|
||||
}
|
||||
|
||||
// 一次性插入所有保存指令
|
||||
if (!save_instrs.empty()) {
|
||||
entry_instrs.insert(insert_pos,
|
||||
std::make_move_iterator(save_instrs.begin()),
|
||||
std::make_move_iterator(save_instrs.end()));
|
||||
}
|
||||
|
||||
// 4. 在函数结尾(ret之前)插入恢复指令
|
||||
for (auto& mbb : mfunc->getBlocks()) {
|
||||
for (auto it = mbb->getInstructions().begin(); it != mbb->getInstructions().end(); ++it) {
|
||||
if ((*it)->getOpcode() == RVOpcodes::RET) {
|
||||
std::vector<std::unique_ptr<MachineInstr>> restore_instrs;
|
||||
current_offset = -16; // 重置偏移量用于恢复
|
||||
|
||||
// 恢复整数寄存器 (ld) - 以与保存时相同的顺序
|
||||
// 准备恢复整数寄存器 (ld) - 以与保存时相同的顺序
|
||||
for (PhysicalReg reg : sorted_int_regs) {
|
||||
current_offset -= 8;
|
||||
auto ld = std::make_unique<MachineInstr>(RVOpcodes::LD);
|
||||
@@ -118,10 +129,10 @@ void CalleeSavedHandler::runOnMachineFunction(MachineFunction* mfunc) {
|
||||
std::make_unique<RegOperand>(PhysicalReg::S0),
|
||||
std::make_unique<ImmOperand>(current_offset)
|
||||
));
|
||||
mbb->getInstructions().insert(it, std::move(ld));
|
||||
restore_instrs.push_back(std::move(ld));
|
||||
}
|
||||
|
||||
// 恢复浮点寄存器 (fld)
|
||||
// 准备恢复浮点寄存器 (fld)
|
||||
for (PhysicalReg reg : sorted_fp_regs) {
|
||||
current_offset -= 8;
|
||||
auto fld = std::make_unique<MachineInstr>(RVOpcodes::FLD); // 使用浮点加载指令
|
||||
@@ -130,7 +141,14 @@ void CalleeSavedHandler::runOnMachineFunction(MachineFunction* mfunc) {
|
||||
std::make_unique<RegOperand>(PhysicalReg::S0),
|
||||
std::make_unique<ImmOperand>(current_offset)
|
||||
));
|
||||
mbb->getInstructions().insert(it, std::move(fld));
|
||||
restore_instrs.push_back(std::move(fld));
|
||||
}
|
||||
|
||||
// 一次性插入所有恢复指令
|
||||
if (!restore_instrs.empty()) {
|
||||
mbb->getInstructions().insert(it,
|
||||
std::make_move_iterator(restore_instrs.begin()),
|
||||
std::make_move_iterator(restore_instrs.end()));
|
||||
}
|
||||
|
||||
// 处理完一个基本块的RET后,迭代器已失效,需跳出当前块的循环
|
||||
|
||||
Reference in New Issue
Block a user