LLVM 18.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 "RISCVMCExpr.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"
26using namespace llvm;
27
28#define DEBUG_TYPE "asm-printer"
29
30// Include the auto-generated portion of the assembly writer.
31#define PRINT_ALIAS_INSTR
32#include "RISCVGenAsmWriter.inc"
33
34static cl::opt<bool>
35 NoAliases("riscv-no-aliases",
36 cl::desc("Disable the emission of assembler pseudo instructions"),
37 cl::init(false), cl::Hidden);
38
39// Print architectural register names rather than the ABI names (such as x2
40// instead of sp).
41// TODO: Make RISCVInstPrinter::getRegisterName non-static so that this can a
42// member.
43static bool ArchRegNames;
44
45// The command-line flags above are used by llvm-mc and llc. They can be used by
46// `llvm-objdump`, but we override their values here to handle options passed to
47// `llvm-objdump` with `-M` (which matches GNU objdump). There did not seem to
48// be an easier way to allow these options in all these tools, without doing it
49// this way.
51 if (Opt == "no-aliases") {
52 PrintAliases = false;
53 return true;
54 }
55 if (Opt == "numeric") {
56 ArchRegNames = true;
57 return true;
58 }
59
60 return false;
61}
62
64 StringRef Annot, const MCSubtargetInfo &STI,
65 raw_ostream &O) {
66 bool Res = false;
67 const MCInst *NewMI = MI;
68 MCInst UncompressedMI;
69 if (PrintAliases && !NoAliases)
70 Res = RISCVRVC::uncompress(UncompressedMI, *MI, STI);
71 if (Res)
72 NewMI = const_cast<MCInst *>(&UncompressedMI);
73 if (!PrintAliases || NoAliases || !printAliasInstr(NewMI, Address, STI, O))
74 printInstruction(NewMI, Address, STI, O);
75 printAnnotation(O, Annot);
76}
77
80}
81
82void RISCVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
83 const MCSubtargetInfo &STI, raw_ostream &O,
84 const char *Modifier) {
85 assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
86 const MCOperand &MO = MI->getOperand(OpNo);
87
88 if (MO.isReg()) {
89 printRegName(O, MO.getReg());
90 return;
91 }
92
93 if (MO.isImm()) {
95 return;
96 }
97
98 assert(MO.isExpr() && "Unknown operand kind in printOperand");
99 MO.getExpr()->print(O, &MAI);
100}
101
103 unsigned OpNo,
104 const MCSubtargetInfo &STI,
105 raw_ostream &O) {
106 const MCOperand &MO = MI->getOperand(OpNo);
107 if (!MO.isImm())
108 return printOperand(MI, OpNo, STI, O);
109
111 uint64_t Target = Address + MO.getImm();
112 if (!STI.hasFeature(RISCV::Feature64Bit))
113 Target &= 0xffffffff;
115 } else {
116 markup(O, Markup::Target) << MO.getImm();
117 }
118}
119
121 const MCSubtargetInfo &STI,
122 raw_ostream &O) {
123 unsigned Imm = MI->getOperand(OpNo).getImm();
124 auto SiFiveReg = RISCVSysReg::lookupSiFiveRegByEncoding(Imm);
125 auto SysReg = RISCVSysReg::lookupSysRegByEncoding(Imm);
126 if (SiFiveReg && SiFiveReg->haveVendorRequiredFeatures(STI.getFeatureBits()))
127 markup(O, Markup::Register) << SiFiveReg->Name;
128 else if (SysReg && SysReg->haveRequiredFeatures(STI.getFeatureBits()))
129 markup(O, Markup::Register) << SysReg->Name;
130 else
131 markup(O, Markup::Register) << Imm;
132}
133
134void RISCVInstPrinter::printFenceArg(const MCInst *MI, unsigned OpNo,
135 const MCSubtargetInfo &STI,
136 raw_ostream &O) {
137 unsigned FenceArg = MI->getOperand(OpNo).getImm();
138 assert (((FenceArg >> 4) == 0) && "Invalid immediate in printFenceArg");
139
140 if ((FenceArg & RISCVFenceField::I) != 0)
141 O << 'i';
142 if ((FenceArg & RISCVFenceField::O) != 0)
143 O << 'o';
144 if ((FenceArg & RISCVFenceField::R) != 0)
145 O << 'r';
146 if ((FenceArg & RISCVFenceField::W) != 0)
147 O << 'w';
148 if (FenceArg == 0)
149 O << "0";
150}
151
152void RISCVInstPrinter::printFRMArg(const MCInst *MI, unsigned OpNo,
153 const MCSubtargetInfo &STI, raw_ostream &O) {
154 auto FRMArg =
155 static_cast<RISCVFPRndMode::RoundingMode>(MI->getOperand(OpNo).getImm());
157 return;
158 O << ", " << RISCVFPRndMode::roundingModeToString(FRMArg);
159}
160
162 const MCSubtargetInfo &STI,
163 raw_ostream &O) {
164 unsigned Imm = MI->getOperand(OpNo).getImm();
165 if (Imm == 1) {
166 markup(O, Markup::Immediate) << "min";
167 } else if (Imm == 30) {
168 markup(O, Markup::Immediate) << "inf";
169 } else if (Imm == 31) {
170 markup(O, Markup::Immediate) << "nan";
171 } else {
172 float FPVal = RISCVLoadFPImm::getFPImm(Imm);
173 // If the value is an integer, print a .0 fraction. Otherwise, use %g to
174 // which will not print trailing zeros and will use scientific notation
175 // if it is shorter than printing as a decimal. The smallest value requires
176 // 12 digits of precision including the decimal.
177 if (FPVal == (int)(FPVal))
178 markup(O, Markup::Immediate) << format("%.1f", FPVal);
179 else
180 markup(O, Markup::Immediate) << format("%.12g", FPVal);
181 }
182}
183
185 const MCSubtargetInfo &STI,
186 raw_ostream &O) {
187 const MCOperand &MO = MI->getOperand(OpNo);
188
189 assert(MO.isReg() && "printZeroOffsetMemOp can only print register operands");
190 O << "(";
191 printRegName(O, MO.getReg());
192 O << ")";
193}
194
195void RISCVInstPrinter::printVTypeI(const MCInst *MI, unsigned OpNo,
196 const MCSubtargetInfo &STI, raw_ostream &O) {
197 unsigned Imm = MI->getOperand(OpNo).getImm();
198 // Print the raw immediate for reserved values: vlmul[2:0]=4, vsew[2:0]=0b1xx,
199 // or non-zero in bits 8 and above.
201 RISCVVType::getSEW(Imm) > 64 || (Imm >> 8) != 0) {
202 O << Imm;
203 return;
204 }
205 // Print the text form.
207}
208
209void RISCVInstPrinter::printRlist(const MCInst *MI, unsigned OpNo,
210 const MCSubtargetInfo &STI, raw_ostream &O) {
211 unsigned Imm = MI->getOperand(OpNo).getImm();
212 O << "{";
213 switch (Imm) {
215 markup(O, Markup::Register) << (ArchRegNames ? "x1" : "ra");
216 break;
218 markup(O, Markup::Register) << (ArchRegNames ? "x1" : "ra");
219 O << ", ";
220 markup(O, Markup::Register) << (ArchRegNames ? "x8" : "s0");
221 break;
223 markup(O, Markup::Register) << (ArchRegNames ? "x1" : "ra");
224 O << ", ";
225 markup(O, Markup::Register) << (ArchRegNames ? "x8" : "s0");
226 O << '-';
227 markup(O, Markup::Register) << (ArchRegNames ? "x9" : "s1");
228 break;
230 markup(O, Markup::Register) << (ArchRegNames ? "x1" : "ra");
231 O << ", ";
232 markup(O, Markup::Register) << (ArchRegNames ? "x8" : "s0");
233 O << '-';
234 markup(O, Markup::Register) << (ArchRegNames ? "x9" : "s2");
235 if (ArchRegNames) {
236 O << ", ";
237 markup(O, Markup::Register) << "x18";
238 }
239 break;
248 markup(O, Markup::Register) << (ArchRegNames ? "x1" : "ra");
249 O << ", ";
250 markup(O, Markup::Register) << (ArchRegNames ? "x8" : "s0");
251 O << '-';
252 if (ArchRegNames) {
253 markup(O, Markup::Register) << "x9";
254 O << ", ";
255 markup(O, Markup::Register) << "x18";
256 O << '-';
257 }
259 RISCV::X19 + (Imm == RISCVZC::RLISTENCODE::RA_S0_S11
260 ? 8
262 break;
263 default:
264 llvm_unreachable("invalid register list");
265 }
266 O << "}";
267}
268
269void RISCVInstPrinter::printSpimm(const MCInst *MI, unsigned OpNo,
270 const MCSubtargetInfo &STI, raw_ostream &O) {
271 int64_t Imm = MI->getOperand(OpNo).getImm();
272 unsigned Opcode = MI->getOpcode();
273 bool IsRV64 = STI.hasFeature(RISCV::Feature64Bit);
274 bool IsEABI = STI.hasFeature(RISCV::FeatureRVE);
275 int64_t Spimm = 0;
276 auto RlistVal = MI->getOperand(0).getImm();
277 assert(RlistVal != 16 && "Incorrect rlist.");
278 auto Base = RISCVZC::getStackAdjBase(RlistVal, IsRV64, IsEABI);
279 Spimm = Imm + Base;
280 assert((Spimm >= Base && Spimm <= Base + 48) && "Incorrect spimm");
281 if (Opcode == RISCV::CM_PUSH)
282 Spimm = -Spimm;
283
284 // RAII guard for ANSI color escape sequences
285 WithMarkup ScopedMarkup = markup(O, Markup::Immediate);
286 RISCVZC::printSpimm(Spimm, O);
287}
288
289void RISCVInstPrinter::printVMaskReg(const MCInst *MI, unsigned OpNo,
290 const MCSubtargetInfo &STI,
291 raw_ostream &O) {
292 const MCOperand &MO = MI->getOperand(OpNo);
293
294 assert(MO.isReg() && "printVMaskReg can only print register operands");
295 if (MO.getReg() == RISCV::NoRegister)
296 return;
297 O << ", ";
298 printRegName(O, MO.getReg());
299 O << ".t";
300}
301
303 return getRegisterName(Reg, ArchRegNames ? RISCV::NoRegAltName
304 : RISCV::ABIRegAltName);
305}
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
static bool ArchRegNames
static cl::opt< bool > NoAliases("riscv-no-aliases", cl::desc("Disable the emission of assembler pseudo instructions"), cl::init(false), cl::Hidden)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:41
format_object< int64_t > formatHex(int64_t Value) const
WithMarkup markup(raw_ostream &OS, Markup M) const
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:51
bool PrintBranchImmAsAddress
If true, a branch immediate (e.g.
Definition: MCInstPrinter.h:74
bool PrintAliases
True if we prefer aliases (e.g. nop) to raw mnemonics.
Definition: MCInstPrinter.h:63
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
int64_t getImm() const
Definition: MCInst.h:80
bool isImm() const
Definition: MCInst.h:62
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:69
bool isReg() const
Definition: MCInst.h:61
const MCExpr * getExpr() const
Definition: MCInst.h:114
bool isExpr() const
Definition: MCInst.h:65
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
static const char * getRegisterName(MCRegister Reg)
void printCSRSystemRegister(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
void printRegName(raw_ostream &O, MCRegister Reg) const override
Print the assembler register name.
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.
bool printAliasInstr(const MCInst *MI, uint64_t Address, 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 printSpimm(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
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 printOperand(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O, const char *Modifier=nullptr)
void printZeroOffsetMemOp(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
void printVTypeI(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
void printRlist(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
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:50
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:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static StringRef roundingModeToString(RoundingMode RndMode)
float getFPImm(unsigned Imm)
bool uncompress(MCInst &OutInst, const MCInst &MI, const MCSubtargetInfo &STI)
static RISCVII::VLMUL getVLMUL(unsigned VType)
void printVType(unsigned VType, raw_ostream &OS)
static unsigned getSEW(unsigned VType)
static unsigned getStackAdjBase(unsigned RlistVal, bool IsRV64, bool IsEABI)
void printSpimm(int64_t Spimm, raw_ostream &OS)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:445
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:125