control_flow.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 DEC_CONTROL_FLOW_H
  22. #define DEC_CONTROL_FLOW_H
  23. #include "graph.h"
  24. #include "decompiler_engine.h"
  25. /**
  26. * Class for doing code flow analysis.
  27. */
  28. class ControlFlow {
  29. private:
  30. Graph _g; ///< The control flow graph.
  31. Engine& mEngine; ///< Pointer to the Engine used for the script.
  32. InstVec &mInsts; ///< The instructions being analyzed
  33. std::map<uint32, GraphVertex> _addrMap; ///< Map between addresses and vertices.
  34. /**
  35. * Finds a graph vertex through an instruction.
  36. *
  37. * @param inst The instruction to find the vertex for.
  38. */
  39. GraphVertex find(const InstPtr inst);
  40. /**
  41. * Finds a graph vertex through an instruction iterator.
  42. *
  43. * @param it The iterator to find the vertex for.
  44. */
  45. GraphVertex find(ConstInstIterator it);
  46. /**
  47. * Finds a graph vertex through an address.
  48. *
  49. * @param address The address to find the vertex for.
  50. */
  51. GraphVertex find(uint32 address);
  52. /**
  53. * Merges two graph vertices. g2 will be merged into g1.
  54. *
  55. * @param g1 The first vertex to merge.
  56. * @param g2 The second vertex to merge.
  57. */
  58. void merge(GraphVertex g1, GraphVertex g2);
  59. /**
  60. * Sets the stack level for all instructions, using depth-first search.
  61. *
  62. * @param g The GraphVertex to search from.
  63. * @param level The stack level when g is reached.
  64. */
  65. void setStackLevel(GraphVertex g, int level);
  66. /**
  67. * Merged groups that are part of the same short-circuited condition check.
  68. */
  69. void detectShortCircuit();
  70. /**
  71. * Detects while blocks.
  72. * Do-while detection must be completed before running this method.
  73. */
  74. void detectWhile();
  75. /**
  76. * Detects do-while blocks.
  77. */
  78. void detectDoWhile();
  79. /**
  80. * Detects break statements.
  81. * Do-while and while detection must be completed before running this method.
  82. */
  83. void detectBreak();
  84. /**
  85. * Detects continue statements.
  86. * Do-while and while detection must be completed before running this method.
  87. */
  88. void detectContinue();
  89. /**
  90. * Checks if a candidate break/continue goes to the closest loop.
  91. *
  92. * @param gr The group containing the candidate break/continue.
  93. * @param condGr The group containing the respective loop condition.
  94. * @returns True if the validation succeeded, false if it did not.
  95. */
  96. bool validateBreakOrContinue(GroupPtr gr, GroupPtr condGr);
  97. /**
  98. * Detects if blocks.
  99. * Must be performed after break and continue detection.
  100. */
  101. void detectIf();
  102. /**
  103. * Detects else blocks.
  104. * Must be performed after if detection.
  105. */
  106. void detectElse();
  107. /**
  108. * Checks if a candidate else block will cross block boundaries.
  109. *
  110. * @param ifGroup The group containing the if this else candidate is associated with.
  111. * @param start The group containing the start of the else.
  112. * @param end The group immediately after the group ending the else.
  113. * @returns True if the validation succeeded, false if it did not.
  114. */
  115. bool validateElseBlock(GroupPtr ifGroup, GroupPtr start, GroupPtr end);
  116. public:
  117. ControlFlow(const ControlFlow&) = delete;
  118. ControlFlow& operator = (const ControlFlow&) = delete;
  119. /**
  120. * Gets the current control flow graph.
  121. *
  122. * @returns The current control flow graph.
  123. */
  124. const Graph &getGraph() const { return _g; };
  125. /**
  126. * Constructor for the control flow graph.
  127. *
  128. * @param insts std::vector containing the instructions to analyze control flow for.
  129. * @param engine Pointer to the Engine used for the script.
  130. */
  131. ControlFlow(InstVec& insts, Engine& engine);
  132. /**
  133. * Creates groups suitable for a stack-based machine.
  134. * Before group creation, the expected stack level for each instruction is determined.
  135. * After group creation, short-circuit detection is applied to the groups.
  136. */
  137. void createGroups();
  138. /**
  139. * Performs control flow analysis.
  140. * The constructs are detected in the following order: do-while, while, break, continue, if/else.
  141. *
  142. * @returns The control flow graph after analysis.
  143. */
  144. const Graph &analyze();
  145. };
  146. #endif