blob: 0d5a9af1eba4582af22338895b8bf153be506ef0 [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},
70{ "fixup_arm_branch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
71{ "fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
72{ "fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
73{ "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
74{ "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
75{ "fixup_arm_thumb_blx", 7, 21, MCFixupKindInfo::FKF_IsPCRel },
76{ "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
77{ "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
78{ "fixup_arm_thumb_bcc", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
79{ "fixup_arm_movt_hi16", 0, 16, 0 },
80{ "fixup_arm_movw_lo16", 0, 16, 0 },
Jason W Kim86a97f22011-01-12 00:19:25 +000081{ "fixup_arm_movt_hi16_pcrel", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
82{ "fixup_arm_movw_lo16_pcrel", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar2761fc42010-12-16 03:20:06 +000083 };
84
85 if (Kind < FirstTargetFixupKind)
86 return TargetAsmBackend::getFixupKindInfo(Kind);
87
88 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
89 "Invalid kind!");
90 return Infos[Kind - FirstTargetFixupKind];
91 }
92
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000093 bool MayNeedRelaxation(const MCInst &Inst) const;
94
95 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
96
97 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
Jim Grosbach3787a402010-09-30 17:45:51 +000098
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000099 void HandleAssemblerFlag(MCAssemblerFlag Flag) {
100 switch (Flag) {
101 default: break;
102 case MCAF_Code16:
103 setIsThumb(true);
104 break;
105 case MCAF_Code32:
106 setIsThumb(false);
107 break;
108 }
Jim Grosbach3787a402010-09-30 17:45:51 +0000109 }
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000110
111 unsigned getPointerSize() const { return 4; }
112 bool isThumb() const { return isThumbMode; }
113 void setIsThumb(bool it) { isThumbMode = it; }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000114};
Chris Lattnerb75c6512010-11-17 05:41:32 +0000115} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000116
117bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
118 // FIXME: Thumb targets, different move constant targets..
119 return false;
120}
121
122void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
123 assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
124 return;
125}
126
127bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000128 if (isThumb()) {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000129 // FIXME: 0xbf00 is the ARMv7 value. For v6 and before, we'll need to
130 // use 0x46c0 (which is a 'mov r8, r8' insn).
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000131 uint64_t NumNops = Count / 2;
132 for (uint64_t i = 0; i != NumNops; ++i)
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000133 OW->Write16(0xbf00);
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000134 if (Count & 1)
135 OW->Write8(0);
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000136 return true;
137 }
138 // ARM mode
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000139 uint64_t NumNops = Count / 4;
140 for (uint64_t i = 0; i != NumNops; ++i)
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000141 OW->Write32(0xe1a00000);
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000142 switch (Count % 4) {
143 default: break; // No leftover bytes to write
144 case 1: OW->Write8(0); break;
145 case 2: OW->Write16(0); break;
146 case 3: OW->Write16(0); OW->Write8(0xa0); break;
147 }
148
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000149 return true;
Jim Grosbach87dc3aa2010-09-30 03:20:34 +0000150}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000151
Jason W Kim0c628c22010-12-01 22:46:50 +0000152static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
153 switch (Kind) {
154 default:
155 llvm_unreachable("Unknown fixup kind!");
Jim Grosbach6ec6eeb2010-12-17 18:39:10 +0000156 case FK_Data_1:
157 case FK_Data_2:
Jason W Kim0c628c22010-12-01 22:46:50 +0000158 case FK_Data_4:
Jason W Kim0c628c22010-12-01 22:46:50 +0000159 return Value;
Jason W Kim2ccf1482010-12-03 19:40:23 +0000160 case ARM::fixup_arm_movt_hi16:
Jason W Kim86a97f22011-01-12 00:19:25 +0000161 case ARM::fixup_arm_movw_lo16:
162 case ARM::fixup_arm_movt_hi16_pcrel:
163 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;
166 // inst{19-16} = Hi4;
167 // inst{11-0} = Lo12;
168 Value = (Hi4 << 16) | (Lo12);
169 return Value;
170 }
Owen Andersond7b3f582010-12-09 01:51:07 +0000171 case ARM::fixup_arm_ldst_pcrel_12:
Jason W Kim0c628c22010-12-01 22:46:50 +0000172 // ARM PC-relative values are offset by 8.
Owen Anderson05018c22010-12-09 20:27:52 +0000173 Value -= 4;
Owen Andersonfe7fac72010-12-09 21:34:47 +0000174 // FALLTHROUGH
Owen Andersond7b3f582010-12-09 01:51:07 +0000175 case ARM::fixup_t2_ldst_pcrel_12: {
176 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Anderson05018c22010-12-09 20:27:52 +0000177 Value -= 4;
Owen Andersond7b3f582010-12-09 01:51:07 +0000178 bool isAdd = true;
Jason W Kim0c628c22010-12-01 22:46:50 +0000179 if ((int64_t)Value < 0) {
180 Value = -Value;
181 isAdd = false;
182 }
183 assert ((Value < 4096) && "Out of range pc-relative fixup value!");
184 Value |= isAdd << 23;
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000185
Owen Andersond7b3f582010-12-09 01:51:07 +0000186 // Same addressing mode as fixup_arm_pcrel_10,
187 // but with 16-bit halfwords swapped.
188 if (Kind == ARM::fixup_t2_ldst_pcrel_12) {
189 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
190 swapped |= (Value & 0x0000FFFF) << 16;
191 return swapped;
192 }
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000193
Jason W Kim0c628c22010-12-01 22:46:50 +0000194 return Value;
195 }
Jim Grosbachd40963c2010-12-14 22:28:03 +0000196 case ARM::fixup_thumb_adr_pcrel_10:
197 return ((Value - 4) >> 2) & 0xff;
Jim Grosbachdff84b02010-12-02 00:28:45 +0000198 case ARM::fixup_arm_adr_pcrel_12: {
199 // ARM PC-relative values are offset by 8.
200 Value -= 8;
201 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
202 if ((int64_t)Value < 0) {
203 Value = -Value;
204 opc = 2; // 0b0010
205 }
206 assert(ARM_AM::getSOImmVal(Value) != -1 &&
207 "Out of range pc-relative fixup value!");
208 // Encode the immediate and shift the opcode into place.
209 return ARM_AM::getSOImmVal(Value) | (opc << 21);
210 }
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000211
Owen Andersona838a252010-12-14 00:36:49 +0000212 case ARM::fixup_t2_adr_pcrel_12: {
213 Value -= 4;
214 unsigned opc = 0;
215 if ((int64_t)Value < 0) {
216 Value = -Value;
217 opc = 5;
218 }
219
220 uint32_t out = (opc << 21);
221 out |= (Value & 0x800) << 14;
222 out |= (Value & 0x700) << 4;
223 out |= (Value & 0x0FF);
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000224
Owen Andersona838a252010-12-14 00:36:49 +0000225 uint64_t swapped = (out & 0xFFFF0000) >> 16;
226 swapped |= (out & 0x0000FFFF) << 16;
227 return swapped;
228 }
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000229
Jason W Kim0c628c22010-12-01 22:46:50 +0000230 case ARM::fixup_arm_branch:
231 // These values don't encode the low two bits since they're always zero.
232 // Offset by 8 just as above.
Jim Grosbach662a8162010-12-06 23:57:07 +0000233 return 0xffffff & ((Value - 8) >> 2);
Owen Andersonc2666002010-12-13 19:31:11 +0000234 case ARM::fixup_t2_uncondbranch: {
Owen Anderson63ee2202010-12-10 23:02:28 +0000235 Value = Value - 4;
Owen Andersonfb20d892010-12-09 00:27:41 +0000236 Value >>= 1; // Low bit is not encoded.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000237
Jim Grosbach56a25352010-12-13 19:25:46 +0000238 uint32_t out = 0;
Owen Andersonc2666002010-12-13 19:31:11 +0000239 bool I = Value & 0x800000;
240 bool J1 = Value & 0x400000;
241 bool J2 = Value & 0x200000;
242 J1 ^= I;
243 J2 ^= I;
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000244
Owen Andersonc2666002010-12-13 19:31:11 +0000245 out |= I << 26; // S bit
246 out |= !J1 << 13; // J1 bit
247 out |= !J2 << 11; // J2 bit
248 out |= (Value & 0x1FF800) << 5; // imm6 field
249 out |= (Value & 0x0007FF); // imm11 field
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000250
Owen Andersonc2666002010-12-13 19:31:11 +0000251 uint64_t swapped = (out & 0xFFFF0000) >> 16;
252 swapped |= (out & 0x0000FFFF) << 16;
253 return swapped;
254 }
255 case ARM::fixup_t2_condbranch: {
256 Value = Value - 4;
257 Value >>= 1; // Low bit is not encoded.
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000258
Owen Andersonc2666002010-12-13 19:31:11 +0000259 uint64_t out = 0;
Owen Anderson8f079432010-12-09 01:02:09 +0000260 out |= (Value & 0x80000) << 7; // S bit
261 out |= (Value & 0x40000) >> 7; // J2 bit
262 out |= (Value & 0x20000) >> 4; // J1 bit
263 out |= (Value & 0x1F800) << 5; // imm6 field
264 out |= (Value & 0x007FF); // imm11 field
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000265
Jim Grosbach56a25352010-12-13 19:25:46 +0000266 uint32_t swapped = (out & 0xFFFF0000) >> 16;
Owen Andersonfb20d892010-12-09 00:27:41 +0000267 swapped |= (out & 0x0000FFFF) << 16;
268 return swapped;
269 }
Jim Grosbach662a8162010-12-06 23:57:07 +0000270 case ARM::fixup_arm_thumb_bl: {
271 // The value doesn't encode the low bit (always zero) and is offset by
272 // four. The value is encoded into disjoint bit positions in the destination
273 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000274 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000275 // BL: xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000276 //
Jim Grosbach662a8162010-12-06 23:57:07 +0000277 // Note that the halfwords are stored high first, low second; so we need
278 // to transpose the fixup value here to map properly.
Bill Wendling09aa3f02010-12-09 00:39:08 +0000279 unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000280 uint32_t Binary = 0;
281 Value = 0x3fffff & ((Value - 4) >> 1);
282 Binary = (Value & 0x7ff) << 16; // Low imm11 value.
283 Binary |= (Value & 0x1ffc00) >> 11; // High imm10 value.
284 Binary |= isNeg << 10; // Sign bit.
Bill Wendling09aa3f02010-12-09 00:39:08 +0000285 return Binary;
286 }
287 case ARM::fixup_arm_thumb_blx: {
288 // The value doesn't encode the low two bits (always zero) and is offset by
289 // four (see fixup_arm_thumb_cp). The value is encoded into disjoint bit
290 // positions in the destination opcode. x = unchanged, I = immediate value
291 // bit, S = sign extension bit, 0 = zero.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000292 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000293 // BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000294 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000295 // Note that the halfwords are stored high first, low second; so we need
296 // to transpose the fixup value here to map properly.
297 unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000298 uint32_t Binary = 0;
299 Value = 0xfffff & ((Value - 2) >> 2);
300 Binary = (Value & 0x3ff) << 17; // Low imm10L value.
301 Binary |= (Value & 0xffc00) >> 10; // High imm10H value.
302 Binary |= isNeg << 10; // Sign bit.
Jim Grosbach662a8162010-12-06 23:57:07 +0000303 return Binary;
304 }
Bill Wendlingb8958b02010-12-08 01:57:09 +0000305 case ARM::fixup_arm_thumb_cp:
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000306 // Offset by 4, and don't encode the low two bits. Two bytes of that
307 // 'off by 4' is implicitly handled by the half-word ordering of the
308 // Thumb encoding, so we only need to adjust by 2 here.
309 return ((Value - 2) >> 2) & 0xff;
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000310 case ARM::fixup_arm_thumb_cb: {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000311 // Offset by 4 and don't encode the lower bit, which is always 0.
312 uint32_t Binary = (Value - 4) >> 1;
Owen Anderson86abd482010-12-14 19:42:53 +0000313 return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000314 }
Jim Grosbache2467172010-12-10 18:21:33 +0000315 case ARM::fixup_arm_thumb_br:
316 // Offset by 4 and don't encode the lower bit, which is always 0.
317 return ((Value - 4) >> 1) & 0x7ff;
Jim Grosbach01086452010-12-10 17:13:40 +0000318 case ARM::fixup_arm_thumb_bcc:
319 // Offset by 4 and don't encode the lower bit, which is always 0.
320 return ((Value - 4) >> 1) & 0xff;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000321 case ARM::fixup_arm_pcrel_10:
Owen Andersone2e0f582010-12-10 22:46:47 +0000322 Value = Value - 4; // ARM fixups offset by an additional word and don't
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000323 // need to adjust for the half-word ordering.
324 // Fall through.
325 case ARM::fixup_t2_pcrel_10: {
326 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Andersone2e0f582010-12-10 22:46:47 +0000327 Value = Value - 4;
Jason W Kim0c628c22010-12-01 22:46:50 +0000328 bool isAdd = true;
329 if ((int64_t)Value < 0) {
330 Value = -Value;
331 isAdd = false;
332 }
333 // These values don't encode the low two bits since they're always zero.
334 Value >>= 2;
335 assert ((Value < 256) && "Out of range pc-relative fixup value!");
336 Value |= isAdd << 23;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000337
Owen Andersoncc78f5c2010-12-08 19:31:11 +0000338 // Same addressing mode as fixup_arm_pcrel_10,
339 // but with 16-bit halfwords swapped.
Owen Andersond8e351b2010-12-08 00:18:36 +0000340 if (Kind == ARM::fixup_t2_pcrel_10) {
Jim Grosbach56a25352010-12-13 19:25:46 +0000341 uint32_t swapped = (Value & 0xFFFF0000) >> 16;
Owen Anderson255eafb2010-12-08 00:21:33 +0000342 swapped |= (Value & 0x0000FFFF) << 16;
Owen Andersond8e351b2010-12-08 00:18:36 +0000343 return swapped;
344 }
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000345
Jason W Kim0c628c22010-12-01 22:46:50 +0000346 return Value;
347 }
348 }
349}
350
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000351namespace {
Bill Wendling52e635e2010-12-07 23:05:20 +0000352
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000353// FIXME: This should be in a separate file.
354// ELF is an ELF of course...
355class ELFARMAsmBackend : public ARMAsmBackend {
356public:
357 Triple::OSType OSType;
358 ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
Daniel Dunbar7b62afa2010-12-17 02:06:08 +0000359 : ARMAsmBackend(T), OSType(_OSType) { }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000360
Rafael Espindola179821a2010-12-06 19:08:48 +0000361 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000362 uint64_t Value) const;
363
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000364 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Rafael Espindolabff66a82010-12-18 03:27:34 +0000365 return createELFObjectWriter(new ARMELFObjectWriter(OSType), OS,
366 /*IsLittleEndian*/ true);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000367 }
368};
369
Bill Wendling52e635e2010-12-07 23:05:20 +0000370// FIXME: Raise this to share code between Darwin and ELF.
Rafael Espindola179821a2010-12-06 19:08:48 +0000371void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
372 unsigned DataSize, uint64_t Value) const {
Bill Wendling52e635e2010-12-07 23:05:20 +0000373 unsigned NumBytes = 4; // FIXME: 2 for Thumb
Bill Wendling52e635e2010-12-07 23:05:20 +0000374 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000375 if (!Value) return; // Doesn't change encoding.
Bill Wendling52e635e2010-12-07 23:05:20 +0000376
377 unsigned Offset = Fixup.getOffset();
378 assert(Offset % NumBytes == 0 && "Offset mod NumBytes is nonzero!");
379
380 // For each byte of the fragment that the fixup touches, mask in the bits from
381 // the fixup value. The Value has been "split up" into the appropriate
382 // bitfields above.
383 for (unsigned i = 0; i != NumBytes; ++i)
384 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000385}
386
387// FIXME: This should be in a separate file.
388class DarwinARMAsmBackend : public ARMAsmBackend {
389public:
Daniel Dunbar7b62afa2010-12-17 02:06:08 +0000390 DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) { }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000391
Rafael Espindola179821a2010-12-06 19:08:48 +0000392 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000393 uint64_t Value) const;
394
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000395 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jim Grosbachc9d14392010-11-05 18:48:58 +0000396 // FIXME: Subtarget info should be derived. Force v7 for now.
Daniel Dunbar5d05d972010-12-16 17:21:02 +0000397 return createMachObjectWriter(new ARMMachObjectWriter(
398 /*Is64Bit=*/false,
399 object::mach::CTM_ARM,
400 object::mach::CSARM_V7),
401 OS,
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000402 /*IsLittleEndian=*/true);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000403 }
404
405 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
406 return false;
407 }
408};
409
Bill Wendlingd832fa02010-12-07 23:11:00 +0000410/// getFixupKindNumBytes - The number of bytes the fixup may change.
Jim Grosbachc466b932010-11-11 18:04:49 +0000411static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000412 switch (Kind) {
Jim Grosbach662a8162010-12-06 23:57:07 +0000413 default:
414 llvm_unreachable("Unknown fixup kind!");
Bill Wendlingb8958b02010-12-08 01:57:09 +0000415
Jim Grosbach6ec6eeb2010-12-17 18:39:10 +0000416 case FK_Data_1:
Jim Grosbach01086452010-12-10 17:13:40 +0000417 case ARM::fixup_arm_thumb_bcc:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000418 case ARM::fixup_arm_thumb_cp:
Jim Grosbachd40963c2010-12-14 22:28:03 +0000419 case ARM::fixup_thumb_adr_pcrel_10:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000420 return 1;
421
Jim Grosbach6ec6eeb2010-12-17 18:39:10 +0000422 case FK_Data_2:
Jim Grosbache2467172010-12-10 18:21:33 +0000423 case ARM::fixup_arm_thumb_br:
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000424 case ARM::fixup_arm_thumb_cb:
Bill Wendlingdff2f712010-12-08 23:01:43 +0000425 return 2;
426
Jim Grosbach662a8162010-12-06 23:57:07 +0000427 case ARM::fixup_arm_ldst_pcrel_12:
428 case ARM::fixup_arm_pcrel_10:
429 case ARM::fixup_arm_adr_pcrel_12:
430 case ARM::fixup_arm_branch:
431 return 3;
Bill Wendlingb8958b02010-12-08 01:57:09 +0000432
433 case FK_Data_4:
Owen Andersond7b3f582010-12-09 01:51:07 +0000434 case ARM::fixup_t2_ldst_pcrel_12:
Owen Andersonc2666002010-12-13 19:31:11 +0000435 case ARM::fixup_t2_condbranch:
436 case ARM::fixup_t2_uncondbranch:
Owen Andersond8e351b2010-12-08 00:18:36 +0000437 case ARM::fixup_t2_pcrel_10:
Owen Andersona838a252010-12-14 00:36:49 +0000438 case ARM::fixup_t2_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +0000439 case ARM::fixup_arm_thumb_bl:
Bill Wendling09aa3f02010-12-09 00:39:08 +0000440 case ARM::fixup_arm_thumb_blx:
Jim Grosbach662a8162010-12-06 23:57:07 +0000441 return 4;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000442 }
443}
444
Rafael Espindola179821a2010-12-06 19:08:48 +0000445void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
446 unsigned DataSize, uint64_t Value) const {
Jim Grosbachc466b932010-11-11 18:04:49 +0000447 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Jim Grosbach679cbd32010-11-09 01:37:15 +0000448 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000449 if (!Value) return; // Doesn't change encoding.
Jim Grosbach679cbd32010-11-09 01:37:15 +0000450
Bill Wendlingd832fa02010-12-07 23:11:00 +0000451 unsigned Offset = Fixup.getOffset();
452 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
453
Jim Grosbach679cbd32010-11-09 01:37:15 +0000454 // For each byte of the fragment that the fixup touches, mask in the
455 // bits from the fixup value.
456 for (unsigned i = 0; i != NumBytes; ++i)
Bill Wendlingd832fa02010-12-07 23:11:00 +0000457 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000458}
Bill Wendling52e635e2010-12-07 23:05:20 +0000459
Jim Grosbachf73fd722010-09-30 03:21:00 +0000460} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000461
462TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
463 const std::string &TT) {
464 switch (Triple(TT).getOS()) {
465 case Triple::Darwin:
466 return new DarwinARMAsmBackend(T);
467 case Triple::MinGW32:
468 case Triple::Cygwin:
469 case Triple::Win32:
470 assert(0 && "Windows not supported on ARM");
471 default:
472 return new ELFARMAsmBackend(T, Triple(TT).getOS());
473 }
474}