16122f3e6SDimitry Andric //===-- MipsInstPrinter.cpp - Convert Mips MCInst to assembly syntax ------===// 217a519f9SDimitry Andric // 317a519f9SDimitry Andric // The LLVM Compiler Infrastructure 417a519f9SDimitry Andric // 517a519f9SDimitry Andric // This file is distributed under the University of Illinois Open Source 617a519f9SDimitry Andric // License. See LICENSE.TXT for details. 717a519f9SDimitry Andric // 817a519f9SDimitry Andric //===----------------------------------------------------------------------===// 917a519f9SDimitry Andric // 1017a519f9SDimitry Andric // This class prints an Mips MCInst to a .s file. 1117a519f9SDimitry Andric // 1217a519f9SDimitry Andric //===----------------------------------------------------------------------===// 1317a519f9SDimitry Andric 1417a519f9SDimitry Andric #include "MipsInstPrinter.h" 1591bc56edSDimitry Andric #include "MCTargetDesc/MipsMCExpr.h" 167ae0e2c9SDimitry Andric #include "MipsInstrInfo.h" 17dff0c46cSDimitry Andric #include "llvm/ADT/StringExtras.h" 1817a519f9SDimitry Andric #include "llvm/MC/MCExpr.h" 1917a519f9SDimitry Andric #include "llvm/MC/MCInst.h" 20dff0c46cSDimitry Andric #include "llvm/MC/MCInstrInfo.h" 21dff0c46cSDimitry Andric #include "llvm/MC/MCSymbol.h" 2217a519f9SDimitry Andric #include "llvm/Support/ErrorHandling.h" 2317a519f9SDimitry Andric #include "llvm/Support/raw_ostream.h" 2417a519f9SDimitry Andric using namespace llvm; 2517a519f9SDimitry Andric 2691bc56edSDimitry Andric #define DEBUG_TYPE "asm-printer" 2791bc56edSDimitry Andric 28139f7f9bSDimitry Andric #define PRINT_ALIAS_INSTR 2917a519f9SDimitry Andric #include "MipsGenAsmWriter.inc" 3017a519f9SDimitry Andric 31f785676fSDimitry Andric template<unsigned R> 32f785676fSDimitry Andric static bool isReg(const MCInst &MI, unsigned OpNo) { 33f785676fSDimitry Andric assert(MI.getOperand(OpNo).isReg() && "Register operand expected."); 34f785676fSDimitry Andric return MI.getOperand(OpNo).getReg() == R; 35f785676fSDimitry Andric } 36f785676fSDimitry Andric 3717a519f9SDimitry Andric const char* Mips::MipsFCCToString(Mips::CondCode CC) { 3817a519f9SDimitry Andric switch (CC) { 3917a519f9SDimitry Andric case FCOND_F: 4017a519f9SDimitry Andric case FCOND_T: return "f"; 4117a519f9SDimitry Andric case FCOND_UN: 4217a519f9SDimitry Andric case FCOND_OR: return "un"; 4317a519f9SDimitry Andric case FCOND_OEQ: 4417a519f9SDimitry Andric case FCOND_UNE: return "eq"; 4517a519f9SDimitry Andric case FCOND_UEQ: 4617a519f9SDimitry Andric case FCOND_ONE: return "ueq"; 4717a519f9SDimitry Andric case FCOND_OLT: 4817a519f9SDimitry Andric case FCOND_UGE: return "olt"; 4917a519f9SDimitry Andric case FCOND_ULT: 5017a519f9SDimitry Andric case FCOND_OGE: return "ult"; 5117a519f9SDimitry Andric case FCOND_OLE: 5217a519f9SDimitry Andric case FCOND_UGT: return "ole"; 5317a519f9SDimitry Andric case FCOND_ULE: 5417a519f9SDimitry Andric case FCOND_OGT: return "ule"; 5517a519f9SDimitry Andric case FCOND_SF: 5617a519f9SDimitry Andric case FCOND_ST: return "sf"; 5717a519f9SDimitry Andric case FCOND_NGLE: 5817a519f9SDimitry Andric case FCOND_GLE: return "ngle"; 5917a519f9SDimitry Andric case FCOND_SEQ: 6017a519f9SDimitry Andric case FCOND_SNE: return "seq"; 6117a519f9SDimitry Andric case FCOND_NGL: 6217a519f9SDimitry Andric case FCOND_GL: return "ngl"; 6317a519f9SDimitry Andric case FCOND_LT: 6417a519f9SDimitry Andric case FCOND_NLT: return "lt"; 6517a519f9SDimitry Andric case FCOND_NGE: 6617a519f9SDimitry Andric case FCOND_GE: return "nge"; 6717a519f9SDimitry Andric case FCOND_LE: 6817a519f9SDimitry Andric case FCOND_NLE: return "le"; 6917a519f9SDimitry Andric case FCOND_NGT: 7017a519f9SDimitry Andric case FCOND_GT: return "ngt"; 7117a519f9SDimitry Andric } 7217a519f9SDimitry Andric llvm_unreachable("Impossible condition code!"); 7317a519f9SDimitry Andric } 7417a519f9SDimitry Andric 7517a519f9SDimitry Andric void MipsInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const { 76dff0c46cSDimitry Andric OS << '$' << StringRef(getRegisterName(RegNo)).lower(); 7717a519f9SDimitry Andric } 7817a519f9SDimitry Andric 796122f3e6SDimitry Andric void MipsInstPrinter::printInst(const MCInst *MI, raw_ostream &O, 806122f3e6SDimitry Andric StringRef Annot) { 817ae0e2c9SDimitry Andric switch (MI->getOpcode()) { 827ae0e2c9SDimitry Andric default: 837ae0e2c9SDimitry Andric break; 847ae0e2c9SDimitry Andric case Mips::RDHWR: 857ae0e2c9SDimitry Andric case Mips::RDHWR64: 867ae0e2c9SDimitry Andric O << "\t.set\tpush\n"; 877ae0e2c9SDimitry Andric O << "\t.set\tmips32r2\n"; 8891bc56edSDimitry Andric break; 8991bc56edSDimitry Andric case Mips::Save16: 9091bc56edSDimitry Andric O << "\tsave\t"; 9191bc56edSDimitry Andric printSaveRestore(MI, O); 9291bc56edSDimitry Andric O << " # 16 bit inst\n"; 9391bc56edSDimitry Andric return; 9491bc56edSDimitry Andric case Mips::SaveX16: 9591bc56edSDimitry Andric O << "\tsave\t"; 9691bc56edSDimitry Andric printSaveRestore(MI, O); 9791bc56edSDimitry Andric O << "\n"; 9891bc56edSDimitry Andric return; 9991bc56edSDimitry Andric case Mips::Restore16: 10091bc56edSDimitry Andric O << "\trestore\t"; 10191bc56edSDimitry Andric printSaveRestore(MI, O); 10291bc56edSDimitry Andric O << " # 16 bit inst\n"; 10391bc56edSDimitry Andric return; 10491bc56edSDimitry Andric case Mips::RestoreX16: 10591bc56edSDimitry Andric O << "\trestore\t"; 10691bc56edSDimitry Andric printSaveRestore(MI, O); 10791bc56edSDimitry Andric O << "\n"; 10891bc56edSDimitry Andric return; 1097ae0e2c9SDimitry Andric } 1107ae0e2c9SDimitry Andric 111139f7f9bSDimitry Andric // Try to print any aliases first. 112f785676fSDimitry Andric if (!printAliasInstr(MI, O) && !printAlias(*MI, O)) 11317a519f9SDimitry Andric printInstruction(MI, O); 1146122f3e6SDimitry Andric printAnnotation(O, Annot); 1157ae0e2c9SDimitry Andric 1167ae0e2c9SDimitry Andric switch (MI->getOpcode()) { 1177ae0e2c9SDimitry Andric default: 1187ae0e2c9SDimitry Andric break; 1197ae0e2c9SDimitry Andric case Mips::RDHWR: 1207ae0e2c9SDimitry Andric case Mips::RDHWR64: 1217ae0e2c9SDimitry Andric O << "\n\t.set\tpop"; 1227ae0e2c9SDimitry Andric } 12317a519f9SDimitry Andric } 12417a519f9SDimitry Andric 125dff0c46cSDimitry Andric static void printExpr(const MCExpr *Expr, raw_ostream &OS) { 126dff0c46cSDimitry Andric int Offset = 0; 127dff0c46cSDimitry Andric const MCSymbolRefExpr *SRE; 128dff0c46cSDimitry Andric 129dff0c46cSDimitry Andric if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) { 130dff0c46cSDimitry Andric SRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS()); 131dff0c46cSDimitry Andric const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(BE->getRHS()); 132dff0c46cSDimitry Andric assert(SRE && CE && "Binary expression must be sym+const."); 133dff0c46cSDimitry Andric Offset = CE->getValue(); 13491bc56edSDimitry Andric } else if (const MipsMCExpr *ME = dyn_cast<MipsMCExpr>(Expr)) { 13591bc56edSDimitry Andric ME->print(OS); 13691bc56edSDimitry Andric return; 13791bc56edSDimitry Andric } else if (!(SRE = dyn_cast<MCSymbolRefExpr>(Expr))) 138dff0c46cSDimitry Andric assert(false && "Unexpected MCExpr type."); 139dff0c46cSDimitry Andric 140dff0c46cSDimitry Andric MCSymbolRefExpr::VariantKind Kind = SRE->getKind(); 141dff0c46cSDimitry Andric 142dff0c46cSDimitry Andric switch (Kind) { 143dff0c46cSDimitry Andric default: llvm_unreachable("Invalid kind!"); 144dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_None: break; 145dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_GPREL: OS << "%gp_rel("; break; 146dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_GOT_CALL: OS << "%call16("; break; 147dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_GOT16: OS << "%got("; break; 148dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_GOT: OS << "%got("; break; 149dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_ABS_HI: OS << "%hi("; break; 150dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_ABS_LO: OS << "%lo("; break; 151dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_TLSGD: OS << "%tlsgd("; break; 152dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_TLSLDM: OS << "%tlsldm("; break; 153dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_DTPREL_HI: OS << "%dtprel_hi("; break; 154dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_DTPREL_LO: OS << "%dtprel_lo("; break; 155dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_GOTTPREL: OS << "%gottprel("; break; 156dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_TPREL_HI: OS << "%tprel_hi("; break; 157dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_TPREL_LO: OS << "%tprel_lo("; break; 158dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_GPOFF_HI: OS << "%hi(%neg(%gp_rel("; break; 159dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_GPOFF_LO: OS << "%lo(%neg(%gp_rel("; break; 160dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_GOT_DISP: OS << "%got_disp("; break; 161dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_GOT_PAGE: OS << "%got_page("; break; 162dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_GOT_OFST: OS << "%got_ofst("; break; 1637ae0e2c9SDimitry Andric case MCSymbolRefExpr::VK_Mips_HIGHER: OS << "%higher("; break; 1647ae0e2c9SDimitry Andric case MCSymbolRefExpr::VK_Mips_HIGHEST: OS << "%highest("; break; 165c80e6c4bSDimitry Andric case MCSymbolRefExpr::VK_Mips_GOT_HI16: OS << "%got_hi("; break; 166c80e6c4bSDimitry Andric case MCSymbolRefExpr::VK_Mips_GOT_LO16: OS << "%got_lo("; break; 167c80e6c4bSDimitry Andric case MCSymbolRefExpr::VK_Mips_CALL_HI16: OS << "%call_hi("; break; 168c80e6c4bSDimitry Andric case MCSymbolRefExpr::VK_Mips_CALL_LO16: OS << "%call_lo("; break; 16991bc56edSDimitry Andric case MCSymbolRefExpr::VK_Mips_PCREL_HI16: OS << "%pcrel_hi("; break; 17091bc56edSDimitry Andric case MCSymbolRefExpr::VK_Mips_PCREL_LO16: OS << "%pcrel_lo("; break; 171dff0c46cSDimitry Andric } 172dff0c46cSDimitry Andric 173dff0c46cSDimitry Andric OS << SRE->getSymbol(); 174dff0c46cSDimitry Andric 175dff0c46cSDimitry Andric if (Offset) { 176dff0c46cSDimitry Andric if (Offset > 0) 177dff0c46cSDimitry Andric OS << '+'; 178dff0c46cSDimitry Andric OS << Offset; 179dff0c46cSDimitry Andric } 180dff0c46cSDimitry Andric 181dff0c46cSDimitry Andric if ((Kind == MCSymbolRefExpr::VK_Mips_GPOFF_HI) || 182dff0c46cSDimitry Andric (Kind == MCSymbolRefExpr::VK_Mips_GPOFF_LO)) 183dff0c46cSDimitry Andric OS << ")))"; 184dff0c46cSDimitry Andric else if (Kind != MCSymbolRefExpr::VK_None) 185dff0c46cSDimitry Andric OS << ')'; 186dff0c46cSDimitry Andric } 187dff0c46cSDimitry Andric 18817a519f9SDimitry Andric void MipsInstPrinter::printOperand(const MCInst *MI, unsigned OpNo, 18917a519f9SDimitry Andric raw_ostream &O) { 19017a519f9SDimitry Andric const MCOperand &Op = MI->getOperand(OpNo); 19117a519f9SDimitry Andric if (Op.isReg()) { 19217a519f9SDimitry Andric printRegName(O, Op.getReg()); 19317a519f9SDimitry Andric return; 19417a519f9SDimitry Andric } 19517a519f9SDimitry Andric 19617a519f9SDimitry Andric if (Op.isImm()) { 19717a519f9SDimitry Andric O << Op.getImm(); 19817a519f9SDimitry Andric return; 19917a519f9SDimitry Andric } 20017a519f9SDimitry Andric 20117a519f9SDimitry Andric assert(Op.isExpr() && "unknown operand kind in printOperand"); 202dff0c46cSDimitry Andric printExpr(Op.getExpr(), O); 20317a519f9SDimitry Andric } 20417a519f9SDimitry Andric 20517a519f9SDimitry Andric void MipsInstPrinter::printUnsignedImm(const MCInst *MI, int opNum, 20617a519f9SDimitry Andric raw_ostream &O) { 20717a519f9SDimitry Andric const MCOperand &MO = MI->getOperand(opNum); 20817a519f9SDimitry Andric if (MO.isImm()) 20917a519f9SDimitry Andric O << (unsigned short int)MO.getImm(); 21017a519f9SDimitry Andric else 21117a519f9SDimitry Andric printOperand(MI, opNum, O); 21217a519f9SDimitry Andric } 21317a519f9SDimitry Andric 214f785676fSDimitry Andric void MipsInstPrinter::printUnsignedImm8(const MCInst *MI, int opNum, 215f785676fSDimitry Andric raw_ostream &O) { 216f785676fSDimitry Andric const MCOperand &MO = MI->getOperand(opNum); 217f785676fSDimitry Andric if (MO.isImm()) 218f785676fSDimitry Andric O << (unsigned short int)(unsigned char)MO.getImm(); 219f785676fSDimitry Andric else 220f785676fSDimitry Andric printOperand(MI, opNum, O); 221f785676fSDimitry Andric } 222f785676fSDimitry Andric 22317a519f9SDimitry Andric void MipsInstPrinter:: 22417a519f9SDimitry Andric printMemOperand(const MCInst *MI, int opNum, raw_ostream &O) { 22517a519f9SDimitry Andric // Load/Store memory operands -- imm($reg) 22617a519f9SDimitry Andric // If PIC target the target is loaded as the 22717a519f9SDimitry Andric // pattern lw $25,%call16($28) 22817a519f9SDimitry Andric printOperand(MI, opNum+1, O); 22917a519f9SDimitry Andric O << "("; 23017a519f9SDimitry Andric printOperand(MI, opNum, O); 23117a519f9SDimitry Andric O << ")"; 23217a519f9SDimitry Andric } 23317a519f9SDimitry Andric 23417a519f9SDimitry Andric void MipsInstPrinter:: 23517a519f9SDimitry Andric printMemOperandEA(const MCInst *MI, int opNum, raw_ostream &O) { 23617a519f9SDimitry Andric // when using stack locations for not load/store instructions 23717a519f9SDimitry Andric // print the same way as all normal 3 operand instructions. 23817a519f9SDimitry Andric printOperand(MI, opNum, O); 23917a519f9SDimitry Andric O << ", "; 24017a519f9SDimitry Andric printOperand(MI, opNum+1, O); 24117a519f9SDimitry Andric return; 24217a519f9SDimitry Andric } 24317a519f9SDimitry Andric 24417a519f9SDimitry Andric void MipsInstPrinter:: 24517a519f9SDimitry Andric printFCCOperand(const MCInst *MI, int opNum, raw_ostream &O) { 24617a519f9SDimitry Andric const MCOperand& MO = MI->getOperand(opNum); 24717a519f9SDimitry Andric O << MipsFCCToString((Mips::CondCode)MO.getImm()); 24817a519f9SDimitry Andric } 249f785676fSDimitry Andric 250f785676fSDimitry Andric void MipsInstPrinter:: 251f785676fSDimitry Andric printSHFMask(const MCInst *MI, int opNum, raw_ostream &O) { 252f785676fSDimitry Andric llvm_unreachable("TODO"); 253f785676fSDimitry Andric } 254f785676fSDimitry Andric 255f785676fSDimitry Andric bool MipsInstPrinter::printAlias(const char *Str, const MCInst &MI, 256f785676fSDimitry Andric unsigned OpNo, raw_ostream &OS) { 257f785676fSDimitry Andric OS << "\t" << Str << "\t"; 258f785676fSDimitry Andric printOperand(&MI, OpNo, OS); 259f785676fSDimitry Andric return true; 260f785676fSDimitry Andric } 261f785676fSDimitry Andric 262f785676fSDimitry Andric bool MipsInstPrinter::printAlias(const char *Str, const MCInst &MI, 263f785676fSDimitry Andric unsigned OpNo0, unsigned OpNo1, 264f785676fSDimitry Andric raw_ostream &OS) { 265f785676fSDimitry Andric printAlias(Str, MI, OpNo0, OS); 266f785676fSDimitry Andric OS << ", "; 267f785676fSDimitry Andric printOperand(&MI, OpNo1, OS); 268f785676fSDimitry Andric return true; 269f785676fSDimitry Andric } 270f785676fSDimitry Andric 271f785676fSDimitry Andric bool MipsInstPrinter::printAlias(const MCInst &MI, raw_ostream &OS) { 272f785676fSDimitry Andric switch (MI.getOpcode()) { 273f785676fSDimitry Andric case Mips::BEQ: 274f785676fSDimitry Andric // beq $zero, $zero, $L2 => b $L2 275f785676fSDimitry Andric // beq $r0, $zero, $L2 => beqz $r0, $L2 276f785676fSDimitry Andric return (isReg<Mips::ZERO>(MI, 0) && isReg<Mips::ZERO>(MI, 1) && 277f785676fSDimitry Andric printAlias("b", MI, 2, OS)) || 278f785676fSDimitry Andric (isReg<Mips::ZERO>(MI, 1) && printAlias("beqz", MI, 0, 2, OS)); 279f785676fSDimitry Andric case Mips::BEQ64: 280f785676fSDimitry Andric // beq $r0, $zero, $L2 => beqz $r0, $L2 281f785676fSDimitry Andric return isReg<Mips::ZERO_64>(MI, 1) && printAlias("beqz", MI, 0, 2, OS); 282f785676fSDimitry Andric case Mips::BNE: 283f785676fSDimitry Andric // bne $r0, $zero, $L2 => bnez $r0, $L2 284f785676fSDimitry Andric return isReg<Mips::ZERO>(MI, 1) && printAlias("bnez", MI, 0, 2, OS); 285f785676fSDimitry Andric case Mips::BNE64: 286f785676fSDimitry Andric // bne $r0, $zero, $L2 => bnez $r0, $L2 287f785676fSDimitry Andric return isReg<Mips::ZERO_64>(MI, 1) && printAlias("bnez", MI, 0, 2, OS); 288f785676fSDimitry Andric case Mips::BGEZAL: 289f785676fSDimitry Andric // bgezal $zero, $L1 => bal $L1 290f785676fSDimitry Andric return isReg<Mips::ZERO>(MI, 0) && printAlias("bal", MI, 1, OS); 291f785676fSDimitry Andric case Mips::BC1T: 292f785676fSDimitry Andric // bc1t $fcc0, $L1 => bc1t $L1 293f785676fSDimitry Andric return isReg<Mips::FCC0>(MI, 0) && printAlias("bc1t", MI, 1, OS); 294f785676fSDimitry Andric case Mips::BC1F: 295f785676fSDimitry Andric // bc1f $fcc0, $L1 => bc1f $L1 296f785676fSDimitry Andric return isReg<Mips::FCC0>(MI, 0) && printAlias("bc1f", MI, 1, OS); 297f785676fSDimitry Andric case Mips::JALR: 298f785676fSDimitry Andric // jalr $ra, $r1 => jalr $r1 299f785676fSDimitry Andric return isReg<Mips::RA>(MI, 0) && printAlias("jalr", MI, 1, OS); 300f785676fSDimitry Andric case Mips::JALR64: 301f785676fSDimitry Andric // jalr $ra, $r1 => jalr $r1 302f785676fSDimitry Andric return isReg<Mips::RA_64>(MI, 0) && printAlias("jalr", MI, 1, OS); 303f785676fSDimitry Andric case Mips::NOR: 304f785676fSDimitry Andric case Mips::NOR_MM: 305f785676fSDimitry Andric // nor $r0, $r1, $zero => not $r0, $r1 306f785676fSDimitry Andric return isReg<Mips::ZERO>(MI, 2) && printAlias("not", MI, 0, 1, OS); 307f785676fSDimitry Andric case Mips::NOR64: 308f785676fSDimitry Andric // nor $r0, $r1, $zero => not $r0, $r1 309f785676fSDimitry Andric return isReg<Mips::ZERO_64>(MI, 2) && printAlias("not", MI, 0, 1, OS); 310f785676fSDimitry Andric case Mips::OR: 311f785676fSDimitry Andric // or $r0, $r1, $zero => move $r0, $r1 312f785676fSDimitry Andric return isReg<Mips::ZERO>(MI, 2) && printAlias("move", MI, 0, 1, OS); 313f785676fSDimitry Andric default: return false; 314f785676fSDimitry Andric } 315f785676fSDimitry Andric } 31691bc56edSDimitry Andric 31791bc56edSDimitry Andric void MipsInstPrinter::printSaveRestore(const MCInst *MI, raw_ostream &O) { 31891bc56edSDimitry Andric for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 31991bc56edSDimitry Andric if (i != 0) O << ", "; 32091bc56edSDimitry Andric if (MI->getOperand(i).isReg()) 32191bc56edSDimitry Andric printRegName(O, MI->getOperand(i).getReg()); 32291bc56edSDimitry Andric else 32391bc56edSDimitry Andric printUnsignedImm(MI, i, O); 32491bc56edSDimitry Andric } 32591bc56edSDimitry Andric } 32691bc56edSDimitry Andric 327