blob: e815a71754a102538e80f1c532f787e3a05664c6 [file] [log] [blame]
Jason W Kimb3212452010-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
Jim Grosbach45e50d82011-08-16 17:06:20 +000010#include "MCTargetDesc/ARMMCTargetDesc.h"
Evan Chengad5f4852011-07-23 00:00:19 +000011#include "MCTargetDesc/ARMBaseInfo.h"
12#include "MCTargetDesc/ARMFixupKinds.h"
Evan Chenga20cde32011-07-20 23:34:39 +000013#include "MCTargetDesc/ARMAddressingModes.h"
Jason W Kimb3212452010-09-30 02:17:26 +000014#include "llvm/ADT/Twine.h"
Jason W Kimb3212452010-09-30 02:17:26 +000015#include "llvm/MC/MCAssembler.h"
Jim Grosbach87055ed2010-12-08 01:16:55 +000016#include "llvm/MC/MCDirectives.h"
Rafael Espindolaf0e24d42010-12-17 16:59:53 +000017#include "llvm/MC/MCELFObjectWriter.h"
Jason W Kimb3212452010-09-30 02:17:26 +000018#include "llvm/MC/MCExpr.h"
Daniel Dunbar73b87132010-12-16 16:08:33 +000019#include "llvm/MC/MCMachObjectWriter.h"
Jason W Kimb3212452010-09-30 02:17:26 +000020#include "llvm/MC/MCObjectWriter.h"
Jason W Kimb3212452010-09-30 02:17:26 +000021#include "llvm/MC/MCSectionELF.h"
22#include "llvm/MC/MCSectionMachO.h"
Evan Cheng5928e692011-07-25 23:24:55 +000023#include "llvm/MC/MCAsmBackend.h"
Jim Grosbach45e50d82011-08-16 17:06:20 +000024#include "llvm/MC/MCSubtargetInfo.h"
Daniel Dunbara5f50c12010-11-27 04:38:36 +000025#include "llvm/Object/MachOFormat.h"
Wesley Peck18510902010-10-22 15:52:49 +000026#include "llvm/Support/ELF.h"
Jason W Kimb3212452010-09-30 02:17:26 +000027#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/raw_ostream.h"
Jason W Kimb3212452010-09-30 02:17:26 +000029using namespace llvm;
30
31namespace {
Rafael Espindola6b5e56c2010-12-17 17:45:22 +000032class ARMELFObjectWriter : public MCELFObjectTargetWriter {
33public:
Rafael Espindolafdaae0d2010-12-18 03:27:34 +000034 ARMELFObjectWriter(Triple::OSType OSType)
35 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSType, ELF::EM_ARM,
36 /*HasRelocationAddend*/ false) {}
Rafael Espindola6b5e56c2010-12-17 17:45:22 +000037};
38
Evan Cheng5928e692011-07-25 23:24:55 +000039class ARMAsmBackend : public MCAsmBackend {
Jim Grosbach45e50d82011-08-16 17:06:20 +000040 const MCSubtargetInfo* STI;
Jim Grosbach87055ed2010-12-08 01:16:55 +000041 bool isThumbMode; // Currently emitting Thumb code.
Jason W Kimb3212452010-09-30 02:17:26 +000042public:
Jim Grosbach45e50d82011-08-16 17:06:20 +000043 ARMAsmBackend(const Target &T, const StringRef TT)
44 : MCAsmBackend(), STI(ARM_MC::createARMMCSubtargetInfo(TT, "", "")),
Jim Grosbach21a60b62011-08-24 22:27:35 +000045 isThumbMode(TT.startswith("thumb")) {}
Jim Grosbach45e50d82011-08-16 17:06:20 +000046
47 ~ARMAsmBackend() {
48 delete STI;
49 }
Jason W Kimb3212452010-09-30 02:17:26 +000050
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +000051 unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
52
Jim Grosbach45e50d82011-08-16 17:06:20 +000053 bool hasNOP() const {
54 return (STI->getFeatureBits() & ARM::HasV6T2Ops) != 0;
55 }
56
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +000057 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
58 const static MCFixupKindInfo Infos[ARM::NumTargetFixupKinds] = {
59// This table *must* be in the order that the fixup_* kinds are defined in
60// ARMFixupKinds.h.
61//
62// Name Offset (bits) Size (bits) Flags
63{ "fixup_arm_ldst_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
64{ "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
65 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
66{ "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
67{ "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
68 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
69{ "fixup_thumb_adr_pcrel_10",0, 8, MCFixupKindInfo::FKF_IsPCRel |
70 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
71{ "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
72{ "fixup_t2_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
73 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Jason W Kimd2e2f562011-02-04 19:47:15 +000074{ "fixup_arm_condbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
75{ "fixup_arm_uncondbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +000076{ "fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
77{ "fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
78{ "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
79{ "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachf00b9cc2011-08-18 16:57:50 +000080{ "fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +000081{ "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbach181d2f92011-08-19 18:20:48 +000082{ "fixup_arm_thumb_cp", 0, 8, MCFixupKindInfo::FKF_IsPCRel },
Eric Christopher368976f2011-05-28 03:16:22 +000083{ "fixup_arm_thumb_bcc", 0, 8, MCFixupKindInfo::FKF_IsPCRel },
Evan Chengd4a5c052011-01-14 02:38:49 +000084// movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16 - 19.
85{ "fixup_arm_movt_hi16", 0, 20, 0 },
86{ "fixup_arm_movw_lo16", 0, 20, 0 },
87{ "fixup_t2_movt_hi16", 0, 20, 0 },
88{ "fixup_t2_movw_lo16", 0, 20, 0 },
89{ "fixup_arm_movt_hi16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
90{ "fixup_arm_movw_lo16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
91{ "fixup_t2_movt_hi16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
92{ "fixup_t2_movw_lo16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +000093 };
94
95 if (Kind < FirstTargetFixupKind)
Evan Cheng5928e692011-07-25 23:24:55 +000096 return MCAsmBackend::getFixupKindInfo(Kind);
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +000097
98 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
99 "Invalid kind!");
100 return Infos[Kind - FirstTargetFixupKind];
101 }
102
Jason W Kimb3212452010-09-30 02:17:26 +0000103 bool MayNeedRelaxation(const MCInst &Inst) const;
104
105 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
106
107 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
Jim Grosbach7e872962010-09-30 17:45:51 +0000108
Jim Grosbach87055ed2010-12-08 01:16:55 +0000109 void HandleAssemblerFlag(MCAssemblerFlag Flag) {
110 switch (Flag) {
111 default: break;
112 case MCAF_Code16:
113 setIsThumb(true);
114 break;
115 case MCAF_Code32:
116 setIsThumb(false);
117 break;
118 }
Jim Grosbach7e872962010-09-30 17:45:51 +0000119 }
Jim Grosbach87055ed2010-12-08 01:16:55 +0000120
121 unsigned getPointerSize() const { return 4; }
122 bool isThumb() const { return isThumbMode; }
123 void setIsThumb(bool it) { isThumbMode = it; }
Jason W Kimb3212452010-09-30 02:17:26 +0000124};
Chris Lattner9fdd10d2010-11-17 05:41:32 +0000125} // end anonymous namespace
Jason W Kimb3212452010-09-30 02:17:26 +0000126
127bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
128 // FIXME: Thumb targets, different move constant targets..
129 return false;
130}
131
132void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
133 assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
134 return;
135}
136
137bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
Jim Grosbach45e50d82011-08-16 17:06:20 +0000138 const uint16_t Thumb1_16bitNopEncoding = 0x46c0; // using MOV r8,r8
139 const uint16_t Thumb2_16bitNopEncoding = 0xbf00; // NOP
140 const uint32_t ARMv4_NopEncoding = 0xe1a0000; // using MOV r0,r0
141 const uint32_t ARMv6T2_NopEncoding = 0xe3207800; // NOP
Jim Grosbach87055ed2010-12-08 01:16:55 +0000142 if (isThumb()) {
Jim Grosbach45e50d82011-08-16 17:06:20 +0000143 const uint16_t nopEncoding = hasNOP() ? Thumb2_16bitNopEncoding
144 : Thumb1_16bitNopEncoding;
Jim Grosbach97f1de72010-12-17 19:03:02 +0000145 uint64_t NumNops = Count / 2;
146 for (uint64_t i = 0; i != NumNops; ++i)
Jim Grosbach45e50d82011-08-16 17:06:20 +0000147 OW->Write16(nopEncoding);
Jim Grosbach97f1de72010-12-17 19:03:02 +0000148 if (Count & 1)
149 OW->Write8(0);
Jim Grosbach87055ed2010-12-08 01:16:55 +0000150 return true;
151 }
152 // ARM mode
Jim Grosbach45e50d82011-08-16 17:06:20 +0000153 const uint32_t nopEncoding = hasNOP() ? ARMv6T2_NopEncoding
154 : ARMv4_NopEncoding;
Jim Grosbach97f1de72010-12-17 19:03:02 +0000155 uint64_t NumNops = Count / 4;
156 for (uint64_t i = 0; i != NumNops; ++i)
Jim Grosbach45e50d82011-08-16 17:06:20 +0000157 OW->Write32(nopEncoding);
158 // FIXME: should this function return false when unable to write exactly
159 // 'Count' bytes with NOP encodings?
Jim Grosbach97f1de72010-12-17 19:03:02 +0000160 switch (Count % 4) {
161 default: break; // No leftover bytes to write
162 case 1: OW->Write8(0); break;
163 case 2: OW->Write16(0); break;
164 case 3: OW->Write16(0); OW->Write8(0xa0); break;
165 }
166
Rafael Espindola0ed15432010-10-25 17:50:35 +0000167 return true;
Jim Grosbach58bce992010-09-30 03:20:34 +0000168}
Jason W Kimb3212452010-09-30 02:17:26 +0000169
Jason W Kimfc5c5222010-12-01 22:46:50 +0000170static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
171 switch (Kind) {
172 default:
173 llvm_unreachable("Unknown fixup kind!");
Jim Grosbach4416dfa2010-12-17 18:39:10 +0000174 case FK_Data_1:
175 case FK_Data_2:
Jason W Kimfc5c5222010-12-01 22:46:50 +0000176 case FK_Data_4:
Jason W Kimfc5c5222010-12-01 22:46:50 +0000177 return Value;
Jason W Kimd5e6e542010-12-03 19:40:23 +0000178 case ARM::fixup_arm_movt_hi16:
Evan Chengd4a5c052011-01-14 02:38:49 +0000179 Value >>= 16;
180 // Fallthrough
181 case ARM::fixup_arm_movw_lo16:
Jason W Kimd0c937d2011-05-19 20:55:25 +0000182 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim9c5b65d2011-01-12 00:19:25 +0000183 case ARM::fixup_arm_movw_lo16_pcrel: {
Jason W Kimd5e6e542010-12-03 19:40:23 +0000184 unsigned Hi4 = (Value & 0xF000) >> 12;
185 unsigned Lo12 = Value & 0x0FFF;
186 // inst{19-16} = Hi4;
187 // inst{11-0} = Lo12;
188 Value = (Hi4 << 16) | (Lo12);
189 return Value;
190 }
Evan Chengd4a5c052011-01-14 02:38:49 +0000191 case ARM::fixup_t2_movt_hi16:
Evan Chengd4a5c052011-01-14 02:38:49 +0000192 Value >>= 16;
193 // Fallthrough
194 case ARM::fixup_t2_movw_lo16:
Jim Grosbach6629b572011-06-24 20:06:59 +0000195 case ARM::fixup_t2_movt_hi16_pcrel: //FIXME: Shouldn't this be shifted like
196 // the other hi16 fixup?
Evan Chengd4a5c052011-01-14 02:38:49 +0000197 case ARM::fixup_t2_movw_lo16_pcrel: {
198 unsigned Hi4 = (Value & 0xF000) >> 12;
199 unsigned i = (Value & 0x800) >> 11;
200 unsigned Mid3 = (Value & 0x700) >> 8;
201 unsigned Lo8 = Value & 0x0FF;
202 // inst{19-16} = Hi4;
203 // inst{26} = i;
204 // inst{14-12} = Mid3;
205 // inst{7-0} = Lo8;
Evan Chengd4a5c052011-01-14 02:38:49 +0000206 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
207 swapped |= (Value & 0x0000FFFF) << 16;
208 return swapped;
209 }
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000210 case ARM::fixup_arm_ldst_pcrel_12:
Jason W Kimfc5c5222010-12-01 22:46:50 +0000211 // ARM PC-relative values are offset by 8.
Owen Anderson3ef19d92010-12-09 20:27:52 +0000212 Value -= 4;
Owen Andersoncb4d8f22010-12-09 21:34:47 +0000213 // FALLTHROUGH
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000214 case ARM::fixup_t2_ldst_pcrel_12: {
215 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Anderson3ef19d92010-12-09 20:27:52 +0000216 Value -= 4;
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000217 bool isAdd = true;
Jason W Kimfc5c5222010-12-01 22:46:50 +0000218 if ((int64_t)Value < 0) {
219 Value = -Value;
220 isAdd = false;
221 }
222 assert ((Value < 4096) && "Out of range pc-relative fixup value!");
223 Value |= isAdd << 23;
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000224
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000225 // Same addressing mode as fixup_arm_pcrel_10,
226 // but with 16-bit halfwords swapped.
227 if (Kind == ARM::fixup_t2_ldst_pcrel_12) {
228 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
229 swapped |= (Value & 0x0000FFFF) << 16;
230 return swapped;
231 }
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000232
Jason W Kimfc5c5222010-12-01 22:46:50 +0000233 return Value;
234 }
Jim Grosbach509dc2a2010-12-14 22:28:03 +0000235 case ARM::fixup_thumb_adr_pcrel_10:
236 return ((Value - 4) >> 2) & 0xff;
Jim Grosbachce2bd8d2010-12-02 00:28:45 +0000237 case ARM::fixup_arm_adr_pcrel_12: {
238 // ARM PC-relative values are offset by 8.
239 Value -= 8;
240 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
241 if ((int64_t)Value < 0) {
242 Value = -Value;
243 opc = 2; // 0b0010
244 }
245 assert(ARM_AM::getSOImmVal(Value) != -1 &&
246 "Out of range pc-relative fixup value!");
247 // Encode the immediate and shift the opcode into place.
248 return ARM_AM::getSOImmVal(Value) | (opc << 21);
249 }
Jim Grosbache34793e2010-12-14 16:25:15 +0000250
Owen Anderson6d375e52010-12-14 00:36:49 +0000251 case ARM::fixup_t2_adr_pcrel_12: {
252 Value -= 4;
253 unsigned opc = 0;
254 if ((int64_t)Value < 0) {
255 Value = -Value;
256 opc = 5;
257 }
258
259 uint32_t out = (opc << 21);
Owen Anderson8543d4f2011-03-23 22:03:44 +0000260 out |= (Value & 0x800) << 15;
Owen Anderson6d375e52010-12-14 00:36:49 +0000261 out |= (Value & 0x700) << 4;
262 out |= (Value & 0x0FF);
Jim Grosbache34793e2010-12-14 16:25:15 +0000263
Owen Anderson6d375e52010-12-14 00:36:49 +0000264 uint64_t swapped = (out & 0xFFFF0000) >> 16;
265 swapped |= (out & 0x0000FFFF) << 16;
266 return swapped;
267 }
Jim Grosbache34793e2010-12-14 16:25:15 +0000268
Jason W Kimd2e2f562011-02-04 19:47:15 +0000269 case ARM::fixup_arm_condbranch:
270 case ARM::fixup_arm_uncondbranch:
Jason W Kimfc5c5222010-12-01 22:46:50 +0000271 // These values don't encode the low two bits since they're always zero.
272 // Offset by 8 just as above.
Jim Grosbach9e199462010-12-06 23:57:07 +0000273 return 0xffffff & ((Value - 8) >> 2);
Owen Anderson578074b2010-12-13 19:31:11 +0000274 case ARM::fixup_t2_uncondbranch: {
Owen Anderson235c2762010-12-10 23:02:28 +0000275 Value = Value - 4;
Owen Anderson302d5fd2010-12-09 00:27:41 +0000276 Value >>= 1; // Low bit is not encoded.
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000277
Jim Grosbachf588c512010-12-13 19:25:46 +0000278 uint32_t out = 0;
Owen Anderson578074b2010-12-13 19:31:11 +0000279 bool I = Value & 0x800000;
280 bool J1 = Value & 0x400000;
281 bool J2 = Value & 0x200000;
282 J1 ^= I;
283 J2 ^= I;
Jim Grosbache34793e2010-12-14 16:25:15 +0000284
Owen Anderson578074b2010-12-13 19:31:11 +0000285 out |= I << 26; // S bit
286 out |= !J1 << 13; // J1 bit
287 out |= !J2 << 11; // J2 bit
288 out |= (Value & 0x1FF800) << 5; // imm6 field
289 out |= (Value & 0x0007FF); // imm11 field
Jim Grosbache34793e2010-12-14 16:25:15 +0000290
Owen Anderson578074b2010-12-13 19:31:11 +0000291 uint64_t swapped = (out & 0xFFFF0000) >> 16;
292 swapped |= (out & 0x0000FFFF) << 16;
293 return swapped;
294 }
295 case ARM::fixup_t2_condbranch: {
296 Value = Value - 4;
297 Value >>= 1; // Low bit is not encoded.
Jim Grosbache34793e2010-12-14 16:25:15 +0000298
Owen Anderson578074b2010-12-13 19:31:11 +0000299 uint64_t out = 0;
Owen Anderson14e41272010-12-09 01:02:09 +0000300 out |= (Value & 0x80000) << 7; // S bit
301 out |= (Value & 0x40000) >> 7; // J2 bit
302 out |= (Value & 0x20000) >> 4; // J1 bit
303 out |= (Value & 0x1F800) << 5; // imm6 field
304 out |= (Value & 0x007FF); // imm11 field
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000305
Jim Grosbachf588c512010-12-13 19:25:46 +0000306 uint32_t swapped = (out & 0xFFFF0000) >> 16;
Owen Anderson302d5fd2010-12-09 00:27:41 +0000307 swapped |= (out & 0x0000FFFF) << 16;
308 return swapped;
309 }
Jim Grosbach9e199462010-12-06 23:57:07 +0000310 case ARM::fixup_arm_thumb_bl: {
311 // The value doesn't encode the low bit (always zero) and is offset by
312 // four. The value is encoded into disjoint bit positions in the destination
313 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000314 //
Bill Wendling3392bfc2010-12-09 00:39:08 +0000315 // BL: xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000316 //
Jim Grosbach9e199462010-12-06 23:57:07 +0000317 // Note that the halfwords are stored high first, low second; so we need
318 // to transpose the fixup value here to map properly.
Rafael Espindola18668082011-05-20 20:01:01 +0000319 unsigned isNeg = (int64_t(Value - 4) < 0) ? 1 : 0;
Bill Wendlingc4d333f2010-12-09 00:44:33 +0000320 uint32_t Binary = 0;
321 Value = 0x3fffff & ((Value - 4) >> 1);
322 Binary = (Value & 0x7ff) << 16; // Low imm11 value.
323 Binary |= (Value & 0x1ffc00) >> 11; // High imm10 value.
324 Binary |= isNeg << 10; // Sign bit.
Bill Wendling3392bfc2010-12-09 00:39:08 +0000325 return Binary;
326 }
327 case ARM::fixup_arm_thumb_blx: {
328 // The value doesn't encode the low two bits (always zero) and is offset by
329 // four (see fixup_arm_thumb_cp). The value is encoded into disjoint bit
330 // positions in the destination opcode. x = unchanged, I = immediate value
331 // bit, S = sign extension bit, 0 = zero.
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000332 //
Bill Wendling3392bfc2010-12-09 00:39:08 +0000333 // BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000334 //
Bill Wendling3392bfc2010-12-09 00:39:08 +0000335 // Note that the halfwords are stored high first, low second; so we need
336 // to transpose the fixup value here to map properly.
Rafael Espindola18668082011-05-20 20:01:01 +0000337 unsigned isNeg = (int64_t(Value-4) < 0) ? 1 : 0;
Bill Wendlingc4d333f2010-12-09 00:44:33 +0000338 uint32_t Binary = 0;
339 Value = 0xfffff & ((Value - 2) >> 2);
340 Binary = (Value & 0x3ff) << 17; // Low imm10L value.
341 Binary |= (Value & 0xffc00) >> 10; // High imm10H value.
342 Binary |= isNeg << 10; // Sign bit.
Jim Grosbach9e199462010-12-06 23:57:07 +0000343 return Binary;
344 }
Bill Wendling8a6449c2010-12-08 01:57:09 +0000345 case ARM::fixup_arm_thumb_cp:
Jim Grosbach3c685612010-12-08 20:32:07 +0000346 // Offset by 4, and don't encode the low two bits. Two bytes of that
347 // 'off by 4' is implicitly handled by the half-word ordering of the
348 // Thumb encoding, so we only need to adjust by 2 here.
349 return ((Value - 2) >> 2) & 0xff;
Jim Grosbach68b27eb2010-12-09 19:50:12 +0000350 case ARM::fixup_arm_thumb_cb: {
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000351 // Offset by 4 and don't encode the lower bit, which is always 0.
352 uint32_t Binary = (Value - 4) >> 1;
Owen Andersonf636a642010-12-14 19:42:53 +0000353 return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000354 }
Jim Grosbache119da12010-12-10 18:21:33 +0000355 case ARM::fixup_arm_thumb_br:
356 // Offset by 4 and don't encode the lower bit, which is always 0.
357 return ((Value - 4) >> 1) & 0x7ff;
Jim Grosbach78485ad2010-12-10 17:13:40 +0000358 case ARM::fixup_arm_thumb_bcc:
359 // Offset by 4 and don't encode the lower bit, which is always 0.
360 return ((Value - 4) >> 1) & 0xff;
Jim Grosbach3c685612010-12-08 20:32:07 +0000361 case ARM::fixup_arm_pcrel_10:
Owen Anderson4743d752010-12-10 22:46:47 +0000362 Value = Value - 4; // ARM fixups offset by an additional word and don't
Jim Grosbach3c685612010-12-08 20:32:07 +0000363 // need to adjust for the half-word ordering.
364 // Fall through.
365 case ARM::fixup_t2_pcrel_10: {
366 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Anderson4743d752010-12-10 22:46:47 +0000367 Value = Value - 4;
Jason W Kimfc5c5222010-12-01 22:46:50 +0000368 bool isAdd = true;
369 if ((int64_t)Value < 0) {
370 Value = -Value;
371 isAdd = false;
372 }
373 // These values don't encode the low two bits since they're always zero.
374 Value >>= 2;
375 assert ((Value < 256) && "Out of range pc-relative fixup value!");
376 Value |= isAdd << 23;
Jim Grosbach3c685612010-12-08 20:32:07 +0000377
Owen Andersondae32fd2010-12-08 19:31:11 +0000378 // Same addressing mode as fixup_arm_pcrel_10,
379 // but with 16-bit halfwords swapped.
Owen Anderson0f7142d2010-12-08 00:18:36 +0000380 if (Kind == ARM::fixup_t2_pcrel_10) {
Jim Grosbachf588c512010-12-13 19:25:46 +0000381 uint32_t swapped = (Value & 0xFFFF0000) >> 16;
Owen Anderson72ce4532010-12-08 00:21:33 +0000382 swapped |= (Value & 0x0000FFFF) << 16;
Owen Anderson0f7142d2010-12-08 00:18:36 +0000383 return swapped;
384 }
Jim Grosbach3c685612010-12-08 20:32:07 +0000385
Jason W Kimfc5c5222010-12-01 22:46:50 +0000386 return Value;
387 }
388 }
389}
390
Jason W Kimb3212452010-09-30 02:17:26 +0000391namespace {
Bill Wendling721724e2010-12-07 23:05:20 +0000392
Jason W Kimb3212452010-09-30 02:17:26 +0000393// FIXME: This should be in a separate file.
394// ELF is an ELF of course...
395class ELFARMAsmBackend : public ARMAsmBackend {
396public:
397 Triple::OSType OSType;
Jim Grosbach45e50d82011-08-16 17:06:20 +0000398 ELFARMAsmBackend(const Target &T, const StringRef TT,
399 Triple::OSType _OSType)
400 : ARMAsmBackend(T, TT), OSType(_OSType) { }
Jason W Kimb3212452010-09-30 02:17:26 +0000401
Rafael Espindola0f30fec2010-12-06 19:08:48 +0000402 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimb3212452010-09-30 02:17:26 +0000403 uint64_t Value) const;
404
Jason W Kimb3212452010-09-30 02:17:26 +0000405 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000406 return createELFObjectWriter(new ARMELFObjectWriter(OSType), OS,
407 /*IsLittleEndian*/ true);
Jason W Kimb3212452010-09-30 02:17:26 +0000408 }
409};
410
Bill Wendling721724e2010-12-07 23:05:20 +0000411// FIXME: Raise this to share code between Darwin and ELF.
Rafael Espindola0f30fec2010-12-06 19:08:48 +0000412void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
413 unsigned DataSize, uint64_t Value) const {
Bill Wendling721724e2010-12-07 23:05:20 +0000414 unsigned NumBytes = 4; // FIXME: 2 for Thumb
Bill Wendling721724e2010-12-07 23:05:20 +0000415 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingf09c44c2010-12-07 23:11:00 +0000416 if (!Value) return; // Doesn't change encoding.
Bill Wendling721724e2010-12-07 23:05:20 +0000417
418 unsigned Offset = Fixup.getOffset();
Bill Wendling721724e2010-12-07 23:05:20 +0000419
420 // For each byte of the fragment that the fixup touches, mask in the bits from
421 // the fixup value. The Value has been "split up" into the appropriate
422 // bitfields above.
423 for (unsigned i = 0; i != NumBytes; ++i)
424 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimb3212452010-09-30 02:17:26 +0000425}
426
427// FIXME: This should be in a separate file.
428class DarwinARMAsmBackend : public ARMAsmBackend {
429public:
Owen Anderson975ddf82011-04-01 21:07:39 +0000430 const object::mach::CPUSubtypeARM Subtype;
Jim Grosbach45e50d82011-08-16 17:06:20 +0000431 DarwinARMAsmBackend(const Target &T, const StringRef TT,
432 object::mach::CPUSubtypeARM st)
433 : ARMAsmBackend(T, TT), Subtype(st) { }
Jason W Kimb3212452010-09-30 02:17:26 +0000434
Jason W Kimb3212452010-09-30 02:17:26 +0000435 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jim Grosbach2354f872011-06-22 20:14:52 +0000436 return createARMMachObjectWriter(OS, /*Is64Bit=*/false,
437 object::mach::CTM_ARM,
438 Subtype);
Jason W Kimb3212452010-09-30 02:17:26 +0000439 }
440
Owen Anderson975ddf82011-04-01 21:07:39 +0000441 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
442 uint64_t Value) const;
443
Jason W Kimb3212452010-09-30 02:17:26 +0000444 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
445 return false;
446 }
447};
448
Bill Wendlingf09c44c2010-12-07 23:11:00 +0000449/// getFixupKindNumBytes - The number of bytes the fixup may change.
Jim Grosbach9d6d77a2010-11-11 18:04:49 +0000450static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach90987142010-11-09 01:37:15 +0000451 switch (Kind) {
Jim Grosbach9e199462010-12-06 23:57:07 +0000452 default:
453 llvm_unreachable("Unknown fixup kind!");
Bill Wendling8a6449c2010-12-08 01:57:09 +0000454
Jim Grosbach4416dfa2010-12-17 18:39:10 +0000455 case FK_Data_1:
Jim Grosbach78485ad2010-12-10 17:13:40 +0000456 case ARM::fixup_arm_thumb_bcc:
Bill Wendling8a6449c2010-12-08 01:57:09 +0000457 case ARM::fixup_arm_thumb_cp:
Jim Grosbach509dc2a2010-12-14 22:28:03 +0000458 case ARM::fixup_thumb_adr_pcrel_10:
Bill Wendling8a6449c2010-12-08 01:57:09 +0000459 return 1;
460
Jim Grosbach4416dfa2010-12-17 18:39:10 +0000461 case FK_Data_2:
Jim Grosbache119da12010-12-10 18:21:33 +0000462 case ARM::fixup_arm_thumb_br:
Jim Grosbach68b27eb2010-12-09 19:50:12 +0000463 case ARM::fixup_arm_thumb_cb:
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000464 return 2;
465
Jim Grosbach9e199462010-12-06 23:57:07 +0000466 case ARM::fixup_arm_ldst_pcrel_12:
467 case ARM::fixup_arm_pcrel_10:
468 case ARM::fixup_arm_adr_pcrel_12:
Jason W Kimd2e2f562011-02-04 19:47:15 +0000469 case ARM::fixup_arm_condbranch:
470 case ARM::fixup_arm_uncondbranch:
Jim Grosbach9e199462010-12-06 23:57:07 +0000471 return 3;
Bill Wendling8a6449c2010-12-08 01:57:09 +0000472
473 case FK_Data_4:
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000474 case ARM::fixup_t2_ldst_pcrel_12:
Owen Anderson578074b2010-12-13 19:31:11 +0000475 case ARM::fixup_t2_condbranch:
476 case ARM::fixup_t2_uncondbranch:
Owen Anderson0f7142d2010-12-08 00:18:36 +0000477 case ARM::fixup_t2_pcrel_10:
Owen Anderson6d375e52010-12-14 00:36:49 +0000478 case ARM::fixup_t2_adr_pcrel_12:
Jim Grosbach9e199462010-12-06 23:57:07 +0000479 case ARM::fixup_arm_thumb_bl:
Bill Wendling3392bfc2010-12-09 00:39:08 +0000480 case ARM::fixup_arm_thumb_blx:
Evan Chengd4a5c052011-01-14 02:38:49 +0000481 case ARM::fixup_arm_movt_hi16:
482 case ARM::fixup_arm_movw_lo16:
483 case ARM::fixup_arm_movt_hi16_pcrel:
484 case ARM::fixup_arm_movw_lo16_pcrel:
485 case ARM::fixup_t2_movt_hi16:
486 case ARM::fixup_t2_movw_lo16:
487 case ARM::fixup_t2_movt_hi16_pcrel:
488 case ARM::fixup_t2_movw_lo16_pcrel:
Jim Grosbach9e199462010-12-06 23:57:07 +0000489 return 4;
Jim Grosbach90987142010-11-09 01:37:15 +0000490 }
491}
492
Rafael Espindola0f30fec2010-12-06 19:08:48 +0000493void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
494 unsigned DataSize, uint64_t Value) const {
Jim Grosbach9d6d77a2010-11-11 18:04:49 +0000495 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Jim Grosbach90987142010-11-09 01:37:15 +0000496 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingf09c44c2010-12-07 23:11:00 +0000497 if (!Value) return; // Doesn't change encoding.
Jim Grosbach90987142010-11-09 01:37:15 +0000498
Bill Wendlingf09c44c2010-12-07 23:11:00 +0000499 unsigned Offset = Fixup.getOffset();
500 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
501
Jim Grosbach90987142010-11-09 01:37:15 +0000502 // For each byte of the fragment that the fixup touches, mask in the
503 // bits from the fixup value.
504 for (unsigned i = 0; i != NumBytes; ++i)
Bill Wendlingf09c44c2010-12-07 23:11:00 +0000505 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimb3212452010-09-30 02:17:26 +0000506}
Bill Wendling721724e2010-12-07 23:05:20 +0000507
Jim Grosbach689651c2010-09-30 03:21:00 +0000508} // end anonymous namespace
Jason W Kimb3212452010-09-30 02:17:26 +0000509
Evan Cheng5928e692011-07-25 23:24:55 +0000510MCAsmBackend *llvm::createARMAsmBackend(const Target &T, StringRef TT) {
Owen Anderson975ddf82011-04-01 21:07:39 +0000511 Triple TheTriple(TT);
Daniel Dunbar2b9b0e32011-04-19 21:14:45 +0000512
513 if (TheTriple.isOSDarwin()) {
Evan Cheng965ed2e2011-06-14 18:08:33 +0000514 if (TheTriple.getArchName() == "armv4t" ||
515 TheTriple.getArchName() == "thumbv4t")
Jim Grosbach45e50d82011-08-16 17:06:20 +0000516 return new DarwinARMAsmBackend(T, TT, object::mach::CSARM_V4T);
Evan Cheng965ed2e2011-06-14 18:08:33 +0000517 else if (TheTriple.getArchName() == "armv5e" ||
518 TheTriple.getArchName() == "thumbv5e")
Jim Grosbach45e50d82011-08-16 17:06:20 +0000519 return new DarwinARMAsmBackend(T, TT, object::mach::CSARM_V5TEJ);
Evan Cheng965ed2e2011-06-14 18:08:33 +0000520 else if (TheTriple.getArchName() == "armv6" ||
Owen Anderson975ddf82011-04-01 21:07:39 +0000521 TheTriple.getArchName() == "thumbv6")
Jim Grosbach45e50d82011-08-16 17:06:20 +0000522 return new DarwinARMAsmBackend(T, TT, object::mach::CSARM_V6);
523 return new DarwinARMAsmBackend(T, TT, object::mach::CSARM_V7);
Owen Anderson975ddf82011-04-01 21:07:39 +0000524 }
Daniel Dunbar2b9b0e32011-04-19 21:14:45 +0000525
526 if (TheTriple.isOSWindows())
Jason W Kimb3212452010-09-30 02:17:26 +0000527 assert(0 && "Windows not supported on ARM");
Daniel Dunbar2b9b0e32011-04-19 21:14:45 +0000528
Jim Grosbach45e50d82011-08-16 17:06:20 +0000529 return new ELFARMAsmBackend(T, TT, Triple(TT).getOS());
Jason W Kimb3212452010-09-30 02:17:26 +0000530}