blob: 789bae09bee1e9f1eb611d14e4f381966fcec36c [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
35 bool MayNeedRelaxation(const MCInst &Inst) const;
36
37 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
38
39 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
Jim Grosbach3787a402010-09-30 17:45:51 +000040
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000041 void HandleAssemblerFlag(MCAssemblerFlag Flag) {
42 switch (Flag) {
43 default: break;
44 case MCAF_Code16:
45 setIsThumb(true);
46 break;
47 case MCAF_Code32:
48 setIsThumb(false);
49 break;
50 }
Jim Grosbach3787a402010-09-30 17:45:51 +000051 }
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000052
53 unsigned getPointerSize() const { return 4; }
54 bool isThumb() const { return isThumbMode; }
55 void setIsThumb(bool it) { isThumbMode = it; }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000056};
Chris Lattnerb75c6512010-11-17 05:41:32 +000057} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000058
59bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
60 // FIXME: Thumb targets, different move constant targets..
61 return false;
62}
63
64void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
65 assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
66 return;
67}
68
69bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000070 if (isThumb()) {
71 assert (((Count & 1) == 0) && "Unaligned Nop data fragment!");
72 // FIXME: 0xbf00 is the ARMv7 value. For v6 and before, we'll need to
73 // use 0x46c0 (which is a 'mov r8, r8' insn).
74 Count /= 2;
75 for (uint64_t i = 0; i != Count; ++i)
76 OW->Write16(0xbf00);
77 return true;
78 }
79 // ARM mode
80 Count /= 4;
Jim Grosbache50e6bc2010-11-11 23:41:09 +000081 for (uint64_t i = 0; i != Count; ++i)
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000082 OW->Write32(0xe1a00000);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000083 return true;
Jim Grosbach87dc3aa2010-09-30 03:20:34 +000084}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000085
Jason W Kim0c628c22010-12-01 22:46:50 +000086static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
87 switch (Kind) {
88 default:
89 llvm_unreachable("Unknown fixup kind!");
90 case FK_Data_4:
Jason W Kim0c628c22010-12-01 22:46:50 +000091 return Value;
Jason W Kim2ccf1482010-12-03 19:40:23 +000092 case ARM::fixup_arm_movt_hi16:
93 case ARM::fixup_arm_movw_lo16: {
94 unsigned Hi4 = (Value & 0xF000) >> 12;
95 unsigned Lo12 = Value & 0x0FFF;
96 // inst{19-16} = Hi4;
97 // inst{11-0} = Lo12;
98 Value = (Hi4 << 16) | (Lo12);
99 return Value;
100 }
Owen Andersond7b3f582010-12-09 01:51:07 +0000101 case ARM::fixup_arm_ldst_pcrel_12:
Jason W Kim0c628c22010-12-01 22:46:50 +0000102 // ARM PC-relative values are offset by 8.
Owen Anderson05018c22010-12-09 20:27:52 +0000103 Value -= 4;
Owen Andersonfe7fac72010-12-09 21:34:47 +0000104 // FALLTHROUGH
Owen Andersond7b3f582010-12-09 01:51:07 +0000105 case ARM::fixup_t2_ldst_pcrel_12: {
106 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Anderson05018c22010-12-09 20:27:52 +0000107 Value -= 4;
Owen Andersond7b3f582010-12-09 01:51:07 +0000108 bool isAdd = true;
Jason W Kim0c628c22010-12-01 22:46:50 +0000109 if ((int64_t)Value < 0) {
110 Value = -Value;
111 isAdd = false;
112 }
113 assert ((Value < 4096) && "Out of range pc-relative fixup value!");
114 Value |= isAdd << 23;
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000115
Owen Andersond7b3f582010-12-09 01:51:07 +0000116 // Same addressing mode as fixup_arm_pcrel_10,
117 // but with 16-bit halfwords swapped.
118 if (Kind == ARM::fixup_t2_ldst_pcrel_12) {
119 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
120 swapped |= (Value & 0x0000FFFF) << 16;
121 return swapped;
122 }
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000123
Jason W Kim0c628c22010-12-01 22:46:50 +0000124 return Value;
125 }
Jim Grosbachdff84b02010-12-02 00:28:45 +0000126 case ARM::fixup_arm_adr_pcrel_12: {
127 // ARM PC-relative values are offset by 8.
128 Value -= 8;
129 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
130 if ((int64_t)Value < 0) {
131 Value = -Value;
132 opc = 2; // 0b0010
133 }
134 assert(ARM_AM::getSOImmVal(Value) != -1 &&
135 "Out of range pc-relative fixup value!");
136 // Encode the immediate and shift the opcode into place.
137 return ARM_AM::getSOImmVal(Value) | (opc << 21);
138 }
Owen Andersona838a252010-12-14 00:36:49 +0000139
140 case ARM::fixup_t2_adr_pcrel_12: {
141 Value -= 4;
142 unsigned opc = 0;
143 if ((int64_t)Value < 0) {
144 Value = -Value;
145 opc = 5;
146 }
147
148 uint32_t out = (opc << 21);
149 out |= (Value & 0x800) << 14;
150 out |= (Value & 0x700) << 4;
151 out |= (Value & 0x0FF);
152
153 uint64_t swapped = (out & 0xFFFF0000) >> 16;
154 swapped |= (out & 0x0000FFFF) << 16;
155 return swapped;
156 }
157
Jason W Kim0c628c22010-12-01 22:46:50 +0000158 case ARM::fixup_arm_branch:
159 // These values don't encode the low two bits since they're always zero.
160 // Offset by 8 just as above.
Jim Grosbach662a8162010-12-06 23:57:07 +0000161 return 0xffffff & ((Value - 8) >> 2);
Owen Andersonc2666002010-12-13 19:31:11 +0000162 case ARM::fixup_t2_uncondbranch: {
Owen Anderson63ee2202010-12-10 23:02:28 +0000163 Value = Value - 4;
Owen Andersonfb20d892010-12-09 00:27:41 +0000164 Value >>= 1; // Low bit is not encoded.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000165
Jim Grosbach56a25352010-12-13 19:25:46 +0000166 uint32_t out = 0;
Owen Andersonc2666002010-12-13 19:31:11 +0000167 bool I = Value & 0x800000;
168 bool J1 = Value & 0x400000;
169 bool J2 = Value & 0x200000;
170 J1 ^= I;
171 J2 ^= I;
172
173 out |= I << 26; // S bit
174 out |= !J1 << 13; // J1 bit
175 out |= !J2 << 11; // J2 bit
176 out |= (Value & 0x1FF800) << 5; // imm6 field
177 out |= (Value & 0x0007FF); // imm11 field
178
179 uint64_t swapped = (out & 0xFFFF0000) >> 16;
180 swapped |= (out & 0x0000FFFF) << 16;
181 return swapped;
182 }
183 case ARM::fixup_t2_condbranch: {
184 Value = Value - 4;
185 Value >>= 1; // Low bit is not encoded.
186
187 uint64_t out = 0;
Owen Anderson8f079432010-12-09 01:02:09 +0000188 out |= (Value & 0x80000) << 7; // S bit
189 out |= (Value & 0x40000) >> 7; // J2 bit
190 out |= (Value & 0x20000) >> 4; // J1 bit
191 out |= (Value & 0x1F800) << 5; // imm6 field
192 out |= (Value & 0x007FF); // imm11 field
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000193
Jim Grosbach56a25352010-12-13 19:25:46 +0000194 uint32_t swapped = (out & 0xFFFF0000) >> 16;
Owen Andersonfb20d892010-12-09 00:27:41 +0000195 swapped |= (out & 0x0000FFFF) << 16;
196 return swapped;
197 }
Jim Grosbach662a8162010-12-06 23:57:07 +0000198 case ARM::fixup_arm_thumb_bl: {
199 // The value doesn't encode the low bit (always zero) and is offset by
200 // four. The value is encoded into disjoint bit positions in the destination
201 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000202 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000203 // BL: xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000204 //
Jim Grosbach662a8162010-12-06 23:57:07 +0000205 // Note that the halfwords are stored high first, low second; so we need
206 // to transpose the fixup value here to map properly.
Bill Wendling09aa3f02010-12-09 00:39:08 +0000207 unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000208 uint32_t Binary = 0;
209 Value = 0x3fffff & ((Value - 4) >> 1);
210 Binary = (Value & 0x7ff) << 16; // Low imm11 value.
211 Binary |= (Value & 0x1ffc00) >> 11; // High imm10 value.
212 Binary |= isNeg << 10; // Sign bit.
Bill Wendling09aa3f02010-12-09 00:39:08 +0000213 return Binary;
214 }
215 case ARM::fixup_arm_thumb_blx: {
216 // The value doesn't encode the low two bits (always zero) and is offset by
217 // four (see fixup_arm_thumb_cp). The value is encoded into disjoint bit
218 // positions in the destination opcode. x = unchanged, I = immediate value
219 // bit, S = sign extension bit, 0 = zero.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000220 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000221 // BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000222 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000223 // Note that the halfwords are stored high first, low second; so we need
224 // to transpose the fixup value here to map properly.
225 unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000226 uint32_t Binary = 0;
227 Value = 0xfffff & ((Value - 2) >> 2);
228 Binary = (Value & 0x3ff) << 17; // Low imm10L value.
229 Binary |= (Value & 0xffc00) >> 10; // High imm10H value.
230 Binary |= isNeg << 10; // Sign bit.
Jim Grosbach662a8162010-12-06 23:57:07 +0000231 return Binary;
232 }
Bill Wendlingb8958b02010-12-08 01:57:09 +0000233 case ARM::fixup_arm_thumb_cp:
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000234 // Offset by 4, and don't encode the low two bits. Two bytes of that
235 // 'off by 4' is implicitly handled by the half-word ordering of the
236 // Thumb encoding, so we only need to adjust by 2 here.
237 return ((Value - 2) >> 2) & 0xff;
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000238 case ARM::fixup_arm_thumb_cb: {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000239 // Offset by 4 and don't encode the lower bit, which is always 0.
240 uint32_t Binary = (Value - 4) >> 1;
241 return ((Binary & 0x20) << 9) | ((Binary & 0x1f) << 3);
242 }
Jim Grosbache2467172010-12-10 18:21:33 +0000243 case ARM::fixup_arm_thumb_br:
244 // Offset by 4 and don't encode the lower bit, which is always 0.
245 return ((Value - 4) >> 1) & 0x7ff;
Jim Grosbach01086452010-12-10 17:13:40 +0000246 case ARM::fixup_arm_thumb_bcc:
247 // Offset by 4 and don't encode the lower bit, which is always 0.
248 return ((Value - 4) >> 1) & 0xff;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000249 case ARM::fixup_arm_pcrel_10:
Owen Andersone2e0f582010-12-10 22:46:47 +0000250 Value = Value - 4; // ARM fixups offset by an additional word and don't
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000251 // need to adjust for the half-word ordering.
252 // Fall through.
253 case ARM::fixup_t2_pcrel_10: {
254 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Andersone2e0f582010-12-10 22:46:47 +0000255 Value = Value - 4;
Jason W Kim0c628c22010-12-01 22:46:50 +0000256 bool isAdd = true;
257 if ((int64_t)Value < 0) {
258 Value = -Value;
259 isAdd = false;
260 }
261 // These values don't encode the low two bits since they're always zero.
262 Value >>= 2;
263 assert ((Value < 256) && "Out of range pc-relative fixup value!");
264 Value |= isAdd << 23;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000265
Owen Andersoncc78f5c2010-12-08 19:31:11 +0000266 // Same addressing mode as fixup_arm_pcrel_10,
267 // but with 16-bit halfwords swapped.
Owen Andersond8e351b2010-12-08 00:18:36 +0000268 if (Kind == ARM::fixup_t2_pcrel_10) {
Jim Grosbach56a25352010-12-13 19:25:46 +0000269 uint32_t swapped = (Value & 0xFFFF0000) >> 16;
Owen Anderson255eafb2010-12-08 00:21:33 +0000270 swapped |= (Value & 0x0000FFFF) << 16;
Owen Andersond8e351b2010-12-08 00:18:36 +0000271 return swapped;
272 }
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000273
Jason W Kim0c628c22010-12-01 22:46:50 +0000274 return Value;
275 }
276 }
277}
278
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000279namespace {
Bill Wendling52e635e2010-12-07 23:05:20 +0000280
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000281// FIXME: This should be in a separate file.
282// ELF is an ELF of course...
283class ELFARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000284 MCELFObjectFormat Format;
285
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000286public:
287 Triple::OSType OSType;
288 ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
289 : ARMAsmBackend(T), OSType(_OSType) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000290 HasScatteredSymbols = true;
291 }
292
Rafael Espindolaf230df92010-10-16 18:23:53 +0000293 virtual const MCObjectFormat &getObjectFormat() const {
294 return Format;
295 }
296
Rafael Espindola179821a2010-12-06 19:08:48 +0000297 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000298 uint64_t Value) const;
299
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000300 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000301 return createELFObjectWriter(OS, /*Is64Bit=*/false,
302 OSType, ELF::EM_ARM,
303 /*IsLittleEndian=*/true,
304 /*HasRelocationAddend=*/false);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000305 }
306};
307
Bill Wendling52e635e2010-12-07 23:05:20 +0000308// FIXME: Raise this to share code between Darwin and ELF.
Rafael Espindola179821a2010-12-06 19:08:48 +0000309void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
310 unsigned DataSize, uint64_t Value) const {
Bill Wendling52e635e2010-12-07 23:05:20 +0000311 unsigned NumBytes = 4; // FIXME: 2 for Thumb
Bill Wendling52e635e2010-12-07 23:05:20 +0000312 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000313 if (!Value) return; // Doesn't change encoding.
Bill Wendling52e635e2010-12-07 23:05:20 +0000314
315 unsigned Offset = Fixup.getOffset();
316 assert(Offset % NumBytes == 0 && "Offset mod NumBytes is nonzero!");
317
318 // For each byte of the fragment that the fixup touches, mask in the bits from
319 // the fixup value. The Value has been "split up" into the appropriate
320 // bitfields above.
321 for (unsigned i = 0; i != NumBytes; ++i)
322 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000323}
324
325// FIXME: This should be in a separate file.
326class DarwinARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000327 MCMachOObjectFormat Format;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000328public:
Chris Lattnerb75c6512010-11-17 05:41:32 +0000329 DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000330 HasScatteredSymbols = true;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000331 }
332
Rafael Espindolaf230df92010-10-16 18:23:53 +0000333 virtual const MCObjectFormat &getObjectFormat() const {
334 return Format;
335 }
336
Rafael Espindola179821a2010-12-06 19:08:48 +0000337 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000338 uint64_t Value) const;
339
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000340 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jim Grosbachc9d14392010-11-05 18:48:58 +0000341 // FIXME: Subtarget info should be derived. Force v7 for now.
Daniel Dunbar36d76a82010-11-27 04:38:36 +0000342 return createMachObjectWriter(OS, /*Is64Bit=*/false,
343 object::mach::CTM_ARM,
344 object::mach::CSARM_V7,
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000345 /*IsLittleEndian=*/true);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000346 }
347
348 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
349 return false;
350 }
351};
352
Bill Wendlingd832fa02010-12-07 23:11:00 +0000353/// getFixupKindNumBytes - The number of bytes the fixup may change.
Jim Grosbachc466b932010-11-11 18:04:49 +0000354static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000355 switch (Kind) {
Jim Grosbach662a8162010-12-06 23:57:07 +0000356 default:
357 llvm_unreachable("Unknown fixup kind!");
Bill Wendlingb8958b02010-12-08 01:57:09 +0000358
Jim Grosbach01086452010-12-10 17:13:40 +0000359 case ARM::fixup_arm_thumb_bcc:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000360 case ARM::fixup_arm_thumb_cp:
361 return 1;
362
Jim Grosbache2467172010-12-10 18:21:33 +0000363 case ARM::fixup_arm_thumb_br:
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000364 case ARM::fixup_arm_thumb_cb:
Bill Wendlingdff2f712010-12-08 23:01:43 +0000365 return 2;
366
Jim Grosbach662a8162010-12-06 23:57:07 +0000367 case ARM::fixup_arm_ldst_pcrel_12:
368 case ARM::fixup_arm_pcrel_10:
369 case ARM::fixup_arm_adr_pcrel_12:
370 case ARM::fixup_arm_branch:
371 return 3;
Bill Wendlingb8958b02010-12-08 01:57:09 +0000372
373 case FK_Data_4:
Owen Andersond7b3f582010-12-09 01:51:07 +0000374 case ARM::fixup_t2_ldst_pcrel_12:
Owen Andersonc2666002010-12-13 19:31:11 +0000375 case ARM::fixup_t2_condbranch:
376 case ARM::fixup_t2_uncondbranch:
Owen Andersond8e351b2010-12-08 00:18:36 +0000377 case ARM::fixup_t2_pcrel_10:
Owen Andersona838a252010-12-14 00:36:49 +0000378 case ARM::fixup_t2_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +0000379 case ARM::fixup_arm_thumb_bl:
Bill Wendling09aa3f02010-12-09 00:39:08 +0000380 case ARM::fixup_arm_thumb_blx:
Jim Grosbach662a8162010-12-06 23:57:07 +0000381 return 4;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000382 }
383}
384
Rafael Espindola179821a2010-12-06 19:08:48 +0000385void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
386 unsigned DataSize, uint64_t Value) const {
Jim Grosbachc466b932010-11-11 18:04:49 +0000387 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Jim Grosbach679cbd32010-11-09 01:37:15 +0000388 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000389 if (!Value) return; // Doesn't change encoding.
Jim Grosbach679cbd32010-11-09 01:37:15 +0000390
Bill Wendlingd832fa02010-12-07 23:11:00 +0000391 unsigned Offset = Fixup.getOffset();
392 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
393
Jim Grosbach679cbd32010-11-09 01:37:15 +0000394 // For each byte of the fragment that the fixup touches, mask in the
395 // bits from the fixup value.
396 for (unsigned i = 0; i != NumBytes; ++i)
Bill Wendlingd832fa02010-12-07 23:11:00 +0000397 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000398}
Bill Wendling52e635e2010-12-07 23:05:20 +0000399
Jim Grosbachf73fd722010-09-30 03:21:00 +0000400} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000401
402TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
403 const std::string &TT) {
404 switch (Triple(TT).getOS()) {
405 case Triple::Darwin:
406 return new DarwinARMAsmBackend(T);
407 case Triple::MinGW32:
408 case Triple::Cygwin:
409 case Triple::Win32:
410 assert(0 && "Windows not supported on ARM");
411 default:
412 return new ELFARMAsmBackend(T, Triple(TT).getOS());
413 }
414}