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

pidgeot at users.sourceforge.net pidgeot at users.sourceforge.net
Sat Jul 24 01:03:40 CEST 2010


Revision: 51228
          http://scummvm.svn.sourceforge.net/scummvm/?rev=51228&view=rev
Author:   pidgeot
Date:     2010-07-23 23:03:40 +0000 (Fri, 23 Jul 2010)

Log Message:
-----------
Add indenting to code generator

Slightly hackish, but works as a simple, temporary solution

Modified Paths:
--------------
    tools/branches/gsoc2010-decompiler/decompiler/codegen.cpp
    tools/branches/gsoc2010-decompiler/decompiler/codegen.h
    tools/branches/gsoc2010-decompiler/decompiler/graph.h

Modified: tools/branches/gsoc2010-decompiler/decompiler/codegen.cpp
===================================================================
--- tools/branches/gsoc2010-decompiler/decompiler/codegen.cpp	2010-07-23 21:27:28 UTC (rev 51227)
+++ tools/branches/gsoc2010-decompiler/decompiler/codegen.cpp	2010-07-23 23:03:40 UTC (rev 51228)
@@ -93,18 +93,19 @@
 	// TODO: Proper indenting
 	p = GET(entryPoint);
 	while (p != NULL) {
-		for (std::vector<std::string>::iterator it = p->_code.begin(); it != p->_code.end(); ++it)
-			_output << *it << std::endl;
+		for (std::vector<CodeLine>::iterator it = p->_code.begin(); it != p->_code.end(); ++it) {
+			if (it->_unindentBefore)
+				_indentLevel--;
+			_output << boost::format("%08X: %s") % p->_start->_address % indentString(it->_line) << std::endl;
+			if (it->_indentAfter)
+				_indentLevel++;
+		}
 		p = p->_next;
 	}
 }
 
-void CodeGenerator::addOutputLine(std::string s) {
-	std::stringstream stream;
-	stream << boost::format("%08X: ") % _curGroup->_start->_address;
-//	stream << indentString(s);
-	stream << s;
-	_curGroup->_code.push_back(stream.str());
+void CodeGenerator::addOutputLine(std::string s, bool unindentBefore, bool indentAfter) {
+	_curGroup->_code.push_back(CodeLine(s, unindentBefore, indentAfter));
 }
 
 void CodeGenerator::writeAssignment(EntryPtr dst, EntryPtr src) {
@@ -118,7 +119,7 @@
 
 	// Check if we should add else start
 	if (_curGroup->_startElse && _curGroup->_type != kIfCond)
-		addOutputLine("} else {");
+		addOutputLine("} else {", true, true);
 
 	// Check ingoing edges to see if we want to add any extra output
 	InEdgeRange ier = boost::in_edges(v, _g);
@@ -129,14 +130,14 @@
 			continue;
 		switch (inGroup->_type) {
 		case kDoWhileCond:
-			addOutputLine("do {");
+			addOutputLine("do {", false, true);
 			break;
 		case kIfCond:
 			if (!_curGroup->_startElse)
-				addOutputLine("}");
+				addOutputLine("}", true, false);
 			break;
 		case kWhileCond:
-			addOutputLine("}");
+			addOutputLine("}", true, false);
 			break;
 		default:
 			break;
@@ -188,18 +189,19 @@
 							s << "} else ";
 						}
 						s << "if (" << _stack.pop() << ") {";
+						addOutputLine(s.str(), _curGroup->_startElse, true);
 						break;
 					case kWhileCond:
 						s << "while (" << _stack.pop() << ") {";
+						addOutputLine(s.str(), false, true);
 						break;
 					case kDoWhileCond:
 						s << "} while (" << _stack.pop() << ")";
+						addOutputLine(s.str(), true, false);
 						break;
 					default:
-						processInst(*it);
 						break;
 					}
-					addOutputLine(s.str());
 				}
 				break;
 			case kJump:
@@ -268,7 +270,7 @@
 
 	// Add else end if necessary
 	if (_curGroup->_endElse != NULL && (_curGroup->_endElse->_type != kIfCond || !_curGroup->_endElse->_startElse))
-		addOutputLine("}");
+		addOutputLine("}", true, false);
 }
 
 void CodeGenerator::addArg(EntryPtr p) {

Modified: tools/branches/gsoc2010-decompiler/decompiler/codegen.h
===================================================================
--- tools/branches/gsoc2010-decompiler/decompiler/codegen.h	2010-07-23 21:27:28 UTC (rev 51227)
+++ tools/branches/gsoc2010-decompiler/decompiler/codegen.h	2010-07-23 23:03:40 UTC (rev 51228)
@@ -418,8 +418,10 @@
 	 * Adds a line of code to the current group.
 	 *
 	 * @param s The line to add.
+	 * @param unindentBefore Whether or not to remove an indentation level before the line. Defaults to false.
+	 * @param indentAfter Whether or not to add an indentation level after the line. Defaults to false.
 	 */
-	void addOutputLine(std::string s);
+	void addOutputLine(std::string s, bool unindentBefore = false, bool indentAfter = false);
 
 	/**
 	 * Generate an assignment statement.

Modified: tools/branches/gsoc2010-decompiler/decompiler/graph.h
===================================================================
--- tools/branches/gsoc2010-decompiler/decompiler/graph.h	2010-07-23 21:27:28 UTC (rev 51227)
+++ tools/branches/gsoc2010-decompiler/decompiler/graph.h	2010-07-23 23:03:40 UTC (rev 51228)
@@ -196,6 +196,28 @@
 } // End of namespace boost
 
 /**
+ * Structure representing a line of code.
+ */
+struct CodeLine {
+	std::string _line;    ///< The line of code.
+	bool _unindentBefore; ///< Whether or not to add an indentation level before outputting the line.
+	bool _indentAfter;    ///< Whether or not to remove an indentation level after outputting the line.
+
+	/**
+	 * Constructor for CodeLine.
+	 *
+	 * @param s The line of code.
+	 * @param unindentBefore Whether or not to remove an indentation level before the line. Defaults to false.
+	 * @param indentAfter Whether or not to add an indentation level after the line. Defaults to false.
+	 */
+	CodeLine(const std::string& line, bool unindentBefore, bool indentAfter) {
+		_line = line;
+		_unindentBefore = unindentBefore;
+		_indentAfter = indentAfter;
+	}
+};
+
+/**
  * Structure representing a group of instructions.
  */
 struct Group {
@@ -205,16 +227,16 @@
   friend void ::boost::intrusive_ptr_release(Group *p); ///< Allow access by reference counting methods in boost namespace.
 
 public:
-	GraphVertex _vertex;            ///< Vertex the group belongs to.
-	ConstInstIterator _start;       ///< First instruction in the group.
-	ConstInstIterator _end;         ///< Last instruction in the group.
-	int _stackLevel;                ///< Level of the stack upon entry.
-	GroupType _type;                ///< Type of the group.
-	bool _startElse;                ///< Group is start of an else block.
-	Group *_endElse;                ///< Group is end of an else block.
-	Group *_prev;                   ///< Pointer to the previous group, when ordered by address. Used for short-circuit analysis.
-	Group *_next;                   ///< Pointer to the next group, when ordered by address.
-	std::vector<std::string> _code; ///< Container for decompiled lines of code.
+	GraphVertex _vertex;         ///< Vertex the group belongs to.
+	ConstInstIterator _start;    ///< First instruction in the group.
+	ConstInstIterator _end;      ///< Last instruction in the group.
+	int _stackLevel;             ///< Level of the stack upon entry.
+	GroupType _type;             ///< Type of the group.
+	bool _startElse;             ///< Group is start of an else block.
+	Group *_endElse;             ///< Group is end of an else block.
+	Group *_prev;                ///< Pointer to the previous group, when ordered by address. Used for short-circuit analysis.
+	Group *_next;                ///< Pointer to the next group, when ordered by address.
+	std::vector<CodeLine> _code; ///< Container for decompiled lines of code.
 
 	/**
 	 * Parameterless constructor for Group. Required for use with STL and Boost, should not be called manually.


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