From bbae30aef31f9f9be8bf2f975227bfe3900715ea Mon Sep 17 00:00:00 2001 From: Michael Madsen Date: Tue, 13 Jul 2010 22:52:10 +0000 Subject: [PATCH] Add some metadata to StackEntry to easily tell the various types apart Add functionality to create a duplicate entry from any entry Add CodeGenerator creation to Engine Prepare a SCUMMv6 code generator class svn-id: r50855 --- Makefile.common | 3 +- decompiler/codegen.cpp | 19 +++++++-- decompiler/codegen.h | 74 +++++++++++++++++++++++++++++----- decompiler/engine.h | 12 +++++- decompiler/scummv6/codegen.cpp | 27 +++++++++++++ decompiler/scummv6/codegen.h | 46 +++++++++++++++++++++ decompiler/scummv6/engine.cpp | 5 +++ decompiler/scummv6/engine.h | 1 + decompiler/test/module.mk | 2 + 9 files changed, 172 insertions(+), 17 deletions(-) create mode 100644 decompiler/scummv6/codegen.cpp create mode 100644 decompiler/scummv6/codegen.h diff --git a/Makefile.common b/Makefile.common index 5a633a4b..575612cb 100644 --- a/Makefile.common +++ b/Makefile.common @@ -277,7 +277,8 @@ decompile_OBJS := \ decompiler/control_flow.o \ decompiler/codegen.o \ decompiler/scummv6/disassembler.o \ - decompiler/scummv6/engine.o + decompiler/scummv6/engine.o \ + decompiler/scummv6/codegen.o decompile_LIBS := \ -lboost_program_options diff --git a/decompiler/codegen.cpp b/decompiler/codegen.cpp index 8ffb12ee..c6f05f7a 100644 --- a/decompiler/codegen.cpp +++ b/decompiler/codegen.cpp @@ -24,12 +24,23 @@ #include -CodeGenerator::CodeGenerator(Engine *engine, const Graph &g) { - _engine = engine; - _g = g; +static int dupindex = 0; + +StackEntry *StackEntry::dup(std::ostream &output) { + if (_type == seDup) + return new DupEntry(*(DupEntry *)this); + + StackEntry* dupEntry = new DupEntry(++dupindex); + output << this << " = " << dupEntry; + return dupEntry; } -void CodeGenerator::generate() { +CodeGenerator::CodeGenerator(Engine *engine, std::ostream &output) : _output(output) { + _engine = engine; +} + +void CodeGenerator::generate(const Graph &g) { + _g = g; // TODO } diff --git a/decompiler/codegen.h b/decompiler/codegen.h index d70918eb..f45aa561 100644 --- a/decompiler/codegen.h +++ b/decompiler/codegen.h @@ -20,7 +20,6 @@ * */ -#include "engine.h" #include "graph.h" #include @@ -29,11 +28,23 @@ #ifndef DEC_CODEGEN_H #define DEC_CODEGEN_H +class Engine; + +typedef int StackEntryType; + +const StackEntryType seInt = 0; +const StackEntryType seVar = 1; +const StackEntryType seBinOp = 2; +const StackEntryType seUnaryOp = 3; +const StackEntryType seDup = 4; + /** * Base class for stack entries. */ class StackEntry { public: + StackEntryType _type; + virtual ~StackEntry() {} /** @@ -43,6 +54,14 @@ public: * @return The std::ostream used for output. */ virtual std::ostream &print(std::ostream &output) const = 0; + + /** + * Duplicates a stack entry. + * + * @param output The std::ostream to output to. + * @return A StackEntry corresponding to a duplicate of this entry. + */ + virtual StackEntry *dup(std::ostream &output); /** * Output a stack entry to an std::ostream. @@ -71,7 +90,9 @@ public: * @param val The value contained in the stack entry. * @param isSigned Whether or not the value is signed. This will affect output. */ - IntEntry(int32 val, bool isSigned) : _val(val), _isSigned(isSigned) {} + IntEntry(int32 val, bool isSigned) : _val(val), _isSigned(isSigned) { + _type = seInt; + } /** * Constructor for IntEntry. @@ -81,6 +102,7 @@ public: */ IntEntry(uint32 val, bool isSigned) : _isSigned(isSigned) { _val = (int32)val; + _type = seInt; } virtual std::ostream &print(std::ostream &output) const { @@ -89,7 +111,9 @@ public: else output << (uint32)_val; return output; - }; + } + + virtual StackEntry *dup() { return new IntEntry(_val, _isSigned); } }; /** @@ -105,7 +129,9 @@ public: * * @param varName The name of the variable. */ - VarEntry(std::string varName) : _varName(varName) {}; + VarEntry(std::string varName) : _varName(varName) { + _type = seVar; + }; virtual std::ostream &print(std::ostream &output) const { return output << _varName; } }; @@ -127,7 +153,9 @@ public: * @param rhs Stack entry representing the right side of the operator. * @param op The operator for this entry. */ - BinaryOpEntry(StackEntry *lhs, StackEntry *rhs, std::string op) : _lhs(lhs), _rhs(rhs), _op(op) {} + BinaryOpEntry(StackEntry *lhs, StackEntry *rhs, std::string op) : _lhs(lhs), _rhs(rhs), _op(op) { + _type = seBinOp; + } virtual std::ostream &print(std::ostream &output) const { return output << _lhs << " " << _op << " " << _rhs; } }; @@ -147,11 +175,32 @@ public: * @param operand Stack entry representing the operand of the operation. * @param op The operator for this entry. */ - UnaryOpEntry(StackEntry *operand, std::string op) : _operand(operand), _op(op) {} + UnaryOpEntry(StackEntry *operand, std::string op) : _operand(operand), _op(op) { + _type = seUnaryOp; + } virtual std::ostream &print(std::ostream &output) const { return output << _op << _operand; } }; +/** + * Duplicated stack entry. + */ +class DupEntry : public StackEntry { +private: + int _idx; ///< Index to distinguish multiple duplicated entries. + +public: + /** + * Constructor for DupEntry. + * + * @param idx Index to distinguish multiple duplicated entries. + */ + DupEntry(int idx) : _idx(idx) { + _type = seDup; + } + virtual std::ostream &print(std::ostream &output) const { return output << "dup[" << _idx << "]"; } +}; + /** * Base class for code generators. */ @@ -168,13 +217,14 @@ private: void process(GraphVertex v); protected: - std::stack _stack; ///< The stack at the current point of the generation. + std::ostream &_output; ///< The std::ostream to output the code to. + /** * Processes an instruction. * * @param inst The instruction to process. */ - virtual void process(Instruction inst) = 0; + virtual void processInst(Instruction inst) = 0; public: virtual ~CodeGenerator() {}; @@ -183,14 +233,16 @@ public: * Constructor for CodeGenerator. * * @param engine Pointer to the Engine used for the script. - * @param g The annotated graph of the script. + * @param output The std::ostream to output the code to. */ - CodeGenerator(Engine *engine, const Graph &g); + CodeGenerator(Engine *engine, std::ostream &output); /** * Generates code from the provided graph and outputs it to stdout. + * + * @param g The annotated graph of the script. */ - void generate(); + void generate(const Graph &g); }; #endif diff --git a/decompiler/engine.h b/decompiler/engine.h index bd447cc7..5cab3507 100644 --- a/decompiler/engine.h +++ b/decompiler/engine.h @@ -24,6 +24,7 @@ #define ENGINE_H #include "disassembler.h" +#include "codegen.h" /** * Base class for engines. @@ -40,11 +41,20 @@ public: virtual Disassembler *getDisassembler() const = 0; /** - * Decode a jump-instruction to get the destination address. + * Decode a jump instruction to get the destination address. * * @param it Iterator pointing to the instruction to decode. + * @return The destination address of the jump instruction */ virtual uint32 getDestAddress(ConstInstIterator it) const = 0; + + /** + * Retrieve the code generator for the engine. + * + * @param output The std::ostream to output the code to. + * @return Pointer to a CodeGenerator for the engine. + */ + virtual CodeGenerator *getCodeGenerator(std::ostream &output) = 0; }; #endif diff --git a/decompiler/scummv6/codegen.cpp b/decompiler/scummv6/codegen.cpp new file mode 100644 index 00000000..1230ba1f --- /dev/null +++ b/decompiler/scummv6/codegen.cpp @@ -0,0 +1,27 @@ +/* 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$ + * + */ + +#include "codegen.h" + +void Scumm::v6::CodeGenerator::processInst(Instruction inst) { + // TODO +} diff --git a/decompiler/scummv6/codegen.h b/decompiler/scummv6/codegen.h new file mode 100644 index 00000000..2f89233b --- /dev/null +++ b/decompiler/scummv6/codegen.h @@ -0,0 +1,46 @@ +/* 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 SCUMM_V6_CODEGEN_H +#define SCUMM_V6_CODEGEN_H + +#include "../codegen.h" + +namespace Scumm { + +namespace v6 { + +/** + * SCUMMv6 code generator. + */ +class CodeGenerator : public ::CodeGenerator { +public: + CodeGenerator(Engine *engine, std::ostream &output) : ::CodeGenerator(engine, output) {} +protected: + void processInst(Instruction inst); +}; + +} // End of namespace v6 + +} // End of namespace Scumm + +#endif diff --git a/decompiler/scummv6/engine.cpp b/decompiler/scummv6/engine.cpp index 1491941a..bf9b842b 100644 --- a/decompiler/scummv6/engine.cpp +++ b/decompiler/scummv6/engine.cpp @@ -22,6 +22,7 @@ #include "engine.h" #include "disassembler.h" +#include "codegen.h" ::Disassembler *Scumm::v6::Engine::getDisassembler() const { return new Disassembler(); @@ -39,3 +40,7 @@ uint32 Scumm::v6::Engine::getDestAddress(ConstInstIterator it) const { return 0; } } + +::CodeGenerator *Scumm::v6::Engine::getCodeGenerator(std::ostream &output) { + return new CodeGenerator(this, output); +} diff --git a/decompiler/scummv6/engine.h b/decompiler/scummv6/engine.h index afcc5efc..60af9d5d 100644 --- a/decompiler/scummv6/engine.h +++ b/decompiler/scummv6/engine.h @@ -36,6 +36,7 @@ class Engine : public ::Engine { public: ::Disassembler *getDisassembler() const; uint32 getDestAddress(ConstInstIterator it) const; + ::CodeGenerator *getCodeGenerator(std::ostream &output); }; } // End of namespace v6 diff --git a/decompiler/test/module.mk b/decompiler/test/module.mk index 5d01ed60..1d46cc84 100644 --- a/decompiler/test/module.mk +++ b/decompiler/test/module.mk @@ -8,10 +8,12 @@ TESTS := $(srcdir)/decompiler/test/*.h TEST_LIBS := \ common/file.o\ + decompiler/codegen.o \ decompiler/control_flow.o \ decompiler/disassembler.o \ decompiler/simple_disassembler.o \ decompiler/scummv6/disassembler.o \ + decompiler/scummv6/codegen.o \ decompiler/scummv6/engine.o \ decompiler/test/disassembler/pasc.o \ decompiler/test/disassembler/subopcode.o \