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