jit_compiler_x86_static.S 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. # Copyright (c) 2018-2019, tevador <tevador@gmail.com>
  2. #
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are met:
  7. # * Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # * Redistributions in binary form must reproduce the above copyright
  10. # notice, this list of conditions and the following disclaimer in the
  11. # documentation and/or other materials provided with the distribution.
  12. # * Neither the name of the copyright holder nor the
  13. # names of its contributors may be used to endorse or promote products
  14. # derived from this software without specific prior written permission.
  15. #
  16. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. .intel_syntax noprefix
  27. #if defined(__APPLE__)
  28. .text
  29. #define DECL(x) _##x
  30. #else
  31. .section .text
  32. #define DECL(x) x
  33. #endif
  34. #if defined(__WIN32__) || defined(__CYGWIN__)
  35. #define WINABI
  36. #endif
  37. .global DECL(randomx_prefetch_scratchpad)
  38. .global DECL(randomx_prefetch_scratchpad_end)
  39. .global DECL(randomx_program_prologue)
  40. .global DECL(randomx_program_prologue_first_load)
  41. .global DECL(randomx_program_loop_begin)
  42. .global DECL(randomx_program_loop_load)
  43. .global DECL(randomx_program_start)
  44. .global DECL(randomx_program_read_dataset)
  45. .global DECL(randomx_program_read_dataset_sshash_init)
  46. .global DECL(randomx_program_read_dataset_sshash_fin)
  47. .global DECL(randomx_program_loop_store)
  48. .global DECL(randomx_program_loop_end)
  49. .global DECL(randomx_dataset_init)
  50. .global DECL(randomx_program_epilogue)
  51. .global DECL(randomx_sshash_load)
  52. .global DECL(randomx_sshash_prefetch)
  53. .global DECL(randomx_sshash_end)
  54. .global DECL(randomx_sshash_init)
  55. .global DECL(randomx_program_end)
  56. .global DECL(randomx_reciprocal_fast)
  57. #include "configuration.h"
  58. #define RANDOMX_SCRATCHPAD_MASK (RANDOMX_SCRATCHPAD_L3-64)
  59. #define RANDOMX_DATASET_BASE_MASK (RANDOMX_DATASET_BASE_SIZE-64)
  60. #define RANDOMX_CACHE_MASK (RANDOMX_ARGON_MEMORY*16-1)
  61. #define RANDOMX_ALIGN 4096
  62. #define SUPERSCALAR_OFFSET ((((RANDOMX_ALIGN + 32 * RANDOMX_PROGRAM_SIZE) - 1) / (RANDOMX_ALIGN) + 1) * (RANDOMX_ALIGN))
  63. #define db .byte
  64. DECL(randomx_prefetch_scratchpad):
  65. mov rdx, rax
  66. and eax, RANDOMX_SCRATCHPAD_MASK
  67. prefetcht0 [rsi+rax]
  68. ror rdx, 32
  69. and edx, RANDOMX_SCRATCHPAD_MASK
  70. prefetcht0 [rsi+rdx]
  71. DECL(randomx_prefetch_scratchpad_end):
  72. .balign 64
  73. DECL(randomx_program_prologue):
  74. #if defined(WINABI)
  75. #include "asm/program_prologue_win64.inc"
  76. #else
  77. #include "asm/program_prologue_linux.inc"
  78. #endif
  79. movapd xmm13, xmmword ptr [mantissaMask+rip]
  80. movapd xmm14, xmmword ptr [exp240+rip]
  81. movapd xmm15, xmmword ptr [scaleMask+rip]
  82. DECL(randomx_program_prologue_first_load):
  83. xor rax, r8
  84. xor rax, r8
  85. mov rdx, rax
  86. and eax, RANDOMX_SCRATCHPAD_MASK
  87. ror rdx, 32
  88. and edx, RANDOMX_SCRATCHPAD_MASK
  89. jmp DECL(randomx_program_loop_begin)
  90. .balign 64
  91. #include "asm/program_xmm_constants.inc"
  92. .balign 64
  93. DECL(randomx_program_loop_begin):
  94. nop
  95. DECL(randomx_program_loop_load):
  96. #include "asm/program_loop_load.inc"
  97. DECL(randomx_program_start):
  98. nop
  99. DECL(randomx_program_read_dataset):
  100. #include "asm/program_read_dataset.inc"
  101. DECL(randomx_program_read_dataset_sshash_init):
  102. #include "asm/program_read_dataset_sshash_init.inc"
  103. DECL(randomx_program_read_dataset_sshash_fin):
  104. #include "asm/program_read_dataset_sshash_fin.inc"
  105. DECL(randomx_program_loop_store):
  106. #include "asm/program_loop_store.inc"
  107. DECL(randomx_program_loop_end):
  108. nop
  109. .balign 64
  110. DECL(randomx_dataset_init):
  111. push rbx
  112. push rbp
  113. push r12
  114. push r13
  115. push r14
  116. push r15
  117. #if defined(WINABI)
  118. push rdi
  119. push rsi
  120. mov rdi, qword ptr [rcx] ;# cache->memory
  121. mov rsi, rdx ;# dataset
  122. mov rbp, r8 ;# block index
  123. push r9 ;# max. block index
  124. #else
  125. mov rdi, qword ptr [rdi] ;# cache->memory
  126. ;# dataset in rsi
  127. mov rbp, rdx ;# block index
  128. push rcx ;# max. block index
  129. #endif
  130. init_block_loop:
  131. prefetchw byte ptr [rsi]
  132. mov rbx, rbp
  133. .byte 232 ;# 0xE8 = call
  134. .int SUPERSCALAR_OFFSET - (call_offset - DECL(randomx_dataset_init))
  135. call_offset:
  136. mov qword ptr [rsi+0], r8
  137. mov qword ptr [rsi+8], r9
  138. mov qword ptr [rsi+16], r10
  139. mov qword ptr [rsi+24], r11
  140. mov qword ptr [rsi+32], r12
  141. mov qword ptr [rsi+40], r13
  142. mov qword ptr [rsi+48], r14
  143. mov qword ptr [rsi+56], r15
  144. add rbp, 1
  145. add rsi, 64
  146. cmp rbp, qword ptr [rsp]
  147. jb init_block_loop
  148. pop rax
  149. #if defined(WINABI)
  150. pop rsi
  151. pop rdi
  152. #endif
  153. pop r15
  154. pop r14
  155. pop r13
  156. pop r12
  157. pop rbp
  158. pop rbx
  159. ret
  160. .balign 64
  161. DECL(randomx_program_epilogue):
  162. #include "asm/program_epilogue_store.inc"
  163. #if defined(WINABI)
  164. #include "asm/program_epilogue_win64.inc"
  165. #else
  166. #include "asm/program_epilogue_linux.inc"
  167. #endif
  168. .balign 64
  169. DECL(randomx_sshash_load):
  170. #include "asm/program_sshash_load.inc"
  171. DECL(randomx_sshash_prefetch):
  172. #include "asm/program_sshash_prefetch.inc"
  173. DECL(randomx_sshash_end):
  174. nop
  175. .balign 64
  176. DECL(randomx_sshash_init):
  177. lea r8, [rbx+1]
  178. #include "asm/program_sshash_prefetch.inc"
  179. imul r8, qword ptr [r0_mul+rip]
  180. mov r9, qword ptr [r1_add+rip]
  181. xor r9, r8
  182. mov r10, qword ptr [r2_add+rip]
  183. xor r10, r8
  184. mov r11, qword ptr [r3_add+rip]
  185. xor r11, r8
  186. mov r12, qword ptr [r4_add+rip]
  187. xor r12, r8
  188. mov r13, qword ptr [r5_add+rip]
  189. xor r13, r8
  190. mov r14, qword ptr [r6_add+rip]
  191. xor r14, r8
  192. mov r15, qword ptr [r7_add+rip]
  193. xor r15, r8
  194. jmp DECL(randomx_program_end)
  195. .balign 64
  196. #include "asm/program_sshash_constants.inc"
  197. .balign 64
  198. DECL(randomx_program_end):
  199. nop
  200. DECL(randomx_reciprocal_fast):
  201. #if !defined(WINABI)
  202. mov rcx, rdi
  203. #endif
  204. #include "asm/randomx_reciprocal.inc"