[Scummvm-cvs-logs] SF.net SVN: scummvm:[42311] tools/branches/gsoc2009-decompiler/decompiler

kjdf at users.sourceforge.net kjdf at users.sourceforge.net
Thu Jul 9 19:45:38 CEST 2009


Revision: 42311
          http://scummvm.svn.sourceforge.net/scummvm/?rev=42311&view=rev
Author:   kjdf
Date:     2009-07-09 17:45:36 +0000 (Thu, 09 Jul 2009)

Log Message:
-----------
decompiler: removed dysfunctional code printing

Modified Paths:
--------------
    tools/branches/gsoc2009-decompiler/decompiler/Makefile
    tools/branches/gsoc2009-decompiler/decompiler/decompiler.cpp

Removed Paths:
-------------
    tools/branches/gsoc2009-decompiler/decompiler/syntax.h

Modified: tools/branches/gsoc2009-decompiler/decompiler/Makefile
===================================================================
--- tools/branches/gsoc2009-decompiler/decompiler/Makefile	2009-07-09 17:45:12 UTC (rev 42310)
+++ tools/branches/gsoc2009-decompiler/decompiler/Makefile	2009-07-09 17:45:36 UTC (rev 42311)
@@ -78,7 +78,7 @@
 decompiler$(EXEEXT): decompiler.o graph.o misc.o block.o
 	$(CXX) $(LDFLAGS) -o $@ $+ -lboost_program_options
 
-decompiler.o: instruction.h misc.h parser.h reader.h syntax.h
+decompiler.o: instruction.h misc.h parser.h reader.h
 graph.o: graph.h
 
 ######################################################################

Modified: tools/branches/gsoc2009-decompiler/decompiler/decompiler.cpp
===================================================================
--- tools/branches/gsoc2009-decompiler/decompiler/decompiler.cpp	2009-07-09 17:45:12 UTC (rev 42310)
+++ tools/branches/gsoc2009-decompiler/decompiler/decompiler.cpp	2009-07-09 17:45:36 UTC (rev 42311)
@@ -5,12 +5,15 @@
 
 #include "parser.h"
 #include "graph.h"
-#include "syntax.h"
 
 using namespace std;
 using namespace boost::program_options;
 
+#ifndef foreach
+#define foreach BOOST_FOREACH
+#endif
 
+
 variables_map parseArgs(int argc, char **argv) {
 	variables_map vars;
 	options_description visible("Allowed options");
@@ -21,7 +24,7 @@
 		("blocks", "print basic blocks")
 		("graph-intervals", value<unsigned>(), "print arg-th graph intervals")
 		("graph-struct", "print graph with marked structure information")
-		("decompile", "print decompiled program and exit")
+		//		("decompile", "print decompiled program and exit")
 		("no-remove-jumps", "don't remove jumps-to-jumps")
 		("fontname", value<string>()->default_value("Courier"), "font to use with dot output");
 	options_description options("Allowed options");
@@ -84,10 +87,5 @@
 		cout << cfg.graphvizToString(vars["fontname"].as<string>());
 		exit(0);
 	}
-	if (vars.count("decompile")) {
-		foreach (Statement *stmt, buildAbstractSyntaxTree(cfg))
-			cout << stmt->toString(0);
-		exit(0);
-	}
 	return 0;
 }

Deleted: tools/branches/gsoc2009-decompiler/decompiler/syntax.h
===================================================================
--- tools/branches/gsoc2009-decompiler/decompiler/syntax.h	2009-07-09 17:45:12 UTC (rev 42310)
+++ tools/branches/gsoc2009-decompiler/decompiler/syntax.h	2009-07-09 17:45:36 UTC (rev 42311)
@@ -1,211 +0,0 @@
-#ifndef SYNTAX_H
-#define SYNTAX_H
-
-#include <cassert>
-#include <list>
-#include <sstream>
-#include <string>
-
-#include "instruction.h"
-#include "misc.h"
-
-
-#include <boost/foreach.hpp>
-#ifndef foreach
-#define foreach BOOST_FOREACH
-#endif
-
-#include <iostream>
-using namespace std;
-
-
-struct Statement {
-	virtual std::string toString(unsigned i) = 0;
-	virtual ~Statement() {
-	}
-};
-
-
-struct InstructionWrapper : public Statement {
-
-	Instruction *_instruction;
-
-	InstructionWrapper(Instruction *instruction) : _instruction(instruction) {
-	};
-
-	std::string toString(unsigned i) {
-		return _instruction->toString(i);
-	}
-};
-
-
-struct Goto : public Statement {
-
-	address_t _address;
-
-	Goto(address_t address) : _address(address) {
-	}
-
-	std::string toString(unsigned i) {
-		std::ostringstream ret;
-		ret << spaces(i) << "goto " << phex(_address) << std::endl;
-		return ret.str();
-	}
-};
-
-
-struct WhileLoop : public Statement {
-
-	std::list<InstructionWrapper*> _condition;
-	std::list<Statement*> _body;
-
-	std::string toString(unsigned i) {
-		std::ostringstream ret;
-		ret << std::endl << spaces(i) << "while (" << std::endl;
-		foreach (InstructionWrapper* insn, _condition)
-			ret << insn->toString(i);
-		ret << spaces(i) << ") {" << std::endl;
-		foreach (Statement *stmt, _body)
-			ret << stmt->toString(i+4);
-		ret << spaces(i) << "}" << std::endl << std::endl;
-		return ret.str();
-	}
-};
-
-
-struct DoWhileLoop : public Statement {
-
-	std::list<InstructionWrapper*> _condition;
-	std::list<Statement*> _body;
-
-	std::string toString(unsigned i) {
-		std::ostringstream ret;
-		ret << std::endl << spaces(i) << "do {" << std::endl;
-		foreach (Statement *stmt, _body)
-			ret << stmt->toString(i+4);
-		ret << spaces(i) << "} while (" << std::endl;
-		foreach (InstructionWrapper* insn, _condition)
-			ret << insn->toString(i);
-		ret << spaces(i) << ")" << std::endl << std::endl;
-		return ret.str();
-	}
-};
-
-
-struct IfThenElse : public Statement {
-
-	std::list<InstructionWrapper*> _condition;
-	std::list<Statement*> _consequence;
-	std::list<Statement*> _alternative;
-
-	std::string toString(unsigned i) {
-		std::ostringstream ret;
-		ret << std::endl << spaces(i) << "if (" << std::endl;
-		foreach (InstructionWrapper* insn, _condition)
-			ret << insn->toString(i);
-		ret << spaces(i) << ") {" << std::endl;
-		foreach (Statement *stmt, _consequence)
-			ret << stmt->toString(i+4);
-		ret << spaces(i) << "}";
-		if (!_alternative.empty()) {
-			ret << " else {" << std::endl;
-			foreach (Statement *stmt, _alternative)
-				ret << stmt->toString(i+4);
-			ret << spaces(i) << "}";
-		}
-		ret << std::endl << std::endl;
-		return ret.str();
-	}
-};
-
-
-void append(Block *block, Block *until, std::list<Statement*> &seq);
-
-
-IfThenElse *buildIfThenElse(Block *head) {
-	IfThenElse *ifte = new IfThenElse;
-	foreach (Instruction *insn, head->_instructions)
-		ifte->_condition.push_back(new InstructionWrapper(insn));
-	append(head->_out.back(), head->_ifFollow, ifte->_consequence);
-	append(head->_out.front(), head->_ifFollow, ifte->_alternative);
-	return ifte;
-}
-
-
-DoWhileLoop *buildDoWhileLoop(Block *head) {
-	DoWhileLoop *loop = new DoWhileLoop;
-	foreach (Instruction *insn, head->_loopLatch->_instructions)
-		loop->_condition.push_back(new InstructionWrapper(insn));
-	append(head, head->_loopLatch, loop->_body);
-	return loop;
-}
-
-
-WhileLoop *buildWhileLoop(Block *head) {
-	WhileLoop *loop = new WhileLoop;
-	foreach (Instruction *insn, head->_instructions)
-		loop->_condition.push_back(new InstructionWrapper(insn));
-	append(head->nonFollowEdge(), head->_loopFollow, loop->_body);
-	return loop;
-}
-
-void append(Block *block, Block *until, std::list<Statement*> &seq) {
-	if (block == until)
-		return;
-	if (block->_visited) {
-		// TODO they should be printed more before append() only sometimes
-		seq.push_back(new Goto(block->_instructions.front()->_addr));
-		return;
-	}
-	block->_visited = true;
-	if (block->_loopFollow) {
-		if (block->_loopType == PRE_TESTED)
-			seq.push_back(buildWhileLoop(block));
-		else if (block->_loopType == POST_TESTED)
-			seq.push_back(buildDoWhileLoop(block));
-		// TODO ENDLESS
-		append(block->_loopFollow, until, seq);
-		return;
-	}
-	if (block->_ifFollow) {
-		seq.push_back(buildIfThenElse(block));
-		append(block->_ifFollow, until, seq);
-		return;
-	}
-	foreach (Instruction *insn, block->_instructions) {
-		// TODO jump targets will be broken (pointing to not printed basic blocks) if the jump was rewired
-		Jump *jump = dynamic_cast<Jump*>(insn);
-		CondJump *condjump = dynamic_cast<CondJump*>(insn);
-		if (condjump) {
-			IfThenElse *ifte = new IfThenElse;
-			ifte->_condition.push_back(new InstructionWrapper(new Instruction("not", 0xffff)));
-			ifte->_consequence.push_back(new Goto(jump->target()));
-			seq.push_back(ifte);
-		} else if (jump)
-			seq.push_back(new Goto(jump->target()));
-		else
-			seq.push_back(new InstructionWrapper(insn));
-	}
-	if (block->_out.size() == 2) // TODO?
-		append(block->_out.back(), until, seq);
-	if (block->_out.size() >= 1)
-		append(block->_out.front(), until, seq);
-}
-
-
-std::list<Statement*> buildSequence(Block *block, Block *until=0) {
-	std::list<Statement*> seq;
-	append(block, until, seq);
-	return seq;
-}
-
-
-std::list<Statement*> buildAbstractSyntaxTree(ControlFlowGraph &graph) {
-	foreach (Block *block, graph._blocks)
-		if (dynamic_cast<Jump*>(block->_instructions.back()))
-			block->_instructions.pop_back();
-	return buildSequence(graph._entry);
-}
-
-
-#endif


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list