blob: d0154966cb4c503f4d7be6a85309e3fc81b0f48c [file] [log] [blame]
Tim Northover3b0846e2014-05-24 12:50:23 +00001//===-- 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 "AArch64.h"
11#include "AArch64RegisterInfo.h"
12#include "MCTargetDesc/AArch64FixupKinds.h"
Daniel Sanders50f17232015-09-15 16:17:27 +000013#include "llvm/ADT/Triple.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000014#include "llvm/BinaryFormat/MachO.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000015#include "llvm/MC/MCAsmBackend.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "llvm/MC/MCAssembler.h"
Oliver Stannarda5520b02016-04-01 09:14:50 +000017#include "llvm/MC/MCContext.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000018#include "llvm/MC/MCDirectives.h"
Chad Rosierafe7c932014-08-06 16:05:02 +000019#include "llvm/MC/MCELFObjectWriter.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000020#include "llvm/MC/MCFixupKindInfo.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000021#include "llvm/MC/MCObjectWriter.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000022#include "llvm/MC/MCSectionELF.h"
Benjamin Kramer1f8930e2014-07-25 11:42:14 +000023#include "llvm/MC/MCSectionMachO.h"
Peter Collingbournee8813e62015-03-24 21:47:03 +000024#include "llvm/MC/MCValue.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000025#include "llvm/Support/ErrorHandling.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000026using namespace llvm;
27
28namespace {
29
30class AArch64AsmBackend : public MCAsmBackend {
31 static const unsigned PCRelFlagVal =
32 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits | MCFixupKindInfo::FKF_IsPCRel;
Keith Walker8c44bf12016-01-20 15:59:14 +000033public:
34 bool IsLittleEndian;
Tim Northover3b0846e2014-05-24 12:50:23 +000035
36public:
Keith Walker8c44bf12016-01-20 15:59:14 +000037 AArch64AsmBackend(const Target &T, bool IsLittleEndian)
38 : MCAsmBackend(), IsLittleEndian(IsLittleEndian) {}
Tim Northover3b0846e2014-05-24 12:50:23 +000039
40 unsigned getNumFixupKinds() const override {
41 return AArch64::NumTargetFixupKinds;
42 }
43
44 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
45 const static MCFixupKindInfo Infos[AArch64::NumTargetFixupKinds] = {
Rafael Espindola3ac4c092017-06-20 22:53:29 +000046 // This table *must* be in the order that the fixup_* kinds are defined
47 // in AArch64FixupKinds.h.
48 //
49 // Name Offset (bits) Size (bits) Flags
50 {"fixup_aarch64_pcrel_adr_imm21", 0, 32, PCRelFlagVal},
51 {"fixup_aarch64_pcrel_adrp_imm21", 0, 32, PCRelFlagVal},
52 {"fixup_aarch64_add_imm12", 10, 12, 0},
53 {"fixup_aarch64_ldst_imm12_scale1", 10, 12, 0},
54 {"fixup_aarch64_ldst_imm12_scale2", 10, 12, 0},
55 {"fixup_aarch64_ldst_imm12_scale4", 10, 12, 0},
56 {"fixup_aarch64_ldst_imm12_scale8", 10, 12, 0},
57 {"fixup_aarch64_ldst_imm12_scale16", 10, 12, 0},
58 {"fixup_aarch64_ldr_pcrel_imm19", 5, 19, PCRelFlagVal},
59 {"fixup_aarch64_movw", 5, 16, 0},
60 {"fixup_aarch64_pcrel_branch14", 5, 14, PCRelFlagVal},
61 {"fixup_aarch64_pcrel_branch19", 5, 19, PCRelFlagVal},
62 {"fixup_aarch64_pcrel_branch26", 0, 26, PCRelFlagVal},
63 {"fixup_aarch64_pcrel_call26", 0, 26, PCRelFlagVal},
64 {"fixup_aarch64_tlsdesc_call", 0, 0, 0}};
Tim Northover3b0846e2014-05-24 12:50:23 +000065
66 if (Kind < FirstTargetFixupKind)
67 return MCAsmBackend::getFixupKindInfo(Kind);
68
69 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
70 "Invalid kind!");
71 return Infos[Kind - FirstTargetFixupKind];
72 }
73
74 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Alex Bradbury866113c2017-04-05 10:16:14 +000075 uint64_t Value, bool IsPCRel, MCContext &Ctx) const override;
Tim Northover3b0846e2014-05-24 12:50:23 +000076
77 bool mayNeedRelaxation(const MCInst &Inst) const override;
78 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
79 const MCRelaxableFragment *DF,
80 const MCAsmLayout &Layout) const override;
Nirav Dave86030622016-07-11 14:23:53 +000081 void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
82 MCInst &Res) const override;
Tim Northover3b0846e2014-05-24 12:50:23 +000083 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
84
85 void HandleAssemblerFlag(MCAssemblerFlag Flag) {}
86
87 unsigned getPointerSize() const { return 8; }
Keith Walker8c44bf12016-01-20 15:59:14 +000088
89 unsigned getFixupKindContainereSizeInBytes(unsigned Kind) const;
Tim Northover3b0846e2014-05-24 12:50:23 +000090};
91
92} // end anonymous namespace
93
94/// \brief The number of bytes the fixup may change.
95static unsigned getFixupKindNumBytes(unsigned Kind) {
96 switch (Kind) {
97 default:
Craig Topper2a30d782014-06-18 05:05:13 +000098 llvm_unreachable("Unknown fixup kind!");
Tim Northover3b0846e2014-05-24 12:50:23 +000099
100 case AArch64::fixup_aarch64_tlsdesc_call:
101 return 0;
102
103 case FK_Data_1:
104 return 1;
105
106 case FK_Data_2:
107 case AArch64::fixup_aarch64_movw:
108 return 2;
109
110 case AArch64::fixup_aarch64_pcrel_branch14:
111 case AArch64::fixup_aarch64_add_imm12:
112 case AArch64::fixup_aarch64_ldst_imm12_scale1:
113 case AArch64::fixup_aarch64_ldst_imm12_scale2:
114 case AArch64::fixup_aarch64_ldst_imm12_scale4:
115 case AArch64::fixup_aarch64_ldst_imm12_scale8:
116 case AArch64::fixup_aarch64_ldst_imm12_scale16:
117 case AArch64::fixup_aarch64_ldr_pcrel_imm19:
118 case AArch64::fixup_aarch64_pcrel_branch19:
119 return 3;
120
121 case AArch64::fixup_aarch64_pcrel_adr_imm21:
122 case AArch64::fixup_aarch64_pcrel_adrp_imm21:
123 case AArch64::fixup_aarch64_pcrel_branch26:
124 case AArch64::fixup_aarch64_pcrel_call26:
125 case FK_Data_4:
126 return 4;
127
128 case FK_Data_8:
129 return 8;
130 }
131}
132
133static unsigned AdrImmBits(unsigned Value) {
134 unsigned lo2 = Value & 0x3;
135 unsigned hi19 = (Value & 0x1ffffc) >> 2;
136 return (hi19 << 5) | (lo2 << 29);
137}
138
Oliver Stannarda5520b02016-04-01 09:14:50 +0000139static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
Alex Bradbury866113c2017-04-05 10:16:14 +0000140 MCContext &Ctx) {
Oliver Stannarda5520b02016-04-01 09:14:50 +0000141 unsigned Kind = Fixup.getKind();
Tim Northover3b0846e2014-05-24 12:50:23 +0000142 int64_t SignedValue = static_cast<int64_t>(Value);
143 switch (Kind) {
144 default:
Craig Topperd3c02f12015-01-05 10:15:49 +0000145 llvm_unreachable("Unknown fixup kind!");
Tim Northover3b0846e2014-05-24 12:50:23 +0000146 case AArch64::fixup_aarch64_pcrel_adr_imm21:
Alex Bradbury866113c2017-04-05 10:16:14 +0000147 if (SignedValue > 2097151 || SignedValue < -2097152)
148 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
Tim Northover3b0846e2014-05-24 12:50:23 +0000149 return AdrImmBits(Value & 0x1fffffULL);
150 case AArch64::fixup_aarch64_pcrel_adrp_imm21:
151 return AdrImmBits((Value & 0x1fffff000ULL) >> 12);
152 case AArch64::fixup_aarch64_ldr_pcrel_imm19:
153 case AArch64::fixup_aarch64_pcrel_branch19:
154 // Signed 21-bit immediate
155 if (SignedValue > 2097151 || SignedValue < -2097152)
Alex Bradbury866113c2017-04-05 10:16:14 +0000156 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
157 if (Value & 0x3)
158 Ctx.reportError(Fixup.getLoc(), "fixup not sufficiently aligned");
Tim Northover3b0846e2014-05-24 12:50:23 +0000159 // Low two bits are not encoded.
160 return (Value >> 2) & 0x7ffff;
161 case AArch64::fixup_aarch64_add_imm12:
162 case AArch64::fixup_aarch64_ldst_imm12_scale1:
163 // Unsigned 12-bit immediate
Alex Bradbury866113c2017-04-05 10:16:14 +0000164 if (Value >= 0x1000)
165 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
Tim Northover3b0846e2014-05-24 12:50:23 +0000166 return Value;
167 case AArch64::fixup_aarch64_ldst_imm12_scale2:
168 // Unsigned 12-bit immediate which gets multiplied by 2
Alex Bradbury866113c2017-04-05 10:16:14 +0000169 if (Value >= 0x2000)
170 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
171 if (Value & 0x1)
172 Ctx.reportError(Fixup.getLoc(), "fixup must be 2-byte aligned");
Tim Northover3b0846e2014-05-24 12:50:23 +0000173 return Value >> 1;
174 case AArch64::fixup_aarch64_ldst_imm12_scale4:
175 // Unsigned 12-bit immediate which gets multiplied by 4
Alex Bradbury866113c2017-04-05 10:16:14 +0000176 if (Value >= 0x4000)
177 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
178 if (Value & 0x3)
179 Ctx.reportError(Fixup.getLoc(), "fixup must be 4-byte aligned");
Tim Northover3b0846e2014-05-24 12:50:23 +0000180 return Value >> 2;
181 case AArch64::fixup_aarch64_ldst_imm12_scale8:
182 // Unsigned 12-bit immediate which gets multiplied by 8
Alex Bradbury866113c2017-04-05 10:16:14 +0000183 if (Value >= 0x8000)
184 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
185 if (Value & 0x7)
186 Ctx.reportError(Fixup.getLoc(), "fixup must be 8-byte aligned");
Tim Northover3b0846e2014-05-24 12:50:23 +0000187 return Value >> 3;
188 case AArch64::fixup_aarch64_ldst_imm12_scale16:
189 // Unsigned 12-bit immediate which gets multiplied by 16
Alex Bradbury866113c2017-04-05 10:16:14 +0000190 if (Value >= 0x10000)
191 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
192 if (Value & 0xf)
193 Ctx.reportError(Fixup.getLoc(), "fixup must be 16-byte aligned");
Tim Northover3b0846e2014-05-24 12:50:23 +0000194 return Value >> 4;
195 case AArch64::fixup_aarch64_movw:
Alex Bradbury866113c2017-04-05 10:16:14 +0000196 Ctx.reportError(Fixup.getLoc(),
197 "no resolvable MOVZ/MOVK fixups supported yet");
Tim Northover3b0846e2014-05-24 12:50:23 +0000198 return Value;
199 case AArch64::fixup_aarch64_pcrel_branch14:
200 // Signed 16-bit immediate
Alex Bradbury866113c2017-04-05 10:16:14 +0000201 if (SignedValue > 32767 || SignedValue < -32768)
202 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
Tim Northover3b0846e2014-05-24 12:50:23 +0000203 // Low two bits are not encoded (4-byte alignment assumed).
Alex Bradbury866113c2017-04-05 10:16:14 +0000204 if (Value & 0x3)
205 Ctx.reportError(Fixup.getLoc(), "fixup not sufficiently aligned");
Tim Northover3b0846e2014-05-24 12:50:23 +0000206 return (Value >> 2) & 0x3fff;
207 case AArch64::fixup_aarch64_pcrel_branch26:
208 case AArch64::fixup_aarch64_pcrel_call26:
209 // Signed 28-bit immediate
Alex Bradbury866113c2017-04-05 10:16:14 +0000210 if (SignedValue > 134217727 || SignedValue < -134217728)
211 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
Tim Northover3b0846e2014-05-24 12:50:23 +0000212 // Low two bits are not encoded (4-byte alignment assumed).
Alex Bradbury866113c2017-04-05 10:16:14 +0000213 if (Value & 0x3)
214 Ctx.reportError(Fixup.getLoc(), "fixup not sufficiently aligned");
Tim Northover3b0846e2014-05-24 12:50:23 +0000215 return (Value >> 2) & 0x3ffffff;
216 case FK_Data_1:
217 case FK_Data_2:
218 case FK_Data_4:
219 case FK_Data_8:
220 return Value;
221 }
222}
223
Keith Walker8c44bf12016-01-20 15:59:14 +0000224/// getFixupKindContainereSizeInBytes - The number of bytes of the
225/// container involved in big endian or 0 if the item is little endian
226unsigned AArch64AsmBackend::getFixupKindContainereSizeInBytes(unsigned Kind) const {
227 if (IsLittleEndian)
228 return 0;
229
230 switch (Kind) {
231 default:
232 llvm_unreachable("Unknown fixup kind!");
233
234 case FK_Data_1:
235 return 1;
236 case FK_Data_2:
237 return 2;
238 case FK_Data_4:
239 return 4;
240 case FK_Data_8:
241 return 8;
242
243 case AArch64::fixup_aarch64_tlsdesc_call:
244 case AArch64::fixup_aarch64_movw:
245 case AArch64::fixup_aarch64_pcrel_branch14:
246 case AArch64::fixup_aarch64_add_imm12:
247 case AArch64::fixup_aarch64_ldst_imm12_scale1:
248 case AArch64::fixup_aarch64_ldst_imm12_scale2:
249 case AArch64::fixup_aarch64_ldst_imm12_scale4:
250 case AArch64::fixup_aarch64_ldst_imm12_scale8:
251 case AArch64::fixup_aarch64_ldst_imm12_scale16:
252 case AArch64::fixup_aarch64_ldr_pcrel_imm19:
253 case AArch64::fixup_aarch64_pcrel_branch19:
254 case AArch64::fixup_aarch64_pcrel_adr_imm21:
255 case AArch64::fixup_aarch64_pcrel_adrp_imm21:
256 case AArch64::fixup_aarch64_pcrel_branch26:
257 case AArch64::fixup_aarch64_pcrel_call26:
258 // Instructions are always little endian
259 return 0;
260 }
261}
262
Tim Northover3b0846e2014-05-24 12:50:23 +0000263void AArch64AsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
264 unsigned DataSize, uint64_t Value,
Alex Bradbury866113c2017-04-05 10:16:14 +0000265 bool IsPCRel, MCContext &Ctx) const {
Tim Northover3b0846e2014-05-24 12:50:23 +0000266 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
267 if (!Value)
268 return; // Doesn't change encoding.
269 MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
270 // Apply any target-specific value adjustments.
Alex Bradbury866113c2017-04-05 10:16:14 +0000271 Value = adjustFixupValue(Fixup, Value, Ctx);
Tim Northover3b0846e2014-05-24 12:50:23 +0000272
273 // Shift the value into position.
274 Value <<= Info.TargetOffset;
275
276 unsigned Offset = Fixup.getOffset();
277 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
278
Keith Walker8c44bf12016-01-20 15:59:14 +0000279 // Used to point to big endian bytes.
280 unsigned FulleSizeInBytes = getFixupKindContainereSizeInBytes(Fixup.getKind());
281
Tim Northover3b0846e2014-05-24 12:50:23 +0000282 // For each byte of the fragment that the fixup touches, mask in the
283 // bits from the fixup value.
Keith Walker8c44bf12016-01-20 15:59:14 +0000284 if (FulleSizeInBytes == 0) {
285 // Handle as little-endian
286 for (unsigned i = 0; i != NumBytes; ++i) {
287 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
288 }
289 } else {
290 // Handle as big-endian
291 assert((Offset + FulleSizeInBytes) <= DataSize && "Invalid fixup size!");
292 assert(NumBytes <= FulleSizeInBytes && "Invalid fixup size!");
293 for (unsigned i = 0; i != NumBytes; ++i) {
294 unsigned Idx = FulleSizeInBytes - 1 - i;
295 Data[Offset + Idx] |= uint8_t((Value >> (i * 8)) & 0xff);
296 }
297 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000298}
299
300bool AArch64AsmBackend::mayNeedRelaxation(const MCInst &Inst) const {
301 return false;
302}
303
304bool AArch64AsmBackend::fixupNeedsRelaxation(const MCFixup &Fixup,
305 uint64_t Value,
306 const MCRelaxableFragment *DF,
307 const MCAsmLayout &Layout) const {
308 // FIXME: This isn't correct for AArch64. Just moving the "generic" logic
309 // into the targets for now.
310 //
311 // Relax if the value is too big for a (signed) i8.
312 return int64_t(Value) != int64_t(int8_t(Value));
313}
314
315void AArch64AsmBackend::relaxInstruction(const MCInst &Inst,
Nirav Dave86030622016-07-11 14:23:53 +0000316 const MCSubtargetInfo &STI,
Tim Northover3b0846e2014-05-24 12:50:23 +0000317 MCInst &Res) const {
Craig Topperd3c02f12015-01-05 10:15:49 +0000318 llvm_unreachable("AArch64AsmBackend::relaxInstruction() unimplemented");
Tim Northover3b0846e2014-05-24 12:50:23 +0000319}
320
321bool AArch64AsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
322 // If the count is not 4-byte aligned, we must be writing data into the text
323 // section (otherwise we have unaligned instructions, and thus have far
324 // bigger problems), so just write zeros instead.
Benjamin Kramer97fbdd52015-04-17 11:12:43 +0000325 OW->WriteZeros(Count % 4);
Tim Northover3b0846e2014-05-24 12:50:23 +0000326
327 // We are properly aligned, so write NOPs as requested.
328 Count /= 4;
329 for (uint64_t i = 0; i != Count; ++i)
Jim Grosbach36e60e92015-06-04 22:24:41 +0000330 OW->write32(0xd503201f);
Tim Northover3b0846e2014-05-24 12:50:23 +0000331 return true;
332}
333
334namespace {
335
336namespace CU {
337
338/// \brief Compact unwind encoding values.
339enum CompactUnwindEncodings {
340 /// \brief A "frameless" leaf function, where no non-volatile registers are
341 /// saved. The return remains in LR throughout the function.
Tim Northover87442c12016-02-23 21:49:05 +0000342 UNWIND_ARM64_MODE_FRAMELESS = 0x02000000,
Tim Northover3b0846e2014-05-24 12:50:23 +0000343
344 /// \brief No compact unwind encoding available. Instead the low 23-bits of
345 /// the compact unwind encoding is the offset of the DWARF FDE in the
346 /// __eh_frame section. This mode is never used in object files. It is only
347 /// generated by the linker in final linked images, which have only DWARF info
348 /// for a function.
Tim Northover87442c12016-02-23 21:49:05 +0000349 UNWIND_ARM64_MODE_DWARF = 0x03000000,
Tim Northover3b0846e2014-05-24 12:50:23 +0000350
351 /// \brief This is a standard arm64 prologue where FP/LR are immediately
352 /// pushed on the stack, then SP is copied to FP. If there are any
353 /// non-volatile register saved, they are copied into the stack fame in pairs
354 /// in a contiguous ranger right below the saved FP/LR pair. Any subset of the
355 /// five X pairs and four D pairs can be saved, but the memory layout must be
356 /// in register number order.
Tim Northover87442c12016-02-23 21:49:05 +0000357 UNWIND_ARM64_MODE_FRAME = 0x04000000,
Tim Northover3b0846e2014-05-24 12:50:23 +0000358
359 /// \brief Frame register pair encodings.
Tim Northover87442c12016-02-23 21:49:05 +0000360 UNWIND_ARM64_FRAME_X19_X20_PAIR = 0x00000001,
361 UNWIND_ARM64_FRAME_X21_X22_PAIR = 0x00000002,
362 UNWIND_ARM64_FRAME_X23_X24_PAIR = 0x00000004,
363 UNWIND_ARM64_FRAME_X25_X26_PAIR = 0x00000008,
364 UNWIND_ARM64_FRAME_X27_X28_PAIR = 0x00000010,
365 UNWIND_ARM64_FRAME_D8_D9_PAIR = 0x00000100,
366 UNWIND_ARM64_FRAME_D10_D11_PAIR = 0x00000200,
367 UNWIND_ARM64_FRAME_D12_D13_PAIR = 0x00000400,
368 UNWIND_ARM64_FRAME_D14_D15_PAIR = 0x00000800
Tim Northover3b0846e2014-05-24 12:50:23 +0000369};
370
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000371} // end CU namespace
Tim Northover3b0846e2014-05-24 12:50:23 +0000372
373// FIXME: This should be in a separate file.
374class DarwinAArch64AsmBackend : public AArch64AsmBackend {
375 const MCRegisterInfo &MRI;
376
377 /// \brief Encode compact unwind stack adjustment for frameless functions.
Tim Northover87442c12016-02-23 21:49:05 +0000378 /// See UNWIND_ARM64_FRAMELESS_STACK_SIZE_MASK in compact_unwind_encoding.h.
Tim Northover3b0846e2014-05-24 12:50:23 +0000379 /// The stack size always needs to be 16 byte aligned.
380 uint32_t encodeStackAdjustment(uint32_t StackSize) const {
381 return (StackSize / 16) << 12;
382 }
383
384public:
385 DarwinAArch64AsmBackend(const Target &T, const MCRegisterInfo &MRI)
Keith Walker8c44bf12016-01-20 15:59:14 +0000386 : AArch64AsmBackend(T, /*IsLittleEndian*/true), MRI(MRI) {}
Tim Northover3b0846e2014-05-24 12:50:23 +0000387
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000388 MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
Tim Northover3b0846e2014-05-24 12:50:23 +0000389 return createAArch64MachObjectWriter(OS, MachO::CPU_TYPE_ARM64,
390 MachO::CPU_SUBTYPE_ARM64_ALL);
391 }
392
Tim Northover3b0846e2014-05-24 12:50:23 +0000393 /// \brief Generate the compact unwind encoding from the CFI directives.
394 uint32_t generateCompactUnwindEncoding(
395 ArrayRef<MCCFIInstruction> Instrs) const override {
396 if (Instrs.empty())
Tim Northover87442c12016-02-23 21:49:05 +0000397 return CU::UNWIND_ARM64_MODE_FRAMELESS;
Tim Northover3b0846e2014-05-24 12:50:23 +0000398
399 bool HasFP = false;
400 unsigned StackSize = 0;
401
402 uint32_t CompactUnwindEncoding = 0;
403 for (size_t i = 0, e = Instrs.size(); i != e; ++i) {
404 const MCCFIInstruction &Inst = Instrs[i];
405
406 switch (Inst.getOperation()) {
407 default:
408 // Cannot handle this directive: bail out.
Tim Northover87442c12016-02-23 21:49:05 +0000409 return CU::UNWIND_ARM64_MODE_DWARF;
Tim Northover3b0846e2014-05-24 12:50:23 +0000410 case MCCFIInstruction::OpDefCfa: {
411 // Defines a frame pointer.
412 assert(getXRegFromWReg(MRI.getLLVMRegNum(Inst.getRegister(), true)) ==
413 AArch64::FP &&
414 "Invalid frame pointer!");
415 assert(i + 2 < e && "Insufficient CFI instructions to define a frame!");
416
417 const MCCFIInstruction &LRPush = Instrs[++i];
418 assert(LRPush.getOperation() == MCCFIInstruction::OpOffset &&
419 "Link register not pushed!");
420 const MCCFIInstruction &FPPush = Instrs[++i];
421 assert(FPPush.getOperation() == MCCFIInstruction::OpOffset &&
422 "Frame pointer not pushed!");
423
424 unsigned LRReg = MRI.getLLVMRegNum(LRPush.getRegister(), true);
425 unsigned FPReg = MRI.getLLVMRegNum(FPPush.getRegister(), true);
426
427 LRReg = getXRegFromWReg(LRReg);
428 FPReg = getXRegFromWReg(FPReg);
429
430 assert(LRReg == AArch64::LR && FPReg == AArch64::FP &&
431 "Pushing invalid registers for frame!");
432
433 // Indicate that the function has a frame.
Tim Northover87442c12016-02-23 21:49:05 +0000434 CompactUnwindEncoding |= CU::UNWIND_ARM64_MODE_FRAME;
Tim Northover3b0846e2014-05-24 12:50:23 +0000435 HasFP = true;
436 break;
437 }
438 case MCCFIInstruction::OpDefCfaOffset: {
439 assert(StackSize == 0 && "We already have the CFA offset!");
440 StackSize = std::abs(Inst.getOffset());
441 break;
442 }
443 case MCCFIInstruction::OpOffset: {
444 // Registers are saved in pairs. We expect there to be two consecutive
445 // `.cfi_offset' instructions with the appropriate registers specified.
446 unsigned Reg1 = MRI.getLLVMRegNum(Inst.getRegister(), true);
447 if (i + 1 == e)
Tim Northover87442c12016-02-23 21:49:05 +0000448 return CU::UNWIND_ARM64_MODE_DWARF;
Tim Northover3b0846e2014-05-24 12:50:23 +0000449
450 const MCCFIInstruction &Inst2 = Instrs[++i];
451 if (Inst2.getOperation() != MCCFIInstruction::OpOffset)
Tim Northover87442c12016-02-23 21:49:05 +0000452 return CU::UNWIND_ARM64_MODE_DWARF;
Tim Northover3b0846e2014-05-24 12:50:23 +0000453 unsigned Reg2 = MRI.getLLVMRegNum(Inst2.getRegister(), true);
454
455 // N.B. The encodings must be in register number order, and the X
456 // registers before the D registers.
457
458 // X19/X20 pair = 0x00000001,
459 // X21/X22 pair = 0x00000002,
460 // X23/X24 pair = 0x00000004,
461 // X25/X26 pair = 0x00000008,
462 // X27/X28 pair = 0x00000010
463 Reg1 = getXRegFromWReg(Reg1);
464 Reg2 = getXRegFromWReg(Reg2);
465
466 if (Reg1 == AArch64::X19 && Reg2 == AArch64::X20 &&
467 (CompactUnwindEncoding & 0xF1E) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000468 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X19_X20_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000469 else if (Reg1 == AArch64::X21 && Reg2 == AArch64::X22 &&
470 (CompactUnwindEncoding & 0xF1C) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000471 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X21_X22_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000472 else if (Reg1 == AArch64::X23 && Reg2 == AArch64::X24 &&
473 (CompactUnwindEncoding & 0xF18) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000474 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X23_X24_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000475 else if (Reg1 == AArch64::X25 && Reg2 == AArch64::X26 &&
476 (CompactUnwindEncoding & 0xF10) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000477 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X25_X26_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000478 else if (Reg1 == AArch64::X27 && Reg2 == AArch64::X28 &&
479 (CompactUnwindEncoding & 0xF00) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000480 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X27_X28_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000481 else {
482 Reg1 = getDRegFromBReg(Reg1);
483 Reg2 = getDRegFromBReg(Reg2);
484
485 // D8/D9 pair = 0x00000100,
486 // D10/D11 pair = 0x00000200,
487 // D12/D13 pair = 0x00000400,
488 // D14/D15 pair = 0x00000800
489 if (Reg1 == AArch64::D8 && Reg2 == AArch64::D9 &&
490 (CompactUnwindEncoding & 0xE00) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000491 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D8_D9_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000492 else if (Reg1 == AArch64::D10 && Reg2 == AArch64::D11 &&
493 (CompactUnwindEncoding & 0xC00) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000494 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D10_D11_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000495 else if (Reg1 == AArch64::D12 && Reg2 == AArch64::D13 &&
496 (CompactUnwindEncoding & 0x800) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000497 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D12_D13_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000498 else if (Reg1 == AArch64::D14 && Reg2 == AArch64::D15)
Tim Northover87442c12016-02-23 21:49:05 +0000499 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D14_D15_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000500 else
501 // A pair was pushed which we cannot handle.
Tim Northover87442c12016-02-23 21:49:05 +0000502 return CU::UNWIND_ARM64_MODE_DWARF;
Tim Northover3b0846e2014-05-24 12:50:23 +0000503 }
504
505 break;
506 }
507 }
508 }
509
510 if (!HasFP) {
511 // With compact unwind info we can only represent stack adjustments of up
512 // to 65520 bytes.
513 if (StackSize > 65520)
Tim Northover87442c12016-02-23 21:49:05 +0000514 return CU::UNWIND_ARM64_MODE_DWARF;
Tim Northover3b0846e2014-05-24 12:50:23 +0000515
Tim Northover87442c12016-02-23 21:49:05 +0000516 CompactUnwindEncoding |= CU::UNWIND_ARM64_MODE_FRAMELESS;
Tim Northover3b0846e2014-05-24 12:50:23 +0000517 CompactUnwindEncoding |= encodeStackAdjustment(StackSize);
518 }
519
520 return CompactUnwindEncoding;
521 }
522};
523
524} // end anonymous namespace
525
526namespace {
527
528class ELFAArch64AsmBackend : public AArch64AsmBackend {
529public:
530 uint8_t OSABI;
Joel Jones504bf332016-10-24 13:37:13 +0000531 bool IsILP32;
Tim Northover3b0846e2014-05-24 12:50:23 +0000532
Joel Jones504bf332016-10-24 13:37:13 +0000533 ELFAArch64AsmBackend(const Target &T, uint8_t OSABI, bool IsLittleEndian,
534 bool IsILP32)
535 : AArch64AsmBackend(T, IsLittleEndian), OSABI(OSABI), IsILP32(IsILP32) {}
Tim Northover3b0846e2014-05-24 12:50:23 +0000536
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000537 MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
Joel Jones504bf332016-10-24 13:37:13 +0000538 return createAArch64ELFObjectWriter(OS, OSABI, IsLittleEndian, IsILP32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000539 }
540
541 void processFixupValue(const MCAssembler &Asm, const MCAsmLayout &Layout,
542 const MCFixup &Fixup, const MCFragment *DF,
543 const MCValue &Target, uint64_t &Value,
544 bool &IsResolved) override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000545};
546
547void ELFAArch64AsmBackend::processFixupValue(
548 const MCAssembler &Asm, const MCAsmLayout &Layout, const MCFixup &Fixup,
549 const MCFragment *DF, const MCValue &Target, uint64_t &Value,
550 bool &IsResolved) {
551 // The ADRP instruction adds some multiple of 0x1000 to the current PC &
552 // ~0xfff. This means that the required offset to reach a symbol can vary by
553 // up to one step depending on where the ADRP is in memory. For example:
554 //
555 // ADRP x0, there
556 // there:
557 //
558 // If the ADRP occurs at address 0xffc then "there" will be at 0x1000 and
559 // we'll need that as an offset. At any other address "there" will be in the
560 // same page as the ADRP and the instruction should encode 0x0. Assuming the
561 // section isn't 0x1000-aligned, we therefore need to delegate this decision
562 // to the linker -- a relocation!
563 if ((uint32_t)Fixup.getKind() == AArch64::fixup_aarch64_pcrel_adrp_imm21)
564 IsResolved = false;
565}
566
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000567}
Tim Northover3b0846e2014-05-24 12:50:23 +0000568
569MCAsmBackend *llvm::createAArch64leAsmBackend(const Target &T,
Daniel Sanders418caf52015-06-10 10:35:34 +0000570 const MCRegisterInfo &MRI,
Daniel Sanders50f17232015-09-15 16:17:27 +0000571 const Triple &TheTriple,
Joel Jones373d7d32016-07-25 17:18:28 +0000572 StringRef CPU,
573 const MCTargetOptions &Options) {
Daniel Sanders50f17232015-09-15 16:17:27 +0000574 if (TheTriple.isOSBinFormatMachO())
Tim Northover3b0846e2014-05-24 12:50:23 +0000575 return new DarwinAArch64AsmBackend(T, MRI);
576
Daniel Sanders50f17232015-09-15 16:17:27 +0000577 assert(TheTriple.isOSBinFormatELF() && "Expect either MachO or ELF target");
578 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TheTriple.getOS());
Joel Jones504bf332016-10-24 13:37:13 +0000579 bool IsILP32 = Options.getABIName() == "ilp32";
580 return new ELFAArch64AsmBackend(T, OSABI, /*IsLittleEndian=*/true, IsILP32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000581}
582
583MCAsmBackend *llvm::createAArch64beAsmBackend(const Target &T,
Daniel Sanders418caf52015-06-10 10:35:34 +0000584 const MCRegisterInfo &MRI,
Daniel Sanders50f17232015-09-15 16:17:27 +0000585 const Triple &TheTriple,
Joel Jones373d7d32016-07-25 17:18:28 +0000586 StringRef CPU,
587 const MCTargetOptions &Options) {
Daniel Sanders50f17232015-09-15 16:17:27 +0000588 assert(TheTriple.isOSBinFormatELF() &&
Tim Northover3b0846e2014-05-24 12:50:23 +0000589 "Big endian is only supported for ELF targets!");
Daniel Sanders50f17232015-09-15 16:17:27 +0000590 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TheTriple.getOS());
Joel Jones504bf332016-10-24 13:37:13 +0000591 bool IsILP32 = Options.getABIName() == "ilp32";
592 return new ELFAArch64AsmBackend(T, OSABI, /*IsLittleEndian=*/false, IsILP32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000593}