V-Gears 0
Free Final Fantasy VII engine.
FieldCodeGenerator Class Reference

The code generator. More...

#include <FieldCodeGenerator.h>

Inheritance diagram for FieldCodeGenerator:
Collaboration diagram for FieldCodeGenerator:

Public Types

enum struct  ValueType : int { Integer = 0 , Float = 1 }
 Type of data. More...
 

Public Member Functions

 FieldCodeGenerator (Engine *engine, const InstVec &insts, std::ostream &output, FieldScriptFormatter &formatter)
 Constructor. More...
 
virtual void Generate (InstVec &insts, const Graph &graph) override
 Generates the script from the instructions. More...
 
virtual void AddOutputLine (std::string line, bool unindent_before=false, bool indent_after=false) override
 Adds a line to the script. More...
 
float GetScaleFactor () const
 Retrieves the engine scale factor. More...
 
FieldScriptFormatterGetFormatter ()
 Retrieves the formatter. More...
 
- Public Member Functions inherited from CodeGenerator
 CodeGenerator (Engine *engine, std::ostream &output, ARGUMENT_ORDER bin_order, ARGUMENT_ORDER call_order)
 Constructor. More...
 
virtual ~CodeGenerator ()
 Destructor. More...
 
LuaLanguageGetLanguage ()
 Retrieves the target language. More...
 
void WriteFunctionCall (std::string function_name, std::string param_format, const std::vector< ValuePtr > &params)
 Writes a function call. More...
 
virtual void Generate (InstVec &insts, const Graph &graph)
 Generates code from the provided graph and outputs it to stdout. More...
 
virtual void AddOutputLine (std::string line, bool unindent_before=false, bool indent_after=false)
 Adds a line of code to the current group. More...
 
void WriteTodo (std::string class_name, std::string instruction)
 Writes a comment line indicating an unimplemented opcode. More...
 
void WriteAssignment (ValuePtr dst, ValuePtr src)
 Generate an assignment statement. More...
 
void AddArg (ValuePtr arg)
 Add an argument to the argument list. More...
 
virtual void ProcessSpecialMetadata (const InstPtr inst, char c, int pos)
 Process a single character of metadata. More...
 
virtual ValueList GetArgList ()
 Retrieves the argument list. More...
 
virtual ARGUMENT_ORDER GetBinaryOrder ()
 Retrieves the order of operands for binary operations. More...
 

Static Public Member Functions

static const std::string FormatInstructionNotImplemented (const std::string &entity, uint32 address, uint32 opcode)
 Generates code for an unimplemented instruction. More...
 
static const std::string FormatInstructionNotImplemented (const std::string &entity, uint32 address, const Instruction &instruction)
 Generates code for an unimplemented instruction. More...
 
static const std::string FormatBool (uint32 value)
 Formats a value as a boolean. More...
 
static const std::string FormatInvertedBool (uint32 value)
 Formats a value as a boolean. More...
 
template<typename TValue >
static const std::string FormatValueOrVariable (FieldScriptFormatter &formatter, uint32 bank, TValue value_or_address, ValueType type=ValueType::Integer, float scale=1.0f)
 Formats a data access for a map script. More...
 

Protected Member Functions

virtual std::string ConstructFuncSignature (const Function &function) override
 Constructs the function signature. More...
 
virtual void OnEndFunction (const Function &function) override
 Finalizes a function. More...
 
virtual void OnBeforeStartFunction (const Function &function) override
 Adds lines to the script before a function. More...
 
virtual void OnStartFunction (const Function &function) override
 Initializes a function. More...
 
virtual bool OutputOnlyRequiredLabels () const override
 Checks if only required labels are to be written. More...
 
- Protected Member Functions inherited from CodeGenerator
void ProcessInst (Function &function, InstVec &insts, const InstPtr inst)
 Processes an instruction. More...
 
void ProcessUncondJumpInst (Function &function, InstVec &insts, const InstPtr inst)
 Processes an unconditional jump instruction. More...
 
void ProcessCondJumpInst (const InstPtr inst)
 Processes a conditional jump instruction. More...
 
std::string IndentString (std::string s)
 Indents a string according to the current indentation level. More...
 
virtual std::string ConstructFuncSignature (const Function &function)
 Construct the signature for a function. More...
 
virtual void OnBeforeStartFunction (const Function &function)
 Adds lines to the script before a function. More...
 
virtual void OnEndFunction (const Function &function)
 Adds lines to the script at the end a function. More...
 
virtual void OnStartFunction (const Function &function)
 Adds lines to the script before a function instructions. More...
 
virtual bool OutputOnlyRequiredLabels () const
 Checks if only required labels are to be written. More...
 
void GeneratePass (InstVec &insts, const Graph &graph)
 Generates a pass. More...
 

Private Attributes

const InstVecinsts_
 The list of instructions in the function to generate. More...
 
std::vector< CodeLinelines_
 Generated lines. More...
 
FieldScriptFormatterformatter_
 The formatter. More...
 

Additional Inherited Members

- Protected Attributes inherited from CodeGenerator
bool is_label_pass_ = true
 Indicates if a label is being processed. More...
 
GroupPtr cur_group_
 The group currently being processed. More...
 
Engineengine_
 The engine used for teh script. More...
 
std::ostream & output_
 The stream to output the code to. More...
 
ValueStack stack_
 The stack currently being processed. More...
 
uint indent_level_
 Current indentation level. More...
 
GraphVertex cur_vertex_
 Graph vertex currently being processed. More...
 
std::unique_ptr< LuaLanguagetarget_lang_
 The target language. More...
 

Detailed Description

The code generator.

Generates code for a field map function.

Member Enumeration Documentation

◆ ValueType

enum struct FieldCodeGenerator::ValueType : int
strong

Type of data.

Enumerator
Integer 

Integer.

Float 

Float.

Constructor & Destructor Documentation

◆ FieldCodeGenerator()

FieldCodeGenerator::FieldCodeGenerator ( Engine engine,
const InstVec insts,
std::ostream &  output,
FieldScriptFormatter formatter 
)
inline

Constructor.

Parameters
[in]engineThe engine.
[in]instsThe list of instructions to parse.
[out]outputThe generated script.
[in]formatterThe code formatter.

Member Function Documentation

◆ AddOutputLine()

void FieldCodeGenerator::AddOutputLine ( std::string  line,
bool  unindent_before = false,
bool  indent_after = false 
)
overridevirtual

Adds a line to the script.

Parameters
[in]lineThe line to add.
[in]unindent_beforeIf true, the current script indentation will be moved back one position before the line.
[in]indent_afterIf true, the current script indentation will be moved forward one position after the line.

Reimplemented from CodeGenerator.

◆ ConstructFuncSignature()

std::string FieldCodeGenerator::ConstructFuncSignature ( const Function function)
overrideprotectedvirtual

Constructs the function signature.

The function signature is the LUA function declaration, and it looks like this:

function_name = function(self)

Parameters
[in]functionThe function to construct the signature for.
Returns
The function signature.

Reimplemented from CodeGenerator.

◆ FormatBool()

const std::string FieldCodeGenerator::FormatBool ( uint32  value)
static

Formats a value as a boolean.

Parameters
[in]valueThe value to format.
Returns
"false" if the value is 0, "true" otherwise.

◆ FormatInstructionNotImplemented() [1/2]

const std::string FieldCodeGenerator::FormatInstructionNotImplemented ( const std::string &  entity,
uint32  address,
const Instruction instruction 
)
static

Generates code for an unimplemented instruction.

The code will actually be a comment, indicating where the function call must have been.

Parameters
[in]entityThe entity name.
[in]addressThe address of the instruction.
[in]instructionThe unimplemented instruction.

◆ FormatInstructionNotImplemented() [2/2]

const std::string FieldCodeGenerator::FormatInstructionNotImplemented ( const std::string &  entity,
uint32  address,
uint32  opcode 
)
static

Generates code for an unimplemented instruction.

The code will actually be a comment, indicating where the function call must have been.

Parameters
[in]entityThe entity name.
[in]addressThe address of the instruction.
[in]opcodeThe opcode not implemented.

◆ FormatInvertedBool()

const std::string FieldCodeGenerator::FormatInvertedBool ( uint32  value)
static

Formats a value as a boolean.

Parameters
[in]valueThe value to format.
Returns
"true" if the value is 0, "false" otherwise.

◆ FormatValueOrVariable()

template<typename TValue >
static const std::string FieldCodeGenerator::FormatValueOrVariable ( FieldScriptFormatter formatter,
uint32  bank,
TValue  value_or_address,
ValueType  type = ValueType::Integer,
float  scale = 1.0f 
)
inlinestatic

Formats a data access for a map script.

If possible, it will look for friendly names for variables.

Parameters
[in]formatterFormatter to look up variable friendly names.
[in]bankThe memory bank to use.
[in]value_or_addressThe value or memory address to use. When bank is 0, it will be considered as a value. When bank is non 0, it will be considered an address of the bank.
[in]typeData type to use. Used only when getting a value, not a bank address.
[in]scaleScale to scale values to. Used only when using float values, unused when type is integer or when using a bank address.
Returns
String with the friendly variable name, value, or bank address.

◆ Generate()

void FieldCodeGenerator::Generate ( InstVec insts,
const Graph graph 
)
overridevirtual

Generates the script from the instructions.

Parameters
[in]instsThe list of instructions.
[in]graphCode graph, unused.

Reimplemented from CodeGenerator.

◆ GetFormatter()

FieldScriptFormatter & FieldCodeGenerator::GetFormatter ( )

Retrieves the formatter.

◆ GetScaleFactor()

float FieldCodeGenerator::GetScaleFactor ( ) const

Retrieves the engine scale factor.

Returns
The scale factor

◆ OnBeforeStartFunction()

void FieldCodeGenerator::OnBeforeStartFunction ( const Function function)
overrideprotectedvirtual

Adds lines to the script before a function.

If the function is the first in a class, it initializes the class and, if the function belongs to a character, creates the class variable for the character. If the function has a comment associated, it will also be added before the function.

Parameters
[in]functionThe function about to start.

Reimplemented from CodeGenerator.

◆ OnEndFunction()

void FieldCodeGenerator::OnEndFunction ( const Function function)
overrideprotectedvirtual

Finalizes a function.

It appends an "end" to finalize the function. If the function is the last of a class, it also closes the class braces.

Parameters
[in]functionThe function to end.

Reimplemented from CodeGenerator.

◆ OnStartFunction()

void FieldCodeGenerator::OnStartFunction ( const Function function)
overrideprotectedvirtual

Initializes a function.

It adds a comment with the function opcodes.

Parameters
[in]functionThe function to start.

Reimplemented from CodeGenerator.

◆ OutputOnlyRequiredLabels()

bool FieldCodeGenerator::OutputOnlyRequiredLabels ( ) const
overrideprotectedvirtual

Checks if only required labels are to be written.

Returns
Always true.

Reimplemented from CodeGenerator.

Member Data Documentation

◆ formatter_

FieldScriptFormatter& FieldCodeGenerator::formatter_
private

The formatter.

◆ insts_

const InstVec& FieldCodeGenerator::insts_
private

The list of instructions in the function to generate.

◆ lines_

std::vector<CodeLine> FieldCodeGenerator::lines_
private

Generated lines.


The documentation for this class was generated from the following files: