Debian Codegraph @

Source/Core/Common/Arm64Emitter.h

raw symbols SCIP
1 // Copyright 2015 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <array>
7 #include <bit>
8 #include <cstring>
9 #include <functional>
10 #include <optional>
11 #include <type_traits>
12 #include <utility>
13
14 #include "Common/ArmCommon.h"
15 #include "Common/Assert.h"
16 #include "Common/BitSet.h"
17 #include "Common/BitUtils.h"
18 #include "Common/CodeBlock.h"
19 #include "Common/Common.h"
20 #include "Common/CommonTypes.h"
21 #include "Common/MathUtil.h"
22 #include "Common/SmallVector.h"
23
24 namespace Arm64Gen
25 {
26 // X30 serves a dual purpose as a link register
27 // Encoded as <u3:type><u5:reg>
28 // Types:
29 // 000 - 32bit GPR
30 // 001 - 64bit GPR
31 // 010 - VFP single precision
32 // 100 - VFP double precision
33 // 110 - VFP quad precision
34 enum class ARM64Reg
35 {
36 // 32bit registers
37 W0 = 0,
38 W1,
39 W2,
40 W3,
41 W4,
42 W5,
43 W6,
44 W7,
45 W8,
46 W9,
47 W10,
48 W11,
49 W12,
50 W13,
51 W14,
52 W15,
53 W16,
54 W17,
55 W18,
56 W19,
57 W20,
58 W21,
59 W22,
60 W23,
61 W24,
62 W25,
63 W26,
64 W27,
65 W28,
66 W29,
67 W30,
68
69 WSP, // 32bit stack pointer
70
71 // 64bit registers
72 X0 = 0x20,
73 X1,
74 X2,
75 X3,
76 X4,
77 X5,
78 X6,
79 X7,
80 X8,
81 X9,
82 X10,
83 X11,
84 X12,
85 X13,
86 X14,
87 X15,
88 X16,
89 X17,
90 X18,
91 X19,
92 X20,
93 X21,
94 X22,
95 X23,
96 X24,
97 X25,
98 X26,
99 X27,
100 X28,
101 X29,
102 X30,
103
104 SP, // 64bit stack pointer
105
106 // VFP single precision registers
107 S0 = 0x40,
108 S1,
109 S2,
110 S3,
111 S4,
112 S5,
113 S6,
114 S7,
115 S8,
116 S9,
117 S10,
118 S11,
119 S12,
120 S13,
121 S14,
122 S15,
123 S16,
124 S17,
125 S18,
126 S19,
127 S20,
128 S21,
129 S22,
130 S23,
131 S24,
132 S25,
133 S26,
134 S27,
135 S28,
136 S29,
137 S30,
138 S31,
139
140 // VFP Double Precision registers
141 D0 = 0x80,
142 D1,
143 D2,
144 D3,
145 D4,
146 D5,
147 D6,
148 D7,
149 D8,
150 D9,
151 D10,
152 D11,
153 D12,
154 D13,
155 D14,
156 D15,
157 D16,
158 D17,
159 D18,
160 D19,
161 D20,
162 D21,
163 D22,
164 D23,
165 D24,
166 D25,
167 D26,
168 D27,
169 D28,
170 D29,
171 D30,
172 D31,
173
174 // ASIMD Quad-Word registers
175 Q0 = 0xC0,
176 Q1,
177 Q2,
178 Q3,
179 Q4,
180 Q5,
181 Q6,
182 Q7,
183 Q8,
184 Q9,
185 Q10,
186 Q11,
187 Q12,
188 Q13,
189 Q14,
190 Q15,
191 Q16,
192 Q17,
193 Q18,
194 Q19,
195 Q20,
196 Q21,
197 Q22,
198 Q23,
199 Q24,
200 Q25,
201 Q26,
202 Q27,
203 Q28,
204 Q29,
205 Q30,
206 Q31,
207
208 // For PRFM(prefetch memory) encoding
209 // This is encoded in the Rt register
210 // Data preload
211 PLDL1KEEP = 0,
212 PLDL1STRM,
213 PLDL2KEEP,
214 PLDL2STRM,
215 PLDL3KEEP,
216 PLDL3STRM,
217 // Instruction preload
218 PLIL1KEEP = 8,
219 PLIL1STRM,
220 PLIL2KEEP,
221 PLIL2STRM,
222 PLIL3KEEP,
223 PLIL3STRM,
224 // Prepare for store
225 PLTL1KEEP = 16,
226 PLTL1STRM,
227 PLTL2KEEP,
228 PLTL2STRM,
229 PLTL3KEEP,
230 PLTL3STRM,
231
232 WZR = WSP,
233 ZR = SP,
234
235 INVALID_REG = -1,
236 };
237
238 constexpr int operator&(const ARM64Reg& reg, const int mask)
239 {
240 return static_cast<int>(reg) & mask;
241 }
242 constexpr int operator|(const ARM64Reg& reg, const int mask)
243 {
244 return static_cast<int>(reg) | mask;
245 }
246 constexpr ARM64Reg operator+(const ARM64Reg& reg, const int addend)
247 {
248 return static_cast<ARM64Reg>(static_cast<int>(reg) + addend);
249 }
250 constexpr bool Is64Bit(ARM64Reg reg)
251 {
252 return (reg & 0x20) != 0;
253 }
254 constexpr bool IsSingle(ARM64Reg reg)
255 {
256 return (reg & 0xC0) == 0x40;
257 }
258 constexpr bool IsDouble(ARM64Reg reg)
259 {
260 return (reg & 0xC0) == 0x80;
261 }
262 constexpr bool IsScalar(ARM64Reg reg)
263 {
264 return IsSingle(reg) || IsDouble(reg);
265 }
266 constexpr bool IsQuad(ARM64Reg reg)
267 {
268 return (reg & 0xC0) == 0xC0;
269 }
270 constexpr bool IsVector(ARM64Reg reg)
271 {
272 return (reg & 0xC0) != 0;
273 }
274 constexpr bool IsGPR(ARM64Reg reg)
275 {
276 return static_cast<int>(reg) < 0x40;
277 }
278
279 constexpr int DecodeReg(ARM64Reg reg)
280 {
281 return reg & 0x1F;
282 }
283 constexpr ARM64Reg EncodeRegTo32(ARM64Reg reg)
284 {
285 return static_cast<ARM64Reg>(DecodeReg(reg));
286 }
287 constexpr ARM64Reg EncodeRegTo64(ARM64Reg reg)
288 {
289 return static_cast<ARM64Reg>(reg | 0x20);
290 }
291 constexpr ARM64Reg EncodeRegToSingle(ARM64Reg reg)
292 {
293 return static_cast<ARM64Reg>(ARM64Reg::S0 | DecodeReg(reg));
294 }
295 constexpr ARM64Reg EncodeRegToDouble(ARM64Reg reg)
296 {
297 return static_cast<ARM64Reg>((reg & ~0xC0) | 0x80);
298 }
299 constexpr ARM64Reg EncodeRegToQuad(ARM64Reg reg)
300 {
301 return static_cast<ARM64Reg>(reg | 0xC0);
302 }
303
304 enum class ShiftType
305 {
306 // Logical Shift Left
307 LSL = 0,
308 // Logical Shift Right
309 LSR = 1,
310 // Arithmetic Shift Right
311 ASR = 2,
312 // Rotate Right
313 ROR = 3,
314 };
315
316 enum class ExtendSpecifier
317 {
318 UXTB = 0x0,
319 UXTH = 0x1,
320 UXTW = 0x2, /* Also LSL on 32bit width */
321 UXTX = 0x3, /* Also LSL on 64bit width */
322 SXTB = 0x4,
323 SXTH = 0x5,
324 SXTW = 0x6,
325 SXTX = 0x7,
326 };
327
328 enum class IndexType
329 {
330 Unsigned,
331 Post,
332 Pre,
333 Signed, // used in LDP/STP
334 };
335
336 enum class ShiftAmount
337 {
338 Shift0,
339 Shift16,
340 Shift32,
341 Shift48,
342 };
343
344 enum class RoundingMode
345 {
346 A, // round to nearest, ties to away
347 M, // round towards -inf
348 N, // round to nearest, ties to even
349 P, // round towards +inf
350 Z, // round towards zero
351 };
352
353 enum class GPRSize
354 {
355 B32,
356 B64,
357 };
358
359 struct FixupBranch
360 {
361 enum class Type : u32
362 {
363 CBZ,
364 CBNZ,
365 BConditional,
366 TBZ,
367 TBNZ,
368 B,
369 BL,
370 };
371
372 u8* ptr;
373 Type type;
374 // Used with B.cond
375 CCFlags cond;
376 // Used with TBZ/TBNZ
377 u8 bit;
378 // Used with Test/Compare and Branch
379 ARM64Reg reg;
380 };
381
382 enum class PStateField
383 {
384 SPSel = 0,
385 DAIFSet,
386 DAIFClr,
387 NZCV, // The only system registers accessible from EL0 (user space)
388 PMCR_EL0,
389 PMCCNTR_EL0,
390 FPCR = 0x340,
391 FPSR = 0x341,
392 };
393
394 enum class SystemHint
395 {
396 NOP,
397 YIELD,
398 WFE,
399 WFI,
400 SEV,
401 SEVL,
402 };
403
404 enum class BarrierType
405 {
406 OSHLD = 1,
407 OSHST = 2,
408 OSH = 3,
409 NSHLD = 5,
410 NSHST = 6,
411 NSH = 7,
412 ISHLD = 9,
413 ISHST = 10,
414 ISH = 11,
415 LD = 13,
416 ST = 14,
417 SY = 15,
418 };
419
420 class ArithOption
421 {
422 private:
423 enum class WidthSpecifier
424 {
425 Default,
426 Width32Bit,
427 Width64Bit,
428 };
429
430 enum class TypeSpecifier
431 {
432 ExtendedReg,
433 Immediate,
434 ShiftedReg,
435 };
436
437 ARM64Reg m_destReg;
438 WidthSpecifier m_width;
439 ExtendSpecifier m_extend;
440 TypeSpecifier m_type;
441 ShiftType m_shifttype;
442 u32 m_shift;
443
444 public:
445 ArithOption(ARM64Reg Rd, bool index = false)
446 {
447 // Indexed registers are a certain feature of AARch64
448 // On Loadstore instructions that use a register offset
449 // We can have the register as an index
450 // If we are indexing then the offset register will
451 // be shifted to the left so we are indexing at intervals
452 // of the size of what we are loading
453 // 8-bit: Index does nothing
454 // 16-bit: Index LSL 1
455 // 32-bit: Index LSL 2
456 // 64-bit: Index LSL 3
457 if (index)
458 m_shift = 4;
459 else
460 m_shift = 0;
461
462 m_destReg = Rd;
463 m_type = TypeSpecifier::ExtendedReg;
464 if (Is64Bit(Rd))
465 {
466 m_width = WidthSpecifier::Width64Bit;
467 m_extend = ExtendSpecifier::UXTX;
468 }
469 else
470 {
471 m_width = WidthSpecifier::Width32Bit;
472 m_extend = ExtendSpecifier::UXTW;
473 }
474 m_shifttype = ShiftType::LSL;
475 }
476 ArithOption(ARM64Reg Rd, ExtendSpecifier extend_type, u32 shift = 0)
477 {
478 m_destReg = Rd;
479 m_width = Is64Bit(Rd) ? WidthSpecifier::Width64Bit : WidthSpecifier::Width32Bit;
480 m_extend = extend_type;
481 m_type = TypeSpecifier::ExtendedReg;
482 m_shifttype = ShiftType::LSL;
483 m_shift = shift;
484 }
485 ArithOption(ARM64Reg Rd, ShiftType shift_type, u32 shift)
486 {
487 m_destReg = Rd;
488 m_shift = shift;
489 m_shifttype = shift_type;
490 m_type = TypeSpecifier::ShiftedReg;
491 if (Is64Bit(Rd))
492 {
493 m_width = WidthSpecifier::Width64Bit;
494 if (shift == 64)
495 m_shift = 0;
496 m_extend = ExtendSpecifier::UXTX;
497 }
498 else
499 {
500 m_width = WidthSpecifier::Width32Bit;
501 if (shift == 32)
502 m_shift = 0;
503 m_extend = ExtendSpecifier::UXTW;
504 }
505 }
506 ARM64Reg GetReg() const { return m_destReg; }
507 u32 GetData() const
508 {
509 switch (m_type)
510 {
511 case TypeSpecifier::ExtendedReg:
512 return (static_cast<u32>(m_extend) << 13) | (m_shift << 10);
513 case TypeSpecifier::ShiftedReg:
514 return (static_cast<u32>(m_shifttype) << 22) | (m_shift << 10);
515 default:
516 DEBUG_ASSERT_MSG(DYNA_REC, false, "Invalid type in GetData");
517 break;
518 }
519 return 0;
520 }
521
522 bool IsExtended() const { return m_type == TypeSpecifier::ExtendedReg; }
523 };
524
525 struct LogicalImm
526 {
527 constexpr LogicalImm() {}
528
529 constexpr LogicalImm(u8 r_, u8 s_, bool n_) : r(r_), s(s_), n(n_), valid(true) {}
530
531 constexpr LogicalImm(u64 value, GPRSize size)
532 {
533 // Logical immediates are encoded using parameters n, imm_s and imm_r using
534 // the following table:
535 //
536 // N imms immr size S R
537 // 1 ssssss rrrrrr 64 UInt(ssssss) UInt(rrrrrr)
538 // 0 0sssss xrrrrr 32 UInt(sssss) UInt(rrrrr)
539 // 0 10ssss xxrrrr 16 UInt(ssss) UInt(rrrr)
540 // 0 110sss xxxrrr 8 UInt(sss) UInt(rrr)
541 // 0 1110ss xxxxrr 4 UInt(ss) UInt(rr)
542 // 0 11110s xxxxxr 2 UInt(s) UInt(r)
543 // (s bits must not be all set)
544 //
545 // A pattern is constructed of size bits, where the least significant S+1 bits
546 // are set. The pattern is rotated right by R, and repeated across a 32 or
547 // 64-bit value, depending on destination register width.
548
549 if (size == GPRSize::B32)
550 {
551 // To handle 32-bit logical immediates, the very easiest thing is to repeat
552 // the input value twice to make a 64-bit word. The correct encoding of that
553 // as a logical immediate will also be the correct encoding of the 32-bit
554 // value.
555
556 value = (value << 32) | (value & 0xFFFFFFFF);
557 }
558
559 if (value == 0 || (~value) == 0)
560 {
561 valid = false;
562 return;
563 }
564
565 // Normalize value, rotating it such that the LSB is 1:
566 // If LSB is already one, we mask away the trailing sequence of ones and
567 // pick the next sequence of ones. This ensures we get a complete element
568 // that has not been cut-in-half due to rotation across the word boundary.
569
570 const int rotation = std::countr_zero(value & (value + 1));
571 const u64 normalized = std::rotr(value, rotation);
572
573 const int element_size = std::countr_zero(normalized & (normalized + 1));
574 const int ones = std::countr_one(normalized);
575
576 // Check the value is repeating; also ensures element size is a power of two.
577
578 if (std::rotr(value, element_size) != value)
579 {
580 valid = false;
581 return;
582 }
583
584 // Now we're done. We just have to encode the S output in such a way that
585 // it gives both the number of set bits and the length of the repeated
586 // segment.
587
588 r = static_cast<u8>((element_size - rotation) & (element_size - 1));
589 s = static_cast<u8>((((~element_size + 1) << 1) | (ones - 1)) & 0x3f);
590 n = Common::ExtractBit<6>(element_size);
591
592 valid = true;
593 }
594
595 constexpr operator bool() const { return valid; }
596
597 u8 r = 0;
598 u8 s = 0;
599 bool n = false;
600 bool valid = false;
601 };
602
603 class ARM64XEmitter
604 {
605 friend class ARM64FloatEmitter;
606
607 private:
608 struct RegisterMove
609 {
610 ARM64Reg dst;
611 ARM64Reg src;
612 };
613
614 // Pointer to memory where code will be emitted to.
615 u8* m_code = nullptr;
616
617 // Pointer past the end of the memory region we're allowed to emit to.
618 // Writes that would reach this memory are refused and will set the m_write_failed flag instead.
619 u8* m_code_end = nullptr;
620
621 u8* m_lastCacheFlushEnd = nullptr;
622
623 // Set to true when a write request happens that would write past m_code_end.
624 // Must be cleared with SetCodePtr() afterwards.
625 bool m_write_failed = false;
626
627 void AddImmediate(ARM64Reg Rd, ARM64Reg Rn, u64 imm, bool shift, bool negative, bool flags);
628 void EncodeCompareBranchInst(u32 op, ARM64Reg Rt, const void* ptr);
629 void EncodeTestBranchInst(u32 op, ARM64Reg Rt, u8 bits, const void* ptr);
630 void EncodeUnconditionalBranchInst(u32 op, const void* ptr);
631 void EncodeUnconditionalBranchInst(u32 opc, u32 op2, u32 op3, u32 op4, ARM64Reg Rn);
632 void EncodeExceptionInst(u32 instenc, u32 imm);
633 void EncodeSystemInst(u32 op0, u32 op1, u32 CRn, u32 CRm, u32 op2, ARM64Reg Rt);
634 void EncodeArithmeticInst(u32 instenc, bool flags, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm,
635 ArithOption Option);
636 void EncodeArithmeticCarryInst(u32 op, bool flags, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
637 void EncodeCondCompareImmInst(u32 op, ARM64Reg Rn, u32 imm, u32 nzcv, CCFlags cond);
638 void EncodeCondCompareRegInst(u32 op, ARM64Reg Rn, ARM64Reg Rm, u32 nzcv, CCFlags cond);
639 void EncodeCondSelectInst(u32 instenc, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, CCFlags cond);
640 void EncodeData1SrcInst(u32 instenc, ARM64Reg Rd, ARM64Reg Rn);
641 void EncodeData2SrcInst(u32 instenc, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
642 void EncodeData3SrcInst(u32 instenc, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ARM64Reg Ra);
643 void EncodeLogicalInst(u32 instenc, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ArithOption Shift);
644 void EncodeLoadRegisterInst(u32 bitop, ARM64Reg Rt, u32 imm);
645 void EncodeLoadStoreExcInst(u32 instenc, ARM64Reg Rs, ARM64Reg Rt2, ARM64Reg Rn, ARM64Reg Rt);
646 void EncodeLoadStorePairedInst(u32 op, ARM64Reg Rt, ARM64Reg Rt2, ARM64Reg Rn, u32 imm);
647 void EncodeLoadStoreIndexedInst(u32 op, u32 op2, ARM64Reg Rt, ARM64Reg Rn, s32 imm);
648 void EncodeLoadStoreIndexedInst(u32 op, ARM64Reg Rt, ARM64Reg Rn, s32 imm, u8 size);
649 void EncodeMOVWideInst(u32 op, ARM64Reg Rd, u32 imm, ShiftAmount pos);
650 void EncodeBitfieldMOVInst(u32 op, ARM64Reg Rd, ARM64Reg Rn, u32 immr, u32 imms);
651 void EncodeLoadStoreRegisterOffset(u32 size, u32 opc, ARM64Reg Rt, ARM64Reg Rn, ArithOption Rm);
652 void EncodeAddSubImmInst(u32 op, bool flags, u32 shift, u32 imm, ARM64Reg Rn, ARM64Reg Rd);
653 void EncodeLogicalImmInst(u32 op, ARM64Reg Rd, ARM64Reg Rn, LogicalImm imm);
654 void EncodeLoadStorePair(u32 op, u32 load, IndexType type, ARM64Reg Rt, ARM64Reg Rt2, ARM64Reg Rn,
655 s32 imm);
656 void EncodeAddressInst(u32 op, ARM64Reg Rd, s32 imm);
657 void EncodeLoadStoreUnscaled(u32 size, u32 op, ARM64Reg Rt, ARM64Reg Rn, s32 imm);
658
659 [[nodiscard]] FixupBranch WriteFixupBranch();
660
661 // This function solves the "parallel moves" problem common in compilers.
662 // The arguments are mutated!
663 void ParallelMoves(RegisterMove* begin, RegisterMove* end, std::array<u8, 32>* source_gpr_usages);
664
665 template <typename T>
666 void MOVI2RImpl(ARM64Reg Rd, T imm);
667
668 protected:
669 void Write32(u32 value);
670
671 public:
672 ARM64XEmitter() = default;
673 ARM64XEmitter(u8* code, u8* code_end)
674 : m_code(code), m_code_end(code_end), m_lastCacheFlushEnd(code)
675 {
676 }
677
678 virtual ~ARM64XEmitter() {}
679
680 void SetCodePtr(u8* ptr, u8* end, bool write_failed = false);
681
682 void SetCodePtrUnsafe(u8* ptr, u8* end, bool write_failed = false);
683 const u8* GetCodePtr() const { return m_code; }
684 u8* GetWritableCodePtr() { return m_code; }
685 const u8* GetCodeEnd() const { return m_code_end; }
686 u8* GetWritableCodeEnd() { return m_code_end; }
687 void ReserveCodeSpace(u32 bytes);
688 u8* AlignCode16();
689 u8* AlignCodePage();
690 void FlushIcache();
691 void FlushIcacheSection(u8* start, u8* end);
692
693 // Should be checked after a block of code has been generated to see if the code has been
694 // successfully written to memory. Do not call the generated code when this returns true!
695 bool HasWriteFailed() const { return m_write_failed; }
696
697 // FixupBranch branching
698 void SetJumpTarget(FixupBranch const& branch);
699 [[nodiscard]] FixupBranch CBZ(ARM64Reg Rt);
700 [[nodiscard]] FixupBranch CBNZ(ARM64Reg Rt);
701 [[nodiscard]] FixupBranch B(CCFlags cond);
702 [[nodiscard]] FixupBranch TBZ(ARM64Reg Rt, u8 bit);
703 [[nodiscard]] FixupBranch TBNZ(ARM64Reg Rt, u8 bit);
704 [[nodiscard]] FixupBranch B();
705 [[nodiscard]] FixupBranch BL();
706
707 // Compare and Branch
708 void CBZ(ARM64Reg Rt, const void* ptr);
709 void CBNZ(ARM64Reg Rt, const void* ptr);
710
711 // Conditional Branch
712 void B(CCFlags cond, const void* ptr);
713
714 // Test and Branch
715 void TBZ(ARM64Reg Rt, u8 bits, const void* ptr);
716 void TBNZ(ARM64Reg Rt, u8 bits, const void* ptr);
717
718 // Unconditional Branch
719 void B(const void* ptr);
720 void BL(const void* ptr);
721
722 // Unconditional Branch (register)
723 void BR(ARM64Reg Rn);
724 void BLR(ARM64Reg Rn);
725 void RET(ARM64Reg Rn = ARM64Reg::X30);
726 void ERET();
727 void DRPS();
728
729 // Exception generation
730 void SVC(u32 imm);
731 void HVC(u32 imm);
732 void SMC(u32 imm);
733 void BRK(u32 imm);
734 void HLT(u32 imm);
735 void DCPS1(u32 imm);
736 void DCPS2(u32 imm);
737 void DCPS3(u32 imm);
738
739 // System
740 void _MSR(PStateField field, u8 imm);
741 void _MSR(PStateField field, ARM64Reg Rt);
742 void MRS(ARM64Reg Rt, PStateField field);
743 void CNTVCT(ARM64Reg Rt);
744
745 void HINT(SystemHint op);
746 void NOP() { HINT(SystemHint::NOP); }
747 void SEV() { HINT(SystemHint::SEV); }
748 void SEVL() { HINT(SystemHint::SEVL); }
749 void WFE() { HINT(SystemHint::WFE); }
750 void WFI() { HINT(SystemHint::WFI); }
751 void YIELD() { HINT(SystemHint::YIELD); }
752
753 void CLREX();
754 void DSB(BarrierType type);
755 void DMB(BarrierType type);
756 void ISB(BarrierType type);
757
758 // Add/Subtract (Extended/Shifted register)
759 void ADD(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
760 void ADD(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ArithOption Option);
761 void ADDS(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
762 void ADDS(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ArithOption Option);
763 void SUB(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
764 void SUB(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ArithOption Option);
765 void SUBS(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
766 void SUBS(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ArithOption Option);
767 void CMN(ARM64Reg Rn, ARM64Reg Rm);
768 void CMN(ARM64Reg Rn, ARM64Reg Rm, ArithOption Option);
769 void CMP(ARM64Reg Rn, ARM64Reg Rm);
770 void CMP(ARM64Reg Rn, ARM64Reg Rm, ArithOption Option);
771
772 // Add/Subtract (with carry)
773 void ADC(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
774 void ADCS(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
775 void SBC(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
776 void SBCS(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
777
778 // Conditional Compare (immediate)
779 void CCMN(ARM64Reg Rn, u32 imm, u32 nzcv, CCFlags cond);
780 void CCMP(ARM64Reg Rn, u32 imm, u32 nzcv, CCFlags cond);
781
782 // Conditional Compare (register)
783 void CCMN(ARM64Reg Rn, ARM64Reg Rm, u32 nzcv, CCFlags cond);
784 void CCMP(ARM64Reg Rn, ARM64Reg Rm, u32 nzcv, CCFlags cond);
785
786 // Conditional Select
787 void CSEL(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, CCFlags cond);
788 void CSINC(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, CCFlags cond);
789 void CSINV(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, CCFlags cond);
790 void CSNEG(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, CCFlags cond);
791
792 // Aliases
793 void CSET(ARM64Reg Rd, CCFlags cond)
794 {
795 ARM64Reg zr = Is64Bit(Rd) ? ARM64Reg::ZR : ARM64Reg::WZR;
796 CSINC(Rd, zr, zr, (CCFlags)((u32)cond ^ 1));
797 }
798 void CSETM(ARM64Reg Rd, CCFlags cond)
799 {
800 ARM64Reg zr = Is64Bit(Rd) ? ARM64Reg::ZR : ARM64Reg::WZR;
801 CSINV(Rd, zr, zr, (CCFlags)((u32)cond ^ 1));
802 }
803 void CNEG(ARM64Reg Rd, ARM64Reg Rn, CCFlags cond) { CSNEG(Rd, Rn, Rn, (CCFlags)((u32)cond ^ 1)); }
804 void NEG(ARM64Reg Rd, ARM64Reg Rs) { SUB(Rd, Is64Bit(Rd) ? ARM64Reg::ZR : ARM64Reg::WZR, Rs); }
805 void NEG(ARM64Reg Rd, ARM64Reg Rs, ArithOption Option)
806 {
807 SUB(Rd, Is64Bit(Rd) ? ARM64Reg::ZR : ARM64Reg::WZR, Rs, Option);
808 }
809 void NEGS(ARM64Reg Rd, ARM64Reg Rs) { SUBS(Rd, Is64Bit(Rd) ? ARM64Reg::ZR : ARM64Reg::WZR, Rs); }
810 void NEGS(ARM64Reg Rd, ARM64Reg Rs, ArithOption Option)
811 {
812 SUBS(Rd, Is64Bit(Rd) ? ARM64Reg::ZR : ARM64Reg::WZR, Rs, Option);
813 }
814 // Data-Processing 1 source
815 void RBIT(ARM64Reg Rd, ARM64Reg Rn);
816 void REV16(ARM64Reg Rd, ARM64Reg Rn);
817 void REV32(ARM64Reg Rd, ARM64Reg Rn);
818 void REV64(ARM64Reg Rd, ARM64Reg Rn);
819 void CLZ(ARM64Reg Rd, ARM64Reg Rn);
820 void CLS(ARM64Reg Rd, ARM64Reg Rn);
821
822 // Data-Processing 2 source
823 void UDIV(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
824 void SDIV(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
825 void LSLV(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
826 void LSRV(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
827 void ASRV(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
828 void RORV(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
829 void CRC32B(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
830 void CRC32H(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
831 void CRC32W(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
832 void CRC32CB(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
833 void CRC32CH(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
834 void CRC32CW(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
835 void CRC32X(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
836 void CRC32CX(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
837
838 // Data-Processing 3 source
839 void MADD(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ARM64Reg Ra);
840 void MSUB(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ARM64Reg Ra);
841 void SMADDL(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ARM64Reg Ra);
842 void SMULL(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
843 void SMSUBL(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ARM64Reg Ra);
844 void SMULH(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
845 void UMADDL(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ARM64Reg Ra);
846 void UMULL(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
847 void UMSUBL(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ARM64Reg Ra);
848 void UMULH(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
849 void MUL(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
850 void MNEG(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
851
852 // Logical (shifted register)
853 void AND(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ArithOption Shift);
854 void BIC(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ArithOption Shift);
855 void ORR(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ArithOption Shift);
856 void ORN(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ArithOption Shift);
857 void EOR(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ArithOption Shift);
858 void EON(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ArithOption Shift);
859 void ANDS(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ArithOption Shift);
860 void BICS(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ArithOption Shift);
861 void TST(ARM64Reg Rn, ARM64Reg Rm) { ANDS(Is64Bit(Rn) ? ARM64Reg::ZR : ARM64Reg::WZR, Rn, Rm); }
862 void TST(ARM64Reg Rn, ARM64Reg Rm, ArithOption Shift)
863 {
864 ANDS(Is64Bit(Rn) ? ARM64Reg::ZR : ARM64Reg::WZR, Rn, Rm, Shift);
865 }
866
867 // Wrap the above for saner syntax
868 void AND(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm)
869 {
870 AND(Rd, Rn, Rm, ArithOption(Rd, ShiftType::LSL, 0));
871 }
872 void BIC(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm)
873 {
874 BIC(Rd, Rn, Rm, ArithOption(Rd, ShiftType::LSL, 0));
875 }
876 void ORR(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm)
877 {
878 ORR(Rd, Rn, Rm, ArithOption(Rd, ShiftType::LSL, 0));
879 }
880 void ORN(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm)
881 {
882 ORN(Rd, Rn, Rm, ArithOption(Rd, ShiftType::LSL, 0));
883 }
884 void EOR(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm)
885 {
886 EOR(Rd, Rn, Rm, ArithOption(Rd, ShiftType::LSL, 0));
887 }
888 void EON(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm)
889 {
890 EON(Rd, Rn, Rm, ArithOption(Rd, ShiftType::LSL, 0));
891 }
892 void ANDS(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm)
893 {
894 ANDS(Rd, Rn, Rm, ArithOption(Rd, ShiftType::LSL, 0));
895 }
896 void BICS(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm)
897 {
898 BICS(Rd, Rn, Rm, ArithOption(Rd, ShiftType::LSL, 0));
899 }
900 // Convenience wrappers around ORR. These match the official convenience syntax.
901 void MOV(ARM64Reg Rd, ARM64Reg Rm, ArithOption Shift);
902 void MOV(ARM64Reg Rd, ARM64Reg Rm);
903 void MVN(ARM64Reg Rd, ARM64Reg Rm);
904
905 // Convenience wrappers around UBFM/EXTR.
906 void LSR(ARM64Reg Rd, ARM64Reg Rm, int shift);
907 void LSL(ARM64Reg Rd, ARM64Reg Rm, int shift);
908 void ASR(ARM64Reg Rd, ARM64Reg Rm, int shift);
909 void ROR(ARM64Reg Rd, ARM64Reg Rm, int shift);
910
911 // Logical (immediate)
912 void AND(ARM64Reg Rd, ARM64Reg Rn, LogicalImm imm);
913 void ANDS(ARM64Reg Rd, ARM64Reg Rn, LogicalImm imm);
914 void EOR(ARM64Reg Rd, ARM64Reg Rn, LogicalImm imm);
915 void ORR(ARM64Reg Rd, ARM64Reg Rn, LogicalImm imm);
916 void TST(ARM64Reg Rn, LogicalImm imm);
917 // Add/subtract (immediate)
918 void ADD(ARM64Reg Rd, ARM64Reg Rn, u32 imm, bool shift = false);
919 void ADDS(ARM64Reg Rd, ARM64Reg Rn, u32 imm, bool shift = false);
920 void SUB(ARM64Reg Rd, ARM64Reg Rn, u32 imm, bool shift = false);
921 void SUBS(ARM64Reg Rd, ARM64Reg Rn, u32 imm, bool shift = false);
922 void CMP(ARM64Reg Rn, u32 imm, bool shift = false);
923 void CMN(ARM64Reg Rn, u32 imm, bool shift = false);
924
925 // Data Processing (Immediate)
926 void MOVZ(ARM64Reg Rd, u32 imm, ShiftAmount pos = ShiftAmount::Shift0);
927 void MOVN(ARM64Reg Rd, u32 imm, ShiftAmount pos = ShiftAmount::Shift0);
928 void MOVK(ARM64Reg Rd, u32 imm, ShiftAmount pos = ShiftAmount::Shift0);
929
930 // Bitfield move
931 void BFM(ARM64Reg Rd, ARM64Reg Rn, u32 immr, u32 imms);
932 void SBFM(ARM64Reg Rd, ARM64Reg Rn, u32 immr, u32 imms);
933 void UBFM(ARM64Reg Rd, ARM64Reg Rn, u32 immr, u32 imms);
934 void BFI(ARM64Reg Rd, ARM64Reg Rn, u32 lsb, u32 width);
935 void BFXIL(ARM64Reg Rd, ARM64Reg Rn, u32 lsb, u32 width);
936 void UBFIZ(ARM64Reg Rd, ARM64Reg Rn, u32 lsb, u32 width);
937
938 // Extract register (ROR with two inputs, if same then faster on A67)
939 void EXTR(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, u32 shift);
940
941 // Aliases
942 void SXTB(ARM64Reg Rd, ARM64Reg Rn);
943 void SXTH(ARM64Reg Rd, ARM64Reg Rn);
944 void SXTW(ARM64Reg Rd, ARM64Reg Rn);
945 void UXTB(ARM64Reg Rd, ARM64Reg Rn);
946 void UXTH(ARM64Reg Rd, ARM64Reg Rn);
947
948 void UBFX(ARM64Reg Rd, ARM64Reg Rn, int lsb, int width) { UBFM(Rd, Rn, lsb, lsb + width - 1); }
949 // Load Register (Literal)
950 void LDR(ARM64Reg Rt, u32 imm);
951 void LDRSW(ARM64Reg Rt, u32 imm);
952 void PRFM(ARM64Reg Rt, u32 imm);
953
954 // Load/Store Exclusive
955 void STXRB(ARM64Reg Rs, ARM64Reg Rt, ARM64Reg Rn);
956 void STLXRB(ARM64Reg Rs, ARM64Reg Rt, ARM64Reg Rn);
957 void LDXRB(ARM64Reg Rt, ARM64Reg Rn);
958 void LDAXRB(ARM64Reg Rt, ARM64Reg Rn);
959 void STLRB(ARM64Reg Rt, ARM64Reg Rn);
960 void LDARB(ARM64Reg Rt, ARM64Reg Rn);
961 void STXRH(ARM64Reg Rs, ARM64Reg Rt, ARM64Reg Rn);
962 void STLXRH(ARM64Reg Rs, ARM64Reg Rt, ARM64Reg Rn);
963 void LDXRH(ARM64Reg Rt, ARM64Reg Rn);
964 void LDAXRH(ARM64Reg Rt, ARM64Reg Rn);
965 void STLRH(ARM64Reg Rt, ARM64Reg Rn);
966 void LDARH(ARM64Reg Rt, ARM64Reg Rn);
967 void STXR(ARM64Reg Rs, ARM64Reg Rt, ARM64Reg Rn);
968 void STLXR(ARM64Reg Rs, ARM64Reg Rt, ARM64Reg Rn);
969 void STXP(ARM64Reg Rs, ARM64Reg Rt, ARM64Reg Rt2, ARM64Reg Rn);
970 void STLXP(ARM64Reg Rs, ARM64Reg Rt, ARM64Reg Rt2, ARM64Reg Rn);
971 void LDXR(ARM64Reg Rt, ARM64Reg Rn);
972 void LDAXR(ARM64Reg Rt, ARM64Reg Rn);
973 void LDXP(ARM64Reg Rt, ARM64Reg Rt2, ARM64Reg Rn);
974 void LDAXP(ARM64Reg Rt, ARM64Reg Rt2, ARM64Reg Rn);
975 void STLR(ARM64Reg Rt, ARM64Reg Rn);
976 void LDAR(ARM64Reg Rt, ARM64Reg Rn);
977
978 // Load/Store no-allocate pair (offset)
979 void STNP(ARM64Reg Rt, ARM64Reg Rt2, ARM64Reg Rn, u32 imm);
980 void LDNP(ARM64Reg Rt, ARM64Reg Rt2, ARM64Reg Rn, u32 imm);
981
982 // Load/Store register (immediate indexed)
983 void STRB(IndexType type, ARM64Reg Rt, ARM64Reg Rn, s32 imm);
984 void LDRB(IndexType type, ARM64Reg Rt, ARM64Reg Rn, s32 imm);
985 void LDRSB(IndexType type, ARM64Reg Rt, ARM64Reg Rn, s32 imm);
986 void STRH(IndexType type, ARM64Reg Rt, ARM64Reg Rn, s32 imm);
987 void LDRH(IndexType type, ARM64Reg Rt, ARM64Reg Rn, s32 imm);
988 void LDRSH(IndexType type, ARM64Reg Rt, ARM64Reg Rn, s32 imm);
989 void STR(IndexType type, ARM64Reg Rt, ARM64Reg Rn, s32 imm);
990 void LDR(IndexType type, ARM64Reg Rt, ARM64Reg Rn, s32 imm);
991 void LDRSW(IndexType type, ARM64Reg Rt, ARM64Reg Rn, s32 imm);
992
993 // Load/Store register (register offset)
994 void STRB(ARM64Reg Rt, ARM64Reg Rn, ArithOption Rm);
995 void LDRB(ARM64Reg Rt, ARM64Reg Rn, ArithOption Rm);
996 void LDRSB(ARM64Reg Rt, ARM64Reg Rn, ArithOption Rm);
997 void STRH(ARM64Reg Rt, ARM64Reg Rn, ArithOption Rm);
998 void LDRH(ARM64Reg Rt, ARM64Reg Rn, ArithOption Rm);
999 void LDRSH(ARM64Reg Rt, ARM64Reg Rn, ArithOption Rm);
1000 void STR(ARM64Reg Rt, ARM64Reg Rn, ArithOption Rm);
1001 void LDR(ARM64Reg Rt, ARM64Reg Rn, ArithOption Rm);
1002 void LDRSW(ARM64Reg Rt, ARM64Reg Rn, ArithOption Rm);
1003 void PRFM(ARM64Reg Rt, ARM64Reg Rn, ArithOption Rm);
1004
1005 // Load/Store register (unscaled offset)
1006 void STURB(ARM64Reg Rt, ARM64Reg Rn, s32 imm);
1007 void LDURB(ARM64Reg Rt, ARM64Reg Rn, s32 imm);
1008 void LDURSB(ARM64Reg Rt, ARM64Reg Rn, s32 imm);
1009 void STURH(ARM64Reg Rt, ARM64Reg Rn, s32 imm);
1010 void LDURH(ARM64Reg Rt, ARM64Reg Rn, s32 imm);
1011 void LDURSH(ARM64Reg Rt, ARM64Reg Rn, s32 imm);
1012 void STUR(ARM64Reg Rt, ARM64Reg Rn, s32 imm);
1013 void LDUR(ARM64Reg Rt, ARM64Reg Rn, s32 imm);
1014 void LDURSW(ARM64Reg Rt, ARM64Reg Rn, s32 imm);
1015
1016 // Load/Store pair
1017 void LDP(IndexType type, ARM64Reg Rt, ARM64Reg Rt2, ARM64Reg Rn, s32 imm);
1018 void LDPSW(IndexType type, ARM64Reg Rt, ARM64Reg Rt2, ARM64Reg Rn, s32 imm);
1019 void STP(IndexType type, ARM64Reg Rt, ARM64Reg Rt2, ARM64Reg Rn, s32 imm);
1020
1021 // Address of label/page PC-relative
1022 void ADR(ARM64Reg Rd, s32 imm);
1023 void ADRP(ARM64Reg Rd, s64 imm);
1024
1025 // Wrapper around ADR/ADRP/MOVZ/MOVN/MOVK
1026 void MOVI2R(ARM64Reg Rd, u64 imm);
1027 bool MOVI2R2(ARM64Reg Rd, u64 imm1, u64 imm2);
1028 template <class P>
1029 void MOVP2R(ARM64Reg Rd, P* ptr)
1030 {
1031 ASSERT_MSG(DYNA_REC, Is64Bit(Rd), "Can't store pointers in 32-bit registers");
1032 MOVI2R(Rd, reinterpret_cast<uintptr_t>(ptr));
1033 }
1034 template <class P>
1035 // Given an address, stores the page address into a register and returns the page-relative offset
1036 s32 MOVPage2R(ARM64Reg Rd, P* ptr)
1037 {
1038 ASSERT_MSG(DYNA_REC, Is64Bit(Rd), "Can't store pointers in 32-bit registers");
1039 MOVI2R(Rd, reinterpret_cast<uintptr_t>(ptr) & ~0xFFFULL);
1040 return static_cast<s32>(reinterpret_cast<uintptr_t>(ptr) & 0xFFFULL);
1041 }
1042
1043 // Wrappers around bitwise operations with an immediate. If you're sure an imm can be encoded
1044 // without a scratch register, preferably construct a LogicalImm directly instead,
1045 // since that is constexpr and thus can be done at compile time for constant values.
1046 void ANDI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm, ARM64Reg scratch);
1047 void ANDSI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm, ARM64Reg scratch);
1048 void TSTI2R(ARM64Reg Rn, u64 imm, ARM64Reg scratch)
1049 {
1050 ANDSI2R(Is64Bit(Rn) ? ARM64Reg::ZR : ARM64Reg::WZR, Rn, imm, scratch);
1051 }
1052 void ORRI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm, ARM64Reg scratch);
1053 void EORI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm, ARM64Reg scratch);
1054
1055 // Wrappers around arithmetic operations with an immediate.
1056 void ADDI2R_internal(ARM64Reg Rd, ARM64Reg Rn, u64 imm, bool negative, bool flags,
1057 ARM64Reg scratch);
1058 void ADDI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm, ARM64Reg scratch = ARM64Reg::INVALID_REG);
1059 void ADDSI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm, ARM64Reg scratch = ARM64Reg::INVALID_REG);
1060 void SUBI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm, ARM64Reg scratch = ARM64Reg::INVALID_REG);
1061 void SUBSI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm, ARM64Reg scratch = ARM64Reg::INVALID_REG);
1062 void CMPI2R(ARM64Reg Rn, u64 imm, ARM64Reg scratch = ARM64Reg::INVALID_REG);
1063
1064 bool TryADDI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm);
1065 bool TrySUBI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm);
1066 bool TryCMPI2R(ARM64Reg Rn, u64 imm);
1067
1068 bool TryANDI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm);
1069 bool TryORRI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm);
1070 bool TryEORI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm);
1071
1072 // ABI related
1073 static constexpr BitSet32 CALLER_SAVED_GPRS = BitSet32(0x4007FFFF);
1074 static constexpr BitSet32 CALLER_SAVED_FPRS = BitSet32(0xFFFF00FF);
1075 void ABI_PushRegisters(BitSet32 registers);
1076 void ABI_PopRegisters(BitSet32 registers, BitSet32 ignore_mask = BitSet32(0));
1077
1078 // Plain function call
1079 void QuickCallFunction(ARM64Reg scratchreg, const void* func);
1080 template <typename T>
1081 void QuickCallFunction(ARM64Reg scratchreg, T func)
1082 {
1083 QuickCallFunction(scratchreg, (const void*)func);
1084 }
1085
1086 template <typename FuncRet, typename... FuncArgs, typename... Args>
1087 void ABI_CallFunction(FuncRet (*func)(FuncArgs...), Args... args)
1088 {
1089 static_assert(sizeof...(FuncArgs) == sizeof...(Args), "Wrong number of arguments");
1090 static_assert(sizeof...(FuncArgs) <= 8, "Passing arguments on the stack is not supported");
1091
1092 if constexpr (!std::is_void_v<FuncRet>)
1093 static_assert(sizeof(FuncRet) <= 16, "Large return types are not supported");
1094
1095 std::array<u8, 32> source_gpr_uses{};
1096
1097 auto check_argument = [&](auto& arg) {
1098 using Arg = std::decay_t<decltype(arg)>;
1099
1100 if constexpr (std::is_same_v<Arg, ARM64Reg>)
1101 {
1102 ASSERT(IsGPR(arg));
1103 source_gpr_uses[DecodeReg(arg)]++;
1104 }
1105 else
1106 {
1107 // To be more correct, we should be checking FuncArgs here rather than Args, but that's a
1108 // lot more effort to implement. Let's just do these best-effort checks for now.
1109 static_assert(!std::is_floating_point_v<Arg>, "Floating-point arguments are not supported");
1110 static_assert(sizeof(Arg) <= 8, "Arguments bigger than a register are not supported");
1111 }
1112 };
1113
1114 (check_argument(args), ...);
1115
1116 {
1117 Common::SmallVector<RegisterMove, sizeof...(Args)> pending_moves;
1118
1119 size_t i = 0;
1120
1121 auto handle_register_argument = [&](auto& arg) {
1122 using Arg = std::decay_t<decltype(arg)>;
1123
1124 if constexpr (std::is_same_v<Arg, ARM64Reg>)
1125 {
1126 const ARM64Reg dst_reg =
1127 (Is64Bit(arg) ? EncodeRegTo64 : EncodeRegTo32)(static_cast<ARM64Reg>(i));
1128
1129 if (dst_reg == arg)
1130 {
1131 // The value is already in the right register.
1132 source_gpr_uses[DecodeReg(arg)]--;
1133 }
1134 else if (source_gpr_uses[i] == 0)
1135 {
1136 // The destination register isn't used as the source of another move.
1137 // We can go ahead and do the move right away.
1138 MOV(dst_reg, arg);
1139 source_gpr_uses[DecodeReg(arg)]--;
1140 }
1141 else
1142 {
1143 // The destination register is used as the source of a move we haven't gotten to yet.
1144 // Let's record that we need to deal with this move later.
1145 pending_moves.emplace_back(dst_reg, arg);
1146 }
1147 }
1148
1149 ++i;
1150 };
1151
1152 (handle_register_argument(args), ...);
1153
1154 if (!pending_moves.empty())
1155 {
1156 ParallelMoves(pending_moves.data(), pending_moves.data() + pending_moves.size(),
1157 &source_gpr_uses);
1158 }
1159 }
1160
1161 {
1162 size_t i = 0;
1163
1164 auto handle_immediate_argument = [&](auto& arg) {
1165 using Arg = std::decay_t<decltype(arg)>;
1166
1167 if constexpr (!std::is_same_v<Arg, ARM64Reg>)
1168 {
1169 const ARM64Reg dst_reg =
1170 (sizeof(arg) == 8 ? EncodeRegTo64 : EncodeRegTo32)(static_cast<ARM64Reg>(i));
1171 if constexpr (std::is_pointer_v<Arg>)
1172 MOVP2R(dst_reg, arg);
1173 else
1174 MOVI2R(dst_reg, arg);
1175 }
1176
1177 ++i;
1178 };
1179
1180 (handle_immediate_argument(args), ...);
1181 }
1182
1183 QuickCallFunction(ARM64Reg::X8, func);
1184 }
1185
1186 // Utility to generate a call to a std::function object.
1187 //
1188 // Unfortunately, calling operator() directly is undefined behavior in C++
1189 // (this method might be a thunk in the case of multi-inheritance) so we
1190 // have to go through a trampoline function.
1191 template <typename T, typename... Args>
1192 static T CallLambdaTrampoline(const std::function<T(Args...)>* f, Args... args)
1193 {
1194 return (*f)(args...);
1195 }
1196
1197 template <typename FuncRet, typename... FuncArgs, typename... Args>
1198 void ABI_CallLambdaFunction(const std::function<FuncRet(FuncArgs...)>* f, Args... args)
1199 {
1200 auto trampoline = &ARM64XEmitter::CallLambdaTrampoline<FuncRet, FuncArgs...>;
1201 ABI_CallFunction(trampoline, f, args...);
1202 }
1203 };
1204
1205 class ARM64FloatEmitter
1206 {
1207 public:
1208 ARM64FloatEmitter(ARM64XEmitter* emit) : m_emit(emit) {}
1209 void LDR(u8 size, IndexType type, ARM64Reg Rt, ARM64Reg Rn, s32 imm);
1210 void STR(u8 size, IndexType type, ARM64Reg Rt, ARM64Reg Rn, s32 imm);
1211
1212 // Loadstore unscaled
1213 void LDUR(u8 size, ARM64Reg Rt, ARM64Reg Rn, s32 imm);
1214 void STUR(u8 size, ARM64Reg Rt, ARM64Reg Rn, s32 imm);
1215
1216 // Loadstore single structure
1217 void LD1(u8 size, ARM64Reg Rt, u8 index, ARM64Reg Rn);
1218 void LD1(u8 size, ARM64Reg Rt, u8 index, ARM64Reg Rn, ARM64Reg Rm);
1219 void LD1R(u8 size, ARM64Reg Rt, ARM64Reg Rn);
1220 void LD2R(u8 size, ARM64Reg Rt, ARM64Reg Rn);
1221 void LD1R(u8 size, ARM64Reg Rt, ARM64Reg Rn, ARM64Reg Rm);
1222 void LD2R(u8 size, ARM64Reg Rt, ARM64Reg Rn, ARM64Reg Rm);
1223 void ST1(u8 size, ARM64Reg Rt, u8 index, ARM64Reg Rn);
1224 void ST1(u8 size, ARM64Reg Rt, u8 index, ARM64Reg Rn, ARM64Reg Rm);
1225
1226 // Loadstore multiple structure
1227 void LD1(u8 size, u8 count, ARM64Reg Rt, ARM64Reg Rn);
1228 void LD1(u8 size, u8 count, IndexType type, ARM64Reg Rt, ARM64Reg Rn, ARM64Reg Rm = ARM64Reg::SP);
1229 void ST1(u8 size, u8 count, ARM64Reg Rt, ARM64Reg Rn);
1230 void ST1(u8 size, u8 count, IndexType type, ARM64Reg Rt, ARM64Reg Rn, ARM64Reg Rm = ARM64Reg::SP);
1231
1232 // Loadstore paired
1233 void LDP(u8 size, IndexType type, ARM64Reg Rt, ARM64Reg Rt2, ARM64Reg Rn, s32 imm);
1234 void STP(u8 size, IndexType type, ARM64Reg Rt, ARM64Reg Rt2, ARM64Reg Rn, s32 imm);
1235
1236 // Loadstore register offset
1237 void STR(u8 size, ARM64Reg Rt, ARM64Reg Rn, ArithOption Rm);
1238 void LDR(u8 size, ARM64Reg Rt, ARM64Reg Rn, ArithOption Rm);
1239
1240 // Scalar - 1 Source
1241 void FABS(ARM64Reg Rd, ARM64Reg Rn);
1242 void FNEG(ARM64Reg Rd, ARM64Reg Rn);
1243 void FSQRT(ARM64Reg Rd, ARM64Reg Rn);
1244 void FRINTI(ARM64Reg Rd, ARM64Reg Rn);
1245 void FMOV(ARM64Reg Rd, ARM64Reg Rn, bool top = false); // Also generalized move between GPR/FP
1246 void FRECPE(ARM64Reg Rd, ARM64Reg Rn);
1247 void FRSQRTE(ARM64Reg Rd, ARM64Reg Rn);
1248
1249 // Scalar - pairwise
1250 void FADDP(ARM64Reg Rd, ARM64Reg Rn);
1251 void FMAXP(ARM64Reg Rd, ARM64Reg Rn);
1252 void FMINP(ARM64Reg Rd, ARM64Reg Rn);
1253 void FMAXNMP(ARM64Reg Rd, ARM64Reg Rn);
1254 void FMINNMP(ARM64Reg Rd, ARM64Reg Rn);
1255
1256 // Scalar - 2 Source
1257 void ADD(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1258 void FADD(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1259 void FMUL(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1260 void FSUB(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1261 void FDIV(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1262 void FMAX(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1263 void FMIN(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1264 void FMAXNM(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1265 void FMINNM(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1266 void FNMUL(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1267
1268 // Scalar - 3 Source. Note - the accumulator is last on ARM!
1269 void FMADD(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ARM64Reg Ra);
1270 void FMSUB(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ARM64Reg Ra);
1271 void FNMADD(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ARM64Reg Ra);
1272 void FNMSUB(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ARM64Reg Ra);
1273
1274 // Scalar floating point immediate
1275 void FMOV(ARM64Reg Rd, uint8_t imm8);
1276
1277 // Vector
1278 void ADD(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1279 void AND(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1280 void BIC(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1281 void BIF(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1282 void BIT(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1283 void BSL(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1284 void DUP(u8 size, ARM64Reg Rd, ARM64Reg Rn, u8 index);
1285 void EOR(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1286 void FABS(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1287 void FADD(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1288 void FMAX(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1289 void FMLA(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1290 void FMLS(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1291 void FMIN(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1292 void FCVTL(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1293 void FCVTL2(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1294 void FCVTN(u8 dest_size, ARM64Reg Rd, ARM64Reg Rn);
1295 void FCVTN2(u8 dest_size, ARM64Reg Rd, ARM64Reg Rn);
1296 void FCVTZS(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1297 void FCVTZU(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1298 void FDIV(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1299 void FMUL(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1300 void FNEG(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1301 void FRECPE(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1302 void FRSQRTE(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1303 void FSUB(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1304 void NOT(ARM64Reg Rd, ARM64Reg Rn);
1305 void ORR(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1306 void ORN(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1307 void MOV(ARM64Reg Rd, ARM64Reg Rn) { ORR(Rd, Rn, Rn); }
1308 void REV16(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1309 void REV32(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1310 void REV64(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1311 void SCVTF(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1312 void UCVTF(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1313 void SCVTF(u8 size, ARM64Reg Rd, ARM64Reg Rn, int scale);
1314 void UCVTF(u8 size, ARM64Reg Rd, ARM64Reg Rn, int scale);
1315 void SQXTN(u8 dest_size, ARM64Reg Rd, ARM64Reg Rn);
1316 void SQXTN2(u8 dest_size, ARM64Reg Rd, ARM64Reg Rn);
1317 void UQXTN(u8 dest_size, ARM64Reg Rd, ARM64Reg Rn);
1318 void UQXTN2(u8 dest_size, ARM64Reg Rd, ARM64Reg Rn);
1319 void XTN(u8 dest_size, ARM64Reg Rd, ARM64Reg Rn);
1320 void XTN2(u8 dest_size, ARM64Reg Rd, ARM64Reg Rn);
1321
1322 // Move
1323 void DUP(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1324 void INS(u8 size, ARM64Reg Rd, u8 index, ARM64Reg Rn);
1325 void INS(u8 size, ARM64Reg Rd, u8 index1, ARM64Reg Rn, u8 index2);
1326 void UMOV(u8 size, ARM64Reg Rd, ARM64Reg Rn, u8 index);
1327 void SMOV(u8 size, ARM64Reg Rd, ARM64Reg Rn, u8 index);
1328
1329 // One source
1330 void FCVT(u8 size_to, u8 size_from, ARM64Reg Rd, ARM64Reg Rn);
1331
1332 // Scalar convert float to int, in a lot of variants.
1333 // Note that the scalar version of this operation has two encodings, one that goes to an integer
1334 // register
1335 // and one that outputs to a scalar fp register.
1336 void FCVTS(ARM64Reg Rd, ARM64Reg Rn, RoundingMode round);
1337 void FCVTU(ARM64Reg Rd, ARM64Reg Rn, RoundingMode round);
1338
1339 // Scalar convert int to float. No rounding mode specifier necessary.
1340 void SCVTF(ARM64Reg Rd, ARM64Reg Rn);
1341 void UCVTF(ARM64Reg Rd, ARM64Reg Rn);
1342
1343 // Scalar fixed point to float. scale is the number of fractional bits.
1344 void SCVTF(ARM64Reg Rd, ARM64Reg Rn, int scale);
1345 void UCVTF(ARM64Reg Rd, ARM64Reg Rn, int scale);
1346
1347 // Comparison
1348 void CMEQ(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1349 void CMEQ(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1350 void CMGE(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1351 void CMGE(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1352 void CMGT(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1353 void CMGT(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1354 void CMHI(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1355 void CMHS(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1356 void CMLE(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1357 void CMLT(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1358 void CMTST(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1359
1360 // Float comparison
1361 void FCMP(ARM64Reg Rn, ARM64Reg Rm);
1362 void FCMP(ARM64Reg Rn);
1363 void FCMPE(ARM64Reg Rn, ARM64Reg Rm);
1364 void FCMPE(ARM64Reg Rn);
1365 void FCMEQ(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1366 void FCMEQ(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1367 void FCMGE(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1368 void FCMGE(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1369 void FCMGT(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1370 void FCMGT(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1371 void FCMLE(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1372 void FCMLT(u8 size, ARM64Reg Rd, ARM64Reg Rn);
1373 void FACGE(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1374 void FACGT(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1375
1376 // Conditional select
1377 void FCSEL(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, CCFlags cond);
1378
1379 // Permute
1380 void UZP1(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1381 void TRN1(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1382 void ZIP1(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1383 void UZP2(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1384 void TRN2(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1385 void ZIP2(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1386
1387 // Extract
1388 void EXT(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, u32 index);
1389
1390 // Scalar shift by immediate
1391 void SHL(ARM64Reg Rd, ARM64Reg Rn, u32 shift);
1392 void URSHR(ARM64Reg Rd, ARM64Reg Rn, u32 shift);
1393
1394 // Vector shift by immediate
1395 void SHL(u8 src_size, ARM64Reg Rd, ARM64Reg Rn, u32 shift);
1396 void SSHLL(u8 src_size, ARM64Reg Rd, ARM64Reg Rn, u32 shift);
1397 void SSHLL2(u8 src_size, ARM64Reg Rd, ARM64Reg Rn, u32 shift);
1398 void SSHR(u8 src_size, ARM64Reg Rd, ARM64Reg Rn, u32 shift);
1399 void URSHR(u8 src_size, ARM64Reg Rd, ARM64Reg Rn, u32 shift);
1400 void USHLL(u8 src_size, ARM64Reg Rd, ARM64Reg Rn, u32 shift);
1401 void USHLL2(u8 src_size, ARM64Reg Rd, ARM64Reg Rn, u32 shift);
1402 void SHRN(u8 dest_size, ARM64Reg Rd, ARM64Reg Rn, u32 shift);
1403 void SHRN2(u8 dest_size, ARM64Reg Rd, ARM64Reg Rn, u32 shift);
1404 void SXTL(u8 src_size, ARM64Reg Rd, ARM64Reg Rn);
1405 void SXTL2(u8 src_size, ARM64Reg Rd, ARM64Reg Rn);
1406 void UXTL(u8 src_size, ARM64Reg Rd, ARM64Reg Rn);
1407 void UXTL2(u8 src_size, ARM64Reg Rd, ARM64Reg Rn);
1408
1409 // vector x indexed element
1410 void FMUL(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, u8 index);
1411 void FMLA(u8 esize, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, u8 index);
1412
1413 // Modified Immediate
1414 void MOVI(u8 size, ARM64Reg Rd, u64 imm, u8 shift = 0);
1415 void ORR(u8 size, ARM64Reg Rd, u8 imm, u8 shift = 0);
1416 void BIC(u8 size, ARM64Reg Rd, u8 imm, u8 shift = 0);
1417
1418 void MOVI2F(ARM64Reg Rd, float value, ARM64Reg scratch = ARM64Reg::INVALID_REG,
1419 bool negate = false);
1420 void MOVI2FDUP(ARM64Reg Rd, float value, ARM64Reg scratch = ARM64Reg::INVALID_REG);
1421
1422 // ABI related
1423 void ABI_PushRegisters(BitSet32 registers, ARM64Reg tmp = ARM64Reg::INVALID_REG);
1424 void ABI_PopRegisters(BitSet32 registers, ARM64Reg tmp = ARM64Reg::INVALID_REG);
1425
1426 private:
1427 ARM64XEmitter* m_emit;
1428 inline void Write32(u32 value) { m_emit->Write32(value); }
1429 // Emitting functions
1430 void EmitLoadStoreImmediate(u8 size, u32 opc, IndexType type, ARM64Reg Rt, ARM64Reg Rn, s32 imm);
1431 void EmitScalar2Source(bool M, bool S, u32 type, u32 opcode, ARM64Reg Rd, ARM64Reg Rn,
1432 ARM64Reg Rm);
1433 void EmitScalarThreeSame(bool U, u32 size, u32 opcode, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1434 void EmitThreeSame(bool U, u32 size, u32 opcode, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1435 void EmitCopy(bool Q, u32 op, u32 imm5, u32 imm4, ARM64Reg Rd, ARM64Reg Rn);
1436 void EmitScalar2RegMisc(bool U, u32 size, u32 opcode, ARM64Reg Rd, ARM64Reg Rn);
1437 void EmitScalarPairwise(bool U, u32 size, u32 opcode, ARM64Reg Rd, ARM64Reg Rn);
1438 void Emit2RegMisc(bool Q, bool U, u32 size, u32 opcode, ARM64Reg Rd, ARM64Reg Rn);
1439 void EmitLoadStoreSingleStructure(bool L, bool R, u32 opcode, bool S, u32 size, ARM64Reg Rt,
1440 ARM64Reg Rn);
1441 void EmitLoadStoreSingleStructure(bool L, bool R, u32 opcode, bool S, u32 size, ARM64Reg Rt,
1442 ARM64Reg Rn, ARM64Reg Rm);
1443 void Emit1Source(bool M, bool S, u32 type, u32 opcode, ARM64Reg Rd, ARM64Reg Rn);
1444 void EmitConversion(bool sf, bool S, u32 type, u32 rmode, u32 opcode, ARM64Reg Rd, ARM64Reg Rn);
1445 void EmitConversion2(bool sf, bool S, bool direction, u32 type, u32 rmode, u32 opcode, int scale,
1446 ARM64Reg Rd, ARM64Reg Rn);
1447 void EmitCompare(bool M, bool S, u32 op, u32 opcode2, ARM64Reg Rn, ARM64Reg Rm);
1448 void EmitCondSelect(bool M, bool S, CCFlags cond, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1449 void EmitPermute(u32 size, u32 op, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1450 void EmitExtract(u32 imm4, u32 op, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm);
1451 void EmitScalarImm(bool M, bool S, u32 type, u32 imm5, ARM64Reg Rd, u32 imm8);
1452 void EmitShiftImm(bool Q, bool U, u32 imm, u32 opcode, ARM64Reg Rd, ARM64Reg Rn);
1453 void EmitScalarShiftImm(bool U, u32 imm, u32 opcode, ARM64Reg Rd, ARM64Reg Rn);
1454 void EmitLoadStoreMultipleStructure(u32 size, bool L, u32 opcode, ARM64Reg Rt, ARM64Reg Rn);
1455 void EmitLoadStoreMultipleStructurePost(u32 size, bool L, u32 opcode, ARM64Reg Rt, ARM64Reg Rn,
1456 ARM64Reg Rm);
1457 void EmitScalar1Source(bool M, bool S, u32 type, u32 opcode, ARM64Reg Rd, ARM64Reg Rn);
1458 void EmitVectorxElement(bool U, u32 size, bool L, u32 opcode, bool H, ARM64Reg Rd, ARM64Reg Rn,
1459 ARM64Reg Rm);
1460 void EmitLoadStoreUnscaled(u32 size, u32 op, ARM64Reg Rt, ARM64Reg Rn, s32 imm);
1461 void EmitConvertScalarToInt(ARM64Reg Rd, ARM64Reg Rn, RoundingMode round, bool sign);
1462 void EmitScalar3Source(bool isDouble, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ARM64Reg Ra,
1463 int opcode);
1464 void EncodeLoadStorePair(u32 size, bool load, IndexType type, ARM64Reg Rt, ARM64Reg Rt2,
1465 ARM64Reg Rn, s32 imm);
1466 void EncodeLoadStoreRegisterOffset(u32 size, bool load, ARM64Reg Rt, ARM64Reg Rn, ArithOption Rm);
1467 void EncodeModImm(bool Q, u8 op, u8 cmode, u8 o2, ARM64Reg Rd, u8 abcdefgh);
1468
1469 void ORR_BIC(u8 size, ARM64Reg Rd, u8 imm, u8 shift, u8 op);
1470
1471 void SSHLL(u8 src_size, ARM64Reg Rd, ARM64Reg Rn, u32 shift, bool upper);
1472 void USHLL(u8 src_size, ARM64Reg Rd, ARM64Reg Rn, u32 shift, bool upper);
1473 void SHRN(u8 dest_size, ARM64Reg Rd, ARM64Reg Rn, u32 shift, bool upper);
1474 void SXTL(u8 src_size, ARM64Reg Rd, ARM64Reg Rn, bool upper);
1475 void UXTL(u8 src_size, ARM64Reg Rd, ARM64Reg Rn, bool upper);
1476 };
1477
1478 class ARM64CodeBlock : public Common::CodeBlock<ARM64XEmitter>
1479 {
1480 private:
1481 void PoisonMemory() override
1482 {
1483 // If our memory isn't a multiple of u32 then this won't write the last remaining bytes with
1484 // anything
1485 // Less than optimal, but there would be nothing we could do but throw a runtime warning anyway.
1486 // AArch64: 0xD4200000 = BRK 0
1487 constexpr u32 brk_0 = 0xD4200000;
1488
1489 for (size_t i = 0; i < region_size; i += sizeof(u32))
1490 {
1491 std::memcpy(region + i, &brk_0, sizeof(u32));
1492 }
1493 }
1494 };
1495 } // namespace Arm64Gen