LLVM  5.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"
17 #include "llvm/MC/MCValue.h"
19 
20 using namespace llvm;
21 
22 static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
23  switch (Kind) {
24  default:
25  llvm_unreachable("Unknown fixup kind!");
26  case FK_Data_1:
27  case FK_Data_2:
28  case FK_Data_4:
29  case FK_Data_8:
30  return Value;
31 
34  return (Value >> 2) & 0x3fffffff;
35 
37  return (Value >> 2) & 0x3fffff;
38 
40  return (Value >> 2) & 0x7ffff;
41 
43  return (Value >> 2) & 0xc000;
44 
46  return (Value >> 2) & 0x3fff;
47 
54  return (Value >> 10) & 0x3fffff;
55 
62  return Value & 0x3ff;
63 
65  return (Value >> 22) & 0x3fffff;
66 
68  return (Value >> 12) & 0x3ff;
69 
71  return Value & 0xfff;
72 
74  return (Value >> 42) & 0x3fffff;
75 
77  return (Value >> 32) & 0x3ff;
78 
83  assert(Value == 0 && "Sparc TLS relocs expect zero Value");
84  return 0;
85 
94  return 0;
95  }
96 }
97 
98 namespace {
99  class SparcAsmBackend : public MCAsmBackend {
100  protected:
101  const Target &TheTarget;
102  bool IsLittleEndian;
103  bool Is64Bit;
104 
105  public:
106  SparcAsmBackend(const Target &T)
107  : MCAsmBackend(), TheTarget(T),
108  IsLittleEndian(StringRef(TheTarget.getName()) == "sparcel"),
109  Is64Bit(StringRef(TheTarget.getName()) == "sparcv9") {}
110 
111  unsigned getNumFixupKinds() const override {
113  }
114 
115  const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
116  const static MCFixupKindInfo InfosBE[Sparc::NumTargetFixupKinds] = {
117  // name offset bits flags
118  { "fixup_sparc_call30", 2, 30, MCFixupKindInfo::FKF_IsPCRel },
119  { "fixup_sparc_br22", 10, 22, MCFixupKindInfo::FKF_IsPCRel },
120  { "fixup_sparc_br19", 13, 19, MCFixupKindInfo::FKF_IsPCRel },
121  { "fixup_sparc_br16_2", 10, 2, MCFixupKindInfo::FKF_IsPCRel },
122  { "fixup_sparc_br16_14", 18, 14, MCFixupKindInfo::FKF_IsPCRel },
123  { "fixup_sparc_hi22", 10, 22, 0 },
124  { "fixup_sparc_lo10", 22, 10, 0 },
125  { "fixup_sparc_h44", 10, 22, 0 },
126  { "fixup_sparc_m44", 22, 10, 0 },
127  { "fixup_sparc_l44", 20, 12, 0 },
128  { "fixup_sparc_hh", 10, 22, 0 },
129  { "fixup_sparc_hm", 22, 10, 0 },
130  { "fixup_sparc_pc22", 10, 22, MCFixupKindInfo::FKF_IsPCRel },
131  { "fixup_sparc_pc10", 22, 10, MCFixupKindInfo::FKF_IsPCRel },
132  { "fixup_sparc_got22", 10, 22, 0 },
133  { "fixup_sparc_got10", 22, 10, 0 },
134  { "fixup_sparc_wplt30", 2, 30, MCFixupKindInfo::FKF_IsPCRel },
135  { "fixup_sparc_tls_gd_hi22", 10, 22, 0 },
136  { "fixup_sparc_tls_gd_lo10", 22, 10, 0 },
137  { "fixup_sparc_tls_gd_add", 0, 0, 0 },
138  { "fixup_sparc_tls_gd_call", 0, 0, 0 },
139  { "fixup_sparc_tls_ldm_hi22", 10, 22, 0 },
140  { "fixup_sparc_tls_ldm_lo10", 22, 10, 0 },
141  { "fixup_sparc_tls_ldm_add", 0, 0, 0 },
142  { "fixup_sparc_tls_ldm_call", 0, 0, 0 },
143  { "fixup_sparc_tls_ldo_hix22", 10, 22, 0 },
144  { "fixup_sparc_tls_ldo_lox10", 22, 10, 0 },
145  { "fixup_sparc_tls_ldo_add", 0, 0, 0 },
146  { "fixup_sparc_tls_ie_hi22", 10, 22, 0 },
147  { "fixup_sparc_tls_ie_lo10", 22, 10, 0 },
148  { "fixup_sparc_tls_ie_ld", 0, 0, 0 },
149  { "fixup_sparc_tls_ie_ldx", 0, 0, 0 },
150  { "fixup_sparc_tls_ie_add", 0, 0, 0 },
151  { "fixup_sparc_tls_le_hix22", 0, 0, 0 },
152  { "fixup_sparc_tls_le_lox10", 0, 0, 0 }
153  };
154 
155  const static MCFixupKindInfo InfosLE[Sparc::NumTargetFixupKinds] = {
156  // name offset bits flags
157  { "fixup_sparc_call30", 0, 30, MCFixupKindInfo::FKF_IsPCRel },
158  { "fixup_sparc_br22", 0, 22, MCFixupKindInfo::FKF_IsPCRel },
159  { "fixup_sparc_br19", 0, 19, MCFixupKindInfo::FKF_IsPCRel },
160  { "fixup_sparc_br16_2", 20, 2, MCFixupKindInfo::FKF_IsPCRel },
161  { "fixup_sparc_br16_14", 0, 14, MCFixupKindInfo::FKF_IsPCRel },
162  { "fixup_sparc_hi22", 0, 22, 0 },
163  { "fixup_sparc_lo10", 0, 10, 0 },
164  { "fixup_sparc_h44", 0, 22, 0 },
165  { "fixup_sparc_m44", 0, 10, 0 },
166  { "fixup_sparc_l44", 0, 12, 0 },
167  { "fixup_sparc_hh", 0, 22, 0 },
168  { "fixup_sparc_hm", 0, 10, 0 },
169  { "fixup_sparc_pc22", 0, 22, MCFixupKindInfo::FKF_IsPCRel },
170  { "fixup_sparc_pc10", 0, 10, MCFixupKindInfo::FKF_IsPCRel },
171  { "fixup_sparc_got22", 0, 22, 0 },
172  { "fixup_sparc_got10", 0, 10, 0 },
173  { "fixup_sparc_wplt30", 0, 30, MCFixupKindInfo::FKF_IsPCRel },
174  { "fixup_sparc_tls_gd_hi22", 0, 22, 0 },
175  { "fixup_sparc_tls_gd_lo10", 0, 10, 0 },
176  { "fixup_sparc_tls_gd_add", 0, 0, 0 },
177  { "fixup_sparc_tls_gd_call", 0, 0, 0 },
178  { "fixup_sparc_tls_ldm_hi22", 0, 22, 0 },
179  { "fixup_sparc_tls_ldm_lo10", 0, 10, 0 },
180  { "fixup_sparc_tls_ldm_add", 0, 0, 0 },
181  { "fixup_sparc_tls_ldm_call", 0, 0, 0 },
182  { "fixup_sparc_tls_ldo_hix22", 0, 22, 0 },
183  { "fixup_sparc_tls_ldo_lox10", 0, 10, 0 },
184  { "fixup_sparc_tls_ldo_add", 0, 0, 0 },
185  { "fixup_sparc_tls_ie_hi22", 0, 22, 0 },
186  { "fixup_sparc_tls_ie_lo10", 0, 10, 0 },
187  { "fixup_sparc_tls_ie_ld", 0, 0, 0 },
188  { "fixup_sparc_tls_ie_ldx", 0, 0, 0 },
189  { "fixup_sparc_tls_ie_add", 0, 0, 0 },
190  { "fixup_sparc_tls_le_hix22", 0, 0, 0 },
191  { "fixup_sparc_tls_le_lox10", 0, 0, 0 }
192  };
193 
194  if (Kind < FirstTargetFixupKind)
195  return MCAsmBackend::getFixupKindInfo(Kind);
196 
197  assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
198  "Invalid kind!");
199  if (IsLittleEndian)
200  return InfosLE[Kind - FirstTargetFixupKind];
201 
202  return InfosBE[Kind - FirstTargetFixupKind];
203  }
204 
205  bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,
206  const MCValue &Target) override {
207  switch ((Sparc::Fixups)Fixup.getKind()) {
208  default:
209  return false;
211  if (Target.getSymA()->getSymbol().isTemporary())
212  return false;
232  return true;
233  }
234  }
235 
236  bool mayNeedRelaxation(const MCInst &Inst) const override {
237  // FIXME.
238  return false;
239  }
240 
241  /// fixupNeedsRelaxation - Target specific predicate for whether a given
242  /// fixup requires the associated instruction to be relaxed.
243  bool fixupNeedsRelaxation(const MCFixup &Fixup,
244  uint64_t Value,
245  const MCRelaxableFragment *DF,
246  const MCAsmLayout &Layout) const override {
247  // FIXME.
248  llvm_unreachable("fixupNeedsRelaxation() unimplemented");
249  return false;
250  }
251  void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
252  MCInst &Res) const override {
253  // FIXME.
254  llvm_unreachable("relaxInstruction() unimplemented");
255  }
256 
257  bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override {
258  // Cannot emit NOP with size not multiple of 32 bits.
259  if (Count % 4 != 0)
260  return false;
261 
262  uint64_t NumNops = Count / 4;
263  for (uint64_t i = 0; i != NumNops; ++i)
264  OW->write32(0x01000000);
265 
266  return true;
267  }
268  };
269 
270  class ELFSparcAsmBackend : public SparcAsmBackend {
271  Triple::OSType OSType;
272  public:
273  ELFSparcAsmBackend(const Target &T, Triple::OSType OSType) :
274  SparcAsmBackend(T), OSType(OSType) { }
275 
276  void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
277  const MCValue &Target, MutableArrayRef<char> Data,
278  uint64_t Value, bool IsResolved) const override {
279 
280  Value = adjustFixupValue(Fixup.getKind(), Value);
281  if (!Value) return; // Doesn't change encoding.
282 
283  unsigned Offset = Fixup.getOffset();
284 
285  // For each byte of the fragment that the fixup touches, mask in the bits
286  // from the fixup value. The Value has been "split up" into the
287  // appropriate bitfields above.
288  for (unsigned i = 0; i != 4; ++i) {
289  unsigned Idx = IsLittleEndian ? i : 3 - i;
290  Data[Offset + Idx] |= uint8_t((Value >> (i * 8)) & 0xff);
291  }
292  }
293 
294  MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
295  uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(OSType);
296  return createSparcELFObjectWriter(OS, Is64Bit, IsLittleEndian, OSABI);
297  }
298  };
299 
300 } // end anonymous namespace
301 
303  const MCRegisterInfo &MRI,
304  const Triple &TT, StringRef CPU,
305  const MCTargetOptions &Options) {
306  return new ELFSparcAsmBackend(T, TT.getOS());
307 }
OSType getOS() const
getOS - Get the parsed operating system type of this triple.
Definition: Triple.h:290
fixup_sparc_got22 - 22-bit fixup corresponding to got22(foo)
const MCSymbol & getSymbol() const
Definition: MCExpr.h:318
This represents an "assembler immediate".
Definition: MCValue.h:40
fixup_sparc_m44 - 10-bit fixup corresponding to m44(foo)
Defines the object file and target independent interfaces used by the assembler backend to write nati...
MCObjectWriter * createSparcELFObjectWriter(raw_pwrite_stream &OS, bool Is64Bit, bool IsLIttleEndian, uint8_t OSABI)
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:66
Is this fixup kind PCrelative? This is used by the assembler backend to evaluate fixup values in a ta...
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)
static StringRef getName(Value *V)
A four-byte fixup.
Definition: MCFixup.h:26
fixup_sparc_hm - 10-bit fixup corresponding to hm(foo)
uint32_t getOffset() const
Definition: MCFixup.h:95
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:159
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...
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:255
void write32(uint32_t Value)
MCAsmBackend * createSparcAsmBackend(const Target &T, const MCRegisterInfo &MRI, const Triple &TT, StringRef CPU, const MCTargetOptions &Options)
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
fixup_sparc_l44 - 12-bit fixup corresponding to l44(foo)
MCFixupKind getKind() const
Definition: MCFixup.h:93
fixup_sparc_bpr - 16-bit fixup for bpr
static unsigned adjustFixupValue(unsigned Kind, uint64_t Value)
A one-byte fixup.
Definition: MCFixup.h:24
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
fixup_sparc_lo10 - 10-bit fixup corresponding to lo(foo)
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:47
Target - Wrapper for Target specific information.
fixup_sparc_pc22 - 22-bit fixup corresponding to pc22(foo)
bool isTemporary() const
isTemporary - Check if this is an assembler temporary symbol.
Definition: MCSymbol.h:220
llvm_unreachable("Unknown RegBank!")
MCSubtargetInfo - Generic base class for all target subtargets.
A eight-byte fixup.
Definition: MCFixup.h:27
Target independent information on a fixup kind.
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:333
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:35
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:235
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
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
A two-byte fixup.
Definition: MCFixup.h:25
fixup_sparc_h44 - 22-bit fixup corresponding to h44(foo)
fixup_sparc_pc10 - 10-bit fixup corresponding to pc10(foo)