Add support for loading from a constant pool.
[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() {}
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_arm_thumb_bl: {
131     // The value doesn't encode the low bit (always zero) and is offset by
132     // four. The value is encoded into disjoint bit positions in the destination
133     // opcode. x = unchanged, I = immediate value bit, S = sign extension bit
134     // xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
135     // Note that the halfwords are stored high first, low second; so we need
136     // to transpose the fixup value here to map properly.
137     uint32_t Binary = 0x3fffff & ((Value - 4) >> 1);
138     Binary = ((Binary & 0x7ff) << 16) | (Binary >> 11);
139     return Binary;
140   }
141   case ARM::fixup_arm_thumb_cp:
142     // Offset by 4, and don't encode the low two bits.
143     return ((Value - 4) >> 2) & 0xff;
144   case ARM::fixup_t2_pcrel_10:
145   case ARM::fixup_arm_pcrel_10: {
146     // Offset by 8 just as above.
147     Value = Value - 8;
148     bool isAdd = true;
149     if ((int64_t)Value < 0) {
150       Value = -Value;
151       isAdd = false;
152     }
153     // These values don't encode the low two bits since they're always zero.
154     Value >>= 2;
155     assert ((Value < 256) && "Out of range pc-relative fixup value!");
156     Value |= isAdd << 23;
157     
158     // Same addressing mode as fixup_arm_pcrel_10, but with the bytes reordered.
159     if (Kind == ARM::fixup_t2_pcrel_10) {
160       uint64_t swapped = (Value & 0xFFFF0000) >> 16;
161       swapped |= (Value & 0x0000FFFF) << 16;
162       return swapped;
163     }
164     
165     return Value;
166   }
167   }
168 }
169
170 namespace {
171
172 // FIXME: This should be in a separate file.
173 // ELF is an ELF of course...
174 class ELFARMAsmBackend : public ARMAsmBackend {
175   MCELFObjectFormat Format;
176
177 public:
178   Triple::OSType OSType;
179   ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
180     : ARMAsmBackend(T), OSType(_OSType) {
181     HasScatteredSymbols = true;
182   }
183
184   virtual const MCObjectFormat &getObjectFormat() const {
185     return Format;
186   }
187
188   void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
189                   uint64_t Value) const;
190
191   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
192     return createELFObjectWriter(OS, /*Is64Bit=*/false,
193                                  OSType, ELF::EM_ARM,
194                                  /*IsLittleEndian=*/true,
195                                  /*HasRelocationAddend=*/false);
196   }
197 };
198
199 // FIXME: Raise this to share code between Darwin and ELF.
200 void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
201                                   unsigned DataSize, uint64_t Value) const {
202   unsigned NumBytes = 4;        // FIXME: 2 for Thumb
203   Value = adjustFixupValue(Fixup.getKind(), Value);
204   if (!Value) return;           // Doesn't change encoding.
205
206   unsigned Offset = Fixup.getOffset();
207   assert(Offset % NumBytes == 0 && "Offset mod NumBytes is nonzero!");
208
209   // For each byte of the fragment that the fixup touches, mask in the bits from
210   // the fixup value. The Value has been "split up" into the appropriate
211   // bitfields above.
212   for (unsigned i = 0; i != NumBytes; ++i)
213     Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
214 }
215
216 // FIXME: This should be in a separate file.
217 class DarwinARMAsmBackend : public ARMAsmBackend {
218   MCMachOObjectFormat Format;
219 public:
220   DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) {
221     HasScatteredSymbols = true;
222   }
223
224   virtual const MCObjectFormat &getObjectFormat() const {
225     return Format;
226   }
227
228   void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
229                   uint64_t Value) const;
230
231   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
232     // FIXME: Subtarget info should be derived. Force v7 for now.
233     return createMachObjectWriter(OS, /*Is64Bit=*/false,
234                                   object::mach::CTM_ARM,
235                                   object::mach::CSARM_V7,
236                                   /*IsLittleEndian=*/true);
237   }
238
239   virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
240     return false;
241   }
242 };
243
244 /// getFixupKindNumBytes - The number of bytes the fixup may change.
245 static unsigned getFixupKindNumBytes(unsigned Kind) {
246   switch (Kind) {
247   default:
248     llvm_unreachable("Unknown fixup kind!");
249
250   case ARM::fixup_arm_thumb_cp:
251     return 1;
252
253   case ARM::fixup_arm_ldst_pcrel_12:
254   case ARM::fixup_arm_pcrel_10:
255   case ARM::fixup_arm_adr_pcrel_12:
256   case ARM::fixup_arm_branch:
257     return 3;
258
259   case FK_Data_4:
260   case ARM::fixup_t2_pcrel_10:
261   case ARM::fixup_arm_thumb_bl:
262     return 4;
263   }
264 }
265
266 void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
267                                      unsigned DataSize, uint64_t Value) const {
268   unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
269   Value = adjustFixupValue(Fixup.getKind(), Value);
270   if (!Value) return;           // Doesn't change encoding.
271
272   unsigned Offset = Fixup.getOffset();
273   assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
274
275   // For each byte of the fragment that the fixup touches, mask in the
276   // bits from the fixup value.
277   for (unsigned i = 0; i != NumBytes; ++i)
278     Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
279 }
280
281 } // end anonymous namespace
282
283 TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
284                                             const std::string &TT) {
285   switch (Triple(TT).getOS()) {
286   case Triple::Darwin:
287     return new DarwinARMAsmBackend(T);
288   case Triple::MinGW32:
289   case Triple::Cygwin:
290   case Triple::Win32:
291     assert(0 && "Windows not supported on ARM");
292   default:
293     return new ELFARMAsmBackend(T, Triple(TT).getOS());
294   }
295 }