LLVM  10.0.0svn
PPCMCTargetDesc.cpp
Go to the documentation of this file.
1 //===-- PPCMCTargetDesc.cpp - PowerPC Target Descriptions -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file provides PowerPC specific target descriptions.
10 //
11 //===----------------------------------------------------------------------===//
12 
16 #include "PPCTargetStreamer.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/BinaryFormat/ELF.h"
22 #include "llvm/MC/MCAssembler.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCDwarf.h"
25 #include "llvm/MC/MCELFStreamer.h"
26 #include "llvm/MC/MCExpr.h"
27 #include "llvm/MC/MCInstrInfo.h"
28 #include "llvm/MC/MCRegisterInfo.h"
29 #include "llvm/MC/MCStreamer.h"
31 #include "llvm/MC/MCSymbol.h"
32 #include "llvm/MC/MCSymbolELF.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/CodeGen.h"
39 
40 using namespace llvm;
41 
42 #define GET_INSTRINFO_MC_DESC
43 #include "PPCGenInstrInfo.inc"
44 
45 #define GET_SUBTARGETINFO_MC_DESC
46 #include "PPCGenSubtargetInfo.inc"
47 
48 #define GET_REGINFO_MC_DESC
49 #include "PPCGenRegisterInfo.inc"
50 
52 
53 // Pin the vtable to this file.
55 
57  MCInstrInfo *X = new MCInstrInfo();
58  InitPPCMCInstrInfo(X);
59  return X;
60 }
61 
63  bool isPPC64 =
64  (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le);
65  unsigned Flavour = isPPC64 ? 0 : 1;
66  unsigned RA = isPPC64 ? PPC::LR8 : PPC::LR;
67 
69  InitPPCMCRegisterInfo(X, RA, Flavour, Flavour);
70  return X;
71 }
72 
74  StringRef CPU, StringRef FS) {
75  return createPPCMCSubtargetInfoImpl(TT, CPU, FS);
76 }
77 
79  const Triple &TheTriple) {
80  bool isPPC64 = (TheTriple.getArch() == Triple::ppc64 ||
81  TheTriple.getArch() == Triple::ppc64le);
82 
83  MCAsmInfo *MAI;
84  if (TheTriple.isOSDarwin())
85  MAI = new PPCMCAsmInfoDarwin(isPPC64, TheTriple);
86  else if (TheTriple.isOSBinFormatXCOFF())
87  MAI = new PPCXCOFFMCAsmInfo(isPPC64, TheTriple);
88  else
89  MAI = new PPCELFMCAsmInfo(isPPC64, TheTriple);
90 
91  // Initial state of the frame pointer is R1.
92  unsigned Reg = isPPC64 ? PPC::X1 : PPC::R1;
93  MCCFIInstruction Inst =
94  MCCFIInstruction::createDefCfa(nullptr, MRI.getDwarfRegNum(Reg, true), 0);
95  MAI->addInitialFrameState(Inst);
96 
97  return MAI;
98 }
99 
100 namespace {
101 
102 class PPCTargetAsmStreamer : public PPCTargetStreamer {
104 
105 public:
106  PPCTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
107  : PPCTargetStreamer(S), OS(OS) {}
108 
109  void emitTCEntry(const MCSymbol &S) override {
110  OS << "\t.tc ";
111  OS << S.getName();
112  OS << "[TC],";
113  OS << S.getName();
114  OS << '\n';
115  }
116 
117  void emitMachine(StringRef CPU) override {
118  OS << "\t.machine " << CPU << '\n';
119  }
120 
121  void emitAbiVersion(int AbiVersion) override {
122  OS << "\t.abiversion " << AbiVersion << '\n';
123  }
124 
125  void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
126  const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
127 
128  OS << "\t.localentry\t";
129  S->print(OS, MAI);
130  OS << ", ";
131  LocalOffset->print(OS, MAI);
132  OS << '\n';
133  }
134 };
135 
136 class PPCTargetELFStreamer : public PPCTargetStreamer {
137 public:
138  PPCTargetELFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
139 
141  return static_cast<MCELFStreamer &>(Streamer);
142  }
143 
144  void emitTCEntry(const MCSymbol &S) override {
145  // Creates a R_PPC64_TOC relocation
147  Streamer.EmitSymbolValue(&S, 8);
148  }
149 
150  void emitMachine(StringRef CPU) override {
151  // FIXME: Is there anything to do in here or does this directive only
152  // limit the parser?
153  }
154 
155  void emitAbiVersion(int AbiVersion) override {
156  MCAssembler &MCA = getStreamer().getAssembler();
157  unsigned Flags = MCA.getELFHeaderEFlags();
158  Flags &= ~ELF::EF_PPC64_ABI;
159  Flags |= (AbiVersion & ELF::EF_PPC64_ABI);
160  MCA.setELFHeaderEFlags(Flags);
161  }
162 
163  void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
164  MCAssembler &MCA = getStreamer().getAssembler();
165 
166  int64_t Res;
167  if (!LocalOffset->evaluateAsAbsolute(Res, MCA))
168  report_fatal_error(".localentry expression must be absolute.");
169 
170  unsigned Encoded = ELF::encodePPC64LocalEntryOffset(Res);
171  if (Res != ELF::decodePPC64LocalEntryOffset(Encoded))
172  report_fatal_error(".localentry expression cannot be encoded.");
173 
174  unsigned Other = S->getOther();
175  Other &= ~ELF::STO_PPC64_LOCAL_MASK;
176  Other |= Encoded;
177  S->setOther(Other);
178 
179  // For GAS compatibility, unless we already saw a .abiversion directive,
180  // set e_flags to indicate ELFv2 ABI.
181  unsigned Flags = MCA.getELFHeaderEFlags();
182  if ((Flags & ELF::EF_PPC64_ABI) == 0)
183  MCA.setELFHeaderEFlags(Flags | 2);
184  }
185 
186  void emitAssignment(MCSymbol *S, const MCExpr *Value) override {
187  auto *Symbol = cast<MCSymbolELF>(S);
188 
189  // When encoding an assignment to set symbol A to symbol B, also copy
190  // the st_other bits encoding the local entry point offset.
191  if (copyLocalEntry(Symbol, Value))
192  UpdateOther.insert(Symbol);
193  else
194  UpdateOther.erase(Symbol);
195  }
196 
197  void finish() override {
198  for (auto *Sym : UpdateOther)
199  copyLocalEntry(Sym, Sym->getVariableValue());
200  }
201 
202 private:
203  SmallPtrSet<MCSymbolELF *, 32> UpdateOther;
204 
205  bool copyLocalEntry(MCSymbolELF *D, const MCExpr *S) {
206  auto *Ref = dyn_cast<const MCSymbolRefExpr>(S);
207  if (!Ref)
208  return false;
209  const auto &RhsSym = cast<MCSymbolELF>(Ref->getSymbol());
210  unsigned Other = D->getOther();
211  Other &= ~ELF::STO_PPC64_LOCAL_MASK;
212  Other |= RhsSym.getOther() & ELF::STO_PPC64_LOCAL_MASK;
213  D->setOther(Other);
214  return true;
215  }
216 };
217 
218 class PPCTargetMachOStreamer : public PPCTargetStreamer {
219 public:
220  PPCTargetMachOStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
221 
222  void emitTCEntry(const MCSymbol &S) override {
223  llvm_unreachable("Unknown pseudo-op: .tc");
224  }
225 
226  void emitMachine(StringRef CPU) override {
227  // FIXME: We should update the CPUType, CPUSubType in the Object file if
228  // the new values are different from the defaults.
229  }
230 
231  void emitAbiVersion(int AbiVersion) override {
232  llvm_unreachable("Unknown pseudo-op: .abiversion");
233  }
234 
235  void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
236  llvm_unreachable("Unknown pseudo-op: .localentry");
237  }
238 };
239 
240 class PPCTargetXCOFFStreamer : public PPCTargetStreamer {
241 public:
242  PPCTargetXCOFFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
243 
244  void emitTCEntry(const MCSymbol &S) override {
245  report_fatal_error("TOC entries not supported yet.");
246  }
247 
248  void emitMachine(StringRef CPU) override {
249  llvm_unreachable("Machine pseudo-ops are invalid for XCOFF.");
250  }
251 
252  void emitAbiVersion(int AbiVersion) override {
253  llvm_unreachable("ABI-version pseudo-ops are invalid for XCOFF.");
254  }
255 
256  void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
257  llvm_unreachable("Local-entry pseudo-ops are invalid for XCOFF.");
258  }
259 };
260 
261 } // end anonymous namespace
262 
265  MCInstPrinter *InstPrint,
266  bool isVerboseAsm) {
267  return new PPCTargetAsmStreamer(S, OS);
268 }
269 
270 static MCTargetStreamer *
272  const Triple &TT = STI.getTargetTriple();
273  if (TT.isOSBinFormatELF())
274  return new PPCTargetELFStreamer(S);
275  if (TT.isOSBinFormatXCOFF())
276  return new PPCTargetXCOFFStreamer(S);
277  return new PPCTargetMachOStreamer(S);
278 }
279 
281  unsigned SyntaxVariant,
282  const MCAsmInfo &MAI,
283  const MCInstrInfo &MII,
284  const MCRegisterInfo &MRI) {
285  return new PPCInstPrinter(MAI, MII, MRI, T);
286 }
287 
288 extern "C" void LLVMInitializePowerPCTargetMC() {
289  for (Target *T :
291  // Register the MC asm info.
293 
294  // Register the MC instruction info.
296 
297  // Register the MC register info.
299 
300  // Register the MC subtarget info.
302 
303  // Register the MC Code Emitter
305 
306  // Register the asm backend.
308 
309  // Register the object target streamer.
312 
313  // Register the asm target streamer.
315 
316  // Register the MCInstPrinter.
318  }
319 }
const MCAsmInfo * getAsmInfo() const
Definition: MCContext.h:318
uint64_t CallInst * C
void setELFHeaderEFlags(unsigned Flags)
Definition: MCAssembler.h:255
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
Definition: Triple.h:481
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void EmitSymbolValue(const MCSymbol *Sym, unsigned Size, bool IsSectionRelative=false)
Special case of EmitValue that avoids the client having to pass in a MCExpr for MCSymbols.
Definition: MCStreamer.cpp:165
static MCAsmInfo * createPPCMCAsmInfo(const MCRegisterInfo &MRI, const Triple &TheTriple)
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:623
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
Target specific streamer interface.
Definition: MCStreamer.h:85
unsigned Reg
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:953
Target & getThePPC32Target()
static void RegisterMCInstPrinter(Target &T, Target::MCInstPrinterCtorTy Fn)
RegisterMCInstPrinter - Register a MCInstPrinter implementation for the given target.
static void RegisterAsmTargetStreamer(Target &T, Target::AsmTargetStreamerCtorTy Fn)
const Triple & getTargetTriple() const
SI optimize exec mask operations pre RA
MCContext & getContext() const
Definition: MCStreamer.h:252
static int64_t decodePPC64LocalEntryOffset(unsigned Other)
Definition: ELF.h:392
~PPCTargetStreamer() override
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
The access may reference the value stored in memory.
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:169
static unsigned encodePPC64LocalEntryOffset(int64_t Offset)
Definition: ELF.h:396
int getDwarfRegNum(MCRegister RegNum, bool isEH) const
Map a target register to an equivalent dwarf register number.
virtual void finish()
Definition: MCStreamer.cpp:52
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:296
virtual void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset)=0
Target & getThePPC64Target()
unsigned getOther() const
void LLVMInitializePowerPCTargetMC()
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn)
RegisterMCAsmBackend - Register a MCAsmBackend implementation for the given target.
Streaming machine code generation interface.
Definition: MCStreamer.h:190
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:42
unsigned const MachineRegisterInfo * MRI
static MCCFIInstruction createDefCfa(MCSymbol *L, unsigned Register, int Offset)
.cfi_def_cfa defines a rule for computing CFA as: take address from Register and add Offset to it...
Definition: MCDwarf.h:480
void setOther(unsigned Other)
virtual void emitAbiVersion(int AbiVersion)=0
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:23
virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value=0, unsigned ValueSize=1, unsigned MaxBytesToEmit=0)
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
MCAsmBackend * createPPCAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
static MCInstPrinter * createPPCMCInstPrinter(const Triple &T, unsigned SyntaxVariant, const MCAsmInfo &MAI, const MCInstrInfo &MII, const MCRegisterInfo &MRI)
unsigned getELFHeaderEFlags() const
ELF e_header flags.
Definition: MCAssembler.h:254
static void RegisterMCSubtargetInfo(Target &T, Target::MCSubtargetInfoCtorFnTy Fn)
RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for the given target...
static void RegisterObjectTargetStreamer(Target &T, Target::ObjectTargetStreamerCtorTy Fn)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static MCInstrInfo * createPPCMCInstrInfo()
static MCSubtargetInfo * createPPCMCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS)
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
MCStreamer & getStreamer()
Definition: MCStreamer.h:93
void addInitialFrameState(const MCCFIInstruction &Inst)
Definition: MCAsmInfo.cpp:73
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:417
static void RegisterMCCodeEmitter(Target &T, Target::MCCodeEmitterCtorTy Fn)
RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the given target.
MCStreamer & Streamer
Definition: MCStreamer.h:87
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn)
RegisterMCRegInfo - Register a MCRegisterInfo implementation for the given target.
Target - Wrapper for Target specific information.
Target & getThePPC64LETarget()
bool isOSBinFormatXCOFF() const
Tests whether the OS uses the XCOFF binary format.
Definition: Triple.h:643
static MCRegisterInfo * createPPCMCRegisterInfo(const Triple &TT)
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:39
static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn)
RegisterMCInstrInfo - Register a MCInstrInfo implementation for the given target. ...
Generic base class for all target subtargets.
static MCTargetStreamer * createAsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint, bool isVerboseAsm)
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:204
PPCTargetStreamer(MCStreamer &S)
RegisterMCAsmInfoFn - Helper template for registering a target assembly info implementation.
LLVM Value Representation.
Definition: Value.h:74
virtual void emitTCEntry(const MCSymbol &S)=0
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
virtual void emitMachine(StringRef CPU)=0
static MCTargetStreamer * createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI)
virtual void emitAssignment(MCSymbol *Symbol, const MCExpr *Value)
Definition: MCStreamer.cpp:88
MCCodeEmitter * createPPCMCCodeEmitter(const MCInstrInfo &MCII, const MCRegisterInfo &MRI, MCContext &Ctx)
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:59