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