Add support for ARM ldrexd/strexd intrinsics. They both use i32 register pairs
[oota-llvm.git] / lib / Target / ARM / ARMAsmBackend.cpp
1 //===-- 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
10 #include "ARM.h"
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"
28 using namespace llvm;
29
30 namespace {
31 class ARMMachObjectWriter : public MCMachObjectTargetWriter {
32 public:
33   ARMMachObjectWriter(bool Is64Bit, uint32_t CPUType,
34                       uint32_t CPUSubtype)
35     : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype,
36                                /*UseAggressiveSymbolFolding=*/true) {}
37 };
38
39 class ARMELFObjectWriter : public MCELFObjectTargetWriter {
40 public:
41   ARMELFObjectWriter(Triple::OSType OSType)
42     : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSType, ELF::EM_ARM,
43                               /*HasRelocationAddend*/ false) {}
44 };
45
46 class ARMAsmBackend : public TargetAsmBackend {
47   bool isThumbMode;  // Currently emitting Thumb code.
48 public:
49   ARMAsmBackend(const Target &T) : TargetAsmBackend(), isThumbMode(false) {}
50
51   unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
52
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
56 // ARMFixupKinds.h.
57 //
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",     0,             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 },
89     };
90
91     if (Kind < FirstTargetFixupKind)
92       return TargetAsmBackend::getFixupKindInfo(Kind);
93
94     assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
95            "Invalid kind!");
96     return Infos[Kind - FirstTargetFixupKind];
97   }
98
99   bool MayNeedRelaxation(const MCInst &Inst) const;
100
101   void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
102
103   bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
104
105   void HandleAssemblerFlag(MCAssemblerFlag Flag) {
106     switch (Flag) {
107     default: break;
108     case MCAF_Code16:
109       setIsThumb(true);
110       break;
111     case MCAF_Code32:
112       setIsThumb(false);
113       break;
114     }
115   }
116
117   unsigned getPointerSize() const { return 4; }
118   bool isThumb() const { return isThumbMode; }
119   void setIsThumb(bool it) { isThumbMode = it; }
120 };
121 } // end anonymous namespace
122
123 bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
124   // FIXME: Thumb targets, different move constant targets..
125   return false;
126 }
127
128 void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
129   assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
130   return;
131 }
132
133 bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
134   if (isThumb()) {
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)
139       OW->Write16(0xbf00);
140     if (Count & 1)
141       OW->Write8(0);
142     return true;
143   }
144   // ARM mode
145   uint64_t NumNops = Count / 4;
146   for (uint64_t i = 0; i != NumNops; ++i)
147     OW->Write32(0xe1a00000);
148   switch (Count % 4) {
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;
153   }
154
155   return true;
156 }
157
158 static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
159   switch (Kind) {
160   default:
161     llvm_unreachable("Unknown fixup kind!");
162   case FK_Data_1:
163   case FK_Data_2:
164   case FK_Data_4:
165     return Value;
166   case ARM::fixup_arm_movt_hi16:
167     Value >>= 16;
168     // Fallthrough
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);
179     return Value;
180   }
181   case ARM::fixup_t2_movt_hi16:
182     Value >>= 16;
183     // Fallthrough
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;
192     // inst{26} = i;
193     // inst{14-12} = Mid3;
194     // inst{7-0} = Lo8;
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;
200     return swapped;
201   }
202   case ARM::fixup_arm_ldst_pcrel_12:
203     // ARM PC-relative values are offset by 8.
204     Value -= 4;
205     // FALLTHROUGH
206   case ARM::fixup_t2_ldst_pcrel_12: {
207     // Offset by 4, adjusted by two due to the half-word ordering of thumb.
208     Value -= 4;
209     bool isAdd = true;
210     if ((int64_t)Value < 0) {
211       Value = -Value;
212       isAdd = false;
213     }
214     assert ((Value < 4096) && "Out of range pc-relative fixup value!");
215     Value |= isAdd << 23;
216
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;
222       return swapped;
223     }
224
225     return Value;
226   }
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.
231     Value -= 8;
232     unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
233     if ((int64_t)Value < 0) {
234       Value = -Value;
235       opc = 2; // 0b0010
236     }
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);
241   }
242
243   case ARM::fixup_t2_adr_pcrel_12: {
244     Value -= 4;
245     unsigned opc = 0;
246     if ((int64_t)Value < 0) {
247       Value = -Value;
248       opc = 5;
249     }
250
251     uint32_t out = (opc << 21);
252     out |= (Value & 0x800) << 15;
253     out |= (Value & 0x700) << 4;
254     out |= (Value & 0x0FF);
255
256     uint64_t swapped = (out & 0xFFFF0000) >> 16;
257     swapped |= (out & 0x0000FFFF) << 16;
258     return swapped;
259   }
260
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: {
267     Value = Value - 4;
268     Value >>= 1; // Low bit is not encoded.
269
270     uint32_t out = 0;
271     bool I =  Value & 0x800000;
272     bool J1 = Value & 0x400000;
273     bool J2 = Value & 0x200000;
274     J1 ^= I;
275     J2 ^= I;
276
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
282
283     uint64_t swapped = (out & 0xFFFF0000) >> 16;
284     swapped |= (out & 0x0000FFFF) << 16;
285     return swapped;
286   }
287   case ARM::fixup_t2_condbranch: {
288     Value = Value - 4;
289     Value >>= 1; // Low bit is not encoded.
290
291     uint64_t out = 0;
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
297
298     uint32_t swapped = (out & 0xFFFF0000) >> 16;
299     swapped |= (out & 0x0000FFFF) << 16;
300     return swapped;
301   }
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
306     //
307     //   BL:  xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
308     //
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 - 4) < 0) ? 1 : 0;
312     uint32_t Binary = 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.
317     return Binary;
318   }
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.
324     //
325     //   BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0
326     //
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-4) < 0) ? 1 : 0;
330     uint32_t Binary = 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.
335     return Binary;
336   }
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);
346   }
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.
356     // Fall through.
357   case ARM::fixup_t2_pcrel_10: {
358     // Offset by 4, adjusted by two due to the half-word ordering of thumb.
359     Value = Value - 4;
360     bool isAdd = true;
361     if ((int64_t)Value < 0) {
362       Value = -Value;
363       isAdd = false;
364     }
365     // These values don't encode the low two bits since they're always zero.
366     Value >>= 2;
367     assert ((Value < 256) && "Out of range pc-relative fixup value!");
368     Value |= isAdd << 23;
369
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;
375       return swapped;
376     }
377
378     return Value;
379   }
380   }
381 }
382
383 namespace {
384
385 // FIXME: This should be in a separate file.
386 // ELF is an ELF of course...
387 class ELFARMAsmBackend : public ARMAsmBackend {
388 public:
389   Triple::OSType OSType;
390   ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
391     : ARMAsmBackend(T), OSType(_OSType) { }
392
393   void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
394                   uint64_t Value) const;
395
396   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
397     return createELFObjectWriter(new ARMELFObjectWriter(OSType), OS,
398                               /*IsLittleEndian*/ true);
399   }
400 };
401
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.
408
409   unsigned Offset = Fixup.getOffset();
410
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
413   // bitfields above.
414   for (unsigned i = 0; i != NumBytes; ++i)
415     Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
416 }
417
418 // FIXME: This should be in a separate file.
419 class DarwinARMAsmBackend : public ARMAsmBackend {
420 public:
421   const object::mach::CPUSubtypeARM Subtype;
422   DarwinARMAsmBackend(const Target &T, object::mach::CPUSubtypeARM st)
423     : ARMAsmBackend(T), Subtype(st) { }
424
425   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
426     return createMachObjectWriter(new ARMMachObjectWriter(
427                                     /*Is64Bit=*/false,
428                                     object::mach::CTM_ARM,
429                                     Subtype),
430                                   OS,
431                                   /*IsLittleEndian=*/true);
432   }
433
434   void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
435                   uint64_t Value) const;
436
437   virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
438     return false;
439   }
440 };
441
442 /// getFixupKindNumBytes - The number of bytes the fixup may change.
443 static unsigned getFixupKindNumBytes(unsigned Kind) {
444   switch (Kind) {
445   default:
446     llvm_unreachable("Unknown fixup kind!");
447
448   case FK_Data_1:
449   case ARM::fixup_arm_thumb_bcc:
450   case ARM::fixup_arm_thumb_cp:
451   case ARM::fixup_thumb_adr_pcrel_10:
452     return 1;
453
454   case FK_Data_2:
455   case ARM::fixup_arm_thumb_br:
456   case ARM::fixup_arm_thumb_cb:
457     return 2;
458
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:
464     return 3;
465
466   case FK_Data_4:
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:
482     return 4;
483   }
484 }
485
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.
491
492   unsigned Offset = Fixup.getOffset();
493   assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
494
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);
499 }
500
501 } // end anonymous namespace
502
503 TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
504                                             const std::string &TT) {
505   Triple TheTriple(TT);
506
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);
512   }
513
514   if (TheTriple.isOSWindows())
515     assert(0 && "Windows not supported on ARM");
516
517   return new ELFARMAsmBackend(T, Triple(TT).getOS());
518 }