JitCompilerX86.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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&);
  24. constexpr uint32_t CodeSize = 64 * 1024;
  25. class JitCompilerX86 {
  26. public:
  27. JitCompilerX86();
  28. void generateProgram(Program&);
  29. ProgramFunc getProgramFunc() {
  30. return (ProgramFunc)code;
  31. }
  32. uint8_t* getCode() {
  33. return code;
  34. }
  35. size_t getCodeSize();
  36. private:
  37. static InstructionGeneratorX86 engine[256];
  38. uint8_t* code;
  39. int32_t codePos;
  40. void genAddressReg(Instruction&, bool);
  41. void genAddressRegDst(Instruction&, bool);
  42. void genAddressImm(Instruction&);
  43. void genSIB(int scale, int index, int base);
  44. void generateCode(Instruction&);
  45. void emitByte(uint8_t val) {
  46. code[codePos] = val;
  47. codePos++;
  48. }
  49. void emit32(uint32_t val) {
  50. code[codePos + 0] = val;
  51. code[codePos + 1] = val >> 8;
  52. code[codePos + 2] = val >> 16;
  53. code[codePos + 3] = val >> 24;
  54. codePos += 4;
  55. }
  56. void emit64(uint64_t val) {
  57. code[codePos + 0] = val;
  58. code[codePos + 1] = val >> 8;
  59. code[codePos + 2] = val >> 16;
  60. code[codePos + 3] = val >> 24;
  61. code[codePos + 4] = val >> 32;
  62. code[codePos + 5] = val >> 40;
  63. code[codePos + 6] = val >> 48;
  64. code[codePos + 7] = val >> 56;
  65. codePos += 8;
  66. }
  67. template<size_t N>
  68. void emit(const uint8_t (&src)[N]) {
  69. for (unsigned i = 0; i < N; ++i) {
  70. code[codePos + i] = src[i];
  71. }
  72. codePos += N;
  73. }
  74. void h_IADD_R(Instruction&);
  75. void h_IADD_M(Instruction&);
  76. void h_IADD_RC(Instruction&);
  77. void h_ISUB_R(Instruction&);
  78. void h_ISUB_M(Instruction&);
  79. void h_IMUL_9C(Instruction&);
  80. void h_IMUL_R(Instruction&);
  81. void h_IMUL_M(Instruction&);
  82. void h_IMULH_R(Instruction&);
  83. void h_IMULH_M(Instruction&);
  84. void h_ISMULH_R(Instruction&);
  85. void h_ISMULH_M(Instruction&);
  86. void h_IDIV_C(Instruction&);
  87. void h_ISDIV_C(Instruction&);
  88. void h_INEG_R(Instruction&);
  89. void h_IXOR_R(Instruction&);
  90. void h_IXOR_M(Instruction&);
  91. void h_IROR_R(Instruction&);
  92. void h_IROL_R(Instruction&);
  93. void h_ISWAP_R(Instruction&);
  94. void h_FSWAP_R(Instruction&);
  95. void h_FADD_R(Instruction&);
  96. void h_FADD_M(Instruction&);
  97. void h_FSUB_R(Instruction&);
  98. void h_FSUB_M(Instruction&);
  99. void h_FNEG_R(Instruction&);
  100. void h_FMUL_R(Instruction&);
  101. void h_FMUL_M(Instruction&);
  102. void h_FDIV_R(Instruction&);
  103. void h_FDIV_M(Instruction&);
  104. void h_FSQRT_R(Instruction&);
  105. void h_COND_R(Instruction&);
  106. void h_COND_M(Instruction&);
  107. void h_CFROUND(Instruction&);
  108. void h_ISTORE(Instruction&);
  109. void h_FSTORE(Instruction&);
  110. void h_NOP(Instruction&);
  111. };
  112. }