1 //===-- PPCMCTargetDesc.cpp - PowerPC Target Descriptions -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides PowerPC specific target descriptions.
12 //===----------------------------------------------------------------------===//
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/MCELF.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/MCSymbol.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"
35 #define GET_INSTRINFO_MC_DESC
36 #include "PPCGenInstrInfo.inc"
38 #define GET_SUBTARGETINFO_MC_DESC
39 #include "PPCGenSubtargetInfo.inc"
41 #define GET_REGINFO_MC_DESC
42 #include "PPCGenRegisterInfo.inc"
44 // Pin the vtable to this file.
45 PPCTargetStreamer::~PPCTargetStreamer() {}
46 PPCTargetStreamer::PPCTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
48 static MCInstrInfo *createPPCMCInstrInfo() {
49 MCInstrInfo *X = new MCInstrInfo();
50 InitPPCMCInstrInfo(X);
54 static MCRegisterInfo *createPPCMCRegisterInfo(StringRef 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;
61 MCRegisterInfo *X = new MCRegisterInfo();
62 InitPPCMCRegisterInfo(X, RA, Flavour, Flavour);
66 static MCSubtargetInfo *createPPCMCSubtargetInfo(StringRef TT, StringRef CPU,
68 MCSubtargetInfo *X = new MCSubtargetInfo();
69 InitPPCMCSubtargetInfo(X, TT, CPU, FS);
73 static MCAsmInfo *createPPCMCAsmInfo(const MCRegisterInfo &MRI, StringRef TT) {
75 bool isPPC64 = (TheTriple.getArch() == Triple::ppc64 ||
76 TheTriple.getArch() == Triple::ppc64le);
79 if (TheTriple.isOSDarwin())
80 MAI = new PPCMCAsmInfoDarwin(isPPC64, TheTriple);
82 MAI = new PPCELFMCAsmInfo(isPPC64, TheTriple);
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);
93 static MCCodeGenInfo *createPPCMCCodeGenInfo(StringRef TT, Reloc::Model RM,
95 CodeGenOpt::Level OL) {
96 MCCodeGenInfo *X = new MCCodeGenInfo();
98 if (RM == Reloc::Default) {
101 RM = Reloc::DynamicNoPIC;
105 if (CM == CodeModel::Default) {
107 if (!T.isOSDarwin() &&
108 (T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le))
109 CM = CodeModel::Medium;
111 X->InitMCCodeGenInfo(RM, CM, OL);
116 class PPCTargetAsmStreamer : public PPCTargetStreamer {
117 formatted_raw_ostream &OS;
120 PPCTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
121 : PPCTargetStreamer(S), OS(OS) {}
122 void emitTCEntry(const MCSymbol &S) override {
129 void emitMachine(StringRef CPU) override {
130 OS << "\t.machine " << CPU << '\n';
132 virtual void emitAbiVersion(int AbiVersion) override {
133 OS << "\t.abiversion " << AbiVersion << '\n';
135 virtual void emitLocalEntry(MCSymbol *S, const MCExpr *LocalOffset) {
136 OS << "\t.localentry\t" << *S << ", " << *LocalOffset << '\n';
140 class PPCTargetELFStreamer : public PPCTargetStreamer {
142 PPCTargetELFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
143 MCELFStreamer &getStreamer() {
144 return static_cast<MCELFStreamer &>(Streamer);
146 virtual void emitTCEntry(const MCSymbol &S) override {
147 // Creates a R_PPC64_TOC relocation
148 Streamer.EmitSymbolValue(&S, 8);
150 void emitMachine(StringRef CPU) override {
151 // FIXME: Is there anything to do in here or does this directive only
154 virtual void emitAbiVersion(int AbiVersion) override {
155 MCAssembler &MCA = getStreamer().getAssembler();
156 unsigned Flags = MCA.getELFHeaderEFlags();
157 Flags &= ~ELF::EF_PPC64_ABI;
158 Flags |= (AbiVersion & ELF::EF_PPC64_ABI);
159 MCA.setELFHeaderEFlags(Flags);
161 virtual void emitLocalEntry(MCSymbol *S, const MCExpr *LocalOffset) {
162 MCAssembler &MCA = getStreamer().getAssembler();
163 MCSymbolData &Data = getStreamer().getOrCreateSymbolData(S);
166 if (!LocalOffset->EvaluateAsAbsolute(Res, MCA))
167 report_fatal_error(".localentry expression must be absolute.");
169 unsigned Encoded = ELF::encodePPC64LocalEntryOffset(Res);
170 if (Res != ELF::decodePPC64LocalEntryOffset(Encoded))
171 report_fatal_error(".localentry expression cannot be encoded.");
173 // The "other" values are stored in the last 6 bits of the second byte.
174 // The traditional defines for STO values assume the full byte and thus
175 // the shift to pack it.
176 unsigned Other = MCELF::getOther(Data) << 2;
177 Other &= ~ELF::STO_PPC64_LOCAL_MASK;
179 MCELF::setOther(Data, Other >> 2);
181 // For GAS compatibility, unless we already saw a .abiversion directive,
182 // set e_flags to indicate ELFv2 ABI.
183 unsigned Flags = MCA.getELFHeaderEFlags();
184 if ((Flags & ELF::EF_PPC64_ABI) == 0)
185 MCA.setELFHeaderEFlags(Flags | 2);
189 class PPCTargetMachOStreamer : public PPCTargetStreamer {
191 PPCTargetMachOStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
192 void emitTCEntry(const MCSymbol &S) override {
193 llvm_unreachable("Unknown pseudo-op: .tc");
195 void emitMachine(StringRef CPU) override {
196 // FIXME: We should update the CPUType, CPUSubType in the Object file if
197 // the new values are different from the defaults.
199 virtual void emitAbiVersion(int AbiVersion) override {
200 llvm_unreachable("Unknown pseudo-op: .abiversion");
202 virtual void emitLocalEntry(MCSymbol *S, const MCExpr *LocalOffset) {
203 llvm_unreachable("Unknown pseudo-op: .localentry");
208 // This is duplicated code. Refactor this.
209 static MCStreamer *createMCStreamer(const Target &T, StringRef TT,
210 MCContext &Ctx, MCAsmBackend &MAB,
212 MCCodeEmitter *Emitter,
213 const MCSubtargetInfo &STI,
216 if (Triple(TT).isOSDarwin()) {
217 MCStreamer *S = createMachOStreamer(Ctx, MAB, OS, Emitter, RelaxAll);
218 new PPCTargetMachOStreamer(*S);
223 createELFStreamer(Ctx, MAB, OS, Emitter, RelaxAll, NoExecStack);
224 new PPCTargetELFStreamer(*S);
229 createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
230 bool isVerboseAsm, bool useDwarfDirectory,
231 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
232 MCAsmBackend *TAB, bool ShowInst) {
234 MCStreamer *S = llvm::createAsmStreamer(
235 Ctx, OS, isVerboseAsm, useDwarfDirectory, InstPrint, CE, TAB, ShowInst);
236 new PPCTargetAsmStreamer(*S, OS);
240 static MCInstPrinter *createPPCMCInstPrinter(const Target &T,
241 unsigned SyntaxVariant,
242 const MCAsmInfo &MAI,
243 const MCInstrInfo &MII,
244 const MCRegisterInfo &MRI,
245 const MCSubtargetInfo &STI) {
246 bool isDarwin = Triple(STI.getTargetTriple()).isOSDarwin();
247 return new PPCInstPrinter(MAI, MII, MRI, isDarwin);
250 extern "C" void LLVMInitializePowerPCTargetMC() {
251 // Register the MC asm info.
252 RegisterMCAsmInfoFn C(ThePPC32Target, createPPCMCAsmInfo);
253 RegisterMCAsmInfoFn D(ThePPC64Target, createPPCMCAsmInfo);
254 RegisterMCAsmInfoFn E(ThePPC64LETarget, createPPCMCAsmInfo);
256 // Register the MC codegen info.
257 TargetRegistry::RegisterMCCodeGenInfo(ThePPC32Target, createPPCMCCodeGenInfo);
258 TargetRegistry::RegisterMCCodeGenInfo(ThePPC64Target, createPPCMCCodeGenInfo);
259 TargetRegistry::RegisterMCCodeGenInfo(ThePPC64LETarget,
260 createPPCMCCodeGenInfo);
262 // Register the MC instruction info.
263 TargetRegistry::RegisterMCInstrInfo(ThePPC32Target, createPPCMCInstrInfo);
264 TargetRegistry::RegisterMCInstrInfo(ThePPC64Target, createPPCMCInstrInfo);
265 TargetRegistry::RegisterMCInstrInfo(ThePPC64LETarget,
266 createPPCMCInstrInfo);
268 // Register the MC register info.
269 TargetRegistry::RegisterMCRegInfo(ThePPC32Target, createPPCMCRegisterInfo);
270 TargetRegistry::RegisterMCRegInfo(ThePPC64Target, createPPCMCRegisterInfo);
271 TargetRegistry::RegisterMCRegInfo(ThePPC64LETarget, createPPCMCRegisterInfo);
273 // Register the MC subtarget info.
274 TargetRegistry::RegisterMCSubtargetInfo(ThePPC32Target,
275 createPPCMCSubtargetInfo);
276 TargetRegistry::RegisterMCSubtargetInfo(ThePPC64Target,
277 createPPCMCSubtargetInfo);
278 TargetRegistry::RegisterMCSubtargetInfo(ThePPC64LETarget,
279 createPPCMCSubtargetInfo);
281 // Register the MC Code Emitter
282 TargetRegistry::RegisterMCCodeEmitter(ThePPC32Target, createPPCMCCodeEmitter);
283 TargetRegistry::RegisterMCCodeEmitter(ThePPC64Target, createPPCMCCodeEmitter);
284 TargetRegistry::RegisterMCCodeEmitter(ThePPC64LETarget,
285 createPPCMCCodeEmitter);
287 // Register the asm backend.
288 TargetRegistry::RegisterMCAsmBackend(ThePPC32Target, createPPCAsmBackend);
289 TargetRegistry::RegisterMCAsmBackend(ThePPC64Target, createPPCAsmBackend);
290 TargetRegistry::RegisterMCAsmBackend(ThePPC64LETarget, createPPCAsmBackend);
292 // Register the object streamer.
293 TargetRegistry::RegisterMCObjectStreamer(ThePPC32Target, createMCStreamer);
294 TargetRegistry::RegisterMCObjectStreamer(ThePPC64Target, createMCStreamer);
295 TargetRegistry::RegisterMCObjectStreamer(ThePPC64LETarget, createMCStreamer);
297 // Register the asm streamer.
298 TargetRegistry::RegisterAsmStreamer(ThePPC32Target, createMCAsmStreamer);
299 TargetRegistry::RegisterAsmStreamer(ThePPC64Target, createMCAsmStreamer);
300 TargetRegistry::RegisterAsmStreamer(ThePPC64LETarget, createMCAsmStreamer);
302 // Register the MCInstPrinter.
303 TargetRegistry::RegisterMCInstPrinter(ThePPC32Target, createPPCMCInstPrinter);
304 TargetRegistry::RegisterMCInstPrinter(ThePPC64Target, createPPCMCInstPrinter);
305 TargetRegistry::RegisterMCInstPrinter(ThePPC64LETarget,
306 createPPCMCInstPrinter);