blob: f24edf62ed71cb51f0ad4135c92269600bd8d59f [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"
Daniel Dunbara5f50c12010-11-27 04:38:36 +000019#include "llvm/Object/MachOFormat.h"
Roman Divacky038c1a12011-08-02 15:51:38 +000020#include "llvm/Support/ELF.h"
21#include "llvm/Support/ErrorHandling.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
Roman Divacky038c1a12011-08-02 15:51:38 +000025static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
26 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:
33 case PPC::fixup_ppc_toc:
Bill Schmidtca4a0c92012-12-04 16:18:08 +000034 case PPC::fixup_ppc_tlsreg:
Bill Schmidt24b8dd62012-12-12 19:29:35 +000035 case PPC::fixup_ppc_nofixup:
Roman Divacky038c1a12011-08-02 15:51:38 +000036 return Value;
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000037 case PPC::fixup_ppc_lo14:
38 case PPC::fixup_ppc_toc16_ds:
39 return (Value & 0xffff) << 2;
Roman Divacky038c1a12011-08-02 15:51:38 +000040 case PPC::fixup_ppc_brcond14:
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000041 return Value & 0xfffc;
Roman Divacky038c1a12011-08-02 15:51:38 +000042 case PPC::fixup_ppc_br24:
43 return Value & 0x3fffffc;
44#if 0
45 case PPC::fixup_ppc_hi16:
46 return (Value >> 16) & 0xffff;
47#endif
48 case PPC::fixup_ppc_ha16:
49 return ((Value >> 16) + ((Value & 0x8000) ? 1 : 0)) & 0xffff;
50 case PPC::fixup_ppc_lo16:
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000051 case PPC::fixup_ppc_toc16:
Roman Divacky038c1a12011-08-02 15:51:38 +000052 return Value & 0xffff;
53 }
54}
55
Chris Lattneraac9fa72010-11-15 08:49:58 +000056namespace {
Daniel Dunbar8888a962010-12-16 16:09:19 +000057class PPCMachObjectWriter : public MCMachObjectTargetWriter {
Daniel Dunbar03fcccb2010-12-16 17:21:02 +000058public:
59 PPCMachObjectWriter(bool Is64Bit, uint32_t CPUType,
60 uint32_t CPUSubtype)
61 : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {}
Jim Grosbach28fcafb2011-06-24 23:44:37 +000062
63 void RecordRelocation(MachObjectWriter *Writer,
64 const MCAssembler &Asm, const MCAsmLayout &Layout,
65 const MCFragment *Fragment, const MCFixup &Fixup,
Benjamin Kramer1694f912012-11-24 15:23:49 +000066 MCValue Target, uint64_t &FixedValue) {
67 llvm_unreachable("Relocation emission for MachO/PPC unimplemented!");
68 }
Daniel Dunbar8888a962010-12-16 16:09:19 +000069};
70
Evan Cheng5928e692011-07-25 23:24:55 +000071class PPCAsmBackend : public MCAsmBackend {
Daniel Dunbar7ee21812010-12-16 16:08:43 +000072const Target &TheTarget;
73public:
Evan Cheng5928e692011-07-25 23:24:55 +000074 PPCAsmBackend(const Target &T) : MCAsmBackend(), TheTarget(T) {}
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +000075
Daniel Dunbar7ee21812010-12-16 16:08:43 +000076 unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; }
77
78 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
79 const static MCFixupKindInfo Infos[PPC::NumTargetFixupKinds] = {
80 // name offset bits flags
81 { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel },
82 { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel },
83 { "fixup_ppc_lo16", 16, 16, 0 },
84 { "fixup_ppc_ha16", 16, 16, 0 },
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000085 { "fixup_ppc_lo14", 16, 14, 0 },
86 { "fixup_ppc_toc", 0, 64, 0 },
87 { "fixup_ppc_toc16", 16, 16, 0 },
Bill Schmidtca4a0c92012-12-04 16:18:08 +000088 { "fixup_ppc_toc16_ds", 16, 14, 0 },
Bill Schmidtc56f1d32012-12-11 20:30:11 +000089 { "fixup_ppc_tlsreg", 0, 0, 0 },
Bill Schmidt24b8dd62012-12-12 19:29:35 +000090 { "fixup_ppc_nofixup", 0, 0, 0 }
Daniel Dunbar7ee21812010-12-16 16:08:43 +000091 };
Jim Grosbache2d29812012-01-18 18:52:20 +000092
Daniel Dunbar7ee21812010-12-16 16:08:43 +000093 if (Kind < FirstTargetFixupKind)
Evan Cheng5928e692011-07-25 23:24:55 +000094 return MCAsmBackend::getFixupKindInfo(Kind);
Jim Grosbache2d29812012-01-18 18:52:20 +000095
Daniel Dunbar7ee21812010-12-16 16:08:43 +000096 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
97 "Invalid kind!");
98 return Infos[Kind - FirstTargetFixupKind];
99 }
Jim Grosbache2d29812012-01-18 18:52:20 +0000100
Benjamin Kramer8abfec32012-11-24 13:18:17 +0000101 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
102 uint64_t Value) const {
103 Value = adjustFixupValue(Fixup.getKind(), Value);
104 if (!Value) return; // Doesn't change encoding.
105
106 unsigned Offset = Fixup.getOffset();
107
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.
111 for (unsigned i = 0; i != 4; ++i)
112 Data[Offset + i] |= uint8_t((Value >> ((4 - i - 1)*8)) & 0xff);
113 }
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 {
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000135 // FIXME: Zero fill for now. That's not right, but at least will get the
136 // section size right.
137 for (uint64_t i = 0; i != Count; ++i)
138 OW->Write8(0);
139 return true;
Jim Grosbache2d29812012-01-18 18:52:20 +0000140 }
141
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000142 unsigned getPointerSize() const {
143 StringRef Name = TheTarget.getName();
144 if (Name == "ppc64") return 8;
145 assert(Name == "ppc32" && "Unknown target name!");
146 return 4;
147 }
148};
Chris Lattneraac9fa72010-11-15 08:49:58 +0000149} // end anonymous namespace
150
151
152// FIXME: This should be in a separate file.
153namespace {
154 class DarwinPPCAsmBackend : public PPCAsmBackend {
Chris Lattneraac9fa72010-11-15 08:49:58 +0000155 public:
Daniel Dunbard2867f12010-12-17 02:06:08 +0000156 DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) { }
Jim Grosbache2d29812012-01-18 18:52:20 +0000157
Chris Lattneraac9fa72010-11-15 08:49:58 +0000158 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
159 bool is64 = getPointerSize() == 8;
Daniel Dunbar03fcccb2010-12-16 17:21:02 +0000160 return createMachObjectWriter(new PPCMachObjectWriter(
161 /*Is64Bit=*/is64,
162 (is64 ? object::mach::CTM_PowerPC64 :
163 object::mach::CTM_PowerPC),
164 object::mach::CSPPC_ALL),
165 OS, /*IsLittleEndian=*/false);
Chris Lattneraac9fa72010-11-15 08:49:58 +0000166 }
Jim Grosbache2d29812012-01-18 18:52:20 +0000167
Chris Lattneraac9fa72010-11-15 08:49:58 +0000168 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
169 return false;
170 }
171 };
Roman Divacky038c1a12011-08-02 15:51:38 +0000172
173 class ELFPPCAsmBackend : public PPCAsmBackend {
Rafael Espindola1ad40952011-12-21 17:00:36 +0000174 uint8_t OSABI;
Roman Divacky038c1a12011-08-02 15:51:38 +0000175 public:
Rafael Espindola1ad40952011-12-21 17:00:36 +0000176 ELFPPCAsmBackend(const Target &T, uint8_t OSABI) :
177 PPCAsmBackend(T), OSABI(OSABI) { }
Jim Grosbache2d29812012-01-18 18:52:20 +0000178
Jim Grosbache2d29812012-01-18 18:52:20 +0000179
Roman Divacky038c1a12011-08-02 15:51:38 +0000180 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
181 bool is64 = getPointerSize() == 8;
Rafael Espindola25009622011-12-22 18:38:06 +0000182 return createPPCELFObjectWriter(OS, is64, OSABI);
Roman Divacky038c1a12011-08-02 15:51:38 +0000183 }
Jim Grosbache2d29812012-01-18 18:52:20 +0000184
Roman Divacky038c1a12011-08-02 15:51:38 +0000185 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
186 return false;
187 }
188 };
189
Chris Lattneraac9fa72010-11-15 08:49:58 +0000190} // end anonymous namespace
191
192
193
194
Roman Divacky5dd4ccb2012-09-18 16:08:49 +0000195MCAsmBackend *llvm::createPPCAsmBackend(const Target &T, StringRef TT, StringRef CPU) {
Daniel Dunbar2b9b0e32011-04-19 21:14:45 +0000196 if (Triple(TT).isOSDarwin())
Chris Lattneraac9fa72010-11-15 08:49:58 +0000197 return new DarwinPPCAsmBackend(T);
Daniel Dunbar2b9b0e32011-04-19 21:14:45 +0000198
Rafael Espindola1ad40952011-12-21 17:00:36 +0000199 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS());
200 return new ELFPPCAsmBackend(T, OSABI);
Chris Lattneraac9fa72010-11-15 08:49:58 +0000201}