Gyoji Compiler
|
Function inside a translation unit. More...
#include <functions.hpp>
Public Member Functions | |
Function (std::string _name, const Type *_return_type, const std::vector< FunctionArgument > &_arguments, const Gyoji::context::SourceReference &_source_ref) | |
Creates a new function. | |
const std::string & | get_name () const |
Name of the function. | |
const Type * | get_return_type () const |
Return type of the function. | |
const std::vector< FunctionArgument > & | get_arguments () const |
Arguments to the function. | |
const BasicBlock & | get_basic_block (size_t blockid) const |
Get an immutable basic block from the function by ID. | |
BasicBlock & | get_basic_block (size_t blockid) |
Get a mutable block by ID. | |
size_t | add_block () |
Creates a new basic block and returns the ID. | |
const std::map< size_t, Gyoji::owned< BasicBlock > > & | get_blocks () const |
Get the blocks of the function. | |
void | dump (FILE *out) const |
Dump a function to the given file handle for debugging. | |
const Gyoji::context::SourceReference & | get_source_ref () const |
Reference to the source location where the function is defined. | |
const Type * | tmpvar_get (size_t tmpvar_id) const |
Temporary values used by opcodes. | |
size_t | tmpvar_define (const Type *tmpvar_type) |
Define a new temporary variable with the given type. | |
size_t | tmpvar_duplicate (size_t tmpvar_id) |
Duplicate a temporary variable/reigster. | |
Function inside a translation unit.
A function represents an actual defined function (not just a prototype). It is defined by its name, return type, and list of arguments. Inside each function is a series of basic blocks containing a set of operations. Each of these operations is an 'opcode' in the MIR virtual machine. That machine operates on the tempoary variables (i.e. registers) of the virtual machine.
Function::Function | ( | std::string | _name, |
const Type * | _return_type, | ||
const std::vector< FunctionArgument > & | _arguments, | ||
const Gyoji::context::SourceReference & | _source_ref | ||
) |
Creates a new function.
A new function is created with the given name, return-type, and arguments. The source-reference is the location of the beginning of the function definition.
The name of the function is assumed to be already 'mangled' and at this point, it should be globally unique.
size_t Function::add_block | ( | ) |
Creates a new basic block and returns the ID.
Adds a new basic block to the function and returns the ID of that new block.
void Function::dump | ( | FILE * | out | ) | const |
Dump a function to the given file handle for debugging.
This dumps a function, all of its basic blocks, and its operations to the file handle given. This is mainly for debugging purposes.
const std::vector< FunctionArgument > & Function::get_arguments | ( | ) | const |
Arguments to the function.
This returns a list of the arguments to the function which are each a pair of name and type for the argument.
BasicBlock & Function::get_basic_block | ( | size_t | blockid | ) |
Get a mutable block by ID.
Returns a mutable basic block from the function by its ID. Note that it is assumed that the blockid exists, and this may dump core if the blockid does not exist.
const BasicBlock & Function::get_basic_block | ( | size_t | blockid | ) | const |
Get an immutable basic block from the function by ID.
This returns the basic block with the given ID. Note that it is assumed that the blockid exists, and this may dump core if the blockid does not exist.
const std::map< size_t, Gyoji::owned< BasicBlock > > & Function::get_blocks | ( | ) | const |
Get the blocks of the function.
This returns a map of all of the defined basic blocks and their associated IDs as a pair. This list is not ordered, but each of the blocks "naturally" forms a control-flow graph because the end of each block is a flow-control decision (branch/goto) or a flow-terminator (return) which ends the control of that block, so the execution order can be derived by the code-generator based on these control-flow decisions.
const std::string & Function::get_name | ( | ) | const |
Name of the function.
Returns the name of the function. The name should be globally unique already when it gets all the way down to the MIR, so any desired name-mangling should be done when creating the function. The back-end code-generators may assume that this is globally unique and already has the namespace and function arguments baked into the name.
const Type * Function::get_return_type | ( | ) | const |
Return type of the function.
This is the type of value that this function is declared to return.
const Gyoji::context::SourceReference & Function::get_source_ref | ( | ) | const |
Reference to the source location where the function is defined.
This returns a reference to where this function was defined. Since functions tend to be large, this reference points to where the function name is located in the source.
size_t Function::tmpvar_define | ( | const Type * | tmpvar_type | ) |
Define a new temporary variable with the given type.
Defines a new 'temporary' variable for use by opcodes as a 'register' for the MIR virtual machine.
size_t Function::tmpvar_duplicate | ( | size_t | tmpvar_id | ) |
Duplicate a temporary variable/reigster.
Duplicate a temporary variable with exactly the same type as another one. This is mainly a convenience for places where there isn't direct access to the type of a temporary variable, but we just want to duplicate the type from another one. Note that this does NOT duplicate the value inside a variable, but only the variable itself.
const Type * Function::tmpvar_get | ( | size_t | tmpvar_id | ) | const |
Temporary values used by opcodes.
Temporary values are the 'registers' of the abstract MIR virtual machine. They are typically used exactly once in order to be easy to reason about and ensure that the flow of the data can be reasoned about cleanly. Each temporary value will typically have a lifetime of only a few opcodes, being produced by one opcode and consumed by another one fairly soon afterward. Each temporary value has an ID and a type. The ID is a size_t and typically only