LLVM 22.0.0git
SystemZInstPrinterCommon.cpp
Go to the documentation of this file.
1//=- SystemZInstPrinterCommon.cpp - Common SystemZ MCInst to assembly funcs -=//
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
11#include "llvm/MC/MCExpr.h"
12#include "llvm/MC/MCInst.h"
13#include "llvm/MC/MCRegister.h"
14#include "llvm/MC/MCSymbol.h"
19#include <cassert>
20#include <cstdint>
21
22using namespace llvm;
23
24#define DEBUG_TYPE "asm-printer"
25
28 const MCOperand &DispMO,
29 MCRegister Index, raw_ostream &O) {
30 printOperand(DispMO, MAI, O);
31 if (Base || Index) {
32 O << '(';
33 if (Index) {
34 printRegName(O, Index);
35 O << ',';
36 }
37 if (Base)
39 else
40 O << '0';
41 O << ')';
42 }
43}
44
46 const MCAsmInfo *MAI,
47 raw_ostream &O) {
48 if (MO.isReg()) {
49 if (!MO.getReg())
50 O << '0';
51 else
52 printRegName(O, MO.getReg());
53 } else if (MO.isImm())
55 else if (MO.isExpr())
56 MAI->printExpr(O, *MO.getExpr());
57 else
58 llvm_unreachable("Invalid operand");
59}
60
62 printFormattedRegName(&MAI, Reg, O);
63}
64
65template <unsigned N>
67 raw_ostream &O) {
68 const MCOperand &MO = MI->getOperand(OpNum);
69 if (MO.isExpr()) {
70 MAI.printExpr(O, *MO.getExpr());
71 return;
72 }
73 uint64_t Value = static_cast<uint64_t>(MO.getImm());
74 assert(isUInt<N>(Value) && "Invalid uimm argument");
76}
77
78template <unsigned N>
80 raw_ostream &O) {
81 const MCOperand &MO = MI->getOperand(OpNum);
82 if (MO.isExpr()) {
83 MAI.printExpr(O, *MO.getExpr());
84 return;
85 }
86 int64_t Value = MI->getOperand(OpNum).getImm();
87 assert(isInt<N>(Value) && "Invalid simm argument");
89}
90
92 raw_ostream &O) {
93 printUImmOperand<1>(MI, OpNum, O);
94}
95
97 raw_ostream &O) {
98 printUImmOperand<2>(MI, OpNum, O);
99}
100
102 raw_ostream &O) {
103 printUImmOperand<3>(MI, OpNum, O);
104}
105
107 raw_ostream &O) {
108 printUImmOperand<4>(MI, OpNum, O);
109}
110
112 raw_ostream &O) {
113 printSImmOperand<8>(MI, OpNum, O);
114}
115
117 raw_ostream &O) {
118 printUImmOperand<8>(MI, OpNum, O);
119}
120
122 raw_ostream &O) {
123 printUImmOperand<12>(MI, OpNum, O);
124}
125
127 raw_ostream &O) {
128 printSImmOperand<16>(MI, OpNum, O);
129}
130
132 raw_ostream &O) {
133 printUImmOperand<16>(MI, OpNum, O);
134}
135
137 raw_ostream &O) {
138 printSImmOperand<32>(MI, OpNum, O);
139}
140
142 raw_ostream &O) {
143 printUImmOperand<32>(MI, OpNum, O);
144}
145
147 raw_ostream &O) {
148 printUImmOperand<48>(MI, OpNum, O);
149}
150
152 uint64_t Address, int OpNum,
153 raw_ostream &O) {
154 const MCOperand &MO = MI->getOperand(OpNum);
155
156 // If the label has already been resolved to an immediate offset (say, when
157 // we're running the disassembler), just print the immediate.
158 if (MO.isImm()) {
159 int64_t Offset = MO.getImm();
162 else
164 return;
165 }
166
167 // If the branch target is simply an address then print it in hex.
168 const MCConstantExpr *BranchTarget = dyn_cast<MCConstantExpr>(MO.getExpr());
169 int64_t TargetAddress;
170 if (BranchTarget && BranchTarget->evaluateAsAbsolute(TargetAddress)) {
171 markup(O, Markup::Target) << formatHex((uint64_t)TargetAddress);
172 } else {
173 // Otherwise, just print the expression.
174 MAI.printExpr(O, *MO.getExpr());
175 }
176}
177
179 uint64_t Address, int OpNum,
180 raw_ostream &O) {
181 // Output the PC-relative operand.
182 printPCRelOperand(MI, Address, OpNum, O);
183
184 // Output the TLS marker if present.
185 if ((unsigned)OpNum + 1 < MI->getNumOperands()) {
186 const MCOperand &MO = MI->getOperand(OpNum + 1);
187 const MCSymbolRefExpr &refExp = cast<MCSymbolRefExpr>(*MO.getExpr());
188 switch (refExp.getSpecifier()) {
189 case SystemZ::S_TLSGD:
190 O << ":tls_gdcall:";
191 break;
193 O << ":tls_ldcall:";
194 break;
195 default:
196 llvm_unreachable("Unexpected symbol kind");
197 }
198 O << refExp.getSymbol().getName();
199 }
200}
201
203 raw_ostream &O) {
204 printOperand(MI->getOperand(OpNum), &MAI, O);
205}
206
208 raw_ostream &O) {
209 printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1),
210 0, O);
211}
212
214 raw_ostream &O) {
215 printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1),
216 MI->getOperand(OpNum + 2).getReg(), O);
217}
218
220 raw_ostream &O) {
221 unsigned Base = MI->getOperand(OpNum).getReg();
222 const MCOperand &DispMO = MI->getOperand(OpNum + 1);
223 uint64_t Length = MI->getOperand(OpNum + 2).getImm();
224 printOperand(DispMO, &MAI, O);
225 O << '(' << Length;
226 if (Base) {
227 O << ",";
228 printRegName(O, Base);
229 }
230 O << ')';
231}
232
234 raw_ostream &O) {
235 unsigned Base = MI->getOperand(OpNum).getReg();
236 const MCOperand &DispMO = MI->getOperand(OpNum + 1);
237 unsigned Length = MI->getOperand(OpNum + 2).getReg();
238 printOperand(DispMO, &MAI, O);
239 O << "(";
241 if (Base) {
242 O << ",";
243 printRegName(O, Base);
244 }
245 O << ')';
246}
247
249 raw_ostream &O) {
250 printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1),
251 MI->getOperand(OpNum + 2).getReg(), O);
252}
253
255 raw_ostream &O) {
256 printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1),
257 MI->getOperand(OpNum + 2).getReg(), O);
258}
259
261 raw_ostream &O) {
262 static const char *const CondNames[] = {"o", "h", "nle", "l", "nhe",
263 "lh", "ne", "e", "nlh", "he",
264 "nl", "le", "nh", "no"};
265 uint64_t Imm = MI->getOperand(OpNum).getImm();
266 assert(Imm > 0 && Imm < 15 && "Invalid condition");
267 O << CondNames[Imm - 1];
268}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
IRTranslator LLVM IR MI
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:64
void printExpr(raw_ostream &, const MCExpr &) const
Definition: MCAsmInfo.cpp:153
WithMarkup markup(raw_ostream &OS, Markup M)
format_object< int64_t > formatHex(int64_t Value) const
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:52
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.
Definition: MCInstPrinter.h:75
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:33
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:190
const MCSymbol & getSymbol() const
Definition: MCExpr.h:227
uint16_t getSpecifier() const
Definition: MCExpr.h:233
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:188
void printUImmOperand(const MCInst *MI, int OpNum, raw_ostream &O)
void printU2ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O)
void printU3ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O)
void printBDXAddrOperand(const MCInst *MI, int OpNum, raw_ostream &O)
void printBDVAddrOperand(const MCInst *MI, int OpNum, raw_ostream &O)
void printU4ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O)
void printSImmOperand(const MCInst *MI, int OpNum, raw_ostream &O)
void printPCRelOperand(const MCInst *MI, uint64_t Address, int OpNum, raw_ostream &O)
void printS8ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O)
void printBDLAddrOperand(const MCInst *MI, int OpNum, raw_ostream &O)
void printOperand(const MCOperand &MO, const MCAsmInfo *MAI, raw_ostream &O)
void printPCRelTLSOperand(const MCInst *MI, uint64_t Address, int OpNum, raw_ostream &O)
void printRegName(raw_ostream &O, MCRegister Reg) override
Print the assembler register name.
virtual void printFormattedRegName(const MCAsmInfo *MAI, MCRegister Reg, raw_ostream &O)
void printLXAAddrOperand(const MCInst *MI, int OpNum, raw_ostream &O)
void printU12ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O)
void printU32ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O)
void printBDAddrOperand(const MCInst *MI, int OpNum, raw_ostream &O)
void printU1ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O)
void printU48ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O)
void printU8ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O)
void printAddress(const MCAsmInfo *MAI, MCRegister Base, const MCOperand &DispMO, MCRegister Index, raw_ostream &O)
void printCond4Operand(const MCInst *MI, int OpNum, raw_ostream &O)
void printBDRAddrOperand(const MCInst *MI, int OpNum, raw_ostream &O)
void printS32ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O)
void printS16ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O)
void printU16ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O)
LLVM Value Representation.
Definition: Value.h:75
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.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
@ Length
Definition: DWP.cpp:477