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

kjdf at users.sourceforge.net kjdf at users.sourceforge.net
Thu Jul 2 18:06:10 CEST 2009


Revision: 42029
          http://scummvm.svn.sourceforge.net/scummvm/?rev=42029&view=rev
Author:   kjdf
Date:     2009-07-02 16:06:09 +0000 (Thu, 02 Jul 2009)

Log Message:
-----------
decompiler: (ugly) loop printing

Modified Paths:
--------------
    tools/branches/gsoc2009-decompiler/decompiler/Makefile
    tools/branches/gsoc2009-decompiler/decompiler/decompiler.cpp
    tools/branches/gsoc2009-decompiler/decompiler/graph.cpp
    tools/branches/gsoc2009-decompiler/decompiler/graph.h
    tools/branches/gsoc2009-decompiler/decompiler/instruction.h
    tools/branches/gsoc2009-decompiler/decompiler/misc.cpp
    tools/branches/gsoc2009-decompiler/decompiler/misc.h
    tools/branches/gsoc2009-decompiler/decompiler/parser.h

Modified: tools/branches/gsoc2009-decompiler/decompiler/Makefile
===================================================================
--- tools/branches/gsoc2009-decompiler/decompiler/Makefile	2009-07-02 16:04:45 UTC (rev 42028)
+++ tools/branches/gsoc2009-decompiler/decompiler/Makefile	2009-07-02 16:06:09 UTC (rev 42029)
@@ -78,7 +78,7 @@
 decompiler$(EXEEXT): decompiler.o graph.o misc.o
 	$(CXX) $(LDFLAGS) -o $@ $+ -lboost_program_options
 
-decompiler.o: graph.h instruction.h misc.h parser.h reader.h
+decompiler.o: graph.h instruction.h misc.h parser.h reader.h syntax.h
 
 
 ######################################################################

Modified: tools/branches/gsoc2009-decompiler/decompiler/decompiler.cpp
===================================================================
--- tools/branches/gsoc2009-decompiler/decompiler/decompiler.cpp	2009-07-02 16:04:45 UTC (rev 42028)
+++ tools/branches/gsoc2009-decompiler/decompiler/decompiler.cpp	2009-07-02 16:06:09 UTC (rev 42029)
@@ -1,9 +1,11 @@
 #include <iostream>
 
+#include <boost/foreach.hpp>
 #include <boost/program_options.hpp>
 
 #include "parser.h"
 #include "graph.h"
+#include "syntax.h"
 
 using namespace std;
 using namespace boost::program_options;
@@ -17,10 +19,10 @@
 		("disasm", "print disassembly and exit")
 		("blocks", "print basic blocks and exit")
 		("graph",  "print graph and exit")
+		("decompile", "print decompiled program and exit")
 		("fontname", value<string>()->default_value("Courier"), "font to use with graphical output");
 	options_description options("Allowed options");
 	options.add(visible).add_options()
-		//		("derive", value<int>()->default_value(0), "find arg-th order intervals")
 		("inputfile", value<string>(), "input file");
 	positional_options_description pos;
 	pos.add("inputfile", 1);
@@ -55,15 +57,18 @@
 			cout << block->toString() << endl;
 		exit(0);
 	}
+	cfg.removeJumpsToJumps();
+	cfg.orderBlocks();
+	cfg.removeUnreachableBlocks();
+	cfg.loopStruct();
 	if (vars.count("graph")) {
-		cfg.removeJumpsToJumps();
-		cfg.orderBlocks();
-		cfg.removeUnreachableBlocks();
-		//		for (int i = 0; i < vars["derive"].as<int>(); i++)
-		//			cfg.extendIntervals();
-		cfg.loopStruct();
 		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;
 }

Modified: tools/branches/gsoc2009-decompiler/decompiler/graph.cpp
===================================================================
--- tools/branches/gsoc2009-decompiler/decompiler/graph.cpp	2009-07-02 16:04:45 UTC (rev 42028)
+++ tools/branches/gsoc2009-decompiler/decompiler/graph.cpp	2009-07-02 16:06:09 UTC (rev 42029)
@@ -108,9 +108,15 @@
 			}
 		ret << "}" << std::endl;
 	}
-	foreach (Block *u, _blocks)
-		foreach (Block *v, u->_out)
-		ret << '"' << u << "\" -> \"" << v << '"' << (v == u->_loopFollow ? "[color=blue]" : "") << ";" << std::endl;
+	foreach (Block *u, _blocks) {
+		bool hadFollow = false;
+		foreach (Block *v, u->_out) {
+			hadFollow |= v == u->_loopFollow;
+		    ret << '"' << u << "\" -> \"" << v << '"' << (v == u->_loopFollow ? "[color=blue]" : "") << ";" << std::endl;
+		}
+		if (u->_loopFollow && !hadFollow)
+		    ret << '"' << u << "\" -> \"" << u->_loopFollow << '"' << "[color=blue,style=dashed];" << std::endl;
+	}
 	ret << "}" << std::endl;
 	return ret.str();
 }

Modified: tools/branches/gsoc2009-decompiler/decompiler/graph.h
===================================================================
--- tools/branches/gsoc2009-decompiler/decompiler/graph.h	2009-07-02 16:04:45 UTC (rev 42028)
+++ tools/branches/gsoc2009-decompiler/decompiler/graph.h	2009-07-02 16:06:09 UTC (rev 42029)
@@ -21,6 +21,7 @@
 
 struct Block : boost::noncopyable {
 
+	bool _visited;
 	Block *_interval;        // header node of the interval this block belongs to
 	Block *_loopFollow;      // if not null, this block is a loop header, and follow is a first block after exit
     Block *_loopHead;        // if not null, this is a latching block
@@ -40,17 +41,24 @@
 	}
 
 	bool inLoop(Block *head) {
-		return _interval == head && head->_loopLatch->_number <= _number && _number <= head->_number;
+		return _interval == head && head->_loopLatch->_number <= _number && _number < head->_number;
 	}
 
+	Block *nonFollowEdge() {
+		foreach (Block *u, _out)
+			if (u != _loopFollow)
+				return u;
+		return 0;
+	}
+
 	Block *outEdgeOutsideLoop(Block *head) {
 		foreach (Block *u, _out)
-			if (!u->inLoop(head))
+			if (!u->inLoop(head) && u != head)
 				return u;
 		return 0;
 	}
 
-	Block() : _interval(), _number(), _loopHead(), _loopFollow() {
+	Block() : _interval(), _number(), _loopHead(), _loopFollow(), _loopLatch(), _visited() {
 	}
 
 	~Block() {

Modified: tools/branches/gsoc2009-decompiler/decompiler/instruction.h
===================================================================
--- tools/branches/gsoc2009-decompiler/decompiler/instruction.h	2009-07-02 16:04:45 UTC (rev 42028)
+++ tools/branches/gsoc2009-decompiler/decompiler/instruction.h	2009-07-02 16:06:09 UTC (rev 42029)
@@ -14,7 +14,7 @@
 
 	virtual std::string toString() {
 		std::ostringstream ret;
-		ret << phex(_addr-8) << "  " << _description << std::endl;
+		ret << _description << "  [" << phex(_addr-8) << "]" << std::endl;
 		return ret.str();
 	}
 
@@ -32,7 +32,7 @@
 	}
 	std::string toString() {
 		std::ostringstream ret;
-		ret << phex(_addr-8) << "  " << _description << " (" << phex(target()-8) << ")" << std::endl;
+		ret << _description << " (" << phex(target()-8) << ")  [" << phex(_addr-8) << "]" << std::endl;
 		return ret.str();
 	}
 

Modified: tools/branches/gsoc2009-decompiler/decompiler/misc.cpp
===================================================================
--- tools/branches/gsoc2009-decompiler/decompiler/misc.cpp	2009-07-02 16:04:45 UTC (rev 42028)
+++ tools/branches/gsoc2009-decompiler/decompiler/misc.cpp	2009-07-02 16:06:09 UTC (rev 42029)
@@ -6,6 +6,13 @@
 	return ret.str();
 }
 
+std::string spaces(int width) {
+	std::ostringstream ret;
+	while (width--)
+		ret << ' ';
+	return ret.str();
+}
+
 uint32 read_be_uint32(std::ifstream &f) {
 	uint32 ret = 0;
 	ret |= f.get() << 24;

Modified: tools/branches/gsoc2009-decompiler/decompiler/misc.h
===================================================================
--- tools/branches/gsoc2009-decompiler/decompiler/misc.h	2009-07-02 16:04:45 UTC (rev 42028)
+++ tools/branches/gsoc2009-decompiler/decompiler/misc.h	2009-07-02 16:06:09 UTC (rev 42029)
@@ -26,6 +26,7 @@
 }
 
 std::string phex(int i, int width=4);
+std::string spaces(int width);
 uint32 read_be_uint32(std::ifstream &f);
 uint32 read_le_uint32(std::ifstream &f);
 uint16 read_le_uint16(std::ifstream &f);

Modified: tools/branches/gsoc2009-decompiler/decompiler/parser.h
===================================================================
--- tools/branches/gsoc2009-decompiler/decompiler/parser.h	2009-07-02 16:04:45 UTC (rev 42028)
+++ tools/branches/gsoc2009-decompiler/decompiler/parser.h	2009-07-02 16:06:09 UTC (rev 42029)
@@ -65,8 +65,8 @@
 		_reader->registerOpcode(0x57, new SimpleReader("wordVarDec", "w"));
 		_reader->registerOpcode(0x5a, new SimpleReader("byteArrayDec", "b"));
 		_reader->registerOpcode(0x5b, new SimpleReader("wordArrayDec", "w"));
-		_reader->registerOpcode(0x5c, new CondJumpReader("jumpIf", "o3"));
-		_reader->registerOpcode(0x5d, new SeqReader(new SimpleReader("not"), new CondJumpReader("jumpIf", "o3")));
+		_reader->registerOpcode(0x5c, new SeqReader(new SimpleReader("not"), new CondJumpReader("jumpIfNot", "o3")));
+		_reader->registerOpcode(0x5d, new CondJumpReader("jumpIfNot", "o3"));
 		_reader->registerOpcode(0x5e, new SimpleReader("startScript"));
 		_reader->registerOpcode(0x5f, new SimpleReader("startScriptQuick"));
 
@@ -314,12 +314,12 @@
 		srVerbs->registerOpcode(142, new SimpleReader("saveRestoreVerbs.restoreVerbs"));
 		srVerbs->registerOpcode(143, new SimpleReader("saveRestoreVerbs.deleteVerbs"));
 
-		wait->registerOpcode(168, new SeqReader(new SimpleReader("wait.forActor.pushCond"),	new CondJumpReader("jumpIf", "o4")));
+		wait->registerOpcode(168, new SeqReader(new SimpleReader("wait.forActor.pushCondNeg"),	new CondJumpReader("jumpIfNot", "o4")));
 		wait->registerOpcode(169, new SimpleReader("wait.forMessage"));
 		wait->registerOpcode(170, new SimpleReader("wait.forCamera"));
 		wait->registerOpcode(171, new SimpleReader("wait.forSentence"));
-		wait->registerOpcode(226, new SeqReader(new SimpleReader("wait.forAnimation.pushCond"),	new CondJumpReader("jumpIf", "o4")));
-		wait->registerOpcode(232, new SeqReader(new SimpleReader("wait.forTurn.pushCond"),	new CondJumpReader("jumpIf", "o4")));
+		wait->registerOpcode(226, new SeqReader(new SimpleReader("wait.forAnimation.pushCondNeg"),	new CondJumpReader("jumpIfNot", "o4")));
+		wait->registerOpcode(232, new SeqReader(new SimpleReader("wait.forTurn.pushCondNeg"),	new CondJumpReader("jumpIfNot", "o4")));
 
 		system->registerOpcode(158, new SimpleReader("system.restart"));
 		system->registerOpcode(159, new SimpleReader("system.pause"));


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