AssemblyGeneratorX86.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "Instruction.hpp"
  17. #include "configuration.h"
  18. #include "common.hpp"
  19. #include <sstream>
  20. namespace RandomX {
  21. class Program;
  22. class AssemblyGeneratorX86;
  23. typedef void(AssemblyGeneratorX86::*InstructionGenerator)(Instruction&, int);
  24. class AssemblyGeneratorX86 {
  25. public:
  26. template<class P>
  27. void generateProgram(P& prog) {
  28. for (unsigned i = 0; i < 8; ++i) {
  29. registerUsage[i] = -1;
  30. }
  31. asmCode.str(std::string()); //clear
  32. for (unsigned i = 0; i < prog.getSize(); ++i) {
  33. asmCode << "randomx_isn_" << i << ":" << std::endl;
  34. Instruction& instr = prog(i);
  35. instr.src %= RegistersCount;
  36. instr.dst %= RegistersCount;
  37. generateCode(instr, i);
  38. //asmCode << std::endl;
  39. }
  40. }
  41. void printCode(std::ostream& os) {
  42. os << asmCode.rdbuf();
  43. }
  44. private:
  45. static InstructionGenerator engine[256];
  46. std::stringstream asmCode;
  47. int registerUsage[8];
  48. void genAddressReg(Instruction&, const char*);
  49. void genAddressRegDst(Instruction&, int);
  50. int32_t genAddressImm(Instruction&);
  51. int getConditionRegister();
  52. void handleCondition(Instruction&, int);
  53. void generateCode(Instruction&, int);
  54. void traceint(Instruction&);
  55. void traceflt(Instruction&);
  56. void tracenop(Instruction&);
  57. void h_IADD_RS(Instruction&, int);
  58. void h_IADD_M(Instruction&, int);
  59. void h_IADD_RC(Instruction&, int);
  60. void h_ISUB_R(Instruction&, int);
  61. void h_ISUB_M(Instruction&, int);
  62. void h_IMUL_9C(Instruction&, int);
  63. void h_IMUL_R(Instruction&, int);
  64. void h_IMUL_M(Instruction&, int);
  65. void h_IMULH_R(Instruction&, int);
  66. void h_IMULH_M(Instruction&, int);
  67. void h_ISMULH_R(Instruction&, int);
  68. void h_ISMULH_M(Instruction&, int);
  69. void h_IMUL_RCP(Instruction&, int);
  70. void h_ISDIV_C(Instruction&, int);
  71. void h_INEG_R(Instruction&, int);
  72. void h_IXOR_R(Instruction&, int);
  73. void h_IXOR_M(Instruction&, int);
  74. void h_IROR_R(Instruction&, int);
  75. void h_IROL_R(Instruction&, int);
  76. void h_ISWAP_R(Instruction&, int);
  77. void h_FSWAP_R(Instruction&, int);
  78. void h_FADD_R(Instruction&, int);
  79. void h_FADD_M(Instruction&, int);
  80. void h_FSUB_R(Instruction&, int);
  81. void h_FSUB_M(Instruction&, int);
  82. void h_FSCAL_R(Instruction&, int);
  83. void h_FMUL_R(Instruction&, int);
  84. void h_FMUL_M(Instruction&, int);
  85. void h_FDIV_R(Instruction&, int);
  86. void h_FDIV_M(Instruction&, int);
  87. void h_FSQRT_R(Instruction&, int);
  88. void h_COND_R(Instruction&, int);
  89. void h_COND_M(Instruction&, int);
  90. void h_CFROUND(Instruction&, int);
  91. void h_ISTORE(Instruction&, int);
  92. void h_FSTORE(Instruction&, int);
  93. void h_NOP(Instruction&, int);
  94. };
  95. }