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