decompiler.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "objectFactory.h"
  22. #include "decompiler_disassembler.h"
  23. #include "decompiler_engine.h"
  24. #include "instruction.h"
  25. #include "control_flow.h"
  26. //#include "scummv6/engine.h"
  27. #include "decompiler_engine.h"
  28. #include <fstream>
  29. #include <iostream>
  30. #include <map>
  31. #include <string>
  32. #include <vector>
  33. #ifdef _MSC_VER
  34. #pragma warning (push)
  35. #pragma warning(disable:4512)
  36. #pragma warning(disable:4100)
  37. #endif
  38. #include <boost/program_options.hpp>
  39. #include <boost/graph/graphviz.hpp>
  40. #ifdef _MSC_VER
  41. #pragma warning (pop)
  42. #endif
  43. namespace po = boost::program_options;
  44. #define ENGINE(id, description, engineClass) engines[std::string(id)] = description; engineFactory.addEntry<engineClass>(std::string(id));
  45. int main(int argc, char** argv) {
  46. try {
  47. std::map<std::string, std::string> engines;
  48. ObjectFactory<std::string, Engine> engineFactory;
  49. ENGINE("scummv6", "SCUMM v6", Engine);//Scumm::v6::Scummv6Engine);
  50. po::options_description visible("Options", 1024);
  51. visible.add_options()
  52. ("help,h", "Produce this help message.")
  53. ("engine,e", po::value<std::string>(), "Engine the script originates from.")
  54. ("list,l", "List the supported engines.")
  55. ("dump-disassembly,d", po::value<std::string>()->implicit_value(""), "Dump the disassembly to a file. Leave out filename to output to stdout.")
  56. ("dump-graph,g", po::value<std::string>()->implicit_value(""), "Output the control flow graph in dot format to a file. Leave out filename to output to stdout.")
  57. ("only-disassembly,D", "Stops after disassembly. Implies -d.")
  58. ("only-graph,G", "Stops after control flow graph has been generated. Implies -g.")
  59. ("show-unreachable,u", "Show the address and contents of unreachable groups in the script.")
  60. ("variant,v", po::value<std::string>()->default_value(""), "Tell the engine that the script is from a specific variant. To see a list of variants supported by a specific engine, use the -h option and the -e option together.")
  61. ("no-stack-effect,s", "Leave out the stack effect when printing raw instructions.");
  62. po::options_description args("", 1024);
  63. args.add(visible).add_options()
  64. ("input-file", po::value<std::string>(), "Input file");
  65. po::positional_options_description fileArg;
  66. fileArg.add("input-file", -1);
  67. po::variables_map vm;
  68. try {
  69. // FIXME: If specified as the last parameter before the input file name, -d currently requires a filename to specified. -d must be specified earlier than that if outputting to stdout.
  70. po::store(po::command_line_parser(argc, argv).options(args).positional(fileArg).run(), vm);
  71. po::notify(vm);
  72. } catch (std::exception& e) {
  73. std::cout << e.what() << std::endl;
  74. }
  75. if (vm.count("list")) {
  76. std::cout << "Available engines:" << "\n";
  77. std::map<std::string, std::string>::iterator it;
  78. for (it = engines.begin(); it != engines.end(); it++)
  79. std::cout << (*it).first << " " << (*it).second << "\n";
  80. return 0;
  81. }
  82. if (vm.count("help") || !vm.count("input-file")) {
  83. std::cout << "Usage: " << argv[0] << " [option...] file" << "\n";
  84. std::cout << visible << "\n";
  85. if (vm.count("engine") && engines.find(vm["engine"].as<std::string>()) != engines.end()) {
  86. Engine *engine = engineFactory.create(vm["engine"].as<std::string>());
  87. std::vector<std::string> variants;
  88. engine->getVariants(variants);
  89. if (variants.empty()) {
  90. std::cout << engines[vm["engine"].as<std::string>()] << " does not use variants.\n";
  91. } else {
  92. std::cout << "Supported variants for " << engines[vm["engine"].as<std::string>()] << ":\n";
  93. for (std::vector<std::string>::iterator i = variants.begin(); i != variants.end(); ++i) {
  94. std::cout << " " << *i << "\n";
  95. }
  96. }
  97. delete engine;
  98. std::cout << "\n";
  99. }
  100. std::cout << "Note: If outputting to stdout, -d or -g must NOT be specified immediately before the input file.\n";
  101. return 1;
  102. }
  103. if (!vm.count("engine")) {
  104. std::cout << "Engine must be specified.\n";
  105. return 2;
  106. } else if (engines.find(vm["engine"].as<std::string>()) == engines.end()) {
  107. std::cout << "Unknown engine.\n";
  108. return 2;
  109. }
  110. if (vm.count("no-stack-effect")) {
  111. setOutputStackEffect(false);
  112. }
  113. std::unique_ptr<Engine> engine(engineFactory.create(vm["engine"].as<std::string>()));
  114. engine->_variant = vm["variant"].as<std::string>();
  115. std::string inputFile = vm["input-file"].as<std::string>();
  116. // Disassembly
  117. InstVec insts;
  118. auto disassembler = engine->GetDisassembler(insts);
  119. disassembler->open(inputFile.c_str());
  120. disassembler->disassemble();
  121. if (vm.count("dump-disassembly")) {
  122. std::streambuf *buf;
  123. std::ofstream of;
  124. if (vm["dump-disassembly"].as<std::string>() != "") {
  125. of.open(vm["dump-disassembly"].as<std::string>().c_str());
  126. buf = of.rdbuf();
  127. } else {
  128. buf = std::cout.rdbuf();
  129. }
  130. std::ostream out(buf);
  131. disassembler->dumpDisassembly(out);
  132. }
  133. if (!engine->supportsCodeFlow() || vm.count("only-disassembly") || insts.empty()) {
  134. if (!vm.count("dump-disassembly")) {
  135. disassembler->dumpDisassembly(std::cout);
  136. }
  137. return 0;
  138. }
  139. // Control flow analysis
  140. auto cf = std::make_unique<ControlFlow>(insts, *engine);
  141. cf->createGroups();
  142. Graph g = cf->analyze();
  143. if (vm.count("dump-graph")) {
  144. std::streambuf *buf;
  145. std::ofstream of;
  146. if (vm["dump-graph"].as<std::string>() != "") {
  147. of.open(vm["dump-graph"].as<std::string>().c_str());
  148. buf = of.rdbuf();
  149. } else {
  150. buf = std::cout.rdbuf();
  151. }
  152. std::ostream out(buf);
  153. boost::write_graphviz(out, g, boost::make_label_writer(get(boost::vertex_name, g)), boost::makeArrowheadWriter(get(boost::edge_attribute, g)), GraphProperties(engine.get(), g));
  154. }
  155. if (!engine->supportsCodeGen() || vm.count("only-graph")) {
  156. if (!vm.count("dump-graph")) {
  157. boost::write_graphviz(std::cout, g, boost::make_label_writer(get(boost::vertex_name, g)), boost::makeArrowheadWriter(get(boost::edge_attribute, g)), GraphProperties(engine.get(), g));
  158. }
  159. return 0;
  160. }
  161. // Post-processing of CFG
  162. engine->PostCFG(insts, g);
  163. // Code generation
  164. auto cg = engine->GetCodeGenerator(insts, std::cout);
  165. cg->generate(insts, g);
  166. if (vm.count("show-unreachable")) {
  167. std::vector<GroupPtr> unreachable;
  168. VertexRange vr = boost::vertices(g);
  169. for (VertexIterator v = vr.first; v != vr.second; ++v)
  170. {
  171. GroupPtr gr = boost::get(boost::vertex_name, g, *v);
  172. if (gr->_stackLevel == -1)
  173. unreachable.push_back(gr);
  174. }
  175. if (!unreachable.empty()) {
  176. for (size_t i = 0; i < unreachable.size(); i++) {
  177. if (i == 0) {
  178. if (unreachable.size() == 1)
  179. std::cout << boost::format("\n%d unreachable group detected.\n") % unreachable.size();
  180. else
  181. std::cout << boost::format("\n%d unreachable groups detected.\n") % unreachable.size();
  182. }
  183. std::cout << "Group " << (i + 1) << ":\n";
  184. ConstInstIterator inst = unreachable[i]->_start;
  185. do {
  186. std::cout << *inst;
  187. } while (inst++ != unreachable[i]->_end);
  188. std::cout << "----------\n";
  189. }
  190. }
  191. }
  192. } catch (UnknownOpcodeException &e) {
  193. std::cerr << "ERROR: " << e.what() << "\n";
  194. return 3;
  195. } catch (std::exception &e) {
  196. std::cerr << "ERROR: " << e.what() << "\n";
  197. return 4;
  198. }
  199. return 0;
  200. }