blob: db7e20cfc01b54d8ca4ebd315b0c251a848c0fee [file] [log] [blame]
Jason W Kimd4d4f4f2010-09-30 02:17:26 +00001//===-- ARMAsmBackend.cpp - ARM 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
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000010#include "ARM.h"
Jim Grosbachdff84b02010-12-02 00:28:45 +000011#include "ARMAddressingModes.h"
Jim Grosbach679cbd32010-11-09 01:37:15 +000012#include "ARMFixupKinds.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000013#include "llvm/ADT/Twine.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000014#include "llvm/MC/MCAssembler.h"
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000015#include "llvm/MC/MCDirectives.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000016#include "llvm/MC/MCExpr.h"
Daniel Dunbaraa4b7dd2010-12-16 16:08:33 +000017#include "llvm/MC/MCMachObjectWriter.h"
Rafael Espindolaf230df92010-10-16 18:23:53 +000018#include "llvm/MC/MCObjectFormat.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000019#include "llvm/MC/MCObjectWriter.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000020#include "llvm/MC/MCSectionELF.h"
21#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbar36d76a82010-11-27 04:38:36 +000022#include "llvm/Object/MachOFormat.h"
Wesley Peckeecb8582010-10-22 15:52:49 +000023#include "llvm/Support/ELF.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000024#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/raw_ostream.h"
Jim Grosbachdff84b02010-12-02 00:28:45 +000026#include "llvm/Target/TargetAsmBackend.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000027#include "llvm/Target/TargetRegistry.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000028using namespace llvm;
29
30namespace {
Daniel Dunbarae5abd52010-12-16 16:09:19 +000031class ARMMachObjectWriter : public MCMachObjectTargetWriter {
Daniel Dunbar5d05d972010-12-16 17:21:02 +000032public:
33 ARMMachObjectWriter(bool Is64Bit, uint32_t CPUType,
34 uint32_t CPUSubtype)
35 : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {}
Daniel Dunbarae5abd52010-12-16 16:09:19 +000036};
37
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000038class ARMAsmBackend : public TargetAsmBackend {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000039 bool isThumbMode; // Currently emitting Thumb code.
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000040public:
Jim Grosbach022ab372010-12-08 15:36:45 +000041 ARMAsmBackend(const Target &T) : TargetAsmBackend(), isThumbMode(false) {}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000042
Daniel Dunbar2761fc42010-12-16 03:20:06 +000043 unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
44
45 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
46 const static MCFixupKindInfo Infos[ARM::NumTargetFixupKinds] = {
47// This table *must* be in the order that the fixup_* kinds are defined in
48// ARMFixupKinds.h.
49//
50// Name Offset (bits) Size (bits) Flags
51{ "fixup_arm_ldst_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
52{ "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
53 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
54{ "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
55{ "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
56 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
57{ "fixup_thumb_adr_pcrel_10",0, 8, MCFixupKindInfo::FKF_IsPCRel |
58 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
59{ "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
60{ "fixup_t2_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
61 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
62{ "fixup_arm_branch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
63{ "fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
64{ "fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
65{ "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
66{ "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
67{ "fixup_arm_thumb_blx", 7, 21, MCFixupKindInfo::FKF_IsPCRel },
68{ "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
69{ "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
70{ "fixup_arm_thumb_bcc", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
71{ "fixup_arm_movt_hi16", 0, 16, 0 },
72{ "fixup_arm_movw_lo16", 0, 16, 0 },
73 };
74
75 if (Kind < FirstTargetFixupKind)
76 return TargetAsmBackend::getFixupKindInfo(Kind);
77
78 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
79 "Invalid kind!");
80 return Infos[Kind - FirstTargetFixupKind];
81 }
82
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000083 bool MayNeedRelaxation(const MCInst &Inst) const;
84
85 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
86
87 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
Jim Grosbach3787a402010-09-30 17:45:51 +000088
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000089 void HandleAssemblerFlag(MCAssemblerFlag Flag) {
90 switch (Flag) {
91 default: break;
92 case MCAF_Code16:
93 setIsThumb(true);
94 break;
95 case MCAF_Code32:
96 setIsThumb(false);
97 break;
98 }
Jim Grosbach3787a402010-09-30 17:45:51 +000099 }
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000100
101 unsigned getPointerSize() const { return 4; }
102 bool isThumb() const { return isThumbMode; }
103 void setIsThumb(bool it) { isThumbMode = it; }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000104};
Chris Lattnerb75c6512010-11-17 05:41:32 +0000105} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000106
107bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
108 // FIXME: Thumb targets, different move constant targets..
109 return false;
110}
111
112void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
113 assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
114 return;
115}
116
117bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000118 if (isThumb()) {
119 assert (((Count & 1) == 0) && "Unaligned Nop data fragment!");
120 // FIXME: 0xbf00 is the ARMv7 value. For v6 and before, we'll need to
121 // use 0x46c0 (which is a 'mov r8, r8' insn).
122 Count /= 2;
123 for (uint64_t i = 0; i != Count; ++i)
124 OW->Write16(0xbf00);
125 return true;
126 }
127 // ARM mode
128 Count /= 4;
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000129 for (uint64_t i = 0; i != Count; ++i)
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000130 OW->Write32(0xe1a00000);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000131 return true;
Jim Grosbach87dc3aa2010-09-30 03:20:34 +0000132}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000133
Jason W Kim0c628c22010-12-01 22:46:50 +0000134static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
135 switch (Kind) {
136 default:
137 llvm_unreachable("Unknown fixup kind!");
138 case FK_Data_4:
Jason W Kim0c628c22010-12-01 22:46:50 +0000139 return Value;
Jason W Kim2ccf1482010-12-03 19:40:23 +0000140 case ARM::fixup_arm_movt_hi16:
141 case ARM::fixup_arm_movw_lo16: {
142 unsigned Hi4 = (Value & 0xF000) >> 12;
143 unsigned Lo12 = Value & 0x0FFF;
144 // inst{19-16} = Hi4;
145 // inst{11-0} = Lo12;
146 Value = (Hi4 << 16) | (Lo12);
147 return Value;
148 }
Owen Andersond7b3f582010-12-09 01:51:07 +0000149 case ARM::fixup_arm_ldst_pcrel_12:
Jason W Kim0c628c22010-12-01 22:46:50 +0000150 // ARM PC-relative values are offset by 8.
Owen Anderson05018c22010-12-09 20:27:52 +0000151 Value -= 4;
Owen Andersonfe7fac72010-12-09 21:34:47 +0000152 // FALLTHROUGH
Owen Andersond7b3f582010-12-09 01:51:07 +0000153 case ARM::fixup_t2_ldst_pcrel_12: {
154 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Anderson05018c22010-12-09 20:27:52 +0000155 Value -= 4;
Owen Andersond7b3f582010-12-09 01:51:07 +0000156 bool isAdd = true;
Jason W Kim0c628c22010-12-01 22:46:50 +0000157 if ((int64_t)Value < 0) {
158 Value = -Value;
159 isAdd = false;
160 }
161 assert ((Value < 4096) && "Out of range pc-relative fixup value!");
162 Value |= isAdd << 23;
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000163
Owen Andersond7b3f582010-12-09 01:51:07 +0000164 // Same addressing mode as fixup_arm_pcrel_10,
165 // but with 16-bit halfwords swapped.
166 if (Kind == ARM::fixup_t2_ldst_pcrel_12) {
167 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
168 swapped |= (Value & 0x0000FFFF) << 16;
169 return swapped;
170 }
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000171
Jason W Kim0c628c22010-12-01 22:46:50 +0000172 return Value;
173 }
Jim Grosbachd40963c2010-12-14 22:28:03 +0000174 case ARM::fixup_thumb_adr_pcrel_10:
175 return ((Value - 4) >> 2) & 0xff;
Jim Grosbachdff84b02010-12-02 00:28:45 +0000176 case ARM::fixup_arm_adr_pcrel_12: {
177 // ARM PC-relative values are offset by 8.
178 Value -= 8;
179 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
180 if ((int64_t)Value < 0) {
181 Value = -Value;
182 opc = 2; // 0b0010
183 }
184 assert(ARM_AM::getSOImmVal(Value) != -1 &&
185 "Out of range pc-relative fixup value!");
186 // Encode the immediate and shift the opcode into place.
187 return ARM_AM::getSOImmVal(Value) | (opc << 21);
188 }
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000189
Owen Andersona838a252010-12-14 00:36:49 +0000190 case ARM::fixup_t2_adr_pcrel_12: {
191 Value -= 4;
192 unsigned opc = 0;
193 if ((int64_t)Value < 0) {
194 Value = -Value;
195 opc = 5;
196 }
197
198 uint32_t out = (opc << 21);
199 out |= (Value & 0x800) << 14;
200 out |= (Value & 0x700) << 4;
201 out |= (Value & 0x0FF);
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000202
Owen Andersona838a252010-12-14 00:36:49 +0000203 uint64_t swapped = (out & 0xFFFF0000) >> 16;
204 swapped |= (out & 0x0000FFFF) << 16;
205 return swapped;
206 }
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000207
Jason W Kim0c628c22010-12-01 22:46:50 +0000208 case ARM::fixup_arm_branch:
209 // These values don't encode the low two bits since they're always zero.
210 // Offset by 8 just as above.
Jim Grosbach662a8162010-12-06 23:57:07 +0000211 return 0xffffff & ((Value - 8) >> 2);
Owen Andersonc2666002010-12-13 19:31:11 +0000212 case ARM::fixup_t2_uncondbranch: {
Owen Anderson63ee2202010-12-10 23:02:28 +0000213 Value = Value - 4;
Owen Andersonfb20d892010-12-09 00:27:41 +0000214 Value >>= 1; // Low bit is not encoded.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000215
Jim Grosbach56a25352010-12-13 19:25:46 +0000216 uint32_t out = 0;
Owen Andersonc2666002010-12-13 19:31:11 +0000217 bool I = Value & 0x800000;
218 bool J1 = Value & 0x400000;
219 bool J2 = Value & 0x200000;
220 J1 ^= I;
221 J2 ^= I;
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000222
Owen Andersonc2666002010-12-13 19:31:11 +0000223 out |= I << 26; // S bit
224 out |= !J1 << 13; // J1 bit
225 out |= !J2 << 11; // J2 bit
226 out |= (Value & 0x1FF800) << 5; // imm6 field
227 out |= (Value & 0x0007FF); // imm11 field
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000228
Owen Andersonc2666002010-12-13 19:31:11 +0000229 uint64_t swapped = (out & 0xFFFF0000) >> 16;
230 swapped |= (out & 0x0000FFFF) << 16;
231 return swapped;
232 }
233 case ARM::fixup_t2_condbranch: {
234 Value = Value - 4;
235 Value >>= 1; // Low bit is not encoded.
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000236
Owen Andersonc2666002010-12-13 19:31:11 +0000237 uint64_t out = 0;
Owen Anderson8f079432010-12-09 01:02:09 +0000238 out |= (Value & 0x80000) << 7; // S bit
239 out |= (Value & 0x40000) >> 7; // J2 bit
240 out |= (Value & 0x20000) >> 4; // J1 bit
241 out |= (Value & 0x1F800) << 5; // imm6 field
242 out |= (Value & 0x007FF); // imm11 field
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000243
Jim Grosbach56a25352010-12-13 19:25:46 +0000244 uint32_t swapped = (out & 0xFFFF0000) >> 16;
Owen Andersonfb20d892010-12-09 00:27:41 +0000245 swapped |= (out & 0x0000FFFF) << 16;
246 return swapped;
247 }
Jim Grosbach662a8162010-12-06 23:57:07 +0000248 case ARM::fixup_arm_thumb_bl: {
249 // The value doesn't encode the low bit (always zero) and is offset by
250 // four. The value is encoded into disjoint bit positions in the destination
251 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000252 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000253 // BL: xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000254 //
Jim Grosbach662a8162010-12-06 23:57:07 +0000255 // Note that the halfwords are stored high first, low second; so we need
256 // to transpose the fixup value here to map properly.
Bill Wendling09aa3f02010-12-09 00:39:08 +0000257 unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000258 uint32_t Binary = 0;
259 Value = 0x3fffff & ((Value - 4) >> 1);
260 Binary = (Value & 0x7ff) << 16; // Low imm11 value.
261 Binary |= (Value & 0x1ffc00) >> 11; // High imm10 value.
262 Binary |= isNeg << 10; // Sign bit.
Bill Wendling09aa3f02010-12-09 00:39:08 +0000263 return Binary;
264 }
265 case ARM::fixup_arm_thumb_blx: {
266 // The value doesn't encode the low two bits (always zero) and is offset by
267 // four (see fixup_arm_thumb_cp). The value is encoded into disjoint bit
268 // positions in the destination opcode. x = unchanged, I = immediate value
269 // bit, S = sign extension bit, 0 = zero.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000270 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000271 // BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000272 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000273 // Note that the halfwords are stored high first, low second; so we need
274 // to transpose the fixup value here to map properly.
275 unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000276 uint32_t Binary = 0;
277 Value = 0xfffff & ((Value - 2) >> 2);
278 Binary = (Value & 0x3ff) << 17; // Low imm10L value.
279 Binary |= (Value & 0xffc00) >> 10; // High imm10H value.
280 Binary |= isNeg << 10; // Sign bit.
Jim Grosbach662a8162010-12-06 23:57:07 +0000281 return Binary;
282 }
Bill Wendlingb8958b02010-12-08 01:57:09 +0000283 case ARM::fixup_arm_thumb_cp:
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000284 // Offset by 4, and don't encode the low two bits. Two bytes of that
285 // 'off by 4' is implicitly handled by the half-word ordering of the
286 // Thumb encoding, so we only need to adjust by 2 here.
287 return ((Value - 2) >> 2) & 0xff;
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000288 case ARM::fixup_arm_thumb_cb: {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000289 // Offset by 4 and don't encode the lower bit, which is always 0.
290 uint32_t Binary = (Value - 4) >> 1;
Owen Anderson86abd482010-12-14 19:42:53 +0000291 return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000292 }
Jim Grosbache2467172010-12-10 18:21:33 +0000293 case ARM::fixup_arm_thumb_br:
294 // Offset by 4 and don't encode the lower bit, which is always 0.
295 return ((Value - 4) >> 1) & 0x7ff;
Jim Grosbach01086452010-12-10 17:13:40 +0000296 case ARM::fixup_arm_thumb_bcc:
297 // Offset by 4 and don't encode the lower bit, which is always 0.
298 return ((Value - 4) >> 1) & 0xff;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000299 case ARM::fixup_arm_pcrel_10:
Owen Andersone2e0f582010-12-10 22:46:47 +0000300 Value = Value - 4; // ARM fixups offset by an additional word and don't
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000301 // need to adjust for the half-word ordering.
302 // Fall through.
303 case ARM::fixup_t2_pcrel_10: {
304 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Andersone2e0f582010-12-10 22:46:47 +0000305 Value = Value - 4;
Jason W Kim0c628c22010-12-01 22:46:50 +0000306 bool isAdd = true;
307 if ((int64_t)Value < 0) {
308 Value = -Value;
309 isAdd = false;
310 }
311 // These values don't encode the low two bits since they're always zero.
312 Value >>= 2;
313 assert ((Value < 256) && "Out of range pc-relative fixup value!");
314 Value |= isAdd << 23;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000315
Owen Andersoncc78f5c2010-12-08 19:31:11 +0000316 // Same addressing mode as fixup_arm_pcrel_10,
317 // but with 16-bit halfwords swapped.
Owen Andersond8e351b2010-12-08 00:18:36 +0000318 if (Kind == ARM::fixup_t2_pcrel_10) {
Jim Grosbach56a25352010-12-13 19:25:46 +0000319 uint32_t swapped = (Value & 0xFFFF0000) >> 16;
Owen Anderson255eafb2010-12-08 00:21:33 +0000320 swapped |= (Value & 0x0000FFFF) << 16;
Owen Andersond8e351b2010-12-08 00:18:36 +0000321 return swapped;
322 }
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000323
Jason W Kim0c628c22010-12-01 22:46:50 +0000324 return Value;
325 }
326 }
327}
328
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000329namespace {
Bill Wendling52e635e2010-12-07 23:05:20 +0000330
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000331// FIXME: This should be in a separate file.
332// ELF is an ELF of course...
333class ELFARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000334 MCELFObjectFormat Format;
335
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000336public:
337 Triple::OSType OSType;
338 ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
339 : ARMAsmBackend(T), OSType(_OSType) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000340 HasScatteredSymbols = true;
341 }
342
Rafael Espindolaf230df92010-10-16 18:23:53 +0000343 virtual const MCObjectFormat &getObjectFormat() const {
344 return Format;
345 }
346
Rafael Espindola179821a2010-12-06 19:08:48 +0000347 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000348 uint64_t Value) const;
349
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000350 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000351 return createELFObjectWriter(OS, /*Is64Bit=*/false,
352 OSType, ELF::EM_ARM,
353 /*IsLittleEndian=*/true,
354 /*HasRelocationAddend=*/false);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000355 }
356};
357
Bill Wendling52e635e2010-12-07 23:05:20 +0000358// FIXME: Raise this to share code between Darwin and ELF.
Rafael Espindola179821a2010-12-06 19:08:48 +0000359void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
360 unsigned DataSize, uint64_t Value) const {
Bill Wendling52e635e2010-12-07 23:05:20 +0000361 unsigned NumBytes = 4; // FIXME: 2 for Thumb
Bill Wendling52e635e2010-12-07 23:05:20 +0000362 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000363 if (!Value) return; // Doesn't change encoding.
Bill Wendling52e635e2010-12-07 23:05:20 +0000364
365 unsigned Offset = Fixup.getOffset();
366 assert(Offset % NumBytes == 0 && "Offset mod NumBytes is nonzero!");
367
368 // For each byte of the fragment that the fixup touches, mask in the bits from
369 // the fixup value. The Value has been "split up" into the appropriate
370 // bitfields above.
371 for (unsigned i = 0; i != NumBytes; ++i)
372 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000373}
374
375// FIXME: This should be in a separate file.
376class DarwinARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000377 MCMachOObjectFormat Format;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000378public:
Chris Lattnerb75c6512010-11-17 05:41:32 +0000379 DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000380 HasScatteredSymbols = true;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000381 }
382
Rafael Espindolaf230df92010-10-16 18:23:53 +0000383 virtual const MCObjectFormat &getObjectFormat() const {
384 return Format;
385 }
386
Rafael Espindola179821a2010-12-06 19:08:48 +0000387 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000388 uint64_t Value) const;
389
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000390 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jim Grosbachc9d14392010-11-05 18:48:58 +0000391 // FIXME: Subtarget info should be derived. Force v7 for now.
Daniel Dunbar5d05d972010-12-16 17:21:02 +0000392 return createMachObjectWriter(new ARMMachObjectWriter(
393 /*Is64Bit=*/false,
394 object::mach::CTM_ARM,
395 object::mach::CSARM_V7),
396 OS,
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000397 /*IsLittleEndian=*/true);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000398 }
399
400 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
401 return false;
402 }
403};
404
Bill Wendlingd832fa02010-12-07 23:11:00 +0000405/// getFixupKindNumBytes - The number of bytes the fixup may change.
Jim Grosbachc466b932010-11-11 18:04:49 +0000406static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000407 switch (Kind) {
Jim Grosbach662a8162010-12-06 23:57:07 +0000408 default:
409 llvm_unreachable("Unknown fixup kind!");
Bill Wendlingb8958b02010-12-08 01:57:09 +0000410
Jim Grosbach01086452010-12-10 17:13:40 +0000411 case ARM::fixup_arm_thumb_bcc:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000412 case ARM::fixup_arm_thumb_cp:
Jim Grosbachd40963c2010-12-14 22:28:03 +0000413 case ARM::fixup_thumb_adr_pcrel_10:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000414 return 1;
415
Jim Grosbache2467172010-12-10 18:21:33 +0000416 case ARM::fixup_arm_thumb_br:
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000417 case ARM::fixup_arm_thumb_cb:
Bill Wendlingdff2f712010-12-08 23:01:43 +0000418 return 2;
419
Jim Grosbach662a8162010-12-06 23:57:07 +0000420 case ARM::fixup_arm_ldst_pcrel_12:
421 case ARM::fixup_arm_pcrel_10:
422 case ARM::fixup_arm_adr_pcrel_12:
423 case ARM::fixup_arm_branch:
424 return 3;
Bill Wendlingb8958b02010-12-08 01:57:09 +0000425
426 case FK_Data_4:
Owen Andersond7b3f582010-12-09 01:51:07 +0000427 case ARM::fixup_t2_ldst_pcrel_12:
Owen Andersonc2666002010-12-13 19:31:11 +0000428 case ARM::fixup_t2_condbranch:
429 case ARM::fixup_t2_uncondbranch:
Owen Andersond8e351b2010-12-08 00:18:36 +0000430 case ARM::fixup_t2_pcrel_10:
Owen Andersona838a252010-12-14 00:36:49 +0000431 case ARM::fixup_t2_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +0000432 case ARM::fixup_arm_thumb_bl:
Bill Wendling09aa3f02010-12-09 00:39:08 +0000433 case ARM::fixup_arm_thumb_blx:
Jim Grosbach662a8162010-12-06 23:57:07 +0000434 return 4;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000435 }
436}
437
Rafael Espindola179821a2010-12-06 19:08:48 +0000438void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
439 unsigned DataSize, uint64_t Value) const {
Jim Grosbachc466b932010-11-11 18:04:49 +0000440 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Jim Grosbach679cbd32010-11-09 01:37:15 +0000441 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000442 if (!Value) return; // Doesn't change encoding.
Jim Grosbach679cbd32010-11-09 01:37:15 +0000443
Bill Wendlingd832fa02010-12-07 23:11:00 +0000444 unsigned Offset = Fixup.getOffset();
445 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
446
Jim Grosbach679cbd32010-11-09 01:37:15 +0000447 // For each byte of the fragment that the fixup touches, mask in the
448 // bits from the fixup value.
449 for (unsigned i = 0; i != NumBytes; ++i)
Bill Wendlingd832fa02010-12-07 23:11:00 +0000450 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000451}
Bill Wendling52e635e2010-12-07 23:05:20 +0000452
Jim Grosbachf73fd722010-09-30 03:21:00 +0000453} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000454
455TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
456 const std::string &TT) {
457 switch (Triple(TT).getOS()) {
458 case Triple::Darwin:
459 return new DarwinARMAsmBackend(T);
460 case Triple::MinGW32:
461 case Triple::Cygwin:
462 case Triple::Win32:
463 assert(0 && "Windows not supported on ARM");
464 default:
465 return new ELFARMAsmBackend(T, Triple(TT).getOS());
466 }
467}