LLVM  13.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 
27  int64_t Disp, unsigned Index,
28  raw_ostream &O) {
29  O << Disp;
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 << MO.getImm();
53  else if (MO.isExpr())
54  MO.getExpr()->print(O, MAI);
55  else
56  llvm_unreachable("Invalid operand");
57 }
58 
60  unsigned RegNo, raw_ostream &O) {
61  const char *RegName = getRegisterName(RegNo);
62  if (MAI->getAssemblerDialect() == AD_HLASM) {
63  // Skip register prefix so that only register number is left
64  assert(isalpha(RegName[0]) && isdigit(RegName[1]));
65  O << (RegName + 1);
66  } else
67  O << '%' << RegName;
68 }
69 
70 void SystemZInstPrinter::printInst(const MCInst *MI, uint64_t Address,
71  StringRef Annot, const MCSubtargetInfo &STI,
72  raw_ostream &O) {
74  printAnnotation(O, Annot);
75 }
76 
77 template <unsigned N>
78 static void printUImmOperand(const MCInst *MI, int OpNum, raw_ostream &O) {
79  int64_t Value = MI->getOperand(OpNum).getImm();
80  assert(isUInt<N>(Value) && "Invalid uimm argument");
81  O << Value;
82 }
83 
84 template <unsigned N>
85 static void printSImmOperand(const MCInst *MI, int OpNum, raw_ostream &O) {
86  int64_t Value = MI->getOperand(OpNum).getImm();
87  assert(isInt<N>(Value) && "Invalid simm argument");
88  O << Value;
89 }
90 
91 void SystemZInstPrinter::printU1ImmOperand(const MCInst *MI, int OpNum,
92  raw_ostream &O) {
93  printUImmOperand<1>(MI, OpNum, O);
94 }
95 
96 void SystemZInstPrinter::printU2ImmOperand(const MCInst *MI, int OpNum,
97  raw_ostream &O) {
98  printUImmOperand<2>(MI, OpNum, O);
99 }
100 
101 void SystemZInstPrinter::printU3ImmOperand(const MCInst *MI, int OpNum,
102  raw_ostream &O) {
103  printUImmOperand<3>(MI, OpNum, O);
104 }
105 
106 void SystemZInstPrinter::printU4ImmOperand(const MCInst *MI, int OpNum,
107  raw_ostream &O) {
108  printUImmOperand<4>(MI, OpNum, O);
109 }
110 
111 void SystemZInstPrinter::printU6ImmOperand(const MCInst *MI, int OpNum,
112  raw_ostream &O) {
113  printUImmOperand<6>(MI, OpNum, O);
114 }
115 
116 void SystemZInstPrinter::printS8ImmOperand(const MCInst *MI, int OpNum,
117  raw_ostream &O) {
118  printSImmOperand<8>(MI, OpNum, O);
119 }
120 
121 void SystemZInstPrinter::printU8ImmOperand(const MCInst *MI, int OpNum,
122  raw_ostream &O) {
123  printUImmOperand<8>(MI, OpNum, O);
124 }
125 
126 void SystemZInstPrinter::printU12ImmOperand(const MCInst *MI, int OpNum,
127  raw_ostream &O) {
128  printUImmOperand<12>(MI, OpNum, O);
129 }
130 
131 void SystemZInstPrinter::printS16ImmOperand(const MCInst *MI, int OpNum,
132  raw_ostream &O) {
133  printSImmOperand<16>(MI, OpNum, O);
134 }
135 
136 void SystemZInstPrinter::printU16ImmOperand(const MCInst *MI, int OpNum,
137  raw_ostream &O) {
138  printUImmOperand<16>(MI, OpNum, O);
139 }
140 
141 void SystemZInstPrinter::printS32ImmOperand(const MCInst *MI, int OpNum,
142  raw_ostream &O) {
143  printSImmOperand<32>(MI, OpNum, O);
144 }
145 
146 void SystemZInstPrinter::printU32ImmOperand(const MCInst *MI, int OpNum,
147  raw_ostream &O) {
148  printUImmOperand<32>(MI, OpNum, O);
149 }
150 
151 void SystemZInstPrinter::printU48ImmOperand(const MCInst *MI, int OpNum,
152  raw_ostream &O) {
153  printUImmOperand<48>(MI, OpNum, O);
154 }
155 
156 void SystemZInstPrinter::printPCRelOperand(const MCInst *MI, int OpNum,
157  raw_ostream &O) {
158  const MCOperand &MO = MI->getOperand(OpNum);
159  if (MO.isImm()) {
160  O << "0x";
161  O.write_hex(MO.getImm());
162  } else
163  MO.getExpr()->print(O, &MAI);
164 }
165 
166 void SystemZInstPrinter::printPCRelTLSOperand(const MCInst *MI,
167  uint64_t Address, int OpNum,
168  raw_ostream &O) {
169  // Output the PC-relative operand.
170  printPCRelOperand(MI, OpNum, O);
171 
172  // Output the TLS marker if present.
173  if ((unsigned)OpNum + 1 < MI->getNumOperands()) {
174  const MCOperand &MO = MI->getOperand(OpNum + 1);
175  const MCSymbolRefExpr &refExp = cast<MCSymbolRefExpr>(*MO.getExpr());
176  switch (refExp.getKind()) {
178  O << ":tls_gdcall:";
179  break;
181  O << ":tls_ldcall:";
182  break;
183  default:
184  llvm_unreachable("Unexpected symbol kind");
185  }
186  O << refExp.getSymbol().getName();
187  }
188 }
189 
190 void SystemZInstPrinter::printOperand(const MCInst *MI, int OpNum,
191  raw_ostream &O) {
192  printOperand(MI->getOperand(OpNum), &MAI, O);
193 }
194 
195 void SystemZInstPrinter::printBDAddrOperand(const MCInst *MI, int OpNum,
196  raw_ostream &O) {
197  printAddress(&MAI, MI->getOperand(OpNum).getReg(),
198  MI->getOperand(OpNum + 1).getImm(), 0, O);
199 }
200 
201 void SystemZInstPrinter::printBDXAddrOperand(const MCInst *MI, int OpNum,
202  raw_ostream &O) {
203  printAddress(&MAI, MI->getOperand(OpNum).getReg(),
204  MI->getOperand(OpNum + 1).getImm(),
205  MI->getOperand(OpNum + 2).getReg(), O);
206 }
207 
208 void SystemZInstPrinter::printBDLAddrOperand(const MCInst *MI, int OpNum,
209  raw_ostream &O) {
210  unsigned Base = MI->getOperand(OpNum).getReg();
211  uint64_t Disp = MI->getOperand(OpNum + 1).getImm();
212  uint64_t Length = MI->getOperand(OpNum + 2).getImm();
213  O << Disp << '(' << Length;
214  if (Base) {
215  O << ",";
216  printRegName(O, Base);
217  }
218  O << ')';
219 }
220 
221 void SystemZInstPrinter::printBDRAddrOperand(const MCInst *MI, int OpNum,
222  raw_ostream &O) {
223  unsigned Base = MI->getOperand(OpNum).getReg();
224  uint64_t Disp = MI->getOperand(OpNum + 1).getImm();
225  unsigned Length = MI->getOperand(OpNum + 2).getReg();
226  O << Disp << "(";
227  printRegName(O, Length);
228  if (Base) {
229  O << ",";
230  printRegName(O, Base);
231  }
232  O << ')';
233 }
234 
235 void SystemZInstPrinter::printBDVAddrOperand(const MCInst *MI, int OpNum,
236  raw_ostream &O) {
237  printAddress(&MAI, MI->getOperand(OpNum).getReg(),
238  MI->getOperand(OpNum + 1).getImm(),
239  MI->getOperand(OpNum + 2).getReg(), O);
240 }
241 
242 void SystemZInstPrinter::printCond4Operand(const MCInst *MI, int OpNum,
243  raw_ostream &O) {
244  static const char *const CondNames[] = {
245  "o", "h", "nle", "l", "nhe", "lh", "ne",
246  "e", "nlh", "he", "nl", "le", "nh", "no"
247  };
248  uint64_t Imm = MI->getOperand(OpNum).getImm();
249  assert(Imm > 0 && Imm < 15 && "Invalid condition");
250  O << CondNames[Imm - 1];
251 }
llvm::MCSymbolRefExpr::getKind
VariantKind getKind() const
Definition: MCExpr.h:399
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:102
MathExtras.h
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
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:70
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
ErrorHandling.h
llvm::SystemZInstPrinter::printAddress
static void printAddress(const MCAsmInfo *MAI, unsigned Base, int64_t Disp, unsigned Index, raw_ostream &O)
Definition: SystemZInstPrinter.cpp:26
llvm::SystemZInstPrinter::getRegisterName
static const char * getRegisterName(unsigned RegNo)
llvm::MCAsmInfo::getAssemblerDialect
unsigned getAssemblerDialect() const
Definition: MCAsmInfo.h:672
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::SystemZInstPrinter::printOperand
static void printOperand(const MCOperand &MO, const MCAsmInfo *MAI, raw_ostream &O)
Definition: SystemZInstPrinter.cpp:43
SystemZInstPrinter.h
printSImmOperand
static void printSImmOperand(const MCInst *MI, int OpNum, raw_ostream &O)
Definition: SystemZInstPrinter.cpp:85
llvm::MCOperand::getImm
int64_t getImm() const
Definition: MCInst.h:80
MCSymbol.h
llvm::MCSymbolRefExpr::VK_TLSLDM
@ VK_TLSLDM
Definition: MCExpr.h:210
MCInst.h
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:397
llvm::MCSymbol::getName
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:197
printUImmOperand
static void printUImmOperand(const MCInst *MI, int OpNum, raw_ostream &O)
Definition: SystemZInstPrinter.cpp:78
llvm::MCInstPrinter::printAnnotation
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
Definition: MCInstPrinter.cpp:49
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:179
llvm::MCOperand::isImm
bool isImm() const
Definition: MCInst.h:62
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
llvm::HighlightColor::Address
@ Address
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:58
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::SystemZInstPrinter::printInstruction
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O)
llvm::AD_HLASM
@ AD_HLASM
Definition: SystemZMCAsmInfo.h:17
Casting.h
llvm::MCOperand::getExpr
const MCExpr * getExpr() const
Definition: MCInst.h:114
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::MCSymbolRefExpr::VK_TLSGD
@ VK_TLSGD
Definition: MCExpr.h:208
llvm::MCOperand::isExpr
bool isExpr() const
Definition: MCInst.h:65
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:42
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::printFormattedRegName
static void printFormattedRegName(const MCAsmInfo *MAI, unsigned RegNo, raw_ostream &O)
Definition: SystemZInstPrinter.cpp:59
llvm::SystemZInstPrinter::printRegName
void printRegName(raw_ostream &O, unsigned RegNo) const override
Print the assembler register name.
Definition: SystemZInstPrinter.h:47
MCExpr.h
llvm::MCSubtargetInfo
Generic base class for all target subtargets.
Definition: MCSubtargetInfo.h:75
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::sampleprof::Base
@ Base
Definition: Discriminator.h:58
llvm::MCOperand::getReg
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:69