blob: 8a53694f57f7d4485bb332b0d5c86ebef4ee1e69 [file] [log] [blame]
Chris Lattneraac9fa72010-11-15 08:49:58 +00001//===-- PPCAsmBackend.cpp - PPC Assembler Backend -------------------------===//
2//
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
Evan Cheng61d4a202011-07-25 19:53:23 +000010#include "MCTargetDesc/PPCFixupKinds.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000011#include "MCTargetDesc/PPCMCTargetDesc.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000012#include "llvm/BinaryFormat/ELF.h"
13#include "llvm/BinaryFormat/MachO.h"
Craig Topperb25fda92012-03-17 18:46:09 +000014#include "llvm/MC/MCAsmBackend.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000015#include "llvm/MC/MCAssembler.h"
Roman Divacky038c1a12011-08-02 15:51:38 +000016#include "llvm/MC/MCELFObjectWriter.h"
Craig Topper6e80c282012-03-26 06:58:25 +000017#include "llvm/MC/MCFixupKindInfo.h"
Daniel Dunbar73b87132010-12-16 16:08:33 +000018#include "llvm/MC/MCMachObjectWriter.h"
Chris Lattneraac9fa72010-11-15 08:49:58 +000019#include "llvm/MC/MCObjectWriter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/MC/MCSectionMachO.h"
Alex Bradburyb22f7512018-01-03 08:53:05 +000021#include "llvm/MC/MCSubtargetInfo.h"
Rafael Espindola95fb9b92015-06-02 20:38:46 +000022#include "llvm/MC/MCSymbolELF.h"
Jim Grosbach28fcafb2011-06-24 23:44:37 +000023#include "llvm/MC/MCValue.h"
Roman Divacky038c1a12011-08-02 15:51:38 +000024#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000025#include "llvm/Support/TargetRegistry.h"
Chris Lattneraac9fa72010-11-15 08:49:58 +000026using namespace llvm;
27
Ulrich Weigand56f5b282013-05-15 15:01:46 +000028static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value) {
Roman Divacky038c1a12011-08-02 15:51:38 +000029 switch (Kind) {
30 default:
31 llvm_unreachable("Unknown fixup kind!");
32 case FK_Data_1:
33 case FK_Data_2:
34 case FK_Data_4:
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000035 case FK_Data_8:
Bill Schmidt24b8dd62012-12-12 19:29:35 +000036 case PPC::fixup_ppc_nofixup:
Roman Divacky038c1a12011-08-02 15:51:38 +000037 return Value;
38 case PPC::fixup_ppc_brcond14:
Ulrich Weigandb6a30d12013-06-24 11:03:33 +000039 case PPC::fixup_ppc_brcond14abs:
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000040 return Value & 0xfffc;
Roman Divacky038c1a12011-08-02 15:51:38 +000041 case PPC::fixup_ppc_br24:
Ulrich Weigandb6a30d12013-06-24 11:03:33 +000042 case PPC::fixup_ppc_br24abs:
Roman Divacky038c1a12011-08-02 15:51:38 +000043 return Value & 0x3fffffc;
Ulrich Weigand6e23ac62013-05-17 12:37:21 +000044 case PPC::fixup_ppc_half16:
Roman Divacky038c1a12011-08-02 15:51:38 +000045 return Value & 0xffff;
Ulrich Weigand6e23ac62013-05-17 12:37:21 +000046 case PPC::fixup_ppc_half16ds:
Ulrich Weigand3e186012013-03-26 10:56:47 +000047 return Value & 0xfffc;
Roman Divacky038c1a12011-08-02 15:51:38 +000048 }
49}
50
Ulrich Weigand56f5b282013-05-15 15:01:46 +000051static unsigned getFixupKindNumBytes(unsigned Kind) {
52 switch (Kind) {
53 default:
54 llvm_unreachable("Unknown fixup kind!");
55 case FK_Data_1:
56 return 1;
57 case FK_Data_2:
Ulrich Weigand6e23ac62013-05-17 12:37:21 +000058 case PPC::fixup_ppc_half16:
59 case PPC::fixup_ppc_half16ds:
Ulrich Weigand56f5b282013-05-15 15:01:46 +000060 return 2;
61 case FK_Data_4:
62 case PPC::fixup_ppc_brcond14:
Ulrich Weigandb6a30d12013-06-24 11:03:33 +000063 case PPC::fixup_ppc_brcond14abs:
Ulrich Weigand56f5b282013-05-15 15:01:46 +000064 case PPC::fixup_ppc_br24:
Ulrich Weigandb6a30d12013-06-24 11:03:33 +000065 case PPC::fixup_ppc_br24abs:
Ulrich Weigand56f5b282013-05-15 15:01:46 +000066 return 4;
67 case FK_Data_8:
68 return 8;
Ulrich Weigand56f5b282013-05-15 15:01:46 +000069 case PPC::fixup_ppc_nofixup:
70 return 0;
71 }
72}
73
Chris Lattneraac9fa72010-11-15 08:49:58 +000074namespace {
Daniel Dunbar8888a962010-12-16 16:09:19 +000075
Evan Cheng5928e692011-07-25 23:24:55 +000076class PPCAsmBackend : public MCAsmBackend {
Ulrich Weigandcae3a172014-03-24 18:16:09 +000077 const Target &TheTarget;
Daniel Dunbar7ee21812010-12-16 16:08:43 +000078public:
Peter Collingbourne571a3302018-05-21 17:57:19 +000079 PPCAsmBackend(const Target &T, support::endianness Endian)
80 : MCAsmBackend(Endian), TheTarget(T) {}
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +000081
Craig Topper0d3fa922014-04-29 07:57:37 +000082 unsigned getNumFixupKinds() const override {
83 return PPC::NumTargetFixupKinds;
84 }
Daniel Dunbar7ee21812010-12-16 16:08:43 +000085
Craig Topper0d3fa922014-04-29 07:57:37 +000086 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
Ulrich Weigandcae3a172014-03-24 18:16:09 +000087 const static MCFixupKindInfo InfosBE[PPC::NumTargetFixupKinds] = {
Daniel Dunbar7ee21812010-12-16 16:08:43 +000088 // name offset bits flags
89 { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel },
90 { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel },
Ulrich Weigandb6a30d12013-06-24 11:03:33 +000091 { "fixup_ppc_br24abs", 6, 24, 0 },
92 { "fixup_ppc_brcond14abs", 16, 14, 0 },
Ulrich Weigand6e23ac62013-05-17 12:37:21 +000093 { "fixup_ppc_half16", 0, 16, 0 },
94 { "fixup_ppc_half16ds", 0, 14, 0 },
Bill Schmidt24b8dd62012-12-12 19:29:35 +000095 { "fixup_ppc_nofixup", 0, 0, 0 }
Daniel Dunbar7ee21812010-12-16 16:08:43 +000096 };
Ulrich Weigandcae3a172014-03-24 18:16:09 +000097 const static MCFixupKindInfo InfosLE[PPC::NumTargetFixupKinds] = {
98 // name offset bits flags
99 { "fixup_ppc_br24", 2, 24, MCFixupKindInfo::FKF_IsPCRel },
100 { "fixup_ppc_brcond14", 2, 14, MCFixupKindInfo::FKF_IsPCRel },
101 { "fixup_ppc_br24abs", 2, 24, 0 },
102 { "fixup_ppc_brcond14abs", 2, 14, 0 },
103 { "fixup_ppc_half16", 0, 16, 0 },
104 { "fixup_ppc_half16ds", 2, 14, 0 },
105 { "fixup_ppc_nofixup", 0, 0, 0 }
106 };
Jim Grosbache2d29812012-01-18 18:52:20 +0000107
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000108 if (Kind < FirstTargetFixupKind)
Evan Cheng5928e692011-07-25 23:24:55 +0000109 return MCAsmBackend::getFixupKindInfo(Kind);
Jim Grosbache2d29812012-01-18 18:52:20 +0000110
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000111 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
112 "Invalid kind!");
Peter Collingbourne571a3302018-05-21 17:57:19 +0000113 return (Endian == support::little
114 ? InfosLE
115 : InfosBE)[Kind - FirstTargetFixupKind];
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000116 }
Jim Grosbache2d29812012-01-18 18:52:20 +0000117
Rafael Espindola801b42d2017-06-23 22:52:36 +0000118 void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
119 const MCValue &Target, MutableArrayRef<char> Data,
Rafael Espindola1beb7022017-07-11 23:18:25 +0000120 uint64_t Value, bool IsResolved) const override {
Benjamin Kramer8abfec32012-11-24 13:18:17 +0000121 Value = adjustFixupValue(Fixup.getKind(), Value);
122 if (!Value) return; // Doesn't change encoding.
123
124 unsigned Offset = Fixup.getOffset();
Ulrich Weigand56f5b282013-05-15 15:01:46 +0000125 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Benjamin Kramer8abfec32012-11-24 13:18:17 +0000126
127 // For each byte of the fragment that the fixup touches, mask in the bits
128 // from the fixup value. The Value has been "split up" into the appropriate
129 // bitfields above.
Ulrich Weigandcae3a172014-03-24 18:16:09 +0000130 for (unsigned i = 0; i != NumBytes; ++i) {
Peter Collingbourne571a3302018-05-21 17:57:19 +0000131 unsigned Idx = Endian == support::little ? i : (NumBytes - 1 - i);
Ulrich Weigandcae3a172014-03-24 18:16:09 +0000132 Data[Offset + i] |= uint8_t((Value >> (Idx * 8)) & 0xff);
133 }
Benjamin Kramer8abfec32012-11-24 13:18:17 +0000134 }
135
Rafael Espindola76287ab2017-06-30 22:47:27 +0000136 bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,
137 const MCValue &Target) override {
Ulrich Weigandbb686102014-07-20 23:06:03 +0000138 switch ((PPC::Fixups)Fixup.getKind()) {
Rafael Espindola76287ab2017-06-30 22:47:27 +0000139 default:
140 return false;
Ulrich Weigandbb686102014-07-20 23:06:03 +0000141 case PPC::fixup_ppc_br24:
142 case PPC::fixup_ppc_br24abs:
143 // If the target symbol has a local entry point we must not attempt
144 // to resolve the fixup directly. Emit a relocation and leave
145 // resolution of the final target address to the linker.
146 if (const MCSymbolRefExpr *A = Target.getSymA()) {
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000147 if (const auto *S = dyn_cast<MCSymbolELF>(&A->getSymbol())) {
148 // The "other" values are stored in the last 6 bits of the second
149 // byte. The traditional defines for STO values assume the full byte
150 // and thus the shift to pack it.
151 unsigned Other = S->getOther() << 2;
152 if ((Other & ELF::STO_PPC64_LOCAL_MASK) != 0)
Rafael Espindola76287ab2017-06-30 22:47:27 +0000153 return true;
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000154 }
Ulrich Weigandbb686102014-07-20 23:06:03 +0000155 }
Rafael Espindola76287ab2017-06-30 22:47:27 +0000156 return false;
Ulrich Weigandbb686102014-07-20 23:06:03 +0000157 }
158 }
159
Craig Topper0d3fa922014-04-29 07:57:37 +0000160 bool mayNeedRelaxation(const MCInst &Inst) const override {
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000161 // FIXME.
162 return false;
163 }
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000164
165 bool fixupNeedsRelaxation(const MCFixup &Fixup,
166 uint64_t Value,
Eli Bendersky4d9ada02013-01-08 00:22:56 +0000167 const MCRelaxableFragment *DF,
Craig Topper0d3fa922014-04-29 07:57:37 +0000168 const MCAsmLayout &Layout) const override {
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000169 // FIXME.
Craig Toppere55c5562012-02-07 02:50:20 +0000170 llvm_unreachable("relaxInstruction() unimplemented");
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000171 }
172
Nirav Dave86030622016-07-11 14:23:53 +0000173 void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
174 MCInst &Res) const override {
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000175 // FIXME.
Craig Toppere55c5562012-02-07 02:50:20 +0000176 llvm_unreachable("relaxInstruction() unimplemented");
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000177 }
Jim Grosbache2d29812012-01-18 18:52:20 +0000178
Peter Collingbourne571a3302018-05-21 17:57:19 +0000179 bool writeNopData(raw_ostream &OS, uint64_t Count) const override {
Ulrich Weigandd3ac7c02013-07-04 18:28:46 +0000180 uint64_t NumNops = Count / 4;
181 for (uint64_t i = 0; i != NumNops; ++i)
Peter Collingbourne571a3302018-05-21 17:57:19 +0000182 support::endian::write<uint32_t>(OS, 0x60000000, Endian);
Ulrich Weigandd3ac7c02013-07-04 18:28:46 +0000183
Peter Collingbourne571a3302018-05-21 17:57:19 +0000184 OS.write_zeros(Count % 4);
David Majnemer71374202013-09-26 09:18:48 +0000185
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000186 return true;
Jim Grosbache2d29812012-01-18 18:52:20 +0000187 }
188
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000189 unsigned getPointerSize() const {
190 StringRef Name = TheTarget.getName();
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000191 if (Name == "ppc64" || Name == "ppc64le") return 8;
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000192 assert(Name == "ppc32" && "Unknown target name!");
193 return 4;
194 }
195};
Chris Lattneraac9fa72010-11-15 08:49:58 +0000196} // end anonymous namespace
197
198
199// FIXME: This should be in a separate file.
200namespace {
201 class DarwinPPCAsmBackend : public PPCAsmBackend {
Chris Lattneraac9fa72010-11-15 08:49:58 +0000202 public:
Peter Collingbourne571a3302018-05-21 17:57:19 +0000203 DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T, support::big) { }
Jim Grosbache2d29812012-01-18 18:52:20 +0000204
Lang Hames60fbc7c2017-10-10 16:28:07 +0000205 std::unique_ptr<MCObjectWriter>
206 createObjectWriter(raw_pwrite_stream &OS) const override {
Chris Lattneraac9fa72010-11-15 08:49:58 +0000207 bool is64 = getPointerSize() == 8;
David Fangb88cdf62013-08-08 20:14:40 +0000208 return createPPCMachObjectWriter(
209 OS,
210 /*Is64Bit=*/is64,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000211 (is64 ? MachO::CPU_TYPE_POWERPC64 : MachO::CPU_TYPE_POWERPC),
212 MachO::CPU_SUBTYPE_POWERPC_ALL);
Chris Lattneraac9fa72010-11-15 08:49:58 +0000213 }
Chris Lattneraac9fa72010-11-15 08:49:58 +0000214 };
Roman Divacky038c1a12011-08-02 15:51:38 +0000215
216 class ELFPPCAsmBackend : public PPCAsmBackend {
Rafael Espindola1ad40952011-12-21 17:00:36 +0000217 uint8_t OSABI;
Roman Divacky038c1a12011-08-02 15:51:38 +0000218 public:
Peter Collingbourne571a3302018-05-21 17:57:19 +0000219 ELFPPCAsmBackend(const Target &T, support::endianness Endian,
220 uint8_t OSABI)
221 : PPCAsmBackend(T, Endian), OSABI(OSABI) {}
Jim Grosbache2d29812012-01-18 18:52:20 +0000222
Lang Hames60fbc7c2017-10-10 16:28:07 +0000223 std::unique_ptr<MCObjectWriter>
224 createObjectWriter(raw_pwrite_stream &OS) const override {
Roman Divacky038c1a12011-08-02 15:51:38 +0000225 bool is64 = getPointerSize() == 8;
Peter Collingbourne571a3302018-05-21 17:57:19 +0000226 return createPPCELFObjectWriter(OS, is64, Endian == support::little,
227 OSABI);
Roman Divacky038c1a12011-08-02 15:51:38 +0000228 }
Roman Divacky038c1a12011-08-02 15:51:38 +0000229 };
230
Chris Lattneraac9fa72010-11-15 08:49:58 +0000231} // end anonymous namespace
232
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000233MCAsmBackend *llvm::createPPCAsmBackend(const Target &T,
Alex Bradburyb22f7512018-01-03 08:53:05 +0000234 const MCSubtargetInfo &STI,
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000235 const MCRegisterInfo &MRI,
Joel Jones373d7d32016-07-25 17:18:28 +0000236 const MCTargetOptions &Options) {
Alex Bradburyb22f7512018-01-03 08:53:05 +0000237 const Triple &TT = STI.getTargetTriple();
Daniel Sanders418caf52015-06-10 10:35:34 +0000238 if (TT.isOSDarwin())
Chris Lattneraac9fa72010-11-15 08:49:58 +0000239 return new DarwinPPCAsmBackend(T);
Daniel Dunbar2b9b0e32011-04-19 21:14:45 +0000240
Daniel Sanders418caf52015-06-10 10:35:34 +0000241 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
Daniel Sanders50f17232015-09-15 16:17:27 +0000242 bool IsLittleEndian = TT.getArch() == Triple::ppc64le;
Peter Collingbourne571a3302018-05-21 17:57:19 +0000243 return new ELFPPCAsmBackend(
244 T, IsLittleEndian ? support::little : support::big, OSABI);
Chris Lattneraac9fa72010-11-15 08:49:58 +0000245}