LLVM  16.0.0git
SystemZInstPrinter.cpp
Go to the documentation of this file.
1 //===- SystemZInstPrinter.cpp - Convert SystemZ MCInst to assembly 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 #include "SystemZInstPrinter.h"
10 #include "llvm/MC/MCExpr.h"
11 #include "llvm/MC/MCInst.h"
12 #include "llvm/MC/MCSymbol.h"
13 #include "llvm/Support/Casting.h"
17 #include <cassert>
18 #include <cstdint>
19 
20 using namespace llvm;
21 
22 #define DEBUG_TYPE "asm-printer"
23 
24 #include "SystemZGenAsmWriter.inc"
25 
26 void SystemZInstPrinter::printAddress(const MCAsmInfo *MAI, unsigned Base,
27  const MCOperand &DispMO, unsigned Index,
28  raw_ostream &O) {
29  printOperand(DispMO, MAI, O);
30  if (Base || Index) {
31  O << '(';
32  if (Index) {
34  if (Base)
35  O << ',';
36  }
37  if (Base)
39  O << ')';
40  }
41 }
42 
44  raw_ostream &O) {
45  if (MO.isReg()) {
46  if (!MO.getReg())
47  O << '0';
48  else
50  }
51  else if (MO.isImm())
52  O << markup("<imm:") << MO.getImm() << markup(">");
53  else if (MO.isExpr())
54  MO.getExpr()->print(O, MAI);
55  else
56  llvm_unreachable("Invalid operand");
57 }
58 
60  unsigned RegNo,
61  raw_ostream &O) const {
62  const char *RegName = getRegisterName(RegNo);
63  if (MAI->getAssemblerDialect() == AD_HLASM) {
64  // Skip register prefix so that only register number is left
65  assert(isalpha(RegName[0]) && isdigit(RegName[1]));
66  O << markup("<reg:") << (RegName + 1) << markup(">");
67  } else
68  O << markup("<reg:") << '%' << RegName << markup(">");
69 }
70 
72  StringRef Annot, const MCSubtargetInfo &STI,
73  raw_ostream &O) {
74  printInstruction(MI, Address, O);
75  printAnnotation(O, Annot);
76 }
77 
78 template <unsigned N>
79 void SystemZInstPrinter::printUImmOperand(const MCInst *MI, int OpNum,
80  raw_ostream &O) {
81  int64_t Value = MI->getOperand(OpNum).getImm();
82  assert(isUInt<N>(Value) && "Invalid uimm argument");
83  O << markup("<imm:") << Value << markup(">");
84 }
85 
86 template <unsigned N>
87 void SystemZInstPrinter::printSImmOperand(const MCInst *MI, int OpNum,
88  raw_ostream &O) {
89  int64_t Value = MI->getOperand(OpNum).getImm();
90  assert(isInt<N>(Value) && "Invalid simm argument");
91  O << markup("<imm:") << Value << markup(">");
92 }
93 
94 void SystemZInstPrinter::printU1ImmOperand(const MCInst *MI, int OpNum,
95  raw_ostream &O) {
96  printUImmOperand<1>(MI, OpNum, O);
97 }
98 
99 void SystemZInstPrinter::printU2ImmOperand(const MCInst *MI, int OpNum,
100  raw_ostream &O) {
101  printUImmOperand<2>(MI, OpNum, O);
102 }
103 
104 void SystemZInstPrinter::printU3ImmOperand(const MCInst *MI, int OpNum,
105  raw_ostream &O) {
106  printUImmOperand<3>(MI, OpNum, O);
107 }
108 
109 void SystemZInstPrinter::printU4ImmOperand(const MCInst *MI, int OpNum,
110  raw_ostream &O) {
111  printUImmOperand<4>(MI, OpNum, O);
112 }
113 
114 void SystemZInstPrinter::printU6ImmOperand(const MCInst *MI, int OpNum,
115  raw_ostream &O) {
116  printUImmOperand<6>(MI, OpNum, O);
117 }
118 
119 void SystemZInstPrinter::printS8ImmOperand(const MCInst *MI, int OpNum,
120  raw_ostream &O) {
121  printSImmOperand<8>(MI, OpNum, O);
122 }
123 
124 void SystemZInstPrinter::printU8ImmOperand(const MCInst *MI, int OpNum,
125  raw_ostream &O) {
126  printUImmOperand<8>(MI, OpNum, O);
127 }
128 
129 void SystemZInstPrinter::printU12ImmOperand(const MCInst *MI, int OpNum,
130  raw_ostream &O) {
131  printUImmOperand<12>(MI, OpNum, O);
132 }
133 
134 void SystemZInstPrinter::printS16ImmOperand(const MCInst *MI, int OpNum,
135  raw_ostream &O) {
136  printSImmOperand<16>(MI, OpNum, O);
137 }
138 
139 void SystemZInstPrinter::printU16ImmOperand(const MCInst *MI, int OpNum,
140  raw_ostream &O) {
141  printUImmOperand<16>(MI, OpNum, O);
142 }
143 
144 void SystemZInstPrinter::printS32ImmOperand(const MCInst *MI, int OpNum,
145  raw_ostream &O) {
146  printSImmOperand<32>(MI, OpNum, O);
147 }
148 
149 void SystemZInstPrinter::printU32ImmOperand(const MCInst *MI, int OpNum,
150  raw_ostream &O) {
151  printUImmOperand<32>(MI, OpNum, O);
152 }
153 
154 void SystemZInstPrinter::printU48ImmOperand(const MCInst *MI, int OpNum,
155  raw_ostream &O) {
156  printUImmOperand<48>(MI, OpNum, O);
157 }
158 
159 void SystemZInstPrinter::printPCRelOperand(const MCInst *MI, int OpNum,
160  raw_ostream &O) {
161  const MCOperand &MO = MI->getOperand(OpNum);
162  if (MO.isImm()) {
163  O << markup("<imm:") << "0x";
164  O.write_hex(MO.getImm());
165  O << markup(">");
166  } else
167  MO.getExpr()->print(O, &MAI);
168 }
169 
170 void SystemZInstPrinter::printPCRelTLSOperand(const MCInst *MI,
171  uint64_t Address, int OpNum,
172  raw_ostream &O) {
173  // Output the PC-relative operand.
174  printPCRelOperand(MI, OpNum, O);
175 
176  // Output the TLS marker if present.
177  if ((unsigned)OpNum + 1 < MI->getNumOperands()) {
178  const MCOperand &MO = MI->getOperand(OpNum + 1);
179  const MCSymbolRefExpr &refExp = cast<MCSymbolRefExpr>(*MO.getExpr());
180  switch (refExp.getKind()) {
182  O << ":tls_gdcall:";
183  break;
185  O << ":tls_ldcall:";
186  break;
187  default:
188  llvm_unreachable("Unexpected symbol kind");
189  }
190  O << refExp.getSymbol().getName();
191  }
192 }
193 
194 void SystemZInstPrinter::printOperand(const MCInst *MI, int OpNum,
195  raw_ostream &O) {
196  printOperand(MI->getOperand(OpNum), &MAI, O);
197 }
198 
199 void SystemZInstPrinter::printBDAddrOperand(const MCInst *MI, int OpNum,
200  raw_ostream &O) {
201  printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1),
202  0, O);
203 }
204 
205 void SystemZInstPrinter::printBDXAddrOperand(const MCInst *MI, int OpNum,
206  raw_ostream &O) {
207  printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1),
208  MI->getOperand(OpNum + 2).getReg(), O);
209 }
210 
211 void SystemZInstPrinter::printBDLAddrOperand(const MCInst *MI, int OpNum,
212  raw_ostream &O) {
213  unsigned Base = MI->getOperand(OpNum).getReg();
214  const MCOperand &DispMO = MI->getOperand(OpNum + 1);
215  uint64_t Length = MI->getOperand(OpNum + 2).getImm();
216  printOperand(DispMO, &MAI, O);
217  O << '(' << Length;
218  if (Base) {
219  O << ",";
220  printRegName(O, Base);
221  }
222  O << ')';
223 }
224 
225 void SystemZInstPrinter::printBDRAddrOperand(const MCInst *MI, int OpNum,
226  raw_ostream &O) {
227  unsigned Base = MI->getOperand(OpNum).getReg();
228  const MCOperand &DispMO = MI->getOperand(OpNum + 1);
229  unsigned Length = MI->getOperand(OpNum + 2).getReg();
230  printOperand(DispMO, &MAI, O);
231  O << "(";
232  printRegName(O, Length);
233  if (Base) {
234  O << ",";
235  printRegName(O, Base);
236  }
237  O << ')';
238 }
239 
240 void SystemZInstPrinter::printBDVAddrOperand(const MCInst *MI, int OpNum,
241  raw_ostream &O) {
242  printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1),
243  MI->getOperand(OpNum + 2).getReg(), O);
244 }
245 
246 void SystemZInstPrinter::printCond4Operand(const MCInst *MI, int OpNum,
247  raw_ostream &O) {
248  static const char *const CondNames[] = {
249  "o", "h", "nle", "l", "nhe", "lh", "ne",
250  "e", "nlh", "he", "nl", "le", "nh", "no"
251  };
252  uint64_t Imm = MI->getOperand(OpNum).getImm();
253  assert(Imm > 0 && Imm < 15 && "Invalid condition");
254  O << CondNames[Imm - 1];
255 }
llvm::MCSymbolRefExpr::getKind
VariantKind getKind() const
Definition: MCExpr.h:401
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:109
MathExtras.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::SystemZInstPrinter::printInst
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.
Definition: SystemZInstPrinter.cpp:71
llvm::MCOperand::isReg
bool isReg() const
Definition: MCInst.h:61
llvm::MCAsmInfo
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
llvm::SystemZInstPrinter::printFormattedRegName
void printFormattedRegName(const MCAsmInfo *MAI, unsigned RegNo, raw_ostream &O) const
Definition: SystemZInstPrinter.cpp:59
ErrorHandling.h
llvm::SystemZInstPrinter::getRegisterName
static const char * getRegisterName(unsigned RegNo)
llvm::MCAsmInfo::getAssemblerDialect
unsigned getAssemblerDialect() const
Definition: MCAsmInfo.h:686
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::SystemZInstPrinter::printOperand
void printOperand(const MCOperand &MO, const MCAsmInfo *MAI, raw_ostream &O)
Definition: SystemZInstPrinter.cpp:43
SystemZInstPrinter.h
llvm::MCOperand::getImm
int64_t getImm() const
Definition: MCInst.h:80
MCSymbol.h
llvm::MCSymbolRefExpr::VK_TLSLDM
@ VK_TLSLDM
Definition: MCExpr.h:211
MCInst.h
llvm::dwarf::Index
Index
Definition: Dwarf.h:472
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::MCSymbolRefExpr::getSymbol
const MCSymbol & getSymbol() const
Definition: MCExpr.h:399
llvm::MCSymbol::getName
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:198
llvm::MCInstPrinter::printAnnotation
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
Definition: MCInstPrinter.cpp:50
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:264
llvm::MCOperand::isImm
bool isImm() const
Definition: MCInst.h:62
uint64_t
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MCSymbolRefExpr
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::SystemZInstPrinter::printInstruction
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O)
llvm::AD_HLASM
@ AD_HLASM
Definition: SystemZMCAsmInfo.h:18
llvm::SystemZInstPrinter::printAddress
void printAddress(const MCAsmInfo *MAI, unsigned Base, const MCOperand &DispMO, unsigned Index, raw_ostream &O)
Definition: SystemZInstPrinter.cpp:26
Casting.h
llvm::MCOperand::getExpr
const MCExpr * getExpr() const
Definition: MCInst.h:114
llvm::MCSymbolRefExpr::VK_TLSGD
@ VK_TLSGD
Definition: MCExpr.h:209
llvm::MCOperand::isExpr
bool isExpr() const
Definition: MCInst.h:65
llvm::RISCVMatInt::Imm
@ Imm
Definition: RISCVMatInt.h:23
llvm::MCInstPrinter::markup
StringRef markup(StringRef s) const
Utility functions to make adding mark ups simpler.
Definition: MCInstPrinter.cpp:174
llvm::MCInstPrinter::MAI
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:49
llvm::MCExpr::print
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:41
RegName
#define RegName(no)
llvm::MCOperand
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
raw_ostream.h
llvm::SystemZInstPrinter::printRegName
void printRegName(raw_ostream &O, unsigned RegNo) const override
Print the assembler register name.
Definition: SystemZInstPrinter.h:46
MCExpr.h
llvm::MCSubtargetInfo
Generic base class for all target subtargets.
Definition: MCSubtargetInfo.h:77
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::sampleprof::Base
@ Base
Definition: Discriminator.h:58
llvm::MCOperand::getReg
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:69