xbyak_abi.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Copyright 2016 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <bitset>
  6. #include <initializer_list>
  7. #include <xbyak/xbyak.h>
  8. #include "common/assert.h"
  9. namespace Common::X64 {
  10. constexpr size_t RegToIndex(const Xbyak::Reg& reg) {
  11. using Kind = Xbyak::Reg::Kind;
  12. ASSERT_MSG((reg.getKind() & (Kind::REG | Kind::XMM)) != 0,
  13. "RegSet only support GPRs and XMM registers.");
  14. ASSERT_MSG(reg.getIdx() < 16, "RegSet only supports XXM0-15.");
  15. return static_cast<size_t>(reg.getIdx()) + (reg.getKind() == Kind::REG ? 0 : 16);
  16. }
  17. constexpr Xbyak::Reg64 IndexToReg64(size_t reg_index) {
  18. ASSERT(reg_index < 16);
  19. return Xbyak::Reg64(static_cast<int>(reg_index));
  20. }
  21. constexpr Xbyak::Xmm IndexToXmm(size_t reg_index) {
  22. ASSERT(reg_index >= 16 && reg_index < 32);
  23. return Xbyak::Xmm(static_cast<int>(reg_index - 16));
  24. }
  25. constexpr Xbyak::Reg IndexToReg(size_t reg_index) {
  26. if (reg_index < 16) {
  27. return IndexToReg64(reg_index);
  28. } else {
  29. return IndexToXmm(reg_index);
  30. }
  31. }
  32. constexpr std::bitset<32> BuildRegSet(std::initializer_list<Xbyak::Reg> regs) {
  33. size_t bits = 0;
  34. for (const Xbyak::Reg& reg : regs) {
  35. bits |= size_t{1} << RegToIndex(reg);
  36. }
  37. return {bits};
  38. }
  39. constexpr inline std::bitset<32> ABI_ALL_GPRS(0x0000FFFF);
  40. constexpr inline std::bitset<32> ABI_ALL_XMMS(0xFFFF0000);
  41. #ifdef _WIN32
  42. // Microsoft x64 ABI
  43. constexpr inline Xbyak::Reg ABI_RETURN = Xbyak::util::rax;
  44. constexpr inline Xbyak::Reg ABI_PARAM1 = Xbyak::util::rcx;
  45. constexpr inline Xbyak::Reg ABI_PARAM2 = Xbyak::util::rdx;
  46. constexpr inline Xbyak::Reg ABI_PARAM3 = Xbyak::util::r8;
  47. constexpr inline Xbyak::Reg ABI_PARAM4 = Xbyak::util::r9;
  48. constexpr inline std::bitset<32> ABI_ALL_CALLER_SAVED = BuildRegSet({
  49. // GPRs
  50. Xbyak::util::rcx,
  51. Xbyak::util::rdx,
  52. Xbyak::util::r8,
  53. Xbyak::util::r9,
  54. Xbyak::util::r10,
  55. Xbyak::util::r11,
  56. // XMMs
  57. Xbyak::util::xmm0,
  58. Xbyak::util::xmm1,
  59. Xbyak::util::xmm2,
  60. Xbyak::util::xmm3,
  61. Xbyak::util::xmm4,
  62. Xbyak::util::xmm5,
  63. });
  64. constexpr inline std::bitset<32> ABI_ALL_CALLEE_SAVED = BuildRegSet({
  65. // GPRs
  66. Xbyak::util::rbx,
  67. Xbyak::util::rsi,
  68. Xbyak::util::rdi,
  69. Xbyak::util::rbp,
  70. Xbyak::util::r12,
  71. Xbyak::util::r13,
  72. Xbyak::util::r14,
  73. Xbyak::util::r15,
  74. // XMMs
  75. Xbyak::util::xmm6,
  76. Xbyak::util::xmm7,
  77. Xbyak::util::xmm8,
  78. Xbyak::util::xmm9,
  79. Xbyak::util::xmm10,
  80. Xbyak::util::xmm11,
  81. Xbyak::util::xmm12,
  82. Xbyak::util::xmm13,
  83. Xbyak::util::xmm14,
  84. Xbyak::util::xmm15,
  85. });
  86. constexpr size_t ABI_SHADOW_SPACE = 0x20;
  87. #else
  88. // System V x86-64 ABI
  89. constexpr inline Xbyak::Reg ABI_RETURN = Xbyak::util::rax;
  90. constexpr inline Xbyak::Reg ABI_PARAM1 = Xbyak::util::rdi;
  91. constexpr inline Xbyak::Reg ABI_PARAM2 = Xbyak::util::rsi;
  92. constexpr inline Xbyak::Reg ABI_PARAM3 = Xbyak::util::rdx;
  93. constexpr inline Xbyak::Reg ABI_PARAM4 = Xbyak::util::rcx;
  94. constexpr inline std::bitset<32> ABI_ALL_CALLER_SAVED = BuildRegSet({
  95. // GPRs
  96. Xbyak::util::rcx,
  97. Xbyak::util::rdx,
  98. Xbyak::util::rdi,
  99. Xbyak::util::rsi,
  100. Xbyak::util::r8,
  101. Xbyak::util::r9,
  102. Xbyak::util::r10,
  103. Xbyak::util::r11,
  104. // XMMs
  105. Xbyak::util::xmm0,
  106. Xbyak::util::xmm1,
  107. Xbyak::util::xmm2,
  108. Xbyak::util::xmm3,
  109. Xbyak::util::xmm4,
  110. Xbyak::util::xmm5,
  111. Xbyak::util::xmm6,
  112. Xbyak::util::xmm7,
  113. Xbyak::util::xmm8,
  114. Xbyak::util::xmm9,
  115. Xbyak::util::xmm10,
  116. Xbyak::util::xmm11,
  117. Xbyak::util::xmm12,
  118. Xbyak::util::xmm13,
  119. Xbyak::util::xmm14,
  120. Xbyak::util::xmm15,
  121. });
  122. constexpr inline std::bitset<32> ABI_ALL_CALLEE_SAVED = BuildRegSet({
  123. // GPRs
  124. Xbyak::util::rbx,
  125. Xbyak::util::rbp,
  126. Xbyak::util::r12,
  127. Xbyak::util::r13,
  128. Xbyak::util::r14,
  129. Xbyak::util::r15,
  130. });
  131. constexpr size_t ABI_SHADOW_SPACE = 0;
  132. #endif
  133. struct ABIFrameInfo {
  134. s32 subtraction;
  135. s32 xmm_offset;
  136. };
  137. inline ABIFrameInfo ABI_CalculateFrameSize(std::bitset<32> regs, size_t rsp_alignment,
  138. size_t needed_frame_size) {
  139. const auto count = (regs & ABI_ALL_GPRS).count();
  140. rsp_alignment -= count * 8;
  141. size_t subtraction = 0;
  142. const auto xmm_count = (regs & ABI_ALL_XMMS).count();
  143. if (xmm_count) {
  144. // If we have any XMMs to save, we must align the stack here.
  145. subtraction = rsp_alignment & 0xF;
  146. }
  147. subtraction += 0x10 * xmm_count;
  148. size_t xmm_base_subtraction = subtraction;
  149. subtraction += needed_frame_size;
  150. subtraction += ABI_SHADOW_SPACE;
  151. // Final alignment.
  152. rsp_alignment -= subtraction;
  153. subtraction += rsp_alignment & 0xF;
  154. return ABIFrameInfo{static_cast<s32>(subtraction),
  155. static_cast<s32>(subtraction - xmm_base_subtraction)};
  156. }
  157. inline size_t ABI_PushRegistersAndAdjustStack(Xbyak::CodeGenerator& code, std::bitset<32> regs,
  158. size_t rsp_alignment, size_t needed_frame_size = 0) {
  159. auto frame_info = ABI_CalculateFrameSize(regs, rsp_alignment, needed_frame_size);
  160. for (size_t i = 0; i < regs.size(); ++i) {
  161. if (regs[i] && ABI_ALL_GPRS[i]) {
  162. code.push(IndexToReg64(i));
  163. }
  164. }
  165. if (frame_info.subtraction != 0) {
  166. code.sub(code.rsp, frame_info.subtraction);
  167. }
  168. for (size_t i = 0; i < regs.size(); ++i) {
  169. if (regs[i] && ABI_ALL_XMMS[i]) {
  170. code.movaps(code.xword[code.rsp + frame_info.xmm_offset], IndexToXmm(i));
  171. frame_info.xmm_offset += 0x10;
  172. }
  173. }
  174. return ABI_SHADOW_SPACE;
  175. }
  176. inline void ABI_PopRegistersAndAdjustStack(Xbyak::CodeGenerator& code, std::bitset<32> regs,
  177. size_t rsp_alignment, size_t needed_frame_size = 0) {
  178. auto frame_info = ABI_CalculateFrameSize(regs, rsp_alignment, needed_frame_size);
  179. for (size_t i = 0; i < regs.size(); ++i) {
  180. if (regs[i] && ABI_ALL_XMMS[i]) {
  181. code.movaps(IndexToXmm(i), code.xword[code.rsp + frame_info.xmm_offset]);
  182. frame_info.xmm_offset += 0x10;
  183. }
  184. }
  185. if (frame_info.subtraction != 0) {
  186. code.add(code.rsp, frame_info.subtraction);
  187. }
  188. // GPRs need to be popped in reverse order
  189. for (size_t j = 0; j < regs.size(); ++j) {
  190. const size_t i = regs.size() - j - 1;
  191. if (regs[i] && ABI_ALL_GPRS[i]) {
  192. code.pop(IndexToReg64(i));
  193. }
  194. }
  195. }
  196. } // namespace Common::X64