LLVM 24.0.0git
SparcInstPrinter.cpp
Go to the documentation of this file.
1//===-- SparcInstPrinter.cpp - Convert Sparc 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// This class prints an Sparc MCInst to a .s file.
10//
11//===----------------------------------------------------------------------===//
12
13#include "SparcInstPrinter.h"
14#include "Sparc.h"
16#include "llvm/MC/MCAsmInfo.h"
17#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCInst.h"
20#include "llvm/MC/MCSymbol.h"
22using namespace llvm;
23
24#define DEBUG_TYPE "asm-printer"
25
26// The generated AsmMatcher SparcGenAsmWriter uses "Sparc" as the target
27// namespace. But SPARC backend uses "SP" as its namespace.
28namespace llvm {
29namespace Sparc {
30 using namespace SP;
31}
32}
33
34#define GET_INSTRUCTION_NAME
35#define PRINT_ALIAS_INSTR
36#include "SparcGenAsmWriter.inc"
37
39 return (STI.hasFeature(Sparc::FeatureV9)) != 0;
40}
41
43 OS << '%' << getRegisterName(Reg);
44}
45
47 unsigned AltIdx) const {
48 OS << '%' << getRegisterName(Reg, AltIdx);
49}
50
52 StringRef Annot, const MCSubtargetInfo &STI,
53 raw_ostream &O) {
54 if (!printAliasInstr(MI, Address, STI, O) &&
55 !printSparcAliasInstr(MI, STI, O))
56 printInstruction(MI, Address, STI, O);
57 printAnnotation(O, Annot);
58}
59
61 const MCSubtargetInfo &STI,
62 raw_ostream &O) {
63 switch (MI->getOpcode()) {
64 default: return false;
65 case SP::JMPLrr:
66 case SP::JMPLri: {
67 if (MI->getNumOperands() != 3)
68 return false;
69 if (!MI->getOperand(0).isReg())
70 return false;
71 switch (MI->getOperand(0).getReg().id()) {
72 default: return false;
73 case SP::G0: // jmp $addr | ret | retl
74 if (MI->getOperand(2).isImm() &&
75 MI->getOperand(2).getImm() == 8) {
76 switch (MI->getOperand(1).getReg().id()) {
77 default: break;
78 case SP::I7: O << "\tret"; return true;
79 case SP::O7: O << "\tretl"; return true;
80 }
81 }
82 O << "\tjmp "; printMemOperand(MI, 1, STI, O);
83 return true;
84 case SP::O7: // call $addr
85 O << "\tcall "; printMemOperand(MI, 1, STI, O);
86 return true;
87 }
88 }
89 case SP::V9FCMPS: case SP::V9FCMPD: case SP::V9FCMPQ:
90 case SP::V9FCMPES: case SP::V9FCMPED: case SP::V9FCMPEQ: {
91 if (isV9(STI)
92 || (MI->getNumOperands() != 3)
93 || (!MI->getOperand(0).isReg())
94 || (MI->getOperand(0).getReg() != SP::FCC0))
95 return false;
96 // if V8, skip printing %fcc0.
97 switch(MI->getOpcode()) {
98 default:
99 case SP::V9FCMPS: O << "\tfcmps "; break;
100 case SP::V9FCMPD: O << "\tfcmpd "; break;
101 case SP::V9FCMPQ: O << "\tfcmpq "; break;
102 case SP::V9FCMPES: O << "\tfcmpes "; break;
103 case SP::V9FCMPED: O << "\tfcmped "; break;
104 case SP::V9FCMPEQ: O << "\tfcmpeq "; break;
105 }
106 printOperand(MI, 1, STI, O);
107 O << ", ";
108 printOperand(MI, 2, STI, O);
109 return true;
110 }
111 }
112}
113
115 const MCSubtargetInfo &STI,
116 raw_ostream &O) {
117 const MCOperand &MO = MI->getOperand (opNum);
118
119 if (MO.isReg()) {
120 MCRegister Reg = MO.getReg();
121 if (isV9(STI))
122 printRegName(O, Reg, SP::RegNamesStateReg);
123 else
124 printRegName(O, Reg);
125 return ;
126 }
127
128 if (MO.isImm()) {
129 markup(O, Markup::Immediate) << formatImm(int32_t(MO.getImm()));
130 return;
131 }
132
133 assert(MO.isExpr() && "Unknown operand kind in printOperand");
134 MAI.printExpr(O, *MO.getExpr());
135}
136
138 const MCSubtargetInfo &STI,
139 raw_ostream &O) {
140 const MCOperand &Op1 = MI->getOperand(opNum);
141 const MCOperand &Op2 = MI->getOperand(opNum + 1);
142
143 bool PrintedFirstOperand = false;
144 if (Op1.isReg() && Op1.getReg() != SP::G0) {
145 printOperand(MI, opNum, STI, O);
146 PrintedFirstOperand = true;
147 }
148
149 // Skip the second operand iff it adds nothing (literal 0 or %g0) and we've
150 // already printed the first one
151 const bool SkipSecondOperand =
152 PrintedFirstOperand && ((Op2.isReg() && Op2.getReg() == SP::G0) ||
153 (Op2.isImm() && Op2.getImm() == 0));
154
155 if (!SkipSecondOperand) {
156 if (PrintedFirstOperand)
157 O << '+';
158 printOperand(MI, opNum + 1, STI, O);
159 }
160}
161
163 const MCSubtargetInfo &STI,
164 raw_ostream &O) {
165 int CC = (int)MI->getOperand(opNum).getImm();
166 switch (MI->getOpcode()) {
167 default: break;
168 case SP::FBCOND:
169 case SP::FBCONDA:
170 case SP::FBCOND_V9:
171 case SP::FBCONDA_V9:
172 case SP::BPFCC:
173 case SP::BPFCCA:
174 case SP::BPFCCNT:
175 case SP::BPFCCANT:
176 case SP::MOVFCCrr: case SP::V9MOVFCCrr:
177 case SP::MOVFCCri: case SP::V9MOVFCCri:
178 case SP::FMOVS_FCC: case SP::V9FMOVS_FCC:
179 case SP::FMOVD_FCC: case SP::V9FMOVD_FCC:
180 case SP::FMOVQ_FCC: case SP::V9FMOVQ_FCC:
181 // Make sure CC is a fp conditional flag.
183 break;
184 case SP::CPBCOND:
185 case SP::CPBCONDA:
186 // Make sure CC is a cp conditional flag.
188 break;
189 case SP::BPR:
190 case SP::BPRA:
191 case SP::BPRNT:
192 case SP::BPRANT:
193 case SP::MOVRri:
194 case SP::MOVRrr:
195 case SP::FMOVRS:
196 case SP::FMOVRD:
197 case SP::FMOVRQ:
198 // Make sure CC is a register conditional flag.
200 break;
201 }
203}
204
205bool SparcInstPrinter::printGetPCX(const MCInst *MI, unsigned opNum,
206 const MCSubtargetInfo &STI,
207 raw_ostream &O) {
208 llvm_unreachable("FIXME: Implement SparcInstPrinter::printGetPCX.");
209 return true;
210}
211
213 const MCSubtargetInfo &STI,
214 raw_ostream &O) {
215 static const char *const TagNames[] = {
216 "#LoadLoad", "#StoreLoad", "#LoadStore", "#StoreStore",
217 "#Lookaside", "#MemIssue", "#Sync"};
218
219 unsigned Imm = MI->getOperand(opNum).getImm();
220
221 if (Imm > 127) {
222 O << Imm;
223 return;
224 }
225
226 ListSeparator LS(" | ");
227 for (unsigned i = 0; i < std::size(TagNames); i++) {
228 if (Imm & (1 << i))
229 O << LS << TagNames[i];
230 }
231}
232
234 const MCSubtargetInfo &STI, raw_ostream &O) {
235 unsigned Imm = MI->getOperand(opNum).getImm();
236 auto ASITag = SparcASITag::lookupASITagByEncoding(Imm);
237 if (isV9(STI) && ASITag)
238 O << '#' << SparcASITag::getASITagStr(ASITag->Name);
239 else
240 O << Imm;
241}
242
244 const MCSubtargetInfo &STI,
245 raw_ostream &O) {
246 unsigned Imm = MI->getOperand(opNum).getImm();
247 auto PrefetchTag = SparcPrefetchTag::lookupPrefetchTagByEncoding(Imm);
248 if (PrefetchTag)
249 O << '#' << SparcPrefetchTag::getPrefetchTagStr(PrefetchTag->Name);
250 else
251 O << Imm;
252}
253
255 unsigned OpNum, const MCSubtargetInfo &STI,
256 raw_ostream &O) {
257 const MCOperand &Op = MI->getOperand(OpNum);
258
259 // If the label has already been resolved to an immediate offset (say, when
260 // we're running the disassembler), just print the immediate.
261 if (Op.isImm()) {
262 int64_t Offset = Op.getImm();
264 uint64_t Target = Address + Offset;
265 if (STI.getTargetTriple().isSPARC32())
266 Target &= 0xffffffff;
267 O << formatHex(Target);
268 } else {
269 O << ".";
270 if (Offset >= 0)
271 O << "+";
272 O << Offset;
273 }
274 return;
275 }
276
277 // Otherwise, just print the expression.
278 MAI.printExpr(O, *Op.getExpr());
279}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t Imm
IRTranslator LLVM IR MI
RISCVCC::CondCode CC
This file contains some functions that are useful when dealing with strings.
A helper class to return the specified delimiter string after the first invocation of operator String...
WithMarkup markup(raw_ostream &OS, Markup M)
format_object< int64_t > formatHex(int64_t Value) const
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.
bool PrintBranchImmAsAddress
If true, a branch immediate (e.g.
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:41
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
const Triple & getTargetTriple() const
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.
bool printAliasInstr(const MCInst *MI, uint64_t Address, const MCSubtargetInfo &STI, raw_ostream &O)
void printOperand(const MCInst *MI, int opNum, const MCSubtargetInfo &STI, raw_ostream &OS)
void printASITag(const MCInst *MI, int opNum, const MCSubtargetInfo &STI, raw_ostream &O)
bool isV9(const MCSubtargetInfo &STI) const
void printMemOperand(const MCInst *MI, int opNum, const MCSubtargetInfo &STI, raw_ostream &OS)
static const char * getRegisterName(MCRegister Reg, unsigned AltIdx=SP::NoRegAltName)
bool printSparcAliasInstr(const MCInst *MI, const MCSubtargetInfo &STI, raw_ostream &OS)
void printCCOperand(const MCInst *MI, int opNum, const MCSubtargetInfo &STI, raw_ostream &OS)
bool printGetPCX(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &OS)
void printPrefetchTag(const MCInst *MI, int opNum, const MCSubtargetInfo &STI, raw_ostream &O)
void printInstruction(const MCInst *MI, uint64_t Address, const MCSubtargetInfo &STI, raw_ostream &O)
void printCTILabel(const MCInst *MI, uint64_t Address, unsigned OpNum, const MCSubtargetInfo &STI, raw_ostream &O)
void printMembarTag(const MCInst *MI, int opNum, const MCSubtargetInfo &STI, raw_ostream &O)
void printRegName(raw_ostream &OS, MCRegister Reg) override
Print the assembler register name.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
bool isSPARC32() const
Tests whether the target is 32-bit SPARC (little and big endian).
Definition Triple.h:1189
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.
@ FCC_BEGIN
Definition Sparc.h:88
@ REG_BEGIN
Definition Sparc.h:124
@ CPCC_BEGIN
Definition Sparc.h:106
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
static const char * SPARCCondCodeToString(SPCC::CondCodes CC)
Definition Sparc.h:134
DWARFExpression::Operation Op