blob: 1a58961a8e2ad3a5e63a66bd54cc122042d1f10f [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
Benjamin Kramer8abfec32012-11-24 13:18:17 +000095 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
96 uint64_t Value) const {
97 Value = adjustFixupValue(Fixup.getKind(), Value);
98 if (!Value) return; // Doesn't change encoding.
99
100 unsigned Offset = Fixup.getOffset();
101
102 // For each byte of the fragment that the fixup touches, mask in the bits
103 // from the fixup value. The Value has been "split up" into the appropriate
104 // bitfields above.
105 for (unsigned i = 0; i != 4; ++i)
106 Data[Offset + i] |= uint8_t((Value >> ((4 - i - 1)*8)) & 0xff);
107 }
108
Jim Grosbachaba3de92012-01-18 18:52:16 +0000109 bool mayNeedRelaxation(const MCInst &Inst) const {
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000110 // FIXME.
111 return false;
112 }
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000113
114 bool fixupNeedsRelaxation(const MCFixup &Fixup,
115 uint64_t Value,
116 const MCInstFragment *DF,
117 const MCAsmLayout &Layout) const {
118 // FIXME.
Craig Toppere55c5562012-02-07 02:50:20 +0000119 llvm_unreachable("relaxInstruction() unimplemented");
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000120 }
121
Jim Grosbache2d29812012-01-18 18:52:20 +0000122
Jim Grosbachaba3de92012-01-18 18:52:16 +0000123 void relaxInstruction(const MCInst &Inst, MCInst &Res) const {
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000124 // FIXME.
Craig Toppere55c5562012-02-07 02:50:20 +0000125 llvm_unreachable("relaxInstruction() unimplemented");
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000126 }
Jim Grosbache2d29812012-01-18 18:52:20 +0000127
Jim Grosbachaba3de92012-01-18 18:52:16 +0000128 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const {
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000129 // FIXME: Zero fill for now. That's not right, but at least will get the
130 // section size right.
131 for (uint64_t i = 0; i != Count; ++i)
132 OW->Write8(0);
133 return true;
Jim Grosbache2d29812012-01-18 18:52:20 +0000134 }
135
Daniel Dunbar7ee21812010-12-16 16:08:43 +0000136 unsigned getPointerSize() const {
137 StringRef Name = TheTarget.getName();
138 if (Name == "ppc64") return 8;
139 assert(Name == "ppc32" && "Unknown target name!");
140 return 4;
141 }
142};
Chris Lattneraac9fa72010-11-15 08:49:58 +0000143} // end anonymous namespace
144
145
146// FIXME: This should be in a separate file.
147namespace {
148 class DarwinPPCAsmBackend : public PPCAsmBackend {
Chris Lattneraac9fa72010-11-15 08:49:58 +0000149 public:
Daniel Dunbard2867f12010-12-17 02:06:08 +0000150 DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) { }
Jim Grosbache2d29812012-01-18 18:52:20 +0000151
Chris Lattneraac9fa72010-11-15 08:49:58 +0000152 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
153 bool is64 = getPointerSize() == 8;
Daniel Dunbar03fcccb2010-12-16 17:21:02 +0000154 return createMachObjectWriter(new PPCMachObjectWriter(
155 /*Is64Bit=*/is64,
156 (is64 ? object::mach::CTM_PowerPC64 :
157 object::mach::CTM_PowerPC),
158 object::mach::CSPPC_ALL),
159 OS, /*IsLittleEndian=*/false);
Chris Lattneraac9fa72010-11-15 08:49:58 +0000160 }
Jim Grosbache2d29812012-01-18 18:52:20 +0000161
Chris Lattneraac9fa72010-11-15 08:49:58 +0000162 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
163 return false;
164 }
165 };
Roman Divacky038c1a12011-08-02 15:51:38 +0000166
167 class ELFPPCAsmBackend : public PPCAsmBackend {
Rafael Espindola1ad40952011-12-21 17:00:36 +0000168 uint8_t OSABI;
Roman Divacky038c1a12011-08-02 15:51:38 +0000169 public:
Rafael Espindola1ad40952011-12-21 17:00:36 +0000170 ELFPPCAsmBackend(const Target &T, uint8_t OSABI) :
171 PPCAsmBackend(T), OSABI(OSABI) { }
Jim Grosbache2d29812012-01-18 18:52:20 +0000172
Jim Grosbache2d29812012-01-18 18:52:20 +0000173
Roman Divacky038c1a12011-08-02 15:51:38 +0000174 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
175 bool is64 = getPointerSize() == 8;
Rafael Espindola25009622011-12-22 18:38:06 +0000176 return createPPCELFObjectWriter(OS, is64, OSABI);
Roman Divacky038c1a12011-08-02 15:51:38 +0000177 }
Jim Grosbache2d29812012-01-18 18:52:20 +0000178
Roman Divacky038c1a12011-08-02 15:51:38 +0000179 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
180 return false;
181 }
182 };
183
Chris Lattneraac9fa72010-11-15 08:49:58 +0000184} // end anonymous namespace
185
186
187
188
Roman Divacky5dd4ccb2012-09-18 16:08:49 +0000189MCAsmBackend *llvm::createPPCAsmBackend(const Target &T, StringRef TT, StringRef CPU) {
Daniel Dunbar2b9b0e32011-04-19 21:14:45 +0000190 if (Triple(TT).isOSDarwin())
Chris Lattneraac9fa72010-11-15 08:49:58 +0000191 return new DarwinPPCAsmBackend(T);
Daniel Dunbar2b9b0e32011-04-19 21:14:45 +0000192
Rafael Espindola1ad40952011-12-21 17:00:36 +0000193 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS());
194 return new ELFPPCAsmBackend(T, OSABI);
Chris Lattneraac9fa72010-11-15 08:49:58 +0000195}