Revert 124230. It was causing test failures.
[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_branch",        0,            24,  MCFixupKindInfo::FKF_IsPCRel },
71 { "fixup_t2_condbranch",     0,            32,  MCFixupKindInfo::FKF_IsPCRel },
72 { "fixup_t2_uncondbranch",   0,            32,  MCFixupKindInfo::FKF_IsPCRel },
73 { "fixup_arm_thumb_br",      0,            16,  MCFixupKindInfo::FKF_IsPCRel },
74 { "fixup_arm_thumb_bl",      0,            32,  MCFixupKindInfo::FKF_IsPCRel },
75 { "fixup_arm_thumb_blx",     7,            21,  MCFixupKindInfo::FKF_IsPCRel },
76 { "fixup_arm_thumb_cb",      0,            16,  MCFixupKindInfo::FKF_IsPCRel },
77 { "fixup_arm_thumb_cp",      1,             8,  MCFixupKindInfo::FKF_IsPCRel },
78 { "fixup_arm_thumb_bcc",     1,             8,  MCFixupKindInfo::FKF_IsPCRel },
79 // movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16 - 19.
80 { "fixup_arm_movt_hi16",     0,            20,  0 },
81 { "fixup_arm_movw_lo16",     0,            20,  0 },
82 { "fixup_t2_movt_hi16",      0,            20,  0 },
83 { "fixup_t2_movw_lo16",      0,            20,  0 },
84 { "fixup_arm_movt_hi16_pcrel", 0,          20,  MCFixupKindInfo::FKF_IsPCRel },
85 { "fixup_arm_movw_lo16_pcrel", 0,          20,  MCFixupKindInfo::FKF_IsPCRel },
86 { "fixup_t2_movt_hi16_pcrel", 0,           20,  MCFixupKindInfo::FKF_IsPCRel },
87 { "fixup_t2_movw_lo16_pcrel", 0,           20,  MCFixupKindInfo::FKF_IsPCRel },
88     };
89
90     if (Kind < FirstTargetFixupKind)
91       return TargetAsmBackend::getFixupKindInfo(Kind);
92
93     assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
94            "Invalid kind!");
95     return Infos[Kind - FirstTargetFixupKind];
96   }
97
98   bool MayNeedRelaxation(const MCInst &Inst) const;
99
100   void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
101
102   bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
103
104   void HandleAssemblerFlag(MCAssemblerFlag Flag) {
105     switch (Flag) {
106     default: break;
107     case MCAF_Code16:
108       setIsThumb(true);
109       break;
110     case MCAF_Code32:
111       setIsThumb(false);
112       break;
113     }
114   }
115
116   unsigned getPointerSize() const { return 4; }
117   bool isThumb() const { return isThumbMode; }
118   void setIsThumb(bool it) { isThumbMode = it; }
119 };
120 } // end anonymous namespace
121
122 bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
123   // FIXME: Thumb targets, different move constant targets..
124   return false;
125 }
126
127 void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
128   assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
129   return;
130 }
131
132 bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
133   if (isThumb()) {
134     // FIXME: 0xbf00 is the ARMv7 value. For v6 and before, we'll need to
135     // use 0x46c0 (which is a 'mov r8, r8' insn).
136     uint64_t NumNops = Count / 2;
137     for (uint64_t i = 0; i != NumNops; ++i)
138       OW->Write16(0xbf00);
139     if (Count & 1)
140       OW->Write8(0);
141     return true;
142   }
143   // ARM mode
144   uint64_t NumNops = Count / 4;
145   for (uint64_t i = 0; i != NumNops; ++i)
146     OW->Write32(0xe1a00000);
147   switch (Count % 4) {
148   default: break; // No leftover bytes to write
149   case 1: OW->Write8(0); break;
150   case 2: OW->Write16(0); break;
151   case 3: OW->Write16(0); OW->Write8(0xa0); break;
152   }
153
154   return true;
155 }
156
157 static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
158   switch (Kind) {
159   default:
160     llvm_unreachable("Unknown fixup kind!");
161   case FK_Data_1:
162   case FK_Data_2:
163   case FK_Data_4:
164     return Value;
165   case ARM::fixup_arm_movt_hi16:
166   case ARM::fixup_arm_movt_hi16_pcrel:
167     Value >>= 16;
168     // Fallthrough
169   case ARM::fixup_arm_movw_lo16:
170   case ARM::fixup_arm_movw_lo16_pcrel: {
171     unsigned Hi4 = (Value & 0xF000) >> 12;
172     unsigned Lo12 = Value & 0x0FFF;
173     // inst{19-16} = Hi4;
174     // inst{11-0} = Lo12;
175     Value = (Hi4 << 16) | (Lo12);
176     return Value;
177   }
178   case ARM::fixup_t2_movt_hi16:
179   case ARM::fixup_t2_movt_hi16_pcrel:
180     Value >>= 16;
181     // Fallthrough
182   case ARM::fixup_t2_movw_lo16:
183   case ARM::fixup_t2_movw_lo16_pcrel: {
184     unsigned Hi4 = (Value & 0xF000) >> 12;
185     unsigned i = (Value & 0x800) >> 11;
186     unsigned Mid3 = (Value & 0x700) >> 8;
187     unsigned Lo8 = Value & 0x0FF;
188     // inst{19-16} = Hi4;
189     // inst{26} = i;
190     // inst{14-12} = Mid3;
191     // inst{7-0} = Lo8;
192     Value = (Hi4 << 16) | (i << 26) | (Mid3 << 12) | (Lo8);
193
194     uint64_t swapped = (Value & 0xFFFF0000) >> 16;
195     swapped |= (Value & 0x0000FFFF) << 16;
196     return swapped;
197   }
198   case ARM::fixup_arm_ldst_pcrel_12:
199     // ARM PC-relative values are offset by 8.
200     Value -= 4;
201     // FALLTHROUGH
202   case ARM::fixup_t2_ldst_pcrel_12: {
203     // Offset by 4, adjusted by two due to the half-word ordering of thumb.
204     Value -= 4;
205     bool isAdd = true;
206     if ((int64_t)Value < 0) {
207       Value = -Value;
208       isAdd = false;
209     }
210     assert ((Value < 4096) && "Out of range pc-relative fixup value!");
211     Value |= isAdd << 23;
212
213     // Same addressing mode as fixup_arm_pcrel_10,
214     // but with 16-bit halfwords swapped.
215     if (Kind == ARM::fixup_t2_ldst_pcrel_12) {
216       uint64_t swapped = (Value & 0xFFFF0000) >> 16;
217       swapped |= (Value & 0x0000FFFF) << 16;
218       return swapped;
219     }
220
221     return Value;
222   }
223   case ARM::fixup_thumb_adr_pcrel_10:
224     return ((Value - 4) >> 2) & 0xff;
225   case ARM::fixup_arm_adr_pcrel_12: {
226     // ARM PC-relative values are offset by 8.
227     Value -= 8;
228     unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
229     if ((int64_t)Value < 0) {
230       Value = -Value;
231       opc = 2; // 0b0010
232     }
233     assert(ARM_AM::getSOImmVal(Value) != -1 &&
234            "Out of range pc-relative fixup value!");
235     // Encode the immediate and shift the opcode into place.
236     return ARM_AM::getSOImmVal(Value) | (opc << 21);
237   }
238
239   case ARM::fixup_t2_adr_pcrel_12: {
240     Value -= 4;
241     unsigned opc = 0;
242     if ((int64_t)Value < 0) {
243       Value = -Value;
244       opc = 5;
245     }
246
247     uint32_t out = (opc << 21);
248     out |= (Value & 0x800) << 14;
249     out |= (Value & 0x700) << 4;
250     out |= (Value & 0x0FF);
251
252     uint64_t swapped = (out & 0xFFFF0000) >> 16;
253     swapped |= (out & 0x0000FFFF) << 16;
254     return swapped;
255   }
256
257   case ARM::fixup_arm_branch:
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: {
262     Value = Value - 4;
263     Value >>= 1; // Low bit is not encoded.
264
265     uint32_t out = 0;
266     bool I =  Value & 0x800000;
267     bool J1 = Value & 0x400000;
268     bool J2 = Value & 0x200000;
269     J1 ^= I;
270     J2 ^= I;
271
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
277
278     uint64_t swapped = (out & 0xFFFF0000) >> 16;
279     swapped |= (out & 0x0000FFFF) << 16;
280     return swapped;
281   }
282   case ARM::fixup_t2_condbranch: {
283     Value = Value - 4;
284     Value >>= 1; // Low bit is not encoded.
285
286     uint64_t out = 0;
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
292
293     uint32_t swapped = (out & 0xFFFF0000) >> 16;
294     swapped |= (out & 0x0000FFFF) << 16;
295     return swapped;
296   }
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
301     //
302     //   BL:  xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
303     //
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) < 0) ? 1 : 0;
307     uint32_t Binary = 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.
312     return Binary;
313   }
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.
319     //
320     //   BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0
321     //
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) < 0) ? 1 : 0;
325     uint32_t Binary = 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.
330     return Binary;
331   }
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);
341   }
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.
351     // Fall through.
352   case ARM::fixup_t2_pcrel_10: {
353     // Offset by 4, adjusted by two due to the half-word ordering of thumb.
354     Value = Value - 4;
355     bool isAdd = true;
356     if ((int64_t)Value < 0) {
357       Value = -Value;
358       isAdd = false;
359     }
360     // These values don't encode the low two bits since they're always zero.
361     Value >>= 2;
362     assert ((Value < 256) && "Out of range pc-relative fixup value!");
363     Value |= isAdd << 23;
364
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;
370       return swapped;
371     }
372
373     return Value;
374   }
375   }
376 }
377
378 namespace {
379
380 // FIXME: This should be in a separate file.
381 // ELF is an ELF of course...
382 class ELFARMAsmBackend : public ARMAsmBackend {
383 public:
384   Triple::OSType OSType;
385   ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
386     : ARMAsmBackend(T), OSType(_OSType) { }
387
388   void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
389                   uint64_t Value) const;
390
391   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
392     return createELFObjectWriter(new ARMELFObjectWriter(OSType), OS,
393                               /*IsLittleEndian*/ true);
394   }
395 };
396
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.
403
404   unsigned Offset = Fixup.getOffset();
405   assert(Offset % NumBytes == 0 && "Offset mod NumBytes is nonzero!");
406
407   // For each byte of the fragment that the fixup touches, mask in the bits from
408   // the fixup value. The Value has been "split up" into the appropriate
409   // bitfields above.
410   for (unsigned i = 0; i != NumBytes; ++i)
411     Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
412 }
413
414 // FIXME: This should be in a separate file.
415 class DarwinARMAsmBackend : public ARMAsmBackend {
416 public:
417   DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) { }
418
419   void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
420                   uint64_t Value) const;
421
422   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
423     // FIXME: Subtarget info should be derived. Force v7 for now.
424     return createMachObjectWriter(new ARMMachObjectWriter(
425                                     /*Is64Bit=*/false,
426                                     object::mach::CTM_ARM,
427                                     object::mach::CSARM_V7),
428                                   OS,
429                                   /*IsLittleEndian=*/true);
430   }
431
432   virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
433     return false;
434   }
435 };
436
437 /// getFixupKindNumBytes - The number of bytes the fixup may change.
438 static unsigned getFixupKindNumBytes(unsigned Kind) {
439   switch (Kind) {
440   default:
441     llvm_unreachable("Unknown fixup kind!");
442
443   case FK_Data_1:
444   case ARM::fixup_arm_thumb_bcc:
445   case ARM::fixup_arm_thumb_cp:
446   case ARM::fixup_thumb_adr_pcrel_10:
447     return 1;
448
449   case FK_Data_2:
450   case ARM::fixup_arm_thumb_br:
451   case ARM::fixup_arm_thumb_cb:
452     return 2;
453
454   case ARM::fixup_arm_ldst_pcrel_12:
455   case ARM::fixup_arm_pcrel_10:
456   case ARM::fixup_arm_adr_pcrel_12:
457   case ARM::fixup_arm_branch:
458     return 3;
459
460   case FK_Data_4:
461   case ARM::fixup_t2_ldst_pcrel_12:
462   case ARM::fixup_t2_condbranch:
463   case ARM::fixup_t2_uncondbranch:
464   case ARM::fixup_t2_pcrel_10:
465   case ARM::fixup_t2_adr_pcrel_12:
466   case ARM::fixup_arm_thumb_bl:
467   case ARM::fixup_arm_thumb_blx:
468   case ARM::fixup_arm_movt_hi16:
469   case ARM::fixup_arm_movw_lo16:
470   case ARM::fixup_arm_movt_hi16_pcrel:
471   case ARM::fixup_arm_movw_lo16_pcrel:
472   case ARM::fixup_t2_movt_hi16:
473   case ARM::fixup_t2_movw_lo16:
474   case ARM::fixup_t2_movt_hi16_pcrel:
475   case ARM::fixup_t2_movw_lo16_pcrel:
476     return 4;
477   }
478 }
479
480 void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
481                                      unsigned DataSize, uint64_t Value) const {
482   unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
483   Value = adjustFixupValue(Fixup.getKind(), Value);
484   if (!Value) return;           // Doesn't change encoding.
485
486   unsigned Offset = Fixup.getOffset();
487   assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
488
489   // For each byte of the fragment that the fixup touches, mask in the
490   // bits from the fixup value.
491   for (unsigned i = 0; i != NumBytes; ++i)
492     Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
493 }
494
495 } // end anonymous namespace
496
497 TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
498                                             const std::string &TT) {
499   switch (Triple(TT).getOS()) {
500   case Triple::Darwin:
501     return new DarwinARMAsmBackend(T);
502   case Triple::MinGW32:
503   case Triple::Cygwin:
504   case Triple::Win32:
505     assert(0 && "Windows not supported on ARM");
506   default:
507     return new ELFARMAsmBackend(T, Triple(TT).getOS());
508   }
509 }