LLVM 17.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"
21#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
38// Print architectural register names rather than the ABI names (such as x2
39// instead of sp).
40// TODO: Make RISCVInstPrinter::getRegisterName non-static so that this can a
41// member.
42static bool ArchRegNames;
43
44// The command-line flags above are used by llvm-mc and llc. They can be used by
45// `llvm-objdump`, but we override their values here to handle options passed to
46// `llvm-objdump` with `-M` (which matches GNU objdump). There did not seem to
47// be an easier way to allow these options in all these tools, without doing it
48// this way.
50 if (Opt == "no-aliases") {
51 PrintAliases = false;
52 return true;
53 }
54 if (Opt == "numeric") {
55 ArchRegNames = true;
56 return true;
57 }
58
59 return false;
60}
61
63 StringRef Annot, const MCSubtargetInfo &STI,
64 raw_ostream &O) {
65 bool Res = false;
66 const MCInst *NewMI = MI;
67 MCInst UncompressedMI;
68 if (PrintAliases && !NoAliases)
69 Res = RISCVRVC::uncompress(UncompressedMI, *MI, STI);
70 if (Res)
71 NewMI = const_cast<MCInst *>(&UncompressedMI);
72 if (!PrintAliases || NoAliases || !printAliasInstr(NewMI, Address, STI, O))
73 printInstruction(NewMI, Address, STI, O);
74 printAnnotation(O, Annot);
75}
76
78 O << getRegisterName(Reg);
79}
80
81void RISCVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
82 const MCSubtargetInfo &STI, raw_ostream &O,
83 const char *Modifier) {
84 assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
85 const MCOperand &MO = MI->getOperand(OpNo);
86
87 if (MO.isReg()) {
88 printRegName(O, MO.getReg());
89 return;
90 }
91
92 if (MO.isImm()) {
93 O << MO.getImm();
94 return;
95 }
96
97 assert(MO.isExpr() && "Unknown operand kind in printOperand");
98 MO.getExpr()->print(O, &MAI);
99}
100
102 unsigned OpNo,
103 const MCSubtargetInfo &STI,
104 raw_ostream &O) {
105 const MCOperand &MO = MI->getOperand(OpNo);
106 if (!MO.isImm())
107 return printOperand(MI, OpNo, STI, O);
108
110 uint64_t Target = Address + MO.getImm();
111 if (!STI.hasFeature(RISCV::Feature64Bit))
112 Target &= 0xffffffff;
113 O << formatHex(Target);
114 } else {
115 O << MO.getImm();
116 }
117}
118
120 const MCSubtargetInfo &STI,
121 raw_ostream &O) {
122 unsigned Imm = MI->getOperand(OpNo).getImm();
123 auto SysReg = RISCVSysReg::lookupSysRegByEncoding(Imm);
124 if (SysReg && SysReg->haveRequiredFeatures(STI.getFeatureBits()))
125 O << SysReg->Name;
126 else
127 O << Imm;
128}
129
130void RISCVInstPrinter::printFenceArg(const MCInst *MI, unsigned OpNo,
131 const MCSubtargetInfo &STI,
132 raw_ostream &O) {
133 unsigned FenceArg = MI->getOperand(OpNo).getImm();
134 assert (((FenceArg >> 4) == 0) && "Invalid immediate in printFenceArg");
135
136 if ((FenceArg & RISCVFenceField::I) != 0)
137 O << 'i';
138 if ((FenceArg & RISCVFenceField::O) != 0)
139 O << 'o';
140 if ((FenceArg & RISCVFenceField::R) != 0)
141 O << 'r';
142 if ((FenceArg & RISCVFenceField::W) != 0)
143 O << 'w';
144 if (FenceArg == 0)
145 O << "0";
146}
147
148void RISCVInstPrinter::printFRMArg(const MCInst *MI, unsigned OpNo,
149 const MCSubtargetInfo &STI, raw_ostream &O) {
150 auto FRMArg =
151 static_cast<RISCVFPRndMode::RoundingMode>(MI->getOperand(OpNo).getImm());
153 return;
154 O << ", " << RISCVFPRndMode::roundingModeToString(FRMArg);
155}
156
158 const MCSubtargetInfo &STI,
159 raw_ostream &O) {
160 unsigned Imm = MI->getOperand(OpNo).getImm();
161 if (Imm == 1) {
162 O << "min";
163 } else if (Imm == 30) {
164 O << "inf";
165 } else if (Imm == 31) {
166 O << "nan";
167 } else {
168 float FPVal = RISCVLoadFPImm::getFPImm(Imm);
169 // If the value is an integer, print a .0 fraction. Otherwise, use %g to
170 // which will not print trailing zeros and will use scientific notation
171 // if it is shorter than printing as a decimal. The smallest value requires
172 // 12 digits of precision including the decimal.
173 if (FPVal == (int)(FPVal))
174 O << format("%.1f", FPVal);
175 else
176 O << format("%.12g", FPVal);
177 }
178}
179
181 const MCSubtargetInfo &STI,
182 raw_ostream &O) {
183 const MCOperand &MO = MI->getOperand(OpNo);
184
185 assert(MO.isReg() && "printZeroOffsetMemOp can only print register operands");
186 O << "(";
187 printRegName(O, MO.getReg());
188 O << ")";
189}
190
191void RISCVInstPrinter::printVTypeI(const MCInst *MI, unsigned OpNo,
192 const MCSubtargetInfo &STI, raw_ostream &O) {
193 unsigned Imm = MI->getOperand(OpNo).getImm();
194 // Print the raw immediate for reserved values: vlmul[2:0]=4, vsew[2:0]=0b1xx,
195 // or non-zero in bits 8 and above.
197 RISCVVType::getSEW(Imm) > 64 || (Imm >> 8) != 0) {
198 O << Imm;
199 return;
200 }
201 // Print the text form.
203}
204
205void RISCVInstPrinter::printVMaskReg(const MCInst *MI, unsigned OpNo,
206 const MCSubtargetInfo &STI,
207 raw_ostream &O) {
208 const MCOperand &MO = MI->getOperand(OpNo);
209
210 assert(MO.isReg() && "printVMaskReg can only print register operands");
211 if (MO.getReg() == RISCV::NoRegister)
212 return;
213 O << ", ";
214 printRegName(O, MO.getReg());
215 O << ".t";
216}
217
219 return getRegisterName(Reg, ArchRegNames ? RISCV::NoRegAltName
220 : RISCV::ABIRegAltName);
221}
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
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:50
bool PrintBranchImmAsAddress
If true, a branch immediate (e.g.
Definition: MCInstPrinter.h:70
bool PrintAliases
True if we prefer aliases (e.g. nop) to raw mnemonics.
Definition: MCInstPrinter.h:59
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:24
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 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 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
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)
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:124