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

pidgeot at users.sourceforge.net pidgeot at users.sourceforge.net
Wed Jul 21 03:18:40 CEST 2010


Revision: 51081
          http://scummvm.svn.sourceforge.net/scummvm/?rev=51081&view=rev
Author:   pidgeot
Date:     2010-07-21 01:18:39 +0000 (Wed, 21 Jul 2010)

Log Message:
-----------
Add array handling to current instruction types

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

Modified: tools/branches/gsoc2010-decompiler/decompiler/codegen.h
===================================================================
--- tools/branches/gsoc2010-decompiler/decompiler/codegen.h	2010-07-21 01:01:22 UTC (rev 51080)
+++ tools/branches/gsoc2010-decompiler/decompiler/codegen.h	2010-07-21 01:18:39 UTC (rev 51081)
@@ -40,6 +40,7 @@
 const StackEntryType seBinOp = 2;
 const StackEntryType seUnaryOp = 3;
 const StackEntryType seDup = 4;
+const StackEntryType seArray = 5;
 
 class StackEntry;
 
@@ -251,6 +252,36 @@
 };
 
 /**
+ * Type representing index list for an array.
+ */
+typedef std::deque<EntryPtr> ArrayIdxType;
+
+/**
+ * Stack entry representing array access.
+ */
+class ArrayEntry : public StackEntry {
+private:
+	const std::string _arrayName; ///< The name of the array.
+	ArrayIdxType _idxs;  ///< std::deque of stack entries representing the indexes used (left-to-right).
+
+public:
+	/**
+	 * Constructor for ArrayEntry.
+	 *
+	 * @param arrayName The name of the array.
+	 * @param idxs std::deque of stack entries representing the indexes used (left-to-right).
+	 */
+	ArrayEntry(std::string arrayName, std::deque<EntryPtr> idxs) : StackEntry(seArray), _arrayName(arrayName), _idxs(idxs) { }
+
+	virtual std::ostream &print(std::ostream &output) const {
+		output << _arrayName;
+		for (ArrayIdxType::const_iterator i = _idxs.begin(); i != _idxs.end(); ++i)
+			output << "[" << *i << "]";
+		return output;
+	}
+};
+
+/**
  * Type representing a stack.
  */
 typedef Stack<EntryPtr> EntryStack;

Modified: tools/branches/gsoc2010-decompiler/decompiler/scummv6/codegen.cpp
===================================================================
--- tools/branches/gsoc2010-decompiler/decompiler/scummv6/codegen.cpp	2010-07-21 01:01:22 UTC (rev 51080)
+++ tools/branches/gsoc2010-decompiler/decompiler/scummv6/codegen.cpp	2010-07-21 01:18:39 UTC (rev 51081)
@@ -41,36 +41,54 @@
 			_stack.push(new VarEntry(decodeVarName(inst._params[0].getUnsigned())));
 			break;
 		case 0x06: // byteArrayRead
-			// TODO
-			break;
 		case 0x07: // wordArrayRead
-			// TODO
-			break;
+			{
+				ArrayIdxType idxs;
+				idxs.push_front(_stack.pop());
+				_stack.push(new ArrayEntry(decodeArrayName(inst._params[0].getUnsigned()), idxs));
+				break;
+			}
 		case 0x0A: // byteArrayIndexedRead
-			// TODO
-			break;
 		case 0x0B: // wordArrayIndexedRead
-			// TODO
-			break;
+			{
+				ArrayIdxType idxs;
+				idxs.push_front(_stack.pop());
+				idxs.push_front(_stack.pop());
+				_stack.push(new ArrayEntry(decodeArrayName(inst._params[0].getUnsigned()), idxs));
+				break;
+			}
 		}
 		break;
 	case kStore:
 		switch (inst._opcode) {
-			case 0x42:
-			case 0x43:
+			case 0x42: // writeByteVar
+			case 0x43: // writeWordVar
 			{
 				EntryPtr p = new VarEntry(decodeVarName(inst._params[0].getUnsigned()));
 				writeAssignment(p, _stack.pop());
 				break;
 			}
-			case 0x46:
+			case 0x46: // byteArrayWrite
+			case 0x47: // wordArrayWrite
+			{
+				EntryPtr value = _stack.pop();
+				ArrayIdxType idxs;
+				idxs.push_back(_stack.pop());
+				EntryPtr p = new ArrayEntry(decodeArrayName(inst._params[0].getUnsigned()), idxs);
+				writeAssignment(p, value);
 				break;
-			case 0x47:
+			}
+			case 0x4A: // byteArrayIndexedWrite
+			case 0x4B: // wordArrayIndexedWrite
+			{
+				EntryPtr value = _stack.pop();
+				ArrayIdxType idxs;
+				idxs.push_front(_stack.pop());
+				idxs.push_front(_stack.pop());
+				EntryPtr p = new ArrayEntry(decodeArrayName(inst._params[0].getUnsigned()), idxs);
+				writeAssignment(p, value);
 				break;
-			case 0x4A:
-				break;
-			case 0x4B:
-				break;
+			}
 		}
 		break;
 	case kBinaryOp:
@@ -114,7 +132,15 @@
 		case 0x53: // wordArrayInc
 		case 0x5A: // byteArrayDec
 		case 0x5B: // wordArrayDec
-			// TODO
+			{
+				std::stringstream s;
+				ArrayIdxType idxs;
+				idxs.push_front(_stack.pop());
+				EntryPtr p = new UnaryOpEntry(new ArrayEntry(decodeVarName(inst._params[0].getUnsigned()), idxs), inst._operator);
+				s << p;
+				addOutputLine(s.str());
+				break;
+			}
 			break;
 		}
 		break;
@@ -296,12 +322,19 @@
 	NULL,
 };
 
+const char *Scumm::v6::CodeGenerator::getVarName(uint16 varID) {
+	if (varID >= sizeof(var_names) / sizeof(var_names[0]))
+		return NULL;
+	return var_names[varID];
+}
+
 std::string Scumm::v6::CodeGenerator::decodeVarName(uint16 varID) {
 	std::stringstream s;
 	if (!(varID & 0xF000)) {
 		uint16 var = varID & 0xFFF;
-		if (var < (sizeof(var_names) / sizeof(var_names[0])) && var_names[var] != NULL)
-			return var_names[var];
+		const char* varName = getVarName(var);
+		if (varName	 != NULL)
+			return varName;
 		else
 			s << boost::format("var%d") % (varID & 0xFFF);
 	} else if (varID & 0x8000) {
@@ -313,3 +346,12 @@
 	}
 	return s.str();
 }
+
+std::string Scumm::v6::CodeGenerator::decodeArrayName(uint16 arrID) {
+	std::stringstream s;
+	const char* varName;
+	if (!(arrID & 0xF000) && (varName = getVarName(arrID & 0xFFF)) != NULL)
+		return varName;
+	s << boost::format("array%d") % arrID;
+	return s.str();
+}

Modified: tools/branches/gsoc2010-decompiler/decompiler/scummv6/codegen.h
===================================================================
--- tools/branches/gsoc2010-decompiler/decompiler/scummv6/codegen.h	2010-07-21 01:01:22 UTC (rev 51080)
+++ tools/branches/gsoc2010-decompiler/decompiler/scummv6/codegen.h	2010-07-21 01:18:39 UTC (rev 51081)
@@ -35,12 +35,28 @@
 class CodeGenerator : public ::CodeGenerator {
 private:
 	/**
+	 * Get the name associated with a variable ID.
+	 *
+	 * @param varID The ID to get the name for.
+	 * @return Pointer to char array containing the name of the variable, or NULL if no name exists.
+	 */
+	const char *getVarName(uint16 varID);
+
+	/**
 	 * Decode a variable ID to a name.
 	 *
 	 * @param varID The ID to decode.
 	 * @return The decoded variable name.
 	 */
 	std::string decodeVarName(uint16 varID);
+
+	/**
+	 * Decode an array ID to a name.
+	 *
+	 * @param arrID The ID to decode.
+	 * @return The decoded array name.
+	 */
+	std::string decodeArrayName(uint16 arrID);
 public:
 	CodeGenerator(Engine *engine, std::ostream &output) : ::CodeGenerator(engine, output) {}
 protected:


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