LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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/MC/MCAsmBackend.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/MC/MCSectionMachO.h"
18 #include "llvm/MC/MCValue.h"
19 #include "llvm/Support/ELF.h"
21 #include "llvm/Support/MachO.h"
23 using namespace llvm;
24 
25 static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value) {
26  switch (Kind) {
27  default:
28  llvm_unreachable("Unknown fixup kind!");
29  case FK_Data_1:
30  case FK_Data_2:
31  case FK_Data_4:
32  case FK_Data_8:
34  return Value;
37  return Value & 0xfffc;
40  return Value & 0x3fffffc;
42  return Value & 0xffff;
44  return Value & 0xfffc;
45  }
46 }
47 
48 static unsigned getFixupKindNumBytes(unsigned Kind) {
49  switch (Kind) {
50  default:
51  llvm_unreachable("Unknown fixup kind!");
52  case FK_Data_1:
53  return 1;
54  case FK_Data_2:
57  return 2;
58  case FK_Data_4:
63  return 4;
64  case FK_Data_8:
65  return 8;
67  return 0;
68  }
69 }
70 
71 namespace {
72 
73 class PPCAsmBackend : public MCAsmBackend {
74 const Target &TheTarget;
75 public:
76  PPCAsmBackend(const Target &T) : MCAsmBackend(), TheTarget(T) {}
77 
78  unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; }
79 
80  const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
81  const static MCFixupKindInfo Infos[PPC::NumTargetFixupKinds] = {
82  // name offset bits flags
83  { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel },
84  { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel },
85  { "fixup_ppc_br24abs", 6, 24, 0 },
86  { "fixup_ppc_brcond14abs", 16, 14, 0 },
87  { "fixup_ppc_half16", 0, 16, 0 },
88  { "fixup_ppc_half16ds", 0, 14, 0 },
89  { "fixup_ppc_nofixup", 0, 0, 0 }
90  };
91 
92  if (Kind < FirstTargetFixupKind)
93  return MCAsmBackend::getFixupKindInfo(Kind);
94 
95  assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
96  "Invalid kind!");
97  return Infos[Kind - FirstTargetFixupKind];
98  }
99 
100  void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
101  uint64_t Value) const {
102  Value = adjustFixupValue(Fixup.getKind(), Value);
103  if (!Value) return; // Doesn't change encoding.
104 
105  unsigned Offset = Fixup.getOffset();
106  unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
107 
108  // For each byte of the fragment that the fixup touches, mask in the bits
109  // from the fixup value. The Value has been "split up" into the appropriate
110  // bitfields above.
111  for (unsigned i = 0; i != NumBytes; ++i)
112  Data[Offset + i] |= uint8_t((Value >> ((NumBytes - i - 1)*8)) & 0xff);
113  }
114 
115  bool mayNeedRelaxation(const MCInst &Inst) const {
116  // FIXME.
117  return false;
118  }
119 
120  bool fixupNeedsRelaxation(const MCFixup &Fixup,
121  uint64_t Value,
122  const MCRelaxableFragment *DF,
123  const MCAsmLayout &Layout) const {
124  // FIXME.
125  llvm_unreachable("relaxInstruction() unimplemented");
126  }
127 
128 
129  void relaxInstruction(const MCInst &Inst, MCInst &Res) const {
130  // FIXME.
131  llvm_unreachable("relaxInstruction() unimplemented");
132  }
133 
134  bool writeNopData(uint64_t Count, MCObjectWriter *OW) const {
135  uint64_t NumNops = Count / 4;
136  for (uint64_t i = 0; i != NumNops; ++i)
137  OW->Write32(0x60000000);
138 
139  switch (Count % 4) {
140  default: break; // No leftover bytes to write
141  case 1: OW->Write8(0); break;
142  case 2: OW->Write16(0); break;
143  case 3: OW->Write16(0); OW->Write8(0); break;
144  }
145 
146  return true;
147  }
148 
149  unsigned getPointerSize() const {
150  StringRef Name = TheTarget.getName();
151  if (Name == "ppc64" || Name == "ppc64le") return 8;
152  assert(Name == "ppc32" && "Unknown target name!");
153  return 4;
154  }
155 };
156 } // end anonymous namespace
157 
158 
159 // FIXME: This should be in a separate file.
160 namespace {
161  class DarwinPPCAsmBackend : public PPCAsmBackend {
162  public:
163  DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) { }
164 
165  MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
166  bool is64 = getPointerSize() == 8;
168  OS,
169  /*Is64Bit=*/is64,
172  }
173 
174  virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
175  return false;
176  }
177  };
178 
179  class ELFPPCAsmBackend : public PPCAsmBackend {
180  uint8_t OSABI;
181  public:
182  ELFPPCAsmBackend(const Target &T, uint8_t OSABI) :
183  PPCAsmBackend(T), OSABI(OSABI) { }
184 
185 
186  MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
187  bool is64 = getPointerSize() == 8;
188  return createPPCELFObjectWriter(OS, is64, OSABI);
189  }
190 
191  virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
192  return false;
193  }
194  };
195 
196 } // end anonymous namespace
197 
199  const MCRegisterInfo &MRI,
200  StringRef TT, StringRef CPU) {
201  if (Triple(TT).isOSDarwin())
202  return new DarwinPPCAsmBackend(T);
203 
204  uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS());
205  return new ELFPPCAsmBackend(T, OSABI);
206 }
MCObjectWriter * createPPCELFObjectWriter(raw_ostream &OS, bool Is64Bit, uint8_t OSABI)
createPPCELFObjectWriter - Construct an PPC ELF object writer.
void Write32(uint32_t Value)
static uint64_t getPointerSize(const Value *V, AliasAnalysis &AA)
#define llvm_unreachable(msg)
A four-byte fixup.
Definition: MCFixup.h:25
static unsigned getFixupKindNumBytes(unsigned Kind)
uint32_t getOffset() const
Definition: MCFixup.h:90
void Write8(uint8_t Value)
MCAsmBackend * createPPCAsmBackend(const Target &T, const MCRegisterInfo &MRI, StringRef TT, StringRef CPU)
MCFixupKind
MCFixupKind - Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:22
MCFixupKind getKind() const
Definition: MCFixup.h:88
A one-byte fixup.
Definition: MCFixup.h:23
MCObjectWriter * createPPCMachObjectWriter(raw_ostream &OS, bool Is64Bit, uint32_t CPUType, uint32_t CPUSubtype)
createPPCELFObjectWriter - Construct a PPC Mach-O object writer.
void Write16(uint16_t Value)
A eight-byte fixup.
Definition: MCFixup.h:26
MCFixupKindInfo - Target independent information on a fixup kind.
LLVM Value Representation.
Definition: Value.h:66
MCAsmBackend - Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:34
static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value)
const MCRegisterInfo & MRI
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
getFixupKindInfo - Get information on a fixup kind.
A two-byte fixup.
Definition: MCFixup.h:24