1 //===-- ARM64AsmBackend.cpp - ARM64 Assembler Backend ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "ARM64RegisterInfo.h"
12 #include "MCTargetDesc/ARM64FixupKinds.h"
13 #include "llvm/ADT/Triple.h"
14 #include "llvm/MC/MCAsmBackend.h"
15 #include "llvm/MC/MCDirectives.h"
16 #include "llvm/MC/MCFixupKindInfo.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCSectionMachO.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/MachO.h"
25 class ARM64AsmBackend : public MCAsmBackend {
26 static const unsigned PCRelFlagVal =
27 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits | MCFixupKindInfo::FKF_IsPCRel;
30 ARM64AsmBackend(const Target &T) : MCAsmBackend() {}
32 unsigned getNumFixupKinds() const override {
33 return ARM64::NumTargetFixupKinds;
36 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
37 const static MCFixupKindInfo Infos[ARM64::NumTargetFixupKinds] = {
38 // This table *must* be in the order that the fixup_* kinds are defined in
41 // Name Offset (bits) Size (bits) Flags
42 { "fixup_arm64_pcrel_adr_imm21", 0, 32, PCRelFlagVal },
43 { "fixup_arm64_pcrel_adrp_imm21", 0, 32, PCRelFlagVal },
44 { "fixup_arm64_add_imm12", 10, 12, 0 },
45 { "fixup_arm64_ldst_imm12_scale1", 10, 12, 0 },
46 { "fixup_arm64_ldst_imm12_scale2", 10, 12, 0 },
47 { "fixup_arm64_ldst_imm12_scale4", 10, 12, 0 },
48 { "fixup_arm64_ldst_imm12_scale8", 10, 12, 0 },
49 { "fixup_arm64_ldst_imm12_scale16", 10, 12, 0 },
50 { "fixup_arm64_ldr_pcrel_imm19", 5, 19, PCRelFlagVal },
51 { "fixup_arm64_movw", 5, 16, 0 },
52 { "fixup_arm64_pcrel_branch14", 5, 14, PCRelFlagVal },
53 { "fixup_arm64_pcrel_branch19", 5, 19, PCRelFlagVal },
54 { "fixup_arm64_pcrel_branch26", 0, 26, PCRelFlagVal },
55 { "fixup_arm64_pcrel_call26", 0, 26, PCRelFlagVal },
56 { "fixup_arm64_tlsdesc_call", 0, 0, 0 }
59 if (Kind < FirstTargetFixupKind)
60 return MCAsmBackend::getFixupKindInfo(Kind);
62 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
64 return Infos[Kind - FirstTargetFixupKind];
67 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
68 uint64_t Value, bool IsPCRel) const override;
70 bool mayNeedRelaxation(const MCInst &Inst) const override;
71 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
72 const MCRelaxableFragment *DF,
73 const MCAsmLayout &Layout) const override;
74 void relaxInstruction(const MCInst &Inst, MCInst &Res) const override;
75 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
77 void HandleAssemblerFlag(MCAssemblerFlag Flag) {}
79 unsigned getPointerSize() const { return 8; }
82 } // end anonymous namespace
84 /// \brief The number of bytes the fixup may change.
85 static unsigned getFixupKindNumBytes(unsigned Kind) {
88 assert(0 && "Unknown fixup kind!");
90 case ARM64::fixup_arm64_tlsdesc_call:
97 case ARM64::fixup_arm64_movw:
100 case ARM64::fixup_arm64_pcrel_branch14:
101 case ARM64::fixup_arm64_add_imm12:
102 case ARM64::fixup_arm64_ldst_imm12_scale1:
103 case ARM64::fixup_arm64_ldst_imm12_scale2:
104 case ARM64::fixup_arm64_ldst_imm12_scale4:
105 case ARM64::fixup_arm64_ldst_imm12_scale8:
106 case ARM64::fixup_arm64_ldst_imm12_scale16:
107 case ARM64::fixup_arm64_ldr_pcrel_imm19:
108 case ARM64::fixup_arm64_pcrel_branch19:
111 case ARM64::fixup_arm64_pcrel_adr_imm21:
112 case ARM64::fixup_arm64_pcrel_adrp_imm21:
113 case ARM64::fixup_arm64_pcrel_branch26:
114 case ARM64::fixup_arm64_pcrel_call26:
123 static unsigned AdrImmBits(unsigned Value) {
124 unsigned lo2 = Value & 0x3;
125 unsigned hi19 = (Value & 0x1ffffc) >> 2;
126 return (hi19 << 5) | (lo2 << 29);
129 static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value) {
130 int64_t SignedValue = static_cast<int64_t>(Value);
133 assert(false && "Unknown fixup kind!");
134 case ARM64::fixup_arm64_pcrel_adr_imm21:
135 if (SignedValue > 2097151 || SignedValue < -2097152)
136 report_fatal_error("fixup value out of range");
137 return AdrImmBits(Value & 0x1fffffULL);
138 case ARM64::fixup_arm64_pcrel_adrp_imm21:
139 return AdrImmBits((Value & 0x1fffff000ULL) >> 12);
140 case ARM64::fixup_arm64_ldr_pcrel_imm19:
141 case ARM64::fixup_arm64_pcrel_branch19:
142 // Signed 21-bit immediate
143 if (SignedValue > 2097151 || SignedValue < -2097152)
144 report_fatal_error("fixup value out of range");
145 // Low two bits are not encoded.
146 return (Value >> 2) & 0x7ffff;
147 case ARM64::fixup_arm64_add_imm12:
148 case ARM64::fixup_arm64_ldst_imm12_scale1:
149 // Unsigned 12-bit immediate
151 report_fatal_error("invalid imm12 fixup value");
153 case ARM64::fixup_arm64_ldst_imm12_scale2:
154 // Unsigned 12-bit immediate which gets multiplied by 2
155 if (Value & 1 || Value >= 0x2000)
156 report_fatal_error("invalid imm12 fixup value");
158 case ARM64::fixup_arm64_ldst_imm12_scale4:
159 // Unsigned 12-bit immediate which gets multiplied by 4
160 if (Value & 3 || Value >= 0x4000)
161 report_fatal_error("invalid imm12 fixup value");
163 case ARM64::fixup_arm64_ldst_imm12_scale8:
164 // Unsigned 12-bit immediate which gets multiplied by 8
165 if (Value & 7 || Value >= 0x8000)
166 report_fatal_error("invalid imm12 fixup value");
168 case ARM64::fixup_arm64_ldst_imm12_scale16:
169 // Unsigned 12-bit immediate which gets multiplied by 16
170 if (Value & 15 || Value >= 0x10000)
171 report_fatal_error("invalid imm12 fixup value");
173 case ARM64::fixup_arm64_movw:
174 report_fatal_error("no resolvable MOVZ/MOVK fixups supported yet");
176 case ARM64::fixup_arm64_pcrel_branch14:
177 // Signed 16-bit immediate
178 if (SignedValue > 32767 || SignedValue < -32768)
179 report_fatal_error("fixup value out of range");
180 // Low two bits are not encoded (4-byte alignment assumed).
182 report_fatal_error("fixup not sufficiently aligned");
183 return (Value >> 2) & 0x3fff;
184 case ARM64::fixup_arm64_pcrel_branch26:
185 case ARM64::fixup_arm64_pcrel_call26:
186 // Signed 28-bit immediate
187 if (SignedValue > 134217727 || SignedValue < -134217728)
188 report_fatal_error("fixup value out of range");
189 // Low two bits are not encoded (4-byte alignment assumed).
191 report_fatal_error("fixup not sufficiently aligned");
192 return (Value >> 2) & 0x3ffffff;
201 void ARM64AsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
202 unsigned DataSize, uint64_t Value,
203 bool IsPCRel) const {
204 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
206 return; // Doesn't change encoding.
207 MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
208 // Apply any target-specific value adjustments.
209 Value = adjustFixupValue(Fixup.getKind(), Value);
211 // Shift the value into position.
212 Value <<= Info.TargetOffset;
214 unsigned Offset = Fixup.getOffset();
215 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
217 // For each byte of the fragment that the fixup touches, mask in the
218 // bits from the fixup value.
219 for (unsigned i = 0; i != NumBytes; ++i)
220 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
223 bool ARM64AsmBackend::mayNeedRelaxation(const MCInst &Inst) const {
227 bool ARM64AsmBackend::fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
228 const MCRelaxableFragment *DF,
229 const MCAsmLayout &Layout) const {
230 // FIXME: This isn't correct for ARM64. Just moving the "generic" logic
231 // into the targets for now.
233 // Relax if the value is too big for a (signed) i8.
234 return int64_t(Value) != int64_t(int8_t(Value));
237 void ARM64AsmBackend::relaxInstruction(const MCInst &Inst, MCInst &Res) const {
238 assert(false && "ARM64AsmBackend::relaxInstruction() unimplemented");
241 bool ARM64AsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
242 // If the count is not 4-byte aligned, we must be writing data into the text
243 // section (otherwise we have unaligned instructions, and thus have far
244 // bigger problems), so just write zeros instead.
245 if ((Count & 3) != 0) {
246 for (uint64_t i = 0, e = (Count & 3); i != e; ++i)
250 // We are properly aligned, so write NOPs as requested.
252 for (uint64_t i = 0; i != Count; ++i)
253 OW->Write32(0xd503201f);
261 /// \brief Compact unwind encoding values.
262 enum CompactUnwindEncodings {
263 /// \brief A "frameless" leaf function, where no non-volatile registers are
264 /// saved. The return remains in LR throughout the function.
265 UNWIND_ARM64_MODE_FRAMELESS = 0x02000000,
267 /// \brief No compact unwind encoding available. Instead the low 23-bits of
268 /// the compact unwind encoding is the offset of the DWARF FDE in the
269 /// __eh_frame section. This mode is never used in object files. It is only
270 /// generated by the linker in final linked images, which have only DWARF info
272 UNWIND_ARM64_MODE_DWARF = 0x03000000,
274 /// \brief This is a standard arm64 prologue where FP/LR are immediately
275 /// pushed on the stack, then SP is copied to FP. If there are any
276 /// non-volatile register saved, they are copied into the stack fame in pairs
277 /// in a contiguous ranger right below the saved FP/LR pair. Any subset of the
278 /// five X pairs and four D pairs can be saved, but the memory layout must be
279 /// in register number order.
280 UNWIND_ARM64_MODE_FRAME = 0x04000000,
282 /// \brief Frame register pair encodings.
283 UNWIND_ARM64_FRAME_X19_X20_PAIR = 0x00000001,
284 UNWIND_ARM64_FRAME_X21_X22_PAIR = 0x00000002,
285 UNWIND_ARM64_FRAME_X23_X24_PAIR = 0x00000004,
286 UNWIND_ARM64_FRAME_X25_X26_PAIR = 0x00000008,
287 UNWIND_ARM64_FRAME_X27_X28_PAIR = 0x00000010,
288 UNWIND_ARM64_FRAME_D8_D9_PAIR = 0x00000100,
289 UNWIND_ARM64_FRAME_D10_D11_PAIR = 0x00000200,
290 UNWIND_ARM64_FRAME_D12_D13_PAIR = 0x00000400,
291 UNWIND_ARM64_FRAME_D14_D15_PAIR = 0x00000800
294 } // end CU namespace
296 // FIXME: This should be in a separate file.
297 class DarwinARM64AsmBackend : public ARM64AsmBackend {
298 const MCRegisterInfo &MRI;
300 /// \brief Encode compact unwind stack adjustment for frameless functions.
301 /// See UNWIND_ARM64_FRAMELESS_STACK_SIZE_MASK in compact_unwind_encoding.h.
302 /// The stack size always needs to be 16 byte aligned.
303 uint32_t encodeStackAdjustment(uint32_t StackSize) const {
304 return (StackSize / 16) << 12;
308 DarwinARM64AsmBackend(const Target &T, const MCRegisterInfo &MRI)
309 : ARM64AsmBackend(T), MRI(MRI) {}
311 MCObjectWriter *createObjectWriter(raw_ostream &OS) const override {
312 return createARM64MachObjectWriter(OS, MachO::CPU_TYPE_ARM64,
313 MachO::CPU_SUBTYPE_ARM64_ALL);
316 bool doesSectionRequireSymbols(const MCSection &Section) const override {
317 // Any section for which the linker breaks things into atoms needs to
318 // preserve symbols, including assembler local symbols, to identify
319 // those atoms. These sections are:
322 // S_CSTRING_LITERALS (e.g. __cstring)
323 // S_LITERAL_POINTERS (e.g. objc selector pointers)
324 // S_16BYTE_LITERALS, S_8BYTE_LITERALS, S_4BYTE_LITERALS
331 // __DATA,__objc_classrefs
332 // __DATA,__objc_catlist
334 // FIXME: It would be better if the compiler used actual linker local
335 // symbols for each of these sections rather than preserving what
336 // are ostensibly assembler local symbols.
337 const MCSectionMachO &SMO = static_cast<const MCSectionMachO &>(Section);
338 return (SMO.getType() == MachO::S_CSTRING_LITERALS ||
339 SMO.getType() == MachO::S_4BYTE_LITERALS ||
340 SMO.getType() == MachO::S_8BYTE_LITERALS ||
341 SMO.getType() == MachO::S_16BYTE_LITERALS ||
342 SMO.getType() == MachO::S_LITERAL_POINTERS ||
343 (SMO.getSegmentName() == "__TEXT" &&
344 (SMO.getSectionName() == "__eh_frame" ||
345 SMO.getSectionName() == "__ustring")) ||
346 (SMO.getSegmentName() == "__DATA" &&
347 (SMO.getSectionName() == "__cfstring" ||
348 SMO.getSectionName() == "__objc_classrefs" ||
349 SMO.getSectionName() == "__objc_catlist")));
352 /// \brief Generate the compact unwind encoding from the CFI directives.
353 uint32_t generateCompactUnwindEncoding(
354 ArrayRef<MCCFIInstruction> Instrs) const override {
356 return CU::UNWIND_ARM64_MODE_FRAMELESS;
359 unsigned StackSize = 0;
361 uint32_t CompactUnwindEncoding = 0;
362 for (size_t i = 0, e = Instrs.size(); i != e; ++i) {
363 const MCCFIInstruction &Inst = Instrs[i];
365 switch (Inst.getOperation()) {
367 // Cannot handle this directive: bail out.
368 return CU::UNWIND_ARM64_MODE_DWARF;
369 case MCCFIInstruction::OpDefCfa: {
370 // Defines a frame pointer.
371 assert(getXRegFromWReg(MRI.getLLVMRegNum(Inst.getRegister(), true)) ==
373 "Invalid frame pointer!");
374 assert(i + 2 < e && "Insufficient CFI instructions to define a frame!");
376 const MCCFIInstruction &LRPush = Instrs[++i];
377 assert(LRPush.getOperation() == MCCFIInstruction::OpOffset &&
378 "Link register not pushed!");
379 const MCCFIInstruction &FPPush = Instrs[++i];
380 assert(FPPush.getOperation() == MCCFIInstruction::OpOffset &&
381 "Frame pointer not pushed!");
383 unsigned LRReg = MRI.getLLVMRegNum(LRPush.getRegister(), true);
384 unsigned FPReg = MRI.getLLVMRegNum(FPPush.getRegister(), true);
386 LRReg = getXRegFromWReg(LRReg);
387 FPReg = getXRegFromWReg(FPReg);
389 assert(LRReg == ARM64::LR && FPReg == ARM64::FP &&
390 "Pushing invalid registers for frame!");
392 // Indicate that the function has a frame.
393 CompactUnwindEncoding |= CU::UNWIND_ARM64_MODE_FRAME;
397 case MCCFIInstruction::OpDefCfaOffset: {
398 assert(StackSize == 0 && "We already have the CFA offset!");
399 StackSize = std::abs(Inst.getOffset());
402 case MCCFIInstruction::OpOffset: {
403 // Registers are saved in pairs. We expect there to be two consecutive
404 // `.cfi_offset' instructions with the appropriate registers specified.
405 unsigned Reg1 = MRI.getLLVMRegNum(Inst.getRegister(), true);
407 return CU::UNWIND_ARM64_MODE_DWARF;
409 const MCCFIInstruction &Inst2 = Instrs[++i];
410 if (Inst2.getOperation() != MCCFIInstruction::OpOffset)
411 return CU::UNWIND_ARM64_MODE_DWARF;
412 unsigned Reg2 = MRI.getLLVMRegNum(Inst2.getRegister(), true);
414 // N.B. The encodings must be in register number order, and the X
415 // registers before the D registers.
417 // X19/X20 pair = 0x00000001,
418 // X21/X22 pair = 0x00000002,
419 // X23/X24 pair = 0x00000004,
420 // X25/X26 pair = 0x00000008,
421 // X27/X28 pair = 0x00000010
422 Reg1 = getXRegFromWReg(Reg1);
423 Reg2 = getXRegFromWReg(Reg2);
425 if (Reg1 == ARM64::X19 && Reg2 == ARM64::X20 &&
426 (CompactUnwindEncoding & 0xF1E) == 0)
427 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X19_X20_PAIR;
428 else if (Reg1 == ARM64::X21 && Reg2 == ARM64::X22 &&
429 (CompactUnwindEncoding & 0xF1C) == 0)
430 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X21_X22_PAIR;
431 else if (Reg1 == ARM64::X23 && Reg2 == ARM64::X24 &&
432 (CompactUnwindEncoding & 0xF18) == 0)
433 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X23_X24_PAIR;
434 else if (Reg1 == ARM64::X25 && Reg2 == ARM64::X26 &&
435 (CompactUnwindEncoding & 0xF10) == 0)
436 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X25_X26_PAIR;
437 else if (Reg1 == ARM64::X27 && Reg2 == ARM64::X28 &&
438 (CompactUnwindEncoding & 0xF00) == 0)
439 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X27_X28_PAIR;
441 Reg1 = getDRegFromBReg(Reg1);
442 Reg2 = getDRegFromBReg(Reg2);
444 // D8/D9 pair = 0x00000100,
445 // D10/D11 pair = 0x00000200,
446 // D12/D13 pair = 0x00000400,
447 // D14/D15 pair = 0x00000800
448 if (Reg1 == ARM64::D8 && Reg2 == ARM64::D9 &&
449 (CompactUnwindEncoding & 0xE00) == 0)
450 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D8_D9_PAIR;
451 else if (Reg1 == ARM64::D10 && Reg2 == ARM64::D11 &&
452 (CompactUnwindEncoding & 0xC00) == 0)
453 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D10_D11_PAIR;
454 else if (Reg1 == ARM64::D12 && Reg2 == ARM64::D13 &&
455 (CompactUnwindEncoding & 0x800) == 0)
456 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D12_D13_PAIR;
457 else if (Reg1 == ARM64::D14 && Reg2 == ARM64::D15)
458 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D14_D15_PAIR;
460 // A pair was pushed which we cannot handle.
461 return CU::UNWIND_ARM64_MODE_DWARF;
470 // With compact unwind info we can only represent stack adjustments of up
472 if (StackSize > 65520)
473 return CU::UNWIND_ARM64_MODE_DWARF;
475 CompactUnwindEncoding |= CU::UNWIND_ARM64_MODE_FRAMELESS;
476 CompactUnwindEncoding |= encodeStackAdjustment(StackSize);
479 return CompactUnwindEncoding;
483 } // end anonymous namespace
487 class ELFARM64AsmBackend : public ARM64AsmBackend {
492 ELFARM64AsmBackend(const Target &T, uint8_t OSABI, bool IsLittleEndian)
493 : ARM64AsmBackend(T), OSABI(OSABI), IsLittleEndian(IsLittleEndian) {}
495 MCObjectWriter *createObjectWriter(raw_ostream &OS) const override {
496 return createARM64ELFObjectWriter(OS, OSABI, IsLittleEndian);
499 void processFixupValue(const MCAssembler &Asm, const MCAsmLayout &Layout,
500 const MCFixup &Fixup, const MCFragment *DF,
501 const MCValue &Target, uint64_t &Value,
502 bool &IsResolved) override;
505 void ELFARM64AsmBackend::processFixupValue(const MCAssembler &Asm,
506 const MCAsmLayout &Layout,
507 const MCFixup &Fixup,
508 const MCFragment *DF,
509 const MCValue &Target,
510 uint64_t &Value, bool &IsResolved) {
511 // The ADRP instruction adds some multiple of 0x1000 to the current PC &
512 // ~0xfff. This means that the required offset to reach a symbol can vary by
513 // up to one step depending on where the ADRP is in memory. For example:
518 // If the ADRP occurs at address 0xffc then "there" will be at 0x1000 and
519 // we'll need that as an offset. At any other address "there" will be in the
520 // same page as the ADRP and the instruction should encode 0x0. Assuming the
521 // section isn't 0x1000-aligned, we therefore need to delegate this decision
522 // to the linker -- a relocation!
523 if ((uint32_t)Fixup.getKind() == ARM64::fixup_arm64_pcrel_adrp_imm21)
528 MCAsmBackend *llvm::createARM64leAsmBackend(const Target &T,
529 const MCRegisterInfo &MRI,
530 StringRef TT, StringRef CPU) {
531 Triple TheTriple(TT);
533 if (TheTriple.isOSDarwin())
534 return new DarwinARM64AsmBackend(T, MRI);
536 assert(TheTriple.isOSBinFormatELF() && "Expect either MachO or ELF target");
537 return new ELFARM64AsmBackend(T, TheTriple.getOS(), /*IsLittleEndian=*/true);
540 MCAsmBackend *llvm::createARM64beAsmBackend(const Target &T,
541 const MCRegisterInfo &MRI,
542 StringRef TT, StringRef CPU) {
543 Triple TheTriple(TT);
545 assert(TheTriple.isOSBinFormatELF() && "Big endian is only supported for ELF targets!");
546 return new ELFARM64AsmBackend(T, TheTriple.getOS(), /*IsLittleEndian=*/false);