LLVM  5.0.0
PPCMCTargetDesc.cpp
Go to the documentation of this file.
1 //===-- PPCMCTargetDesc.cpp - PowerPC Target Descriptions -----------------===//
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 //
10 // This file provides PowerPC specific target descriptions.
11 //
12 //===----------------------------------------------------------------------===//
13 
17 #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 static void adjustCodeGenOpts(const Triple &TT, Reloc::Model RM,
98  CodeModel::Model &CM) {
99  if (CM == CodeModel::Default) {
100  if (!TT.isOSDarwin() &&
101  (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le))
102  CM = CodeModel::Medium;
103  }
104 }
105 
106 namespace {
107 
108 class PPCTargetAsmStreamer : public PPCTargetStreamer {
110 
111 public:
112  PPCTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
113  : PPCTargetStreamer(S), OS(OS) {}
114 
115  void emitTCEntry(const MCSymbol &S) override {
116  OS << "\t.tc ";
117  OS << S.getName();
118  OS << "[TC],";
119  OS << S.getName();
120  OS << '\n';
121  }
122 
123  void emitMachine(StringRef CPU) override {
124  OS << "\t.machine " << CPU << '\n';
125  }
126 
127  void emitAbiVersion(int AbiVersion) override {
128  OS << "\t.abiversion " << AbiVersion << '\n';
129  }
130 
131  void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
132  const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
133 
134  OS << "\t.localentry\t";
135  S->print(OS, MAI);
136  OS << ", ";
137  LocalOffset->print(OS, MAI);
138  OS << '\n';
139  }
140 };
141 
142 class PPCTargetELFStreamer : public PPCTargetStreamer {
143 public:
144  PPCTargetELFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
145 
146  MCELFStreamer &getStreamer() {
147  return static_cast<MCELFStreamer &>(Streamer);
148  }
149 
150  void emitTCEntry(const MCSymbol &S) override {
151  // Creates a R_PPC64_TOC relocation
152  Streamer.EmitValueToAlignment(8);
153  Streamer.EmitSymbolValue(&S, 8);
154  }
155 
156  void emitMachine(StringRef CPU) override {
157  // FIXME: Is there anything to do in here or does this directive only
158  // limit the parser?
159  }
160 
161  void emitAbiVersion(int AbiVersion) override {
162  MCAssembler &MCA = getStreamer().getAssembler();
163  unsigned Flags = MCA.getELFHeaderEFlags();
164  Flags &= ~ELF::EF_PPC64_ABI;
165  Flags |= (AbiVersion & ELF::EF_PPC64_ABI);
166  MCA.setELFHeaderEFlags(Flags);
167  }
168 
169  void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
170  MCAssembler &MCA = getStreamer().getAssembler();
171 
172  int64_t Res;
173  if (!LocalOffset->evaluateAsAbsolute(Res, MCA))
174  report_fatal_error(".localentry expression must be absolute.");
175 
176  unsigned Encoded = ELF::encodePPC64LocalEntryOffset(Res);
177  if (Res != ELF::decodePPC64LocalEntryOffset(Encoded))
178  report_fatal_error(".localentry expression cannot be encoded.");
179 
180  unsigned Other = S->getOther();
181  Other &= ~ELF::STO_PPC64_LOCAL_MASK;
182  Other |= Encoded;
183  S->setOther(Other);
184 
185  // For GAS compatibility, unless we already saw a .abiversion directive,
186  // set e_flags to indicate ELFv2 ABI.
187  unsigned Flags = MCA.getELFHeaderEFlags();
188  if ((Flags & ELF::EF_PPC64_ABI) == 0)
189  MCA.setELFHeaderEFlags(Flags | 2);
190  }
191 
192  void emitAssignment(MCSymbol *S, const MCExpr *Value) override {
193  auto *Symbol = cast<MCSymbolELF>(S);
194  // When encoding an assignment to set symbol A to symbol B, also copy
195  // the st_other bits encoding the local entry point offset.
196  if (Value->getKind() != MCExpr::SymbolRef)
197  return;
198  const auto &RhsSym = cast<MCSymbolELF>(
199  static_cast<const MCSymbolRefExpr *>(Value)->getSymbol());
200  unsigned Other = Symbol->getOther();
201  Other &= ~ELF::STO_PPC64_LOCAL_MASK;
202  Other |= RhsSym.getOther() & ELF::STO_PPC64_LOCAL_MASK;
203  Symbol->setOther(Other);
204  }
205 };
206 
207 class PPCTargetMachOStreamer : public PPCTargetStreamer {
208 public:
209  PPCTargetMachOStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
210 
211  void emitTCEntry(const MCSymbol &S) override {
212  llvm_unreachable("Unknown pseudo-op: .tc");
213  }
214 
215  void emitMachine(StringRef CPU) override {
216  // FIXME: We should update the CPUType, CPUSubType in the Object file if
217  // the new values are different from the defaults.
218  }
219 
220  void emitAbiVersion(int AbiVersion) override {
221  llvm_unreachable("Unknown pseudo-op: .abiversion");
222  }
223 
224  void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
225  llvm_unreachable("Unknown pseudo-op: .localentry");
226  }
227 };
228 
229 } // end anonymous namespace
230 
233  MCInstPrinter *InstPrint,
234  bool isVerboseAsm) {
235  return new PPCTargetAsmStreamer(S, OS);
236 }
237 
238 static MCTargetStreamer *
240  const Triple &TT = STI.getTargetTriple();
241  if (TT.isOSBinFormatELF())
242  return new PPCTargetELFStreamer(S);
243  return new PPCTargetMachOStreamer(S);
244 }
245 
247  unsigned SyntaxVariant,
248  const MCAsmInfo &MAI,
249  const MCInstrInfo &MII,
250  const MCRegisterInfo &MRI) {
251  return new PPCInstPrinter(MAI, MII, MRI, T.isOSDarwin());
252 }
253 
254 extern "C" void LLVMInitializePowerPCTargetMC() {
255  for (Target *T :
257  // Register the MC asm info.
259 
260  // Register the MC codegen info.
262 
263  // Register the MC instruction info.
265 
266  // Register the MC register info.
268 
269  // Register the MC subtarget info.
271 
272  // Register the MC Code Emitter
274 
275  // Register the asm backend.
277 
278  // Register the object target streamer.
281 
282  // Register the asm target streamer.
284 
285  // Register the MCInstPrinter.
287  }
288 }
MCAsmBackend * createPPCAsmBackend(const Target &T, const MCRegisterInfo &MRI, const Triple &TT, StringRef CPU, const MCTargetOptions &Options)
void setELFHeaderEFlags(unsigned Flags)
Definition: MCAssembler.h:241
int getDwarfRegNum(unsigned RegNum, bool isEH) const
Map a target register to an equivalent dwarf register number.
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:59
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:103
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:42
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:78
ExprKind getKind() const
Definition: MCExpr.h:73
Target & getThePPC32Target()
static void RegisterMCInstPrinter(Target &T, Target::MCInstPrinterCtorTy Fn)
RegisterMCInstPrinter - Register a MCInstPrinter implementation for the given target.
unsigned getELFHeaderEFlags() const
ELF e_header flags.
Definition: MCAssembler.h:240
static void RegisterAsmTargetStreamer(Target &T, Target::AsmTargetStreamerCtorTy Fn)
static void adjustCodeGenOpts(const Triple &TT, Reloc::Model RM, CodeModel::Model &CM)
static int64_t decodePPC64LocalEntryOffset(unsigned Other)
Definition: ELF.h:394
~PPCTargetStreamer() override
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
Reg
All possible values of the reg field in the ModR/M byte.
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:165
static unsigned encodePPC64LocalEntryOffset(int64_t Offset)
Definition: ELF.h:398
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:663
void EmitValueToAlignment(unsigned, int64_t, unsigned, unsigned) override
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
void addInitialFrameState(const MCCFIInstruction &Inst)
Definition: MCAsmInfo.h:582
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:281
Target & getThePPC64Target()
void LLVMInitializePowerPCTargetMC()
Expected< const typename ELFT::Sym * > getSymbol(typename ELFT::SymRange Symbols, uint32_t Index)
Definition: ELF.h:243
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:167
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:377
void setOther(unsigned Other)
unsigned getOther() const
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:24
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
static void registerMCAdjustCodeGenOpts(Target &T, Target::MCAdjustCodeGenOptsFnTy Fn)
static MCInstPrinter * createPPCMCInstPrinter(const Triple &T, unsigned SyntaxVariant, const MCAsmInfo &MAI, const MCInstrInfo &MII, const MCRegisterInfo &MRI)
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)
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:44
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
Definition: Triple.h:466
static void RegisterMCCodeEmitter(Target &T, Target::MCCodeEmitterCtorTy Fn)
RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the given target.
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()
RegisterMCAsmInfoFn - Helper template for registering a target assembly info implementation.
llvm_unreachable("Unknown RegBank!")
static MCRegisterInfo * createPPCMCRegisterInfo(const Triple &TT)
static std::vector< std::string > Flags
Definition: FlagsTest.cpp:8
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:41
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:577
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:203
#define C
Definition: LargeTest.cpp:25
static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn)
RegisterMCInstrInfo - Register a MCInstrInfo implementation for the given target. ...
MCSubtargetInfo - 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:41
const Triple & getTargetTriple() const
getTargetTriple - Return the target triple string.
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:40
PPCTargetStreamer(MCStreamer &S)
LLVM Value Representation.
Definition: Value.h:73
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static MCTargetStreamer * createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI)
MCCodeEmitter * createPPCMCCodeEmitter(const MCInstrInfo &MCII, const MCRegisterInfo &MRI, MCContext &Ctx)