blob: f93933d626805e2645a45d126c129955c3736ce6 [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"
Rafael Espindolaf230df92010-10-16 18:23:53 +000017#include "llvm/MC/MCObjectFormat.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000018#include "llvm/MC/MCObjectWriter.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000019#include "llvm/MC/MCSectionELF.h"
20#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbar36d76a82010-11-27 04:38:36 +000021#include "llvm/Object/MachOFormat.h"
Wesley Peckeecb8582010-10-22 15:52:49 +000022#include "llvm/Support/ELF.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000023#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/raw_ostream.h"
Jim Grosbachdff84b02010-12-02 00:28:45 +000025#include "llvm/Target/TargetAsmBackend.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000026#include "llvm/Target/TargetRegistry.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000027using namespace llvm;
28
29namespace {
30class ARMAsmBackend : public TargetAsmBackend {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000031 bool isThumbMode; // Currently emitting Thumb code.
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000032public:
Jim Grosbach022ab372010-12-08 15:36:45 +000033 ARMAsmBackend(const Target &T) : TargetAsmBackend(), isThumbMode(false) {}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000034
Daniel Dunbar2761fc42010-12-16 03:20:06 +000035 unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
36
37 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
38 const static MCFixupKindInfo Infos[ARM::NumTargetFixupKinds] = {
39// This table *must* be in the order that the fixup_* kinds are defined in
40// ARMFixupKinds.h.
41//
42// Name Offset (bits) Size (bits) Flags
43{ "fixup_arm_ldst_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
44{ "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
45 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
46{ "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
47{ "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
48 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
49{ "fixup_thumb_adr_pcrel_10",0, 8, MCFixupKindInfo::FKF_IsPCRel |
50 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
51{ "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
52{ "fixup_t2_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
53 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
54{ "fixup_arm_branch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
55{ "fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
56{ "fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
57{ "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
58{ "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
59{ "fixup_arm_thumb_blx", 7, 21, MCFixupKindInfo::FKF_IsPCRel },
60{ "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
61{ "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
62{ "fixup_arm_thumb_bcc", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
63{ "fixup_arm_movt_hi16", 0, 16, 0 },
64{ "fixup_arm_movw_lo16", 0, 16, 0 },
65 };
66
67 if (Kind < FirstTargetFixupKind)
68 return TargetAsmBackend::getFixupKindInfo(Kind);
69
70 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
71 "Invalid kind!");
72 return Infos[Kind - FirstTargetFixupKind];
73 }
74
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000075 bool MayNeedRelaxation(const MCInst &Inst) const;
76
77 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
78
79 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
Jim Grosbach3787a402010-09-30 17:45:51 +000080
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000081 void HandleAssemblerFlag(MCAssemblerFlag Flag) {
82 switch (Flag) {
83 default: break;
84 case MCAF_Code16:
85 setIsThumb(true);
86 break;
87 case MCAF_Code32:
88 setIsThumb(false);
89 break;
90 }
Jim Grosbach3787a402010-09-30 17:45:51 +000091 }
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000092
93 unsigned getPointerSize() const { return 4; }
94 bool isThumb() const { return isThumbMode; }
95 void setIsThumb(bool it) { isThumbMode = it; }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000096};
Chris Lattnerb75c6512010-11-17 05:41:32 +000097} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000098
99bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
100 // FIXME: Thumb targets, different move constant targets..
101 return false;
102}
103
104void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
105 assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
106 return;
107}
108
109bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000110 if (isThumb()) {
111 assert (((Count & 1) == 0) && "Unaligned Nop data fragment!");
112 // FIXME: 0xbf00 is the ARMv7 value. For v6 and before, we'll need to
113 // use 0x46c0 (which is a 'mov r8, r8' insn).
114 Count /= 2;
115 for (uint64_t i = 0; i != Count; ++i)
116 OW->Write16(0xbf00);
117 return true;
118 }
119 // ARM mode
120 Count /= 4;
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000121 for (uint64_t i = 0; i != Count; ++i)
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000122 OW->Write32(0xe1a00000);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000123 return true;
Jim Grosbach87dc3aa2010-09-30 03:20:34 +0000124}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000125
Jason W Kim0c628c22010-12-01 22:46:50 +0000126static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
127 switch (Kind) {
128 default:
129 llvm_unreachable("Unknown fixup kind!");
130 case FK_Data_4:
Jason W Kim0c628c22010-12-01 22:46:50 +0000131 return Value;
Jason W Kim2ccf1482010-12-03 19:40:23 +0000132 case ARM::fixup_arm_movt_hi16:
133 case ARM::fixup_arm_movw_lo16: {
134 unsigned Hi4 = (Value & 0xF000) >> 12;
135 unsigned Lo12 = Value & 0x0FFF;
136 // inst{19-16} = Hi4;
137 // inst{11-0} = Lo12;
138 Value = (Hi4 << 16) | (Lo12);
139 return Value;
140 }
Owen Andersond7b3f582010-12-09 01:51:07 +0000141 case ARM::fixup_arm_ldst_pcrel_12:
Jason W Kim0c628c22010-12-01 22:46:50 +0000142 // ARM PC-relative values are offset by 8.
Owen Anderson05018c22010-12-09 20:27:52 +0000143 Value -= 4;
Owen Andersonfe7fac72010-12-09 21:34:47 +0000144 // FALLTHROUGH
Owen Andersond7b3f582010-12-09 01:51:07 +0000145 case ARM::fixup_t2_ldst_pcrel_12: {
146 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Anderson05018c22010-12-09 20:27:52 +0000147 Value -= 4;
Owen Andersond7b3f582010-12-09 01:51:07 +0000148 bool isAdd = true;
Jason W Kim0c628c22010-12-01 22:46:50 +0000149 if ((int64_t)Value < 0) {
150 Value = -Value;
151 isAdd = false;
152 }
153 assert ((Value < 4096) && "Out of range pc-relative fixup value!");
154 Value |= isAdd << 23;
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000155
Owen Andersond7b3f582010-12-09 01:51:07 +0000156 // Same addressing mode as fixup_arm_pcrel_10,
157 // but with 16-bit halfwords swapped.
158 if (Kind == ARM::fixup_t2_ldst_pcrel_12) {
159 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
160 swapped |= (Value & 0x0000FFFF) << 16;
161 return swapped;
162 }
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000163
Jason W Kim0c628c22010-12-01 22:46:50 +0000164 return Value;
165 }
Jim Grosbachd40963c2010-12-14 22:28:03 +0000166 case ARM::fixup_thumb_adr_pcrel_10:
167 return ((Value - 4) >> 2) & 0xff;
Jim Grosbachdff84b02010-12-02 00:28:45 +0000168 case ARM::fixup_arm_adr_pcrel_12: {
169 // ARM PC-relative values are offset by 8.
170 Value -= 8;
171 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
172 if ((int64_t)Value < 0) {
173 Value = -Value;
174 opc = 2; // 0b0010
175 }
176 assert(ARM_AM::getSOImmVal(Value) != -1 &&
177 "Out of range pc-relative fixup value!");
178 // Encode the immediate and shift the opcode into place.
179 return ARM_AM::getSOImmVal(Value) | (opc << 21);
180 }
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000181
Owen Andersona838a252010-12-14 00:36:49 +0000182 case ARM::fixup_t2_adr_pcrel_12: {
183 Value -= 4;
184 unsigned opc = 0;
185 if ((int64_t)Value < 0) {
186 Value = -Value;
187 opc = 5;
188 }
189
190 uint32_t out = (opc << 21);
191 out |= (Value & 0x800) << 14;
192 out |= (Value & 0x700) << 4;
193 out |= (Value & 0x0FF);
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000194
Owen Andersona838a252010-12-14 00:36:49 +0000195 uint64_t swapped = (out & 0xFFFF0000) >> 16;
196 swapped |= (out & 0x0000FFFF) << 16;
197 return swapped;
198 }
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000199
Jason W Kim0c628c22010-12-01 22:46:50 +0000200 case ARM::fixup_arm_branch:
201 // These values don't encode the low two bits since they're always zero.
202 // Offset by 8 just as above.
Jim Grosbach662a8162010-12-06 23:57:07 +0000203 return 0xffffff & ((Value - 8) >> 2);
Owen Andersonc2666002010-12-13 19:31:11 +0000204 case ARM::fixup_t2_uncondbranch: {
Owen Anderson63ee2202010-12-10 23:02:28 +0000205 Value = Value - 4;
Owen Andersonfb20d892010-12-09 00:27:41 +0000206 Value >>= 1; // Low bit is not encoded.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000207
Jim Grosbach56a25352010-12-13 19:25:46 +0000208 uint32_t out = 0;
Owen Andersonc2666002010-12-13 19:31:11 +0000209 bool I = Value & 0x800000;
210 bool J1 = Value & 0x400000;
211 bool J2 = Value & 0x200000;
212 J1 ^= I;
213 J2 ^= I;
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000214
Owen Andersonc2666002010-12-13 19:31:11 +0000215 out |= I << 26; // S bit
216 out |= !J1 << 13; // J1 bit
217 out |= !J2 << 11; // J2 bit
218 out |= (Value & 0x1FF800) << 5; // imm6 field
219 out |= (Value & 0x0007FF); // imm11 field
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000220
Owen Andersonc2666002010-12-13 19:31:11 +0000221 uint64_t swapped = (out & 0xFFFF0000) >> 16;
222 swapped |= (out & 0x0000FFFF) << 16;
223 return swapped;
224 }
225 case ARM::fixup_t2_condbranch: {
226 Value = Value - 4;
227 Value >>= 1; // Low bit is not encoded.
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000228
Owen Andersonc2666002010-12-13 19:31:11 +0000229 uint64_t out = 0;
Owen Anderson8f079432010-12-09 01:02:09 +0000230 out |= (Value & 0x80000) << 7; // S bit
231 out |= (Value & 0x40000) >> 7; // J2 bit
232 out |= (Value & 0x20000) >> 4; // J1 bit
233 out |= (Value & 0x1F800) << 5; // imm6 field
234 out |= (Value & 0x007FF); // imm11 field
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000235
Jim Grosbach56a25352010-12-13 19:25:46 +0000236 uint32_t swapped = (out & 0xFFFF0000) >> 16;
Owen Andersonfb20d892010-12-09 00:27:41 +0000237 swapped |= (out & 0x0000FFFF) << 16;
238 return swapped;
239 }
Jim Grosbach662a8162010-12-06 23:57:07 +0000240 case ARM::fixup_arm_thumb_bl: {
241 // The value doesn't encode the low bit (always zero) and is offset by
242 // four. The value is encoded into disjoint bit positions in the destination
243 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000244 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000245 // BL: xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000246 //
Jim Grosbach662a8162010-12-06 23:57:07 +0000247 // Note that the halfwords are stored high first, low second; so we need
248 // to transpose the fixup value here to map properly.
Bill Wendling09aa3f02010-12-09 00:39:08 +0000249 unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000250 uint32_t Binary = 0;
251 Value = 0x3fffff & ((Value - 4) >> 1);
252 Binary = (Value & 0x7ff) << 16; // Low imm11 value.
253 Binary |= (Value & 0x1ffc00) >> 11; // High imm10 value.
254 Binary |= isNeg << 10; // Sign bit.
Bill Wendling09aa3f02010-12-09 00:39:08 +0000255 return Binary;
256 }
257 case ARM::fixup_arm_thumb_blx: {
258 // The value doesn't encode the low two bits (always zero) and is offset by
259 // four (see fixup_arm_thumb_cp). The value is encoded into disjoint bit
260 // positions in the destination opcode. x = unchanged, I = immediate value
261 // bit, S = sign extension bit, 0 = zero.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000262 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000263 // BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000264 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000265 // Note that the halfwords are stored high first, low second; so we need
266 // to transpose the fixup value here to map properly.
267 unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000268 uint32_t Binary = 0;
269 Value = 0xfffff & ((Value - 2) >> 2);
270 Binary = (Value & 0x3ff) << 17; // Low imm10L value.
271 Binary |= (Value & 0xffc00) >> 10; // High imm10H value.
272 Binary |= isNeg << 10; // Sign bit.
Jim Grosbach662a8162010-12-06 23:57:07 +0000273 return Binary;
274 }
Bill Wendlingb8958b02010-12-08 01:57:09 +0000275 case ARM::fixup_arm_thumb_cp:
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000276 // Offset by 4, and don't encode the low two bits. Two bytes of that
277 // 'off by 4' is implicitly handled by the half-word ordering of the
278 // Thumb encoding, so we only need to adjust by 2 here.
279 return ((Value - 2) >> 2) & 0xff;
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000280 case ARM::fixup_arm_thumb_cb: {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000281 // Offset by 4 and don't encode the lower bit, which is always 0.
282 uint32_t Binary = (Value - 4) >> 1;
Owen Anderson86abd482010-12-14 19:42:53 +0000283 return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000284 }
Jim Grosbache2467172010-12-10 18:21:33 +0000285 case ARM::fixup_arm_thumb_br:
286 // Offset by 4 and don't encode the lower bit, which is always 0.
287 return ((Value - 4) >> 1) & 0x7ff;
Jim Grosbach01086452010-12-10 17:13:40 +0000288 case ARM::fixup_arm_thumb_bcc:
289 // Offset by 4 and don't encode the lower bit, which is always 0.
290 return ((Value - 4) >> 1) & 0xff;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000291 case ARM::fixup_arm_pcrel_10:
Owen Andersone2e0f582010-12-10 22:46:47 +0000292 Value = Value - 4; // ARM fixups offset by an additional word and don't
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000293 // need to adjust for the half-word ordering.
294 // Fall through.
295 case ARM::fixup_t2_pcrel_10: {
296 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Andersone2e0f582010-12-10 22:46:47 +0000297 Value = Value - 4;
Jason W Kim0c628c22010-12-01 22:46:50 +0000298 bool isAdd = true;
299 if ((int64_t)Value < 0) {
300 Value = -Value;
301 isAdd = false;
302 }
303 // These values don't encode the low two bits since they're always zero.
304 Value >>= 2;
305 assert ((Value < 256) && "Out of range pc-relative fixup value!");
306 Value |= isAdd << 23;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000307
Owen Andersoncc78f5c2010-12-08 19:31:11 +0000308 // Same addressing mode as fixup_arm_pcrel_10,
309 // but with 16-bit halfwords swapped.
Owen Andersond8e351b2010-12-08 00:18:36 +0000310 if (Kind == ARM::fixup_t2_pcrel_10) {
Jim Grosbach56a25352010-12-13 19:25:46 +0000311 uint32_t swapped = (Value & 0xFFFF0000) >> 16;
Owen Anderson255eafb2010-12-08 00:21:33 +0000312 swapped |= (Value & 0x0000FFFF) << 16;
Owen Andersond8e351b2010-12-08 00:18:36 +0000313 return swapped;
314 }
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000315
Jason W Kim0c628c22010-12-01 22:46:50 +0000316 return Value;
317 }
318 }
319}
320
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000321namespace {
Bill Wendling52e635e2010-12-07 23:05:20 +0000322
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000323// FIXME: This should be in a separate file.
324// ELF is an ELF of course...
325class ELFARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000326 MCELFObjectFormat Format;
327
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000328public:
329 Triple::OSType OSType;
330 ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
331 : ARMAsmBackend(T), OSType(_OSType) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000332 HasScatteredSymbols = true;
333 }
334
Rafael Espindolaf230df92010-10-16 18:23:53 +0000335 virtual const MCObjectFormat &getObjectFormat() const {
336 return Format;
337 }
338
Rafael Espindola179821a2010-12-06 19:08:48 +0000339 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000340 uint64_t Value) const;
341
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000342 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000343 return createELFObjectWriter(OS, /*Is64Bit=*/false,
344 OSType, ELF::EM_ARM,
345 /*IsLittleEndian=*/true,
346 /*HasRelocationAddend=*/false);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000347 }
348};
349
Bill Wendling52e635e2010-12-07 23:05:20 +0000350// FIXME: Raise this to share code between Darwin and ELF.
Rafael Espindola179821a2010-12-06 19:08:48 +0000351void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
352 unsigned DataSize, uint64_t Value) const {
Bill Wendling52e635e2010-12-07 23:05:20 +0000353 unsigned NumBytes = 4; // FIXME: 2 for Thumb
Bill Wendling52e635e2010-12-07 23:05:20 +0000354 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000355 if (!Value) return; // Doesn't change encoding.
Bill Wendling52e635e2010-12-07 23:05:20 +0000356
357 unsigned Offset = Fixup.getOffset();
358 assert(Offset % NumBytes == 0 && "Offset mod NumBytes is nonzero!");
359
360 // For each byte of the fragment that the fixup touches, mask in the bits from
361 // the fixup value. The Value has been "split up" into the appropriate
362 // bitfields above.
363 for (unsigned i = 0; i != NumBytes; ++i)
364 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000365}
366
367// FIXME: This should be in a separate file.
368class DarwinARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000369 MCMachOObjectFormat Format;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000370public:
Chris Lattnerb75c6512010-11-17 05:41:32 +0000371 DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000372 HasScatteredSymbols = true;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000373 }
374
Rafael Espindolaf230df92010-10-16 18:23:53 +0000375 virtual const MCObjectFormat &getObjectFormat() const {
376 return Format;
377 }
378
Rafael Espindola179821a2010-12-06 19:08:48 +0000379 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000380 uint64_t Value) const;
381
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000382 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jim Grosbachc9d14392010-11-05 18:48:58 +0000383 // FIXME: Subtarget info should be derived. Force v7 for now.
Daniel Dunbar36d76a82010-11-27 04:38:36 +0000384 return createMachObjectWriter(OS, /*Is64Bit=*/false,
385 object::mach::CTM_ARM,
386 object::mach::CSARM_V7,
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000387 /*IsLittleEndian=*/true);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000388 }
389
390 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
391 return false;
392 }
393};
394
Bill Wendlingd832fa02010-12-07 23:11:00 +0000395/// getFixupKindNumBytes - The number of bytes the fixup may change.
Jim Grosbachc466b932010-11-11 18:04:49 +0000396static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000397 switch (Kind) {
Jim Grosbach662a8162010-12-06 23:57:07 +0000398 default:
399 llvm_unreachable("Unknown fixup kind!");
Bill Wendlingb8958b02010-12-08 01:57:09 +0000400
Jim Grosbach01086452010-12-10 17:13:40 +0000401 case ARM::fixup_arm_thumb_bcc:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000402 case ARM::fixup_arm_thumb_cp:
Jim Grosbachd40963c2010-12-14 22:28:03 +0000403 case ARM::fixup_thumb_adr_pcrel_10:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000404 return 1;
405
Jim Grosbache2467172010-12-10 18:21:33 +0000406 case ARM::fixup_arm_thumb_br:
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000407 case ARM::fixup_arm_thumb_cb:
Bill Wendlingdff2f712010-12-08 23:01:43 +0000408 return 2;
409
Jim Grosbach662a8162010-12-06 23:57:07 +0000410 case ARM::fixup_arm_ldst_pcrel_12:
411 case ARM::fixup_arm_pcrel_10:
412 case ARM::fixup_arm_adr_pcrel_12:
413 case ARM::fixup_arm_branch:
414 return 3;
Bill Wendlingb8958b02010-12-08 01:57:09 +0000415
416 case FK_Data_4:
Owen Andersond7b3f582010-12-09 01:51:07 +0000417 case ARM::fixup_t2_ldst_pcrel_12:
Owen Andersonc2666002010-12-13 19:31:11 +0000418 case ARM::fixup_t2_condbranch:
419 case ARM::fixup_t2_uncondbranch:
Owen Andersond8e351b2010-12-08 00:18:36 +0000420 case ARM::fixup_t2_pcrel_10:
Owen Andersona838a252010-12-14 00:36:49 +0000421 case ARM::fixup_t2_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +0000422 case ARM::fixup_arm_thumb_bl:
Bill Wendling09aa3f02010-12-09 00:39:08 +0000423 case ARM::fixup_arm_thumb_blx:
Jim Grosbach662a8162010-12-06 23:57:07 +0000424 return 4;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000425 }
426}
427
Rafael Espindola179821a2010-12-06 19:08:48 +0000428void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
429 unsigned DataSize, uint64_t Value) const {
Jim Grosbachc466b932010-11-11 18:04:49 +0000430 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Jim Grosbach679cbd32010-11-09 01:37:15 +0000431 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000432 if (!Value) return; // Doesn't change encoding.
Jim Grosbach679cbd32010-11-09 01:37:15 +0000433
Bill Wendlingd832fa02010-12-07 23:11:00 +0000434 unsigned Offset = Fixup.getOffset();
435 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
436
Jim Grosbach679cbd32010-11-09 01:37:15 +0000437 // For each byte of the fragment that the fixup touches, mask in the
438 // bits from the fixup value.
439 for (unsigned i = 0; i != NumBytes; ++i)
Bill Wendlingd832fa02010-12-07 23:11:00 +0000440 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000441}
Bill Wendling52e635e2010-12-07 23:05:20 +0000442
Jim Grosbachf73fd722010-09-30 03:21:00 +0000443} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000444
445TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
446 const std::string &TT) {
447 switch (Triple(TT).getOS()) {
448 case Triple::Darwin:
449 return new DarwinARMAsmBackend(T);
450 case Triple::MinGW32:
451 case Triple::Cygwin:
452 case Triple::Win32:
453 assert(0 && "Windows not supported on ARM");
454 default:
455 return new ELFARMAsmBackend(T, Triple(TT).getOS());
456 }
457}