[Scummvm-cvs-logs] SF.net SVN: scummvm:[41329] tools/branches/gsoc2009-decompiler
kjdf at users.sourceforge.net
kjdf at users.sourceforge.net
Sun Jun 7 13:15:58 CEST 2009
Revision: 41329
http://scummvm.svn.sourceforge.net/scummvm/?rev=41329&view=rev
Author: kjdf
Date: 2009-06-07 11:15:58 +0000 (Sun, 07 Jun 2009)
Log Message:
-----------
decompiler: initial commit
Added Paths:
-----------
tools/branches/gsoc2009-decompiler/decompiler/
tools/branches/gsoc2009-decompiler/decompiler/Makefile
tools/branches/gsoc2009-decompiler/decompiler/decompiler.cc
tools/branches/gsoc2009-decompiler/decompiler/instruction.h
tools/branches/gsoc2009-decompiler/decompiler/misc.h
tools/branches/gsoc2009-decompiler/decompiler/parser.h
Added: tools/branches/gsoc2009-decompiler/decompiler/Makefile
===================================================================
--- tools/branches/gsoc2009-decompiler/decompiler/Makefile (rev 0)
+++ tools/branches/gsoc2009-decompiler/decompiler/Makefile 2009-06-07 11:15:58 UTC (rev 41329)
@@ -0,0 +1,15 @@
+.PHONY: clean
+
+DEPS = $(patsubst %.cc,%.o,$(*.cc))
+
+#%.o: %.cc
+# g++ -Wall -g -c $< -o $@
+
+decompiler: decompiler.o $(DEPS)
+ g++ -Wall -g $^ -o $@
+
+decompiler.o: decompiler.cc misc.h instruction.h parser.h
+ g++ -Wall -g -c decompiler.cc -o decompiler.o
+
+clean:
+ rm -rf decompiler *.o
Property changes on: tools/branches/gsoc2009-decompiler/decompiler/Makefile
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:eol-style
+ native
Added: tools/branches/gsoc2009-decompiler/decompiler/decompiler.cc
===================================================================
--- tools/branches/gsoc2009-decompiler/decompiler/decompiler.cc (rev 0)
+++ tools/branches/gsoc2009-decompiler/decompiler/decompiler.cc 2009-06-07 11:15:58 UTC (rev 41329)
@@ -0,0 +1,15 @@
+#include <iostream>
+#include <vector>
+
+using namespace std;
+
+#include "parser.h"
+#include "instruction.h"
+
+
+int main(int argc, char **argv) {
+ vector<Instruction*> v = Scumm6Parser().parseFile(argv[1]);
+ for (unsigned i = 0; i < v.size(); i++)
+ cout << v[i]->_addr << ": " << v[i]->_description << endl;
+ return 0;
+}
Property changes on: tools/branches/gsoc2009-decompiler/decompiler/decompiler.cc
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:eol-style
+ native
Added: tools/branches/gsoc2009-decompiler/decompiler/instruction.h
===================================================================
--- tools/branches/gsoc2009-decompiler/decompiler/instruction.h (rev 0)
+++ tools/branches/gsoc2009-decompiler/decompiler/instruction.h 2009-06-07 11:15:58 UTC (rev 41329)
@@ -0,0 +1,14 @@
+#ifndef INSTRUCTION_H
+#define INSTRUCTION_H
+
+#include <string>
+
+#include "misc.h"
+
+struct Instruction {
+ string _description;
+ uint32 _addr;
+ Instruction(string description, uint32 addr) : _description(description), _addr(addr) {}
+};
+
+#endif
Property changes on: tools/branches/gsoc2009-decompiler/decompiler/instruction.h
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:eol-style
+ native
Added: tools/branches/gsoc2009-decompiler/decompiler/misc.h
===================================================================
--- tools/branches/gsoc2009-decompiler/decompiler/misc.h (rev 0)
+++ tools/branches/gsoc2009-decompiler/decompiler/misc.h 2009-06-07 11:15:58 UTC (rev 41329)
@@ -0,0 +1,37 @@
+#ifndef MISC_H
+#define MISC_H
+
+#include <fstream>
+
+using namespace std;
+
+typedef unsigned char uint8;
+typedef unsigned short uint16;
+typedef unsigned uint32;
+
+uint32 read_be_uint32(ifstream &f) {
+ uint32 ret = 0;
+ ret |= f.get() << 24;
+ ret |= f.get() << 16;
+ ret |= f.get() << 8;
+ ret |= f.get();
+ return ret;
+}
+
+uint32 read_le_uint32(ifstream &f) {
+ uint32 ret = 0;
+ ret |= f.get();
+ ret |= f.get() << 8;
+ ret |= f.get() << 16;
+ ret |= f.get() << 24;
+ return ret;
+}
+
+uint16 read_le_uint16(ifstream &f) {
+ uint16 ret = 0;
+ ret |= f.get();
+ ret |= f.get() << 8;
+ return ret;
+}
+
+#endif
Property changes on: tools/branches/gsoc2009-decompiler/decompiler/misc.h
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:eol-style
+ native
Added: tools/branches/gsoc2009-decompiler/decompiler/parser.h
===================================================================
--- tools/branches/gsoc2009-decompiler/decompiler/parser.h (rev 0)
+++ tools/branches/gsoc2009-decompiler/decompiler/parser.h 2009-06-07 11:15:58 UTC (rev 41329)
@@ -0,0 +1,129 @@
+#ifndef READER_H
+#define READER_H
+
+
+#include <fstream>
+#include <vector>
+
+#include <cassert>
+#include <cstring>
+#include <cstdio>
+
+using namespace std;
+
+
+#include "misc.h"
+#include "instruction.h"
+
+
+using namespace std;
+
+
+struct Reader {
+ virtual void readInstruction(ifstream &f, vector<Instruction*> &v, uint32 addr) = 0;
+};
+
+
+struct SimpleReader : public Reader {
+
+ string _description;
+ int _skip;
+
+ SimpleReader(string description, int skip=0) : _description(description), _skip(skip) {};
+
+ virtual void readInstruction(ifstream &f, vector<Instruction*> &v, uint32 addr) {
+ for (int i = 0; i < _skip; i++) {
+ char c = f.get();
+ printf("SKIPPED: 0x%x\n", (unsigned int) c);
+ }
+ v.push_back(new Instruction(_description, addr));
+ }
+};
+
+
+struct SubopcodeReader : public Reader {
+
+ Reader *_dispatchTable[256];
+
+ SubopcodeReader() {
+ memset(_dispatchTable, 0, sizeof(_dispatchTable));
+ }
+
+ void registerOpcode(uint8 opcode, Reader *reader) {
+ _dispatchTable[opcode] = reader;
+ }
+
+ void readInstruction(ifstream& f, vector<Instruction*> &v, uint32 addr) {
+ uint8 opcode = f.get();
+ Reader* reader = _dispatchTable[opcode];
+ assert(reader);
+ reader->readInstruction(f, v, addr);
+ }
+};
+
+
+struct Scumm6Parser {
+
+ Reader *_dispatchTable[256];
+
+ Scumm6Parser() {
+ memset(_dispatchTable, 0, sizeof(_dispatchTable));
+ _dispatchTable[0] = new SimpleReader("zero", 0);
+ _dispatchTable[1] = new SimpleReader("one", 1);
+ _dispatchTable[2] = new SimpleReader("two", 2);
+ SubopcodeReader *three = new SubopcodeReader();
+ three->registerOpcode(1, new SimpleReader("three/1", 1));
+ three->registerOpcode(2, new SimpleReader("three/2", 2));
+ _dispatchTable[3] = three;
+ _dispatchTable[4] = new SimpleReader("four", 4);
+ }
+
+ void parseHeader(ifstream &f) {
+ switch (read_be_uint32(f)) {
+ case 'LSC2':
+ read_le_uint32(f);
+ read_le_uint32(f); // script number
+ break;
+ case 'LSCR':
+ read_le_uint32(f);
+ f.get(); // script number
+ break;
+ case 'SCRP':
+ case 'ENCD':
+ case 'EXCD':
+ read_le_uint32(f);
+ break;
+ case 'VERB':
+ read_le_uint32(f);
+ uint16 minOffset = 65535;
+ for (uint8 code = f.get(); code != 0; code = f.get()) {
+ uint16 offset = read_le_uint16(f);
+ printf("%2X - %.4X\n", code, offset);
+ if (offset < minOffset)
+ minOffset = offset;
+ }
+ f.seekg(minOffset);
+ break;
+ }
+ }
+
+ vector<Instruction*> parseFile(const char *filename) {
+ vector<Instruction*> v;
+ ifstream f;
+ f.open(filename, ios::binary);
+ parseHeader(f);
+ while (true) {
+ uint8 addr = f.tellg();
+ uint8 opcode = f.get();
+ if (f.eof())
+ return v;
+ Reader* reader = _dispatchTable[opcode];
+ assert(reader);
+ reader->readInstruction(f, v, addr);
+ }
+ }
+
+};
+
+
+#endif
Property changes on: tools/branches/gsoc2009-decompiler/decompiler/parser.h
___________________________________________________________________
Added: svn:mime-type
+ text/plain
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