blob: 395b4236fa8354ca4bcf1b2726317c23777be548 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- PPCMCTargetDesc.cpp - PowerPC Target Descriptions -----------------===//
Evan Chengbc153d42011-07-14 20:59:42 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Evan Chengbc153d42011-07-14 20:59:42 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file provides PowerPC specific target descriptions.
10//
11//===----------------------------------------------------------------------===//
12
Chandler Carruth6bda14b2017-06-06 11:49:48 +000013#include "MCTargetDesc/PPCMCTargetDesc.h"
Richard Trieu4bdb1362019-05-11 02:33:18 +000014#include "MCTargetDesc/PPCInstPrinter.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000015#include "MCTargetDesc/PPCMCAsmInfo.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000016#include "PPCTargetStreamer.h"
Richard Trieuee6ced12019-05-15 00:09:58 +000017#include "TargetInfo/PowerPCTargetInfo.h"
Fangrui Songcd36a282019-05-21 10:41:25 +000018#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000019#include "llvm/ADT/StringRef.h"
20#include "llvm/ADT/Triple.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000021#include "llvm/BinaryFormat/ELF.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000022#include "llvm/MC/MCAssembler.h"
Matt Arsenault8b643552015-06-09 00:31:39 +000023#include "llvm/MC/MCContext.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000024#include "llvm/MC/MCDwarf.h"
Ulrich Weigand0daa5162014-07-20 22:56:57 +000025#include "llvm/MC/MCELFStreamer.h"
Ulrich Weigandbb686102014-07-20 23:06:03 +000026#include "llvm/MC/MCExpr.h"
Evan Chengbc153d42011-07-14 20:59:42 +000027#include "llvm/MC/MCInstrInfo.h"
28#include "llvm/MC/MCRegisterInfo.h"
Evan Cheng61d4a202011-07-25 19:53:23 +000029#include "llvm/MC/MCStreamer.h"
Evan Chengbc153d42011-07-14 20:59:42 +000030#include "llvm/MC/MCSubtargetInfo.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000031#include "llvm/MC/MCSymbol.h"
Rafael Espindola95fb9b92015-06-02 20:38:46 +000032#include "llvm/MC/MCSymbolELF.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000033#include "llvm/Support/Casting.h"
34#include "llvm/Support/CodeGen.h"
Craig Topperc4965bc2012-02-05 07:21:30 +000035#include "llvm/Support/ErrorHandling.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000036#include "llvm/Support/FormattedStream.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000037#include "llvm/Support/TargetRegistry.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000038#include "llvm/Support/raw_ostream.h"
Evan Chengbc153d42011-07-14 20:59:42 +000039
Chandler Carruthd174b722014-04-22 02:03:14 +000040using namespace llvm;
41
Evan Chengbc153d42011-07-14 20:59:42 +000042#define GET_INSTRINFO_MC_DESC
43#include "PPCGenInstrInfo.inc"
44
45#define GET_SUBTARGETINFO_MC_DESC
46#include "PPCGenSubtargetInfo.inc"
47
48#define GET_REGINFO_MC_DESC
49#include "PPCGenRegisterInfo.inc"
50
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000051// Pin the vtable to this file.
Rafael Espindola24ea09e2014-01-26 06:06:37 +000052PPCTargetStreamer::PPCTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000053
Eugene Zelenko8187c192017-01-13 00:58:58 +000054PPCTargetStreamer::~PPCTargetStreamer() = default;
55
Evan Cheng1705ab02011-07-14 23:50:31 +000056static MCInstrInfo *createPPCMCInstrInfo() {
Evan Chengbc153d42011-07-14 20:59:42 +000057 MCInstrInfo *X = new MCInstrInfo();
58 InitPPCMCInstrInfo(X);
59 return X;
60}
61
Daniel Sanders50f17232015-09-15 16:17:27 +000062static MCRegisterInfo *createPPCMCRegisterInfo(const Triple &TT) {
63 bool isPPC64 =
64 (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le);
Evan Chengd60fa58b2011-07-18 20:57:22 +000065 unsigned Flavour = isPPC64 ? 0 : 1;
66 unsigned RA = isPPC64 ? PPC::LR8 : PPC::LR;
67
68 MCRegisterInfo *X = new MCRegisterInfo();
69 InitPPCMCRegisterInfo(X, RA, Flavour, Flavour);
70 return X;
71}
72
Daniel Sanders50f17232015-09-15 16:17:27 +000073static MCSubtargetInfo *createPPCMCSubtargetInfo(const Triple &TT,
Daniel Sandersa73f1fd2015-06-10 12:11:26 +000074 StringRef CPU, StringRef FS) {
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +000075 return createPPCMCSubtargetInfoImpl(TT, CPU, FS);
Evan Chengbc153d42011-07-14 20:59:42 +000076}
77
Daniel Sanders7813ae82015-06-04 13:12:25 +000078static MCAsmInfo *createPPCMCAsmInfo(const MCRegisterInfo &MRI,
Daniel Sanders50f17232015-09-15 16:17:27 +000079 const Triple &TheTriple) {
80 bool isPPC64 = (TheTriple.getArch() == Triple::ppc64 ||
81 TheTriple.getArch() == Triple::ppc64le);
Evan Cheng67c033e2011-07-18 22:29:13 +000082
83 MCAsmInfo *MAI;
Daniel Sanders50f17232015-09-15 16:17:27 +000084 if (TheTriple.isOSDarwin())
85 MAI = new PPCMCAsmInfoDarwin(isPPC64, TheTriple);
Evan Cheng67c033e2011-07-18 22:29:13 +000086 else
Daniel Sanders50f17232015-09-15 16:17:27 +000087 MAI = new PPCELFMCAsmInfo(isPPC64, TheTriple);
Evan Cheng67c033e2011-07-18 22:29:13 +000088
89 // Initial state of the frame pointer is R1.
Rafael Espindola227144c2013-05-13 01:16:13 +000090 unsigned Reg = isPPC64 ? PPC::X1 : PPC::R1;
91 MCCFIInstruction Inst =
Craig Topper062a2ba2014-04-25 05:30:21 +000092 MCCFIInstruction::createDefCfa(nullptr, MRI.getDwarfRegNum(Reg, true), 0);
Rafael Espindola227144c2013-05-13 01:16:13 +000093 MAI->addInitialFrameState(Inst);
Evan Cheng67c033e2011-07-18 22:29:13 +000094
95 return MAI;
Evan Cheng1705ab02011-07-14 23:50:31 +000096}
97
Rafael Espindolaa17151a2013-10-08 13:08:17 +000098namespace {
Eugene Zelenko8187c192017-01-13 00:58:58 +000099
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000100class PPCTargetAsmStreamer : public PPCTargetStreamer {
101 formatted_raw_ostream &OS;
102
103public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000104 PPCTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
105 : PPCTargetStreamer(S), OS(OS) {}
Eugene Zelenko8187c192017-01-13 00:58:58 +0000106
Craig Topper0d3fa922014-04-29 07:57:37 +0000107 void emitTCEntry(const MCSymbol &S) override {
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000108 OS << "\t.tc ";
109 OS << S.getName();
110 OS << "[TC],";
111 OS << S.getName();
112 OS << '\n';
113 }
Eugene Zelenko8187c192017-01-13 00:58:58 +0000114
Craig Topper0d3fa922014-04-29 07:57:37 +0000115 void emitMachine(StringRef CPU) override {
Rafael Espindola6b9ee9b2014-01-25 02:35:56 +0000116 OS << "\t.machine " << CPU << '\n';
117 }
Eugene Zelenko8187c192017-01-13 00:58:58 +0000118
Craig Topperfd38cbe2014-08-30 16:48:34 +0000119 void emitAbiVersion(int AbiVersion) override {
Ulrich Weigand0daa5162014-07-20 22:56:57 +0000120 OS << "\t.abiversion " << AbiVersion << '\n';
121 }
Eugene Zelenko8187c192017-01-13 00:58:58 +0000122
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000123 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
Matt Arsenault8b643552015-06-09 00:31:39 +0000124 const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
125
126 OS << "\t.localentry\t";
127 S->print(OS, MAI);
128 OS << ", ";
129 LocalOffset->print(OS, MAI);
130 OS << '\n';
Ulrich Weigandbb686102014-07-20 23:06:03 +0000131 }
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000132};
133
134class PPCTargetELFStreamer : public PPCTargetStreamer {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000135public:
136 PPCTargetELFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
Eugene Zelenko8187c192017-01-13 00:58:58 +0000137
Ulrich Weigand0daa5162014-07-20 22:56:57 +0000138 MCELFStreamer &getStreamer() {
139 return static_cast<MCELFStreamer &>(Streamer);
140 }
Eugene Zelenko8187c192017-01-13 00:58:58 +0000141
Craig Topperfd38cbe2014-08-30 16:48:34 +0000142 void emitTCEntry(const MCSymbol &S) override {
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000143 // Creates a R_PPC64_TOC relocation
Bill Schmidte3959eb2015-02-27 22:14:10 +0000144 Streamer.EmitValueToAlignment(8);
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000145 Streamer.EmitSymbolValue(&S, 8);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000146 }
Eugene Zelenko8187c192017-01-13 00:58:58 +0000147
Craig Topper0d3fa922014-04-29 07:57:37 +0000148 void emitMachine(StringRef CPU) override {
Rafael Espindola6b9ee9b2014-01-25 02:35:56 +0000149 // FIXME: Is there anything to do in here or does this directive only
150 // limit the parser?
151 }
Eugene Zelenko8187c192017-01-13 00:58:58 +0000152
Craig Topperfd38cbe2014-08-30 16:48:34 +0000153 void emitAbiVersion(int AbiVersion) override {
Ulrich Weigand0daa5162014-07-20 22:56:57 +0000154 MCAssembler &MCA = getStreamer().getAssembler();
155 unsigned Flags = MCA.getELFHeaderEFlags();
156 Flags &= ~ELF::EF_PPC64_ABI;
157 Flags |= (AbiVersion & ELF::EF_PPC64_ABI);
158 MCA.setELFHeaderEFlags(Flags);
159 }
Eugene Zelenko8187c192017-01-13 00:58:58 +0000160
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 }
Eugene Zelenko8187c192017-01-13 00:58:58 +0000183
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000184 void emitAssignment(MCSymbol *S, const MCExpr *Value) override {
185 auto *Symbol = cast<MCSymbolELF>(S);
Fangrui Songcd36a282019-05-21 10:41:25 +0000186
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.
Fangrui Songcd36a282019-05-21 10:41:25 +0000189 if (copyLocalEntry(Symbol, Value))
190 UpdateOther.insert(Symbol);
191 else
192 UpdateOther.erase(Symbol);
193 }
194
195 void finish() override {
196 for (auto *Sym : UpdateOther)
197 copyLocalEntry(Sym, Sym->getVariableValue());
198 }
199
200private:
201 SmallPtrSet<MCSymbolELF *, 32> UpdateOther;
202
203 bool copyLocalEntry(MCSymbolELF *D, const MCExpr *S) {
204 auto *Ref = dyn_cast<const MCSymbolRefExpr>(S);
205 if (!Ref)
206 return false;
207 const auto &RhsSym = cast<MCSymbolELF>(Ref->getSymbol());
208 unsigned Other = D->getOther();
Ulrich Weiganda69bcd52014-11-24 18:09:47 +0000209 Other &= ~ELF::STO_PPC64_LOCAL_MASK;
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000210 Other |= RhsSym.getOther() & ELF::STO_PPC64_LOCAL_MASK;
Fangrui Songcd36a282019-05-21 10:41:25 +0000211 D->setOther(Other);
212 return true;
Ulrich Weiganda69bcd52014-11-24 18:09:47 +0000213 }
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000214};
Iain Sandoe625b65a2014-01-28 11:03:17 +0000215
216class PPCTargetMachOStreamer : public PPCTargetStreamer {
217public:
218 PPCTargetMachOStreamer(MCStreamer &S) : PPCTargetStreamer(S) {}
Eugene Zelenko8187c192017-01-13 00:58:58 +0000219
Craig Topper0d3fa922014-04-29 07:57:37 +0000220 void emitTCEntry(const MCSymbol &S) override {
Iain Sandoe625b65a2014-01-28 11:03:17 +0000221 llvm_unreachable("Unknown pseudo-op: .tc");
222 }
Eugene Zelenko8187c192017-01-13 00:58:58 +0000223
Craig Topper0d3fa922014-04-29 07:57:37 +0000224 void emitMachine(StringRef CPU) override {
Iain Sandoe625b65a2014-01-28 11:03:17 +0000225 // FIXME: We should update the CPUType, CPUSubType in the Object file if
226 // the new values are different from the defaults.
227 }
Eugene Zelenko8187c192017-01-13 00:58:58 +0000228
Craig Topperfd38cbe2014-08-30 16:48:34 +0000229 void emitAbiVersion(int AbiVersion) override {
Ulrich Weigand0daa5162014-07-20 22:56:57 +0000230 llvm_unreachable("Unknown pseudo-op: .abiversion");
231 }
Eugene Zelenko8187c192017-01-13 00:58:58 +0000232
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000233 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override {
Ulrich Weigandbb686102014-07-20 23:06:03 +0000234 llvm_unreachable("Unknown pseudo-op: .localentry");
235 }
Iain Sandoe625b65a2014-01-28 11:03:17 +0000236};
Eugene Zelenko8187c192017-01-13 00:58:58 +0000237
238} // end anonymous namespace
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000239
Rafael Espindola73870dd2015-03-16 21:43:42 +0000240static MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
241 formatted_raw_ostream &OS,
242 MCInstPrinter *InstPrint,
243 bool isVerboseAsm) {
244 return new PPCTargetAsmStreamer(S, OS);
Evan Cheng61d4a202011-07-25 19:53:23 +0000245}
246
Rafael Espindolacd584a82015-03-19 01:50:16 +0000247static MCTargetStreamer *
248createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
Daniel Sanders50f17232015-09-15 16:17:27 +0000249 const Triple &TT = STI.getTargetTriple();
Daniel Sandersfbdab432015-07-06 16:33:18 +0000250 if (TT.isOSBinFormatELF())
Rafael Espindolacd584a82015-03-19 01:50:16 +0000251 return new PPCTargetELFStreamer(S);
252 return new PPCTargetMachOStreamer(S);
253}
254
Daniel Sanders50f17232015-09-15 16:17:27 +0000255static MCInstPrinter *createPPCMCInstPrinter(const Triple &T,
Eric Christopherf8019402015-03-31 00:10:04 +0000256 unsigned SyntaxVariant,
James Molloy4c493e82011-09-07 17:24:38 +0000257 const MCAsmInfo &MAI,
Craig Topper54bfde72012-04-02 06:09:36 +0000258 const MCInstrInfo &MII,
Eric Christopherf8019402015-03-31 00:10:04 +0000259 const MCRegisterInfo &MRI) {
Joerg Sonnenberger4b1acff2017-11-29 23:05:56 +0000260 return new PPCInstPrinter(MAI, MII, MRI, T);
Evan Cheng61faa552011-07-25 21:20:24 +0000261}
262
Tom Stellard4b0b2612019-06-11 03:21:13 +0000263extern "C" void LLVMInitializePowerPCTargetMC() {
Mehdi Aminif42454b2016-10-09 23:00:34 +0000264 for (Target *T :
265 {&getThePPC32Target(), &getThePPC64Target(), &getThePPC64LETarget()}) {
Rafael Espindola69244c32015-03-18 23:15:49 +0000266 // Register the MC asm info.
267 RegisterMCAsmInfoFn C(*T, createPPCMCAsmInfo);
Evan Cheng8c886a42011-07-22 21:58:54 +0000268
Rafael Espindola69244c32015-03-18 23:15:49 +0000269 // Register the MC instruction info.
270 TargetRegistry::RegisterMCInstrInfo(*T, createPPCMCInstrInfo);
Evan Cheng8c886a42011-07-22 21:58:54 +0000271
Rafael Espindola69244c32015-03-18 23:15:49 +0000272 // Register the MC register info.
273 TargetRegistry::RegisterMCRegInfo(*T, createPPCMCRegisterInfo);
Evan Cheng8c886a42011-07-22 21:58:54 +0000274
Rafael Espindola69244c32015-03-18 23:15:49 +0000275 // Register the MC subtarget info.
276 TargetRegistry::RegisterMCSubtargetInfo(*T, createPPCMCSubtargetInfo);
Evan Cheng61d4a202011-07-25 19:53:23 +0000277
Rafael Espindola69244c32015-03-18 23:15:49 +0000278 // Register the MC Code Emitter
279 TargetRegistry::RegisterMCCodeEmitter(*T, createPPCMCCodeEmitter);
280
Evan Cheng61d4a202011-07-25 19:53:23 +0000281 // Register the asm backend.
Rafael Espindola69244c32015-03-18 23:15:49 +0000282 TargetRegistry::RegisterMCAsmBackend(*T, createPPCAsmBackend);
Evan Cheng61faa552011-07-25 21:20:24 +0000283
Rafael Espindolacd584a82015-03-19 01:50:16 +0000284 // Register the object target streamer.
285 TargetRegistry::RegisterObjectTargetStreamer(*T,
286 createObjectTargetStreamer);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000287
Rafael Espindola69244c32015-03-18 23:15:49 +0000288 // Register the asm target streamer.
289 TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer);
290
291 // Register the MCInstPrinter.
292 TargetRegistry::RegisterMCInstPrinter(*T, createPPCMCInstPrinter);
293 }
Evan Cheng2129f592011-07-19 06:37:02 +0000294}