1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
/* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "graph.h"
#include "value.h"
#include <ostream>
#include <utility>
#include <boost/intrusive_ptr.hpp>
#ifndef DEC_CODEGEN_H
#define DEC_CODEGEN_H
class Engine;
struct Function;
const int kIndentAmount = 2; ///< How many spaces to use for each indent.
/**
* Enumeration for the different argument/operand orderings.
*/
enum ArgOrder {
kFIFOArgOrder, ///< First argument is pushed to stack first.
kLIFOArgOrder ///< First argument is pushed to stack last.
};
/**
* 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(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.
/**
* 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(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);
public:
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 _curGroup; ///< 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.
*/
void generate(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.
*/
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);
};
#endif
|