blob: 12e0b3d436d1fff7cca16907895635910071cea6 [file] [log] [blame]
Chris Lattnerb46443a2010-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 Cheng78c10ee2011-07-25 23:24:55 +000010#include "llvm/MC/MCAsmBackend.h"
Evan Cheng966aeb52011-07-25 19:53:23 +000011#include "MCTargetDesc/PPCMCTargetDesc.h"
12#include "MCTargetDesc/PPCFixupKinds.h"
Roman Divacky2c0d69f2011-08-02 15:51:38 +000013#include "llvm/MC/MCELFObjectWriter.h"
Daniel Dunbaraa4b7dd2010-12-16 16:08:33 +000014#include "llvm/MC/MCMachObjectWriter.h"
Chris Lattnerb46443a2010-11-15 08:49:58 +000015#include "llvm/MC/MCSectionMachO.h"
Chris Lattnerb46443a2010-11-15 08:49:58 +000016#include "llvm/MC/MCObjectWriter.h"
Jim Grosbachba8297e2011-06-24 23:44:37 +000017#include "llvm/MC/MCValue.h"
Daniel Dunbar36d76a82010-11-27 04:38:36 +000018#include "llvm/Object/MachOFormat.h"
Roman Divacky2c0d69f2011-08-02 15:51:38 +000019#include "llvm/Support/ELF.h"
20#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000021#include "llvm/Support/TargetRegistry.h"
Chris Lattnerb46443a2010-11-15 08:49:58 +000022using namespace llvm;
23
Roman Divacky2c0d69f2011-08-02 15:51:38 +000024static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
25 switch (Kind) {
26 default:
27 llvm_unreachable("Unknown fixup kind!");
28 case FK_Data_1:
29 case FK_Data_2:
30 case FK_Data_4:
31 return Value;
32 case PPC::fixup_ppc_brcond14:
33 return Value & 0x3ffc;
34 case PPC::fixup_ppc_br24:
35 return Value & 0x3fffffc;
36#if 0
37 case PPC::fixup_ppc_hi16:
38 return (Value >> 16) & 0xffff;
39#endif
40 case PPC::fixup_ppc_ha16:
41 return ((Value >> 16) + ((Value & 0x8000) ? 1 : 0)) & 0xffff;
42 case PPC::fixup_ppc_lo16:
43 return Value & 0xffff;
44 }
45}
46
Chris Lattnerb46443a2010-11-15 08:49:58 +000047namespace {
Daniel Dunbarae5abd52010-12-16 16:09:19 +000048class PPCMachObjectWriter : public MCMachObjectTargetWriter {
Daniel Dunbar5d05d972010-12-16 17:21:02 +000049public:
50 PPCMachObjectWriter(bool Is64Bit, uint32_t CPUType,
51 uint32_t CPUSubtype)
52 : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {}
Jim Grosbachba8297e2011-06-24 23:44:37 +000053
54 void RecordRelocation(MachObjectWriter *Writer,
55 const MCAssembler &Asm, const MCAsmLayout &Layout,
56 const MCFragment *Fragment, const MCFixup &Fixup,
57 MCValue Target, uint64_t &FixedValue) {}
Daniel Dunbarae5abd52010-12-16 16:09:19 +000058};
59
Roman Divacky2c0d69f2011-08-02 15:51:38 +000060class PPCELFObjectWriter : public MCELFObjectTargetWriter {
61public:
62 PPCELFObjectWriter(bool Is64Bit, Triple::OSType OSType, uint16_t EMachine,
63 bool HasRelocationAddend, bool isLittleEndian)
64 : MCELFObjectTargetWriter(Is64Bit, OSType, EMachine, HasRelocationAddend) {}
65};
66
Evan Cheng78c10ee2011-07-25 23:24:55 +000067class PPCAsmBackend : public MCAsmBackend {
Daniel Dunbar297ed282010-12-16 16:08:43 +000068const Target &TheTarget;
69public:
Evan Cheng78c10ee2011-07-25 23:24:55 +000070 PPCAsmBackend(const Target &T) : MCAsmBackend(), TheTarget(T) {}
Daniel Dunbar2761fc42010-12-16 03:20:06 +000071
Daniel Dunbar297ed282010-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 },
81 { "fixup_ppc_lo14", 16, 14, 0 }
82 };
83
84 if (Kind < FirstTargetFixupKind)
Evan Cheng78c10ee2011-07-25 23:24:55 +000085 return MCAsmBackend::getFixupKindInfo(Kind);
Daniel Dunbar297ed282010-12-16 16:08:43 +000086
87 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
88 "Invalid kind!");
89 return Infos[Kind - FirstTargetFixupKind];
90 }
91
92 bool MayNeedRelaxation(const MCInst &Inst) const {
93 // FIXME.
94 return false;
95 }
Jim Grosbach370b78d2011-12-06 00:47:03 +000096
97 bool fixupNeedsRelaxation(const MCFixup &Fixup,
98 uint64_t Value,
99 const MCInstFragment *DF,
100 const MCAsmLayout &Layout) const {
101 // FIXME.
102 assert(0 && "RelaxInstruction() unimplemented");
103 }
104
Daniel Dunbar297ed282010-12-16 16:08:43 +0000105
106 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
107 // FIXME.
108 assert(0 && "RelaxInstruction() unimplemented");
109 }
110
111 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
112 // FIXME: Zero fill for now. That's not right, but at least will get the
113 // section size right.
114 for (uint64_t i = 0; i != Count; ++i)
115 OW->Write8(0);
116 return true;
117 }
118
119 unsigned getPointerSize() const {
120 StringRef Name = TheTarget.getName();
121 if (Name == "ppc64") return 8;
122 assert(Name == "ppc32" && "Unknown target name!");
123 return 4;
124 }
125};
Chris Lattnerb46443a2010-11-15 08:49:58 +0000126} // end anonymous namespace
127
128
129// FIXME: This should be in a separate file.
130namespace {
131 class DarwinPPCAsmBackend : public PPCAsmBackend {
Chris Lattnerb46443a2010-11-15 08:49:58 +0000132 public:
Daniel Dunbar7b62afa2010-12-17 02:06:08 +0000133 DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) { }
Chris Lattnerb46443a2010-11-15 08:49:58 +0000134
Rafael Espindola179821a2010-12-06 19:08:48 +0000135 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Chris Lattnerb46443a2010-11-15 08:49:58 +0000136 uint64_t Value) const {
137 assert(0 && "UNIMP");
138 }
139
Chris Lattnerb46443a2010-11-15 08:49:58 +0000140 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
141 bool is64 = getPointerSize() == 8;
Daniel Dunbar5d05d972010-12-16 17:21:02 +0000142 return createMachObjectWriter(new PPCMachObjectWriter(
143 /*Is64Bit=*/is64,
144 (is64 ? object::mach::CTM_PowerPC64 :
145 object::mach::CTM_PowerPC),
146 object::mach::CSPPC_ALL),
147 OS, /*IsLittleEndian=*/false);
Chris Lattnerb46443a2010-11-15 08:49:58 +0000148 }
149
150 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
151 return false;
152 }
153 };
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000154
155 class ELFPPCAsmBackend : public PPCAsmBackend {
156 Triple::OSType OSType;
157 public:
158 ELFPPCAsmBackend(const Target &T, Triple::OSType OSType) :
159 PPCAsmBackend(T), OSType(OSType) { }
160
161 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
162 uint64_t Value) const {
163 Value = adjustFixupValue(Fixup.getKind(), Value);
164 if (!Value) return; // Doesn't change encoding.
165
166 unsigned Offset = Fixup.getOffset();
167
168 // For each byte of the fragment that the fixup touches, mask in the bits from
169 // the fixup value. The Value has been "split up" into the appropriate
170 // bitfields above.
171 for (unsigned i = 0; i != 4; ++i)
172 Data[Offset + i] |= uint8_t((Value >> ((4 - i - 1)*8)) & 0xff);
173 }
174
175 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
176 bool is64 = getPointerSize() == 8;
177 return createELFObjectWriter(new PPCELFObjectWriter(
178 /*Is64Bit=*/is64,
179 OSType,
180 is64 ? ELF::EM_PPC64 : ELF::EM_PPC,
181 /*addend*/ true, /*isLittleEndian*/ false),
182 OS, /*IsLittleEndian=*/false);
183 }
184
185 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
186 return false;
187 }
188 };
189
Chris Lattnerb46443a2010-11-15 08:49:58 +0000190} // end anonymous namespace
191
192
193
194
Evan Cheng78c10ee2011-07-25 23:24:55 +0000195MCAsmBackend *llvm::createPPCAsmBackend(const Target &T, StringRef TT) {
Daniel Dunbar912225e2011-04-19 21:14:45 +0000196 if (Triple(TT).isOSDarwin())
Chris Lattnerb46443a2010-11-15 08:49:58 +0000197 return new DarwinPPCAsmBackend(T);
Daniel Dunbar912225e2011-04-19 21:14:45 +0000198
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000199 return new ELFPPCAsmBackend(T, Triple(TT).getOS());
Chris Lattnerb46443a2010-11-15 08:49:58 +0000200}