JitCompilerX86.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <cstring>
  19. #include <vector>
  20. namespace RandomX {
  21. class Program;
  22. class JitCompilerX86;
  23. typedef void(JitCompilerX86::*InstructionGeneratorX86)(Instruction&, int);
  24. constexpr uint32_t CodeSize = 64 * 1024;
  25. class JitCompilerX86 {
  26. public:
  27. JitCompilerX86();
  28. void generateProgram(Program&);
  29. void generateProgramLight(Program&);
  30. ProgramFunc getProgramFunc() {
  31. return (ProgramFunc)code;
  32. }
  33. uint8_t* getCode() {
  34. return code;
  35. }
  36. size_t getCodeSize();
  37. private:
  38. static InstructionGeneratorX86 engine[256];
  39. std::vector<int32_t> instructionOffsets;
  40. int registerUsage[8];
  41. uint8_t* code;
  42. int32_t codePos;
  43. template<class P>
  44. void generateCode(P& prog) {
  45. for (unsigned i = 0; i < prog.getSize(); ++i) {
  46. Instruction& instr = prog(i);
  47. instr.src %= RegistersCount;
  48. instr.dst %= RegistersCount;
  49. generateCode(instr, i);
  50. }
  51. }
  52. void generateProgramPrologue(Program&);
  53. void generateProgramEpilogue(Program&);
  54. int getConditionRegister();
  55. void genAddressReg(Instruction&, bool);
  56. void genAddressRegDst(Instruction&, bool);
  57. void genAddressImm(Instruction&);
  58. void genSIB(int scale, int index, int base);
  59. void handleCondition(Instruction&, int);
  60. void generateCode(Instruction&, int);
  61. void emitByte(uint8_t val) {
  62. code[codePos] = val;
  63. codePos++;
  64. }
  65. void emit32(uint32_t val) {
  66. code[codePos + 0] = val;
  67. code[codePos + 1] = val >> 8;
  68. code[codePos + 2] = val >> 16;
  69. code[codePos + 3] = val >> 24;
  70. codePos += 4;
  71. }
  72. void emit64(uint64_t val) {
  73. code[codePos + 0] = val;
  74. code[codePos + 1] = val >> 8;
  75. code[codePos + 2] = val >> 16;
  76. code[codePos + 3] = val >> 24;
  77. code[codePos + 4] = val >> 32;
  78. code[codePos + 5] = val >> 40;
  79. code[codePos + 6] = val >> 48;
  80. code[codePos + 7] = val >> 56;
  81. codePos += 8;
  82. }
  83. template<size_t N>
  84. void emit(const uint8_t (&src)[N]) {
  85. for (unsigned i = 0; i < N; ++i) {
  86. code[codePos + i] = src[i];
  87. }
  88. codePos += N;
  89. }
  90. void h_IADD_R(Instruction&, int);
  91. void h_IADD_M(Instruction&, int);
  92. void h_IADD_RC(Instruction&, int);
  93. void h_ISUB_R(Instruction&, int);
  94. void h_ISUB_M(Instruction&, int);
  95. void h_IMUL_9C(Instruction&, int);
  96. void h_IMUL_R(Instruction&, int);
  97. void h_IMUL_M(Instruction&, int);
  98. void h_IMULH_R(Instruction&, int);
  99. void h_IMULH_M(Instruction&, int);
  100. void h_ISMULH_R(Instruction&, int);
  101. void h_ISMULH_M(Instruction&, int);
  102. void h_IMUL_RCP(Instruction&, int);
  103. void h_ISDIV_C(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_FMUL_M(Instruction&, int);
  118. void h_FDIV_R(Instruction&, int);
  119. void h_FDIV_M(Instruction&, int);
  120. void h_FSQRT_R(Instruction&, int);
  121. void h_COND_R(Instruction&, int);
  122. void h_COND_M(Instruction&, int);
  123. void h_CFROUND(Instruction&, int);
  124. void h_ISTORE(Instruction&, int);
  125. void h_FSTORE(Instruction&, int);
  126. void h_NOP(Instruction&, int);
  127. };
  128. }