blob: 4484d673112750e692ce58efa1a8a60f929e2258 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- PPCMCTargetDesc.cpp - PowerPC Target Descriptions -----------------===//
Evan Chengbc153d42011-07-14 20:59:42 +00002//
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"
Evan Cheng61faa552011-07-25 21:20:24 +000015#include "InstPrinter/PPCInstPrinter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "PPCMCAsmInfo.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000017#include "PPCTargetStreamer.h"
Evan Cheng4d6c9d72011-08-23 20:15:21 +000018#include "llvm/MC/MCCodeGenInfo.h"
Ulrich Weigand0daa5162014-07-20 22:56:57 +000019#include "llvm/MC/MCELFStreamer.h"
Ulrich Weigandbb686102014-07-20 23:06:03 +000020#include "llvm/MC/MCExpr.h"
Evan Chengbc153d42011-07-14 20:59:42 +000021#include "llvm/MC/MCInstrInfo.h"
22#include "llvm/MC/MCRegisterInfo.h"
Evan Cheng61d4a202011-07-25 19:53:23 +000023#include "llvm/MC/MCStreamer.h"
Evan Chengbc153d42011-07-14 20:59:42 +000024#include "llvm/MC/MCSubtargetInfo.h"
Rafael Espindola95fb9b92015-06-02 20:38:46 +000025#include "llvm/MC/MCSymbolELF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/MC/MachineLocation.h"
Ulrich Weigand0daa5162014-07-20 22:56:57 +000027#include "llvm/Support/ELF.h"
Craig Topperc4965bc2012-02-05 07:21:30 +000028#include "llvm/Support/ErrorHandling.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000029#include "llvm/Support/FormattedStream.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000030#include "llvm/Support/TargetRegistry.h"
Evan Chengbc153d42011-07-14 20:59:42 +000031
Chandler Carruthd174b722014-04-22 02:03:14 +000032using namespace llvm;
33
Evan Chengbc153d42011-07-14 20:59:42 +000034#define GET_INSTRINFO_MC_DESC
35#include "PPCGenInstrInfo.inc"
36
37#define GET_SUBTARGETINFO_MC_DESC
38#include "PPCGenSubtargetInfo.inc"
39
40#define GET_REGINFO_MC_DESC
41#include "PPCGenRegisterInfo.inc"
42
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000043// Pin the vtable to this file.
44PPCTargetStreamer::~PPCTargetStreamer() {}
Rafael Espindola24ea09e2014-01-26 06:06:37 +000045PPCTargetStreamer::PPCTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000046
Evan Cheng1705ab02011-07-14 23:50:31 +000047static MCInstrInfo *createPPCMCInstrInfo() {
Evan Chengbc153d42011-07-14 20:59:42 +000048 MCInstrInfo *X = new MCInstrInfo();
49 InitPPCMCInstrInfo(X);
50 return X;
51}
52
Evan Chengd60fa58b2011-07-18 20:57:22 +000053static MCRegisterInfo *createPPCMCRegisterInfo(StringRef TT) {
54 Triple TheTriple(TT);
Bill Schmidt0a9170d2013-07-26 01:35:43 +000055 bool isPPC64 = (TheTriple.getArch() == Triple::ppc64 ||
56 TheTriple.getArch() == Triple::ppc64le);
Evan Chengd60fa58b2011-07-18 20:57:22 +000057 unsigned Flavour = isPPC64 ? 0 : 1;
58 unsigned RA = isPPC64 ? PPC::LR8 : PPC::LR;
59
60 MCRegisterInfo *X = new MCRegisterInfo();
61 InitPPCMCRegisterInfo(X, RA, Flavour, Flavour);
62 return X;
63}
64
Evan Cheng1705ab02011-07-14 23:50:31 +000065static MCSubtargetInfo *createPPCMCSubtargetInfo(StringRef TT, StringRef CPU,
66 StringRef FS) {
Evan Chengbc153d42011-07-14 20:59:42 +000067 MCSubtargetInfo *X = new MCSubtargetInfo();
68 InitPPCMCSubtargetInfo(X, TT, CPU, FS);
69 return X;
70}
71
Daniel Sanders7813ae82015-06-04 13:12:25 +000072static MCAsmInfo *createPPCMCAsmInfo(const MCRegisterInfo &MRI,
73 const Triple &TheTriple) {
Bill Schmidt0a9170d2013-07-26 01:35:43 +000074 bool isPPC64 = (TheTriple.getArch() == Triple::ppc64 ||
75 TheTriple.getArch() == Triple::ppc64le);
Evan Cheng67c033e2011-07-18 22:29:13 +000076
77 MCAsmInfo *MAI;
Evan Cheng1705ab02011-07-14 23:50:31 +000078 if (TheTriple.isOSDarwin())
David Fang1b018492013-12-10 21:37:41 +000079 MAI = new PPCMCAsmInfoDarwin(isPPC64, TheTriple);
Evan Cheng67c033e2011-07-18 22:29:13 +000080 else
Joerg Sonnenberger5f233fc2014-08-04 18:46:13 +000081 MAI = new PPCELFMCAsmInfo(isPPC64, TheTriple);
Evan Cheng67c033e2011-07-18 22:29:13 +000082
83 // Initial state of the frame pointer is R1.
Rafael Espindola227144c2013-05-13 01:16:13 +000084 unsigned Reg = isPPC64 ? PPC::X1 : PPC::R1;
85 MCCFIInstruction Inst =
Craig Topper062a2ba2014-04-25 05:30:21 +000086 MCCFIInstruction::createDefCfa(nullptr, MRI.getDwarfRegNum(Reg, true), 0);
Rafael Espindola227144c2013-05-13 01:16:13 +000087 MAI->addInitialFrameState(Inst);
Evan Cheng67c033e2011-07-18 22:29:13 +000088
89 return MAI;
Evan Cheng1705ab02011-07-14 23:50:31 +000090}
91
Evan Cheng63765932011-07-23 00:01:04 +000092static MCCodeGenInfo *createPPCMCCodeGenInfo(StringRef TT, Reloc::Model RM,
Evan Chengecb29082011-11-16 08:38:26 +000093 CodeModel::Model CM,
94 CodeGenOpt::Level OL) {
Evan Cheng2129f592011-07-19 06:37:02 +000095 MCCodeGenInfo *X = new MCCodeGenInfo();
96
97 if (RM == Reloc::Default) {
98 Triple T(TT);
99 if (T.isOSDarwin())
100 RM = Reloc::DynamicNoPIC;
101 else
102 RM = Reloc::Static;
103 }
Bill Schmidte0a68a52012-11-27 23:36:26 +0000104 if (CM == CodeModel::Default) {
105 Triple T(TT);
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000106 if (!T.isOSDarwin() &&
107 (T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le))
Bill Schmidte0a68a52012-11-27 23:36:26 +0000108 CM = CodeModel::Medium;
109 }
Jim Grosbach4c98cf72015-05-15 19:13:31 +0000110 X->initMCCodeGenInfo(RM, CM, OL);
Evan Cheng2129f592011-07-19 06:37:02 +0000111 return X;
112}
113
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000114namespace {
115class PPCTargetAsmStreamer : public PPCTargetStreamer {
116 formatted_raw_ostream &OS;
117
118public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000119 PPCTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
120 : PPCTargetStreamer(S), OS(OS) {}
Craig Topper0d3fa922014-04-29 07:57:37 +0000121 void emitTCEntry(const MCSymbol &S) override {
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000122 OS << "\t.tc ";
123 OS << S.getName();
124 OS << "[TC],";
125 OS << S.getName();
126 OS << '\n';
127 }
Craig Topper0d3fa922014-04-29 07:57:37 +0000128 void emitMachine(StringRef CPU) override {
Rafael Espindola6b9ee9b2014-01-25 02:35:56 +0000129 OS << "\t.machine " << CPU << '\n';
130 }
Craig Topperfd38cbe2014-08-30 16:48:34 +0000131 void emitAbiVersion(int AbiVersion) override {
Ulrich Weigand0daa5162014-07-20 22:56:57 +0000132 OS << "\t.abiversion " << AbiVersion << '\n';
133 }
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000134 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
Ulrich Weigandbb686102014-07-20 23:06:03 +0000135 OS << "\t.localentry\t" << *S << ", " << *LocalOffset << '\n';
136 }
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000137};
138
139class PPCTargetELFStreamer : public PPCTargetStreamer {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000140public:
141 PPCTargetELFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
Ulrich Weigand0daa5162014-07-20 22:56:57 +0000142 MCELFStreamer &getStreamer() {
143 return static_cast<MCELFStreamer &>(Streamer);
144 }
Craig Topperfd38cbe2014-08-30 16:48:34 +0000145 void emitTCEntry(const MCSymbol &S) override {
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000146 // Creates a R_PPC64_TOC relocation
Bill Schmidte3959eb2015-02-27 22:14:10 +0000147 Streamer.EmitValueToAlignment(8);
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000148 Streamer.EmitSymbolValue(&S, 8);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000149 }
Craig Topper0d3fa922014-04-29 07:57:37 +0000150 void emitMachine(StringRef CPU) override {
Rafael Espindola6b9ee9b2014-01-25 02:35:56 +0000151 // FIXME: Is there anything to do in here or does this directive only
152 // limit the parser?
153 }
Craig Topperfd38cbe2014-08-30 16:48:34 +0000154 void emitAbiVersion(int AbiVersion) override {
Ulrich Weigand0daa5162014-07-20 22:56:57 +0000155 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);
160 }
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000161 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
Ulrich Weigandbb686102014-07-20 23:06:03 +0000162 MCAssembler &MCA = getStreamer().getAssembler();
Ulrich Weigandbb686102014-07-20 23:06:03 +0000163
164 int64_t Res;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000165 if (!LocalOffset->evaluateAsAbsolute(Res, MCA))
Ulrich Weigandbb686102014-07-20 23:06:03 +0000166 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
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000172 unsigned Other = S->getOther();
Ulrich Weigandbb686102014-07-20 23:06:03 +0000173 Other &= ~ELF::STO_PPC64_LOCAL_MASK;
174 Other |= Encoded;
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000175 S->setOther(Other);
Ulrich Weigandbb686102014-07-20 23:06:03 +0000176
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 }
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000183 void emitAssignment(MCSymbol *S, const MCExpr *Value) override {
184 auto *Symbol = cast<MCSymbolELF>(S);
Ulrich Weiganda69bcd52014-11-24 18:09:47 +0000185 // When encoding an assignment to set symbol A to symbol B, also copy
186 // the st_other bits encoding the local entry point offset.
187 if (Value->getKind() != MCExpr::SymbolRef)
188 return;
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000189 const auto &RhsSym = cast<MCSymbolELF>(
190 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol());
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000191 unsigned Other = Symbol->getOther();
Ulrich Weiganda69bcd52014-11-24 18:09:47 +0000192 Other &= ~ELF::STO_PPC64_LOCAL_MASK;
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000193 Other |= RhsSym.getOther() & ELF::STO_PPC64_LOCAL_MASK;
194 Symbol->setOther(Other);
Ulrich Weiganda69bcd52014-11-24 18:09:47 +0000195 }
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000196};
Iain Sandoe625b65a2014-01-28 11:03:17 +0000197
198class PPCTargetMachOStreamer : public PPCTargetStreamer {
199public:
200 PPCTargetMachOStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
Craig Topper0d3fa922014-04-29 07:57:37 +0000201 void emitTCEntry(const MCSymbol &S) override {
Iain Sandoe625b65a2014-01-28 11:03:17 +0000202 llvm_unreachable("Unknown pseudo-op: .tc");
203 }
Craig Topper0d3fa922014-04-29 07:57:37 +0000204 void emitMachine(StringRef CPU) override {
Iain Sandoe625b65a2014-01-28 11:03:17 +0000205 // FIXME: We should update the CPUType, CPUSubType in the Object file if
206 // the new values are different from the defaults.
207 }
Craig Topperfd38cbe2014-08-30 16:48:34 +0000208 void emitAbiVersion(int AbiVersion) override {
Ulrich Weigand0daa5162014-07-20 22:56:57 +0000209 llvm_unreachable("Unknown pseudo-op: .abiversion");
210 }
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000211 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
Ulrich Weigandbb686102014-07-20 23:06:03 +0000212 llvm_unreachable("Unknown pseudo-op: .localentry");
213 }
Iain Sandoe625b65a2014-01-28 11:03:17 +0000214};
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000215}
216
Rafael Espindola73870dd2015-03-16 21:43:42 +0000217static MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
218 formatted_raw_ostream &OS,
219 MCInstPrinter *InstPrint,
220 bool isVerboseAsm) {
221 return new PPCTargetAsmStreamer(S, OS);
Evan Cheng61d4a202011-07-25 19:53:23 +0000222}
223
Rafael Espindolacd584a82015-03-19 01:50:16 +0000224static MCTargetStreamer *
225createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
226 Triple TT(STI.getTargetTriple());
227 if (TT.getObjectFormat() == Triple::ELF)
228 return new PPCTargetELFStreamer(S);
229 return new PPCTargetMachOStreamer(S);
230}
231
Eric Christopherf8019402015-03-31 00:10:04 +0000232static MCInstPrinter *createPPCMCInstPrinter(const Triple &T,
233 unsigned SyntaxVariant,
James Molloy4c493e82011-09-07 17:24:38 +0000234 const MCAsmInfo &MAI,
Craig Topper54bfde72012-04-02 06:09:36 +0000235 const MCInstrInfo &MII,
Eric Christopherf8019402015-03-31 00:10:04 +0000236 const MCRegisterInfo &MRI) {
237 return new PPCInstPrinter(MAI, MII, MRI, T.isOSDarwin());
Evan Cheng61faa552011-07-25 21:20:24 +0000238}
239
Evan Cheng8c886a42011-07-22 21:58:54 +0000240extern "C" void LLVMInitializePowerPCTargetMC() {
Rafael Espindola69244c32015-03-18 23:15:49 +0000241 for (Target *T : {&ThePPC32Target, &ThePPC64Target, &ThePPC64LETarget}) {
242 // Register the MC asm info.
243 RegisterMCAsmInfoFn C(*T, createPPCMCAsmInfo);
Evan Cheng8c886a42011-07-22 21:58:54 +0000244
Rafael Espindola69244c32015-03-18 23:15:49 +0000245 // Register the MC codegen info.
246 TargetRegistry::RegisterMCCodeGenInfo(*T, createPPCMCCodeGenInfo);
Evan Cheng8c886a42011-07-22 21:58:54 +0000247
Rafael Espindola69244c32015-03-18 23:15:49 +0000248 // Register the MC instruction info.
249 TargetRegistry::RegisterMCInstrInfo(*T, createPPCMCInstrInfo);
Evan Cheng8c886a42011-07-22 21:58:54 +0000250
Rafael Espindola69244c32015-03-18 23:15:49 +0000251 // Register the MC register info.
252 TargetRegistry::RegisterMCRegInfo(*T, createPPCMCRegisterInfo);
Evan Cheng8c886a42011-07-22 21:58:54 +0000253
Rafael Espindola69244c32015-03-18 23:15:49 +0000254 // Register the MC subtarget info.
255 TargetRegistry::RegisterMCSubtargetInfo(*T, createPPCMCSubtargetInfo);
Evan Cheng61d4a202011-07-25 19:53:23 +0000256
Rafael Espindola69244c32015-03-18 23:15:49 +0000257 // Register the MC Code Emitter
258 TargetRegistry::RegisterMCCodeEmitter(*T, createPPCMCCodeEmitter);
259
Evan Cheng61d4a202011-07-25 19:53:23 +0000260 // Register the asm backend.
Rafael Espindola69244c32015-03-18 23:15:49 +0000261 TargetRegistry::RegisterMCAsmBackend(*T, createPPCAsmBackend);
Evan Cheng61faa552011-07-25 21:20:24 +0000262
Rafael Espindolacd584a82015-03-19 01:50:16 +0000263 // Register the object target streamer.
264 TargetRegistry::RegisterObjectTargetStreamer(*T,
265 createObjectTargetStreamer);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000266
Rafael Espindola69244c32015-03-18 23:15:49 +0000267 // Register the asm target streamer.
268 TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer);
269
270 // Register the MCInstPrinter.
271 TargetRegistry::RegisterMCInstPrinter(*T, createPPCMCInstPrinter);
272 }
Evan Cheng2129f592011-07-19 06:37:02 +0000273}