JitCompilerX86.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /*
  2. Copyright (c) 2018 tevador
  3. This file is part of RandomX.
  4. RandomX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. RandomX is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with RandomX. If not, see<http://www.gnu.org/licenses/>.
  14. */
  15. #include "JitCompilerX86.hpp"
  16. #include "Pcg32.hpp"
  17. #include <cstring>
  18. #include <stdexcept>
  19. #ifdef _WIN32
  20. #include <windows.h>
  21. #else
  22. #include <sys/types.h>
  23. #include <sys/mman.h>
  24. #ifndef MAP_ANONYMOUS
  25. #define MAP_ANONYMOUS MAP_ANON
  26. #endif
  27. #endif
  28. namespace RandomX {
  29. /*
  30. REGISTER ALLOCATION:
  31. rax -> temporary
  32. rbx -> MemoryRegisters& memory
  33. rcx -> temporary
  34. rdx -> temporary
  35. rsi -> convertible_t& scratchpad
  36. rdi -> "ic" (instruction counter)
  37. rbp -> beginning of VM stack
  38. rsp -> end of VM stack
  39. r8 -> "r0"
  40. r9 -> "r1"
  41. r10 -> "r2"
  42. r11 -> "r3"
  43. r12 -> "r4"
  44. r13 -> "r5"
  45. r14 -> "r6"
  46. r15 -> "r7"
  47. xmm0 -> temporary
  48. xmm1 -> temporary
  49. xmm2 -> "f2"
  50. xmm3 -> "f3"
  51. xmm4 -> "f4"
  52. xmm5 -> "f5"
  53. xmm6 -> "f6"
  54. xmm7 -> "f7"
  55. xmm8 -> "f0"
  56. xmm9 -> "f1"
  57. STACK STRUCTURE:
  58. |
  59. |
  60. | saved registers
  61. |
  62. v
  63. [rbp] RegisterFile& registerFile
  64. |
  65. |
  66. | VM stack
  67. |
  68. v
  69. [rsp] last element of VM stack
  70. */
  71. constexpr uint8_t ic3 = ((InstructionCount + 1) >> 24);
  72. constexpr uint8_t ic2 = ((InstructionCount + 1) >> 16);
  73. constexpr uint8_t ic1 = ((InstructionCount + 1) >> 8);
  74. constexpr uint8_t ic0 = ((InstructionCount + 1) >> 0);
  75. const uint8_t prologue[] = {
  76. 0x53, //push rbx
  77. 0x55, //push rbp
  78. #ifdef _WIN32
  79. 0x57, //push rdi
  80. 0x56, //push rsi
  81. #endif
  82. 0x41, 0x54, //push r12
  83. 0x41, 0x55, //push r13
  84. 0x41, 0x56, //push r14
  85. 0x41, 0x57, //push r15
  86. #ifdef _WIN32
  87. 0x48, 0x83, 0xec, 0x48, //sub rsp,0x48
  88. 0xf3, 0x0f, 0x7f, 0x74, 0x24, 0x30, //movdqu XMMWORD PTR[rsp + 0x30],xmm6
  89. 0xf3, 0x0f, 0x7f, 0x7c, 0x24, 0x20, //movdqu XMMWORD PTR[rsp + 0x20],xmm7
  90. 0xf3, 0x44, 0x0f, 0x7f, 0x44, 0x24, 0x10, //movdqu XMMWORD PTR[rsp + 0x10],xmm8
  91. 0xf3, 0x44, 0x0f, 0x7f, 0x0c, 0x24, //movdqu XMMWORD PTR[rsp],xmm9
  92. 0x51, //push rcx
  93. 0x48, 0x8b, 0xda, //mov rbx,rdx
  94. 0x49, 0x8b, 0xf0, //mov rsi,r8
  95. #else
  96. 0x57, //push rdi
  97. 0x48, 0x8b, 0xde, //mov rbx, rsi
  98. 0x48, 0x8b, 0xf2, //mov rsi, rdx
  99. 0x48, 0x8b, 0xcf, //mov rcx, rdi
  100. #endif
  101. 0x48, 0x8b, 0xec, //mov rbp,rsp
  102. 0x48, 0xc7, 0xc7, ic0, ic1, ic2, ic3, //mov rdi, "InstructionCount"
  103. 0x4c, 0x8b, 0x01, //mov r8,QWORD PTR[rcx]
  104. 0x4c, 0x8b, 0x49, 0x08, //mov r9,QWORD PTR[rcx+0x8]
  105. 0x4c, 0x8b, 0x51, 0x10, //mov r10,QWORD PTR[rcx+0x10]
  106. 0x4c, 0x8b, 0x59, 0x18, //mov r11,QWORD PTR[rcx+0x18]
  107. 0x4c, 0x8b, 0x61, 0x20, //mov r12,QWORD PTR[rcx+0x20]
  108. 0x4c, 0x8b, 0x69, 0x28, //mov r13,QWORD PTR[rcx+0x28]
  109. 0x4c, 0x8b, 0x71, 0x30, //mov r14,QWORD PTR[rcx+0x30]
  110. 0x4c, 0x8b, 0x79, 0x38, //mov r15,QWORD PTR[rcx+0x38]
  111. 0xc7, 0x44, 0x24, 0xf8, 0xc0, 0x9f, 0x00, //mov DWORD PTR[rsp-0x8],0x9fc0
  112. 0x00,
  113. 0x0f, 0xae, 0x54, 0x24, 0xf8, //ldmxcsr DWORD PTR[rsp-0x8]
  114. 0xf2, 0x4c, 0x0f, 0x2a, 0x41, 0x40, //cvtsi2sd xmm8,QWORD PTR[rcx+0x40]
  115. 0xf2, 0x4c, 0x0f, 0x2a, 0x49, 0x48, //cvtsi2sd xmm9,QWORD PTR[rcx+0x48]
  116. 0xf2, 0x48, 0x0f, 0x2a, 0x51, 0x50, //cvtsi2sd xmm2,QWORD PTR[rcx+0x50]
  117. 0xf2, 0x48, 0x0f, 0x2a, 0x59, 0x58, //cvtsi2sd xmm3,QWORD PTR[rcx+0x58]
  118. 0xf2, 0x48, 0x0f, 0x2a, 0x61, 0x60, //cvtsi2sd xmm4,QWORD PTR[rcx+0x60]
  119. 0xf2, 0x48, 0x0f, 0x2a, 0x69, 0x68, //cvtsi2sd xmm5,QWORD PTR[rcx+0x68]
  120. 0xf2, 0x48, 0x0f, 0x2a, 0x71, 0x70, //cvtsi2sd xmm6,QWORD PTR[rcx+0x70]
  121. 0xf2, 0x48, 0x0f, 0x2a, 0x79, 0x78, //cvtsi2sd xmm7,QWORD PTR[rcx+0x78]
  122. };
  123. const uint8_t epilogue[] = {
  124. 0x48, 0x8b, 0xe5, //mov rsp,rbp
  125. 0x59, //pop rcx
  126. 0x4c, 0x89, 0x01, //mov QWORD PTR [rcx],r8
  127. 0x4c, 0x89, 0x49, 0x08, //mov QWORD PTR [rcx+0x8],r9
  128. 0x4c, 0x89, 0x51, 0x10, //mov QWORD PTR [rcx+0x10],r10
  129. 0x4c, 0x89, 0x59, 0x18, //mov QWORD PTR [rcx+0x18],r11
  130. 0x4c, 0x89, 0x61, 0x20, //mov QWORD PTR [rcx+0x20],r12
  131. 0x4c, 0x89, 0x69, 0x28, //mov QWORD PTR [rcx+0x28],r13
  132. 0x4c, 0x89, 0x71, 0x30, //mov QWORD PTR [rcx+0x30],r14
  133. 0x4c, 0x89, 0x79, 0x38, //mov QWORD PTR [rcx+0x38],r15
  134. 0x66, 0x4c, 0x0f, 0x7e, 0x41, 0x40, //movq QWORD PTR [rcx+0x40],xmm8
  135. 0x66, 0x4c, 0x0f, 0x7e, 0x49, 0x48, //movq QWORD PTR [rcx+0x48],xmm9
  136. 0x66, 0x48, 0x0f, 0x7e, 0x51, 0x50, //movq QWORD PTR [rcx+0x50],xmm2
  137. 0x66, 0x48, 0x0f, 0x7e, 0x59, 0x58, //movq QWORD PTR [rcx+0x58],xmm3
  138. 0x66, 0x48, 0x0f, 0x7e, 0x61, 0x60, //movq QWORD PTR [rcx+0x60],xmm4
  139. 0x66, 0x48, 0x0f, 0x7e, 0x69, 0x68, //movq QWORD PTR [rcx+0x68],xmm5
  140. 0x66, 0x48, 0x0f, 0x7e, 0x71, 0x70, //movq QWORD PTR [rcx+0x70],xmm6
  141. 0x66, 0x48, 0x0f, 0x7e, 0x79, 0x78, //movq QWORD PTR [rcx+0x78],xmm7
  142. #ifdef _WIN32
  143. 0xf3, 0x44, 0x0f, 0x6f, 0x0c, 0x24, //movdqu xmm9,XMMWORD PTR [rsp]
  144. 0xf3, 0x44, 0x0f, 0x6f, 0x44, 0x24, 0x10, //movdqu xmm8,XMMWORD PTR [rsp+0x10]
  145. 0xf3, 0x0f, 0x6f, 0x7c, 0x24, 0x20, //movdqu xmm7,XMMWORD PTR [rsp+0x20]
  146. 0xf3, 0x0f, 0x6f, 0x74, 0x24, 0x30, //movdqu xmm6,XMMWORD PTR [rsp+0x30]
  147. 0x48, 0x83, 0xc4, 0x48, //add rsp,0x48
  148. #endif
  149. 0x41, 0x5f, //pop r15
  150. 0x41, 0x5e, //pop r14
  151. 0x41, 0x5d, //pop r13
  152. 0x41, 0x5c, //pop r12
  153. #ifdef _WIN32
  154. 0x5e, //pop rsi
  155. 0x5f, //pop rdi
  156. #endif
  157. 0x5d, //pop rbp
  158. 0x5b, //pop rbx
  159. 0xc3, //ret
  160. };
  161. //41 bytes -> 1 cache line
  162. const uint8_t readDatasetSub[] = {
  163. 0x8b, 0x13, //mov edx,DWORD PTR [rbx]
  164. 0x48, 0x8b, 0x43, 0x08, //mov rax,QWORD PTR [rbx+0x8]
  165. 0x48, 0x8b, 0x04, 0x10, //mov rax,QWORD PTR [rax+rdx*1]
  166. 0x83, 0x03, 0x08, //add DWORD PTR [rbx],0x8
  167. 0x33, 0x4b, 0x04, //xor ecx,DWORD PTR [rbx+0x4]
  168. 0x89, 0x4b, 0x04, //mov DWORD PTR [rbx+0x4],ecx
  169. 0xf7, 0xc1, 0xf8, 0xff, 0x00, 0x00, //test ecx,0xfff8
  170. 0x75, 0x0d, //jne
  171. 0x83, 0xe1, 0xf8, //and ecx,0xfffffff8
  172. 0x89, 0x0b, //mov DWORD PTR [rbx],ecx
  173. 0x48, 0x8b, 0x53, 0x08, //mov rdx,QWORD PTR [rbx+0x8]
  174. 0x0f, 0x18, 0x0c, 0x0a, //prefetcht0 BYTE PTR [rdx+rcx*1]
  175. 0xc3, //ret
  176. };
  177. constexpr int getNumCacheLines(size_t size) {
  178. return (size + (CacheLineSize - 1)) / CacheLineSize;
  179. }
  180. constexpr int32_t align(int32_t pos, int32_t align) {
  181. return ((pos - 1) / align + 1) * align;
  182. }
  183. constexpr int32_t readDatasetSubOffset = CodeSize - CacheLineSize * getNumCacheLines(sizeof(readDatasetSub));
  184. constexpr int32_t epilogueOffset = readDatasetSubOffset - CacheLineSize * getNumCacheLines(sizeof(epilogue));
  185. constexpr int32_t startOffsetAligned = align(sizeof(prologue), CacheLineSize);
  186. JitCompilerX86::JitCompilerX86() {
  187. #ifdef _WIN32
  188. code = (uint8_t*)VirtualAlloc(nullptr, CodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  189. if (code == nullptr)
  190. throw std::runtime_error("VirtualAlloc failed");
  191. #else
  192. code = (uint8_t*)mmap(nullptr, CodeSize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  193. if (code == (uint8_t*)-1)
  194. throw std::runtime_error("mmap failed");
  195. #endif
  196. memcpy(code, prologue, sizeof(prologue));
  197. codePos = sizeof(prologue);
  198. if (startOffsetAligned - codePos > 4) {
  199. emitByte(0xeb);
  200. emitByte(startOffsetAligned - (codePos + 1));
  201. }
  202. else {
  203. while (codePos < startOffsetAligned)
  204. emitByte(0x90); //nop
  205. }
  206. memcpy(code + readDatasetSubOffset, readDatasetSub, sizeof(readDatasetSub));
  207. memcpy(code + epilogueOffset, epilogue, sizeof(epilogue));
  208. }
  209. void JitCompilerX86::generateProgram(Pcg32& gen) {
  210. instructionOffsets.clear();
  211. callOffsets.clear();
  212. codePos = startOffsetAligned;
  213. Instruction instr;
  214. for (unsigned i = 0; i < ProgramLength; ++i) {
  215. for (unsigned j = 0; j < sizeof(instr) / sizeof(Pcg32::result_type); ++j) {
  216. *(((uint32_t*)&instr) + j) = gen();
  217. }
  218. generateCode(instr, i);
  219. }
  220. emitByte(0xe9);
  221. emit(instructionOffsets[0] - (codePos + 4));
  222. fixCallOffsets();
  223. }
  224. void JitCompilerX86::generateCode(Instruction& instr, int i) {
  225. instructionOffsets.push_back(codePos);
  226. emit(0x840fcfff); //dec edx; jz <epilogue>
  227. emit(epilogueOffset - (codePos + 4)); //jump offset (RIP-relative)
  228. gena(instr);
  229. auto generator = engine[instr.opcode];
  230. (this->*generator)(instr, i);
  231. }
  232. void JitCompilerX86::fixCallOffsets() {
  233. for (CallOffset& co : callOffsets) {
  234. *reinterpret_cast<int32_t*>(code + co.pos) = instructionOffsets[co.index] - (co.pos + 4);
  235. }
  236. }
  237. void JitCompilerX86::gena(Instruction& instr) {
  238. emit(uint16_t(0x8149)); //xor
  239. emitByte(0xf0 + (instr.rega % RegistersCount));
  240. emit(instr.addra);
  241. int32_t pc;
  242. switch (instr.loca & 7)
  243. {
  244. case 0:
  245. case 1:
  246. case 2:
  247. case 3:
  248. emit(uint16_t(0x8b41)); //mov
  249. emitByte(0xc8 + (instr.rega % RegistersCount)); //ecx, rega
  250. emitByte(0xe8); //call
  251. emit(readDatasetSubOffset - (codePos + 4));
  252. return;
  253. case 4:
  254. emit(uint16_t(0x8b41)); //mov
  255. emitByte(0xc0 + (instr.rega % RegistersCount)); //eax, rega
  256. emitByte(0x25); //and
  257. emit(ScratchpadL2 - 1); //whole scratchpad
  258. emit(0xc6048b48); // mov rax,QWORD PTR [rsi+rax*8]
  259. return;
  260. default:
  261. emit(uint16_t(0x8b41)); //mov
  262. emitByte(0xc0 + (instr.rega % RegistersCount)); //eax, rega
  263. emitByte(0x25); //and
  264. emit(ScratchpadL1 - 1); //first 16 KiB of scratchpad
  265. emit(0xc6048b48); // mov rax,QWORD PTR [rsi+rax*8]
  266. return;
  267. }
  268. }
  269. void JitCompilerX86::genbr0(Instruction& instr, uint16_t opcodeReg, uint16_t opcodeImm) {
  270. if ((instr.locb & 7) <= 5) {
  271. emit(uint16_t(0x8b49)); //mov
  272. emitByte(0xc8 + (instr.regb % RegistersCount)); //rcx, regb
  273. emitByte(0x48); //REX.W
  274. emit(opcodeReg); //xxx rax, cl
  275. }
  276. else {
  277. emitByte(0x48); //REX.W
  278. emit(opcodeImm); //xxx rax, imm8
  279. emitByte((instr.imm8 & 63));
  280. }
  281. }
  282. void JitCompilerX86::genbr1(Instruction& instr, uint16_t opcodeReg, uint16_t opcodeImm) {
  283. if ((instr.locb & 7) <= 5) {
  284. emit(opcodeReg); // xxx rax, r64
  285. emitByte(0xc0 + (instr.regb % RegistersCount));
  286. }
  287. else {
  288. emit(opcodeImm); // xxx rax, imm32
  289. emit(instr.imm32);
  290. }
  291. }
  292. void JitCompilerX86::genbr132(Instruction& instr, uint16_t opcodeReg, uint8_t opcodeImm) {
  293. if ((instr.locb & 7) <= 5) {
  294. emit(opcodeReg); // xxx eax, r32
  295. emitByte(0xc0 + (instr.regb % RegistersCount));
  296. }
  297. else {
  298. emitByte(opcodeImm); // xxx eax, imm32
  299. emit(instr.imm32);
  300. }
  301. }
  302. void JitCompilerX86::genbf(Instruction& instr, uint8_t opcode) {
  303. emit(0x48f2fffff8002548); //and rax,0xfffffffffffff800; cvtsi2sd xmm0,rax
  304. emit(uint16_t(0x2a0f));
  305. emitByte(0xc0);
  306. if ((instr.locb & 7) <= 5) {
  307. int regb = (instr.regb % RegistersCount);
  308. emitByte(0xf2); //xxxsd xmm0,regb
  309. if (regb <= 1) {
  310. emitByte(0x41); //REX
  311. }
  312. emitByte(0x0f);
  313. emitByte(opcode);
  314. emitByte(0xc0 + regb);
  315. }
  316. else {
  317. convertible_t bimm;
  318. bimm.f64 = (double)instr.imm32;
  319. emit(uint16_t(0xb848)); //movabs rax,imm64
  320. emit(bimm.i64);
  321. emitByte(0x66); //movq xmm1,rax
  322. emit(0xc86e0f48);
  323. emit(uint16_t(0x0ff2)); //xxxsd xmm0,xmm1
  324. emitByte(opcode);
  325. emitByte(0xc1);
  326. }
  327. }
  328. void JitCompilerX86::gencr(Instruction& instr) {
  329. switch (instr.locc & 7)
  330. {
  331. case 0:
  332. emit(0x41c88b48); //mov rcx, rax; REX
  333. emitByte(0x8b); // mov
  334. emitByte(0xc0 + (instr.regc % RegistersCount)); //eax, regc
  335. emitByte(0x35); // xor eax
  336. emit(instr.addrc);
  337. emitByte(0x25); //and
  338. emit(ScratchpadL2 - 1); //whole scratchpad
  339. emit(0xc60c8948); // mov QWORD PTR [rsi+rax*8],rcx
  340. break;
  341. case 1:
  342. case 2:
  343. case 3:
  344. emit(0x41c88b48); //mov rcx, rax; REX
  345. emitByte(0x8b); // mov
  346. emitByte(0xc0 + (instr.regc % RegistersCount)); //eax, regc
  347. emitByte(0x35); // xor eax
  348. emit(instr.addrc);
  349. emitByte(0x25); //and
  350. emit(ScratchpadL1 - 1); //first 16 KiB of scratchpad
  351. emit(0xc60c8948); // mov QWORD PTR [rsi+rax*8],rcx
  352. break;
  353. default:
  354. emit(uint16_t(0x8b4c)); //mov
  355. emitByte(0xc0 + 8 * (instr.regc % RegistersCount)); //regc, rax
  356. break;
  357. }
  358. }
  359. void JitCompilerX86::gencf(Instruction& instr) {
  360. int regc = (instr.regc % RegistersCount);
  361. switch (instr.locc & 7)
  362. {
  363. case 0:
  364. emit(uint16_t(0x8b41)); //mov
  365. emitByte(0xc0 + regc); //eax, regc
  366. emitByte(0x35); // xor eax
  367. emit(instr.addrc);
  368. emitByte(0x25); //and
  369. emit(ScratchpadL2 - 1); //whole scratchpad
  370. emit(uint16_t(0x4866)); //prefix
  371. emit(0xc6047e0f); // movq QWORD PTR [rsi+rax*8],xmm0
  372. break;
  373. case 1:
  374. case 2:
  375. case 3:
  376. emit(uint16_t(0x8b41)); //mov
  377. emitByte(0xc0 + regc); //eax, regc
  378. emitByte(0x35); // xor eax
  379. emit(instr.addrc);
  380. emitByte(0x25); //and
  381. emit(ScratchpadL1 - 1); //first 16 KiB of scratchpad
  382. emit(uint16_t(0x4866)); //prefix
  383. emit(0xc6047e0f); // movq QWORD PTR [rsi+rax*8],xmm0
  384. break;
  385. default:
  386. emitByte(0xf2);
  387. if (regc <= 1) {
  388. emitByte(0x44); //REX
  389. }
  390. emit(uint16_t(0x100f)); //movsd
  391. emitByte(0xc0 + 8 * regc); // regc, xmm0
  392. break;
  393. }
  394. }
  395. void JitCompilerX86::h_ADD_64(Instruction& instr, int i) {
  396. genbr1(instr, 0x0349, 0x0548);
  397. gencr(instr);
  398. }
  399. void JitCompilerX86::h_ADD_32(Instruction& instr, int i) {
  400. genbr132(instr, 0x0341, 0x05);
  401. gencr(instr);
  402. }
  403. void JitCompilerX86::h_SUB_64(Instruction& instr, int i) {
  404. genbr1(instr, 0x2b49, 0x2d48);
  405. gencr(instr);
  406. }
  407. void JitCompilerX86::h_SUB_32(Instruction& instr, int i) {
  408. genbr132(instr, 0x2b41, 0x2d);
  409. gencr(instr);
  410. }
  411. void JitCompilerX86::h_MUL_64(Instruction& instr, int i) {
  412. if ((instr.locb & 7) <= 5) {
  413. emitByte(0x49); //REX
  414. emit(uint16_t(0xaf0f)); // imul rax, r64
  415. emitByte(0xc0 + (instr.regb % RegistersCount));
  416. }
  417. else {
  418. emitByte(0x48); //REX
  419. emit(uint16_t(0xc069)); // imul rax, rax, imm32
  420. emit(instr.imm32);
  421. }
  422. gencr(instr);
  423. }
  424. void JitCompilerX86::h_MULH_64(Instruction& instr, int i) {
  425. if ((instr.locb & 7) <= 5) {
  426. emit(uint16_t(0x8b49)); //mov rcx, r64
  427. emitByte(0xc8 + (instr.regb % RegistersCount));
  428. }
  429. else {
  430. emitByte(0x48);
  431. emit(uint16_t(0xc1c7)); // mov rcx, imm32
  432. emit(instr.imm32);
  433. }
  434. emitByte(0x48);
  435. emit(uint16_t(0xe1f7)); // mul rcx
  436. emitByte(0x48);
  437. emit(uint16_t(0xc28b)); // mov rax,rdx
  438. gencr(instr);
  439. }
  440. void JitCompilerX86::h_MUL_32(Instruction& instr, int i) {
  441. emit(uint16_t(0xc88b)); //mov ecx, eax
  442. if ((instr.locb & 7) <= 5) {
  443. emit(uint16_t(0x8b41)); // mov eax, r32
  444. emitByte(0xc0 + (instr.regb % RegistersCount));
  445. }
  446. else {
  447. emitByte(0xb8); // mov eax, imm32
  448. emit(instr.imm32);
  449. }
  450. emit(0xc1af0f48); //imul rax,rcx
  451. gencr(instr);
  452. }
  453. void JitCompilerX86::h_IMUL_32(Instruction& instr, int i) {
  454. emitByte(0x48);
  455. emit(uint16_t(0xc863)); //movsxd rcx,eax
  456. if ((instr.locb & 7) <= 5) {
  457. emit(uint16_t(0x6349)); //movsxd rax,r32
  458. emitByte(0xc0 + (instr.regb % RegistersCount));
  459. }
  460. else {
  461. emitByte(0x48);
  462. emit(uint16_t(0xc0c7)); // mov rax, imm32
  463. emit(instr.imm32);
  464. }
  465. emit(0xc1af0f48); //imul rax,rcx
  466. gencr(instr);
  467. }
  468. void JitCompilerX86::h_IMULH_64(Instruction& instr, int i) {
  469. if ((instr.locb & 7) <= 5) {
  470. emit(uint16_t(0x8b49)); //mov rcx, r64
  471. emitByte(0xc8 + (instr.regb % RegistersCount));
  472. }
  473. else {
  474. emitByte(0x48);
  475. emit(uint16_t(0xc1c7)); // mov rcx, imm32
  476. emit(instr.imm32);
  477. }
  478. emitByte(0x48);
  479. emit(uint16_t(0xe9f7)); // imul rcx
  480. emitByte(0x48);
  481. emit(uint16_t(0xc28b)); // mov rax,rdx
  482. gencr(instr);
  483. }
  484. void JitCompilerX86::h_DIV_64(Instruction& instr, int i) {
  485. if ((instr.locb & 7) <= 5) {
  486. emitByte(0xb9); //mov ecx, 1
  487. emit(1);
  488. emit(uint16_t(0x8b41)); //mov edx, r32
  489. emitByte(0xd0 + (instr.regb % RegistersCount));
  490. emit(0x450fd285); //test edx, edx; cmovne ecx,edx
  491. emitByte(0xca);
  492. }
  493. else {
  494. emitByte(0xb9); //mov ecx, imm32
  495. emit(instr.imm32 != 0 ? instr.imm32 : 1);
  496. }
  497. emit(0xf748d233); //xor edx,edx; div rcx
  498. emitByte(0xf1);
  499. gencr(instr);
  500. }
  501. void JitCompilerX86::h_IDIV_64(Instruction& instr, int i) {
  502. if ((instr.locb & 7) <= 5) {
  503. emit(uint16_t(0x8b41)); //mov edx, r32
  504. emitByte(0xd0 + (instr.regb % RegistersCount));
  505. }
  506. else {
  507. emitByte(0xba); // xxx edx, imm32
  508. emit(instr.imm32);
  509. }
  510. emit(0xc88b480b75fffa83);
  511. emit(0x1274c9ff48c1d148);
  512. emit(0x0fd28500000001b9);
  513. emit(0x489948c96348ca45);
  514. emit(uint16_t(0xf9f7)); //idiv rcx
  515. gencr(instr);
  516. }
  517. void JitCompilerX86::h_AND_64(Instruction& instr, int i) {
  518. genbr1(instr, 0x2349, 0x2548);
  519. gencr(instr);
  520. }
  521. void JitCompilerX86::h_AND_32(Instruction& instr, int i) {
  522. genbr132(instr, 0x2341, 0x25);
  523. gencr(instr);
  524. }
  525. void JitCompilerX86::h_OR_64(Instruction& instr, int i) {
  526. genbr1(instr, 0x0b49, 0x0d48);
  527. gencr(instr);
  528. }
  529. void JitCompilerX86::h_OR_32(Instruction& instr, int i) {
  530. genbr132(instr, 0x0b41, 0x0d);
  531. gencr(instr);
  532. }
  533. void JitCompilerX86::h_XOR_64(Instruction& instr, int i) {
  534. genbr1(instr, 0x3349, 0x3548);
  535. gencr(instr);
  536. }
  537. void JitCompilerX86::h_XOR_32(Instruction& instr, int i) {
  538. genbr132(instr, 0x3341, 0x35);
  539. gencr(instr);
  540. }
  541. void JitCompilerX86::h_SHL_64(Instruction& instr, int i) {
  542. genbr0(instr, 0xe0d3, 0xe0c1);
  543. gencr(instr);
  544. }
  545. void JitCompilerX86::h_SHR_64(Instruction& instr, int i) {
  546. genbr0(instr, 0xe8d3, 0xe8c1);
  547. gencr(instr);
  548. }
  549. void JitCompilerX86::h_SAR_64(Instruction& instr, int i) {
  550. genbr0(instr, 0xf8d3, 0xf8c1);
  551. gencr(instr);
  552. }
  553. void JitCompilerX86::h_ROL_64(Instruction& instr, int i) {
  554. genbr0(instr, 0xc0d3, 0xc0c1);
  555. gencr(instr);
  556. }
  557. void JitCompilerX86::h_ROR_64(Instruction& instr, int i) {
  558. genbr0(instr, 0xc8d3, 0xc8c1);
  559. gencr(instr);
  560. }
  561. void JitCompilerX86::h_FPADD(Instruction& instr, int i) {
  562. genbf(instr, 0x58);
  563. gencf(instr);
  564. }
  565. void JitCompilerX86::h_FPSUB(Instruction& instr, int i) {
  566. genbf(instr, 0x5c);
  567. gencf(instr);
  568. }
  569. void JitCompilerX86::h_FPMUL(Instruction& instr, int i) {
  570. emit(uint16_t(0x0d48)); //or rax,0x800
  571. emit(0x00000800);
  572. genbf(instr, 0x59);
  573. gencf(instr);
  574. }
  575. void JitCompilerX86::h_FPDIV(Instruction& instr, int i) {
  576. emit(uint16_t(0x0d48)); //or rax,0x800
  577. emit(0x00000800);
  578. genbf(instr, 0x5e);
  579. gencf(instr);
  580. }
  581. void JitCompilerX86::h_FPSQRT(Instruction& instr, int i) {
  582. emit(uint16_t(0xb948)); //or movabs rcx, imm64
  583. emit(0x7ffffffffffff800);
  584. emit(0xc02a0f48f2c12348); //and rax,rcx; cvtsi2sd xmm0,rax
  585. emit(0xc0510ff2); //sqrtsd xmm0,xmm0
  586. gencf(instr);
  587. }
  588. void JitCompilerX86::h_FPROUND(Instruction& instr, int i) {
  589. emit(0x81480de0c1c88b48);
  590. emit(0x600025fffff800e1);
  591. emit(0x0dc12a0f48f20000);
  592. emit(0xf824448900009fc0);
  593. emit(0x2454ae0f); //ldmxcsr DWORD PTR [rsp-0x8]
  594. emitByte(0xf8);
  595. gencf(instr);
  596. }
  597. static inline uint8_t jumpCondition(Instruction& instr, bool invert = false) {
  598. switch ((instr.locb & 7) ^ invert)
  599. {
  600. case 0:
  601. return 0x76; //jbe
  602. case 1:
  603. return 0x77; //ja
  604. case 2:
  605. return 0x78; //js
  606. case 3:
  607. return 0x79; //jns
  608. case 4:
  609. return 0x70; //jo
  610. case 5:
  611. return 0x71; //jno
  612. case 6:
  613. return 0x7c; //jl
  614. case 7:
  615. return 0x7d; //jge
  616. }
  617. }
  618. void JitCompilerX86::h_CALL(Instruction& instr, int i) {
  619. emit(uint16_t(0x8141)); //cmp regb, imm32
  620. emitByte(0xf8 + (instr.regb % RegistersCount));
  621. emit(instr.imm32);
  622. emitByte(jumpCondition(instr));
  623. if ((instr.locc & 7) <= 3) {
  624. emitByte(0x16);
  625. }
  626. else {
  627. emitByte(0x05);
  628. }
  629. gencr(instr);
  630. emit(uint16_t(0x06eb)); //jmp to next
  631. emitByte(0x50); //push rax
  632. emitByte(0xe8); //call
  633. i = wrapInstr(i + (instr.imm8 & 127) + 2);
  634. if (i < instructionOffsets.size()) {
  635. emit(instructionOffsets[i] - (codePos + 4));
  636. }
  637. else {
  638. callOffsets.push_back(CallOffset(codePos, i));
  639. codePos += 4;
  640. }
  641. }
  642. void JitCompilerX86::h_RET(Instruction& instr, int i) {
  643. int crlen = 0;
  644. if ((instr.locc & 7) <= 3) {
  645. crlen = 17;
  646. }
  647. emit(0x74e53b48); //cmp rsp, rbp; je
  648. emitByte(11 + crlen);
  649. emitByte(0x48);
  650. emit(0x08244433); //xor rax,QWORD PTR [rsp+0x8]
  651. gencr(instr);
  652. emitByte(0xc2); //ret 8
  653. emit(uint16_t(0x0008));
  654. gencr(instr);
  655. }
  656. #include "instructionWeights.hpp"
  657. #define INST_HANDLE(x) REPN(&JitCompilerX86::h_##x, WT(x))
  658. InstructionGeneratorX86 JitCompilerX86::engine[256] = {
  659. INST_HANDLE(ADD_64)
  660. INST_HANDLE(ADD_32)
  661. INST_HANDLE(SUB_64)
  662. INST_HANDLE(SUB_32)
  663. INST_HANDLE(MUL_64)
  664. INST_HANDLE(MULH_64)
  665. INST_HANDLE(MUL_32)
  666. INST_HANDLE(IMUL_32)
  667. INST_HANDLE(IMULH_64)
  668. INST_HANDLE(DIV_64)
  669. INST_HANDLE(IDIV_64)
  670. INST_HANDLE(AND_64)
  671. INST_HANDLE(AND_32)
  672. INST_HANDLE(OR_64)
  673. INST_HANDLE(OR_32)
  674. INST_HANDLE(XOR_64)
  675. INST_HANDLE(XOR_32)
  676. INST_HANDLE(SHL_64)
  677. INST_HANDLE(SHR_64)
  678. INST_HANDLE(SAR_64)
  679. INST_HANDLE(ROL_64)
  680. INST_HANDLE(ROR_64)
  681. INST_HANDLE(FPADD)
  682. INST_HANDLE(FPSUB)
  683. INST_HANDLE(FPMUL)
  684. INST_HANDLE(FPDIV)
  685. INST_HANDLE(FPSQRT)
  686. INST_HANDLE(FPROUND)
  687. INST_HANDLE(CALL)
  688. INST_HANDLE(RET)
  689. };
  690. }