LLVM  3.7.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/MCCodeGenInfo.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCELFStreamer.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCInstrInfo.h"
23 #include "llvm/MC/MCRegisterInfo.h"
24 #include "llvm/MC/MCStreamer.h"
26 #include "llvm/MC/MCSymbolELF.h"
28 #include "llvm/Support/ELF.h"
32 
33 using namespace llvm;
34 
35 #define GET_INSTRINFO_MC_DESC
36 #include "PPCGenInstrInfo.inc"
37 
38 #define GET_SUBTARGETINFO_MC_DESC
39 #include "PPCGenSubtargetInfo.inc"
40 
41 #define GET_REGINFO_MC_DESC
42 #include "PPCGenRegisterInfo.inc"
43 
44 // Pin the vtable to this file.
47 
49  MCInstrInfo *X = new MCInstrInfo();
50  InitPPCMCInstrInfo(X);
51  return X;
52 }
53 
55  bool isPPC64 =
56  (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le);
57  unsigned Flavour = isPPC64 ? 0 : 1;
58  unsigned RA = isPPC64 ? PPC::LR8 : PPC::LR;
59 
61  InitPPCMCRegisterInfo(X, RA, Flavour, Flavour);
62  return X;
63 }
64 
66  StringRef CPU, StringRef FS) {
67  return createPPCMCSubtargetInfoImpl(TT, CPU, FS);
68 }
69 
71  const Triple &TheTriple) {
72  bool isPPC64 = (TheTriple.getArch() == Triple::ppc64 ||
73  TheTriple.getArch() == Triple::ppc64le);
74 
75  MCAsmInfo *MAI;
76  if (TheTriple.isOSDarwin())
77  MAI = new PPCMCAsmInfoDarwin(isPPC64, TheTriple);
78  else
79  MAI = new PPCELFMCAsmInfo(isPPC64, TheTriple);
80 
81  // Initial state of the frame pointer is R1.
82  unsigned Reg = isPPC64 ? PPC::X1 : PPC::R1;
83  MCCFIInstruction Inst =
84  MCCFIInstruction::createDefCfa(nullptr, MRI.getDwarfRegNum(Reg, true), 0);
85  MAI->addInitialFrameState(Inst);
86 
87  return MAI;
88 }
89 
92  CodeGenOpt::Level OL) {
93  MCCodeGenInfo *X = new MCCodeGenInfo();
94 
95  if (RM == Reloc::Default) {
96  if (TT.isOSDarwin())
98  else
99  RM = Reloc::Static;
100  }
101  if (CM == CodeModel::Default) {
102  if (!TT.isOSDarwin() &&
103  (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le))
104  CM = CodeModel::Medium;
105  }
106  X->initMCCodeGenInfo(RM, CM, OL);
107  return X;
108 }
109 
110 namespace {
111 class PPCTargetAsmStreamer : public PPCTargetStreamer {
113 
114 public:
115  PPCTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
116  : PPCTargetStreamer(S), OS(OS) {}
117  void emitTCEntry(const MCSymbol &S) override {
118  OS << "\t.tc ";
119  OS << S.getName();
120  OS << "[TC],";
121  OS << S.getName();
122  OS << '\n';
123  }
124  void emitMachine(StringRef CPU) override {
125  OS << "\t.machine " << CPU << '\n';
126  }
127  void emitAbiVersion(int AbiVersion) override {
128  OS << "\t.abiversion " << AbiVersion << '\n';
129  }
130  void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
131  const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
132 
133  OS << "\t.localentry\t";
134  S->print(OS, MAI);
135  OS << ", ";
136  LocalOffset->print(OS, MAI);
137  OS << '\n';
138  }
139 };
140 
141 class PPCTargetELFStreamer : public PPCTargetStreamer {
142 public:
143  PPCTargetELFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
144  MCELFStreamer &getStreamer() {
145  return static_cast<MCELFStreamer &>(Streamer);
146  }
147  void emitTCEntry(const MCSymbol &S) override {
148  // Creates a R_PPC64_TOC relocation
149  Streamer.EmitValueToAlignment(8);
150  Streamer.EmitSymbolValue(&S, 8);
151  }
152  void emitMachine(StringRef CPU) override {
153  // FIXME: Is there anything to do in here or does this directive only
154  // limit the parser?
155  }
156  void emitAbiVersion(int AbiVersion) override {
157  MCAssembler &MCA = getStreamer().getAssembler();
158  unsigned Flags = MCA.getELFHeaderEFlags();
159  Flags &= ~ELF::EF_PPC64_ABI;
160  Flags |= (AbiVersion & ELF::EF_PPC64_ABI);
161  MCA.setELFHeaderEFlags(Flags);
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  void emitAssignment(MCSymbol *S, const MCExpr *Value) override {
186  auto *Symbol = cast<MCSymbolELF>(S);
187  // When encoding an assignment to set symbol A to symbol B, also copy
188  // the st_other bits encoding the local entry point offset.
189  if (Value->getKind() != MCExpr::SymbolRef)
190  return;
191  const auto &RhsSym = cast<MCSymbolELF>(
192  static_cast<const MCSymbolRefExpr *>(Value)->getSymbol());
193  unsigned Other = Symbol->getOther();
194  Other &= ~ELF::STO_PPC64_LOCAL_MASK;
195  Other |= RhsSym.getOther() & ELF::STO_PPC64_LOCAL_MASK;
196  Symbol->setOther(Other);
197  }
198 };
199 
200 class PPCTargetMachOStreamer : public PPCTargetStreamer {
201 public:
202  PPCTargetMachOStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
203  void emitTCEntry(const MCSymbol &S) override {
204  llvm_unreachable("Unknown pseudo-op: .tc");
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  void emitAbiVersion(int AbiVersion) override {
211  llvm_unreachable("Unknown pseudo-op: .abiversion");
212  }
213  void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
214  llvm_unreachable("Unknown pseudo-op: .localentry");
215  }
216 };
217 }
218 
221  MCInstPrinter *InstPrint,
222  bool isVerboseAsm) {
223  return new PPCTargetAsmStreamer(S, OS);
224 }
225 
226 static MCTargetStreamer *
228  const Triple &TT = STI.getTargetTriple();
229  if (TT.isOSBinFormatELF())
230  return new PPCTargetELFStreamer(S);
231  return new PPCTargetMachOStreamer(S);
232 }
233 
235  unsigned SyntaxVariant,
236  const MCAsmInfo &MAI,
237  const MCInstrInfo &MII,
238  const MCRegisterInfo &MRI) {
239  return new PPCInstPrinter(MAI, MII, MRI, T.isOSDarwin());
240 }
241 
242 extern "C" void LLVMInitializePowerPCTargetMC() {
244  // Register the MC asm info.
246 
247  // Register the MC codegen info.
249 
250  // Register the MC instruction info.
252 
253  // Register the MC register info.
255 
256  // Register the MC subtarget info.
258 
259  // Register the MC Code Emitter
261 
262  // Register the asm backend.
264 
265  // Register the object target streamer.
268 
269  // Register the asm target streamer.
271 
272  // Register the MCInstPrinter.
274  }
275 }
void setELFHeaderEFlags(unsigned Flags)
Definition: MCAssembler.h:701
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:51
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:69
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:700
static void RegisterAsmTargetStreamer(Target &T, Target::AsmTargetStreamerCtorTy Fn)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
Reports a serious error, calling any installed error handler.
static int64_t decodePPC64LocalEntryOffset(unsigned Other)
Definition: Support/ELF.h:388
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:33
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:159
static unsigned encodePPC64LocalEntryOffset(int64_t Offset)
Definition: Support/ELF.h:393
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:591
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:534
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:242
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:58
static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn)
RegisterMCAsmBackend - Register a MCAsmBackend implementation for the given target.
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
Definition: MCExpr.cpp:33
Streaming machine code generation interface.
Definition: MCStreamer.h:157
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:354
MCAsmBackend * createPPCAsmBackend(const Target &T, const MCRegisterInfo &MRI, const Triple &TT, StringRef CPU)
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 RegisterMCCodeGenInfo(Target &T, Target::MCCodeGenInfoCtorFnTy Fn)
RegisterMCCodeGenInfo - Register a MCCodeGenInfo implementation for the given target.
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 or iOS).
Definition: Triple.h:404
static void RegisterMCCodeEmitter(Target &T, Target::MCCodeEmitterCtorTy Fn)
RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the given target.
Target ThePPC32Target
Target ThePPC64LETarget
static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn)
RegisterMCRegInfo - Register a MCRegisterInfo implementation for the given target.
Target - Wrapper for Target specific information.
RegisterMCAsmInfoFn - Helper template for registering a target assembly info implementation.
static MCRegisterInfo * createPPCMCRegisterInfo(const Triple &TT)
static MCCodeGenInfo * createPPCMCCodeGenInfo(const Triple &TT, Reloc::Model RM, CodeModel::Model CM, CodeGenOpt::Level OL)
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:38
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:479
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
void initMCCodeGenInfo(Reloc::Model RM=Reloc::Default, CodeModel::Model CM=CodeModel::Default, CodeGenOpt::Level OL=CodeGenOpt::Default)
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:38
const Triple & getTargetTriple() const
getTargetTriple - Return the target triple string.
PPCTargetStreamer(MCStreamer &S)
LLVM Value Representation.
Definition: Value.h:69
Target ThePPC64Target
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
static MCTargetStreamer * createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI)
MCCodeEmitter * createPPCMCCodeEmitter(const MCInstrInfo &MCII, const MCRegisterInfo &MRI, MCContext &Ctx)