xbyak_abi.h 5.8 KB

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