codegen.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #include "decompiler/control_flow.h"
  22. #include "decompiler/decompiler_disassembler.h"
  23. #include "decompiler/graph.h"
  24. #include "decompiler/decompiler_codegen.h"
  25. #include "decompiler/scummv6/engine.h"
  26. #include <vector>
  27. #define GET(vertex) (boost::get(boost::vertex_name, g, vertex))
  28. #include <streambuf>
  29. #include <ostream>
  30. #include <gmock/gmock.h>
  31. #include "util.h"
  32. #include "make_unique.h"
  33. std::string removeSpaces(std::string s) {
  34. size_t found;
  35. while ((found = s.find(' ')) != std::string::npos)
  36. s = s.erase(found, 1);
  37. return s;
  38. }
  39. typedef std::vector<std::string>::iterator CodeIterator;
  40. TEST(CodeGen, testContinue) {
  41. InstVec insts;
  42. auto engine = std::make_unique<Scumm::v6::Scummv6Engine>();
  43. auto d = engine->getDisassembler(insts);
  44. d->open("decompiler/test/continue-do-while.dmp");
  45. d->disassemble();
  46. auto c = std::make_unique<ControlFlow>(insts, *engine);
  47. c->createGroups();
  48. Graph g = c->analyze();
  49. onullstream ns;
  50. auto cg = engine->getCodeGenerator(insts, ns);
  51. cg->generate(insts, g);
  52. VertexIterator v = boost::vertices(g).first;
  53. std::vector<std::string> output, expected;
  54. expected.push_back("do{");
  55. expected.push_back("if(18 != var321) {");
  56. expected.push_back("continue;");
  57. expected.push_back("}");
  58. expected.push_back("VAR_CHARSET_MASK--;");
  59. expected.push_back("} while (42 == VAR_CHARSET_MASK)");
  60. expected.push_back("stopObjectCodeA();");
  61. GroupPtr gr = GET(*v);
  62. // Find first node
  63. while (gr->_prev != NULL)
  64. gr = gr->_prev;
  65. // Copy out all lines of code
  66. while (gr != NULL) {
  67. for (std::vector<CodeLine>::iterator it = gr->_code.begin(); it != gr->_code.end(); ++it)
  68. output.push_back(it->_line);
  69. gr = gr->_next;
  70. }
  71. ASSERT_TRUE(output.size() == expected.size());
  72. CodeIterator it, it2;
  73. for (it = output.begin(), it2 = expected.begin(); it != output.end() && it2 != expected.end(); ++it, ++it2) {
  74. ASSERT_TRUE(removeSpaces(*it).compare(removeSpaces(*it2)) == 0);
  75. }
  76. }
  77. TEST(CodeGen, testBreak) {
  78. InstVec insts;
  79. auto engine = std::make_unique<Scumm::v6::Scummv6Engine>();
  80. auto d = engine->getDisassembler(insts);
  81. d->open("decompiler/test/break-while.dmp");
  82. d->disassemble();
  83. auto c = std::make_unique<ControlFlow>(insts, *engine);
  84. c->createGroups();
  85. Graph g = c->analyze();
  86. onullstream ns;
  87. auto cg = engine->getCodeGenerator(insts, ns);
  88. cg->generate(insts, g);
  89. VertexIterator v = boost::vertices(g).first;
  90. std::vector<std::string> output, expected;
  91. expected.push_back("while (42 != VAR_CHARSET_MASK) {");
  92. expected.push_back("if (18 != var321) {");
  93. expected.push_back("break;");
  94. expected.push_back("}");
  95. expected.push_back("VAR_CHARSET_MASK--;");
  96. expected.push_back("}");
  97. expected.push_back("stopObjectCodeA();");
  98. GroupPtr gr = GET(*v);
  99. // Find first node
  100. while (gr->_prev != NULL)
  101. gr = gr->_prev;
  102. // Copy out all lines of code
  103. while (gr != NULL) {
  104. for (std::vector<CodeLine>::iterator it = gr->_code.begin(); it != gr->_code.end(); ++it)
  105. output.push_back(it->_line);
  106. gr = gr->_next;
  107. }
  108. ASSERT_TRUE(output.size() == expected.size());
  109. CodeIterator it, it2;
  110. for (it = output.begin(), it2 = expected.begin(); it != output.end() && it2 != expected.end(); ++it, ++it2) {
  111. ASSERT_TRUE(removeSpaces(*it).compare(removeSpaces(*it2)) == 0);
  112. }
  113. }
  114. TEST(CodeGen, testElse) {
  115. InstVec insts;
  116. auto engine = std::make_unique<Scumm::v6::Scummv6Engine>();
  117. auto d = engine->getDisassembler(insts);
  118. d->open("decompiler/test/if-else.dmp");
  119. d->disassemble();
  120. auto c = std::make_unique<ControlFlow>(insts, *engine);
  121. c->createGroups();
  122. Graph g = c->analyze();
  123. onullstream ns;
  124. auto cg = engine->getCodeGenerator(insts, ns);
  125. cg->generate(insts, g);
  126. VertexIterator v = boost::vertices(g).first;
  127. std::vector<std::string> output, expected;
  128. expected.push_back("if (42 != VAR_CHARSET_MASK) {");
  129. expected.push_back("VAR_CHARSET_MASK--;");
  130. expected.push_back("} else {");
  131. expected.push_back("VAR_CHARSET_MASK++;");
  132. expected.push_back("}");
  133. expected.push_back("stopObjectCodeA();");
  134. GroupPtr gr = GET(*v);
  135. // Find first node
  136. while (gr->_prev != NULL)
  137. gr = gr->_prev;
  138. // Copy out all lines of code
  139. while (gr != NULL) {
  140. for (std::vector<CodeLine>::iterator it = gr->_code.begin(); it != gr->_code.end(); ++it)
  141. output.push_back(it->_line);
  142. gr = gr->_next;
  143. }
  144. ASSERT_TRUE(output.size() == expected.size());
  145. CodeIterator it, it2;
  146. for (it = output.begin(), it2 = expected.begin(); it != output.end() && it2 != expected.end(); ++it, ++it2) {
  147. ASSERT_TRUE(removeSpaces(*it).compare(removeSpaces(*it2)) == 0);
  148. }
  149. }
  150. // This test requires script-30 and script-48.dmp from Sam & Max: Hit The Road.
  151. // 6e48faca13e1f6df9341567608962744 *script-30.dmp
  152. // afd7dc5d377894b3b9d0504927adf1b1 *script-48.dmp
  153. // Disabled as mentioned file is copyrighted
  154. TEST(CodeGen, DISABLED_testCoalescing) {
  155. InstVec insts;
  156. auto engine = std::make_unique<Scumm::v6::Scummv6Engine>();
  157. auto d = engine->getDisassembler(insts);
  158. d->open("decompiler/test/script-30.dmp");
  159. d->disassemble();
  160. auto c = std::make_unique<ControlFlow>(insts, *engine);
  161. c->createGroups();
  162. Graph g = c->analyze();
  163. onullstream ns;
  164. auto cg = engine->getCodeGenerator(insts, ns);
  165. cg->generate(insts, g);
  166. VertexIterator v = boost::vertices(g).first;
  167. GroupPtr gr = GET(*v);
  168. // Find first node
  169. while (gr->_prev != NULL)
  170. gr = gr->_prev;
  171. // Find vertex to test
  172. while ((*gr->_start)->_address != 0x91)
  173. gr = gr->_next;
  174. ASSERT_TRUE(gr->_code.size() == 2);
  175. ASSERT_TRUE(removeSpaces(gr->_code[0]._line).compare("}else{") == 0);
  176. ASSERT_TRUE(removeSpaces(gr->_code[1]._line).substr(0, 2).compare("if") == 0);
  177. insts.clear();
  178. engine = std::make_unique<Scumm::v6::Scummv6Engine>();
  179. d = engine->getDisassembler(insts);
  180. d->open("decompiler/test/script-48.dmp");
  181. d->disassemble();
  182. c = std::make_unique<ControlFlow>(insts, *engine);
  183. c->createGroups();
  184. g = c->analyze();
  185. cg = engine->getCodeGenerator(insts, ns);
  186. cg->generate(insts, g);
  187. v = boost::vertices(g).first;
  188. gr = GET(*v);
  189. // Find first node
  190. while (gr->_prev != NULL)
  191. gr = gr->_prev;
  192. // Find vertex to test
  193. while ((*gr->_start)->_address != 0x191)
  194. gr = gr->_next;
  195. ASSERT_TRUE(gr->_code.size() == 1);
  196. ASSERT_TRUE(removeSpaces(gr->_code[0]._line).substr(0, 7).compare("}elseif") == 0);
  197. }