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