blob: bd0df721010a120ad324186f132c5f3b1e9e6e67 [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
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +000010#include "MCTargetDesc/ARMMCTargetDesc.h"
Evan Chengbe740292011-07-23 00:00:19 +000011#include "MCTargetDesc/ARMBaseInfo.h"
12#include "MCTargetDesc/ARMFixupKinds.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000013#include "MCTargetDesc/ARMAddressingModes.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000014#include "llvm/ADT/Twine.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000015#include "llvm/MC/MCAssembler.h"
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000016#include "llvm/MC/MCDirectives.h"
Rafael Espindola285b3e52010-12-17 16:59:53 +000017#include "llvm/MC/MCELFObjectWriter.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000018#include "llvm/MC/MCExpr.h"
Daniel Dunbaraa4b7dd2010-12-16 16:08:33 +000019#include "llvm/MC/MCMachObjectWriter.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000020#include "llvm/MC/MCObjectWriter.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000021#include "llvm/MC/MCSectionELF.h"
22#include "llvm/MC/MCSectionMachO.h"
Evan Cheng78c10ee2011-07-25 23:24:55 +000023#include "llvm/MC/MCAsmBackend.h"
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +000024#include "llvm/MC/MCSubtargetInfo.h"
Daniel Dunbar36d76a82010-11-27 04:38:36 +000025#include "llvm/Object/MachOFormat.h"
Wesley Peckeecb8582010-10-22 15:52:49 +000026#include "llvm/Support/ELF.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000027#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/raw_ostream.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000029using namespace llvm;
30
31namespace {
Rafael Espindola6024c972010-12-17 17:45:22 +000032class ARMELFObjectWriter : public MCELFObjectTargetWriter {
33public:
Rafael Espindolabff66a82010-12-18 03:27:34 +000034 ARMELFObjectWriter(Triple::OSType OSType)
35 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSType, ELF::EM_ARM,
36 /*HasRelocationAddend*/ false) {}
Rafael Espindola6024c972010-12-17 17:45:22 +000037};
38
Evan Cheng78c10ee2011-07-25 23:24:55 +000039class ARMAsmBackend : public MCAsmBackend {
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +000040 const MCSubtargetInfo* STI;
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000041 bool isThumbMode; // Currently emitting Thumb code.
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000042public:
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +000043 ARMAsmBackend(const Target &T, const StringRef TT)
44 : MCAsmBackend(), STI(ARM_MC::createARMMCSubtargetInfo(TT, "", "")),
Jim Grosbachb9d3ff82011-08-24 22:27:35 +000045 isThumbMode(TT.startswith("thumb")) {}
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +000046
47 ~ARMAsmBackend() {
48 delete STI;
49 }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000050
Daniel Dunbar2761fc42010-12-16 03:20:06 +000051 unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
52
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +000053 bool hasNOP() const {
54 return (STI->getFeatureBits() & ARM::HasV6T2Ops) != 0;
55 }
56
Daniel Dunbar2761fc42010-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
Jim Grosbach2abba842011-11-16 22:48:37 +000063{ "fixup_arm_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar2761fc42010-12-16 03:20:06 +000064{ "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
65 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Jim Grosbach2f196742011-12-19 23:06:24 +000066{ "fixup_arm_pcrel_10_unscaled", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbach681460f2011-11-01 01:24:45 +000067{ "fixup_arm_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar2761fc42010-12-16 03:20:06 +000068{ "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
69 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
70{ "fixup_thumb_adr_pcrel_10",0, 8, MCFixupKindInfo::FKF_IsPCRel |
71 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Jim Grosbach2abba842011-11-16 22:48:37 +000072{ "fixup_arm_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar2761fc42010-12-16 03:20:06 +000073{ "fixup_t2_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
74 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Jason W Kim685c3502011-02-04 19:47:15 +000075{ "fixup_arm_condbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
76{ "fixup_arm_uncondbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar2761fc42010-12-16 03:20:06 +000077{ "fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
78{ "fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
79{ "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
80{ "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbach90b5a082011-08-18 16:57:50 +000081{ "fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar2761fc42010-12-16 03:20:06 +000082{ "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbach67b95f92011-08-19 18:20:48 +000083{ "fixup_arm_thumb_cp", 0, 8, MCFixupKindInfo::FKF_IsPCRel },
Eric Christopherfea51fc2011-05-28 03:16:22 +000084{ "fixup_arm_thumb_bcc", 0, 8, MCFixupKindInfo::FKF_IsPCRel },
Evan Chengf3eb3bb2011-01-14 02:38:49 +000085// movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16 - 19.
86{ "fixup_arm_movt_hi16", 0, 20, 0 },
87{ "fixup_arm_movw_lo16", 0, 20, 0 },
88{ "fixup_t2_movt_hi16", 0, 20, 0 },
89{ "fixup_t2_movw_lo16", 0, 20, 0 },
90{ "fixup_arm_movt_hi16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
91{ "fixup_arm_movw_lo16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
92{ "fixup_t2_movt_hi16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
93{ "fixup_t2_movw_lo16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar2761fc42010-12-16 03:20:06 +000094 };
95
96 if (Kind < FirstTargetFixupKind)
Evan Cheng78c10ee2011-07-25 23:24:55 +000097 return MCAsmBackend::getFixupKindInfo(Kind);
Daniel Dunbar2761fc42010-12-16 03:20:06 +000098
99 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
100 "Invalid kind!");
101 return Infos[Kind - FirstTargetFixupKind];
102 }
103
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000104 bool MayNeedRelaxation(const MCInst &Inst) const;
105
Jim Grosbach370b78d2011-12-06 00:47:03 +0000106 bool fixupNeedsRelaxation(const MCFixup &Fixup,
107 uint64_t Value,
108 const MCInstFragment *DF,
109 const MCAsmLayout &Layout) const;
110
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000111 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
112
113 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
Jim Grosbach3787a402010-09-30 17:45:51 +0000114
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000115 void HandleAssemblerFlag(MCAssemblerFlag Flag) {
116 switch (Flag) {
117 default: break;
118 case MCAF_Code16:
119 setIsThumb(true);
120 break;
121 case MCAF_Code32:
122 setIsThumb(false);
123 break;
124 }
Jim Grosbach3787a402010-09-30 17:45:51 +0000125 }
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000126
127 unsigned getPointerSize() const { return 4; }
128 bool isThumb() const { return isThumbMode; }
129 void setIsThumb(bool it) { isThumbMode = it; }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000130};
Chris Lattnerb75c6512010-11-17 05:41:32 +0000131} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000132
Jim Grosbachf503ef62011-12-05 23:45:46 +0000133static unsigned getRelaxedOpcode(unsigned Op) {
134 switch (Op) {
135 default: return Op;
136 case ARM::tBcc: return ARM::t2Bcc;
137 }
138}
139
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000140bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
Jim Grosbachf503ef62011-12-05 23:45:46 +0000141 if (getRelaxedOpcode(Inst.getOpcode()) != Inst.getOpcode())
142 return true;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000143 return false;
144}
145
Jim Grosbach370b78d2011-12-06 00:47:03 +0000146bool ARMAsmBackend::fixupNeedsRelaxation(const MCFixup &Fixup,
147 uint64_t Value,
148 const MCInstFragment *DF,
149 const MCAsmLayout &Layout) const {
Jim Grosbachd9a6e892011-12-06 01:08:19 +0000150 // Relaxing tBcc to t2Bcc. tBcc has a signed 9-bit displacement with the
151 // low bit being an implied zero. There's an implied +4 offset for the
152 // branch, so we adjust the other way here to determine what's
153 // encodable.
Jim Grosbach370b78d2011-12-06 00:47:03 +0000154 //
155 // Relax if the value is too big for a (signed) i8.
Jim Grosbachcb865092011-12-06 01:53:17 +0000156 int64_t Offset = int64_t(Value) - 4;
157 return Offset > 254 || Offset < -256;
Jim Grosbach370b78d2011-12-06 00:47:03 +0000158}
159
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000160void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
Jim Grosbachf503ef62011-12-05 23:45:46 +0000161 unsigned RelaxedOp = getRelaxedOpcode(Inst.getOpcode());
162
163 // Sanity check w/ diagnostic if we get here w/ a bogus instruction.
164 if (RelaxedOp == Inst.getOpcode()) {
165 SmallString<256> Tmp;
166 raw_svector_ostream OS(Tmp);
167 Inst.dump_pretty(OS);
168 OS << "\n";
169 report_fatal_error("unexpected instruction to relax: " + OS.str());
170 }
171
172 // The instructions we're relaxing have (so far) the same operands.
173 // We just need to update to the proper opcode.
174 Res = Inst;
175 Res.setOpcode(RelaxedOp);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000176}
177
178bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +0000179 const uint16_t Thumb1_16bitNopEncoding = 0x46c0; // using MOV r8,r8
180 const uint16_t Thumb2_16bitNopEncoding = 0xbf00; // NOP
181 const uint32_t ARMv4_NopEncoding = 0xe1a0000; // using MOV r0,r0
Jim Grosbachb84acd22011-11-16 22:40:25 +0000182 const uint32_t ARMv6T2_NopEncoding = 0xe320f000; // NOP
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000183 if (isThumb()) {
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +0000184 const uint16_t nopEncoding = hasNOP() ? Thumb2_16bitNopEncoding
185 : Thumb1_16bitNopEncoding;
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000186 uint64_t NumNops = Count / 2;
187 for (uint64_t i = 0; i != NumNops; ++i)
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +0000188 OW->Write16(nopEncoding);
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000189 if (Count & 1)
190 OW->Write8(0);
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000191 return true;
192 }
193 // ARM mode
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +0000194 const uint32_t nopEncoding = hasNOP() ? ARMv6T2_NopEncoding
195 : ARMv4_NopEncoding;
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000196 uint64_t NumNops = Count / 4;
197 for (uint64_t i = 0; i != NumNops; ++i)
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +0000198 OW->Write32(nopEncoding);
199 // FIXME: should this function return false when unable to write exactly
200 // 'Count' bytes with NOP encodings?
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000201 switch (Count % 4) {
202 default: break; // No leftover bytes to write
203 case 1: OW->Write8(0); break;
204 case 2: OW->Write16(0); break;
205 case 3: OW->Write16(0); OW->Write8(0xa0); break;
206 }
207
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000208 return true;
Jim Grosbach87dc3aa2010-09-30 03:20:34 +0000209}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000210
Jason W Kim0c628c22010-12-01 22:46:50 +0000211static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
212 switch (Kind) {
213 default:
214 llvm_unreachable("Unknown fixup kind!");
Jim Grosbach6ec6eeb2010-12-17 18:39:10 +0000215 case FK_Data_1:
216 case FK_Data_2:
Jason W Kim0c628c22010-12-01 22:46:50 +0000217 case FK_Data_4:
Jason W Kim0c628c22010-12-01 22:46:50 +0000218 return Value;
Jason W Kim2ccf1482010-12-03 19:40:23 +0000219 case ARM::fixup_arm_movt_hi16:
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000220 Value >>= 16;
221 // Fallthrough
222 case ARM::fixup_arm_movw_lo16:
Jason W Kim861b9c62011-05-19 20:55:25 +0000223 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim86a97f22011-01-12 00:19:25 +0000224 case ARM::fixup_arm_movw_lo16_pcrel: {
Jason W Kim2ccf1482010-12-03 19:40:23 +0000225 unsigned Hi4 = (Value & 0xF000) >> 12;
226 unsigned Lo12 = Value & 0x0FFF;
227 // inst{19-16} = Hi4;
228 // inst{11-0} = Lo12;
229 Value = (Hi4 << 16) | (Lo12);
230 return Value;
231 }
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000232 case ARM::fixup_t2_movt_hi16:
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000233 Value >>= 16;
234 // Fallthrough
235 case ARM::fixup_t2_movw_lo16:
Jim Grosbach8b454562011-06-24 20:06:59 +0000236 case ARM::fixup_t2_movt_hi16_pcrel: //FIXME: Shouldn't this be shifted like
237 // the other hi16 fixup?
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000238 case ARM::fixup_t2_movw_lo16_pcrel: {
239 unsigned Hi4 = (Value & 0xF000) >> 12;
240 unsigned i = (Value & 0x800) >> 11;
241 unsigned Mid3 = (Value & 0x700) >> 8;
242 unsigned Lo8 = Value & 0x0FF;
243 // inst{19-16} = Hi4;
244 // inst{26} = i;
245 // inst{14-12} = Mid3;
246 // inst{7-0} = Lo8;
Jim Grosbachf391e9f2011-09-30 22:02:45 +0000247 Value = (Hi4 << 16) | (i << 26) | (Mid3 << 12) | (Lo8);
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000248 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
249 swapped |= (Value & 0x0000FFFF) << 16;
250 return swapped;
251 }
Owen Andersond7b3f582010-12-09 01:51:07 +0000252 case ARM::fixup_arm_ldst_pcrel_12:
Jason W Kim0c628c22010-12-01 22:46:50 +0000253 // ARM PC-relative values are offset by 8.
Owen Anderson05018c22010-12-09 20:27:52 +0000254 Value -= 4;
Owen Andersonfe7fac72010-12-09 21:34:47 +0000255 // FALLTHROUGH
Owen Andersond7b3f582010-12-09 01:51:07 +0000256 case ARM::fixup_t2_ldst_pcrel_12: {
257 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Anderson05018c22010-12-09 20:27:52 +0000258 Value -= 4;
Owen Andersond7b3f582010-12-09 01:51:07 +0000259 bool isAdd = true;
Jason W Kim0c628c22010-12-01 22:46:50 +0000260 if ((int64_t)Value < 0) {
261 Value = -Value;
262 isAdd = false;
263 }
264 assert ((Value < 4096) && "Out of range pc-relative fixup value!");
265 Value |= isAdd << 23;
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000266
Owen Andersond7b3f582010-12-09 01:51:07 +0000267 // Same addressing mode as fixup_arm_pcrel_10,
268 // but with 16-bit halfwords swapped.
269 if (Kind == ARM::fixup_t2_ldst_pcrel_12) {
270 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
271 swapped |= (Value & 0x0000FFFF) << 16;
272 return swapped;
273 }
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000274
Jason W Kim0c628c22010-12-01 22:46:50 +0000275 return Value;
276 }
Jim Grosbachd40963c2010-12-14 22:28:03 +0000277 case ARM::fixup_thumb_adr_pcrel_10:
278 return ((Value - 4) >> 2) & 0xff;
Jim Grosbachdff84b02010-12-02 00:28:45 +0000279 case ARM::fixup_arm_adr_pcrel_12: {
280 // ARM PC-relative values are offset by 8.
281 Value -= 8;
282 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
283 if ((int64_t)Value < 0) {
284 Value = -Value;
285 opc = 2; // 0b0010
286 }
287 assert(ARM_AM::getSOImmVal(Value) != -1 &&
288 "Out of range pc-relative fixup value!");
289 // Encode the immediate and shift the opcode into place.
290 return ARM_AM::getSOImmVal(Value) | (opc << 21);
291 }
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000292
Owen Andersona838a252010-12-14 00:36:49 +0000293 case ARM::fixup_t2_adr_pcrel_12: {
294 Value -= 4;
295 unsigned opc = 0;
296 if ((int64_t)Value < 0) {
297 Value = -Value;
298 opc = 5;
299 }
300
301 uint32_t out = (opc << 21);
Owen Anderson741ad152011-03-23 22:03:44 +0000302 out |= (Value & 0x800) << 15;
Owen Andersona838a252010-12-14 00:36:49 +0000303 out |= (Value & 0x700) << 4;
304 out |= (Value & 0x0FF);
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000305
Owen Andersona838a252010-12-14 00:36:49 +0000306 uint64_t swapped = (out & 0xFFFF0000) >> 16;
307 swapped |= (out & 0x0000FFFF) << 16;
308 return swapped;
309 }
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000310
Jason W Kim685c3502011-02-04 19:47:15 +0000311 case ARM::fixup_arm_condbranch:
312 case ARM::fixup_arm_uncondbranch:
Jason W Kim0c628c22010-12-01 22:46:50 +0000313 // These values don't encode the low two bits since they're always zero.
314 // Offset by 8 just as above.
Jim Grosbach662a8162010-12-06 23:57:07 +0000315 return 0xffffff & ((Value - 8) >> 2);
Owen Andersonc2666002010-12-13 19:31:11 +0000316 case ARM::fixup_t2_uncondbranch: {
Owen Anderson63ee2202010-12-10 23:02:28 +0000317 Value = Value - 4;
Owen Andersonfb20d892010-12-09 00:27:41 +0000318 Value >>= 1; // Low bit is not encoded.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000319
Jim Grosbach56a25352010-12-13 19:25:46 +0000320 uint32_t out = 0;
Owen Andersonc2666002010-12-13 19:31:11 +0000321 bool I = Value & 0x800000;
322 bool J1 = Value & 0x400000;
323 bool J2 = Value & 0x200000;
324 J1 ^= I;
325 J2 ^= I;
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000326
Owen Andersonc2666002010-12-13 19:31:11 +0000327 out |= I << 26; // S bit
328 out |= !J1 << 13; // J1 bit
329 out |= !J2 << 11; // J2 bit
330 out |= (Value & 0x1FF800) << 5; // imm6 field
331 out |= (Value & 0x0007FF); // imm11 field
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000332
Owen Andersonc2666002010-12-13 19:31:11 +0000333 uint64_t swapped = (out & 0xFFFF0000) >> 16;
334 swapped |= (out & 0x0000FFFF) << 16;
335 return swapped;
336 }
337 case ARM::fixup_t2_condbranch: {
338 Value = Value - 4;
339 Value >>= 1; // Low bit is not encoded.
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000340
Owen Andersonc2666002010-12-13 19:31:11 +0000341 uint64_t out = 0;
Owen Anderson8f079432010-12-09 01:02:09 +0000342 out |= (Value & 0x80000) << 7; // S bit
343 out |= (Value & 0x40000) >> 7; // J2 bit
344 out |= (Value & 0x20000) >> 4; // J1 bit
345 out |= (Value & 0x1F800) << 5; // imm6 field
346 out |= (Value & 0x007FF); // imm11 field
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000347
Jim Grosbach56a25352010-12-13 19:25:46 +0000348 uint32_t swapped = (out & 0xFFFF0000) >> 16;
Owen Andersonfb20d892010-12-09 00:27:41 +0000349 swapped |= (out & 0x0000FFFF) << 16;
350 return swapped;
351 }
Jim Grosbach662a8162010-12-06 23:57:07 +0000352 case ARM::fixup_arm_thumb_bl: {
353 // The value doesn't encode the low bit (always zero) and is offset by
354 // four. The value is encoded into disjoint bit positions in the destination
355 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000356 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000357 // BL: xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000358 //
Jim Grosbach662a8162010-12-06 23:57:07 +0000359 // Note that the halfwords are stored high first, low second; so we need
360 // to transpose the fixup value here to map properly.
Rafael Espindola298c8e12011-05-20 20:01:01 +0000361 unsigned isNeg = (int64_t(Value - 4) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000362 uint32_t Binary = 0;
363 Value = 0x3fffff & ((Value - 4) >> 1);
364 Binary = (Value & 0x7ff) << 16; // Low imm11 value.
365 Binary |= (Value & 0x1ffc00) >> 11; // High imm10 value.
366 Binary |= isNeg << 10; // Sign bit.
Bill Wendling09aa3f02010-12-09 00:39:08 +0000367 return Binary;
368 }
369 case ARM::fixup_arm_thumb_blx: {
370 // The value doesn't encode the low two bits (always zero) and is offset by
371 // four (see fixup_arm_thumb_cp). The value is encoded into disjoint bit
372 // positions in the destination opcode. x = unchanged, I = immediate value
373 // bit, S = sign extension bit, 0 = zero.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000374 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000375 // BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000376 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000377 // Note that the halfwords are stored high first, low second; so we need
378 // to transpose the fixup value here to map properly.
Rafael Espindola298c8e12011-05-20 20:01:01 +0000379 unsigned isNeg = (int64_t(Value-4) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000380 uint32_t Binary = 0;
381 Value = 0xfffff & ((Value - 2) >> 2);
382 Binary = (Value & 0x3ff) << 17; // Low imm10L value.
383 Binary |= (Value & 0xffc00) >> 10; // High imm10H value.
384 Binary |= isNeg << 10; // Sign bit.
Jim Grosbach662a8162010-12-06 23:57:07 +0000385 return Binary;
386 }
Bill Wendlingb8958b02010-12-08 01:57:09 +0000387 case ARM::fixup_arm_thumb_cp:
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000388 // Offset by 4, and don't encode the low two bits. Two bytes of that
389 // 'off by 4' is implicitly handled by the half-word ordering of the
390 // Thumb encoding, so we only need to adjust by 2 here.
391 return ((Value - 2) >> 2) & 0xff;
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000392 case ARM::fixup_arm_thumb_cb: {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000393 // Offset by 4 and don't encode the lower bit, which is always 0.
394 uint32_t Binary = (Value - 4) >> 1;
Owen Anderson86abd482010-12-14 19:42:53 +0000395 return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000396 }
Jim Grosbache2467172010-12-10 18:21:33 +0000397 case ARM::fixup_arm_thumb_br:
398 // Offset by 4 and don't encode the lower bit, which is always 0.
399 return ((Value - 4) >> 1) & 0x7ff;
Jim Grosbach01086452010-12-10 17:13:40 +0000400 case ARM::fixup_arm_thumb_bcc:
401 // Offset by 4 and don't encode the lower bit, which is always 0.
402 return ((Value - 4) >> 1) & 0xff;
Jim Grosbach2f196742011-12-19 23:06:24 +0000403 case ARM::fixup_arm_pcrel_10_unscaled: {
404 Value = Value - 8; // ARM fixups offset by an additional word and don't
405 // need to adjust for the half-word ordering.
406 bool isAdd = true;
407 if ((int64_t)Value < 0) {
408 Value = -Value;
409 isAdd = false;
410 }
411 assert ((Value < 256) && "Out of range pc-relative fixup value!");
412 return Value | (isAdd << 23);
413 }
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000414 case ARM::fixup_arm_pcrel_10:
Owen Andersone2e0f582010-12-10 22:46:47 +0000415 Value = Value - 4; // ARM fixups offset by an additional word and don't
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000416 // need to adjust for the half-word ordering.
417 // Fall through.
418 case ARM::fixup_t2_pcrel_10: {
419 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Andersone2e0f582010-12-10 22:46:47 +0000420 Value = Value - 4;
Jason W Kim0c628c22010-12-01 22:46:50 +0000421 bool isAdd = true;
422 if ((int64_t)Value < 0) {
423 Value = -Value;
424 isAdd = false;
425 }
426 // These values don't encode the low two bits since they're always zero.
427 Value >>= 2;
428 assert ((Value < 256) && "Out of range pc-relative fixup value!");
429 Value |= isAdd << 23;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000430
Jim Grosbach2f196742011-12-19 23:06:24 +0000431 // Same addressing mode as fixup_arm_pcrel_10, but with 16-bit halfwords
432 // swapped.
Owen Andersond8e351b2010-12-08 00:18:36 +0000433 if (Kind == ARM::fixup_t2_pcrel_10) {
Jim Grosbach56a25352010-12-13 19:25:46 +0000434 uint32_t swapped = (Value & 0xFFFF0000) >> 16;
Owen Anderson255eafb2010-12-08 00:21:33 +0000435 swapped |= (Value & 0x0000FFFF) << 16;
Owen Andersond8e351b2010-12-08 00:18:36 +0000436 return swapped;
437 }
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000438
Jason W Kim0c628c22010-12-01 22:46:50 +0000439 return Value;
440 }
441 }
442}
443
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000444namespace {
Bill Wendling52e635e2010-12-07 23:05:20 +0000445
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000446// FIXME: This should be in a separate file.
447// ELF is an ELF of course...
448class ELFARMAsmBackend : public ARMAsmBackend {
449public:
450 Triple::OSType OSType;
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +0000451 ELFARMAsmBackend(const Target &T, const StringRef TT,
452 Triple::OSType _OSType)
453 : ARMAsmBackend(T, TT), OSType(_OSType) { }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000454
Rafael Espindola179821a2010-12-06 19:08:48 +0000455 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000456 uint64_t Value) const;
457
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000458 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Rafael Espindolabff66a82010-12-18 03:27:34 +0000459 return createELFObjectWriter(new ARMELFObjectWriter(OSType), OS,
460 /*IsLittleEndian*/ true);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000461 }
462};
463
Bill Wendling52e635e2010-12-07 23:05:20 +0000464// FIXME: Raise this to share code between Darwin and ELF.
Rafael Espindola179821a2010-12-06 19:08:48 +0000465void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
466 unsigned DataSize, uint64_t Value) const {
Bill Wendling52e635e2010-12-07 23:05:20 +0000467 unsigned NumBytes = 4; // FIXME: 2 for Thumb
Bill Wendling52e635e2010-12-07 23:05:20 +0000468 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000469 if (!Value) return; // Doesn't change encoding.
Bill Wendling52e635e2010-12-07 23:05:20 +0000470
471 unsigned Offset = Fixup.getOffset();
Bill Wendling52e635e2010-12-07 23:05:20 +0000472
473 // For each byte of the fragment that the fixup touches, mask in the bits from
474 // the fixup value. The Value has been "split up" into the appropriate
475 // bitfields above.
476 for (unsigned i = 0; i != NumBytes; ++i)
477 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000478}
479
480// FIXME: This should be in a separate file.
481class DarwinARMAsmBackend : public ARMAsmBackend {
482public:
Owen Anderson17213242011-04-01 21:07:39 +0000483 const object::mach::CPUSubtypeARM Subtype;
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +0000484 DarwinARMAsmBackend(const Target &T, const StringRef TT,
485 object::mach::CPUSubtypeARM st)
486 : ARMAsmBackend(T, TT), Subtype(st) { }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000487
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000488 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jim Grosbach2fc68982011-06-22 20:14:52 +0000489 return createARMMachObjectWriter(OS, /*Is64Bit=*/false,
490 object::mach::CTM_ARM,
491 Subtype);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000492 }
493
Owen Anderson17213242011-04-01 21:07:39 +0000494 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
495 uint64_t Value) const;
496
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000497 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
498 return false;
499 }
500};
501
Bill Wendlingd832fa02010-12-07 23:11:00 +0000502/// getFixupKindNumBytes - The number of bytes the fixup may change.
Jim Grosbachc466b932010-11-11 18:04:49 +0000503static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000504 switch (Kind) {
Jim Grosbach662a8162010-12-06 23:57:07 +0000505 default:
506 llvm_unreachable("Unknown fixup kind!");
Bill Wendlingb8958b02010-12-08 01:57:09 +0000507
Jim Grosbach6ec6eeb2010-12-17 18:39:10 +0000508 case FK_Data_1:
Jim Grosbach01086452010-12-10 17:13:40 +0000509 case ARM::fixup_arm_thumb_bcc:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000510 case ARM::fixup_arm_thumb_cp:
Jim Grosbachd40963c2010-12-14 22:28:03 +0000511 case ARM::fixup_thumb_adr_pcrel_10:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000512 return 1;
513
Jim Grosbach6ec6eeb2010-12-17 18:39:10 +0000514 case FK_Data_2:
Jim Grosbache2467172010-12-10 18:21:33 +0000515 case ARM::fixup_arm_thumb_br:
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000516 case ARM::fixup_arm_thumb_cb:
Bill Wendlingdff2f712010-12-08 23:01:43 +0000517 return 2;
518
Jim Grosbach2f196742011-12-19 23:06:24 +0000519 case ARM::fixup_arm_pcrel_10_unscaled:
Jim Grosbach662a8162010-12-06 23:57:07 +0000520 case ARM::fixup_arm_ldst_pcrel_12:
521 case ARM::fixup_arm_pcrel_10:
522 case ARM::fixup_arm_adr_pcrel_12:
Jason W Kim685c3502011-02-04 19:47:15 +0000523 case ARM::fixup_arm_condbranch:
524 case ARM::fixup_arm_uncondbranch:
Jim Grosbach662a8162010-12-06 23:57:07 +0000525 return 3;
Bill Wendlingb8958b02010-12-08 01:57:09 +0000526
527 case FK_Data_4:
Owen Andersond7b3f582010-12-09 01:51:07 +0000528 case ARM::fixup_t2_ldst_pcrel_12:
Owen Andersonc2666002010-12-13 19:31:11 +0000529 case ARM::fixup_t2_condbranch:
530 case ARM::fixup_t2_uncondbranch:
Owen Andersond8e351b2010-12-08 00:18:36 +0000531 case ARM::fixup_t2_pcrel_10:
Owen Andersona838a252010-12-14 00:36:49 +0000532 case ARM::fixup_t2_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +0000533 case ARM::fixup_arm_thumb_bl:
Bill Wendling09aa3f02010-12-09 00:39:08 +0000534 case ARM::fixup_arm_thumb_blx:
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000535 case ARM::fixup_arm_movt_hi16:
536 case ARM::fixup_arm_movw_lo16:
537 case ARM::fixup_arm_movt_hi16_pcrel:
538 case ARM::fixup_arm_movw_lo16_pcrel:
539 case ARM::fixup_t2_movt_hi16:
540 case ARM::fixup_t2_movw_lo16:
541 case ARM::fixup_t2_movt_hi16_pcrel:
542 case ARM::fixup_t2_movw_lo16_pcrel:
Jim Grosbach662a8162010-12-06 23:57:07 +0000543 return 4;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000544 }
545}
546
Rafael Espindola179821a2010-12-06 19:08:48 +0000547void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
548 unsigned DataSize, uint64_t Value) const {
Jim Grosbachc466b932010-11-11 18:04:49 +0000549 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Jim Grosbach679cbd32010-11-09 01:37:15 +0000550 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000551 if (!Value) return; // Doesn't change encoding.
Jim Grosbach679cbd32010-11-09 01:37:15 +0000552
Bill Wendlingd832fa02010-12-07 23:11:00 +0000553 unsigned Offset = Fixup.getOffset();
554 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
555
Jim Grosbach679cbd32010-11-09 01:37:15 +0000556 // For each byte of the fragment that the fixup touches, mask in the
557 // bits from the fixup value.
558 for (unsigned i = 0; i != NumBytes; ++i)
Bill Wendlingd832fa02010-12-07 23:11:00 +0000559 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000560}
Bill Wendling52e635e2010-12-07 23:05:20 +0000561
Jim Grosbachf73fd722010-09-30 03:21:00 +0000562} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000563
Evan Cheng78c10ee2011-07-25 23:24:55 +0000564MCAsmBackend *llvm::createARMAsmBackend(const Target &T, StringRef TT) {
Owen Anderson17213242011-04-01 21:07:39 +0000565 Triple TheTriple(TT);
Daniel Dunbar912225e2011-04-19 21:14:45 +0000566
567 if (TheTriple.isOSDarwin()) {
Evan Chenga6eb2562011-06-14 18:08:33 +0000568 if (TheTriple.getArchName() == "armv4t" ||
569 TheTriple.getArchName() == "thumbv4t")
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +0000570 return new DarwinARMAsmBackend(T, TT, object::mach::CSARM_V4T);
Evan Chenga6eb2562011-06-14 18:08:33 +0000571 else if (TheTriple.getArchName() == "armv5e" ||
572 TheTriple.getArchName() == "thumbv5e")
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +0000573 return new DarwinARMAsmBackend(T, TT, object::mach::CSARM_V5TEJ);
Evan Chenga6eb2562011-06-14 18:08:33 +0000574 else if (TheTriple.getArchName() == "armv6" ||
Owen Anderson17213242011-04-01 21:07:39 +0000575 TheTriple.getArchName() == "thumbv6")
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +0000576 return new DarwinARMAsmBackend(T, TT, object::mach::CSARM_V6);
577 return new DarwinARMAsmBackend(T, TT, object::mach::CSARM_V7);
Owen Anderson17213242011-04-01 21:07:39 +0000578 }
Daniel Dunbar912225e2011-04-19 21:14:45 +0000579
580 if (TheTriple.isOSWindows())
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000581 assert(0 && "Windows not supported on ARM");
Daniel Dunbar912225e2011-04-19 21:14:45 +0000582
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +0000583 return new ELFARMAsmBackend(T, TT, Triple(TT).getOS());
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000584}