xref: /llvm-project/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCTargetDesc.cpp (revision 241c2da4064c850d58896e0a870b0c3485dfd82d)
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 "PPCELFStreamer.h"
17 #include "PPCTargetStreamer.h"
18 #include "PPCXCOFFStreamer.h"
19 #include "TargetInfo/PowerPCTargetInfo.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Triple.h"
23 #include "llvm/BinaryFormat/ELF.h"
24 #include "llvm/MC/MCAsmBackend.h"
25 #include "llvm/MC/MCAssembler.h"
26 #include "llvm/MC/MCCodeEmitter.h"
27 #include "llvm/MC/MCContext.h"
28 #include "llvm/MC/MCDwarf.h"
29 #include "llvm/MC/MCELFStreamer.h"
30 #include "llvm/MC/MCExpr.h"
31 #include "llvm/MC/MCInstrInfo.h"
32 #include "llvm/MC/MCObjectWriter.h"
33 #include "llvm/MC/MCRegisterInfo.h"
34 #include "llvm/MC/MCSectionXCOFF.h"
35 #include "llvm/MC/MCStreamer.h"
36 #include "llvm/MC/MCSubtargetInfo.h"
37 #include "llvm/MC/MCSymbol.h"
38 #include "llvm/MC/MCSymbolELF.h"
39 #include "llvm/MC/MCSymbolXCOFF.h"
40 #include "llvm/Support/Casting.h"
41 #include "llvm/Support/CodeGen.h"
42 #include "llvm/Support/ErrorHandling.h"
43 #include "llvm/Support/FormattedStream.h"
44 #include "llvm/Support/TargetRegistry.h"
45 #include "llvm/Support/raw_ostream.h"
46 
47 using namespace llvm;
48 
49 #define GET_INSTRINFO_MC_DESC
50 #include "PPCGenInstrInfo.inc"
51 
52 #define GET_SUBTARGETINFO_MC_DESC
53 #include "PPCGenSubtargetInfo.inc"
54 
55 #define GET_REGINFO_MC_DESC
56 #include "PPCGenRegisterInfo.inc"
57 
58 PPCTargetStreamer::PPCTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
59 
60 // Pin the vtable to this file.
61 PPCTargetStreamer::~PPCTargetStreamer() = default;
62 
63 static MCInstrInfo *createPPCMCInstrInfo() {
64   MCInstrInfo *X = new MCInstrInfo();
65   InitPPCMCInstrInfo(X);
66   return X;
67 }
68 
69 static MCRegisterInfo *createPPCMCRegisterInfo(const Triple &TT) {
70   bool isPPC64 =
71       (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le);
72   unsigned Flavour = isPPC64 ? 0 : 1;
73   unsigned RA = isPPC64 ? PPC::LR8 : PPC::LR;
74 
75   MCRegisterInfo *X = new MCRegisterInfo();
76   InitPPCMCRegisterInfo(X, RA, Flavour, Flavour);
77   return X;
78 }
79 
80 static MCSubtargetInfo *createPPCMCSubtargetInfo(const Triple &TT,
81                                                  StringRef CPU, StringRef FS) {
82   // Set some default feature to MC layer.
83   std::string FullFS = std::string(FS);
84 
85   if (TT.isOSAIX()) {
86     if (!FullFS.empty())
87       FullFS = "+aix," + FullFS;
88     else
89       FullFS = "+aix";
90   }
91 
92   return createPPCMCSubtargetInfoImpl(TT, CPU, /*TuneCPU*/ CPU, FullFS);
93 }
94 
95 static MCAsmInfo *createPPCMCAsmInfo(const MCRegisterInfo &MRI,
96                                      const Triple &TheTriple,
97                                      const MCTargetOptions &Options) {
98   bool isPPC64 = (TheTriple.getArch() == Triple::ppc64 ||
99                   TheTriple.getArch() == Triple::ppc64le);
100 
101   MCAsmInfo *MAI;
102   if (TheTriple.isOSBinFormatXCOFF())
103     MAI = new PPCXCOFFMCAsmInfo(isPPC64, TheTriple);
104   else
105     MAI = new PPCELFMCAsmInfo(isPPC64, TheTriple);
106 
107   // Initial state of the frame pointer is R1.
108   unsigned Reg = isPPC64 ? PPC::X1 : PPC::R1;
109   MCCFIInstruction Inst =
110       MCCFIInstruction::cfiDefCfa(nullptr, MRI.getDwarfRegNum(Reg, true), 0);
111   MAI->addInitialFrameState(Inst);
112 
113   return MAI;
114 }
115 
116 static MCStreamer *
117 createPPCELFStreamer(const Triple &T, MCContext &Context,
118                      std::unique_ptr<MCAsmBackend> &&MAB,
119                      std::unique_ptr<MCObjectWriter> &&OW,
120                      std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll) {
121   return createPPCELFStreamer(Context, std::move(MAB), std::move(OW),
122                               std::move(Emitter));
123 }
124 
125 static MCStreamer *createPPCXCOFFStreamer(
126     const Triple &T, MCContext &Context, std::unique_ptr<MCAsmBackend> &&MAB,
127     std::unique_ptr<MCObjectWriter> &&OW,
128     std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll) {
129   return createPPCXCOFFStreamer(Context, std::move(MAB), std::move(OW),
130                                 std::move(Emitter));
131 }
132 
133 namespace {
134 
135 class PPCTargetAsmStreamer : public PPCTargetStreamer {
136   formatted_raw_ostream &OS;
137 
138 public:
139   PPCTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
140       : PPCTargetStreamer(S), OS(OS) {}
141 
142   void emitTCEntry(const MCSymbol &S,
143                    MCSymbolRefExpr::VariantKind Kind) override {
144     if (const MCSymbolXCOFF *XSym = dyn_cast<MCSymbolXCOFF>(&S)) {
145       MCSymbolXCOFF *TCSym =
146           cast<MCSectionXCOFF>(Streamer.getCurrentSectionOnly())
147               ->getQualNameSymbol();
148       // If the variant kind is TLSGD the entry represents the region handle for
149       // the symbol, we prefix the name with a dot and we add the @m
150       // relocation specifier.
151       if (Kind == MCSymbolRefExpr::VariantKind::VK_PPC_TLSGD)
152         OS << "\t.tc ." << TCSym->getName() << "," << XSym->getName() << "@m\n";
153       else
154         OS << "\t.tc " << TCSym->getName() << "," << XSym->getName() << '\n';
155 
156       if (TCSym->hasRename())
157         Streamer.emitXCOFFRenameDirective(TCSym, TCSym->getSymbolTableName());
158       return;
159     }
160 
161     OS << "\t.tc " << S.getName() << "[TC]," << S.getName() << '\n';
162   }
163 
164   void emitMachine(StringRef CPU) override {
165     OS << "\t.machine " << CPU << '\n';
166   }
167 
168   void emitAbiVersion(int AbiVersion) override {
169     OS << "\t.abiversion " << AbiVersion << '\n';
170   }
171 
172   void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
173     const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
174 
175     OS << "\t.localentry\t";
176     S->print(OS, MAI);
177     OS << ", ";
178     LocalOffset->print(OS, MAI);
179     OS << '\n';
180   }
181 };
182 
183 class PPCTargetELFStreamer : public PPCTargetStreamer {
184 public:
185   PPCTargetELFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
186 
187   MCELFStreamer &getStreamer() {
188     return static_cast<MCELFStreamer &>(Streamer);
189   }
190 
191   void emitTCEntry(const MCSymbol &S,
192                    MCSymbolRefExpr::VariantKind Kind) override {
193     // Creates a R_PPC64_TOC relocation
194     Streamer.emitValueToAlignment(8);
195     Streamer.emitSymbolValue(&S, 8);
196   }
197 
198   void emitMachine(StringRef CPU) override {
199     // FIXME: Is there anything to do in here or does this directive only
200     // limit the parser?
201   }
202 
203   void emitAbiVersion(int AbiVersion) override {
204     MCAssembler &MCA = getStreamer().getAssembler();
205     unsigned Flags = MCA.getELFHeaderEFlags();
206     Flags &= ~ELF::EF_PPC64_ABI;
207     Flags |= (AbiVersion & ELF::EF_PPC64_ABI);
208     MCA.setELFHeaderEFlags(Flags);
209   }
210 
211   void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
212     MCAssembler &MCA = getStreamer().getAssembler();
213 
214     // encodePPC64LocalEntryOffset will report an error if it cannot
215     // encode LocalOffset.
216     unsigned Encoded = encodePPC64LocalEntryOffset(LocalOffset);
217 
218     unsigned Other = S->getOther();
219     Other &= ~ELF::STO_PPC64_LOCAL_MASK;
220     Other |= Encoded;
221     S->setOther(Other);
222 
223     // For GAS compatibility, unless we already saw a .abiversion directive,
224     // set e_flags to indicate ELFv2 ABI.
225     unsigned Flags = MCA.getELFHeaderEFlags();
226     if ((Flags & ELF::EF_PPC64_ABI) == 0)
227       MCA.setELFHeaderEFlags(Flags | 2);
228   }
229 
230   void emitAssignment(MCSymbol *S, const MCExpr *Value) override {
231     auto *Symbol = cast<MCSymbolELF>(S);
232 
233     // When encoding an assignment to set symbol A to symbol B, also copy
234     // the st_other bits encoding the local entry point offset.
235     if (copyLocalEntry(Symbol, Value))
236       UpdateOther.insert(Symbol);
237     else
238       UpdateOther.erase(Symbol);
239   }
240 
241   void finish() override {
242     for (auto *Sym : UpdateOther)
243       if (Sym->isVariable())
244         copyLocalEntry(Sym, Sym->getVariableValue());
245 
246     // Clear the set of symbols that needs to be updated so the streamer can
247     // be reused without issues.
248     UpdateOther.clear();
249   }
250 
251 private:
252   SmallPtrSet<MCSymbolELF *, 32> UpdateOther;
253 
254   bool copyLocalEntry(MCSymbolELF *D, const MCExpr *S) {
255     auto *Ref = dyn_cast<const MCSymbolRefExpr>(S);
256     if (!Ref)
257       return false;
258     const auto &RhsSym = cast<MCSymbolELF>(Ref->getSymbol());
259     unsigned Other = D->getOther();
260     Other &= ~ELF::STO_PPC64_LOCAL_MASK;
261     Other |= RhsSym.getOther() & ELF::STO_PPC64_LOCAL_MASK;
262     D->setOther(Other);
263     return true;
264   }
265 
266   unsigned encodePPC64LocalEntryOffset(const MCExpr *LocalOffset) {
267     MCAssembler &MCA = getStreamer().getAssembler();
268     int64_t Offset;
269     if (!LocalOffset->evaluateAsAbsolute(Offset, MCA))
270       MCA.getContext().reportFatalError(
271           LocalOffset->getLoc(), ".localentry expression must be absolute.");
272 
273     switch (Offset) {
274     default:
275       MCA.getContext().reportFatalError(
276           LocalOffset->getLoc(),
277           ".localentry expression is not a valid power of 2.");
278     case 0:
279       return 0;
280     case 1:
281       return 1 << ELF::STO_PPC64_LOCAL_BIT;
282     case 4:
283     case 8:
284     case 16:
285     case 32:
286     case 64:
287       return (int)Log2(Offset) << (int)ELF::STO_PPC64_LOCAL_BIT;
288     }
289   }
290 };
291 
292 class PPCTargetMachOStreamer : public PPCTargetStreamer {
293 public:
294   PPCTargetMachOStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
295 
296   void emitTCEntry(const MCSymbol &S,
297                    MCSymbolRefExpr::VariantKind Kind) override {
298     llvm_unreachable("Unknown pseudo-op: .tc");
299   }
300 
301   void emitMachine(StringRef CPU) override {
302     // FIXME: We should update the CPUType, CPUSubType in the Object file if
303     // the new values are different from the defaults.
304   }
305 
306   void emitAbiVersion(int AbiVersion) override {
307     llvm_unreachable("Unknown pseudo-op: .abiversion");
308   }
309 
310   void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
311     llvm_unreachable("Unknown pseudo-op: .localentry");
312   }
313 };
314 
315 class PPCTargetXCOFFStreamer : public PPCTargetStreamer {
316 public:
317   PPCTargetXCOFFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
318 
319   void emitTCEntry(const MCSymbol &S,
320                    MCSymbolRefExpr::VariantKind Kind) override {
321     const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
322     const unsigned PointerSize = MAI->getCodePointerSize();
323     Streamer.emitValueToAlignment(PointerSize);
324     Streamer.emitSymbolValue(&S, PointerSize);
325   }
326 
327   void emitMachine(StringRef CPU) override {
328     llvm_unreachable("Machine pseudo-ops are invalid for XCOFF.");
329   }
330 
331   void emitAbiVersion(int AbiVersion) override {
332     llvm_unreachable("ABI-version pseudo-ops are invalid for XCOFF.");
333   }
334 
335   void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
336     llvm_unreachable("Local-entry pseudo-ops are invalid for XCOFF.");
337   }
338 };
339 
340 } // end anonymous namespace
341 
342 static MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
343                                                  formatted_raw_ostream &OS,
344                                                  MCInstPrinter *InstPrint,
345                                                  bool isVerboseAsm) {
346   return new PPCTargetAsmStreamer(S, OS);
347 }
348 
349 static MCTargetStreamer *
350 createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
351   const Triple &TT = STI.getTargetTriple();
352   if (TT.isOSBinFormatELF())
353     return new PPCTargetELFStreamer(S);
354   if (TT.isOSBinFormatXCOFF())
355     return new PPCTargetXCOFFStreamer(S);
356   return new PPCTargetMachOStreamer(S);
357 }
358 
359 static MCInstPrinter *createPPCMCInstPrinter(const Triple &T,
360                                              unsigned SyntaxVariant,
361                                              const MCAsmInfo &MAI,
362                                              const MCInstrInfo &MII,
363                                              const MCRegisterInfo &MRI) {
364   return new PPCInstPrinter(MAI, MII, MRI, T);
365 }
366 
367 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializePowerPCTargetMC() {
368   for (Target *T : {&getThePPC32Target(), &getThePPC32LETarget(),
369                     &getThePPC64Target(), &getThePPC64LETarget()}) {
370     // Register the MC asm info.
371     RegisterMCAsmInfoFn C(*T, createPPCMCAsmInfo);
372 
373     // Register the MC instruction info.
374     TargetRegistry::RegisterMCInstrInfo(*T, createPPCMCInstrInfo);
375 
376     // Register the MC register info.
377     TargetRegistry::RegisterMCRegInfo(*T, createPPCMCRegisterInfo);
378 
379     // Register the MC subtarget info.
380     TargetRegistry::RegisterMCSubtargetInfo(*T, createPPCMCSubtargetInfo);
381 
382     // Register the MC Code Emitter
383     TargetRegistry::RegisterMCCodeEmitter(*T, createPPCMCCodeEmitter);
384 
385     // Register the asm backend.
386     TargetRegistry::RegisterMCAsmBackend(*T, createPPCAsmBackend);
387 
388     // Register the elf streamer.
389     TargetRegistry::RegisterELFStreamer(*T, createPPCELFStreamer);
390 
391     // Register the XCOFF streamer.
392     TargetRegistry::RegisterXCOFFStreamer(*T, createPPCXCOFFStreamer);
393 
394     // Register the object target streamer.
395     TargetRegistry::RegisterObjectTargetStreamer(*T,
396                                                  createObjectTargetStreamer);
397 
398     // Register the asm target streamer.
399     TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer);
400 
401     // Register the MCInstPrinter.
402     TargetRegistry::RegisterMCInstPrinter(*T, createPPCMCInstPrinter);
403   }
404 }
405