LLVM 24.0.0git
MipsInstPrinter.cpp
Go to the documentation of this file.
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 "Mips.h"
16#include "llvm/MC/MCAsmInfo.h"
17#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCInst.h"
19#include "llvm/MC/MCInstrInfo.h"
22#include "llvm/MC/MCSymbol.h"
25using namespace llvm;
26
27#define DEBUG_TYPE "asm-printer"
28
29#define PRINT_ALIAS_INSTR
30#include "MipsGenAsmWriter.inc"
31
32template<unsigned R>
33static bool isReg(const MCInst &MI, unsigned OpNo) {
34 assert(MI.getOperand(OpNo).isReg() && "Register operand expected.");
35 return MI.getOperand(OpNo).getReg() == R;
36}
37
39 switch (CC) {
40 case FCOND_F:
41 case FCOND_T: return "f";
42 case FCOND_UN:
43 case FCOND_OR: return "un";
44 case FCOND_OEQ:
45 case FCOND_UNE: return "eq";
46 case FCOND_UEQ:
47 case FCOND_ONE: return "ueq";
48 case FCOND_OLT:
49 case FCOND_UGE: return "olt";
50 case FCOND_ULT:
51 case FCOND_OGE: return "ult";
52 case FCOND_OLE:
53 case FCOND_UGT: return "ole";
54 case FCOND_ULE:
55 case FCOND_OGT: return "ule";
56 case FCOND_SF:
57 case FCOND_ST: return "sf";
58 case FCOND_NGLE:
59 case FCOND_GLE: return "ngle";
60 case FCOND_SEQ:
61 case FCOND_SNE: return "seq";
62 case FCOND_NGL:
63 case FCOND_GL: return "ngl";
64 case FCOND_LT:
65 case FCOND_NLT: return "lt";
66 case FCOND_NGE:
67 case FCOND_GE: return "nge";
68 case FCOND_LE:
69 case FCOND_NLE: return "le";
70 case FCOND_NGT:
71 case FCOND_GT: return "ngt";
72 }
73 llvm_unreachable("Impossible condition code!");
74}
75
76void MipsInstPrinter::printCOP0Operand(const MCInst *MI, unsigned OpNo,
77 const MCSubtargetInfo &STI,
78 raw_ostream &O) {
79 unsigned Encoding = MRI.getEncodingValue(MI->getOperand(OpNo).getReg());
80 markup(O, Markup::Register) << '$' << MIPS_MC::getCOP0RegNum(Encoding);
81 O << ", ";
83}
84
89
91 StringRef Annot, const MCSubtargetInfo &STI,
92 raw_ostream &O) {
93 switch (MI->getOpcode()) {
94 default:
95 break;
96 case Mips::RDHWR:
97 case Mips::RDHWR64:
98 O << "\t.set\tpush\n";
99 O << "\t.set\tmips32r2\n";
100 break;
101 case Mips::Save16:
102 O << "\tsave\t";
103 printSaveRestore(MI, STI, O);
104 O << " # 16 bit inst\n";
105 return;
106 case Mips::SaveX16:
107 O << "\tsave\t";
108 printSaveRestore(MI, STI, O);
109 O << "\n";
110 return;
111 case Mips::Restore16:
112 O << "\trestore\t";
113 printSaveRestore(MI, STI, O);
114 O << " # 16 bit inst\n";
115 return;
116 case Mips::RestoreX16:
117 O << "\trestore\t";
118 printSaveRestore(MI, STI, O);
119 O << "\n";
120 return;
121 }
122
123 // Try to print any aliases first.
124 if (!printAliasInstr(MI, Address, STI, O) &&
125 !printAlias(*MI, Address, STI, O))
126 printInstruction(MI, Address, STI, O);
127 printAnnotation(O, Annot);
128
129 switch (MI->getOpcode()) {
130 default:
131 break;
132 case Mips::RDHWR:
133 case Mips::RDHWR64:
134 O << "\n\t.set\tpop";
135 }
136}
137
138void MipsInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
139 const MCSubtargetInfo &STI, raw_ostream &O) {
140 const MCOperand &Op = MI->getOperand(OpNo);
141 if (Op.isReg()) {
142 printRegName(O, Op.getReg());
143 return;
144 }
145
146 if (Op.isImm()) {
147 markup(O, Markup::Immediate) << formatImm(Op.getImm());
148 return;
149 }
150
151 assert(Op.isExpr() && "unknown operand kind in printOperand");
152 MAI.printExpr(O, *Op.getExpr());
153}
154
155template <unsigned FromEnd>
156void MipsInstPrinter::printOperandFromEnd(const MCInst *MI, int OpNo,
157 const MCSubtargetInfo &STI,
158 raw_ostream &O) {
159 printOperand(MI, MI->getNumOperands() - FromEnd, STI, O);
160}
161
162void MipsInstPrinter::printJumpOperand(const MCInst *MI, unsigned OpNo,
163 const MCSubtargetInfo &STI,
164 raw_ostream &O) {
165 const MCOperand &Op = MI->getOperand(OpNo);
166 if (!Op.isImm())
167 return printOperand(MI, OpNo, STI, O);
168
170 markup(O, Markup::Immediate) << formatHex(Op.getImm());
171 else
172 markup(O, Markup::Immediate) << formatImm(Op.getImm());
173}
174
175void MipsInstPrinter::printBranchOperand(const MCInst *MI, uint64_t Address,
176 unsigned OpNo,
177 const MCSubtargetInfo &STI,
178 raw_ostream &O) {
179 const MCOperand &Op = MI->getOperand(OpNo);
180 if (!Op.isImm())
181 return printOperand(MI, OpNo, STI, O);
182
184 uint64_t Target = Address + Op.getImm();
185 if (STI.hasFeature(Mips::FeatureMips32))
186 Target &= 0xffffffff;
187 else if (STI.hasFeature(Mips::FeatureMips16))
188 Target &= 0xffff;
190 } else {
191 markup(O, Markup::Immediate) << formatImm(Op.getImm());
192 }
193}
194
195template <unsigned Bits, unsigned Offset>
196void MipsInstPrinter::printUImm(const MCInst *MI, int opNum,
197 const MCSubtargetInfo &STI, raw_ostream &O) {
198 const MCOperand &MO = MI->getOperand(opNum);
199 if (MO.isImm()) {
200 uint64_t Imm = MO.getImm();
201 Imm -= Offset;
202 Imm &= (1 << Bits) - 1;
203 Imm += Offset;
205 return;
206 }
207
208 printOperand(MI, opNum, STI, O);
209}
210
211void MipsInstPrinter::printMemOperand(const MCInst *MI, int opNum,
212 const MCSubtargetInfo &STI,
213 raw_ostream &O) {
214 // Load/Store memory operands -- imm($reg)
215 // If PIC target the target is loaded as the
216 // pattern lw $25,%call16($28)
217
219 printOperand(MI, opNum + 1, STI, O);
220 O << "(";
221 printOperand(MI, opNum, STI, O);
222 O << ")";
223}
224
225void MipsInstPrinter::printFCCOperand(const MCInst *MI, int opNum,
226 const MCSubtargetInfo & /* STI */,
227 raw_ostream &O) {
228 const MCOperand &MO = MI->getOperand(opNum);
230}
231
232void MipsInstPrinter::
233printSHFMask(const MCInst *MI, int opNum, raw_ostream &O) {
234 llvm_unreachable("TODO");
235}
236
237bool MipsInstPrinter::printAlias(const char *Str, const MCInst &MI,
238 uint64_t Address, unsigned OpNo,
239 const MCSubtargetInfo &STI, raw_ostream &OS,
240 bool IsBranch) {
241 OS << "\t" << Str << "\t";
242 if (IsBranch)
243 printBranchOperand(&MI, Address, OpNo, STI, OS);
244 else
245 printOperand(&MI, OpNo, STI, OS);
246 return true;
247}
248
249bool MipsInstPrinter::printAlias(const char *Str, const MCInst &MI,
250 uint64_t Address, unsigned OpNo0,
251 unsigned OpNo1, const MCSubtargetInfo &STI,
252 raw_ostream &OS, bool IsBranch) {
253 printAlias(Str, MI, Address, OpNo0, STI, OS, IsBranch);
254 OS << ", ";
255 if (IsBranch)
256 printBranchOperand(&MI, Address, OpNo1, STI, OS);
257 else
258 printOperand(&MI, OpNo1, STI, OS);
259 return true;
260}
261
262bool MipsInstPrinter::printAlias(const MCInst &MI, uint64_t Address,
263 const MCSubtargetInfo &STI, raw_ostream &OS) {
264 switch (MI.getOpcode()) {
265 case Mips::BEQ:
266 case Mips::BEQ_MM:
267 // beq $zero, $zero, $L2 => b $L2
268 // beq $r0, $zero, $L2 => beqz $r0, $L2
269 return (isReg<Mips::R0>(MI, 0) && isReg<Mips::R0>(MI, 1) &&
270 printAlias("b", MI, Address, 2, STI, OS, true)) ||
271 (isReg<Mips::R0>(MI, 1) &&
272 printAlias("beqz", MI, Address, 0, 2, STI, OS, true));
273 case Mips::BEQ64:
274 // beq $r0, $zero, $L2 => beqz $r0, $L2
275 return isReg<Mips::R0_64>(MI, 1) &&
276 printAlias("beqz", MI, Address, 0, 2, STI, OS, true);
277 case Mips::BNE:
278 case Mips::BNE_MM:
279 // bne $r0, $zero, $L2 => bnez $r0, $L2
280 return isReg<Mips::R0>(MI, 1) &&
281 printAlias("bnez", MI, Address, 0, 2, STI, OS, true);
282 case Mips::BNE64:
283 // bne $r0, $zero, $L2 => bnez $r0, $L2
284 return isReg<Mips::R0_64>(MI, 1) &&
285 printAlias("bnez", MI, Address, 0, 2, STI, OS, true);
286 case Mips::BGEZAL:
287 // bgezal $zero, $L1 => bal $L1
288 return isReg<Mips::R0>(MI, 0) &&
289 printAlias("bal", MI, Address, 1, STI, OS, true);
290 case Mips::BC1T:
291 // bc1t $fcc0, $L1 => bc1t $L1
292 return isReg<Mips::FCC0>(MI, 0) &&
293 printAlias("bc1t", MI, Address, 1, STI, OS, true);
294 case Mips::BC1F:
295 // bc1f $fcc0, $L1 => bc1f $L1
296 return isReg<Mips::FCC0>(MI, 0) &&
297 printAlias("bc1f", MI, Address, 1, STI, OS, true);
298 case Mips::JALR:
299 // jalr $zero, $r1 => jr $r1
300 // jalr $ra, $r1 => jalr $r1
301 return (isReg<Mips::R0>(MI, 0) &&
302 printAlias("jr", MI, Address, 1, STI, OS)) ||
303 (isReg<Mips::RA>(MI, 0) &&
304 printAlias("jalr", MI, Address, 1, STI, OS));
305 case Mips::JALR64:
306 // jalr $zero, $r1 => jr $r1
307 // jalr $ra, $r1 => jalr $r1
308 return (isReg<Mips::R0_64>(MI, 0) &&
309 printAlias("jr", MI, Address, 1, STI, OS)) ||
310 (isReg<Mips::RA_64>(MI, 0) &&
311 printAlias("jalr", MI, Address, 1, STI, OS));
312 case Mips::NOR:
313 case Mips::NOR_MM:
314 case Mips::NOR_MMR6:
315 // nor $r0, $r1, $zero => not $r0, $r1
316 return isReg<Mips::R0>(MI, 2) &&
317 printAlias("not", MI, Address, 0, 1, STI, OS);
318 case Mips::NOR64:
319 // nor $r0, $r1, $zero => not $r0, $r1
320 return isReg<Mips::R0_64>(MI, 2) &&
321 printAlias("not", MI, Address, 0, 1, STI, OS);
322 case Mips::OR:
323 case Mips::ADDu:
324 // or $r0, $r1, $zero => move $r0, $r1
325 // addu $r0, $r1, $zero => move $r0, $r1
326 return isReg<Mips::R0>(MI, 2) &&
327 printAlias("move", MI, Address, 0, 1, STI, OS);
328 default:
329 return false;
330 }
331}
332
333void MipsInstPrinter::printSaveRestore(const MCInst *MI,
334 const MCSubtargetInfo &STI,
335 raw_ostream &O) {
336 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
337 if (i != 0) O << ", ";
338 if (MI->getOperand(i).isReg())
339 printRegName(O, MI->getOperand(i).getReg());
340 else
341 printUImm<16>(MI, i, STI, O);
342 }
343}
344
345void MipsInstPrinter::printRegisterList(const MCInst *MI, int opNum,
346 const MCSubtargetInfo & /* STI */,
347 raw_ostream &O) {
348 // - 2 because register List is always first operand of instruction and it is
349 // always followed by memory operand (base + offset).
350 for (int i = opNum, e = MI->getNumOperands() - 2; i != e; ++i) {
351 if (i != opNum)
352 O << ", ";
353 printRegName(O, MI->getOperand(i).getReg());
354 }
355}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
uint64_t Imm
IRTranslator LLVM IR MI
static bool isReg(const MCInst &MI, unsigned OpNo)
RISCVCC::CondCode CC
This file contains some functions that are useful when dealing with strings.
WithMarkup markup(raw_ostream &OS, Markup M)
format_object< int64_t > formatHex(int64_t Value) const
const MCRegisterInfo & MRI
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
const MCAsmInfo & MAI
format_object< int64_t > formatImm(int64_t Value) const
Utility function to print immediates in decimal or hex.
bool PrintBranchImmAsAddress
If true, a branch immediate (e.g.
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
Instances of this class represent operands of the MCInst class.
Definition MCInst.h:40
int64_t getImm() const
Definition MCInst.h:84
bool isImm() const
Definition MCInst.h:66
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
void printCOP0Operand(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
static const char * getRegisterName(MCRegister Reg, unsigned AltIdx=Mips::NoRegAltName)
void printRegName(raw_ostream &OS, MCRegister Reg) override
Print the assembler register name.
void printInstruction(const MCInst *MI, uint64_t Address, const MCSubtargetInfo &STI, raw_ostream &O)
void printInst(const MCInst *MI, uint64_t Address, StringRef Annot, const MCSubtargetInfo &STI, raw_ostream &O) override
Print the specified MCInst to the specified raw_ostream.
bool printAliasInstr(const MCInst *MI, uint64_t Address, const MCSubtargetInfo &STI, raw_ostream &OS)
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM_ABI std::string lower() const
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr unsigned getCOP0RegNum(unsigned Encoding)
constexpr unsigned getCOP0Sel(unsigned Encoding)
const char * MipsFCCToString(Mips::CondCode CC)
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
DWARFExpression::Operation Op