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 #define DEBUG_TYPE "asm-printer" 1517a519f9SDimitry Andric #include "MipsInstPrinter.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 2617a519f9SDimitry Andric #include "MipsGenAsmWriter.inc" 2717a519f9SDimitry Andric 2817a519f9SDimitry Andric const char* Mips::MipsFCCToString(Mips::CondCode CC) { 2917a519f9SDimitry Andric switch (CC) { 3017a519f9SDimitry Andric case FCOND_F: 3117a519f9SDimitry Andric case FCOND_T: return "f"; 3217a519f9SDimitry Andric case FCOND_UN: 3317a519f9SDimitry Andric case FCOND_OR: return "un"; 3417a519f9SDimitry Andric case FCOND_OEQ: 3517a519f9SDimitry Andric case FCOND_UNE: return "eq"; 3617a519f9SDimitry Andric case FCOND_UEQ: 3717a519f9SDimitry Andric case FCOND_ONE: return "ueq"; 3817a519f9SDimitry Andric case FCOND_OLT: 3917a519f9SDimitry Andric case FCOND_UGE: return "olt"; 4017a519f9SDimitry Andric case FCOND_ULT: 4117a519f9SDimitry Andric case FCOND_OGE: return "ult"; 4217a519f9SDimitry Andric case FCOND_OLE: 4317a519f9SDimitry Andric case FCOND_UGT: return "ole"; 4417a519f9SDimitry Andric case FCOND_ULE: 4517a519f9SDimitry Andric case FCOND_OGT: return "ule"; 4617a519f9SDimitry Andric case FCOND_SF: 4717a519f9SDimitry Andric case FCOND_ST: return "sf"; 4817a519f9SDimitry Andric case FCOND_NGLE: 4917a519f9SDimitry Andric case FCOND_GLE: return "ngle"; 5017a519f9SDimitry Andric case FCOND_SEQ: 5117a519f9SDimitry Andric case FCOND_SNE: return "seq"; 5217a519f9SDimitry Andric case FCOND_NGL: 5317a519f9SDimitry Andric case FCOND_GL: return "ngl"; 5417a519f9SDimitry Andric case FCOND_LT: 5517a519f9SDimitry Andric case FCOND_NLT: return "lt"; 5617a519f9SDimitry Andric case FCOND_NGE: 5717a519f9SDimitry Andric case FCOND_GE: return "nge"; 5817a519f9SDimitry Andric case FCOND_LE: 5917a519f9SDimitry Andric case FCOND_NLE: return "le"; 6017a519f9SDimitry Andric case FCOND_NGT: 6117a519f9SDimitry Andric case FCOND_GT: return "ngt"; 6217a519f9SDimitry Andric } 6317a519f9SDimitry Andric llvm_unreachable("Impossible condition code!"); 6417a519f9SDimitry Andric } 6517a519f9SDimitry Andric 6617a519f9SDimitry Andric void MipsInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const { 67dff0c46cSDimitry Andric OS << '$' << StringRef(getRegisterName(RegNo)).lower(); 6817a519f9SDimitry Andric } 6917a519f9SDimitry Andric 706122f3e6SDimitry Andric void MipsInstPrinter::printInst(const MCInst *MI, raw_ostream &O, 716122f3e6SDimitry Andric StringRef Annot) { 727ae0e2c9SDimitry Andric switch (MI->getOpcode()) { 737ae0e2c9SDimitry Andric default: 747ae0e2c9SDimitry Andric break; 757ae0e2c9SDimitry Andric case Mips::RDHWR: 767ae0e2c9SDimitry Andric case Mips::RDHWR64: 777ae0e2c9SDimitry Andric O << "\t.set\tpush\n"; 787ae0e2c9SDimitry Andric O << "\t.set\tmips32r2\n"; 797ae0e2c9SDimitry Andric } 807ae0e2c9SDimitry Andric 8117a519f9SDimitry Andric printInstruction(MI, O); 826122f3e6SDimitry Andric printAnnotation(O, Annot); 837ae0e2c9SDimitry Andric 847ae0e2c9SDimitry Andric switch (MI->getOpcode()) { 857ae0e2c9SDimitry Andric default: 867ae0e2c9SDimitry Andric break; 877ae0e2c9SDimitry Andric case Mips::RDHWR: 887ae0e2c9SDimitry Andric case Mips::RDHWR64: 897ae0e2c9SDimitry Andric O << "\n\t.set\tpop"; 907ae0e2c9SDimitry Andric } 9117a519f9SDimitry Andric } 9217a519f9SDimitry Andric 93dff0c46cSDimitry Andric static void printExpr(const MCExpr *Expr, raw_ostream &OS) { 94dff0c46cSDimitry Andric int Offset = 0; 95dff0c46cSDimitry Andric const MCSymbolRefExpr *SRE; 96dff0c46cSDimitry Andric 97dff0c46cSDimitry Andric if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) { 98dff0c46cSDimitry Andric SRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS()); 99dff0c46cSDimitry Andric const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(BE->getRHS()); 100dff0c46cSDimitry Andric assert(SRE && CE && "Binary expression must be sym+const."); 101dff0c46cSDimitry Andric Offset = CE->getValue(); 102dff0c46cSDimitry Andric } 103dff0c46cSDimitry Andric else if (!(SRE = dyn_cast<MCSymbolRefExpr>(Expr))) 104dff0c46cSDimitry Andric assert(false && "Unexpected MCExpr type."); 105dff0c46cSDimitry Andric 106dff0c46cSDimitry Andric MCSymbolRefExpr::VariantKind Kind = SRE->getKind(); 107dff0c46cSDimitry Andric 108dff0c46cSDimitry Andric switch (Kind) { 109dff0c46cSDimitry Andric default: llvm_unreachable("Invalid kind!"); 110dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_None: break; 111dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_GPREL: OS << "%gp_rel("; break; 112dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_GOT_CALL: OS << "%call16("; break; 113dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_GOT16: OS << "%got("; break; 114dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_GOT: OS << "%got("; break; 115dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_ABS_HI: OS << "%hi("; break; 116dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_ABS_LO: OS << "%lo("; break; 117dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_TLSGD: OS << "%tlsgd("; break; 118dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_TLSLDM: OS << "%tlsldm("; break; 119dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_DTPREL_HI: OS << "%dtprel_hi("; break; 120dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_DTPREL_LO: OS << "%dtprel_lo("; break; 121dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_GOTTPREL: OS << "%gottprel("; break; 122dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_TPREL_HI: OS << "%tprel_hi("; break; 123dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_TPREL_LO: OS << "%tprel_lo("; break; 124dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_GPOFF_HI: OS << "%hi(%neg(%gp_rel("; break; 125dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_GPOFF_LO: OS << "%lo(%neg(%gp_rel("; break; 126dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_GOT_DISP: OS << "%got_disp("; break; 127dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_GOT_PAGE: OS << "%got_page("; break; 128dff0c46cSDimitry Andric case MCSymbolRefExpr::VK_Mips_GOT_OFST: OS << "%got_ofst("; break; 1297ae0e2c9SDimitry Andric case MCSymbolRefExpr::VK_Mips_HIGHER: OS << "%higher("; break; 1307ae0e2c9SDimitry Andric case MCSymbolRefExpr::VK_Mips_HIGHEST: OS << "%highest("; break; 131c80e6c4bSDimitry Andric case MCSymbolRefExpr::VK_Mips_GOT_HI16: OS << "%got_hi("; break; 132c80e6c4bSDimitry Andric case MCSymbolRefExpr::VK_Mips_GOT_LO16: OS << "%got_lo("; break; 133c80e6c4bSDimitry Andric case MCSymbolRefExpr::VK_Mips_CALL_HI16: OS << "%call_hi("; break; 134c80e6c4bSDimitry Andric case MCSymbolRefExpr::VK_Mips_CALL_LO16: OS << "%call_lo("; break; 135dff0c46cSDimitry Andric } 136dff0c46cSDimitry Andric 137dff0c46cSDimitry Andric OS << SRE->getSymbol(); 138dff0c46cSDimitry Andric 139dff0c46cSDimitry Andric if (Offset) { 140dff0c46cSDimitry Andric if (Offset > 0) 141dff0c46cSDimitry Andric OS << '+'; 142dff0c46cSDimitry Andric OS << Offset; 143dff0c46cSDimitry Andric } 144dff0c46cSDimitry Andric 145dff0c46cSDimitry Andric if ((Kind == MCSymbolRefExpr::VK_Mips_GPOFF_HI) || 146dff0c46cSDimitry Andric (Kind == MCSymbolRefExpr::VK_Mips_GPOFF_LO)) 147dff0c46cSDimitry Andric OS << ")))"; 148dff0c46cSDimitry Andric else if (Kind != MCSymbolRefExpr::VK_None) 149dff0c46cSDimitry Andric OS << ')'; 150dff0c46cSDimitry Andric } 151dff0c46cSDimitry Andric 15217a519f9SDimitry Andric void MipsInstPrinter::printOperand(const MCInst *MI, unsigned OpNo, 15317a519f9SDimitry Andric raw_ostream &O) { 15417a519f9SDimitry Andric const MCOperand &Op = MI->getOperand(OpNo); 15517a519f9SDimitry Andric if (Op.isReg()) { 15617a519f9SDimitry Andric printRegName(O, Op.getReg()); 15717a519f9SDimitry Andric return; 15817a519f9SDimitry Andric } 15917a519f9SDimitry Andric 16017a519f9SDimitry Andric if (Op.isImm()) { 16117a519f9SDimitry Andric O << Op.getImm(); 16217a519f9SDimitry Andric return; 16317a519f9SDimitry Andric } 16417a519f9SDimitry Andric 16517a519f9SDimitry Andric assert(Op.isExpr() && "unknown operand kind in printOperand"); 166dff0c46cSDimitry Andric printExpr(Op.getExpr(), O); 16717a519f9SDimitry Andric } 16817a519f9SDimitry Andric 16917a519f9SDimitry Andric void MipsInstPrinter::printUnsignedImm(const MCInst *MI, int opNum, 17017a519f9SDimitry Andric raw_ostream &O) { 17117a519f9SDimitry Andric const MCOperand &MO = MI->getOperand(opNum); 17217a519f9SDimitry Andric if (MO.isImm()) 17317a519f9SDimitry Andric O << (unsigned short int)MO.getImm(); 17417a519f9SDimitry Andric else 17517a519f9SDimitry Andric printOperand(MI, opNum, O); 17617a519f9SDimitry Andric } 17717a519f9SDimitry Andric 17817a519f9SDimitry Andric void MipsInstPrinter:: 17917a519f9SDimitry Andric printMemOperand(const MCInst *MI, int opNum, raw_ostream &O) { 18017a519f9SDimitry Andric // Load/Store memory operands -- imm($reg) 18117a519f9SDimitry Andric // If PIC target the target is loaded as the 18217a519f9SDimitry Andric // pattern lw $25,%call16($28) 18317a519f9SDimitry Andric printOperand(MI, opNum+1, O); 18417a519f9SDimitry Andric O << "("; 18517a519f9SDimitry Andric printOperand(MI, opNum, O); 18617a519f9SDimitry Andric O << ")"; 18717a519f9SDimitry Andric } 18817a519f9SDimitry Andric 18917a519f9SDimitry Andric void MipsInstPrinter:: 19017a519f9SDimitry Andric printMemOperandEA(const MCInst *MI, int opNum, raw_ostream &O) { 19117a519f9SDimitry Andric // when using stack locations for not load/store instructions 19217a519f9SDimitry Andric // print the same way as all normal 3 operand instructions. 19317a519f9SDimitry Andric printOperand(MI, opNum, O); 19417a519f9SDimitry Andric O << ", "; 19517a519f9SDimitry Andric printOperand(MI, opNum+1, O); 19617a519f9SDimitry Andric return; 19717a519f9SDimitry Andric } 19817a519f9SDimitry Andric 19917a519f9SDimitry Andric void MipsInstPrinter:: 20017a519f9SDimitry Andric printFCCOperand(const MCInst *MI, int opNum, raw_ostream &O) { 20117a519f9SDimitry Andric const MCOperand& MO = MI->getOperand(opNum); 20217a519f9SDimitry Andric O << MipsFCCToString((Mips::CondCode)MO.getImm()); 20317a519f9SDimitry Andric } 204