blob: 9556a7d9e326331bdfbfa7cfefa1bcd11444ba5c [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 {
32};
33
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000034class ARMAsmBackend : public TargetAsmBackend {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000035 bool isThumbMode; // Currently emitting Thumb code.
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000036public:
Jim Grosbach022ab372010-12-08 15:36:45 +000037 ARMAsmBackend(const Target &T) : TargetAsmBackend(), isThumbMode(false) {}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000038
Daniel Dunbar2761fc42010-12-16 03:20:06 +000039 unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
40
41 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
42 const static MCFixupKindInfo Infos[ARM::NumTargetFixupKinds] = {
43// This table *must* be in the order that the fixup_* kinds are defined in
44// ARMFixupKinds.h.
45//
46// Name Offset (bits) Size (bits) Flags
47{ "fixup_arm_ldst_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
48{ "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
49 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
50{ "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
51{ "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
52 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
53{ "fixup_thumb_adr_pcrel_10",0, 8, MCFixupKindInfo::FKF_IsPCRel |
54 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
55{ "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
56{ "fixup_t2_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
57 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
58{ "fixup_arm_branch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
59{ "fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
60{ "fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
61{ "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
62{ "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
63{ "fixup_arm_thumb_blx", 7, 21, MCFixupKindInfo::FKF_IsPCRel },
64{ "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
65{ "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
66{ "fixup_arm_thumb_bcc", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
67{ "fixup_arm_movt_hi16", 0, 16, 0 },
68{ "fixup_arm_movw_lo16", 0, 16, 0 },
69 };
70
71 if (Kind < FirstTargetFixupKind)
72 return TargetAsmBackend::getFixupKindInfo(Kind);
73
74 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
75 "Invalid kind!");
76 return Infos[Kind - FirstTargetFixupKind];
77 }
78
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000079 bool MayNeedRelaxation(const MCInst &Inst) const;
80
81 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
82
83 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
Jim Grosbach3787a402010-09-30 17:45:51 +000084
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000085 void HandleAssemblerFlag(MCAssemblerFlag Flag) {
86 switch (Flag) {
87 default: break;
88 case MCAF_Code16:
89 setIsThumb(true);
90 break;
91 case MCAF_Code32:
92 setIsThumb(false);
93 break;
94 }
Jim Grosbach3787a402010-09-30 17:45:51 +000095 }
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000096
97 unsigned getPointerSize() const { return 4; }
98 bool isThumb() const { return isThumbMode; }
99 void setIsThumb(bool it) { isThumbMode = it; }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000100};
Chris Lattnerb75c6512010-11-17 05:41:32 +0000101} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000102
103bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
104 // FIXME: Thumb targets, different move constant targets..
105 return false;
106}
107
108void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
109 assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
110 return;
111}
112
113bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000114 if (isThumb()) {
115 assert (((Count & 1) == 0) && "Unaligned Nop data fragment!");
116 // FIXME: 0xbf00 is the ARMv7 value. For v6 and before, we'll need to
117 // use 0x46c0 (which is a 'mov r8, r8' insn).
118 Count /= 2;
119 for (uint64_t i = 0; i != Count; ++i)
120 OW->Write16(0xbf00);
121 return true;
122 }
123 // ARM mode
124 Count /= 4;
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000125 for (uint64_t i = 0; i != Count; ++i)
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000126 OW->Write32(0xe1a00000);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000127 return true;
Jim Grosbach87dc3aa2010-09-30 03:20:34 +0000128}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000129
Jason W Kim0c628c22010-12-01 22:46:50 +0000130static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
131 switch (Kind) {
132 default:
133 llvm_unreachable("Unknown fixup kind!");
134 case FK_Data_4:
Jason W Kim0c628c22010-12-01 22:46:50 +0000135 return Value;
Jason W Kim2ccf1482010-12-03 19:40:23 +0000136 case ARM::fixup_arm_movt_hi16:
137 case ARM::fixup_arm_movw_lo16: {
138 unsigned Hi4 = (Value & 0xF000) >> 12;
139 unsigned Lo12 = Value & 0x0FFF;
140 // inst{19-16} = Hi4;
141 // inst{11-0} = Lo12;
142 Value = (Hi4 << 16) | (Lo12);
143 return Value;
144 }
Owen Andersond7b3f582010-12-09 01:51:07 +0000145 case ARM::fixup_arm_ldst_pcrel_12:
Jason W Kim0c628c22010-12-01 22:46:50 +0000146 // ARM PC-relative values are offset by 8.
Owen Anderson05018c22010-12-09 20:27:52 +0000147 Value -= 4;
Owen Andersonfe7fac72010-12-09 21:34:47 +0000148 // FALLTHROUGH
Owen Andersond7b3f582010-12-09 01:51:07 +0000149 case ARM::fixup_t2_ldst_pcrel_12: {
150 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Anderson05018c22010-12-09 20:27:52 +0000151 Value -= 4;
Owen Andersond7b3f582010-12-09 01:51:07 +0000152 bool isAdd = true;
Jason W Kim0c628c22010-12-01 22:46:50 +0000153 if ((int64_t)Value < 0) {
154 Value = -Value;
155 isAdd = false;
156 }
157 assert ((Value < 4096) && "Out of range pc-relative fixup value!");
158 Value |= isAdd << 23;
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000159
Owen Andersond7b3f582010-12-09 01:51:07 +0000160 // Same addressing mode as fixup_arm_pcrel_10,
161 // but with 16-bit halfwords swapped.
162 if (Kind == ARM::fixup_t2_ldst_pcrel_12) {
163 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
164 swapped |= (Value & 0x0000FFFF) << 16;
165 return swapped;
166 }
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000167
Jason W Kim0c628c22010-12-01 22:46:50 +0000168 return Value;
169 }
Jim Grosbachd40963c2010-12-14 22:28:03 +0000170 case ARM::fixup_thumb_adr_pcrel_10:
171 return ((Value - 4) >> 2) & 0xff;
Jim Grosbachdff84b02010-12-02 00:28:45 +0000172 case ARM::fixup_arm_adr_pcrel_12: {
173 // ARM PC-relative values are offset by 8.
174 Value -= 8;
175 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
176 if ((int64_t)Value < 0) {
177 Value = -Value;
178 opc = 2; // 0b0010
179 }
180 assert(ARM_AM::getSOImmVal(Value) != -1 &&
181 "Out of range pc-relative fixup value!");
182 // Encode the immediate and shift the opcode into place.
183 return ARM_AM::getSOImmVal(Value) | (opc << 21);
184 }
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000185
Owen Andersona838a252010-12-14 00:36:49 +0000186 case ARM::fixup_t2_adr_pcrel_12: {
187 Value -= 4;
188 unsigned opc = 0;
189 if ((int64_t)Value < 0) {
190 Value = -Value;
191 opc = 5;
192 }
193
194 uint32_t out = (opc << 21);
195 out |= (Value & 0x800) << 14;
196 out |= (Value & 0x700) << 4;
197 out |= (Value & 0x0FF);
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000198
Owen Andersona838a252010-12-14 00:36:49 +0000199 uint64_t swapped = (out & 0xFFFF0000) >> 16;
200 swapped |= (out & 0x0000FFFF) << 16;
201 return swapped;
202 }
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000203
Jason W Kim0c628c22010-12-01 22:46:50 +0000204 case ARM::fixup_arm_branch:
205 // These values don't encode the low two bits since they're always zero.
206 // Offset by 8 just as above.
Jim Grosbach662a8162010-12-06 23:57:07 +0000207 return 0xffffff & ((Value - 8) >> 2);
Owen Andersonc2666002010-12-13 19:31:11 +0000208 case ARM::fixup_t2_uncondbranch: {
Owen Anderson63ee2202010-12-10 23:02:28 +0000209 Value = Value - 4;
Owen Andersonfb20d892010-12-09 00:27:41 +0000210 Value >>= 1; // Low bit is not encoded.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000211
Jim Grosbach56a25352010-12-13 19:25:46 +0000212 uint32_t out = 0;
Owen Andersonc2666002010-12-13 19:31:11 +0000213 bool I = Value & 0x800000;
214 bool J1 = Value & 0x400000;
215 bool J2 = Value & 0x200000;
216 J1 ^= I;
217 J2 ^= I;
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000218
Owen Andersonc2666002010-12-13 19:31:11 +0000219 out |= I << 26; // S bit
220 out |= !J1 << 13; // J1 bit
221 out |= !J2 << 11; // J2 bit
222 out |= (Value & 0x1FF800) << 5; // imm6 field
223 out |= (Value & 0x0007FF); // imm11 field
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000224
Owen Andersonc2666002010-12-13 19:31:11 +0000225 uint64_t swapped = (out & 0xFFFF0000) >> 16;
226 swapped |= (out & 0x0000FFFF) << 16;
227 return swapped;
228 }
229 case ARM::fixup_t2_condbranch: {
230 Value = Value - 4;
231 Value >>= 1; // Low bit is not encoded.
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000232
Owen Andersonc2666002010-12-13 19:31:11 +0000233 uint64_t out = 0;
Owen Anderson8f079432010-12-09 01:02:09 +0000234 out |= (Value & 0x80000) << 7; // S bit
235 out |= (Value & 0x40000) >> 7; // J2 bit
236 out |= (Value & 0x20000) >> 4; // J1 bit
237 out |= (Value & 0x1F800) << 5; // imm6 field
238 out |= (Value & 0x007FF); // imm11 field
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000239
Jim Grosbach56a25352010-12-13 19:25:46 +0000240 uint32_t swapped = (out & 0xFFFF0000) >> 16;
Owen Andersonfb20d892010-12-09 00:27:41 +0000241 swapped |= (out & 0x0000FFFF) << 16;
242 return swapped;
243 }
Jim Grosbach662a8162010-12-06 23:57:07 +0000244 case ARM::fixup_arm_thumb_bl: {
245 // The value doesn't encode the low bit (always zero) and is offset by
246 // four. The value is encoded into disjoint bit positions in the destination
247 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000248 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000249 // BL: xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000250 //
Jim Grosbach662a8162010-12-06 23:57:07 +0000251 // Note that the halfwords are stored high first, low second; so we need
252 // to transpose the fixup value here to map properly.
Bill Wendling09aa3f02010-12-09 00:39:08 +0000253 unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000254 uint32_t Binary = 0;
255 Value = 0x3fffff & ((Value - 4) >> 1);
256 Binary = (Value & 0x7ff) << 16; // Low imm11 value.
257 Binary |= (Value & 0x1ffc00) >> 11; // High imm10 value.
258 Binary |= isNeg << 10; // Sign bit.
Bill Wendling09aa3f02010-12-09 00:39:08 +0000259 return Binary;
260 }
261 case ARM::fixup_arm_thumb_blx: {
262 // The value doesn't encode the low two bits (always zero) and is offset by
263 // four (see fixup_arm_thumb_cp). The value is encoded into disjoint bit
264 // positions in the destination opcode. x = unchanged, I = immediate value
265 // bit, S = sign extension bit, 0 = zero.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000266 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000267 // BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000268 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000269 // Note that the halfwords are stored high first, low second; so we need
270 // to transpose the fixup value here to map properly.
271 unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000272 uint32_t Binary = 0;
273 Value = 0xfffff & ((Value - 2) >> 2);
274 Binary = (Value & 0x3ff) << 17; // Low imm10L value.
275 Binary |= (Value & 0xffc00) >> 10; // High imm10H value.
276 Binary |= isNeg << 10; // Sign bit.
Jim Grosbach662a8162010-12-06 23:57:07 +0000277 return Binary;
278 }
Bill Wendlingb8958b02010-12-08 01:57:09 +0000279 case ARM::fixup_arm_thumb_cp:
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000280 // Offset by 4, and don't encode the low two bits. Two bytes of that
281 // 'off by 4' is implicitly handled by the half-word ordering of the
282 // Thumb encoding, so we only need to adjust by 2 here.
283 return ((Value - 2) >> 2) & 0xff;
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000284 case ARM::fixup_arm_thumb_cb: {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000285 // Offset by 4 and don't encode the lower bit, which is always 0.
286 uint32_t Binary = (Value - 4) >> 1;
Owen Anderson86abd482010-12-14 19:42:53 +0000287 return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000288 }
Jim Grosbache2467172010-12-10 18:21:33 +0000289 case ARM::fixup_arm_thumb_br:
290 // Offset by 4 and don't encode the lower bit, which is always 0.
291 return ((Value - 4) >> 1) & 0x7ff;
Jim Grosbach01086452010-12-10 17:13:40 +0000292 case ARM::fixup_arm_thumb_bcc:
293 // Offset by 4 and don't encode the lower bit, which is always 0.
294 return ((Value - 4) >> 1) & 0xff;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000295 case ARM::fixup_arm_pcrel_10:
Owen Andersone2e0f582010-12-10 22:46:47 +0000296 Value = Value - 4; // ARM fixups offset by an additional word and don't
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000297 // need to adjust for the half-word ordering.
298 // Fall through.
299 case ARM::fixup_t2_pcrel_10: {
300 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Andersone2e0f582010-12-10 22:46:47 +0000301 Value = Value - 4;
Jason W Kim0c628c22010-12-01 22:46:50 +0000302 bool isAdd = true;
303 if ((int64_t)Value < 0) {
304 Value = -Value;
305 isAdd = false;
306 }
307 // These values don't encode the low two bits since they're always zero.
308 Value >>= 2;
309 assert ((Value < 256) && "Out of range pc-relative fixup value!");
310 Value |= isAdd << 23;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000311
Owen Andersoncc78f5c2010-12-08 19:31:11 +0000312 // Same addressing mode as fixup_arm_pcrel_10,
313 // but with 16-bit halfwords swapped.
Owen Andersond8e351b2010-12-08 00:18:36 +0000314 if (Kind == ARM::fixup_t2_pcrel_10) {
Jim Grosbach56a25352010-12-13 19:25:46 +0000315 uint32_t swapped = (Value & 0xFFFF0000) >> 16;
Owen Anderson255eafb2010-12-08 00:21:33 +0000316 swapped |= (Value & 0x0000FFFF) << 16;
Owen Andersond8e351b2010-12-08 00:18:36 +0000317 return swapped;
318 }
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000319
Jason W Kim0c628c22010-12-01 22:46:50 +0000320 return Value;
321 }
322 }
323}
324
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000325namespace {
Bill Wendling52e635e2010-12-07 23:05:20 +0000326
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000327// FIXME: This should be in a separate file.
328// ELF is an ELF of course...
329class ELFARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000330 MCELFObjectFormat Format;
331
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000332public:
333 Triple::OSType OSType;
334 ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
335 : ARMAsmBackend(T), OSType(_OSType) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000336 HasScatteredSymbols = true;
337 }
338
Rafael Espindolaf230df92010-10-16 18:23:53 +0000339 virtual const MCObjectFormat &getObjectFormat() const {
340 return Format;
341 }
342
Rafael Espindola179821a2010-12-06 19:08:48 +0000343 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000344 uint64_t Value) const;
345
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000346 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000347 return createELFObjectWriter(OS, /*Is64Bit=*/false,
348 OSType, ELF::EM_ARM,
349 /*IsLittleEndian=*/true,
350 /*HasRelocationAddend=*/false);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000351 }
352};
353
Bill Wendling52e635e2010-12-07 23:05:20 +0000354// FIXME: Raise this to share code between Darwin and ELF.
Rafael Espindola179821a2010-12-06 19:08:48 +0000355void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
356 unsigned DataSize, uint64_t Value) const {
Bill Wendling52e635e2010-12-07 23:05:20 +0000357 unsigned NumBytes = 4; // FIXME: 2 for Thumb
Bill Wendling52e635e2010-12-07 23:05:20 +0000358 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000359 if (!Value) return; // Doesn't change encoding.
Bill Wendling52e635e2010-12-07 23:05:20 +0000360
361 unsigned Offset = Fixup.getOffset();
362 assert(Offset % NumBytes == 0 && "Offset mod NumBytes is nonzero!");
363
364 // For each byte of the fragment that the fixup touches, mask in the bits from
365 // the fixup value. The Value has been "split up" into the appropriate
366 // bitfields above.
367 for (unsigned i = 0; i != NumBytes; ++i)
368 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000369}
370
371// FIXME: This should be in a separate file.
372class DarwinARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000373 MCMachOObjectFormat Format;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000374public:
Chris Lattnerb75c6512010-11-17 05:41:32 +0000375 DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000376 HasScatteredSymbols = true;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000377 }
378
Rafael Espindolaf230df92010-10-16 18:23:53 +0000379 virtual const MCObjectFormat &getObjectFormat() const {
380 return Format;
381 }
382
Rafael Espindola179821a2010-12-06 19:08:48 +0000383 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000384 uint64_t Value) const;
385
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000386 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jim Grosbachc9d14392010-11-05 18:48:58 +0000387 // FIXME: Subtarget info should be derived. Force v7 for now.
Daniel Dunbarae5abd52010-12-16 16:09:19 +0000388 return createMachObjectWriter(new ARMMachObjectWriter,
389 OS, /*Is64Bit=*/false,
Daniel Dunbar36d76a82010-11-27 04:38:36 +0000390 object::mach::CTM_ARM,
391 object::mach::CSARM_V7,
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000392 /*IsLittleEndian=*/true);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000393 }
394
395 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
396 return false;
397 }
398};
399
Bill Wendlingd832fa02010-12-07 23:11:00 +0000400/// getFixupKindNumBytes - The number of bytes the fixup may change.
Jim Grosbachc466b932010-11-11 18:04:49 +0000401static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000402 switch (Kind) {
Jim Grosbach662a8162010-12-06 23:57:07 +0000403 default:
404 llvm_unreachable("Unknown fixup kind!");
Bill Wendlingb8958b02010-12-08 01:57:09 +0000405
Jim Grosbach01086452010-12-10 17:13:40 +0000406 case ARM::fixup_arm_thumb_bcc:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000407 case ARM::fixup_arm_thumb_cp:
Jim Grosbachd40963c2010-12-14 22:28:03 +0000408 case ARM::fixup_thumb_adr_pcrel_10:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000409 return 1;
410
Jim Grosbache2467172010-12-10 18:21:33 +0000411 case ARM::fixup_arm_thumb_br:
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000412 case ARM::fixup_arm_thumb_cb:
Bill Wendlingdff2f712010-12-08 23:01:43 +0000413 return 2;
414
Jim Grosbach662a8162010-12-06 23:57:07 +0000415 case ARM::fixup_arm_ldst_pcrel_12:
416 case ARM::fixup_arm_pcrel_10:
417 case ARM::fixup_arm_adr_pcrel_12:
418 case ARM::fixup_arm_branch:
419 return 3;
Bill Wendlingb8958b02010-12-08 01:57:09 +0000420
421 case FK_Data_4:
Owen Andersond7b3f582010-12-09 01:51:07 +0000422 case ARM::fixup_t2_ldst_pcrel_12:
Owen Andersonc2666002010-12-13 19:31:11 +0000423 case ARM::fixup_t2_condbranch:
424 case ARM::fixup_t2_uncondbranch:
Owen Andersond8e351b2010-12-08 00:18:36 +0000425 case ARM::fixup_t2_pcrel_10:
Owen Andersona838a252010-12-14 00:36:49 +0000426 case ARM::fixup_t2_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +0000427 case ARM::fixup_arm_thumb_bl:
Bill Wendling09aa3f02010-12-09 00:39:08 +0000428 case ARM::fixup_arm_thumb_blx:
Jim Grosbach662a8162010-12-06 23:57:07 +0000429 return 4;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000430 }
431}
432
Rafael Espindola179821a2010-12-06 19:08:48 +0000433void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
434 unsigned DataSize, uint64_t Value) const {
Jim Grosbachc466b932010-11-11 18:04:49 +0000435 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Jim Grosbach679cbd32010-11-09 01:37:15 +0000436 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000437 if (!Value) return; // Doesn't change encoding.
Jim Grosbach679cbd32010-11-09 01:37:15 +0000438
Bill Wendlingd832fa02010-12-07 23:11:00 +0000439 unsigned Offset = Fixup.getOffset();
440 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
441
Jim Grosbach679cbd32010-11-09 01:37:15 +0000442 // For each byte of the fragment that the fixup touches, mask in the
443 // bits from the fixup value.
444 for (unsigned i = 0; i != NumBytes; ++i)
Bill Wendlingd832fa02010-12-07 23:11:00 +0000445 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000446}
Bill Wendling52e635e2010-12-07 23:05:20 +0000447
Jim Grosbachf73fd722010-09-30 03:21:00 +0000448} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000449
450TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
451 const std::string &TT) {
452 switch (Triple(TT).getOS()) {
453 case Triple::Darwin:
454 return new DarwinARMAsmBackend(T);
455 case Triple::MinGW32:
456 case Triple::Cygwin:
457 case Triple::Win32:
458 assert(0 && "Windows not supported on ARM");
459 default:
460 return new ELFARMAsmBackend(T, Triple(TT).getOS());
461 }
462}