LLVM 23.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 "RISCVMCAsmInfo.h"
16#include "llvm/MC/MCAsmInfo.h"
17#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCInst.h"
22#include "llvm/MC/MCSymbol.h"
25using namespace llvm;
26
27#define DEBUG_TYPE "asm-printer"
28
29// Include the auto-generated portion of the assembly writer.
30#define PRINT_ALIAS_INSTR
31#include "RISCVGenAsmWriter.inc"
32
33static cl::opt<bool>
34 NoAliases("riscv-no-aliases",
35 cl::desc("Disable the emission of assembler pseudo instructions"),
36 cl::init(false), cl::Hidden);
37
38static cl::opt<bool> EmitX8AsFP("riscv-emit-x8-as-fp",
39 cl::desc("Emit x8 as fp instead of s0"),
40 cl::init(false), cl::Hidden);
41
42// Print architectural register names rather than the ABI names (such as x2
43// instead of sp).
44// TODO: Make RISCVInstPrinter::getRegisterName non-static so that this can a
45// member.
46static bool ArchRegNames;
47
48// The command-line flags above are used by llvm-mc and llc. They can be used by
49// `llvm-objdump`, but we override their values here to handle options passed to
50// `llvm-objdump` with `-M` (which matches GNU objdump). There did not seem to
51// be an easier way to allow these options in all these tools, without doing it
52// this way.
54 if (Opt == "no-aliases") {
55 PrintAliases = false;
56 return true;
57 }
58 if (Opt == "numeric") {
59 ArchRegNames = true;
60 return true;
61 }
62 if (Opt == "emit-x8-as-fp") {
63 if (!ArchRegNames)
64 EmitX8AsFP = true;
65 return true;
66 }
67
68 return false;
69}
70
72 StringRef Annot, const MCSubtargetInfo &STI,
73 raw_ostream &O) {
74 bool Res = false;
75 const MCInst *NewMI = MI;
76 MCInst UncompressedMI;
77 if (PrintAliases && !NoAliases)
78 Res = RISCVRVC::uncompress(UncompressedMI, *MI, STI);
79 if (Res)
80 NewMI = &UncompressedMI;
81 if (!PrintAliases || NoAliases || !printAliasInstr(NewMI, Address, STI, O))
82 printInstruction(NewMI, Address, STI, O);
83 printAnnotation(O, Annot);
84}
85
89
90void RISCVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
91 const MCSubtargetInfo &STI,
92 raw_ostream &O) {
93 const MCOperand &MO = MI->getOperand(OpNo);
94
95 if (MO.isReg()) {
96 printRegName(O, MO.getReg());
97 return;
98 }
99
100 if (MO.isImm()) {
101 printImm(MI, OpNo, STI, O);
102 return;
103 }
104
105 assert(MO.isExpr() && "Unknown operand kind in printOperand");
106 MAI.printExpr(O, *MO.getExpr());
107}
108
110 unsigned OpNo,
111 const MCSubtargetInfo &STI,
112 raw_ostream &O) {
113 // Do not print the numeric target address when symbolizing.
115 return;
116
117 const MCOperand &MO = MI->getOperand(OpNo);
118 if (!MO.isImm())
119 return printOperand(MI, OpNo, STI, O);
120
122 uint64_t Target = Address + MO.getImm();
123 if (!STI.hasFeature(RISCV::Feature64Bit))
124 Target &= 0xffffffff;
126 } else {
128 }
129}
130
132 const MCSubtargetInfo &STI,
133 raw_ostream &O) {
134 unsigned Imm = MI->getOperand(OpNo).getImm();
135 auto Range = RISCVSysReg::lookupSysRegByEncoding(Imm);
136 for (auto &Reg : Range) {
137 if (Reg.IsAltName || Reg.IsDeprecatedName)
138 continue;
139 if (Reg.haveRequiredFeatures(STI.getFeatureBits())) {
140 markup(O, Markup::Register) << Reg.Name;
141 return;
142 }
143 }
145}
146
147void RISCVInstPrinter::printFenceArg(const MCInst *MI, unsigned OpNo,
148 const MCSubtargetInfo &STI,
149 raw_ostream &O) {
150 unsigned FenceArg = MI->getOperand(OpNo).getImm();
151 assert (((FenceArg >> 4) == 0) && "Invalid immediate in printFenceArg");
152
153 if ((FenceArg & RISCVFenceField::I) != 0)
154 O << 'i';
155 if ((FenceArg & RISCVFenceField::O) != 0)
156 O << 'o';
157 if ((FenceArg & RISCVFenceField::R) != 0)
158 O << 'r';
159 if ((FenceArg & RISCVFenceField::W) != 0)
160 O << 'w';
161 if (FenceArg == 0)
162 O << "0";
163}
164
165void RISCVInstPrinter::printFRMArg(const MCInst *MI, unsigned OpNo,
166 const MCSubtargetInfo &STI, raw_ostream &O) {
167 auto FRMArg =
168 static_cast<RISCVFPRndMode::RoundingMode>(MI->getOperand(OpNo).getImm());
170 return;
171 O << ", " << RISCVFPRndMode::roundingModeToString(FRMArg);
172}
173
175 const MCSubtargetInfo &STI,
176 raw_ostream &O) {
177 auto FRMArg =
178 static_cast<RISCVFPRndMode::RoundingMode>(MI->getOperand(OpNo).getImm());
179 // Never print rounding mode if it's the default 'rne'. This ensures the
180 // output can still be parsed by older tools that erroneously failed to
181 // accept a rounding mode.
183 return;
184 O << ", " << RISCVFPRndMode::roundingModeToString(FRMArg);
185}
186
188 const MCSubtargetInfo &STI,
189 raw_ostream &O) {
190 unsigned Imm = MI->getOperand(OpNo).getImm();
191 if (Imm == 1) {
192 markup(O, Markup::Immediate) << "min";
193 } else if (Imm == 30) {
194 markup(O, Markup::Immediate) << "inf";
195 } else if (Imm == 31) {
196 markup(O, Markup::Immediate) << "nan";
197 } else {
198 float FPVal = RISCVLoadFPImm::getFPImm(Imm);
199 // If the value is an integer, print a .0 fraction. Otherwise, use %g to
200 // which will not print trailing zeros and will use scientific notation
201 // if it is shorter than printing as a decimal. The smallest value requires
202 // 12 digits of precision including the decimal.
203 if (FPVal == (int)(FPVal))
204 markup(O, Markup::Immediate) << format("%.1f", FPVal);
205 else
206 markup(O, Markup::Immediate) << format("%.12g", FPVal);
207 }
208}
209
211 const MCSubtargetInfo &STI,
212 raw_ostream &O) {
213 const MCOperand &MO = MI->getOperand(OpNo);
214
215 assert(MO.isReg() && "printZeroOffsetMemOp can only print register operands");
216 O << "(";
217 printRegName(O, MO.getReg());
218 O << ")";
219}
220
221void RISCVInstPrinter::printVTypeI(const MCInst *MI, unsigned OpNo,
222 const MCSubtargetInfo &STI, raw_ostream &O) {
223 unsigned Imm = MI->getOperand(OpNo).getImm();
224 // Print the raw immediate for reserved values: vlmul[2:0]=4, vsew[2:0]=0b1xx,
225 // altfmt=1 without zvfbfa or zvfofp8min extension, or non-zero in bits 9 and
226 // above.
227 if (!RISCVVType::isValidVType(Imm) ||
228 (RISCVVType::isAltFmt(Imm) &&
229 !(STI.hasFeature(RISCV::FeatureStdExtZvqwdota8i) ||
230 STI.hasFeature(RISCV::FeatureStdExtZvqwdota16i) ||
231 STI.hasFeature(RISCV::FeatureStdExtZvfwdota16bf) ||
232 STI.hasFeature(RISCV::FeatureStdExtZvfqwdota8f) ||
233 STI.hasFeature(RISCV::FeatureStdExtZvfbfa) ||
234 STI.hasFeature(RISCV::FeatureStdExtZvfofp8min) ||
235 STI.hasFeature(RISCV::FeatureVendorXSfvfbfexp16e) ||
236 STI.hasFeature(RISCV::FeatureStdExtZvqwbdota8i) ||
237 STI.hasFeature(RISCV::FeatureStdExtZvqwbdota16i) ||
238 STI.hasFeature(RISCV::FeatureStdExtZvfqwbdota8f) ||
239 STI.hasFeature(RISCV::FeatureStdExtZvfwbdota16bf))) ||
240 (Imm >> 9) != 0) {
241 O << formatImm(Imm);
242 return;
243 }
244 // Print the text form.
246}
247
248void RISCVInstPrinter::printXSfmmVType(const MCInst *MI, unsigned OpNo,
249 const MCSubtargetInfo &STI,
250 raw_ostream &O) {
251 unsigned Imm = MI->getOperand(OpNo).getImm();
253 unsigned SEW = RISCVVType::getSEW(Imm);
254 O << "e" << SEW;
255 bool AltFmt = RISCVVType::isAltFmt(Imm);
256 if (AltFmt)
257 O << "alt";
258 unsigned Widen = RISCVVType::getXSfmmWiden(Imm);
259 O << ", w" << Widen;
260}
261
262// Print a Zcmp RList. If we are printing architectural register names rather
263// than ABI register names, we need to print "{x1, x8-x9, x18-x27}" for all
264// registers. Otherwise, we print "{ra, s0-s11}".
265void RISCVInstPrinter::printRegList(const MCInst *MI, unsigned OpNo,
266 const MCSubtargetInfo &STI, raw_ostream &O) {
267 unsigned Imm = MI->getOperand(OpNo).getImm();
268
270 Imm <= RISCVZC::RLISTENCODE::RA_S0_S11 && "Invalid Rlist");
271
272 O << "{";
273 printRegName(O, RISCV::X1);
274
275 if (Imm >= RISCVZC::RLISTENCODE::RA_S0) {
276 O << ", ";
277 printRegName(O, RISCV::X8);
278 }
279
281 O << '-';
283 printRegName(O, RISCV::X9);
284 }
285
287 if (ArchRegNames)
288 O << ", ";
290 printRegName(O, RISCV::X18);
291 }
292
294 if (ArchRegNames)
295 O << '-';
296 unsigned Offset = (Imm - RISCVZC::RLISTENCODE::RA_S0_S3);
297 // Encodings for S3-S9 are contiguous. There is no encoding for S10, so we
298 // must skip to S11(X27).
300 ++Offset;
301 printRegName(O, RISCV::X19 + Offset);
302 }
303
304 O << "}";
305}
306
307void RISCVInstPrinter::printRegReg(const MCInst *MI, unsigned OpNo,
308 const MCSubtargetInfo &STI, raw_ostream &O) {
309 const MCOperand &OffsetMO = MI->getOperand(OpNo + 1);
310
311 assert(OffsetMO.isReg() && "printRegReg can only print register operands");
312 printRegName(O, OffsetMO.getReg());
313
314 O << "(";
315 const MCOperand &BaseMO = MI->getOperand(OpNo);
316 assert(BaseMO.isReg() && "printRegReg can only print register operands");
317 printRegName(O, BaseMO.getReg());
318 O << ")";
319}
320
321void RISCVInstPrinter::printStackAdj(const MCInst *MI, unsigned OpNo,
322 const MCSubtargetInfo &STI, raw_ostream &O,
323 bool Negate) {
324 int64_t Imm = MI->getOperand(OpNo).getImm();
325 bool IsRV64 = STI.hasFeature(RISCV::Feature64Bit);
326 int64_t StackAdj = 0;
327 auto RlistVal = MI->getOperand(0).getImm();
328 auto Base = RISCVZC::getStackAdjBase(RlistVal, IsRV64);
329 StackAdj = Imm + Base;
330 assert((StackAdj >= Base && StackAdj <= Base + 48) &&
331 "Incorrect stack adjust");
332 if (Negate)
333 StackAdj = -StackAdj;
334
335 // RAII guard for ANSI color escape sequences
336 WithMarkup ScopedMarkup = markup(O, Markup::Immediate);
337 O << StackAdj;
338}
339
340void RISCVInstPrinter::printVMaskReg(const MCInst *MI, unsigned OpNo,
341 const MCSubtargetInfo &STI,
342 raw_ostream &O) {
343 const MCOperand &MO = MI->getOperand(OpNo);
344
345 assert(MO.isReg() && "printVMaskReg can only print register operands");
346 if (MO.getReg() == RISCV::NoRegister)
347 return;
348 O << ", ";
349 printRegName(O, MO.getReg());
350 O << ".t";
351}
352
353void RISCVInstPrinter::printVScaleReg(const MCInst *MI, unsigned OpNo,
354 const MCSubtargetInfo &STI,
355 raw_ostream &O) {
356 const MCOperand &MO = MI->getOperand(OpNo);
357
358 assert(MO.isReg() && "printVScaleReg can only print register operands");
359 O << ", ";
360 printRegName(O, MO.getReg());
361 O << ".scale";
362}
363
364void RISCVInstPrinter::printTileLambda(const MCInst *MI, unsigned OpNo,
365 const MCSubtargetInfo &STI,
366 raw_ostream &O) {
367 const MCOperand &MO = MI->getOperand(OpNo);
368
369 assert(MO.isImm() && "printTileLambda can only print immediate operands");
370 unsigned Lambda = MO.getImm();
371 if (Lambda == 0)
372 return;
373
374 assert(Lambda <= 7 && "Unexpected tile lambda encoding");
375 O << ", L" << (1U << (Lambda - 1));
376}
377
378void RISCVInstPrinter::printImm(const MCInst *MI, unsigned OpNo,
379 const MCSubtargetInfo &STI, raw_ostream &O) {
380 const MCOperand &Op = MI->getOperand(OpNo);
381 const unsigned Opcode = MI->getOpcode();
382 uint64_t Imm = Op.getImm();
384 (Opcode == RISCV::ANDI || Opcode == RISCV::ORI || Opcode == RISCV::XORI ||
385 Opcode == RISCV::C_ANDI || Opcode == RISCV::AUIPC ||
386 Opcode == RISCV::LUI)) {
387 if (!STI.hasFeature(RISCV::Feature64Bit))
388 Imm &= 0xffffffff;
390 } else
392}
393
395 // When PrintAliases is enabled, and EmitX8AsFP is enabled, x8 will be printed
396 // as fp instead of s0. Note that these similar registers are not replaced:
397 // - X8_H: used for f16 register in zhinx
398 // - X8_W: used for f32 register in zfinx
399 // - X8_X9: used for GPR Pair
400 if (!ArchRegNames && EmitX8AsFP && Reg == RISCV::X8)
401 return "fp";
402 return getRegisterName(Reg, ArchRegNames ? RISCV::NoRegAltName
403 : RISCV::ABIRegAltName);
404}
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 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)
WithMarkup markup(raw_ostream &OS, Markup M)
format_object< int64_t > formatHex(int64_t Value) const
bool SymbolizeOperands
If true, symbolize branch target and memory reference operands.
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.
bool PrintAliases
True if we prefer aliases (e.g. nop) to raw mnemonics.
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:41
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
const Triple & getTargetTriple() const
const FeatureBitset & getFeatureBits() const
void printRegList(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
void printTileLambda(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 printImm(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 printVScaleReg(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)
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
bool isOSBinFormatMachO() const
Tests whether the environment is MachO.
Definition Triple.h:791
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)
static bool isValidVType(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 unsigned getStackAdjBase(unsigned RlistVal, bool IsRV64)
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:558
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition Format.h:129
DWARFExpression::Operation Op