blob: 7f9d3081df47adbdd5dc1a91e9e3faacf6b237eb [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"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000016#include "llvm/MC/MCExpr.h"
Daniel Dunbaraa4b7dd2010-12-16 16:08:33 +000017#include "llvm/MC/MCMachObjectWriter.h"
Rafael Espindolaf230df92010-10-16 18:23:53 +000018#include "llvm/MC/MCObjectFormat.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000019#include "llvm/MC/MCObjectWriter.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000020#include "llvm/MC/MCSectionELF.h"
21#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbar36d76a82010-11-27 04:38:36 +000022#include "llvm/Object/MachOFormat.h"
Wesley Peckeecb8582010-10-22 15:52:49 +000023#include "llvm/Support/ELF.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000024#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/raw_ostream.h"
Jim Grosbachdff84b02010-12-02 00:28:45 +000026#include "llvm/Target/TargetAsmBackend.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000027#include "llvm/Target/TargetRegistry.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000028using namespace llvm;
29
30namespace {
Daniel Dunbarae5abd52010-12-16 16:09:19 +000031class ARMMachObjectWriter : public MCMachObjectTargetWriter {
Daniel Dunbar5d05d972010-12-16 17:21:02 +000032public:
33 ARMMachObjectWriter(bool Is64Bit, uint32_t CPUType,
34 uint32_t CPUSubtype)
Daniel Dunbar1139d502010-12-17 06:00:24 +000035 : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype,
36 /*UseAggressiveSymbolFolding=*/true) {}
Daniel Dunbarae5abd52010-12-16 16:09:19 +000037};
38
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000039class ARMAsmBackend : public TargetAsmBackend {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000040 bool isThumbMode; // Currently emitting Thumb code.
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000041public:
Jim Grosbach022ab372010-12-08 15:36:45 +000042 ARMAsmBackend(const Target &T) : TargetAsmBackend(), isThumbMode(false) {}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000043
Daniel Dunbar2761fc42010-12-16 03:20:06 +000044 unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
45
46 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
47 const static MCFixupKindInfo Infos[ARM::NumTargetFixupKinds] = {
48// This table *must* be in the order that the fixup_* kinds are defined in
49// ARMFixupKinds.h.
50//
51// Name Offset (bits) Size (bits) Flags
52{ "fixup_arm_ldst_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
53{ "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
54 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
55{ "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
56{ "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
57 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
58{ "fixup_thumb_adr_pcrel_10",0, 8, MCFixupKindInfo::FKF_IsPCRel |
59 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
60{ "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
61{ "fixup_t2_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
62 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
63{ "fixup_arm_branch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
64{ "fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
65{ "fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
66{ "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
67{ "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
68{ "fixup_arm_thumb_blx", 7, 21, MCFixupKindInfo::FKF_IsPCRel },
69{ "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
70{ "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
71{ "fixup_arm_thumb_bcc", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
72{ "fixup_arm_movt_hi16", 0, 16, 0 },
73{ "fixup_arm_movw_lo16", 0, 16, 0 },
74 };
75
76 if (Kind < FirstTargetFixupKind)
77 return TargetAsmBackend::getFixupKindInfo(Kind);
78
79 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
80 "Invalid kind!");
81 return Infos[Kind - FirstTargetFixupKind];
82 }
83
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000084 bool MayNeedRelaxation(const MCInst &Inst) const;
85
86 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
87
88 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
Jim Grosbach3787a402010-09-30 17:45:51 +000089
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000090 void HandleAssemblerFlag(MCAssemblerFlag Flag) {
91 switch (Flag) {
92 default: break;
93 case MCAF_Code16:
94 setIsThumb(true);
95 break;
96 case MCAF_Code32:
97 setIsThumb(false);
98 break;
99 }
Jim Grosbach3787a402010-09-30 17:45:51 +0000100 }
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000101
102 unsigned getPointerSize() const { return 4; }
103 bool isThumb() const { return isThumbMode; }
104 void setIsThumb(bool it) { isThumbMode = it; }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000105};
Chris Lattnerb75c6512010-11-17 05:41:32 +0000106} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000107
108bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
109 // FIXME: Thumb targets, different move constant targets..
110 return false;
111}
112
113void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
114 assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
115 return;
116}
117
118bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000119 if (isThumb()) {
120 assert (((Count & 1) == 0) && "Unaligned Nop data fragment!");
121 // FIXME: 0xbf00 is the ARMv7 value. For v6 and before, we'll need to
122 // use 0x46c0 (which is a 'mov r8, r8' insn).
123 Count /= 2;
124 for (uint64_t i = 0; i != Count; ++i)
125 OW->Write16(0xbf00);
126 return true;
127 }
128 // ARM mode
129 Count /= 4;
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000130 for (uint64_t i = 0; i != Count; ++i)
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000131 OW->Write32(0xe1a00000);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000132 return true;
Jim Grosbach87dc3aa2010-09-30 03:20:34 +0000133}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000134
Jason W Kim0c628c22010-12-01 22:46:50 +0000135static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
136 switch (Kind) {
137 default:
138 llvm_unreachable("Unknown fixup kind!");
139 case FK_Data_4:
Jason W Kim0c628c22010-12-01 22:46:50 +0000140 return Value;
Jason W Kim2ccf1482010-12-03 19:40:23 +0000141 case ARM::fixup_arm_movt_hi16:
142 case ARM::fixup_arm_movw_lo16: {
143 unsigned Hi4 = (Value & 0xF000) >> 12;
144 unsigned Lo12 = Value & 0x0FFF;
145 // inst{19-16} = Hi4;
146 // inst{11-0} = Lo12;
147 Value = (Hi4 << 16) | (Lo12);
148 return Value;
149 }
Owen Andersond7b3f582010-12-09 01:51:07 +0000150 case ARM::fixup_arm_ldst_pcrel_12:
Jason W Kim0c628c22010-12-01 22:46:50 +0000151 // ARM PC-relative values are offset by 8.
Owen Anderson05018c22010-12-09 20:27:52 +0000152 Value -= 4;
Owen Andersonfe7fac72010-12-09 21:34:47 +0000153 // FALLTHROUGH
Owen Andersond7b3f582010-12-09 01:51:07 +0000154 case ARM::fixup_t2_ldst_pcrel_12: {
155 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Anderson05018c22010-12-09 20:27:52 +0000156 Value -= 4;
Owen Andersond7b3f582010-12-09 01:51:07 +0000157 bool isAdd = true;
Jason W Kim0c628c22010-12-01 22:46:50 +0000158 if ((int64_t)Value < 0) {
159 Value = -Value;
160 isAdd = false;
161 }
162 assert ((Value < 4096) && "Out of range pc-relative fixup value!");
163 Value |= isAdd << 23;
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000164
Owen Andersond7b3f582010-12-09 01:51:07 +0000165 // Same addressing mode as fixup_arm_pcrel_10,
166 // but with 16-bit halfwords swapped.
167 if (Kind == ARM::fixup_t2_ldst_pcrel_12) {
168 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
169 swapped |= (Value & 0x0000FFFF) << 16;
170 return swapped;
171 }
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000172
Jason W Kim0c628c22010-12-01 22:46:50 +0000173 return Value;
174 }
Jim Grosbachd40963c2010-12-14 22:28:03 +0000175 case ARM::fixup_thumb_adr_pcrel_10:
176 return ((Value - 4) >> 2) & 0xff;
Jim Grosbachdff84b02010-12-02 00:28:45 +0000177 case ARM::fixup_arm_adr_pcrel_12: {
178 // ARM PC-relative values are offset by 8.
179 Value -= 8;
180 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
181 if ((int64_t)Value < 0) {
182 Value = -Value;
183 opc = 2; // 0b0010
184 }
185 assert(ARM_AM::getSOImmVal(Value) != -1 &&
186 "Out of range pc-relative fixup value!");
187 // Encode the immediate and shift the opcode into place.
188 return ARM_AM::getSOImmVal(Value) | (opc << 21);
189 }
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000190
Owen Andersona838a252010-12-14 00:36:49 +0000191 case ARM::fixup_t2_adr_pcrel_12: {
192 Value -= 4;
193 unsigned opc = 0;
194 if ((int64_t)Value < 0) {
195 Value = -Value;
196 opc = 5;
197 }
198
199 uint32_t out = (opc << 21);
200 out |= (Value & 0x800) << 14;
201 out |= (Value & 0x700) << 4;
202 out |= (Value & 0x0FF);
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000203
Owen Andersona838a252010-12-14 00:36:49 +0000204 uint64_t swapped = (out & 0xFFFF0000) >> 16;
205 swapped |= (out & 0x0000FFFF) << 16;
206 return swapped;
207 }
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000208
Jason W Kim0c628c22010-12-01 22:46:50 +0000209 case ARM::fixup_arm_branch:
210 // These values don't encode the low two bits since they're always zero.
211 // Offset by 8 just as above.
Jim Grosbach662a8162010-12-06 23:57:07 +0000212 return 0xffffff & ((Value - 8) >> 2);
Owen Andersonc2666002010-12-13 19:31:11 +0000213 case ARM::fixup_t2_uncondbranch: {
Owen Anderson63ee2202010-12-10 23:02:28 +0000214 Value = Value - 4;
Owen Andersonfb20d892010-12-09 00:27:41 +0000215 Value >>= 1; // Low bit is not encoded.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000216
Jim Grosbach56a25352010-12-13 19:25:46 +0000217 uint32_t out = 0;
Owen Andersonc2666002010-12-13 19:31:11 +0000218 bool I = Value & 0x800000;
219 bool J1 = Value & 0x400000;
220 bool J2 = Value & 0x200000;
221 J1 ^= I;
222 J2 ^= I;
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000223
Owen Andersonc2666002010-12-13 19:31:11 +0000224 out |= I << 26; // S bit
225 out |= !J1 << 13; // J1 bit
226 out |= !J2 << 11; // J2 bit
227 out |= (Value & 0x1FF800) << 5; // imm6 field
228 out |= (Value & 0x0007FF); // imm11 field
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000229
Owen Andersonc2666002010-12-13 19:31:11 +0000230 uint64_t swapped = (out & 0xFFFF0000) >> 16;
231 swapped |= (out & 0x0000FFFF) << 16;
232 return swapped;
233 }
234 case ARM::fixup_t2_condbranch: {
235 Value = Value - 4;
236 Value >>= 1; // Low bit is not encoded.
Jim Grosbache8eb1ea2010-12-14 16:25:15 +0000237
Owen Andersonc2666002010-12-13 19:31:11 +0000238 uint64_t out = 0;
Owen Anderson8f079432010-12-09 01:02:09 +0000239 out |= (Value & 0x80000) << 7; // S bit
240 out |= (Value & 0x40000) >> 7; // J2 bit
241 out |= (Value & 0x20000) >> 4; // J1 bit
242 out |= (Value & 0x1F800) << 5; // imm6 field
243 out |= (Value & 0x007FF); // imm11 field
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000244
Jim Grosbach56a25352010-12-13 19:25:46 +0000245 uint32_t swapped = (out & 0xFFFF0000) >> 16;
Owen Andersonfb20d892010-12-09 00:27:41 +0000246 swapped |= (out & 0x0000FFFF) << 16;
247 return swapped;
248 }
Jim Grosbach662a8162010-12-06 23:57:07 +0000249 case ARM::fixup_arm_thumb_bl: {
250 // The value doesn't encode the low bit (always zero) and is offset by
251 // four. The value is encoded into disjoint bit positions in the destination
252 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000253 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000254 // BL: xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000255 //
Jim Grosbach662a8162010-12-06 23:57:07 +0000256 // Note that the halfwords are stored high first, low second; so we need
257 // to transpose the fixup value here to map properly.
Bill Wendling09aa3f02010-12-09 00:39:08 +0000258 unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000259 uint32_t Binary = 0;
260 Value = 0x3fffff & ((Value - 4) >> 1);
261 Binary = (Value & 0x7ff) << 16; // Low imm11 value.
262 Binary |= (Value & 0x1ffc00) >> 11; // High imm10 value.
263 Binary |= isNeg << 10; // Sign bit.
Bill Wendling09aa3f02010-12-09 00:39:08 +0000264 return Binary;
265 }
266 case ARM::fixup_arm_thumb_blx: {
267 // The value doesn't encode the low two bits (always zero) and is offset by
268 // four (see fixup_arm_thumb_cp). The value is encoded into disjoint bit
269 // positions in the destination opcode. x = unchanged, I = immediate value
270 // bit, S = sign extension bit, 0 = zero.
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000271 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000272 // BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0
Jim Grosbach7e294cf2010-12-13 19:18:13 +0000273 //
Bill Wendling09aa3f02010-12-09 00:39:08 +0000274 // Note that the halfwords are stored high first, low second; so we need
275 // to transpose the fixup value here to map properly.
276 unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
Bill Wendling797b7aa2010-12-09 00:44:33 +0000277 uint32_t Binary = 0;
278 Value = 0xfffff & ((Value - 2) >> 2);
279 Binary = (Value & 0x3ff) << 17; // Low imm10L value.
280 Binary |= (Value & 0xffc00) >> 10; // High imm10H value.
281 Binary |= isNeg << 10; // Sign bit.
Jim Grosbach662a8162010-12-06 23:57:07 +0000282 return Binary;
283 }
Bill Wendlingb8958b02010-12-08 01:57:09 +0000284 case ARM::fixup_arm_thumb_cp:
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000285 // Offset by 4, and don't encode the low two bits. Two bytes of that
286 // 'off by 4' is implicitly handled by the half-word ordering of the
287 // Thumb encoding, so we only need to adjust by 2 here.
288 return ((Value - 2) >> 2) & 0xff;
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000289 case ARM::fixup_arm_thumb_cb: {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000290 // Offset by 4 and don't encode the lower bit, which is always 0.
291 uint32_t Binary = (Value - 4) >> 1;
Owen Anderson86abd482010-12-14 19:42:53 +0000292 return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000293 }
Jim Grosbache2467172010-12-10 18:21:33 +0000294 case ARM::fixup_arm_thumb_br:
295 // Offset by 4 and don't encode the lower bit, which is always 0.
296 return ((Value - 4) >> 1) & 0x7ff;
Jim Grosbach01086452010-12-10 17:13:40 +0000297 case ARM::fixup_arm_thumb_bcc:
298 // Offset by 4 and don't encode the lower bit, which is always 0.
299 return ((Value - 4) >> 1) & 0xff;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000300 case ARM::fixup_arm_pcrel_10:
Owen Andersone2e0f582010-12-10 22:46:47 +0000301 Value = Value - 4; // ARM fixups offset by an additional word and don't
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000302 // need to adjust for the half-word ordering.
303 // Fall through.
304 case ARM::fixup_t2_pcrel_10: {
305 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Andersone2e0f582010-12-10 22:46:47 +0000306 Value = Value - 4;
Jason W Kim0c628c22010-12-01 22:46:50 +0000307 bool isAdd = true;
308 if ((int64_t)Value < 0) {
309 Value = -Value;
310 isAdd = false;
311 }
312 // These values don't encode the low two bits since they're always zero.
313 Value >>= 2;
314 assert ((Value < 256) && "Out of range pc-relative fixup value!");
315 Value |= isAdd << 23;
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000316
Owen Andersoncc78f5c2010-12-08 19:31:11 +0000317 // Same addressing mode as fixup_arm_pcrel_10,
318 // but with 16-bit halfwords swapped.
Owen Andersond8e351b2010-12-08 00:18:36 +0000319 if (Kind == ARM::fixup_t2_pcrel_10) {
Jim Grosbach56a25352010-12-13 19:25:46 +0000320 uint32_t swapped = (Value & 0xFFFF0000) >> 16;
Owen Anderson255eafb2010-12-08 00:21:33 +0000321 swapped |= (Value & 0x0000FFFF) << 16;
Owen Andersond8e351b2010-12-08 00:18:36 +0000322 return swapped;
323 }
Jim Grosbach0c2c2172010-12-08 20:32:07 +0000324
Jason W Kim0c628c22010-12-01 22:46:50 +0000325 return Value;
326 }
327 }
328}
329
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000330namespace {
Bill Wendling52e635e2010-12-07 23:05:20 +0000331
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000332// FIXME: This should be in a separate file.
333// ELF is an ELF of course...
334class ELFARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000335 MCELFObjectFormat Format;
336
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000337public:
338 Triple::OSType OSType;
339 ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
Daniel Dunbar7b62afa2010-12-17 02:06:08 +0000340 : ARMAsmBackend(T), OSType(_OSType) { }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000341
Rafael Espindolaf230df92010-10-16 18:23:53 +0000342 virtual const MCObjectFormat &getObjectFormat() const {
343 return Format;
344 }
345
Rafael Espindola179821a2010-12-06 19:08:48 +0000346 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000347 uint64_t Value) const;
348
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000349 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000350 return createELFObjectWriter(OS, /*Is64Bit=*/false,
351 OSType, ELF::EM_ARM,
352 /*IsLittleEndian=*/true,
353 /*HasRelocationAddend=*/false);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000354 }
355};
356
Bill Wendling52e635e2010-12-07 23:05:20 +0000357// FIXME: Raise this to share code between Darwin and ELF.
Rafael Espindola179821a2010-12-06 19:08:48 +0000358void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
359 unsigned DataSize, uint64_t Value) const {
Bill Wendling52e635e2010-12-07 23:05:20 +0000360 unsigned NumBytes = 4; // FIXME: 2 for Thumb
Bill Wendling52e635e2010-12-07 23:05:20 +0000361 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000362 if (!Value) return; // Doesn't change encoding.
Bill Wendling52e635e2010-12-07 23:05:20 +0000363
364 unsigned Offset = Fixup.getOffset();
365 assert(Offset % NumBytes == 0 && "Offset mod NumBytes is nonzero!");
366
367 // For each byte of the fragment that the fixup touches, mask in the bits from
368 // the fixup value. The Value has been "split up" into the appropriate
369 // bitfields above.
370 for (unsigned i = 0; i != NumBytes; ++i)
371 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000372}
373
374// FIXME: This should be in a separate file.
375class DarwinARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000376 MCMachOObjectFormat Format;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000377public:
Daniel Dunbar7b62afa2010-12-17 02:06:08 +0000378 DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) { }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000379
Rafael Espindolaf230df92010-10-16 18:23:53 +0000380 virtual const MCObjectFormat &getObjectFormat() const {
381 return Format;
382 }
383
Rafael Espindola179821a2010-12-06 19:08:48 +0000384 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000385 uint64_t Value) const;
386
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000387 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jim Grosbachc9d14392010-11-05 18:48:58 +0000388 // FIXME: Subtarget info should be derived. Force v7 for now.
Daniel Dunbar5d05d972010-12-16 17:21:02 +0000389 return createMachObjectWriter(new ARMMachObjectWriter(
390 /*Is64Bit=*/false,
391 object::mach::CTM_ARM,
392 object::mach::CSARM_V7),
393 OS,
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000394 /*IsLittleEndian=*/true);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000395 }
396
397 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
398 return false;
399 }
400};
401
Bill Wendlingd832fa02010-12-07 23:11:00 +0000402/// getFixupKindNumBytes - The number of bytes the fixup may change.
Jim Grosbachc466b932010-11-11 18:04:49 +0000403static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000404 switch (Kind) {
Jim Grosbach662a8162010-12-06 23:57:07 +0000405 default:
406 llvm_unreachable("Unknown fixup kind!");
Bill Wendlingb8958b02010-12-08 01:57:09 +0000407
Jim Grosbach01086452010-12-10 17:13:40 +0000408 case ARM::fixup_arm_thumb_bcc:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000409 case ARM::fixup_arm_thumb_cp:
Jim Grosbachd40963c2010-12-14 22:28:03 +0000410 case ARM::fixup_thumb_adr_pcrel_10:
Bill Wendlingb8958b02010-12-08 01:57:09 +0000411 return 1;
412
Jim Grosbache2467172010-12-10 18:21:33 +0000413 case ARM::fixup_arm_thumb_br:
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000414 case ARM::fixup_arm_thumb_cb:
Bill Wendlingdff2f712010-12-08 23:01:43 +0000415 return 2;
416
Jim Grosbach662a8162010-12-06 23:57:07 +0000417 case ARM::fixup_arm_ldst_pcrel_12:
418 case ARM::fixup_arm_pcrel_10:
419 case ARM::fixup_arm_adr_pcrel_12:
420 case ARM::fixup_arm_branch:
421 return 3;
Bill Wendlingb8958b02010-12-08 01:57:09 +0000422
423 case FK_Data_4:
Owen Andersond7b3f582010-12-09 01:51:07 +0000424 case ARM::fixup_t2_ldst_pcrel_12:
Owen Andersonc2666002010-12-13 19:31:11 +0000425 case ARM::fixup_t2_condbranch:
426 case ARM::fixup_t2_uncondbranch:
Owen Andersond8e351b2010-12-08 00:18:36 +0000427 case ARM::fixup_t2_pcrel_10:
Owen Andersona838a252010-12-14 00:36:49 +0000428 case ARM::fixup_t2_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +0000429 case ARM::fixup_arm_thumb_bl:
Bill Wendling09aa3f02010-12-09 00:39:08 +0000430 case ARM::fixup_arm_thumb_blx:
Jim Grosbach662a8162010-12-06 23:57:07 +0000431 return 4;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000432 }
433}
434
Rafael Espindola179821a2010-12-06 19:08:48 +0000435void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
436 unsigned DataSize, uint64_t Value) const {
Jim Grosbachc466b932010-11-11 18:04:49 +0000437 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Jim Grosbach679cbd32010-11-09 01:37:15 +0000438 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000439 if (!Value) return; // Doesn't change encoding.
Jim Grosbach679cbd32010-11-09 01:37:15 +0000440
Bill Wendlingd832fa02010-12-07 23:11:00 +0000441 unsigned Offset = Fixup.getOffset();
442 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
443
Jim Grosbach679cbd32010-11-09 01:37:15 +0000444 // For each byte of the fragment that the fixup touches, mask in the
445 // bits from the fixup value.
446 for (unsigned i = 0; i != NumBytes; ++i)
Bill Wendlingd832fa02010-12-07 23:11:00 +0000447 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000448}
Bill Wendling52e635e2010-12-07 23:05:20 +0000449
Jim Grosbachf73fd722010-09-30 03:21:00 +0000450} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000451
452TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
453 const std::string &TT) {
454 switch (Triple(TT).getOS()) {
455 case Triple::Darwin:
456 return new DarwinARMAsmBackend(T);
457 case Triple::MinGW32:
458 case Triple::Cygwin:
459 case Triple::Win32:
460 assert(0 && "Windows not supported on ARM");
461 default:
462 return new ELFARMAsmBackend(T, Triple(TT).getOS());
463 }
464}