instruction.hpp 4.0 KB

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