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>
isReg(const MCInst & MI,unsigned OpNo)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 
MipsFCCToString(Mips::CondCode CC)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 
printRegName(raw_ostream & OS,unsigned RegNo) const7517a519f9SDimitry Andric void MipsInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
76dff0c46cSDimitry Andric   OS << '$' << StringRef(getRegisterName(RegNo)).lower();
7717a519f9SDimitry Andric }
7817a519f9SDimitry Andric 
printInst(const MCInst * MI,raw_ostream & O,StringRef Annot,const MCSubtargetInfo & STI)796122f3e6SDimitry Andric void MipsInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
80ff0cc061SDimitry Andric                                 StringRef Annot, const MCSubtargetInfo &STI) {
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 
printOperand(const MCInst * MI,unsigned OpNo,raw_ostream & O)12517a519f9SDimitry Andric void MipsInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
12617a519f9SDimitry Andric                                    raw_ostream &O) {
12717a519f9SDimitry Andric   const MCOperand &Op = MI->getOperand(OpNo);
12817a519f9SDimitry Andric   if (Op.isReg()) {
12917a519f9SDimitry Andric     printRegName(O, Op.getReg());
13017a519f9SDimitry Andric     return;
13117a519f9SDimitry Andric   }
13217a519f9SDimitry Andric 
13317a519f9SDimitry Andric   if (Op.isImm()) {
1343ca95b02SDimitry Andric     O << formatImm(Op.getImm());
13517a519f9SDimitry Andric     return;
13617a519f9SDimitry Andric   }
13717a519f9SDimitry Andric 
13817a519f9SDimitry Andric   assert(Op.isExpr() && "unknown operand kind in printOperand");
1393ca95b02SDimitry Andric   Op.getExpr()->print(O, &MAI, true);
14017a519f9SDimitry Andric }
14117a519f9SDimitry Andric 
1423ca95b02SDimitry Andric template <unsigned Bits, unsigned Offset>
printUImm(const MCInst * MI,int opNum,raw_ostream & O)1433ca95b02SDimitry Andric void MipsInstPrinter::printUImm(const MCInst *MI, int opNum, raw_ostream &O) {
14417a519f9SDimitry Andric   const MCOperand &MO = MI->getOperand(opNum);
1453ca95b02SDimitry Andric   if (MO.isImm()) {
1463ca95b02SDimitry Andric     uint64_t Imm = MO.getImm();
1473ca95b02SDimitry Andric     Imm -= Offset;
1483ca95b02SDimitry Andric     Imm &= (1 << Bits) - 1;
1493ca95b02SDimitry Andric     Imm += Offset;
1503ca95b02SDimitry Andric     O << formatImm(Imm);
1513ca95b02SDimitry Andric     return;
15217a519f9SDimitry Andric   }
15317a519f9SDimitry Andric 
154f785676fSDimitry Andric   printOperand(MI, opNum, O);
155f785676fSDimitry Andric }
156f785676fSDimitry Andric 
15717a519f9SDimitry Andric void MipsInstPrinter::
printMemOperand(const MCInst * MI,int opNum,raw_ostream & O)15817a519f9SDimitry Andric printMemOperand(const MCInst *MI, int opNum, raw_ostream &O) {
15917a519f9SDimitry Andric   // Load/Store memory operands -- imm($reg)
16017a519f9SDimitry Andric   // If PIC target the target is loaded as the
16117a519f9SDimitry Andric   // pattern lw $25,%call16($28)
16239d628a0SDimitry Andric 
16339d628a0SDimitry Andric   // opNum can be invalid if instruction had reglist as operand.
16439d628a0SDimitry Andric   // MemOperand is always last operand of instruction (base + offset).
16539d628a0SDimitry Andric   switch (MI->getOpcode()) {
16639d628a0SDimitry Andric   default:
16739d628a0SDimitry Andric     break;
16839d628a0SDimitry Andric   case Mips::SWM32_MM:
16939d628a0SDimitry Andric   case Mips::LWM32_MM:
17039d628a0SDimitry Andric   case Mips::SWM16_MM:
1717d523365SDimitry Andric   case Mips::SWM16_MMR6:
17239d628a0SDimitry Andric   case Mips::LWM16_MM:
1737d523365SDimitry Andric   case Mips::LWM16_MMR6:
17439d628a0SDimitry Andric     opNum = MI->getNumOperands() - 2;
17539d628a0SDimitry Andric     break;
17639d628a0SDimitry Andric   }
17739d628a0SDimitry Andric 
17817a519f9SDimitry Andric   printOperand(MI, opNum+1, O);
17917a519f9SDimitry Andric   O << "(";
18017a519f9SDimitry Andric   printOperand(MI, opNum, O);
18117a519f9SDimitry Andric   O << ")";
18217a519f9SDimitry Andric }
18317a519f9SDimitry Andric 
18417a519f9SDimitry Andric void MipsInstPrinter::
printMemOperandEA(const MCInst * MI,int opNum,raw_ostream & O)18517a519f9SDimitry Andric printMemOperandEA(const MCInst *MI, int opNum, raw_ostream &O) {
18617a519f9SDimitry Andric   // when using stack locations for not load/store instructions
18717a519f9SDimitry Andric   // print the same way as all normal 3 operand instructions.
18817a519f9SDimitry Andric   printOperand(MI, opNum, O);
18917a519f9SDimitry Andric   O << ", ";
19017a519f9SDimitry Andric   printOperand(MI, opNum+1, O);
19117a519f9SDimitry Andric }
19217a519f9SDimitry Andric 
19317a519f9SDimitry Andric void MipsInstPrinter::
printFCCOperand(const MCInst * MI,int opNum,raw_ostream & O)19417a519f9SDimitry Andric printFCCOperand(const MCInst *MI, int opNum, raw_ostream &O) {
19517a519f9SDimitry Andric   const MCOperand& MO = MI->getOperand(opNum);
19617a519f9SDimitry Andric   O << MipsFCCToString((Mips::CondCode)MO.getImm());
19717a519f9SDimitry Andric }
198f785676fSDimitry Andric 
199f785676fSDimitry Andric void MipsInstPrinter::
printSHFMask(const MCInst * MI,int opNum,raw_ostream & O)200f785676fSDimitry Andric printSHFMask(const MCInst *MI, int opNum, raw_ostream &O) {
201f785676fSDimitry Andric   llvm_unreachable("TODO");
202f785676fSDimitry Andric }
203f785676fSDimitry Andric 
printAlias(const char * Str,const MCInst & MI,unsigned OpNo,raw_ostream & OS)204f785676fSDimitry Andric bool MipsInstPrinter::printAlias(const char *Str, const MCInst &MI,
205f785676fSDimitry Andric                                  unsigned OpNo, raw_ostream &OS) {
206f785676fSDimitry Andric   OS << "\t" << Str << "\t";
207f785676fSDimitry Andric   printOperand(&MI, OpNo, OS);
208f785676fSDimitry Andric   return true;
209f785676fSDimitry Andric }
210f785676fSDimitry Andric 
printAlias(const char * Str,const MCInst & MI,unsigned OpNo0,unsigned OpNo1,raw_ostream & OS)211f785676fSDimitry Andric bool MipsInstPrinter::printAlias(const char *Str, const MCInst &MI,
212f785676fSDimitry Andric                                  unsigned OpNo0, unsigned OpNo1,
213f785676fSDimitry Andric                                  raw_ostream &OS) {
214f785676fSDimitry Andric   printAlias(Str, MI, OpNo0, OS);
215f785676fSDimitry Andric   OS << ", ";
216f785676fSDimitry Andric   printOperand(&MI, OpNo1, OS);
217f785676fSDimitry Andric   return true;
218f785676fSDimitry Andric }
219f785676fSDimitry Andric 
printAlias(const MCInst & MI,raw_ostream & OS)220f785676fSDimitry Andric bool MipsInstPrinter::printAlias(const MCInst &MI, raw_ostream &OS) {
221f785676fSDimitry Andric   switch (MI.getOpcode()) {
222f785676fSDimitry Andric   case Mips::BEQ:
223ff0cc061SDimitry Andric   case Mips::BEQ_MM:
224f785676fSDimitry Andric     // beq $zero, $zero, $L2 => b $L2
225f785676fSDimitry Andric     // beq $r0, $zero, $L2 => beqz $r0, $L2
226f785676fSDimitry Andric     return (isReg<Mips::ZERO>(MI, 0) && isReg<Mips::ZERO>(MI, 1) &&
227f785676fSDimitry Andric             printAlias("b", MI, 2, OS)) ||
228f785676fSDimitry Andric            (isReg<Mips::ZERO>(MI, 1) && printAlias("beqz", MI, 0, 2, OS));
229f785676fSDimitry Andric   case Mips::BEQ64:
230f785676fSDimitry Andric     // beq $r0, $zero, $L2 => beqz $r0, $L2
231f785676fSDimitry Andric     return isReg<Mips::ZERO_64>(MI, 1) && printAlias("beqz", MI, 0, 2, OS);
232f785676fSDimitry Andric   case Mips::BNE:
233d88c1a5aSDimitry Andric   case Mips::BNE_MM:
234f785676fSDimitry Andric     // bne $r0, $zero, $L2 => bnez $r0, $L2
235f785676fSDimitry Andric     return isReg<Mips::ZERO>(MI, 1) && printAlias("bnez", MI, 0, 2, OS);
236f785676fSDimitry Andric   case Mips::BNE64:
237f785676fSDimitry Andric     // bne $r0, $zero, $L2 => bnez $r0, $L2
238f785676fSDimitry Andric     return isReg<Mips::ZERO_64>(MI, 1) && printAlias("bnez", MI, 0, 2, OS);
239f785676fSDimitry Andric   case Mips::BGEZAL:
240f785676fSDimitry Andric     // bgezal $zero, $L1 => bal $L1
241f785676fSDimitry Andric     return isReg<Mips::ZERO>(MI, 0) && printAlias("bal", MI, 1, OS);
242f785676fSDimitry Andric   case Mips::BC1T:
243f785676fSDimitry Andric     // bc1t $fcc0, $L1 => bc1t $L1
244f785676fSDimitry Andric     return isReg<Mips::FCC0>(MI, 0) && printAlias("bc1t", MI, 1, OS);
245f785676fSDimitry Andric   case Mips::BC1F:
246f785676fSDimitry Andric     // bc1f $fcc0, $L1 => bc1f $L1
247f785676fSDimitry Andric     return isReg<Mips::FCC0>(MI, 0) && printAlias("bc1f", MI, 1, OS);
248f785676fSDimitry Andric   case Mips::JALR:
249f785676fSDimitry Andric     // jalr $ra, $r1 => jalr $r1
250f785676fSDimitry Andric     return isReg<Mips::RA>(MI, 0) && printAlias("jalr", MI, 1, OS);
251f785676fSDimitry Andric   case Mips::JALR64:
252f785676fSDimitry Andric     // jalr $ra, $r1 => jalr $r1
253f785676fSDimitry Andric     return isReg<Mips::RA_64>(MI, 0) && printAlias("jalr", MI, 1, OS);
254f785676fSDimitry Andric   case Mips::NOR:
255f785676fSDimitry Andric   case Mips::NOR_MM:
2563ca95b02SDimitry Andric   case Mips::NOR_MMR6:
257f785676fSDimitry Andric     // nor $r0, $r1, $zero => not $r0, $r1
258f785676fSDimitry Andric     return isReg<Mips::ZERO>(MI, 2) && printAlias("not", MI, 0, 1, OS);
259f785676fSDimitry Andric   case Mips::NOR64:
260f785676fSDimitry Andric     // nor $r0, $r1, $zero => not $r0, $r1
261f785676fSDimitry Andric     return isReg<Mips::ZERO_64>(MI, 2) && printAlias("not", MI, 0, 1, OS);
262f785676fSDimitry Andric   case Mips::OR:
263f785676fSDimitry Andric     // or $r0, $r1, $zero => move $r0, $r1
264f785676fSDimitry Andric     return isReg<Mips::ZERO>(MI, 2) && printAlias("move", MI, 0, 1, OS);
265f785676fSDimitry Andric   default: return false;
266f785676fSDimitry Andric   }
267f785676fSDimitry Andric }
26891bc56edSDimitry Andric 
printSaveRestore(const MCInst * MI,raw_ostream & O)26991bc56edSDimitry Andric void MipsInstPrinter::printSaveRestore(const MCInst *MI, raw_ostream &O) {
27091bc56edSDimitry Andric   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
27191bc56edSDimitry Andric     if (i != 0) O << ", ";
27291bc56edSDimitry Andric     if (MI->getOperand(i).isReg())
27391bc56edSDimitry Andric       printRegName(O, MI->getOperand(i).getReg());
27491bc56edSDimitry Andric     else
2753ca95b02SDimitry Andric       printUImm<16>(MI, i, O);
27691bc56edSDimitry Andric   }
27791bc56edSDimitry Andric }
27891bc56edSDimitry Andric 
27939d628a0SDimitry Andric void MipsInstPrinter::
printRegisterList(const MCInst * MI,int opNum,raw_ostream & O)28039d628a0SDimitry Andric printRegisterList(const MCInst *MI, int opNum, raw_ostream &O) {
28139d628a0SDimitry Andric   // - 2 because register List is always first operand of instruction and it is
28239d628a0SDimitry Andric   // always followed by memory operand (base + offset).
28339d628a0SDimitry Andric   for (int i = opNum, e = MI->getNumOperands() - 2; i != e; ++i) {
28439d628a0SDimitry Andric     if (i != opNum)
28539d628a0SDimitry Andric       O << ", ";
28639d628a0SDimitry Andric     printRegName(O, MI->getOperand(i).getReg());
28739d628a0SDimitry Andric   }
28839d628a0SDimitry Andric }
289