소스 검색

Decompiler: All target languages except Lua remove. Lua language refactored and documented.

Iñigo Valentin 3 년 전
부모
커밋
69f8e13a4c

+ 4 - 2
V-Gears-Installer/CMakeLists.txt

@@ -22,7 +22,8 @@ set(HEADER_FILES
     include/common/DrawSkeleton.h
     include/common/DrawSkeleton.h
     include/common/Logger.h
     include/common/Logger.h
     include/decompiler/sudm.h
     include/decompiler/sudm.h
-    include/decompiler/decompiler_codegen.h
+    include/decompiler/LuaLanguage.h
+    include/decompiler/CodeGenerator.h
     include/decompiler/ControlFlow.h
     include/decompiler/ControlFlow.h
     include/decompiler/decompiler_disassembler.h
     include/decompiler/decompiler_disassembler.h
     include/decompiler/decompiler_engine.h
     include/decompiler/decompiler_engine.h
@@ -83,7 +84,8 @@ set(SOURCE_FILES
     src/common/OgreBase.cpp
     src/common/OgreBase.cpp
     src/VGearsUtility.cpp
     src/VGearsUtility.cpp
     src/decompiler/sudm.cpp
     src/decompiler/sudm.cpp
-    src/decompiler/decompiler_codegen.cpp
+    src/decompiler/LuaLanguage.cpp
+    src/decompiler/CodeGenerator.cpp
     src/decompiler/ControlFlow.cpp
     src/decompiler/ControlFlow.cpp
     src/decompiler/decompiler_disassembler.cpp
     src/decompiler/decompiler_disassembler.cpp
     src/decompiler/graph.cpp
     src/decompiler/graph.cpp

+ 185 - 0
V-Gears-Installer/include/decompiler/CodeGenerator.h

@@ -0,0 +1,185 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears 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.
+ */
+
+#pragma once
+
+#include <ostream>
+#include <utility>
+#include <memory>
+#include <boost/intrusive_ptr.hpp>
+#include "LuaLanguage.h"
+#include "graph.h"
+#include "value.h"
+#include "unknown_opcode_exception.h"
+
+
+class Engine;
+
+class Function;
+
+const int INDENT_SPACES = 4; ///< How many spaces to use for each indent.
+
+/**
+ * Different argument/operand orderings.
+ */
+enum ARGUMENT_ORDER{
+
+    /**
+     * First argument is pushed to stack first.
+     */
+    FIFO_ARGUMENT_ORDER,
+
+    /**
+     * First argument is pushed to stack last.
+     */
+    LIFO_ARGUMENT_ORDER
+};
+
+
+
+/**
+ * Base class for code generators.
+ */
+class CodeGenerator {
+
+    public:
+        LuaLanguage& TargetLang()
+        {
+            assert(target_lang_);
+            return *target_lang_;
+        }
+
+        void writeFunctionCall(std::string functionName, std::string paramsFormat, const std::vector<ValuePtr>& params);
+
+        const ARGUMENT_ORDER _binOrder;  ///< Order of operands for binary operations.
+        const ARGUMENT_ORDER _callOrder; ///< Order of operands for call arguments.
+        ValueList _argList;        ///< Storage for lists of arguments to be built when processing function calls.
+        GroupPtr cur_group_;     ///< 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, ARGUMENT_ORDER binOrder, ARGUMENT_ORDER 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);
+
+        /**
+         * Writes a comment line indicating an unimplemented opcode.
+         *
+         * @param code_gen[in|out] The code generator.
+         * @param class_name[in] The class where the instruction is. Unused.
+         * @param instruction[in] The unimplemented instruction.
+         */
+        void WriteTodo(std::string class_name, std::string instruction){
+            AddOutputLine("-- UNIMPLMENTED INSTRUCTION: \"" + instruction + "\")");
+        }
+
+        /**
+         * 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);
+
+    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<LuaLanguage> 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;
+
+    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);
+
+};

+ 184 - 0
V-Gears-Installer/include/decompiler/LuaLanguage.h

@@ -0,0 +1,184 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears 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.
+ */
+
+#pragma once
+
+#include "value.h"
+
+/**
+ * Provides formatters and keywords for the LUA language.
+ */
+class LuaLanguage{
+
+    public:
+
+        enum CONTEXT{
+
+            /**
+             * End of if/elseif block and about to start a final else.
+             */
+            TO_ELSE_BLOCK,
+
+            /**
+             * Begin of else block.
+             */
+            BEGIN_ELSE,
+
+            /**
+             * End of if block.
+             */
+            END_OF_IF,
+
+            /**
+             * End of while block.
+             */
+            END_OF_WHILE,
+
+            /**
+             * End of an if/elseif/else block.
+             */
+            END_OF_IF_ELSE_CHAIN,
+
+            /**
+             * Begin of a while block.
+             */
+            BEGIN_WHILE,
+
+            /**
+             * End of a while block.
+             */
+            END_WHILE
+        };
+
+        /**
+         * The Lua "break" keyword.
+         *
+         * @return "break".
+         */
+        virtual const std::string LoopBreak();
+
+        /**
+         * The Lua "continue" keyword.
+         *
+         * Continue is not implemented in Lua.
+         *
+         * @return An emopty string.
+         */
+        virtual const std::string LoopContinue();
+
+        /**
+         * A Lua "goto" instruction.
+         *
+         * @param target[in] A memory address label.
+         * @return "goto label_0x<target>X".
+         */
+        virtual std::string Goto(uint32 target);
+
+        /**
+         * The Lua "repeat" keyword.
+         *
+         * @return "repeat".
+         */
+        virtual const std::string DoLoopHeader();
+
+        /**
+         * The Lua parts of a do-loop footer
+         *
+         * @param before_expr[in] True to fetch the initial part of the
+         * footer, false for the ending part.
+         * @return "until (" if before_expr is true, ")" if not.
+         */
+        virtual std::string DoLoopFooter(bool before_expr);
+
+        /**
+         * The Lua parts of a if control sentence
+         *
+         * @param before_expr[in] True to fetch the initial part of the
+         * sentence, false for the ending part.
+         * @return "if (" if before_expr is true, ") then" if not.
+         */
+        virtual std::string If(bool before_expr);
+
+        /**
+         * The Lua parts of a while control sentence
+         *
+         * @param before_expr[in] True to fetch the initial part of the
+         * sentence, false for the ending part.
+         * @return "while (" if before_expr is true, ") do" if not.
+         */
+        virtual std::string WhileHeader(bool before_expr);
+
+        /**
+         * The Lua argument separator.
+         *
+         * @return ",".
+         */
+        virtual const std::string FunctionCallArgumentSeperator();
+
+        /**
+         * The Lua function argument list opener.
+         *
+         * @return "(".
+         */
+        virtual const std::string FunctionCallBegin();
+
+        /**
+         * The Lua function argument list closer.
+         *
+         * @return ")".
+         */
+        virtual const std::string FunctionCallEnd();
+
+        /**
+         * Generates a Lua label to use with goto.
+         *
+         * @param addr[in] Target address label.
+         * @return "::label_0x<target>X::"
+         */
+        virtual std::string Label(uint32 addr);
+
+        /**
+         * The Lua "else" keyword.
+         *
+         * @return "else".
+         */
+        virtual const std::string Else();
+
+
+        /**
+         * The Lua block starter (none).
+         *
+         * @param context[in] Unused.
+         * @return An empty string.
+         */
+        virtual std::string StartBlock(CONTEXT context);
+
+        /**
+         * A Lua block ender.
+         *
+         * @param context[in] The current context.
+         * @return "end", unles the current context is {@see TO_ELSE_BLOCK},
+         * in which case it will return an empty string, because "end" is not
+         * needed before an else.
+         */
+        virtual std::string EndBlock(CONTEXT context);
+
+        /**
+         * The Lua line terminator (none).
+         *
+         * @return An empty string.
+         */
+        virtual const std::string LineTerminator();
+};

+ 0 - 410
V-Gears-Installer/include/decompiler/decompiler_codegen.h

@@ -1,410 +0,0 @@
-#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 cur_group_;     ///< 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);
-
-    /**
-     * Writes a comment line indicating an unimplemented opcode.
-     *
-     * @param code_gen[in|out] The code generator.
-     * @param class_name[in] The class where the instruction is. Unused.
-     * @param instruction[in] The unimplemented instruction.
-     */
-    void WriteTodo(std::string class_name, std::string instruction){
-        AddOutputLine("-- UNIMPLMENTED INSTRUCTION: \"" + instruction + "\")");
-    }
-
-    /**
-     * 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);
-};

+ 1 - 2
V-Gears-Installer/include/decompiler/decompiler_engine.h

@@ -23,11 +23,10 @@
 #define ENGINE_H
 #define ENGINE_H
 
 
 #include "decompiler_disassembler.h"
 #include "decompiler_disassembler.h"
-#include "decompiler_codegen.h"
-
 #include <set>
 #include <set>
 #include <string>
 #include <string>
 #include <vector>
 #include <vector>
+#include "CodeGenerator.h"
 
 
 /**
 /**
  * Structure representing a function.
  * Structure representing a function.

+ 3 - 2
V-Gears-Installer/include/decompiler/field/FieldCodeGenerator.h

@@ -18,7 +18,8 @@
 #include <boost/algorithm/string.hpp>
 #include <boost/algorithm/string.hpp>
 #include <deque>
 #include <deque>
 #include <unordered_map>
 #include <unordered_map>
-#include "decompiler/decompiler_codegen.h"
+
+#include "decompiler/CodeGenerator.h"
 #include "decompiler/sudm.h"
 #include "decompiler/sudm.h"
 
 
 namespace FF7{
 namespace FF7{
@@ -306,7 +307,7 @@ namespace FF7{
             ) :
             ) :
               CodeGenerator(engine, output, FIFO_ARGUMENT_ORDER, LIFO_ARGUMENT_ORDER),
               CodeGenerator(engine, output, FIFO_ARGUMENT_ORDER, LIFO_ARGUMENT_ORDER),
               insts_(insts), formatter_(formatter)
               insts_(insts), formatter_(formatter)
-            {target_lang_ = std::make_unique<LuaTargetLanguage>();}
+            {target_lang_ = std::make_unique<LuaLanguage>();}
 
 
             /**
             /**
              * Generates the script from the instructions.
              * Generates the script from the instructions.

+ 1 - 1
V-Gears-Installer/include/decompiler/test/codegen.cpp

@@ -22,11 +22,11 @@
 
 
 #include "decompiler/decompiler_disassembler.h"
 #include "decompiler/decompiler_disassembler.h"
 #include "decompiler/graph.h"
 #include "decompiler/graph.h"
-#include "decompiler/decompiler_codegen.h"
 #include "decompiler/scummv6/engine.h"
 #include "decompiler/scummv6/engine.h"
 
 
 #include <vector>
 #include <vector>
 
 
+#include "../CodeGenerator.h"
 #include "../ControlFlow.h"
 #include "../ControlFlow.h"
 #define GET(vertex) (boost::get(boost::vertex_name, g, vertex))
 #define GET(vertex) (boost::get(boost::vertex_name, g, vertex))
 
 

+ 1 - 1
V-Gears-Installer/include/decompiler/world/WorldCodeGenerator.h

@@ -18,7 +18,7 @@
 
 
 #pragma once
 #pragma once
 
 
-#include "decompiler/decompiler_codegen.h"
+#include "../CodeGenerator.h"
 
 
 namespace FF7{
 namespace FF7{
 
 

+ 20 - 25
V-Gears-Installer/src/decompiler/decompiler_codegen.cpp → V-Gears-Installer/src/decompiler/CodeGenerator.cpp

@@ -1,30 +1,25 @@
-/* ScummVM Tools
+/*
+ * Copyright (C) 2022 The V-Gears Team
  *
  *
- * 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 file is part of V-Gears
  *
  *
- * 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.
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
  *
  *
- * This program is distributed in the hope that it will be useful,
+ * V-Gears is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  * GNU General Public License for more details.
  * 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 "decompiler/decompiler_codegen.h"
 #include "decompiler/decompiler_engine.h"
 #include "decompiler/decompiler_engine.h"
 #include <algorithm>
 #include <algorithm>
 #include <iostream>
 #include <iostream>
 #include <set>
 #include <set>
 #include <boost/format.hpp>
 #include <boost/format.hpp>
+#include "decompiler/CodeGenerator.h"
+#include "decompiler/LuaLanguage.h"
 
 
 #define GET(vertex)    (boost::get(boost::vertex_name, _g, vertex))
 #define GET(vertex)    (boost::get(boost::vertex_name, _g, vertex))
 #define GET_EDGE(edge) (boost::get(boost::edge_attribute, _g, edge))
 #define GET_EDGE(edge) (boost::get(boost::edge_attribute, _g, edge))
@@ -46,18 +41,18 @@ std::string CodeGenerator::ConstructFuncSignature(const Function &)
 std::string CodeGenerator::indentString(std::string s)
 std::string CodeGenerator::indentString(std::string s)
 {
 {
     std::stringstream stream;
     std::stringstream stream;
-    stream << std::string(kIndentAmount * _indentLevel, ' ') << s;
+    stream << std::string(INDENT_SPACES * _indentLevel, ' ') << s;
     return stream.str();
     return stream.str();
 }
 }
 
 
-CodeGenerator::CodeGenerator(Engine *engine, std::ostream &output, ArgOrder binOrder, ArgOrder callOrder)
+CodeGenerator::CodeGenerator(Engine *engine, std::ostream &output, ARGUMENT_ORDER binOrder, ARGUMENT_ORDER callOrder)
    : _output(output),
    : _output(output),
     _binOrder(binOrder),
     _binOrder(binOrder),
     _callOrder(callOrder)
     _callOrder(callOrder)
 {
 {
     _engine = engine;
     _engine = engine;
     _indentLevel = 0;
     _indentLevel = 0;
-    target_lang_ = std::make_unique<CTargetLanguage>();
+    target_lang_ = std::make_unique<LuaLanguage>();
 }
 }
 
 
 typedef std::pair<GraphVertex, ValueStack> DFSEntry;
 typedef std::pair<GraphVertex, ValueStack> DFSEntry;
@@ -196,7 +191,7 @@ void CodeGenerator::process(Function& func, InstVec& insts, GraphVertex v)
     // Check if we should add else start
     // Check if we should add else start
     if (cur_group_->_startElse)
     if (cur_group_->_startElse)
     {
     {
-        AddOutputLine(target_lang_->EndBlock(ITargetLanaguge::eToElseBlock) + " " + target_lang_->Else() + " " + target_lang_->StartBlock(ITargetLanaguge::eBeginElse), true, true);
+        AddOutputLine(target_lang_->EndBlock(LuaLanguage::TO_ELSE_BLOCK) + " " + target_lang_->Else() + " " + target_lang_->StartBlock(LuaLanguage::BEGIN_ELSE), true, true);
     }
     }
 
 
     // Check ingoing edges to see if we want to add any extra output
     // Check ingoing edges to see if we want to add any extra output
@@ -219,11 +214,11 @@ void CodeGenerator::process(Function& func, InstVec& insts, GraphVertex v)
         case kIfCondGroupType:
         case kIfCondGroupType:
             if (!cur_group_->_startElse)
             if (!cur_group_->_startElse)
             {
             {
-                AddOutputLine(target_lang_->EndBlock(ITargetLanaguge::eEndOfIf), true, false);
+                AddOutputLine(target_lang_->EndBlock(LuaLanguage::END_OF_IF), true, false);
             }
             }
             break;
             break;
         case kWhileCondGroupType:
         case kWhileCondGroupType:
-            AddOutputLine(target_lang_->EndBlock(ITargetLanaguge::eEndOfWhile), true, false);
+            AddOutputLine(target_lang_->EndBlock(LuaLanguage::END_OF_WHILE), true, false);
             break;
             break;
         default:
         default:
             break;
             break;
@@ -248,7 +243,7 @@ void CodeGenerator::process(Function& func, InstVec& insts, GraphVertex v)
     {
     {
         if (!(*elseIt)->_coalescedElse)
         if (!(*elseIt)->_coalescedElse)
         {
         {
-            AddOutputLine(target_lang_->EndBlock(ITargetLanaguge::eEndIfElseChain), true, false);
+            AddOutputLine(target_lang_->EndBlock(LuaLanguage::END_OF_IF_ELSE_CHAIN), true, false);
         }
         }
     }
     }
 }
 }
@@ -398,18 +393,18 @@ void CodeGenerator::ProcessCondJumpInst(const InstPtr inst)
             {
             {
                 cur_group_->_code.clear();
                 cur_group_->_code.clear();
                 cur_group_->_coalescedElse = true;
                 cur_group_->_coalescedElse = true;
-                s << target_lang_->EndBlock(ITargetLanaguge::eToElseBlock) << " " << target_lang_->Else() << " ";
+                s << target_lang_->EndBlock(LuaLanguage::TO_ELSE_BLOCK) << " " << target_lang_->Else() << " ";
             }
             }
         }
         }
         s << target_lang_->If(true) << _stack.pop()->negate() << target_lang_->If(false);
         s << target_lang_->If(true) << _stack.pop()->negate() << target_lang_->If(false);
         AddOutputLine(s.str(), cur_group_->_coalescedElse, true);
         AddOutputLine(s.str(), cur_group_->_coalescedElse, true);
         break;
         break;
     case kWhileCondGroupType:
     case kWhileCondGroupType:
-        s << target_lang_->WhileHeader(true) << _stack.pop()->negate() << target_lang_->WhileHeader(false) << " " << target_lang_->StartBlock(ITargetLanaguge::eBeginWhile);
+        s << target_lang_->WhileHeader(true) << _stack.pop()->negate() << target_lang_->WhileHeader(false) << " " << target_lang_->StartBlock(LuaLanguage::BEGIN_WHILE);
         AddOutputLine(s.str(), false, true);
         AddOutputLine(s.str(), false, true);
         break;
         break;
     case kDoWhileCondGroupType:
     case kDoWhileCondGroupType:
-        s << target_lang_->EndBlock(ITargetLanaguge::eEndWhile) <<  " " << target_lang_->WhileHeader(true) << _stack.pop() << target_lang_->WhileHeader(false);
+        s << target_lang_->EndBlock(LuaLanguage::END_WHILE) <<  " " << target_lang_->WhileHeader(true) << _stack.pop() << target_lang_->WhileHeader(false);
         AddOutputLine(s.str(), true, false);
         AddOutputLine(s.str(), true, false);
         break;
         break;
     default:
     default:

+ 72 - 0
V-Gears-Installer/src/decompiler/LuaLanguage.cpp

@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears 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.
+ */
+
+#include "decompiler/LuaLanguage.h"
+#include <sstream>
+#include <boost/format.hpp>
+
+const std::string LuaLanguage::LoopBreak(){return "break";}
+
+const std::string LuaLanguage::LoopContinue(){
+    // LUA has no continue keyword
+    return "-- TODO continue not supported in LUA!";
+}
+
+std::string LuaLanguage::Goto(uint32 target){
+    std::stringstream s;
+    s << boost::format("goto label_0x%X") % target;
+    return s.str();
+}
+
+const std::string LuaLanguage::DoLoopHeader(){return "repeat";}
+
+std::string LuaLanguage::DoLoopFooter(bool before_expr){
+    if (before_expr) return "until (";
+    return ")";
+}
+
+std::string LuaLanguage::If(bool before_expr){
+    if (before_expr) return "if (";
+    return ") then";
+}
+
+std::string LuaLanguage::WhileHeader(bool before_expr){
+    if (before_expr) return "while (";
+    return ") do";
+}
+
+const std::string LuaLanguage::FunctionCallArgumentSeperator(){return ",";}
+
+const std::string LuaLanguage::FunctionCallBegin(){return "(";}
+
+const std::string LuaLanguage::FunctionCallEnd(){return ")";}
+
+std::string LuaLanguage::Label(uint32 addr){
+    std::stringstream s;
+    s << boost::format("::label_0x%X::") % addr;
+    return s.str();
+}
+
+const std::string LuaLanguage::Else(){return "else";}
+
+std::string LuaLanguage::StartBlock(CONTEXT context){return "";}
+
+std::string LuaLanguage::EndBlock(CONTEXT context){
+    // For the final else, an end it's not needed before it
+    if (context == TO_ELSE_BLOCK) return "";
+    return "end";
+}
+
+const std::string LuaLanguage::LineTerminator(){return "";}

+ 2 - 1
V-Gears-Installer/src/decompiler/instruction.cpp

@@ -20,7 +20,8 @@
  */
  */
 
 
 #include "decompiler/instruction.h"
 #include "decompiler/instruction.h"
-#include "decompiler/decompiler_codegen.h"
+
+#include "../../include/decompiler/CodeGenerator.h"
 #include "decompiler/decompiler_engine.h"
 #include "decompiler/decompiler_engine.h"
 
 
 bool outputStackEffect = true;
 bool outputStackEffect = true;