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 
26139f7f9bSDimitry Andric #define PRINT_ALIAS_INSTR
2717a519f9SDimitry Andric #include "MipsGenAsmWriter.inc"
2817a519f9SDimitry Andric 
29f785676fSDimitry Andric template<unsigned R>
30f785676fSDimitry Andric static bool isReg(const MCInst &MI, unsigned OpNo) {
31f785676fSDimitry Andric   assert(MI.getOperand(OpNo).isReg() && "Register operand expected.");
32f785676fSDimitry Andric   return MI.getOperand(OpNo).getReg() == R;
33f785676fSDimitry Andric }
34f785676fSDimitry Andric 
3517a519f9SDimitry Andric const char* Mips::MipsFCCToString(Mips::CondCode CC) {
3617a519f9SDimitry Andric   switch (CC) {
3717a519f9SDimitry Andric   case FCOND_F:
3817a519f9SDimitry Andric   case FCOND_T:   return "f";
3917a519f9SDimitry Andric   case FCOND_UN:
4017a519f9SDimitry Andric   case FCOND_OR:  return "un";
4117a519f9SDimitry Andric   case FCOND_OEQ:
4217a519f9SDimitry Andric   case FCOND_UNE: return "eq";
4317a519f9SDimitry Andric   case FCOND_UEQ:
4417a519f9SDimitry Andric   case FCOND_ONE: return "ueq";
4517a519f9SDimitry Andric   case FCOND_OLT:
4617a519f9SDimitry Andric   case FCOND_UGE: return "olt";
4717a519f9SDimitry Andric   case FCOND_ULT:
4817a519f9SDimitry Andric   case FCOND_OGE: return "ult";
4917a519f9SDimitry Andric   case FCOND_OLE:
5017a519f9SDimitry Andric   case FCOND_UGT: return "ole";
5117a519f9SDimitry Andric   case FCOND_ULE:
5217a519f9SDimitry Andric   case FCOND_OGT: return "ule";
5317a519f9SDimitry Andric   case FCOND_SF:
5417a519f9SDimitry Andric   case FCOND_ST:  return "sf";
5517a519f9SDimitry Andric   case FCOND_NGLE:
5617a519f9SDimitry Andric   case FCOND_GLE: return "ngle";
5717a519f9SDimitry Andric   case FCOND_SEQ:
5817a519f9SDimitry Andric   case FCOND_SNE: return "seq";
5917a519f9SDimitry Andric   case FCOND_NGL:
6017a519f9SDimitry Andric   case FCOND_GL:  return "ngl";
6117a519f9SDimitry Andric   case FCOND_LT:
6217a519f9SDimitry Andric   case FCOND_NLT: return "lt";
6317a519f9SDimitry Andric   case FCOND_NGE:
6417a519f9SDimitry Andric   case FCOND_GE:  return "nge";
6517a519f9SDimitry Andric   case FCOND_LE:
6617a519f9SDimitry Andric   case FCOND_NLE: return "le";
6717a519f9SDimitry Andric   case FCOND_NGT:
6817a519f9SDimitry Andric   case FCOND_GT:  return "ngt";
6917a519f9SDimitry Andric   }
7017a519f9SDimitry Andric   llvm_unreachable("Impossible condition code!");
7117a519f9SDimitry Andric }
7217a519f9SDimitry Andric 
7317a519f9SDimitry Andric void MipsInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
74dff0c46cSDimitry Andric   OS << '$' << StringRef(getRegisterName(RegNo)).lower();
7517a519f9SDimitry Andric }
7617a519f9SDimitry Andric 
776122f3e6SDimitry Andric void MipsInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
786122f3e6SDimitry Andric                                 StringRef Annot) {
797ae0e2c9SDimitry Andric   switch (MI->getOpcode()) {
807ae0e2c9SDimitry Andric   default:
817ae0e2c9SDimitry Andric     break;
827ae0e2c9SDimitry Andric   case Mips::RDHWR:
837ae0e2c9SDimitry Andric   case Mips::RDHWR64:
847ae0e2c9SDimitry Andric     O << "\t.set\tpush\n";
857ae0e2c9SDimitry Andric     O << "\t.set\tmips32r2\n";
867ae0e2c9SDimitry Andric   }
877ae0e2c9SDimitry Andric 
88139f7f9bSDimitry Andric   // Try to print any aliases first.
89f785676fSDimitry Andric   if (!printAliasInstr(MI, O) && !printAlias(*MI, O))
9017a519f9SDimitry Andric     printInstruction(MI, O);
916122f3e6SDimitry Andric   printAnnotation(O, Annot);
927ae0e2c9SDimitry Andric 
937ae0e2c9SDimitry Andric   switch (MI->getOpcode()) {
947ae0e2c9SDimitry Andric   default:
957ae0e2c9SDimitry Andric     break;
967ae0e2c9SDimitry Andric   case Mips::RDHWR:
977ae0e2c9SDimitry Andric   case Mips::RDHWR64:
987ae0e2c9SDimitry Andric     O << "\n\t.set\tpop";
997ae0e2c9SDimitry Andric   }
10017a519f9SDimitry Andric }
10117a519f9SDimitry Andric 
102dff0c46cSDimitry Andric static void printExpr(const MCExpr *Expr, raw_ostream &OS) {
103dff0c46cSDimitry Andric   int Offset = 0;
104dff0c46cSDimitry Andric   const MCSymbolRefExpr *SRE;
105dff0c46cSDimitry Andric 
106dff0c46cSDimitry Andric   if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
107dff0c46cSDimitry Andric     SRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS());
108dff0c46cSDimitry Andric     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(BE->getRHS());
109dff0c46cSDimitry Andric     assert(SRE && CE && "Binary expression must be sym+const.");
110dff0c46cSDimitry Andric     Offset = CE->getValue();
111dff0c46cSDimitry Andric   }
112dff0c46cSDimitry Andric   else if (!(SRE = dyn_cast<MCSymbolRefExpr>(Expr)))
113dff0c46cSDimitry Andric     assert(false && "Unexpected MCExpr type.");
114dff0c46cSDimitry Andric 
115dff0c46cSDimitry Andric   MCSymbolRefExpr::VariantKind Kind = SRE->getKind();
116dff0c46cSDimitry Andric 
117dff0c46cSDimitry Andric   switch (Kind) {
118dff0c46cSDimitry Andric   default:                                 llvm_unreachable("Invalid kind!");
119dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_None:           break;
120dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_GPREL:     OS << "%gp_rel("; break;
121dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_GOT_CALL:  OS << "%call16("; break;
122dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_GOT16:     OS << "%got(";    break;
123dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_GOT:       OS << "%got(";    break;
124dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_ABS_HI:    OS << "%hi(";     break;
125dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_ABS_LO:    OS << "%lo(";     break;
126dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_TLSGD:     OS << "%tlsgd(";  break;
127dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_TLSLDM:    OS << "%tlsldm(";  break;
128dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_DTPREL_HI: OS << "%dtprel_hi(";  break;
129dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_DTPREL_LO: OS << "%dtprel_lo(";  break;
130dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_GOTTPREL:  OS << "%gottprel("; break;
131dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_TPREL_HI:  OS << "%tprel_hi("; break;
132dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_TPREL_LO:  OS << "%tprel_lo("; break;
133dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_GPOFF_HI:  OS << "%hi(%neg(%gp_rel("; break;
134dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_GPOFF_LO:  OS << "%lo(%neg(%gp_rel("; break;
135dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_GOT_DISP:  OS << "%got_disp("; break;
136dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_GOT_PAGE:  OS << "%got_page("; break;
137dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_GOT_OFST:  OS << "%got_ofst("; break;
1387ae0e2c9SDimitry Andric   case MCSymbolRefExpr::VK_Mips_HIGHER:    OS << "%higher("; break;
1397ae0e2c9SDimitry Andric   case MCSymbolRefExpr::VK_Mips_HIGHEST:   OS << "%highest("; break;
140c80e6c4bSDimitry Andric   case MCSymbolRefExpr::VK_Mips_GOT_HI16:  OS << "%got_hi("; break;
141c80e6c4bSDimitry Andric   case MCSymbolRefExpr::VK_Mips_GOT_LO16:  OS << "%got_lo("; break;
142c80e6c4bSDimitry Andric   case MCSymbolRefExpr::VK_Mips_CALL_HI16: OS << "%call_hi("; break;
143c80e6c4bSDimitry Andric   case MCSymbolRefExpr::VK_Mips_CALL_LO16: OS << "%call_lo("; break;
144dff0c46cSDimitry Andric   }
145dff0c46cSDimitry Andric 
146dff0c46cSDimitry Andric   OS << SRE->getSymbol();
147dff0c46cSDimitry Andric 
148dff0c46cSDimitry Andric   if (Offset) {
149dff0c46cSDimitry Andric     if (Offset > 0)
150dff0c46cSDimitry Andric       OS << '+';
151dff0c46cSDimitry Andric     OS << Offset;
152dff0c46cSDimitry Andric   }
153dff0c46cSDimitry Andric 
154dff0c46cSDimitry Andric   if ((Kind == MCSymbolRefExpr::VK_Mips_GPOFF_HI) ||
155dff0c46cSDimitry Andric       (Kind == MCSymbolRefExpr::VK_Mips_GPOFF_LO))
156dff0c46cSDimitry Andric     OS << ")))";
157dff0c46cSDimitry Andric   else if (Kind != MCSymbolRefExpr::VK_None)
158dff0c46cSDimitry Andric     OS << ')';
159dff0c46cSDimitry Andric }
160dff0c46cSDimitry Andric 
16117a519f9SDimitry Andric void MipsInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
16217a519f9SDimitry Andric                                    raw_ostream &O) {
16317a519f9SDimitry Andric   const MCOperand &Op = MI->getOperand(OpNo);
16417a519f9SDimitry Andric   if (Op.isReg()) {
16517a519f9SDimitry Andric     printRegName(O, Op.getReg());
16617a519f9SDimitry Andric     return;
16717a519f9SDimitry Andric   }
16817a519f9SDimitry Andric 
16917a519f9SDimitry Andric   if (Op.isImm()) {
17017a519f9SDimitry Andric     O << Op.getImm();
17117a519f9SDimitry Andric     return;
17217a519f9SDimitry Andric   }
17317a519f9SDimitry Andric 
17417a519f9SDimitry Andric   assert(Op.isExpr() && "unknown operand kind in printOperand");
175dff0c46cSDimitry Andric   printExpr(Op.getExpr(), O);
17617a519f9SDimitry Andric }
17717a519f9SDimitry Andric 
17817a519f9SDimitry Andric void MipsInstPrinter::printUnsignedImm(const MCInst *MI, int opNum,
17917a519f9SDimitry Andric                                        raw_ostream &O) {
18017a519f9SDimitry Andric   const MCOperand &MO = MI->getOperand(opNum);
18117a519f9SDimitry Andric   if (MO.isImm())
18217a519f9SDimitry Andric     O << (unsigned short int)MO.getImm();
18317a519f9SDimitry Andric   else
18417a519f9SDimitry Andric     printOperand(MI, opNum, O);
18517a519f9SDimitry Andric }
18617a519f9SDimitry Andric 
187f785676fSDimitry Andric void MipsInstPrinter::printUnsignedImm8(const MCInst *MI, int opNum,
188f785676fSDimitry Andric                                         raw_ostream &O) {
189f785676fSDimitry Andric   const MCOperand &MO = MI->getOperand(opNum);
190f785676fSDimitry Andric   if (MO.isImm())
191f785676fSDimitry Andric     O << (unsigned short int)(unsigned char)MO.getImm();
192f785676fSDimitry Andric   else
193f785676fSDimitry Andric     printOperand(MI, opNum, O);
194f785676fSDimitry Andric }
195f785676fSDimitry Andric 
19617a519f9SDimitry Andric void MipsInstPrinter::
19717a519f9SDimitry Andric printMemOperand(const MCInst *MI, int opNum, raw_ostream &O) {
19817a519f9SDimitry Andric   // Load/Store memory operands -- imm($reg)
19917a519f9SDimitry Andric   // If PIC target the target is loaded as the
20017a519f9SDimitry Andric   // pattern lw $25,%call16($28)
20117a519f9SDimitry Andric   printOperand(MI, opNum+1, O);
20217a519f9SDimitry Andric   O << "(";
20317a519f9SDimitry Andric   printOperand(MI, opNum, O);
20417a519f9SDimitry Andric   O << ")";
20517a519f9SDimitry Andric }
20617a519f9SDimitry Andric 
20717a519f9SDimitry Andric void MipsInstPrinter::
20817a519f9SDimitry Andric printMemOperandEA(const MCInst *MI, int opNum, raw_ostream &O) {
20917a519f9SDimitry Andric   // when using stack locations for not load/store instructions
21017a519f9SDimitry Andric   // print the same way as all normal 3 operand instructions.
21117a519f9SDimitry Andric   printOperand(MI, opNum, O);
21217a519f9SDimitry Andric   O << ", ";
21317a519f9SDimitry Andric   printOperand(MI, opNum+1, O);
21417a519f9SDimitry Andric   return;
21517a519f9SDimitry Andric }
21617a519f9SDimitry Andric 
21717a519f9SDimitry Andric void MipsInstPrinter::
21817a519f9SDimitry Andric printFCCOperand(const MCInst *MI, int opNum, raw_ostream &O) {
21917a519f9SDimitry Andric   const MCOperand& MO = MI->getOperand(opNum);
22017a519f9SDimitry Andric   O << MipsFCCToString((Mips::CondCode)MO.getImm());
22117a519f9SDimitry Andric }
222f785676fSDimitry Andric 
223f785676fSDimitry Andric void MipsInstPrinter::
224f785676fSDimitry Andric printSHFMask(const MCInst *MI, int opNum, raw_ostream &O) {
225f785676fSDimitry Andric   llvm_unreachable("TODO");
226f785676fSDimitry Andric }
227f785676fSDimitry Andric 
228f785676fSDimitry Andric bool MipsInstPrinter::printAlias(const char *Str, const MCInst &MI,
229f785676fSDimitry Andric                                  unsigned OpNo, raw_ostream &OS) {
230f785676fSDimitry Andric   OS << "\t" << Str << "\t";
231f785676fSDimitry Andric   printOperand(&MI, OpNo, OS);
232f785676fSDimitry Andric   return true;
233f785676fSDimitry Andric }
234f785676fSDimitry Andric 
235f785676fSDimitry Andric bool MipsInstPrinter::printAlias(const char *Str, const MCInst &MI,
236f785676fSDimitry Andric                                  unsigned OpNo0, unsigned OpNo1,
237f785676fSDimitry Andric                                  raw_ostream &OS) {
238f785676fSDimitry Andric   printAlias(Str, MI, OpNo0, OS);
239f785676fSDimitry Andric   OS << ", ";
240f785676fSDimitry Andric   printOperand(&MI, OpNo1, OS);
241f785676fSDimitry Andric   return true;
242f785676fSDimitry Andric }
243f785676fSDimitry Andric 
244f785676fSDimitry Andric bool MipsInstPrinter::printAlias(const MCInst &MI, raw_ostream &OS) {
245f785676fSDimitry Andric   switch (MI.getOpcode()) {
246f785676fSDimitry Andric   case Mips::BEQ:
247f785676fSDimitry Andric     // beq $zero, $zero, $L2 => b $L2
248f785676fSDimitry Andric     // beq $r0, $zero, $L2 => beqz $r0, $L2
249f785676fSDimitry Andric     return (isReg<Mips::ZERO>(MI, 0) && isReg<Mips::ZERO>(MI, 1) &&
250f785676fSDimitry Andric             printAlias("b", MI, 2, OS)) ||
251f785676fSDimitry Andric            (isReg<Mips::ZERO>(MI, 1) && printAlias("beqz", MI, 0, 2, OS));
252f785676fSDimitry Andric   case Mips::BEQ64:
253f785676fSDimitry Andric     // beq $r0, $zero, $L2 => beqz $r0, $L2
254f785676fSDimitry Andric     return isReg<Mips::ZERO_64>(MI, 1) && printAlias("beqz", MI, 0, 2, OS);
255f785676fSDimitry Andric   case Mips::BNE:
256f785676fSDimitry Andric     // bne $r0, $zero, $L2 => bnez $r0, $L2
257f785676fSDimitry Andric     return isReg<Mips::ZERO>(MI, 1) && printAlias("bnez", MI, 0, 2, OS);
258f785676fSDimitry Andric   case Mips::BNE64:
259f785676fSDimitry Andric     // bne $r0, $zero, $L2 => bnez $r0, $L2
260f785676fSDimitry Andric     return isReg<Mips::ZERO_64>(MI, 1) && printAlias("bnez", MI, 0, 2, OS);
261f785676fSDimitry Andric   case Mips::BGEZAL:
262f785676fSDimitry Andric     // bgezal $zero, $L1 => bal $L1
263f785676fSDimitry Andric     return isReg<Mips::ZERO>(MI, 0) && printAlias("bal", MI, 1, OS);
264f785676fSDimitry Andric   case Mips::BC1T:
265f785676fSDimitry Andric     // bc1t $fcc0, $L1 => bc1t $L1
266f785676fSDimitry Andric     return isReg<Mips::FCC0>(MI, 0) && printAlias("bc1t", MI, 1, OS);
267f785676fSDimitry Andric   case Mips::BC1F:
268f785676fSDimitry Andric     // bc1f $fcc0, $L1 => bc1f $L1
269f785676fSDimitry Andric     return isReg<Mips::FCC0>(MI, 0) && printAlias("bc1f", MI, 1, OS);
270f785676fSDimitry Andric   case Mips::JALR:
271f785676fSDimitry Andric     // jalr $ra, $r1 => jalr $r1
272f785676fSDimitry Andric     return isReg<Mips::RA>(MI, 0) && printAlias("jalr", MI, 1, OS);
273f785676fSDimitry Andric   case Mips::JALR64:
274f785676fSDimitry Andric     // jalr $ra, $r1 => jalr $r1
275f785676fSDimitry Andric     return isReg<Mips::RA_64>(MI, 0) && printAlias("jalr", MI, 1, OS);
276f785676fSDimitry Andric   case Mips::NOR:
277f785676fSDimitry Andric   case Mips::NOR_MM:
278f785676fSDimitry Andric     // nor $r0, $r1, $zero => not $r0, $r1
279f785676fSDimitry Andric     return isReg<Mips::ZERO>(MI, 2) && printAlias("not", MI, 0, 1, OS);
280f785676fSDimitry Andric   case Mips::NOR64:
281f785676fSDimitry Andric     // nor $r0, $r1, $zero => not $r0, $r1
282f785676fSDimitry Andric     return isReg<Mips::ZERO_64>(MI, 2) && printAlias("not", MI, 0, 1, OS);
283f785676fSDimitry Andric   case Mips::OR:
284f785676fSDimitry Andric     // or $r0, $r1, $zero => move $r0, $r1
285f785676fSDimitry Andric     return isReg<Mips::ZERO>(MI, 2) && printAlias("move", MI, 0, 1, OS);
286f785676fSDimitry Andric   default: return false;
287f785676fSDimitry Andric   }
288f785676fSDimitry Andric }
289