blob: b777a3e05befc84885d8c6ed9436b4a7a3b4f8cf [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"
Tim Northover3b0846e2014-05-24 12:50:23 +000014#include "llvm/MC/MCAsmBackend.h"
Oliver Stannarda5520b02016-04-01 09:14:50 +000015#include "llvm/MC/MCContext.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000016#include "llvm/MC/MCDirectives.h"
Chad Rosierafe7c932014-08-06 16:05:02 +000017#include "llvm/MC/MCELFObjectWriter.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000018#include "llvm/MC/MCFixupKindInfo.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000019#include "llvm/MC/MCObjectWriter.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000020#include "llvm/MC/MCSectionELF.h"
Benjamin Kramer1f8930e2014-07-25 11:42:14 +000021#include "llvm/MC/MCSectionMachO.h"
Peter Collingbournee8813e62015-03-24 21:47:03 +000022#include "llvm/MC/MCValue.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000023#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/MachO.h"
25using namespace llvm;
26
27namespace {
28
29class AArch64AsmBackend : public MCAsmBackend {
30 static const unsigned PCRelFlagVal =
31 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits | MCFixupKindInfo::FKF_IsPCRel;
Keith Walker8c44bf12016-01-20 15:59:14 +000032public:
33 bool IsLittleEndian;
Tim Northover3b0846e2014-05-24 12:50:23 +000034
35public:
Keith Walker8c44bf12016-01-20 15:59:14 +000036 AArch64AsmBackend(const Target &T, bool IsLittleEndian)
37 : MCAsmBackend(), IsLittleEndian(IsLittleEndian) {}
Tim Northover3b0846e2014-05-24 12:50:23 +000038
39 unsigned getNumFixupKinds() const override {
40 return AArch64::NumTargetFixupKinds;
41 }
42
43 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
44 const static MCFixupKindInfo Infos[AArch64::NumTargetFixupKinds] = {
45 // This table *must* be in the order that the fixup_* kinds are defined in
46 // AArch64FixupKinds.h.
47 //
48 // Name Offset (bits) Size (bits) Flags
49 { "fixup_aarch64_pcrel_adr_imm21", 0, 32, PCRelFlagVal },
50 { "fixup_aarch64_pcrel_adrp_imm21", 0, 32, PCRelFlagVal },
51 { "fixup_aarch64_add_imm12", 10, 12, 0 },
52 { "fixup_aarch64_ldst_imm12_scale1", 10, 12, 0 },
53 { "fixup_aarch64_ldst_imm12_scale2", 10, 12, 0 },
54 { "fixup_aarch64_ldst_imm12_scale4", 10, 12, 0 },
55 { "fixup_aarch64_ldst_imm12_scale8", 10, 12, 0 },
56 { "fixup_aarch64_ldst_imm12_scale16", 10, 12, 0 },
57 { "fixup_aarch64_ldr_pcrel_imm19", 5, 19, PCRelFlagVal },
58 { "fixup_aarch64_movw", 5, 16, 0 },
59 { "fixup_aarch64_pcrel_branch14", 5, 14, PCRelFlagVal },
60 { "fixup_aarch64_pcrel_branch19", 5, 19, PCRelFlagVal },
61 { "fixup_aarch64_pcrel_branch26", 0, 26, PCRelFlagVal },
62 { "fixup_aarch64_pcrel_call26", 0, 26, PCRelFlagVal },
63 { "fixup_aarch64_tlsdesc_call", 0, 0, 0 }
64 };
65
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,
75 uint64_t Value, bool IsPCRel) const override;
76
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,
140 MCContext *Ctx) {
141 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:
Oliver Stannarda5520b02016-04-01 09:14:50 +0000147 if (Ctx && (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)
Oliver Stannarda5520b02016-04-01 09:14:50 +0000156 if (Ctx) Ctx->reportError(Fixup.getLoc(), "fixup value out of range");
157 if (Ctx && (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
Oliver Stannarda5520b02016-04-01 09:14:50 +0000164 if (Ctx && 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
Oliver Stannarda5520b02016-04-01 09:14:50 +0000169 if (Ctx && (Value >= 0x2000))
170 Ctx->reportError(Fixup.getLoc(), "fixup value out of range");
171 if (Ctx && (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
Oliver Stannarda5520b02016-04-01 09:14:50 +0000176 if (Ctx && (Value >= 0x4000))
177 Ctx->reportError(Fixup.getLoc(), "fixup value out of range");
178 if (Ctx && (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
Oliver Stannarda5520b02016-04-01 09:14:50 +0000183 if (Ctx && (Value >= 0x8000))
184 Ctx->reportError(Fixup.getLoc(), "fixup value out of range");
185 if (Ctx && (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
Oliver Stannarda5520b02016-04-01 09:14:50 +0000190 if (Ctx && (Value >= 0x10000))
191 Ctx->reportError(Fixup.getLoc(), "fixup value out of range");
192 if (Ctx && (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:
Oliver Stannarda5520b02016-04-01 09:14:50 +0000196 if (Ctx)
197 Ctx->reportError(Fixup.getLoc(),
198 "no resolvable MOVZ/MOVK fixups supported yet");
Tim Northover3b0846e2014-05-24 12:50:23 +0000199 return Value;
200 case AArch64::fixup_aarch64_pcrel_branch14:
201 // Signed 16-bit immediate
Oliver Stannarda5520b02016-04-01 09:14:50 +0000202 if (Ctx && (SignedValue > 32767 || SignedValue < -32768))
203 Ctx->reportError(Fixup.getLoc(), "fixup value out of range");
Tim Northover3b0846e2014-05-24 12:50:23 +0000204 // Low two bits are not encoded (4-byte alignment assumed).
Oliver Stannarda5520b02016-04-01 09:14:50 +0000205 if (Ctx && (Value & 0x3))
206 Ctx->reportError(Fixup.getLoc(), "fixup not sufficiently aligned");
Tim Northover3b0846e2014-05-24 12:50:23 +0000207 return (Value >> 2) & 0x3fff;
208 case AArch64::fixup_aarch64_pcrel_branch26:
209 case AArch64::fixup_aarch64_pcrel_call26:
210 // Signed 28-bit immediate
Oliver Stannarda5520b02016-04-01 09:14:50 +0000211 if (Ctx && (SignedValue > 134217727 || SignedValue < -134217728))
212 Ctx->reportError(Fixup.getLoc(), "fixup value out of range");
Tim Northover3b0846e2014-05-24 12:50:23 +0000213 // Low two bits are not encoded (4-byte alignment assumed).
Oliver Stannarda5520b02016-04-01 09:14:50 +0000214 if (Ctx && (Value & 0x3))
215 Ctx->reportError(Fixup.getLoc(), "fixup not sufficiently aligned");
Tim Northover3b0846e2014-05-24 12:50:23 +0000216 return (Value >> 2) & 0x3ffffff;
217 case FK_Data_1:
218 case FK_Data_2:
219 case FK_Data_4:
220 case FK_Data_8:
221 return Value;
222 }
223}
224
Keith Walker8c44bf12016-01-20 15:59:14 +0000225/// getFixupKindContainereSizeInBytes - The number of bytes of the
226/// container involved in big endian or 0 if the item is little endian
227unsigned AArch64AsmBackend::getFixupKindContainereSizeInBytes(unsigned Kind) const {
228 if (IsLittleEndian)
229 return 0;
230
231 switch (Kind) {
232 default:
233 llvm_unreachable("Unknown fixup kind!");
234
235 case FK_Data_1:
236 return 1;
237 case FK_Data_2:
238 return 2;
239 case FK_Data_4:
240 return 4;
241 case FK_Data_8:
242 return 8;
243
244 case AArch64::fixup_aarch64_tlsdesc_call:
245 case AArch64::fixup_aarch64_movw:
246 case AArch64::fixup_aarch64_pcrel_branch14:
247 case AArch64::fixup_aarch64_add_imm12:
248 case AArch64::fixup_aarch64_ldst_imm12_scale1:
249 case AArch64::fixup_aarch64_ldst_imm12_scale2:
250 case AArch64::fixup_aarch64_ldst_imm12_scale4:
251 case AArch64::fixup_aarch64_ldst_imm12_scale8:
252 case AArch64::fixup_aarch64_ldst_imm12_scale16:
253 case AArch64::fixup_aarch64_ldr_pcrel_imm19:
254 case AArch64::fixup_aarch64_pcrel_branch19:
255 case AArch64::fixup_aarch64_pcrel_adr_imm21:
256 case AArch64::fixup_aarch64_pcrel_adrp_imm21:
257 case AArch64::fixup_aarch64_pcrel_branch26:
258 case AArch64::fixup_aarch64_pcrel_call26:
259 // Instructions are always little endian
260 return 0;
261 }
262}
263
Tim Northover3b0846e2014-05-24 12:50:23 +0000264void AArch64AsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
265 unsigned DataSize, uint64_t Value,
266 bool IsPCRel) const {
267 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
268 if (!Value)
269 return; // Doesn't change encoding.
270 MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
271 // Apply any target-specific value adjustments.
Oliver Stannarda5520b02016-04-01 09:14:50 +0000272 Value = adjustFixupValue(Fixup, Value, nullptr);
Tim Northover3b0846e2014-05-24 12:50:23 +0000273
274 // Shift the value into position.
275 Value <<= Info.TargetOffset;
276
277 unsigned Offset = Fixup.getOffset();
278 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
279
Keith Walker8c44bf12016-01-20 15:59:14 +0000280 // Used to point to big endian bytes.
281 unsigned FulleSizeInBytes = getFixupKindContainereSizeInBytes(Fixup.getKind());
282
Tim Northover3b0846e2014-05-24 12:50:23 +0000283 // For each byte of the fragment that the fixup touches, mask in the
284 // bits from the fixup value.
Keith Walker8c44bf12016-01-20 15:59:14 +0000285 if (FulleSizeInBytes == 0) {
286 // Handle as little-endian
287 for (unsigned i = 0; i != NumBytes; ++i) {
288 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
289 }
290 } else {
291 // Handle as big-endian
292 assert((Offset + FulleSizeInBytes) <= DataSize && "Invalid fixup size!");
293 assert(NumBytes <= FulleSizeInBytes && "Invalid fixup size!");
294 for (unsigned i = 0; i != NumBytes; ++i) {
295 unsigned Idx = FulleSizeInBytes - 1 - i;
296 Data[Offset + Idx] |= uint8_t((Value >> (i * 8)) & 0xff);
297 }
298 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000299}
300
301bool AArch64AsmBackend::mayNeedRelaxation(const MCInst &Inst) const {
302 return false;
303}
304
305bool AArch64AsmBackend::fixupNeedsRelaxation(const MCFixup &Fixup,
306 uint64_t Value,
307 const MCRelaxableFragment *DF,
308 const MCAsmLayout &Layout) const {
309 // FIXME: This isn't correct for AArch64. Just moving the "generic" logic
310 // into the targets for now.
311 //
312 // Relax if the value is too big for a (signed) i8.
313 return int64_t(Value) != int64_t(int8_t(Value));
314}
315
316void AArch64AsmBackend::relaxInstruction(const MCInst &Inst,
Nirav Dave86030622016-07-11 14:23:53 +0000317 const MCSubtargetInfo &STI,
Tim Northover3b0846e2014-05-24 12:50:23 +0000318 MCInst &Res) const {
Craig Topperd3c02f12015-01-05 10:15:49 +0000319 llvm_unreachable("AArch64AsmBackend::relaxInstruction() unimplemented");
Tim Northover3b0846e2014-05-24 12:50:23 +0000320}
321
322bool AArch64AsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
323 // If the count is not 4-byte aligned, we must be writing data into the text
324 // section (otherwise we have unaligned instructions, and thus have far
325 // bigger problems), so just write zeros instead.
Benjamin Kramer97fbdd52015-04-17 11:12:43 +0000326 OW->WriteZeros(Count % 4);
Tim Northover3b0846e2014-05-24 12:50:23 +0000327
328 // We are properly aligned, so write NOPs as requested.
329 Count /= 4;
330 for (uint64_t i = 0; i != Count; ++i)
Jim Grosbach36e60e92015-06-04 22:24:41 +0000331 OW->write32(0xd503201f);
Tim Northover3b0846e2014-05-24 12:50:23 +0000332 return true;
333}
334
335namespace {
336
337namespace CU {
338
339/// \brief Compact unwind encoding values.
340enum CompactUnwindEncodings {
341 /// \brief A "frameless" leaf function, where no non-volatile registers are
342 /// saved. The return remains in LR throughout the function.
Tim Northover87442c12016-02-23 21:49:05 +0000343 UNWIND_ARM64_MODE_FRAMELESS = 0x02000000,
Tim Northover3b0846e2014-05-24 12:50:23 +0000344
345 /// \brief No compact unwind encoding available. Instead the low 23-bits of
346 /// the compact unwind encoding is the offset of the DWARF FDE in the
347 /// __eh_frame section. This mode is never used in object files. It is only
348 /// generated by the linker in final linked images, which have only DWARF info
349 /// for a function.
Tim Northover87442c12016-02-23 21:49:05 +0000350 UNWIND_ARM64_MODE_DWARF = 0x03000000,
Tim Northover3b0846e2014-05-24 12:50:23 +0000351
352 /// \brief This is a standard arm64 prologue where FP/LR are immediately
353 /// pushed on the stack, then SP is copied to FP. If there are any
354 /// non-volatile register saved, they are copied into the stack fame in pairs
355 /// in a contiguous ranger right below the saved FP/LR pair. Any subset of the
356 /// five X pairs and four D pairs can be saved, but the memory layout must be
357 /// in register number order.
Tim Northover87442c12016-02-23 21:49:05 +0000358 UNWIND_ARM64_MODE_FRAME = 0x04000000,
Tim Northover3b0846e2014-05-24 12:50:23 +0000359
360 /// \brief Frame register pair encodings.
Tim Northover87442c12016-02-23 21:49:05 +0000361 UNWIND_ARM64_FRAME_X19_X20_PAIR = 0x00000001,
362 UNWIND_ARM64_FRAME_X21_X22_PAIR = 0x00000002,
363 UNWIND_ARM64_FRAME_X23_X24_PAIR = 0x00000004,
364 UNWIND_ARM64_FRAME_X25_X26_PAIR = 0x00000008,
365 UNWIND_ARM64_FRAME_X27_X28_PAIR = 0x00000010,
366 UNWIND_ARM64_FRAME_D8_D9_PAIR = 0x00000100,
367 UNWIND_ARM64_FRAME_D10_D11_PAIR = 0x00000200,
368 UNWIND_ARM64_FRAME_D12_D13_PAIR = 0x00000400,
369 UNWIND_ARM64_FRAME_D14_D15_PAIR = 0x00000800
Tim Northover3b0846e2014-05-24 12:50:23 +0000370};
371
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000372} // end CU namespace
Tim Northover3b0846e2014-05-24 12:50:23 +0000373
374// FIXME: This should be in a separate file.
375class DarwinAArch64AsmBackend : public AArch64AsmBackend {
376 const MCRegisterInfo &MRI;
377
378 /// \brief Encode compact unwind stack adjustment for frameless functions.
Tim Northover87442c12016-02-23 21:49:05 +0000379 /// See UNWIND_ARM64_FRAMELESS_STACK_SIZE_MASK in compact_unwind_encoding.h.
Tim Northover3b0846e2014-05-24 12:50:23 +0000380 /// The stack size always needs to be 16 byte aligned.
381 uint32_t encodeStackAdjustment(uint32_t StackSize) const {
382 return (StackSize / 16) << 12;
383 }
384
385public:
386 DarwinAArch64AsmBackend(const Target &T, const MCRegisterInfo &MRI)
Keith Walker8c44bf12016-01-20 15:59:14 +0000387 : AArch64AsmBackend(T, /*IsLittleEndian*/true), MRI(MRI) {}
Tim Northover3b0846e2014-05-24 12:50:23 +0000388
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000389 MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
Tim Northover3b0846e2014-05-24 12:50:23 +0000390 return createAArch64MachObjectWriter(OS, MachO::CPU_TYPE_ARM64,
391 MachO::CPU_SUBTYPE_ARM64_ALL);
392 }
393
Tim Northover3b0846e2014-05-24 12:50:23 +0000394 /// \brief Generate the compact unwind encoding from the CFI directives.
395 uint32_t generateCompactUnwindEncoding(
396 ArrayRef<MCCFIInstruction> Instrs) const override {
397 if (Instrs.empty())
Tim Northover87442c12016-02-23 21:49:05 +0000398 return CU::UNWIND_ARM64_MODE_FRAMELESS;
Tim Northover3b0846e2014-05-24 12:50:23 +0000399
400 bool HasFP = false;
401 unsigned StackSize = 0;
402
403 uint32_t CompactUnwindEncoding = 0;
404 for (size_t i = 0, e = Instrs.size(); i != e; ++i) {
405 const MCCFIInstruction &Inst = Instrs[i];
406
407 switch (Inst.getOperation()) {
408 default:
409 // Cannot handle this directive: bail out.
Tim Northover87442c12016-02-23 21:49:05 +0000410 return CU::UNWIND_ARM64_MODE_DWARF;
Tim Northover3b0846e2014-05-24 12:50:23 +0000411 case MCCFIInstruction::OpDefCfa: {
412 // Defines a frame pointer.
413 assert(getXRegFromWReg(MRI.getLLVMRegNum(Inst.getRegister(), true)) ==
414 AArch64::FP &&
415 "Invalid frame pointer!");
416 assert(i + 2 < e && "Insufficient CFI instructions to define a frame!");
417
418 const MCCFIInstruction &LRPush = Instrs[++i];
419 assert(LRPush.getOperation() == MCCFIInstruction::OpOffset &&
420 "Link register not pushed!");
421 const MCCFIInstruction &FPPush = Instrs[++i];
422 assert(FPPush.getOperation() == MCCFIInstruction::OpOffset &&
423 "Frame pointer not pushed!");
424
425 unsigned LRReg = MRI.getLLVMRegNum(LRPush.getRegister(), true);
426 unsigned FPReg = MRI.getLLVMRegNum(FPPush.getRegister(), true);
427
428 LRReg = getXRegFromWReg(LRReg);
429 FPReg = getXRegFromWReg(FPReg);
430
431 assert(LRReg == AArch64::LR && FPReg == AArch64::FP &&
432 "Pushing invalid registers for frame!");
433
434 // Indicate that the function has a frame.
Tim Northover87442c12016-02-23 21:49:05 +0000435 CompactUnwindEncoding |= CU::UNWIND_ARM64_MODE_FRAME;
Tim Northover3b0846e2014-05-24 12:50:23 +0000436 HasFP = true;
437 break;
438 }
439 case MCCFIInstruction::OpDefCfaOffset: {
440 assert(StackSize == 0 && "We already have the CFA offset!");
441 StackSize = std::abs(Inst.getOffset());
442 break;
443 }
444 case MCCFIInstruction::OpOffset: {
445 // Registers are saved in pairs. We expect there to be two consecutive
446 // `.cfi_offset' instructions with the appropriate registers specified.
447 unsigned Reg1 = MRI.getLLVMRegNum(Inst.getRegister(), true);
448 if (i + 1 == e)
Tim Northover87442c12016-02-23 21:49:05 +0000449 return CU::UNWIND_ARM64_MODE_DWARF;
Tim Northover3b0846e2014-05-24 12:50:23 +0000450
451 const MCCFIInstruction &Inst2 = Instrs[++i];
452 if (Inst2.getOperation() != MCCFIInstruction::OpOffset)
Tim Northover87442c12016-02-23 21:49:05 +0000453 return CU::UNWIND_ARM64_MODE_DWARF;
Tim Northover3b0846e2014-05-24 12:50:23 +0000454 unsigned Reg2 = MRI.getLLVMRegNum(Inst2.getRegister(), true);
455
456 // N.B. The encodings must be in register number order, and the X
457 // registers before the D registers.
458
459 // X19/X20 pair = 0x00000001,
460 // X21/X22 pair = 0x00000002,
461 // X23/X24 pair = 0x00000004,
462 // X25/X26 pair = 0x00000008,
463 // X27/X28 pair = 0x00000010
464 Reg1 = getXRegFromWReg(Reg1);
465 Reg2 = getXRegFromWReg(Reg2);
466
467 if (Reg1 == AArch64::X19 && Reg2 == AArch64::X20 &&
468 (CompactUnwindEncoding & 0xF1E) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000469 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X19_X20_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000470 else if (Reg1 == AArch64::X21 && Reg2 == AArch64::X22 &&
471 (CompactUnwindEncoding & 0xF1C) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000472 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X21_X22_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000473 else if (Reg1 == AArch64::X23 && Reg2 == AArch64::X24 &&
474 (CompactUnwindEncoding & 0xF18) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000475 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X23_X24_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000476 else if (Reg1 == AArch64::X25 && Reg2 == AArch64::X26 &&
477 (CompactUnwindEncoding & 0xF10) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000478 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X25_X26_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000479 else if (Reg1 == AArch64::X27 && Reg2 == AArch64::X28 &&
480 (CompactUnwindEncoding & 0xF00) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000481 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X27_X28_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000482 else {
483 Reg1 = getDRegFromBReg(Reg1);
484 Reg2 = getDRegFromBReg(Reg2);
485
486 // D8/D9 pair = 0x00000100,
487 // D10/D11 pair = 0x00000200,
488 // D12/D13 pair = 0x00000400,
489 // D14/D15 pair = 0x00000800
490 if (Reg1 == AArch64::D8 && Reg2 == AArch64::D9 &&
491 (CompactUnwindEncoding & 0xE00) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000492 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D8_D9_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000493 else if (Reg1 == AArch64::D10 && Reg2 == AArch64::D11 &&
494 (CompactUnwindEncoding & 0xC00) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000495 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D10_D11_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000496 else if (Reg1 == AArch64::D12 && Reg2 == AArch64::D13 &&
497 (CompactUnwindEncoding & 0x800) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000498 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D12_D13_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000499 else if (Reg1 == AArch64::D14 && Reg2 == AArch64::D15)
Tim Northover87442c12016-02-23 21:49:05 +0000500 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D14_D15_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000501 else
502 // A pair was pushed which we cannot handle.
Tim Northover87442c12016-02-23 21:49:05 +0000503 return CU::UNWIND_ARM64_MODE_DWARF;
Tim Northover3b0846e2014-05-24 12:50:23 +0000504 }
505
506 break;
507 }
508 }
509 }
510
511 if (!HasFP) {
512 // With compact unwind info we can only represent stack adjustments of up
513 // to 65520 bytes.
514 if (StackSize > 65520)
Tim Northover87442c12016-02-23 21:49:05 +0000515 return CU::UNWIND_ARM64_MODE_DWARF;
Tim Northover3b0846e2014-05-24 12:50:23 +0000516
Tim Northover87442c12016-02-23 21:49:05 +0000517 CompactUnwindEncoding |= CU::UNWIND_ARM64_MODE_FRAMELESS;
Tim Northover3b0846e2014-05-24 12:50:23 +0000518 CompactUnwindEncoding |= encodeStackAdjustment(StackSize);
519 }
520
521 return CompactUnwindEncoding;
522 }
523};
524
525} // end anonymous namespace
526
527namespace {
528
529class ELFAArch64AsmBackend : public AArch64AsmBackend {
530public:
531 uint8_t OSABI;
Tim Northover3b0846e2014-05-24 12:50:23 +0000532
533 ELFAArch64AsmBackend(const Target &T, uint8_t OSABI, bool IsLittleEndian)
Keith Walker8c44bf12016-01-20 15:59:14 +0000534 : AArch64AsmBackend(T, IsLittleEndian), OSABI(OSABI) {}
Tim Northover3b0846e2014-05-24 12:50:23 +0000535
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000536 MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
Tim Northover3b0846e2014-05-24 12:50:23 +0000537 return createAArch64ELFObjectWriter(OS, OSABI, IsLittleEndian);
538 }
539
540 void processFixupValue(const MCAssembler &Asm, const MCAsmLayout &Layout,
541 const MCFixup &Fixup, const MCFragment *DF,
542 const MCValue &Target, uint64_t &Value,
543 bool &IsResolved) override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000544};
545
546void ELFAArch64AsmBackend::processFixupValue(
547 const MCAssembler &Asm, const MCAsmLayout &Layout, const MCFixup &Fixup,
548 const MCFragment *DF, const MCValue &Target, uint64_t &Value,
549 bool &IsResolved) {
550 // The ADRP instruction adds some multiple of 0x1000 to the current PC &
551 // ~0xfff. This means that the required offset to reach a symbol can vary by
552 // up to one step depending on where the ADRP is in memory. For example:
553 //
554 // ADRP x0, there
555 // there:
556 //
557 // If the ADRP occurs at address 0xffc then "there" will be at 0x1000 and
558 // we'll need that as an offset. At any other address "there" will be in the
559 // same page as the ADRP and the instruction should encode 0x0. Assuming the
560 // section isn't 0x1000-aligned, we therefore need to delegate this decision
561 // to the linker -- a relocation!
562 if ((uint32_t)Fixup.getKind() == AArch64::fixup_aarch64_pcrel_adrp_imm21)
563 IsResolved = false;
Oliver Stannarda5520b02016-04-01 09:14:50 +0000564
565 // Try to get the encoded value for the fixup as-if we're mapping it into
566 // the instruction. This allows adjustFixupValue() to issue a diagnostic
567 // if the value is invalid.
568 if (IsResolved)
569 (void)adjustFixupValue(Fixup, Value, &Asm.getContext());
Tim Northover3b0846e2014-05-24 12:50:23 +0000570}
571
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000572}
Tim Northover3b0846e2014-05-24 12:50:23 +0000573
574MCAsmBackend *llvm::createAArch64leAsmBackend(const Target &T,
Daniel Sanders418caf52015-06-10 10:35:34 +0000575 const MCRegisterInfo &MRI,
Daniel Sanders50f17232015-09-15 16:17:27 +0000576 const Triple &TheTriple,
Joel Jones373d7d32016-07-25 17:18:28 +0000577 StringRef CPU,
578 const MCTargetOptions &Options) {
Daniel Sanders50f17232015-09-15 16:17:27 +0000579 if (TheTriple.isOSBinFormatMachO())
Tim Northover3b0846e2014-05-24 12:50:23 +0000580 return new DarwinAArch64AsmBackend(T, MRI);
581
Daniel Sanders50f17232015-09-15 16:17:27 +0000582 assert(TheTriple.isOSBinFormatELF() && "Expect either MachO or ELF target");
583 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TheTriple.getOS());
Chad Rosierafe7c932014-08-06 16:05:02 +0000584 return new ELFAArch64AsmBackend(T, OSABI, /*IsLittleEndian=*/true);
Tim Northover3b0846e2014-05-24 12:50:23 +0000585}
586
587MCAsmBackend *llvm::createAArch64beAsmBackend(const Target &T,
Daniel Sanders418caf52015-06-10 10:35:34 +0000588 const MCRegisterInfo &MRI,
Daniel Sanders50f17232015-09-15 16:17:27 +0000589 const Triple &TheTriple,
Joel Jones373d7d32016-07-25 17:18:28 +0000590 StringRef CPU,
591 const MCTargetOptions &Options) {
Daniel Sanders50f17232015-09-15 16:17:27 +0000592 assert(TheTriple.isOSBinFormatELF() &&
Tim Northover3b0846e2014-05-24 12:50:23 +0000593 "Big endian is only supported for ELF targets!");
Daniel Sanders50f17232015-09-15 16:17:27 +0000594 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TheTriple.getOS());
Joel Jones373d7d32016-07-25 17:18:28 +0000595 return new ELFAArch64AsmBackend(T, OSABI, /*IsLittleEndian=*/false);
Tim Northover3b0846e2014-05-24 12:50:23 +0000596}