[backend-IRC]修复了栈传递参数逻辑

This commit is contained in:
Lixuanwang
2025-08-01 05:21:37 +08:00
parent 873dbf64d0
commit 166d0fc372
2 changed files with 181 additions and 202 deletions

View File

@@ -4,6 +4,7 @@
namespace sysy {
// getTypeSizeInBytes 是一个通用辅助函数,保持不变
unsigned EliminateFrameIndicesPass::getTypeSizeInBytes(Type* type) {
if (!type) {
assert(false && "Cannot get size of a null type.");
@@ -31,25 +32,14 @@ void EliminateFrameIndicesPass::runOnMachineFunction(MachineFunction* mfunc) {
Function* F = mfunc->getFunc();
RISCv64ISel* isel = mfunc->getISel();
// 1. 栈传递的参数计算偏移量
if (F) {
int arg_idx = 0;
for (Argument* arg : F->getArguments()) {
// 只关心第8个索引及之后的参数 (即第9个参数开始)
if (arg_idx >= 8) {
// 第一个栈参数(idx=8)在0(s0), 第二个(idx=9)在8(s0)
int offset = (arg_idx - 8) * 8;
unsigned vreg = isel->getVReg(arg);
frame_info.alloca_offsets[vreg] = offset;
}
arg_idx++;
}
}
// 1. [已移除] 不再处理栈传递的参数
// 原先处理栈参数 (arg_idx >= 8) 的逻辑已被移除。
// 这项职责已完全转移到 PrologueEpilogueInsertionPass以避免逻辑冲突和错误。
// 2. 为局部变量分配空间,起始点在 [ra, s0] (16字节) 之后
// 2. 为局部变量(AllocaInst)分配空间和计算偏移量
// 局部变量从 s0 下方(负偏移量)开始分配,紧接着为 ra 和 s0 预留的16字节之后
int local_var_offset = 16;
// 处理局部变量 (AllocaInst)
if(F) { // 确保函数指针有效
for (auto& bb : F->getBasicBlocks()) {
for (auto& inst : bb->getInstructions()) {
@@ -73,7 +63,8 @@ void EliminateFrameIndicesPass::runOnMachineFunction(MachineFunction* mfunc) {
// 记录仅由AllocaInst分配的局部变量的总大小
frame_info.locals_size = local_var_offset - 16;
// 3. 遍历所有机器指令,将伪指令展开为真实指令
// 3. 遍历所有机器指令,将访问局部变量的伪指令展开为真实指令
// 由于处理参数的逻辑已移除,这里的展开现在只针对局部变量,因此是正确的。
for (auto& mbb : mfunc->getBlocks()) {
std::vector<std::unique_ptr<MachineInstr>> new_instructions;
for (auto& instr_ptr : mbb->getInstructions()) {