Simple branch relaxation for Thumb2 Bcc instructions.
[oota-llvm.git] / lib / Target / ARM / MCTargetDesc / 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 "MCTargetDesc/ARMMCTargetDesc.h"
11 #include "MCTargetDesc/ARMBaseInfo.h"
12 #include "MCTargetDesc/ARMFixupKinds.h"
13 #include "MCTargetDesc/ARMAddressingModes.h"
14 #include "llvm/ADT/Twine.h"
15 #include "llvm/MC/MCAssembler.h"
16 #include "llvm/MC/MCDirectives.h"
17 #include "llvm/MC/MCELFObjectWriter.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCMachObjectWriter.h"
20 #include "llvm/MC/MCObjectWriter.h"
21 #include "llvm/MC/MCSectionELF.h"
22 #include "llvm/MC/MCSectionMachO.h"
23 #include "llvm/MC/MCAsmBackend.h"
24 #include "llvm/MC/MCSubtargetInfo.h"
25 #include "llvm/Object/MachOFormat.h"
26 #include "llvm/Support/ELF.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/raw_ostream.h"
29 using namespace llvm;
30
31 namespace {
32 class ARMELFObjectWriter : public MCELFObjectTargetWriter {
33 public:
34   ARMELFObjectWriter(Triple::OSType OSType)
35     : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSType, ELF::EM_ARM,
36                               /*HasRelocationAddend*/ false) {}
37 };
38
39 class ARMAsmBackend : public MCAsmBackend {
40   const MCSubtargetInfo* STI;
41   bool isThumbMode;  // Currently emitting Thumb code.
42 public:
43   ARMAsmBackend(const Target &T, const StringRef TT)
44     : MCAsmBackend(), STI(ARM_MC::createARMMCSubtargetInfo(TT, "", "")),
45       isThumbMode(TT.startswith("thumb")) {}
46
47   ~ARMAsmBackend() {
48     delete STI;
49   }
50
51   unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
52
53   bool hasNOP() const {
54     return (STI->getFeatureBits() & ARM::HasV6T2Ops) != 0;
55   }
56
57   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
58     const static MCFixupKindInfo Infos[ARM::NumTargetFixupKinds] = {
59 // This table *must* be in the order that the fixup_* kinds are defined in
60 // ARMFixupKinds.h.
61 //
62 // Name                      Offset (bits) Size (bits)     Flags
63 { "fixup_arm_ldst_pcrel_12", 0,            32,  MCFixupKindInfo::FKF_IsPCRel },
64 { "fixup_t2_ldst_pcrel_12",  0,            32,  MCFixupKindInfo::FKF_IsPCRel |
65                                    MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
66 { "fixup_arm_pcrel_10",      0,            32,  MCFixupKindInfo::FKF_IsPCRel },
67 { "fixup_t2_pcrel_10",       0,            32,  MCFixupKindInfo::FKF_IsPCRel |
68                                    MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
69 { "fixup_thumb_adr_pcrel_10",0,            8,   MCFixupKindInfo::FKF_IsPCRel |
70                                    MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
71 { "fixup_arm_adr_pcrel_12",  0,            32,  MCFixupKindInfo::FKF_IsPCRel },
72 { "fixup_t2_adr_pcrel_12",   0,            32,  MCFixupKindInfo::FKF_IsPCRel |
73                                    MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
74 { "fixup_arm_condbranch",    0,            24,  MCFixupKindInfo::FKF_IsPCRel },
75 { "fixup_arm_uncondbranch",  0,            24,  MCFixupKindInfo::FKF_IsPCRel },
76 { "fixup_t2_condbranch",     0,            32,  MCFixupKindInfo::FKF_IsPCRel },
77 { "fixup_t2_uncondbranch",   0,            32,  MCFixupKindInfo::FKF_IsPCRel },
78 { "fixup_arm_thumb_br",      0,            16,  MCFixupKindInfo::FKF_IsPCRel },
79 { "fixup_arm_thumb_bl",      0,            32,  MCFixupKindInfo::FKF_IsPCRel },
80 { "fixup_arm_thumb_blx",     0,            32,  MCFixupKindInfo::FKF_IsPCRel },
81 { "fixup_arm_thumb_cb",      0,            16,  MCFixupKindInfo::FKF_IsPCRel },
82 { "fixup_arm_thumb_cp",      0,             8,  MCFixupKindInfo::FKF_IsPCRel },
83 { "fixup_arm_thumb_bcc",     0,             8,  MCFixupKindInfo::FKF_IsPCRel },
84 // movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16 - 19.
85 { "fixup_arm_movt_hi16",     0,            20,  0 },
86 { "fixup_arm_movw_lo16",     0,            20,  0 },
87 { "fixup_t2_movt_hi16",      0,            20,  0 },
88 { "fixup_t2_movw_lo16",      0,            20,  0 },
89 { "fixup_arm_movt_hi16_pcrel", 0,          20,  MCFixupKindInfo::FKF_IsPCRel },
90 { "fixup_arm_movw_lo16_pcrel", 0,          20,  MCFixupKindInfo::FKF_IsPCRel },
91 { "fixup_t2_movt_hi16_pcrel", 0,           20,  MCFixupKindInfo::FKF_IsPCRel },
92 { "fixup_t2_movw_lo16_pcrel", 0,           20,  MCFixupKindInfo::FKF_IsPCRel },
93     };
94
95     if (Kind < FirstTargetFixupKind)
96       return MCAsmBackend::getFixupKindInfo(Kind);
97
98     assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
99            "Invalid kind!");
100     return Infos[Kind - FirstTargetFixupKind];
101   }
102
103   bool MayNeedRelaxation(const MCInst &Inst) const;
104
105   void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
106
107   bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
108
109   void HandleAssemblerFlag(MCAssemblerFlag Flag) {
110     switch (Flag) {
111     default: break;
112     case MCAF_Code16:
113       setIsThumb(true);
114       break;
115     case MCAF_Code32:
116       setIsThumb(false);
117       break;
118     }
119   }
120
121   unsigned getPointerSize() const { return 4; }
122   bool isThumb() const { return isThumbMode; }
123   void setIsThumb(bool it) { isThumbMode = it; }
124 };
125 } // end anonymous namespace
126
127 static unsigned getRelaxedOpcode(unsigned Op) {
128   switch (Op) {
129   default: return Op;
130   case ARM::tBcc: return ARM::t2Bcc;
131   }
132 }
133
134 bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
135   if (getRelaxedOpcode(Inst.getOpcode()) != Inst.getOpcode())
136     return true;
137   return false;
138 }
139
140 void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
141   unsigned RelaxedOp = getRelaxedOpcode(Inst.getOpcode());
142
143   // Sanity check w/ diagnostic if we get here w/ a bogus instruction.
144   if (RelaxedOp == Inst.getOpcode()) {
145     SmallString<256> Tmp;
146     raw_svector_ostream OS(Tmp);
147     Inst.dump_pretty(OS);
148     OS << "\n";
149     report_fatal_error("unexpected instruction to relax: " + OS.str());
150   }
151
152   // The instructions we're relaxing have (so far) the same operands.
153   // We just need to update to the proper opcode.
154   Res = Inst;
155   Res.setOpcode(RelaxedOp);
156 }
157
158 bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
159   const uint16_t Thumb1_16bitNopEncoding = 0x46c0; // using MOV r8,r8
160   const uint16_t Thumb2_16bitNopEncoding = 0xbf00; // NOP
161   const uint32_t ARMv4_NopEncoding = 0xe1a0000; // using MOV r0,r0
162   const uint32_t ARMv6T2_NopEncoding = 0xe320f000; // NOP
163   if (isThumb()) {
164     const uint16_t nopEncoding = hasNOP() ? Thumb2_16bitNopEncoding
165                                           : Thumb1_16bitNopEncoding;
166     uint64_t NumNops = Count / 2;
167     for (uint64_t i = 0; i != NumNops; ++i)
168       OW->Write16(nopEncoding);
169     if (Count & 1)
170       OW->Write8(0);
171     return true;
172   }
173   // ARM mode
174   const uint32_t nopEncoding = hasNOP() ? ARMv6T2_NopEncoding
175                                         : ARMv4_NopEncoding;
176   uint64_t NumNops = Count / 4;
177   for (uint64_t i = 0; i != NumNops; ++i)
178     OW->Write32(nopEncoding);
179   // FIXME: should this function return false when unable to write exactly
180   // 'Count' bytes with NOP encodings?
181   switch (Count % 4) {
182   default: break; // No leftover bytes to write
183   case 1: OW->Write8(0); break;
184   case 2: OW->Write16(0); break;
185   case 3: OW->Write16(0); OW->Write8(0xa0); break;
186   }
187
188   return true;
189 }
190
191 static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
192   switch (Kind) {
193   default:
194     llvm_unreachable("Unknown fixup kind!");
195   case FK_Data_1:
196   case FK_Data_2:
197   case FK_Data_4:
198     return Value;
199   case ARM::fixup_arm_movt_hi16:
200     Value >>= 16;
201     // Fallthrough
202   case ARM::fixup_arm_movw_lo16:
203   case ARM::fixup_arm_movt_hi16_pcrel:
204   case ARM::fixup_arm_movw_lo16_pcrel: {
205     unsigned Hi4 = (Value & 0xF000) >> 12;
206     unsigned Lo12 = Value & 0x0FFF;
207     // inst{19-16} = Hi4;
208     // inst{11-0} = Lo12;
209     Value = (Hi4 << 16) | (Lo12);
210     return Value;
211   }
212   case ARM::fixup_t2_movt_hi16:
213     Value >>= 16;
214     // Fallthrough
215   case ARM::fixup_t2_movw_lo16:
216   case ARM::fixup_t2_movt_hi16_pcrel:  //FIXME: Shouldn't this be shifted like
217                                        // the other hi16 fixup?
218   case ARM::fixup_t2_movw_lo16_pcrel: {
219     unsigned Hi4 = (Value & 0xF000) >> 12;
220     unsigned i = (Value & 0x800) >> 11;
221     unsigned Mid3 = (Value & 0x700) >> 8;
222     unsigned Lo8 = Value & 0x0FF;
223     // inst{19-16} = Hi4;
224     // inst{26} = i;
225     // inst{14-12} = Mid3;
226     // inst{7-0} = Lo8;
227     Value = (Hi4 << 16) | (i << 26) | (Mid3 << 12) | (Lo8);
228     uint64_t swapped = (Value & 0xFFFF0000) >> 16;
229     swapped |= (Value & 0x0000FFFF) << 16;
230     return swapped;
231   }
232   case ARM::fixup_arm_ldst_pcrel_12:
233     // ARM PC-relative values are offset by 8.
234     Value -= 4;
235     // FALLTHROUGH
236   case ARM::fixup_t2_ldst_pcrel_12: {
237     // Offset by 4, adjusted by two due to the half-word ordering of thumb.
238     Value -= 4;
239     bool isAdd = true;
240     if ((int64_t)Value < 0) {
241       Value = -Value;
242       isAdd = false;
243     }
244     assert ((Value < 4096) && "Out of range pc-relative fixup value!");
245     Value |= isAdd << 23;
246
247     // Same addressing mode as fixup_arm_pcrel_10,
248     // but with 16-bit halfwords swapped.
249     if (Kind == ARM::fixup_t2_ldst_pcrel_12) {
250       uint64_t swapped = (Value & 0xFFFF0000) >> 16;
251       swapped |= (Value & 0x0000FFFF) << 16;
252       return swapped;
253     }
254
255     return Value;
256   }
257   case ARM::fixup_thumb_adr_pcrel_10:
258     return ((Value - 4) >> 2) & 0xff;
259   case ARM::fixup_arm_adr_pcrel_12: {
260     // ARM PC-relative values are offset by 8.
261     Value -= 8;
262     unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
263     if ((int64_t)Value < 0) {
264       Value = -Value;
265       opc = 2; // 0b0010
266     }
267     assert(ARM_AM::getSOImmVal(Value) != -1 &&
268            "Out of range pc-relative fixup value!");
269     // Encode the immediate and shift the opcode into place.
270     return ARM_AM::getSOImmVal(Value) | (opc << 21);
271   }
272
273   case ARM::fixup_t2_adr_pcrel_12: {
274     Value -= 4;
275     unsigned opc = 0;
276     if ((int64_t)Value < 0) {
277       Value = -Value;
278       opc = 5;
279     }
280
281     uint32_t out = (opc << 21);
282     out |= (Value & 0x800) << 15;
283     out |= (Value & 0x700) << 4;
284     out |= (Value & 0x0FF);
285
286     uint64_t swapped = (out & 0xFFFF0000) >> 16;
287     swapped |= (out & 0x0000FFFF) << 16;
288     return swapped;
289   }
290
291   case ARM::fixup_arm_condbranch:
292   case ARM::fixup_arm_uncondbranch:
293     // These values don't encode the low two bits since they're always zero.
294     // Offset by 8 just as above.
295     return 0xffffff & ((Value - 8) >> 2);
296   case ARM::fixup_t2_uncondbranch: {
297     Value = Value - 4;
298     Value >>= 1; // Low bit is not encoded.
299
300     uint32_t out = 0;
301     bool I =  Value & 0x800000;
302     bool J1 = Value & 0x400000;
303     bool J2 = Value & 0x200000;
304     J1 ^= I;
305     J2 ^= I;
306
307     out |= I  << 26; // S bit
308     out |= !J1 << 13; // J1 bit
309     out |= !J2 << 11; // J2 bit
310     out |= (Value & 0x1FF800)  << 5; // imm6 field
311     out |= (Value & 0x0007FF);        // imm11 field
312
313     uint64_t swapped = (out & 0xFFFF0000) >> 16;
314     swapped |= (out & 0x0000FFFF) << 16;
315     return swapped;
316   }
317   case ARM::fixup_t2_condbranch: {
318     Value = Value - 4;
319     Value >>= 1; // Low bit is not encoded.
320
321     uint64_t out = 0;
322     out |= (Value & 0x80000) << 7; // S bit
323     out |= (Value & 0x40000) >> 7; // J2 bit
324     out |= (Value & 0x20000) >> 4; // J1 bit
325     out |= (Value & 0x1F800) << 5; // imm6 field
326     out |= (Value & 0x007FF);      // imm11 field
327
328     uint32_t swapped = (out & 0xFFFF0000) >> 16;
329     swapped |= (out & 0x0000FFFF) << 16;
330     return swapped;
331   }
332   case ARM::fixup_arm_thumb_bl: {
333     // The value doesn't encode the low bit (always zero) and is offset by
334     // four. The value is encoded into disjoint bit positions in the destination
335     // opcode. x = unchanged, I = immediate value bit, S = sign extension bit
336     //
337     //   BL:  xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
338     //
339     // Note that the halfwords are stored high first, low second; so we need
340     // to transpose the fixup value here to map properly.
341     unsigned isNeg = (int64_t(Value - 4) < 0) ? 1 : 0;
342     uint32_t Binary = 0;
343     Value = 0x3fffff & ((Value - 4) >> 1);
344     Binary  = (Value & 0x7ff) << 16;    // Low imm11 value.
345     Binary |= (Value & 0x1ffc00) >> 11; // High imm10 value.
346     Binary |= isNeg << 10;              // Sign bit.
347     return Binary;
348   }
349   case ARM::fixup_arm_thumb_blx: {
350     // The value doesn't encode the low two bits (always zero) and is offset by
351     // four (see fixup_arm_thumb_cp). The value is encoded into disjoint bit
352     // positions in the destination opcode. x = unchanged, I = immediate value
353     // bit, S = sign extension bit, 0 = zero.
354     //
355     //   BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0
356     //
357     // Note that the halfwords are stored high first, low second; so we need
358     // to transpose the fixup value here to map properly.
359     unsigned isNeg = (int64_t(Value-4) < 0) ? 1 : 0;
360     uint32_t Binary = 0;
361     Value = 0xfffff & ((Value - 2) >> 2);
362     Binary  = (Value & 0x3ff) << 17;    // Low imm10L value.
363     Binary |= (Value & 0xffc00) >> 10;  // High imm10H value.
364     Binary |= isNeg << 10;              // Sign bit.
365     return Binary;
366   }
367   case ARM::fixup_arm_thumb_cp:
368     // Offset by 4, and don't encode the low two bits. Two bytes of that
369     // 'off by 4' is implicitly handled by the half-word ordering of the
370     // Thumb encoding, so we only need to adjust by 2 here.
371     return ((Value - 2) >> 2) & 0xff;
372   case ARM::fixup_arm_thumb_cb: {
373     // Offset by 4 and don't encode the lower bit, which is always 0.
374     uint32_t Binary = (Value - 4) >> 1;
375     return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
376   }
377   case ARM::fixup_arm_thumb_br:
378     // Offset by 4 and don't encode the lower bit, which is always 0.
379     return ((Value - 4) >> 1) & 0x7ff;
380   case ARM::fixup_arm_thumb_bcc:
381     // Offset by 4 and don't encode the lower bit, which is always 0.
382     return ((Value - 4) >> 1) & 0xff;
383   case ARM::fixup_arm_pcrel_10:
384     Value = Value - 4; // ARM fixups offset by an additional word and don't
385                        // need to adjust for the half-word ordering.
386     // Fall through.
387   case ARM::fixup_t2_pcrel_10: {
388     // Offset by 4, adjusted by two due to the half-word ordering of thumb.
389     Value = Value - 4;
390     bool isAdd = true;
391     if ((int64_t)Value < 0) {
392       Value = -Value;
393       isAdd = false;
394     }
395     // These values don't encode the low two bits since they're always zero.
396     Value >>= 2;
397     assert ((Value < 256) && "Out of range pc-relative fixup value!");
398     Value |= isAdd << 23;
399
400     // Same addressing mode as fixup_arm_pcrel_10,
401     // but with 16-bit halfwords swapped.
402     if (Kind == ARM::fixup_t2_pcrel_10) {
403       uint32_t swapped = (Value & 0xFFFF0000) >> 16;
404       swapped |= (Value & 0x0000FFFF) << 16;
405       return swapped;
406     }
407
408     return Value;
409   }
410   }
411 }
412
413 namespace {
414
415 // FIXME: This should be in a separate file.
416 // ELF is an ELF of course...
417 class ELFARMAsmBackend : public ARMAsmBackend {
418 public:
419   Triple::OSType OSType;
420   ELFARMAsmBackend(const Target &T, const StringRef TT,
421                    Triple::OSType _OSType)
422     : ARMAsmBackend(T, TT), OSType(_OSType) { }
423
424   void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
425                   uint64_t Value) const;
426
427   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
428     return createELFObjectWriter(new ARMELFObjectWriter(OSType), OS,
429                               /*IsLittleEndian*/ true);
430   }
431 };
432
433 // FIXME: Raise this to share code between Darwin and ELF.
434 void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
435                                   unsigned DataSize, uint64_t Value) const {
436   unsigned NumBytes = 4;        // FIXME: 2 for Thumb
437   Value = adjustFixupValue(Fixup.getKind(), Value);
438   if (!Value) return;           // Doesn't change encoding.
439
440   unsigned Offset = Fixup.getOffset();
441
442   // For each byte of the fragment that the fixup touches, mask in the bits from
443   // the fixup value. The Value has been "split up" into the appropriate
444   // bitfields above.
445   for (unsigned i = 0; i != NumBytes; ++i)
446     Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
447 }
448
449 // FIXME: This should be in a separate file.
450 class DarwinARMAsmBackend : public ARMAsmBackend {
451 public:
452   const object::mach::CPUSubtypeARM Subtype;
453   DarwinARMAsmBackend(const Target &T, const StringRef TT,
454                       object::mach::CPUSubtypeARM st)
455     : ARMAsmBackend(T, TT), Subtype(st) { }
456
457   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
458     return createARMMachObjectWriter(OS, /*Is64Bit=*/false,
459                                      object::mach::CTM_ARM,
460                                      Subtype);
461   }
462
463   void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
464                   uint64_t Value) const;
465
466   virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
467     return false;
468   }
469 };
470
471 /// getFixupKindNumBytes - The number of bytes the fixup may change.
472 static unsigned getFixupKindNumBytes(unsigned Kind) {
473   switch (Kind) {
474   default:
475     llvm_unreachable("Unknown fixup kind!");
476
477   case FK_Data_1:
478   case ARM::fixup_arm_thumb_bcc:
479   case ARM::fixup_arm_thumb_cp:
480   case ARM::fixup_thumb_adr_pcrel_10:
481     return 1;
482
483   case FK_Data_2:
484   case ARM::fixup_arm_thumb_br:
485   case ARM::fixup_arm_thumb_cb:
486     return 2;
487
488   case ARM::fixup_arm_ldst_pcrel_12:
489   case ARM::fixup_arm_pcrel_10:
490   case ARM::fixup_arm_adr_pcrel_12:
491   case ARM::fixup_arm_condbranch:
492   case ARM::fixup_arm_uncondbranch:
493     return 3;
494
495   case FK_Data_4:
496   case ARM::fixup_t2_ldst_pcrel_12:
497   case ARM::fixup_t2_condbranch:
498   case ARM::fixup_t2_uncondbranch:
499   case ARM::fixup_t2_pcrel_10:
500   case ARM::fixup_t2_adr_pcrel_12:
501   case ARM::fixup_arm_thumb_bl:
502   case ARM::fixup_arm_thumb_blx:
503   case ARM::fixup_arm_movt_hi16:
504   case ARM::fixup_arm_movw_lo16:
505   case ARM::fixup_arm_movt_hi16_pcrel:
506   case ARM::fixup_arm_movw_lo16_pcrel:
507   case ARM::fixup_t2_movt_hi16:
508   case ARM::fixup_t2_movw_lo16:
509   case ARM::fixup_t2_movt_hi16_pcrel:
510   case ARM::fixup_t2_movw_lo16_pcrel:
511     return 4;
512   }
513 }
514
515 void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
516                                      unsigned DataSize, uint64_t Value) const {
517   unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
518   Value = adjustFixupValue(Fixup.getKind(), Value);
519   if (!Value) return;           // Doesn't change encoding.
520
521   unsigned Offset = Fixup.getOffset();
522   assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
523
524   // For each byte of the fragment that the fixup touches, mask in the
525   // bits from the fixup value.
526   for (unsigned i = 0; i != NumBytes; ++i)
527     Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
528 }
529
530 } // end anonymous namespace
531
532 MCAsmBackend *llvm::createARMAsmBackend(const Target &T, StringRef TT) {
533   Triple TheTriple(TT);
534
535   if (TheTriple.isOSDarwin()) {
536     if (TheTriple.getArchName() == "armv4t" ||
537         TheTriple.getArchName() == "thumbv4t")
538       return new DarwinARMAsmBackend(T, TT, object::mach::CSARM_V4T);
539     else if (TheTriple.getArchName() == "armv5e" ||
540         TheTriple.getArchName() == "thumbv5e")
541       return new DarwinARMAsmBackend(T, TT, object::mach::CSARM_V5TEJ);
542     else if (TheTriple.getArchName() == "armv6" ||
543         TheTriple.getArchName() == "thumbv6")
544       return new DarwinARMAsmBackend(T, TT, object::mach::CSARM_V6);
545     return new DarwinARMAsmBackend(T, TT, object::mach::CSARM_V7);
546   }
547
548   if (TheTriple.isOSWindows())
549     assert(0 && "Windows not supported on ARM");
550
551   return new ELFARMAsmBackend(T, TT, Triple(TT).getOS());
552 }