blob: 95aeb3e0f595ac8287c7431b3eba7859a1f97100 [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
Rafael Espindola801b42d2017-06-23 22:52:36 +000074 void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
75 const MCValue &Target, MutableArrayRef<char> Data,
Alex Bradbury866113c2017-04-05 10:16:14 +000076 uint64_t Value, bool IsPCRel, MCContext &Ctx) const override;
Tim Northover3b0846e2014-05-24 12:50:23 +000077
78 bool mayNeedRelaxation(const MCInst &Inst) const override;
79 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
80 const MCRelaxableFragment *DF,
81 const MCAsmLayout &Layout) const override;
Nirav Dave86030622016-07-11 14:23:53 +000082 void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
83 MCInst &Res) const override;
Tim Northover3b0846e2014-05-24 12:50:23 +000084 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
85
86 void HandleAssemblerFlag(MCAssemblerFlag Flag) {}
87
88 unsigned getPointerSize() const { return 8; }
Keith Walker8c44bf12016-01-20 15:59:14 +000089
90 unsigned getFixupKindContainereSizeInBytes(unsigned Kind) const;
Tim Northover3b0846e2014-05-24 12:50:23 +000091};
92
93} // end anonymous namespace
94
95/// \brief The number of bytes the fixup may change.
96static unsigned getFixupKindNumBytes(unsigned Kind) {
97 switch (Kind) {
98 default:
Craig Topper2a30d782014-06-18 05:05:13 +000099 llvm_unreachable("Unknown fixup kind!");
Tim Northover3b0846e2014-05-24 12:50:23 +0000100
101 case AArch64::fixup_aarch64_tlsdesc_call:
102 return 0;
103
104 case FK_Data_1:
105 return 1;
106
107 case FK_Data_2:
108 case AArch64::fixup_aarch64_movw:
109 return 2;
110
111 case AArch64::fixup_aarch64_pcrel_branch14:
112 case AArch64::fixup_aarch64_add_imm12:
113 case AArch64::fixup_aarch64_ldst_imm12_scale1:
114 case AArch64::fixup_aarch64_ldst_imm12_scale2:
115 case AArch64::fixup_aarch64_ldst_imm12_scale4:
116 case AArch64::fixup_aarch64_ldst_imm12_scale8:
117 case AArch64::fixup_aarch64_ldst_imm12_scale16:
118 case AArch64::fixup_aarch64_ldr_pcrel_imm19:
119 case AArch64::fixup_aarch64_pcrel_branch19:
120 return 3;
121
122 case AArch64::fixup_aarch64_pcrel_adr_imm21:
123 case AArch64::fixup_aarch64_pcrel_adrp_imm21:
124 case AArch64::fixup_aarch64_pcrel_branch26:
125 case AArch64::fixup_aarch64_pcrel_call26:
126 case FK_Data_4:
127 return 4;
128
129 case FK_Data_8:
130 return 8;
131 }
132}
133
134static unsigned AdrImmBits(unsigned Value) {
135 unsigned lo2 = Value & 0x3;
136 unsigned hi19 = (Value & 0x1ffffc) >> 2;
137 return (hi19 << 5) | (lo2 << 29);
138}
139
Oliver Stannarda5520b02016-04-01 09:14:50 +0000140static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
Alex Bradbury866113c2017-04-05 10:16:14 +0000141 MCContext &Ctx) {
Oliver Stannarda5520b02016-04-01 09:14:50 +0000142 unsigned Kind = Fixup.getKind();
Tim Northover3b0846e2014-05-24 12:50:23 +0000143 int64_t SignedValue = static_cast<int64_t>(Value);
144 switch (Kind) {
145 default:
Craig Topperd3c02f12015-01-05 10:15:49 +0000146 llvm_unreachable("Unknown fixup kind!");
Tim Northover3b0846e2014-05-24 12:50:23 +0000147 case AArch64::fixup_aarch64_pcrel_adr_imm21:
Alex Bradbury866113c2017-04-05 10:16:14 +0000148 if (SignedValue > 2097151 || SignedValue < -2097152)
149 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
Tim Northover3b0846e2014-05-24 12:50:23 +0000150 return AdrImmBits(Value & 0x1fffffULL);
151 case AArch64::fixup_aarch64_pcrel_adrp_imm21:
152 return AdrImmBits((Value & 0x1fffff000ULL) >> 12);
153 case AArch64::fixup_aarch64_ldr_pcrel_imm19:
154 case AArch64::fixup_aarch64_pcrel_branch19:
155 // Signed 21-bit immediate
156 if (SignedValue > 2097151 || SignedValue < -2097152)
Alex Bradbury866113c2017-04-05 10:16:14 +0000157 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
158 if (Value & 0x3)
159 Ctx.reportError(Fixup.getLoc(), "fixup not sufficiently aligned");
Tim Northover3b0846e2014-05-24 12:50:23 +0000160 // Low two bits are not encoded.
161 return (Value >> 2) & 0x7ffff;
162 case AArch64::fixup_aarch64_add_imm12:
163 case AArch64::fixup_aarch64_ldst_imm12_scale1:
164 // Unsigned 12-bit immediate
Alex Bradbury866113c2017-04-05 10:16:14 +0000165 if (Value >= 0x1000)
166 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
Tim Northover3b0846e2014-05-24 12:50:23 +0000167 return Value;
168 case AArch64::fixup_aarch64_ldst_imm12_scale2:
169 // Unsigned 12-bit immediate which gets multiplied by 2
Alex Bradbury866113c2017-04-05 10:16:14 +0000170 if (Value >= 0x2000)
171 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
172 if (Value & 0x1)
173 Ctx.reportError(Fixup.getLoc(), "fixup must be 2-byte aligned");
Tim Northover3b0846e2014-05-24 12:50:23 +0000174 return Value >> 1;
175 case AArch64::fixup_aarch64_ldst_imm12_scale4:
176 // Unsigned 12-bit immediate which gets multiplied by 4
Alex Bradbury866113c2017-04-05 10:16:14 +0000177 if (Value >= 0x4000)
178 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
179 if (Value & 0x3)
180 Ctx.reportError(Fixup.getLoc(), "fixup must be 4-byte aligned");
Tim Northover3b0846e2014-05-24 12:50:23 +0000181 return Value >> 2;
182 case AArch64::fixup_aarch64_ldst_imm12_scale8:
183 // Unsigned 12-bit immediate which gets multiplied by 8
Alex Bradbury866113c2017-04-05 10:16:14 +0000184 if (Value >= 0x8000)
185 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
186 if (Value & 0x7)
187 Ctx.reportError(Fixup.getLoc(), "fixup must be 8-byte aligned");
Tim Northover3b0846e2014-05-24 12:50:23 +0000188 return Value >> 3;
189 case AArch64::fixup_aarch64_ldst_imm12_scale16:
190 // Unsigned 12-bit immediate which gets multiplied by 16
Alex Bradbury866113c2017-04-05 10:16:14 +0000191 if (Value >= 0x10000)
192 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
193 if (Value & 0xf)
194 Ctx.reportError(Fixup.getLoc(), "fixup must be 16-byte aligned");
Tim Northover3b0846e2014-05-24 12:50:23 +0000195 return Value >> 4;
196 case AArch64::fixup_aarch64_movw:
Alex Bradbury866113c2017-04-05 10:16:14 +0000197 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
Alex Bradbury866113c2017-04-05 10:16:14 +0000202 if (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).
Alex Bradbury866113c2017-04-05 10:16:14 +0000205 if (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
Alex Bradbury866113c2017-04-05 10:16:14 +0000211 if (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).
Alex Bradbury866113c2017-04-05 10:16:14 +0000214 if (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
Rafael Espindola801b42d2017-06-23 22:52:36 +0000264void AArch64AsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
265 const MCValue &Target,
Rafael Espindola88d9e372017-06-21 23:06:53 +0000266 MutableArrayRef<char> Data, uint64_t Value,
Alex Bradbury866113c2017-04-05 10:16:14 +0000267 bool IsPCRel, MCContext &Ctx) const {
Tim Northover3b0846e2014-05-24 12:50:23 +0000268 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
269 if (!Value)
270 return; // Doesn't change encoding.
271 MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
272 // Apply any target-specific value adjustments.
Alex Bradbury866113c2017-04-05 10:16:14 +0000273 Value = adjustFixupValue(Fixup, Value, Ctx);
Tim Northover3b0846e2014-05-24 12:50:23 +0000274
275 // Shift the value into position.
276 Value <<= Info.TargetOffset;
277
278 unsigned Offset = Fixup.getOffset();
Rafael Espindola88d9e372017-06-21 23:06:53 +0000279 assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
Tim Northover3b0846e2014-05-24 12:50:23 +0000280
Keith Walker8c44bf12016-01-20 15:59:14 +0000281 // Used to point to big endian bytes.
282 unsigned FulleSizeInBytes = getFixupKindContainereSizeInBytes(Fixup.getKind());
283
Tim Northover3b0846e2014-05-24 12:50:23 +0000284 // For each byte of the fragment that the fixup touches, mask in the
285 // bits from the fixup value.
Keith Walker8c44bf12016-01-20 15:59:14 +0000286 if (FulleSizeInBytes == 0) {
287 // Handle as little-endian
288 for (unsigned i = 0; i != NumBytes; ++i) {
289 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
290 }
291 } else {
292 // Handle as big-endian
Rafael Espindola88d9e372017-06-21 23:06:53 +0000293 assert((Offset + FulleSizeInBytes) <= Data.size() && "Invalid fixup size!");
Keith Walker8c44bf12016-01-20 15:59:14 +0000294 assert(NumBytes <= FulleSizeInBytes && "Invalid fixup size!");
295 for (unsigned i = 0; i != NumBytes; ++i) {
296 unsigned Idx = FulleSizeInBytes - 1 - i;
297 Data[Offset + Idx] |= uint8_t((Value >> (i * 8)) & 0xff);
298 }
299 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000300}
301
302bool AArch64AsmBackend::mayNeedRelaxation(const MCInst &Inst) const {
303 return false;
304}
305
306bool AArch64AsmBackend::fixupNeedsRelaxation(const MCFixup &Fixup,
307 uint64_t Value,
308 const MCRelaxableFragment *DF,
309 const MCAsmLayout &Layout) const {
310 // FIXME: This isn't correct for AArch64. Just moving the "generic" logic
311 // into the targets for now.
312 //
313 // Relax if the value is too big for a (signed) i8.
314 return int64_t(Value) != int64_t(int8_t(Value));
315}
316
317void AArch64AsmBackend::relaxInstruction(const MCInst &Inst,
Nirav Dave86030622016-07-11 14:23:53 +0000318 const MCSubtargetInfo &STI,
Tim Northover3b0846e2014-05-24 12:50:23 +0000319 MCInst &Res) const {
Craig Topperd3c02f12015-01-05 10:15:49 +0000320 llvm_unreachable("AArch64AsmBackend::relaxInstruction() unimplemented");
Tim Northover3b0846e2014-05-24 12:50:23 +0000321}
322
323bool AArch64AsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
324 // If the count is not 4-byte aligned, we must be writing data into the text
325 // section (otherwise we have unaligned instructions, and thus have far
326 // bigger problems), so just write zeros instead.
Benjamin Kramer97fbdd52015-04-17 11:12:43 +0000327 OW->WriteZeros(Count % 4);
Tim Northover3b0846e2014-05-24 12:50:23 +0000328
329 // We are properly aligned, so write NOPs as requested.
330 Count /= 4;
331 for (uint64_t i = 0; i != Count; ++i)
Jim Grosbach36e60e92015-06-04 22:24:41 +0000332 OW->write32(0xd503201f);
Tim Northover3b0846e2014-05-24 12:50:23 +0000333 return true;
334}
335
336namespace {
337
338namespace CU {
339
340/// \brief Compact unwind encoding values.
341enum CompactUnwindEncodings {
342 /// \brief A "frameless" leaf function, where no non-volatile registers are
343 /// saved. The return remains in LR throughout the function.
Tim Northover87442c12016-02-23 21:49:05 +0000344 UNWIND_ARM64_MODE_FRAMELESS = 0x02000000,
Tim Northover3b0846e2014-05-24 12:50:23 +0000345
346 /// \brief No compact unwind encoding available. Instead the low 23-bits of
347 /// the compact unwind encoding is the offset of the DWARF FDE in the
348 /// __eh_frame section. This mode is never used in object files. It is only
349 /// generated by the linker in final linked images, which have only DWARF info
350 /// for a function.
Tim Northover87442c12016-02-23 21:49:05 +0000351 UNWIND_ARM64_MODE_DWARF = 0x03000000,
Tim Northover3b0846e2014-05-24 12:50:23 +0000352
353 /// \brief This is a standard arm64 prologue where FP/LR are immediately
354 /// pushed on the stack, then SP is copied to FP. If there are any
355 /// non-volatile register saved, they are copied into the stack fame in pairs
356 /// in a contiguous ranger right below the saved FP/LR pair. Any subset of the
357 /// five X pairs and four D pairs can be saved, but the memory layout must be
358 /// in register number order.
Tim Northover87442c12016-02-23 21:49:05 +0000359 UNWIND_ARM64_MODE_FRAME = 0x04000000,
Tim Northover3b0846e2014-05-24 12:50:23 +0000360
361 /// \brief Frame register pair encodings.
Tim Northover87442c12016-02-23 21:49:05 +0000362 UNWIND_ARM64_FRAME_X19_X20_PAIR = 0x00000001,
363 UNWIND_ARM64_FRAME_X21_X22_PAIR = 0x00000002,
364 UNWIND_ARM64_FRAME_X23_X24_PAIR = 0x00000004,
365 UNWIND_ARM64_FRAME_X25_X26_PAIR = 0x00000008,
366 UNWIND_ARM64_FRAME_X27_X28_PAIR = 0x00000010,
367 UNWIND_ARM64_FRAME_D8_D9_PAIR = 0x00000100,
368 UNWIND_ARM64_FRAME_D10_D11_PAIR = 0x00000200,
369 UNWIND_ARM64_FRAME_D12_D13_PAIR = 0x00000400,
370 UNWIND_ARM64_FRAME_D14_D15_PAIR = 0x00000800
Tim Northover3b0846e2014-05-24 12:50:23 +0000371};
372
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000373} // end CU namespace
Tim Northover3b0846e2014-05-24 12:50:23 +0000374
375// FIXME: This should be in a separate file.
376class DarwinAArch64AsmBackend : public AArch64AsmBackend {
377 const MCRegisterInfo &MRI;
378
379 /// \brief Encode compact unwind stack adjustment for frameless functions.
Tim Northover87442c12016-02-23 21:49:05 +0000380 /// See UNWIND_ARM64_FRAMELESS_STACK_SIZE_MASK in compact_unwind_encoding.h.
Tim Northover3b0846e2014-05-24 12:50:23 +0000381 /// The stack size always needs to be 16 byte aligned.
382 uint32_t encodeStackAdjustment(uint32_t StackSize) const {
383 return (StackSize / 16) << 12;
384 }
385
386public:
387 DarwinAArch64AsmBackend(const Target &T, const MCRegisterInfo &MRI)
Keith Walker8c44bf12016-01-20 15:59:14 +0000388 : AArch64AsmBackend(T, /*IsLittleEndian*/true), MRI(MRI) {}
Tim Northover3b0846e2014-05-24 12:50:23 +0000389
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000390 MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
Tim Northover3b0846e2014-05-24 12:50:23 +0000391 return createAArch64MachObjectWriter(OS, MachO::CPU_TYPE_ARM64,
392 MachO::CPU_SUBTYPE_ARM64_ALL);
393 }
394
Tim Northover3b0846e2014-05-24 12:50:23 +0000395 /// \brief Generate the compact unwind encoding from the CFI directives.
396 uint32_t generateCompactUnwindEncoding(
397 ArrayRef<MCCFIInstruction> Instrs) const override {
398 if (Instrs.empty())
Tim Northover87442c12016-02-23 21:49:05 +0000399 return CU::UNWIND_ARM64_MODE_FRAMELESS;
Tim Northover3b0846e2014-05-24 12:50:23 +0000400
401 bool HasFP = false;
402 unsigned StackSize = 0;
403
404 uint32_t CompactUnwindEncoding = 0;
405 for (size_t i = 0, e = Instrs.size(); i != e; ++i) {
406 const MCCFIInstruction &Inst = Instrs[i];
407
408 switch (Inst.getOperation()) {
409 default:
410 // Cannot handle this directive: bail out.
Tim Northover87442c12016-02-23 21:49:05 +0000411 return CU::UNWIND_ARM64_MODE_DWARF;
Tim Northover3b0846e2014-05-24 12:50:23 +0000412 case MCCFIInstruction::OpDefCfa: {
413 // Defines a frame pointer.
414 assert(getXRegFromWReg(MRI.getLLVMRegNum(Inst.getRegister(), true)) ==
415 AArch64::FP &&
416 "Invalid frame pointer!");
417 assert(i + 2 < e && "Insufficient CFI instructions to define a frame!");
418
419 const MCCFIInstruction &LRPush = Instrs[++i];
420 assert(LRPush.getOperation() == MCCFIInstruction::OpOffset &&
421 "Link register not pushed!");
422 const MCCFIInstruction &FPPush = Instrs[++i];
423 assert(FPPush.getOperation() == MCCFIInstruction::OpOffset &&
424 "Frame pointer not pushed!");
425
426 unsigned LRReg = MRI.getLLVMRegNum(LRPush.getRegister(), true);
427 unsigned FPReg = MRI.getLLVMRegNum(FPPush.getRegister(), true);
428
429 LRReg = getXRegFromWReg(LRReg);
430 FPReg = getXRegFromWReg(FPReg);
431
432 assert(LRReg == AArch64::LR && FPReg == AArch64::FP &&
433 "Pushing invalid registers for frame!");
434
435 // Indicate that the function has a frame.
Tim Northover87442c12016-02-23 21:49:05 +0000436 CompactUnwindEncoding |= CU::UNWIND_ARM64_MODE_FRAME;
Tim Northover3b0846e2014-05-24 12:50:23 +0000437 HasFP = true;
438 break;
439 }
440 case MCCFIInstruction::OpDefCfaOffset: {
441 assert(StackSize == 0 && "We already have the CFA offset!");
442 StackSize = std::abs(Inst.getOffset());
443 break;
444 }
445 case MCCFIInstruction::OpOffset: {
446 // Registers are saved in pairs. We expect there to be two consecutive
447 // `.cfi_offset' instructions with the appropriate registers specified.
448 unsigned Reg1 = MRI.getLLVMRegNum(Inst.getRegister(), true);
449 if (i + 1 == e)
Tim Northover87442c12016-02-23 21:49:05 +0000450 return CU::UNWIND_ARM64_MODE_DWARF;
Tim Northover3b0846e2014-05-24 12:50:23 +0000451
452 const MCCFIInstruction &Inst2 = Instrs[++i];
453 if (Inst2.getOperation() != MCCFIInstruction::OpOffset)
Tim Northover87442c12016-02-23 21:49:05 +0000454 return CU::UNWIND_ARM64_MODE_DWARF;
Tim Northover3b0846e2014-05-24 12:50:23 +0000455 unsigned Reg2 = MRI.getLLVMRegNum(Inst2.getRegister(), true);
456
457 // N.B. The encodings must be in register number order, and the X
458 // registers before the D registers.
459
460 // X19/X20 pair = 0x00000001,
461 // X21/X22 pair = 0x00000002,
462 // X23/X24 pair = 0x00000004,
463 // X25/X26 pair = 0x00000008,
464 // X27/X28 pair = 0x00000010
465 Reg1 = getXRegFromWReg(Reg1);
466 Reg2 = getXRegFromWReg(Reg2);
467
468 if (Reg1 == AArch64::X19 && Reg2 == AArch64::X20 &&
469 (CompactUnwindEncoding & 0xF1E) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000470 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X19_X20_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000471 else if (Reg1 == AArch64::X21 && Reg2 == AArch64::X22 &&
472 (CompactUnwindEncoding & 0xF1C) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000473 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X21_X22_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000474 else if (Reg1 == AArch64::X23 && Reg2 == AArch64::X24 &&
475 (CompactUnwindEncoding & 0xF18) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000476 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X23_X24_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000477 else if (Reg1 == AArch64::X25 && Reg2 == AArch64::X26 &&
478 (CompactUnwindEncoding & 0xF10) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000479 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X25_X26_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000480 else if (Reg1 == AArch64::X27 && Reg2 == AArch64::X28 &&
481 (CompactUnwindEncoding & 0xF00) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000482 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X27_X28_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000483 else {
484 Reg1 = getDRegFromBReg(Reg1);
485 Reg2 = getDRegFromBReg(Reg2);
486
487 // D8/D9 pair = 0x00000100,
488 // D10/D11 pair = 0x00000200,
489 // D12/D13 pair = 0x00000400,
490 // D14/D15 pair = 0x00000800
491 if (Reg1 == AArch64::D8 && Reg2 == AArch64::D9 &&
492 (CompactUnwindEncoding & 0xE00) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000493 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D8_D9_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000494 else if (Reg1 == AArch64::D10 && Reg2 == AArch64::D11 &&
495 (CompactUnwindEncoding & 0xC00) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000496 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D10_D11_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000497 else if (Reg1 == AArch64::D12 && Reg2 == AArch64::D13 &&
498 (CompactUnwindEncoding & 0x800) == 0)
Tim Northover87442c12016-02-23 21:49:05 +0000499 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D12_D13_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000500 else if (Reg1 == AArch64::D14 && Reg2 == AArch64::D15)
Tim Northover87442c12016-02-23 21:49:05 +0000501 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D14_D15_PAIR;
Tim Northover3b0846e2014-05-24 12:50:23 +0000502 else
503 // A pair was pushed which we cannot handle.
Tim Northover87442c12016-02-23 21:49:05 +0000504 return CU::UNWIND_ARM64_MODE_DWARF;
Tim Northover3b0846e2014-05-24 12:50:23 +0000505 }
506
507 break;
508 }
509 }
510 }
511
512 if (!HasFP) {
513 // With compact unwind info we can only represent stack adjustments of up
514 // to 65520 bytes.
515 if (StackSize > 65520)
Tim Northover87442c12016-02-23 21:49:05 +0000516 return CU::UNWIND_ARM64_MODE_DWARF;
Tim Northover3b0846e2014-05-24 12:50:23 +0000517
Tim Northover87442c12016-02-23 21:49:05 +0000518 CompactUnwindEncoding |= CU::UNWIND_ARM64_MODE_FRAMELESS;
Tim Northover3b0846e2014-05-24 12:50:23 +0000519 CompactUnwindEncoding |= encodeStackAdjustment(StackSize);
520 }
521
522 return CompactUnwindEncoding;
523 }
524};
525
526} // end anonymous namespace
527
528namespace {
529
530class ELFAArch64AsmBackend : public AArch64AsmBackend {
531public:
532 uint8_t OSABI;
Joel Jones504bf332016-10-24 13:37:13 +0000533 bool IsILP32;
Tim Northover3b0846e2014-05-24 12:50:23 +0000534
Joel Jones504bf332016-10-24 13:37:13 +0000535 ELFAArch64AsmBackend(const Target &T, uint8_t OSABI, bool IsLittleEndian,
536 bool IsILP32)
537 : AArch64AsmBackend(T, IsLittleEndian), OSABI(OSABI), IsILP32(IsILP32) {}
Tim Northover3b0846e2014-05-24 12:50:23 +0000538
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000539 MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
Joel Jones504bf332016-10-24 13:37:13 +0000540 return createAArch64ELFObjectWriter(OS, OSABI, IsLittleEndian, IsILP32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000541 }
542
543 void processFixupValue(const MCAssembler &Asm, const MCAsmLayout &Layout,
544 const MCFixup &Fixup, const MCFragment *DF,
545 const MCValue &Target, uint64_t &Value,
546 bool &IsResolved) override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000547};
548
549void ELFAArch64AsmBackend::processFixupValue(
550 const MCAssembler &Asm, const MCAsmLayout &Layout, const MCFixup &Fixup,
551 const MCFragment *DF, const MCValue &Target, uint64_t &Value,
552 bool &IsResolved) {
553 // The ADRP instruction adds some multiple of 0x1000 to the current PC &
554 // ~0xfff. This means that the required offset to reach a symbol can vary by
555 // up to one step depending on where the ADRP is in memory. For example:
556 //
557 // ADRP x0, there
558 // there:
559 //
560 // If the ADRP occurs at address 0xffc then "there" will be at 0x1000 and
561 // we'll need that as an offset. At any other address "there" will be in the
562 // same page as the ADRP and the instruction should encode 0x0. Assuming the
563 // section isn't 0x1000-aligned, we therefore need to delegate this decision
564 // to the linker -- a relocation!
565 if ((uint32_t)Fixup.getKind() == AArch64::fixup_aarch64_pcrel_adrp_imm21)
566 IsResolved = false;
567}
568
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000569}
Tim Northover3b0846e2014-05-24 12:50:23 +0000570
571MCAsmBackend *llvm::createAArch64leAsmBackend(const Target &T,
Daniel Sanders418caf52015-06-10 10:35:34 +0000572 const MCRegisterInfo &MRI,
Daniel Sanders50f17232015-09-15 16:17:27 +0000573 const Triple &TheTriple,
Joel Jones373d7d32016-07-25 17:18:28 +0000574 StringRef CPU,
575 const MCTargetOptions &Options) {
Daniel Sanders50f17232015-09-15 16:17:27 +0000576 if (TheTriple.isOSBinFormatMachO())
Tim Northover3b0846e2014-05-24 12:50:23 +0000577 return new DarwinAArch64AsmBackend(T, MRI);
578
Daniel Sanders50f17232015-09-15 16:17:27 +0000579 assert(TheTriple.isOSBinFormatELF() && "Expect either MachO or ELF target");
580 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TheTriple.getOS());
Joel Jones504bf332016-10-24 13:37:13 +0000581 bool IsILP32 = Options.getABIName() == "ilp32";
582 return new ELFAArch64AsmBackend(T, OSABI, /*IsLittleEndian=*/true, IsILP32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000583}
584
585MCAsmBackend *llvm::createAArch64beAsmBackend(const Target &T,
Daniel Sanders418caf52015-06-10 10:35:34 +0000586 const MCRegisterInfo &MRI,
Daniel Sanders50f17232015-09-15 16:17:27 +0000587 const Triple &TheTriple,
Joel Jones373d7d32016-07-25 17:18:28 +0000588 StringRef CPU,
589 const MCTargetOptions &Options) {
Daniel Sanders50f17232015-09-15 16:17:27 +0000590 assert(TheTriple.isOSBinFormatELF() &&
Tim Northover3b0846e2014-05-24 12:50:23 +0000591 "Big endian is only supported for ELF targets!");
Daniel Sanders50f17232015-09-15 16:17:27 +0000592 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TheTriple.getOS());
Joel Jones504bf332016-10-24 13:37:13 +0000593 bool IsILP32 = Options.getABIName() == "ilp32";
594 return new ELFAArch64AsmBackend(T, OSABI, /*IsLittleEndian=*/false, IsILP32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000595}