LLVM 20.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 "SparcMCExpr.h"
15#include "SparcMCTargetDesc.h"
17#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"
30#include "llvm/Support/Endian.h"
35#include <cassert>
36#include <cstdint>
37
38using namespace llvm;
39
40#define DEBUG_TYPE "mccodeemitter"
41
42STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
43
44namespace {
45
46class SparcMCCodeEmitter : public MCCodeEmitter {
47 MCContext &Ctx;
48
49public:
50 SparcMCCodeEmitter(const MCInstrInfo &, MCContext &ctx)
51 : Ctx(ctx) {}
52 SparcMCCodeEmitter(const SparcMCCodeEmitter &) = delete;
53 SparcMCCodeEmitter &operator=(const SparcMCCodeEmitter &) = delete;
54 ~SparcMCCodeEmitter() override = default;
55
58 const MCSubtargetInfo &STI) const override;
59
60 // getBinaryCodeForInstr - TableGen'erated function for getting the
61 // binary encoding for an instruction.
62 uint64_t getBinaryCodeForInstr(const MCInst &MI,
64 const MCSubtargetInfo &STI) const;
65
66 /// getMachineOpValue - Return binary encoding of operand. If the machine
67 /// operand requires relocation, record the relocation and return zero.
68 unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
70 const MCSubtargetInfo &STI) const;
71 unsigned getCallTargetOpValue(const MCInst &MI, unsigned OpNo,
73 const MCSubtargetInfo &STI) const;
74 unsigned getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
76 const MCSubtargetInfo &STI) const;
77 unsigned getSImm13OpValue(const MCInst &MI, unsigned OpNo,
79 const MCSubtargetInfo &STI) const;
80 unsigned getBranchPredTargetOpValue(const MCInst &MI, unsigned OpNo,
82 const MCSubtargetInfo &STI) const;
83 unsigned getBranchOnRegTargetOpValue(const MCInst &MI, unsigned OpNo,
85 const MCSubtargetInfo &STI) const;
86};
87
88} // end anonymous namespace
89
90void SparcMCCodeEmitter::encodeInstruction(const MCInst &MI,
93 const MCSubtargetInfo &STI) const {
94 unsigned Bits = getBinaryCodeForInstr(MI, Fixups, STI);
96 Ctx.getAsmInfo()->isLittleEndian()
99
100 // Some instructions have phantom operands that only contribute a fixup entry.
101 unsigned SymOpNo = 0;
102 switch (MI.getOpcode()) {
103 default: break;
104 case SP::TLS_CALL: SymOpNo = 1; break;
105 case SP::GDOP_LDrr:
106 case SP::GDOP_LDXrr:
107 case SP::TLS_ADDrr:
108 case SP::TLS_LDrr:
109 case SP::TLS_LDXrr: SymOpNo = 3; break;
110 }
111 if (SymOpNo != 0) {
112 const MCOperand &MO = MI.getOperand(SymOpNo);
113 uint64_t op = getMachineOpValue(MI, MO, Fixups, STI);
114 assert(op == 0 && "Unexpected operand value!");
115 (void)op; // suppress warning.
116 }
117
118 ++MCNumEmitted; // Keep track of the # of mi's emitted.
119}
120
121unsigned SparcMCCodeEmitter::
122getMachineOpValue(const MCInst &MI, const MCOperand &MO,
124 const MCSubtargetInfo &STI) const {
125 if (MO.isReg())
126 return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
127
128 if (MO.isImm())
129 return MO.getImm();
130
131 assert(MO.isExpr());
132 const MCExpr *Expr = MO.getExpr();
133 if (const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(Expr)) {
134 MCFixupKind Kind = (MCFixupKind)SExpr->getFixupKind();
135 Fixups.push_back(MCFixup::create(0, Expr, Kind));
136 return 0;
137 }
138
139 int64_t Res;
140 if (Expr->evaluateAsAbsolute(Res))
141 return Res;
142
143 llvm_unreachable("Unhandled expression!");
144 return 0;
145}
146
147unsigned
148SparcMCCodeEmitter::getSImm13OpValue(const MCInst &MI, unsigned OpNo,
150 const MCSubtargetInfo &STI) const {
151 const MCOperand &MO = MI.getOperand(OpNo);
152
153 if (MO.isImm())
154 return MO.getImm();
155
156 assert(MO.isExpr() &&
157 "getSImm13OpValue expects only expressions or an immediate");
158
159 const MCExpr *Expr = MO.getExpr();
160
161 // Constant value, no fixup is needed
162 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
163 return CE->getValue();
164
166 if (const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(Expr)) {
167 Kind = MCFixupKind(SExpr->getFixupKind());
168 } else {
169 bool IsPic = Ctx.getObjectFileInfo()->isPositionIndependent();
171 : MCFixupKind(Sparc::fixup_sparc_13);
172 }
173
174 Fixups.push_back(MCFixup::create(0, Expr, Kind));
175 return 0;
176}
177
178unsigned SparcMCCodeEmitter::
179getCallTargetOpValue(const MCInst &MI, unsigned OpNo,
181 const MCSubtargetInfo &STI) const {
182 const MCOperand &MO = MI.getOperand(OpNo);
183 const MCExpr *Expr = MO.getExpr();
184 const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(Expr);
185
186 if (MI.getOpcode() == SP::TLS_CALL) {
187 // No fixups for __tls_get_addr. Will emit for fixups for tls_symbol in
188 // encodeInstruction.
189#ifndef NDEBUG
190 // Verify that the callee is actually __tls_get_addr.
191 assert(SExpr && SExpr->getSubExpr()->getKind() == MCExpr::SymbolRef &&
192 "Unexpected expression in TLS_CALL");
193 const MCSymbolRefExpr *SymExpr = cast<MCSymbolRefExpr>(SExpr->getSubExpr());
194 assert(SymExpr->getSymbol().getName() == "__tls_get_addr" &&
195 "Unexpected function for TLS_CALL");
196#endif
197 return 0;
198 }
199
201 Fixups.push_back(MCFixup::create(0, Expr, Kind));
202 return 0;
203}
204
205unsigned SparcMCCodeEmitter::
206getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
208 const MCSubtargetInfo &STI) const {
209 const MCOperand &MO = MI.getOperand(OpNo);
210 if (MO.isReg() || MO.isImm())
211 return getMachineOpValue(MI, MO, Fixups, STI);
212
213 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
215 return 0;
216}
217
218unsigned SparcMCCodeEmitter::
219getBranchPredTargetOpValue(const MCInst &MI, unsigned OpNo,
221 const MCSubtargetInfo &STI) const {
222 const MCOperand &MO = MI.getOperand(OpNo);
223 if (MO.isReg() || MO.isImm())
224 return getMachineOpValue(MI, MO, Fixups, STI);
225
226 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
228 return 0;
229}
230
231unsigned SparcMCCodeEmitter::
232getBranchOnRegTargetOpValue(const MCInst &MI, unsigned OpNo,
234 const MCSubtargetInfo &STI) const {
235 const MCOperand &MO = MI.getOperand(OpNo);
236 if (MO.isReg() || MO.isImm())
237 return getMachineOpValue(MI, MO, Fixups, STI);
238
239 Fixups.push_back(
241
242 return 0;
243}
244
245#include "SparcGenMCCodeEmitter.inc"
246
248 MCContext &Ctx) {
249 return new SparcMCCodeEmitter(MCII, Ctx);
250}
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
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
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:166
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:21
virtual void encodeInstruction(const MCInst &Inst, SmallVectorImpl< char > &CB, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const =0
Encode the given Inst to bytes and append to CB.
MCCodeEmitter & operator=(const MCCodeEmitter &)=delete
Context object for machine code objects.
Definition: MCContext.h:83
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
@ SymbolRef
References to labels and assigned expressions.
Definition: MCExpr.h:39
ExprKind getKind() const
Definition: MCExpr.h:78
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, SMLoc Loc=SMLoc())
Definition: MCFixup.h:87
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:26
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
int64_t getImm() const
Definition: MCInst.h:80
bool isImm() const
Definition: MCInst.h:62
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:69
bool isReg() const
Definition: MCInst.h:61
const MCExpr * getExpr() const
Definition: MCInst.h:114
bool isExpr() const
Definition: MCInst.h:65
Generic base class for all target subtargets.
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:188
const MCSymbol & getSymbol() const
Definition: MCExpr.h:406
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
const MCExpr * getSubExpr() const
getSubExpr - Get the child of this expression.
Definition: SparcMCExpr.h:90
Sparc::Fixups getFixupKind() const
getFixupKind - Get the fixup kind of this expression.
Definition: SparcMCExpr.h:93
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ fixup_sparc_br16
fixup_sparc_bpr - 16-bit fixup for bpr
@ fixup_sparc_got13
fixup_sparc_got13 - 13-bit fixup corresponding to got13(foo)
@ fixup_sparc_br19
fixup_sparc_br19 - 19-bit PC relative relocation for branches on icc/xcc
@ fixup_sparc_13
fixup_sparc_13 - 13-bit fixup
@ fixup_sparc_br22
fixup_sparc_br22 - 22-bit PC relative relocation for branches
@ CE
Windows NT (Windows on ARM)
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition: Endian.h:92
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:21
MCCodeEmitter * createSparcMCCodeEmitter(const MCInstrInfo &MCII, MCContext &Ctx)