jit_compiler_x86.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #pragma once
  26. #include <cstdint>
  27. #include <cstring>
  28. #include <vector>
  29. #include "common.hpp"
  30. namespace randomx {
  31. class Program;
  32. struct ProgramConfiguration;
  33. class SuperscalarProgram;
  34. class JitCompilerX86;
  35. class Instruction;
  36. typedef void(JitCompilerX86::*InstructionGeneratorX86)(Instruction&, int);
  37. class JitCompilerX86 {
  38. public:
  39. JitCompilerX86();
  40. ~JitCompilerX86();
  41. void generateProgram(Program&, ProgramConfiguration&);
  42. void generateProgramLight(Program&, ProgramConfiguration&, uint32_t);
  43. template<size_t N>
  44. void generateSuperscalarHash(SuperscalarProgram (&programs)[N], std::vector<uint64_t> &);
  45. void generateDatasetInitCode();
  46. ProgramFunc* getProgramFunc() {
  47. return (ProgramFunc*)code;
  48. }
  49. DatasetInitFunc* getDatasetInitFunc() {
  50. return (DatasetInitFunc*)code;
  51. }
  52. uint8_t* getCode() {
  53. return code;
  54. }
  55. size_t getCodeSize();
  56. void enableWriting();
  57. void enableExecution();
  58. void enableAll();
  59. private:
  60. static InstructionGeneratorX86 engine[256];
  61. std::vector<int32_t> instructionOffsets;
  62. int registerUsage[RegistersCount];
  63. uint8_t* code;
  64. int32_t codePos;
  65. void generateProgramPrologue(Program&, ProgramConfiguration&);
  66. void generateProgramEpilogue(Program&, ProgramConfiguration&);
  67. void genAddressReg(Instruction&, bool);
  68. void genAddressRegDst(Instruction&);
  69. void genAddressImm(Instruction&);
  70. void genSIB(int scale, int index, int base);
  71. void generateCode(Instruction&, int);
  72. void generateSuperscalarCode(Instruction &, std::vector<uint64_t> &);
  73. void emitByte(uint8_t val) {
  74. code[codePos] = val;
  75. codePos++;
  76. }
  77. void emit32(uint32_t val) {
  78. memcpy(code + codePos, &val, sizeof val);
  79. codePos += sizeof val;
  80. }
  81. void emit64(uint64_t val) {
  82. memcpy(code + codePos, &val, sizeof val);
  83. codePos += sizeof val;
  84. }
  85. template<size_t N>
  86. void emit(const uint8_t (&src)[N]) {
  87. emit(src, N);
  88. }
  89. void emit(const uint8_t* src, size_t count) {
  90. memcpy(code + codePos, src, count);
  91. codePos += count;
  92. }
  93. void h_IADD_RS(Instruction&, int);
  94. void h_IADD_M(Instruction&, int);
  95. void h_ISUB_R(Instruction&, int);
  96. void h_ISUB_M(Instruction&, int);
  97. void h_IMUL_R(Instruction&, int);
  98. void h_IMUL_M(Instruction&, int);
  99. void h_IMULH_R(Instruction&, int);
  100. void h_IMULH_M(Instruction&, int);
  101. void h_ISMULH_R(Instruction&, int);
  102. void h_ISMULH_M(Instruction&, int);
  103. void h_IMUL_RCP(Instruction&, int);
  104. void h_INEG_R(Instruction&, int);
  105. void h_IXOR_R(Instruction&, int);
  106. void h_IXOR_M(Instruction&, int);
  107. void h_IROR_R(Instruction&, int);
  108. void h_IROL_R(Instruction&, int);
  109. void h_ISWAP_R(Instruction&, int);
  110. void h_FSWAP_R(Instruction&, int);
  111. void h_FADD_R(Instruction&, int);
  112. void h_FADD_M(Instruction&, int);
  113. void h_FSUB_R(Instruction&, int);
  114. void h_FSUB_M(Instruction&, int);
  115. void h_FSCAL_R(Instruction&, int);
  116. void h_FMUL_R(Instruction&, int);
  117. void h_FDIV_M(Instruction&, int);
  118. void h_FSQRT_R(Instruction&, int);
  119. void h_CBRANCH(Instruction&, int);
  120. void h_CFROUND(Instruction&, int);
  121. void h_ISTORE(Instruction&, int);
  122. void h_NOP(Instruction&, int);
  123. };
  124. }