decompiler_engine.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /**
  60. * Type representing a map of functions, indexed by starting address.
  61. */
  62. typedef std::map<uint32, Function> FuncMap;
  63. /**
  64. * Base class for engines.
  65. */
  66. class Engine
  67. {
  68. public:
  69. virtual ~Engine() = default;
  70. virtual std::unique_ptr<Disassembler> GetDisassembler(InstVec &, const std::vector<unsigned char>& )
  71. {
  72. throw NotImplementedException();
  73. }
  74. /**
  75. * Retrieve the disassembler for the engine.
  76. *
  77. * @param insts Reference to the std::vector to place the Instructions in.
  78. * @return Pointer to a Disassembler for the engine.
  79. */
  80. virtual std::unique_ptr<Disassembler> GetDisassembler(InstVec &insts) = 0;
  81. /**
  82. * Retrieve the code generator for the engine.
  83. *
  84. * @param output The std::ostream to output the code to.
  85. * @return Pointer to a CodeGenerator for the engine.
  86. */
  87. virtual std::unique_ptr<CodeGenerator> GetCodeGenerator(const InstVec& insts, std::ostream &output) = 0;
  88. /**
  89. * Post-processing step after CFG analysis.
  90. * @param insts Reference to the std::vector to place the Instructions in.
  91. * @param g Graph generated from the CFG analysis.
  92. */
  93. virtual void PostCFG(InstVec&, Graph) { }
  94. /**
  95. * Whether or not code flow analysis is supported for this engine.
  96. *
  97. * @return True if supported, false if not. If false is returned, code flow analysis should not take place, and -D should be implied.
  98. */
  99. virtual bool supportsCodeFlow() const { return true; }
  100. /**
  101. * Whether or not code generation is supported for this engine.
  102. *
  103. * @return True if supported, false if not. If false is returned, code generation should not take place, and -G should be implied.
  104. */
  105. virtual bool supportsCodeGen() const { return true; }
  106. FuncMap _functions; ///< Map to functions in the current script, indexed by starting address.
  107. /**
  108. * Fill a vector with the names of all variants supported for this engine.
  109. * If variants are not used by this engine, leave the vector empty (default implementation).
  110. *
  111. * @param variants Vector to add the supported variants to.
  112. */
  113. virtual void GetVariants(std::vector<std::string>&) const { };
  114. std::string _variant; ///< Engine variant to use for the script.
  115. /**
  116. * Whether or not to use "pure" grouping during code flow analysis.
  117. * With pure grouping, code flow analysis only looks at branches when merging.
  118. * This method may be more appropriate for non-stack-based engines.
  119. *
  120. * @return True if pure grouping should be used, false if not.
  121. */
  122. virtual bool UsePureGrouping() const { return false; }
  123. };
  124. #endif