assembly_generator_x86.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. Copyright (c) 2018-2019, tevador <tevador@gmail.com>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of the copyright holder nor the
  12. names of its contributors may be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  18. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  20. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  22. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include <climits>
  26. #include "assembly_generator_x86.hpp"
  27. #include "common.hpp"
  28. #include "reciprocal.h"
  29. #include "program.hpp"
  30. #include "superscalar.hpp"
  31. namespace randomx {
  32. static const char* regR[] = { "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
  33. static const char* regR32[] = { "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d" };
  34. static const char* regFE[] = { "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7" };
  35. static const char* regF[] = { "xmm0", "xmm1", "xmm2", "xmm3" };
  36. static const char* regE[] = { "xmm4", "xmm5", "xmm6", "xmm7" };
  37. static const char* regA[] = { "xmm8", "xmm9", "xmm10", "xmm11" };
  38. static const char* tempRegx = "xmm12";
  39. static const char* mantissaMaskReg = "xmm13";
  40. static const char* exponentMaskReg = "xmm14";
  41. static const char* scaleMaskReg = "xmm15";
  42. static const char* regIc = "rbx";
  43. static const char* regIc32 = "ebx";
  44. static const char* regIc8 = "bl";
  45. static const char* regScratchpadAddr = "rsi";
  46. void AssemblyGeneratorX86::generateProgram(Program& prog) {
  47. for (unsigned i = 0; i < RegistersCount; ++i) {
  48. registerUsage[i] = -1;
  49. }
  50. asmCode.str(std::string()); //clear
  51. for (unsigned i = 0; i < prog.getSize(); ++i) {
  52. asmCode << "randomx_isn_" << i << ":" << std::endl;
  53. Instruction& instr = prog(i);
  54. instr.src %= RegistersCount;
  55. instr.dst %= RegistersCount;
  56. generateCode(instr, i);
  57. }
  58. }
  59. void AssemblyGeneratorX86::generateAsm(SuperscalarProgram& prog) {
  60. asmCode.str(std::string()); //clear
  61. #ifdef RANDOMX_ALIGN
  62. asmCode << "ALIGN 16" << std::endl;
  63. #endif
  64. for (unsigned i = 0; i < prog.getSize(); ++i) {
  65. Instruction& instr = prog(i);
  66. switch ((SuperscalarInstructionType)instr.opcode)
  67. {
  68. case SuperscalarInstructionType::ISUB_R:
  69. asmCode << "sub " << regR[instr.dst] << ", " << regR[instr.src] << std::endl;
  70. break;
  71. case SuperscalarInstructionType::IXOR_R:
  72. asmCode << "xor " << regR[instr.dst] << ", " << regR[instr.src] << std::endl;
  73. break;
  74. case SuperscalarInstructionType::IADD_RS:
  75. asmCode << "lea " << regR[instr.dst] << ", [" << regR[instr.dst] << "+" << regR[instr.src] << "*" << (1 << (instr.getModShift())) << "]" << std::endl;
  76. break;
  77. case SuperscalarInstructionType::IMUL_R:
  78. asmCode << "imul " << regR[instr.dst] << ", " << regR[instr.src] << std::endl;
  79. break;
  80. case SuperscalarInstructionType::IROR_C:
  81. asmCode << "ror " << regR[instr.dst] << ", " << instr.getImm32() << std::endl;
  82. break;
  83. case SuperscalarInstructionType::IADD_C7:
  84. asmCode << "add " << regR[instr.dst] << ", " << (int32_t)instr.getImm32() << std::endl;
  85. break;
  86. case SuperscalarInstructionType::IXOR_C7:
  87. asmCode << "xor " << regR[instr.dst] << ", " << (int32_t)instr.getImm32() << std::endl;
  88. break;
  89. case SuperscalarInstructionType::IADD_C8:
  90. asmCode << "add " << regR[instr.dst] << ", " << (int32_t)instr.getImm32() << std::endl;
  91. #ifdef RANDOMX_ALIGN
  92. asmCode << "nop" << std::endl;
  93. #endif
  94. break;
  95. case SuperscalarInstructionType::IXOR_C8:
  96. asmCode << "xor " << regR[instr.dst] << ", " << (int32_t)instr.getImm32() << std::endl;
  97. #ifdef RANDOMX_ALIGN
  98. asmCode << "nop" << std::endl;
  99. #endif
  100. break;
  101. case SuperscalarInstructionType::IADD_C9:
  102. asmCode << "add " << regR[instr.dst] << ", " << (int32_t)instr.getImm32() << std::endl;
  103. #ifdef RANDOMX_ALIGN
  104. asmCode << "xchg ax, ax ;nop" << std::endl;
  105. #endif
  106. break;
  107. case SuperscalarInstructionType::IXOR_C9:
  108. asmCode << "xor " << regR[instr.dst] << ", " << (int32_t)instr.getImm32() << std::endl;
  109. #ifdef RANDOMX_ALIGN
  110. asmCode << "xchg ax, ax ;nop" << std::endl;
  111. #endif
  112. break;
  113. case SuperscalarInstructionType::IMULH_R:
  114. asmCode << "mov rax, " << regR[instr.dst] << std::endl;
  115. asmCode << "mul " << regR[instr.src] << std::endl;
  116. asmCode << "mov " << regR[instr.dst] << ", rdx" << std::endl;
  117. break;
  118. case SuperscalarInstructionType::ISMULH_R:
  119. asmCode << "mov rax, " << regR[instr.dst] << std::endl;
  120. asmCode << "imul " << regR[instr.src] << std::endl;
  121. asmCode << "mov " << regR[instr.dst] << ", rdx" << std::endl;
  122. break;
  123. case SuperscalarInstructionType::IMUL_RCP:
  124. asmCode << "mov rax, " << (int64_t)randomx_reciprocal(instr.getImm32()) << std::endl;
  125. asmCode << "imul " << regR[instr.dst] << ", rax" << std::endl;
  126. break;
  127. default:
  128. UNREACHABLE;
  129. }
  130. }
  131. }
  132. void AssemblyGeneratorX86::generateC(SuperscalarProgram& prog) {
  133. asmCode.str(std::string()); //clear
  134. asmCode << "#include <stdint.h>" << std::endl;
  135. asmCode << "#if defined(__SIZEOF_INT128__)" << std::endl;
  136. asmCode << " static inline uint64_t mulh(uint64_t a, uint64_t b) {" << std::endl;
  137. asmCode << " return ((unsigned __int128)a * b) >> 64;" << std::endl;
  138. asmCode << " }" << std::endl;
  139. asmCode << " static inline int64_t smulh(int64_t a, int64_t b) {" << std::endl;
  140. asmCode << " return ((__int128)a * b) >> 64;" << std::endl;
  141. asmCode << " }" << std::endl;
  142. asmCode << " #define HAVE_MULH" << std::endl;
  143. asmCode << " #define HAVE_SMULH" << std::endl;
  144. asmCode << "#endif" << std::endl;
  145. asmCode << "#if defined(_MSC_VER)" << std::endl;
  146. asmCode << " #define HAS_VALUE(X) X ## 0" << std::endl;
  147. asmCode << " #define EVAL_DEFINE(X) HAS_VALUE(X)" << std::endl;
  148. asmCode << " #include <intrin.h>" << std::endl;
  149. asmCode << " #include <stdlib.h>" << std::endl;
  150. asmCode << " static __inline uint64_t rotr(uint64_t x , int c) {" << std::endl;
  151. asmCode << " return _rotr64(x, c);" << std::endl;
  152. asmCode << " }" << std::endl;
  153. asmCode << " #define HAVE_ROTR" << std::endl;
  154. asmCode << " #if EVAL_DEFINE(__MACHINEARM64_X64(1))" << std::endl;
  155. asmCode << " static __inline uint64_t mulh(uint64_t a, uint64_t b) {" << std::endl;
  156. asmCode << " return __umulh(a, b);" << std::endl;
  157. asmCode << " }" << std::endl;
  158. asmCode << " #define HAVE_MULH" << std::endl;
  159. asmCode << " #endif" << std::endl;
  160. asmCode << " #if EVAL_DEFINE(__MACHINEX64(1))" << std::endl;
  161. asmCode << " static __inline int64_t smulh(int64_t a, int64_t b) {" << std::endl;
  162. asmCode << " int64_t hi;" << std::endl;
  163. asmCode << " _mul128(a, b, &hi);" << std::endl;
  164. asmCode << " return hi;" << std::endl;
  165. asmCode << " }" << std::endl;
  166. asmCode << " #define HAVE_SMULH" << std::endl;
  167. asmCode << " #endif" << std::endl;
  168. asmCode << "#endif" << std::endl;
  169. asmCode << "#ifndef HAVE_ROTR" << std::endl;
  170. asmCode << " static inline uint64_t rotr(uint64_t a, int b) {" << std::endl;
  171. asmCode << " return (a >> b) | (a << (64 - b));" << std::endl;
  172. asmCode << " }" << std::endl;
  173. asmCode << " #define HAVE_ROTR" << std::endl;
  174. asmCode << "#endif" << std::endl;
  175. asmCode << "#if !defined(HAVE_MULH) || !defined(HAVE_SMULH) || !defined(HAVE_ROTR)" << std::endl;
  176. asmCode << " #error \"Required functions are not defined\"" << std::endl;
  177. asmCode << "#endif" << std::endl;
  178. asmCode << "void superScalar(uint64_t r[8]) {" << std::endl;
  179. asmCode << "uint64_t r8 = r[0], r9 = r[1], r10 = r[2], r11 = r[3], r12 = r[4], r13 = r[5], r14 = r[6], r15 = r[7];" << std::endl;
  180. for (unsigned i = 0; i < prog.getSize(); ++i) {
  181. Instruction& instr = prog(i);
  182. switch ((SuperscalarInstructionType)instr.opcode)
  183. {
  184. case SuperscalarInstructionType::ISUB_R:
  185. asmCode << regR[instr.dst] << " -= " << regR[instr.src] << ";" << std::endl;
  186. break;
  187. case SuperscalarInstructionType::IXOR_R:
  188. asmCode << regR[instr.dst] << " ^= " << regR[instr.src] << ";" << std::endl;
  189. break;
  190. case SuperscalarInstructionType::IADD_RS:
  191. asmCode << regR[instr.dst] << " += " << regR[instr.src] << "*" << (1 << (instr.getModShift())) << ";" << std::endl;
  192. break;
  193. case SuperscalarInstructionType::IMUL_R:
  194. asmCode << regR[instr.dst] << " *= " << regR[instr.src] << ";" << std::endl;
  195. break;
  196. case SuperscalarInstructionType::IROR_C:
  197. asmCode << regR[instr.dst] << " = rotr(" << regR[instr.dst] << ", " << instr.getImm32() << ");" << std::endl;
  198. break;
  199. case SuperscalarInstructionType::IADD_C7:
  200. case SuperscalarInstructionType::IADD_C8:
  201. case SuperscalarInstructionType::IADD_C9:
  202. asmCode << regR[instr.dst] << " += " << (int32_t)instr.getImm32() << ";" << std::endl;
  203. break;
  204. case SuperscalarInstructionType::IXOR_C7:
  205. case SuperscalarInstructionType::IXOR_C8:
  206. case SuperscalarInstructionType::IXOR_C9:
  207. asmCode << regR[instr.dst] << " ^= " << (int32_t)instr.getImm32() << ";" << std::endl;
  208. break;
  209. case SuperscalarInstructionType::IMULH_R:
  210. asmCode << regR[instr.dst] << " = mulh(" << regR[instr.dst] << ", " << regR[instr.src] << ");" << std::endl;
  211. break;
  212. case SuperscalarInstructionType::ISMULH_R:
  213. asmCode << regR[instr.dst] << " = smulh(" << regR[instr.dst] << ", " << regR[instr.src] << ");" << std::endl;
  214. break;
  215. case SuperscalarInstructionType::IMUL_RCP:
  216. asmCode << regR[instr.dst] << " *= " << (int64_t)randomx_reciprocal(instr.getImm32()) << ";" << std::endl;
  217. break;
  218. default:
  219. UNREACHABLE;
  220. }
  221. }
  222. asmCode << "r[0] = r8; r[1] = r9; r[2] = r10; r[3] = r11; r[4] = r12; r[5] = r13; r[6] = r14; r[7] = r15;" << std::endl;
  223. asmCode << "}" << std::endl;
  224. }
  225. void AssemblyGeneratorX86::traceint(Instruction& instr) {
  226. if (trace) {
  227. asmCode << "\tpush " << regR[instr.dst] << std::endl;
  228. }
  229. }
  230. void AssemblyGeneratorX86::traceflt(Instruction& instr) {
  231. if (trace) {
  232. asmCode << "\tpush 0" << std::endl;
  233. }
  234. }
  235. void AssemblyGeneratorX86::tracenop(Instruction& instr) {
  236. if (trace) {
  237. asmCode << "\tpush 0" << std::endl;
  238. }
  239. }
  240. void AssemblyGeneratorX86::generateCode(Instruction& instr, int i) {
  241. asmCode << "\t; " << instr;
  242. auto generator = engine[instr.opcode];
  243. (this->*generator)(instr, i);
  244. }
  245. void AssemblyGeneratorX86::genAddressReg(Instruction& instr, const char* reg = "eax") {
  246. asmCode << "\tlea " << reg << ", [" << regR32[instr.src] << std::showpos << (int32_t)instr.getImm32() << std::noshowpos << "]" << std::endl;
  247. asmCode << "\tand " << reg << ", " << ((instr.getModMem()) ? ScratchpadL1Mask : ScratchpadL2Mask) << std::endl;
  248. }
  249. void AssemblyGeneratorX86::genAddressRegDst(Instruction& instr, int maskAlign = 8) {
  250. asmCode << "\tlea eax, [" << regR32[instr.dst] << std::showpos << (int32_t)instr.getImm32() << std::noshowpos << "]" << std::endl;
  251. int mask;
  252. if (instr.getModCond() < StoreL3Condition) {
  253. mask = instr.getModMem() ? ScratchpadL1Mask : ScratchpadL2Mask;
  254. }
  255. else {
  256. mask = ScratchpadL3Mask;
  257. }
  258. asmCode << "\tand eax" << ", " << (mask & (-maskAlign)) << std::endl;
  259. }
  260. int32_t AssemblyGeneratorX86::genAddressImm(Instruction& instr) {
  261. return (int32_t)instr.getImm32() & ScratchpadL3Mask;
  262. }
  263. void AssemblyGeneratorX86::h_IADD_RS(Instruction& instr, int i) {
  264. registerUsage[instr.dst] = i;
  265. if(instr.dst == RegisterNeedsDisplacement)
  266. asmCode << "\tlea " << regR[instr.dst] << ", [" << regR[instr.dst] << "+" << regR[instr.src] << "*" << (1 << (instr.getModShift())) << std::showpos << (int32_t)instr.getImm32() << std::noshowpos << "]" << std::endl;
  267. else
  268. asmCode << "\tlea " << regR[instr.dst] << ", [" << regR[instr.dst] << "+" << regR[instr.src] << "*" << (1 << (instr.getModShift())) << "]" << std::endl;
  269. traceint(instr);
  270. }
  271. void AssemblyGeneratorX86::h_IADD_M(Instruction& instr, int i) {
  272. registerUsage[instr.dst] = i;
  273. if (instr.src != instr.dst) {
  274. genAddressReg(instr);
  275. asmCode << "\tadd " << regR[instr.dst] << ", qword ptr [" << regScratchpadAddr << "+rax]" << std::endl;
  276. }
  277. else {
  278. asmCode << "\tadd " << regR[instr.dst] << ", qword ptr [" << regScratchpadAddr << "+" << genAddressImm(instr) << "]" << std::endl;
  279. }
  280. traceint(instr);
  281. }
  282. void AssemblyGeneratorX86::h_ISUB_R(Instruction& instr, int i) {
  283. registerUsage[instr.dst] = i;
  284. if (instr.src != instr.dst) {
  285. asmCode << "\tsub " << regR[instr.dst] << ", " << regR[instr.src] << std::endl;
  286. }
  287. else {
  288. asmCode << "\tsub " << regR[instr.dst] << ", " << (int32_t)instr.getImm32() << std::endl;
  289. }
  290. traceint(instr);
  291. }
  292. void AssemblyGeneratorX86::h_ISUB_M(Instruction& instr, int i) {
  293. registerUsage[instr.dst] = i;
  294. if (instr.src != instr.dst) {
  295. genAddressReg(instr);
  296. asmCode << "\tsub " << regR[instr.dst] << ", qword ptr [" << regScratchpadAddr << "+rax]" << std::endl;
  297. }
  298. else {
  299. asmCode << "\tsub " << regR[instr.dst] << ", qword ptr [" << regScratchpadAddr << "+" << genAddressImm(instr) << "]" << std::endl;
  300. }
  301. traceint(instr);
  302. }
  303. void AssemblyGeneratorX86::h_IMUL_R(Instruction& instr, int i) {
  304. registerUsage[instr.dst] = i;
  305. if (instr.src != instr.dst) {
  306. asmCode << "\timul " << regR[instr.dst] << ", " << regR[instr.src] << std::endl;
  307. }
  308. else {
  309. asmCode << "\timul " << regR[instr.dst] << ", " << (int32_t)instr.getImm32() << std::endl;
  310. }
  311. traceint(instr);
  312. }
  313. void AssemblyGeneratorX86::h_IMUL_M(Instruction& instr, int i) {
  314. registerUsage[instr.dst] = i;
  315. if (instr.src != instr.dst) {
  316. genAddressReg(instr);
  317. asmCode << "\timul " << regR[instr.dst] << ", qword ptr [" << regScratchpadAddr << "+rax]" << std::endl;
  318. }
  319. else {
  320. asmCode << "\timul " << regR[instr.dst] << ", qword ptr [" << regScratchpadAddr << "+" << genAddressImm(instr) << "]" << std::endl;
  321. }
  322. traceint(instr);
  323. }
  324. void AssemblyGeneratorX86::h_IMULH_R(Instruction& instr, int i) {
  325. registerUsage[instr.dst] = i;
  326. asmCode << "\tmov rax, " << regR[instr.dst] << std::endl;
  327. asmCode << "\tmul " << regR[instr.src] << std::endl;
  328. asmCode << "\tmov " << regR[instr.dst] << ", rdx" << std::endl;
  329. traceint(instr);
  330. }
  331. void AssemblyGeneratorX86::h_IMULH_M(Instruction& instr, int i) {
  332. registerUsage[instr.dst] = i;
  333. if (instr.src != instr.dst) {
  334. genAddressReg(instr, "ecx");
  335. asmCode << "\tmov rax, " << regR[instr.dst] << std::endl;
  336. asmCode << "\tmul qword ptr [" << regScratchpadAddr << "+rcx]" << std::endl;
  337. }
  338. else {
  339. asmCode << "\tmov rax, " << regR[instr.dst] << std::endl;
  340. asmCode << "\tmul qword ptr [" << regScratchpadAddr << "+" << genAddressImm(instr) << "]" << std::endl;
  341. }
  342. asmCode << "\tmov " << regR[instr.dst] << ", rdx" << std::endl;
  343. traceint(instr);
  344. }
  345. void AssemblyGeneratorX86::h_ISMULH_R(Instruction& instr, int i) {
  346. registerUsage[instr.dst] = i;
  347. asmCode << "\tmov rax, " << regR[instr.dst] << std::endl;
  348. asmCode << "\timul " << regR[instr.src] << std::endl;
  349. asmCode << "\tmov " << regR[instr.dst] << ", rdx" << std::endl;
  350. traceint(instr);
  351. }
  352. void AssemblyGeneratorX86::h_ISMULH_M(Instruction& instr, int i) {
  353. registerUsage[instr.dst] = i;
  354. if (instr.src != instr.dst) {
  355. genAddressReg(instr, "ecx");
  356. asmCode << "\tmov rax, " << regR[instr.dst] << std::endl;
  357. asmCode << "\timul qword ptr [" << regScratchpadAddr << "+rcx]" << std::endl;
  358. }
  359. else {
  360. asmCode << "\tmov rax, " << regR[instr.dst] << std::endl;
  361. asmCode << "\timul qword ptr [" << regScratchpadAddr << "+" << genAddressImm(instr) << "]" << std::endl;
  362. }
  363. asmCode << "\tmov " << regR[instr.dst] << ", rdx" << std::endl;
  364. traceint(instr);
  365. }
  366. void AssemblyGeneratorX86::h_INEG_R(Instruction& instr, int i) {
  367. registerUsage[instr.dst] = i;
  368. asmCode << "\tneg " << regR[instr.dst] << std::endl;
  369. traceint(instr);
  370. }
  371. void AssemblyGeneratorX86::h_IXOR_R(Instruction& instr, int i) {
  372. registerUsage[instr.dst] = i;
  373. if (instr.src != instr.dst) {
  374. asmCode << "\txor " << regR[instr.dst] << ", " << regR[instr.src] << std::endl;
  375. }
  376. else {
  377. asmCode << "\txor " << regR[instr.dst] << ", " << (int32_t)instr.getImm32() << std::endl;
  378. }
  379. traceint(instr);
  380. }
  381. void AssemblyGeneratorX86::h_IXOR_M(Instruction& instr, int i) {
  382. registerUsage[instr.dst] = i;
  383. if (instr.src != instr.dst) {
  384. genAddressReg(instr);
  385. asmCode << "\txor " << regR[instr.dst] << ", qword ptr [" << regScratchpadAddr << "+rax]" << std::endl;
  386. }
  387. else {
  388. asmCode << "\txor " << regR[instr.dst] << ", qword ptr [" << regScratchpadAddr << "+" << genAddressImm(instr) << "]" << std::endl;
  389. }
  390. traceint(instr);
  391. }
  392. void AssemblyGeneratorX86::h_IROR_R(Instruction& instr, int i) {
  393. registerUsage[instr.dst] = i;
  394. if (instr.src != instr.dst) {
  395. asmCode << "\tmov ecx, " << regR32[instr.src] << std::endl;
  396. asmCode << "\tror " << regR[instr.dst] << ", cl" << std::endl;
  397. }
  398. else {
  399. asmCode << "\tror " << regR[instr.dst] << ", " << (instr.getImm32() & 63) << std::endl;
  400. }
  401. traceint(instr);
  402. }
  403. void AssemblyGeneratorX86::h_IROL_R(Instruction& instr, int i) {
  404. registerUsage[instr.dst] = i;
  405. if (instr.src != instr.dst) {
  406. asmCode << "\tmov ecx, " << regR32[instr.src] << std::endl;
  407. asmCode << "\trol " << regR[instr.dst] << ", cl" << std::endl;
  408. }
  409. else {
  410. asmCode << "\trol " << regR[instr.dst] << ", " << (instr.getImm32() & 63) << std::endl;
  411. }
  412. traceint(instr);
  413. }
  414. void AssemblyGeneratorX86::h_IMUL_RCP(Instruction& instr, int i) {
  415. uint64_t divisor = instr.getImm32();
  416. if (!isZeroOrPowerOf2(divisor)) {
  417. registerUsage[instr.dst] = i;
  418. asmCode << "\tmov rax, " << randomx_reciprocal(divisor) << std::endl;
  419. asmCode << "\timul " << regR[instr.dst] << ", rax" << std::endl;
  420. traceint(instr);
  421. }
  422. else {
  423. tracenop(instr);
  424. }
  425. }
  426. void AssemblyGeneratorX86::h_ISWAP_R(Instruction& instr, int i) {
  427. if (instr.src != instr.dst) {
  428. registerUsage[instr.dst] = i;
  429. registerUsage[instr.src] = i;
  430. asmCode << "\txchg " << regR[instr.dst] << ", " << regR[instr.src] << std::endl;
  431. traceint(instr);
  432. }
  433. else {
  434. tracenop(instr);
  435. }
  436. }
  437. void AssemblyGeneratorX86::h_FSWAP_R(Instruction& instr, int i) {
  438. asmCode << "\tshufpd " << regFE[instr.dst] << ", " << regFE[instr.dst] << ", 1" << std::endl;
  439. traceflt(instr);
  440. }
  441. void AssemblyGeneratorX86::h_FADD_R(Instruction& instr, int i) {
  442. instr.dst %= RegisterCountFlt;
  443. instr.src %= RegisterCountFlt;
  444. asmCode << "\taddpd " << regF[instr.dst] << ", " << regA[instr.src] << std::endl;
  445. traceflt(instr);
  446. }
  447. void AssemblyGeneratorX86::h_FADD_M(Instruction& instr, int i) {
  448. instr.dst %= RegisterCountFlt;
  449. genAddressReg(instr);
  450. asmCode << "\tcvtdq2pd " << tempRegx << ", qword ptr [" << regScratchpadAddr << "+rax]" << std::endl;
  451. asmCode << "\taddpd " << regF[instr.dst] << ", " << tempRegx << std::endl;
  452. traceflt(instr);
  453. }
  454. void AssemblyGeneratorX86::h_FSUB_R(Instruction& instr, int i) {
  455. instr.dst %= RegisterCountFlt;
  456. instr.src %= RegisterCountFlt;
  457. asmCode << "\tsubpd " << regF[instr.dst] << ", " << regA[instr.src] << std::endl;
  458. traceflt(instr);
  459. }
  460. void AssemblyGeneratorX86::h_FSUB_M(Instruction& instr, int i) {
  461. instr.dst %= RegisterCountFlt;
  462. genAddressReg(instr);
  463. asmCode << "\tcvtdq2pd " << tempRegx << ", qword ptr [" << regScratchpadAddr << "+rax]" << std::endl;
  464. asmCode << "\tsubpd " << regF[instr.dst] << ", " << tempRegx << std::endl;
  465. traceflt(instr);
  466. }
  467. void AssemblyGeneratorX86::h_FSCAL_R(Instruction& instr, int i) {
  468. instr.dst %= RegisterCountFlt;
  469. asmCode << "\txorps " << regF[instr.dst] << ", " << scaleMaskReg << std::endl;
  470. traceflt(instr);
  471. }
  472. void AssemblyGeneratorX86::h_FMUL_R(Instruction& instr, int i) {
  473. instr.dst %= RegisterCountFlt;
  474. instr.src %= RegisterCountFlt;
  475. asmCode << "\tmulpd " << regE[instr.dst] << ", " << regA[instr.src] << std::endl;
  476. traceflt(instr);
  477. }
  478. void AssemblyGeneratorX86::h_FDIV_M(Instruction& instr, int i) {
  479. instr.dst %= RegisterCountFlt;
  480. genAddressReg(instr);
  481. asmCode << "\tcvtdq2pd " << tempRegx << ", qword ptr [" << regScratchpadAddr << "+rax]" << std::endl;
  482. asmCode << "\tandps " << tempRegx << ", " << mantissaMaskReg << std::endl;
  483. asmCode << "\torps " << tempRegx << ", " << exponentMaskReg << std::endl;
  484. asmCode << "\tdivpd " << regE[instr.dst] << ", " << tempRegx << std::endl;
  485. traceflt(instr);
  486. }
  487. void AssemblyGeneratorX86::h_FSQRT_R(Instruction& instr, int i) {
  488. instr.dst %= RegisterCountFlt;
  489. asmCode << "\tsqrtpd " << regE[instr.dst] << ", " << regE[instr.dst] << std::endl;
  490. traceflt(instr);
  491. }
  492. void AssemblyGeneratorX86::h_CFROUND(Instruction& instr, int i) {
  493. asmCode << "\tmov rax, " << regR[instr.src] << std::endl;
  494. int rotate = (13 - (instr.getImm32() & 63)) & 63;
  495. if (rotate != 0)
  496. asmCode << "\trol rax, " << rotate << std::endl;
  497. asmCode << "\tand eax, 24576" << std::endl;
  498. asmCode << "\tor eax, 40896" << std::endl;
  499. asmCode << "\tpush rax" << std::endl;
  500. asmCode << "\tldmxcsr dword ptr [rsp]" << std::endl;
  501. asmCode << "\tpop rax" << std::endl;
  502. tracenop(instr);
  503. }
  504. void AssemblyGeneratorX86::h_CBRANCH(Instruction& instr, int i) {
  505. int reg = instr.dst;
  506. int target = registerUsage[reg] + 1;
  507. int shift = instr.getModCond() + ConditionOffset;
  508. int32_t imm = instr.getImm32() | (1L << shift);
  509. if (ConditionOffset > 0 || shift > 0)
  510. imm &= ~(1L << (shift - 1));
  511. asmCode << "\tadd " << regR[reg] << ", " << imm << std::endl;
  512. asmCode << "\ttest " << regR[reg] << ", " << (ConditionMask << shift) << std::endl;
  513. asmCode << "\tjz randomx_isn_" << target << std::endl;
  514. //mark all registers as used
  515. for (unsigned j = 0; j < RegistersCount; ++j) {
  516. registerUsage[j] = i;
  517. }
  518. }
  519. void AssemblyGeneratorX86::h_ISTORE(Instruction& instr, int i) {
  520. genAddressRegDst(instr);
  521. asmCode << "\tmov qword ptr [" << regScratchpadAddr << "+rax], " << regR[instr.src] << std::endl;
  522. tracenop(instr);
  523. }
  524. void AssemblyGeneratorX86::h_NOP(Instruction& instr, int i) {
  525. asmCode << "\tnop" << std::endl;
  526. tracenop(instr);
  527. }
  528. #include "instruction_weights.hpp"
  529. #define INST_HANDLE(x) REPN(&AssemblyGeneratorX86::h_##x, WT(x))
  530. InstructionGenerator AssemblyGeneratorX86::engine[256] = {
  531. INST_HANDLE(IADD_RS)
  532. INST_HANDLE(IADD_M)
  533. INST_HANDLE(ISUB_R)
  534. INST_HANDLE(ISUB_M)
  535. INST_HANDLE(IMUL_R)
  536. INST_HANDLE(IMUL_M)
  537. INST_HANDLE(IMULH_R)
  538. INST_HANDLE(IMULH_M)
  539. INST_HANDLE(ISMULH_R)
  540. INST_HANDLE(ISMULH_M)
  541. INST_HANDLE(IMUL_RCP)
  542. INST_HANDLE(INEG_R)
  543. INST_HANDLE(IXOR_R)
  544. INST_HANDLE(IXOR_M)
  545. INST_HANDLE(IROR_R)
  546. INST_HANDLE(IROL_R)
  547. INST_HANDLE(ISWAP_R)
  548. INST_HANDLE(FSWAP_R)
  549. INST_HANDLE(FADD_R)
  550. INST_HANDLE(FADD_M)
  551. INST_HANDLE(FSUB_R)
  552. INST_HANDLE(FSUB_M)
  553. INST_HANDLE(FSCAL_R)
  554. INST_HANDLE(FMUL_R)
  555. INST_HANDLE(FDIV_M)
  556. INST_HANDLE(FSQRT_R)
  557. INST_HANDLE(CBRANCH)
  558. INST_HANDLE(CFROUND)
  559. INST_HANDLE(ISTORE)
  560. INST_HANDLE(NOP)
  561. };
  562. }