LLVM 24.0.0git
SparcMCCodeEmitter.cpp
Go to the documentation of this file.
1//===-- SparcMCCodeEmitter.cpp - Convert Sparc code to machine code -------===//
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 file implements the SparcMCCodeEmitter class.
10//
11//===----------------------------------------------------------------------===//
12
14#include "SparcMCTargetDesc.h"
16#include "llvm/ADT/Statistic.h"
18#include "llvm/MC/MCAsmInfo.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCFixup.h"
23#include "llvm/MC/MCInst.h"
24#include "llvm/MC/MCInstrInfo.h"
28#include "llvm/MC/MCSymbol.h"
31#include <cassert>
32#include <cstdint>
33
34using namespace llvm;
35
36#define DEBUG_TYPE "mccodeemitter"
37
38STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
39
40namespace {
41
42class SparcMCCodeEmitter : public MCCodeEmitter {
43 MCContext &Ctx;
44
45public:
46 SparcMCCodeEmitter(const MCInstrInfo &, MCContext &ctx)
47 : Ctx(ctx) {}
48 SparcMCCodeEmitter(const SparcMCCodeEmitter &) = delete;
49 SparcMCCodeEmitter &operator=(const SparcMCCodeEmitter &) = delete;
50 ~SparcMCCodeEmitter() override = default;
51
52 void encodeInstruction(const MCInst &MI, SmallVectorImpl<char> &CB,
53 SmallVectorImpl<MCFixup> &Fixups,
54 const MCSubtargetInfo &STI) const override;
55
56 // getBinaryCodeForInstr - TableGen'erated function for getting the
57 // binary encoding for an instruction.
58 uint64_t getBinaryCodeForInstr(const MCInst &MI,
59 SmallVectorImpl<MCFixup> &Fixups,
60 const MCSubtargetInfo &STI) const;
61
62 /// getMachineOpValue - Return binary encoding of operand. If the machine
63 /// operand requires relocation, record the relocation and return zero.
64 unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
65 SmallVectorImpl<MCFixup> &Fixups,
66 const MCSubtargetInfo &STI) const;
67 unsigned getCallTargetOpValue(const MCInst &MI, unsigned OpNo,
68 SmallVectorImpl<MCFixup> &Fixups,
69 const MCSubtargetInfo &STI) const;
70 unsigned getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
71 SmallVectorImpl<MCFixup> &Fixups,
72 const MCSubtargetInfo &STI) const;
73 unsigned getSImm5OpValue(const MCInst &MI, unsigned OpNo,
74 SmallVectorImpl<MCFixup> &Fixups,
75 const MCSubtargetInfo &STI) const;
76 unsigned getSImm10OpValue(const MCInst &MI, unsigned OpNo,
77 SmallVectorImpl<MCFixup> &Fixups,
78 const MCSubtargetInfo &STI) const;
79 unsigned getSImm11OpValue(const MCInst &MI, unsigned OpNo,
80 SmallVectorImpl<MCFixup> &Fixups,
81 const MCSubtargetInfo &STI) const;
82 unsigned getSImm13OpValue(const MCInst &MI, unsigned OpNo,
83 SmallVectorImpl<MCFixup> &Fixups,
84 const MCSubtargetInfo &STI) const;
85 unsigned getImm22OpValue(const MCInst &MI, unsigned OpNo,
86 SmallVectorImpl<MCFixup> &Fixups,
87 const MCSubtargetInfo &STI) const;
88 unsigned getTrapNumOpValue(const MCInst &MI, unsigned OpNo,
89 SmallVectorImpl<MCFixup> &Fixups,
90 const MCSubtargetInfo &STI) const;
91 unsigned getBranchPredTargetOpValue(const MCInst &MI, unsigned OpNo,
92 SmallVectorImpl<MCFixup> &Fixups,
93 const MCSubtargetInfo &STI) const;
94 unsigned getBranchOnRegTargetOpValue(const MCInst &MI, unsigned OpNo,
95 SmallVectorImpl<MCFixup> &Fixups,
96 const MCSubtargetInfo &STI) const;
97 unsigned getCompareAndBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
98 SmallVectorImpl<MCFixup> &Fixups,
99 const MCSubtargetInfo &STI) const;
100};
101
102} // end anonymous namespace
103
105 const MCExpr *Value, uint16_t Kind) {
106 bool PCRel = false;
107 switch (Kind) {
108 case ELF::R_SPARC_PC10:
109 case ELF::R_SPARC_PC22:
110 case ELF::R_SPARC_WDISP10:
111 case ELF::R_SPARC_WDISP16:
112 case ELF::R_SPARC_WDISP19:
113 case ELF::R_SPARC_WDISP22:
115 PCRel = true;
116 }
117 Fixups.push_back(MCFixup::create(Offset, Value, Kind, PCRel));
118}
119
120void SparcMCCodeEmitter::encodeInstruction(const MCInst &MI,
123 const MCSubtargetInfo &STI) const {
124 unsigned Bits = getBinaryCodeForInstr(MI, Fixups, STI);
125 support::endian::write(CB, Bits,
129
130 // Some instructions have phantom operands that only contribute a fixup entry.
131 unsigned SymOpNo = 0;
132 switch (MI.getOpcode()) {
133 default: break;
134 case SP::TLS_CALL: SymOpNo = 1; break;
135 case SP::GDOP_LDrr:
136 case SP::GDOP_LDXrr:
137 case SP::TLS_ADDrr:
138 case SP::TLS_LDrr:
139 case SP::TLS_LDXrr: SymOpNo = 3; break;
140 }
141 if (SymOpNo != 0) {
142 const MCOperand &MO = MI.getOperand(SymOpNo);
143 uint64_t op = getMachineOpValue(MI, MO, Fixups, STI);
144 assert(op == 0 && "Unexpected operand value!");
145 (void)op; // suppress warning.
146 }
147
148 ++MCNumEmitted; // Keep track of the # of mi's emitted.
149}
150
151unsigned SparcMCCodeEmitter::
152getMachineOpValue(const MCInst &MI, const MCOperand &MO,
153 SmallVectorImpl<MCFixup> &Fixups,
154 const MCSubtargetInfo &STI) const {
155 if (MO.isReg())
156 return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
157
158 if (MO.isImm())
159 return MO.getImm();
160
161 assert(MO.isExpr());
162 const MCExpr *Expr = MO.getExpr();
163 if (auto *SExpr = dyn_cast<MCSpecifierExpr>(Expr)) {
164 addFixup(Fixups, 0, Expr, SExpr->getSpecifier());
165 return 0;
166 }
167
168 int64_t Res;
169 if (Expr->evaluateAsAbsolute(Res))
170 return Res;
171
172 llvm_unreachable("Unhandled expression!");
173 return 0;
174}
175
176// Encode an immediate operand. A symbolic operand gets a fixup of the given
177// kind, or of its relocation specifier if it has one.
178static unsigned getImmOpValue(const MCOperand &MO,
179 SmallVectorImpl<MCFixup> &Fixups, uint16_t Kind) {
180 if (MO.isImm())
181 return MO.getImm();
182
183 assert(MO.isExpr() && "expected an immediate or an expression");
184 const MCExpr *Expr = MO.getExpr();
185
186 // Constant value, no fixup is needed
187 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
188 return CE->getValue();
189
190 if (auto *SExpr = dyn_cast<MCSpecifierExpr>(Expr)) {
191 addFixup(Fixups, 0, Expr, SExpr->getSpecifier());
192 return 0;
193 }
194 addFixup(Fixups, 0, Expr, Kind);
195 return 0;
196}
197
198unsigned SparcMCCodeEmitter::getSImm5OpValue(const MCInst &MI, unsigned OpNo,
199 SmallVectorImpl<MCFixup> &Fixups,
200 const MCSubtargetInfo &STI) const {
201 return getImmOpValue(MI.getOperand(OpNo), Fixups, ELF::R_SPARC_5);
202}
203
204unsigned
205SparcMCCodeEmitter::getSImm10OpValue(const MCInst &MI, unsigned OpNo,
206 SmallVectorImpl<MCFixup> &Fixups,
207 const MCSubtargetInfo &STI) const {
208 return getImmOpValue(MI.getOperand(OpNo), Fixups, ELF::R_SPARC_10);
209}
210
211unsigned
212SparcMCCodeEmitter::getSImm11OpValue(const MCInst &MI, unsigned OpNo,
213 SmallVectorImpl<MCFixup> &Fixups,
214 const MCSubtargetInfo &STI) const {
215 return getImmOpValue(MI.getOperand(OpNo), Fixups, ELF::R_SPARC_11);
216}
217
218unsigned
219SparcMCCodeEmitter::getSImm13OpValue(const MCInst &MI, unsigned OpNo,
220 SmallVectorImpl<MCFixup> &Fixups,
221 const MCSubtargetInfo &STI) const {
222 return getImmOpValue(MI.getOperand(OpNo), Fixups, Sparc::fixup_sparc_13);
223}
224
225unsigned SparcMCCodeEmitter::getImm22OpValue(const MCInst &MI, unsigned OpNo,
226 SmallVectorImpl<MCFixup> &Fixups,
227 const MCSubtargetInfo &STI) const {
228 return getImmOpValue(MI.getOperand(OpNo), Fixups, ELF::R_SPARC_22);
229}
230
231// GNU as uses R_SPARC_13 for a symbolic trap number, but on V9 bits 12-11 of
232// that field are the condition code. R_SPARC_7 covers just the trap number.
233unsigned
234SparcMCCodeEmitter::getTrapNumOpValue(const MCInst &MI, unsigned OpNo,
235 SmallVectorImpl<MCFixup> &Fixups,
236 const MCSubtargetInfo &STI) const {
237 return getImmOpValue(MI.getOperand(OpNo), Fixups, ELF::R_SPARC_7);
238}
239
240unsigned SparcMCCodeEmitter::
241getCallTargetOpValue(const MCInst &MI, unsigned OpNo,
242 SmallVectorImpl<MCFixup> &Fixups,
243 const MCSubtargetInfo &STI) const {
244 if (MI.getOpcode() == SP::TLS_CALL) {
245 // No fixups for __tls_get_addr. Will emit for fixups for tls_symbol in
246 // encodeInstruction.
247 return 0;
248 }
249
250 const MCOperand &MO = MI.getOperand(OpNo);
252 return 0;
253}
254
255unsigned SparcMCCodeEmitter::
256getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
257 SmallVectorImpl<MCFixup> &Fixups,
258 const MCSubtargetInfo &STI) const {
259 const MCOperand &MO = MI.getOperand(OpNo);
260 if (MO.isReg() || MO.isImm())
261 return getMachineOpValue(MI, MO, Fixups, STI);
262
263 addFixup(Fixups, 0, MO.getExpr(), ELF::R_SPARC_WDISP22);
264 return 0;
265}
266
267unsigned SparcMCCodeEmitter::getBranchPredTargetOpValue(
268 const MCInst &MI, unsigned OpNo, SmallVectorImpl<MCFixup> &Fixups,
269 const MCSubtargetInfo &STI) const {
270 const MCOperand &MO = MI.getOperand(OpNo);
271 if (MO.isReg() || MO.isImm())
272 return getMachineOpValue(MI, MO, Fixups, STI);
273
274 addFixup(Fixups, 0, MO.getExpr(), ELF::R_SPARC_WDISP19);
275 return 0;
276}
277
278unsigned SparcMCCodeEmitter::getBranchOnRegTargetOpValue(
279 const MCInst &MI, unsigned OpNo, SmallVectorImpl<MCFixup> &Fixups,
280 const MCSubtargetInfo &STI) const {
281 const MCOperand &MO = MI.getOperand(OpNo);
282 if (MO.isReg() || MO.isImm())
283 return getMachineOpValue(MI, MO, Fixups, STI);
284
285 addFixup(Fixups, 0, MO.getExpr(), ELF::R_SPARC_WDISP16);
286 return 0;
287}
288
289unsigned SparcMCCodeEmitter::getCompareAndBranchTargetOpValue(
290 const MCInst &MI, unsigned OpNo, SmallVectorImpl<MCFixup> &Fixups,
291 const MCSubtargetInfo &STI) const {
292 const MCOperand &MO = MI.getOperand(OpNo);
293 if (MO.isImm())
294 return getMachineOpValue(MI, MO, Fixups, STI);
295
296 addFixup(Fixups, 0, MO.getExpr(), ELF::R_SPARC_WDISP10);
297 return 0;
298}
299
300#include "SparcGenMCCodeEmitter.inc"
301
303 MCContext &Ctx) {
304 return new SparcMCCodeEmitter(MCII, Ctx);
305}
static void addFixup(SmallVectorImpl< MCFixup > &Fixups, uint32_t Offset, const MCExpr *Value, uint16_t Kind, bool PCRel=false)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx, unsigned FixupKind, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI)
getBranchTargetOpValue - Helper function to get the branch target operand, which is either an immedia...
#define op(i)
IRTranslator LLVM IR MI
This file defines the SmallVector class.
static unsigned getImmOpValue(const MCOperand &MO, SmallVectorImpl< MCFixup > &Fixups, uint16_t Kind)
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:170
bool isLittleEndian() const
True if the target is little endian.
Definition MCAsmInfo.h:501
MCCodeEmitter - Generic instruction encoding interface.
Context object for machine code objects.
Definition MCContext.h:84
const MCRegisterInfo * getRegisterInfo() const
Definition MCContext.h:411
const MCAsmInfo & getAsmInfo() const
Definition MCContext.h:409
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:31
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, bool PCRel=false)
Consider bit fields if we need more flags.
Definition MCFixup.h:90
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:187
Interface to description of machine instruction set.
Definition MCInstrInfo.h:27
Instances of this class represent operands of the MCInst class.
Definition MCInst.h:39
int64_t getImm() const
Definition MCInst.h:83
bool isImm() const
Definition MCInst.h:65
bool isReg() const
Definition MCInst.h:64
MCRegister getReg() const
Returns the register number.
Definition MCInst.h:72
const MCExpr * getExpr() const
Definition MCInst.h:117
bool isExpr() const
Definition MCInst.h:68
uint16_t getEncodingValue(MCRegister Reg) const
Returns the encoding for Reg.
Generic base class for all target subtargets.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
LLVM Value Representation.
Definition Value.h:75
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ fixup_sparc_13
fixup_sparc_13 - 13-bit fixup
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition Endian.h:82
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
static void addFixup(SmallVectorImpl< MCFixup > &Fixups, uint32_t Offset, const MCExpr *Value, uint16_t Kind)
MCCodeEmitter * createSparcMCCodeEmitter(const MCInstrInfo &MCII, MCContext &Ctx)