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 "InstPrinter/PPCInstPrinter.h"
15 #include "MCTargetDesc/PPCMCAsmInfo.h"
16 #include "PPCTargetStreamer.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/BinaryFormat/ELF.h"
20 #include "llvm/MC/MCAssembler.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCDwarf.h"
23 #include "llvm/MC/MCELFStreamer.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCInstrInfo.h"
26 #include "llvm/MC/MCRegisterInfo.h"
27 #include "llvm/MC/MCStreamer.h"
28 #include "llvm/MC/MCSubtargetInfo.h"
29 #include "llvm/MC/MCSymbol.h"
30 #include "llvm/MC/MCSymbolELF.h"
31 #include "llvm/Support/Casting.h"
32 #include "llvm/Support/CodeGen.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/FormattedStream.h"
35 #include "llvm/Support/TargetRegistry.h"
36 #include "llvm/Support/raw_ostream.h"
37 
38 using namespace llvm;
39 
40 #define GET_INSTRINFO_MC_DESC
41 #include "PPCGenInstrInfo.inc"
42 
43 #define GET_SUBTARGETINFO_MC_DESC
44 #include "PPCGenSubtargetInfo.inc"
45 
46 #define GET_REGINFO_MC_DESC
47 #include "PPCGenRegisterInfo.inc"
48 
49 // Pin the vtable to this file.
50 PPCTargetStreamer::PPCTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
51 
52 PPCTargetStreamer::~PPCTargetStreamer() = default;
53 
54 static MCInstrInfo *createPPCMCInstrInfo() {
55   MCInstrInfo *X = new MCInstrInfo();
56   InitPPCMCInstrInfo(X);
57   return X;
58 }
59 
60 static MCRegisterInfo *createPPCMCRegisterInfo(const Triple &TT) {
61   bool isPPC64 =
62       (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le);
63   unsigned Flavour = isPPC64 ? 0 : 1;
64   unsigned RA = isPPC64 ? PPC::LR8 : PPC::LR;
65 
66   MCRegisterInfo *X = new MCRegisterInfo();
67   InitPPCMCRegisterInfo(X, RA, Flavour, Flavour);
68   return X;
69 }
70 
71 static MCSubtargetInfo *createPPCMCSubtargetInfo(const Triple &TT,
72                                                  StringRef CPU, StringRef FS) {
73   return createPPCMCSubtargetInfoImpl(TT, CPU, FS);
74 }
75 
76 static MCAsmInfo *createPPCMCAsmInfo(const MCRegisterInfo &MRI,
77                                      const Triple &TheTriple) {
78   bool isPPC64 = (TheTriple.getArch() == Triple::ppc64 ||
79                   TheTriple.getArch() == Triple::ppc64le);
80 
81   MCAsmInfo *MAI;
82   if (TheTriple.isOSDarwin())
83     MAI = new PPCMCAsmInfoDarwin(isPPC64, TheTriple);
84   else
85     MAI = new PPCELFMCAsmInfo(isPPC64, TheTriple);
86 
87   // Initial state of the frame pointer is R1.
88   unsigned Reg = isPPC64 ? PPC::X1 : PPC::R1;
89   MCCFIInstruction Inst =
90       MCCFIInstruction::createDefCfa(nullptr, MRI.getDwarfRegNum(Reg, true), 0);
91   MAI->addInitialFrameState(Inst);
92 
93   return MAI;
94 }
95 
96 namespace {
97 
98 class PPCTargetAsmStreamer : public PPCTargetStreamer {
99   formatted_raw_ostream &OS;
100 
101 public:
102   PPCTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
103       : PPCTargetStreamer(S), OS(OS) {}
104 
105   void emitTCEntry(const MCSymbol &S) override {
106     OS << "\t.tc ";
107     OS << S.getName();
108     OS << "[TC],";
109     OS << S.getName();
110     OS << '\n';
111   }
112 
113   void emitMachine(StringRef CPU) override {
114     OS << "\t.machine " << CPU << '\n';
115   }
116 
117   void emitAbiVersion(int AbiVersion) override {
118     OS << "\t.abiversion " << AbiVersion << '\n';
119   }
120 
121   void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
122     const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
123 
124     OS << "\t.localentry\t";
125     S->print(OS, MAI);
126     OS << ", ";
127     LocalOffset->print(OS, MAI);
128     OS << '\n';
129   }
130 };
131 
132 class PPCTargetELFStreamer : public PPCTargetStreamer {
133 public:
134   PPCTargetELFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
135 
136   MCELFStreamer &getStreamer() {
137     return static_cast<MCELFStreamer &>(Streamer);
138   }
139 
140   void emitTCEntry(const MCSymbol &S) override {
141     // Creates a R_PPC64_TOC relocation
142     Streamer.EmitValueToAlignment(8);
143     Streamer.EmitSymbolValue(&S, 8);
144   }
145 
146   void emitMachine(StringRef CPU) override {
147     // FIXME: Is there anything to do in here or does this directive only
148     // limit the parser?
149   }
150 
151   void emitAbiVersion(int AbiVersion) override {
152     MCAssembler &MCA = getStreamer().getAssembler();
153     unsigned Flags = MCA.getELFHeaderEFlags();
154     Flags &= ~ELF::EF_PPC64_ABI;
155     Flags |= (AbiVersion & ELF::EF_PPC64_ABI);
156     MCA.setELFHeaderEFlags(Flags);
157   }
158 
159   void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
160     MCAssembler &MCA = getStreamer().getAssembler();
161 
162     int64_t Res;
163     if (!LocalOffset->evaluateAsAbsolute(Res, MCA))
164       report_fatal_error(".localentry expression must be absolute.");
165 
166     unsigned Encoded = ELF::encodePPC64LocalEntryOffset(Res);
167     if (Res != ELF::decodePPC64LocalEntryOffset(Encoded))
168       report_fatal_error(".localentry expression cannot be encoded.");
169 
170     unsigned Other = S->getOther();
171     Other &= ~ELF::STO_PPC64_LOCAL_MASK;
172     Other |= Encoded;
173     S->setOther(Other);
174 
175     // For GAS compatibility, unless we already saw a .abiversion directive,
176     // set e_flags to indicate ELFv2 ABI.
177     unsigned Flags = MCA.getELFHeaderEFlags();
178     if ((Flags & ELF::EF_PPC64_ABI) == 0)
179       MCA.setELFHeaderEFlags(Flags | 2);
180   }
181 
182   void emitAssignment(MCSymbol *S, const MCExpr *Value) override {
183     auto *Symbol = cast<MCSymbolELF>(S);
184     // When encoding an assignment to set symbol A to symbol B, also copy
185     // the st_other bits encoding the local entry point offset.
186     if (Value->getKind() != MCExpr::SymbolRef)
187       return;
188     const auto &RhsSym = cast<MCSymbolELF>(
189         static_cast<const MCSymbolRefExpr *>(Value)->getSymbol());
190     unsigned Other = Symbol->getOther();
191     Other &= ~ELF::STO_PPC64_LOCAL_MASK;
192     Other |= RhsSym.getOther() & ELF::STO_PPC64_LOCAL_MASK;
193     Symbol->setOther(Other);
194   }
195 };
196 
197 class PPCTargetMachOStreamer : public PPCTargetStreamer {
198 public:
199   PPCTargetMachOStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
200 
201   void emitTCEntry(const MCSymbol &S) override {
202     llvm_unreachable("Unknown pseudo-op: .tc");
203   }
204 
205   void emitMachine(StringRef CPU) override {
206     // FIXME: We should update the CPUType, CPUSubType in the Object file if
207     // the new values are different from the defaults.
208   }
209 
210   void emitAbiVersion(int AbiVersion) override {
211     llvm_unreachable("Unknown pseudo-op: .abiversion");
212   }
213 
214   void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
215     llvm_unreachable("Unknown pseudo-op: .localentry");
216   }
217 };
218 
219 } // end anonymous namespace
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);
242 }
243 
244 extern "C" void LLVMInitializePowerPCTargetMC() {
245   for (Target *T :
246        {&getThePPC32Target(), &getThePPC64Target(), &getThePPC64LETarget()}) {
247     // Register the MC asm info.
248     RegisterMCAsmInfoFn C(*T, createPPCMCAsmInfo);
249 
250     // Register the MC instruction info.
251     TargetRegistry::RegisterMCInstrInfo(*T, createPPCMCInstrInfo);
252 
253     // Register the MC register info.
254     TargetRegistry::RegisterMCRegInfo(*T, createPPCMCRegisterInfo);
255 
256     // Register the MC subtarget info.
257     TargetRegistry::RegisterMCSubtargetInfo(*T, createPPCMCSubtargetInfo);
258 
259     // Register the MC Code Emitter
260     TargetRegistry::RegisterMCCodeEmitter(*T, createPPCMCCodeEmitter);
261 
262     // Register the asm backend.
263     TargetRegistry::RegisterMCAsmBackend(*T, createPPCAsmBackend);
264 
265     // Register the object target streamer.
266     TargetRegistry::RegisterObjectTargetStreamer(*T,
267                                                  createObjectTargetStreamer);
268 
269     // Register the asm target streamer.
270     TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer);
271 
272     // Register the MCInstPrinter.
273     TargetRegistry::RegisterMCInstPrinter(*T, createPPCMCInstPrinter);
274   }
275 }
276