1 //===-- MipsInstPrinter.cpp - Convert Mips MCInst to assembly syntax ------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This class prints an Mips MCInst to a .s file.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "MipsInstPrinter.h"
14 #include "MipsInstrInfo.h"
15 #include "MipsMCExpr.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCInstrInfo.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/raw_ostream.h"
23 using namespace llvm;
24
25 #define DEBUG_TYPE "asm-printer"
26
27 #define PRINT_ALIAS_INSTR
28 #include "MipsGenAsmWriter.inc"
29
30 template<unsigned R>
isReg(const MCInst & MI,unsigned OpNo)31 static bool isReg(const MCInst &MI, unsigned OpNo) {
32 assert(MI.getOperand(OpNo).isReg() && "Register operand expected.");
33 return MI.getOperand(OpNo).getReg() == R;
34 }
35
MipsFCCToString(Mips::CondCode CC)36 const char* Mips::MipsFCCToString(Mips::CondCode CC) {
37 switch (CC) {
38 case FCOND_F:
39 case FCOND_T: return "f";
40 case FCOND_UN:
41 case FCOND_OR: return "un";
42 case FCOND_OEQ:
43 case FCOND_UNE: return "eq";
44 case FCOND_UEQ:
45 case FCOND_ONE: return "ueq";
46 case FCOND_OLT:
47 case FCOND_UGE: return "olt";
48 case FCOND_ULT:
49 case FCOND_OGE: return "ult";
50 case FCOND_OLE:
51 case FCOND_UGT: return "ole";
52 case FCOND_ULE:
53 case FCOND_OGT: return "ule";
54 case FCOND_SF:
55 case FCOND_ST: return "sf";
56 case FCOND_NGLE:
57 case FCOND_GLE: return "ngle";
58 case FCOND_SEQ:
59 case FCOND_SNE: return "seq";
60 case FCOND_NGL:
61 case FCOND_GL: return "ngl";
62 case FCOND_LT:
63 case FCOND_NLT: return "lt";
64 case FCOND_NGE:
65 case FCOND_GE: return "nge";
66 case FCOND_LE:
67 case FCOND_NLE: return "le";
68 case FCOND_NGT:
69 case FCOND_GT: return "ngt";
70 }
71 llvm_unreachable("Impossible condition code!");
72 }
73
printRegName(raw_ostream & OS,unsigned RegNo) const74 void MipsInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
75 OS << '$' << StringRef(getRegisterName(RegNo)).lower();
76 }
77
printInst(const MCInst * MI,uint64_t Address,StringRef Annot,const MCSubtargetInfo & STI,raw_ostream & O)78 void MipsInstPrinter::printInst(const MCInst *MI, uint64_t Address,
79 StringRef Annot, const MCSubtargetInfo &STI,
80 raw_ostream &O) {
81 switch (MI->getOpcode()) {
82 default:
83 break;
84 case Mips::RDHWR:
85 case Mips::RDHWR64:
86 O << "\t.set\tpush\n";
87 O << "\t.set\tmips32r2\n";
88 break;
89 case Mips::Save16:
90 O << "\tsave\t";
91 printSaveRestore(MI, O);
92 O << " # 16 bit inst\n";
93 return;
94 case Mips::SaveX16:
95 O << "\tsave\t";
96 printSaveRestore(MI, O);
97 O << "\n";
98 return;
99 case Mips::Restore16:
100 O << "\trestore\t";
101 printSaveRestore(MI, O);
102 O << " # 16 bit inst\n";
103 return;
104 case Mips::RestoreX16:
105 O << "\trestore\t";
106 printSaveRestore(MI, O);
107 O << "\n";
108 return;
109 }
110
111 // Try to print any aliases first.
112 if (!printAliasInstr(MI, Address, O) && !printAlias(*MI, O))
113 printInstruction(MI, Address, O);
114 printAnnotation(O, Annot);
115
116 switch (MI->getOpcode()) {
117 default:
118 break;
119 case Mips::RDHWR:
120 case Mips::RDHWR64:
121 O << "\n\t.set\tpop";
122 }
123 }
124
printOperand(const MCInst * MI,unsigned OpNo,raw_ostream & O)125 void MipsInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
126 raw_ostream &O) {
127 const MCOperand &Op = MI->getOperand(OpNo);
128 if (Op.isReg()) {
129 printRegName(O, Op.getReg());
130 return;
131 }
132
133 if (Op.isImm()) {
134 O << formatImm(Op.getImm());
135 return;
136 }
137
138 assert(Op.isExpr() && "unknown operand kind in printOperand");
139 Op.getExpr()->print(O, &MAI, true);
140 }
141
142 template <unsigned Bits, unsigned Offset>
printUImm(const MCInst * MI,int opNum,raw_ostream & O)143 void MipsInstPrinter::printUImm(const MCInst *MI, int opNum, raw_ostream &O) {
144 const MCOperand &MO = MI->getOperand(opNum);
145 if (MO.isImm()) {
146 uint64_t Imm = MO.getImm();
147 Imm -= Offset;
148 Imm &= (1 << Bits) - 1;
149 Imm += Offset;
150 O << formatImm(Imm);
151 return;
152 }
153
154 printOperand(MI, opNum, O);
155 }
156
157 void MipsInstPrinter::
printMemOperand(const MCInst * MI,int opNum,raw_ostream & O)158 printMemOperand(const MCInst *MI, int opNum, raw_ostream &O) {
159 if (opNum+1 >= (int)MI->getNumOperands()) {
160 printOperand(MI, opNum, O);
161 return;
162 }
163 // Load/Store memory operands -- imm($reg)
164 // If PIC target the target is loaded as the
165 // pattern lw $25,%call16($28)
166
167 // opNum can be invalid if instruction had reglist as operand.
168 // MemOperand is always last operand of instruction (base + offset).
169 switch (MI->getOpcode()) {
170 default:
171 break;
172 case Mips::SWM32_MM:
173 case Mips::LWM32_MM:
174 case Mips::SWM16_MM:
175 case Mips::SWM16_MMR6:
176 case Mips::LWM16_MM:
177 case Mips::LWM16_MMR6:
178 opNum = MI->getNumOperands() - 2;
179 break;
180 }
181
182 printOperand(MI, opNum+1, O);
183 O << "(";
184 printOperand(MI, opNum, O);
185 O << ")";
186 }
187
188 void MipsInstPrinter::
printMemOperandEA(const MCInst * MI,int opNum,raw_ostream & O)189 printMemOperandEA(const MCInst *MI, int opNum, raw_ostream &O) {
190 // when using stack locations for not load/store instructions
191 // print the same way as all normal 3 operand instructions.
192 printOperand(MI, opNum, O);
193 O << ", ";
194 printOperand(MI, opNum+1, O);
195 }
196
197 void MipsInstPrinter::
printFCCOperand(const MCInst * MI,int opNum,raw_ostream & O)198 printFCCOperand(const MCInst *MI, int opNum, raw_ostream &O) {
199 const MCOperand& MO = MI->getOperand(opNum);
200 O << MipsFCCToString((Mips::CondCode)MO.getImm());
201 }
202
203 void MipsInstPrinter::
printSHFMask(const MCInst * MI,int opNum,raw_ostream & O)204 printSHFMask(const MCInst *MI, int opNum, raw_ostream &O) {
205 llvm_unreachable("TODO");
206 }
207
printAlias(const char * Str,const MCInst & MI,unsigned OpNo,raw_ostream & OS)208 bool MipsInstPrinter::printAlias(const char *Str, const MCInst &MI,
209 unsigned OpNo, raw_ostream &OS) {
210 OS << "\t" << Str << "\t";
211 printOperand(&MI, OpNo, OS);
212 return true;
213 }
214
printAlias(const char * Str,const MCInst & MI,unsigned OpNo0,unsigned OpNo1,raw_ostream & OS)215 bool MipsInstPrinter::printAlias(const char *Str, const MCInst &MI,
216 unsigned OpNo0, unsigned OpNo1,
217 raw_ostream &OS) {
218 printAlias(Str, MI, OpNo0, OS);
219 OS << ", ";
220 printOperand(&MI, OpNo1, OS);
221 return true;
222 }
223
printAlias(const MCInst & MI,raw_ostream & OS)224 bool MipsInstPrinter::printAlias(const MCInst &MI, raw_ostream &OS) {
225 switch (MI.getOpcode()) {
226 case Mips::BEQ:
227 case Mips::BEQ_MM:
228 // beq $zero, $zero, $L2 => b $L2
229 // beq $r0, $zero, $L2 => beqz $r0, $L2
230 return (isReg<Mips::ZERO>(MI, 0) && isReg<Mips::ZERO>(MI, 1) &&
231 printAlias("b", MI, 2, OS)) ||
232 (isReg<Mips::ZERO>(MI, 1) && printAlias("beqz", MI, 0, 2, OS));
233 case Mips::BEQ64:
234 // beq $r0, $zero, $L2 => beqz $r0, $L2
235 return isReg<Mips::ZERO_64>(MI, 1) && printAlias("beqz", MI, 0, 2, OS);
236 case Mips::BNE:
237 case Mips::BNE_MM:
238 // bne $r0, $zero, $L2 => bnez $r0, $L2
239 return isReg<Mips::ZERO>(MI, 1) && printAlias("bnez", MI, 0, 2, OS);
240 case Mips::BNE64:
241 // bne $r0, $zero, $L2 => bnez $r0, $L2
242 return isReg<Mips::ZERO_64>(MI, 1) && printAlias("bnez", MI, 0, 2, OS);
243 case Mips::BGEZAL:
244 // bgezal $zero, $L1 => bal $L1
245 return isReg<Mips::ZERO>(MI, 0) && printAlias("bal", MI, 1, OS);
246 case Mips::BC1T:
247 // bc1t $fcc0, $L1 => bc1t $L1
248 return isReg<Mips::FCC0>(MI, 0) && printAlias("bc1t", MI, 1, OS);
249 case Mips::BC1F:
250 // bc1f $fcc0, $L1 => bc1f $L1
251 return isReg<Mips::FCC0>(MI, 0) && printAlias("bc1f", MI, 1, OS);
252 case Mips::JALR:
253 // jalr $ra, $r1 => jalr $r1
254 return isReg<Mips::RA>(MI, 0) && printAlias("jalr", MI, 1, OS);
255 case Mips::JALR64:
256 // jalr $ra, $r1 => jalr $r1
257 return isReg<Mips::RA_64>(MI, 0) && printAlias("jalr", MI, 1, OS);
258 case Mips::NOR:
259 case Mips::NOR_MM:
260 case Mips::NOR_MMR6:
261 // nor $r0, $r1, $zero => not $r0, $r1
262 return isReg<Mips::ZERO>(MI, 2) && printAlias("not", MI, 0, 1, OS);
263 case Mips::NOR64:
264 // nor $r0, $r1, $zero => not $r0, $r1
265 return isReg<Mips::ZERO_64>(MI, 2) && printAlias("not", MI, 0, 1, OS);
266 case Mips::OR:
267 // or $r0, $r1, $zero => move $r0, $r1
268 return isReg<Mips::ZERO>(MI, 2) && printAlias("move", MI, 0, 1, OS);
269 default: return false;
270 }
271 }
272
printSaveRestore(const MCInst * MI,raw_ostream & O)273 void MipsInstPrinter::printSaveRestore(const MCInst *MI, raw_ostream &O) {
274 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
275 if (i != 0) O << ", ";
276 if (MI->getOperand(i).isReg())
277 printRegName(O, MI->getOperand(i).getReg());
278 else
279 printUImm<16>(MI, i, O);
280 }
281 }
282
283 void MipsInstPrinter::
printRegisterList(const MCInst * MI,int opNum,raw_ostream & O)284 printRegisterList(const MCInst *MI, int opNum, raw_ostream &O) {
285 // - 2 because register List is always first operand of instruction and it is
286 // always followed by memory operand (base + offset).
287 for (int i = opNum, e = MI->getNumOperands() - 2; i != e; ++i) {
288 if (i != opNum)
289 O << ", ";
290 printRegName(O, MI->getOperand(i).getReg());
291 }
292 }
293