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 ARMMachObjectWriter : public MCMachObjectTargetWriter {
33 ARMMachObjectWriter(bool Is64Bit, uint32_t CPUType,
35 : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype,
36 /*UseAggressiveSymbolFolding=*/true) {}
39 class ARMELFObjectWriter : public MCELFObjectTargetWriter {
41 ARMELFObjectWriter(Triple::OSType OSType)
42 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSType, ELF::EM_ARM,
43 /*HasRelocationAddend*/ false) {}
46 class ARMAsmBackend : public TargetAsmBackend {
47 bool isThumbMode; // Currently emitting Thumb code.
49 ARMAsmBackend(const Target &T) : TargetAsmBackend(), isThumbMode(false) {}
51 unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
53 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
54 const static MCFixupKindInfo Infos[ARM::NumTargetFixupKinds] = {
55 // This table *must* be in the order that the fixup_* kinds are defined in
58 // Name Offset (bits) Size (bits) Flags
59 { "fixup_arm_ldst_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
60 { "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
61 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
62 { "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
63 { "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
64 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
65 { "fixup_thumb_adr_pcrel_10",0, 8, MCFixupKindInfo::FKF_IsPCRel |
66 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
67 { "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
68 { "fixup_t2_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
69 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
70 { "fixup_arm_condbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
71 { "fixup_arm_uncondbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
72 { "fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
73 { "fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
74 { "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
75 { "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
76 { "fixup_arm_thumb_blx", 7, 21, MCFixupKindInfo::FKF_IsPCRel },
77 { "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
78 { "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
79 { "fixup_arm_thumb_bcc", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
80 // movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16 - 19.
81 { "fixup_arm_movt_hi16", 0, 20, 0 },
82 { "fixup_arm_movw_lo16", 0, 20, 0 },
83 { "fixup_t2_movt_hi16", 0, 20, 0 },
84 { "fixup_t2_movw_lo16", 0, 20, 0 },
85 { "fixup_arm_movt_hi16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
86 { "fixup_arm_movw_lo16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
87 { "fixup_t2_movt_hi16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
88 { "fixup_t2_movw_lo16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
91 if (Kind < FirstTargetFixupKind)
92 return TargetAsmBackend::getFixupKindInfo(Kind);
94 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
96 return Infos[Kind - FirstTargetFixupKind];
99 bool MayNeedRelaxation(const MCInst &Inst) const;
101 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
103 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
105 void HandleAssemblerFlag(MCAssemblerFlag Flag) {
117 unsigned getPointerSize() const { return 4; }
118 bool isThumb() const { return isThumbMode; }
119 void setIsThumb(bool it) { isThumbMode = it; }
121 } // end anonymous namespace
123 bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
124 // FIXME: Thumb targets, different move constant targets..
128 void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
129 assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
133 bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
135 // FIXME: 0xbf00 is the ARMv7 value. For v6 and before, we'll need to
136 // use 0x46c0 (which is a 'mov r8, r8' insn).
137 uint64_t NumNops = Count / 2;
138 for (uint64_t i = 0; i != NumNops; ++i)
145 uint64_t NumNops = Count / 4;
146 for (uint64_t i = 0; i != NumNops; ++i)
147 OW->Write32(0xe1a00000);
149 default: break; // No leftover bytes to write
150 case 1: OW->Write8(0); break;
151 case 2: OW->Write16(0); break;
152 case 3: OW->Write16(0); OW->Write8(0xa0); break;
158 static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
161 llvm_unreachable("Unknown fixup kind!");
166 case ARM::fixup_arm_movt_hi16:
169 case ARM::fixup_arm_movw_lo16:
170 case ARM::fixup_arm_movt_hi16_pcrel:
171 case ARM::fixup_arm_movw_lo16_pcrel: {
172 unsigned Hi4 = (Value & 0xF000) >> 12;
173 unsigned Lo12 = Value & 0x0FFF;
174 assert ((((int64_t)Value) >= -0x8000) && (((int64_t)Value) <= 0x7fff) &&
175 "Out of range pc-relative fixup value!");
176 // inst{19-16} = Hi4;
177 // inst{11-0} = Lo12;
178 Value = (Hi4 << 16) | (Lo12);
181 case ARM::fixup_t2_movt_hi16:
184 case ARM::fixup_t2_movw_lo16:
185 case ARM::fixup_t2_movt_hi16_pcrel:
186 case ARM::fixup_t2_movw_lo16_pcrel: {
187 unsigned Hi4 = (Value & 0xF000) >> 12;
188 unsigned i = (Value & 0x800) >> 11;
189 unsigned Mid3 = (Value & 0x700) >> 8;
190 unsigned Lo8 = Value & 0x0FF;
191 // inst{19-16} = Hi4;
193 // inst{14-12} = Mid3;
195 assert ((((int64_t)Value) >= -0x8000) && (((int64_t)Value) <= 0x7fff) &&
196 "Out of range pc-relative fixup value!");
197 Value = (Hi4 << 16) | (i << 26) | (Mid3 << 12) | (Lo8);
198 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
199 swapped |= (Value & 0x0000FFFF) << 16;
202 case ARM::fixup_arm_ldst_pcrel_12:
203 // ARM PC-relative values are offset by 8.
206 case ARM::fixup_t2_ldst_pcrel_12: {
207 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
210 if ((int64_t)Value < 0) {
214 assert ((Value < 4096) && "Out of range pc-relative fixup value!");
215 Value |= isAdd << 23;
217 // Same addressing mode as fixup_arm_pcrel_10,
218 // but with 16-bit halfwords swapped.
219 if (Kind == ARM::fixup_t2_ldst_pcrel_12) {
220 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
221 swapped |= (Value & 0x0000FFFF) << 16;
227 case ARM::fixup_thumb_adr_pcrel_10:
228 return ((Value - 4) >> 2) & 0xff;
229 case ARM::fixup_arm_adr_pcrel_12: {
230 // ARM PC-relative values are offset by 8.
232 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
233 if ((int64_t)Value < 0) {
237 assert(ARM_AM::getSOImmVal(Value) != -1 &&
238 "Out of range pc-relative fixup value!");
239 // Encode the immediate and shift the opcode into place.
240 return ARM_AM::getSOImmVal(Value) | (opc << 21);
243 case ARM::fixup_t2_adr_pcrel_12: {
246 if ((int64_t)Value < 0) {
251 uint32_t out = (opc << 21);
252 out |= (Value & 0x800) << 15;
253 out |= (Value & 0x700) << 4;
254 out |= (Value & 0x0FF);
256 uint64_t swapped = (out & 0xFFFF0000) >> 16;
257 swapped |= (out & 0x0000FFFF) << 16;
261 case ARM::fixup_arm_condbranch:
262 case ARM::fixup_arm_uncondbranch:
263 // These values don't encode the low two bits since they're always zero.
264 // Offset by 8 just as above.
265 return 0xffffff & ((Value - 8) >> 2);
266 case ARM::fixup_t2_uncondbranch: {
268 Value >>= 1; // Low bit is not encoded.
271 bool I = Value & 0x800000;
272 bool J1 = Value & 0x400000;
273 bool J2 = Value & 0x200000;
277 out |= I << 26; // S bit
278 out |= !J1 << 13; // J1 bit
279 out |= !J2 << 11; // J2 bit
280 out |= (Value & 0x1FF800) << 5; // imm6 field
281 out |= (Value & 0x0007FF); // imm11 field
283 uint64_t swapped = (out & 0xFFFF0000) >> 16;
284 swapped |= (out & 0x0000FFFF) << 16;
287 case ARM::fixup_t2_condbranch: {
289 Value >>= 1; // Low bit is not encoded.
292 out |= (Value & 0x80000) << 7; // S bit
293 out |= (Value & 0x40000) >> 7; // J2 bit
294 out |= (Value & 0x20000) >> 4; // J1 bit
295 out |= (Value & 0x1F800) << 5; // imm6 field
296 out |= (Value & 0x007FF); // imm11 field
298 uint32_t swapped = (out & 0xFFFF0000) >> 16;
299 swapped |= (out & 0x0000FFFF) << 16;
302 case ARM::fixup_arm_thumb_bl: {
303 // The value doesn't encode the low bit (always zero) and is offset by
304 // four. The value is encoded into disjoint bit positions in the destination
305 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit
307 // BL: xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
309 // Note that the halfwords are stored high first, low second; so we need
310 // to transpose the fixup value here to map properly.
311 unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
313 Value = 0x3fffff & ((Value - 4) >> 1);
314 Binary = (Value & 0x7ff) << 16; // Low imm11 value.
315 Binary |= (Value & 0x1ffc00) >> 11; // High imm10 value.
316 Binary |= isNeg << 10; // Sign bit.
319 case ARM::fixup_arm_thumb_blx: {
320 // The value doesn't encode the low two bits (always zero) and is offset by
321 // four (see fixup_arm_thumb_cp). The value is encoded into disjoint bit
322 // positions in the destination opcode. x = unchanged, I = immediate value
323 // bit, S = sign extension bit, 0 = zero.
325 // BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0
327 // Note that the halfwords are stored high first, low second; so we need
328 // to transpose the fixup value here to map properly.
329 unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
331 Value = 0xfffff & ((Value - 2) >> 2);
332 Binary = (Value & 0x3ff) << 17; // Low imm10L value.
333 Binary |= (Value & 0xffc00) >> 10; // High imm10H value.
334 Binary |= isNeg << 10; // Sign bit.
337 case ARM::fixup_arm_thumb_cp:
338 // Offset by 4, and don't encode the low two bits. Two bytes of that
339 // 'off by 4' is implicitly handled by the half-word ordering of the
340 // Thumb encoding, so we only need to adjust by 2 here.
341 return ((Value - 2) >> 2) & 0xff;
342 case ARM::fixup_arm_thumb_cb: {
343 // Offset by 4 and don't encode the lower bit, which is always 0.
344 uint32_t Binary = (Value - 4) >> 1;
345 return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
347 case ARM::fixup_arm_thumb_br:
348 // Offset by 4 and don't encode the lower bit, which is always 0.
349 return ((Value - 4) >> 1) & 0x7ff;
350 case ARM::fixup_arm_thumb_bcc:
351 // Offset by 4 and don't encode the lower bit, which is always 0.
352 return ((Value - 4) >> 1) & 0xff;
353 case ARM::fixup_arm_pcrel_10:
354 Value = Value - 4; // ARM fixups offset by an additional word and don't
355 // need to adjust for the half-word ordering.
357 case ARM::fixup_t2_pcrel_10: {
358 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
361 if ((int64_t)Value < 0) {
365 // These values don't encode the low two bits since they're always zero.
367 assert ((Value < 256) && "Out of range pc-relative fixup value!");
368 Value |= isAdd << 23;
370 // Same addressing mode as fixup_arm_pcrel_10,
371 // but with 16-bit halfwords swapped.
372 if (Kind == ARM::fixup_t2_pcrel_10) {
373 uint32_t swapped = (Value & 0xFFFF0000) >> 16;
374 swapped |= (Value & 0x0000FFFF) << 16;
385 // FIXME: This should be in a separate file.
386 // ELF is an ELF of course...
387 class ELFARMAsmBackend : public ARMAsmBackend {
389 Triple::OSType OSType;
390 ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
391 : ARMAsmBackend(T), OSType(_OSType) { }
393 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
394 uint64_t Value) const;
396 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
397 return createELFObjectWriter(new ARMELFObjectWriter(OSType), OS,
398 /*IsLittleEndian*/ true);
402 // FIXME: Raise this to share code between Darwin and ELF.
403 void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
404 unsigned DataSize, uint64_t Value) const {
405 unsigned NumBytes = 4; // FIXME: 2 for Thumb
406 Value = adjustFixupValue(Fixup.getKind(), Value);
407 if (!Value) return; // Doesn't change encoding.
409 unsigned Offset = Fixup.getOffset();
411 // For each byte of the fragment that the fixup touches, mask in the bits from
412 // the fixup value. The Value has been "split up" into the appropriate
414 for (unsigned i = 0; i != NumBytes; ++i)
415 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
418 // FIXME: This should be in a separate file.
419 class DarwinARMAsmBackend : public ARMAsmBackend {
421 const object::mach::CPUSubtypeARM Subtype;
422 DarwinARMAsmBackend(const Target &T, object::mach::CPUSubtypeARM st)
423 : ARMAsmBackend(T), Subtype(st) { }
425 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
426 return createMachObjectWriter(new ARMMachObjectWriter(
428 object::mach::CTM_ARM,
431 /*IsLittleEndian=*/true);
434 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
435 uint64_t Value) const;
437 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
442 /// getFixupKindNumBytes - The number of bytes the fixup may change.
443 static unsigned getFixupKindNumBytes(unsigned Kind) {
446 llvm_unreachable("Unknown fixup kind!");
449 case ARM::fixup_arm_thumb_bcc:
450 case ARM::fixup_arm_thumb_cp:
451 case ARM::fixup_thumb_adr_pcrel_10:
455 case ARM::fixup_arm_thumb_br:
456 case ARM::fixup_arm_thumb_cb:
459 case ARM::fixup_arm_ldst_pcrel_12:
460 case ARM::fixup_arm_pcrel_10:
461 case ARM::fixup_arm_adr_pcrel_12:
462 case ARM::fixup_arm_condbranch:
463 case ARM::fixup_arm_uncondbranch:
467 case ARM::fixup_t2_ldst_pcrel_12:
468 case ARM::fixup_t2_condbranch:
469 case ARM::fixup_t2_uncondbranch:
470 case ARM::fixup_t2_pcrel_10:
471 case ARM::fixup_t2_adr_pcrel_12:
472 case ARM::fixup_arm_thumb_bl:
473 case ARM::fixup_arm_thumb_blx:
474 case ARM::fixup_arm_movt_hi16:
475 case ARM::fixup_arm_movw_lo16:
476 case ARM::fixup_arm_movt_hi16_pcrel:
477 case ARM::fixup_arm_movw_lo16_pcrel:
478 case ARM::fixup_t2_movt_hi16:
479 case ARM::fixup_t2_movw_lo16:
480 case ARM::fixup_t2_movt_hi16_pcrel:
481 case ARM::fixup_t2_movw_lo16_pcrel:
486 void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
487 unsigned DataSize, uint64_t Value) const {
488 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
489 Value = adjustFixupValue(Fixup.getKind(), Value);
490 if (!Value) return; // Doesn't change encoding.
492 unsigned Offset = Fixup.getOffset();
493 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
495 // For each byte of the fragment that the fixup touches, mask in the
496 // bits from the fixup value.
497 for (unsigned i = 0; i != NumBytes; ++i)
498 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
501 } // end anonymous namespace
503 TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
504 const std::string &TT) {
505 Triple TheTriple(TT);
507 if (TheTriple.isOSDarwin()) {
508 if (TheTriple.getArchName() == "armv6" ||
509 TheTriple.getArchName() == "thumbv6")
510 return new DarwinARMAsmBackend(T, object::mach::CSARM_V6);
511 return new DarwinARMAsmBackend(T, object::mach::CSARM_V7);
514 if (TheTriple.isOSWindows())
515 assert(0 && "Windows not supported on ARM");
517 return new ELFARMAsmBackend(T, Triple(TT).getOS());