1 //===-- PPCMCTargetDesc.cpp - PowerPC Target Descriptions -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file provides PowerPC specific target descriptions.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "MCTargetDesc/PPCMCTargetDesc.h"
14 #include "MCTargetDesc/PPCInstPrinter.h"
15 #include "MCTargetDesc/PPCMCAsmInfo.h"
16 #include "PPCTargetStreamer.h"
17 #include "TargetInfo/PowerPCTargetInfo.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/BinaryFormat/ELF.h"
22 #include "llvm/MC/MCAssembler.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCDwarf.h"
25 #include "llvm/MC/MCELFStreamer.h"
26 #include "llvm/MC/MCExpr.h"
27 #include "llvm/MC/MCInstrInfo.h"
28 #include "llvm/MC/MCRegisterInfo.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCSubtargetInfo.h"
31 #include "llvm/MC/MCSymbol.h"
32 #include "llvm/MC/MCSymbolELF.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/CodeGen.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/FormattedStream.h"
37 #include "llvm/Support/TargetRegistry.h"
38 #include "llvm/Support/raw_ostream.h"
39 
40 using namespace llvm;
41 
42 #define GET_INSTRINFO_MC_DESC
43 #include "PPCGenInstrInfo.inc"
44 
45 #define GET_SUBTARGETINFO_MC_DESC
46 #include "PPCGenSubtargetInfo.inc"
47 
48 #define GET_REGINFO_MC_DESC
49 #include "PPCGenRegisterInfo.inc"
50 
51 PPCTargetStreamer::PPCTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
52 
53 // Pin the vtable to this file.
54 PPCTargetStreamer::~PPCTargetStreamer() = default;
55 
56 static MCInstrInfo *createPPCMCInstrInfo() {
57   MCInstrInfo *X = new MCInstrInfo();
58   InitPPCMCInstrInfo(X);
59   return X;
60 }
61 
62 static MCRegisterInfo *createPPCMCRegisterInfo(const Triple &TT) {
63   bool isPPC64 =
64       (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le);
65   unsigned Flavour = isPPC64 ? 0 : 1;
66   unsigned RA = isPPC64 ? PPC::LR8 : PPC::LR;
67 
68   MCRegisterInfo *X = new MCRegisterInfo();
69   InitPPCMCRegisterInfo(X, RA, Flavour, Flavour);
70   return X;
71 }
72 
73 static MCSubtargetInfo *createPPCMCSubtargetInfo(const Triple &TT,
74                                                  StringRef CPU, StringRef FS) {
75   return createPPCMCSubtargetInfoImpl(TT, CPU, FS);
76 }
77 
78 static MCAsmInfo *createPPCMCAsmInfo(const MCRegisterInfo &MRI,
79                                      const Triple &TheTriple,
80                                      const MCTargetOptions &Options) {
81   bool isPPC64 = (TheTriple.getArch() == Triple::ppc64 ||
82                   TheTriple.getArch() == Triple::ppc64le);
83 
84   MCAsmInfo *MAI;
85   if (TheTriple.isOSDarwin())
86     MAI = new PPCMCAsmInfoDarwin(isPPC64, TheTriple);
87   else if (TheTriple.isOSBinFormatXCOFF())
88     MAI = new PPCXCOFFMCAsmInfo(isPPC64, TheTriple);
89   else
90     MAI = new PPCELFMCAsmInfo(isPPC64, TheTriple);
91 
92   // Initial state of the frame pointer is R1.
93   unsigned Reg = isPPC64 ? PPC::X1 : PPC::R1;
94   MCCFIInstruction Inst =
95       MCCFIInstruction::createDefCfa(nullptr, MRI.getDwarfRegNum(Reg, true), 0);
96   MAI->addInitialFrameState(Inst);
97 
98   return MAI;
99 }
100 
101 namespace {
102 
103 class PPCTargetAsmStreamer : public PPCTargetStreamer {
104   formatted_raw_ostream &OS;
105 
106 public:
107   PPCTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
108       : PPCTargetStreamer(S), OS(OS) {}
109 
110   void emitTCEntry(const MCSymbol &S) override {
111     OS << "\t.tc ";
112     OS << S.getName();
113     OS << "[TC],";
114     OS << S.getName();
115     OS << '\n';
116   }
117 
118   void emitMachine(StringRef CPU) override {
119     OS << "\t.machine " << CPU << '\n';
120   }
121 
122   void emitAbiVersion(int AbiVersion) override {
123     OS << "\t.abiversion " << AbiVersion << '\n';
124   }
125 
126   void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
127     const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
128 
129     OS << "\t.localentry\t";
130     S->print(OS, MAI);
131     OS << ", ";
132     LocalOffset->print(OS, MAI);
133     OS << '\n';
134   }
135 };
136 
137 class PPCTargetELFStreamer : public PPCTargetStreamer {
138 public:
139   PPCTargetELFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
140 
141   MCELFStreamer &getStreamer() {
142     return static_cast<MCELFStreamer &>(Streamer);
143   }
144 
145   void emitTCEntry(const MCSymbol &S) override {
146     // Creates a R_PPC64_TOC relocation
147     Streamer.EmitValueToAlignment(8);
148     Streamer.EmitSymbolValue(&S, 8);
149   }
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 
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 
164   void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
165     MCAssembler &MCA = getStreamer().getAssembler();
166 
167     int64_t Res;
168     if (!LocalOffset->evaluateAsAbsolute(Res, MCA))
169       report_fatal_error(".localentry expression must be absolute.");
170 
171     unsigned Encoded = ELF::encodePPC64LocalEntryOffset(Res);
172     if (Res != ELF::decodePPC64LocalEntryOffset(Encoded))
173       report_fatal_error(".localentry expression cannot be encoded.");
174 
175     unsigned Other = S->getOther();
176     Other &= ~ELF::STO_PPC64_LOCAL_MASK;
177     Other |= Encoded;
178     S->setOther(Other);
179 
180     // For GAS compatibility, unless we already saw a .abiversion directive,
181     // set e_flags to indicate ELFv2 ABI.
182     unsigned Flags = MCA.getELFHeaderEFlags();
183     if ((Flags & ELF::EF_PPC64_ABI) == 0)
184       MCA.setELFHeaderEFlags(Flags | 2);
185   }
186 
187   void emitAssignment(MCSymbol *S, const MCExpr *Value) override {
188     auto *Symbol = cast<MCSymbolELF>(S);
189 
190     // When encoding an assignment to set symbol A to symbol B, also copy
191     // the st_other bits encoding the local entry point offset.
192     if (copyLocalEntry(Symbol, Value))
193       UpdateOther.insert(Symbol);
194     else
195       UpdateOther.erase(Symbol);
196   }
197 
198   void finish() override {
199     for (auto *Sym : UpdateOther)
200       copyLocalEntry(Sym, Sym->getVariableValue());
201   }
202 
203 private:
204   SmallPtrSet<MCSymbolELF *, 32> UpdateOther;
205 
206   bool copyLocalEntry(MCSymbolELF *D, const MCExpr *S) {
207     auto *Ref = dyn_cast<const MCSymbolRefExpr>(S);
208     if (!Ref)
209       return false;
210     const auto &RhsSym = cast<MCSymbolELF>(Ref->getSymbol());
211     unsigned Other = D->getOther();
212     Other &= ~ELF::STO_PPC64_LOCAL_MASK;
213     Other |= RhsSym.getOther() & ELF::STO_PPC64_LOCAL_MASK;
214     D->setOther(Other);
215     return true;
216   }
217 };
218 
219 class PPCTargetMachOStreamer : public PPCTargetStreamer {
220 public:
221   PPCTargetMachOStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
222 
223   void emitTCEntry(const MCSymbol &S) override {
224     llvm_unreachable("Unknown pseudo-op: .tc");
225   }
226 
227   void emitMachine(StringRef CPU) override {
228     // FIXME: We should update the CPUType, CPUSubType in the Object file if
229     // the new values are different from the defaults.
230   }
231 
232   void emitAbiVersion(int AbiVersion) override {
233     llvm_unreachable("Unknown pseudo-op: .abiversion");
234   }
235 
236   void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
237     llvm_unreachable("Unknown pseudo-op: .localentry");
238   }
239 };
240 
241 class PPCTargetXCOFFStreamer : public PPCTargetStreamer {
242 public:
243   PPCTargetXCOFFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
244 
245   void emitTCEntry(const MCSymbol &S) override {
246     report_fatal_error("TOC entries not supported yet.");
247   }
248 
249   void emitMachine(StringRef CPU) override {
250     llvm_unreachable("Machine pseudo-ops are invalid for XCOFF.");
251   }
252 
253   void emitAbiVersion(int AbiVersion) override {
254     llvm_unreachable("ABI-version pseudo-ops are invalid for XCOFF.");
255   }
256 
257   void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
258     llvm_unreachable("Local-entry pseudo-ops are invalid for XCOFF.");
259   }
260 };
261 
262 } // end anonymous namespace
263 
264 static MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
265                                                  formatted_raw_ostream &OS,
266                                                  MCInstPrinter *InstPrint,
267                                                  bool isVerboseAsm) {
268   return new PPCTargetAsmStreamer(S, OS);
269 }
270 
271 static MCTargetStreamer *
272 createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
273   const Triple &TT = STI.getTargetTriple();
274   if (TT.isOSBinFormatELF())
275     return new PPCTargetELFStreamer(S);
276   if (TT.isOSBinFormatXCOFF())
277     return new PPCTargetXCOFFStreamer(S);
278   return new PPCTargetMachOStreamer(S);
279 }
280 
281 static MCInstPrinter *createPPCMCInstPrinter(const Triple &T,
282                                              unsigned SyntaxVariant,
283                                              const MCAsmInfo &MAI,
284                                              const MCInstrInfo &MII,
285                                              const MCRegisterInfo &MRI) {
286   return new PPCInstPrinter(MAI, MII, MRI, T);
287 }
288 
289 extern "C" void LLVMInitializePowerPCTargetMC() {
290   for (Target *T :
291        {&getThePPC32Target(), &getThePPC64Target(), &getThePPC64LETarget()}) {
292     // Register the MC asm info.
293     RegisterMCAsmInfoFn C(*T, createPPCMCAsmInfo);
294 
295     // Register the MC instruction info.
296     TargetRegistry::RegisterMCInstrInfo(*T, createPPCMCInstrInfo);
297 
298     // Register the MC register info.
299     TargetRegistry::RegisterMCRegInfo(*T, createPPCMCRegisterInfo);
300 
301     // Register the MC subtarget info.
302     TargetRegistry::RegisterMCSubtargetInfo(*T, createPPCMCSubtargetInfo);
303 
304     // Register the MC Code Emitter
305     TargetRegistry::RegisterMCCodeEmitter(*T, createPPCMCCodeEmitter);
306 
307     // Register the asm backend.
308     TargetRegistry::RegisterMCAsmBackend(*T, createPPCAsmBackend);
309 
310     // Register the object target streamer.
311     TargetRegistry::RegisterObjectTargetStreamer(*T,
312                                                  createObjectTargetStreamer);
313 
314     // Register the asm target streamer.
315     TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer);
316 
317     // Register the MCInstPrinter.
318     TargetRegistry::RegisterMCInstPrinter(*T, createPPCMCInstPrinter);
319   }
320 }
321