Instruction.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. #include "Instruction.hpp"
  16. #include "common.hpp"
  17. namespace RandomX {
  18. void Instruction::print(std::ostream& os) const {
  19. os << names[opcode] << " ";
  20. auto handler = engine[opcode];
  21. (this->*handler)(os);
  22. }
  23. void Instruction::genAddressReg(std::ostream& os) const {
  24. os << ((mod % 4) ? "L1" : "L2") << "[r" << (int)src << "]";
  25. }
  26. void Instruction::genAddressRegDst(std::ostream& os) const {
  27. os << ((mod % 4) ? "L1" : "L2") << "[r" << (int)dst << "]";
  28. }
  29. void Instruction::genAddressImm(std::ostream& os) const {
  30. os << ((mod % 4) ? "L1" : "L2") << "[" << (imm32 & ((mod % 4) ? ScratchpadL1Mask : ScratchpadL2Mask)) << "]";
  31. }
  32. void Instruction::h_IADD_R(std::ostream& os) const {
  33. if (src != dst) {
  34. os << "r" << (int)dst << ", r" << (int)src << std::endl;
  35. }
  36. else {
  37. os << "r" << (int)dst << ", " << imm32 << std::endl;
  38. }
  39. }
  40. void Instruction::h_IADD_M(std::ostream& os) const {
  41. if (src != dst) {
  42. os << "r" << (int)dst << ", ";
  43. genAddressReg(os);
  44. os << std::endl;
  45. }
  46. else {
  47. os << "r" << (int)dst << ", ";
  48. genAddressImm(os);
  49. os << std::endl;
  50. }
  51. }
  52. void Instruction::h_IADD_RC(std::ostream& os) const {
  53. os << "r" << (int)dst << ", r" << (int)src << ", " << imm32 << std::endl;
  54. }
  55. //1 uOP
  56. void Instruction::h_ISUB_R(std::ostream& os) const {
  57. if (src != dst) {
  58. os << "r" << (int)dst << ", r" << (int)src << std::endl;
  59. }
  60. else {
  61. os << "r" << (int)dst << ", " << imm32 << std::endl;
  62. }
  63. }
  64. void Instruction::h_ISUB_M(std::ostream& os) const {
  65. if (src != dst) {
  66. os << "r" << (int)dst << ", ";
  67. genAddressReg(os);
  68. os << std::endl;
  69. }
  70. else {
  71. os << "r" << (int)dst << ", ";
  72. genAddressImm(os);
  73. os << std::endl;
  74. }
  75. }
  76. void Instruction::h_IMUL_9C(std::ostream& os) const {
  77. os << "r" << (int)dst << ", " << imm32 << std::endl;
  78. }
  79. void Instruction::h_IMUL_R(std::ostream& os) const {
  80. if (src != dst) {
  81. os << "r" << (int)dst << ", r" << (int)src << std::endl;
  82. }
  83. else {
  84. os << "r" << (int)dst << ", " << imm32 << std::endl;
  85. }
  86. }
  87. void Instruction::h_IMUL_M(std::ostream& os) const {
  88. if (src != dst) {
  89. os << "r" << (int)dst << ", ";
  90. genAddressReg(os);
  91. os << std::endl;
  92. }
  93. else {
  94. os << "r" << (int)dst << ", ";
  95. genAddressImm(os);
  96. os << std::endl;
  97. }
  98. }
  99. void Instruction::h_IMULH_R(std::ostream& os) const {
  100. if (src != dst) {
  101. os << "r" << (int)dst << ", r" << (int)src << std::endl;
  102. }
  103. else {
  104. os << "r" << (int)dst << ", " << imm32 << std::endl;
  105. }
  106. }
  107. void Instruction::h_IMULH_M(std::ostream& os) const {
  108. if (src != dst) {
  109. os << "r" << (int)dst << ", ";
  110. genAddressReg(os);
  111. os << std::endl;
  112. }
  113. else {
  114. os << "r" << (int)dst << ", ";
  115. genAddressImm(os);
  116. os << std::endl;
  117. }
  118. }
  119. void Instruction::h_ISMULH_R(std::ostream& os) const {
  120. if (src != dst) {
  121. os << "r" << (int)dst << ", r" << (int)src << std::endl;
  122. }
  123. else {
  124. os << "r" << (int)dst << ", " << imm32 << std::endl;
  125. }
  126. }
  127. void Instruction::h_ISMULH_M(std::ostream& os) const {
  128. if (src != dst) {
  129. os << "r" << (int)dst << ", ";
  130. genAddressReg(os);
  131. os << std::endl;
  132. }
  133. else {
  134. os << "r" << (int)dst << ", ";
  135. genAddressImm(os);
  136. os << std::endl;
  137. }
  138. }
  139. void Instruction::h_INEG_R(std::ostream& os) const {
  140. os << "r" << (int)dst << std::endl;
  141. }
  142. void Instruction::h_IXOR_R(std::ostream& os) const {
  143. if (src != dst) {
  144. os << "r" << (int)dst << ", r" << (int)src << std::endl;
  145. }
  146. else {
  147. os << "r" << (int)dst << ", " << imm32 << std::endl;
  148. }
  149. }
  150. void Instruction::h_IXOR_M(std::ostream& os) const {
  151. if (src != dst) {
  152. os << "r" << (int)dst << ", ";
  153. genAddressReg(os);
  154. os << std::endl;
  155. }
  156. else {
  157. os << "r" << (int)dst << ", ";
  158. genAddressImm(os);
  159. os << std::endl;
  160. }
  161. }
  162. void Instruction::h_IROR_R(std::ostream& os) const {
  163. if (src != dst) {
  164. os << "r" << (int)dst << ", r" << (int)src << std::endl;
  165. }
  166. else {
  167. os << "r" << (int)dst << ", " << (imm32 & 63) << std::endl;
  168. }
  169. }
  170. void Instruction::h_IROL_R(std::ostream& os) const {
  171. if (src != dst) {
  172. os << "r" << (int)dst << ", r" << (int)src << std::endl;
  173. }
  174. else {
  175. os << "r" << (int)dst << ", " << (imm32 & 63) << std::endl;
  176. }
  177. }
  178. void Instruction::h_IDIV_C(std::ostream& os) const {
  179. os << "r" << (int)dst << ", " << (uint32_t)imm32 << std::endl;
  180. }
  181. void Instruction::h_ISDIV_C(std::ostream& os) const {
  182. os << "r" << (int)dst << ", " << imm32 << std::endl;
  183. }
  184. void Instruction::h_ISWAP_R(std::ostream& os) const {
  185. os << "r" << (int)dst << ", r" << (int)src << std::endl;
  186. }
  187. void Instruction::h_FPSWAP_R(std::ostream& os) const {
  188. const char reg = (dst >= 4) ? 'e' : 'f';
  189. auto dstIndex = dst % 4;
  190. os << reg << dstIndex << std::endl;
  191. }
  192. void Instruction::h_FPADD_R(std::ostream& os) const {
  193. auto dstIndex = dst % 4;
  194. auto srcIndex = src % 4;
  195. os << "f" << dstIndex << ", a" << srcIndex << std::endl;
  196. }
  197. void Instruction::h_FPADD_M(std::ostream& os) const {
  198. auto dstIndex = dst % 4;
  199. os << "f" << dstIndex << ", ";
  200. genAddressReg(os);
  201. os << std::endl;
  202. }
  203. void Instruction::h_FPSUB_R(std::ostream& os) const {
  204. auto dstIndex = dst % 4;
  205. auto srcIndex = src % 4;
  206. os << "f" << dstIndex << ", a" << srcIndex << std::endl;
  207. }
  208. void Instruction::h_FPSUB_M(std::ostream& os) const {
  209. auto dstIndex = dst % 4;
  210. os << "f" << dstIndex << ", ";
  211. genAddressReg(os);
  212. os << std::endl;
  213. }
  214. void Instruction::h_FPNEG_R(std::ostream& os) const {
  215. auto dstIndex = dst % 4;
  216. os << "f" << dstIndex << std::endl;
  217. }
  218. void Instruction::h_FPMUL_R(std::ostream& os) const {
  219. auto dstIndex = dst % 4;
  220. auto srcIndex = src % 4;
  221. os << "e" << dstIndex << ", a" << srcIndex << std::endl;
  222. }
  223. void Instruction::h_FPMUL_M(std::ostream& os) const {
  224. auto dstIndex = dst % 4;
  225. os << "e" << dstIndex << ", ";
  226. genAddressReg(os);
  227. os << std::endl;
  228. }
  229. void Instruction::h_FPDIV_R(std::ostream& os) const {
  230. auto dstIndex = dst % 4;
  231. auto srcIndex = src % 4;
  232. os << "e" << dstIndex << ", a" << srcIndex << std::endl;
  233. }
  234. void Instruction::h_FPDIV_M(std::ostream& os) const {
  235. auto dstIndex = dst % 4;
  236. os << "e" << dstIndex << ", ";
  237. genAddressReg(os);
  238. os << std::endl;
  239. }
  240. void Instruction::h_FPSQRT_R(std::ostream& os) const {
  241. auto dstIndex = dst % 4;
  242. os << "e" << dstIndex << std::endl;
  243. }
  244. void Instruction::h_CFROUND(std::ostream& os) const {
  245. os << "r" << (int)src << ", " << (imm32 & 63) << std::endl;
  246. }
  247. static inline const char* condition(int index) {
  248. switch (index)
  249. {
  250. case 0:
  251. return "be";
  252. case 1:
  253. return "ab";
  254. case 2:
  255. return "sg";
  256. case 3:
  257. return "ns";
  258. case 4:
  259. return "of";
  260. case 5:
  261. return "no";
  262. case 6:
  263. return "lt";
  264. case 7:
  265. return "ge";
  266. }
  267. }
  268. void Instruction::h_COND_R(std::ostream& os) const {
  269. os << "r" << (int)dst << ", " << condition((mod >> 2) & 7) << "(r" << (int)src << ", " << imm32 << ")" << std::endl;
  270. }
  271. void Instruction::h_COND_M(std::ostream& os) const {
  272. os << "r" << (int)dst << ", " << condition((mod >> 2) & 7) << "(";
  273. genAddressReg(os);
  274. os << ", " << imm32 << ")" << std::endl;
  275. }
  276. void Instruction::h_ISTORE(std::ostream& os) const {
  277. genAddressRegDst(os);
  278. os << ", r" << (int)src << std::endl;
  279. }
  280. void Instruction::h_FSTORE(std::ostream& os) const {
  281. const char reg = (src >= 4) ? 'e' : 'f';
  282. genAddressRegDst(os);
  283. auto srcIndex = src % 4;
  284. os << ", " << reg << srcIndex << std::endl;
  285. }
  286. void Instruction::h_NOP(std::ostream& os) const {
  287. os << std::endl;
  288. }
  289. #include "instructionWeights.hpp"
  290. #define INST_NAME(x) REPN(#x, WT(x))
  291. #define INST_HANDLE(x) REPN(&Instruction::h_##x, WT(x))
  292. const char* Instruction::names[256] = {
  293. //Integer
  294. INST_NAME(IADD_R)
  295. INST_NAME(IADD_M)
  296. INST_NAME(IADD_RC)
  297. INST_NAME(ISUB_R)
  298. INST_NAME(ISUB_M)
  299. INST_NAME(IMUL_9C)
  300. INST_NAME(IMUL_R)
  301. INST_NAME(IMUL_M)
  302. INST_NAME(IMULH_R)
  303. INST_NAME(IMULH_M)
  304. INST_NAME(ISMULH_R)
  305. INST_NAME(ISMULH_M)
  306. INST_NAME(IDIV_C)
  307. INST_NAME(ISDIV_C)
  308. INST_NAME(INEG_R)
  309. INST_NAME(IXOR_R)
  310. INST_NAME(IXOR_M)
  311. INST_NAME(IROR_R)
  312. INST_NAME(IROL_R)
  313. INST_NAME(ISWAP_R)
  314. //Common floating point
  315. INST_NAME(FPSWAP_R)
  316. //Floating point group F
  317. INST_NAME(FPADD_R)
  318. INST_NAME(FPADD_M)
  319. INST_NAME(FPSUB_R)
  320. INST_NAME(FPSUB_M)
  321. INST_NAME(FPNEG_R)
  322. //Floating point group E
  323. INST_NAME(FPMUL_R)
  324. INST_NAME(FPMUL_M)
  325. INST_NAME(FPDIV_R)
  326. INST_NAME(FPDIV_M)
  327. INST_NAME(FPSQRT_R)
  328. //Control
  329. INST_NAME(COND_R)
  330. INST_NAME(COND_M)
  331. INST_NAME(CFROUND)
  332. INST_NAME(ISTORE)
  333. INST_NAME(FSTORE)
  334. INST_NAME(NOP)
  335. };
  336. InstructionVisualizer Instruction::engine[256] = {
  337. //Integer
  338. INST_HANDLE(IADD_R)
  339. INST_HANDLE(IADD_M)
  340. INST_HANDLE(IADD_RC)
  341. INST_HANDLE(ISUB_R)
  342. INST_HANDLE(ISUB_M)
  343. INST_HANDLE(IMUL_9C)
  344. INST_HANDLE(IMUL_R)
  345. INST_HANDLE(IMUL_M)
  346. INST_HANDLE(IMULH_R)
  347. INST_HANDLE(IMULH_M)
  348. INST_HANDLE(ISMULH_R)
  349. INST_HANDLE(ISMULH_M)
  350. INST_HANDLE(IDIV_C)
  351. INST_HANDLE(ISDIV_C)
  352. INST_HANDLE(INEG_R)
  353. INST_HANDLE(IXOR_R)
  354. INST_HANDLE(IXOR_M)
  355. INST_HANDLE(IROR_R)
  356. INST_HANDLE(IROL_R)
  357. INST_HANDLE(ISWAP_R)
  358. //Common floating point
  359. INST_HANDLE(FPSWAP_R)
  360. //Floating point group F
  361. INST_HANDLE(FPADD_R)
  362. INST_HANDLE(FPADD_M)
  363. INST_HANDLE(FPSUB_R)
  364. INST_HANDLE(FPSUB_M)
  365. INST_HANDLE(FPNEG_R)
  366. //Floating point group E
  367. INST_HANDLE(FPMUL_R)
  368. INST_HANDLE(FPMUL_M)
  369. INST_HANDLE(FPDIV_R)
  370. INST_HANDLE(FPDIV_M)
  371. INST_HANDLE(FPSQRT_R)
  372. //Control
  373. INST_HANDLE(COND_R)
  374. INST_HANDLE(COND_M)
  375. INST_HANDLE(CFROUND)
  376. INST_HANDLE(ISTORE)
  377. INST_HANDLE(FSTORE)
  378. INST_HANDLE(NOP)
  379. };
  380. }