LLVM  8.0.1
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 /// getFixupKindNumBytes - The number of bytes the fixup may change.
104 static unsigned getFixupKindNumBytes(unsigned Kind) {
105  switch (Kind) {
106  default:
107  return 4;
108  case FK_Data_1:
109  return 1;
110  case FK_Data_2:
111  return 2;
112  case FK_Data_8:
113  return 8;
114  }
115 }
116 
117 namespace {
118  class SparcAsmBackend : public MCAsmBackend {
119  protected:
120  const Target &TheTarget;
121  bool Is64Bit;
122 
123  public:
124  SparcAsmBackend(const Target &T)
125  : MCAsmBackend(StringRef(T.getName()) == "sparcel" ? support::little
126  : support::big),
127  TheTarget(T), Is64Bit(StringRef(TheTarget.getName()) == "sparcv9") {}
128 
129  unsigned getNumFixupKinds() const override {
131  }
132 
133  const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
134  const static MCFixupKindInfo InfosBE[Sparc::NumTargetFixupKinds] = {
135  // name offset bits flags
136  { "fixup_sparc_call30", 2, 30, MCFixupKindInfo::FKF_IsPCRel },
137  { "fixup_sparc_br22", 10, 22, MCFixupKindInfo::FKF_IsPCRel },
138  { "fixup_sparc_br19", 13, 19, MCFixupKindInfo::FKF_IsPCRel },
139  { "fixup_sparc_br16_2", 10, 2, MCFixupKindInfo::FKF_IsPCRel },
140  { "fixup_sparc_br16_14", 18, 14, MCFixupKindInfo::FKF_IsPCRel },
141  { "fixup_sparc_13", 19, 13, 0 },
142  { "fixup_sparc_hi22", 10, 22, 0 },
143  { "fixup_sparc_lo10", 22, 10, 0 },
144  { "fixup_sparc_h44", 10, 22, 0 },
145  { "fixup_sparc_m44", 22, 10, 0 },
146  { "fixup_sparc_l44", 20, 12, 0 },
147  { "fixup_sparc_hh", 10, 22, 0 },
148  { "fixup_sparc_hm", 22, 10, 0 },
149  { "fixup_sparc_pc22", 10, 22, MCFixupKindInfo::FKF_IsPCRel },
150  { "fixup_sparc_pc10", 22, 10, MCFixupKindInfo::FKF_IsPCRel },
151  { "fixup_sparc_got22", 10, 22, 0 },
152  { "fixup_sparc_got10", 22, 10, 0 },
153  { "fixup_sparc_got13", 19, 13, 0 },
154  { "fixup_sparc_wplt30", 2, 30, MCFixupKindInfo::FKF_IsPCRel },
155  { "fixup_sparc_tls_gd_hi22", 10, 22, 0 },
156  { "fixup_sparc_tls_gd_lo10", 22, 10, 0 },
157  { "fixup_sparc_tls_gd_add", 0, 0, 0 },
158  { "fixup_sparc_tls_gd_call", 0, 0, 0 },
159  { "fixup_sparc_tls_ldm_hi22", 10, 22, 0 },
160  { "fixup_sparc_tls_ldm_lo10", 22, 10, 0 },
161  { "fixup_sparc_tls_ldm_add", 0, 0, 0 },
162  { "fixup_sparc_tls_ldm_call", 0, 0, 0 },
163  { "fixup_sparc_tls_ldo_hix22", 10, 22, 0 },
164  { "fixup_sparc_tls_ldo_lox10", 22, 10, 0 },
165  { "fixup_sparc_tls_ldo_add", 0, 0, 0 },
166  { "fixup_sparc_tls_ie_hi22", 10, 22, 0 },
167  { "fixup_sparc_tls_ie_lo10", 22, 10, 0 },
168  { "fixup_sparc_tls_ie_ld", 0, 0, 0 },
169  { "fixup_sparc_tls_ie_ldx", 0, 0, 0 },
170  { "fixup_sparc_tls_ie_add", 0, 0, 0 },
171  { "fixup_sparc_tls_le_hix22", 0, 0, 0 },
172  { "fixup_sparc_tls_le_lox10", 0, 0, 0 }
173  };
174 
175  const static MCFixupKindInfo InfosLE[Sparc::NumTargetFixupKinds] = {
176  // name offset bits flags
177  { "fixup_sparc_call30", 0, 30, MCFixupKindInfo::FKF_IsPCRel },
178  { "fixup_sparc_br22", 0, 22, MCFixupKindInfo::FKF_IsPCRel },
179  { "fixup_sparc_br19", 0, 19, MCFixupKindInfo::FKF_IsPCRel },
180  { "fixup_sparc_br16_2", 20, 2, MCFixupKindInfo::FKF_IsPCRel },
181  { "fixup_sparc_br16_14", 0, 14, MCFixupKindInfo::FKF_IsPCRel },
182  { "fixup_sparc_13", 0, 13, 0 },
183  { "fixup_sparc_hi22", 0, 22, 0 },
184  { "fixup_sparc_lo10", 0, 10, 0 },
185  { "fixup_sparc_h44", 0, 22, 0 },
186  { "fixup_sparc_m44", 0, 10, 0 },
187  { "fixup_sparc_l44", 0, 12, 0 },
188  { "fixup_sparc_hh", 0, 22, 0 },
189  { "fixup_sparc_hm", 0, 10, 0 },
190  { "fixup_sparc_pc22", 0, 22, MCFixupKindInfo::FKF_IsPCRel },
191  { "fixup_sparc_pc10", 0, 10, MCFixupKindInfo::FKF_IsPCRel },
192  { "fixup_sparc_got22", 0, 22, 0 },
193  { "fixup_sparc_got10", 0, 10, 0 },
194  { "fixup_sparc_got13", 0, 13, 0 },
195  { "fixup_sparc_wplt30", 0, 30, MCFixupKindInfo::FKF_IsPCRel },
196  { "fixup_sparc_tls_gd_hi22", 0, 22, 0 },
197  { "fixup_sparc_tls_gd_lo10", 0, 10, 0 },
198  { "fixup_sparc_tls_gd_add", 0, 0, 0 },
199  { "fixup_sparc_tls_gd_call", 0, 0, 0 },
200  { "fixup_sparc_tls_ldm_hi22", 0, 22, 0 },
201  { "fixup_sparc_tls_ldm_lo10", 0, 10, 0 },
202  { "fixup_sparc_tls_ldm_add", 0, 0, 0 },
203  { "fixup_sparc_tls_ldm_call", 0, 0, 0 },
204  { "fixup_sparc_tls_ldo_hix22", 0, 22, 0 },
205  { "fixup_sparc_tls_ldo_lox10", 0, 10, 0 },
206  { "fixup_sparc_tls_ldo_add", 0, 0, 0 },
207  { "fixup_sparc_tls_ie_hi22", 0, 22, 0 },
208  { "fixup_sparc_tls_ie_lo10", 0, 10, 0 },
209  { "fixup_sparc_tls_ie_ld", 0, 0, 0 },
210  { "fixup_sparc_tls_ie_ldx", 0, 0, 0 },
211  { "fixup_sparc_tls_ie_add", 0, 0, 0 },
212  { "fixup_sparc_tls_le_hix22", 0, 0, 0 },
213  { "fixup_sparc_tls_le_lox10", 0, 0, 0 }
214  };
215 
216  if (Kind < FirstTargetFixupKind)
217  return MCAsmBackend::getFixupKindInfo(Kind);
218 
219  assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
220  "Invalid kind!");
221  if (Endian == support::little)
222  return InfosLE[Kind - FirstTargetFixupKind];
223 
224  return InfosBE[Kind - FirstTargetFixupKind];
225  }
226 
227  bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,
228  const MCValue &Target) override {
229  switch ((Sparc::Fixups)Fixup.getKind()) {
230  default:
231  return false;
233  if (Target.getSymA()->getSymbol().isTemporary())
234  return false;
254  return true;
255  }
256  }
257 
258  bool mayNeedRelaxation(const MCInst &Inst,
259  const MCSubtargetInfo &STI) const override {
260  // FIXME.
261  return false;
262  }
263 
264  /// fixupNeedsRelaxation - Target specific predicate for whether a given
265  /// fixup requires the associated instruction to be relaxed.
266  bool fixupNeedsRelaxation(const MCFixup &Fixup,
267  uint64_t Value,
268  const MCRelaxableFragment *DF,
269  const MCAsmLayout &Layout) const override {
270  // FIXME.
271  llvm_unreachable("fixupNeedsRelaxation() unimplemented");
272  return false;
273  }
274  void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
275  MCInst &Res) const override {
276  // FIXME.
277  llvm_unreachable("relaxInstruction() unimplemented");
278  }
279 
280  bool writeNopData(raw_ostream &OS, uint64_t Count) const override {
281  // Cannot emit NOP with size not multiple of 32 bits.
282  if (Count % 4 != 0)
283  return false;
284 
285  uint64_t NumNops = Count / 4;
286  for (uint64_t i = 0; i != NumNops; ++i)
287  support::endian::write<uint32_t>(OS, 0x01000000, Endian);
288 
289  return true;
290  }
291  };
292 
293  class ELFSparcAsmBackend : public SparcAsmBackend {
294  Triple::OSType OSType;
295  public:
296  ELFSparcAsmBackend(const Target &T, Triple::OSType OSType) :
297  SparcAsmBackend(T), OSType(OSType) { }
298 
299  void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
301  uint64_t Value, bool IsResolved,
302  const MCSubtargetInfo *STI) const override {
303 
304  Value = adjustFixupValue(Fixup.getKind(), Value);
305  if (!Value) return; // Doesn't change encoding.
306 
307  unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
308  unsigned Offset = Fixup.getOffset();
309  // For each byte of the fragment that the fixup touches, mask in the bits
310  // from the fixup value. The Value has been "split up" into the
311  // appropriate bitfields above.
312  for (unsigned i = 0; i != NumBytes; ++i) {
313  unsigned Idx = Endian == support::little ? i : (NumBytes - 1) - i;
314  Data[Offset + Idx] |= uint8_t((Value >> (i * 8)) & 0xff);
315  }
316  }
317 
318  std::unique_ptr<MCObjectTargetWriter>
319  createObjectTargetWriter() const override {
320  uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(OSType);
321  return createSparcELFObjectWriter(Is64Bit, OSABI);
322  }
323  };
324 
325 } // end anonymous namespace
326 
328  const MCSubtargetInfo &STI,
329  const MCRegisterInfo &MRI,
330  const MCTargetOptions &Options) {
331  return new ELFSparcAsmBackend(T, STI.getTargetTriple().getOS());
332 }
fixup_sparc_got22 - 22-bit fixup corresponding to got22(foo)
fixup_sparc_13 - 13-bit fixup
This class represents lattice values for constants.
Definition: AllocatorList.h:24
This represents an "assembler immediate".
Definition: MCValue.h:40
#define LLVM_FALLTHROUGH
Definition: Compiler.h:86
OSType getOS() const
getOS - Get the parsed operating system type of this triple.
Definition: Triple.h:299
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
static unsigned getFixupKindNumBytes(unsigned Kind)
getFixupKindNumBytes - The number of bytes the fixup may change.
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:336
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
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)