JitCompilerX86.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #pragma once
  16. #include "common.hpp"
  17. #include "Instruction.hpp"
  18. #include "superscalar_program.hpp"
  19. #include <cstring>
  20. #include <vector>
  21. namespace randomx {
  22. class Program;
  23. class ProgramConfiguration;
  24. class SuperscalarProgram;
  25. class JitCompilerX86;
  26. typedef void(JitCompilerX86::*InstructionGeneratorX86)(Instruction&, int);
  27. constexpr uint32_t CodeSize = 64 * 1024;
  28. class JitCompilerX86 {
  29. public:
  30. JitCompilerX86();
  31. ~JitCompilerX86();
  32. void generateProgram(Program&, ProgramConfiguration&);
  33. void generateProgramLight(Program&, ProgramConfiguration&);
  34. template<size_t N>
  35. void generateSuperscalarHash(SuperscalarProgram (&programs)[N], std::vector<uint64_t> &);
  36. void generateDatasetInitCode();
  37. ProgramFunc getProgramFunc() {
  38. return (ProgramFunc)code;
  39. }
  40. DatasetInitFunc getDatasetInitFunc() {
  41. return (DatasetInitFunc)code;
  42. }
  43. uint8_t* getCode() {
  44. return code;
  45. }
  46. size_t getCodeSize();
  47. private:
  48. static InstructionGeneratorX86 engine[256];
  49. std::vector<int32_t> instructionOffsets;
  50. int registerUsage[8];
  51. uint8_t* code;
  52. int32_t codePos;
  53. void generateProgramPrologue(Program&, ProgramConfiguration&);
  54. void generateProgramEpilogue(Program&);
  55. int getConditionRegister();
  56. void genAddressReg(Instruction&, bool);
  57. void genAddressRegDst(Instruction&, bool);
  58. void genAddressImm(Instruction&);
  59. void genSIB(int scale, int index, int base);
  60. void handleCondition(Instruction&, int);
  61. void generateCode(Instruction&, int);
  62. void generateSuperscalarCode(Instruction &, std::vector<uint64_t> &);
  63. void emitByte(uint8_t val) {
  64. code[codePos] = val;
  65. codePos++;
  66. }
  67. void emit32(uint32_t val) {
  68. code[codePos + 0] = val;
  69. code[codePos + 1] = val >> 8;
  70. code[codePos + 2] = val >> 16;
  71. code[codePos + 3] = val >> 24;
  72. codePos += 4;
  73. }
  74. void emit64(uint64_t val) {
  75. code[codePos + 0] = val;
  76. code[codePos + 1] = val >> 8;
  77. code[codePos + 2] = val >> 16;
  78. code[codePos + 3] = val >> 24;
  79. code[codePos + 4] = val >> 32;
  80. code[codePos + 5] = val >> 40;
  81. code[codePos + 6] = val >> 48;
  82. code[codePos + 7] = val >> 56;
  83. codePos += 8;
  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_IADD_RC(Instruction&, int);
  96. void h_ISUB_R(Instruction&, int);
  97. void h_ISUB_M(Instruction&, int);
  98. void h_IMUL_9C(Instruction&, int);
  99. void h_IMUL_R(Instruction&, int);
  100. void h_IMUL_M(Instruction&, int);
  101. void h_IMULH_R(Instruction&, int);
  102. void h_IMULH_M(Instruction&, int);
  103. void h_ISMULH_R(Instruction&, int);
  104. void h_ISMULH_M(Instruction&, int);
  105. void h_IMUL_RCP(Instruction&, int);
  106. void h_ISDIV_C(Instruction&, int);
  107. void h_INEG_R(Instruction&, int);
  108. void h_IXOR_R(Instruction&, int);
  109. void h_IXOR_M(Instruction&, int);
  110. void h_IROR_R(Instruction&, int);
  111. void h_IROL_R(Instruction&, int);
  112. void h_ISWAP_R(Instruction&, int);
  113. void h_FSWAP_R(Instruction&, int);
  114. void h_FADD_R(Instruction&, int);
  115. void h_FADD_M(Instruction&, int);
  116. void h_FSUB_R(Instruction&, int);
  117. void h_FSUB_M(Instruction&, int);
  118. void h_FSCAL_R(Instruction&, int);
  119. void h_FMUL_R(Instruction&, int);
  120. void h_FMUL_M(Instruction&, int);
  121. void h_FDIV_R(Instruction&, int);
  122. void h_FDIV_M(Instruction&, int);
  123. void h_FSQRT_R(Instruction&, int);
  124. void h_COND_R(Instruction&, int);
  125. void h_COND_M(Instruction&, int);
  126. void h_CFROUND(Instruction&, int);
  127. void h_ISTORE(Instruction&, int);
  128. void h_FSTORE(Instruction&, int);
  129. void h_NOP(Instruction&, int);
  130. };
  131. }