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

Base class for code generators. More...

#include <CodeGenerator.h>

Inheritance diagram for CodeGenerator:
Collaboration diagram for CodeGenerator:

Public Member Functions

 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...
 

Protected Member Functions

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...
 

Protected Attributes

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...
 

Private Member Functions

void Process (Function &function, InstVec &insts, GraphVertex vertex)
 Processes a GraphVertex. More...
 

Private Attributes

Graph graph_
 The annotated graph of the script. More...
 
const ARGUMENT_ORDER bin_order_
 Order of operands for binary operations. More...
 
const ARGUMENT_ORDER call_order_
 Order of operands for call arguments. More...
 
ValueList arg_list_
 Lists of arguments to be built when processing function calls. More...
 

Detailed Description

Base class for code generators.

This is to be overriden by each engine.

Constructor & Destructor Documentation

◆ CodeGenerator()

CodeGenerator::CodeGenerator ( Engine engine,
std::ostream &  output,
ARGUMENT_ORDER  bin_order,
ARGUMENT_ORDER  call_order 
)

Constructor.

Parameters
[in]engineThe engine used for the script.
[out]outputThe stream to output the code to.
[in]bin_orderOrder of arguments for binary operators.
[in]call_orderOrder of arguments for function calls.

◆ ~CodeGenerator()

CodeGenerator::~CodeGenerator ( )
virtual

Destructor.

Does nothing.

Member Function Documentation

◆ AddArg()

void CodeGenerator::AddArg ( ValuePtr  arg)

Add an argument to the argument list.

Parameters
[in]argThe argument to add.

◆ AddOutputLine()

void CodeGenerator::AddOutputLine ( std::string  line,
bool  unindent_before = false,
bool  indent_after = false 
)
virtual

Adds a line of code to the current group.

Parameters
[in]lineThe line to add.
[in]unindent_beforeWhether or not to remove an indentation level before the line. Defaults to false.
[in]indent_afterWhether or not to add an indentation level after the line. Defaults to false.

Reimplemented in FieldCodeGenerator.

◆ ConstructFuncSignature()

std::string CodeGenerator::ConstructFuncSignature ( const Function function)
protectedvirtual

Construct the signature for a function.

Parameters
[in]functionReference to the function to construct the signature for.
Returns
For this base class, an empty string.

Reimplemented in WorldCodeGenerator, and FieldCodeGenerator.

◆ Generate()

void CodeGenerator::Generate ( InstVec insts,
const Graph graph 
)
virtual

Generates code from the provided graph and outputs it to stdout.

Parameters
[in]instsThe list of instructions.
[in]graphThe annotated graph of the script.

Reimplemented in FieldCodeGenerator.

◆ GeneratePass()

void CodeGenerator::GeneratePass ( InstVec insts,
const Graph graph 
)
protected

Generates a pass.

Parameters
[in]instsThe list of instructions.
[in]graphThe code graph.
Todo:
Understand and explain.

◆ GetArgList()

ValueList CodeGenerator::GetArgList ( )
virtual

Retrieves the argument list.

Returns
The argument list.

◆ GetBinaryOrder()

ARGUMENT_ORDER CodeGenerator::GetBinaryOrder ( )
virtual

Retrieves the order of operands for binary operations.

Returns
The order of operands

◆ GetLanguage()

LuaLanguage & CodeGenerator::GetLanguage ( )

Retrieves the target language.

◆ IndentString()

std::string CodeGenerator::IndentString ( std::string  s)
protected

Indents a string according to the current indentation level.

Parameters
[in]sThe string to indent.
Returns
The indented string.

◆ OnBeforeStartFunction()

void CodeGenerator::OnBeforeStartFunction ( const Function function)
protectedvirtual

Adds lines to the script before a function.

Called before writing a function start. For this base class, it does nothing.

Parameters
[in]functionThe function about to start.

Reimplemented in FieldCodeGenerator.

◆ OnEndFunction()

void CodeGenerator::OnEndFunction ( const Function function)
protectedvirtual

Adds lines to the script at the end a function.

Called after writing a function. For this base class, it adds a closing bracer "}".

Parameters
[in]functionThe function about to end.

Reimplemented in FieldCodeGenerator.

◆ OnStartFunction()

void CodeGenerator::OnStartFunction ( const Function function)
protectedvirtual

Adds lines to the script before a function instructions.

Called after writing a function start. For this base class, it does nothing.

Parameters
[in]functionThe function starting.

Reimplemented in FieldCodeGenerator.

◆ OutputOnlyRequiredLabels()

bool CodeGenerator::OutputOnlyRequiredLabels ( ) const
protectedvirtual

Checks if only required labels are to be written.

Returns
Always false.

Reimplemented in FieldCodeGenerator.

◆ Process()

void CodeGenerator::Process ( Function function,
InstVec insts,
GraphVertex  vertex 
)
private

Processes a GraphVertex.

Parameters
[in]functionThe function to process.
[in]instsThe list of instructions.
[in]vertexThe vertex to process.

◆ ProcessCondJumpInst()

void CodeGenerator::ProcessCondJumpInst ( const InstPtr  inst)
protected

Processes a conditional jump instruction.

Called by {

See also
ProcessInst()} for those instructions. Call the base class implementation for opcodes not handled by an implemented engine, or where the base class implementation is preferable.
Parameters
[in]instThe instruction to process.

◆ ProcessInst()

void CodeGenerator::ProcessInst ( Function function,
InstVec insts,
const InstPtr  inst 
)
protected

Processes an instruction.

Called by {

See also
Process()} for each instruction. Call the base class implementation for opcodes not handled by an implemented engine, or where the base class implementation is preferable.
Parameters
[in]functionThe function the instruction is is.
[in]instThe instruction to process.
[in]instsEvery instruction in the function.

◆ ProcessSpecialMetadata()

void CodeGenerator::ProcessSpecialMetadata ( const InstPtr  inst,
char  c,
int  pos 
)
virtual

Process a single character of metadata.

Parameters
[in]instThe instruction being processed. Unused.
[in]cThe character signifying the action to be taken. The only valid one is 'p'.
[in]posThe position at which c occurred in the metadata. Unused.

Reimplemented in WorldCodeGenerator.

◆ ProcessUncondJumpInst()

void CodeGenerator::ProcessUncondJumpInst ( Function function,
InstVec insts,
const InstPtr  inst 
)
protected

Processes an unconditional jump instruction.

Called by {

See also
ProcessInst()} for those instructions. Call the base class implementation for opcodes not handled by an implemented engine, or where the base class implementation is preferable.
Parameters
[in]functionThe function the instruction is is.
[in]instThe instruction to process.
[in]instsEvery instruction in the function.

◆ WriteAssignment()

void CodeGenerator::WriteAssignment ( ValuePtr  dst,
ValuePtr  src 
)

Generate an assignment statement.

Parameters
[in]dstThe variable being assigned to.
[in]srcThe value being assigned.

◆ WriteFunctionCall()

void CodeGenerator::WriteFunctionCall ( std::string  function_name,
std::string  param_format,
const std::vector< ValuePtr > &  params 
)

Writes a function call.

Parameters
[in]function_nameThe name of the function.
[in]param_formatCharacters indicating the parameter format. 'b' for boolean parameters, 'n' for integers (treated as unsigned) or 'f' for floats.
[in]paramsThe list of parameters.

◆ WriteTodo()

void CodeGenerator::WriteTodo ( std::string  class_name,
std::string  instruction 
)

Writes a comment line indicating an unimplemented opcode.

Parameters
[in]class_nameThe class where the instruction is. Unused.
[in]instructionThe unimplemented instruction.

Member Data Documentation

◆ arg_list_

ValueList CodeGenerator::arg_list_
private

Lists of arguments to be built when processing function calls.

◆ bin_order_

const ARGUMENT_ORDER CodeGenerator::bin_order_
private

Order of operands for binary operations.

◆ call_order_

const ARGUMENT_ORDER CodeGenerator::call_order_
private

Order of operands for call arguments.

◆ cur_group_

GroupPtr CodeGenerator::cur_group_
protected

The group currently being processed.

◆ cur_vertex_

GraphVertex CodeGenerator::cur_vertex_
protected

Graph vertex currently being processed.

◆ engine_

Engine* CodeGenerator::engine_
protected

The engine used for teh script.

◆ graph_

Graph CodeGenerator::graph_
private

The annotated graph of the script.

◆ indent_level_

uint CodeGenerator::indent_level_
protected

Current indentation level.

◆ is_label_pass_

bool CodeGenerator::is_label_pass_ = true
protected

Indicates if a label is being processed.

◆ output_

std::ostream& CodeGenerator::output_
protected

The stream to output the code to.

◆ stack_

ValueStack CodeGenerator::stack_
protected

The stack currently being processed.

◆ target_lang_

std::unique_ptr<LuaLanguage> CodeGenerator::target_lang_
protected

The target language.


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