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"
16dff0c46cSDimitry Andric #include "llvm/ADT/StringExtras.h"
1717a519f9SDimitry Andric #include "llvm/MC/MCExpr.h"
1817a519f9SDimitry Andric #include "llvm/MC/MCInst.h"
19dff0c46cSDimitry Andric #include "llvm/MC/MCInstrInfo.h"
20dff0c46cSDimitry Andric #include "llvm/MC/MCSymbol.h"
2117a519f9SDimitry Andric #include "llvm/Support/ErrorHandling.h"
2217a519f9SDimitry Andric #include "llvm/Support/raw_ostream.h"
2317a519f9SDimitry Andric using namespace llvm;
2417a519f9SDimitry Andric 
2517a519f9SDimitry Andric #include "MipsGenAsmWriter.inc"
2617a519f9SDimitry Andric 
2717a519f9SDimitry Andric const char* Mips::MipsFCCToString(Mips::CondCode CC) {
2817a519f9SDimitry Andric   switch (CC) {
2917a519f9SDimitry Andric   case FCOND_F:
3017a519f9SDimitry Andric   case FCOND_T:   return "f";
3117a519f9SDimitry Andric   case FCOND_UN:
3217a519f9SDimitry Andric   case FCOND_OR:  return "un";
3317a519f9SDimitry Andric   case FCOND_OEQ:
3417a519f9SDimitry Andric   case FCOND_UNE: return "eq";
3517a519f9SDimitry Andric   case FCOND_UEQ:
3617a519f9SDimitry Andric   case FCOND_ONE: return "ueq";
3717a519f9SDimitry Andric   case FCOND_OLT:
3817a519f9SDimitry Andric   case FCOND_UGE: return "olt";
3917a519f9SDimitry Andric   case FCOND_ULT:
4017a519f9SDimitry Andric   case FCOND_OGE: return "ult";
4117a519f9SDimitry Andric   case FCOND_OLE:
4217a519f9SDimitry Andric   case FCOND_UGT: return "ole";
4317a519f9SDimitry Andric   case FCOND_ULE:
4417a519f9SDimitry Andric   case FCOND_OGT: return "ule";
4517a519f9SDimitry Andric   case FCOND_SF:
4617a519f9SDimitry Andric   case FCOND_ST:  return "sf";
4717a519f9SDimitry Andric   case FCOND_NGLE:
4817a519f9SDimitry Andric   case FCOND_GLE: return "ngle";
4917a519f9SDimitry Andric   case FCOND_SEQ:
5017a519f9SDimitry Andric   case FCOND_SNE: return "seq";
5117a519f9SDimitry Andric   case FCOND_NGL:
5217a519f9SDimitry Andric   case FCOND_GL:  return "ngl";
5317a519f9SDimitry Andric   case FCOND_LT:
5417a519f9SDimitry Andric   case FCOND_NLT: return "lt";
5517a519f9SDimitry Andric   case FCOND_NGE:
5617a519f9SDimitry Andric   case FCOND_GE:  return "nge";
5717a519f9SDimitry Andric   case FCOND_LE:
5817a519f9SDimitry Andric   case FCOND_NLE: return "le";
5917a519f9SDimitry Andric   case FCOND_NGT:
6017a519f9SDimitry Andric   case FCOND_GT:  return "ngt";
6117a519f9SDimitry Andric   }
6217a519f9SDimitry Andric   llvm_unreachable("Impossible condition code!");
6317a519f9SDimitry Andric }
6417a519f9SDimitry Andric 
6517a519f9SDimitry Andric void MipsInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
66dff0c46cSDimitry Andric   OS << '$' << StringRef(getRegisterName(RegNo)).lower();
6717a519f9SDimitry Andric }
6817a519f9SDimitry Andric 
696122f3e6SDimitry Andric void MipsInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
706122f3e6SDimitry Andric                                 StringRef Annot) {
7117a519f9SDimitry Andric   printInstruction(MI, O);
726122f3e6SDimitry Andric   printAnnotation(O, Annot);
7317a519f9SDimitry Andric }
7417a519f9SDimitry Andric 
75dff0c46cSDimitry Andric static void printExpr(const MCExpr *Expr, raw_ostream &OS) {
76dff0c46cSDimitry Andric   int Offset = 0;
77dff0c46cSDimitry Andric   const MCSymbolRefExpr *SRE;
78dff0c46cSDimitry Andric 
79dff0c46cSDimitry Andric   if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
80dff0c46cSDimitry Andric     SRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS());
81dff0c46cSDimitry Andric     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(BE->getRHS());
82dff0c46cSDimitry Andric     assert(SRE && CE && "Binary expression must be sym+const.");
83dff0c46cSDimitry Andric     Offset = CE->getValue();
84dff0c46cSDimitry Andric   }
85dff0c46cSDimitry Andric   else if (!(SRE = dyn_cast<MCSymbolRefExpr>(Expr)))
86dff0c46cSDimitry Andric     assert(false && "Unexpected MCExpr type.");
87dff0c46cSDimitry Andric 
88dff0c46cSDimitry Andric   MCSymbolRefExpr::VariantKind Kind = SRE->getKind();
89dff0c46cSDimitry Andric 
90dff0c46cSDimitry Andric   switch (Kind) {
91dff0c46cSDimitry Andric   default:                                 llvm_unreachable("Invalid kind!");
92dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_None:           break;
93dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_GPREL:     OS << "%gp_rel("; break;
94dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_GOT_CALL:  OS << "%call16("; break;
95dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_GOT16:     OS << "%got(";    break;
96dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_GOT:       OS << "%got(";    break;
97dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_ABS_HI:    OS << "%hi(";     break;
98dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_ABS_LO:    OS << "%lo(";     break;
99dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_TLSGD:     OS << "%tlsgd(";  break;
100dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_TLSLDM:    OS << "%tlsldm(";  break;
101dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_DTPREL_HI: OS << "%dtprel_hi(";  break;
102dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_DTPREL_LO: OS << "%dtprel_lo(";  break;
103dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_GOTTPREL:  OS << "%gottprel("; break;
104dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_TPREL_HI:  OS << "%tprel_hi("; break;
105dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_TPREL_LO:  OS << "%tprel_lo("; break;
106dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_GPOFF_HI:  OS << "%hi(%neg(%gp_rel("; break;
107dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_GPOFF_LO:  OS << "%lo(%neg(%gp_rel("; break;
108dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_GOT_DISP:  OS << "%got_disp("; break;
109dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_GOT_PAGE:  OS << "%got_page("; break;
110dff0c46cSDimitry Andric   case MCSymbolRefExpr::VK_Mips_GOT_OFST:  OS << "%got_ofst("; break;
111dff0c46cSDimitry Andric   }
112dff0c46cSDimitry Andric 
113dff0c46cSDimitry Andric   OS << SRE->getSymbol();
114dff0c46cSDimitry Andric 
115dff0c46cSDimitry Andric   if (Offset) {
116dff0c46cSDimitry Andric     if (Offset > 0)
117dff0c46cSDimitry Andric       OS << '+';
118dff0c46cSDimitry Andric     OS << Offset;
119dff0c46cSDimitry Andric   }
120dff0c46cSDimitry Andric 
121dff0c46cSDimitry Andric   if ((Kind == MCSymbolRefExpr::VK_Mips_GPOFF_HI) ||
122dff0c46cSDimitry Andric       (Kind == MCSymbolRefExpr::VK_Mips_GPOFF_LO))
123dff0c46cSDimitry Andric     OS << ")))";
124dff0c46cSDimitry Andric   else if (Kind != MCSymbolRefExpr::VK_None)
125dff0c46cSDimitry Andric     OS << ')';
126dff0c46cSDimitry Andric }
127dff0c46cSDimitry Andric 
12817a519f9SDimitry Andric void MipsInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
12917a519f9SDimitry Andric                                    raw_ostream &O) {
13017a519f9SDimitry Andric   const MCOperand &Op = MI->getOperand(OpNo);
13117a519f9SDimitry Andric   if (Op.isReg()) {
13217a519f9SDimitry Andric     printRegName(O, Op.getReg());
13317a519f9SDimitry Andric     return;
13417a519f9SDimitry Andric   }
13517a519f9SDimitry Andric 
13617a519f9SDimitry Andric   if (Op.isImm()) {
13717a519f9SDimitry Andric     O << Op.getImm();
13817a519f9SDimitry Andric     return;
13917a519f9SDimitry Andric   }
14017a519f9SDimitry Andric 
14117a519f9SDimitry Andric   assert(Op.isExpr() && "unknown operand kind in printOperand");
142dff0c46cSDimitry Andric   printExpr(Op.getExpr(), O);
14317a519f9SDimitry Andric }
14417a519f9SDimitry Andric 
14517a519f9SDimitry Andric void MipsInstPrinter::printUnsignedImm(const MCInst *MI, int opNum,
14617a519f9SDimitry Andric                                        raw_ostream &O) {
14717a519f9SDimitry Andric   const MCOperand &MO = MI->getOperand(opNum);
14817a519f9SDimitry Andric   if (MO.isImm())
14917a519f9SDimitry Andric     O << (unsigned short int)MO.getImm();
15017a519f9SDimitry Andric   else
15117a519f9SDimitry Andric     printOperand(MI, opNum, O);
15217a519f9SDimitry Andric }
15317a519f9SDimitry Andric 
15417a519f9SDimitry Andric void MipsInstPrinter::
15517a519f9SDimitry Andric printMemOperand(const MCInst *MI, int opNum, raw_ostream &O) {
15617a519f9SDimitry Andric   // Load/Store memory operands -- imm($reg)
15717a519f9SDimitry Andric   // If PIC target the target is loaded as the
15817a519f9SDimitry Andric   // pattern lw $25,%call16($28)
15917a519f9SDimitry Andric   printOperand(MI, opNum+1, O);
16017a519f9SDimitry Andric   O << "(";
16117a519f9SDimitry Andric   printOperand(MI, opNum, O);
16217a519f9SDimitry Andric   O << ")";
16317a519f9SDimitry Andric }
16417a519f9SDimitry Andric 
16517a519f9SDimitry Andric void MipsInstPrinter::
16617a519f9SDimitry Andric printMemOperandEA(const MCInst *MI, int opNum, raw_ostream &O) {
16717a519f9SDimitry Andric   // when using stack locations for not load/store instructions
16817a519f9SDimitry Andric   // print the same way as all normal 3 operand instructions.
16917a519f9SDimitry Andric   printOperand(MI, opNum, O);
17017a519f9SDimitry Andric   O << ", ";
17117a519f9SDimitry Andric   printOperand(MI, opNum+1, O);
17217a519f9SDimitry Andric   return;
17317a519f9SDimitry Andric }
17417a519f9SDimitry Andric 
17517a519f9SDimitry Andric void MipsInstPrinter::
17617a519f9SDimitry Andric printFCCOperand(const MCInst *MI, int opNum, raw_ostream &O) {
17717a519f9SDimitry Andric   const MCOperand& MO = MI->getOperand(opNum);
17817a519f9SDimitry Andric   O << MipsFCCToString((Mips::CondCode)MO.getImm());
17917a519f9SDimitry Andric }
180