blob: 9100ecb4aa371ca209d9d37d4647a792f9a225ae [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/PPCMCTargetDesc.h"
11#include "MCTargetDesc/PPCFixupKinds.h"
Craig Topperb25fda92012-03-17 18:46:09 +000012#include "llvm/MC/MCAsmBackend.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000013#include "llvm/MC/MCAssembler.h"
Roman Divacky038c1a12011-08-02 15:51:38 +000014#include "llvm/MC/MCELFObjectWriter.h"
Craig Topper6e80c282012-03-26 06:58:25 +000015#include "llvm/MC/MCFixupKindInfo.h"
Daniel Dunbar73b87132010-12-16 16:08:33 +000016#include "llvm/MC/MCMachObjectWriter.h"
Chris Lattneraac9fa72010-11-15 08:49:58 +000017#include "llvm/MC/MCObjectWriter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/MC/MCSectionMachO.h"
Rafael Espindola95fb9b92015-06-02 20:38:46 +000019#include "llvm/MC/MCSymbolELF.h"
Jim Grosbach28fcafb2011-06-24 23:44:37 +000020#include "llvm/MC/MCValue.h"
Roman Divacky038c1a12011-08-02 15:51:38 +000021#include "llvm/Support/ELF.h"
22#include "llvm/Support/ErrorHandling.h"
Charles Davis8bdfafd2013-09-01 04:28:48 +000023#include "llvm/Support/MachO.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000024#include "llvm/Support/TargetRegistry.h"
Chris Lattneraac9fa72010-11-15 08:49:58 +000025using namespace llvm;
26
Ulrich Weigand56f5b282013-05-15 15:01:46 +000027static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value) {
Roman Divacky038c1a12011-08-02 15:51:38 +000028 switch (Kind) {
29 default:
30 llvm_unreachable("Unknown fixup kind!");
31 case FK_Data_1:
32 case FK_Data_2:
33 case FK_Data_4:
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000034 case FK_Data_8:
Bill Schmidt24b8dd62012-12-12 19:29:35 +000035 case PPC::fixup_ppc_nofixup:
Roman Divacky038c1a12011-08-02 15:51:38 +000036 return Value;
37 case PPC::fixup_ppc_brcond14:
Ulrich Weigandb6a30d12013-06-24 11:03:33 +000038 case PPC::fixup_ppc_brcond14abs:
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000039 return Value & 0xfffc;
Roman Divacky038c1a12011-08-02 15:51:38 +000040 case PPC::fixup_ppc_br24:
Ulrich Weigandb6a30d12013-06-24 11:03:33 +000041 case PPC::fixup_ppc_br24abs:
Roman Divacky038c1a12011-08-02 15:51:38 +000042 return Value & 0x3fffffc;
Ulrich Weigand6e23ac62013-05-17 12:37:21 +000043 case PPC::fixup_ppc_half16:
Roman Divacky038c1a12011-08-02 15:51:38 +000044 return Value & 0xffff;
Ulrich Weigand6e23ac62013-05-17 12:37:21 +000045 case PPC::fixup_ppc_half16ds:
Ulrich Weigand3e186012013-03-26 10:56:47 +000046 return Value & 0xfffc;
Roman Divacky038c1a12011-08-02 15:51:38 +000047 }
48}
49
Ulrich Weigand56f5b282013-05-15 15:01:46 +000050static unsigned getFixupKindNumBytes(unsigned Kind) {
51 switch (Kind) {
52 default:
53 llvm_unreachable("Unknown fixup kind!");
54 case FK_Data_1:
55 return 1;
56 case FK_Data_2:
Ulrich Weigand6e23ac62013-05-17 12:37:21 +000057 case PPC::fixup_ppc_half16:
58 case PPC::fixup_ppc_half16ds:
Ulrich Weigand56f5b282013-05-15 15:01:46 +000059 return 2;
60 case FK_Data_4:
61 case PPC::fixup_ppc_brcond14:
Ulrich Weigandb6a30d12013-06-24 11:03:33 +000062 case PPC::fixup_ppc_brcond14abs:
Ulrich Weigand56f5b282013-05-15 15:01:46 +000063 case PPC::fixup_ppc_br24:
Ulrich Weigandb6a30d12013-06-24 11:03:33 +000064 case PPC::fixup_ppc_br24abs:
Ulrich Weigand56f5b282013-05-15 15:01:46 +000065 return 4;
66 case FK_Data_8:
67 return 8;
Ulrich Weigand56f5b282013-05-15 15:01:46 +000068 case PPC::fixup_ppc_nofixup:
69 return 0;
70 }
71}
72
Chris Lattneraac9fa72010-11-15 08:49:58 +000073namespace {
Daniel Dunbar8888a962010-12-16 16:09:19 +000074
Evan Cheng5928e692011-07-25 23:24:55 +000075class PPCAsmBackend : public MCAsmBackend {
Ulrich Weigandcae3a172014-03-24 18:16:09 +000076 const Target &TheTarget;
77 bool IsLittleEndian;
Daniel Dunbar7ee21812010-12-16 16:08:43 +000078public:
Ulrich Weigandcae3a172014-03-24 18:16:09 +000079 PPCAsmBackend(const Target &T, bool isLittle) : MCAsmBackend(), TheTarget(T),
80 IsLittleEndian(isLittle) {}
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!");
Ulrich Weigandcae3a172014-03-24 18:16:09 +0000113 return (IsLittleEndian? InfosLE : InfosBE)[Kind - FirstTargetFixupKind];
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000114 }
Jim Grosbache2d29812012-01-18 18:52:20 +0000115
Benjamin Kramer8abfec32012-11-24 13:18:17 +0000116 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Craig Topper0d3fa922014-04-29 07:57:37 +0000117 uint64_t Value, bool IsPCRel) const override {
Benjamin Kramer8abfec32012-11-24 13:18:17 +0000118 Value = adjustFixupValue(Fixup.getKind(), Value);
119 if (!Value) return; // Doesn't change encoding.
120
121 unsigned Offset = Fixup.getOffset();
Ulrich Weigand56f5b282013-05-15 15:01:46 +0000122 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Benjamin Kramer8abfec32012-11-24 13:18:17 +0000123
124 // For each byte of the fragment that the fixup touches, mask in the bits
125 // from the fixup value. The Value has been "split up" into the appropriate
126 // bitfields above.
Ulrich Weigandcae3a172014-03-24 18:16:09 +0000127 for (unsigned i = 0; i != NumBytes; ++i) {
128 unsigned Idx = IsLittleEndian ? i : (NumBytes - 1 - i);
129 Data[Offset + i] |= uint8_t((Value >> (Idx * 8)) & 0xff);
130 }
Benjamin Kramer8abfec32012-11-24 13:18:17 +0000131 }
132
Ulrich Weigandbb686102014-07-20 23:06:03 +0000133 void processFixupValue(const MCAssembler &Asm, const MCAsmLayout &Layout,
134 const MCFixup &Fixup, const MCFragment *DF,
135 const MCValue &Target, uint64_t &Value,
136 bool &IsResolved) override {
137 switch ((PPC::Fixups)Fixup.getKind()) {
138 default: break;
139 case PPC::fixup_ppc_br24:
140 case PPC::fixup_ppc_br24abs:
141 // If the target symbol has a local entry point we must not attempt
142 // to resolve the fixup directly. Emit a relocation and leave
143 // resolution of the final target address to the linker.
144 if (const MCSymbolRefExpr *A = Target.getSymA()) {
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000145 if (const auto *S = dyn_cast<MCSymbolELF>(&A->getSymbol())) {
146 // The "other" values are stored in the last 6 bits of the second
147 // byte. The traditional defines for STO values assume the full byte
148 // and thus the shift to pack it.
149 unsigned Other = S->getOther() << 2;
150 if ((Other & ELF::STO_PPC64_LOCAL_MASK) != 0)
151 IsResolved = false;
152 }
Ulrich Weigandbb686102014-07-20 23:06:03 +0000153 }
154 break;
155 }
156 }
157
Craig Topper0d3fa922014-04-29 07:57:37 +0000158 bool mayNeedRelaxation(const MCInst &Inst) const override {
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000159 // FIXME.
160 return false;
161 }
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000162
163 bool fixupNeedsRelaxation(const MCFixup &Fixup,
164 uint64_t Value,
Eli Bendersky4d9ada02013-01-08 00:22:56 +0000165 const MCRelaxableFragment *DF,
Craig Topper0d3fa922014-04-29 07:57:37 +0000166 const MCAsmLayout &Layout) const override {
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000167 // FIXME.
Craig Toppere55c5562012-02-07 02:50:20 +0000168 llvm_unreachable("relaxInstruction() unimplemented");
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000169 }
170
Nirav Dave86030622016-07-11 14:23:53 +0000171 void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
172 MCInst &Res) const override {
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000173 // FIXME.
Craig Toppere55c5562012-02-07 02:50:20 +0000174 llvm_unreachable("relaxInstruction() unimplemented");
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000175 }
Jim Grosbache2d29812012-01-18 18:52:20 +0000176
Craig Topper0d3fa922014-04-29 07:57:37 +0000177 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override {
Ulrich Weigandd3ac7c02013-07-04 18:28:46 +0000178 uint64_t NumNops = Count / 4;
179 for (uint64_t i = 0; i != NumNops; ++i)
Jim Grosbach36e60e92015-06-04 22:24:41 +0000180 OW->write32(0x60000000);
Ulrich Weigandd3ac7c02013-07-04 18:28:46 +0000181
Benjamin Kramer97fbdd52015-04-17 11:12:43 +0000182 OW->WriteZeros(Count % 4);
David Majnemer71374202013-09-26 09:18:48 +0000183
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000184 return true;
Jim Grosbache2d29812012-01-18 18:52:20 +0000185 }
186
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000187 unsigned getPointerSize() const {
188 StringRef Name = TheTarget.getName();
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000189 if (Name == "ppc64" || Name == "ppc64le") return 8;
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000190 assert(Name == "ppc32" && "Unknown target name!");
191 return 4;
192 }
Ulrich Weigandcae3a172014-03-24 18:16:09 +0000193
194 bool isLittleEndian() const {
195 return IsLittleEndian;
196 }
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000197};
Chris Lattneraac9fa72010-11-15 08:49:58 +0000198} // end anonymous namespace
199
200
201// FIXME: This should be in a separate file.
202namespace {
203 class DarwinPPCAsmBackend : public PPCAsmBackend {
Chris Lattneraac9fa72010-11-15 08:49:58 +0000204 public:
Ulrich Weigandcae3a172014-03-24 18:16:09 +0000205 DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T, false) { }
Jim Grosbache2d29812012-01-18 18:52:20 +0000206
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000207 MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
Chris Lattneraac9fa72010-11-15 08:49:58 +0000208 bool is64 = getPointerSize() == 8;
David Fangb88cdf62013-08-08 20:14:40 +0000209 return createPPCMachObjectWriter(
210 OS,
211 /*Is64Bit=*/is64,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000212 (is64 ? MachO::CPU_TYPE_POWERPC64 : MachO::CPU_TYPE_POWERPC),
213 MachO::CPU_SUBTYPE_POWERPC_ALL);
Chris Lattneraac9fa72010-11-15 08:49:58 +0000214 }
Chris Lattneraac9fa72010-11-15 08:49:58 +0000215 };
Roman Divacky038c1a12011-08-02 15:51:38 +0000216
217 class ELFPPCAsmBackend : public PPCAsmBackend {
Rafael Espindola1ad40952011-12-21 17:00:36 +0000218 uint8_t OSABI;
Roman Divacky038c1a12011-08-02 15:51:38 +0000219 public:
Ulrich Weigandcae3a172014-03-24 18:16:09 +0000220 ELFPPCAsmBackend(const Target &T, bool IsLittleEndian, uint8_t OSABI) :
221 PPCAsmBackend(T, IsLittleEndian), OSABI(OSABI) { }
Jim Grosbache2d29812012-01-18 18:52:20 +0000222
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000223 MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
Roman Divacky038c1a12011-08-02 15:51:38 +0000224 bool is64 = getPointerSize() == 8;
Ulrich Weigandcae3a172014-03-24 18:16:09 +0000225 return createPPCELFObjectWriter(OS, is64, isLittleEndian(), OSABI);
Roman Divacky038c1a12011-08-02 15:51:38 +0000226 }
Roman Divacky038c1a12011-08-02 15:51:38 +0000227 };
228
Chris Lattneraac9fa72010-11-15 08:49:58 +0000229} // end anonymous namespace
230
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000231MCAsmBackend *llvm::createPPCAsmBackend(const Target &T,
232 const MCRegisterInfo &MRI,
Daniel Sanders50f17232015-09-15 16:17:27 +0000233 const Triple &TT, StringRef CPU) {
Daniel Sanders418caf52015-06-10 10:35:34 +0000234 if (TT.isOSDarwin())
Chris Lattneraac9fa72010-11-15 08:49:58 +0000235 return new DarwinPPCAsmBackend(T);
Daniel Dunbar2b9b0e32011-04-19 21:14:45 +0000236
Daniel Sanders418caf52015-06-10 10:35:34 +0000237 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
Daniel Sanders50f17232015-09-15 16:17:27 +0000238 bool IsLittleEndian = TT.getArch() == Triple::ppc64le;
Ulrich Weigandcae3a172014-03-24 18:16:09 +0000239 return new ELFPPCAsmBackend(T, IsLittleEndian, OSABI);
Chris Lattneraac9fa72010-11-15 08:49:58 +0000240}