blob: 0d420815ea62b942bd06c12b7619122be34e6be1 [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"
Roman Divacky038c1a12011-08-02 15:51:38 +000013#include "llvm/MC/MCELFObjectWriter.h"
Craig Topper6e80c282012-03-26 06:58:25 +000014#include "llvm/MC/MCFixupKindInfo.h"
Daniel Dunbar73b87132010-12-16 16:08:33 +000015#include "llvm/MC/MCMachObjectWriter.h"
Chris Lattneraac9fa72010-11-15 08:49:58 +000016#include "llvm/MC/MCObjectWriter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/MC/MCSectionMachO.h"
Jim Grosbach28fcafb2011-06-24 23:44:37 +000018#include "llvm/MC/MCValue.h"
Roman Divacky038c1a12011-08-02 15:51:38 +000019#include "llvm/Support/ELF.h"
20#include "llvm/Support/ErrorHandling.h"
Charles Davis8bdfafd2013-09-01 04:28:48 +000021#include "llvm/Support/MachO.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000022#include "llvm/Support/TargetRegistry.h"
Chris Lattneraac9fa72010-11-15 08:49:58 +000023using namespace llvm;
24
Ulrich Weigand56f5b282013-05-15 15:01:46 +000025static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value) {
Roman Divacky038c1a12011-08-02 15:51:38 +000026 switch (Kind) {
27 default:
28 llvm_unreachable("Unknown fixup kind!");
29 case FK_Data_1:
30 case FK_Data_2:
31 case FK_Data_4:
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000032 case FK_Data_8:
Bill Schmidt24b8dd62012-12-12 19:29:35 +000033 case PPC::fixup_ppc_nofixup:
Roman Divacky038c1a12011-08-02 15:51:38 +000034 return Value;
35 case PPC::fixup_ppc_brcond14:
Ulrich Weigandb6a30d12013-06-24 11:03:33 +000036 case PPC::fixup_ppc_brcond14abs:
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000037 return Value & 0xfffc;
Roman Divacky038c1a12011-08-02 15:51:38 +000038 case PPC::fixup_ppc_br24:
Ulrich Weigandb6a30d12013-06-24 11:03:33 +000039 case PPC::fixup_ppc_br24abs:
Roman Divacky038c1a12011-08-02 15:51:38 +000040 return Value & 0x3fffffc;
Ulrich Weigand6e23ac62013-05-17 12:37:21 +000041 case PPC::fixup_ppc_half16:
Roman Divacky038c1a12011-08-02 15:51:38 +000042 return Value & 0xffff;
Ulrich Weigand6e23ac62013-05-17 12:37:21 +000043 case PPC::fixup_ppc_half16ds:
Ulrich Weigand3e186012013-03-26 10:56:47 +000044 return Value & 0xfffc;
Roman Divacky038c1a12011-08-02 15:51:38 +000045 }
46}
47
Ulrich Weigand56f5b282013-05-15 15:01:46 +000048static unsigned getFixupKindNumBytes(unsigned Kind) {
49 switch (Kind) {
50 default:
51 llvm_unreachable("Unknown fixup kind!");
52 case FK_Data_1:
53 return 1;
54 case FK_Data_2:
Ulrich Weigand6e23ac62013-05-17 12:37:21 +000055 case PPC::fixup_ppc_half16:
56 case PPC::fixup_ppc_half16ds:
Ulrich Weigand56f5b282013-05-15 15:01:46 +000057 return 2;
58 case FK_Data_4:
59 case PPC::fixup_ppc_brcond14:
Ulrich Weigandb6a30d12013-06-24 11:03:33 +000060 case PPC::fixup_ppc_brcond14abs:
Ulrich Weigand56f5b282013-05-15 15:01:46 +000061 case PPC::fixup_ppc_br24:
Ulrich Weigandb6a30d12013-06-24 11:03:33 +000062 case PPC::fixup_ppc_br24abs:
Ulrich Weigand56f5b282013-05-15 15:01:46 +000063 return 4;
64 case FK_Data_8:
65 return 8;
Ulrich Weigand56f5b282013-05-15 15:01:46 +000066 case PPC::fixup_ppc_nofixup:
67 return 0;
68 }
69}
70
Chris Lattneraac9fa72010-11-15 08:49:58 +000071namespace {
Daniel Dunbar8888a962010-12-16 16:09:19 +000072
Evan Cheng5928e692011-07-25 23:24:55 +000073class PPCAsmBackend : public MCAsmBackend {
Daniel Dunbar7ee21812010-12-16 16:08:43 +000074const Target &TheTarget;
75public:
Evan Cheng5928e692011-07-25 23:24:55 +000076 PPCAsmBackend(const Target &T) : MCAsmBackend(), TheTarget(T) {}
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +000077
Daniel Dunbar7ee21812010-12-16 16:08:43 +000078 unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; }
79
80 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
81 const static MCFixupKindInfo Infos[PPC::NumTargetFixupKinds] = {
82 // name offset bits flags
83 { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel },
84 { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel },
Ulrich Weigandb6a30d12013-06-24 11:03:33 +000085 { "fixup_ppc_br24abs", 6, 24, 0 },
86 { "fixup_ppc_brcond14abs", 16, 14, 0 },
Ulrich Weigand6e23ac62013-05-17 12:37:21 +000087 { "fixup_ppc_half16", 0, 16, 0 },
88 { "fixup_ppc_half16ds", 0, 14, 0 },
Bill Schmidt24b8dd62012-12-12 19:29:35 +000089 { "fixup_ppc_nofixup", 0, 0, 0 }
Daniel Dunbar7ee21812010-12-16 16:08:43 +000090 };
Jim Grosbache2d29812012-01-18 18:52:20 +000091
Daniel Dunbar7ee21812010-12-16 16:08:43 +000092 if (Kind < FirstTargetFixupKind)
Evan Cheng5928e692011-07-25 23:24:55 +000093 return MCAsmBackend::getFixupKindInfo(Kind);
Jim Grosbache2d29812012-01-18 18:52:20 +000094
Daniel Dunbar7ee21812010-12-16 16:08:43 +000095 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
96 "Invalid kind!");
97 return Infos[Kind - FirstTargetFixupKind];
98 }
Jim Grosbache2d29812012-01-18 18:52:20 +000099
Benjamin Kramer8abfec32012-11-24 13:18:17 +0000100 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
101 uint64_t Value) const {
102 Value = adjustFixupValue(Fixup.getKind(), Value);
103 if (!Value) return; // Doesn't change encoding.
104
105 unsigned Offset = Fixup.getOffset();
Ulrich Weigand56f5b282013-05-15 15:01:46 +0000106 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Benjamin Kramer8abfec32012-11-24 13:18:17 +0000107
108 // For each byte of the fragment that the fixup touches, mask in the bits
109 // from the fixup value. The Value has been "split up" into the appropriate
110 // bitfields above.
Ulrich Weigand56f5b282013-05-15 15:01:46 +0000111 for (unsigned i = 0; i != NumBytes; ++i)
112 Data[Offset + i] |= uint8_t((Value >> ((NumBytes - i - 1)*8)) & 0xff);
Benjamin Kramer8abfec32012-11-24 13:18:17 +0000113 }
114
Jim Grosbachaba3de92012-01-18 18:52:16 +0000115 bool mayNeedRelaxation(const MCInst &Inst) const {
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000116 // FIXME.
117 return false;
118 }
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000119
120 bool fixupNeedsRelaxation(const MCFixup &Fixup,
121 uint64_t Value,
Eli Bendersky4d9ada02013-01-08 00:22:56 +0000122 const MCRelaxableFragment *DF,
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000123 const MCAsmLayout &Layout) const {
124 // FIXME.
Craig Toppere55c5562012-02-07 02:50:20 +0000125 llvm_unreachable("relaxInstruction() unimplemented");
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000126 }
127
Jim Grosbache2d29812012-01-18 18:52:20 +0000128
Jim Grosbachaba3de92012-01-18 18:52:16 +0000129 void relaxInstruction(const MCInst &Inst, MCInst &Res) const {
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000130 // FIXME.
Craig Toppere55c5562012-02-07 02:50:20 +0000131 llvm_unreachable("relaxInstruction() unimplemented");
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000132 }
Jim Grosbache2d29812012-01-18 18:52:20 +0000133
Jim Grosbachaba3de92012-01-18 18:52:16 +0000134 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const {
Ulrich Weigandd3ac7c02013-07-04 18:28:46 +0000135 uint64_t NumNops = Count / 4;
136 for (uint64_t i = 0; i != NumNops; ++i)
137 OW->Write32(0x60000000);
138
David Majnemer71374202013-09-26 09:18:48 +0000139 switch (Count % 4) {
140 default: break; // No leftover bytes to write
141 case 1: OW->Write8(0); break;
142 case 2: OW->Write16(0); break;
143 case 3: OW->Write16(0); OW->Write8(0); break;
144 }
145
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000146 return true;
Jim Grosbache2d29812012-01-18 18:52:20 +0000147 }
148
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000149 unsigned getPointerSize() const {
150 StringRef Name = TheTarget.getName();
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000151 if (Name == "ppc64" || Name == "ppc64le") return 8;
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000152 assert(Name == "ppc32" && "Unknown target name!");
153 return 4;
154 }
155};
Chris Lattneraac9fa72010-11-15 08:49:58 +0000156} // end anonymous namespace
157
158
159// FIXME: This should be in a separate file.
160namespace {
161 class DarwinPPCAsmBackend : public PPCAsmBackend {
Chris Lattneraac9fa72010-11-15 08:49:58 +0000162 public:
Daniel Dunbard2867f12010-12-17 02:06:08 +0000163 DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) { }
Jim Grosbache2d29812012-01-18 18:52:20 +0000164
Chris Lattneraac9fa72010-11-15 08:49:58 +0000165 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
166 bool is64 = getPointerSize() == 8;
David Fangb88cdf62013-08-08 20:14:40 +0000167 return createPPCMachObjectWriter(
168 OS,
169 /*Is64Bit=*/is64,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000170 (is64 ? MachO::CPU_TYPE_POWERPC64 : MachO::CPU_TYPE_POWERPC),
171 MachO::CPU_SUBTYPE_POWERPC_ALL);
Chris Lattneraac9fa72010-11-15 08:49:58 +0000172 }
Jim Grosbache2d29812012-01-18 18:52:20 +0000173
Chris Lattneraac9fa72010-11-15 08:49:58 +0000174 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
175 return false;
176 }
177 };
Roman Divacky038c1a12011-08-02 15:51:38 +0000178
179 class ELFPPCAsmBackend : public PPCAsmBackend {
Rafael Espindola1ad40952011-12-21 17:00:36 +0000180 uint8_t OSABI;
Roman Divacky038c1a12011-08-02 15:51:38 +0000181 public:
Rafael Espindola1ad40952011-12-21 17:00:36 +0000182 ELFPPCAsmBackend(const Target &T, uint8_t OSABI) :
183 PPCAsmBackend(T), OSABI(OSABI) { }
Jim Grosbache2d29812012-01-18 18:52:20 +0000184
Jim Grosbache2d29812012-01-18 18:52:20 +0000185
Roman Divacky038c1a12011-08-02 15:51:38 +0000186 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
187 bool is64 = getPointerSize() == 8;
Rafael Espindola25009622011-12-22 18:38:06 +0000188 return createPPCELFObjectWriter(OS, is64, OSABI);
Roman Divacky038c1a12011-08-02 15:51:38 +0000189 }
Jim Grosbache2d29812012-01-18 18:52:20 +0000190
Roman Divacky038c1a12011-08-02 15:51:38 +0000191 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
192 return false;
193 }
194 };
195
Chris Lattneraac9fa72010-11-15 08:49:58 +0000196} // end anonymous namespace
197
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000198MCAsmBackend *llvm::createPPCAsmBackend(const Target &T,
199 const MCRegisterInfo &MRI,
200 StringRef TT, StringRef CPU) {
Daniel Dunbar2b9b0e32011-04-19 21:14:45 +0000201 if (Triple(TT).isOSDarwin())
Chris Lattneraac9fa72010-11-15 08:49:58 +0000202 return new DarwinPPCAsmBackend(T);
Daniel Dunbar2b9b0e32011-04-19 21:14:45 +0000203
Rafael Espindola1ad40952011-12-21 17:00:36 +0000204 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS());
205 return new ELFPPCAsmBackend(T, OSABI);
Chris Lattneraac9fa72010-11-15 08:49:58 +0000206}