assembly_generator_x86.hpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright (c) 2018-2019, tevador <tevador@gmail.com>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of the copyright holder nor the
  12. names of its contributors may be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  18. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  20. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  22. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #pragma once
  26. #include "common.hpp"
  27. #include <sstream>
  28. namespace randomx {
  29. class Program;
  30. class SuperscalarProgram;
  31. class AssemblyGeneratorX86;
  32. class Instruction;
  33. typedef void(AssemblyGeneratorX86::*InstructionGenerator)(Instruction&, int);
  34. class AssemblyGeneratorX86 {
  35. public:
  36. void generateProgram(Program& prog);
  37. void generateAsm(SuperscalarProgram& prog);
  38. void generateC(SuperscalarProgram& prog);
  39. void printCode(std::ostream& os) {
  40. os << asmCode.rdbuf();
  41. }
  42. private:
  43. void genAddressReg(Instruction&, const char*);
  44. void genAddressRegDst(Instruction&, int);
  45. int32_t genAddressImm(Instruction&);
  46. void generateCode(Instruction&, int);
  47. void traceint(Instruction&);
  48. void traceflt(Instruction&);
  49. void tracenop(Instruction&);
  50. void h_IADD_RS(Instruction&, int);
  51. void h_IADD_M(Instruction&, int);
  52. void h_ISUB_R(Instruction&, int);
  53. void h_ISUB_M(Instruction&, int);
  54. void h_IMUL_R(Instruction&, int);
  55. void h_IMUL_M(Instruction&, int);
  56. void h_IMULH_R(Instruction&, int);
  57. void h_IMULH_M(Instruction&, int);
  58. void h_ISMULH_R(Instruction&, int);
  59. void h_ISMULH_M(Instruction&, int);
  60. void h_IMUL_RCP(Instruction&, int);
  61. void h_INEG_R(Instruction&, int);
  62. void h_IXOR_R(Instruction&, int);
  63. void h_IXOR_M(Instruction&, int);
  64. void h_IROR_R(Instruction&, int);
  65. void h_IROL_R(Instruction&, int);
  66. void h_ISWAP_R(Instruction&, int);
  67. void h_FSWAP_R(Instruction&, int);
  68. void h_FADD_R(Instruction&, int);
  69. void h_FADD_M(Instruction&, int);
  70. void h_FSUB_R(Instruction&, int);
  71. void h_FSUB_M(Instruction&, int);
  72. void h_FSCAL_R(Instruction&, int);
  73. void h_FMUL_R(Instruction&, int);
  74. void h_FDIV_M(Instruction&, int);
  75. void h_FSQRT_R(Instruction&, int);
  76. void h_CBRANCH(Instruction&, int);
  77. void h_CFROUND(Instruction&, int);
  78. void h_ISTORE(Instruction&, int);
  79. void h_NOP(Instruction&, int);
  80. static InstructionGenerator engine[256];
  81. std::stringstream asmCode;
  82. int registerUsage[RegistersCount];
  83. };
  84. }