blob: 79e98970600392b564868fb09e07c30c6e724c8b [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"
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"
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 {
Rafael Espindola6024c972010-12-17 17:45:22 +000031class ARMELFObjectWriter : public MCELFObjectTargetWriter {
32public:
Rafael Espindolabff66a82010-12-18 03:27:34 +000033 ARMELFObjectWriter(Triple::OSType OSType)
34 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSType, ELF::EM_ARM,
35 /*HasRelocationAddend*/ false) {}
Rafael Espindola6024c972010-12-17 17:45:22 +000036};
37
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000038class ARMAsmBackend : public TargetAsmBackend {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000039 bool isThumbMode; // Currently emitting Thumb code.
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000040public:
Jim Grosbach022ab372010-12-08 15:36:45 +000041 ARMAsmBackend(const Target &T) : TargetAsmBackend(), isThumbMode(false) {}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000042
Daniel Dunbar2761fc42010-12-16 03:20:06 +000043 unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
44
45 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
46 const static MCFixupKindInfo Infos[ARM::NumTargetFixupKinds] = {
47// This table *must* be in the order that the fixup_* kinds are defined in
48// ARMFixupKinds.h.
49//
50// Name Offset (bits) Size (bits) Flags
51{ "fixup_arm_ldst_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
52{ "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
53 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
54{ "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
55{ "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
56 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
57{ "fixup_thumb_adr_pcrel_10",0, 8, MCFixupKindInfo::FKF_IsPCRel |
58 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
59{ "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
60{ "fixup_t2_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
61 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Jason W Kim685c3502011-02-04 19:47:15 +000062{ "fixup_arm_condbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
63{ "fixup_arm_uncondbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar2761fc42010-12-16 03:20:06 +000064{ "fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
65{ "fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
66{ "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
67{ "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
68{ "fixup_arm_thumb_blx", 7, 21, MCFixupKindInfo::FKF_IsPCRel },
69{ "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
70{ "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
Eric Christopherfea51fc2011-05-28 03:16:22 +000071{ "fixup_arm_thumb_bcc", 0, 8, MCFixupKindInfo::FKF_IsPCRel },
Evan Chengf3eb3bb2011-01-14 02:38:49 +000072// movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16 - 19.
73{ "fixup_arm_movt_hi16", 0, 20, 0 },
74{ "fixup_arm_movw_lo16", 0, 20, 0 },
75{ "fixup_t2_movt_hi16", 0, 20, 0 },
76{ "fixup_t2_movw_lo16", 0, 20, 0 },
77{ "fixup_arm_movt_hi16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
78{ "fixup_arm_movw_lo16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
79{ "fixup_t2_movt_hi16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
80{ "fixup_t2_movw_lo16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar2761fc42010-12-16 03:20:06 +000081 };
82
83 if (Kind < FirstTargetFixupKind)
84 return TargetAsmBackend::getFixupKindInfo(Kind);
85
86 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
87 "Invalid kind!");
88 return Infos[Kind - FirstTargetFixupKind];
89 }
90
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000091 bool MayNeedRelaxation(const MCInst &Inst) const;
92
93 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
94
95 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
Jim Grosbach3787a402010-09-30 17:45:51 +000096
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000097 void HandleAssemblerFlag(MCAssemblerFlag Flag) {
98 switch (Flag) {
99 default: break;
100 case MCAF_Code16:
101 setIsThumb(true);
102 break;
103 case MCAF_Code32:
104 setIsThumb(false);
105 break;
106 }
Jim Grosbach3787a402010-09-30 17:45:51 +0000107 }
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000108
109 unsigned getPointerSize() const { return 4; }
110 bool isThumb() const { return isThumbMode; }
111 void setIsThumb(bool it) { isThumbMode = it; }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000112};
Chris Lattnerb75c6512010-11-17 05:41:32 +0000113} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000114
115bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
116 // FIXME: Thumb targets, different move constant targets..
117 return false;
118}
119
120void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
121 assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
122 return;
123}
124
125bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000126 if (isThumb()) {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000127 // FIXME: 0xbf00 is the ARMv7 value. For v6 and before, we'll need to
128 // use 0x46c0 (which is a 'mov r8, r8' insn).
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000129 uint64_t NumNops = Count / 2;
130 for (uint64_t i = 0; i != NumNops; ++i)
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000131 OW->Write16(0xbf00);
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000132 if (Count & 1)
133 OW->Write8(0);
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000134 return true;
135 }
136 // ARM mode
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000137 uint64_t NumNops = Count / 4;
138 for (uint64_t i = 0; i != NumNops; ++i)
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000139 OW->Write32(0xe1a00000);
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000140 switch (Count % 4) {
141 default: break; // No leftover bytes to write
142 case 1: OW->Write8(0); break;
143 case 2: OW->Write16(0); break;
144 case 3: OW->Write16(0); OW->Write8(0xa0); break;
145 }
146
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000147 return true;
Jim Grosbach87dc3aa2010-09-30 03:20:34 +0000148}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000149
Jason W Kim0c628c22010-12-01 22:46:50 +0000150static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
151 switch (Kind) {
152 default:
153 llvm_unreachable("Unknown fixup kind!");
Jim Grosbach6ec6eeb2010-12-17 18:39:10 +0000154 case FK_Data_1:
155 case FK_Data_2:
Jason W Kim0c628c22010-12-01 22:46:50 +0000156 case FK_Data_4:
Jason W Kim0c628c22010-12-01 22:46:50 +0000157 return Value;
Jason W Kim2ccf1482010-12-03 19:40:23 +0000158 case ARM::fixup_arm_movt_hi16:
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000159 Value >>= 16;
160 // Fallthrough
161 case ARM::fixup_arm_movw_lo16:
Jason W Kim861b9c62011-05-19 20:55:25 +0000162 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim86a97f22011-01-12 00:19:25 +0000163 case ARM::fixup_arm_movw_lo16_pcrel: {
Jason W Kim2ccf1482010-12-03 19:40:23 +0000164 unsigned Hi4 = (Value & 0xF000) >> 12;
165 unsigned Lo12 = Value & 0x0FFF;
Jason W Kim861b9c62011-05-19 20:55:25 +0000166 assert ((((int64_t)Value) >= -0x8000) && (((int64_t)Value) <= 0x7fff) &&
167 "Out of range pc-relative fixup value!");
Jason W Kim2ccf1482010-12-03 19:40:23 +0000168 // inst{19-16} = Hi4;
169 // inst{11-0} = Lo12;
170 Value = (Hi4 << 16) | (Lo12);
171 return Value;
172 }
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000173 case ARM::fixup_t2_movt_hi16:
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000174 Value >>= 16;
175 // Fallthrough
176 case ARM::fixup_t2_movw_lo16:
Jason W Kim861b9c62011-05-19 20:55:25 +0000177 case ARM::fixup_t2_movt_hi16_pcrel:
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;
Jason W Kim861b9c62011-05-19 20:55:25 +0000187 assert ((((int64_t)Value) >= -0x8000) && (((int64_t)Value) <= 0x7fff) &&
188 "Out of range pc-relative fixup value!");
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000189 Value = (Hi4 << 16) | (i << 26) | (Mid3 << 12) | (Lo8);
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000190 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
191 swapped |= (Value & 0x0000FFFF) << 16;
192 return swapped;
193 }
Owen Andersond7b3f582010-12-09 01:51:07 +0000194 case ARM::fixup_arm_ldst_pcrel_12:
Jason W Kim0c628c22010-12-01 22:46:50 +0000195 // ARM PC-relative values are offset by 8.
Owen Anderson05018c22010-12-09 20:27:52 +0000196 Value -= 4;
Owen Andersonfe7fac72010-12-09 21:34:47 +0000197 // FALLTHROUGH
Owen Andersond7b3f582010-12-09 01:51:07 +0000198 case ARM::fixup_t2_ldst_pcrel_12: {
199 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Anderson05018c22010-12-09 20:27:52 +0000200 Value -= 4;
Owen Andersond7b3f582010-12-09 01:51:07 +0000201 bool isAdd = true;
Jason W Kim0c628c22010-12-01 22:46:50 +0000202 if ((int64_t)Value < 0) {
203 Value = -Value;
204 isAdd = false;
205 }
206 assert ((Value < 4096) && "Out of range pc-relative fixup value!");
207 Value |= isAdd << 23;
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000208
Owen Andersond7b3f582010-12-09 01:51:07 +0000209 // Same addressing mode as fixup_arm_pcrel_10,
210 // but with 16-bit halfwords swapped.
211 if (Kind == ARM::fixup_t2_ldst_pcrel_12) {
212 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
213 swapped |= (Value & 0x0000FFFF) << 16;
214 return swapped;
215 }
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000216
Jason W Kim0c628c22010-12-01 22:46:50 +0000217 return Value;
218 }
Jim Grosbachd40963c2010-12-14 22:28:03 +0000219 case ARM::fixup_thumb_adr_pcrel_10:
220 return ((Value - 4) >> 2) & 0xff;
Jim Grosbachdff84b02010-12-02 00:28:45 +0000221 case ARM::fixup_arm_adr_pcrel_12: {
222 // ARM PC-relative values are offset by 8.
223 Value -= 8;
224 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
225 if ((int64_t)Value < 0) {
226 Value = -Value;
227 opc = 2; // 0b0010
228 }
229 assert(ARM_AM::getSOImmVal(Value) != -1 &&
230 "Out of range pc-relative fixup value!");
231 // Encode the immediate and shift the opcode into place.
232 return ARM_AM::getSOImmVal(Value) | (opc << 21);
233 }
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000234
Owen Andersona838a252010-12-14 00:36:49 +0000235 case ARM::fixup_t2_adr_pcrel_12: {
236 Value -= 4;
237 unsigned opc = 0;
238 if ((int64_t)Value < 0) {
239 Value = -Value;
240 opc = 5;
241 }
242
243 uint32_t out = (opc << 21);
Owen Anderson741ad152011-03-23 22:03:44 +0000244 out |= (Value & 0x800) << 15;
Owen Andersona838a252010-12-14 00:36:49 +0000245 out |= (Value & 0x700) << 4;
246 out |= (Value & 0x0FF);
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000247
Owen Andersona838a252010-12-14 00:36:49 +0000248 uint64_t swapped = (out & 0xFFFF0000) >> 16;
249 swapped |= (out & 0x0000FFFF) << 16;
250 return swapped;
251 }
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000252
Jason W Kim685c3502011-02-04 19:47:15 +0000253 case ARM::fixup_arm_condbranch:
254 case ARM::fixup_arm_uncondbranch:
Jason W Kim0c628c22010-12-01 22:46:50 +0000255 // These values don't encode the low two bits since they're always zero.
256 // Offset by 8 just as above.
Jim Grosbach662a8162010-12-06 23:57:07 +0000257 return 0xffffff & ((Value - 8) >> 2);
Owen Andersonc2666002010-12-13 19:31:11 +0000258 case ARM::fixup_t2_uncondbranch: {
Owen Anderson63ee2202010-12-10 23:02:28 +0000259 Value = Value - 4;
Owen Andersonfb20d892010-12-09 00:27:41 +0000260 Value >>= 1; // Low bit is not encoded.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000261
Jim Grosbach56a25352010-12-13 19:25:46 +0000262 uint32_t out = 0;
Owen Andersonc2666002010-12-13 19:31:11 +0000263 bool I = Value & 0x800000;
264 bool J1 = Value & 0x400000;
265 bool J2 = Value & 0x200000;
266 J1 ^= I;
267 J2 ^= I;
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000268
Owen Andersonc2666002010-12-13 19:31:11 +0000269 out |= I << 26; // S bit
270 out |= !J1 << 13; // J1 bit
271 out |= !J2 << 11; // J2 bit
272 out |= (Value & 0x1FF800) << 5; // imm6 field
273 out |= (Value & 0x0007FF); // imm11 field
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000274
Owen Andersonc2666002010-12-13 19:31:11 +0000275 uint64_t swapped = (out & 0xFFFF0000) >> 16;
276 swapped |= (out & 0x0000FFFF) << 16;
277 return swapped;
278 }
279 case ARM::fixup_t2_condbranch: {
280 Value = Value - 4;
281 Value >>= 1; // Low bit is not encoded.
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000282
Owen Andersonc2666002010-12-13 19:31:11 +0000283 uint64_t out = 0;
Owen Anderson8f079432010-12-09 01:02:09 +0000284 out |= (Value & 0x80000) << 7; // S bit
285 out |= (Value & 0x40000) >> 7; // J2 bit
286 out |= (Value & 0x20000) >> 4; // J1 bit
287 out |= (Value & 0x1F800) << 5; // imm6 field
288 out |= (Value & 0x007FF); // imm11 field
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000289
Jim Grosbach56a25352010-12-13 19:25:46 +0000290 uint32_t swapped = (out & 0xFFFF0000) >> 16;
Owen Andersonfb20d892010-12-09 00:27:41 +0000291 swapped |= (out & 0x0000FFFF) << 16;
292 return swapped;
293 }
Jim Grosbach662a8162010-12-06 23:57:07 +0000294 case ARM::fixup_arm_thumb_bl: {
295 // The value doesn't encode the low bit (always zero) and is offset by
296 // four. The value is encoded into disjoint bit positions in the destination
297 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000298 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000299 // BL: xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000300 //
Jim Grosbach662a8162010-12-06 23:57:07 +0000301 // Note that the halfwords are stored high first, low second; so we need
302 // to transpose the fixup value here to map properly.
Rafael Espindola298c8e12011-05-20 20:01:01 +0000303 unsigned isNeg = (int64_t(Value - 4) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000304 uint32_t Binary = 0;
305 Value = 0x3fffff & ((Value - 4) >> 1);
306 Binary = (Value & 0x7ff) << 16; // Low imm11 value.
307 Binary |= (Value & 0x1ffc00) >> 11; // High imm10 value.
308 Binary |= isNeg << 10; // Sign bit.
Bill Wendling09aa3f02010-12-09 00:39:08 +0000309 return Binary;
310 }
311 case ARM::fixup_arm_thumb_blx: {
312 // The value doesn't encode the low two bits (always zero) and is offset by
313 // four (see fixup_arm_thumb_cp). The value is encoded into disjoint bit
314 // positions in the destination opcode. x = unchanged, I = immediate value
315 // bit, S = sign extension bit, 0 = zero.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000316 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000317 // BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000318 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000319 // Note that the halfwords are stored high first, low second; so we need
320 // to transpose the fixup value here to map properly.
Rafael Espindola298c8e12011-05-20 20:01:01 +0000321 unsigned isNeg = (int64_t(Value-4) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000322 uint32_t Binary = 0;
323 Value = 0xfffff & ((Value - 2) >> 2);
324 Binary = (Value & 0x3ff) << 17; // Low imm10L value.
325 Binary |= (Value & 0xffc00) >> 10; // High imm10H value.
326 Binary |= isNeg << 10; // Sign bit.
Jim Grosbach662a8162010-12-06 23:57:07 +0000327 return Binary;
328 }
Bill Wendlingb8958b02010-12-08 01:57:09 +0000329 case ARM::fixup_arm_thumb_cp:
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000330 // Offset by 4, and don't encode the low two bits. Two bytes of that
331 // 'off by 4' is implicitly handled by the half-word ordering of the
332 // Thumb encoding, so we only need to adjust by 2 here.
333 return ((Value - 2) >> 2) & 0xff;
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000334 case ARM::fixup_arm_thumb_cb: {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000335 // Offset by 4 and don't encode the lower bit, which is always 0.
336 uint32_t Binary = (Value - 4) >> 1;
Owen Anderson86abd482010-12-14 19:42:53 +0000337 return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000338 }
Jim Grosbache2467172010-12-10 18:21:33 +0000339 case ARM::fixup_arm_thumb_br:
340 // Offset by 4 and don't encode the lower bit, which is always 0.
341 return ((Value - 4) >> 1) & 0x7ff;
Jim Grosbach01086452010-12-10 17:13:40 +0000342 case ARM::fixup_arm_thumb_bcc:
343 // Offset by 4 and don't encode the lower bit, which is always 0.
344 return ((Value - 4) >> 1) & 0xff;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000345 case ARM::fixup_arm_pcrel_10:
Owen Andersone2e0f582010-12-10 22:46:47 +0000346 Value = Value - 4; // ARM fixups offset by an additional word and don't
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000347 // need to adjust for the half-word ordering.
348 // Fall through.
349 case ARM::fixup_t2_pcrel_10: {
350 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Andersone2e0f582010-12-10 22:46:47 +0000351 Value = Value - 4;
Jason W Kim0c628c22010-12-01 22:46:50 +0000352 bool isAdd = true;
353 if ((int64_t)Value < 0) {
354 Value = -Value;
355 isAdd = false;
356 }
357 // These values don't encode the low two bits since they're always zero.
358 Value >>= 2;
359 assert ((Value < 256) && "Out of range pc-relative fixup value!");
360 Value |= isAdd << 23;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000361
Owen Andersoncc78f5c2010-12-08 19:31:11 +0000362 // Same addressing mode as fixup_arm_pcrel_10,
363 // but with 16-bit halfwords swapped.
Owen Andersond8e351b2010-12-08 00:18:36 +0000364 if (Kind == ARM::fixup_t2_pcrel_10) {
Jim Grosbach56a25352010-12-13 19:25:46 +0000365 uint32_t swapped = (Value & 0xFFFF0000) >> 16;
Owen Anderson255eafb2010-12-08 00:21:33 +0000366 swapped |= (Value & 0x0000FFFF) << 16;
Owen Andersond8e351b2010-12-08 00:18:36 +0000367 return swapped;
368 }
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000369
Jason W Kim0c628c22010-12-01 22:46:50 +0000370 return Value;
371 }
372 }
373}
374
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000375namespace {
Bill Wendling52e635e2010-12-07 23:05:20 +0000376
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000377// FIXME: This should be in a separate file.
378// ELF is an ELF of course...
379class ELFARMAsmBackend : public ARMAsmBackend {
380public:
381 Triple::OSType OSType;
382 ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
Daniel Dunbar7b62afa2010-12-17 02:06:08 +0000383 : ARMAsmBackend(T), OSType(_OSType) { }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000384
Rafael Espindola179821a2010-12-06 19:08:48 +0000385 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000386 uint64_t Value) const;
387
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000388 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Rafael Espindolabff66a82010-12-18 03:27:34 +0000389 return createELFObjectWriter(new ARMELFObjectWriter(OSType), OS,
390 /*IsLittleEndian*/ true);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000391 }
392};
393
Bill Wendling52e635e2010-12-07 23:05:20 +0000394// FIXME: Raise this to share code between Darwin and ELF.
Rafael Espindola179821a2010-12-06 19:08:48 +0000395void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
396 unsigned DataSize, uint64_t Value) const {
Bill Wendling52e635e2010-12-07 23:05:20 +0000397 unsigned NumBytes = 4; // FIXME: 2 for Thumb
Bill Wendling52e635e2010-12-07 23:05:20 +0000398 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000399 if (!Value) return; // Doesn't change encoding.
Bill Wendling52e635e2010-12-07 23:05:20 +0000400
401 unsigned Offset = Fixup.getOffset();
Bill Wendling52e635e2010-12-07 23:05:20 +0000402
403 // For each byte of the fragment that the fixup touches, mask in the bits from
404 // the fixup value. The Value has been "split up" into the appropriate
405 // bitfields above.
406 for (unsigned i = 0; i != NumBytes; ++i)
407 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000408}
409
410// FIXME: This should be in a separate file.
411class DarwinARMAsmBackend : public ARMAsmBackend {
412public:
Owen Anderson17213242011-04-01 21:07:39 +0000413 const object::mach::CPUSubtypeARM Subtype;
414 DarwinARMAsmBackend(const Target &T, object::mach::CPUSubtypeARM st)
415 : ARMAsmBackend(T), Subtype(st) { }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000416
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000417 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jim Grosbach2fc68982011-06-22 20:14:52 +0000418 return createARMMachObjectWriter(OS, /*Is64Bit=*/false,
419 object::mach::CTM_ARM,
420 Subtype);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000421 }
422
Owen Anderson17213242011-04-01 21:07:39 +0000423 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
424 uint64_t Value) const;
425
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000426 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
427 return false;
428 }
429};
430
Bill Wendlingd832fa02010-12-07 23:11:00 +0000431/// getFixupKindNumBytes - The number of bytes the fixup may change.
Jim Grosbachc466b932010-11-11 18:04:49 +0000432static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000433 switch (Kind) {
Jim Grosbach662a8162010-12-06 23:57:07 +0000434 default:
435 llvm_unreachable("Unknown fixup kind!");
Bill Wendlingb8958b02010-12-08 01:57:09 +0000436
Jim Grosbach6ec6eeb2010-12-17 18:39:10 +0000437 case FK_Data_1:
Jim Grosbach01086452010-12-10 17:13:40 +0000438 case ARM::fixup_arm_thumb_bcc:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000439 case ARM::fixup_arm_thumb_cp:
Jim Grosbachd40963c2010-12-14 22:28:03 +0000440 case ARM::fixup_thumb_adr_pcrel_10:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000441 return 1;
442
Jim Grosbach6ec6eeb2010-12-17 18:39:10 +0000443 case FK_Data_2:
Jim Grosbache2467172010-12-10 18:21:33 +0000444 case ARM::fixup_arm_thumb_br:
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000445 case ARM::fixup_arm_thumb_cb:
Bill Wendlingdff2f712010-12-08 23:01:43 +0000446 return 2;
447
Jim Grosbach662a8162010-12-06 23:57:07 +0000448 case ARM::fixup_arm_ldst_pcrel_12:
449 case ARM::fixup_arm_pcrel_10:
450 case ARM::fixup_arm_adr_pcrel_12:
Jason W Kim685c3502011-02-04 19:47:15 +0000451 case ARM::fixup_arm_condbranch:
452 case ARM::fixup_arm_uncondbranch:
Jim Grosbach662a8162010-12-06 23:57:07 +0000453 return 3;
Bill Wendlingb8958b02010-12-08 01:57:09 +0000454
455 case FK_Data_4:
Owen Andersond7b3f582010-12-09 01:51:07 +0000456 case ARM::fixup_t2_ldst_pcrel_12:
Owen Andersonc2666002010-12-13 19:31:11 +0000457 case ARM::fixup_t2_condbranch:
458 case ARM::fixup_t2_uncondbranch:
Owen Andersond8e351b2010-12-08 00:18:36 +0000459 case ARM::fixup_t2_pcrel_10:
Owen Andersona838a252010-12-14 00:36:49 +0000460 case ARM::fixup_t2_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +0000461 case ARM::fixup_arm_thumb_bl:
Bill Wendling09aa3f02010-12-09 00:39:08 +0000462 case ARM::fixup_arm_thumb_blx:
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000463 case ARM::fixup_arm_movt_hi16:
464 case ARM::fixup_arm_movw_lo16:
465 case ARM::fixup_arm_movt_hi16_pcrel:
466 case ARM::fixup_arm_movw_lo16_pcrel:
467 case ARM::fixup_t2_movt_hi16:
468 case ARM::fixup_t2_movw_lo16:
469 case ARM::fixup_t2_movt_hi16_pcrel:
470 case ARM::fixup_t2_movw_lo16_pcrel:
Jim Grosbach662a8162010-12-06 23:57:07 +0000471 return 4;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000472 }
473}
474
Rafael Espindola179821a2010-12-06 19:08:48 +0000475void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
476 unsigned DataSize, uint64_t Value) const {
Jim Grosbachc466b932010-11-11 18:04:49 +0000477 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Jim Grosbach679cbd32010-11-09 01:37:15 +0000478 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000479 if (!Value) return; // Doesn't change encoding.
Jim Grosbach679cbd32010-11-09 01:37:15 +0000480
Bill Wendlingd832fa02010-12-07 23:11:00 +0000481 unsigned Offset = Fixup.getOffset();
482 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
483
Jim Grosbach679cbd32010-11-09 01:37:15 +0000484 // For each byte of the fragment that the fixup touches, mask in the
485 // bits from the fixup value.
486 for (unsigned i = 0; i != NumBytes; ++i)
Bill Wendlingd832fa02010-12-07 23:11:00 +0000487 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000488}
Bill Wendling52e635e2010-12-07 23:05:20 +0000489
Jim Grosbachf73fd722010-09-30 03:21:00 +0000490} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000491
492TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
493 const std::string &TT) {
Owen Anderson17213242011-04-01 21:07:39 +0000494 Triple TheTriple(TT);
Daniel Dunbar912225e2011-04-19 21:14:45 +0000495
496 if (TheTriple.isOSDarwin()) {
Evan Chenga6eb2562011-06-14 18:08:33 +0000497 if (TheTriple.getArchName() == "armv4t" ||
498 TheTriple.getArchName() == "thumbv4t")
499 return new DarwinARMAsmBackend(T, object::mach::CSARM_V4T);
500 else if (TheTriple.getArchName() == "armv5e" ||
501 TheTriple.getArchName() == "thumbv5e")
502 return new DarwinARMAsmBackend(T, object::mach::CSARM_V5TEJ);
503 else if (TheTriple.getArchName() == "armv6" ||
Owen Anderson17213242011-04-01 21:07:39 +0000504 TheTriple.getArchName() == "thumbv6")
505 return new DarwinARMAsmBackend(T, object::mach::CSARM_V6);
506 return new DarwinARMAsmBackend(T, object::mach::CSARM_V7);
507 }
Daniel Dunbar912225e2011-04-19 21:14:45 +0000508
509 if (TheTriple.isOSWindows())
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000510 assert(0 && "Windows not supported on ARM");
Daniel Dunbar912225e2011-04-19 21:14:45 +0000511
512 return new ELFARMAsmBackend(T, Triple(TT).getOS());
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000513}