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

kjdf at users.sourceforge.net kjdf at users.sourceforge.net
Wed Jun 17 23:19:24 CEST 2009


Revision: 41613
          http://scummvm.svn.sourceforge.net/scummvm/?rev=41613&view=rev
Author:   kjdf
Date:     2009-06-17 21:19:23 +0000 (Wed, 17 Jun 2009)

Log Message:
-----------
decompiler: cleaned up graph library

Modified Paths:
--------------
    tools/branches/gsoc2009-decompiler/decompiler/decompiler.cc
    tools/branches/gsoc2009-decompiler/decompiler/misc.h
    tools/branches/gsoc2009-decompiler/decompiler/test.cc

Added Paths:
-----------
    tools/branches/gsoc2009-decompiler/decompiler/graph.h

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

Modified: tools/branches/gsoc2009-decompiler/decompiler/decompiler.cc
===================================================================
--- tools/branches/gsoc2009-decompiler/decompiler/decompiler.cc	2009-06-17 21:07:59 UTC (rev 41612)
+++ tools/branches/gsoc2009-decompiler/decompiler/decompiler.cc	2009-06-17 21:19:23 UTC (rev 41613)
@@ -35,12 +35,12 @@
 	if (g_disasm)
 		for (index_t i = 0; i < script.size(); i++)
 			script.print(i);
-	CFG *cfg = new CFG(script);
+	CFG cfg(script);
 	if (g_blocks)
-		cfg->printBasicBlocks();
-	cfg->removeJumpsToJumps();
-	cfg->removeDeadBlocks();
+		cfg.printBasicBlocks();
+	cfg.removeJumpsToJumps();
+	cfg.removeDeadBlocks();
 	if (g_graph)
-		cfg->printDot();
+		cfg.printDot();
 	return 0;
 }

Added: tools/branches/gsoc2009-decompiler/decompiler/graph.h
===================================================================
--- tools/branches/gsoc2009-decompiler/decompiler/graph.h	                        (rev 0)
+++ tools/branches/gsoc2009-decompiler/decompiler/graph.h	2009-06-17 21:19:23 UTC (rev 41613)
@@ -0,0 +1,93 @@
+#ifndef GRAPH_H
+#define GRAPH_H
+
+#include <list>
+#include <sstream>
+
+#include <boost/foreach.hpp>
+#ifndef foreach
+#define foreach BOOST_FOREACH
+#endif
+
+
+template<typename Data>
+struct Graph : boost::noncopyable {
+
+	struct Node : boost::noncopyable {
+
+		Data _data;
+
+		const std::list<Node*> &out() const {
+			return _out;
+		}
+
+	private:
+
+		friend class Graph;
+
+		std::list<Node*> _in;
+		std::list<Node*> _out;
+
+		Node(const Data &data) : _data(data) {
+		}
+
+		~Node() {
+		}
+	};
+
+	std::list<Node*> _nodes;
+
+	Graph() {
+	}
+
+	~Graph() {
+		foreach (Node *u, _nodes)
+			delete u;
+	}
+
+	Node *addNode(const Data &data) {
+		Node* node = new Node(data);
+		_nodes.push_back(node);
+		return node;
+	}
+
+	void addEdge(Node *from, Node *to) {
+		from->_out.push_back(to);
+		to->_in.push_back(from);
+	}
+
+	template<typename Printer>   // printer is a functor taking Data and returning a string
+	std::string graphvizPrint(Printer printer, const std::string &fontname="Courier", int fontsize=14) const {
+		std::stringstream ret;
+		ret << "digraph G {" << std::endl;
+		foreach (Node *u, _nodes) {
+			ret << '"' << u << '"'
+				<< "[fontname=" << '"' << fontname << '"'
+				<< ",fontsize=" << fontsize
+				<< ",shape=box"
+				<< ",label=" << '"' << graphvizEscapeLabel(printer(u->_data)) << '"'
+				<< "];" << std::endl;
+			foreach (Node *v, u->_out)
+				ret << '"' << u << '"'
+					<< " -> "
+					<< '"' << v << '"'
+					<< ";" << std::endl;
+		}
+		ret << "}" << std::endl;
+		return ret.str();
+	}
+
+private:
+
+	static std::string graphvizEscapeLabel(const std::string &s) {
+		std::string ret;
+		foreach (char c, s) {
+			if (c == '\n' || c == '"' || c == '\\')
+				ret.push_back('\\');
+			ret.push_back(c == '\n' ? 'l' : c);   // align lines to the left
+		}
+		return ret;
+	}
+};
+
+#endif


Property changes on: tools/branches/gsoc2009-decompiler/decompiler/graph.h
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:eol-style
   + native

Deleted: tools/branches/gsoc2009-decompiler/decompiler/graph2.h
===================================================================
--- tools/branches/gsoc2009-decompiler/decompiler/graph2.h	2009-06-17 21:07:59 UTC (rev 41612)
+++ tools/branches/gsoc2009-decompiler/decompiler/graph2.h	2009-06-17 21:19:23 UTC (rev 41613)
@@ -1,99 +0,0 @@
-#ifndef GRAPH_H
-#define GRAPH_H
-
-typedef unsigned id_t;
-
-#include <map>
-#include <list>
-#include <string>
-#include <sstream>
-
-#include "misc.h"
-
-using namespace std;
-
-
-string escape(const string &s) {
-	string ret;
-	foreach (char c, s) {
-		if (c == '\\' || c == '"')
-			ret.push_back('\\');
-		ret.push_back(c);
-	}
-	return ret;
-};
-
-
-template<typename Data>
-struct Graph {
-
-
-	struct NodeInfo {
-		list<NodeInfo*> _in;
-		list<NodeInfo*> _out;
-		Data _data;
-		NodeInfo(Data data) : _data(data) {
-		}
-	};
-
-
-	struct Node {
-		Graph *_graph;
-		NodeInfo* _info;
-
-		Node(Graph *graph, NodeInfo *info): _graph(graph), _info(info) {
-		}
-
-		list<Node> out() {
-			list<Node> ret;
-			foreach (NodeInfo *v, _info->_out)
-				ret.push_back(Node(_graph, v));
-			return ret;
-		}
-
-		void addEdge(Node v) {
-			_graph->addEdge(*this, v);
-		}
-
-		Data &operator*() {
-			return _info->_data;
-		}
-
-		Data *operator->() {
-			return &_info->_data;
-		}
-	};
-
-	list<NodeInfo*> _nodes;
-
-	Graph() {
-	}
-
-	Node addNode(Data data) {
-		NodeInfo* info = new NodeInfo(data);
-		_nodes.push_back(info);
-		return Node(this, info);
-	}
-
-	void addEdge(Node from, Node to) {
-		from._info->_out.push_back(to._info);
-		to._info->_in.push_back(from._info);
-	}
-
-	// printer :: Data -> string
-	template<typename Printer>
-	string print(Printer printer) {
-		stringstream ret;
-		ret << "digraph G {" << endl;
-		foreach (NodeInfo *u, _nodes) {
-			ret << (unsigned) u << " [shape=box,label=\"" << escape(printer(u->_data)) << "\"];" << endl;
-			foreach (NodeInfo *v, u->_out)
-				ret << (unsigned) u << " -> " << (unsigned) v << ";" << endl;
-		}
-		ret << "}" << endl;
-		return ret.str();
-	}
-
-};
-
-#endif

Modified: tools/branches/gsoc2009-decompiler/decompiler/misc.h
===================================================================
--- tools/branches/gsoc2009-decompiler/decompiler/misc.h	2009-06-17 21:07:59 UTC (rev 41612)
+++ tools/branches/gsoc2009-decompiler/decompiler/misc.h	2009-06-17 21:19:23 UTC (rev 41613)
@@ -4,8 +4,9 @@
 #include <fstream>
 
 #include <boost/foreach.hpp>
-
+#ifndef foreach
 #define foreach BOOST_FOREACH
+#endif
 
 using namespace std;
 

Modified: tools/branches/gsoc2009-decompiler/decompiler/test.cc
===================================================================
--- tools/branches/gsoc2009-decompiler/decompiler/test.cc	2009-06-17 21:07:59 UTC (rev 41612)
+++ tools/branches/gsoc2009-decompiler/decompiler/test.cc	2009-06-17 21:19:23 UTC (rev 41613)
@@ -1,30 +1,46 @@
 #include <cstdio>
 
-#include "graph2.h"
+#include "graph.h"
 
 #include <string>
 #include <sstream>
 #include <iostream>
+
+#include <functional>
+
 using namespace std;
 
-string printer(int i) {
-	stringstream ret;
-	ret << "node \\ containing \"" << i << "\"";
-	return ret.str();
+#include <boost/shared_ptr.hpp>
+
+
+struct BBlock {
+	int a, b;
+	virtual string print() {
+		return string("something");
+	}
 };
 
-typedef Graph<int>::Node node;
+struct BBlock2 : public BBlock {
+	int c;
+	virtual string print() {
+		return string("[0000] push(1);\n[0003] not();\n       if ()\n           goto 0;");
+	}
+};
 
+typedef boost::shared_ptr<BBlock> block_t;
+typedef Graph<block_t> graph_t;
+typedef graph_t::Node *node_t;
+
+string printer(block_t b) {
+	return b->print();
+};
+
 int main() {
-	Graph<int> g;
-	node a = g.addNode(0);
-	g.addEdge(a, g.addNode(1));
-	g.addEdge(a, g.addNode(2));
-	g.addEdge(a, g.addNode(0));
-	g.addEdge(a, g.addNode(0));
-	*a = 6;
-	foreach (node u, a.out()) 
-		*u = 12;
-	cout << g.print(printer) << endl;
+	graph_t g;
+	node_t a = g.addNode(block_t(new BBlock()));
+	g.addEdge(a, g.addNode(block_t(new BBlock2())));
+	cout << g.graphvizPrint(printer) << endl;
+	//	cout << "===" << endl;
+	//	cout << a->_data->print() << endl;
 	return 0;
 }


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