Instruction.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 <cstdint>
  17. #include <iostream>
  18. #include "blake2/endian.h"
  19. namespace RandomX {
  20. class Instruction;
  21. typedef void(Instruction::*InstructionVisualizer)(std::ostream&) const;
  22. namespace InstructionType {
  23. constexpr int IADD_R = 0;
  24. constexpr int IADD_M = 1;
  25. constexpr int IADD_RC = 2;
  26. constexpr int ISUB_R = 3;
  27. constexpr int ISUB_M = 4;
  28. constexpr int IMUL_9C = 5;
  29. constexpr int IMUL_R = 6;
  30. constexpr int IMUL_M = 7;
  31. constexpr int IMULH_R = 8;
  32. constexpr int IMULH_M = 9;
  33. constexpr int ISMULH_R = 10;
  34. constexpr int ISMULH_M = 11;
  35. constexpr int IMUL_RCP = 12;
  36. //constexpr int ISDIV_C = 13;
  37. constexpr int INEG_R = 14;
  38. constexpr int IXOR_R = 15;
  39. constexpr int IXOR_M = 16;
  40. constexpr int IROR_R = 17;
  41. constexpr int IROL_R = 18;
  42. constexpr int ISWAP_R = 19;
  43. constexpr int FSWAP_R = 20;
  44. constexpr int FADD_R = 21;
  45. constexpr int FADD_M = 22;
  46. constexpr int FSUB_R = 23;
  47. constexpr int FSUB_M = 24;
  48. constexpr int FSCAL_R = 25;
  49. constexpr int FMUL_R = 26;
  50. constexpr int FMUL_M = 27;
  51. constexpr int FDIV_R = 28;
  52. constexpr int FDIV_M = 29;
  53. constexpr int FSQRT_R = 30;
  54. constexpr int COND_R = 31;
  55. constexpr int COND_M = 32;
  56. constexpr int CFROUND = 33;
  57. constexpr int ISTORE = 34;
  58. constexpr int FSTORE = 35;
  59. constexpr int NOP = 36;
  60. }
  61. class Instruction {
  62. public:
  63. uint8_t opcode;
  64. uint8_t dst;
  65. uint8_t src;
  66. uint8_t mod;
  67. uint32_t getImm32() const {
  68. return load32(&imm32);
  69. }
  70. void setImm32(uint32_t val) {
  71. return store32(&imm32, val);
  72. }
  73. const char* getName() const {
  74. return names[opcode];
  75. }
  76. friend std::ostream& operator<<(std::ostream& os, const Instruction& i) {
  77. i.print(os);
  78. return os;
  79. }
  80. private:
  81. uint32_t imm32;
  82. void print(std::ostream&) const;
  83. static const char* names[256];
  84. static InstructionVisualizer engine[256];
  85. void genAddressReg(std::ostream& os) const;
  86. void genAddressImm(std::ostream& os) const;
  87. void genAddressRegDst(std::ostream&) const;
  88. void h_IADD_RS(std::ostream&) const;
  89. void h_IADD_M(std::ostream&) const;
  90. void h_IADD_RC(std::ostream&) const;
  91. void h_ISUB_R(std::ostream&) const;
  92. void h_ISUB_M(std::ostream&) const;
  93. void h_IMUL_9C(std::ostream&) const;
  94. void h_IMUL_R(std::ostream&) const;
  95. void h_IMUL_M(std::ostream&) const;
  96. void h_IMULH_R(std::ostream&) const;
  97. void h_IMULH_M(std::ostream&) const;
  98. void h_ISMULH_R(std::ostream&) const;
  99. void h_ISMULH_M(std::ostream&) const;
  100. void h_IMUL_RCP(std::ostream&) const;
  101. void h_ISDIV_C(std::ostream&) const;
  102. void h_INEG_R(std::ostream&) const;
  103. void h_IXOR_R(std::ostream&) const;
  104. void h_IXOR_M(std::ostream&) const;
  105. void h_IROR_R(std::ostream&) const;
  106. void h_IROL_R(std::ostream&) const;
  107. void h_ISWAP_R(std::ostream&) const;
  108. void h_FSWAP_R(std::ostream&) const;
  109. void h_FADD_R(std::ostream&) const;
  110. void h_FADD_M(std::ostream&) const;
  111. void h_FSUB_R(std::ostream&) const;
  112. void h_FSUB_M(std::ostream&) const;
  113. void h_FSCAL_R(std::ostream&) const;
  114. void h_FMUL_R(std::ostream&) const;
  115. void h_FMUL_M(std::ostream&) const;
  116. void h_FDIV_R(std::ostream&) const;
  117. void h_FDIV_M(std::ostream&) const;
  118. void h_FSQRT_R(std::ostream&) const;
  119. void h_COND_R(std::ostream&) const;
  120. void h_COND_M(std::ostream&) const;
  121. void h_CFROUND(std::ostream&) const;
  122. void h_ISTORE(std::ostream&) const;
  123. void h_FSTORE(std::ostream&) const;
  124. void h_NOP(std::ostream&) const;
  125. };
  126. static_assert(sizeof(Instruction) == 8, "Invalid alignment of struct Instruction");
  127. }