| 1 | // Copyright 2008 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "Common/x64ABI.h" |
| 5 | |
| 6 | #include "Common/CommonTypes.h" |
| 7 | #include "Common/x64Emitter.h" |
| 8 | |
| 9 | using namespace Gen; |
| 10 | |
| 11 | // Shared code between Win64 and Unix64 |
| 12 | |
| 13 | void XEmitter::ABI_CalculateFrameSize(BitSet32 mask, size_t rsp_alignment, size_t needed_frame_size, |
| 14 | size_t* shadowp, size_t* subtractionp, size_t* xmm_offsetp) |
| 15 | { |
| 16 | size_t shadow = 0; |
| 17 | #if defined(_WIN32) |
| 18 | shadow = 0x20; |
| 19 | #endif |
| 20 | |
| 21 | int count = (mask & ABI_ALL_GPRS).Count(); |
| 22 | rsp_alignment -= count * 8; |
| 23 | size_t subtraction = 0; |
| 24 | int fpr_count = (mask & ABI_ALL_FPRS).Count(); |
| 25 | if (fpr_count) |
| 26 | { |
| 27 | // If we have any XMMs to save, we must align the stack here. |
| 28 | subtraction = rsp_alignment & 0xf; |
| 29 | } |
| 30 | subtraction += 16 * fpr_count; |
| 31 | size_t xmm_base_subtraction = subtraction; |
| 32 | subtraction += needed_frame_size; |
| 33 | subtraction += shadow; |
| 34 | // Final alignment. |
| 35 | rsp_alignment -= subtraction; |
| 36 | subtraction += rsp_alignment & 0xf; |
| 37 | |
| 38 | *shadowp = shadow; |
| 39 | *subtractionp = subtraction; |
| 40 | *xmm_offsetp = subtraction - xmm_base_subtraction; |
| 41 | } |
| 42 | |
| 43 | size_t XEmitter::ABI_PushRegistersAndAdjustStack(BitSet32 mask, size_t rsp_alignment, |
| 44 | size_t needed_frame_size) |
| 45 | { |
| 46 | mask[RSP] = false; // Stack pointer is never pushed |
| 47 | size_t shadow, subtraction, xmm_offset; |
| 48 | ABI_CalculateFrameSize(mask, rsp_alignment, needed_frame_size, &shadow, &subtraction, |
| 49 | &xmm_offset); |
| 50 | |
| 51 | if (mask[RBP]) |
| 52 | { |
| 53 | // Make a nice stack frame for any debuggers or profilers that might be looking at this |
| 54 | PUSH(RBP); |
| 55 | MOV(64, R(RBP), R(RSP)); |
| 56 | } |
| 57 | for (int r : (mask & ABI_ALL_GPRS & ~BitSet32{RBP})) |
| 58 | PUSH((X64Reg)r); |
| 59 | |
| 60 | if (subtraction) |
| 61 | SUB(64, R(RSP), subtraction >= 0x80 ? Imm32((u32)subtraction) : Imm8((u8)subtraction)); |
| 62 | |
| 63 | for (int x : (mask & ABI_ALL_FPRS)) |
| 64 | { |
| 65 | MOVAPD(MDisp(RSP, (int)xmm_offset), (X64Reg)(x - 16)); |
| 66 | xmm_offset += 16; |
| 67 | } |
| 68 | |
| 69 | return shadow; |
| 70 | } |
| 71 | |
| 72 | void XEmitter::ABI_PopRegistersAndAdjustStack(BitSet32 mask, size_t rsp_alignment, |
| 73 | size_t needed_frame_size) |
| 74 | { |
| 75 | mask[RSP] = false; // Stack pointer is never pushed |
| 76 | size_t shadow, subtraction, xmm_offset; |
| 77 | ABI_CalculateFrameSize(mask, rsp_alignment, needed_frame_size, &shadow, &subtraction, |
| 78 | &xmm_offset); |
| 79 | |
| 80 | for (int x : (mask & ABI_ALL_FPRS)) |
| 81 | { |
| 82 | MOVAPD((X64Reg)(x - 16), MDisp(RSP, (int)xmm_offset)); |
| 83 | xmm_offset += 16; |
| 84 | } |
| 85 | |
| 86 | if (subtraction) |
| 87 | ADD(64, R(RSP), subtraction >= 0x80 ? Imm32((u32)subtraction) : Imm8((u8)subtraction)); |
| 88 | |
| 89 | for (int r = 15; r >= 0; r--) |
| 90 | { |
| 91 | if (r != RBP && mask[r]) |
| 92 | POP((X64Reg)r); |
| 93 | } |
| 94 | // RSP is pushed first and popped last to make debuggers/profilers happy |
| 95 | if (mask[RBP]) |
| 96 | POP(RBP); |
| 97 | } |
| 98 | |
| 99 | void XEmitter::MOVTwo(int bits, Gen::X64Reg dst1, Gen::X64Reg src1, s32 offset1, Gen::X64Reg dst2, |
| 100 | Gen::X64Reg src2) |
| 101 | { |
| 102 | if (dst1 == src2 && dst2 == src1) |
| 103 | { |
| 104 | XCHG(bits, R(src1), R(src2)); |
| 105 | if (offset1) |
| 106 | ADD(bits, R(dst1), Imm32(offset1)); |
| 107 | } |
| 108 | else if (src2 != dst1) |
| 109 | { |
| 110 | if (dst1 != src1 && offset1) |
| 111 | LEA(bits, dst1, MDisp(src1, offset1)); |
| 112 | else if (dst1 != src1) |
| 113 | MOV(bits, R(dst1), R(src1)); |
| 114 | else if (offset1) |
| 115 | ADD(bits, R(dst1), Imm32(offset1)); |
| 116 | if (dst2 != src2) |
| 117 | MOV(bits, R(dst2), R(src2)); |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | if (dst2 != src2) |
| 122 | MOV(bits, R(dst2), R(src2)); |
| 123 | if (dst1 != src1 && offset1) |
| 124 | LEA(bits, dst1, MDisp(src1, offset1)); |
| 125 | else if (dst1 != src1) |
| 126 | MOV(bits, R(dst1), R(src1)); |
| 127 | else if (offset1) |
| 128 | ADD(bits, R(dst1), Imm32(offset1)); |
| 129 | } |
| 130 | } |