blob: 0eed821b767e190c98543086146585d66854156d [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 Grosbach681460f2011-11-01 01:24:45 +000066{ "fixup_arm_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar2761fc42010-12-16 03:20:06 +000067{ "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},
Jim Grosbach2abba842011-11-16 22:48:37 +000071{ "fixup_arm_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar2761fc42010-12-16 03:20:06 +000072{ "fixup_t2_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
73 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Jason W Kim685c3502011-02-04 19:47:15 +000074{ "fixup_arm_condbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
75{ "fixup_arm_uncondbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar2761fc42010-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 Grosbach90b5a082011-08-18 16:57:50 +000080{ "fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar2761fc42010-12-16 03:20:06 +000081{ "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbach67b95f92011-08-19 18:20:48 +000082{ "fixup_arm_thumb_cp", 0, 8, MCFixupKindInfo::FKF_IsPCRel },
Eric Christopherfea51fc2011-05-28 03:16:22 +000083{ "fixup_arm_thumb_bcc", 0, 8, MCFixupKindInfo::FKF_IsPCRel },
Evan Chengf3eb3bb2011-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 Dunbar2761fc42010-12-16 03:20:06 +000093 };
94
95 if (Kind < FirstTargetFixupKind)
Evan Cheng78c10ee2011-07-25 23:24:55 +000096 return MCAsmBackend::getFixupKindInfo(Kind);
Daniel Dunbar2761fc42010-12-16 03:20:06 +000097
98 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
99 "Invalid kind!");
100 return Infos[Kind - FirstTargetFixupKind];
101 }
102
Jason W Kimd4d4f4f2010-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 Grosbach3787a402010-09-30 17:45:51 +0000108
Jim Grosbach5be6d2a2010-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 Grosbach3787a402010-09-30 17:45:51 +0000119 }
Jim Grosbach5be6d2a2010-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 Kimd4d4f4f2010-09-30 02:17:26 +0000124};
Chris Lattnerb75c6512010-11-17 05:41:32 +0000125} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000126
Jim Grosbachf503ef62011-12-05 23:45:46 +0000127static unsigned getRelaxedOpcode(unsigned Op) {
128 switch (Op) {
129 default: return Op;
130 case ARM::tBcc: return ARM::t2Bcc;
131 }
132}
133
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000134bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
Jim Grosbachf503ef62011-12-05 23:45:46 +0000135 if (getRelaxedOpcode(Inst.getOpcode()) != Inst.getOpcode())
136 return true;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000137 return false;
138}
139
140void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
Jim Grosbachf503ef62011-12-05 23:45:46 +0000141 unsigned RelaxedOp = getRelaxedOpcode(Inst.getOpcode());
142
143 // Sanity check w/ diagnostic if we get here w/ a bogus instruction.
144 if (RelaxedOp == Inst.getOpcode()) {
145 SmallString<256> Tmp;
146 raw_svector_ostream OS(Tmp);
147 Inst.dump_pretty(OS);
148 OS << "\n";
149 report_fatal_error("unexpected instruction to relax: " + OS.str());
150 }
151
152 // The instructions we're relaxing have (so far) the same operands.
153 // We just need to update to the proper opcode.
154 Res = Inst;
155 Res.setOpcode(RelaxedOp);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000156}
157
158bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +0000159 const uint16_t Thumb1_16bitNopEncoding = 0x46c0; // using MOV r8,r8
160 const uint16_t Thumb2_16bitNopEncoding = 0xbf00; // NOP
161 const uint32_t ARMv4_NopEncoding = 0xe1a0000; // using MOV r0,r0
Jim Grosbachb84acd22011-11-16 22:40:25 +0000162 const uint32_t ARMv6T2_NopEncoding = 0xe320f000; // NOP
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000163 if (isThumb()) {
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +0000164 const uint16_t nopEncoding = hasNOP() ? Thumb2_16bitNopEncoding
165 : Thumb1_16bitNopEncoding;
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000166 uint64_t NumNops = Count / 2;
167 for (uint64_t i = 0; i != NumNops; ++i)
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +0000168 OW->Write16(nopEncoding);
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000169 if (Count & 1)
170 OW->Write8(0);
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000171 return true;
172 }
173 // ARM mode
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +0000174 const uint32_t nopEncoding = hasNOP() ? ARMv6T2_NopEncoding
175 : ARMv4_NopEncoding;
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000176 uint64_t NumNops = Count / 4;
177 for (uint64_t i = 0; i != NumNops; ++i)
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +0000178 OW->Write32(nopEncoding);
179 // FIXME: should this function return false when unable to write exactly
180 // 'Count' bytes with NOP encodings?
Jim Grosbacha3dbd3a2010-12-17 19:03:02 +0000181 switch (Count % 4) {
182 default: break; // No leftover bytes to write
183 case 1: OW->Write8(0); break;
184 case 2: OW->Write16(0); break;
185 case 3: OW->Write16(0); OW->Write8(0xa0); break;
186 }
187
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000188 return true;
Jim Grosbach87dc3aa2010-09-30 03:20:34 +0000189}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000190
Jason W Kim0c628c22010-12-01 22:46:50 +0000191static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
192 switch (Kind) {
193 default:
194 llvm_unreachable("Unknown fixup kind!");
Jim Grosbach6ec6eeb2010-12-17 18:39:10 +0000195 case FK_Data_1:
196 case FK_Data_2:
Jason W Kim0c628c22010-12-01 22:46:50 +0000197 case FK_Data_4:
Jason W Kim0c628c22010-12-01 22:46:50 +0000198 return Value;
Jason W Kim2ccf1482010-12-03 19:40:23 +0000199 case ARM::fixup_arm_movt_hi16:
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000200 Value >>= 16;
201 // Fallthrough
202 case ARM::fixup_arm_movw_lo16:
Jason W Kim861b9c62011-05-19 20:55:25 +0000203 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim86a97f22011-01-12 00:19:25 +0000204 case ARM::fixup_arm_movw_lo16_pcrel: {
Jason W Kim2ccf1482010-12-03 19:40:23 +0000205 unsigned Hi4 = (Value & 0xF000) >> 12;
206 unsigned Lo12 = Value & 0x0FFF;
207 // inst{19-16} = Hi4;
208 // inst{11-0} = Lo12;
209 Value = (Hi4 << 16) | (Lo12);
210 return Value;
211 }
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000212 case ARM::fixup_t2_movt_hi16:
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000213 Value >>= 16;
214 // Fallthrough
215 case ARM::fixup_t2_movw_lo16:
Jim Grosbach8b454562011-06-24 20:06:59 +0000216 case ARM::fixup_t2_movt_hi16_pcrel: //FIXME: Shouldn't this be shifted like
217 // the other hi16 fixup?
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000218 case ARM::fixup_t2_movw_lo16_pcrel: {
219 unsigned Hi4 = (Value & 0xF000) >> 12;
220 unsigned i = (Value & 0x800) >> 11;
221 unsigned Mid3 = (Value & 0x700) >> 8;
222 unsigned Lo8 = Value & 0x0FF;
223 // inst{19-16} = Hi4;
224 // inst{26} = i;
225 // inst{14-12} = Mid3;
226 // inst{7-0} = Lo8;
Jim Grosbachf391e9f2011-09-30 22:02:45 +0000227 Value = (Hi4 << 16) | (i << 26) | (Mid3 << 12) | (Lo8);
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000228 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
229 swapped |= (Value & 0x0000FFFF) << 16;
230 return swapped;
231 }
Owen Andersond7b3f582010-12-09 01:51:07 +0000232 case ARM::fixup_arm_ldst_pcrel_12:
Jason W Kim0c628c22010-12-01 22:46:50 +0000233 // ARM PC-relative values are offset by 8.
Owen Anderson05018c22010-12-09 20:27:52 +0000234 Value -= 4;
Owen Andersonfe7fac72010-12-09 21:34:47 +0000235 // FALLTHROUGH
Owen Andersond7b3f582010-12-09 01:51:07 +0000236 case ARM::fixup_t2_ldst_pcrel_12: {
237 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Anderson05018c22010-12-09 20:27:52 +0000238 Value -= 4;
Owen Andersond7b3f582010-12-09 01:51:07 +0000239 bool isAdd = true;
Jason W Kim0c628c22010-12-01 22:46:50 +0000240 if ((int64_t)Value < 0) {
241 Value = -Value;
242 isAdd = false;
243 }
244 assert ((Value < 4096) && "Out of range pc-relative fixup value!");
245 Value |= isAdd << 23;
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000246
Owen Andersond7b3f582010-12-09 01:51:07 +0000247 // Same addressing mode as fixup_arm_pcrel_10,
248 // but with 16-bit halfwords swapped.
249 if (Kind == ARM::fixup_t2_ldst_pcrel_12) {
250 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
251 swapped |= (Value & 0x0000FFFF) << 16;
252 return swapped;
253 }
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000254
Jason W Kim0c628c22010-12-01 22:46:50 +0000255 return Value;
256 }
Jim Grosbachd40963c2010-12-14 22:28:03 +0000257 case ARM::fixup_thumb_adr_pcrel_10:
258 return ((Value - 4) >> 2) & 0xff;
Jim Grosbachdff84b02010-12-02 00:28:45 +0000259 case ARM::fixup_arm_adr_pcrel_12: {
260 // ARM PC-relative values are offset by 8.
261 Value -= 8;
262 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
263 if ((int64_t)Value < 0) {
264 Value = -Value;
265 opc = 2; // 0b0010
266 }
267 assert(ARM_AM::getSOImmVal(Value) != -1 &&
268 "Out of range pc-relative fixup value!");
269 // Encode the immediate and shift the opcode into place.
270 return ARM_AM::getSOImmVal(Value) | (opc << 21);
271 }
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000272
Owen Andersona838a252010-12-14 00:36:49 +0000273 case ARM::fixup_t2_adr_pcrel_12: {
274 Value -= 4;
275 unsigned opc = 0;
276 if ((int64_t)Value < 0) {
277 Value = -Value;
278 opc = 5;
279 }
280
281 uint32_t out = (opc << 21);
Owen Anderson741ad152011-03-23 22:03:44 +0000282 out |= (Value & 0x800) << 15;
Owen Andersona838a252010-12-14 00:36:49 +0000283 out |= (Value & 0x700) << 4;
284 out |= (Value & 0x0FF);
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000285
Owen Andersona838a252010-12-14 00:36:49 +0000286 uint64_t swapped = (out & 0xFFFF0000) >> 16;
287 swapped |= (out & 0x0000FFFF) << 16;
288 return swapped;
289 }
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000290
Jason W Kim685c3502011-02-04 19:47:15 +0000291 case ARM::fixup_arm_condbranch:
292 case ARM::fixup_arm_uncondbranch:
Jason W Kim0c628c22010-12-01 22:46:50 +0000293 // These values don't encode the low two bits since they're always zero.
294 // Offset by 8 just as above.
Jim Grosbach662a8162010-12-06 23:57:07 +0000295 return 0xffffff & ((Value - 8) >> 2);
Owen Andersonc2666002010-12-13 19:31:11 +0000296 case ARM::fixup_t2_uncondbranch: {
Owen Anderson63ee2202010-12-10 23:02:28 +0000297 Value = Value - 4;
Owen Andersonfb20d892010-12-09 00:27:41 +0000298 Value >>= 1; // Low bit is not encoded.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000299
Jim Grosbach56a25352010-12-13 19:25:46 +0000300 uint32_t out = 0;
Owen Andersonc2666002010-12-13 19:31:11 +0000301 bool I = Value & 0x800000;
302 bool J1 = Value & 0x400000;
303 bool J2 = Value & 0x200000;
304 J1 ^= I;
305 J2 ^= I;
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000306
Owen Andersonc2666002010-12-13 19:31:11 +0000307 out |= I << 26; // S bit
308 out |= !J1 << 13; // J1 bit
309 out |= !J2 << 11; // J2 bit
310 out |= (Value & 0x1FF800) << 5; // imm6 field
311 out |= (Value & 0x0007FF); // imm11 field
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000312
Owen Andersonc2666002010-12-13 19:31:11 +0000313 uint64_t swapped = (out & 0xFFFF0000) >> 16;
314 swapped |= (out & 0x0000FFFF) << 16;
315 return swapped;
316 }
317 case ARM::fixup_t2_condbranch: {
318 Value = Value - 4;
319 Value >>= 1; // Low bit is not encoded.
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000320
Owen Andersonc2666002010-12-13 19:31:11 +0000321 uint64_t out = 0;
Owen Anderson8f079432010-12-09 01:02:09 +0000322 out |= (Value & 0x80000) << 7; // S bit
323 out |= (Value & 0x40000) >> 7; // J2 bit
324 out |= (Value & 0x20000) >> 4; // J1 bit
325 out |= (Value & 0x1F800) << 5; // imm6 field
326 out |= (Value & 0x007FF); // imm11 field
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000327
Jim Grosbach56a25352010-12-13 19:25:46 +0000328 uint32_t swapped = (out & 0xFFFF0000) >> 16;
Owen Andersonfb20d892010-12-09 00:27:41 +0000329 swapped |= (out & 0x0000FFFF) << 16;
330 return swapped;
331 }
Jim Grosbach662a8162010-12-06 23:57:07 +0000332 case ARM::fixup_arm_thumb_bl: {
333 // The value doesn't encode the low bit (always zero) and is offset by
334 // four. The value is encoded into disjoint bit positions in the destination
335 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000336 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000337 // BL: xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000338 //
Jim Grosbach662a8162010-12-06 23:57:07 +0000339 // Note that the halfwords are stored high first, low second; so we need
340 // to transpose the fixup value here to map properly.
Rafael Espindola298c8e12011-05-20 20:01:01 +0000341 unsigned isNeg = (int64_t(Value - 4) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000342 uint32_t Binary = 0;
343 Value = 0x3fffff & ((Value - 4) >> 1);
344 Binary = (Value & 0x7ff) << 16; // Low imm11 value.
345 Binary |= (Value & 0x1ffc00) >> 11; // High imm10 value.
346 Binary |= isNeg << 10; // Sign bit.
Bill Wendling09aa3f02010-12-09 00:39:08 +0000347 return Binary;
348 }
349 case ARM::fixup_arm_thumb_blx: {
350 // The value doesn't encode the low two bits (always zero) and is offset by
351 // four (see fixup_arm_thumb_cp). The value is encoded into disjoint bit
352 // positions in the destination opcode. x = unchanged, I = immediate value
353 // bit, S = sign extension bit, 0 = zero.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000354 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000355 // BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000356 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000357 // Note that the halfwords are stored high first, low second; so we need
358 // to transpose the fixup value here to map properly.
Rafael Espindola298c8e12011-05-20 20:01:01 +0000359 unsigned isNeg = (int64_t(Value-4) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000360 uint32_t Binary = 0;
361 Value = 0xfffff & ((Value - 2) >> 2);
362 Binary = (Value & 0x3ff) << 17; // Low imm10L value.
363 Binary |= (Value & 0xffc00) >> 10; // High imm10H value.
364 Binary |= isNeg << 10; // Sign bit.
Jim Grosbach662a8162010-12-06 23:57:07 +0000365 return Binary;
366 }
Bill Wendlingb8958b02010-12-08 01:57:09 +0000367 case ARM::fixup_arm_thumb_cp:
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000368 // Offset by 4, and don't encode the low two bits. Two bytes of that
369 // 'off by 4' is implicitly handled by the half-word ordering of the
370 // Thumb encoding, so we only need to adjust by 2 here.
371 return ((Value - 2) >> 2) & 0xff;
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000372 case ARM::fixup_arm_thumb_cb: {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000373 // Offset by 4 and don't encode the lower bit, which is always 0.
374 uint32_t Binary = (Value - 4) >> 1;
Owen Anderson86abd482010-12-14 19:42:53 +0000375 return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000376 }
Jim Grosbache2467172010-12-10 18:21:33 +0000377 case ARM::fixup_arm_thumb_br:
378 // Offset by 4 and don't encode the lower bit, which is always 0.
379 return ((Value - 4) >> 1) & 0x7ff;
Jim Grosbach01086452010-12-10 17:13:40 +0000380 case ARM::fixup_arm_thumb_bcc:
381 // Offset by 4 and don't encode the lower bit, which is always 0.
382 return ((Value - 4) >> 1) & 0xff;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000383 case ARM::fixup_arm_pcrel_10:
Owen Andersone2e0f582010-12-10 22:46:47 +0000384 Value = Value - 4; // ARM fixups offset by an additional word and don't
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000385 // need to adjust for the half-word ordering.
386 // Fall through.
387 case ARM::fixup_t2_pcrel_10: {
388 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Andersone2e0f582010-12-10 22:46:47 +0000389 Value = Value - 4;
Jason W Kim0c628c22010-12-01 22:46:50 +0000390 bool isAdd = true;
391 if ((int64_t)Value < 0) {
392 Value = -Value;
393 isAdd = false;
394 }
395 // These values don't encode the low two bits since they're always zero.
396 Value >>= 2;
397 assert ((Value < 256) && "Out of range pc-relative fixup value!");
398 Value |= isAdd << 23;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000399
Owen Andersoncc78f5c2010-12-08 19:31:11 +0000400 // Same addressing mode as fixup_arm_pcrel_10,
401 // but with 16-bit halfwords swapped.
Owen Andersond8e351b2010-12-08 00:18:36 +0000402 if (Kind == ARM::fixup_t2_pcrel_10) {
Jim Grosbach56a25352010-12-13 19:25:46 +0000403 uint32_t swapped = (Value & 0xFFFF0000) >> 16;
Owen Anderson255eafb2010-12-08 00:21:33 +0000404 swapped |= (Value & 0x0000FFFF) << 16;
Owen Andersond8e351b2010-12-08 00:18:36 +0000405 return swapped;
406 }
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000407
Jason W Kim0c628c22010-12-01 22:46:50 +0000408 return Value;
409 }
410 }
411}
412
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000413namespace {
Bill Wendling52e635e2010-12-07 23:05:20 +0000414
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000415// FIXME: This should be in a separate file.
416// ELF is an ELF of course...
417class ELFARMAsmBackend : public ARMAsmBackend {
418public:
419 Triple::OSType OSType;
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +0000420 ELFARMAsmBackend(const Target &T, const StringRef TT,
421 Triple::OSType _OSType)
422 : ARMAsmBackend(T, TT), OSType(_OSType) { }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000423
Rafael Espindola179821a2010-12-06 19:08:48 +0000424 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000425 uint64_t Value) const;
426
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000427 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Rafael Espindolabff66a82010-12-18 03:27:34 +0000428 return createELFObjectWriter(new ARMELFObjectWriter(OSType), OS,
429 /*IsLittleEndian*/ true);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000430 }
431};
432
Bill Wendling52e635e2010-12-07 23:05:20 +0000433// FIXME: Raise this to share code between Darwin and ELF.
Rafael Espindola179821a2010-12-06 19:08:48 +0000434void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
435 unsigned DataSize, uint64_t Value) const {
Bill Wendling52e635e2010-12-07 23:05:20 +0000436 unsigned NumBytes = 4; // FIXME: 2 for Thumb
Bill Wendling52e635e2010-12-07 23:05:20 +0000437 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000438 if (!Value) return; // Doesn't change encoding.
Bill Wendling52e635e2010-12-07 23:05:20 +0000439
440 unsigned Offset = Fixup.getOffset();
Bill Wendling52e635e2010-12-07 23:05:20 +0000441
442 // For each byte of the fragment that the fixup touches, mask in the bits from
443 // the fixup value. The Value has been "split up" into the appropriate
444 // bitfields above.
445 for (unsigned i = 0; i != NumBytes; ++i)
446 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000447}
448
449// FIXME: This should be in a separate file.
450class DarwinARMAsmBackend : public ARMAsmBackend {
451public:
Owen Anderson17213242011-04-01 21:07:39 +0000452 const object::mach::CPUSubtypeARM Subtype;
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +0000453 DarwinARMAsmBackend(const Target &T, const StringRef TT,
454 object::mach::CPUSubtypeARM st)
455 : ARMAsmBackend(T, TT), Subtype(st) { }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000456
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000457 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jim Grosbach2fc68982011-06-22 20:14:52 +0000458 return createARMMachObjectWriter(OS, /*Is64Bit=*/false,
459 object::mach::CTM_ARM,
460 Subtype);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000461 }
462
Owen Anderson17213242011-04-01 21:07:39 +0000463 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
464 uint64_t Value) const;
465
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000466 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
467 return false;
468 }
469};
470
Bill Wendlingd832fa02010-12-07 23:11:00 +0000471/// getFixupKindNumBytes - The number of bytes the fixup may change.
Jim Grosbachc466b932010-11-11 18:04:49 +0000472static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000473 switch (Kind) {
Jim Grosbach662a8162010-12-06 23:57:07 +0000474 default:
475 llvm_unreachable("Unknown fixup kind!");
Bill Wendlingb8958b02010-12-08 01:57:09 +0000476
Jim Grosbach6ec6eeb2010-12-17 18:39:10 +0000477 case FK_Data_1:
Jim Grosbach01086452010-12-10 17:13:40 +0000478 case ARM::fixup_arm_thumb_bcc:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000479 case ARM::fixup_arm_thumb_cp:
Jim Grosbachd40963c2010-12-14 22:28:03 +0000480 case ARM::fixup_thumb_adr_pcrel_10:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000481 return 1;
482
Jim Grosbach6ec6eeb2010-12-17 18:39:10 +0000483 case FK_Data_2:
Jim Grosbache2467172010-12-10 18:21:33 +0000484 case ARM::fixup_arm_thumb_br:
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000485 case ARM::fixup_arm_thumb_cb:
Bill Wendlingdff2f712010-12-08 23:01:43 +0000486 return 2;
487
Jim Grosbach662a8162010-12-06 23:57:07 +0000488 case ARM::fixup_arm_ldst_pcrel_12:
489 case ARM::fixup_arm_pcrel_10:
490 case ARM::fixup_arm_adr_pcrel_12:
Jason W Kim685c3502011-02-04 19:47:15 +0000491 case ARM::fixup_arm_condbranch:
492 case ARM::fixup_arm_uncondbranch:
Jim Grosbach662a8162010-12-06 23:57:07 +0000493 return 3;
Bill Wendlingb8958b02010-12-08 01:57:09 +0000494
495 case FK_Data_4:
Owen Andersond7b3f582010-12-09 01:51:07 +0000496 case ARM::fixup_t2_ldst_pcrel_12:
Owen Andersonc2666002010-12-13 19:31:11 +0000497 case ARM::fixup_t2_condbranch:
498 case ARM::fixup_t2_uncondbranch:
Owen Andersond8e351b2010-12-08 00:18:36 +0000499 case ARM::fixup_t2_pcrel_10:
Owen Andersona838a252010-12-14 00:36:49 +0000500 case ARM::fixup_t2_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +0000501 case ARM::fixup_arm_thumb_bl:
Bill Wendling09aa3f02010-12-09 00:39:08 +0000502 case ARM::fixup_arm_thumb_blx:
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000503 case ARM::fixup_arm_movt_hi16:
504 case ARM::fixup_arm_movw_lo16:
505 case ARM::fixup_arm_movt_hi16_pcrel:
506 case ARM::fixup_arm_movw_lo16_pcrel:
507 case ARM::fixup_t2_movt_hi16:
508 case ARM::fixup_t2_movw_lo16:
509 case ARM::fixup_t2_movt_hi16_pcrel:
510 case ARM::fixup_t2_movw_lo16_pcrel:
Jim Grosbach662a8162010-12-06 23:57:07 +0000511 return 4;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000512 }
513}
514
Rafael Espindola179821a2010-12-06 19:08:48 +0000515void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
516 unsigned DataSize, uint64_t Value) const {
Jim Grosbachc466b932010-11-11 18:04:49 +0000517 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Jim Grosbach679cbd32010-11-09 01:37:15 +0000518 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000519 if (!Value) return; // Doesn't change encoding.
Jim Grosbach679cbd32010-11-09 01:37:15 +0000520
Bill Wendlingd832fa02010-12-07 23:11:00 +0000521 unsigned Offset = Fixup.getOffset();
522 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
523
Jim Grosbach679cbd32010-11-09 01:37:15 +0000524 // For each byte of the fragment that the fixup touches, mask in the
525 // bits from the fixup value.
526 for (unsigned i = 0; i != NumBytes; ++i)
Bill Wendlingd832fa02010-12-07 23:11:00 +0000527 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000528}
Bill Wendling52e635e2010-12-07 23:05:20 +0000529
Jim Grosbachf73fd722010-09-30 03:21:00 +0000530} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000531
Evan Cheng78c10ee2011-07-25 23:24:55 +0000532MCAsmBackend *llvm::createARMAsmBackend(const Target &T, StringRef TT) {
Owen Anderson17213242011-04-01 21:07:39 +0000533 Triple TheTriple(TT);
Daniel Dunbar912225e2011-04-19 21:14:45 +0000534
535 if (TheTriple.isOSDarwin()) {
Evan Chenga6eb2562011-06-14 18:08:33 +0000536 if (TheTriple.getArchName() == "armv4t" ||
537 TheTriple.getArchName() == "thumbv4t")
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +0000538 return new DarwinARMAsmBackend(T, TT, object::mach::CSARM_V4T);
Evan Chenga6eb2562011-06-14 18:08:33 +0000539 else if (TheTriple.getArchName() == "armv5e" ||
540 TheTriple.getArchName() == "thumbv5e")
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +0000541 return new DarwinARMAsmBackend(T, TT, object::mach::CSARM_V5TEJ);
Evan Chenga6eb2562011-06-14 18:08:33 +0000542 else if (TheTriple.getArchName() == "armv6" ||
Owen Anderson17213242011-04-01 21:07:39 +0000543 TheTriple.getArchName() == "thumbv6")
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +0000544 return new DarwinARMAsmBackend(T, TT, object::mach::CSARM_V6);
545 return new DarwinARMAsmBackend(T, TT, object::mach::CSARM_V7);
Owen Anderson17213242011-04-01 21:07:39 +0000546 }
Daniel Dunbar912225e2011-04-19 21:14:45 +0000547
548 if (TheTriple.isOSWindows())
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000549 assert(0 && "Windows not supported on ARM");
Daniel Dunbar912225e2011-04-19 21:14:45 +0000550
Jim Grosbachd0d3f7e2011-08-16 17:06:20 +0000551 return new ELFARMAsmBackend(T, TT, Triple(TT).getOS());
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000552}