Disassembler.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "instruction.h"
  20. #include "unknown_opcode_exception.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. * Used to perform file I/O.
  71. */
  72. std::unique_ptr<BinaryReader> stream_;
  73. /**
  74. * List of disassembled instructions.
  75. */
  76. InstVec &insts_;
  77. /**
  78. * Base address where the script starts.
  79. */
  80. uint32 address_base_;
  81. };