PPCMCTargetDesc.cpp revision 0c7f116bb6950ef819323d855415b2f2b0aad987
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"
15#include "InstPrinter/PPCInstPrinter.h"
16#include "PPCMCAsmInfo.h"
17#include "PPCTargetStreamer.h"
18#include "llvm/MC/MCCodeGenInfo.h"
19#include "llvm/MC/MCELF.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"
25#include "llvm/MC/MCSubtargetInfo.h"
26#include "llvm/MC/MCSymbol.h"
27#include "llvm/MC/MachineLocation.h"
28#include "llvm/Support/ELF.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/FormattedStream.h"
31#include "llvm/Support/TargetRegistry.h"
32
33using 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.
45PPCTargetStreamer::~PPCTargetStreamer() {}
46PPCTargetStreamer::PPCTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
47
48static MCInstrInfo *createPPCMCInstrInfo() {
49  MCInstrInfo *X = new MCInstrInfo();
50  InitPPCMCInstrInfo(X);
51  return X;
52}
53
54static MCRegisterInfo *createPPCMCRegisterInfo(StringRef TT) {
55  Triple TheTriple(TT);
56  bool isPPC64 = (TheTriple.getArch() == Triple::ppc64 ||
57                  TheTriple.getArch() == Triple::ppc64le);
58  unsigned Flavour = isPPC64 ? 0 : 1;
59  unsigned RA = isPPC64 ? PPC::LR8 : PPC::LR;
60
61  MCRegisterInfo *X = new MCRegisterInfo();
62  InitPPCMCRegisterInfo(X, RA, Flavour, Flavour);
63  return X;
64}
65
66static MCSubtargetInfo *createPPCMCSubtargetInfo(StringRef TT, StringRef CPU,
67                                                 StringRef FS) {
68  MCSubtargetInfo *X = new MCSubtargetInfo();
69  InitPPCMCSubtargetInfo(X, TT, CPU, FS);
70  return X;
71}
72
73static MCAsmInfo *createPPCMCAsmInfo(const MCRegisterInfo &MRI, StringRef TT) {
74  Triple TheTriple(TT);
75  bool isPPC64 = (TheTriple.getArch() == Triple::ppc64 ||
76                  TheTriple.getArch() == Triple::ppc64le);
77
78  MCAsmInfo *MAI;
79  if (TheTriple.isOSDarwin())
80    MAI = new PPCMCAsmInfoDarwin(isPPC64, TheTriple);
81  else
82    MAI = new PPCELFMCAsmInfo(isPPC64, TheTriple);
83
84  // Initial state of the frame pointer is R1.
85  unsigned Reg = isPPC64 ? PPC::X1 : PPC::R1;
86  MCCFIInstruction Inst =
87      MCCFIInstruction::createDefCfa(nullptr, MRI.getDwarfRegNum(Reg, true), 0);
88  MAI->addInitialFrameState(Inst);
89
90  return MAI;
91}
92
93static MCCodeGenInfo *createPPCMCCodeGenInfo(StringRef TT, Reloc::Model RM,
94                                             CodeModel::Model CM,
95                                             CodeGenOpt::Level OL) {
96  MCCodeGenInfo *X = new MCCodeGenInfo();
97
98  if (RM == Reloc::Default) {
99    Triple T(TT);
100    if (T.isOSDarwin())
101      RM = Reloc::DynamicNoPIC;
102    else
103      RM = Reloc::Static;
104  }
105  if (CM == CodeModel::Default) {
106    Triple T(TT);
107    if (!T.isOSDarwin() &&
108        (T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le))
109      CM = CodeModel::Medium;
110  }
111  X->InitMCCodeGenInfo(RM, CM, OL);
112  return X;
113}
114
115namespace {
116class PPCTargetAsmStreamer : public PPCTargetStreamer {
117  formatted_raw_ostream &OS;
118
119public:
120  PPCTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
121      : PPCTargetStreamer(S), OS(OS) {}
122  void emitTCEntry(const MCSymbol &S) override {
123    OS << "\t.tc ";
124    OS << S.getName();
125    OS << "[TC],";
126    OS << S.getName();
127    OS << '\n';
128  }
129  void emitMachine(StringRef CPU) override {
130    OS << "\t.machine " << CPU << '\n';
131  }
132  void emitAbiVersion(int AbiVersion) override {
133    OS << "\t.abiversion " << AbiVersion << '\n';
134  }
135  void emitLocalEntry(MCSymbol *S, const MCExpr *LocalOffset) override {
136    OS << "\t.localentry\t" << *S << ", " << *LocalOffset << '\n';
137  }
138};
139
140class PPCTargetELFStreamer : public PPCTargetStreamer {
141public:
142  PPCTargetELFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
143  MCELFStreamer &getStreamer() {
144    return static_cast<MCELFStreamer &>(Streamer);
145  }
146  void emitTCEntry(const MCSymbol &S) override {
147    // Creates a R_PPC64_TOC relocation
148    Streamer.EmitValueToAlignment(8);
149    Streamer.EmitSymbolValue(&S, 8);
150  }
151  void emitMachine(StringRef CPU) override {
152    // FIXME: Is there anything to do in here or does this directive only
153    // limit the parser?
154  }
155  void emitAbiVersion(int AbiVersion) override {
156    MCAssembler &MCA = getStreamer().getAssembler();
157    unsigned Flags = MCA.getELFHeaderEFlags();
158    Flags &= ~ELF::EF_PPC64_ABI;
159    Flags |= (AbiVersion & ELF::EF_PPC64_ABI);
160    MCA.setELFHeaderEFlags(Flags);
161  }
162  void emitLocalEntry(MCSymbol *S, const MCExpr *LocalOffset) override {
163    MCAssembler &MCA = getStreamer().getAssembler();
164    MCSymbolData &Data = getStreamer().getOrCreateSymbolData(S);
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    // The "other" values are stored in the last 6 bits of the second byte.
175    // The traditional defines for STO values assume the full byte and thus
176    // the shift to pack it.
177    unsigned Other = MCELF::getOther(Data) << 2;
178    Other &= ~ELF::STO_PPC64_LOCAL_MASK;
179    Other |= Encoded;
180    MCELF::setOther(Data, Other >> 2);
181
182    // For GAS compatibility, unless we already saw a .abiversion directive,
183    // set e_flags to indicate ELFv2 ABI.
184    unsigned Flags = MCA.getELFHeaderEFlags();
185    if ((Flags & ELF::EF_PPC64_ABI) == 0)
186      MCA.setELFHeaderEFlags(Flags | 2);
187  }
188  void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override {
189    // When encoding an assignment to set symbol A to symbol B, also copy
190    // the st_other bits encoding the local entry point offset.
191    if (Value->getKind() != MCExpr::SymbolRef)
192      return;
193    const MCSymbol &RhsSym =
194        static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
195    MCSymbolData &Data = getStreamer().getOrCreateSymbolData(&RhsSym);
196    MCSymbolData &SymbolData = getStreamer().getOrCreateSymbolData(Symbol);
197    // The "other" values are stored in the last 6 bits of the second byte.
198    // The traditional defines for STO values assume the full byte and thus
199    // the shift to pack it.
200    unsigned Other = MCELF::getOther(SymbolData) << 2;
201    Other &= ~ELF::STO_PPC64_LOCAL_MASK;
202    Other |= (MCELF::getOther(Data) << 2) & ELF::STO_PPC64_LOCAL_MASK;
203    MCELF::setOther(SymbolData, Other >> 2);
204  }
205};
206
207class PPCTargetMachOStreamer : public PPCTargetStreamer {
208public:
209  PPCTargetMachOStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
210  void emitTCEntry(const MCSymbol &S) override {
211    llvm_unreachable("Unknown pseudo-op: .tc");
212  }
213  void emitMachine(StringRef CPU) override {
214    // FIXME: We should update the CPUType, CPUSubType in the Object file if
215    // the new values are different from the defaults.
216  }
217  void emitAbiVersion(int AbiVersion) override {
218    llvm_unreachable("Unknown pseudo-op: .abiversion");
219  }
220  void emitLocalEntry(MCSymbol *S, const MCExpr *LocalOffset) override {
221    llvm_unreachable("Unknown pseudo-op: .localentry");
222  }
223};
224}
225
226static MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
227                                                 formatted_raw_ostream &OS,
228                                                 MCInstPrinter *InstPrint,
229                                                 bool isVerboseAsm) {
230  return new PPCTargetAsmStreamer(S, OS);
231}
232
233static MCTargetStreamer *
234createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
235  Triple TT(STI.getTargetTriple());
236  if (TT.getObjectFormat() == Triple::ELF)
237    return new PPCTargetELFStreamer(S);
238  return new PPCTargetMachOStreamer(S);
239}
240
241static MCInstPrinter *createPPCMCInstPrinter(const Triple &T,
242                                             unsigned SyntaxVariant,
243                                             const MCAsmInfo &MAI,
244                                             const MCInstrInfo &MII,
245                                             const MCRegisterInfo &MRI) {
246  return new PPCInstPrinter(MAI, MII, MRI, T.isOSDarwin());
247}
248
249extern "C" void LLVMInitializePowerPCTargetMC() {
250  for (Target *T : {&ThePPC32Target, &ThePPC64Target, &ThePPC64LETarget}) {
251    // Register the MC asm info.
252    RegisterMCAsmInfoFn C(*T, createPPCMCAsmInfo);
253
254    // Register the MC codegen info.
255    TargetRegistry::RegisterMCCodeGenInfo(*T, createPPCMCCodeGenInfo);
256
257    // Register the MC instruction info.
258    TargetRegistry::RegisterMCInstrInfo(*T, createPPCMCInstrInfo);
259
260    // Register the MC register info.
261    TargetRegistry::RegisterMCRegInfo(*T, createPPCMCRegisterInfo);
262
263    // Register the MC subtarget info.
264    TargetRegistry::RegisterMCSubtargetInfo(*T, createPPCMCSubtargetInfo);
265
266    // Register the MC Code Emitter
267    TargetRegistry::RegisterMCCodeEmitter(*T, createPPCMCCodeEmitter);
268
269    // Register the asm backend.
270    TargetRegistry::RegisterMCAsmBackend(*T, createPPCAsmBackend);
271
272    // Register the object target streamer.
273    TargetRegistry::RegisterObjectTargetStreamer(*T,
274                                                 createObjectTargetStreamer);
275
276    // Register the asm target streamer.
277    TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer);
278
279    // Register the MCInstPrinter.
280    TargetRegistry::RegisterMCInstPrinter(*T, createPPCMCInstPrinter);
281  }
282}
283