| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- #pragma once
- /* ScummVM Tools
- *
- * ScummVM Tools is the legal property of its developers, whose
- * names are too numerous to list here. Please refer to the
- * COPYRIGHT file distributed with this source distribution.
- *
- * 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.
- */
- #include "graph.h"
- #include "value.h"
- #include "unknown_opcode_exception.h"
- #include <ostream>
- #include <utility>
- #include <boost/intrusive_ptr.hpp>
- #include <memory>
- class Engine;
- class Function;
- const int kIndentAmount = 4; ///< How many spaces to use for each indent.
- /**
- * Enumeration for the different argument/operand orderings.
- */
- enum ArgOrder
- {
- FIFO_ARGUMENT_ORDER, ///< First argument is pushed to stack first.
- LIFO_ARGUMENT_ORDER ///< First argument is pushed to stack last.
- };
- class ITargetLanaguge
- {
- public:
- enum eContext
- {
- eToElseBlock, // End of if/elseif block and about to start a final else
- eBeginElse,
- eEndOfIf,
- eEndOfWhile,
- eEndIfElseChain,
- eBeginWhile,
- eEndWhile
- };
- virtual ~ITargetLanaguge() = default;
- virtual std::string LoopBreak() = 0;
- virtual std::string LoopContinue() = 0;
- virtual std::string Goto(uint32 target) = 0;
- virtual std::string DoLoopHeader() = 0;
- virtual std::string DoLoopFooter(bool beforeExpr) = 0;
- virtual std::string If(bool beforeExpr) = 0;
- virtual std::string WhileHeader(bool beforeExpr) = 0;
- virtual std::string FunctionCallArgumentSeperator() = 0;
- virtual std::string FunctionCallBegin() = 0;
- virtual std::string FunctionCallEnd() = 0;
- virtual std::string Label(uint32 addr) = 0;
- virtual std::string Else() = 0;
- virtual std::string StartBlock(eContext) = 0;
- virtual std::string EndBlock(eContext) = 0;
- virtual std::string LineTerminator() = 0;
- };
- class CTargetLanguage : public ITargetLanaguge
- {
- public:
- virtual std::string LoopBreak() override
- {
- return "break;";
- }
- virtual std::string LoopContinue() override
- {
- return "continue;";
- }
- virtual std::string Goto(uint32 target) override
- {
- std::stringstream s;
- s << boost::format("goto label_0x%X;") % target;
- return s.str();
- }
- virtual std::string DoLoopHeader() override
- {
- return "do {";
- }
- virtual std::string DoLoopFooter(bool beforeExpr) override
- {
- if (beforeExpr)
- {
- return " } while (";
- }
- return ");";
- }
- virtual std::string If(bool beforeExpr) override
- {
- if (beforeExpr)
- {
- return "if (";
- }
- return ") {";
- }
- virtual std::string WhileHeader(bool beforeExpr) override
- {
- if (beforeExpr)
- {
- return "while (";
- }
- return ")";
- }
- virtual std::string FunctionCallArgumentSeperator() override
- {
- return ",";
- }
-
- virtual std::string FunctionCallBegin() override
- {
- return "(";
- }
-
- virtual std::string FunctionCallEnd() override
- {
- return ");";
- }
- virtual std::string Label(uint32 addr) override
- {
- std::stringstream s;
- s << boost::format("label_0x%X:") % addr;
- return s.str();
- }
- virtual std::string Else() override
- {
- return "else";
- }
- virtual std::string StartBlock(eContext) override
- {
- return "{";
- }
- virtual std::string EndBlock(eContext) override
- {
- return "}";
- }
- virtual std::string LineTerminator() override
- {
- return ";";
- }
- };
- class LuaTargetLanguage : public ITargetLanaguge
- {
- public:
- virtual std::string LoopBreak() override
- {
- return "break";
- }
- virtual std::string LoopContinue() override
- {
- // LUA has no continue keyword
- //throw InternalDecompilerError();
- return "-- TODO continue not supported in LUA!";
- }
- virtual std::string Goto(uint32 target) override
- {
- std::stringstream s;
- s << boost::format("goto label_0x%X") % target;
- return s.str();
- }
- virtual std::string DoLoopHeader() override
- {
- return "repeat";
- }
- virtual std::string DoLoopFooter(bool beforeExpr) override
- {
- if (beforeExpr)
- {
- return "until (";
- }
- return ")";
- }
- virtual std::string If(bool beforeExpr) override
- {
- if (beforeExpr)
- {
- return "if (";
- }
- return ") then";
- }
- virtual std::string WhileHeader(bool beforeExpr) override
- {
- if (beforeExpr)
- {
- return "while (";
- }
- return ") do";
- }
- virtual std::string FunctionCallArgumentSeperator() override
- {
- return ",";
- }
- virtual std::string FunctionCallBegin() override
- {
- return "(";
- }
- virtual std::string FunctionCallEnd() override
- {
- return ")";
- }
- virtual std::string Label(uint32 addr) override
- {
- std::stringstream s;
- s << boost::format("::label_0x%X::") % addr;
- return s.str();
- }
- virtual std::string Else() override
- {
- return "else";
- }
- virtual std::string StartBlock(eContext) override
- {
- return "";
- }
- virtual std::string EndBlock(eContext ctx) override
- {
- if (ctx == eToElseBlock)
- {
- // For the final else we don't need an end before it
- return "";
- }
- return "end";
- }
- virtual std::string LineTerminator() override
- {
- return "";
- }
- };
- /**
- * Base class for code generators.
- */
- class CodeGenerator
- {
- private:
- Graph _g; ///< The annotated graph of the script.
- /**
- * Processes a GraphVertex.
- *
- * @param v The vertex to process.
- */
- void process(Function& func, InstVec& insts, GraphVertex v);
- protected:
- Engine *_engine; ///< Pointer to the Engine used for the script.
- std::ostream &_output; ///< The std::ostream to output the code to.
- ValueStack _stack; ///< The stack currently being processed.
- uint _indentLevel; ///< Indentation level.
- GraphVertex _curVertex; ///< Graph vertex currently being processed.
- std::unique_ptr<ITargetLanaguge> target_lang_;
- /**
- * Processes an instruction. Called by process() for each instruction.
- * Call the base class implementation for opcodes you cannot handle yourself,
- * or where the base class implementation is preferable.
- *
- * @param inst The instruction to process.
- */
- void ProcessInst(Function& func, InstVec& insts, const InstPtr inst);
- void processUncondJumpInst(Function& func, InstVec& insts, const InstPtr inst);
- void processCondJumpInst(const InstPtr inst);
- /**
- * Indents a string according to the current indentation level.
- *
- * @param s The string to indent.
- * @result The indented string.
- */
- std::string indentString(std::string s);
- /**
- * Construct the signature for a function.
- *
- * @param func Reference to the function to construct the signature for.
- */
- virtual std::string ConstructFuncSignature(const Function& func);
- virtual void OnBeforeStartFunction(const Function& func);
- virtual void OnEndFunction(const Function& func);
- virtual void OnStartFunction(const Function&) { }
- virtual bool OutputOnlyRequiredLabels() const { return false; }
- void generatePass(InstVec& insts, const Graph& g);
- bool mIsLabelPass = true;
- public:
- ITargetLanaguge& TargetLang()
- {
- assert(target_lang_);
- return *target_lang_;
- }
- void writeFunctionCall(std::string functionName, std::string paramsFormat, const std::vector<ValuePtr>& params);
- const ArgOrder _binOrder; ///< Order of operands for binary operations.
- const ArgOrder _callOrder; ///< Order of operands for call arguments.
- ValueList _argList; ///< Storage for lists of arguments to be built when processing function calls.
- GroupPtr mCurGroup; ///< Pointer to the group currently being processed.
- virtual ~CodeGenerator() { }
- /**
- * Constructor for CodeGenerator.
- *
- * @param engine Pointer to the Engine used for the script.
- * @param output The std::ostream to output the code to.
- * @param binOrder Order of arguments for binary operators.
- * @param callOrder Order of arguments for function calls.
- */
- CodeGenerator(Engine *engine, std::ostream &output, ArgOrder binOrder, ArgOrder callOrder);
- /**
- * Generates code from the provided graph and outputs it to stdout.
- *
- * @param g The annotated graph of the script.
- */
- virtual void Generate(InstVec& insts, const Graph &g);
- /**
- * 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.
- */
- virtual void AddOutputLine(std::string s, bool unindentBefore = false, bool indentAfter = false);
- /**
- * Generate an assignment statement.
- *
- * @param dst The variable being assigned to.
- * @param src The value being assigned.
- */
- void writeAssignment(ValuePtr dst, ValuePtr src);
- /**
- * Add an argument to the argument list.
- *
- * @param p The argument to add.
- */
- void addArg(ValuePtr p);
- /**
- * Process a single character of metadata.
- *
- * @param inst The instruction being processed.
- * @param c The character signifying the action to be taken.
- * @param pos The position at which c occurred in the metadata.
- */
- virtual void processSpecialMetadata(const InstPtr inst, char c, int pos);
- };
|