[backend]重构了后端

This commit is contained in:
Lixuanwang
2025-07-19 16:06:35 +08:00
parent 75e61bf274
commit d4a6996d74
10 changed files with 1336 additions and 1564 deletions

246
src/RISCv64AsmPrinter.cpp Normal file
View File

@@ -0,0 +1,246 @@
#include "RISCv64AsmPrinter.h"
#include <stdexcept>
namespace sysy {
void RISCv64AsmPrinter::runOnMachineFunction(MachineFunction* mfunc, std::ostream& os) {
OS = &os;
// 打印函数声明和全局符号
*OS << ".text\n";
*OS << ".globl " << mfunc->getName() << "\n";
*OS << mfunc->getName() << ":\n";
// 打印函数序言
printPrologue(mfunc);
// 遍历并打印所有基本块
for (auto& mbb : mfunc->getBlocks()) {
printBasicBlock(mbb.get());
}
}
void RISCv64AsmPrinter::printPrologue(MachineFunction* mfunc) {
int stack_size = mfunc->getFrameInfo().frame_size;
// 确保栈大小是16字节对齐
int aligned_stack_size = (stack_size + 15) & ~15;
if (aligned_stack_size > 0) {
*OS << " addi sp, sp, -" << aligned_stack_size << "\n";
// RV64中ra和s0都是8字节
*OS << " sd ra, " << (aligned_stack_size - 8) << "(sp)\n";
*OS << " sd s0, " << (aligned_stack_size - 16) << "(sp)\n";
*OS << " mv s0, sp\n";
}
}
void RISCv64AsmPrinter::printEpilogue(MachineFunction* mfunc) {
int stack_size = mfunc->getFrameInfo().frame_size;
int aligned_stack_size = (stack_size + 15) & ~15;
if (aligned_stack_size > 0) {
*OS << " ld ra, " << (aligned_stack_size - 8) << "(sp)\n";
*OS << " ld s0, " << (aligned_stack_size - 16) << "(sp)\n";
*OS << " addi sp, sp, " << aligned_stack_size << "\n";
}
}
void RISCv64AsmPrinter::printBasicBlock(MachineBasicBlock* mbb) {
// 打印基本块标签
if (!mbb->getName().empty()) {
*OS << mbb->getName() << ":\n";
}
// 打印指令
for (auto& instr : mbb->getInstructions()) {
printInstruction(instr.get(), mbb);
}
}
void RISCv64AsmPrinter::printInstruction(MachineInstr* instr, MachineBasicBlock* parent_bb) {
*OS << " "; // 指令缩进
auto opcode = instr->getOpcode();
// RET指令需要特殊处理在打印ret之前先打印函数尾声
if (opcode == RVOpcodes::RET) {
printEpilogue(parent_bb->getParent());
}
// 使用switch将Opcode转换为汇编助记符
switch (opcode) {
// Arithmatic
case RVOpcodes::ADD: *OS << "add "; break;
case RVOpcodes::ADDI: *OS << "addi "; break;
case RVOpcodes::ADDW: *OS << "addw "; break;
case RVOpcodes::ADDIW: *OS << "addiw "; break;
case RVOpcodes::SUB: *OS << "sub "; break;
case RVOpcodes::SUBW: *OS << "subw "; break;
case RVOpcodes::MUL: *OS << "mul "; break;
case RVOpcodes::MULW: *OS << "mulw "; break;
case RVOpcodes::DIV: *OS << "div "; break;
case RVOpcodes::DIVW: *OS << "divw "; break;
case RVOpcodes::REM: *OS << "rem "; break;
case RVOpcodes::REMW: *OS << "remw "; break;
// Logical
case RVOpcodes::XOR: *OS << "xor "; break;
case RVOpcodes::XORI: *OS << "xori "; break;
case RVOpcodes::OR: *OS << "or "; break;
case RVOpcodes::ORI: *OS << "ori "; break;
case RVOpcodes::AND: *OS << "and "; break;
case RVOpcodes::ANDI: *OS << "andi "; break;
// Shift
case RVOpcodes::SLL: *OS << "sll "; break;
case RVOpcodes::SLLI: *OS << "slli "; break;
case RVOpcodes::SLLW: *OS << "sllw "; break;
case RVOpcodes::SLLIW: *OS << "slliw "; break;
case RVOpcodes::SRL: *OS << "srl "; break;
case RVOpcodes::SRLI: *OS << "srli "; break;
case RVOpcodes::SRLW: *OS << "srlw "; break;
case RVOpcodes::SRLIW: *OS << "srliw "; break;
case RVOpcodes::SRA: *OS << "sra "; break;
case RVOpcodes::SRAI: *OS << "srai "; break;
case RVOpcodes::SRAW: *OS << "sraw "; break;
case RVOpcodes::SRAIW: *OS << "sraiw "; break;
// Compare
case RVOpcodes::SLT: *OS << "slt "; break;
case RVOpcodes::SLTI: *OS << "slti "; break;
case RVOpcodes::SLTU: *OS << "sltu "; break;
case RVOpcodes::SLTIU: *OS << "sltiu "; break;
// Memory
case RVOpcodes::LW: *OS << "lw "; break;
case RVOpcodes::LH: *OS << "lh "; break;
case RVOpcodes::LB: *OS << "lb "; break;
case RVOpcodes::LWU: *OS << "lwu "; break;
case RVOpcodes::LHU: *OS << "lhu "; break;
case RVOpcodes::LBU: *OS << "lbu "; break;
case RVOpcodes::SW: *OS << "sw "; break;
case RVOpcodes::SH: *OS << "sh "; break;
case RVOpcodes::SB: *OS << "sb "; break;
case RVOpcodes::LD: *OS << "ld "; break;
case RVOpcodes::SD: *OS << "sd "; break;
// Control Flow
case RVOpcodes::J: *OS << "j "; break;
case RVOpcodes::JAL: *OS << "jal "; break;
case RVOpcodes::JALR: *OS << "jalr "; break;
case RVOpcodes::RET: *OS << "ret"; break;
case RVOpcodes::BEQ: *OS << "beq "; break;
case RVOpcodes::BNE: *OS << "bne "; break;
case RVOpcodes::BLT: *OS << "blt "; break;
case RVOpcodes::BGE: *OS << "bge "; break;
case RVOpcodes::BLTU: *OS << "bltu "; break;
case RVOpcodes::BGEU: *OS << "bgeu "; break;
// Pseudo-Instructions
case RVOpcodes::LI: *OS << "li "; break;
case RVOpcodes::LA: *OS << "la "; break;
case RVOpcodes::MV: *OS << "mv "; break;
case RVOpcodes::NEG: *OS << "neg "; break;
case RVOpcodes::NEGW: *OS << "negw "; break;
case RVOpcodes::SEQZ: *OS << "seqz "; break;
case RVOpcodes::SNEZ: *OS << "snez "; break;
// Call
case RVOpcodes::CALL: *OS << "call "; break;
// Special
case RVOpcodes::LABEL:
*OS << "\b\b\b\b";
printOperand(instr->getOperands()[0].get());
*OS << ":";
break;
default:
throw std::runtime_error("Unknown opcode in AsmPrinter");
}
// 打印操作数
const auto& operands = instr->getOperands();
for (size_t i = 0; i < operands.size(); ++i) {
// 对于LW/SW, 操作数格式是 rd, offset(rs1)
if (opcode == RVOpcodes::LW || opcode == RVOpcodes::SW || opcode == RVOpcodes::LD || opcode == RVOpcodes::SD) {
printOperand(operands[0].get());
*OS << ", ";
printOperand(operands[1].get());
break; // LW/SW只有两个操作数部分
}
printOperand(operands[i].get());
if (i < operands.size() - 1) {
*OS << ", ";
}
}
*OS << "\n";
}
void RISCv64AsmPrinter::printOperand(MachineOperand* op) {
if (!op) return;
switch(op->getKind()) {
case MachineOperand::KIND_REG: {
auto reg_op = static_cast<RegOperand*>(op);
if (reg_op->isVirtual()) {
// 在这个阶段不应该再有虚拟寄存器了
*OS << "%vreg" << reg_op->getVRegNum();
} else {
*OS << regToString(reg_op->getPReg());
}
break;
}
case MachineOperand::KIND_IMM: {
*OS << static_cast<ImmOperand*>(op)->getValue();
break;
}
case MachineOperand::KIND_LABEL: {
*OS << static_cast<LabelOperand*>(op)->getName();
break;
}
case MachineOperand::KIND_MEM: {
auto mem_op = static_cast<MemOperand*>(op);
printOperand(mem_op->getOffset());
*OS << "(";
printOperand(mem_op->getBase());
*OS << ")";
break;
}
}
}
// 物理寄存器到字符串的转换 (从原RISCv64Backend.cpp迁移)
std::string RISCv64AsmPrinter::regToString(PhysicalReg reg) {
switch (reg) {
case PhysicalReg::ZERO: return "x0";
case PhysicalReg::RA: return "ra";
case PhysicalReg::SP: return "sp";
case PhysicalReg::GP: return "gp";
case PhysicalReg::TP: return "tp";
case PhysicalReg::T0: return "t0";
case PhysicalReg::T1: return "t1";
case PhysicalReg::T2: return "t2";
case PhysicalReg::S0: return "s0";
case PhysicalReg::S1: return "s1";
case PhysicalReg::A0: return "a0";
case PhysicalReg::A1: return "a1";
case PhysicalReg::A2: return "a2";
case PhysicalReg::A3: return "a3";
case PhysicalReg::A4: return "a4";
case PhysicalReg::A5: return "a5";
case PhysicalReg::A6: return "a6";
case PhysicalReg::A7: return "a7";
case PhysicalReg::S2: return "s2";
case PhysicalReg::S3: return "s3";
case PhysicalReg::S4: return "s4";
case PhysicalReg::S5: return "s5";
case PhysicalReg::S6: return "s6";
case PhysicalReg::S7: return "s7";
case PhysicalReg::S8: return "s8";
case PhysicalReg::S9: return "s9";
case PhysicalReg::S10: return "s10";
case PhysicalReg::S11: return "s11";
case PhysicalReg::T3: return "t3";
case PhysicalReg::T4: return "t4";
case PhysicalReg::T5: return "t5";
case PhysicalReg::T6: return "t6";
default: return "UNKNOWN_REG";
}
}
} // namespace sysy