blob: d3031c686aae609dbb03f472b3e3b82c1c9588e8 [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
Bill Schmidte0a68a52012-11-27 23:36:26 +000095 if (CM == CodeModel::Default) {
Daniel Sanders50f17232015-09-15 16:17:27 +000096 if (!TT.isOSDarwin() &&
97 (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le))
Bill Schmidte0a68a52012-11-27 23:36:26 +000098 CM = CodeModel::Medium;
99 }
Jim Grosbach4c98cf72015-05-15 19:13:31 +0000100 X->initMCCodeGenInfo(RM, CM, OL);
Evan Cheng2129f592011-07-19 06:37:02 +0000101 return X;
102}
103
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000104namespace {
105class PPCTargetAsmStreamer : public PPCTargetStreamer {
106 formatted_raw_ostream &OS;
107
108public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000109 PPCTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
110 : PPCTargetStreamer(S), OS(OS) {}
Craig Topper0d3fa922014-04-29 07:57:37 +0000111 void emitTCEntry(const MCSymbol &S) override {
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000112 OS << "\t.tc ";
113 OS << S.getName();
114 OS << "[TC],";
115 OS << S.getName();
116 OS << '\n';
117 }
Craig Topper0d3fa922014-04-29 07:57:37 +0000118 void emitMachine(StringRef CPU) override {
Rafael Espindola6b9ee9b2014-01-25 02:35:56 +0000119 OS << "\t.machine " << CPU << '\n';
120 }
Craig Topperfd38cbe2014-08-30 16:48:34 +0000121 void emitAbiVersion(int AbiVersion) override {
Ulrich Weigand0daa5162014-07-20 22:56:57 +0000122 OS << "\t.abiversion " << AbiVersion << '\n';
123 }
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000124 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
Matt Arsenault8b643552015-06-09 00:31:39 +0000125 const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
126
127 OS << "\t.localentry\t";
128 S->print(OS, MAI);
129 OS << ", ";
130 LocalOffset->print(OS, MAI);
131 OS << '\n';
Ulrich Weigandbb686102014-07-20 23:06:03 +0000132 }
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000133};
134
135class PPCTargetELFStreamer : public PPCTargetStreamer {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000136public:
137 PPCTargetELFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
Ulrich Weigand0daa5162014-07-20 22:56:57 +0000138 MCELFStreamer &getStreamer() {
139 return static_cast<MCELFStreamer &>(Streamer);
140 }
Craig Topperfd38cbe2014-08-30 16:48:34 +0000141 void emitTCEntry(const MCSymbol &S) override {
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000142 // Creates a R_PPC64_TOC relocation
Bill Schmidte3959eb2015-02-27 22:14:10 +0000143 Streamer.EmitValueToAlignment(8);
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000144 Streamer.EmitSymbolValue(&S, 8);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000145 }
Craig Topper0d3fa922014-04-29 07:57:37 +0000146 void emitMachine(StringRef CPU) override {
Rafael Espindola6b9ee9b2014-01-25 02:35:56 +0000147 // FIXME: Is there anything to do in here or does this directive only
148 // limit the parser?
149 }
Craig Topperfd38cbe2014-08-30 16:48:34 +0000150 void emitAbiVersion(int AbiVersion) override {
Ulrich Weigand0daa5162014-07-20 22:56:57 +0000151 MCAssembler &MCA = getStreamer().getAssembler();
152 unsigned Flags = MCA.getELFHeaderEFlags();
153 Flags &= ~ELF::EF_PPC64_ABI;
154 Flags |= (AbiVersion & ELF::EF_PPC64_ABI);
155 MCA.setELFHeaderEFlags(Flags);
156 }
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000157 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
Ulrich Weigandbb686102014-07-20 23:06:03 +0000158 MCAssembler &MCA = getStreamer().getAssembler();
Ulrich Weigandbb686102014-07-20 23:06:03 +0000159
160 int64_t Res;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000161 if (!LocalOffset->evaluateAsAbsolute(Res, MCA))
Ulrich Weigandbb686102014-07-20 23:06:03 +0000162 report_fatal_error(".localentry expression must be absolute.");
163
164 unsigned Encoded = ELF::encodePPC64LocalEntryOffset(Res);
165 if (Res != ELF::decodePPC64LocalEntryOffset(Encoded))
166 report_fatal_error(".localentry expression cannot be encoded.");
167
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000168 unsigned Other = S->getOther();
Ulrich Weigandbb686102014-07-20 23:06:03 +0000169 Other &= ~ELF::STO_PPC64_LOCAL_MASK;
170 Other |= Encoded;
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000171 S->setOther(Other);
Ulrich Weigandbb686102014-07-20 23:06:03 +0000172
173 // For GAS compatibility, unless we already saw a .abiversion directive,
174 // set e_flags to indicate ELFv2 ABI.
175 unsigned Flags = MCA.getELFHeaderEFlags();
176 if ((Flags & ELF::EF_PPC64_ABI) == 0)
177 MCA.setELFHeaderEFlags(Flags | 2);
178 }
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000179 void emitAssignment(MCSymbol *S, const MCExpr *Value) override {
180 auto *Symbol = cast<MCSymbolELF>(S);
Ulrich Weiganda69bcd52014-11-24 18:09:47 +0000181 // When encoding an assignment to set symbol A to symbol B, also copy
182 // the st_other bits encoding the local entry point offset.
183 if (Value->getKind() != MCExpr::SymbolRef)
184 return;
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000185 const auto &RhsSym = cast<MCSymbolELF>(
186 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol());
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000187 unsigned Other = Symbol->getOther();
Ulrich Weiganda69bcd52014-11-24 18:09:47 +0000188 Other &= ~ELF::STO_PPC64_LOCAL_MASK;
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000189 Other |= RhsSym.getOther() & ELF::STO_PPC64_LOCAL_MASK;
190 Symbol->setOther(Other);
Ulrich Weiganda69bcd52014-11-24 18:09:47 +0000191 }
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000192};
Iain Sandoe625b65a2014-01-28 11:03:17 +0000193
194class PPCTargetMachOStreamer : public PPCTargetStreamer {
195public:
196 PPCTargetMachOStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
Craig Topper0d3fa922014-04-29 07:57:37 +0000197 void emitTCEntry(const MCSymbol &S) override {
Iain Sandoe625b65a2014-01-28 11:03:17 +0000198 llvm_unreachable("Unknown pseudo-op: .tc");
199 }
Craig Topper0d3fa922014-04-29 07:57:37 +0000200 void emitMachine(StringRef CPU) override {
Iain Sandoe625b65a2014-01-28 11:03:17 +0000201 // FIXME: We should update the CPUType, CPUSubType in the Object file if
202 // the new values are different from the defaults.
203 }
Craig Topperfd38cbe2014-08-30 16:48:34 +0000204 void emitAbiVersion(int AbiVersion) override {
Ulrich Weigand0daa5162014-07-20 22:56:57 +0000205 llvm_unreachable("Unknown pseudo-op: .abiversion");
206 }
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000207 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
Ulrich Weigandbb686102014-07-20 23:06:03 +0000208 llvm_unreachable("Unknown pseudo-op: .localentry");
209 }
Iain Sandoe625b65a2014-01-28 11:03:17 +0000210};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000211}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000212
Rafael Espindola73870dd2015-03-16 21:43:42 +0000213static MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
214 formatted_raw_ostream &OS,
215 MCInstPrinter *InstPrint,
216 bool isVerboseAsm) {
217 return new PPCTargetAsmStreamer(S, OS);
Evan Cheng61d4a202011-07-25 19:53:23 +0000218}
219
Rafael Espindolacd584a82015-03-19 01:50:16 +0000220static MCTargetStreamer *
221createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
Daniel Sanders50f17232015-09-15 16:17:27 +0000222 const Triple &TT = STI.getTargetTriple();
Daniel Sandersfbdab432015-07-06 16:33:18 +0000223 if (TT.isOSBinFormatELF())
Rafael Espindolacd584a82015-03-19 01:50:16 +0000224 return new PPCTargetELFStreamer(S);
225 return new PPCTargetMachOStreamer(S);
226}
227
Daniel Sanders50f17232015-09-15 16:17:27 +0000228static MCInstPrinter *createPPCMCInstPrinter(const Triple &T,
Eric Christopherf8019402015-03-31 00:10:04 +0000229 unsigned SyntaxVariant,
James Molloy4c493e82011-09-07 17:24:38 +0000230 const MCAsmInfo &MAI,
Craig Topper54bfde72012-04-02 06:09:36 +0000231 const MCInstrInfo &MII,
Eric Christopherf8019402015-03-31 00:10:04 +0000232 const MCRegisterInfo &MRI) {
Daniel Sanders50f17232015-09-15 16:17:27 +0000233 return new PPCInstPrinter(MAI, MII, MRI, T.isOSDarwin());
Evan Cheng61faa552011-07-25 21:20:24 +0000234}
235
Evan Cheng8c886a42011-07-22 21:58:54 +0000236extern "C" void LLVMInitializePowerPCTargetMC() {
Rafael Espindola69244c32015-03-18 23:15:49 +0000237 for (Target *T : {&ThePPC32Target, &ThePPC64Target, &ThePPC64LETarget}) {
238 // Register the MC asm info.
239 RegisterMCAsmInfoFn C(*T, createPPCMCAsmInfo);
Evan Cheng8c886a42011-07-22 21:58:54 +0000240
Rafael Espindola69244c32015-03-18 23:15:49 +0000241 // Register the MC codegen info.
242 TargetRegistry::RegisterMCCodeGenInfo(*T, createPPCMCCodeGenInfo);
Evan Cheng8c886a42011-07-22 21:58:54 +0000243
Rafael Espindola69244c32015-03-18 23:15:49 +0000244 // Register the MC instruction info.
245 TargetRegistry::RegisterMCInstrInfo(*T, createPPCMCInstrInfo);
Evan Cheng8c886a42011-07-22 21:58:54 +0000246
Rafael Espindola69244c32015-03-18 23:15:49 +0000247 // Register the MC register info.
248 TargetRegistry::RegisterMCRegInfo(*T, createPPCMCRegisterInfo);
Evan Cheng8c886a42011-07-22 21:58:54 +0000249
Rafael Espindola69244c32015-03-18 23:15:49 +0000250 // Register the MC subtarget info.
251 TargetRegistry::RegisterMCSubtargetInfo(*T, createPPCMCSubtargetInfo);
Evan Cheng61d4a202011-07-25 19:53:23 +0000252
Rafael Espindola69244c32015-03-18 23:15:49 +0000253 // Register the MC Code Emitter
254 TargetRegistry::RegisterMCCodeEmitter(*T, createPPCMCCodeEmitter);
255
Evan Cheng61d4a202011-07-25 19:53:23 +0000256 // Register the asm backend.
Rafael Espindola69244c32015-03-18 23:15:49 +0000257 TargetRegistry::RegisterMCAsmBackend(*T, createPPCAsmBackend);
Evan Cheng61faa552011-07-25 21:20:24 +0000258
Rafael Espindolacd584a82015-03-19 01:50:16 +0000259 // Register the object target streamer.
260 TargetRegistry::RegisterObjectTargetStreamer(*T,
261 createObjectTargetStreamer);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000262
Rafael Espindola69244c32015-03-18 23:15:49 +0000263 // Register the asm target streamer.
264 TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer);
265
266 // Register the MCInstPrinter.
267 TargetRegistry::RegisterMCInstPrinter(*T, createPPCMCInstPrinter);
268 }
Evan Cheng2129f592011-07-19 06:37:02 +0000269}