LLVM  4.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 
14 #include "PPCMCTargetDesc.h"
16 #include "PPCMCAsmInfo.h"
17 #include "PPCTargetStreamer.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCELFStreamer.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCInstrInfo.h"
22 #include "llvm/MC/MCRegisterInfo.h"
23 #include "llvm/MC/MCStreamer.h"
25 #include "llvm/MC/MCSymbolELF.h"
27 #include "llvm/Support/ELF.h"
31 
32 using namespace llvm;
33 
34 #define GET_INSTRINFO_MC_DESC
35 #include "PPCGenInstrInfo.inc"
36 
37 #define GET_SUBTARGETINFO_MC_DESC
38 #include "PPCGenSubtargetInfo.inc"
39 
40 #define GET_REGINFO_MC_DESC
41 #include "PPCGenRegisterInfo.inc"
42 
43 // Pin the vtable to this file.
46 
48  MCInstrInfo *X = new MCInstrInfo();
49  InitPPCMCInstrInfo(X);
50  return X;
51 }
52 
54  bool isPPC64 =
55  (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le);
56  unsigned Flavour = isPPC64 ? 0 : 1;
57  unsigned RA = isPPC64 ? PPC::LR8 : PPC::LR;
58 
60  InitPPCMCRegisterInfo(X, RA, Flavour, Flavour);
61  return X;
62 }
63 
65  StringRef CPU, StringRef FS) {
66  return createPPCMCSubtargetInfoImpl(TT, CPU, FS);
67 }
68 
70  const Triple &TheTriple) {
71  bool isPPC64 = (TheTriple.getArch() == Triple::ppc64 ||
72  TheTriple.getArch() == Triple::ppc64le);
73 
74  MCAsmInfo *MAI;
75  if (TheTriple.isOSDarwin())
76  MAI = new PPCMCAsmInfoDarwin(isPPC64, TheTriple);
77  else
78  MAI = new PPCELFMCAsmInfo(isPPC64, TheTriple);
79 
80  // Initial state of the frame pointer is R1.
81  unsigned Reg = isPPC64 ? PPC::X1 : PPC::R1;
82  MCCFIInstruction Inst =
83  MCCFIInstruction::createDefCfa(nullptr, MRI.getDwarfRegNum(Reg, true), 0);
84  MAI->addInitialFrameState(Inst);
85 
86  return MAI;
87 }
88 
89 static void adjustCodeGenOpts(const Triple &TT, Reloc::Model RM,
90  CodeModel::Model &CM) {
91  if (CM == CodeModel::Default) {
92  if (!TT.isOSDarwin() &&
93  (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le))
94  CM = CodeModel::Medium;
95  }
96 }
97 
98 namespace {
99 class PPCTargetAsmStreamer : public PPCTargetStreamer {
101 
102 public:
103  PPCTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
104  : PPCTargetStreamer(S), OS(OS) {}
105  void emitTCEntry(const MCSymbol &S) override {
106  OS << "\t.tc ";
107  OS << S.getName();
108  OS << "[TC],";
109  OS << S.getName();
110  OS << '\n';
111  }
112  void emitMachine(StringRef CPU) override {
113  OS << "\t.machine " << CPU << '\n';
114  }
115  void emitAbiVersion(int AbiVersion) override {
116  OS << "\t.abiversion " << AbiVersion << '\n';
117  }
118  void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
119  const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
120 
121  OS << "\t.localentry\t";
122  S->print(OS, MAI);
123  OS << ", ";
124  LocalOffset->print(OS, MAI);
125  OS << '\n';
126  }
127 };
128 
129 class PPCTargetELFStreamer : public PPCTargetStreamer {
130 public:
131  PPCTargetELFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
132  MCELFStreamer &getStreamer() {
133  return static_cast<MCELFStreamer &>(Streamer);
134  }
135  void emitTCEntry(const MCSymbol &S) override {
136  // Creates a R_PPC64_TOC relocation
137  Streamer.EmitValueToAlignment(8);
138  Streamer.EmitSymbolValue(&S, 8);
139  }
140  void emitMachine(StringRef CPU) override {
141  // FIXME: Is there anything to do in here or does this directive only
142  // limit the parser?
143  }
144  void emitAbiVersion(int AbiVersion) override {
145  MCAssembler &MCA = getStreamer().getAssembler();
146  unsigned Flags = MCA.getELFHeaderEFlags();
147  Flags &= ~ELF::EF_PPC64_ABI;
148  Flags |= (AbiVersion & ELF::EF_PPC64_ABI);
149  MCA.setELFHeaderEFlags(Flags);
150  }
151  void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
152  MCAssembler &MCA = getStreamer().getAssembler();
153 
154  int64_t Res;
155  if (!LocalOffset->evaluateAsAbsolute(Res, MCA))
156  report_fatal_error(".localentry expression must be absolute.");
157 
158  unsigned Encoded = ELF::encodePPC64LocalEntryOffset(Res);
159  if (Res != ELF::decodePPC64LocalEntryOffset(Encoded))
160  report_fatal_error(".localentry expression cannot be encoded.");
161 
162  unsigned Other = S->getOther();
163  Other &= ~ELF::STO_PPC64_LOCAL_MASK;
164  Other |= Encoded;
165  S->setOther(Other);
166 
167  // For GAS compatibility, unless we already saw a .abiversion directive,
168  // set e_flags to indicate ELFv2 ABI.
169  unsigned Flags = MCA.getELFHeaderEFlags();
170  if ((Flags & ELF::EF_PPC64_ABI) == 0)
171  MCA.setELFHeaderEFlags(Flags | 2);
172  }
173  void emitAssignment(MCSymbol *S, const MCExpr *Value) override {
174  auto *Symbol = cast<MCSymbolELF>(S);
175  // When encoding an assignment to set symbol A to symbol B, also copy
176  // the st_other bits encoding the local entry point offset.
177  if (Value->getKind() != MCExpr::SymbolRef)
178  return;
179  const auto &RhsSym = cast<MCSymbolELF>(
180  static_cast<const MCSymbolRefExpr *>(Value)->getSymbol());
181  unsigned Other = Symbol->getOther();
182  Other &= ~ELF::STO_PPC64_LOCAL_MASK;
183  Other |= RhsSym.getOther() & ELF::STO_PPC64_LOCAL_MASK;
184  Symbol->setOther(Other);
185  }
186 };
187 
188 class PPCTargetMachOStreamer : public PPCTargetStreamer {
189 public:
190  PPCTargetMachOStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
191  void emitTCEntry(const MCSymbol &S) override {
192  llvm_unreachable("Unknown pseudo-op: .tc");
193  }
194  void emitMachine(StringRef CPU) override {
195  // FIXME: We should update the CPUType, CPUSubType in the Object file if
196  // the new values are different from the defaults.
197  }
198  void emitAbiVersion(int AbiVersion) override {
199  llvm_unreachable("Unknown pseudo-op: .abiversion");
200  }
201  void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
202  llvm_unreachable("Unknown pseudo-op: .localentry");
203  }
204 };
205 }
206 
209  MCInstPrinter *InstPrint,
210  bool isVerboseAsm) {
211  return new PPCTargetAsmStreamer(S, OS);
212 }
213 
214 static MCTargetStreamer *
216  const Triple &TT = STI.getTargetTriple();
217  if (TT.isOSBinFormatELF())
218  return new PPCTargetELFStreamer(S);
219  return new PPCTargetMachOStreamer(S);
220 }
221 
223  unsigned SyntaxVariant,
224  const MCAsmInfo &MAI,
225  const MCInstrInfo &MII,
226  const MCRegisterInfo &MRI) {
227  return new PPCInstPrinter(MAI, MII, MRI, T.isOSDarwin());
228 }
229 
230 extern "C" void LLVMInitializePowerPCTargetMC() {
231  for (Target *T :
233  // Register the MC asm info.
235 
236  // Register the MC codegen info.
238 
239  // Register the MC instruction info.
241 
242  // Register the MC register info.
244 
245  // Register the MC subtarget info.
247 
248  // Register the MC Code Emitter
250 
251  // Register the asm backend.
253 
254  // Register the object target streamer.
257 
258  // Register the asm target streamer.
260 
261  // Register the MCInstPrinter.
263  }
264 }
MCAsmBackend * createPPCAsmBackend(const Target &T, const MCRegisterInfo &MRI, const Triple &TT, StringRef CPU, const MCTargetOptions &Options)
void setELFHeaderEFlags(unsigned Flags)
Definition: MCAssembler.h:231
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:53
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
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:39
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:73
ExprKind getKind() const
Definition: MCExpr.h:70
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:230
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: Support/ELF.h:392
struct fuzzer::@269 Flags
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
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:161
static unsigned encodePPC64LocalEntryOffset(int64_t Offset)
Definition: Support/ELF.h:396
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:662
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:565
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:270
Target & getThePPC64Target()
void LLVMInitializePowerPCTargetMC()
Expected< const typename ELFT::Sym * > getSymbol(typename ELFT::SymRange Symbols, uint32_t Index)
Definition: Object/ELF.h:236
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:57
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:161
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:369
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)
#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:44
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
Definition: Triple.h:455
static void RegisterMCCodeEmitter(Target &T, Target::MCCodeEmitterCtorTy Fn)
RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the given target.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
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.
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:41
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:565
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:199
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:39
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:33
PPCTargetStreamer(MCStreamer &S)
LLVM Value Representation.
Definition: Value.h:71
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
static MCTargetStreamer * createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI)
MCCodeEmitter * createPPCMCCodeEmitter(const MCInstrInfo &MCII, const MCRegisterInfo &MRI, MCContext &Ctx)