Give explicit suffix to integer constant over 32-bits.
[oota-llvm.git] / lib / Target / AArch64 / MCTargetDesc / AArch64AsmBackend.cpp
1 //===-- AArch64AsmBackend.cpp - AArch64 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/AArch64FixupKinds.h"
11 #include "MCTargetDesc/AArch64MCTargetDesc.h"
12 #include "llvm/MC/MCAsmBackend.h"
13 #include "llvm/MC/MCSubtargetInfo.h"
14 #include "llvm/MC/MCELFObjectWriter.h"
15 #include "llvm/MC/MCFixupKindInfo.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/Support/ELF.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/raw_ostream.h"
20 using namespace llvm;
21
22 namespace {
23 class AArch64AsmBackend : public MCAsmBackend {
24   const MCSubtargetInfo* STI;
25 public:
26   AArch64AsmBackend(const Target &T, const StringRef TT)
27     : MCAsmBackend(),
28       STI(AArch64_MC::createAArch64MCSubtargetInfo(TT, "", ""))
29     {}
30
31
32   ~AArch64AsmBackend() {
33     delete STI;
34   }
35
36   bool writeNopData(uint64_t Count, MCObjectWriter *OW) const;
37
38   virtual void processFixupValue(const MCAssembler &Asm,
39                                  const MCAsmLayout &Layout,
40                                  const MCFixup &Fixup, const MCFragment *DF,
41                                  MCValue &Target, uint64_t &Value,
42                                  bool &IsResolved);
43 };
44 } // end anonymous namespace
45
46 void AArch64AsmBackend::processFixupValue(const MCAssembler &Asm,
47                                           const MCAsmLayout &Layout,
48                                           const MCFixup &Fixup,
49                                           const MCFragment *DF,
50                                           MCValue &Target, uint64_t &Value,
51                                           bool &IsResolved) {
52   // The ADRP instruction adds some multiple of 0x1000 to the current PC &
53   // ~0xfff. This means that the required offset to reach a symbol can vary by
54   // up to one step depending on where the ADRP is in memory. For example:
55   //
56   //     ADRP x0, there
57   //  there:
58   //
59   // If the ADRP occurs at address 0xffc then "there" will be at 0x1000 and
60   // we'll need that as an offset. At any other address "there" will be in the
61   // same page as the ADRP and the instruction should encode 0x0. Assuming the
62   // section isn't 0x1000-aligned, we therefore need to delegate this decision
63   // to the linker -- a relocation!
64   if ((uint32_t)Fixup.getKind() == AArch64::fixup_a64_adr_prel_page ||
65       (uint32_t)Fixup.getKind() == AArch64::fixup_a64_adr_prel_got_page ||
66       (uint32_t)Fixup.getKind() == AArch64::fixup_a64_adr_gottprel_page ||
67       (uint32_t)Fixup.getKind() == AArch64::fixup_a64_tlsdesc_adr_page)
68     IsResolved = false;
69 }
70
71
72 static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value);
73
74 namespace {
75
76 class ELFAArch64AsmBackend : public AArch64AsmBackend {
77 public:
78   uint8_t OSABI;
79   ELFAArch64AsmBackend(const Target &T, const StringRef TT,
80                        uint8_t _OSABI)
81     : AArch64AsmBackend(T, TT), OSABI(_OSABI) { }
82
83   bool fixupNeedsRelaxation(const MCFixup &Fixup,
84                             uint64_t Value,
85                             const MCRelaxableFragment *DF,
86                             const MCAsmLayout &Layout) const;
87
88   unsigned int getNumFixupKinds() const {
89     return AArch64::NumTargetFixupKinds;
90   }
91
92   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
93     const static MCFixupKindInfo Infos[AArch64::NumTargetFixupKinds] = {
94 // This table *must* be in the order that the fixup_* kinds are defined in
95 // AArch64FixupKinds.h.
96 //
97 // Name                         Offset (bits)    Size (bits)      Flags
98       { "fixup_a64_ld_prel",               0,    32, MCFixupKindInfo::FKF_IsPCRel },
99       { "fixup_a64_adr_prel",              0,    32, MCFixupKindInfo::FKF_IsPCRel },
100       { "fixup_a64_adr_prel_page",         0,    32, MCFixupKindInfo::FKF_IsPCRel },
101       { "fixup_a64_add_lo12",              0,    32,             0 },
102       { "fixup_a64_ldst8_lo12",            0,    32,             0 },
103       { "fixup_a64_ldst16_lo12",           0,    32,             0 },
104       { "fixup_a64_ldst32_lo12",           0,    32,             0 },
105       { "fixup_a64_ldst64_lo12",           0,    32,             0 },
106       { "fixup_a64_ldst128_lo12",          0,    32,             0 },
107       { "fixup_a64_tstbr",                 0,    32, MCFixupKindInfo::FKF_IsPCRel },
108       { "fixup_a64_condbr",                0,    32, MCFixupKindInfo::FKF_IsPCRel },
109       { "fixup_a64_uncondbr",              0,    32, MCFixupKindInfo::FKF_IsPCRel },
110       { "fixup_a64_call",                  0,    32, MCFixupKindInfo::FKF_IsPCRel },
111       { "fixup_a64_movw_uabs_g0",          0,    32,             0 },
112       { "fixup_a64_movw_uabs_g0_nc",       0,    32,             0 },
113       { "fixup_a64_movw_uabs_g1",          0,    32,             0 },
114       { "fixup_a64_movw_uabs_g1_nc",       0,    32,             0 },
115       { "fixup_a64_movw_uabs_g2",          0,    32,             0 },
116       { "fixup_a64_movw_uabs_g2_nc",       0,    32,             0 },
117       { "fixup_a64_movw_uabs_g3",          0,    32,             0 },
118       { "fixup_a64_movw_sabs_g0",          0,    32,             0 },
119       { "fixup_a64_movw_sabs_g1",          0,    32,             0 },
120       { "fixup_a64_movw_sabs_g2",          0,    32,             0 },
121       { "fixup_a64_adr_prel_got_page",     0,    32, MCFixupKindInfo::FKF_IsPCRel },
122       { "fixup_a64_ld64_got_lo12_nc",      0,    32,             0 },
123       { "fixup_a64_movw_dtprel_g2",        0,    32,             0 },
124       { "fixup_a64_movw_dtprel_g1",        0,    32,             0 },
125       { "fixup_a64_movw_dtprel_g1_nc",     0,    32,             0 },
126       { "fixup_a64_movw_dtprel_g0",        0,    32,             0 },
127       { "fixup_a64_movw_dtprel_g0_nc",     0,    32,             0 },
128       { "fixup_a64_add_dtprel_hi12",       0,    32,             0 },
129       { "fixup_a64_add_dtprel_lo12",       0,    32,             0 },
130       { "fixup_a64_add_dtprel_lo12_nc",    0,    32,             0 },
131       { "fixup_a64_ldst8_dtprel_lo12",     0,    32,             0 },
132       { "fixup_a64_ldst8_dtprel_lo12_nc",  0,    32,             0 },
133       { "fixup_a64_ldst16_dtprel_lo12",    0,    32,             0 },
134       { "fixup_a64_ldst16_dtprel_lo12_nc", 0,    32,             0 },
135       { "fixup_a64_ldst32_dtprel_lo12",    0,    32,             0 },
136       { "fixup_a64_ldst32_dtprel_lo12_nc", 0,    32,             0 },
137       { "fixup_a64_ldst64_dtprel_lo12",    0,    32,             0 },
138       { "fixup_a64_ldst64_dtprel_lo12_nc", 0,    32,             0 },
139       { "fixup_a64_movw_gottprel_g1",      0,    32,             0 },
140       { "fixup_a64_movw_gottprel_g0_nc",   0,    32,             0 },
141       { "fixup_a64_adr_gottprel_page",     0,    32, MCFixupKindInfo::FKF_IsPCRel },
142       { "fixup_a64_ld64_gottprel_lo12_nc", 0,    32,             0 },
143       { "fixup_a64_ld_gottprel_prel19",    0,    32, MCFixupKindInfo::FKF_IsPCRel },
144       { "fixup_a64_movw_tprel_g2",         0,    32,             0 },
145       { "fixup_a64_movw_tprel_g1",         0,    32,             0 },
146       { "fixup_a64_movw_tprel_g1_nc",      0,    32,             0 },
147       { "fixup_a64_movw_tprel_g0",         0,    32,             0 },
148       { "fixup_a64_movw_tprel_g0_nc",      0,    32,             0 },
149       { "fixup_a64_add_tprel_hi12",        0,    32,             0 },
150       { "fixup_a64_add_tprel_lo12",        0,    32,             0 },
151       { "fixup_a64_add_tprel_lo12_nc",     0,    32,             0 },
152       { "fixup_a64_ldst8_tprel_lo12",      0,    32,             0 },
153       { "fixup_a64_ldst8_tprel_lo12_nc",   0,    32,             0 },
154       { "fixup_a64_ldst16_tprel_lo12",     0,    32,             0 },
155       { "fixup_a64_ldst16_tprel_lo12_nc",  0,    32,             0 },
156       { "fixup_a64_ldst32_tprel_lo12",     0,    32,             0 },
157       { "fixup_a64_ldst32_tprel_lo12_nc",  0,    32,             0 },
158       { "fixup_a64_ldst64_tprel_lo12",     0,    32,             0 },
159       { "fixup_a64_ldst64_tprel_lo12_nc",  0,    32,             0 },
160       { "fixup_a64_tlsdesc_adr_page",      0,    32, MCFixupKindInfo::FKF_IsPCRel },
161       { "fixup_a64_tlsdesc_ld64_lo12_nc",  0,    32,             0 },
162       { "fixup_a64_tlsdesc_add_lo12_nc",   0,    32,             0 },
163       { "fixup_a64_tlsdesc_call",          0,     0,             0 }
164     };
165     if (Kind < FirstTargetFixupKind)
166       return MCAsmBackend::getFixupKindInfo(Kind);
167
168     assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
169            "Invalid kind!");
170     return Infos[Kind - FirstTargetFixupKind];
171   }
172
173   void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
174                   uint64_t Value) const {
175     unsigned NumBytes = getFixupKindInfo(Fixup.getKind()).TargetSize / 8;
176     Value = adjustFixupValue(Fixup.getKind(), Value);
177     if (!Value) return;           // Doesn't change encoding.
178
179     unsigned Offset = Fixup.getOffset();
180     assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
181
182     // For each byte of the fragment that the fixup touches, mask in the bits
183     // from the fixup value.
184     for (unsigned i = 0; i != NumBytes; ++i) {
185       Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
186     }
187   }
188
189   bool mayNeedRelaxation(const MCInst&) const {
190     return false;
191   }
192
193   void relaxInstruction(const MCInst&, llvm::MCInst&) const {
194     llvm_unreachable("Cannot relax instructions");
195   }
196
197   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
198     return createAArch64ELFObjectWriter(OS, OSABI);
199   }
200 };
201
202 } // end anonymous namespace
203
204 bool
205 ELFAArch64AsmBackend::fixupNeedsRelaxation(const MCFixup &Fixup,
206                                            uint64_t Value,
207                                            const MCRelaxableFragment *DF,
208                                            const MCAsmLayout &Layout) const {
209   // Correct for now. With all instructions 32-bit only very low-level
210   // considerations could make you select something which may fail.
211   return false;
212 }
213
214
215 bool AArch64AsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
216   // Can't emit NOP with size not multiple of 32-bits
217   if (Count % 4 != 0)
218     return false;
219
220   uint64_t NumNops = Count / 4;
221   for (uint64_t i = 0; i != NumNops; ++i)
222     OW->Write32(0xd503201f);
223
224   return true;
225 }
226
227 static unsigned ADRImmBits(unsigned Value) {
228   unsigned lo2 = Value & 0x3;
229   unsigned hi19 = (Value & 0x1fffff) >> 2;
230
231   return (hi19 << 5) | (lo2 << 29);
232 }
233
234 static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value) {
235   switch (Kind) {
236   default:
237     llvm_unreachable("Unknown fixup kind!");
238   case FK_Data_2:
239     assert((int64_t)Value >= -32768 &&
240            (int64_t)Value <= 65536 &&
241            "Out of range ABS16 fixup");
242     return Value;
243   case FK_Data_4:
244     assert((int64_t)Value >= -(1LL << 31) &&
245            (int64_t)Value <= (1LL << 32) - 1 &&
246            "Out of range ABS32 fixup");
247     return Value;
248   case FK_Data_8:
249     return Value;
250
251   case AArch64::fixup_a64_ld_gottprel_prel19:
252     // R_AARCH64_LD_GOTTPREL_PREL19: Set a load-literal immediate to bits 1F
253     // FFFC of G(TPREL(S+A)) - P; check -2^20 <= X < 2^20.
254   case AArch64::fixup_a64_ld_prel:
255     // R_AARCH64_LD_PREL_LO19: Sets a load-literal (immediate) value to bits
256     // 1F FFFC of S+A-P, checking that -2^20 <= S+A-P < 2^20.
257     assert((int64_t)Value >= -(1LL << 20) &&
258            (int64_t)Value < (1LL << 20) && "Out of range LDR (lit) fixup");
259     return (Value & 0x1ffffc) << 3;
260
261   case AArch64::fixup_a64_adr_prel:
262     // R_AARCH64_ADR_PREL_LO21: Sets an ADR immediate value to bits 1F FFFF of
263     // the result of S+A-P, checking that -2^20 <= S+A-P < 2^20.
264     assert((int64_t)Value >= -(1LL << 20) &&
265            (int64_t)Value < (1LL << 20) && "Out of range ADR fixup");
266     return ADRImmBits(Value & 0x1fffff);
267
268   case AArch64::fixup_a64_adr_prel_page:
269     // R_AARCH64_ADR_PREL_PG_HI21: Sets an ADRP immediate value to bits 1 FFFF
270     // F000 of the result of the operation, checking that -2^32 <= result <
271     // 2^32.
272     assert((int64_t)Value >= -(1LL << 32) &&
273            (int64_t)Value < (1LL << 32) && "Out of range ADRP fixup");
274     return ADRImmBits((Value & 0x1fffff000ULL) >> 12);
275
276   case AArch64::fixup_a64_add_dtprel_hi12:
277     // R_AARCH64_TLSLD_ADD_DTPREL_LO12: Set an ADD immediate field to bits
278     // FF F000 of DTPREL(S+A), check 0 <= X < 2^24.
279   case AArch64::fixup_a64_add_tprel_hi12:
280     // R_AARCH64_TLSLD_ADD_TPREL_LO12: Set an ADD immediate field to bits
281     // FF F000 of TPREL(S+A), check 0 <= X < 2^24.
282     assert((int64_t)Value >= 0 &&
283            (int64_t)Value < (1LL << 24) && "Out of range ADD fixup");
284     return (Value & 0xfff000) >> 2;
285
286   case AArch64::fixup_a64_add_dtprel_lo12:
287     // R_AARCH64_TLSLD_ADD_DTPREL_LO12: Set an ADD immediate field to bits
288     // FFF of DTPREL(S+A), check 0 <= X < 2^12.
289   case AArch64::fixup_a64_add_tprel_lo12:
290     // R_AARCH64_TLSLD_ADD_TPREL_LO12: Set an ADD immediate field to bits
291     // FFF of TPREL(S+A), check 0 <= X < 2^12.
292     assert((int64_t)Value >= 0 &&
293            (int64_t)Value < (1LL << 12) && "Out of range ADD fixup");
294     // ... fallthrough to no-checking versions ...
295   case AArch64::fixup_a64_add_dtprel_lo12_nc:
296     // R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC: Set an ADD immediate field to bits
297     // FFF of DTPREL(S+A) with no overflow check.
298   case AArch64::fixup_a64_add_tprel_lo12_nc:
299     // R_AARCH64_TLSLD_ADD_TPREL_LO12_NC: Set an ADD immediate field to bits
300     // FFF of TPREL(S+A) with no overflow check.
301   case AArch64::fixup_a64_tlsdesc_add_lo12_nc:
302     // R_AARCH64_TLSDESC_ADD_LO12_NC: Set an ADD immediate field to bits
303     // FFF of G(TLSDESC(S+A)), with no overflow check.
304   case AArch64::fixup_a64_add_lo12:
305     // R_AARCH64_ADD_ABS_LO12_NC: Sets an ADD immediate value to bits FFF of
306     // S+A, with no overflow check.
307     return (Value & 0xfff) << 10;
308
309   case AArch64::fixup_a64_ldst8_dtprel_lo12:
310     // R_AARCH64_TLSLD_LDST8_DTPREL_LO12: Set an LD/ST offset field to bits FFF
311     // of DTPREL(S+A), check 0 <= X < 2^12.
312   case AArch64::fixup_a64_ldst8_tprel_lo12:
313     // R_AARCH64_TLSLE_LDST8_TPREL_LO12: Set an LD/ST offset field to bits FFF
314     // of DTPREL(S+A), check 0 <= X < 2^12.
315     assert((int64_t) Value >= 0 &&
316            (int64_t) Value < (1LL << 12) && "Out of range LD/ST fixup");
317     // ... fallthrough to no-checking versions ...
318   case AArch64::fixup_a64_ldst8_dtprel_lo12_nc:
319     // R_AARCH64_TLSLD_LDST8_DTPREL_LO12: Set an LD/ST offset field to bits FFF
320     // of DTPREL(S+A), with no overflow check.
321   case AArch64::fixup_a64_ldst8_tprel_lo12_nc:
322     // R_AARCH64_TLSLD_LDST8_TPREL_LO12: Set an LD/ST offset field to bits FFF
323     // of TPREL(S+A), with no overflow check.
324   case AArch64::fixup_a64_ldst8_lo12:
325     // R_AARCH64_LDST8_ABS_LO12_NC: Sets an LD/ST immediate value to bits FFF
326     // of S+A, with no overflow check.
327     return (Value & 0xfff) << 10;
328
329   case AArch64::fixup_a64_ldst16_dtprel_lo12:
330     // R_AARCH64_TLSLD_LDST16_DTPREL_LO12: Set an LD/ST offset field to bits FFE
331     // of DTPREL(S+A), check 0 <= X < 2^12.
332   case AArch64::fixup_a64_ldst16_tprel_lo12:
333     // R_AARCH64_TLSLE_LDST16_TPREL_LO12: Set an LD/ST offset field to bits FFE
334     // of DTPREL(S+A), check 0 <= X < 2^12.
335     assert((int64_t) Value >= 0 &&
336            (int64_t) Value < (1LL << 12) && "Out of range LD/ST fixup");
337     // ... fallthrough to no-checking versions ...
338   case AArch64::fixup_a64_ldst16_dtprel_lo12_nc:
339     // R_AARCH64_TLSLD_LDST16_DTPREL_LO12: Set an LD/ST offset field to bits FFE
340     // of DTPREL(S+A), with no overflow check.
341   case AArch64::fixup_a64_ldst16_tprel_lo12_nc:
342     // R_AARCH64_TLSLD_LDST16_TPREL_LO12: Set an LD/ST offset field to bits FFE
343     // of TPREL(S+A), with no overflow check.
344   case AArch64::fixup_a64_ldst16_lo12:
345     // R_AARCH64_LDST16_ABS_LO12_NC: Sets an LD/ST immediate value to bits FFE
346     // of S+A, with no overflow check.
347     return (Value & 0xffe) << 9;
348
349   case AArch64::fixup_a64_ldst32_dtprel_lo12:
350     // R_AARCH64_TLSLD_LDST32_DTPREL_LO12: Set an LD/ST offset field to bits FFC
351     // of DTPREL(S+A), check 0 <= X < 2^12.
352   case AArch64::fixup_a64_ldst32_tprel_lo12:
353     // R_AARCH64_TLSLE_LDST32_TPREL_LO12: Set an LD/ST offset field to bits FFC
354     // of DTPREL(S+A), check 0 <= X < 2^12.
355     assert((int64_t) Value >= 0 &&
356            (int64_t) Value < (1LL << 12) && "Out of range LD/ST fixup");
357     // ... fallthrough to no-checking versions ...
358   case AArch64::fixup_a64_ldst32_dtprel_lo12_nc:
359     // R_AARCH64_TLSLD_LDST32_DTPREL_LO12: Set an LD/ST offset field to bits FFC
360     // of DTPREL(S+A), with no overflow check.
361   case AArch64::fixup_a64_ldst32_tprel_lo12_nc:
362     // R_AARCH64_TLSLD_LDST32_TPREL_LO12: Set an LD/ST offset field to bits FFC
363     // of TPREL(S+A), with no overflow check.
364   case AArch64::fixup_a64_ldst32_lo12:
365     // R_AARCH64_LDST32_ABS_LO12_NC: Sets an LD/ST immediate value to bits FFC
366     // of S+A, with no overflow check.
367     return (Value & 0xffc) << 8;
368
369   case AArch64::fixup_a64_ldst64_dtprel_lo12:
370     // R_AARCH64_TLSLD_LDST64_DTPREL_LO12: Set an LD/ST offset field to bits FF8
371     // of DTPREL(S+A), check 0 <= X < 2^12.
372   case AArch64::fixup_a64_ldst64_tprel_lo12:
373     // R_AARCH64_TLSLE_LDST64_TPREL_LO12: Set an LD/ST offset field to bits FF8
374     // of DTPREL(S+A), check 0 <= X < 2^12.
375     assert((int64_t) Value >= 0 &&
376            (int64_t) Value < (1LL << 12) && "Out of range LD/ST fixup");
377     // ... fallthrough to no-checking versions ...
378   case AArch64::fixup_a64_ldst64_dtprel_lo12_nc:
379     // R_AARCH64_TLSLD_LDST64_DTPREL_LO12: Set an LD/ST offset field to bits FF8
380     // of DTPREL(S+A), with no overflow check.
381   case AArch64::fixup_a64_ldst64_tprel_lo12_nc:
382     // R_AARCH64_TLSLD_LDST64_TPREL_LO12: Set an LD/ST offset field to bits FF8
383     // of TPREL(S+A), with no overflow check.
384   case AArch64::fixup_a64_ldst64_lo12:
385     // R_AARCH64_LDST64_ABS_LO12_NC: Sets an LD/ST immediate value to bits FF8
386     // of S+A, with no overflow check.
387     return (Value & 0xff8) << 7;
388
389   case AArch64::fixup_a64_ldst128_lo12:
390     // R_AARCH64_LDST128_ABS_LO12_NC: Sets an LD/ST immediate value to bits FF0
391     // of S+A, with no overflow check.
392     return (Value & 0xff0) << 6;
393
394   case AArch64::fixup_a64_movw_uabs_g0:
395     // R_AARCH64_MOVW_UABS_G0: Sets a MOVZ immediate field to bits FFFF of S+A
396     // with a check that S+A < 2^16
397     assert(Value <= 0xffff && "Out of range move wide fixup");
398     return (Value & 0xffff) << 5;
399
400   case AArch64::fixup_a64_movw_dtprel_g0_nc:
401     // R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC: Sets a MOVK immediate field to bits
402     // FFFF of DTPREL(S+A) with no overflow check.
403   case AArch64::fixup_a64_movw_gottprel_g0_nc:
404     // R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC: Sets a MOVK immediate field to bits
405     // FFFF of G(TPREL(S+A)) - GOT with no overflow check.
406   case AArch64::fixup_a64_movw_tprel_g0_nc:
407     // R_AARCH64_TLSLE_MOVW_TPREL_G0_NC: Sets a MOVK immediate field to bits
408     // FFFF of TPREL(S+A) with no overflow check.
409   case AArch64::fixup_a64_movw_uabs_g0_nc:
410     // R_AARCH64_MOVW_UABS_G0_NC: Sets a MOVK immediate field to bits FFFF of
411     // S+A with no overflow check.
412     return (Value & 0xffff) << 5;
413
414   case AArch64::fixup_a64_movw_uabs_g1:
415     // R_AARCH64_MOVW_UABS_G1: Sets a MOVZ immediate field to bits FFFF0000 of
416     // S+A with a check that S+A < 2^32
417     assert(Value <= 0xffffffffull && "Out of range move wide fixup");
418     return ((Value >> 16) & 0xffff) << 5;
419
420   case AArch64::fixup_a64_movw_dtprel_g1_nc:
421     // R_AARCH64_TLSLD_MOVW_DTPREL_G1_NC: Set a MOVK immediate field
422     // to bits FFFF0000 of DTPREL(S+A), with no overflow check.
423   case AArch64::fixup_a64_movw_tprel_g1_nc:
424     // R_AARCH64_TLSLD_MOVW_TPREL_G1_NC: Set a MOVK immediate field
425     // to bits FFFF0000 of TPREL(S+A), with no overflow check.
426   case AArch64::fixup_a64_movw_uabs_g1_nc:
427     // R_AARCH64_MOVW_UABS_G1_NC: Sets a MOVK immediate field to bits
428     // FFFF0000 of S+A with no overflow check.
429     return ((Value >> 16) & 0xffff) << 5;
430
431   case AArch64::fixup_a64_movw_uabs_g2:
432     // R_AARCH64_MOVW_UABS_G2: Sets a MOVZ immediate field to bits FFFF 0000
433     // 0000 of S+A with a check that S+A < 2^48
434     assert(Value <= 0xffffffffffffull && "Out of range move wide fixup");
435     return ((Value >> 32) & 0xffff) << 5;
436
437   case AArch64::fixup_a64_movw_uabs_g2_nc:
438     // R_AARCH64_MOVW_UABS_G2: Sets a MOVK immediate field to bits FFFF 0000
439     // 0000 of S+A with no overflow check.
440     return ((Value >> 32) & 0xffff) << 5;
441
442   case AArch64::fixup_a64_movw_uabs_g3:
443     // R_AARCH64_MOVW_UABS_G3: Sets a MOVZ immediate field to bits FFFF 0000
444     // 0000 0000 of S+A (no overflow check needed)
445     return ((Value >> 48) & 0xffff) << 5;
446
447   case AArch64::fixup_a64_movw_dtprel_g0:
448     // R_AARCH64_TLSLD_MOVW_DTPREL_G0: Set a MOV[NZ] immediate field
449     // to bits FFFF of DTPREL(S+A).
450   case AArch64::fixup_a64_movw_tprel_g0:
451     // R_AARCH64_TLSLE_MOVW_TPREL_G0: Set a MOV[NZ] immediate field to
452     // bits FFFF of TPREL(S+A).
453   case AArch64::fixup_a64_movw_sabs_g0: {
454     // R_AARCH64_MOVW_SABS_G0: Sets MOV[NZ] immediate field using bits FFFF of
455     // S+A (see notes below); check -2^16 <= S+A < 2^16. (notes say that we
456     // should convert between MOVN and MOVZ to achieve our goals).
457     int64_t Signed = Value;
458     assert(Signed >= -(1LL << 16) && Signed < (1LL << 16)
459            && "Out of range move wide fixup");
460     if (Signed >= 0) {
461       Value = (Value & 0xffff) << 5;
462       // Bit 30 converts the MOVN encoding into a MOVZ
463       Value |= 1 << 30;
464     } else {
465       // MCCodeEmitter should have encoded a MOVN, which is fine.
466       Value = (~Value & 0xffff) << 5;
467     }
468     return Value;
469   }
470
471   case AArch64::fixup_a64_movw_dtprel_g1:
472     // R_AARCH64_TLSLD_MOVW_DTPREL_G1: Set a MOV[NZ] immediate field
473     // to bits FFFF0000 of DTPREL(S+A).
474   case AArch64::fixup_a64_movw_gottprel_g1:
475     // R_AARCH64_TLSIE_MOVW_GOTTPREL_G1: Set a MOV[NZ] immediate field
476     // to bits FFFF0000 of G(TPREL(S+A)) - GOT.
477   case AArch64::fixup_a64_movw_tprel_g1:
478     // R_AARCH64_TLSLE_MOVW_TPREL_G1: Set a MOV[NZ] immediate field to
479     // bits FFFF0000 of TPREL(S+A).
480   case AArch64::fixup_a64_movw_sabs_g1: {
481     // R_AARCH64_MOVW_SABS_G1: Sets MOV[NZ] immediate field using bits FFFF 0000
482     // of S+A (see notes below); check -2^32 <= S+A < 2^32. (notes say that we
483     // should convert between MOVN and MOVZ to achieve our goals).
484     int64_t Signed = Value;
485     assert(Signed >= -(1LL << 32) && Signed < (1LL << 32)
486            && "Out of range move wide fixup");
487     if (Signed >= 0) {
488       Value = ((Value >> 16) & 0xffff) << 5;
489       // Bit 30 converts the MOVN encoding into a MOVZ
490       Value |= 1 << 30;
491     } else {
492       Value = ((~Value >> 16) & 0xffff) << 5;
493     }
494     return Value;
495   }
496
497   case AArch64::fixup_a64_movw_dtprel_g2:
498     // R_AARCH64_TLSLD_MOVW_DTPREL_G2: Set a MOV[NZ] immediate field
499     // to bits FFFF 0000 0000 of DTPREL(S+A).
500   case AArch64::fixup_a64_movw_tprel_g2:
501     // R_AARCH64_TLSLE_MOVW_TPREL_G2: Set a MOV[NZ] immediate field to
502     // bits FFFF 0000 0000 of TPREL(S+A).
503   case AArch64::fixup_a64_movw_sabs_g2: {
504     // R_AARCH64_MOVW_SABS_G2: Sets MOV[NZ] immediate field using bits FFFF 0000
505     // 0000 of S+A (see notes below); check -2^48 <= S+A < 2^48. (notes say that
506     // we should convert between MOVN and MOVZ to achieve our goals).
507     int64_t Signed = Value;
508     assert(Signed >= -(1LL << 48) && Signed < (1LL << 48)
509            && "Out of range move wide fixup");
510     if (Signed >= 0) {
511       Value = ((Value >> 32) & 0xffff) << 5;
512       // Bit 30 converts the MOVN encoding into a MOVZ
513       Value |= 1 << 30;
514     } else {
515       Value = ((~Value >> 32) & 0xffff) << 5;
516     }
517     return Value;
518   }
519
520   case AArch64::fixup_a64_tstbr:
521     // R_AARCH64_TSTBR14: Sets the immediate field of a TBZ/TBNZ instruction to
522     // bits FFFC of S+A-P, checking -2^15 <= S+A-P < 2^15.
523     assert((int64_t)Value >= -(1LL << 15) &&
524            (int64_t)Value < (1LL << 15) && "Out of range TBZ/TBNZ fixup");
525     return (Value & 0xfffc) << (5 - 2);
526
527   case AArch64::fixup_a64_condbr:
528     // R_AARCH64_CONDBR19: Sets the immediate field of a conditional branch
529     // instruction to bits 1FFFFC of S+A-P, checking -2^20 <= S+A-P < 2^20.
530     assert((int64_t)Value >= -(1LL << 20) &&
531            (int64_t)Value < (1LL << 20) && "Out of range B.cond fixup");
532     return (Value & 0x1ffffc) << (5 - 2);
533
534   case AArch64::fixup_a64_uncondbr:
535     // R_AARCH64_JUMP26 same as below (except to a linker, possibly).
536   case AArch64::fixup_a64_call:
537     // R_AARCH64_CALL26: Sets a CALL immediate field to bits FFFFFFC of S+A-P,
538     // checking that -2^27 <= S+A-P < 2^27.
539     assert((int64_t)Value >= -(1LL << 27) &&
540            (int64_t)Value < (1LL << 27) && "Out of range branch fixup");
541     return (Value & 0xffffffc) >> 2;
542
543   case AArch64::fixup_a64_adr_gottprel_page:
544     // R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: Set an ADRP immediate field to bits
545     // 1FFFFF000 of Page(G(TPREL(S+A))) - Page(P); check -2^32 <= X < 2^32.
546   case AArch64::fixup_a64_tlsdesc_adr_page:
547     // R_AARCH64_TLSDESC_ADR_PAGE: Set an ADRP immediate field to bits 1FFFFF000
548     // of Page(G(TLSDESC(S+A))) - Page(P); check -2^32 <= X < 2^32.
549   case AArch64::fixup_a64_adr_prel_got_page:
550     // R_AARCH64_ADR_GOT_PAGE: Sets the immediate value of an ADRP to bits
551     // 1FFFFF000 of the operation, checking that -2^32 < Page(G(S))-Page(GOT) <
552     // 2^32.
553     assert((int64_t)Value >= -(1LL << 32) &&
554            (int64_t)Value < (1LL << 32) && "Out of range ADRP fixup");
555     return ADRImmBits((Value & 0x1fffff000ULL) >> 12);
556
557   case AArch64::fixup_a64_ld64_gottprel_lo12_nc:
558     // R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: Set an LD offset field to bits FF8
559     // of X, with no overflow check. Check that X & 7 == 0.
560   case AArch64::fixup_a64_tlsdesc_ld64_lo12_nc:
561     // R_AARCH64_TLSDESC_LD64_LO12_NC: Set an LD offset field to bits FF8 of
562     // G(TLSDESC(S+A)), with no overflow check. Check that X & 7 == 0.
563   case AArch64::fixup_a64_ld64_got_lo12_nc:
564     // R_AARCH64_LD64_GOT_LO12_NC: Sets the LD/ST immediate field to bits FF8 of
565     // G(S) with no overflow check. Check X & 7 == 0
566     assert(((int64_t)Value & 7) == 0 && "Misaligned fixup");
567     return (Value & 0xff8) << 7;
568
569   case AArch64::fixup_a64_tlsdesc_call:
570     // R_AARCH64_TLSDESC_CALL: For relaxation only.
571     return 0;
572   }
573 }
574
575 MCAsmBackend *
576 llvm::createAArch64AsmBackend(const Target &T, StringRef TT, StringRef CPU) {
577   Triple TheTriple(TT);
578
579   return new ELFAArch64AsmBackend(T, TT, TheTriple.getOS());
580 }