LLVM  12.0.0git
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 "PPCELFStreamer.h"
17 #include "PPCTargetStreamer.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/BinaryFormat/ELF.h"
23 #include "llvm/MC/MCAssembler.h"
24 #include "llvm/MC/MCAsmBackend.h"
25 #include "llvm/MC/MCCodeEmitter.h"
26 #include "llvm/MC/MCContext.h"
27 #include "llvm/MC/MCDwarf.h"
28 #include "llvm/MC/MCELFStreamer.h"
29 #include "llvm/MC/MCExpr.h"
30 #include "llvm/MC/MCInstrInfo.h"
31 #include "llvm/MC/MCObjectWriter.h"
32 #include "llvm/MC/MCRegisterInfo.h"
33 #include "llvm/MC/MCStreamer.h"
35 #include "llvm/MC/MCSymbol.h"
36 #include "llvm/MC/MCSymbolELF.h"
37 #include "llvm/MC/MCSymbolXCOFF.h"
38 #include "llvm/Support/Casting.h"
39 #include "llvm/Support/CodeGen.h"
44 
45 using namespace llvm;
46 
47 #define GET_INSTRINFO_MC_DESC
48 #include "PPCGenInstrInfo.inc"
49 
50 #define GET_SUBTARGETINFO_MC_DESC
51 #include "PPCGenSubtargetInfo.inc"
52 
53 #define GET_REGINFO_MC_DESC
54 #include "PPCGenRegisterInfo.inc"
55 
57 
58 // Pin the vtable to this file.
60 
62  MCInstrInfo *X = new MCInstrInfo();
63  InitPPCMCInstrInfo(X);
64  return X;
65 }
66 
68  bool isPPC64 =
69  (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le);
70  unsigned Flavour = isPPC64 ? 0 : 1;
71  unsigned RA = isPPC64 ? PPC::LR8 : PPC::LR;
72 
74  InitPPCMCRegisterInfo(X, RA, Flavour, Flavour);
75  return X;
76 }
77 
79  StringRef CPU, StringRef FS) {
80  return createPPCMCSubtargetInfoImpl(TT, CPU, FS);
81 }
82 
84  const Triple &TheTriple,
85  const MCTargetOptions &Options) {
86  bool isPPC64 = (TheTriple.getArch() == Triple::ppc64 ||
87  TheTriple.getArch() == Triple::ppc64le);
88 
89  MCAsmInfo *MAI;
90  if (TheTriple.isOSBinFormatXCOFF())
91  MAI = new PPCXCOFFMCAsmInfo(isPPC64, TheTriple);
92  else
93  MAI = new PPCELFMCAsmInfo(isPPC64, TheTriple);
94 
95  // Initial state of the frame pointer is R1.
96  unsigned Reg = isPPC64 ? PPC::X1 : PPC::R1;
97  MCCFIInstruction Inst =
98  MCCFIInstruction::cfiDefCfa(nullptr, MRI.getDwarfRegNum(Reg, true), 0);
99  MAI->addInitialFrameState(Inst);
100 
101  return MAI;
102 }
103 
105  std::unique_ptr<MCAsmBackend> &&MAB,
106  std::unique_ptr<MCObjectWriter> &&OW,
107  std::unique_ptr<MCCodeEmitter> &&Emitter,
108  bool RelaxAll) {
109  return createPPCELFStreamer(Context, std::move(MAB), std::move(OW),
110  std::move(Emitter));
111 }
112 
113 namespace {
114 
115 class PPCTargetAsmStreamer : public PPCTargetStreamer {
117 
118 public:
119  PPCTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
120  : PPCTargetStreamer(S), OS(OS) {}
121 
122  void emitTCEntry(const MCSymbol &S) override {
123  if (const MCSymbolXCOFF *XSym = dyn_cast<MCSymbolXCOFF>(&S)) {
124  MCSymbolXCOFF *TCSym =
125  cast<MCSymbolXCOFF>(Streamer.getContext().getOrCreateSymbol(
126  XSym->getSymbolTableName() + "[TC]"));
127  if (TCSym->hasRename())
129  OS << "\t.tc " << TCSym->getName() << "," << XSym->getName() << '\n';
130  return;
131  }
132 
133  OS << "\t.tc " << S.getName() << "[TC]," << S.getName() << '\n';
134  }
135 
136  void emitMachine(StringRef CPU) override {
137  OS << "\t.machine " << CPU << '\n';
138  }
139 
140  void emitAbiVersion(int AbiVersion) override {
141  OS << "\t.abiversion " << AbiVersion << '\n';
142  }
143 
144  void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
145  const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
146 
147  OS << "\t.localentry\t";
148  S->print(OS, MAI);
149  OS << ", ";
150  LocalOffset->print(OS, MAI);
151  OS << '\n';
152  }
153 };
154 
155 class PPCTargetELFStreamer : public PPCTargetStreamer {
156 public:
157  PPCTargetELFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
158 
160  return static_cast<MCELFStreamer &>(Streamer);
161  }
162 
163  void emitTCEntry(const MCSymbol &S) override {
164  // Creates a R_PPC64_TOC relocation
166  Streamer.emitSymbolValue(&S, 8);
167  }
168 
169  void emitMachine(StringRef CPU) override {
170  // FIXME: Is there anything to do in here or does this directive only
171  // limit the parser?
172  }
173 
174  void emitAbiVersion(int AbiVersion) override {
175  MCAssembler &MCA = getStreamer().getAssembler();
176  unsigned Flags = MCA.getELFHeaderEFlags();
177  Flags &= ~ELF::EF_PPC64_ABI;
178  Flags |= (AbiVersion & ELF::EF_PPC64_ABI);
179  MCA.setELFHeaderEFlags(Flags);
180  }
181 
182  void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
183  MCAssembler &MCA = getStreamer().getAssembler();
184 
185  // encodePPC64LocalEntryOffset will report an error if it cannot
186  // encode LocalOffset.
187  unsigned Encoded = encodePPC64LocalEntryOffset(LocalOffset);
188 
189  unsigned Other = S->getOther();
190  Other &= ~ELF::STO_PPC64_LOCAL_MASK;
191  Other |= Encoded;
192  S->setOther(Other);
193 
194  // For GAS compatibility, unless we already saw a .abiversion directive,
195  // set e_flags to indicate ELFv2 ABI.
196  unsigned Flags = MCA.getELFHeaderEFlags();
197  if ((Flags & ELF::EF_PPC64_ABI) == 0)
198  MCA.setELFHeaderEFlags(Flags | 2);
199  }
200 
201  void emitAssignment(MCSymbol *S, const MCExpr *Value) override {
202  auto *Symbol = cast<MCSymbolELF>(S);
203 
204  // When encoding an assignment to set symbol A to symbol B, also copy
205  // the st_other bits encoding the local entry point offset.
206  if (copyLocalEntry(Symbol, Value))
207  UpdateOther.insert(Symbol);
208  else
209  UpdateOther.erase(Symbol);
210  }
211 
212  void finish() override {
213  for (auto *Sym : UpdateOther)
214  if (Sym->isVariable())
215  copyLocalEntry(Sym, Sym->getVariableValue());
216 
217  // Clear the set of symbols that needs to be updated so the streamer can
218  // be reused without issues.
219  UpdateOther.clear();
220  }
221 
222 private:
223  SmallPtrSet<MCSymbolELF *, 32> UpdateOther;
224 
225  bool copyLocalEntry(MCSymbolELF *D, const MCExpr *S) {
226  auto *Ref = dyn_cast<const MCSymbolRefExpr>(S);
227  if (!Ref)
228  return false;
229  const auto &RhsSym = cast<MCSymbolELF>(Ref->getSymbol());
230  unsigned Other = D->getOther();
231  Other &= ~ELF::STO_PPC64_LOCAL_MASK;
232  Other |= RhsSym.getOther() & ELF::STO_PPC64_LOCAL_MASK;
233  D->setOther(Other);
234  return true;
235  }
236 
237  unsigned encodePPC64LocalEntryOffset(const MCExpr *LocalOffset) {
238  MCAssembler &MCA = getStreamer().getAssembler();
239  int64_t Offset;
240  if (!LocalOffset->evaluateAsAbsolute(Offset, MCA))
242  LocalOffset->getLoc(), ".localentry expression must be absolute.");
243 
244  switch (Offset) {
245  default:
247  LocalOffset->getLoc(),
248  ".localentry expression is not a valid power of 2.");
249  case 0:
250  return 0;
251  case 1:
252  return 1 << ELF::STO_PPC64_LOCAL_BIT;
253  case 4:
254  case 8:
255  case 16:
256  case 32:
257  case 64:
258  return (int)Log2(Offset) << (int)ELF::STO_PPC64_LOCAL_BIT;
259  }
260  }
261 };
262 
263 class PPCTargetMachOStreamer : public PPCTargetStreamer {
264 public:
265  PPCTargetMachOStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
266 
267  void emitTCEntry(const MCSymbol &S) override {
268  llvm_unreachable("Unknown pseudo-op: .tc");
269  }
270 
271  void emitMachine(StringRef CPU) override {
272  // FIXME: We should update the CPUType, CPUSubType in the Object file if
273  // the new values are different from the defaults.
274  }
275 
276  void emitAbiVersion(int AbiVersion) override {
277  llvm_unreachable("Unknown pseudo-op: .abiversion");
278  }
279 
280  void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
281  llvm_unreachable("Unknown pseudo-op: .localentry");
282  }
283 };
284 
285 class PPCTargetXCOFFStreamer : public PPCTargetStreamer {
286 public:
287  PPCTargetXCOFFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
288 
289  void emitTCEntry(const MCSymbol &S) override {
290  const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
291  const unsigned PointerSize = MAI->getCodePointerSize();
292  Streamer.emitValueToAlignment(PointerSize);
293  Streamer.emitSymbolValue(&S, PointerSize);
294  }
295 
296  void emitMachine(StringRef CPU) override {
297  llvm_unreachable("Machine pseudo-ops are invalid for XCOFF.");
298  }
299 
300  void emitAbiVersion(int AbiVersion) override {
301  llvm_unreachable("ABI-version pseudo-ops are invalid for XCOFF.");
302  }
303 
304  void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
305  llvm_unreachable("Local-entry pseudo-ops are invalid for XCOFF.");
306  }
307 };
308 
309 } // end anonymous namespace
310 
313  MCInstPrinter *InstPrint,
314  bool isVerboseAsm) {
315  return new PPCTargetAsmStreamer(S, OS);
316 }
317 
318 static MCTargetStreamer *
320  const Triple &TT = STI.getTargetTriple();
321  if (TT.isOSBinFormatELF())
322  return new PPCTargetELFStreamer(S);
323  if (TT.isOSBinFormatXCOFF())
324  return new PPCTargetXCOFFStreamer(S);
325  return new PPCTargetMachOStreamer(S);
326 }
327 
329  unsigned SyntaxVariant,
330  const MCAsmInfo &MAI,
331  const MCInstrInfo &MII,
332  const MCRegisterInfo &MRI) {
333  return new PPCInstPrinter(MAI, MII, MRI, T);
334 }
335 
337  for (Target *T :
339  // Register the MC asm info.
341 
342  // Register the MC instruction info.
344 
345  // Register the MC register info.
347 
348  // Register the MC subtarget info.
350 
351  // Register the MC Code Emitter
353 
354  // Register the asm backend.
356 
357  // Register the elf streamer.
359 
360  // Register the object target streamer.
363 
364  // Register the asm target streamer.
366 
367  // Register the MCInstPrinter.
369  }
370 }
const MCAsmInfo * getAsmInfo() const
Definition: MCContext.h:363
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition: Alignment.h:217
uint64_t CallInst * C
void setELFHeaderEFlags(unsigned Flags)
Definition: MCAssembler.h:255
bool hasRename() const
Definition: MCSymbolXCOFF.h:61
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
LLVM_NODISCARD std::enable_if_t< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > dyn_cast(const Y &Val)
Definition: Casting.h:334
LLVM_EXTERNAL_VISIBILITY void LLVMInitializePowerPCTargetMC()
LLVMContext & Context
This class represents lattice values for constants.
Definition: AllocatorList.h:23
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
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:164
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:617
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
static MCCFIInstruction cfiDefCfa(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:485
Target specific streamer interface.
Definition: MCStreamer.h:90
unsigned Reg
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)
SMLoc getLoc() const
Definition: MCExpr.h:82
const Triple & getTargetTriple() const
SI optimize exec mask operations pre RA
MCContext & getContext() const
Definition: MCAssembler.h:284
MCContext & getContext() const
Definition: MCStreamer.h:265
~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:192
Context object for machine code objects.
Definition: MCContext.h:67
int getDwarfRegNum(MCRegister RegNum, bool isEH) const
Map a target register to an equivalent dwarf register number.
virtual void finish()
Definition: MCStreamer.cpp:53
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:300
virtual void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset)=0
StringRef getSymbolTableName() const
Definition: MCSymbolXCOFF.h:65
Target & getThePPC64Target()
unsigned getOther() const
LLVM_ATTRIBUTE_NORETURN void reportFatalError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:840
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:196
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:42
unsigned const MachineRegisterInfo * MRI
void setOther(unsigned Other)
virtual void emitAbiVersion(int AbiVersion)=0
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:25
MCAsmBackend * createPPCAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
static MCStreamer * createPPCMCStreamer(const Triple &T, MCContext &Context, std::unique_ptr< MCAsmBackend > &&MAB, std::unique_ptr< MCObjectWriter > &&OW, std::unique_ptr< MCCodeEmitter > &&Emitter, bool RelaxAll)
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:45
MCStreamer & getStreamer()
Definition: MCStreamer.h:98
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:439
static void RegisterMCCodeEmitter(Target &T, Target::MCCodeEmitterCtorTy Fn)
RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the given target.
MCStreamer & Streamer
Definition: MCStreamer.h:92
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:131
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:637
static MCAsmInfo * createPPCMCAsmInfo(const MCRegisterInfo &MRI, const Triple &TheTriple, const MCTargetOptions &Options)
static MCRegisterInfo * createPPCMCRegisterInfo(const Triple &TT)
virtual void emitXCOFFRenameDirective(const MCSymbol *Name, StringRef Rename)
Emit a XCOFF .rename directive which creates a synonym for an illegal or undesirable name...
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:42
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.
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:133
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)
static void RegisterELFStreamer(Target &T, Target::ELFStreamerCtorTy Fn)
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:196
unsigned getCodePointerSize() const
Get the code pointer size in bytes.
Definition: MCAsmInfo.h:417
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:57
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:89
MCCodeEmitter * createPPCMCCodeEmitter(const MCInstrInfo &MCII, const MCRegisterInfo &MRI, MCContext &Ctx)
MCELFStreamer * createPPCELFStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > MAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter)
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:59