Gyoji Compiler
|
Basic block of a function. More...
#include <functions.hpp>
Public Member Functions | |
BasicBlock () | |
Create a new basic block. | |
~BasicBlock () | |
Move along, nothing to see here. | |
void | add_operation (Gyoji::owned< Operation > operation) |
Add an Operation to the block. | |
size_t | size () const |
Return the number of operations in this block. | |
void | insert_operation (size_t position, Gyoji::owned< Operation > operation) |
Insert operations at a specific point. | |
const std::vector< Gyoji::owned< Operation > > & | get_operations () const |
Access to list of Operation of basic block. | |
void | dump (FILE *out) const |
Dump block to file handle. | |
bool | contains_terminator () const |
Basic block of a function.
A basic block is an ordered list of operations that contain no flow-control except at the end which may return from the function, or transfer control to another basic block.
The operations in each basic block are primitive 'opcodes' that make up the instruction-set of the MIR representation.
Basic blocks are "flat" in the sense that they do not recursively contain other basic blocks. The process of 'lowering' in the front-end is responsible for "flattening" the program into a set of basic blocks containing primitive opcodes.
BasicBlock::BasicBlock | ( | ) |
Create a new basic block.
This constructs a new basic block. This is intended to be called only through the Function object with "add_block" (see Function). The newly created block is empty, but empty basic blocks are not permitted, so the caller is assumed to add operations to the block before MIR construction is complete.
BasicBlock::~BasicBlock | ( | ) |
Move along, nothing to see here.
Move along, nothing to see here.
void BasicBlock::add_operation | ( | Gyoji::owned< Operation > | operation | ) |
bool BasicBlock::contains_terminator | ( | ) | const |
This method returns true if the block already returns a 'terminating' instruction such as a branch, goto, or return.
void BasicBlock::dump | ( | FILE * | out | ) | const |
Dump block to file handle.
This method dumps the list of Operation for this basic block to the given file handle.
const std::vector< Gyoji::owned< Operation > > & BasicBlock::get_operations | ( | ) | const |
Access to list of Operation of basic block.
This method returns the list of operations defined in this basic block. The list is returned as an immutable reference to owned pointers. The caller may not aquire ownership through this method, but may get access to the functions by de-referencing the pointer. The pointer is guaranteed to be valid because this basic block owns them and they cannot be removed once created. The returned reference must not live longer than this container.
void BasicBlock::insert_operation | ( | size_t | position, |
Gyoji::owned< Operation > | operation | ||
) |
Insert operations at a specific point.
This inserts the given operation at the specific position in this block instead of appending it to the end. Note that any other insert points will need to have their position shifted down if other operations are required to be inserted.
This is mainly used to 'fix up' the 'goto' statement which needs to insert the 'undeclare' and possibly destructor calls before the goto operation in order to ensure that any variables that should be going out of scope are removed correctly.
Fortunately in the case of 'goto', this will always terminate the basic block, so we don't need to worry about shifting insert points because the next goto statement will always be in a different basic block.
size_t BasicBlock::size | ( | ) | const |
Return the number of operations in this block.
Returns the number of operations contained in this basic block. This is used to form a 'FunctionPoint' where we can insert operations at this point. This is used mainly by the 'goto' operation which needs to insert instructions that won't be determined until after the entire function has been processed, so we must 'fix up' the goto jump statement in order to 'unwind' variables declared in this scope that won't be available in the scope we're jumping to.