blob: 87ecb13a4c763d33d8ca1d3619d9cf17f13747a3 [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/MCSectionMachO.h"
Chris Lattneraac9fa72010-11-15 08:49:58 +000017#include "llvm/MC/MCObjectWriter.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:
Roman Divacky038c1a12011-08-02 15:51:38 +000034 return Value;
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000035 case PPC::fixup_ppc_lo14:
36 case PPC::fixup_ppc_toc16_ds:
37 return (Value & 0xffff) << 2;
Roman Divacky038c1a12011-08-02 15:51:38 +000038 case PPC::fixup_ppc_brcond14:
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000039 return Value & 0xfffc;
Roman Divacky038c1a12011-08-02 15:51:38 +000040 case PPC::fixup_ppc_br24:
41 return Value & 0x3fffffc;
42#if 0
43 case PPC::fixup_ppc_hi16:
44 return (Value >> 16) & 0xffff;
45#endif
46 case PPC::fixup_ppc_ha16:
47 return ((Value >> 16) + ((Value & 0x8000) ? 1 : 0)) & 0xffff;
48 case PPC::fixup_ppc_lo16:
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000049 case PPC::fixup_ppc_toc16:
Roman Divacky038c1a12011-08-02 15:51:38 +000050 return Value & 0xffff;
51 }
52}
53
Chris Lattneraac9fa72010-11-15 08:49:58 +000054namespace {
Daniel Dunbar8888a962010-12-16 16:09:19 +000055class PPCMachObjectWriter : public MCMachObjectTargetWriter {
Daniel Dunbar03fcccb2010-12-16 17:21:02 +000056public:
57 PPCMachObjectWriter(bool Is64Bit, uint32_t CPUType,
58 uint32_t CPUSubtype)
59 : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {}
Jim Grosbach28fcafb2011-06-24 23:44:37 +000060
61 void RecordRelocation(MachObjectWriter *Writer,
62 const MCAssembler &Asm, const MCAsmLayout &Layout,
63 const MCFragment *Fragment, const MCFixup &Fixup,
64 MCValue Target, uint64_t &FixedValue) {}
Daniel Dunbar8888a962010-12-16 16:09:19 +000065};
66
Evan Cheng5928e692011-07-25 23:24:55 +000067class PPCAsmBackend : public MCAsmBackend {
Daniel Dunbar7ee21812010-12-16 16:08:43 +000068const Target &TheTarget;
69public:
Evan Cheng5928e692011-07-25 23:24:55 +000070 PPCAsmBackend(const Target &T) : MCAsmBackend(), TheTarget(T) {}
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +000071
Daniel Dunbar7ee21812010-12-16 16:08:43 +000072 unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; }
73
74 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
75 const static MCFixupKindInfo Infos[PPC::NumTargetFixupKinds] = {
76 // name offset bits flags
77 { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel },
78 { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel },
79 { "fixup_ppc_lo16", 16, 16, 0 },
80 { "fixup_ppc_ha16", 16, 16, 0 },
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000081 { "fixup_ppc_lo14", 16, 14, 0 },
82 { "fixup_ppc_toc", 0, 64, 0 },
83 { "fixup_ppc_toc16", 16, 16, 0 },
84 { "fixup_ppc_toc16_ds", 16, 14, 0 }
Daniel Dunbar7ee21812010-12-16 16:08:43 +000085 };
Jim Grosbache2d29812012-01-18 18:52:20 +000086
Daniel Dunbar7ee21812010-12-16 16:08:43 +000087 if (Kind < FirstTargetFixupKind)
Evan Cheng5928e692011-07-25 23:24:55 +000088 return MCAsmBackend::getFixupKindInfo(Kind);
Jim Grosbache2d29812012-01-18 18:52:20 +000089
Daniel Dunbar7ee21812010-12-16 16:08:43 +000090 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
91 "Invalid kind!");
92 return Infos[Kind - FirstTargetFixupKind];
93 }
Jim Grosbache2d29812012-01-18 18:52:20 +000094
Jim Grosbachaba3de92012-01-18 18:52:16 +000095 bool mayNeedRelaxation(const MCInst &Inst) const {
Daniel Dunbar7ee21812010-12-16 16:08:43 +000096 // FIXME.
97 return false;
98 }
Jim Grosbach25b63fa2011-12-06 00:47:03 +000099
100 bool fixupNeedsRelaxation(const MCFixup &Fixup,
101 uint64_t Value,
102 const MCInstFragment *DF,
103 const MCAsmLayout &Layout) const {
104 // FIXME.
Craig Toppere55c5562012-02-07 02:50:20 +0000105 llvm_unreachable("relaxInstruction() unimplemented");
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000106 }
107
Jim Grosbache2d29812012-01-18 18:52:20 +0000108
Jim Grosbachaba3de92012-01-18 18:52:16 +0000109 void relaxInstruction(const MCInst &Inst, MCInst &Res) const {
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000110 // FIXME.
Craig Toppere55c5562012-02-07 02:50:20 +0000111 llvm_unreachable("relaxInstruction() unimplemented");
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000112 }
Jim Grosbache2d29812012-01-18 18:52:20 +0000113
Jim Grosbachaba3de92012-01-18 18:52:16 +0000114 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const {
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000115 // FIXME: Zero fill for now. That's not right, but at least will get the
116 // section size right.
117 for (uint64_t i = 0; i != Count; ++i)
118 OW->Write8(0);
119 return true;
Jim Grosbache2d29812012-01-18 18:52:20 +0000120 }
121
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000122 unsigned getPointerSize() const {
123 StringRef Name = TheTarget.getName();
124 if (Name == "ppc64") return 8;
125 assert(Name == "ppc32" && "Unknown target name!");
126 return 4;
127 }
128};
Chris Lattneraac9fa72010-11-15 08:49:58 +0000129} // end anonymous namespace
130
131
132// FIXME: This should be in a separate file.
133namespace {
134 class DarwinPPCAsmBackend : public PPCAsmBackend {
Chris Lattneraac9fa72010-11-15 08:49:58 +0000135 public:
Daniel Dunbard2867f12010-12-17 02:06:08 +0000136 DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) { }
Jim Grosbache2d29812012-01-18 18:52:20 +0000137
Jim Grosbachaba3de92012-01-18 18:52:16 +0000138 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Chris Lattneraac9fa72010-11-15 08:49:58 +0000139 uint64_t Value) const {
Craig Toppere55c5562012-02-07 02:50:20 +0000140 llvm_unreachable("UNIMP");
Chris Lattneraac9fa72010-11-15 08:49:58 +0000141 }
Jim Grosbache2d29812012-01-18 18:52:20 +0000142
Chris Lattneraac9fa72010-11-15 08:49:58 +0000143 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
144 bool is64 = getPointerSize() == 8;
Daniel Dunbar03fcccb2010-12-16 17:21:02 +0000145 return createMachObjectWriter(new PPCMachObjectWriter(
146 /*Is64Bit=*/is64,
147 (is64 ? object::mach::CTM_PowerPC64 :
148 object::mach::CTM_PowerPC),
149 object::mach::CSPPC_ALL),
150 OS, /*IsLittleEndian=*/false);
Chris Lattneraac9fa72010-11-15 08:49:58 +0000151 }
Jim Grosbache2d29812012-01-18 18:52:20 +0000152
Chris Lattneraac9fa72010-11-15 08:49:58 +0000153 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
154 return false;
155 }
156 };
Roman Divacky038c1a12011-08-02 15:51:38 +0000157
158 class ELFPPCAsmBackend : public PPCAsmBackend {
Rafael Espindola1ad40952011-12-21 17:00:36 +0000159 uint8_t OSABI;
Roman Divacky038c1a12011-08-02 15:51:38 +0000160 public:
Rafael Espindola1ad40952011-12-21 17:00:36 +0000161 ELFPPCAsmBackend(const Target &T, uint8_t OSABI) :
162 PPCAsmBackend(T), OSABI(OSABI) { }
Jim Grosbache2d29812012-01-18 18:52:20 +0000163
Jim Grosbachaba3de92012-01-18 18:52:16 +0000164 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Roman Divacky038c1a12011-08-02 15:51:38 +0000165 uint64_t Value) const {
166 Value = adjustFixupValue(Fixup.getKind(), Value);
167 if (!Value) return; // Doesn't change encoding.
168
169 unsigned Offset = Fixup.getOffset();
170
171 // For each byte of the fragment that the fixup touches, mask in the bits from
172 // the fixup value. The Value has been "split up" into the appropriate
173 // bitfields above.
174 for (unsigned i = 0; i != 4; ++i)
175 Data[Offset + i] |= uint8_t((Value >> ((4 - i - 1)*8)) & 0xff);
176 }
Jim Grosbache2d29812012-01-18 18:52:20 +0000177
Roman Divacky038c1a12011-08-02 15:51:38 +0000178 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
179 bool is64 = getPointerSize() == 8;
Rafael Espindola25009622011-12-22 18:38:06 +0000180 return createPPCELFObjectWriter(OS, is64, OSABI);
Roman Divacky038c1a12011-08-02 15:51:38 +0000181 }
Jim Grosbache2d29812012-01-18 18:52:20 +0000182
Roman Divacky038c1a12011-08-02 15:51:38 +0000183 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
184 return false;
185 }
186 };
187
Chris Lattneraac9fa72010-11-15 08:49:58 +0000188} // end anonymous namespace
189
190
191
192
Roman Divacky5dd4ccb2012-09-18 16:08:49 +0000193MCAsmBackend *llvm::createPPCAsmBackend(const Target &T, StringRef TT, StringRef CPU) {
Daniel Dunbar2b9b0e32011-04-19 21:14:45 +0000194 if (Triple(TT).isOSDarwin())
Chris Lattneraac9fa72010-11-15 08:49:58 +0000195 return new DarwinPPCAsmBackend(T);
Daniel Dunbar2b9b0e32011-04-19 21:14:45 +0000196
Rafael Espindola1ad40952011-12-21 17:00:36 +0000197 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS());
198 return new ELFPPCAsmBackend(T, OSABI);
Chris Lattneraac9fa72010-11-15 08:49:58 +0000199}