1 //===-- ARMAsmBackend.cpp - ARM Assembler Backend -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "ARMAddressingModes.h"
12 #include "ARMFixupKinds.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCDirectives.h"
16 #include "llvm/MC/MCELFObjectWriter.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCMachObjectWriter.h"
19 #include "llvm/MC/MCObjectWriter.h"
20 #include "llvm/MC/MCSectionELF.h"
21 #include "llvm/MC/MCSectionMachO.h"
22 #include "llvm/Object/MachOFormat.h"
23 #include "llvm/Support/ELF.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/TargetAsmBackend.h"
27 #include "llvm/Target/TargetRegistry.h"
31 class ARMELFObjectWriter
: public MCELFObjectTargetWriter
{
33 ARMELFObjectWriter(Triple::OSType OSType
)
34 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSType
, ELF::EM_ARM
,
35 /*HasRelocationAddend*/ false) {}
38 class ARMAsmBackend
: public TargetAsmBackend
{
39 bool isThumbMode
; // Currently emitting Thumb code.
41 ARMAsmBackend(const Target
&T
) : TargetAsmBackend(), isThumbMode(false) {}
43 unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds
; }
45 const MCFixupKindInfo
&getFixupKindInfo(MCFixupKind Kind
) const {
46 const static MCFixupKindInfo Infos
[ARM::NumTargetFixupKinds
] = {
47 // This table *must* be in the order that the fixup_* kinds are defined in
50 // Name Offset (bits) Size (bits) Flags
51 { "fixup_arm_ldst_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel
},
52 { "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel
|
53 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits
},
54 { "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel
},
55 { "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel
|
56 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits
},
57 { "fixup_thumb_adr_pcrel_10",0, 8, MCFixupKindInfo::FKF_IsPCRel
|
58 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits
},
59 { "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel
},
60 { "fixup_t2_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel
|
61 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits
},
62 { "fixup_arm_condbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel
},
63 { "fixup_arm_uncondbranch", 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", 0, 8, MCFixupKindInfo::FKF_IsPCRel
},
72 // movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16 - 19.
73 { "fixup_arm_movt_hi16", 0, 20, 0 },
74 { "fixup_arm_movw_lo16", 0, 20, 0 },
75 { "fixup_t2_movt_hi16", 0, 20, 0 },
76 { "fixup_t2_movw_lo16", 0, 20, 0 },
77 { "fixup_arm_movt_hi16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel
},
78 { "fixup_arm_movw_lo16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel
},
79 { "fixup_t2_movt_hi16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel
},
80 { "fixup_t2_movw_lo16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel
},
83 if (Kind
< FirstTargetFixupKind
)
84 return TargetAsmBackend::getFixupKindInfo(Kind
);
86 assert(unsigned(Kind
- FirstTargetFixupKind
) < getNumFixupKinds() &&
88 return Infos
[Kind
- FirstTargetFixupKind
];
91 bool MayNeedRelaxation(const MCInst
&Inst
) const;
93 void RelaxInstruction(const MCInst
&Inst
, MCInst
&Res
) const;
95 bool WriteNopData(uint64_t Count
, MCObjectWriter
*OW
) const;
97 void HandleAssemblerFlag(MCAssemblerFlag Flag
) {
109 unsigned getPointerSize() const { return 4; }
110 bool isThumb() const { return isThumbMode
; }
111 void setIsThumb(bool it
) { isThumbMode
= it
; }
113 } // end anonymous namespace
115 bool ARMAsmBackend::MayNeedRelaxation(const MCInst
&Inst
) const {
116 // FIXME: Thumb targets, different move constant targets..
120 void ARMAsmBackend::RelaxInstruction(const MCInst
&Inst
, MCInst
&Res
) const {
121 assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
125 bool ARMAsmBackend::WriteNopData(uint64_t Count
, MCObjectWriter
*OW
) const {
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 uint64_t NumNops
= Count
/ 2;
130 for (uint64_t i
= 0; i
!= NumNops
; ++i
)
137 uint64_t NumNops
= Count
/ 4;
138 for (uint64_t i
= 0; i
!= NumNops
; ++i
)
139 OW
->Write32(0xe1a00000);
141 default: break; // No leftover bytes to write
142 case 1: OW
->Write8(0); break;
143 case 2: OW
->Write16(0); break;
144 case 3: OW
->Write16(0); OW
->Write8(0xa0); break;
150 static unsigned adjustFixupValue(unsigned Kind
, uint64_t Value
) {
153 llvm_unreachable("Unknown fixup kind!");
158 case ARM::fixup_arm_movt_hi16
:
161 case ARM::fixup_arm_movw_lo16
:
162 case ARM::fixup_arm_movt_hi16_pcrel
:
163 case ARM::fixup_arm_movw_lo16_pcrel
: {
164 unsigned Hi4
= (Value
& 0xF000) >> 12;
165 unsigned Lo12
= Value
& 0x0FFF;
166 assert ((((int64_t)Value
) >= -0x8000) && (((int64_t)Value
) <= 0x7fff) &&
167 "Out of range pc-relative fixup value!");
168 // inst{19-16} = Hi4;
169 // inst{11-0} = Lo12;
170 Value
= (Hi4
<< 16) | (Lo12
);
173 case ARM::fixup_t2_movt_hi16
:
176 case ARM::fixup_t2_movw_lo16
:
177 case ARM::fixup_t2_movt_hi16_pcrel
: //FIXME: Shouldn't this be shifted like
178 // the other hi16 fixup?
179 case ARM::fixup_t2_movw_lo16_pcrel
: {
180 unsigned Hi4
= (Value
& 0xF000) >> 12;
181 unsigned i
= (Value
& 0x800) >> 11;
182 unsigned Mid3
= (Value
& 0x700) >> 8;
183 unsigned Lo8
= Value
& 0x0FF;
184 // inst{19-16} = Hi4;
186 // inst{14-12} = Mid3;
188 // The value comes in as the whole thing, not just the portion required
189 // for this fixup, so we need to mask off the bits not handled by this
190 // portion (lo vs. hi).
192 Value
= (Hi4
<< 16) | (i
<< 26) | (Mid3
<< 12) | (Lo8
);
193 uint64_t swapped
= (Value
& 0xFFFF0000) >> 16;
194 swapped
|= (Value
& 0x0000FFFF) << 16;
197 case ARM::fixup_arm_ldst_pcrel_12
:
198 // ARM PC-relative values are offset by 8.
201 case ARM::fixup_t2_ldst_pcrel_12
: {
202 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
205 if ((int64_t)Value
< 0) {
209 assert ((Value
< 4096) && "Out of range pc-relative fixup value!");
210 Value
|= isAdd
<< 23;
212 // Same addressing mode as fixup_arm_pcrel_10,
213 // but with 16-bit halfwords swapped.
214 if (Kind
== ARM::fixup_t2_ldst_pcrel_12
) {
215 uint64_t swapped
= (Value
& 0xFFFF0000) >> 16;
216 swapped
|= (Value
& 0x0000FFFF) << 16;
222 case ARM::fixup_thumb_adr_pcrel_10
:
223 return ((Value
- 4) >> 2) & 0xff;
224 case ARM::fixup_arm_adr_pcrel_12
: {
225 // ARM PC-relative values are offset by 8.
227 unsigned opc
= 4; // bits {24-21}. Default to add: 0b0100
228 if ((int64_t)Value
< 0) {
232 assert(ARM_AM::getSOImmVal(Value
) != -1 &&
233 "Out of range pc-relative fixup value!");
234 // Encode the immediate and shift the opcode into place.
235 return ARM_AM::getSOImmVal(Value
) | (opc
<< 21);
238 case ARM::fixup_t2_adr_pcrel_12
: {
241 if ((int64_t)Value
< 0) {
246 uint32_t out
= (opc
<< 21);
247 out
|= (Value
& 0x800) << 15;
248 out
|= (Value
& 0x700) << 4;
249 out
|= (Value
& 0x0FF);
251 uint64_t swapped
= (out
& 0xFFFF0000) >> 16;
252 swapped
|= (out
& 0x0000FFFF) << 16;
256 case ARM::fixup_arm_condbranch
:
257 case ARM::fixup_arm_uncondbranch
:
258 // These values don't encode the low two bits since they're always zero.
259 // Offset by 8 just as above.
260 return 0xffffff & ((Value
- 8) >> 2);
261 case ARM::fixup_t2_uncondbranch
: {
263 Value
>>= 1; // Low bit is not encoded.
266 bool I
= Value
& 0x800000;
267 bool J1
= Value
& 0x400000;
268 bool J2
= Value
& 0x200000;
272 out
|= I
<< 26; // S bit
273 out
|= !J1
<< 13; // J1 bit
274 out
|= !J2
<< 11; // J2 bit
275 out
|= (Value
& 0x1FF800) << 5; // imm6 field
276 out
|= (Value
& 0x0007FF); // imm11 field
278 uint64_t swapped
= (out
& 0xFFFF0000) >> 16;
279 swapped
|= (out
& 0x0000FFFF) << 16;
282 case ARM::fixup_t2_condbranch
: {
284 Value
>>= 1; // Low bit is not encoded.
287 out
|= (Value
& 0x80000) << 7; // S bit
288 out
|= (Value
& 0x40000) >> 7; // J2 bit
289 out
|= (Value
& 0x20000) >> 4; // J1 bit
290 out
|= (Value
& 0x1F800) << 5; // imm6 field
291 out
|= (Value
& 0x007FF); // imm11 field
293 uint32_t swapped
= (out
& 0xFFFF0000) >> 16;
294 swapped
|= (out
& 0x0000FFFF) << 16;
297 case ARM::fixup_arm_thumb_bl
: {
298 // The value doesn't encode the low bit (always zero) and is offset by
299 // four. The value is encoded into disjoint bit positions in the destination
300 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit
302 // BL: xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
304 // Note that the halfwords are stored high first, low second; so we need
305 // to transpose the fixup value here to map properly.
306 unsigned isNeg
= (int64_t(Value
- 4) < 0) ? 1 : 0;
308 Value
= 0x3fffff & ((Value
- 4) >> 1);
309 Binary
= (Value
& 0x7ff) << 16; // Low imm11 value.
310 Binary
|= (Value
& 0x1ffc00) >> 11; // High imm10 value.
311 Binary
|= isNeg
<< 10; // Sign bit.
314 case ARM::fixup_arm_thumb_blx
: {
315 // The value doesn't encode the low two bits (always zero) and is offset by
316 // four (see fixup_arm_thumb_cp). The value is encoded into disjoint bit
317 // positions in the destination opcode. x = unchanged, I = immediate value
318 // bit, S = sign extension bit, 0 = zero.
320 // BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0
322 // Note that the halfwords are stored high first, low second; so we need
323 // to transpose the fixup value here to map properly.
324 unsigned isNeg
= (int64_t(Value
-4) < 0) ? 1 : 0;
326 Value
= 0xfffff & ((Value
- 2) >> 2);
327 Binary
= (Value
& 0x3ff) << 17; // Low imm10L value.
328 Binary
|= (Value
& 0xffc00) >> 10; // High imm10H value.
329 Binary
|= isNeg
<< 10; // Sign bit.
332 case ARM::fixup_arm_thumb_cp
:
333 // Offset by 4, and don't encode the low two bits. Two bytes of that
334 // 'off by 4' is implicitly handled by the half-word ordering of the
335 // Thumb encoding, so we only need to adjust by 2 here.
336 return ((Value
- 2) >> 2) & 0xff;
337 case ARM::fixup_arm_thumb_cb
: {
338 // Offset by 4 and don't encode the lower bit, which is always 0.
339 uint32_t Binary
= (Value
- 4) >> 1;
340 return ((Binary
& 0x20) << 4) | ((Binary
& 0x1f) << 3);
342 case ARM::fixup_arm_thumb_br
:
343 // Offset by 4 and don't encode the lower bit, which is always 0.
344 return ((Value
- 4) >> 1) & 0x7ff;
345 case ARM::fixup_arm_thumb_bcc
:
346 // Offset by 4 and don't encode the lower bit, which is always 0.
347 return ((Value
- 4) >> 1) & 0xff;
348 case ARM::fixup_arm_pcrel_10
:
349 Value
= Value
- 4; // ARM fixups offset by an additional word and don't
350 // need to adjust for the half-word ordering.
352 case ARM::fixup_t2_pcrel_10
: {
353 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
356 if ((int64_t)Value
< 0) {
360 // These values don't encode the low two bits since they're always zero.
362 assert ((Value
< 256) && "Out of range pc-relative fixup value!");
363 Value
|= isAdd
<< 23;
365 // Same addressing mode as fixup_arm_pcrel_10,
366 // but with 16-bit halfwords swapped.
367 if (Kind
== ARM::fixup_t2_pcrel_10
) {
368 uint32_t swapped
= (Value
& 0xFFFF0000) >> 16;
369 swapped
|= (Value
& 0x0000FFFF) << 16;
380 // FIXME: This should be in a separate file.
381 // ELF is an ELF of course...
382 class ELFARMAsmBackend
: public ARMAsmBackend
{
384 Triple::OSType OSType
;
385 ELFARMAsmBackend(const Target
&T
, Triple::OSType _OSType
)
386 : ARMAsmBackend(T
), OSType(_OSType
) { }
388 void ApplyFixup(const MCFixup
&Fixup
, char *Data
, unsigned DataSize
,
389 uint64_t Value
) const;
391 MCObjectWriter
*createObjectWriter(raw_ostream
&OS
) const {
392 return createELFObjectWriter(new ARMELFObjectWriter(OSType
), OS
,
393 /*IsLittleEndian*/ true);
397 // FIXME: Raise this to share code between Darwin and ELF.
398 void ELFARMAsmBackend::ApplyFixup(const MCFixup
&Fixup
, char *Data
,
399 unsigned DataSize
, uint64_t Value
) const {
400 unsigned NumBytes
= 4; // FIXME: 2 for Thumb
401 Value
= adjustFixupValue(Fixup
.getKind(), Value
);
402 if (!Value
) return; // Doesn't change encoding.
404 unsigned Offset
= Fixup
.getOffset();
406 // For each byte of the fragment that the fixup touches, mask in the bits from
407 // the fixup value. The Value has been "split up" into the appropriate
409 for (unsigned i
= 0; i
!= NumBytes
; ++i
)
410 Data
[Offset
+ i
] |= uint8_t((Value
>> (i
* 8)) & 0xff);
413 // FIXME: This should be in a separate file.
414 class DarwinARMAsmBackend
: public ARMAsmBackend
{
416 const object::mach::CPUSubtypeARM Subtype
;
417 DarwinARMAsmBackend(const Target
&T
, object::mach::CPUSubtypeARM st
)
418 : ARMAsmBackend(T
), Subtype(st
) { }
420 MCObjectWriter
*createObjectWriter(raw_ostream
&OS
) const {
421 return createARMMachObjectWriter(OS
, /*Is64Bit=*/false,
422 object::mach::CTM_ARM
,
426 void ApplyFixup(const MCFixup
&Fixup
, char *Data
, unsigned DataSize
,
427 uint64_t Value
) const;
429 virtual bool doesSectionRequireSymbols(const MCSection
&Section
) const {
434 /// getFixupKindNumBytes - The number of bytes the fixup may change.
435 static unsigned getFixupKindNumBytes(unsigned Kind
) {
438 llvm_unreachable("Unknown fixup kind!");
441 case ARM::fixup_arm_thumb_bcc
:
442 case ARM::fixup_arm_thumb_cp
:
443 case ARM::fixup_thumb_adr_pcrel_10
:
447 case ARM::fixup_arm_thumb_br
:
448 case ARM::fixup_arm_thumb_cb
:
451 case ARM::fixup_arm_ldst_pcrel_12
:
452 case ARM::fixup_arm_pcrel_10
:
453 case ARM::fixup_arm_adr_pcrel_12
:
454 case ARM::fixup_arm_condbranch
:
455 case ARM::fixup_arm_uncondbranch
:
459 case ARM::fixup_t2_ldst_pcrel_12
:
460 case ARM::fixup_t2_condbranch
:
461 case ARM::fixup_t2_uncondbranch
:
462 case ARM::fixup_t2_pcrel_10
:
463 case ARM::fixup_t2_adr_pcrel_12
:
464 case ARM::fixup_arm_thumb_bl
:
465 case ARM::fixup_arm_thumb_blx
:
466 case ARM::fixup_arm_movt_hi16
:
467 case ARM::fixup_arm_movw_lo16
:
468 case ARM::fixup_arm_movt_hi16_pcrel
:
469 case ARM::fixup_arm_movw_lo16_pcrel
:
470 case ARM::fixup_t2_movt_hi16
:
471 case ARM::fixup_t2_movw_lo16
:
472 case ARM::fixup_t2_movt_hi16_pcrel
:
473 case ARM::fixup_t2_movw_lo16_pcrel
:
478 void DarwinARMAsmBackend::ApplyFixup(const MCFixup
&Fixup
, char *Data
,
479 unsigned DataSize
, uint64_t Value
) const {
480 unsigned NumBytes
= getFixupKindNumBytes(Fixup
.getKind());
481 Value
= adjustFixupValue(Fixup
.getKind(), Value
);
482 if (!Value
) return; // Doesn't change encoding.
484 unsigned Offset
= Fixup
.getOffset();
485 assert(Offset
+ NumBytes
<= DataSize
&& "Invalid fixup offset!");
487 // For each byte of the fragment that the fixup touches, mask in the
488 // bits from the fixup value.
489 for (unsigned i
= 0; i
!= NumBytes
; ++i
)
490 Data
[Offset
+ i
] |= uint8_t((Value
>> (i
* 8)) & 0xff);
493 } // end anonymous namespace
495 TargetAsmBackend
*llvm::createARMAsmBackend(const Target
&T
,
496 const std::string
&TT
) {
497 Triple
TheTriple(TT
);
499 if (TheTriple
.isOSDarwin()) {
500 if (TheTriple
.getArchName() == "armv4t" ||
501 TheTriple
.getArchName() == "thumbv4t")
502 return new DarwinARMAsmBackend(T
, object::mach::CSARM_V4T
);
503 else if (TheTriple
.getArchName() == "armv5e" ||
504 TheTriple
.getArchName() == "thumbv5e")
505 return new DarwinARMAsmBackend(T
, object::mach::CSARM_V5TEJ
);
506 else if (TheTriple
.getArchName() == "armv6" ||
507 TheTriple
.getArchName() == "thumbv6")
508 return new DarwinARMAsmBackend(T
, object::mach::CSARM_V6
);
509 return new DarwinARMAsmBackend(T
, object::mach::CSARM_V7
);
512 if (TheTriple
.isOSWindows())
513 assert(0 && "Windows not supported on ARM");
515 return new ELFARMAsmBackend(T
, Triple(TT
).getOS());