Gyoji Compiler
Loading...
Searching...
No Matches
Public Member Functions | List of all members
Gyoji::mir::Function Class Reference

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, bool _is_unsafe, const Gyoji::context::SourceReference &_source_ref)
 Creates a new function.
 
const std::stringget_name () const
 Name of the function.
 
const Typeget_return_type () const
 Return type of the function.
 
const std::vector< FunctionArgument > & get_arguments () const
 Arguments to the function.
 
const BasicBlockget_basic_block (size_t blockid) const
 Get an immutable basic block from the function 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::SourceReferenceget_source_ref () const
 Reference to the source location where the function is defined.
 
bool is_unsafe () const
 
const Typetmpvar_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.
 
void calculate_block_reachability ()
 
void iterate_operations (OperationVisitor &visitor) const
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ Function()

Function::Function ( std::string  _name,
const Type _return_type,
const std::vector< FunctionArgument > &  _arguments,
bool  _is_unsafe,
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.

Member Function Documentation

◆ add_block()

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.

◆ calculate_block_reachability()

void Function::calculate_block_reachability ( )

This must be done after all of the blocks and main control-flow have been added. This calculates whether blocks are reachable or not with the current control-flow graph. Early return in some cases may leave an empty block at the end and render a block unreachable. We need to know about this so we can make a good decision about whether to add a 'return' at the end of a void function or raise an error due to a missing return. At this point, we can also cull unreachable blocks if they're empty because they would have no effect on the generated code and there's no unreachable code to actually cause us to raise an error.

This will happen, for example, if you return from all branches of a switch or if/else, leaving the tail of the function empty.

◆ dump()

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.

◆ get_arguments()

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.

◆ get_basic_block()

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.

◆ get_blocks()

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.

◆ get_name()

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.

◆ get_return_type()

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.

◆ get_source_ref()

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.

◆ is_unsafe()

bool Function::is_unsafe ( ) const

Returns true if this function is declared as unsafe.

◆ iterate_operations()

void Function::iterate_operations ( OperationVisitor &  visitor) const

This method is used to iterate all of the operations inside all basic blocks of a funciton. The operations are visited in no particular order, but this does guarantee that all of the operations in all of the basic blocks will be visited exactly once.

◆ tmpvar_define()

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.

◆ tmpvar_duplicate()

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.

◆ tmpvar_get()

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


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