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

pidgeot at users.sourceforge.net pidgeot at users.sourceforge.net
Tue Jul 20 09:44:24 CEST 2010


Revision: 51052
          http://scummvm.svn.sourceforge.net/scummvm/?rev=51052&view=rev
Author:   pidgeot
Date:     2010-07-20 07:44:23 +0000 (Tue, 20 Jul 2010)

Log Message:
-----------
Add custom stack implementation

This avoids the issue of std::stack::pop not returning the element it
pops - you have to call top to get that element, and then pop it
afterwards. This will then simplify the code and avoids the risk of
forgetting to do the pop separately.

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

Added Paths:
-----------
    tools/branches/gsoc2010-decompiler/decompiler/stack.h

Modified: tools/branches/gsoc2010-decompiler/decompiler/codegen.cpp
===================================================================
--- tools/branches/gsoc2010-decompiler/decompiler/codegen.cpp	2010-07-20 07:11:17 UTC (rev 51051)
+++ tools/branches/gsoc2010-decompiler/decompiler/codegen.cpp	2010-07-20 07:44:23 UTC (rev 51052)
@@ -55,7 +55,7 @@
 	_indentLevel = 0;
 }
 
-typedef std::pair<GraphVertex, Stack> DFSEntry;
+typedef std::pair<GraphVertex, EntryStack> DFSEntry;
 
 void CodeGenerator::generate(const Graph &g) {
 	_g = g;
@@ -71,14 +71,13 @@
 	entryPoint = p->_vertex;
 
 	// DFS from entry point to process each vertex
-	std::stack<DFSEntry> dfsStack;
+	Stack<DFSEntry> dfsStack;
 	std::set<GraphVertex> seen;
-	dfsStack.push(DFSEntry(entryPoint, Stack()));
+	dfsStack.push(DFSEntry(entryPoint, EntryStack()));
 	seen.insert(entryPoint);
 	while (!dfsStack.empty()) {
-		DFSEntry e = dfsStack.top();
+		DFSEntry e = dfsStack.pop();
 		GroupPtr tmp = GET(e.first);
-		dfsStack.pop();
 		_stack = e.second;
 		GraphVertex v = e.first;
 		process(v);
@@ -121,10 +120,9 @@
 		case kDup:
 		{
 			std::stringstream s;
-			EntryPtr p = _stack.top()->dup(s);
+			EntryPtr p = _stack.pop()->dup(s);
 			if (s.str().length() > 0)
 				addOutputLine(s.str());
-			_stack.pop();
 			_stack.push(p);
 			_stack.push(p);
 			break;

Modified: tools/branches/gsoc2010-decompiler/decompiler/codegen.h
===================================================================
--- tools/branches/gsoc2010-decompiler/decompiler/codegen.h	2010-07-20 07:11:17 UTC (rev 51051)
+++ tools/branches/gsoc2010-decompiler/decompiler/codegen.h	2010-07-20 07:44:23 UTC (rev 51052)
@@ -21,9 +21,9 @@
  */
 
 #include "graph.h"
+#include "stack.h"
 
 #include <ostream>
-#include <stack>
 #include <utility>
 
 #include <boost/intrusive_ptr.hpp>
@@ -198,7 +198,7 @@
 	 * @param rhs Stack entry representing the right side of the operator.
 	 * @param op The operator for this entry.
 	 */
-	BinaryOpEntry(EntryPtr lhs, EntryPtr rhs, std::string op) : 
+	BinaryOpEntry(EntryPtr lhs, EntryPtr rhs, std::string op) :
 		StackEntry(seBinOp), _lhs(lhs), _rhs(rhs), _op(op) {
 	}
 
@@ -253,7 +253,7 @@
 /**
  * Type representing a stack.
  */
-typedef std::stack<EntryPtr> Stack;
+typedef Stack<EntryPtr> EntryStack;
 
 const int kIndentAmount = 2; ///< How many spaces to use for each indent.
 
@@ -274,7 +274,7 @@
 
 protected:
 	std::ostream &_output; ///< The std::ostream to output the code to.
-	Stack _stack;          ///< The stack currently being processed.
+	EntryStack _stack;     ///< The stack currently being processed.
 	uint _indentLevel;     ///< Indentation level.
 	GroupPtr _curGroup;    ///< Pointer to the group currently being processed.
 

Modified: tools/branches/gsoc2010-decompiler/decompiler/scummv6/codegen.cpp
===================================================================
--- tools/branches/gsoc2010-decompiler/decompiler/scummv6/codegen.cpp	2010-07-20 07:11:17 UTC (rev 51051)
+++ tools/branches/gsoc2010-decompiler/decompiler/scummv6/codegen.cpp	2010-07-20 07:44:23 UTC (rev 51052)
@@ -60,8 +60,7 @@
 			case 0x43:
 			{
 				EntryPtr p = new VarEntry(decodeVarName(inst._params[0].getUnsigned()));
-				writeAssignment(p, _stack.top());
-				_stack.pop();
+				writeAssignment(p, _stack.pop());
 				break;
 			}
 			case 0x46:

Added: tools/branches/gsoc2010-decompiler/decompiler/stack.h
===================================================================
--- tools/branches/gsoc2010-decompiler/decompiler/stack.h	                        (rev 0)
+++ tools/branches/gsoc2010-decompiler/decompiler/stack.h	2010-07-20 07:44:23 UTC (rev 51052)
@@ -0,0 +1,70 @@
+/* ScummVM Tools
+ * Copyright (C) 2010 The ScummVM project
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL$
+ * $Id$
+ *
+ */
+
+#ifndef DEC_STACK_H
+#define DEC_STACK_H
+
+#include <deque>
+
+/**
+ * Stack class based on a deque.
+ */
+template<typename T>
+class Stack {
+private:
+	std::deque<T> _stack; ///< Container used for the stack.
+public:
+
+	/**
+	 * Returns whether or not the stack is empty.
+	 *
+	 * @return true if the stack is empty, false if it is not.
+	 */
+	bool empty() const { return _stack.empty(); }
+
+	/**
+	 * Push an item onto the stack.
+	 *
+	 * @param item The item to push.
+	 */
+	void push(T item) { _stack.push_back(item); }
+
+	/**
+	 * Pop an item from the stack and return it.
+	 *
+	 * @return The value popped from the stack.
+	 */
+	T pop() {
+		T retval = _stack.back();
+		_stack.pop_back();
+		return retval;
+	}
+
+	/**
+	 * Return the topmost item on the stack without removing it.
+	 *
+	 * @return The topmost item on the stack.
+	 */
+	T peek() { return _stack.back(); }
+};
+
+#endif


Property changes on: tools/branches/gsoc2010-decompiler/decompiler/stack.h
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:keywords
   + Date Rev Author URL Id
Added: svn:eol-style
   + native


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