Fix typo in Thumb2 branch fixup.
[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   bool MayNeedRelaxation(const MCInst &Inst) const;
36
37   void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
38
39   bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
40
41   void HandleAssemblerFlag(MCAssemblerFlag Flag) {
42     switch (Flag) {
43     default: break;
44     case MCAF_Code16:
45       setIsThumb(true);
46       break;
47     case MCAF_Code32:
48       setIsThumb(false);
49       break;
50     }
51   }
52
53   unsigned getPointerSize() const { return 4; }
54   bool isThumb() const { return isThumbMode; }
55   void setIsThumb(bool it) { isThumbMode = it; }
56 };
57 } // end anonymous namespace
58
59 bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
60   // FIXME: Thumb targets, different move constant targets..
61   return false;
62 }
63
64 void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
65   assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
66   return;
67 }
68
69 bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
70   if (isThumb()) {
71     assert (((Count & 1) == 0) && "Unaligned Nop data fragment!");
72     // FIXME: 0xbf00 is the ARMv7 value. For v6 and before, we'll need to
73     // use 0x46c0 (which is a 'mov r8, r8' insn).
74     Count /= 2;
75     for (uint64_t i = 0; i != Count; ++i)
76       OW->Write16(0xbf00);
77     return true;
78   }
79   // ARM mode
80   Count /= 4;
81   for (uint64_t i = 0; i != Count; ++i)
82     OW->Write32(0xe1a00000);
83   return true;
84 }
85
86 static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
87   switch (Kind) {
88   default:
89     llvm_unreachable("Unknown fixup kind!");
90   case FK_Data_4:
91     return Value;
92   case ARM::fixup_arm_movt_hi16:
93   case ARM::fixup_arm_movw_lo16: {
94     unsigned Hi4 = (Value & 0xF000) >> 12;
95     unsigned Lo12 = Value & 0x0FFF;
96     // inst{19-16} = Hi4;
97     // inst{11-0} = Lo12;
98     Value = (Hi4 << 16) | (Lo12);
99     return Value;
100   }
101   case ARM::fixup_arm_ldst_pcrel_12: {
102     bool isAdd = true;
103     // ARM PC-relative values are offset by 8.
104     Value -= 8;
105     if ((int64_t)Value < 0) {
106       Value = -Value;
107       isAdd = false;
108     }
109     assert ((Value < 4096) && "Out of range pc-relative fixup value!");
110     Value |= isAdd << 23;
111     return Value;
112   }
113   case ARM::fixup_arm_adr_pcrel_12: {
114     // ARM PC-relative values are offset by 8.
115     Value -= 8;
116     unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
117     if ((int64_t)Value < 0) {
118       Value = -Value;
119       opc = 2; // 0b0010
120     }
121     assert(ARM_AM::getSOImmVal(Value) != -1 &&
122            "Out of range pc-relative fixup value!");
123     // Encode the immediate and shift the opcode into place.
124     return ARM_AM::getSOImmVal(Value) | (opc << 21);
125   }
126   case ARM::fixup_arm_branch:
127     // These values don't encode the low two bits since they're always zero.
128     // Offset by 8 just as above.
129     return 0xffffff & ((Value - 8) >> 2);
130   case ARM::fixup_t2_branch: {
131     Value = Value - 6;
132     Value >>= 1; // Low bit is not encoded.
133     
134     uint64_t out = 0;
135     out |= (Value & 0x80000) << 7; // S bit
136     out |= (Value & 0x40000) >> 7; // J2 bit
137     out |= (Value & 0x20000) >> 4; // J1 bit
138     out |= (Value & 0x1F800) << 5; // imm6 field
139     out |= (Value & 0x007FF);      // imm11 field
140     
141     uint64_t swapped = (out & 0xFFFF0000) >> 16;
142     swapped |= (out & 0x0000FFFF) << 16;
143     return swapped;
144   }
145   case ARM::fixup_arm_thumb_bl: {
146     // The value doesn't encode the low bit (always zero) and is offset by
147     // four. The value is encoded into disjoint bit positions in the destination
148     // opcode. x = unchanged, I = immediate value bit, S = sign extension bit
149     // 
150     //   BL:  xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
151     // 
152     // Note that the halfwords are stored high first, low second; so we need
153     // to transpose the fixup value here to map properly.
154     unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
155     uint32_t Binary = 0;
156     Value = 0x3fffff & ((Value - 4) >> 1);
157     Binary  = (Value & 0x7ff) << 16;    // Low imm11 value.
158     Binary |= (Value & 0x1ffc00) >> 11; // High imm10 value.
159     Binary |= isNeg << 10;              // Sign bit.
160     return Binary;
161   }
162   case ARM::fixup_arm_thumb_blx: {
163     // The value doesn't encode the low two bits (always zero) and is offset by
164     // four (see fixup_arm_thumb_cp). The value is encoded into disjoint bit
165     // positions in the destination opcode. x = unchanged, I = immediate value
166     // bit, S = sign extension bit, 0 = zero.
167     // 
168     //   BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0
169     // 
170     // Note that the halfwords are stored high first, low second; so we need
171     // to transpose the fixup value here to map properly.
172     unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
173     uint32_t Binary = 0;
174     Value = 0xfffff & ((Value - 2) >> 2);
175     Binary  = (Value & 0x3ff) << 17;    // Low imm10L value.
176     Binary |= (Value & 0xffc00) >> 10;  // High imm10H value.
177     Binary |= isNeg << 10;              // Sign bit.
178     return Binary;
179   }
180   case ARM::fixup_arm_thumb_cp:
181     // Offset by 4, and don't encode the low two bits. Two bytes of that
182     // 'off by 4' is implicitly handled by the half-word ordering of the
183     // Thumb encoding, so we only need to adjust by 2 here.
184     return ((Value - 2) >> 2) & 0xff;
185   case ARM::fixup_arm_thumb_br: {
186     // Offset by 4 and don't encode the lower bit, which is always 0.
187     uint32_t Binary = (Value - 4) >> 1;
188     return ((Binary & 0x20) << 9) | ((Binary & 0x1f) << 3);
189   }
190   case ARM::fixup_arm_pcrel_10:
191     Value = Value - 6; // ARM fixups offset by an additional word and don't
192                        // need to adjust for the half-word ordering.
193     // Fall through.
194   case ARM::fixup_t2_pcrel_10: {
195     // Offset by 4, adjusted by two due to the half-word ordering of thumb.
196     Value = Value - 2;
197     bool isAdd = true;
198     if ((int64_t)Value < 0) {
199       Value = -Value;
200       isAdd = false;
201     }
202     // These values don't encode the low two bits since they're always zero.
203     Value >>= 2;
204     assert ((Value < 256) && "Out of range pc-relative fixup value!");
205     Value |= isAdd << 23;
206
207     // Same addressing mode as fixup_arm_pcrel_10,
208     // but with 16-bit halfwords swapped.
209     if (Kind == ARM::fixup_t2_pcrel_10) {
210       uint64_t swapped = (Value & 0xFFFF0000) >> 16;
211       swapped |= (Value & 0x0000FFFF) << 16;
212       return swapped;
213     }
214
215     return Value;
216   }
217   }
218 }
219
220 namespace {
221
222 // FIXME: This should be in a separate file.
223 // ELF is an ELF of course...
224 class ELFARMAsmBackend : public ARMAsmBackend {
225   MCELFObjectFormat Format;
226
227 public:
228   Triple::OSType OSType;
229   ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
230     : ARMAsmBackend(T), OSType(_OSType) {
231     HasScatteredSymbols = true;
232   }
233
234   virtual const MCObjectFormat &getObjectFormat() const {
235     return Format;
236   }
237
238   void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
239                   uint64_t Value) const;
240
241   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
242     return createELFObjectWriter(OS, /*Is64Bit=*/false,
243                                  OSType, ELF::EM_ARM,
244                                  /*IsLittleEndian=*/true,
245                                  /*HasRelocationAddend=*/false);
246   }
247 };
248
249 // FIXME: Raise this to share code between Darwin and ELF.
250 void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
251                                   unsigned DataSize, uint64_t Value) const {
252   unsigned NumBytes = 4;        // FIXME: 2 for Thumb
253   Value = adjustFixupValue(Fixup.getKind(), Value);
254   if (!Value) return;           // Doesn't change encoding.
255
256   unsigned Offset = Fixup.getOffset();
257   assert(Offset % NumBytes == 0 && "Offset mod NumBytes is nonzero!");
258
259   // For each byte of the fragment that the fixup touches, mask in the bits from
260   // the fixup value. The Value has been "split up" into the appropriate
261   // bitfields above.
262   for (unsigned i = 0; i != NumBytes; ++i)
263     Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
264 }
265
266 // FIXME: This should be in a separate file.
267 class DarwinARMAsmBackend : public ARMAsmBackend {
268   MCMachOObjectFormat Format;
269 public:
270   DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) {
271     HasScatteredSymbols = true;
272   }
273
274   virtual const MCObjectFormat &getObjectFormat() const {
275     return Format;
276   }
277
278   void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
279                   uint64_t Value) const;
280
281   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
282     // FIXME: Subtarget info should be derived. Force v7 for now.
283     return createMachObjectWriter(OS, /*Is64Bit=*/false,
284                                   object::mach::CTM_ARM,
285                                   object::mach::CSARM_V7,
286                                   /*IsLittleEndian=*/true);
287   }
288
289   virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
290     return false;
291   }
292 };
293
294 /// getFixupKindNumBytes - The number of bytes the fixup may change.
295 static unsigned getFixupKindNumBytes(unsigned Kind) {
296   switch (Kind) {
297   default:
298     llvm_unreachable("Unknown fixup kind!");
299
300   case ARM::fixup_arm_thumb_cp:
301     return 1;
302
303   case ARM::fixup_arm_thumb_br:
304     return 2;
305
306   case ARM::fixup_arm_ldst_pcrel_12:
307   case ARM::fixup_arm_pcrel_10:
308   case ARM::fixup_arm_adr_pcrel_12:
309   case ARM::fixup_arm_branch:
310     return 3;
311
312   case FK_Data_4:
313   case ARM::fixup_t2_branch:
314   case ARM::fixup_t2_pcrel_10:
315   case ARM::fixup_arm_thumb_bl:
316   case ARM::fixup_arm_thumb_blx:
317     return 4;
318   }
319 }
320
321 void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
322                                      unsigned DataSize, uint64_t Value) const {
323   unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
324   Value = adjustFixupValue(Fixup.getKind(), Value);
325   if (!Value) return;           // Doesn't change encoding.
326
327   unsigned Offset = Fixup.getOffset();
328   assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
329
330   // For each byte of the fragment that the fixup touches, mask in the
331   // bits from the fixup value.
332   for (unsigned i = 0; i != NumBytes; ++i)
333     Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
334 }
335
336 } // end anonymous namespace
337
338 TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
339                                             const std::string &TT) {
340   switch (Triple(TT).getOS()) {
341   case Triple::Darwin:
342     return new DarwinARMAsmBackend(T);
343   case Triple::MinGW32:
344   case Triple::Cygwin:
345   case Triple::Win32:
346     assert(0 && "Windows not supported on ARM");
347   default:
348     return new ELFARMAsmBackend(T, Triple(TT).getOS());
349   }
350 }