blob: 5c38fe173d961c44506677d9f29eb7cd03820050 [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"
Matt Arsenault8b643552015-06-09 00:31:39 +000019#include "llvm/MC/MCContext.h"
Ulrich Weigand0daa5162014-07-20 22:56:57 +000020#include "llvm/MC/MCELFStreamer.h"
Ulrich Weigandbb686102014-07-20 23:06:03 +000021#include "llvm/MC/MCExpr.h"
Evan Chengbc153d42011-07-14 20:59:42 +000022#include "llvm/MC/MCInstrInfo.h"
23#include "llvm/MC/MCRegisterInfo.h"
Evan Cheng61d4a202011-07-25 19:53:23 +000024#include "llvm/MC/MCStreamer.h"
Evan Chengbc153d42011-07-14 20:59:42 +000025#include "llvm/MC/MCSubtargetInfo.h"
Rafael Espindola95fb9b92015-06-02 20:38:46 +000026#include "llvm/MC/MCSymbolELF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/MC/MachineLocation.h"
Ulrich Weigand0daa5162014-07-20 22:56:57 +000028#include "llvm/Support/ELF.h"
Craig Topperc4965bc2012-02-05 07:21:30 +000029#include "llvm/Support/ErrorHandling.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000030#include "llvm/Support/FormattedStream.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000031#include "llvm/Support/TargetRegistry.h"
Evan Chengbc153d42011-07-14 20:59:42 +000032
Chandler Carruthd174b722014-04-22 02:03:14 +000033using namespace llvm;
34
Evan Chengbc153d42011-07-14 20:59:42 +000035#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
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000044// Pin the vtable to this file.
45PPCTargetStreamer::~PPCTargetStreamer() {}
Rafael Espindola24ea09e2014-01-26 06:06:37 +000046PPCTargetStreamer::PPCTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000047
Evan Cheng1705ab02011-07-14 23:50:31 +000048static MCInstrInfo *createPPCMCInstrInfo() {
Evan Chengbc153d42011-07-14 20:59:42 +000049 MCInstrInfo *X = new MCInstrInfo();
50 InitPPCMCInstrInfo(X);
51 return X;
52}
53
Evan Chengd60fa58b2011-07-18 20:57:22 +000054static MCRegisterInfo *createPPCMCRegisterInfo(StringRef TT) {
55 Triple TheTriple(TT);
Bill Schmidt0a9170d2013-07-26 01:35:43 +000056 bool isPPC64 = (TheTriple.getArch() == Triple::ppc64 ||
57 TheTriple.getArch() == Triple::ppc64le);
Evan Chengd60fa58b2011-07-18 20:57:22 +000058 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
Daniel Sandersa73f1fd2015-06-10 12:11:26 +000066static MCSubtargetInfo *createPPCMCSubtargetInfo(const Triple &TT,
67 StringRef CPU, StringRef FS) {
Evan Chengbc153d42011-07-14 20:59:42 +000068 MCSubtargetInfo *X = new MCSubtargetInfo();
69 InitPPCMCSubtargetInfo(X, TT, CPU, FS);
70 return X;
71}
72
Daniel Sanders7813ae82015-06-04 13:12:25 +000073static MCAsmInfo *createPPCMCAsmInfo(const MCRegisterInfo &MRI,
74 const Triple &TheTriple) {
Bill Schmidt0a9170d2013-07-26 01:35:43 +000075 bool isPPC64 = (TheTriple.getArch() == Triple::ppc64 ||
76 TheTriple.getArch() == Triple::ppc64le);
Evan Cheng67c033e2011-07-18 22:29:13 +000077
78 MCAsmInfo *MAI;
Evan Cheng1705ab02011-07-14 23:50:31 +000079 if (TheTriple.isOSDarwin())
David Fang1b018492013-12-10 21:37:41 +000080 MAI = new PPCMCAsmInfoDarwin(isPPC64, TheTriple);
Evan Cheng67c033e2011-07-18 22:29:13 +000081 else
Joerg Sonnenberger5f233fc2014-08-04 18:46:13 +000082 MAI = new PPCELFMCAsmInfo(isPPC64, TheTriple);
Evan Cheng67c033e2011-07-18 22:29:13 +000083
84 // Initial state of the frame pointer is R1.
Rafael Espindola227144c2013-05-13 01:16:13 +000085 unsigned Reg = isPPC64 ? PPC::X1 : PPC::R1;
86 MCCFIInstruction Inst =
Craig Topper062a2ba2014-04-25 05:30:21 +000087 MCCFIInstruction::createDefCfa(nullptr, MRI.getDwarfRegNum(Reg, true), 0);
Rafael Espindola227144c2013-05-13 01:16:13 +000088 MAI->addInitialFrameState(Inst);
Evan Cheng67c033e2011-07-18 22:29:13 +000089
90 return MAI;
Evan Cheng1705ab02011-07-14 23:50:31 +000091}
92
Evan Cheng63765932011-07-23 00:01:04 +000093static MCCodeGenInfo *createPPCMCCodeGenInfo(StringRef TT, Reloc::Model RM,
Evan Chengecb29082011-11-16 08:38:26 +000094 CodeModel::Model CM,
95 CodeGenOpt::Level OL) {
Evan Cheng2129f592011-07-19 06:37:02 +000096 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 }
Bill Schmidte0a68a52012-11-27 23:36:26 +0000105 if (CM == CodeModel::Default) {
106 Triple T(TT);
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000107 if (!T.isOSDarwin() &&
108 (T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le))
Bill Schmidte0a68a52012-11-27 23:36:26 +0000109 CM = CodeModel::Medium;
110 }
Jim Grosbach4c98cf72015-05-15 19:13:31 +0000111 X->initMCCodeGenInfo(RM, CM, OL);
Evan Cheng2129f592011-07-19 06:37:02 +0000112 return X;
113}
114
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000115namespace {
116class PPCTargetAsmStreamer : public PPCTargetStreamer {
117 formatted_raw_ostream &OS;
118
119public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000120 PPCTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
121 : PPCTargetStreamer(S), OS(OS) {}
Craig Topper0d3fa922014-04-29 07:57:37 +0000122 void emitTCEntry(const MCSymbol &S) override {
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000123 OS << "\t.tc ";
124 OS << S.getName();
125 OS << "[TC],";
126 OS << S.getName();
127 OS << '\n';
128 }
Craig Topper0d3fa922014-04-29 07:57:37 +0000129 void emitMachine(StringRef CPU) override {
Rafael Espindola6b9ee9b2014-01-25 02:35:56 +0000130 OS << "\t.machine " << CPU << '\n';
131 }
Craig Topperfd38cbe2014-08-30 16:48:34 +0000132 void emitAbiVersion(int AbiVersion) override {
Ulrich Weigand0daa5162014-07-20 22:56:57 +0000133 OS << "\t.abiversion " << AbiVersion << '\n';
134 }
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000135 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
Matt Arsenault8b643552015-06-09 00:31:39 +0000136 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';
Ulrich Weigandbb686102014-07-20 23:06:03 +0000143 }
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000144};
145
146class PPCTargetELFStreamer : public PPCTargetStreamer {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000147public:
148 PPCTargetELFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
Ulrich Weigand0daa5162014-07-20 22:56:57 +0000149 MCELFStreamer &getStreamer() {
150 return static_cast<MCELFStreamer &>(Streamer);
151 }
Craig Topperfd38cbe2014-08-30 16:48:34 +0000152 void emitTCEntry(const MCSymbol &S) override {
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000153 // Creates a R_PPC64_TOC relocation
Bill Schmidte3959eb2015-02-27 22:14:10 +0000154 Streamer.EmitValueToAlignment(8);
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000155 Streamer.EmitSymbolValue(&S, 8);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000156 }
Craig Topper0d3fa922014-04-29 07:57:37 +0000157 void emitMachine(StringRef CPU) override {
Rafael Espindola6b9ee9b2014-01-25 02:35:56 +0000158 // FIXME: Is there anything to do in here or does this directive only
159 // limit the parser?
160 }
Craig Topperfd38cbe2014-08-30 16:48:34 +0000161 void emitAbiVersion(int AbiVersion) override {
Ulrich Weigand0daa5162014-07-20 22:56:57 +0000162 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 }
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000168 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
Ulrich Weigandbb686102014-07-20 23:06:03 +0000169 MCAssembler &MCA = getStreamer().getAssembler();
Ulrich Weigandbb686102014-07-20 23:06:03 +0000170
171 int64_t Res;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000172 if (!LocalOffset->evaluateAsAbsolute(Res, MCA))
Ulrich Weigandbb686102014-07-20 23:06:03 +0000173 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
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000179 unsigned Other = S->getOther();
Ulrich Weigandbb686102014-07-20 23:06:03 +0000180 Other &= ~ELF::STO_PPC64_LOCAL_MASK;
181 Other |= Encoded;
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000182 S->setOther(Other);
Ulrich Weigandbb686102014-07-20 23:06:03 +0000183
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 }
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000190 void emitAssignment(MCSymbol *S, const MCExpr *Value) override {
191 auto *Symbol = cast<MCSymbolELF>(S);
Ulrich Weiganda69bcd52014-11-24 18:09:47 +0000192 // 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;
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000196 const auto &RhsSym = cast<MCSymbolELF>(
197 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol());
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000198 unsigned Other = Symbol->getOther();
Ulrich Weiganda69bcd52014-11-24 18:09:47 +0000199 Other &= ~ELF::STO_PPC64_LOCAL_MASK;
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000200 Other |= RhsSym.getOther() & ELF::STO_PPC64_LOCAL_MASK;
201 Symbol->setOther(Other);
Ulrich Weiganda69bcd52014-11-24 18:09:47 +0000202 }
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000203};
Iain Sandoe625b65a2014-01-28 11:03:17 +0000204
205class PPCTargetMachOStreamer : public PPCTargetStreamer {
206public:
207 PPCTargetMachOStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
Craig Topper0d3fa922014-04-29 07:57:37 +0000208 void emitTCEntry(const MCSymbol &S) override {
Iain Sandoe625b65a2014-01-28 11:03:17 +0000209 llvm_unreachable("Unknown pseudo-op: .tc");
210 }
Craig Topper0d3fa922014-04-29 07:57:37 +0000211 void emitMachine(StringRef CPU) override {
Iain Sandoe625b65a2014-01-28 11:03:17 +0000212 // FIXME: We should update the CPUType, CPUSubType in the Object file if
213 // the new values are different from the defaults.
214 }
Craig Topperfd38cbe2014-08-30 16:48:34 +0000215 void emitAbiVersion(int AbiVersion) override {
Ulrich Weigand0daa5162014-07-20 22:56:57 +0000216 llvm_unreachable("Unknown pseudo-op: .abiversion");
217 }
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000218 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
Ulrich Weigandbb686102014-07-20 23:06:03 +0000219 llvm_unreachable("Unknown pseudo-op: .localentry");
220 }
Iain Sandoe625b65a2014-01-28 11:03:17 +0000221};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000222}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000223
Rafael Espindola73870dd2015-03-16 21:43:42 +0000224static MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
225 formatted_raw_ostream &OS,
226 MCInstPrinter *InstPrint,
227 bool isVerboseAsm) {
228 return new PPCTargetAsmStreamer(S, OS);
Evan Cheng61d4a202011-07-25 19:53:23 +0000229}
230
Rafael Espindolacd584a82015-03-19 01:50:16 +0000231static MCTargetStreamer *
232createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
Daniel Sandersc81f4502015-06-16 15:44:21 +0000233 const Triple &TT = STI.getTargetTriple();
Rafael Espindolacd584a82015-03-19 01:50:16 +0000234 if (TT.getObjectFormat() == Triple::ELF)
235 return new PPCTargetELFStreamer(S);
236 return new PPCTargetMachOStreamer(S);
237}
238
Eric Christopherf8019402015-03-31 00:10:04 +0000239static MCInstPrinter *createPPCMCInstPrinter(const Triple &T,
240 unsigned SyntaxVariant,
James Molloy4c493e82011-09-07 17:24:38 +0000241 const MCAsmInfo &MAI,
Craig Topper54bfde72012-04-02 06:09:36 +0000242 const MCInstrInfo &MII,
Eric Christopherf8019402015-03-31 00:10:04 +0000243 const MCRegisterInfo &MRI) {
244 return new PPCInstPrinter(MAI, MII, MRI, T.isOSDarwin());
Evan Cheng61faa552011-07-25 21:20:24 +0000245}
246
Evan Cheng8c886a42011-07-22 21:58:54 +0000247extern "C" void LLVMInitializePowerPCTargetMC() {
Rafael Espindola69244c32015-03-18 23:15:49 +0000248 for (Target *T : {&ThePPC32Target, &ThePPC64Target, &ThePPC64LETarget}) {
249 // Register the MC asm info.
250 RegisterMCAsmInfoFn C(*T, createPPCMCAsmInfo);
Evan Cheng8c886a42011-07-22 21:58:54 +0000251
Rafael Espindola69244c32015-03-18 23:15:49 +0000252 // Register the MC codegen info.
253 TargetRegistry::RegisterMCCodeGenInfo(*T, createPPCMCCodeGenInfo);
Evan Cheng8c886a42011-07-22 21:58:54 +0000254
Rafael Espindola69244c32015-03-18 23:15:49 +0000255 // Register the MC instruction info.
256 TargetRegistry::RegisterMCInstrInfo(*T, createPPCMCInstrInfo);
Evan Cheng8c886a42011-07-22 21:58:54 +0000257
Rafael Espindola69244c32015-03-18 23:15:49 +0000258 // Register the MC register info.
259 TargetRegistry::RegisterMCRegInfo(*T, createPPCMCRegisterInfo);
Evan Cheng8c886a42011-07-22 21:58:54 +0000260
Rafael Espindola69244c32015-03-18 23:15:49 +0000261 // Register the MC subtarget info.
262 TargetRegistry::RegisterMCSubtargetInfo(*T, createPPCMCSubtargetInfo);
Evan Cheng61d4a202011-07-25 19:53:23 +0000263
Rafael Espindola69244c32015-03-18 23:15:49 +0000264 // Register the MC Code Emitter
265 TargetRegistry::RegisterMCCodeEmitter(*T, createPPCMCCodeEmitter);
266
Evan Cheng61d4a202011-07-25 19:53:23 +0000267 // Register the asm backend.
Rafael Espindola69244c32015-03-18 23:15:49 +0000268 TargetRegistry::RegisterMCAsmBackend(*T, createPPCAsmBackend);
Evan Cheng61faa552011-07-25 21:20:24 +0000269
Rafael Espindolacd584a82015-03-19 01:50:16 +0000270 // Register the object target streamer.
271 TargetRegistry::RegisterObjectTargetStreamer(*T,
272 createObjectTargetStreamer);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000273
Rafael Espindola69244c32015-03-18 23:15:49 +0000274 // Register the asm target streamer.
275 TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer);
276
277 // Register the MCInstPrinter.
278 TargetRegistry::RegisterMCInstPrinter(*T, createPPCMCInstPrinter);
279 }
Evan Cheng2129f592011-07-19 06:37:02 +0000280}