LLVM  9.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/StringRef.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/BinaryFormat/ELF.h"
21 #include "llvm/MC/MCAssembler.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCDwarf.h"
24 #include "llvm/MC/MCELFStreamer.h"
25 #include "llvm/MC/MCExpr.h"
26 #include "llvm/MC/MCInstrInfo.h"
27 #include "llvm/MC/MCRegisterInfo.h"
28 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/MC/MCSymbolELF.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/CodeGen.h"
38 
39 using namespace llvm;
40 
41 #define GET_INSTRINFO_MC_DESC
42 #include "PPCGenInstrInfo.inc"
43 
44 #define GET_SUBTARGETINFO_MC_DESC
45 #include "PPCGenSubtargetInfo.inc"
46 
47 #define GET_REGINFO_MC_DESC
48 #include "PPCGenRegisterInfo.inc"
49 
50 // Pin the vtable to this file.
52 
54 
56  MCInstrInfo *X = new MCInstrInfo();
57  InitPPCMCInstrInfo(X);
58  return X;
59 }
60 
62  bool isPPC64 =
63  (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le);
64  unsigned Flavour = isPPC64 ? 0 : 1;
65  unsigned RA = isPPC64 ? PPC::LR8 : PPC::LR;
66 
68  InitPPCMCRegisterInfo(X, RA, Flavour, Flavour);
69  return X;
70 }
71 
73  StringRef CPU, StringRef FS) {
74  return createPPCMCSubtargetInfoImpl(TT, CPU, FS);
75 }
76 
78  const Triple &TheTriple) {
79  bool isPPC64 = (TheTriple.getArch() == Triple::ppc64 ||
80  TheTriple.getArch() == Triple::ppc64le);
81 
82  MCAsmInfo *MAI;
83  if (TheTriple.isOSDarwin())
84  MAI = new PPCMCAsmInfoDarwin(isPPC64, TheTriple);
85  else
86  MAI = new PPCELFMCAsmInfo(isPPC64, TheTriple);
87 
88  // Initial state of the frame pointer is R1.
89  unsigned Reg = isPPC64 ? PPC::X1 : PPC::R1;
90  MCCFIInstruction Inst =
91  MCCFIInstruction::createDefCfa(nullptr, MRI.getDwarfRegNum(Reg, true), 0);
92  MAI->addInitialFrameState(Inst);
93 
94  return MAI;
95 }
96 
97 namespace {
98 
99 class PPCTargetAsmStreamer : public PPCTargetStreamer {
101 
102 public:
103  PPCTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
104  : PPCTargetStreamer(S), OS(OS) {}
105 
106  void emitTCEntry(const MCSymbol &S) override {
107  OS << "\t.tc ";
108  OS << S.getName();
109  OS << "[TC],";
110  OS << S.getName();
111  OS << '\n';
112  }
113 
114  void emitMachine(StringRef CPU) override {
115  OS << "\t.machine " << CPU << '\n';
116  }
117 
118  void emitAbiVersion(int AbiVersion) override {
119  OS << "\t.abiversion " << AbiVersion << '\n';
120  }
121 
122  void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
123  const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
124 
125  OS << "\t.localentry\t";
126  S->print(OS, MAI);
127  OS << ", ";
128  LocalOffset->print(OS, MAI);
129  OS << '\n';
130  }
131 };
132 
133 class PPCTargetELFStreamer : public PPCTargetStreamer {
134 public:
135  PPCTargetELFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
136 
138  return static_cast<MCELFStreamer &>(Streamer);
139  }
140 
141  void emitTCEntry(const MCSymbol &S) override {
142  // Creates a R_PPC64_TOC relocation
144  Streamer.EmitSymbolValue(&S, 8);
145  }
146 
147  void emitMachine(StringRef CPU) override {
148  // FIXME: Is there anything to do in here or does this directive only
149  // limit the parser?
150  }
151 
152  void emitAbiVersion(int AbiVersion) override {
153  MCAssembler &MCA = getStreamer().getAssembler();
154  unsigned Flags = MCA.getELFHeaderEFlags();
155  Flags &= ~ELF::EF_PPC64_ABI;
156  Flags |= (AbiVersion & ELF::EF_PPC64_ABI);
157  MCA.setELFHeaderEFlags(Flags);
158  }
159 
160  void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
161  MCAssembler &MCA = getStreamer().getAssembler();
162 
163  int64_t Res;
164  if (!LocalOffset->evaluateAsAbsolute(Res, MCA))
165  report_fatal_error(".localentry expression must be absolute.");
166 
167  unsigned Encoded = ELF::encodePPC64LocalEntryOffset(Res);
168  if (Res != ELF::decodePPC64LocalEntryOffset(Encoded))
169  report_fatal_error(".localentry expression cannot be encoded.");
170 
171  unsigned Other = S->getOther();
172  Other &= ~ELF::STO_PPC64_LOCAL_MASK;
173  Other |= Encoded;
174  S->setOther(Other);
175 
176  // For GAS compatibility, unless we already saw a .abiversion directive,
177  // set e_flags to indicate ELFv2 ABI.
178  unsigned Flags = MCA.getELFHeaderEFlags();
179  if ((Flags & ELF::EF_PPC64_ABI) == 0)
180  MCA.setELFHeaderEFlags(Flags | 2);
181  }
182 
183  void emitAssignment(MCSymbol *S, const MCExpr *Value) override {
184  auto *Symbol = cast<MCSymbolELF>(S);
185  // When encoding an assignment to set symbol A to symbol B, also copy
186  // the st_other bits encoding the local entry point offset.
187  if (Value->getKind() != MCExpr::SymbolRef)
188  return;
189  const auto &RhsSym = cast<MCSymbolELF>(
190  static_cast<const MCSymbolRefExpr *>(Value)->getSymbol());
191  unsigned Other = Symbol->getOther();
192  Other &= ~ELF::STO_PPC64_LOCAL_MASK;
193  Other |= RhsSym.getOther() & ELF::STO_PPC64_LOCAL_MASK;
194  Symbol->setOther(Other);
195  }
196 };
197 
198 class PPCTargetMachOStreamer : public PPCTargetStreamer {
199 public:
200  PPCTargetMachOStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
201 
202  void emitTCEntry(const MCSymbol &S) override {
203  llvm_unreachable("Unknown pseudo-op: .tc");
204  }
205 
206  void emitMachine(StringRef CPU) override {
207  // FIXME: We should update the CPUType, CPUSubType in the Object file if
208  // the new values are different from the defaults.
209  }
210 
211  void emitAbiVersion(int AbiVersion) override {
212  llvm_unreachable("Unknown pseudo-op: .abiversion");
213  }
214 
215  void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
216  llvm_unreachable("Unknown pseudo-op: .localentry");
217  }
218 };
219 
220 } // end anonymous namespace
221 
224  MCInstPrinter *InstPrint,
225  bool isVerboseAsm) {
226  return new PPCTargetAsmStreamer(S, OS);
227 }
228 
229 static MCTargetStreamer *
231  const Triple &TT = STI.getTargetTriple();
232  if (TT.isOSBinFormatELF())
233  return new PPCTargetELFStreamer(S);
234  return new PPCTargetMachOStreamer(S);
235 }
236 
238  unsigned SyntaxVariant,
239  const MCAsmInfo &MAI,
240  const MCInstrInfo &MII,
241  const MCRegisterInfo &MRI) {
242  return new PPCInstPrinter(MAI, MII, MRI, T);
243 }
244 
245 extern "C" void LLVMInitializePowerPCTargetMC() {
246  for (Target *T :
248  // Register the MC asm info.
250 
251  // Register the MC instruction info.
253 
254  // Register the MC register info.
256 
257  // Register the MC subtarget info.
259 
260  // Register the MC Code Emitter
262 
263  // Register the asm backend.
265 
266  // Register the object target streamer.
269 
270  // Register the asm target streamer.
272 
273  // Register the MCInstPrinter.
275  }
276 }
const MCAsmInfo * getAsmInfo() const
Definition: MCContext.h:292
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:477
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:163
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:615
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:83
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)
const Triple & getTargetTriple() const
SI optimize exec mask operations pre RA
MCContext & getContext() const
Definition: MCStreamer.h:250
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
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:165
int getDwarfRegNum(unsigned RegNum, bool isEH) const
Map a target register to an equivalent dwarf register number.
static unsigned encodePPC64LocalEntryOffset(int64_t Offset)
Definition: ELF.h:396
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:851
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:292
virtual void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset)=0
Target & getThePPC64Target()
unsigned getOther() const
void LLVMInitializePowerPCTargetMC()
Expected< const typename ELFT::Sym * > getSymbol(typename ELFT::SymRange Symbols, uint32_t Index)
Definition: ELF.h:334
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:55
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:188
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:41
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:477
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:91
void addInitialFrameState(const MCCFIInstruction &Inst)
Definition: MCAsmInfo.cpp:73
ExprKind getKind() const
Definition: MCExpr.h:72
static void RegisterMCCodeEmitter(Target &T, Target::MCCodeEmitterCtorTy Fn)
RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the given target.
MCStreamer & Streamer
Definition: MCStreamer.h:85
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()
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)
References to labels and assigned expressions.
Definition: MCExpr.h:40
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:202
PPCTargetStreamer(MCStreamer &S)
RegisterMCAsmInfoFn - Helper template for registering a target assembly info implementation.
LLVM Value Representation.
Definition: Value.h:72
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:86
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