abi.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. // Copyright (C) 2003 Dolphin Project.
  2. // This program is free software: you can redistribute it and/or modify
  3. // it under the terms of the GNU General Public License as published by
  4. // the Free Software Foundation, version 2.0 or later versions.
  5. // This program is distributed in the hope that it will be useful,
  6. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. // GNU General Public License 2.0 for more details.
  9. // A copy of the GPL 2.0 should have been included with the program.
  10. // If not, see http://www.gnu.org/licenses/
  11. // Official SVN repository and contact information can be found at
  12. // http://code.google.com/p/dolphin-emu/
  13. #include "x64_emitter.h"
  14. #include "abi.h"
  15. using namespace Gen;
  16. // Shared code between Win64 and Unix64
  17. // Sets up a __cdecl function.
  18. void XEmitter::ABI_EmitPrologue(int maxCallParams)
  19. {
  20. #ifdef _M_IX86
  21. // Don't really need to do anything
  22. #elif defined(_M_X86_64)
  23. #if _WIN32
  24. int stacksize = ((maxCallParams + 1) & ~1) * 8 + 8;
  25. // Set up a stack frame so that we can call functions
  26. // TODO: use maxCallParams
  27. SUB(64, R(RSP), Imm8(stacksize));
  28. #endif
  29. #else
  30. #error Arch not supported
  31. #endif
  32. }
  33. void XEmitter::ABI_EmitEpilogue(int maxCallParams)
  34. {
  35. #ifdef _M_IX86
  36. RET();
  37. #elif defined(_M_X86_64)
  38. #ifdef _WIN32
  39. int stacksize = ((maxCallParams+1)&~1)*8 + 8;
  40. ADD(64, R(RSP), Imm8(stacksize));
  41. #endif
  42. RET();
  43. #else
  44. #error Arch not supported
  45. #endif
  46. }
  47. #ifdef _M_IX86 // All32
  48. // Shared code between Win32 and Unix32
  49. void XEmitter::ABI_CallFunction(const void *func) {
  50. ABI_AlignStack(0);
  51. CALL(func);
  52. ABI_RestoreStack(0);
  53. }
  54. void XEmitter::ABI_CallFunctionC16(const void *func, u16 param1) {
  55. ABI_AlignStack(1 * 2);
  56. PUSH(16, Imm16(param1));
  57. CALL(func);
  58. ABI_RestoreStack(1 * 2);
  59. }
  60. void XEmitter::ABI_CallFunctionCC16(const void *func, u32 param1, u16 param2) {
  61. ABI_AlignStack(1 * 2 + 1 * 4);
  62. PUSH(16, Imm16(param2));
  63. PUSH(32, Imm32(param1));
  64. CALL(func);
  65. ABI_RestoreStack(1 * 2 + 1 * 4);
  66. }
  67. void XEmitter::ABI_CallFunctionC(const void *func, u32 param1) {
  68. ABI_AlignStack(1 * 4);
  69. PUSH(32, Imm32(param1));
  70. CALL(func);
  71. ABI_RestoreStack(1 * 4);
  72. }
  73. void XEmitter::ABI_CallFunctionCC(const void *func, u32 param1, u32 param2) {
  74. ABI_AlignStack(2 * 4);
  75. PUSH(32, Imm32(param2));
  76. PUSH(32, Imm32(param1));
  77. CALL(func);
  78. ABI_RestoreStack(2 * 4);
  79. }
  80. void XEmitter::ABI_CallFunctionCCC(const void *func, u32 param1, u32 param2, u32 param3) {
  81. ABI_AlignStack(3 * 4);
  82. PUSH(32, Imm32(param3));
  83. PUSH(32, Imm32(param2));
  84. PUSH(32, Imm32(param1));
  85. CALL(func);
  86. ABI_RestoreStack(3 * 4);
  87. }
  88. void XEmitter::ABI_CallFunctionCCP(const void *func, u32 param1, u32 param2, void *param3) {
  89. ABI_AlignStack(3 * 4);
  90. PUSH(32, ImmPtr(param3));
  91. PUSH(32, Imm32(param2));
  92. PUSH(32, Imm32(param1));
  93. CALL(func);
  94. ABI_RestoreStack(3 * 4);
  95. }
  96. void XEmitter::ABI_CallFunctionCCCP(const void *func, u32 param1, u32 param2,u32 param3, void *param4) {
  97. ABI_AlignStack(4 * 4);
  98. PUSH(32, ImmPtr(param4));
  99. PUSH(32, Imm32(param3));
  100. PUSH(32, Imm32(param2));
  101. PUSH(32, Imm32(param1));
  102. CALL(func);
  103. ABI_RestoreStack(4 * 4);
  104. }
  105. void XEmitter::ABI_CallFunctionP(const void *func, void *param1) {
  106. ABI_AlignStack(1 * 4);
  107. PUSH(32, ImmPtr(param1));
  108. CALL(func);
  109. ABI_RestoreStack(1 * 4);
  110. }
  111. void XEmitter::ABI_CallFunctionPA(const void *func, void *param1, const Gen::OpArg &arg2) {
  112. ABI_AlignStack(2 * 4);
  113. PUSH(32, arg2);
  114. PUSH(32, ImmPtr(param1));
  115. CALL(func);
  116. ABI_RestoreStack(2 * 4);
  117. }
  118. void XEmitter::ABI_CallFunctionPAA(const void *func, void *param1, const Gen::OpArg &arg2, const Gen::OpArg &arg3) {
  119. ABI_AlignStack(3 * 4);
  120. PUSH(32, arg3);
  121. PUSH(32, arg2);
  122. PUSH(32, ImmPtr(param1));
  123. CALL(func);
  124. ABI_RestoreStack(3 * 4);
  125. }
  126. void XEmitter::ABI_CallFunctionPPC(const void *func, void *param1, void *param2, u32 param3) {
  127. ABI_AlignStack(3 * 4);
  128. PUSH(32, Imm32(param3));
  129. PUSH(32, ImmPtr(param2));
  130. PUSH(32, ImmPtr(param1));
  131. CALL(func);
  132. ABI_RestoreStack(3 * 4);
  133. }
  134. // Pass a register as a parameter.
  135. void XEmitter::ABI_CallFunctionR(const void *func, X64Reg reg1) {
  136. ABI_AlignStack(1 * 4);
  137. PUSH(32, R(reg1));
  138. CALL(func);
  139. ABI_RestoreStack(1 * 4);
  140. }
  141. // Pass two registers as parameters.
  142. void XEmitter::ABI_CallFunctionRR(const void *func, Gen::X64Reg reg1, Gen::X64Reg reg2)
  143. {
  144. ABI_AlignStack(2 * 4);
  145. PUSH(32, R(reg2));
  146. PUSH(32, R(reg1));
  147. CALL(func);
  148. ABI_RestoreStack(2 * 4);
  149. }
  150. void XEmitter::ABI_CallFunctionAC(const void *func, const Gen::OpArg &arg1, u32 param2)
  151. {
  152. ABI_AlignStack(2 * 4);
  153. PUSH(32, Imm32(param2));
  154. PUSH(32, arg1);
  155. CALL(func);
  156. ABI_RestoreStack(2 * 4);
  157. }
  158. void XEmitter::ABI_CallFunctionACC(const void *func, const Gen::OpArg &arg1, u32 param2, u32 param3)
  159. {
  160. ABI_AlignStack(3 * 4);
  161. PUSH(32, Imm32(param3));
  162. PUSH(32, Imm32(param2));
  163. PUSH(32, arg1);
  164. CALL(func);
  165. ABI_RestoreStack(3 * 4);
  166. }
  167. void XEmitter::ABI_CallFunctionA(const void *func, const Gen::OpArg &arg1)
  168. {
  169. ABI_AlignStack(1 * 4);
  170. PUSH(32, arg1);
  171. CALL(func);
  172. ABI_RestoreStack(1 * 4);
  173. }
  174. void XEmitter::ABI_CallFunctionAA(const void *func, const Gen::OpArg &arg1, const Gen::OpArg &arg2)
  175. {
  176. ABI_AlignStack(2 * 4);
  177. PUSH(32, arg2);
  178. PUSH(32, arg1);
  179. CALL(func);
  180. ABI_RestoreStack(2 * 4);
  181. }
  182. void XEmitter::ABI_PushAllCalleeSavedRegsAndAdjustStack() {
  183. // Note: 4 * 4 = 16 bytes, so alignment is preserved.
  184. PUSH(EBP);
  185. PUSH(EBX);
  186. PUSH(ESI);
  187. PUSH(EDI);
  188. }
  189. void XEmitter::ABI_PopAllCalleeSavedRegsAndAdjustStack() {
  190. POP(EDI);
  191. POP(ESI);
  192. POP(EBX);
  193. POP(EBP);
  194. }
  195. unsigned int XEmitter::ABI_GetAlignedFrameSize(unsigned int frameSize) {
  196. frameSize += 4; // reserve space for return address
  197. unsigned int alignedSize =
  198. #ifdef __GNUC__
  199. (frameSize + 15) & -16;
  200. #else
  201. (frameSize + 3) & -4;
  202. #endif
  203. return alignedSize;
  204. }
  205. void XEmitter::ABI_AlignStack(unsigned int frameSize) {
  206. // Mac OS X requires the stack to be 16-byte aligned before every call.
  207. // Linux requires the stack to be 16-byte aligned before calls that put SSE
  208. // vectors on the stack, but since we do not keep track of which calls do that,
  209. // it is effectively every call as well.
  210. // Windows binaries compiled with MSVC do not have such a restriction*, but I
  211. // expect that GCC on Windows acts the same as GCC on Linux in this respect.
  212. // It would be nice if someone could verify this.
  213. // *However, the MSVC optimizing compiler assumes a 4-byte-aligned stack at times.
  214. unsigned int fillSize =
  215. ABI_GetAlignedFrameSize(frameSize) - (frameSize + 4);
  216. if (fillSize != 0) {
  217. SUB(32, R(ESP), Imm8(fillSize));
  218. }
  219. }
  220. void XEmitter::ABI_RestoreStack(unsigned int frameSize) {
  221. unsigned int alignedSize = ABI_GetAlignedFrameSize(frameSize);
  222. alignedSize -= 4; // return address is POPped at end of call
  223. if (alignedSize != 0) {
  224. ADD(32, R(ESP), Imm8(alignedSize));
  225. }
  226. }
  227. #else //64bit
  228. // Common functions
  229. void XEmitter::ABI_CallFunction(const void *func) {
  230. u64 distance = u64(func) - (u64(code) + 5);
  231. if (distance >= 0x0000000080000000ULL
  232. && distance < 0xFFFFFFFF80000000ULL) {
  233. // Far call
  234. MOV(64, R(RAX), ImmPtr(func));
  235. CALLptr(R(RAX));
  236. } else {
  237. CALL(func);
  238. }
  239. }
  240. void XEmitter::ABI_CallFunctionC16(const void *func, u16 param1) {
  241. MOV(32, R(ABI_PARAM1), Imm32((u32)param1));
  242. u64 distance = u64(func) - (u64(code) + 5);
  243. if (distance >= 0x0000000080000000ULL
  244. && distance < 0xFFFFFFFF80000000ULL) {
  245. // Far call
  246. MOV(64, R(RAX), ImmPtr(func));
  247. CALLptr(R(RAX));
  248. } else {
  249. CALL(func);
  250. }
  251. }
  252. void XEmitter::ABI_CallFunctionCC16(const void *func, u32 param1, u16 param2) {
  253. MOV(32, R(ABI_PARAM1), Imm32(param1));
  254. MOV(32, R(ABI_PARAM2), Imm32((u32)param2));
  255. u64 distance = u64(func) - (u64(code) + 5);
  256. if (distance >= 0x0000000080000000ULL
  257. && distance < 0xFFFFFFFF80000000ULL) {
  258. // Far call
  259. MOV(64, R(RAX), ImmPtr(func));
  260. CALLptr(R(RAX));
  261. } else {
  262. CALL(func);
  263. }
  264. }
  265. void XEmitter::ABI_CallFunctionC(const void *func, u32 param1) {
  266. MOV(32, R(ABI_PARAM1), Imm32(param1));
  267. u64 distance = u64(func) - (u64(code) + 5);
  268. if (distance >= 0x0000000080000000ULL
  269. && distance < 0xFFFFFFFF80000000ULL) {
  270. // Far call
  271. MOV(64, R(RAX), ImmPtr(func));
  272. CALLptr(R(RAX));
  273. } else {
  274. CALL(func);
  275. }
  276. }
  277. void XEmitter::ABI_CallFunctionCC(const void *func, u32 param1, u32 param2) {
  278. MOV(32, R(ABI_PARAM1), Imm32(param1));
  279. MOV(32, R(ABI_PARAM2), Imm32(param2));
  280. u64 distance = u64(func) - (u64(code) + 5);
  281. if (distance >= 0x0000000080000000ULL
  282. && distance < 0xFFFFFFFF80000000ULL) {
  283. // Far call
  284. MOV(64, R(RAX), ImmPtr(func));
  285. CALLptr(R(RAX));
  286. } else {
  287. CALL(func);
  288. }
  289. }
  290. void XEmitter::ABI_CallFunctionCCC(const void *func, u32 param1, u32 param2, u32 param3) {
  291. MOV(32, R(ABI_PARAM1), Imm32(param1));
  292. MOV(32, R(ABI_PARAM2), Imm32(param2));
  293. MOV(32, R(ABI_PARAM3), Imm32(param3));
  294. u64 distance = u64(func) - (u64(code) + 5);
  295. if (distance >= 0x0000000080000000ULL
  296. && distance < 0xFFFFFFFF80000000ULL) {
  297. // Far call
  298. MOV(64, R(RAX), ImmPtr(func));
  299. CALLptr(R(RAX));
  300. } else {
  301. CALL(func);
  302. }
  303. }
  304. void XEmitter::ABI_CallFunctionCCP(const void *func, u32 param1, u32 param2, void *param3) {
  305. MOV(32, R(ABI_PARAM1), Imm32(param1));
  306. MOV(32, R(ABI_PARAM2), Imm32(param2));
  307. MOV(64, R(ABI_PARAM3), ImmPtr(param3));
  308. u64 distance = u64(func) - (u64(code) + 5);
  309. if (distance >= 0x0000000080000000ULL
  310. && distance < 0xFFFFFFFF80000000ULL) {
  311. // Far call
  312. MOV(64, R(RAX), ImmPtr(func));
  313. CALLptr(R(RAX));
  314. } else {
  315. CALL(func);
  316. }
  317. }
  318. void XEmitter::ABI_CallFunctionCCCP(const void *func, u32 param1, u32 param2, u32 param3, void *param4) {
  319. MOV(32, R(ABI_PARAM1), Imm32(param1));
  320. MOV(32, R(ABI_PARAM2), Imm32(param2));
  321. MOV(32, R(ABI_PARAM3), Imm32(param3));
  322. MOV(64, R(ABI_PARAM4), ImmPtr(param4));
  323. u64 distance = u64(func) - (u64(code) + 5);
  324. if (distance >= 0x0000000080000000ULL
  325. && distance < 0xFFFFFFFF80000000ULL) {
  326. // Far call
  327. MOV(64, R(RAX), ImmPtr(func));
  328. CALLptr(R(RAX));
  329. } else {
  330. CALL(func);
  331. }
  332. }
  333. void XEmitter::ABI_CallFunctionP(const void *func, void *param1) {
  334. MOV(64, R(ABI_PARAM1), ImmPtr(param1));
  335. u64 distance = u64(func) - (u64(code) + 5);
  336. if (distance >= 0x0000000080000000ULL
  337. && distance < 0xFFFFFFFF80000000ULL) {
  338. // Far call
  339. MOV(64, R(RAX), ImmPtr(func));
  340. CALLptr(R(RAX));
  341. } else {
  342. CALL(func);
  343. }
  344. }
  345. void XEmitter::ABI_CallFunctionPA(const void *func, void *param1, const Gen::OpArg &arg2) {
  346. MOV(64, R(ABI_PARAM1), ImmPtr(param1));
  347. if (!arg2.IsSimpleReg(ABI_PARAM2))
  348. MOV(32, R(ABI_PARAM2), arg2);
  349. u64 distance = u64(func) - (u64(code) + 5);
  350. if (distance >= 0x0000000080000000ULL
  351. && distance < 0xFFFFFFFF80000000ULL) {
  352. // Far call
  353. MOV(64, R(RAX), ImmPtr(func));
  354. CALLptr(R(RAX));
  355. } else {
  356. CALL(func);
  357. }
  358. }
  359. void XEmitter::ABI_CallFunctionPAA(const void *func, void *param1, const Gen::OpArg &arg2, const Gen::OpArg &arg3) {
  360. MOV(64, R(ABI_PARAM1), ImmPtr(param1));
  361. if (!arg2.IsSimpleReg(ABI_PARAM2))
  362. MOV(32, R(ABI_PARAM2), arg2);
  363. if (!arg3.IsSimpleReg(ABI_PARAM3))
  364. MOV(32, R(ABI_PARAM3), arg3);
  365. u64 distance = u64(func) - (u64(code) + 5);
  366. if (distance >= 0x0000000080000000ULL
  367. && distance < 0xFFFFFFFF80000000ULL) {
  368. // Far call
  369. MOV(64, R(RAX), ImmPtr(func));
  370. CALLptr(R(RAX));
  371. } else {
  372. CALL(func);
  373. }
  374. }
  375. void XEmitter::ABI_CallFunctionPPC(const void *func, void *param1, void *param2, u32 param3) {
  376. MOV(64, R(ABI_PARAM1), ImmPtr(param1));
  377. MOV(64, R(ABI_PARAM2), ImmPtr(param2));
  378. MOV(32, R(ABI_PARAM3), Imm32(param3));
  379. u64 distance = u64(func) - (u64(code) + 5);
  380. if (distance >= 0x0000000080000000ULL
  381. && distance < 0xFFFFFFFF80000000ULL) {
  382. // Far call
  383. MOV(64, R(RAX), ImmPtr(func));
  384. CALLptr(R(RAX));
  385. } else {
  386. CALL(func);
  387. }
  388. }
  389. // Pass a register as a parameter.
  390. void XEmitter::ABI_CallFunctionR(const void *func, X64Reg reg1) {
  391. if (reg1 != ABI_PARAM1)
  392. MOV(32, R(ABI_PARAM1), R(reg1));
  393. u64 distance = u64(func) - (u64(code) + 5);
  394. if (distance >= 0x0000000080000000ULL
  395. && distance < 0xFFFFFFFF80000000ULL) {
  396. // Far call
  397. MOV(64, R(RAX), ImmPtr(func));
  398. CALLptr(R(RAX));
  399. } else {
  400. CALL(func);
  401. }
  402. }
  403. // Pass two registers as parameters.
  404. void XEmitter::ABI_CallFunctionRR(const void *func, X64Reg reg1, X64Reg reg2) {
  405. if (reg2 != ABI_PARAM1) {
  406. if (reg1 != ABI_PARAM1)
  407. MOV(64, R(ABI_PARAM1), R(reg1));
  408. if (reg2 != ABI_PARAM2)
  409. MOV(64, R(ABI_PARAM2), R(reg2));
  410. } else {
  411. if (reg2 != ABI_PARAM2)
  412. MOV(64, R(ABI_PARAM2), R(reg2));
  413. if (reg1 != ABI_PARAM1)
  414. MOV(64, R(ABI_PARAM1), R(reg1));
  415. }
  416. u64 distance = u64(func) - (u64(code) + 5);
  417. if (distance >= 0x0000000080000000ULL
  418. && distance < 0xFFFFFFFF80000000ULL) {
  419. // Far call
  420. MOV(64, R(RAX), ImmPtr(func));
  421. CALLptr(R(RAX));
  422. } else {
  423. CALL(func);
  424. }
  425. }
  426. void XEmitter::ABI_CallFunctionAC(const void *func, const Gen::OpArg &arg1, u32 param2)
  427. {
  428. if (!arg1.IsSimpleReg(ABI_PARAM1))
  429. MOV(32, R(ABI_PARAM1), arg1);
  430. MOV(32, R(ABI_PARAM2), Imm32(param2));
  431. u64 distance = u64(func) - (u64(code) + 5);
  432. if (distance >= 0x0000000080000000ULL
  433. && distance < 0xFFFFFFFF80000000ULL) {
  434. // Far call
  435. MOV(64, R(RAX), ImmPtr(func));
  436. CALLptr(R(RAX));
  437. } else {
  438. CALL(func);
  439. }
  440. }
  441. void XEmitter::ABI_CallFunctionACC(const void *func, const Gen::OpArg &arg1, u32 param2, u32 param3)
  442. {
  443. if (!arg1.IsSimpleReg(ABI_PARAM1))
  444. MOV(32, R(ABI_PARAM1), arg1);
  445. MOV(32, R(ABI_PARAM2), Imm32(param2));
  446. MOV(64, R(ABI_PARAM3), Imm64(param3));
  447. u64 distance = u64(func) - (u64(code) + 5);
  448. if (distance >= 0x0000000080000000ULL
  449. && distance < 0xFFFFFFFF80000000ULL) {
  450. // Far call
  451. MOV(64, R(RAX), ImmPtr(func));
  452. CALLptr(R(RAX));
  453. } else {
  454. CALL(func);
  455. }
  456. }
  457. void XEmitter::ABI_CallFunctionA(const void *func, const Gen::OpArg &arg1)
  458. {
  459. if (!arg1.IsSimpleReg(ABI_PARAM1))
  460. MOV(32, R(ABI_PARAM1), arg1);
  461. u64 distance = u64(func) - (u64(code) + 5);
  462. if (distance >= 0x0000000080000000ULL
  463. && distance < 0xFFFFFFFF80000000ULL) {
  464. // Far call
  465. MOV(64, R(RAX), ImmPtr(func));
  466. CALLptr(R(RAX));
  467. } else {
  468. CALL(func);
  469. }
  470. }
  471. void XEmitter::ABI_CallFunctionAA(const void *func, const Gen::OpArg &arg1, const Gen::OpArg &arg2)
  472. {
  473. if (!arg1.IsSimpleReg(ABI_PARAM1))
  474. MOV(32, R(ABI_PARAM1), arg1);
  475. if (!arg2.IsSimpleReg(ABI_PARAM2))
  476. MOV(32, R(ABI_PARAM2), arg2);
  477. u64 distance = u64(func) - (u64(code) + 5);
  478. if (distance >= 0x0000000080000000ULL
  479. && distance < 0xFFFFFFFF80000000ULL) {
  480. // Far call
  481. MOV(64, R(RAX), ImmPtr(func));
  482. CALLptr(R(RAX));
  483. } else {
  484. CALL(func);
  485. }
  486. }
  487. unsigned int XEmitter::ABI_GetAlignedFrameSize(unsigned int frameSize) {
  488. return frameSize;
  489. }
  490. #ifdef _WIN32
  491. // The Windows x64 ABI requires XMM6 - XMM15 to be callee saved. 10 regs.
  492. // But, not saving XMM4 and XMM5 breaks things in VS 2010, even though they are volatile regs.
  493. // Let's just save all 16.
  494. const int XMM_STACK_SPACE = 16 * 16;
  495. // Win64 Specific Code
  496. void XEmitter::ABI_PushAllCalleeSavedRegsAndAdjustStack() {
  497. //we only want to do this once
  498. PUSH(RBX);
  499. PUSH(RSI);
  500. PUSH(RDI);
  501. PUSH(RBP);
  502. PUSH(R12);
  503. PUSH(R13);
  504. PUSH(R14);
  505. PUSH(R15);
  506. ABI_AlignStack(0);
  507. // Do this after aligning, because before it's offset by 8.
  508. SUB(64, R(RSP), Imm32(XMM_STACK_SPACE));
  509. for (int i = 0; i < 16; ++i)
  510. MOVAPS(MDisp(RSP, i * 16), (X64Reg)(XMM0 + i));
  511. }
  512. void XEmitter::ABI_PopAllCalleeSavedRegsAndAdjustStack() {
  513. for (int i = 0; i < 16; ++i)
  514. MOVAPS((X64Reg)(XMM0 + i), MDisp(RSP, i * 16));
  515. ADD(64, R(RSP), Imm32(XMM_STACK_SPACE));
  516. ABI_RestoreStack(0);
  517. POP(R15);
  518. POP(R14);
  519. POP(R13);
  520. POP(R12);
  521. POP(RBP);
  522. POP(RDI);
  523. POP(RSI);
  524. POP(RBX);
  525. }
  526. // Win64 Specific Code
  527. void XEmitter::ABI_PushAllCallerSavedRegsAndAdjustStack() {
  528. PUSH(RCX);
  529. PUSH(RDX);
  530. PUSH(RSI);
  531. PUSH(RDI);
  532. PUSH(R8);
  533. PUSH(R9);
  534. PUSH(R10);
  535. PUSH(R11);
  536. // TODO: Callers preserve XMM4-5 (XMM0-3 are args.)
  537. ABI_AlignStack(0);
  538. }
  539. void XEmitter::ABI_PopAllCallerSavedRegsAndAdjustStack() {
  540. ABI_RestoreStack(0);
  541. POP(R11);
  542. POP(R10);
  543. POP(R9);
  544. POP(R8);
  545. POP(RDI);
  546. POP(RSI);
  547. POP(RDX);
  548. POP(RCX);
  549. }
  550. void XEmitter::ABI_AlignStack(unsigned int /*frameSize*/) {
  551. SUB(64, R(RSP), Imm8(0x28));
  552. }
  553. void XEmitter::ABI_RestoreStack(unsigned int /*frameSize*/) {
  554. ADD(64, R(RSP), Imm8(0x28));
  555. }
  556. #else
  557. // Unix64 Specific Code
  558. void XEmitter::ABI_PushAllCalleeSavedRegsAndAdjustStack() {
  559. PUSH(RBX);
  560. PUSH(RBP);
  561. PUSH(R12);
  562. PUSH(R13);
  563. PUSH(R14);
  564. PUSH(R15);
  565. PUSH(R15); //just to align stack. duped push/pop doesn't hurt.
  566. // TODO: XMM?
  567. }
  568. void XEmitter::ABI_PopAllCalleeSavedRegsAndAdjustStack() {
  569. POP(R15);
  570. POP(R15);
  571. POP(R14);
  572. POP(R13);
  573. POP(R12);
  574. POP(RBP);
  575. POP(RBX);
  576. }
  577. void XEmitter::ABI_PushAllCallerSavedRegsAndAdjustStack() {
  578. PUSH(RCX);
  579. PUSH(RDX);
  580. PUSH(RSI);
  581. PUSH(RDI);
  582. PUSH(R8);
  583. PUSH(R9);
  584. PUSH(R10);
  585. PUSH(R11);
  586. PUSH(R11);
  587. }
  588. void XEmitter::ABI_PopAllCallerSavedRegsAndAdjustStack() {
  589. POP(R11);
  590. POP(R11);
  591. POP(R10);
  592. POP(R9);
  593. POP(R8);
  594. POP(RDI);
  595. POP(RSI);
  596. POP(RDX);
  597. POP(RCX);
  598. }
  599. void XEmitter::ABI_AlignStack(unsigned int /*frameSize*/) {
  600. SUB(64, R(RSP), Imm8(0x08));
  601. }
  602. void XEmitter::ABI_RestoreStack(unsigned int /*frameSize*/) {
  603. ADD(64, R(RSP), Imm8(0x08));
  604. }
  605. #endif // WIN32
  606. #endif // 32bit