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