blob: 30f232a9a91e0a310b7f4508bad224ec85f55f47 [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
Daniel Sanders50f17232015-09-15 16:17:27 +000054static MCRegisterInfo *createPPCMCRegisterInfo(const Triple &TT) {
55 bool isPPC64 =
56 (TT.getArch() == Triple::ppc64 || TT.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
Daniel Sanders50f17232015-09-15 16:17:27 +000065static MCSubtargetInfo *createPPCMCSubtargetInfo(const Triple &TT,
Daniel Sandersa73f1fd2015-06-10 12:11:26 +000066 StringRef CPU, StringRef FS) {
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +000067 return createPPCMCSubtargetInfoImpl(TT, CPU, FS);
Evan Chengbc153d42011-07-14 20:59:42 +000068}
69
Daniel Sanders7813ae82015-06-04 13:12:25 +000070static MCAsmInfo *createPPCMCAsmInfo(const MCRegisterInfo &MRI,
Daniel Sanders50f17232015-09-15 16:17:27 +000071 const Triple &TheTriple) {
72 bool isPPC64 = (TheTriple.getArch() == Triple::ppc64 ||
73 TheTriple.getArch() == Triple::ppc64le);
Evan Cheng67c033e2011-07-18 22:29:13 +000074
75 MCAsmInfo *MAI;
Daniel Sanders50f17232015-09-15 16:17:27 +000076 if (TheTriple.isOSDarwin())
77 MAI = new PPCMCAsmInfoDarwin(isPPC64, TheTriple);
Evan Cheng67c033e2011-07-18 22:29:13 +000078 else
Daniel Sanders50f17232015-09-15 16:17:27 +000079 MAI = new PPCELFMCAsmInfo(isPPC64, TheTriple);
Evan Cheng67c033e2011-07-18 22:29:13 +000080
81 // Initial state of the frame pointer is R1.
Rafael Espindola227144c2013-05-13 01:16:13 +000082 unsigned Reg = isPPC64 ? PPC::X1 : PPC::R1;
83 MCCFIInstruction Inst =
Craig Topper062a2ba2014-04-25 05:30:21 +000084 MCCFIInstruction::createDefCfa(nullptr, MRI.getDwarfRegNum(Reg, true), 0);
Rafael Espindola227144c2013-05-13 01:16:13 +000085 MAI->addInitialFrameState(Inst);
Evan Cheng67c033e2011-07-18 22:29:13 +000086
87 return MAI;
Evan Cheng1705ab02011-07-14 23:50:31 +000088}
89
Daniel Sanders50f17232015-09-15 16:17:27 +000090static MCCodeGenInfo *createPPCMCCodeGenInfo(const Triple &TT, Reloc::Model RM,
Evan Chengecb29082011-11-16 08:38:26 +000091 CodeModel::Model CM,
92 CodeGenOpt::Level OL) {
Evan Cheng2129f592011-07-19 06:37:02 +000093 MCCodeGenInfo *X = new MCCodeGenInfo();
94
95 if (RM == Reloc::Default) {
Daniel Sandersf423f562015-07-06 16:56:07 +000096 if (TT.isOSDarwin())
Evan Cheng2129f592011-07-19 06:37:02 +000097 RM = Reloc::DynamicNoPIC;
98 else
99 RM = Reloc::Static;
100 }
Bill Schmidte0a68a52012-11-27 23:36:26 +0000101 if (CM == CodeModel::Default) {
Daniel Sanders50f17232015-09-15 16:17:27 +0000102 if (!TT.isOSDarwin() &&
103 (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le))
Bill Schmidte0a68a52012-11-27 23:36:26 +0000104 CM = CodeModel::Medium;
105 }
Jim Grosbach4c98cf72015-05-15 19:13:31 +0000106 X->initMCCodeGenInfo(RM, CM, OL);
Evan Cheng2129f592011-07-19 06:37:02 +0000107 return X;
108}
109
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000110namespace {
111class PPCTargetAsmStreamer : public PPCTargetStreamer {
112 formatted_raw_ostream &OS;
113
114public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000115 PPCTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
116 : PPCTargetStreamer(S), OS(OS) {}
Craig Topper0d3fa922014-04-29 07:57:37 +0000117 void emitTCEntry(const MCSymbol &S) override {
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000118 OS << "\t.tc ";
119 OS << S.getName();
120 OS << "[TC],";
121 OS << S.getName();
122 OS << '\n';
123 }
Craig Topper0d3fa922014-04-29 07:57:37 +0000124 void emitMachine(StringRef CPU) override {
Rafael Espindola6b9ee9b2014-01-25 02:35:56 +0000125 OS << "\t.machine " << CPU << '\n';
126 }
Craig Topperfd38cbe2014-08-30 16:48:34 +0000127 void emitAbiVersion(int AbiVersion) override {
Ulrich Weigand0daa5162014-07-20 22:56:57 +0000128 OS << "\t.abiversion " << AbiVersion << '\n';
129 }
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000130 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
Matt Arsenault8b643552015-06-09 00:31:39 +0000131 const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
132
133 OS << "\t.localentry\t";
134 S->print(OS, MAI);
135 OS << ", ";
136 LocalOffset->print(OS, MAI);
137 OS << '\n';
Ulrich Weigandbb686102014-07-20 23:06:03 +0000138 }
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000139};
140
141class PPCTargetELFStreamer : public PPCTargetStreamer {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000142public:
143 PPCTargetELFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
Ulrich Weigand0daa5162014-07-20 22:56:57 +0000144 MCELFStreamer &getStreamer() {
145 return static_cast<MCELFStreamer &>(Streamer);
146 }
Craig Topperfd38cbe2014-08-30 16:48:34 +0000147 void emitTCEntry(const MCSymbol &S) override {
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000148 // Creates a R_PPC64_TOC relocation
Bill Schmidte3959eb2015-02-27 22:14:10 +0000149 Streamer.EmitValueToAlignment(8);
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000150 Streamer.EmitSymbolValue(&S, 8);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000151 }
Craig Topper0d3fa922014-04-29 07:57:37 +0000152 void emitMachine(StringRef CPU) override {
Rafael Espindola6b9ee9b2014-01-25 02:35:56 +0000153 // FIXME: Is there anything to do in here or does this directive only
154 // limit the parser?
155 }
Craig Topperfd38cbe2014-08-30 16:48:34 +0000156 void emitAbiVersion(int AbiVersion) override {
Ulrich Weigand0daa5162014-07-20 22:56:57 +0000157 MCAssembler &MCA = getStreamer().getAssembler();
158 unsigned Flags = MCA.getELFHeaderEFlags();
159 Flags &= ~ELF::EF_PPC64_ABI;
160 Flags |= (AbiVersion & ELF::EF_PPC64_ABI);
161 MCA.setELFHeaderEFlags(Flags);
162 }
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000163 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
Ulrich Weigandbb686102014-07-20 23:06:03 +0000164 MCAssembler &MCA = getStreamer().getAssembler();
Ulrich Weigandbb686102014-07-20 23:06:03 +0000165
166 int64_t Res;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000167 if (!LocalOffset->evaluateAsAbsolute(Res, MCA))
Ulrich Weigandbb686102014-07-20 23:06:03 +0000168 report_fatal_error(".localentry expression must be absolute.");
169
170 unsigned Encoded = ELF::encodePPC64LocalEntryOffset(Res);
171 if (Res != ELF::decodePPC64LocalEntryOffset(Encoded))
172 report_fatal_error(".localentry expression cannot be encoded.");
173
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000174 unsigned Other = S->getOther();
Ulrich Weigandbb686102014-07-20 23:06:03 +0000175 Other &= ~ELF::STO_PPC64_LOCAL_MASK;
176 Other |= Encoded;
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000177 S->setOther(Other);
Ulrich Weigandbb686102014-07-20 23:06:03 +0000178
179 // For GAS compatibility, unless we already saw a .abiversion directive,
180 // set e_flags to indicate ELFv2 ABI.
181 unsigned Flags = MCA.getELFHeaderEFlags();
182 if ((Flags & ELF::EF_PPC64_ABI) == 0)
183 MCA.setELFHeaderEFlags(Flags | 2);
184 }
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000185 void emitAssignment(MCSymbol *S, const MCExpr *Value) override {
186 auto *Symbol = cast<MCSymbolELF>(S);
Ulrich Weiganda69bcd52014-11-24 18:09:47 +0000187 // 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 (Value->getKind() != MCExpr::SymbolRef)
190 return;
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000191 const auto &RhsSym = cast<MCSymbolELF>(
192 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol());
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000193 unsigned Other = Symbol->getOther();
Ulrich Weiganda69bcd52014-11-24 18:09:47 +0000194 Other &= ~ELF::STO_PPC64_LOCAL_MASK;
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000195 Other |= RhsSym.getOther() & ELF::STO_PPC64_LOCAL_MASK;
196 Symbol->setOther(Other);
Ulrich Weiganda69bcd52014-11-24 18:09:47 +0000197 }
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000198};
Iain Sandoe625b65a2014-01-28 11:03:17 +0000199
200class PPCTargetMachOStreamer : public PPCTargetStreamer {
201public:
202 PPCTargetMachOStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
Craig Topper0d3fa922014-04-29 07:57:37 +0000203 void emitTCEntry(const MCSymbol &S) override {
Iain Sandoe625b65a2014-01-28 11:03:17 +0000204 llvm_unreachable("Unknown pseudo-op: .tc");
205 }
Craig Topper0d3fa922014-04-29 07:57:37 +0000206 void emitMachine(StringRef CPU) override {
Iain Sandoe625b65a2014-01-28 11:03:17 +0000207 // FIXME: We should update the CPUType, CPUSubType in the Object file if
208 // the new values are different from the defaults.
209 }
Craig Topperfd38cbe2014-08-30 16:48:34 +0000210 void emitAbiVersion(int AbiVersion) override {
Ulrich Weigand0daa5162014-07-20 22:56:57 +0000211 llvm_unreachable("Unknown pseudo-op: .abiversion");
212 }
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000213 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
Ulrich Weigandbb686102014-07-20 23:06:03 +0000214 llvm_unreachable("Unknown pseudo-op: .localentry");
215 }
Iain Sandoe625b65a2014-01-28 11:03:17 +0000216};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000217}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000218
Rafael Espindola73870dd2015-03-16 21:43:42 +0000219static MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
220 formatted_raw_ostream &OS,
221 MCInstPrinter *InstPrint,
222 bool isVerboseAsm) {
223 return new PPCTargetAsmStreamer(S, OS);
Evan Cheng61d4a202011-07-25 19:53:23 +0000224}
225
Rafael Espindolacd584a82015-03-19 01:50:16 +0000226static MCTargetStreamer *
227createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
Daniel Sanders50f17232015-09-15 16:17:27 +0000228 const Triple &TT = STI.getTargetTriple();
Daniel Sandersfbdab432015-07-06 16:33:18 +0000229 if (TT.isOSBinFormatELF())
Rafael Espindolacd584a82015-03-19 01:50:16 +0000230 return new PPCTargetELFStreamer(S);
231 return new PPCTargetMachOStreamer(S);
232}
233
Daniel Sanders50f17232015-09-15 16:17:27 +0000234static MCInstPrinter *createPPCMCInstPrinter(const Triple &T,
Eric Christopherf8019402015-03-31 00:10:04 +0000235 unsigned SyntaxVariant,
James Molloy4c493e82011-09-07 17:24:38 +0000236 const MCAsmInfo &MAI,
Craig Topper54bfde72012-04-02 06:09:36 +0000237 const MCInstrInfo &MII,
Eric Christopherf8019402015-03-31 00:10:04 +0000238 const MCRegisterInfo &MRI) {
Daniel Sanders50f17232015-09-15 16:17:27 +0000239 return new PPCInstPrinter(MAI, MII, MRI, T.isOSDarwin());
Evan Cheng61faa552011-07-25 21:20:24 +0000240}
241
Evan Cheng8c886a42011-07-22 21:58:54 +0000242extern "C" void LLVMInitializePowerPCTargetMC() {
Rafael Espindola69244c32015-03-18 23:15:49 +0000243 for (Target *T : {&ThePPC32Target, &ThePPC64Target, &ThePPC64LETarget}) {
244 // Register the MC asm info.
245 RegisterMCAsmInfoFn C(*T, createPPCMCAsmInfo);
Evan Cheng8c886a42011-07-22 21:58:54 +0000246
Rafael Espindola69244c32015-03-18 23:15:49 +0000247 // Register the MC codegen info.
248 TargetRegistry::RegisterMCCodeGenInfo(*T, createPPCMCCodeGenInfo);
Evan Cheng8c886a42011-07-22 21:58:54 +0000249
Rafael Espindola69244c32015-03-18 23:15:49 +0000250 // Register the MC instruction info.
251 TargetRegistry::RegisterMCInstrInfo(*T, createPPCMCInstrInfo);
Evan Cheng8c886a42011-07-22 21:58:54 +0000252
Rafael Espindola69244c32015-03-18 23:15:49 +0000253 // Register the MC register info.
254 TargetRegistry::RegisterMCRegInfo(*T, createPPCMCRegisterInfo);
Evan Cheng8c886a42011-07-22 21:58:54 +0000255
Rafael Espindola69244c32015-03-18 23:15:49 +0000256 // Register the MC subtarget info.
257 TargetRegistry::RegisterMCSubtargetInfo(*T, createPPCMCSubtargetInfo);
Evan Cheng61d4a202011-07-25 19:53:23 +0000258
Rafael Espindola69244c32015-03-18 23:15:49 +0000259 // Register the MC Code Emitter
260 TargetRegistry::RegisterMCCodeEmitter(*T, createPPCMCCodeEmitter);
261
Evan Cheng61d4a202011-07-25 19:53:23 +0000262 // Register the asm backend.
Rafael Espindola69244c32015-03-18 23:15:49 +0000263 TargetRegistry::RegisterMCAsmBackend(*T, createPPCAsmBackend);
Evan Cheng61faa552011-07-25 21:20:24 +0000264
Rafael Espindolacd584a82015-03-19 01:50:16 +0000265 // Register the object target streamer.
266 TargetRegistry::RegisterObjectTargetStreamer(*T,
267 createObjectTargetStreamer);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000268
Rafael Espindola69244c32015-03-18 23:15:49 +0000269 // Register the asm target streamer.
270 TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer);
271
272 // Register the MCInstPrinter.
273 TargetRegistry::RegisterMCInstPrinter(*T, createPPCMCInstPrinter);
274 }
Evan Cheng2129f592011-07-19 06:37:02 +0000275}