[midend][backend-GEP]解决了一个32/64位宽的错误问题

This commit is contained in:
Lixuanwang
2025-07-25 22:23:26 +08:00
parent 04c5c6b44d
commit 14fb3dbe48
6 changed files with 272 additions and 77 deletions

View File

@@ -31,6 +31,8 @@ void RISCv64AsmPrinter::run(std::ostream& os, bool debug) {
}
}
// 在 RISCv64AsmPrinter.cpp 文件中
void RISCv64AsmPrinter::printPrologue() {
StackFrameInfo& frame_info = MFunc->getFrameInfo();
// 序言需要为保存ra和s0预留16字节
@@ -44,12 +46,16 @@ void RISCv64AsmPrinter::printPrologue() {
*OS << " sd s0, " << (aligned_stack_size - 16) << "(sp)\n";
*OS << " addi s0, sp, " << aligned_stack_size << "\n";
}
// 忠实还原保存函数入口参数的逻辑
// 为函数参数分配寄存器
Function* F = MFunc->getFunc();
if (F && F->getEntryBlock()) {
int arg_idx = 0;
RISCv64ISel* isel = MFunc->getISel();
// 获取函数所有参数的类型列表
auto param_types = F->getParamTypes();
for (AllocaInst* alloca_for_param : F->getEntryBlock()->getArguments()) {
if (arg_idx >= 8) break;
@@ -57,7 +63,25 @@ void RISCv64AsmPrinter::printPrologue() {
if (frame_info.alloca_offsets.count(vreg)) {
int offset = frame_info.alloca_offsets.at(vreg);
auto arg_reg = static_cast<PhysicalReg>(static_cast<int>(PhysicalReg::A0) + arg_idx);
*OS << " sw " << regToString(arg_reg) << ", " << offset << "(s0)\n";
// 1. 获取当前参数的真实类型
// 注意F->getParamTypes() 返回的是一个 range-based view需要转换为vector或直接使用
Type* current_param_type = nullptr;
int temp_idx = 0;
for(auto p_type : param_types) {
if (temp_idx == arg_idx) {
current_param_type = p_type;
break;
}
temp_idx++;
}
assert(current_param_type && "Could not find parameter type.");
// 2. 根据类型决定使用 "sw" 还是 "sd"
const char* store_op = current_param_type->isPointer() ? "sd" : "sw";
// 3. 打印正确的存储指令
*OS << " " << store_op << " " << regToString(arg_reg) << ", " << offset << "(s0)\n";
}
arg_idx++;
}
@@ -133,17 +157,23 @@ void RISCv64AsmPrinter::printInstruction(MachineInstr* instr, bool debug) {
case RVOpcodes::SNEZ: *OS << "snez "; break;
case RVOpcodes::CALL: *OS << "call "; break;
case RVOpcodes::LABEL:
// printOperand(instr->getOperands()[0].get());
// *OS << ":";
break;
case RVOpcodes::FRAME_LOAD:
case RVOpcodes::FRAME_LOAD_W:
// It should have been eliminated by RegAlloc
if (!debug) throw std::runtime_error("FRAME pseudo-instruction not eliminated before AsmPrinter");
*OS << "frame_load "; break;
case RVOpcodes::FRAME_STORE:
*OS << "frame_load_w "; break;
case RVOpcodes::FRAME_LOAD_D:
// It should have been eliminated by RegAlloc
if (!debug) throw std::runtime_error("FRAME pseudo-instruction not eliminated before AsmPrinter");
*OS << "frame_store "; break;
*OS << "frame_load_d "; break;
case RVOpcodes::FRAME_STORE_W:
// It should have been eliminated by RegAlloc
if (!debug) throw std::runtime_error("FRAME pseudo-instruction not eliminated before AsmPrinter");
*OS << "frame_store_w "; break;
case RVOpcodes::FRAME_STORE_D:
// It should have been eliminated by RegAlloc
if (!debug) throw std::runtime_error("FRAME pseudo-instruction not eliminated before AsmPrinter");
*OS << "frame_store_d "; break;
case RVOpcodes::FRAME_ADDR:
// It should have been eliminated by RegAlloc
if (!debug) throw std::runtime_error("FRAME pseudo-instruction not eliminated before AsmPrinter");