1 //===-- MipsInstPrinter.cpp - Convert Mips MCInst to assembly syntax ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class prints an Mips MCInst to a .s file.
12 //===----------------------------------------------------------------------===//
14 #include "MipsInstPrinter.h"
15 #include "MCTargetDesc/MipsMCExpr.h"
16 #include "MipsInstrInfo.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCInstrInfo.h"
21 #include "llvm/MC/MCSymbol.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/raw_ostream.h"
26 #define DEBUG_TYPE "asm-printer"
28 #define PRINT_ALIAS_INSTR
29 #include "MipsGenAsmWriter.inc"
32 static bool isReg(const MCInst &MI, unsigned OpNo) {
33 assert(MI.getOperand(OpNo).isReg() && "Register operand expected.");
34 return MI.getOperand(OpNo).getReg() == R;
37 const char* Mips::MipsFCCToString(Mips::CondCode CC) {
40 case FCOND_T: return "f";
42 case FCOND_OR: return "un";
44 case FCOND_UNE: return "eq";
46 case FCOND_ONE: return "ueq";
48 case FCOND_UGE: return "olt";
50 case FCOND_OGE: return "ult";
52 case FCOND_UGT: return "ole";
54 case FCOND_OGT: return "ule";
56 case FCOND_ST: return "sf";
58 case FCOND_GLE: return "ngle";
60 case FCOND_SNE: return "seq";
62 case FCOND_GL: return "ngl";
64 case FCOND_NLT: return "lt";
66 case FCOND_GE: return "nge";
68 case FCOND_NLE: return "le";
70 case FCOND_GT: return "ngt";
72 llvm_unreachable("Impossible condition code!");
75 void MipsInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
76 OS << '$' << StringRef(getRegisterName(RegNo)).lower();
79 void MipsInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
80 StringRef Annot, const MCSubtargetInfo &STI) {
81 switch (MI->getOpcode()) {
86 O << "\t.set\tpush\n";
87 O << "\t.set\tmips32r2\n";
91 printSaveRestore(MI, O);
92 O << " # 16 bit inst\n";
96 printSaveRestore(MI, O);
101 printSaveRestore(MI, O);
102 O << " # 16 bit inst\n";
104 case Mips::RestoreX16:
106 printSaveRestore(MI, O);
111 // Try to print any aliases first.
112 if (!printAliasInstr(MI, O) && !printAlias(*MI, O))
113 printInstruction(MI, O);
114 printAnnotation(O, Annot);
116 switch (MI->getOpcode()) {
121 O << "\n\t.set\tpop";
125 static void printExpr(const MCExpr *Expr, const MCAsmInfo *MAI,
128 const MCSymbolRefExpr *SRE;
130 if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
131 SRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS());
132 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(BE->getRHS());
133 assert(SRE && CE && "Binary expression must be sym+const.");
134 Offset = CE->getValue();
135 } else if (const MipsMCExpr *ME = dyn_cast<MipsMCExpr>(Expr)) {
139 SRE = cast<MCSymbolRefExpr>(Expr);
141 MCSymbolRefExpr::VariantKind Kind = SRE->getKind();
144 default: llvm_unreachable("Invalid kind!");
145 case MCSymbolRefExpr::VK_None: break;
146 case MCSymbolRefExpr::VK_Mips_GPREL: OS << "%gp_rel("; break;
147 case MCSymbolRefExpr::VK_Mips_GOT_CALL: OS << "%call16("; break;
148 case MCSymbolRefExpr::VK_Mips_GOT16: OS << "%got("; break;
149 case MCSymbolRefExpr::VK_Mips_GOT: OS << "%got("; break;
150 case MCSymbolRefExpr::VK_Mips_ABS_HI: OS << "%hi("; break;
151 case MCSymbolRefExpr::VK_Mips_ABS_LO: OS << "%lo("; break;
152 case MCSymbolRefExpr::VK_Mips_TLSGD: OS << "%tlsgd("; break;
153 case MCSymbolRefExpr::VK_Mips_TLSLDM: OS << "%tlsldm("; break;
154 case MCSymbolRefExpr::VK_Mips_DTPREL_HI: OS << "%dtprel_hi("; break;
155 case MCSymbolRefExpr::VK_Mips_DTPREL_LO: OS << "%dtprel_lo("; break;
156 case MCSymbolRefExpr::VK_Mips_GOTTPREL: OS << "%gottprel("; break;
157 case MCSymbolRefExpr::VK_Mips_TPREL_HI: OS << "%tprel_hi("; break;
158 case MCSymbolRefExpr::VK_Mips_TPREL_LO: OS << "%tprel_lo("; break;
159 case MCSymbolRefExpr::VK_Mips_GPOFF_HI: OS << "%hi(%neg(%gp_rel("; break;
160 case MCSymbolRefExpr::VK_Mips_GPOFF_LO: OS << "%lo(%neg(%gp_rel("; break;
161 case MCSymbolRefExpr::VK_Mips_GOT_DISP: OS << "%got_disp("; break;
162 case MCSymbolRefExpr::VK_Mips_GOT_PAGE: OS << "%got_page("; break;
163 case MCSymbolRefExpr::VK_Mips_GOT_OFST: OS << "%got_ofst("; break;
164 case MCSymbolRefExpr::VK_Mips_HIGHER: OS << "%higher("; break;
165 case MCSymbolRefExpr::VK_Mips_HIGHEST: OS << "%highest("; break;
166 case MCSymbolRefExpr::VK_Mips_GOT_HI16: OS << "%got_hi("; break;
167 case MCSymbolRefExpr::VK_Mips_GOT_LO16: OS << "%got_lo("; break;
168 case MCSymbolRefExpr::VK_Mips_CALL_HI16: OS << "%call_hi("; break;
169 case MCSymbolRefExpr::VK_Mips_CALL_LO16: OS << "%call_lo("; break;
170 case MCSymbolRefExpr::VK_Mips_PCREL_HI16: OS << "%pcrel_hi("; break;
171 case MCSymbolRefExpr::VK_Mips_PCREL_LO16: OS << "%pcrel_lo("; break;
174 SRE->getSymbol().print(OS, MAI);
182 if ((Kind == MCSymbolRefExpr::VK_Mips_GPOFF_HI) ||
183 (Kind == MCSymbolRefExpr::VK_Mips_GPOFF_LO))
185 else if (Kind != MCSymbolRefExpr::VK_None)
189 void MipsInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
191 const MCOperand &Op = MI->getOperand(OpNo);
193 printRegName(O, Op.getReg());
202 assert(Op.isExpr() && "unknown operand kind in printOperand");
203 printExpr(Op.getExpr(), &MAI, O);
206 void MipsInstPrinter::printUnsignedImm(const MCInst *MI, int opNum,
208 const MCOperand &MO = MI->getOperand(opNum);
210 O << (unsigned short int)MO.getImm();
212 printOperand(MI, opNum, O);
215 void MipsInstPrinter::printUnsignedImm8(const MCInst *MI, int opNum,
217 const MCOperand &MO = MI->getOperand(opNum);
219 O << (unsigned short int)(unsigned char)MO.getImm();
221 printOperand(MI, opNum, O);
224 void MipsInstPrinter::
225 printMemOperand(const MCInst *MI, int opNum, raw_ostream &O) {
226 // Load/Store memory operands -- imm($reg)
227 // If PIC target the target is loaded as the
228 // pattern lw $25,%call16($28)
230 // opNum can be invalid if instruction had reglist as operand.
231 // MemOperand is always last operand of instruction (base + offset).
232 switch (MI->getOpcode()) {
238 case Mips::SWM16_MMR6:
240 case Mips::LWM16_MMR6:
241 opNum = MI->getNumOperands() - 2;
245 printOperand(MI, opNum+1, O);
247 printOperand(MI, opNum, O);
251 void MipsInstPrinter::
252 printMemOperandEA(const MCInst *MI, int opNum, raw_ostream &O) {
253 // when using stack locations for not load/store instructions
254 // print the same way as all normal 3 operand instructions.
255 printOperand(MI, opNum, O);
257 printOperand(MI, opNum+1, O);
261 void MipsInstPrinter::
262 printFCCOperand(const MCInst *MI, int opNum, raw_ostream &O) {
263 const MCOperand& MO = MI->getOperand(opNum);
264 O << MipsFCCToString((Mips::CondCode)MO.getImm());
267 void MipsInstPrinter::
268 printRegisterPair(const MCInst *MI, int opNum, raw_ostream &O) {
269 printRegName(O, MI->getOperand(opNum).getReg());
272 void MipsInstPrinter::
273 printSHFMask(const MCInst *MI, int opNum, raw_ostream &O) {
274 llvm_unreachable("TODO");
277 bool MipsInstPrinter::printAlias(const char *Str, const MCInst &MI,
278 unsigned OpNo, raw_ostream &OS) {
279 OS << "\t" << Str << "\t";
280 printOperand(&MI, OpNo, OS);
284 bool MipsInstPrinter::printAlias(const char *Str, const MCInst &MI,
285 unsigned OpNo0, unsigned OpNo1,
287 printAlias(Str, MI, OpNo0, OS);
289 printOperand(&MI, OpNo1, OS);
293 bool MipsInstPrinter::printAlias(const MCInst &MI, raw_ostream &OS) {
294 switch (MI.getOpcode()) {
297 // beq $zero, $zero, $L2 => b $L2
298 // beq $r0, $zero, $L2 => beqz $r0, $L2
299 return (isReg<Mips::ZERO>(MI, 0) && isReg<Mips::ZERO>(MI, 1) &&
300 printAlias("b", MI, 2, OS)) ||
301 (isReg<Mips::ZERO>(MI, 1) && printAlias("beqz", MI, 0, 2, OS));
303 // beq $r0, $zero, $L2 => beqz $r0, $L2
304 return isReg<Mips::ZERO_64>(MI, 1) && printAlias("beqz", MI, 0, 2, OS);
306 // bne $r0, $zero, $L2 => bnez $r0, $L2
307 return isReg<Mips::ZERO>(MI, 1) && printAlias("bnez", MI, 0, 2, OS);
309 // bne $r0, $zero, $L2 => bnez $r0, $L2
310 return isReg<Mips::ZERO_64>(MI, 1) && printAlias("bnez", MI, 0, 2, OS);
312 // bgezal $zero, $L1 => bal $L1
313 return isReg<Mips::ZERO>(MI, 0) && printAlias("bal", MI, 1, OS);
315 // bc1t $fcc0, $L1 => bc1t $L1
316 return isReg<Mips::FCC0>(MI, 0) && printAlias("bc1t", MI, 1, OS);
318 // bc1f $fcc0, $L1 => bc1f $L1
319 return isReg<Mips::FCC0>(MI, 0) && printAlias("bc1f", MI, 1, OS);
321 // jalr $ra, $r1 => jalr $r1
322 return isReg<Mips::RA>(MI, 0) && printAlias("jalr", MI, 1, OS);
324 // jalr $ra, $r1 => jalr $r1
325 return isReg<Mips::RA_64>(MI, 0) && printAlias("jalr", MI, 1, OS);
328 // nor $r0, $r1, $zero => not $r0, $r1
329 return isReg<Mips::ZERO>(MI, 2) && printAlias("not", MI, 0, 1, OS);
331 // nor $r0, $r1, $zero => not $r0, $r1
332 return isReg<Mips::ZERO_64>(MI, 2) && printAlias("not", MI, 0, 1, OS);
334 // or $r0, $r1, $zero => move $r0, $r1
335 return isReg<Mips::ZERO>(MI, 2) && printAlias("move", MI, 0, 1, OS);
336 default: return false;
340 void MipsInstPrinter::printSaveRestore(const MCInst *MI, raw_ostream &O) {
341 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
342 if (i != 0) O << ", ";
343 if (MI->getOperand(i).isReg())
344 printRegName(O, MI->getOperand(i).getReg());
346 printUnsignedImm(MI, i, O);
350 void MipsInstPrinter::
351 printRegisterList(const MCInst *MI, int opNum, raw_ostream &O) {
352 // - 2 because register List is always first operand of instruction and it is
353 // always followed by memory operand (base + offset).
354 for (int i = opNum, e = MI->getNumOperands() - 2; i != e; ++i) {
357 printRegName(O, MI->getOperand(i).getReg());