LLVM 22.0.0git
RISCVInstPrinter.cpp
Go to the documentation of this file.
1//===-- RISCVInstPrinter.cpp - Convert RISC-V MCInst to asm 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 RISC-V MCInst to a .s file.
10//
11//===----------------------------------------------------------------------===//
12
13#include "RISCVInstPrinter.h"
14#include "RISCVBaseInfo.h"
15#include "llvm/MC/MCAsmInfo.h"
16#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCInst.h"
20#include "llvm/MC/MCSymbol.h"
23using namespace llvm;
24
25#define DEBUG_TYPE "asm-printer"
26
27// Include the auto-generated portion of the assembly writer.
28#define PRINT_ALIAS_INSTR
29#include "RISCVGenAsmWriter.inc"
30
31static cl::opt<bool>
32 NoAliases("riscv-no-aliases",
33 cl::desc("Disable the emission of assembler pseudo instructions"),
34 cl::init(false), cl::Hidden);
35
36static cl::opt<bool> EmitX8AsFP("riscv-emit-x8-as-fp",
37 cl::desc("Emit x8 as fp instead of s0"),
38 cl::init(false), cl::Hidden);
39
40// Print architectural register names rather than the ABI names (such as x2
41// instead of sp).
42// TODO: Make RISCVInstPrinter::getRegisterName non-static so that this can a
43// member.
44static bool ArchRegNames;
45
46// The command-line flags above are used by llvm-mc and llc. They can be used by
47// `llvm-objdump`, but we override their values here to handle options passed to
48// `llvm-objdump` with `-M` (which matches GNU objdump). There did not seem to
49// be an easier way to allow these options in all these tools, without doing it
50// this way.
52 if (Opt == "no-aliases") {
53 PrintAliases = false;
54 return true;
55 }
56 if (Opt == "numeric") {
57 ArchRegNames = true;
58 return true;
59 }
60 if (Opt == "emit-x8-as-fp") {
61 if (!ArchRegNames)
62 EmitX8AsFP = true;
63 return true;
64 }
65
66 return false;
67}
68
70 StringRef Annot, const MCSubtargetInfo &STI,
71 raw_ostream &O) {
72 bool Res = false;
73 const MCInst *NewMI = MI;
74 MCInst UncompressedMI;
75 if (PrintAliases && !NoAliases)
76 Res = RISCVRVC::uncompress(UncompressedMI, *MI, STI);
77 if (Res)
78 NewMI = &UncompressedMI;
79 if (!PrintAliases || NoAliases || !printAliasInstr(NewMI, Address, STI, O))
80 printInstruction(NewMI, Address, STI, O);
81 printAnnotation(O, Annot);
82}
83
86}
87
88void RISCVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
89 const MCSubtargetInfo &STI,
90 raw_ostream &O) {
91 const MCOperand &MO = MI->getOperand(OpNo);
92
93 if (MO.isReg()) {
94 printRegName(O, MO.getReg());
95 return;
96 }
97
98 if (MO.isImm()) {
100 return;
101 }
102
103 assert(MO.isExpr() && "Unknown operand kind in printOperand");
104 MAI.printExpr(O, *MO.getExpr());
105}
106
108 unsigned OpNo,
109 const MCSubtargetInfo &STI,
110 raw_ostream &O) {
111 const MCOperand &MO = MI->getOperand(OpNo);
112 if (!MO.isImm())
113 return printOperand(MI, OpNo, STI, O);
114
116 uint64_t Target = Address + MO.getImm();
117 if (!STI.hasFeature(RISCV::Feature64Bit))
118 Target &= 0xffffffff;
120 } else {
122 }
123}
124
126 const MCSubtargetInfo &STI,
127 raw_ostream &O) {
128 unsigned Imm = MI->getOperand(OpNo).getImm();
129 auto Range = RISCVSysReg::lookupSysRegByEncoding(Imm);
130 for (auto &Reg : Range) {
131 if (Reg.IsAltName || Reg.IsDeprecatedName)
132 continue;
133 if (Reg.haveRequiredFeatures(STI.getFeatureBits())) {
134 markup(O, Markup::Register) << Reg.Name;
135 return;
136 }
137 }
139}
140
141void RISCVInstPrinter::printFenceArg(const MCInst *MI, unsigned OpNo,
142 const MCSubtargetInfo &STI,
143 raw_ostream &O) {
144 unsigned FenceArg = MI->getOperand(OpNo).getImm();
145 assert (((FenceArg >> 4) == 0) && "Invalid immediate in printFenceArg");
146
147 if ((FenceArg & RISCVFenceField::I) != 0)
148 O << 'i';
149 if ((FenceArg & RISCVFenceField::O) != 0)
150 O << 'o';
151 if ((FenceArg & RISCVFenceField::R) != 0)
152 O << 'r';
153 if ((FenceArg & RISCVFenceField::W) != 0)
154 O << 'w';
155 if (FenceArg == 0)
156 O << "0";
157}
158
159void RISCVInstPrinter::printFRMArg(const MCInst *MI, unsigned OpNo,
160 const MCSubtargetInfo &STI, raw_ostream &O) {
161 auto FRMArg =
162 static_cast<RISCVFPRndMode::RoundingMode>(MI->getOperand(OpNo).getImm());
164 return;
165 O << ", " << RISCVFPRndMode::roundingModeToString(FRMArg);
166}
167
169 const MCSubtargetInfo &STI,
170 raw_ostream &O) {
171 auto FRMArg =
172 static_cast<RISCVFPRndMode::RoundingMode>(MI->getOperand(OpNo).getImm());
173 // Never print rounding mode if it's the default 'rne'. This ensures the
174 // output can still be parsed by older tools that erroneously failed to
175 // accept a rounding mode.
177 return;
178 O << ", " << RISCVFPRndMode::roundingModeToString(FRMArg);
179}
180
182 const MCSubtargetInfo &STI,
183 raw_ostream &O) {
184 unsigned Imm = MI->getOperand(OpNo).getImm();
185 if (Imm == 1) {
186 markup(O, Markup::Immediate) << "min";
187 } else if (Imm == 30) {
188 markup(O, Markup::Immediate) << "inf";
189 } else if (Imm == 31) {
190 markup(O, Markup::Immediate) << "nan";
191 } else {
192 float FPVal = RISCVLoadFPImm::getFPImm(Imm);
193 // If the value is an integer, print a .0 fraction. Otherwise, use %g to
194 // which will not print trailing zeros and will use scientific notation
195 // if it is shorter than printing as a decimal. The smallest value requires
196 // 12 digits of precision including the decimal.
197 if (FPVal == (int)(FPVal))
198 markup(O, Markup::Immediate) << format("%.1f", FPVal);
199 else
200 markup(O, Markup::Immediate) << format("%.12g", FPVal);
201 }
202}
203
205 const MCSubtargetInfo &STI,
206 raw_ostream &O) {
207 const MCOperand &MO = MI->getOperand(OpNo);
208
209 assert(MO.isReg() && "printZeroOffsetMemOp can only print register operands");
210 O << "(";
211 printRegName(O, MO.getReg());
212 O << ")";
213}
214
215void RISCVInstPrinter::printVTypeI(const MCInst *MI, unsigned OpNo,
216 const MCSubtargetInfo &STI, raw_ostream &O) {
217 unsigned Imm = MI->getOperand(OpNo).getImm();
218 // Print the raw immediate for reserved values: vlmul[2:0]=4, vsew[2:0]=0b1xx,
219 // altfmt=1 without zvfbfa extension, or non-zero in bits 9 and above.
221 RISCVVType::getSEW(Imm) > 64 ||
222 (RISCVVType::isAltFmt(Imm) &&
223 !STI.hasFeature(RISCV::FeatureStdExtZvfbfa)) ||
224 (Imm >> 9) != 0) {
225 O << formatImm(Imm);
226 return;
227 }
228 // Print the text form.
230}
231
232void RISCVInstPrinter::printXSfmmVType(const MCInst *MI, unsigned OpNo,
233 const MCSubtargetInfo &STI,
234 raw_ostream &O) {
235 unsigned Imm = MI->getOperand(OpNo).getImm();
237 unsigned SEW = RISCVVType::getSEW(Imm);
238 O << "e" << SEW;
239 bool AltFmt = RISCVVType::isAltFmt(Imm);
240 if (AltFmt)
241 O << "alt";
242 unsigned Widen = RISCVVType::getXSfmmWiden(Imm);
243 O << ", w" << Widen;
244}
245
246// Print a Zcmp RList. If we are printing architectural register names rather
247// than ABI register names, we need to print "{x1, x8-x9, x18-x27}" for all
248// registers. Otherwise, we print "{ra, s0-s11}".
249void RISCVInstPrinter::printRegList(const MCInst *MI, unsigned OpNo,
250 const MCSubtargetInfo &STI, raw_ostream &O) {
251 unsigned Imm = MI->getOperand(OpNo).getImm();
252
254 Imm <= RISCVZC::RLISTENCODE::RA_S0_S11 && "Invalid Rlist");
255
256 O << "{";
257 printRegName(O, RISCV::X1);
258
259 if (Imm >= RISCVZC::RLISTENCODE::RA_S0) {
260 O << ", ";
261 printRegName(O, RISCV::X8);
262 }
263
265 O << '-';
267 printRegName(O, RISCV::X9);
268 }
269
271 if (ArchRegNames)
272 O << ", ";
274 printRegName(O, RISCV::X18);
275 }
276
278 if (ArchRegNames)
279 O << '-';
280 unsigned Offset = (Imm - RISCVZC::RLISTENCODE::RA_S0_S3);
281 // Encodings for S3-S9 are contiguous. There is no encoding for S10, so we
282 // must skip to S11(X27).
284 ++Offset;
285 printRegName(O, RISCV::X19 + Offset);
286 }
287
288 O << "}";
289}
290
291void RISCVInstPrinter::printRegReg(const MCInst *MI, unsigned OpNo,
292 const MCSubtargetInfo &STI, raw_ostream &O) {
293 const MCOperand &OffsetMO = MI->getOperand(OpNo + 1);
294
295 assert(OffsetMO.isReg() && "printRegReg can only print register operands");
296 printRegName(O, OffsetMO.getReg());
297
298 O << "(";
299 const MCOperand &BaseMO = MI->getOperand(OpNo);
300 assert(BaseMO.isReg() && "printRegReg can only print register operands");
301 printRegName(O, BaseMO.getReg());
302 O << ")";
303}
304
305void RISCVInstPrinter::printStackAdj(const MCInst *MI, unsigned OpNo,
306 const MCSubtargetInfo &STI, raw_ostream &O,
307 bool Negate) {
308 int64_t Imm = MI->getOperand(OpNo).getImm();
309 bool IsRV64 = STI.hasFeature(RISCV::Feature64Bit);
310 int64_t StackAdj = 0;
311 auto RlistVal = MI->getOperand(0).getImm();
312 auto Base = RISCVZC::getStackAdjBase(RlistVal, IsRV64);
313 StackAdj = Imm + Base;
314 assert((StackAdj >= Base && StackAdj <= Base + 48) &&
315 "Incorrect stack adjust");
316 if (Negate)
317 StackAdj = -StackAdj;
318
319 // RAII guard for ANSI color escape sequences
320 WithMarkup ScopedMarkup = markup(O, Markup::Immediate);
321 O << StackAdj;
322}
323
324void RISCVInstPrinter::printVMaskReg(const MCInst *MI, unsigned OpNo,
325 const MCSubtargetInfo &STI,
326 raw_ostream &O) {
327 const MCOperand &MO = MI->getOperand(OpNo);
328
329 assert(MO.isReg() && "printVMaskReg can only print register operands");
330 if (MO.getReg() == RISCV::NoRegister)
331 return;
332 O << ", ";
333 printRegName(O, MO.getReg());
334 O << ".t";
335}
336
338 // When PrintAliases is enabled, and EmitX8AsFP is enabled, x8 will be printed
339 // as fp instead of s0. Note that these similar registers are not replaced:
340 // - X8_H: used for f16 register in zhinx
341 // - X8_W: used for f32 register in zfinx
342 // - X8_X9: used for GPR Pair
343 if (!ArchRegNames && EmitX8AsFP && Reg == RISCV::X8)
344 return "fp";
345 return getRegisterName(Reg, ArchRegNames ? RISCV::NoRegAltName
346 : RISCV::ABIRegAltName);
347}
static SDValue Widen(SelectionDAG *CurDAG, SDValue N)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static cl::opt< bool > NoAliases("csky-no-aliases", cl::desc("Disable the emission of assembler pseudo instructions"), cl::init(false), cl::Hidden)
static cl::opt< bool > ArchRegNames("csky-arch-reg-names", cl::desc("Print architectural register names rather than the " "ABI names (such as r14 instead of sp)"), cl::init(false), cl::Hidden)
IRTranslator LLVM IR MI
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
static bool ArchRegNames
static cl::opt< bool > EmitX8AsFP("riscv-emit-x8-as-fp", cl::desc("Emit x8 as fp instead of s0"), cl::init(false), cl::Hidden)
static cl::opt< bool > NoAliases("riscv-no-aliases", cl::desc("Disable the emission of assembler pseudo instructions"), cl::init(false), cl::Hidden)
void printExpr(raw_ostream &, const MCExpr &) const
Definition: MCAsmInfo.cpp:153
WithMarkup markup(raw_ostream &OS, Markup M)
format_object< int64_t > formatHex(int64_t Value) const
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:52
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.
Definition: MCInstPrinter.h:75
bool PrintAliases
True if we prefer aliases (e.g. nop) to raw mnemonics.
Definition: MCInstPrinter.h:64
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
bool isReg() const
Definition: MCInst.h:65
MCRegister getReg() const
Returns the register number.
Definition: MCInst.h:73
const MCExpr * getExpr() const
Definition: MCInst.h:118
bool isExpr() const
Definition: MCInst.h:69
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:33
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
const FeatureBitset & getFeatureBits() const
void printRegList(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
static const char * getRegisterName(MCRegister Reg)
void printCSRSystemRegister(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
void printXSfmmVType(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
void printFenceArg(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
void printVMaskReg(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
bool applyTargetSpecificCLOption(StringRef Opt) override
Customize the printer according to a command line option.
void printFRMArgLegacy(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
bool printAliasInstr(const MCInst *MI, uint64_t Address, const MCSubtargetInfo &STI, raw_ostream &O)
void printRegReg(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
void printFRMArg(const MCInst *MI, unsigned OpNo, 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.
void printFPImmOperand(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
void printInstruction(const MCInst *MI, uint64_t Address, const MCSubtargetInfo &STI, raw_ostream &O)
void printZeroOffsetMemOp(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
void printRegName(raw_ostream &O, MCRegister Reg) override
Print the assembler register name.
void printVTypeI(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
void printOperand(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
void printStackAdj(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O, bool Negate=false)
void printBranchOperand(const MCInst *MI, uint64_t Address, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Target - Wrapper for Target specific information.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
static StringRef roundingModeToString(RoundingMode RndMode)
float getFPImm(unsigned Imm)
bool uncompress(MCInst &OutInst, const MCInst &MI, const MCSubtargetInfo &STI)
static unsigned getXSfmmWiden(unsigned VType)
LLVM_ABI void printVType(unsigned VType, raw_ostream &OS)
static bool isValidXSfmmVType(unsigned VTypeI)
static bool isAltFmt(unsigned VType)
static unsigned getSEW(unsigned VType)
static VLMUL getVLMUL(unsigned VType)
static unsigned getStackAdjBase(unsigned RlistVal, bool IsRV64)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:444
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:126