LLVM 24.0.0git
PISAInstPrinter.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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/// \file
10/// This file implements the PISA MCInst printer.
11///
12//===----------------------------------------------------------------------===//
13
14#include "PISAInstPrinter.h"
16#include "PISA.h"
17#include "PISABaseInfo.h"
18#include "PISAEnum.h"
19#include "PISAInstrInfo.h"
20#include "PISAMCExpr.h"
21#include "PISAMCInstLower.h"
22#include "PISAUtils.h"
23
25#include "llvm/MC/MCAsmInfo.h"
26#include "llvm/MC/MCExpr.h"
27#include "llvm/MC/MCInst.h"
28#include "llvm/MC/MCInstrInfo.h"
29#include "llvm/MC/MCSymbol.h"
34
35using namespace llvm;
36using namespace llvm::PISA;
37
38#define DEBUG_TYPE "asm-printer"
39
40static cl::opt<bool>
41 AlwaysPrintDefaultValue("pisa-always-print-default-value",
42 cl::desc("Always print default value"),
44
45// Include the auto-generated portion of the assembly writer.
46#include "PISAGenAsmWriter.inc"
47
49 StringRef Annot, const MCSubtargetInfo &STI,
50 raw_ostream &OS) {
51 this->STI = &STI;
52
54
55 printAnnotation(OS, Annot);
56}
57
59 if (Reg.isPhysical()) {
60 OS << getRegisterName(Reg);
61 } else {
62 std::pair<const char *, unsigned> DecodedReg =
64 OS << DecodedReg.first << DecodedReg.second;
65 }
66}
67
68void PISAInstPrinter::printImm1Opnd(const MCInst *MCI, unsigned OpNo,
69 raw_ostream &OS) {
70 const MCOperand &MCOp = MCI->getOperand(OpNo);
71 OS << formatImm(MCOp.getImm());
72}
73
74void PISAInstPrinter::printImm8Opnd(const MCInst *MCI, unsigned OpNo,
75 raw_ostream &OS) {
76 const MCOperand &MCOp = MCI->getOperand(OpNo);
77 OS << formatImm(MCOp.getImm());
78}
79
80void PISAInstPrinter::printImm16Opnd(const MCInst *MCI, unsigned OpNo,
81 raw_ostream &OS) {
82 const MCOperand &MCOp = MCI->getOperand(OpNo);
83 OS << formatImm(MCOp.getImm());
84}
85
86void PISAInstPrinter::printImm32Opnd(const MCInst *MCI, unsigned OpNo,
87 raw_ostream &OS) {
88 const MCOperand &MCOp = MCI->getOperand(OpNo);
89 OS << formatImm(MCOp.getImm());
90}
91
92void PISAInstPrinter::printImm64Opnd(const MCInst *MCI, unsigned OpNo,
93 raw_ostream &OS) {
94 const MCOperand &MCOp = MCI->getOperand(OpNo);
95 OS << formatImm(MCOp.getImm());
96}
97
98void PISAInstPrinter::printImm128Opnd(const MCInst *MCI, unsigned OpNo,
99 raw_ostream &OS) {
100 const MCOperand &MCOp = MCI->getOperand(OpNo);
101 OS << formatImm(MCOp.getImm());
102}
103
104void PISAInstPrinter::printFpImm16Opnd(const MCInst *MCI, unsigned OpNo,
105 raw_ostream &OS) {
106 const MCOperand &MCOp = MCI->getOperand(OpNo);
107 OS << format_hex(MCOp.getHFPImm(), /*Width=*/6, /*Upper=*/true);
108}
109
110void PISAInstPrinter::printFpImm32Opnd(const MCInst *MCI, unsigned OpNo,
111 raw_ostream &OS) {
112 const MCOperand &MCOp = MCI->getOperand(OpNo);
113 OS << format_hex(MCOp.getSFPImm(), /*Width=*/10, /*Upper=*/true);
114}
115
116void PISAInstPrinter::printFpImm64Opnd(const MCInst *MCI, unsigned OpNo,
117 raw_ostream &OS) {
118 const MCOperand &MCOp = MCI->getOperand(OpNo);
119 OS << format_hex(MCOp.getDFPImm(), /*Width=*/18, /*Upper=*/true);
120}
121
122void PISAInstPrinter::printRegOpnd(unsigned RCID, const MCInst *MCI,
123 unsigned OpNo, raw_ostream &OS) {
124 const MCOperand &MCOp = MCI->getOperand(OpNo);
125 printRegName(OS, MCOp.getReg());
126 printSwizzle(MCI, OpNo, OS);
127}
128
130 unsigned OpNo, raw_ostream &OS) {
131 const MCOperand &MCOp = MCI->getOperand(OpNo);
132 MAI.printExpr(OS, *MCOp.getExpr());
133}
134
135void PISAInstPrinter::printLocalVariableOpnd(const MCInst *MCI, unsigned OpNo,
136 raw_ostream &OS) {
138 printOperand(MCI, OpNo, OS);
139}
140
142 uint64_t Address, unsigned OpNo,
143 raw_ostream &OS) {
144 const MCOperand &MCOp = MCI->getOperand(OpNo);
145 MAI.printExpr(OS, *MCOp.getExpr());
146}
147
148void PISAInstPrinter::printMemScopeOpnd(const MCInst *MCI, unsigned OpNo,
149 raw_ostream &OS) {
150 static const char *MemScopeStrs[] = {
151 ".system", // llvm::AtomicScope::System
152 ".gpu", // llvm::AtomicScope::Device
153 ".workgroup", // llvm::AtomicScope::Workgroup
154 ".subgroup", // llvm::AtomicScope::Wavefront
155 };
156 const MCOperand &MCOp = MCI->getOperand(OpNo);
157 const unsigned Scope = MCOp.getImm();
158 assert(Scope < std::size(MemScopeStrs) && "Invalid memory scope");
159 OS << MemScopeStrs[Scope];
160}
161
162void PISAInstPrinter::printSwizzle(const MCInst *MI, unsigned OpNo,
163 raw_ostream &O) {
165 swizzleRepr(O, static_cast<unsigned>(Swizzle));
166}
167
168void PISAInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
169 raw_ostream &O, const char *Modifier) {
170 assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
172 assert(MI->getOperand(OpNo).isImm() && "expecting imm operand");
173 unsigned int FrameIndex = MI->getOperand(OpNo).getImm();
174 O << "@R" << FrameIndex;
175 } else if (OpNo < MI->getNumOperands()) {
176 const MCOperand &Op = MI->getOperand(OpNo);
177 if (Op.isReg()) {
178 printRegName(O, Op.getReg());
179 printSwizzle(MI, OpNo, O);
180 } else if (Op.isImm())
181 O << formatImm((int64_t)Op.getImm());
182 else if (Op.isSFPImm())
183 printFpImm32Opnd(MI, OpNo, O);
184 else if (Op.isDFPImm())
185 printFpImm64Opnd(MI, OpNo, O);
186 else if (Op.isExpr())
187 MAI.printExpr(O, *Op.getExpr());
188 else
189 llvm_unreachable("Unexpected operand type");
190 }
191}
192
193void PISAInstPrinter::negateRepr(const MCInst *MI, unsigned OpNo,
194 raw_ostream &O) {
195 int64_t DoNegate = MI->getOperand(OpNo).getImm();
196 if (DoNegate)
197 O << "!";
198}
199
200void PISAInstPrinter::swizzleRepr(raw_ostream &O, unsigned SwizzleVal) {
201 switch (static_cast<Swizzle>(SwizzleVal)) {
202 case Swizzle::X:
203 O << ".x";
204 break;
205 case Swizzle::Y:
206 O << ".y";
207 break;
208 case Swizzle::Z:
209 O << ".z";
210 break;
211 case Swizzle::W:
212 O << ".w";
213 break;
214 case Swizzle::XYZW:
215 O << ".xyzw";
216 break;
217 case Swizzle::XY:
218 O << ".xy";
219 break;
220 case Swizzle::ZW:
221 O << ".zw";
222 break;
223 case Swizzle::NONE:
224 break;
225 }
226}
227
229 unsigned OpNo,
230 raw_ostream &O) {
231 printOperand(MI, OpNo, O);
232
233 // print function args if any
234 O << " (";
235 for (unsigned ArgIdx = OpNo + 1; ArgIdx < MI->getNumOperands(); ++ArgIdx) {
236 printOperand(MI, ArgIdx, O);
237 if (ArgIdx + 1 < MI->getNumOperands())
238 O << ", ";
239 }
240 O << ");";
241}
242
244 raw_ostream &O) {
245 int64_t Val = MI->getOperand(OpNo).getImm();
246 // print imm offset only when it's not zero
247 if (Val > 0) {
248 O << " + " << formatImm(Val);
249 } else if (Val < 0) {
250 if (Val == std::numeric_limits<int64_t>::min())
251 O << " - " << llvm::format("%" PRIu64, Val);
252 else
253 O << " - " << formatImm(-Val);
254 }
255}
256
257void PISAInstPrinter::printBfnOpcode(const MCInst *MI, unsigned OpNo,
258 raw_ostream &O) {
259 int64_t Val = MI->getOperand(OpNo).getImm();
260 O << format_hex(Val & 0xFF, 4);
261}
262
263void PISAInstPrinter::printStringImm(const MCInst *MI, unsigned OpNo,
264 raw_ostream &O) {
265 const unsigned NumOps = MI->getNumOperands();
266 unsigned StrStartIndex = OpNo;
267 while (StrStartIndex < NumOps) {
268 if (MI->getOperand(StrStartIndex).isReg())
269 break;
270
271 std::string Str = getPISAStringOperand(*MI, OpNo);
272 if (StrStartIndex != OpNo)
273 O << ' '; // Add a space if we're starting a new string/argument.
274 O << '"';
275 for (char C : Str) {
276 if (C == '"')
277 O.write('\\'); // Escape " characters (might break for complex UTF-8).
278 O.write(C);
279 }
280 O << '"';
281
282 unsigned NumOpsInString = (Str.size() / 4) + 1;
283 StrStartIndex += NumOpsInString;
284 }
285}
286
288 raw_ostream &OS,
289 const char * /*Modifier*/) {
290 // [ Base OP Offset ]
291 OS << "[";
292 printOperand(MI, OpNo, OS);
293 const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
294 if (OffsetOp.isImm()) {
295 int64_t Val = OffsetOp.getImm();
296 if (Val != 0) {
297 if (Val > 0)
298 OS << " + " << formatImm(Val);
299 else if (Val == std::numeric_limits<int64_t>::min())
300 OS << " - " << llvm::format("%" PRIu64, Val);
301 else
302 OS << " - " << formatImm(-Val);
303 }
304 } else {
305 assert(OffsetOp.isReg() && "Register expected");
306 OS << " + ";
307 printRegName(OS, OffsetOp.getReg());
308 printSwizzle(MI, (OpNo + 1), OS);
309 }
310 OS << "]";
311}
312
314 int OpNo, raw_ostream &OS,
315 const char * /* Modifier */) {
316 OS << "[";
317 for (char C : Pattern) {
318 switch (C) {
319 case '{':
320 OS << "{";
321 break;
322 case '}':
323 OS << "}";
324 break;
325 case ',':
326 OS << ", ";
327 break;
328 default:
329 printOperand(MI, OpNo++, OS);
330 break;
331 }
332 }
333 OS << "]";
334}
335
337 raw_ostream &OS) {
338 const MCOperand &ArgIdxOp = MCI->getOperand(OpNo);
339 const MCOperand &OffsetOp = MCI->getOperand(OpNo + 1);
340 assert(ArgIdxOp.isImm());
341
342 // Check for an extra expression operand carrying the kernel argument name.
343 unsigned NameOpIdx = OpNo + 2;
344 if (NameOpIdx < MCI->getNumOperands() &&
345 MCI->getOperand(NameOpIdx).isExpr()) {
346 const MCSymbolRefExpr *SRE =
347 cast<MCSymbolRefExpr>(MCI->getOperand(NameOpIdx).getExpr());
348 OS << "[%" << SRE->getSymbol().getName();
349 } else {
350 OS << "[%arg" << ArgIdxOp.getImm();
351 }
352
353 if (OffsetOp.isImm()) {
354 int64_t Offset = OffsetOp.getImm();
355 if (Offset != 0)
356 OS << llvm::format("%+" PRId64, Offset);
357 } else {
358 assert(OffsetOp.isReg() && "Register expected");
359 OS << " + ";
360 printRegName(OS, OffsetOp.getReg());
361 printSwizzle(MCI, (OpNo + 1), OS);
362 }
363 OS << "]";
364}
365
367 const MCAsmInfo *MAI) {
368 // This is a modification of MCSymbol::print() that prints non-printable
369 // characters differently.
370 if (!MAI || MAI->isValidUnquotedName(Name)) {
371 OS << Name;
372 return;
373 }
374
375 if (MAI && !MAI->supportsNameQuoting())
376 report_fatal_error("Symbol name with unsupported characters");
377
378 OS << '"';
379 for (char C : Name) {
380 if (C == '\n')
381 OS << "\\n";
382 else if (C == '"')
383 OS << "\\\"";
384 else if (isPrint(C))
385 OS << C;
386 else
387 OS << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
388 }
389 OS << '"';
390}
391
392void PISAInstPrinter::printEnumOption(EnumOptionClass OptClass,
393 bool IsMandatoryField,
394 unsigned DefaultVal, const MCInst *MCI,
395 unsigned OpNo, raw_ostream &OS) {
396 const MCOperand &MCOp = MCI->getOperand(OpNo);
397
398 unsigned Val = MCOp.getImm();
399 if (!IsMandatoryField && Val == DefaultVal)
400 return;
401
402 const EnumOptionEntry *Entry = lookupEnumOptionByValue(OptClass, Val);
403 assert(Entry && "Enum value not found from lookup table");
404 if (!Entry)
405 return;
406
407 StringRef OptStrRef = getEnumOptionEntryStr(Entry->OptStr);
408 if (!OptStrRef.empty())
409 OS << "." << OptStrRef;
410}
411
413PISAInstPrinter::printBoolOptionOpnd(PISA::BoolOptionID OptID) {
414 return [=](const MCInst *MCI, unsigned OpNo, raw_ostream &OS) -> void {
415 const MCOperand &MCOp = MCI->getOperand(OpNo);
416 if (MCOp.getImm()) {
417 StringRef OptStr =
418 getBoolOptionTableEntryStr(lookupBoolOptionByID(OptID)->OptStr);
419 OS << "." << OptStr;
420 }
421 };
422}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
IRTranslator LLVM IR MI
std::pair< Instruction::BinaryOps, Value * > OffsetOp
Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
This file declares common definitions used by the PISA MC components.
This file declares PISA enumeration and operand option tables.
static cl::opt< bool > AlwaysPrintDefaultValue("pisa-always-print-default-value", cl::desc("Always print default value"), cl::init(false), cl::ReallyHidden)
This file declares the PISA MCInst printer.
This file declares instruction information for the PISA target.
This file declares PISA-specific MC expressions for floating-point immediates.
This file declares lowering from PISA MachineInstrs to MCInsts.
This file declares encoding of PISA virtual registers.
This file declares utility functions for the PISA backend.
This file declares the top-level interfaces for the PISA target.
This file contains some functions that are useful when dealing with strings.
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition MCAsmInfo.h:69
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.
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:207
const MCOperand & getOperand(unsigned i) const
Definition MCInst.h:229
Instances of this class represent operands of the MCInst class.
Definition MCInst.h:39
int64_t getImm() const
Definition MCInst.h:86
uint16_t getHFPImm() const
Definition MCInst.h:96
bool isImm() const
Definition MCInst.h:67
MCRegister getReg() const
Returns the register number.
Definition MCInst.h:75
const MCExpr * getExpr() const
Definition MCInst.h:130
uint32_t getSFPImm() const
Definition MCInst.h:106
uint64_t getDFPImm() const
Definition MCInst.h:116
bool isExpr() const
Definition MCInst.h:71
Wrapper class representing physical registers and target specific virtual registers.
Definition MCRegister.h:42
Generic base class for all target subtargets.
Represent a reference to a symbol from inside an expression.
Definition MCExpr.h:187
const MCSymbol & getSymbol() const
Definition MCExpr.h:223
StringRef getName() const
getName - Get the symbol name.
Definition MCSymbol.h:188
void printImm16Opnd(const MCInst *MCI, unsigned OpNo, raw_ostream &OS)
void printBfnOpcode(const MCInst *MI, unsigned OpNo, raw_ostream &O)
void printEnumOption(PISA::EnumOptionClass OptClass, bool IsMandatoryField, unsigned DefaultVal, const MCInst *MCI, unsigned OpNo, raw_ostream &OS)
void printLocalVariableOpnd(const MCInst *MCI, unsigned OpNo, raw_ostream &OS)
void printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O, const char *Modifier=nullptr)
void printImm128Opnd(const MCInst *MCI, unsigned OpNo, raw_ostream &OS)
void printImm64Opnd(const MCInst *MCI, unsigned OpNo, raw_ostream &OS)
void printFpImm64Opnd(const MCInst *MCI, unsigned OpNo, raw_ostream &OS)
void printRegOpnd(unsigned RCID, const MCInst *MCI, unsigned OpNo, raw_ostream &OS)
void printImm8Opnd(const MCInst *MCI, unsigned OpNo, raw_ostream &OS)
printOpndFn printBoolOptionOpnd(PISA::BoolOptionID OptID)
static const char * getRegisterName(MCRegister Reg)
void printFpImm16Opnd(const MCInst *MCI, unsigned OpNo, raw_ostream &OS)
void printFpImm32Opnd(const MCInst *MCI, unsigned OpNo, raw_ostream &OS)
void printBrTargetOpnd(const MCInst *MCI, uint64_t Address, unsigned OpNo, raw_ostream &OS)
void printImm1Opnd(const MCInst *MCI, unsigned OpNo, raw_ostream &OS)
void printMemScopeOpnd(const MCInst *MCI, unsigned OpNo, raw_ostream &OS)
void printFunctionCallTargetOpnd(const MCInst *MI, unsigned OpNo, raw_ostream &O)
static void printSymbolName(raw_ostream &OS, StringRef Name, const MCAsmInfo *MAI)
void printParamMemOperand(const MCInst *MI, int OpNo, raw_ostream &O)
void printStringImm(const MCInst *MI, unsigned OpNo, raw_ostream &O)
void printMemSeqOperand(StringRef Pattern, const MCInst *MI, int OpNo, raw_ostream &OS, const char *Modifier)
void negateRepr(const MCInst *MI, unsigned OpNo, raw_ostream &O)
void printAddrOffsetImm(const MCInst *MI, unsigned OpNo, raw_ostream &O)
void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O, const char *Modifier=nullptr)
void printGlobalVariableOpnd(const MCInst *MCI, uint64_t Address, unsigned OpNo, raw_ostream &OS)
void printRegName(raw_ostream &OS, MCRegister Reg) override
Print the assembler register name.
void printInst(const MCInst *MI, uint64_t Address, StringRef Annot, const MCSubtargetInfo &STI, raw_ostream &OS) override
Print the specified MCInst to the specified raw_ostream.
std::function< void(const MCInst *, unsigned, raw_ostream &)> printOpndFn
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O)
void printImm32Opnd(const MCInst *MCI, unsigned OpNo, raw_ostream &OS)
static PISA::Swizzle getSwizzle(const MCInst &MI, unsigned OpNo)
static bool isVariableRef(const MCInst &MI, unsigned int OpNo)
static std::pair< const char *, unsigned > decodeVirtualRegister(MCRegister Reg)
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
std::string getPISAStringOperand(const InstType &MI, unsigned StartIndex)
char hexdigit(unsigned X, bool LowerCase=false)
hexdigit - Return the hexadecimal character for the given number X (which should be less than 16).
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
Definition Format.h:164
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition Format.h:102
DWARFExpression::Operation Op
bool isPrint(char C)
Checks whether character C is printable.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559