• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "MCTargetDesc/PPCMCTargetDesc.h"
15 #include "InstPrinter/PPCInstPrinter.h"
16 #include "MCTargetDesc/PPCMCAsmInfo.h"
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"
29 #include "llvm/MC/MCSubtargetInfo.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"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/FormattedStream.h"
36 #include "llvm/Support/TargetRegistry.h"
37 #include "llvm/Support/raw_ostream.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.
PPCTargetStreamer(MCStreamer & S)51 PPCTargetStreamer::PPCTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
52 
53 PPCTargetStreamer::~PPCTargetStreamer() = default;
54 
createPPCMCInstrInfo()55 static MCInstrInfo *createPPCMCInstrInfo() {
56   MCInstrInfo *X = new MCInstrInfo();
57   InitPPCMCInstrInfo(X);
58   return X;
59 }
60 
createPPCMCRegisterInfo(const Triple & TT)61 static MCRegisterInfo *createPPCMCRegisterInfo(const Triple &TT) {
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 
67   MCRegisterInfo *X = new MCRegisterInfo();
68   InitPPCMCRegisterInfo(X, RA, Flavour, Flavour);
69   return X;
70 }
71 
createPPCMCSubtargetInfo(const Triple & TT,StringRef CPU,StringRef FS)72 static MCSubtargetInfo *createPPCMCSubtargetInfo(const Triple &TT,
73                                                  StringRef CPU, StringRef FS) {
74   return createPPCMCSubtargetInfoImpl(TT, CPU, FS);
75 }
76 
createPPCMCAsmInfo(const MCRegisterInfo & MRI,const Triple & TheTriple)77 static MCAsmInfo *createPPCMCAsmInfo(const MCRegisterInfo &MRI,
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 {
100   formatted_raw_ostream &OS;
101 
102 public:
PPCTargetAsmStreamer(MCStreamer & S,formatted_raw_ostream & OS)103   PPCTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
104       : PPCTargetStreamer(S), OS(OS) {}
105 
emitTCEntry(const MCSymbol & S)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 
emitMachine(StringRef CPU)114   void emitMachine(StringRef CPU) override {
115     OS << "\t.machine " << CPU << '\n';
116   }
117 
emitAbiVersion(int AbiVersion)118   void emitAbiVersion(int AbiVersion) override {
119     OS << "\t.abiversion " << AbiVersion << '\n';
120   }
121 
emitLocalEntry(MCSymbolELF * S,const MCExpr * LocalOffset)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:
PPCTargetELFStreamer(MCStreamer & S)135   PPCTargetELFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
136 
getStreamer()137   MCELFStreamer &getStreamer() {
138     return static_cast<MCELFStreamer &>(Streamer);
139   }
140 
emitTCEntry(const MCSymbol & S)141   void emitTCEntry(const MCSymbol &S) override {
142     // Creates a R_PPC64_TOC relocation
143     Streamer.EmitValueToAlignment(8);
144     Streamer.EmitSymbolValue(&S, 8);
145   }
146 
emitMachine(StringRef CPU)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 
emitAbiVersion(int AbiVersion)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 
emitLocalEntry(MCSymbolELF * S,const MCExpr * LocalOffset)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 
emitAssignment(MCSymbol * S,const MCExpr * Value)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:
PPCTargetMachOStreamer(MCStreamer & S)200   PPCTargetMachOStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
201 
emitTCEntry(const MCSymbol & S)202   void emitTCEntry(const MCSymbol &S) override {
203     llvm_unreachable("Unknown pseudo-op: .tc");
204   }
205 
emitMachine(StringRef CPU)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 
emitAbiVersion(int AbiVersion)211   void emitAbiVersion(int AbiVersion) override {
212     llvm_unreachable("Unknown pseudo-op: .abiversion");
213   }
214 
emitLocalEntry(MCSymbolELF * S,const MCExpr * LocalOffset)215   void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
216     llvm_unreachable("Unknown pseudo-op: .localentry");
217   }
218 };
219 
220 } // end anonymous namespace
221 
createAsmTargetStreamer(MCStreamer & S,formatted_raw_ostream & OS,MCInstPrinter * InstPrint,bool isVerboseAsm)222 static MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
223                                                  formatted_raw_ostream &OS,
224                                                  MCInstPrinter *InstPrint,
225                                                  bool isVerboseAsm) {
226   return new PPCTargetAsmStreamer(S, OS);
227 }
228 
229 static MCTargetStreamer *
createObjectTargetStreamer(MCStreamer & S,const MCSubtargetInfo & STI)230 createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
231   const Triple &TT = STI.getTargetTriple();
232   if (TT.isOSBinFormatELF())
233     return new PPCTargetELFStreamer(S);
234   return new PPCTargetMachOStreamer(S);
235 }
236 
createPPCMCInstPrinter(const Triple & T,unsigned SyntaxVariant,const MCAsmInfo & MAI,const MCInstrInfo & MII,const MCRegisterInfo & MRI)237 static MCInstPrinter *createPPCMCInstPrinter(const Triple &T,
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 
LLVMInitializePowerPCTargetMC()245 extern "C" void LLVMInitializePowerPCTargetMC() {
246   for (Target *T :
247        {&getThePPC32Target(), &getThePPC64Target(), &getThePPC64LETarget()}) {
248     // Register the MC asm info.
249     RegisterMCAsmInfoFn C(*T, createPPCMCAsmInfo);
250 
251     // Register the MC instruction info.
252     TargetRegistry::RegisterMCInstrInfo(*T, createPPCMCInstrInfo);
253 
254     // Register the MC register info.
255     TargetRegistry::RegisterMCRegInfo(*T, createPPCMCRegisterInfo);
256 
257     // Register the MC subtarget info.
258     TargetRegistry::RegisterMCSubtargetInfo(*T, createPPCMCSubtargetInfo);
259 
260     // Register the MC Code Emitter
261     TargetRegistry::RegisterMCCodeEmitter(*T, createPPCMCCodeEmitter);
262 
263     // Register the asm backend.
264     TargetRegistry::RegisterMCAsmBackend(*T, createPPCAsmBackend);
265 
266     // Register the object target streamer.
267     TargetRegistry::RegisterObjectTargetStreamer(*T,
268                                                  createObjectTargetStreamer);
269 
270     // Register the asm target streamer.
271     TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer);
272 
273     // Register the MCInstPrinter.
274     TargetRegistry::RegisterMCInstPrinter(*T, createPPCMCInstPrinter);
275   }
276 }
277