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