LLVM  6.0.0svn
PPCAsmBackend.cpp
Go to the documentation of this file.
1 //===-- PPCAsmBackend.cpp - PPC 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/BinaryFormat/ELF.h"
14 #include "llvm/MC/MCAsmBackend.h"
15 #include "llvm/MC/MCAssembler.h"
19 #include "llvm/MC/MCObjectWriter.h"
20 #include "llvm/MC/MCSectionMachO.h"
21 #include "llvm/MC/MCSymbolELF.h"
22 #include "llvm/MC/MCValue.h"
25 using namespace llvm;
26 
27 static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value) {
28  switch (Kind) {
29  default:
30  llvm_unreachable("Unknown fixup kind!");
31  case FK_Data_1:
32  case FK_Data_2:
33  case FK_Data_4:
34  case FK_Data_8:
36  return Value;
39  return Value & 0xfffc;
42  return Value & 0x3fffffc;
44  return Value & 0xffff;
46  return Value & 0xfffc;
47  }
48 }
49 
50 static unsigned getFixupKindNumBytes(unsigned Kind) {
51  switch (Kind) {
52  default:
53  llvm_unreachable("Unknown fixup kind!");
54  case FK_Data_1:
55  return 1;
56  case FK_Data_2:
59  return 2;
60  case FK_Data_4:
65  return 4;
66  case FK_Data_8:
67  return 8;
69  return 0;
70  }
71 }
72 
73 namespace {
74 
75 class PPCAsmBackend : public MCAsmBackend {
76  const Target &TheTarget;
77  bool IsLittleEndian;
78 public:
79  PPCAsmBackend(const Target &T, bool isLittle) : MCAsmBackend(), TheTarget(T),
80  IsLittleEndian(isLittle) {}
81 
82  unsigned getNumFixupKinds() const override {
84  }
85 
86  const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
87  const static MCFixupKindInfo InfosBE[PPC::NumTargetFixupKinds] = {
88  // name offset bits flags
89  { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel },
90  { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel },
91  { "fixup_ppc_br24abs", 6, 24, 0 },
92  { "fixup_ppc_brcond14abs", 16, 14, 0 },
93  { "fixup_ppc_half16", 0, 16, 0 },
94  { "fixup_ppc_half16ds", 0, 14, 0 },
95  { "fixup_ppc_nofixup", 0, 0, 0 }
96  };
97  const static MCFixupKindInfo InfosLE[PPC::NumTargetFixupKinds] = {
98  // name offset bits flags
99  { "fixup_ppc_br24", 2, 24, MCFixupKindInfo::FKF_IsPCRel },
100  { "fixup_ppc_brcond14", 2, 14, MCFixupKindInfo::FKF_IsPCRel },
101  { "fixup_ppc_br24abs", 2, 24, 0 },
102  { "fixup_ppc_brcond14abs", 2, 14, 0 },
103  { "fixup_ppc_half16", 0, 16, 0 },
104  { "fixup_ppc_half16ds", 2, 14, 0 },
105  { "fixup_ppc_nofixup", 0, 0, 0 }
106  };
107 
108  if (Kind < FirstTargetFixupKind)
109  return MCAsmBackend::getFixupKindInfo(Kind);
110 
111  assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
112  "Invalid kind!");
113  return (IsLittleEndian? InfosLE : InfosBE)[Kind - FirstTargetFixupKind];
114  }
115 
116  void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
118  uint64_t Value, bool IsResolved) const override {
119  Value = adjustFixupValue(Fixup.getKind(), Value);
120  if (!Value) return; // Doesn't change encoding.
121 
122  unsigned Offset = Fixup.getOffset();
123  unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
124 
125  // For each byte of the fragment that the fixup touches, mask in the bits
126  // from the fixup value. The Value has been "split up" into the appropriate
127  // bitfields above.
128  for (unsigned i = 0; i != NumBytes; ++i) {
129  unsigned Idx = IsLittleEndian ? i : (NumBytes - 1 - i);
130  Data[Offset + i] |= uint8_t((Value >> (Idx * 8)) & 0xff);
131  }
132  }
133 
134  bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,
135  const MCValue &Target) override {
136  switch ((PPC::Fixups)Fixup.getKind()) {
137  default:
138  return false;
139  case PPC::fixup_ppc_br24:
141  // If the target symbol has a local entry point we must not attempt
142  // to resolve the fixup directly. Emit a relocation and leave
143  // resolution of the final target address to the linker.
144  if (const MCSymbolRefExpr *A = Target.getSymA()) {
145  if (const auto *S = dyn_cast<MCSymbolELF>(&A->getSymbol())) {
146  // The "other" values are stored in the last 6 bits of the second
147  // byte. The traditional defines for STO values assume the full byte
148  // and thus the shift to pack it.
149  unsigned Other = S->getOther() << 2;
150  if ((Other & ELF::STO_PPC64_LOCAL_MASK) != 0)
151  return true;
152  }
153  }
154  return false;
155  }
156  }
157 
158  bool mayNeedRelaxation(const MCInst &Inst) const override {
159  // FIXME.
160  return false;
161  }
162 
163  bool fixupNeedsRelaxation(const MCFixup &Fixup,
164  uint64_t Value,
165  const MCRelaxableFragment *DF,
166  const MCAsmLayout &Layout) const override {
167  // FIXME.
168  llvm_unreachable("relaxInstruction() unimplemented");
169  }
170 
171  void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
172  MCInst &Res) const override {
173  // FIXME.
174  llvm_unreachable("relaxInstruction() unimplemented");
175  }
176 
177  bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override {
178  uint64_t NumNops = Count / 4;
179  for (uint64_t i = 0; i != NumNops; ++i)
180  OW->write32(0x60000000);
181 
182  OW->WriteZeros(Count % 4);
183 
184  return true;
185  }
186 
187  unsigned getPointerSize() const {
188  StringRef Name = TheTarget.getName();
189  if (Name == "ppc64" || Name == "ppc64le") return 8;
190  assert(Name == "ppc32" && "Unknown target name!");
191  return 4;
192  }
193 
194  bool isLittleEndian() const {
195  return IsLittleEndian;
196  }
197 };
198 } // end anonymous namespace
199 
200 
201 // FIXME: This should be in a separate file.
202 namespace {
203  class DarwinPPCAsmBackend : public PPCAsmBackend {
204  public:
205  DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T, false) { }
206 
207  std::unique_ptr<MCObjectWriter>
208  createObjectWriter(raw_pwrite_stream &OS) const override {
209  bool is64 = getPointerSize() == 8;
211  OS,
212  /*Is64Bit=*/is64,
215  }
216  };
217 
218  class ELFPPCAsmBackend : public PPCAsmBackend {
219  uint8_t OSABI;
220  public:
221  ELFPPCAsmBackend(const Target &T, bool IsLittleEndian, uint8_t OSABI) :
222  PPCAsmBackend(T, IsLittleEndian), OSABI(OSABI) { }
223 
224  std::unique_ptr<MCObjectWriter>
225  createObjectWriter(raw_pwrite_stream &OS) const override {
226  bool is64 = getPointerSize() == 8;
227  return createPPCELFObjectWriter(OS, is64, isLittleEndian(), OSABI);
228  }
229  };
230 
231 } // end anonymous namespace
232 
234  const MCRegisterInfo &MRI,
235  const Triple &TT, StringRef CPU,
236  const MCTargetOptions &Options) {
237  if (TT.isOSDarwin())
238  return new DarwinPPCAsmBackend(T);
239 
240  uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
241  bool IsLittleEndian = TT.getArch() == Triple::ppc64le;
242  return new ELFPPCAsmBackend(T, IsLittleEndian, OSABI);
243 }
MCAsmBackend * createPPCAsmBackend(const Target &T, const MCRegisterInfo &MRI, const Triple &TT, StringRef CPU, const MCTargetOptions &Options)
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
Definition: Triple.h:470
void WriteZeros(unsigned N)
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.
14-bit absolute relocation for conditional branches.
Definition: PPCFixupKinds.h:30
Defines the object file and target independent interfaces used by the assembler backend to write nati...
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...
const char * getName() const
getName - Get the target name.
A 16-bit fixup corresponding to lo16(_foo) or ha16(_foo) for instrs like &#39;li&#39; or &#39;addis&#39;.
Definition: PPCFixupKinds.h:34
24-bit absolute relocation for direct branches like &#39;ba&#39; and &#39;bla&#39;.
Definition: PPCFixupKinds.h:27
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:29
Not a true fixup, but ties a symbol to a call to __tls_get_addr for the TLS general and local dynamic...
Definition: PPCFixupKinds.h:43
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:165
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:736
A four-byte fixup.
Definition: MCFixup.h:26
std::unique_ptr< MCObjectWriter > createPPCMachObjectWriter(raw_pwrite_stream &OS, bool Is64Bit, uint32_t CPUType, uint32_t CPUSubtype)
Construct a PPC Mach-O object writer.
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:285
static unsigned getFixupKindNumBytes(unsigned Kind)
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:159
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:257
void write32(uint32_t Value)
unsigned const MachineRegisterInfo * MRI
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:291
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:23
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:48
uint32_t getOffset() const
Definition: MCFixup.h:95
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
A one-byte fixup.
Definition: MCFixup.h:24
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
PowerPC TLS Dynamic Call Fixup
std::unique_ptr< MCObjectWriter > createPPCELFObjectWriter(raw_pwrite_stream &OS, bool Is64Bit, bool IsLittleEndian, uint8_t OSABI)
Construct an PPC ELF object writer.
Target - Wrapper for Target specific information.
A 14-bit fixup corresponding to lo16(_foo) with implied 2 zero bits for instrs like &#39;std&#39;...
Definition: PPCFixupKinds.h:38
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:337
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:40
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value)
14-bit PC relative relocation for conditional branches.
Definition: PPCFixupKinds.h:24
static uint64_t getPointerSize(const Value *V, const DataLayout &DL, const TargetLibraryInfo &TLI)
A two-byte fixup.
Definition: MCFixup.h:25
MCFixupKind getKind() const
Definition: MCFixup.h:93