blob: 230364d32c0b4cc494403cf979dd1ad114b47e3c [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"
Rafael Espindolaf230df92010-10-16 18:23:53 +000019#include "llvm/MC/MCObjectFormat.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"
Daniel Dunbar36d76a82010-11-27 04:38:36 +000023#include "llvm/Object/MachOFormat.h"
Wesley Peckeecb8582010-10-22 15:52:49 +000024#include "llvm/Support/ELF.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000025#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/raw_ostream.h"
Jim Grosbachdff84b02010-12-02 00:28:45 +000027#include "llvm/Target/TargetAsmBackend.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000028#include "llvm/Target/TargetRegistry.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000029using namespace llvm;
30
31namespace {
Daniel Dunbarae5abd52010-12-16 16:09:19 +000032class ARMMachObjectWriter : public MCMachObjectTargetWriter {
Daniel Dunbar5d05d972010-12-16 17:21:02 +000033public:
34 ARMMachObjectWriter(bool Is64Bit, uint32_t CPUType,
35 uint32_t CPUSubtype)
Daniel Dunbar1139d502010-12-17 06:00:24 +000036 : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype,
37 /*UseAggressiveSymbolFolding=*/true) {}
Daniel Dunbarae5abd52010-12-16 16:09:19 +000038};
39
Rafael Espindola6024c972010-12-17 17:45:22 +000040class ARMELFObjectWriter : public MCELFObjectTargetWriter {
41public:
42 ARMELFObjectWriter() : MCELFObjectTargetWriter() {}
43};
44
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000045class ARMAsmBackend : public TargetAsmBackend {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000046 bool isThumbMode; // Currently emitting Thumb code.
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000047public:
Jim Grosbach022ab372010-12-08 15:36:45 +000048 ARMAsmBackend(const Target &T) : TargetAsmBackend(), isThumbMode(false) {}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000049
Daniel Dunbar2761fc42010-12-16 03:20:06 +000050 unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
51
52 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
53 const static MCFixupKindInfo Infos[ARM::NumTargetFixupKinds] = {
54// This table *must* be in the order that the fixup_* kinds are defined in
55// ARMFixupKinds.h.
56//
57// Name Offset (bits) Size (bits) Flags
58{ "fixup_arm_ldst_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
59{ "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
60 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
61{ "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
62{ "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
63 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
64{ "fixup_thumb_adr_pcrel_10",0, 8, MCFixupKindInfo::FKF_IsPCRel |
65 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
66{ "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
67{ "fixup_t2_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
68 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
69{ "fixup_arm_branch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
70{ "fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
71{ "fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
72{ "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
73{ "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
74{ "fixup_arm_thumb_blx", 7, 21, MCFixupKindInfo::FKF_IsPCRel },
75{ "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
76{ "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
77{ "fixup_arm_thumb_bcc", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
78{ "fixup_arm_movt_hi16", 0, 16, 0 },
79{ "fixup_arm_movw_lo16", 0, 16, 0 },
80 };
81
82 if (Kind < FirstTargetFixupKind)
83 return TargetAsmBackend::getFixupKindInfo(Kind);
84
85 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
86 "Invalid kind!");
87 return Infos[Kind - FirstTargetFixupKind];
88 }
89
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000090 bool MayNeedRelaxation(const MCInst &Inst) const;
91
92 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
93
94 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
Jim Grosbach3787a402010-09-30 17:45:51 +000095
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000096 void HandleAssemblerFlag(MCAssemblerFlag Flag) {
97 switch (Flag) {
98 default: break;
99 case MCAF_Code16:
100 setIsThumb(true);
101 break;
102 case MCAF_Code32:
103 setIsThumb(false);
104 break;
105 }
Jim Grosbach3787a402010-09-30 17:45:51 +0000106 }
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000107
108 unsigned getPointerSize() const { return 4; }
109 bool isThumb() const { return isThumbMode; }
110 void setIsThumb(bool it) { isThumbMode = it; }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000111};
Chris Lattnerb75c6512010-11-17 05:41:32 +0000112} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000113
114bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
115 // FIXME: Thumb targets, different move constant targets..
116 return false;
117}
118
119void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
120 assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
121 return;
122}
123
124bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000125 if (isThumb()) {
126 assert (((Count & 1) == 0) && "Unaligned Nop data fragment!");
127 // FIXME: 0xbf00 is the ARMv7 value. For v6 and before, we'll need to
128 // use 0x46c0 (which is a 'mov r8, r8' insn).
129 Count /= 2;
130 for (uint64_t i = 0; i != Count; ++i)
131 OW->Write16(0xbf00);
132 return true;
133 }
134 // ARM mode
135 Count /= 4;
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000136 for (uint64_t i = 0; i != Count; ++i)
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000137 OW->Write32(0xe1a00000);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000138 return true;
Jim Grosbach87dc3aa2010-09-30 03:20:34 +0000139}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000140
Jason W Kim0c628c22010-12-01 22:46:50 +0000141static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
142 switch (Kind) {
143 default:
144 llvm_unreachable("Unknown fixup kind!");
145 case FK_Data_4:
Jason W Kim0c628c22010-12-01 22:46:50 +0000146 return Value;
Jason W Kim2ccf1482010-12-03 19:40:23 +0000147 case ARM::fixup_arm_movt_hi16:
148 case ARM::fixup_arm_movw_lo16: {
149 unsigned Hi4 = (Value & 0xF000) >> 12;
150 unsigned Lo12 = Value & 0x0FFF;
151 // inst{19-16} = Hi4;
152 // inst{11-0} = Lo12;
153 Value = (Hi4 << 16) | (Lo12);
154 return Value;
155 }
Owen Andersond7b3f582010-12-09 01:51:07 +0000156 case ARM::fixup_arm_ldst_pcrel_12:
Jason W Kim0c628c22010-12-01 22:46:50 +0000157 // ARM PC-relative values are offset by 8.
Owen Anderson05018c22010-12-09 20:27:52 +0000158 Value -= 4;
Owen Andersonfe7fac72010-12-09 21:34:47 +0000159 // FALLTHROUGH
Owen Andersond7b3f582010-12-09 01:51:07 +0000160 case ARM::fixup_t2_ldst_pcrel_12: {
161 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Anderson05018c22010-12-09 20:27:52 +0000162 Value -= 4;
Owen Andersond7b3f582010-12-09 01:51:07 +0000163 bool isAdd = true;
Jason W Kim0c628c22010-12-01 22:46:50 +0000164 if ((int64_t)Value < 0) {
165 Value = -Value;
166 isAdd = false;
167 }
168 assert ((Value < 4096) && "Out of range pc-relative fixup value!");
169 Value |= isAdd << 23;
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000170
Owen Andersond7b3f582010-12-09 01:51:07 +0000171 // Same addressing mode as fixup_arm_pcrel_10,
172 // but with 16-bit halfwords swapped.
173 if (Kind == ARM::fixup_t2_ldst_pcrel_12) {
174 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
175 swapped |= (Value & 0x0000FFFF) << 16;
176 return swapped;
177 }
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000178
Jason W Kim0c628c22010-12-01 22:46:50 +0000179 return Value;
180 }
Jim Grosbachd40963c2010-12-14 22:28:03 +0000181 case ARM::fixup_thumb_adr_pcrel_10:
182 return ((Value - 4) >> 2) & 0xff;
Jim Grosbachdff84b02010-12-02 00:28:45 +0000183 case ARM::fixup_arm_adr_pcrel_12: {
184 // ARM PC-relative values are offset by 8.
185 Value -= 8;
186 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
187 if ((int64_t)Value < 0) {
188 Value = -Value;
189 opc = 2; // 0b0010
190 }
191 assert(ARM_AM::getSOImmVal(Value) != -1 &&
192 "Out of range pc-relative fixup value!");
193 // Encode the immediate and shift the opcode into place.
194 return ARM_AM::getSOImmVal(Value) | (opc << 21);
195 }
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000196
Owen Andersona838a252010-12-14 00:36:49 +0000197 case ARM::fixup_t2_adr_pcrel_12: {
198 Value -= 4;
199 unsigned opc = 0;
200 if ((int64_t)Value < 0) {
201 Value = -Value;
202 opc = 5;
203 }
204
205 uint32_t out = (opc << 21);
206 out |= (Value & 0x800) << 14;
207 out |= (Value & 0x700) << 4;
208 out |= (Value & 0x0FF);
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000209
Owen Andersona838a252010-12-14 00:36:49 +0000210 uint64_t swapped = (out & 0xFFFF0000) >> 16;
211 swapped |= (out & 0x0000FFFF) << 16;
212 return swapped;
213 }
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000214
Jason W Kim0c628c22010-12-01 22:46:50 +0000215 case ARM::fixup_arm_branch:
216 // These values don't encode the low two bits since they're always zero.
217 // Offset by 8 just as above.
Jim Grosbach662a8162010-12-06 23:57:07 +0000218 return 0xffffff & ((Value - 8) >> 2);
Owen Andersonc2666002010-12-13 19:31:11 +0000219 case ARM::fixup_t2_uncondbranch: {
Owen Anderson63ee2202010-12-10 23:02:28 +0000220 Value = Value - 4;
Owen Andersonfb20d892010-12-09 00:27:41 +0000221 Value >>= 1; // Low bit is not encoded.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000222
Jim Grosbach56a25352010-12-13 19:25:46 +0000223 uint32_t out = 0;
Owen Andersonc2666002010-12-13 19:31:11 +0000224 bool I = Value & 0x800000;
225 bool J1 = Value & 0x400000;
226 bool J2 = Value & 0x200000;
227 J1 ^= I;
228 J2 ^= I;
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000229
Owen Andersonc2666002010-12-13 19:31:11 +0000230 out |= I << 26; // S bit
231 out |= !J1 << 13; // J1 bit
232 out |= !J2 << 11; // J2 bit
233 out |= (Value & 0x1FF800) << 5; // imm6 field
234 out |= (Value & 0x0007FF); // imm11 field
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000235
Owen Andersonc2666002010-12-13 19:31:11 +0000236 uint64_t swapped = (out & 0xFFFF0000) >> 16;
237 swapped |= (out & 0x0000FFFF) << 16;
238 return swapped;
239 }
240 case ARM::fixup_t2_condbranch: {
241 Value = Value - 4;
242 Value >>= 1; // Low bit is not encoded.
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000243
Owen Andersonc2666002010-12-13 19:31:11 +0000244 uint64_t out = 0;
Owen Anderson8f079432010-12-09 01:02:09 +0000245 out |= (Value & 0x80000) << 7; // S bit
246 out |= (Value & 0x40000) >> 7; // J2 bit
247 out |= (Value & 0x20000) >> 4; // J1 bit
248 out |= (Value & 0x1F800) << 5; // imm6 field
249 out |= (Value & 0x007FF); // imm11 field
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000250
Jim Grosbach56a25352010-12-13 19:25:46 +0000251 uint32_t swapped = (out & 0xFFFF0000) >> 16;
Owen Andersonfb20d892010-12-09 00:27:41 +0000252 swapped |= (out & 0x0000FFFF) << 16;
253 return swapped;
254 }
Jim Grosbach662a8162010-12-06 23:57:07 +0000255 case ARM::fixup_arm_thumb_bl: {
256 // The value doesn't encode the low bit (always zero) and is offset by
257 // four. The value is encoded into disjoint bit positions in the destination
258 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000259 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000260 // BL: xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000261 //
Jim Grosbach662a8162010-12-06 23:57:07 +0000262 // Note that the halfwords are stored high first, low second; so we need
263 // to transpose the fixup value here to map properly.
Bill Wendling09aa3f02010-12-09 00:39:08 +0000264 unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000265 uint32_t Binary = 0;
266 Value = 0x3fffff & ((Value - 4) >> 1);
267 Binary = (Value & 0x7ff) << 16; // Low imm11 value.
268 Binary |= (Value & 0x1ffc00) >> 11; // High imm10 value.
269 Binary |= isNeg << 10; // Sign bit.
Bill Wendling09aa3f02010-12-09 00:39:08 +0000270 return Binary;
271 }
272 case ARM::fixup_arm_thumb_blx: {
273 // The value doesn't encode the low two bits (always zero) and is offset by
274 // four (see fixup_arm_thumb_cp). The value is encoded into disjoint bit
275 // positions in the destination opcode. x = unchanged, I = immediate value
276 // bit, S = sign extension bit, 0 = zero.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000277 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000278 // BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000279 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000280 // Note that the halfwords are stored high first, low second; so we need
281 // to transpose the fixup value here to map properly.
282 unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000283 uint32_t Binary = 0;
284 Value = 0xfffff & ((Value - 2) >> 2);
285 Binary = (Value & 0x3ff) << 17; // Low imm10L value.
286 Binary |= (Value & 0xffc00) >> 10; // High imm10H value.
287 Binary |= isNeg << 10; // Sign bit.
Jim Grosbach662a8162010-12-06 23:57:07 +0000288 return Binary;
289 }
Bill Wendlingb8958b02010-12-08 01:57:09 +0000290 case ARM::fixup_arm_thumb_cp:
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000291 // Offset by 4, and don't encode the low two bits. Two bytes of that
292 // 'off by 4' is implicitly handled by the half-word ordering of the
293 // Thumb encoding, so we only need to adjust by 2 here.
294 return ((Value - 2) >> 2) & 0xff;
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000295 case ARM::fixup_arm_thumb_cb: {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000296 // Offset by 4 and don't encode the lower bit, which is always 0.
297 uint32_t Binary = (Value - 4) >> 1;
Owen Anderson86abd482010-12-14 19:42:53 +0000298 return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000299 }
Jim Grosbache2467172010-12-10 18:21:33 +0000300 case ARM::fixup_arm_thumb_br:
301 // Offset by 4 and don't encode the lower bit, which is always 0.
302 return ((Value - 4) >> 1) & 0x7ff;
Jim Grosbach01086452010-12-10 17:13:40 +0000303 case ARM::fixup_arm_thumb_bcc:
304 // Offset by 4 and don't encode the lower bit, which is always 0.
305 return ((Value - 4) >> 1) & 0xff;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000306 case ARM::fixup_arm_pcrel_10:
Owen Andersone2e0f582010-12-10 22:46:47 +0000307 Value = Value - 4; // ARM fixups offset by an additional word and don't
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000308 // need to adjust for the half-word ordering.
309 // Fall through.
310 case ARM::fixup_t2_pcrel_10: {
311 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Andersone2e0f582010-12-10 22:46:47 +0000312 Value = Value - 4;
Jason W Kim0c628c22010-12-01 22:46:50 +0000313 bool isAdd = true;
314 if ((int64_t)Value < 0) {
315 Value = -Value;
316 isAdd = false;
317 }
318 // These values don't encode the low two bits since they're always zero.
319 Value >>= 2;
320 assert ((Value < 256) && "Out of range pc-relative fixup value!");
321 Value |= isAdd << 23;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000322
Owen Andersoncc78f5c2010-12-08 19:31:11 +0000323 // Same addressing mode as fixup_arm_pcrel_10,
324 // but with 16-bit halfwords swapped.
Owen Andersond8e351b2010-12-08 00:18:36 +0000325 if (Kind == ARM::fixup_t2_pcrel_10) {
Jim Grosbach56a25352010-12-13 19:25:46 +0000326 uint32_t swapped = (Value & 0xFFFF0000) >> 16;
Owen Anderson255eafb2010-12-08 00:21:33 +0000327 swapped |= (Value & 0x0000FFFF) << 16;
Owen Andersond8e351b2010-12-08 00:18:36 +0000328 return swapped;
329 }
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000330
Jason W Kim0c628c22010-12-01 22:46:50 +0000331 return Value;
332 }
333 }
334}
335
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000336namespace {
Bill Wendling52e635e2010-12-07 23:05:20 +0000337
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000338// FIXME: This should be in a separate file.
339// ELF is an ELF of course...
340class ELFARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000341 MCELFObjectFormat Format;
342
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000343public:
344 Triple::OSType OSType;
345 ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
Daniel Dunbar7b62afa2010-12-17 02:06:08 +0000346 : ARMAsmBackend(T), OSType(_OSType) { }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000347
Rafael Espindolaf230df92010-10-16 18:23:53 +0000348 virtual const MCObjectFormat &getObjectFormat() const {
349 return Format;
350 }
351
Rafael Espindola179821a2010-12-06 19:08:48 +0000352 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000353 uint64_t Value) const;
354
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000355 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Rafael Espindola6024c972010-12-17 17:45:22 +0000356 return createELFObjectWriter(new ARMELFObjectWriter(), OS,
357 /*Is64Bit=*/false,
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000358 OSType, ELF::EM_ARM,
359 /*IsLittleEndian=*/true,
360 /*HasRelocationAddend=*/false);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000361 }
362};
363
Bill Wendling52e635e2010-12-07 23:05:20 +0000364// FIXME: Raise this to share code between Darwin and ELF.
Rafael Espindola179821a2010-12-06 19:08:48 +0000365void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
366 unsigned DataSize, uint64_t Value) const {
Bill Wendling52e635e2010-12-07 23:05:20 +0000367 unsigned NumBytes = 4; // FIXME: 2 for Thumb
Bill Wendling52e635e2010-12-07 23:05:20 +0000368 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000369 if (!Value) return; // Doesn't change encoding.
Bill Wendling52e635e2010-12-07 23:05:20 +0000370
371 unsigned Offset = Fixup.getOffset();
372 assert(Offset % NumBytes == 0 && "Offset mod NumBytes is nonzero!");
373
374 // For each byte of the fragment that the fixup touches, mask in the bits from
375 // the fixup value. The Value has been "split up" into the appropriate
376 // bitfields above.
377 for (unsigned i = 0; i != NumBytes; ++i)
378 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000379}
380
381// FIXME: This should be in a separate file.
382class DarwinARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000383 MCMachOObjectFormat Format;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000384public:
Daniel Dunbar7b62afa2010-12-17 02:06:08 +0000385 DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) { }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000386
Rafael Espindolaf230df92010-10-16 18:23:53 +0000387 virtual const MCObjectFormat &getObjectFormat() const {
388 return Format;
389 }
390
Rafael Espindola179821a2010-12-06 19:08:48 +0000391 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000392 uint64_t Value) const;
393
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000394 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jim Grosbachc9d14392010-11-05 18:48:58 +0000395 // FIXME: Subtarget info should be derived. Force v7 for now.
Daniel Dunbar5d05d972010-12-16 17:21:02 +0000396 return createMachObjectWriter(new ARMMachObjectWriter(
397 /*Is64Bit=*/false,
398 object::mach::CTM_ARM,
399 object::mach::CSARM_V7),
400 OS,
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000401 /*IsLittleEndian=*/true);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000402 }
403
404 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
405 return false;
406 }
407};
408
Bill Wendlingd832fa02010-12-07 23:11:00 +0000409/// getFixupKindNumBytes - The number of bytes the fixup may change.
Jim Grosbachc466b932010-11-11 18:04:49 +0000410static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000411 switch (Kind) {
Jim Grosbach662a8162010-12-06 23:57:07 +0000412 default:
413 llvm_unreachable("Unknown fixup kind!");
Bill Wendlingb8958b02010-12-08 01:57:09 +0000414
Jim Grosbach01086452010-12-10 17:13:40 +0000415 case ARM::fixup_arm_thumb_bcc:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000416 case ARM::fixup_arm_thumb_cp:
Jim Grosbachd40963c2010-12-14 22:28:03 +0000417 case ARM::fixup_thumb_adr_pcrel_10:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000418 return 1;
419
Jim Grosbache2467172010-12-10 18:21:33 +0000420 case ARM::fixup_arm_thumb_br:
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000421 case ARM::fixup_arm_thumb_cb:
Bill Wendlingdff2f712010-12-08 23:01:43 +0000422 return 2;
423
Jim Grosbach662a8162010-12-06 23:57:07 +0000424 case ARM::fixup_arm_ldst_pcrel_12:
425 case ARM::fixup_arm_pcrel_10:
426 case ARM::fixup_arm_adr_pcrel_12:
427 case ARM::fixup_arm_branch:
428 return 3;
Bill Wendlingb8958b02010-12-08 01:57:09 +0000429
430 case FK_Data_4:
Owen Andersond7b3f582010-12-09 01:51:07 +0000431 case ARM::fixup_t2_ldst_pcrel_12:
Owen Andersonc2666002010-12-13 19:31:11 +0000432 case ARM::fixup_t2_condbranch:
433 case ARM::fixup_t2_uncondbranch:
Owen Andersond8e351b2010-12-08 00:18:36 +0000434 case ARM::fixup_t2_pcrel_10:
Owen Andersona838a252010-12-14 00:36:49 +0000435 case ARM::fixup_t2_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +0000436 case ARM::fixup_arm_thumb_bl:
Bill Wendling09aa3f02010-12-09 00:39:08 +0000437 case ARM::fixup_arm_thumb_blx:
Jim Grosbach662a8162010-12-06 23:57:07 +0000438 return 4;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000439 }
440}
441
Rafael Espindola179821a2010-12-06 19:08:48 +0000442void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
443 unsigned DataSize, uint64_t Value) const {
Jim Grosbachc466b932010-11-11 18:04:49 +0000444 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Jim Grosbach679cbd32010-11-09 01:37:15 +0000445 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000446 if (!Value) return; // Doesn't change encoding.
Jim Grosbach679cbd32010-11-09 01:37:15 +0000447
Bill Wendlingd832fa02010-12-07 23:11:00 +0000448 unsigned Offset = Fixup.getOffset();
449 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
450
Jim Grosbach679cbd32010-11-09 01:37:15 +0000451 // For each byte of the fragment that the fixup touches, mask in the
452 // bits from the fixup value.
453 for (unsigned i = 0; i != NumBytes; ++i)
Bill Wendlingd832fa02010-12-07 23:11:00 +0000454 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000455}
Bill Wendling52e635e2010-12-07 23:05:20 +0000456
Jim Grosbachf73fd722010-09-30 03:21:00 +0000457} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000458
459TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
460 const std::string &TT) {
461 switch (Triple(TT).getOS()) {
462 case Triple::Darwin:
463 return new DarwinARMAsmBackend(T);
464 case Triple::MinGW32:
465 case Triple::Cygwin:
466 case Triple::Win32:
467 assert(0 && "Windows not supported on ARM");
468 default:
469 return new ELFARMAsmBackend(T, Triple(TT).getOS());
470 }
471}