10 bits, not 12.
[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 "llvm/Target/TargetAsmBackend.h"
11 #include "ARM.h"
12 #include "ARMFixupKinds.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCObjectFormat.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCSectionELF.h"
19 #include "llvm/MC/MCSectionMachO.h"
20 #include "llvm/Object/MachOFormat.h"
21 #include "llvm/Support/ELF.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Target/TargetRegistry.h"
25 using namespace llvm;
26
27 namespace {
28 class ARMAsmBackend : public TargetAsmBackend {
29 public:
30   ARMAsmBackend(const Target &T) : TargetAsmBackend() {}
31
32   bool MayNeedRelaxation(const MCInst &Inst) const;
33
34   void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
35
36   bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
37
38   unsigned getPointerSize() const {
39     return 4;
40   }
41
42 protected:
43   static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
44     switch (Kind) {
45     default:
46       llvm_unreachable("Unknown fixup kind!");
47     case FK_Data_4:
48     case ARM::fixup_arm_movt_hi16:
49     case ARM::fixup_arm_movw_lo16:
50       return Value;
51     case ARM::fixup_arm_pcrel_12: {
52       bool isAdd = true;
53       // ARM PC-relative values are offset by 8.
54       Value -= 8;
55       if ((int64_t)Value < 0) {
56         Value = -Value;
57         isAdd = false;
58       }
59       assert ((Value < 4096) && "Out of range pc-relative fixup value!");
60       Value |= isAdd << 23;
61       return Value;
62     }
63     case ARM::fixup_arm_branch:
64       // These values don't encode the low two bits since they're always zero.
65       // Offset by 8 just as above.
66       return (Value - 8) >> 2;
67     case ARM::fixup_arm_vfp_pcrel_10: {
68       // Offset by 8 just as above.
69       Value = Value - 8;
70       bool isAdd = true;
71       if ((int64_t)Value < 0) {
72         Value = -Value;
73         isAdd = false;
74       }
75       // These values don't encode the low two bits since they're always zero.
76       Value >>= 2;
77       assert ((Value < 256) && "Out of range pc-relative fixup value!");
78       Value |= isAdd << 23;
79       return Value;
80     }
81     }
82   }
83 };
84 } // end anonymous namespace
85
86 bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
87   // FIXME: Thumb targets, different move constant targets..
88   return false;
89 }
90
91 void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
92   assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
93   return;
94 }
95
96 bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
97   // FIXME: Zero fill for now. That's not right, but at least will get the
98   // section size right.
99   for (uint64_t i = 0; i != Count; ++i)
100     OW->Write8(0);
101   return true;
102 }
103
104 namespace {
105 // FIXME: This should be in a separate file.
106 // ELF is an ELF of course...
107 class ELFARMAsmBackend : public ARMAsmBackend {
108   MCELFObjectFormat Format;
109
110 public:
111   Triple::OSType OSType;
112   ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
113     : ARMAsmBackend(T), OSType(_OSType) {
114     HasScatteredSymbols = true;
115   }
116
117   virtual const MCObjectFormat &getObjectFormat() const {
118     return Format;
119   }
120
121   void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
122                   uint64_t Value) const;
123
124   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
125     return createELFObjectWriter(OS, /*Is64Bit=*/false,
126                                  OSType, ELF::EM_ARM,
127                                  /*IsLittleEndian=*/true,
128                                  /*HasRelocationAddend=*/false);
129   }
130 };
131
132 // Fixme: can we raise this to share code between Darwin and ELF?
133 void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
134                                   uint64_t Value) const {
135   uint32_t Mask = 0;
136   // Fixme: 2 for Thumb
137   unsigned NumBytes = 4; 
138   Value = adjustFixupValue(Fixup.getKind(), Value);
139
140   switch (Fixup.getKind()) {
141   default: assert(0 && "Unsupported Fixup kind"); break;
142   case ARM::fixup_arm_branch: {
143     unsigned Lo24 = Value & 0xFFFFFF;
144     Mask = ~(0xFFFFFF);
145     Value = Lo24;
146   }; break;
147   case ARM::fixup_arm_movt_hi16:
148   case ARM::fixup_arm_movw_lo16: {
149     unsigned Hi4 = (Value & 0xF000) >> 12;
150     unsigned Lo12 = Value & 0x0FFF;
151     // inst{19-16} = Hi4;
152     // inst{11-0} = Lo12;
153     Value = (Hi4 << 16) | (Lo12);
154     Mask = ~(0xF0FFF);
155   }; break;
156   }
157
158   assert((Fixup.getOffset() % NumBytes == 0)
159          && "Offset mod NumBytes is nonzero!");
160   // For each byte of the fragment that the fixup touches, mask in the
161   // bits from the fixup value.
162   // The Value has been "split up" into the appropriate bitfields above.
163   // Fixme: how to share code with the .td generated code?
164   for (unsigned i = 0; i != NumBytes; ++i) {
165     DF.getContents()[Fixup.getOffset() + i] &= uint8_t(Mask >> (i * 8));
166     DF.getContents()[Fixup.getOffset() + i] |= uint8_t(Value >> (i * 8));
167   }
168 }
169
170 namespace {
171 // FIXME: This should be in a separate file.
172 class DarwinARMAsmBackend : public ARMAsmBackend {
173   MCMachOObjectFormat Format;
174 public:
175   DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) {
176     HasScatteredSymbols = true;
177   }
178
179   virtual const MCObjectFormat &getObjectFormat() const {
180     return Format;
181   }
182
183   void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
184                   uint64_t Value) const;
185
186   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
187     // FIXME: Subtarget info should be derived. Force v7 for now.
188     return createMachObjectWriter(OS, /*Is64Bit=*/false,
189                                   object::mach::CTM_ARM,
190                                   object::mach::CSARM_V7,
191                                   /*IsLittleEndian=*/true);
192   }
193
194   virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
195     return false;
196   }
197 };
198 } // end anonymous namespace
199
200 static unsigned getFixupKindNumBytes(unsigned Kind) {
201   switch (Kind) {
202   default: llvm_unreachable("Unknown fixup kind!");
203   case FK_Data_4: return 4;
204   case ARM::fixup_arm_pcrel_12: return 3;
205   case ARM::fixup_arm_vfp_pcrel_10: return 3;
206   case ARM::fixup_arm_branch: return 3;
207   }
208 }
209
210 void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
211                                      uint64_t Value) const {
212   unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
213   Value = adjustFixupValue(Fixup.getKind(), Value);
214
215   assert(Fixup.getOffset() + NumBytes <= DF.getContents().size() &&
216          "Invalid fixup offset!");
217   // For each byte of the fragment that the fixup touches, mask in the
218   // bits from the fixup value.
219   for (unsigned i = 0; i != NumBytes; ++i)
220     DF.getContents()[Fixup.getOffset() + i] |= uint8_t(Value >> (i * 8));
221 }
222 } // end anonymous namespace
223
224 TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
225                                             const std::string &TT) {
226   switch (Triple(TT).getOS()) {
227   case Triple::Darwin:
228     return new DarwinARMAsmBackend(T);
229   case Triple::MinGW32:
230   case Triple::Cygwin:
231   case Triple::Win32:
232     assert(0 && "Windows not supported on ARM");
233   default:
234     return new ELFARMAsmBackend(T, Triple(TT).getOS());
235   }
236 }