MC: Move target specific fixup info descriptors to TargetAsmBackend instead of
[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/MCExpr.h"
17 #include "llvm/MC/MCObjectFormat.h"
18 #include "llvm/MC/MCObjectWriter.h"
19 #include "llvm/MC/MCSectionELF.h"
20 #include "llvm/MC/MCSectionMachO.h"
21 #include "llvm/Object/MachOFormat.h"
22 #include "llvm/Support/ELF.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Target/TargetAsmBackend.h"
26 #include "llvm/Target/TargetRegistry.h"
27 using namespace llvm;
28
29 namespace {
30 class ARMAsmBackend : public TargetAsmBackend {
31   bool isThumbMode;  // Currently emitting Thumb code.
32 public:
33   ARMAsmBackend(const Target &T) : TargetAsmBackend(), isThumbMode(false) {}
34
35   unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
36
37   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
38     const static MCFixupKindInfo Infos[ARM::NumTargetFixupKinds] = {
39 // This table *must* be in the order that the fixup_* kinds are defined in
40 // ARMFixupKinds.h.
41 //
42 // Name                      Offset (bits) Size (bits)     Flags
43 { "fixup_arm_ldst_pcrel_12", 1,            24,  MCFixupKindInfo::FKF_IsPCRel },
44 { "fixup_t2_ldst_pcrel_12",  0,            32,  MCFixupKindInfo::FKF_IsPCRel |
45                                    MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
46 { "fixup_arm_pcrel_10",      1,            24,  MCFixupKindInfo::FKF_IsPCRel },
47 { "fixup_t2_pcrel_10",       0,            32,  MCFixupKindInfo::FKF_IsPCRel |
48                                    MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
49 { "fixup_thumb_adr_pcrel_10",0,            8,   MCFixupKindInfo::FKF_IsPCRel |
50                                    MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
51 { "fixup_arm_adr_pcrel_12",  1,            24,  MCFixupKindInfo::FKF_IsPCRel },
52 { "fixup_t2_adr_pcrel_12",   0,            32,  MCFixupKindInfo::FKF_IsPCRel |
53                                    MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
54 { "fixup_arm_branch",        0,            24,  MCFixupKindInfo::FKF_IsPCRel },
55 { "fixup_t2_condbranch",     0,            32,  MCFixupKindInfo::FKF_IsPCRel },
56 { "fixup_t2_uncondbranch",   0,            32,  MCFixupKindInfo::FKF_IsPCRel },
57 { "fixup_arm_thumb_br",      0,            16,  MCFixupKindInfo::FKF_IsPCRel },
58 { "fixup_arm_thumb_bl",      0,            32,  MCFixupKindInfo::FKF_IsPCRel },
59 { "fixup_arm_thumb_blx",     7,            21,  MCFixupKindInfo::FKF_IsPCRel },
60 { "fixup_arm_thumb_cb",      0,            16,  MCFixupKindInfo::FKF_IsPCRel },
61 { "fixup_arm_thumb_cp",      1,             8,  MCFixupKindInfo::FKF_IsPCRel },
62 { "fixup_arm_thumb_bcc",     1,             8,  MCFixupKindInfo::FKF_IsPCRel },
63 { "fixup_arm_movt_hi16",     0,            16,  0 },
64 { "fixup_arm_movw_lo16",     0,            16,  0 },
65     };
66
67     if (Kind < FirstTargetFixupKind)
68       return TargetAsmBackend::getFixupKindInfo(Kind);
69
70     assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
71            "Invalid kind!");
72     return Infos[Kind - FirstTargetFixupKind];
73   }
74
75   bool MayNeedRelaxation(const MCInst &Inst) const;
76
77   void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
78
79   bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
80
81   void HandleAssemblerFlag(MCAssemblerFlag Flag) {
82     switch (Flag) {
83     default: break;
84     case MCAF_Code16:
85       setIsThumb(true);
86       break;
87     case MCAF_Code32:
88       setIsThumb(false);
89       break;
90     }
91   }
92
93   unsigned getPointerSize() const { return 4; }
94   bool isThumb() const { return isThumbMode; }
95   void setIsThumb(bool it) { isThumbMode = it; }
96 };
97 } // end anonymous namespace
98
99 bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
100   // FIXME: Thumb targets, different move constant targets..
101   return false;
102 }
103
104 void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
105   assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
106   return;
107 }
108
109 bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
110   if (isThumb()) {
111     assert (((Count & 1) == 0) && "Unaligned Nop data fragment!");
112     // FIXME: 0xbf00 is the ARMv7 value. For v6 and before, we'll need to
113     // use 0x46c0 (which is a 'mov r8, r8' insn).
114     Count /= 2;
115     for (uint64_t i = 0; i != Count; ++i)
116       OW->Write16(0xbf00);
117     return true;
118   }
119   // ARM mode
120   Count /= 4;
121   for (uint64_t i = 0; i != Count; ++i)
122     OW->Write32(0xe1a00000);
123   return true;
124 }
125
126 static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
127   switch (Kind) {
128   default:
129     llvm_unreachable("Unknown fixup kind!");
130   case FK_Data_4:
131     return Value;
132   case ARM::fixup_arm_movt_hi16:
133   case ARM::fixup_arm_movw_lo16: {
134     unsigned Hi4 = (Value & 0xF000) >> 12;
135     unsigned Lo12 = Value & 0x0FFF;
136     // inst{19-16} = Hi4;
137     // inst{11-0} = Lo12;
138     Value = (Hi4 << 16) | (Lo12);
139     return Value;
140   }
141   case ARM::fixup_arm_ldst_pcrel_12:
142     // ARM PC-relative values are offset by 8.
143     Value -= 4;
144     // FALLTHROUGH
145   case ARM::fixup_t2_ldst_pcrel_12: {
146     // Offset by 4, adjusted by two due to the half-word ordering of thumb.
147     Value -= 4;
148     bool isAdd = true;
149     if ((int64_t)Value < 0) {
150       Value = -Value;
151       isAdd = false;
152     }
153     assert ((Value < 4096) && "Out of range pc-relative fixup value!");
154     Value |= isAdd << 23;
155
156     // Same addressing mode as fixup_arm_pcrel_10,
157     // but with 16-bit halfwords swapped.
158     if (Kind == ARM::fixup_t2_ldst_pcrel_12) {
159       uint64_t swapped = (Value & 0xFFFF0000) >> 16;
160       swapped |= (Value & 0x0000FFFF) << 16;
161       return swapped;
162     }
163
164     return Value;
165   }
166   case ARM::fixup_thumb_adr_pcrel_10:
167     return ((Value - 4) >> 2) & 0xff;
168   case ARM::fixup_arm_adr_pcrel_12: {
169     // ARM PC-relative values are offset by 8.
170     Value -= 8;
171     unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
172     if ((int64_t)Value < 0) {
173       Value = -Value;
174       opc = 2; // 0b0010
175     }
176     assert(ARM_AM::getSOImmVal(Value) != -1 &&
177            "Out of range pc-relative fixup value!");
178     // Encode the immediate and shift the opcode into place.
179     return ARM_AM::getSOImmVal(Value) | (opc << 21);
180   }
181
182   case ARM::fixup_t2_adr_pcrel_12: {
183     Value -= 4;
184     unsigned opc = 0;
185     if ((int64_t)Value < 0) {
186       Value = -Value;
187       opc = 5;
188     }
189
190     uint32_t out = (opc << 21);
191     out |= (Value & 0x800) << 14;
192     out |= (Value & 0x700) << 4;
193     out |= (Value & 0x0FF);
194
195     uint64_t swapped = (out & 0xFFFF0000) >> 16;
196     swapped |= (out & 0x0000FFFF) << 16;
197     return swapped;
198   }
199
200   case ARM::fixup_arm_branch:
201     // These values don't encode the low two bits since they're always zero.
202     // Offset by 8 just as above.
203     return 0xffffff & ((Value - 8) >> 2);
204   case ARM::fixup_t2_uncondbranch: {
205     Value = Value - 4;
206     Value >>= 1; // Low bit is not encoded.
207
208     uint32_t out = 0;
209     bool I =  Value & 0x800000;
210     bool J1 = Value & 0x400000;
211     bool J2 = Value & 0x200000;
212     J1 ^= I;
213     J2 ^= I;
214
215     out |= I  << 26; // S bit
216     out |= !J1 << 13; // J1 bit
217     out |= !J2 << 11; // J2 bit
218     out |= (Value & 0x1FF800)  << 5; // imm6 field
219     out |= (Value & 0x0007FF);        // imm11 field
220
221     uint64_t swapped = (out & 0xFFFF0000) >> 16;
222     swapped |= (out & 0x0000FFFF) << 16;
223     return swapped;
224   }
225   case ARM::fixup_t2_condbranch: {
226     Value = Value - 4;
227     Value >>= 1; // Low bit is not encoded.
228
229     uint64_t out = 0;
230     out |= (Value & 0x80000) << 7; // S bit
231     out |= (Value & 0x40000) >> 7; // J2 bit
232     out |= (Value & 0x20000) >> 4; // J1 bit
233     out |= (Value & 0x1F800) << 5; // imm6 field
234     out |= (Value & 0x007FF);      // imm11 field
235
236     uint32_t swapped = (out & 0xFFFF0000) >> 16;
237     swapped |= (out & 0x0000FFFF) << 16;
238     return swapped;
239   }
240   case ARM::fixup_arm_thumb_bl: {
241     // The value doesn't encode the low bit (always zero) and is offset by
242     // four. The value is encoded into disjoint bit positions in the destination
243     // opcode. x = unchanged, I = immediate value bit, S = sign extension bit
244     //
245     //   BL:  xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
246     //
247     // Note that the halfwords are stored high first, low second; so we need
248     // to transpose the fixup value here to map properly.
249     unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
250     uint32_t Binary = 0;
251     Value = 0x3fffff & ((Value - 4) >> 1);
252     Binary  = (Value & 0x7ff) << 16;    // Low imm11 value.
253     Binary |= (Value & 0x1ffc00) >> 11; // High imm10 value.
254     Binary |= isNeg << 10;              // Sign bit.
255     return Binary;
256   }
257   case ARM::fixup_arm_thumb_blx: {
258     // The value doesn't encode the low two bits (always zero) and is offset by
259     // four (see fixup_arm_thumb_cp). The value is encoded into disjoint bit
260     // positions in the destination opcode. x = unchanged, I = immediate value
261     // bit, S = sign extension bit, 0 = zero.
262     //
263     //   BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0
264     //
265     // Note that the halfwords are stored high first, low second; so we need
266     // to transpose the fixup value here to map properly.
267     unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
268     uint32_t Binary = 0;
269     Value = 0xfffff & ((Value - 2) >> 2);
270     Binary  = (Value & 0x3ff) << 17;    // Low imm10L value.
271     Binary |= (Value & 0xffc00) >> 10;  // High imm10H value.
272     Binary |= isNeg << 10;              // Sign bit.
273     return Binary;
274   }
275   case ARM::fixup_arm_thumb_cp:
276     // Offset by 4, and don't encode the low two bits. Two bytes of that
277     // 'off by 4' is implicitly handled by the half-word ordering of the
278     // Thumb encoding, so we only need to adjust by 2 here.
279     return ((Value - 2) >> 2) & 0xff;
280   case ARM::fixup_arm_thumb_cb: {
281     // Offset by 4 and don't encode the lower bit, which is always 0.
282     uint32_t Binary = (Value - 4) >> 1;
283     return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
284   }
285   case ARM::fixup_arm_thumb_br:
286     // Offset by 4 and don't encode the lower bit, which is always 0.
287     return ((Value - 4) >> 1) & 0x7ff;
288   case ARM::fixup_arm_thumb_bcc:
289     // Offset by 4 and don't encode the lower bit, which is always 0.
290     return ((Value - 4) >> 1) & 0xff;
291   case ARM::fixup_arm_pcrel_10:
292     Value = Value - 4; // ARM fixups offset by an additional word and don't
293                        // need to adjust for the half-word ordering.
294     // Fall through.
295   case ARM::fixup_t2_pcrel_10: {
296     // Offset by 4, adjusted by two due to the half-word ordering of thumb.
297     Value = Value - 4;
298     bool isAdd = true;
299     if ((int64_t)Value < 0) {
300       Value = -Value;
301       isAdd = false;
302     }
303     // These values don't encode the low two bits since they're always zero.
304     Value >>= 2;
305     assert ((Value < 256) && "Out of range pc-relative fixup value!");
306     Value |= isAdd << 23;
307
308     // Same addressing mode as fixup_arm_pcrel_10,
309     // but with 16-bit halfwords swapped.
310     if (Kind == ARM::fixup_t2_pcrel_10) {
311       uint32_t swapped = (Value & 0xFFFF0000) >> 16;
312       swapped |= (Value & 0x0000FFFF) << 16;
313       return swapped;
314     }
315
316     return Value;
317   }
318   }
319 }
320
321 namespace {
322
323 // FIXME: This should be in a separate file.
324 // ELF is an ELF of course...
325 class ELFARMAsmBackend : public ARMAsmBackend {
326   MCELFObjectFormat Format;
327
328 public:
329   Triple::OSType OSType;
330   ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
331     : ARMAsmBackend(T), OSType(_OSType) {
332     HasScatteredSymbols = true;
333   }
334
335   virtual const MCObjectFormat &getObjectFormat() const {
336     return Format;
337   }
338
339   void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
340                   uint64_t Value) const;
341
342   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
343     return createELFObjectWriter(OS, /*Is64Bit=*/false,
344                                  OSType, ELF::EM_ARM,
345                                  /*IsLittleEndian=*/true,
346                                  /*HasRelocationAddend=*/false);
347   }
348 };
349
350 // FIXME: Raise this to share code between Darwin and ELF.
351 void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
352                                   unsigned DataSize, uint64_t Value) const {
353   unsigned NumBytes = 4;        // FIXME: 2 for Thumb
354   Value = adjustFixupValue(Fixup.getKind(), Value);
355   if (!Value) return;           // Doesn't change encoding.
356
357   unsigned Offset = Fixup.getOffset();
358   assert(Offset % NumBytes == 0 && "Offset mod NumBytes is nonzero!");
359
360   // For each byte of the fragment that the fixup touches, mask in the bits from
361   // the fixup value. The Value has been "split up" into the appropriate
362   // bitfields above.
363   for (unsigned i = 0; i != NumBytes; ++i)
364     Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
365 }
366
367 // FIXME: This should be in a separate file.
368 class DarwinARMAsmBackend : public ARMAsmBackend {
369   MCMachOObjectFormat Format;
370 public:
371   DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) {
372     HasScatteredSymbols = true;
373   }
374
375   virtual const MCObjectFormat &getObjectFormat() const {
376     return Format;
377   }
378
379   void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
380                   uint64_t Value) const;
381
382   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
383     // FIXME: Subtarget info should be derived. Force v7 for now.
384     return createMachObjectWriter(OS, /*Is64Bit=*/false,
385                                   object::mach::CTM_ARM,
386                                   object::mach::CSARM_V7,
387                                   /*IsLittleEndian=*/true);
388   }
389
390   virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
391     return false;
392   }
393 };
394
395 /// getFixupKindNumBytes - The number of bytes the fixup may change.
396 static unsigned getFixupKindNumBytes(unsigned Kind) {
397   switch (Kind) {
398   default:
399     llvm_unreachable("Unknown fixup kind!");
400
401   case ARM::fixup_arm_thumb_bcc:
402   case ARM::fixup_arm_thumb_cp:
403   case ARM::fixup_thumb_adr_pcrel_10:
404     return 1;
405
406   case ARM::fixup_arm_thumb_br:
407   case ARM::fixup_arm_thumb_cb:
408     return 2;
409
410   case ARM::fixup_arm_ldst_pcrel_12:
411   case ARM::fixup_arm_pcrel_10:
412   case ARM::fixup_arm_adr_pcrel_12:
413   case ARM::fixup_arm_branch:
414     return 3;
415
416   case FK_Data_4:
417   case ARM::fixup_t2_ldst_pcrel_12:
418   case ARM::fixup_t2_condbranch:
419   case ARM::fixup_t2_uncondbranch:
420   case ARM::fixup_t2_pcrel_10:
421   case ARM::fixup_t2_adr_pcrel_12:
422   case ARM::fixup_arm_thumb_bl:
423   case ARM::fixup_arm_thumb_blx:
424     return 4;
425   }
426 }
427
428 void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
429                                      unsigned DataSize, uint64_t Value) const {
430   unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
431   Value = adjustFixupValue(Fixup.getKind(), Value);
432   if (!Value) return;           // Doesn't change encoding.
433
434   unsigned Offset = Fixup.getOffset();
435   assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
436
437   // For each byte of the fragment that the fixup touches, mask in the
438   // bits from the fixup value.
439   for (unsigned i = 0; i != NumBytes; ++i)
440     Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
441 }
442
443 } // end anonymous namespace
444
445 TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
446                                             const std::string &TT) {
447   switch (Triple(TT).getOS()) {
448   case Triple::Darwin:
449     return new DarwinARMAsmBackend(T);
450   case Triple::MinGW32:
451   case Triple::Cygwin:
452   case Triple::Win32:
453     assert(0 && "Windows not supported on ARM");
454   default:
455     return new ELFARMAsmBackend(T, Triple(TT).getOS());
456   }
457 }