LLVM  15.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"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCCodeEmitter.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"
26 #include "llvm/MC/MCRegisterInfo.h"
28 #include "llvm/MC/MCSymbol.h"
30 #include "llvm/Support/Casting.h"
31 #include "llvm/Support/Endian.h"
35 #include <cassert>
36 #include <cstdint>
37 
38 using namespace llvm;
39 
40 #define DEBUG_TYPE "mccodeemitter"
41 
42 STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
43 
44 namespace {
45 
46 class SparcMCCodeEmitter : public MCCodeEmitter {
47  const MCInstrInfo &MCII;
48  MCContext &Ctx;
49 
50 public:
51  SparcMCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx)
52  : MCII(mcii), Ctx(ctx) {}
53  SparcMCCodeEmitter(const SparcMCCodeEmitter &) = delete;
54  SparcMCCodeEmitter &operator=(const SparcMCCodeEmitter &) = delete;
55  ~SparcMCCodeEmitter() override = default;
56 
57  void encodeInstruction(const MCInst &MI, raw_ostream &OS,
59  const MCSubtargetInfo &STI) const override;
60 
61  // getBinaryCodeForInstr - TableGen'erated function for getting the
62  // binary encoding for an instruction.
63  uint64_t getBinaryCodeForInstr(const MCInst &MI,
65  const MCSubtargetInfo &STI) const;
66 
67  /// getMachineOpValue - Return binary encoding of operand. If the machine
68  /// operand requires relocation, record the relocation and return zero.
69  unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
71  const MCSubtargetInfo &STI) const;
72  unsigned getCallTargetOpValue(const MCInst &MI, unsigned OpNo,
74  const MCSubtargetInfo &STI) const;
75  unsigned getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
77  const MCSubtargetInfo &STI) const;
78  unsigned getSImm13OpValue(const MCInst &MI, unsigned OpNo,
80  const MCSubtargetInfo &STI) const;
81  unsigned getBranchPredTargetOpValue(const MCInst &MI, unsigned OpNo,
83  const MCSubtargetInfo &STI) const;
84  unsigned getBranchOnRegTargetOpValue(const MCInst &MI, unsigned OpNo,
86  const MCSubtargetInfo &STI) const;
87 
88 private:
89  FeatureBitset computeAvailableFeatures(const FeatureBitset &FB) const;
90  void
91  verifyInstructionPredicates(const MCInst &MI,
92  const FeatureBitset &AvailableFeatures) const;
93 };
94 
95 } // end anonymous namespace
96 
97 void SparcMCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS,
99  const MCSubtargetInfo &STI) const {
100  verifyInstructionPredicates(MI,
101  computeAvailableFeatures(STI.getFeatureBits()));
102 
103  unsigned Bits = getBinaryCodeForInstr(MI, Fixups, STI);
105  Ctx.getAsmInfo()->isLittleEndian() ? support::little
106  : support::big);
107 
108  // Some instructions have phantom operands that only contribute a fixup entry.
109  unsigned SymOpNo = 0;
110  switch (MI.getOpcode()) {
111  default: break;
112  case SP::TLS_CALL: SymOpNo = 1; break;
113  case SP::GDOP_LDrr:
114  case SP::GDOP_LDXrr:
115  case SP::TLS_ADDrr:
116  case SP::TLS_ADDXrr:
117  case SP::TLS_LDrr:
118  case SP::TLS_LDXrr: SymOpNo = 3; break;
119  }
120  if (SymOpNo != 0) {
121  const MCOperand &MO = MI.getOperand(SymOpNo);
122  uint64_t op = getMachineOpValue(MI, MO, Fixups, STI);
123  assert(op == 0 && "Unexpected operand value!");
124  (void)op; // suppress warning.
125  }
126 
127  ++MCNumEmitted; // Keep track of the # of mi's emitted.
128 }
129 
130 unsigned SparcMCCodeEmitter::
131 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
133  const MCSubtargetInfo &STI) const {
134  if (MO.isReg())
135  return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
136 
137  if (MO.isImm())
138  return MO.getImm();
139 
140  assert(MO.isExpr());
141  const MCExpr *Expr = MO.getExpr();
142  if (const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(Expr)) {
143  MCFixupKind Kind = (MCFixupKind)SExpr->getFixupKind();
144  Fixups.push_back(MCFixup::create(0, Expr, Kind));
145  return 0;
146  }
147 
148  int64_t Res;
149  if (Expr->evaluateAsAbsolute(Res))
150  return Res;
151 
152  llvm_unreachable("Unhandled expression!");
153  return 0;
154 }
155 
156 unsigned
157 SparcMCCodeEmitter::getSImm13OpValue(const MCInst &MI, unsigned OpNo,
159  const MCSubtargetInfo &STI) const {
160  const MCOperand &MO = MI.getOperand(OpNo);
161 
162  if (MO.isImm())
163  return MO.getImm();
164 
165  assert(MO.isExpr() &&
166  "getSImm13OpValue expects only expressions or an immediate");
167 
168  const MCExpr *Expr = MO.getExpr();
169 
170  // Constant value, no fixup is needed
171  if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
172  return CE->getValue();
173 
175  if (const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(Expr)) {
176  Kind = MCFixupKind(SExpr->getFixupKind());
177  } else {
178  bool IsPic = Ctx.getObjectFileInfo()->isPositionIndependent();
180  : MCFixupKind(Sparc::fixup_sparc_13);
181  }
182 
183  Fixups.push_back(MCFixup::create(0, Expr, Kind));
184  return 0;
185 }
186 
187 unsigned SparcMCCodeEmitter::
188 getCallTargetOpValue(const MCInst &MI, unsigned OpNo,
190  const MCSubtargetInfo &STI) const {
191  const MCOperand &MO = MI.getOperand(OpNo);
192  const MCExpr *Expr = MO.getExpr();
193  const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(Expr);
194 
195  if (MI.getOpcode() == SP::TLS_CALL) {
196  // No fixups for __tls_get_addr. Will emit for fixups for tls_symbol in
197  // encodeInstruction.
198 #ifndef NDEBUG
199  // Verify that the callee is actually __tls_get_addr.
200  assert(SExpr && SExpr->getSubExpr()->getKind() == MCExpr::SymbolRef &&
201  "Unexpected expression in TLS_CALL");
202  const MCSymbolRefExpr *SymExpr = cast<MCSymbolRefExpr>(SExpr->getSubExpr());
203  assert(SymExpr->getSymbol().getName() == "__tls_get_addr" &&
204  "Unexpected function for TLS_CALL");
205 #endif
206  return 0;
207  }
208 
210  Fixups.push_back(MCFixup::create(0, Expr, Kind));
211  return 0;
212 }
213 
214 unsigned SparcMCCodeEmitter::
215 getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
217  const MCSubtargetInfo &STI) const {
218  const MCOperand &MO = MI.getOperand(OpNo);
219  if (MO.isReg() || MO.isImm())
220  return getMachineOpValue(MI, MO, Fixups, STI);
221 
222  Fixups.push_back(MCFixup::create(0, MO.getExpr(),
224  return 0;
225 }
226 
227 unsigned SparcMCCodeEmitter::
228 getBranchPredTargetOpValue(const MCInst &MI, unsigned OpNo,
230  const MCSubtargetInfo &STI) const {
231  const MCOperand &MO = MI.getOperand(OpNo);
232  if (MO.isReg() || MO.isImm())
233  return getMachineOpValue(MI, MO, Fixups, STI);
234 
235  Fixups.push_back(MCFixup::create(0, MO.getExpr(),
237  return 0;
238 }
239 
240 unsigned SparcMCCodeEmitter::
241 getBranchOnRegTargetOpValue(const MCInst &MI, unsigned OpNo,
243  const MCSubtargetInfo &STI) const {
244  const MCOperand &MO = MI.getOperand(OpNo);
245  if (MO.isReg() || MO.isImm())
246  return getMachineOpValue(MI, MO, Fixups, STI);
247 
248  Fixups.push_back(MCFixup::create(0, MO.getExpr(),
250  Fixups.push_back(MCFixup::create(0, MO.getExpr(),
252 
253  return 0;
254 }
255 
256 #define ENABLE_INSTR_PREDICATE_VERIFIER
257 #include "SparcGenMCCodeEmitter.inc"
258 
260  MCContext &Ctx) {
261  return new SparcMCCodeEmitter(MCII, Ctx);
262 }
llvm::Sparc::fixup_sparc_br22
@ fixup_sparc_br22
fixup_sparc_br22 - 22-bit PC relative relocation for branches
Definition: SparcFixupKinds.h:22
llvm::Sparc::fixup_sparc_br19
@ fixup_sparc_br19
fixup_sparc_br19 - 19-bit PC relative relocation for branches on icc/xcc
Definition: SparcFixupKinds.h:26
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:104
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::Sparc::fixup_sparc_br16_2
@ fixup_sparc_br16_2
fixup_sparc_bpr - 16-bit fixup for bpr
Definition: SparcFixupKinds.h:29
llvm::MCOperand::isReg
bool isReg() const
Definition: MCInst.h:61
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:76
SparcMCTargetDesc.h
MCCodeEmitter.h
llvm::createSparcMCCodeEmitter
MCCodeEmitter * createSparcMCCodeEmitter(const MCInstrInfo &MCII, MCContext &Ctx)
Definition: SparcMCCodeEmitter.cpp:259
op
#define op(i)
Statistic.h
llvm::MCFixup::create
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, SMLoc Loc=SMLoc())
Definition: MCFixup.h:87
ErrorHandling.h
MCObjectFileInfo.h
llvm::tgtok::Bits
@ Bits
Definition: TGLexer.h:50
llvm::FeatureBitset
Container class for subtarget features.
Definition: SubtargetFeature.h:40
llvm::SparcMCExpr
Definition: SparcMCExpr.h:23
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::Sparc::fixup_sparc_br16_14
@ fixup_sparc_br16_14
Definition: SparcFixupKinds.h:30
SubtargetFeature.h
SparcFixupKinds.h
llvm::support::little
@ little
Definition: Endian.h:27
MCContext.h
MCInstrInfo.h
llvm::MCOperand::getImm
int64_t getImm() const
Definition: MCInst.h:80
MCSymbol.h
SparcMCExpr.h
MCInst.h
llvm::AArch64::Fixups
Fixups
Definition: AArch64FixupKinds.h:17
MCSubtargetInfo.h
llvm::MCSubtargetInfo::getFeatureBits
const FeatureBitset & getFeatureBits() const
Definition: MCSubtargetInfo.h:112
getBranchTargetOpValue
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...
Definition: ARMMCCodeEmitter.cpp:620
llvm::STATISTIC
STATISTIC(NumFunctions, "Total number of functions")
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
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::MCExpr::getKind
ExprKind getKind() const
Definition: MCExpr.h:81
llvm::Sparc::fixup_sparc_13
@ fixup_sparc_13
fixup_sparc_13 - 13-bit fixup
Definition: SparcFixupKinds.h:33
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::support::endian::write
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition: Endian.h:97
llvm::MCConstantExpr
Definition: MCExpr.h:144
llvm::MCOperand::isImm
bool isImm() const
Definition: MCInst.h:62
uint64_t
MCRegisterInfo.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::WinEH::EncodingType::CE
@ CE
Windows NT (Windows on ARM)
llvm::MCSymbolRefExpr
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
llvm::Sparc::fixup_sparc_got13
@ fixup_sparc_got13
fixup_sparc_got13 - 13-bit fixup corresponding to got13(foo)
Definition: SparcFixupKinds.h:73
MCAsmInfo.h
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
MCFixup.h
llvm::SparcMCExpr::getSubExpr
const MCExpr * getSubExpr() const
getSubExpr - Get the child of this expression.
Definition: SparcMCExpr.h:90
llvm::SparcMCExpr::getFixupKind
Sparc::Fixups getFixupKind() const
getFixupKind - Get the fixup kind of this expression.
Definition: SparcMCExpr.h:93
llvm::MCInstrInfo
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:26
EndianStream.h
llvm::MCCodeEmitter
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:21
Casting.h
llvm::MCOperand::getExpr
const MCExpr * getExpr() const
Definition: MCInst.h:114
llvm::MCOperand::isExpr
bool isExpr() const
Definition: MCInst.h:65
llvm::MCFixupKind
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:21
SmallVector.h
llvm::MCExpr::SymbolRef
@ SymbolRef
References to labels and assigned expressions.
Definition: MCExpr.h:40
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
llvm::MCOperand
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
raw_ostream.h
llvm::SPISD::TLS_CALL
@ TLS_CALL
Definition: SparcISelLowering.h:51
Endian.h
MCExpr.h
llvm::MCSubtargetInfo
Generic base class for all target subtargets.
Definition: MCSubtargetInfo.h:76
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
llvm::support::big
@ big
Definition: Endian.h:27
llvm::MCOperand::getReg
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:69