Disassembler.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <iostream>
  17. #include <vector>
  18. #include "common/BinaryReader.h"
  19. #include "DecompilerException.h"
  20. #include "instruction/Instruction.h"
  21. #include "ObjectFactory.h"
  22. /**
  23. * Base class for disassemblers.
  24. */
  25. class Disassembler{
  26. public:
  27. /**
  28. * Constructor for Disassembler.
  29. *
  30. * @param insts[in] Where disassembled instructions will be placed.
  31. */
  32. Disassembler(InstVec &insts);
  33. /**
  34. * Destructor.
  35. */
  36. virtual ~Disassembler() = default;
  37. /**
  38. * Open a file for disassembly.
  39. *
  40. * @param filename[in] The file to Disassemble.
  41. */
  42. virtual void Open(const char *filename);
  43. /**
  44. * Request disassembled instructions.
  45. */
  46. void Disassemble();
  47. /**
  48. * Outputs the disassembled code.
  49. *
  50. * Disassembles code if this has not already been done.
  51. *
  52. * @param output[out] The output stream.
  53. */
  54. void DumpDisassembly(std::ostream &output);
  55. protected:
  56. /**
  57. * Performs disassembly.
  58. *
  59. * @throws UnknownOpcodeException on unknown opcode.
  60. * @throws std::exception on other failures.
  61. */
  62. virtual void DoDisassemble() = 0;
  63. /**
  64. * Outputs the disassembled code.
  65. *
  66. * @param output[out] The output stream.
  67. */
  68. virtual void DoDumpDisassembly(std::ostream &output);
  69. /**
  70. * Read parameters and associate them with an instruction.
  71. *
  72. * @param inst[in] The instruction to associate the parameters with.
  73. * @param types[in] NUL-terminated string describing the type of each
  74. * parameter.
  75. */
  76. void ReadParams(InstPtr inst, const char *types);
  77. /**
  78. * Read parameters but it doesn't associate them with an instruction.
  79. *
  80. * @param inst[in] The instruction to associate the parameters with.
  81. * Unused.
  82. * @param types[in] NUL-terminated string describing the type of each
  83. * parameter.
  84. * @param params[in] Unused.
  85. */
  86. void ReadParams(InstPtr inst, const char *types, const std::vector<std::string>& params);
  87. /**
  88. * Reads data for a single parameter.
  89. *
  90. * @param inst The instruction the parameter will belong to. Unused.
  91. * @param type Character describing the type of the parameter.
  92. * @return The read data.
  93. */
  94. virtual ValuePtr ReadParameter(InstPtr inst, std::string type);
  95. /**
  96. * Used to perform file I/O.
  97. */
  98. std::unique_ptr<BinaryReader> stream_;
  99. /**
  100. * List of disassembled instructions.
  101. */
  102. InstVec &insts_;
  103. /**
  104. * Base address where the script starts.
  105. */
  106. uint32 address_base_;
  107. /**
  108. * The current address.
  109. */
  110. uint32 address_;
  111. };