jit_compiler_a64.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Copyright (c) 2018-2019, tevador <tevador@gmail.com>
  3. Copyright (c) 2019, SChernykh <https://github.com/SChernykh>
  4. All rights reserved.
  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. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <cstdint>
  28. #include <vector>
  29. #include <stdexcept>
  30. #include "common.hpp"
  31. #include "jit_compiler_a64_static.hpp"
  32. namespace randomx {
  33. class Program;
  34. struct ProgramConfiguration;
  35. class SuperscalarProgram;
  36. class Instruction;
  37. typedef void(JitCompilerA64::*InstructionGeneratorA64)(Instruction&, uint32_t&);
  38. class JitCompilerA64 {
  39. public:
  40. JitCompilerA64();
  41. ~JitCompilerA64();
  42. void generateProgram(Program&, ProgramConfiguration&);
  43. void generateProgramLight(Program&, ProgramConfiguration&, uint32_t);
  44. template<size_t N>
  45. void generateSuperscalarHash(SuperscalarProgram(&programs)[N], std::vector<uint64_t> &);
  46. void generateDatasetInitCode() {}
  47. ProgramFunc* getProgramFunc() { return reinterpret_cast<ProgramFunc*>(code); }
  48. DatasetInitFunc* getDatasetInitFunc();
  49. uint8_t* getCode() { return code; }
  50. size_t getCodeSize();
  51. void enableWriting();
  52. void enableExecution();
  53. void enableAll();
  54. private:
  55. static InstructionGeneratorA64 engine[256];
  56. uint32_t reg_changed_offset[8];
  57. uint8_t* code;
  58. uint32_t literalPos;
  59. uint32_t num32bitLiterals;
  60. static void emit32(uint32_t val, uint8_t* code, uint32_t& codePos)
  61. {
  62. *(uint32_t*)(code + codePos) = val;
  63. codePos += sizeof(val);
  64. }
  65. static void emit64(uint64_t val, uint8_t* code, uint32_t& codePos)
  66. {
  67. *(uint64_t*)(code + codePos) = val;
  68. codePos += sizeof(val);
  69. }
  70. void emitMovImmediate(uint32_t dst, uint32_t imm, uint8_t* code, uint32_t& codePos);
  71. void emitAddImmediate(uint32_t dst, uint32_t src, uint32_t imm, uint8_t* code, uint32_t& codePos);
  72. template<uint32_t tmp_reg>
  73. void emitMemLoad(uint32_t dst, uint32_t src, Instruction& instr, uint8_t* code, uint32_t& codePos);
  74. template<uint32_t tmp_reg_fp>
  75. void emitMemLoadFP(uint32_t src, Instruction& instr, uint8_t* code, uint32_t& codePos);
  76. void h_IADD_RS(Instruction&, uint32_t&);
  77. void h_IADD_M(Instruction&, uint32_t&);
  78. void h_ISUB_R(Instruction&, uint32_t&);
  79. void h_ISUB_M(Instruction&, uint32_t&);
  80. void h_IMUL_R(Instruction&, uint32_t&);
  81. void h_IMUL_M(Instruction&, uint32_t&);
  82. void h_IMULH_R(Instruction&, uint32_t&);
  83. void h_IMULH_M(Instruction&, uint32_t&);
  84. void h_ISMULH_R(Instruction&, uint32_t&);
  85. void h_ISMULH_M(Instruction&, uint32_t&);
  86. void h_IMUL_RCP(Instruction&, uint32_t&);
  87. void h_INEG_R(Instruction&, uint32_t&);
  88. void h_IXOR_R(Instruction&, uint32_t&);
  89. void h_IXOR_M(Instruction&, uint32_t&);
  90. void h_IROR_R(Instruction&, uint32_t&);
  91. void h_IROL_R(Instruction&, uint32_t&);
  92. void h_ISWAP_R(Instruction&, uint32_t&);
  93. void h_FSWAP_R(Instruction&, uint32_t&);
  94. void h_FADD_R(Instruction&, uint32_t&);
  95. void h_FADD_M(Instruction&, uint32_t&);
  96. void h_FSUB_R(Instruction&, uint32_t&);
  97. void h_FSUB_M(Instruction&, uint32_t&);
  98. void h_FSCAL_R(Instruction&, uint32_t&);
  99. void h_FMUL_R(Instruction&, uint32_t&);
  100. void h_FDIV_M(Instruction&, uint32_t&);
  101. void h_FSQRT_R(Instruction&, uint32_t&);
  102. void h_CBRANCH(Instruction&, uint32_t&);
  103. void h_CFROUND(Instruction&, uint32_t&);
  104. void h_ISTORE(Instruction&, uint32_t&);
  105. void h_NOP(Instruction&, uint32_t&);
  106. };
  107. }