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.
226 if (!RISCVVType::isValidVType(Imm) ||
227 (RISCVVType::isAltFmt(Imm) &&
228 !(STI.hasFeature(RISCV::FeatureStdExtZvfbfa) ||
229 STI.hasFeature(RISCV::FeatureStdExtZvfofp8min) ||
230 STI.hasFeature(RISCV::FeatureVendorXSfvfbfexp16e))) ||
231 (Imm >> 9) != 0) {
232 O << formatImm(Imm);
233 return;
234 }
235 // Print the text form.
237}
238
239void RISCVInstPrinter::printXSfmmVType(const MCInst *MI, unsigned OpNo,
240 const MCSubtargetInfo &STI,
241 raw_ostream &O) {
242 unsigned Imm = MI->getOperand(OpNo).getImm();
244 unsigned SEW = RISCVVType::getSEW(Imm);
245 O << "e" << SEW;
246 bool AltFmt = RISCVVType::isAltFmt(Imm);
247 if (AltFmt)
248 O << "alt";
249 unsigned Widen = RISCVVType::getXSfmmWiden(Imm);
250 O << ", w" << Widen;
251}
252
253// Print a Zcmp RList. If we are printing architectural register names rather
254// than ABI register names, we need to print "{x1, x8-x9, x18-x27}" for all
255// registers. Otherwise, we print "{ra, s0-s11}".
256void RISCVInstPrinter::printRegList(const MCInst *MI, unsigned OpNo,
257 const MCSubtargetInfo &STI, raw_ostream &O) {
258 unsigned Imm = MI->getOperand(OpNo).getImm();
259
261 Imm <= RISCVZC::RLISTENCODE::RA_S0_S11 && "Invalid Rlist");
262
263 O << "{";
264 printRegName(O, RISCV::X1);
265
266 if (Imm >= RISCVZC::RLISTENCODE::RA_S0) {
267 O << ", ";
268 printRegName(O, RISCV::X8);
269 }
270
272 O << '-';
274 printRegName(O, RISCV::X9);
275 }
276
278 if (ArchRegNames)
279 O << ", ";
281 printRegName(O, RISCV::X18);
282 }
283
285 if (ArchRegNames)
286 O << '-';
287 unsigned Offset = (Imm - RISCVZC::RLISTENCODE::RA_S0_S3);
288 // Encodings for S3-S9 are contiguous. There is no encoding for S10, so we
289 // must skip to S11(X27).
291 ++Offset;
292 printRegName(O, RISCV::X19 + Offset);
293 }
294
295 O << "}";
296}
297
298void RISCVInstPrinter::printRegReg(const MCInst *MI, unsigned OpNo,
299 const MCSubtargetInfo &STI, raw_ostream &O) {
300 const MCOperand &OffsetMO = MI->getOperand(OpNo + 1);
301
302 assert(OffsetMO.isReg() && "printRegReg can only print register operands");
303 printRegName(O, OffsetMO.getReg());
304
305 O << "(";
306 const MCOperand &BaseMO = MI->getOperand(OpNo);
307 assert(BaseMO.isReg() && "printRegReg can only print register operands");
308 printRegName(O, BaseMO.getReg());
309 O << ")";
310}
311
312void RISCVInstPrinter::printStackAdj(const MCInst *MI, unsigned OpNo,
313 const MCSubtargetInfo &STI, raw_ostream &O,
314 bool Negate) {
315 int64_t Imm = MI->getOperand(OpNo).getImm();
316 bool IsRV64 = STI.hasFeature(RISCV::Feature64Bit);
317 int64_t StackAdj = 0;
318 auto RlistVal = MI->getOperand(0).getImm();
319 auto Base = RISCVZC::getStackAdjBase(RlistVal, IsRV64);
320 StackAdj = Imm + Base;
321 assert((StackAdj >= Base && StackAdj <= Base + 48) &&
322 "Incorrect stack adjust");
323 if (Negate)
324 StackAdj = -StackAdj;
325
326 // RAII guard for ANSI color escape sequences
327 WithMarkup ScopedMarkup = markup(O, Markup::Immediate);
328 O << StackAdj;
329}
330
331void RISCVInstPrinter::printVMaskReg(const MCInst *MI, unsigned OpNo,
332 const MCSubtargetInfo &STI,
333 raw_ostream &O) {
334 const MCOperand &MO = MI->getOperand(OpNo);
335
336 assert(MO.isReg() && "printVMaskReg can only print register operands");
337 if (MO.getReg() == RISCV::NoRegister)
338 return;
339 O << ", ";
340 printRegName(O, MO.getReg());
341 O << ".t";
342}
343
344void RISCVInstPrinter::printImm(const MCInst *MI, unsigned OpNo,
345 const MCSubtargetInfo &STI, raw_ostream &O) {
346 const MCOperand &Op = MI->getOperand(OpNo);
347 const unsigned Opcode = MI->getOpcode();
348 uint64_t Imm = Op.getImm();
350 (Opcode == RISCV::ANDI || Opcode == RISCV::ORI || Opcode == RISCV::XORI ||
351 Opcode == RISCV::C_ANDI || Opcode == RISCV::AUIPC ||
352 Opcode == RISCV::LUI)) {
353 if (!STI.hasFeature(RISCV::Feature64Bit))
354 Imm &= 0xffffffff;
356 } else
358}
359
361 // When PrintAliases is enabled, and EmitX8AsFP is enabled, x8 will be printed
362 // as fp instead of s0. Note that these similar registers are not replaced:
363 // - X8_H: used for f16 register in zhinx
364 // - X8_W: used for f32 register in zfinx
365 // - X8_X9: used for GPR Pair
366 if (!ArchRegNames && EmitX8AsFP && Reg == RISCV::X8)
367 return "fp";
368 return getRegisterName(Reg, ArchRegNames ? RISCV::NoRegAltName
369 : RISCV::ABIRegAltName);
370}
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:781
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: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