blob: ec23449d7d42238cee5dc1d5120b7dfeaea6aaba [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 {
Daniel Dunbarae5abd52010-12-16 16:09:19 +000031class ARMMachObjectWriter : public MCMachObjectTargetWriter {
Daniel Dunbar5d05d972010-12-16 17:21:02 +000032public:
33 ARMMachObjectWriter(bool Is64Bit, uint32_t CPUType,
34 uint32_t CPUSubtype)
Daniel Dunbar1139d502010-12-17 06:00:24 +000035 : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype,
36 /*UseAggressiveSymbolFolding=*/true) {}
Daniel Dunbarae5abd52010-12-16 16:09:19 +000037};
38
Rafael Espindola6024c972010-12-17 17:45:22 +000039class ARMELFObjectWriter : public MCELFObjectTargetWriter {
40public:
Rafael Espindolabff66a82010-12-18 03:27:34 +000041 ARMELFObjectWriter(Triple::OSType OSType)
42 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSType, ELF::EM_ARM,
43 /*HasRelocationAddend*/ false) {}
Rafael Espindola6024c972010-12-17 17:45:22 +000044};
45
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000046class ARMAsmBackend : public TargetAsmBackend {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000047 bool isThumbMode; // Currently emitting Thumb code.
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000048public:
Jim Grosbach022ab372010-12-08 15:36:45 +000049 ARMAsmBackend(const Target &T) : TargetAsmBackend(), isThumbMode(false) {}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000050
Daniel Dunbar2761fc42010-12-16 03:20:06 +000051 unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
52
53 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
54 const static MCFixupKindInfo Infos[ARM::NumTargetFixupKinds] = {
55// This table *must* be in the order that the fixup_* kinds are defined in
56// ARMFixupKinds.h.
57//
58// Name Offset (bits) Size (bits) Flags
59{ "fixup_arm_ldst_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
60{ "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
61 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
62{ "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
63{ "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
64 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
65{ "fixup_thumb_adr_pcrel_10",0, 8, MCFixupKindInfo::FKF_IsPCRel |
66 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
67{ "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
68{ "fixup_t2_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
69 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Jason W Kim685c3502011-02-04 19:47:15 +000070{ "fixup_arm_condbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
71{ "fixup_arm_uncondbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar2761fc42010-12-16 03:20:06 +000072{ "fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
73{ "fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
74{ "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
75{ "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
76{ "fixup_arm_thumb_blx", 7, 21, MCFixupKindInfo::FKF_IsPCRel },
77{ "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
78{ "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
79{ "fixup_arm_thumb_bcc", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
Evan Chengf3eb3bb2011-01-14 02:38:49 +000080// movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16 - 19.
81{ "fixup_arm_movt_hi16", 0, 20, 0 },
82{ "fixup_arm_movw_lo16", 0, 20, 0 },
83{ "fixup_t2_movt_hi16", 0, 20, 0 },
84{ "fixup_t2_movw_lo16", 0, 20, 0 },
85{ "fixup_arm_movt_hi16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
86{ "fixup_arm_movw_lo16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
87{ "fixup_t2_movt_hi16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
88{ "fixup_t2_movw_lo16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar2761fc42010-12-16 03:20:06 +000089 };
90
91 if (Kind < FirstTargetFixupKind)
92 return TargetAsmBackend::getFixupKindInfo(Kind);
93
94 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
95 "Invalid kind!");
96 return Infos[Kind - FirstTargetFixupKind];
97 }
98
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000099 bool MayNeedRelaxation(const MCInst &Inst) const;
100
101 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
102
103 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
Jim Grosbach3787a402010-09-30 17:45:51 +0000104
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000105 void HandleAssemblerFlag(MCAssemblerFlag Flag) {
106 switch (Flag) {
107 default: break;
108 case MCAF_Code16:
109 setIsThumb(true);
110 break;
111 case MCAF_Code32:
112 setIsThumb(false);
113 break;
114 }
Jim Grosbach3787a402010-09-30 17:45:51 +0000115 }
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000116
117 unsigned getPointerSize() const { return 4; }
118 bool isThumb() const { return isThumbMode; }
119 void setIsThumb(bool it) { isThumbMode = it; }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000120};
Chris Lattnerb75c6512010-11-17 05:41:32 +0000121} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000122
123bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
124 // FIXME: Thumb targets, different move constant targets..
125 return false;
126}
127
128void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
129 assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
130 return;
131}
132
133bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000134 if (isThumb()) {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000135 // FIXME: 0xbf00 is the ARMv7 value. For v6 and before, we'll need to
136 // use 0x46c0 (which is a 'mov r8, r8' insn).
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000137 uint64_t NumNops = Count / 2;
138 for (uint64_t i = 0; i != NumNops; ++i)
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000139 OW->Write16(0xbf00);
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000140 if (Count & 1)
141 OW->Write8(0);
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000142 return true;
143 }
144 // ARM mode
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000145 uint64_t NumNops = Count / 4;
146 for (uint64_t i = 0; i != NumNops; ++i)
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000147 OW->Write32(0xe1a00000);
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000148 switch (Count % 4) {
149 default: break; // No leftover bytes to write
150 case 1: OW->Write8(0); break;
151 case 2: OW->Write16(0); break;
152 case 3: OW->Write16(0); OW->Write8(0xa0); break;
153 }
154
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000155 return true;
Jim Grosbach87dc3aa2010-09-30 03:20:34 +0000156}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000157
Jason W Kim0c628c22010-12-01 22:46:50 +0000158static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
159 switch (Kind) {
160 default:
161 llvm_unreachable("Unknown fixup kind!");
Jim Grosbach6ec6eeb2010-12-17 18:39:10 +0000162 case FK_Data_1:
163 case FK_Data_2:
Jason W Kim0c628c22010-12-01 22:46:50 +0000164 case FK_Data_4:
Jason W Kim0c628c22010-12-01 22:46:50 +0000165 return Value;
Jason W Kim2ccf1482010-12-03 19:40:23 +0000166 case ARM::fixup_arm_movt_hi16:
Jason W Kim86a97f22011-01-12 00:19:25 +0000167 case ARM::fixup_arm_movt_hi16_pcrel:
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000168 Value >>= 16;
169 // Fallthrough
170 case ARM::fixup_arm_movw_lo16:
Jason W Kim86a97f22011-01-12 00:19:25 +0000171 case ARM::fixup_arm_movw_lo16_pcrel: {
Jason W Kim2ccf1482010-12-03 19:40:23 +0000172 unsigned Hi4 = (Value & 0xF000) >> 12;
173 unsigned Lo12 = Value & 0x0FFF;
174 // inst{19-16} = Hi4;
175 // inst{11-0} = Lo12;
176 Value = (Hi4 << 16) | (Lo12);
177 return Value;
178 }
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000179 case ARM::fixup_t2_movt_hi16:
180 case ARM::fixup_t2_movt_hi16_pcrel:
181 Value >>= 16;
182 // Fallthrough
183 case ARM::fixup_t2_movw_lo16:
184 case ARM::fixup_t2_movw_lo16_pcrel: {
185 unsigned Hi4 = (Value & 0xF000) >> 12;
186 unsigned i = (Value & 0x800) >> 11;
187 unsigned Mid3 = (Value & 0x700) >> 8;
188 unsigned Lo8 = Value & 0x0FF;
189 // inst{19-16} = Hi4;
190 // inst{26} = i;
191 // inst{14-12} = Mid3;
192 // inst{7-0} = Lo8;
193 Value = (Hi4 << 16) | (i << 26) | (Mid3 << 12) | (Lo8);
194
195 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
196 swapped |= (Value & 0x0000FFFF) << 16;
197 return swapped;
198 }
Owen Andersond7b3f582010-12-09 01:51:07 +0000199 case ARM::fixup_arm_ldst_pcrel_12:
Jason W Kim0c628c22010-12-01 22:46:50 +0000200 // ARM PC-relative values are offset by 8.
Owen Anderson05018c22010-12-09 20:27:52 +0000201 Value -= 4;
Owen Andersonfe7fac72010-12-09 21:34:47 +0000202 // FALLTHROUGH
Owen Andersond7b3f582010-12-09 01:51:07 +0000203 case ARM::fixup_t2_ldst_pcrel_12: {
204 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Anderson05018c22010-12-09 20:27:52 +0000205 Value -= 4;
Owen Andersond7b3f582010-12-09 01:51:07 +0000206 bool isAdd = true;
Jason W Kim0c628c22010-12-01 22:46:50 +0000207 if ((int64_t)Value < 0) {
208 Value = -Value;
209 isAdd = false;
210 }
211 assert ((Value < 4096) && "Out of range pc-relative fixup value!");
212 Value |= isAdd << 23;
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000213
Owen Andersond7b3f582010-12-09 01:51:07 +0000214 // Same addressing mode as fixup_arm_pcrel_10,
215 // but with 16-bit halfwords swapped.
216 if (Kind == ARM::fixup_t2_ldst_pcrel_12) {
217 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
218 swapped |= (Value & 0x0000FFFF) << 16;
219 return swapped;
220 }
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000221
Jason W Kim0c628c22010-12-01 22:46:50 +0000222 return Value;
223 }
Jim Grosbachd40963c2010-12-14 22:28:03 +0000224 case ARM::fixup_thumb_adr_pcrel_10:
225 return ((Value - 4) >> 2) & 0xff;
Jim Grosbachdff84b02010-12-02 00:28:45 +0000226 case ARM::fixup_arm_adr_pcrel_12: {
227 // ARM PC-relative values are offset by 8.
228 Value -= 8;
229 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
230 if ((int64_t)Value < 0) {
231 Value = -Value;
232 opc = 2; // 0b0010
233 }
234 assert(ARM_AM::getSOImmVal(Value) != -1 &&
235 "Out of range pc-relative fixup value!");
236 // Encode the immediate and shift the opcode into place.
237 return ARM_AM::getSOImmVal(Value) | (opc << 21);
238 }
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000239
Owen Andersona838a252010-12-14 00:36:49 +0000240 case ARM::fixup_t2_adr_pcrel_12: {
241 Value -= 4;
242 unsigned opc = 0;
243 if ((int64_t)Value < 0) {
244 Value = -Value;
245 opc = 5;
246 }
247
248 uint32_t out = (opc << 21);
249 out |= (Value & 0x800) << 14;
250 out |= (Value & 0x700) << 4;
251 out |= (Value & 0x0FF);
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000252
Owen Andersona838a252010-12-14 00:36:49 +0000253 uint64_t swapped = (out & 0xFFFF0000) >> 16;
254 swapped |= (out & 0x0000FFFF) << 16;
255 return swapped;
256 }
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000257
Jason W Kim685c3502011-02-04 19:47:15 +0000258 case ARM::fixup_arm_condbranch:
259 case ARM::fixup_arm_uncondbranch:
Jason W Kim0c628c22010-12-01 22:46:50 +0000260 // These values don't encode the low two bits since they're always zero.
261 // Offset by 8 just as above.
Jim Grosbach662a8162010-12-06 23:57:07 +0000262 return 0xffffff & ((Value - 8) >> 2);
Owen Andersonc2666002010-12-13 19:31:11 +0000263 case ARM::fixup_t2_uncondbranch: {
Owen Anderson63ee2202010-12-10 23:02:28 +0000264 Value = Value - 4;
Owen Andersonfb20d892010-12-09 00:27:41 +0000265 Value >>= 1; // Low bit is not encoded.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000266
Jim Grosbach56a25352010-12-13 19:25:46 +0000267 uint32_t out = 0;
Owen Andersonc2666002010-12-13 19:31:11 +0000268 bool I = Value & 0x800000;
269 bool J1 = Value & 0x400000;
270 bool J2 = Value & 0x200000;
271 J1 ^= I;
272 J2 ^= I;
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000273
Owen Andersonc2666002010-12-13 19:31:11 +0000274 out |= I << 26; // S bit
275 out |= !J1 << 13; // J1 bit
276 out |= !J2 << 11; // J2 bit
277 out |= (Value & 0x1FF800) << 5; // imm6 field
278 out |= (Value & 0x0007FF); // imm11 field
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000279
Owen Andersonc2666002010-12-13 19:31:11 +0000280 uint64_t swapped = (out & 0xFFFF0000) >> 16;
281 swapped |= (out & 0x0000FFFF) << 16;
282 return swapped;
283 }
284 case ARM::fixup_t2_condbranch: {
285 Value = Value - 4;
286 Value >>= 1; // Low bit is not encoded.
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000287
Owen Andersonc2666002010-12-13 19:31:11 +0000288 uint64_t out = 0;
Owen Anderson8f079432010-12-09 01:02:09 +0000289 out |= (Value & 0x80000) << 7; // S bit
290 out |= (Value & 0x40000) >> 7; // J2 bit
291 out |= (Value & 0x20000) >> 4; // J1 bit
292 out |= (Value & 0x1F800) << 5; // imm6 field
293 out |= (Value & 0x007FF); // imm11 field
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000294
Jim Grosbach56a25352010-12-13 19:25:46 +0000295 uint32_t swapped = (out & 0xFFFF0000) >> 16;
Owen Andersonfb20d892010-12-09 00:27:41 +0000296 swapped |= (out & 0x0000FFFF) << 16;
297 return swapped;
298 }
Jim Grosbach662a8162010-12-06 23:57:07 +0000299 case ARM::fixup_arm_thumb_bl: {
300 // The value doesn't encode the low bit (always zero) and is offset by
301 // four. The value is encoded into disjoint bit positions in the destination
302 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000303 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000304 // BL: xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000305 //
Jim Grosbach662a8162010-12-06 23:57:07 +0000306 // Note that the halfwords are stored high first, low second; so we need
307 // to transpose the fixup value here to map properly.
Bill Wendling09aa3f02010-12-09 00:39:08 +0000308 unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000309 uint32_t Binary = 0;
310 Value = 0x3fffff & ((Value - 4) >> 1);
311 Binary = (Value & 0x7ff) << 16; // Low imm11 value.
312 Binary |= (Value & 0x1ffc00) >> 11; // High imm10 value.
313 Binary |= isNeg << 10; // Sign bit.
Bill Wendling09aa3f02010-12-09 00:39:08 +0000314 return Binary;
315 }
316 case ARM::fixup_arm_thumb_blx: {
317 // The value doesn't encode the low two bits (always zero) and is offset by
318 // four (see fixup_arm_thumb_cp). The value is encoded into disjoint bit
319 // positions in the destination opcode. x = unchanged, I = immediate value
320 // bit, S = sign extension bit, 0 = zero.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000321 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000322 // BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000323 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000324 // Note that the halfwords are stored high first, low second; so we need
325 // to transpose the fixup value here to map properly.
326 unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000327 uint32_t Binary = 0;
328 Value = 0xfffff & ((Value - 2) >> 2);
329 Binary = (Value & 0x3ff) << 17; // Low imm10L value.
330 Binary |= (Value & 0xffc00) >> 10; // High imm10H value.
331 Binary |= isNeg << 10; // Sign bit.
Jim Grosbach662a8162010-12-06 23:57:07 +0000332 return Binary;
333 }
Bill Wendlingb8958b02010-12-08 01:57:09 +0000334 case ARM::fixup_arm_thumb_cp:
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000335 // Offset by 4, and don't encode the low two bits. Two bytes of that
336 // 'off by 4' is implicitly handled by the half-word ordering of the
337 // Thumb encoding, so we only need to adjust by 2 here.
338 return ((Value - 2) >> 2) & 0xff;
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000339 case ARM::fixup_arm_thumb_cb: {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000340 // Offset by 4 and don't encode the lower bit, which is always 0.
341 uint32_t Binary = (Value - 4) >> 1;
Owen Anderson86abd482010-12-14 19:42:53 +0000342 return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000343 }
Jim Grosbache2467172010-12-10 18:21:33 +0000344 case ARM::fixup_arm_thumb_br:
345 // Offset by 4 and don't encode the lower bit, which is always 0.
346 return ((Value - 4) >> 1) & 0x7ff;
Jim Grosbach01086452010-12-10 17:13:40 +0000347 case ARM::fixup_arm_thumb_bcc:
348 // Offset by 4 and don't encode the lower bit, which is always 0.
349 return ((Value - 4) >> 1) & 0xff;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000350 case ARM::fixup_arm_pcrel_10:
Owen Andersone2e0f582010-12-10 22:46:47 +0000351 Value = Value - 4; // ARM fixups offset by an additional word and don't
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000352 // need to adjust for the half-word ordering.
353 // Fall through.
354 case ARM::fixup_t2_pcrel_10: {
355 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Andersone2e0f582010-12-10 22:46:47 +0000356 Value = Value - 4;
Jason W Kim0c628c22010-12-01 22:46:50 +0000357 bool isAdd = true;
358 if ((int64_t)Value < 0) {
359 Value = -Value;
360 isAdd = false;
361 }
362 // These values don't encode the low two bits since they're always zero.
363 Value >>= 2;
364 assert ((Value < 256) && "Out of range pc-relative fixup value!");
365 Value |= isAdd << 23;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000366
Owen Andersoncc78f5c2010-12-08 19:31:11 +0000367 // Same addressing mode as fixup_arm_pcrel_10,
368 // but with 16-bit halfwords swapped.
Owen Andersond8e351b2010-12-08 00:18:36 +0000369 if (Kind == ARM::fixup_t2_pcrel_10) {
Jim Grosbach56a25352010-12-13 19:25:46 +0000370 uint32_t swapped = (Value & 0xFFFF0000) >> 16;
Owen Anderson255eafb2010-12-08 00:21:33 +0000371 swapped |= (Value & 0x0000FFFF) << 16;
Owen Andersond8e351b2010-12-08 00:18:36 +0000372 return swapped;
373 }
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000374
Jason W Kim0c628c22010-12-01 22:46:50 +0000375 return Value;
376 }
377 }
378}
379
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000380namespace {
Bill Wendling52e635e2010-12-07 23:05:20 +0000381
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000382// FIXME: This should be in a separate file.
383// ELF is an ELF of course...
384class ELFARMAsmBackend : public ARMAsmBackend {
385public:
386 Triple::OSType OSType;
387 ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
Daniel Dunbar7b62afa2010-12-17 02:06:08 +0000388 : ARMAsmBackend(T), OSType(_OSType) { }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000389
Rafael Espindola179821a2010-12-06 19:08:48 +0000390 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000391 uint64_t Value) const;
392
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000393 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Rafael Espindolabff66a82010-12-18 03:27:34 +0000394 return createELFObjectWriter(new ARMELFObjectWriter(OSType), OS,
395 /*IsLittleEndian*/ true);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000396 }
397};
398
Bill Wendling52e635e2010-12-07 23:05:20 +0000399// FIXME: Raise this to share code between Darwin and ELF.
Rafael Espindola179821a2010-12-06 19:08:48 +0000400void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
401 unsigned DataSize, uint64_t Value) const {
Bill Wendling52e635e2010-12-07 23:05:20 +0000402 unsigned NumBytes = 4; // FIXME: 2 for Thumb
Bill Wendling52e635e2010-12-07 23:05:20 +0000403 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000404 if (!Value) return; // Doesn't change encoding.
Bill Wendling52e635e2010-12-07 23:05:20 +0000405
406 unsigned Offset = Fixup.getOffset();
407 assert(Offset % NumBytes == 0 && "Offset mod NumBytes is nonzero!");
408
409 // For each byte of the fragment that the fixup touches, mask in the bits from
410 // the fixup value. The Value has been "split up" into the appropriate
411 // bitfields above.
412 for (unsigned i = 0; i != NumBytes; ++i)
413 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000414}
415
416// FIXME: This should be in a separate file.
417class DarwinARMAsmBackend : public ARMAsmBackend {
418public:
Daniel Dunbar7b62afa2010-12-17 02:06:08 +0000419 DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) { }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000420
Rafael Espindola179821a2010-12-06 19:08:48 +0000421 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000422 uint64_t Value) const;
423
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000424 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jim Grosbachc9d14392010-11-05 18:48:58 +0000425 // FIXME: Subtarget info should be derived. Force v7 for now.
Daniel Dunbar5d05d972010-12-16 17:21:02 +0000426 return createMachObjectWriter(new ARMMachObjectWriter(
427 /*Is64Bit=*/false,
428 object::mach::CTM_ARM,
429 object::mach::CSARM_V7),
430 OS,
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000431 /*IsLittleEndian=*/true);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000432 }
433
434 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
435 return false;
436 }
437};
438
Bill Wendlingd832fa02010-12-07 23:11:00 +0000439/// getFixupKindNumBytes - The number of bytes the fixup may change.
Jim Grosbachc466b932010-11-11 18:04:49 +0000440static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000441 switch (Kind) {
Jim Grosbach662a8162010-12-06 23:57:07 +0000442 default:
443 llvm_unreachable("Unknown fixup kind!");
Bill Wendlingb8958b02010-12-08 01:57:09 +0000444
Jim Grosbach6ec6eeb2010-12-17 18:39:10 +0000445 case FK_Data_1:
Jim Grosbach01086452010-12-10 17:13:40 +0000446 case ARM::fixup_arm_thumb_bcc:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000447 case ARM::fixup_arm_thumb_cp:
Jim Grosbachd40963c2010-12-14 22:28:03 +0000448 case ARM::fixup_thumb_adr_pcrel_10:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000449 return 1;
450
Jim Grosbach6ec6eeb2010-12-17 18:39:10 +0000451 case FK_Data_2:
Jim Grosbache2467172010-12-10 18:21:33 +0000452 case ARM::fixup_arm_thumb_br:
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000453 case ARM::fixup_arm_thumb_cb:
Bill Wendlingdff2f712010-12-08 23:01:43 +0000454 return 2;
455
Jim Grosbach662a8162010-12-06 23:57:07 +0000456 case ARM::fixup_arm_ldst_pcrel_12:
457 case ARM::fixup_arm_pcrel_10:
458 case ARM::fixup_arm_adr_pcrel_12:
Jason W Kim685c3502011-02-04 19:47:15 +0000459 case ARM::fixup_arm_condbranch:
460 case ARM::fixup_arm_uncondbranch:
Jim Grosbach662a8162010-12-06 23:57:07 +0000461 return 3;
Bill Wendlingb8958b02010-12-08 01:57:09 +0000462
463 case FK_Data_4:
Owen Andersond7b3f582010-12-09 01:51:07 +0000464 case ARM::fixup_t2_ldst_pcrel_12:
Owen Andersonc2666002010-12-13 19:31:11 +0000465 case ARM::fixup_t2_condbranch:
466 case ARM::fixup_t2_uncondbranch:
Owen Andersond8e351b2010-12-08 00:18:36 +0000467 case ARM::fixup_t2_pcrel_10:
Owen Andersona838a252010-12-14 00:36:49 +0000468 case ARM::fixup_t2_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +0000469 case ARM::fixup_arm_thumb_bl:
Bill Wendling09aa3f02010-12-09 00:39:08 +0000470 case ARM::fixup_arm_thumb_blx:
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000471 case ARM::fixup_arm_movt_hi16:
472 case ARM::fixup_arm_movw_lo16:
473 case ARM::fixup_arm_movt_hi16_pcrel:
474 case ARM::fixup_arm_movw_lo16_pcrel:
475 case ARM::fixup_t2_movt_hi16:
476 case ARM::fixup_t2_movw_lo16:
477 case ARM::fixup_t2_movt_hi16_pcrel:
478 case ARM::fixup_t2_movw_lo16_pcrel:
Jim Grosbach662a8162010-12-06 23:57:07 +0000479 return 4;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000480 }
481}
482
Rafael Espindola179821a2010-12-06 19:08:48 +0000483void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
484 unsigned DataSize, uint64_t Value) const {
Jim Grosbachc466b932010-11-11 18:04:49 +0000485 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Jim Grosbach679cbd32010-11-09 01:37:15 +0000486 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000487 if (!Value) return; // Doesn't change encoding.
Jim Grosbach679cbd32010-11-09 01:37:15 +0000488
Bill Wendlingd832fa02010-12-07 23:11:00 +0000489 unsigned Offset = Fixup.getOffset();
490 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
491
Jim Grosbach679cbd32010-11-09 01:37:15 +0000492 // For each byte of the fragment that the fixup touches, mask in the
493 // bits from the fixup value.
494 for (unsigned i = 0; i != NumBytes; ++i)
Bill Wendlingd832fa02010-12-07 23:11:00 +0000495 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000496}
Bill Wendling52e635e2010-12-07 23:05:20 +0000497
Jim Grosbachf73fd722010-09-30 03:21:00 +0000498} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000499
500TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
501 const std::string &TT) {
502 switch (Triple(TT).getOS()) {
503 case Triple::Darwin:
504 return new DarwinARMAsmBackend(T);
505 case Triple::MinGW32:
506 case Triple::Cygwin:
507 case Triple::Win32:
508 assert(0 && "Windows not supported on ARM");
509 default:
510 return new ELFARMAsmBackend(T, Triple(TT).getOS());
511 }
512}