blob: 642e722d73339df7600b333a8650ca126df2762d [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
Evan Chengbe740292011-07-23 00:00:19 +000010#include "MCTargetDesc/ARMBaseInfo.h"
11#include "MCTargetDesc/ARMFixupKinds.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000012#include "MCTargetDesc/ARMAddressingModes.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"
Rafael Espindola285b3e52010-12-17 16:59:53 +000016#include "llvm/MC/MCELFObjectWriter.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000017#include "llvm/MC/MCExpr.h"
Daniel Dunbaraa4b7dd2010-12-16 16:08:33 +000018#include "llvm/MC/MCMachObjectWriter.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"
Evan Cheng78c10ee2011-07-25 23:24:55 +000022#include "llvm/MC/MCAsmBackend.h"
Daniel Dunbar36d76a82010-11-27 04:38:36 +000023#include "llvm/Object/MachOFormat.h"
Wesley Peckeecb8582010-10-22 15:52:49 +000024#include "llvm/Support/ELF.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000025#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/raw_ostream.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000027using namespace llvm;
28
29namespace {
Rafael Espindola6024c972010-12-17 17:45:22 +000030class ARMELFObjectWriter : public MCELFObjectTargetWriter {
31public:
Rafael Espindolabff66a82010-12-18 03:27:34 +000032 ARMELFObjectWriter(Triple::OSType OSType)
33 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSType, ELF::EM_ARM,
34 /*HasRelocationAddend*/ false) {}
Rafael Espindola6024c972010-12-17 17:45:22 +000035};
36
Evan Cheng78c10ee2011-07-25 23:24:55 +000037class ARMAsmBackend : public MCAsmBackend {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000038 bool isThumbMode; // Currently emitting Thumb code.
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000039public:
Evan Cheng78c10ee2011-07-25 23:24:55 +000040 ARMAsmBackend(const Target &T) : MCAsmBackend(), isThumbMode(false) {}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000041
Daniel Dunbar2761fc42010-12-16 03:20:06 +000042 unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
43
44 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
45 const static MCFixupKindInfo Infos[ARM::NumTargetFixupKinds] = {
46// This table *must* be in the order that the fixup_* kinds are defined in
47// ARMFixupKinds.h.
48//
49// Name Offset (bits) Size (bits) Flags
50{ "fixup_arm_ldst_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
51{ "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
52 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
53{ "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
54{ "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
55 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
56{ "fixup_thumb_adr_pcrel_10",0, 8, MCFixupKindInfo::FKF_IsPCRel |
57 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
58{ "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
59{ "fixup_t2_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
60 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Jason W Kim685c3502011-02-04 19:47:15 +000061{ "fixup_arm_condbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
62{ "fixup_arm_uncondbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar2761fc42010-12-16 03:20:06 +000063{ "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 },
Eric Christopherfea51fc2011-05-28 03:16:22 +000070{ "fixup_arm_thumb_bcc", 0, 8, MCFixupKindInfo::FKF_IsPCRel },
Evan Chengf3eb3bb2011-01-14 02:38:49 +000071// movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16 - 19.
72{ "fixup_arm_movt_hi16", 0, 20, 0 },
73{ "fixup_arm_movw_lo16", 0, 20, 0 },
74{ "fixup_t2_movt_hi16", 0, 20, 0 },
75{ "fixup_t2_movw_lo16", 0, 20, 0 },
76{ "fixup_arm_movt_hi16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
77{ "fixup_arm_movw_lo16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
78{ "fixup_t2_movt_hi16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
79{ "fixup_t2_movw_lo16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar2761fc42010-12-16 03:20:06 +000080 };
81
82 if (Kind < FirstTargetFixupKind)
Evan Cheng78c10ee2011-07-25 23:24:55 +000083 return MCAsmBackend::getFixupKindInfo(Kind);
Daniel Dunbar2761fc42010-12-16 03:20:06 +000084
85 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
86 "Invalid kind!");
87 return Infos[Kind - FirstTargetFixupKind];
88 }
89
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000090 bool MayNeedRelaxation(const MCInst &Inst) const;
91
92 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
93
94 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
Jim Grosbach3787a402010-09-30 17:45:51 +000095
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000096 void HandleAssemblerFlag(MCAssemblerFlag Flag) {
97 switch (Flag) {
98 default: break;
99 case MCAF_Code16:
100 setIsThumb(true);
101 break;
102 case MCAF_Code32:
103 setIsThumb(false);
104 break;
105 }
Jim Grosbach3787a402010-09-30 17:45:51 +0000106 }
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000107
108 unsigned getPointerSize() const { return 4; }
109 bool isThumb() const { return isThumbMode; }
110 void setIsThumb(bool it) { isThumbMode = it; }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000111};
Chris Lattnerb75c6512010-11-17 05:41:32 +0000112} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000113
114bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
115 // FIXME: Thumb targets, different move constant targets..
116 return false;
117}
118
119void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
120 assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
121 return;
122}
123
124bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000125 if (isThumb()) {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000126 // FIXME: 0xbf00 is the ARMv7 value. For v6 and before, we'll need to
127 // use 0x46c0 (which is a 'mov r8, r8' insn).
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000128 uint64_t NumNops = Count / 2;
129 for (uint64_t i = 0; i != NumNops; ++i)
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000130 OW->Write16(0xbf00);
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000131 if (Count & 1)
132 OW->Write8(0);
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000133 return true;
134 }
135 // ARM mode
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000136 uint64_t NumNops = Count / 4;
137 for (uint64_t i = 0; i != NumNops; ++i)
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000138 OW->Write32(0xe1a00000);
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000139 switch (Count % 4) {
140 default: break; // No leftover bytes to write
141 case 1: OW->Write8(0); break;
142 case 2: OW->Write16(0); break;
143 case 3: OW->Write16(0); OW->Write8(0xa0); break;
144 }
145
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000146 return true;
Jim Grosbach87dc3aa2010-09-30 03:20:34 +0000147}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000148
Jason W Kim0c628c22010-12-01 22:46:50 +0000149static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
150 switch (Kind) {
151 default:
152 llvm_unreachable("Unknown fixup kind!");
Jim Grosbach6ec6eeb2010-12-17 18:39:10 +0000153 case FK_Data_1:
154 case FK_Data_2:
Jason W Kim0c628c22010-12-01 22:46:50 +0000155 case FK_Data_4:
Jason W Kim0c628c22010-12-01 22:46:50 +0000156 return Value;
Jason W Kim2ccf1482010-12-03 19:40:23 +0000157 case ARM::fixup_arm_movt_hi16:
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000158 Value >>= 16;
159 // Fallthrough
160 case ARM::fixup_arm_movw_lo16:
Jason W Kim861b9c62011-05-19 20:55:25 +0000161 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim86a97f22011-01-12 00:19:25 +0000162 case ARM::fixup_arm_movw_lo16_pcrel: {
Jason W Kim2ccf1482010-12-03 19:40:23 +0000163 unsigned Hi4 = (Value & 0xF000) >> 12;
164 unsigned Lo12 = Value & 0x0FFF;
Jason W Kim861b9c62011-05-19 20:55:25 +0000165 assert ((((int64_t)Value) >= -0x8000) && (((int64_t)Value) <= 0x7fff) &&
166 "Out of range pc-relative fixup value!");
Jason W Kim2ccf1482010-12-03 19:40:23 +0000167 // inst{19-16} = Hi4;
168 // inst{11-0} = Lo12;
169 Value = (Hi4 << 16) | (Lo12);
170 return Value;
171 }
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000172 case ARM::fixup_t2_movt_hi16:
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000173 Value >>= 16;
174 // Fallthrough
175 case ARM::fixup_t2_movw_lo16:
Jim Grosbach8b454562011-06-24 20:06:59 +0000176 case ARM::fixup_t2_movt_hi16_pcrel: //FIXME: Shouldn't this be shifted like
177 // the other hi16 fixup?
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000178 case ARM::fixup_t2_movw_lo16_pcrel: {
179 unsigned Hi4 = (Value & 0xF000) >> 12;
180 unsigned i = (Value & 0x800) >> 11;
181 unsigned Mid3 = (Value & 0x700) >> 8;
182 unsigned Lo8 = Value & 0x0FF;
183 // inst{19-16} = Hi4;
184 // inst{26} = i;
185 // inst{14-12} = Mid3;
186 // inst{7-0} = Lo8;
Jim Grosbach8b454562011-06-24 20:06:59 +0000187 // The value comes in as the whole thing, not just the portion required
188 // for this fixup, so we need to mask off the bits not handled by this
189 // portion (lo vs. hi).
190 Value &= 0xffff;
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000191 Value = (Hi4 << 16) | (i << 26) | (Mid3 << 12) | (Lo8);
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000192 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
193 swapped |= (Value & 0x0000FFFF) << 16;
194 return swapped;
195 }
Owen Andersond7b3f582010-12-09 01:51:07 +0000196 case ARM::fixup_arm_ldst_pcrel_12:
Jason W Kim0c628c22010-12-01 22:46:50 +0000197 // ARM PC-relative values are offset by 8.
Owen Anderson05018c22010-12-09 20:27:52 +0000198 Value -= 4;
Owen Andersonfe7fac72010-12-09 21:34:47 +0000199 // FALLTHROUGH
Owen Andersond7b3f582010-12-09 01:51:07 +0000200 case ARM::fixup_t2_ldst_pcrel_12: {
201 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Anderson05018c22010-12-09 20:27:52 +0000202 Value -= 4;
Owen Andersond7b3f582010-12-09 01:51:07 +0000203 bool isAdd = true;
Jason W Kim0c628c22010-12-01 22:46:50 +0000204 if ((int64_t)Value < 0) {
205 Value = -Value;
206 isAdd = false;
207 }
208 assert ((Value < 4096) && "Out of range pc-relative fixup value!");
209 Value |= isAdd << 23;
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000210
Owen Andersond7b3f582010-12-09 01:51:07 +0000211 // Same addressing mode as fixup_arm_pcrel_10,
212 // but with 16-bit halfwords swapped.
213 if (Kind == ARM::fixup_t2_ldst_pcrel_12) {
214 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
215 swapped |= (Value & 0x0000FFFF) << 16;
216 return swapped;
217 }
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000218
Jason W Kim0c628c22010-12-01 22:46:50 +0000219 return Value;
220 }
Jim Grosbachd40963c2010-12-14 22:28:03 +0000221 case ARM::fixup_thumb_adr_pcrel_10:
222 return ((Value - 4) >> 2) & 0xff;
Jim Grosbachdff84b02010-12-02 00:28:45 +0000223 case ARM::fixup_arm_adr_pcrel_12: {
224 // ARM PC-relative values are offset by 8.
225 Value -= 8;
226 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
227 if ((int64_t)Value < 0) {
228 Value = -Value;
229 opc = 2; // 0b0010
230 }
231 assert(ARM_AM::getSOImmVal(Value) != -1 &&
232 "Out of range pc-relative fixup value!");
233 // Encode the immediate and shift the opcode into place.
234 return ARM_AM::getSOImmVal(Value) | (opc << 21);
235 }
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000236
Owen Andersona838a252010-12-14 00:36:49 +0000237 case ARM::fixup_t2_adr_pcrel_12: {
238 Value -= 4;
239 unsigned opc = 0;
240 if ((int64_t)Value < 0) {
241 Value = -Value;
242 opc = 5;
243 }
244
245 uint32_t out = (opc << 21);
Owen Anderson741ad152011-03-23 22:03:44 +0000246 out |= (Value & 0x800) << 15;
Owen Andersona838a252010-12-14 00:36:49 +0000247 out |= (Value & 0x700) << 4;
248 out |= (Value & 0x0FF);
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000249
Owen Andersona838a252010-12-14 00:36:49 +0000250 uint64_t swapped = (out & 0xFFFF0000) >> 16;
251 swapped |= (out & 0x0000FFFF) << 16;
252 return swapped;
253 }
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000254
Jason W Kim685c3502011-02-04 19:47:15 +0000255 case ARM::fixup_arm_condbranch:
256 case ARM::fixup_arm_uncondbranch:
Jason W Kim0c628c22010-12-01 22:46:50 +0000257 // These values don't encode the low two bits since they're always zero.
258 // Offset by 8 just as above.
Jim Grosbach662a8162010-12-06 23:57:07 +0000259 return 0xffffff & ((Value - 8) >> 2);
Owen Andersonc2666002010-12-13 19:31:11 +0000260 case ARM::fixup_t2_uncondbranch: {
Owen Anderson63ee2202010-12-10 23:02:28 +0000261 Value = Value - 4;
Owen Andersonfb20d892010-12-09 00:27:41 +0000262 Value >>= 1; // Low bit is not encoded.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000263
Jim Grosbach56a25352010-12-13 19:25:46 +0000264 uint32_t out = 0;
Owen Andersonc2666002010-12-13 19:31:11 +0000265 bool I = Value & 0x800000;
266 bool J1 = Value & 0x400000;
267 bool J2 = Value & 0x200000;
268 J1 ^= I;
269 J2 ^= I;
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000270
Owen Andersonc2666002010-12-13 19:31:11 +0000271 out |= I << 26; // S bit
272 out |= !J1 << 13; // J1 bit
273 out |= !J2 << 11; // J2 bit
274 out |= (Value & 0x1FF800) << 5; // imm6 field
275 out |= (Value & 0x0007FF); // imm11 field
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000276
Owen Andersonc2666002010-12-13 19:31:11 +0000277 uint64_t swapped = (out & 0xFFFF0000) >> 16;
278 swapped |= (out & 0x0000FFFF) << 16;
279 return swapped;
280 }
281 case ARM::fixup_t2_condbranch: {
282 Value = Value - 4;
283 Value >>= 1; // Low bit is not encoded.
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000284
Owen Andersonc2666002010-12-13 19:31:11 +0000285 uint64_t out = 0;
Owen Anderson8f079432010-12-09 01:02:09 +0000286 out |= (Value & 0x80000) << 7; // S bit
287 out |= (Value & 0x40000) >> 7; // J2 bit
288 out |= (Value & 0x20000) >> 4; // J1 bit
289 out |= (Value & 0x1F800) << 5; // imm6 field
290 out |= (Value & 0x007FF); // imm11 field
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000291
Jim Grosbach56a25352010-12-13 19:25:46 +0000292 uint32_t swapped = (out & 0xFFFF0000) >> 16;
Owen Andersonfb20d892010-12-09 00:27:41 +0000293 swapped |= (out & 0x0000FFFF) << 16;
294 return swapped;
295 }
Jim Grosbach662a8162010-12-06 23:57:07 +0000296 case ARM::fixup_arm_thumb_bl: {
297 // The value doesn't encode the low bit (always zero) and is offset by
298 // four. The value is encoded into disjoint bit positions in the destination
299 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000300 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000301 // BL: xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000302 //
Jim Grosbach662a8162010-12-06 23:57:07 +0000303 // Note that the halfwords are stored high first, low second; so we need
304 // to transpose the fixup value here to map properly.
Rafael Espindola298c8e12011-05-20 20:01:01 +0000305 unsigned isNeg = (int64_t(Value - 4) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000306 uint32_t Binary = 0;
307 Value = 0x3fffff & ((Value - 4) >> 1);
308 Binary = (Value & 0x7ff) << 16; // Low imm11 value.
309 Binary |= (Value & 0x1ffc00) >> 11; // High imm10 value.
310 Binary |= isNeg << 10; // Sign bit.
Bill Wendling09aa3f02010-12-09 00:39:08 +0000311 return Binary;
312 }
313 case ARM::fixup_arm_thumb_blx: {
314 // The value doesn't encode the low two bits (always zero) and is offset by
315 // four (see fixup_arm_thumb_cp). The value is encoded into disjoint bit
316 // positions in the destination opcode. x = unchanged, I = immediate value
317 // bit, S = sign extension bit, 0 = zero.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000318 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000319 // BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000320 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000321 // Note that the halfwords are stored high first, low second; so we need
322 // to transpose the fixup value here to map properly.
Rafael Espindola298c8e12011-05-20 20:01:01 +0000323 unsigned isNeg = (int64_t(Value-4) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000324 uint32_t Binary = 0;
325 Value = 0xfffff & ((Value - 2) >> 2);
326 Binary = (Value & 0x3ff) << 17; // Low imm10L value.
327 Binary |= (Value & 0xffc00) >> 10; // High imm10H value.
328 Binary |= isNeg << 10; // Sign bit.
Jim Grosbach662a8162010-12-06 23:57:07 +0000329 return Binary;
330 }
Bill Wendlingb8958b02010-12-08 01:57:09 +0000331 case ARM::fixup_arm_thumb_cp:
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000332 // Offset by 4, and don't encode the low two bits. Two bytes of that
333 // 'off by 4' is implicitly handled by the half-word ordering of the
334 // Thumb encoding, so we only need to adjust by 2 here.
335 return ((Value - 2) >> 2) & 0xff;
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000336 case ARM::fixup_arm_thumb_cb: {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000337 // Offset by 4 and don't encode the lower bit, which is always 0.
338 uint32_t Binary = (Value - 4) >> 1;
Owen Anderson86abd482010-12-14 19:42:53 +0000339 return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000340 }
Jim Grosbache2467172010-12-10 18:21:33 +0000341 case ARM::fixup_arm_thumb_br:
342 // Offset by 4 and don't encode the lower bit, which is always 0.
343 return ((Value - 4) >> 1) & 0x7ff;
Jim Grosbach01086452010-12-10 17:13:40 +0000344 case ARM::fixup_arm_thumb_bcc:
345 // Offset by 4 and don't encode the lower bit, which is always 0.
346 return ((Value - 4) >> 1) & 0xff;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000347 case ARM::fixup_arm_pcrel_10:
Owen Andersone2e0f582010-12-10 22:46:47 +0000348 Value = Value - 4; // ARM fixups offset by an additional word and don't
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000349 // need to adjust for the half-word ordering.
350 // Fall through.
351 case ARM::fixup_t2_pcrel_10: {
352 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Andersone2e0f582010-12-10 22:46:47 +0000353 Value = Value - 4;
Jason W Kim0c628c22010-12-01 22:46:50 +0000354 bool isAdd = true;
355 if ((int64_t)Value < 0) {
356 Value = -Value;
357 isAdd = false;
358 }
359 // These values don't encode the low two bits since they're always zero.
360 Value >>= 2;
361 assert ((Value < 256) && "Out of range pc-relative fixup value!");
362 Value |= isAdd << 23;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000363
Owen Andersoncc78f5c2010-12-08 19:31:11 +0000364 // Same addressing mode as fixup_arm_pcrel_10,
365 // but with 16-bit halfwords swapped.
Owen Andersond8e351b2010-12-08 00:18:36 +0000366 if (Kind == ARM::fixup_t2_pcrel_10) {
Jim Grosbach56a25352010-12-13 19:25:46 +0000367 uint32_t swapped = (Value & 0xFFFF0000) >> 16;
Owen Anderson255eafb2010-12-08 00:21:33 +0000368 swapped |= (Value & 0x0000FFFF) << 16;
Owen Andersond8e351b2010-12-08 00:18:36 +0000369 return swapped;
370 }
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000371
Jason W Kim0c628c22010-12-01 22:46:50 +0000372 return Value;
373 }
374 }
375}
376
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000377namespace {
Bill Wendling52e635e2010-12-07 23:05:20 +0000378
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000379// FIXME: This should be in a separate file.
380// ELF is an ELF of course...
381class ELFARMAsmBackend : public ARMAsmBackend {
382public:
383 Triple::OSType OSType;
384 ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
Daniel Dunbar7b62afa2010-12-17 02:06:08 +0000385 : ARMAsmBackend(T), OSType(_OSType) { }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000386
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 {
Rafael Espindolabff66a82010-12-18 03:27:34 +0000391 return createELFObjectWriter(new ARMELFObjectWriter(OSType), OS,
392 /*IsLittleEndian*/ true);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000393 }
394};
395
Bill Wendling52e635e2010-12-07 23:05:20 +0000396// FIXME: Raise this to share code between Darwin and ELF.
Rafael Espindola179821a2010-12-06 19:08:48 +0000397void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
398 unsigned DataSize, uint64_t Value) const {
Bill Wendling52e635e2010-12-07 23:05:20 +0000399 unsigned NumBytes = 4; // FIXME: 2 for Thumb
Bill Wendling52e635e2010-12-07 23:05:20 +0000400 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000401 if (!Value) return; // Doesn't change encoding.
Bill Wendling52e635e2010-12-07 23:05:20 +0000402
403 unsigned Offset = Fixup.getOffset();
Bill Wendling52e635e2010-12-07 23:05:20 +0000404
405 // For each byte of the fragment that the fixup touches, mask in the bits from
406 // the fixup value. The Value has been "split up" into the appropriate
407 // bitfields above.
408 for (unsigned i = 0; i != NumBytes; ++i)
409 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000410}
411
412// FIXME: This should be in a separate file.
413class DarwinARMAsmBackend : public ARMAsmBackend {
414public:
Owen Anderson17213242011-04-01 21:07:39 +0000415 const object::mach::CPUSubtypeARM Subtype;
416 DarwinARMAsmBackend(const Target &T, object::mach::CPUSubtypeARM st)
417 : ARMAsmBackend(T), Subtype(st) { }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000418
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000419 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jim Grosbach2fc68982011-06-22 20:14:52 +0000420 return createARMMachObjectWriter(OS, /*Is64Bit=*/false,
421 object::mach::CTM_ARM,
422 Subtype);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000423 }
424
Owen Anderson17213242011-04-01 21:07:39 +0000425 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
426 uint64_t Value) const;
427
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000428 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
429 return false;
430 }
431};
432
Bill Wendlingd832fa02010-12-07 23:11:00 +0000433/// getFixupKindNumBytes - The number of bytes the fixup may change.
Jim Grosbachc466b932010-11-11 18:04:49 +0000434static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000435 switch (Kind) {
Jim Grosbach662a8162010-12-06 23:57:07 +0000436 default:
437 llvm_unreachable("Unknown fixup kind!");
Bill Wendlingb8958b02010-12-08 01:57:09 +0000438
Jim Grosbach6ec6eeb2010-12-17 18:39:10 +0000439 case FK_Data_1:
Jim Grosbach01086452010-12-10 17:13:40 +0000440 case ARM::fixup_arm_thumb_bcc:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000441 case ARM::fixup_arm_thumb_cp:
Jim Grosbachd40963c2010-12-14 22:28:03 +0000442 case ARM::fixup_thumb_adr_pcrel_10:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000443 return 1;
444
Jim Grosbach6ec6eeb2010-12-17 18:39:10 +0000445 case FK_Data_2:
Jim Grosbache2467172010-12-10 18:21:33 +0000446 case ARM::fixup_arm_thumb_br:
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000447 case ARM::fixup_arm_thumb_cb:
Bill Wendlingdff2f712010-12-08 23:01:43 +0000448 return 2;
449
Jim Grosbach662a8162010-12-06 23:57:07 +0000450 case ARM::fixup_arm_ldst_pcrel_12:
451 case ARM::fixup_arm_pcrel_10:
452 case ARM::fixup_arm_adr_pcrel_12:
Jason W Kim685c3502011-02-04 19:47:15 +0000453 case ARM::fixup_arm_condbranch:
454 case ARM::fixup_arm_uncondbranch:
Jim Grosbach662a8162010-12-06 23:57:07 +0000455 return 3;
Bill Wendlingb8958b02010-12-08 01:57:09 +0000456
457 case FK_Data_4:
Owen Andersond7b3f582010-12-09 01:51:07 +0000458 case ARM::fixup_t2_ldst_pcrel_12:
Owen Andersonc2666002010-12-13 19:31:11 +0000459 case ARM::fixup_t2_condbranch:
460 case ARM::fixup_t2_uncondbranch:
Owen Andersond8e351b2010-12-08 00:18:36 +0000461 case ARM::fixup_t2_pcrel_10:
Owen Andersona838a252010-12-14 00:36:49 +0000462 case ARM::fixup_t2_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +0000463 case ARM::fixup_arm_thumb_bl:
Bill Wendling09aa3f02010-12-09 00:39:08 +0000464 case ARM::fixup_arm_thumb_blx:
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000465 case ARM::fixup_arm_movt_hi16:
466 case ARM::fixup_arm_movw_lo16:
467 case ARM::fixup_arm_movt_hi16_pcrel:
468 case ARM::fixup_arm_movw_lo16_pcrel:
469 case ARM::fixup_t2_movt_hi16:
470 case ARM::fixup_t2_movw_lo16:
471 case ARM::fixup_t2_movt_hi16_pcrel:
472 case ARM::fixup_t2_movw_lo16_pcrel:
Jim Grosbach662a8162010-12-06 23:57:07 +0000473 return 4;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000474 }
475}
476
Rafael Espindola179821a2010-12-06 19:08:48 +0000477void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
478 unsigned DataSize, uint64_t Value) const {
Jim Grosbachc466b932010-11-11 18:04:49 +0000479 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Jim Grosbach679cbd32010-11-09 01:37:15 +0000480 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000481 if (!Value) return; // Doesn't change encoding.
Jim Grosbach679cbd32010-11-09 01:37:15 +0000482
Bill Wendlingd832fa02010-12-07 23:11:00 +0000483 unsigned Offset = Fixup.getOffset();
484 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
485
Jim Grosbach679cbd32010-11-09 01:37:15 +0000486 // For each byte of the fragment that the fixup touches, mask in the
487 // bits from the fixup value.
488 for (unsigned i = 0; i != NumBytes; ++i)
Bill Wendlingd832fa02010-12-07 23:11:00 +0000489 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000490}
Bill Wendling52e635e2010-12-07 23:05:20 +0000491
Jim Grosbachf73fd722010-09-30 03:21:00 +0000492} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000493
Evan Cheng78c10ee2011-07-25 23:24:55 +0000494MCAsmBackend *llvm::createARMAsmBackend(const Target &T, StringRef TT) {
Owen Anderson17213242011-04-01 21:07:39 +0000495 Triple TheTriple(TT);
Daniel Dunbar912225e2011-04-19 21:14:45 +0000496
497 if (TheTriple.isOSDarwin()) {
Evan Chenga6eb2562011-06-14 18:08:33 +0000498 if (TheTriple.getArchName() == "armv4t" ||
499 TheTriple.getArchName() == "thumbv4t")
500 return new DarwinARMAsmBackend(T, object::mach::CSARM_V4T);
501 else if (TheTriple.getArchName() == "armv5e" ||
502 TheTriple.getArchName() == "thumbv5e")
503 return new DarwinARMAsmBackend(T, object::mach::CSARM_V5TEJ);
504 else if (TheTriple.getArchName() == "armv6" ||
Owen Anderson17213242011-04-01 21:07:39 +0000505 TheTriple.getArchName() == "thumbv6")
506 return new DarwinARMAsmBackend(T, object::mach::CSARM_V6);
507 return new DarwinARMAsmBackend(T, object::mach::CSARM_V7);
508 }
Daniel Dunbar912225e2011-04-19 21:14:45 +0000509
510 if (TheTriple.isOSWindows())
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000511 assert(0 && "Windows not supported on ARM");
Daniel Dunbar912225e2011-04-19 21:14:45 +0000512
513 return new ELFARMAsmBackend(T, Triple(TT).getOS());
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000514}