LLVM  7.0.0
SparcAsmBackend.cpp
Go to the documentation of this file.
1 //===-- SparcAsmBackend.cpp - Sparc Assembler Backend ---------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
12 #include "llvm/MC/MCAsmBackend.h"
14 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCValue.h"
20 
21 using namespace llvm;
22 
23 static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
24  switch (Kind) {
25  default:
26  llvm_unreachable("Unknown fixup kind!");
27  case FK_Data_1:
28  case FK_Data_2:
29  case FK_Data_4:
30  case FK_Data_8:
31  return Value;
32 
35  return (Value >> 2) & 0x3fffffff;
36 
38  return (Value >> 2) & 0x3fffff;
39 
41  return (Value >> 2) & 0x7ffff;
42 
44  return (Value >> 2) & 0xc000;
45 
47  return (Value >> 2) & 0x3fff;
48 
55  return (Value >> 10) & 0x3fffff;
56 
59  return Value & 0x1fff;
60 
67  return Value & 0x3ff;
68 
70  return (Value >> 22) & 0x3fffff;
71 
73  return (Value >> 12) & 0x3ff;
74 
76  return Value & 0xfff;
77 
79  return (Value >> 42) & 0x3fffff;
80 
82  return (Value >> 32) & 0x3ff;
83 
88  assert(Value == 0 && "Sparc TLS relocs expect zero Value");
89  return 0;
90 
99  return 0;
100  }
101 }
102 
103 namespace {
104  class SparcAsmBackend : public MCAsmBackend {
105  protected:
106  const Target &TheTarget;
107  bool Is64Bit;
108 
109  public:
110  SparcAsmBackend(const Target &T)
111  : MCAsmBackend(StringRef(T.getName()) == "sparcel" ? support::little
112  : support::big),
113  TheTarget(T), Is64Bit(StringRef(TheTarget.getName()) == "sparcv9") {}
114 
115  unsigned getNumFixupKinds() const override {
117  }
118 
119  const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
120  const static MCFixupKindInfo InfosBE[Sparc::NumTargetFixupKinds] = {
121  // name offset bits flags
122  { "fixup_sparc_call30", 2, 30, MCFixupKindInfo::FKF_IsPCRel },
123  { "fixup_sparc_br22", 10, 22, MCFixupKindInfo::FKF_IsPCRel },
124  { "fixup_sparc_br19", 13, 19, MCFixupKindInfo::FKF_IsPCRel },
125  { "fixup_sparc_br16_2", 10, 2, MCFixupKindInfo::FKF_IsPCRel },
126  { "fixup_sparc_br16_14", 18, 14, MCFixupKindInfo::FKF_IsPCRel },
127  { "fixup_sparc_13", 19, 13, 0 },
128  { "fixup_sparc_hi22", 10, 22, 0 },
129  { "fixup_sparc_lo10", 22, 10, 0 },
130  { "fixup_sparc_h44", 10, 22, 0 },
131  { "fixup_sparc_m44", 22, 10, 0 },
132  { "fixup_sparc_l44", 20, 12, 0 },
133  { "fixup_sparc_hh", 10, 22, 0 },
134  { "fixup_sparc_hm", 22, 10, 0 },
135  { "fixup_sparc_pc22", 10, 22, MCFixupKindInfo::FKF_IsPCRel },
136  { "fixup_sparc_pc10", 22, 10, MCFixupKindInfo::FKF_IsPCRel },
137  { "fixup_sparc_got22", 10, 22, 0 },
138  { "fixup_sparc_got10", 22, 10, 0 },
139  { "fixup_sparc_got13", 19, 13, 0 },
140  { "fixup_sparc_wplt30", 2, 30, MCFixupKindInfo::FKF_IsPCRel },
141  { "fixup_sparc_tls_gd_hi22", 10, 22, 0 },
142  { "fixup_sparc_tls_gd_lo10", 22, 10, 0 },
143  { "fixup_sparc_tls_gd_add", 0, 0, 0 },
144  { "fixup_sparc_tls_gd_call", 0, 0, 0 },
145  { "fixup_sparc_tls_ldm_hi22", 10, 22, 0 },
146  { "fixup_sparc_tls_ldm_lo10", 22, 10, 0 },
147  { "fixup_sparc_tls_ldm_add", 0, 0, 0 },
148  { "fixup_sparc_tls_ldm_call", 0, 0, 0 },
149  { "fixup_sparc_tls_ldo_hix22", 10, 22, 0 },
150  { "fixup_sparc_tls_ldo_lox10", 22, 10, 0 },
151  { "fixup_sparc_tls_ldo_add", 0, 0, 0 },
152  { "fixup_sparc_tls_ie_hi22", 10, 22, 0 },
153  { "fixup_sparc_tls_ie_lo10", 22, 10, 0 },
154  { "fixup_sparc_tls_ie_ld", 0, 0, 0 },
155  { "fixup_sparc_tls_ie_ldx", 0, 0, 0 },
156  { "fixup_sparc_tls_ie_add", 0, 0, 0 },
157  { "fixup_sparc_tls_le_hix22", 0, 0, 0 },
158  { "fixup_sparc_tls_le_lox10", 0, 0, 0 }
159  };
160 
161  const static MCFixupKindInfo InfosLE[Sparc::NumTargetFixupKinds] = {
162  // name offset bits flags
163  { "fixup_sparc_call30", 0, 30, MCFixupKindInfo::FKF_IsPCRel },
164  { "fixup_sparc_br22", 0, 22, MCFixupKindInfo::FKF_IsPCRel },
165  { "fixup_sparc_br19", 0, 19, MCFixupKindInfo::FKF_IsPCRel },
166  { "fixup_sparc_br16_2", 20, 2, MCFixupKindInfo::FKF_IsPCRel },
167  { "fixup_sparc_br16_14", 0, 14, MCFixupKindInfo::FKF_IsPCRel },
168  { "fixup_sparc_13", 0, 13, 0 },
169  { "fixup_sparc_hi22", 0, 22, 0 },
170  { "fixup_sparc_lo10", 0, 10, 0 },
171  { "fixup_sparc_h44", 0, 22, 0 },
172  { "fixup_sparc_m44", 0, 10, 0 },
173  { "fixup_sparc_l44", 0, 12, 0 },
174  { "fixup_sparc_hh", 0, 22, 0 },
175  { "fixup_sparc_hm", 0, 10, 0 },
176  { "fixup_sparc_pc22", 0, 22, MCFixupKindInfo::FKF_IsPCRel },
177  { "fixup_sparc_pc10", 0, 10, MCFixupKindInfo::FKF_IsPCRel },
178  { "fixup_sparc_got22", 0, 22, 0 },
179  { "fixup_sparc_got10", 0, 10, 0 },
180  { "fixup_sparc_got13", 0, 13, 0 },
181  { "fixup_sparc_wplt30", 0, 30, MCFixupKindInfo::FKF_IsPCRel },
182  { "fixup_sparc_tls_gd_hi22", 0, 22, 0 },
183  { "fixup_sparc_tls_gd_lo10", 0, 10, 0 },
184  { "fixup_sparc_tls_gd_add", 0, 0, 0 },
185  { "fixup_sparc_tls_gd_call", 0, 0, 0 },
186  { "fixup_sparc_tls_ldm_hi22", 0, 22, 0 },
187  { "fixup_sparc_tls_ldm_lo10", 0, 10, 0 },
188  { "fixup_sparc_tls_ldm_add", 0, 0, 0 },
189  { "fixup_sparc_tls_ldm_call", 0, 0, 0 },
190  { "fixup_sparc_tls_ldo_hix22", 0, 22, 0 },
191  { "fixup_sparc_tls_ldo_lox10", 0, 10, 0 },
192  { "fixup_sparc_tls_ldo_add", 0, 0, 0 },
193  { "fixup_sparc_tls_ie_hi22", 0, 22, 0 },
194  { "fixup_sparc_tls_ie_lo10", 0, 10, 0 },
195  { "fixup_sparc_tls_ie_ld", 0, 0, 0 },
196  { "fixup_sparc_tls_ie_ldx", 0, 0, 0 },
197  { "fixup_sparc_tls_ie_add", 0, 0, 0 },
198  { "fixup_sparc_tls_le_hix22", 0, 0, 0 },
199  { "fixup_sparc_tls_le_lox10", 0, 0, 0 }
200  };
201 
202  if (Kind < FirstTargetFixupKind)
203  return MCAsmBackend::getFixupKindInfo(Kind);
204 
205  assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
206  "Invalid kind!");
207  if (Endian == support::little)
208  return InfosLE[Kind - FirstTargetFixupKind];
209 
210  return InfosBE[Kind - FirstTargetFixupKind];
211  }
212 
213  bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,
214  const MCValue &Target) override {
215  switch ((Sparc::Fixups)Fixup.getKind()) {
216  default:
217  return false;
219  if (Target.getSymA()->getSymbol().isTemporary())
220  return false;
240  return true;
241  }
242  }
243 
244  bool mayNeedRelaxation(const MCInst &Inst,
245  const MCSubtargetInfo &STI) const override {
246  // FIXME.
247  return false;
248  }
249 
250  /// fixupNeedsRelaxation - Target specific predicate for whether a given
251  /// fixup requires the associated instruction to be relaxed.
252  bool fixupNeedsRelaxation(const MCFixup &Fixup,
253  uint64_t Value,
254  const MCRelaxableFragment *DF,
255  const MCAsmLayout &Layout) const override {
256  // FIXME.
257  llvm_unreachable("fixupNeedsRelaxation() unimplemented");
258  return false;
259  }
260  void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
261  MCInst &Res) const override {
262  // FIXME.
263  llvm_unreachable("relaxInstruction() unimplemented");
264  }
265 
266  bool writeNopData(raw_ostream &OS, uint64_t Count) const override {
267  // Cannot emit NOP with size not multiple of 32 bits.
268  if (Count % 4 != 0)
269  return false;
270 
271  uint64_t NumNops = Count / 4;
272  for (uint64_t i = 0; i != NumNops; ++i)
273  support::endian::write<uint32_t>(OS, 0x01000000, Endian);
274 
275  return true;
276  }
277  };
278 
279  class ELFSparcAsmBackend : public SparcAsmBackend {
280  Triple::OSType OSType;
281  public:
282  ELFSparcAsmBackend(const Target &T, Triple::OSType OSType) :
283  SparcAsmBackend(T), OSType(OSType) { }
284 
285  void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
287  uint64_t Value, bool IsResolved,
288  const MCSubtargetInfo *STI) const override {
289 
290  Value = adjustFixupValue(Fixup.getKind(), Value);
291  if (!Value) return; // Doesn't change encoding.
292 
293  unsigned Offset = Fixup.getOffset();
294 
295  // For each byte of the fragment that the fixup touches, mask in the bits
296  // from the fixup value. The Value has been "split up" into the
297  // appropriate bitfields above.
298  for (unsigned i = 0; i != 4; ++i) {
299  unsigned Idx = Endian == support::little ? i : 3 - i;
300  Data[Offset + Idx] |= uint8_t((Value >> (i * 8)) & 0xff);
301  }
302  }
303 
304  std::unique_ptr<MCObjectTargetWriter>
305  createObjectTargetWriter() const override {
306  uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(OSType);
307  return createSparcELFObjectWriter(Is64Bit, OSABI);
308  }
309  };
310 
311 } // end anonymous namespace
312 
314  const MCSubtargetInfo &STI,
315  const MCRegisterInfo &MRI,
316  const MCTargetOptions &Options) {
317  return new ELFSparcAsmBackend(T, STI.getTargetTriple().getOS());
318 }
fixup_sparc_got22 - 22-bit fixup corresponding to got22(foo)
fixup_sparc_13 - 13-bit fixup
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
This represents an "assembler immediate".
Definition: MCValue.h:40
OSType getOS() const
getOS - Get the parsed operating system type of this triple.
Definition: Triple.h:294
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
fixup_sparc_m44 - 10-bit fixup corresponding to m44(foo)
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:74
const Triple & getTargetTriple() const
Is this fixup kind PCrelative? This is used by the assembler backend to evaluate fixup values in a ta...
const char * getName() const
getName - Get the target name.
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:29
fixup_sparc_got10 - 10-bit fixup corresponding to got10(foo)
A four-byte fixup.
Definition: MCFixup.h:26
std::unique_ptr< MCObjectTargetWriter > createSparcELFObjectWriter(bool Is64Bit, uint8_t OSABI)
fixup_sparc_hm - 10-bit fixup corresponding to hm(foo)
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
fixup_sparc_br19 - 19-bit PC relative relocation for branches on icc/xcc
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
MCAsmBackend * createSparcAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:271
unsigned const MachineRegisterInfo * MRI
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:291
fixup_sparc_hh - 22-bit fixup corresponding to hh(foo)
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:23
bool isTemporary() const
isTemporary - Check if this is an assembler temporary symbol.
Definition: MCSymbol.h:220
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:48
uint32_t getOffset() const
Definition: MCFixup.h:125
fixup_sparc_l44 - 12-bit fixup corresponding to l44(foo)
fixup_sparc_bpr - 16-bit fixup for bpr
static unsigned adjustFixupValue(unsigned Kind, uint64_t Value)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
A one-byte fixup.
Definition: MCFixup.h:24
PowerPC TLS Dynamic Call Fixup
fixup_sparc_lo10 - 10-bit fixup corresponding to lo(foo)
fixup_sparc_got13 - 13-bit fixup corresponding to got13(foo)
const MCSymbol & getSymbol() const
Definition: MCExpr.h:334
Target - Wrapper for Target specific information.
fixup_sparc_pc22 - 22-bit fixup corresponding to pc22(foo)
Generic base class for all target subtargets.
A eight-byte fixup.
Definition: MCFixup.h:27
Target independent information on a fixup kind.
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
fixup_sparc_br22 - 22-bit PC relative relocation for branches
LLVM Value Representation.
Definition: Value.h:73
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:42
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:238
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
fixups for Thread Local Storage
fixup_sparc_hi22 - 22-bit fixup corresponding to hi(foo) for sethi
A two-byte fixup.
Definition: MCFixup.h:25
fixup_sparc_h44 - 22-bit fixup corresponding to h44(foo)
MCFixupKind getKind() const
Definition: MCFixup.h:123
fixup_sparc_pc10 - 10-bit fixup corresponding to pc10(foo)