decompiler_engine.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* ScummVM Tools
  2. *
  3. * ScummVM Tools is the legal property of its developers, whose
  4. * names are too numerous to list here. Please refer to the
  5. * COPYRIGHT file distributed with this source distribution.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. */
  21. #ifndef ENGINE_H
  22. #define ENGINE_H
  23. #include "decompiler_disassembler.h"
  24. #include "decompiler_codegen.h"
  25. #include <set>
  26. #include <string>
  27. #include <vector>
  28. /**
  29. * Structure representing a function.
  30. */
  31. class Function
  32. {
  33. public:
  34. uint32 mStartAddr = 0;
  35. uint32 mEndAddr = 0;
  36. uint32 mNumInstructions = 0;
  37. //InstIterator _startIt; ///< Iterator to of the first instruction in the function, if available.
  38. //InstIterator _endIt; ///< Iterator to the instruction immediately after the function, similar to end() on STL containers. If _endIt == _startIt, the function endpoint is assumed to be unknown.
  39. std::string _name; ///< Function name.
  40. GraphVertex _v; ///< Graph vertex for the entry point to the function.
  41. uint32 _args; ///< Number of arguments to the function.
  42. bool _retVal; ///< Whether or not the function returns a value.
  43. std::string _metadata; ///< Metadata for code generation.
  44. /**
  45. * Parameterless constructor for Function. Required for use with STL, should not be called manually.
  46. */
  47. Function() {
  48. }
  49. /**
  50. * Constructor for Function.
  51. *
  52. * @param startIt Index of the first instruction in the function.
  53. * @param endIt Index of the instruction immediately after the function, similar to end() on STL containers.
  54. */
  55. // Function(InstIterator startIt, InstIterator endIt) : _startIt(startIt), _endIt(endIt) {}
  56. Function(uint32 startAddr, uint32 endAddr) : mStartAddr(startAddr), mEndAddr(endAddr) {}
  57. };
  58. /**
  59. * Type representing a map of functions, indexed by starting address.
  60. */
  61. typedef std::map<uint32, Function> FuncMap;
  62. /**
  63. * Base class for engines.
  64. */
  65. class Engine
  66. {
  67. public:
  68. virtual ~Engine() = default;
  69. virtual std::unique_ptr<Disassembler> getDisassembler(InstVec &, const std::vector<unsigned char>& )
  70. {
  71. throw NotImplementedException();
  72. }
  73. /**
  74. * Retrieve the disassembler for the engine.
  75. *
  76. * @param insts Reference to the std::vector to place the Instructions in.
  77. * @return Pointer to a Disassembler for the engine.
  78. */
  79. virtual std::unique_ptr<Disassembler> getDisassembler(InstVec &insts) = 0;
  80. /**
  81. * Retrieve the code generator for the engine.
  82. *
  83. * @param output The std::ostream to output the code to.
  84. * @return Pointer to a CodeGenerator for the engine.
  85. */
  86. virtual std::unique_ptr<CodeGenerator> getCodeGenerator(const InstVec& insts, std::ostream &output) = 0;
  87. /**
  88. * Post-processing step after CFG analysis.
  89. * @param insts Reference to the std::vector to place the Instructions in.
  90. * @param g Graph generated from the CFG analysis.
  91. */
  92. virtual void postCFG(InstVec&, Graph) { }
  93. /**
  94. * Whether or not code flow analysis is supported for this engine.
  95. *
  96. * @return True if supported, false if not. If false is returned, code flow analysis should not take place, and -D should be implied.
  97. */
  98. virtual bool supportsCodeFlow() const { return true; }
  99. /**
  100. * Whether or not code generation is supported for this engine.
  101. *
  102. * @return True if supported, false if not. If false is returned, code generation should not take place, and -G should be implied.
  103. */
  104. virtual bool supportsCodeGen() const { return true; }
  105. FuncMap _functions; ///< Map to functions in the current script, indexed by starting address.
  106. /**
  107. * Fill a vector with the names of all variants supported for this engine.
  108. * If variants are not used by this engine, leave the vector empty (default implementation).
  109. *
  110. * @param variants Vector to add the supported variants to.
  111. */
  112. virtual void getVariants(std::vector<std::string>&) const { };
  113. std::string _variant; ///< Engine variant to use for the script.
  114. /**
  115. * Whether or not to use "pure" grouping during code flow analysis.
  116. * With pure grouping, code flow analysis only looks at branches when merging.
  117. * This method may be more appropriate for non-stack-based engines.
  118. *
  119. * @return True if pure grouping should be used, false if not.
  120. */
  121. virtual bool usePureGrouping() const { return false; }
  122. };
  123. #endif