Struct cranelift_frontend::FunctionBuilder [−][src]
pub struct FunctionBuilder<'a> { pub func: &'a mut Function, // some fields omitted }
Temporary object used to build a single Cranelift IR Function
.
Fields
func: &'a mut Function
The function currently being built. This field is public so the function can be re-borrowed.
Methods
impl<'a> FunctionBuilder<'a>
[src]
impl<'a> FunctionBuilder<'a>
This module allows you to create a function in Cranelift IR in a straightforward way, hiding all the complexity of its internal representation.
The module is parametrized by one type which is the representation of variables in your
origin language. It offers a way to conveniently append instruction to your program flow.
You are responsible to split your instruction flow into extended blocks (declared with
create_ebb
) whose properties are:
- branch and jump instructions can only point at the top of extended blocks;
- the last instruction of each block is a terminator instruction which has no natural successor, and those instructions can only appear at the end of extended blocks.
The parameters of Cranelift IR instructions are Cranelift IR values, which can only be created
as results of other Cranelift IR instructions. To be able to create variables redefined multiple
times in your program, use the def_var
and use_var
command, that will maintain the
correspondence between your variables and Cranelift IR SSA values.
The first block for which you call switch_to_block
will be assumed to be the beginning of
the function.
At creation, a FunctionBuilder
instance borrows an already allocated Function
which it
modifies with the information stored in the mutable borrowed
FunctionBuilderContext
. The function passed in
argument should be newly created with
Function::with_name_signature()
, whereas the
FunctionBuilderContext
can be kept as is between two function translations.
Errors
The functions below will panic in debug mode whenever you try to modify the Cranelift IR
function in a way that violate the coherence of the code. For instance: switching to a new
Ebb
when you haven't filled the current one with a terminator instruction, inserting a
return instruction with arguments that don't match the function's signature.
pub fn new(
func: &'a mut Function,
func_ctx: &'a mut FunctionBuilderContext
) -> FunctionBuilder<'a>
[src]
pub fn new(
func: &'a mut Function,
func_ctx: &'a mut FunctionBuilderContext
) -> FunctionBuilder<'a>
Creates a new FunctionBuilder structure that will operate on a Function
using a
FunctionBuilderContext
.
pub fn set_srcloc(&mut self, srcloc: SourceLoc)
[src]
pub fn set_srcloc(&mut self, srcloc: SourceLoc)
Set the source location that should be assigned to all new instructions.
pub fn create_ebb(&mut self) -> Ebb
[src]
pub fn create_ebb(&mut self) -> Ebb
Creates a new Ebb
and returns its reference.
pub fn switch_to_block(&mut self, ebb: Ebb)
[src]
pub fn switch_to_block(&mut self, ebb: Ebb)
After the call to this function, new instructions will be inserted into the designated block, in the order they are declared. You must declare the types of the Ebb arguments you will use here.
When inserting the terminator instruction (which doesn't have a fallthrough to its immediate successor), the block will be declared filled and it will not be possible to append instructions to it.
pub fn seal_block(&mut self, ebb: Ebb)
[src]
pub fn seal_block(&mut self, ebb: Ebb)
Declares that all the predecessors of this block are known.
Function to call with ebb
as soon as the last branch instruction to ebb
has been
created. Forgetting to call this method on every block will cause inconsistencies in the
produced functions.
pub fn seal_all_blocks(&mut self)
[src]
pub fn seal_all_blocks(&mut self)
Effectively calls seal_block on all blocks in the function.
It's more efficient to seal Ebb
s as soon as possible, during
translation, but for frontends where this is impractical to do, this
function can be used at the end of translating all blocks to ensure
that everything is sealed.
pub fn declare_var(&mut self, var: Variable, ty: Type)
[src]
pub fn declare_var(&mut self, var: Variable, ty: Type)
In order to use a variable in a use_var
, you need to declare its type with this method.
pub fn use_var(&mut self, var: Variable) -> Value
[src]
pub fn use_var(&mut self, var: Variable) -> Value
Returns the Cranelift IR value corresponding to the utilization at the current program position of a previously defined user variable.
pub fn def_var(&mut self, var: Variable, val: Value)
[src]
pub fn def_var(&mut self, var: Variable, val: Value)
Register a new definition of a user variable. The type of the value must be the same as the type registered for the variable.
pub fn create_jump_table(&mut self, data: JumpTableData) -> JumpTable
[src]
pub fn create_jump_table(&mut self, data: JumpTableData) -> JumpTable
Creates a jump table in the function, to be used by br_table
instructions.
pub fn insert_jump_table_entry(&mut self, jt: JumpTable, index: usize, ebb: Ebb)
[src]
pub fn insert_jump_table_entry(&mut self, jt: JumpTable, index: usize, ebb: Ebb)
Inserts an entry in a previously declared jump table.
pub fn create_stack_slot(&mut self, data: StackSlotData) -> StackSlot
[src]
pub fn create_stack_slot(&mut self, data: StackSlotData) -> StackSlot
Creates a stack slot in the function, to be used by stack_load
, stack_store
and
stack_addr
instructions.
pub fn import_signature(&mut self, signature: Signature) -> SigRef
[src]
pub fn import_signature(&mut self, signature: Signature) -> SigRef
Adds a signature which can later be used to declare an external function import.
pub fn import_function(&mut self, data: ExtFuncData) -> FuncRef
[src]
pub fn import_function(&mut self, data: ExtFuncData) -> FuncRef
Declare an external function import.
pub fn create_global_value(&mut self, data: GlobalValueData) -> GlobalValue
[src]
pub fn create_global_value(&mut self, data: GlobalValueData) -> GlobalValue
Declares a global value accessible to the function.
pub fn create_heap(&mut self, data: HeapData) -> Heap
[src]
pub fn create_heap(&mut self, data: HeapData) -> Heap
Declares a heap accessible to the function.
pub fn ins<'short>(&'short mut self) -> FuncInstBuilder<'short, 'a>
[src]
pub fn ins<'short>(&'short mut self) -> FuncInstBuilder<'short, 'a>
Returns an object with the InstBuilder
trait that allows to conveniently append an instruction to the current Ebb
being built.
pub fn ensure_inserted_ebb(&mut self)
[src]
pub fn ensure_inserted_ebb(&mut self)
Make sure that the current EBB is inserted in the layout.
pub fn cursor(&mut self) -> FuncCursor
[src]
pub fn cursor(&mut self) -> FuncCursor
Returns a FuncCursor
pointed at the current position ready for inserting instructions.
This can be used to insert SSA code that doesn't need to access locals and that doesn't
need to know about FunctionBuilder
at all.
pub fn append_ebb_params_for_function_params(&mut self, ebb: Ebb)
[src]
pub fn append_ebb_params_for_function_params(&mut self, ebb: Ebb)
Append parameters to the given Ebb
corresponding to the function
parameters. This can be used to set up the ebb parameters for the
entry block.
pub fn append_ebb_params_for_function_returns(&mut self, ebb: Ebb)
[src]
pub fn append_ebb_params_for_function_returns(&mut self, ebb: Ebb)
Append parameters to the given Ebb
corresponding to the function
return values. This can be used to set up the ebb parameters for a
function exit block.
pub fn finalize(&mut self)
[src]
pub fn finalize(&mut self)
Declare that translation of the current function is complete. This
resets the state of the FunctionBuilder
in preparation to be used
for another function.
impl<'a> FunctionBuilder<'a>
[src]
impl<'a> FunctionBuilder<'a>
All the functions documented in the previous block are write-only and help you build a valid Cranelift IR functions via multiple debug asserts. However, you might need to improve the performance of your translation perform more complex transformations to your Cranelift IR function. The functions below help you inspect the function you're creating and modify it in ways that can be unsafe if used incorrectly.
pub fn ebb_params(&self, ebb: Ebb) -> &[Value]
[src]
pub fn ebb_params(&self, ebb: Ebb) -> &[Value]
Retrieves all the parameters for an Ebb
currently inferred from the jump instructions
inserted that target it and the SSA construction.
pub fn signature(&self, sigref: SigRef) -> Option<&Signature>
[src]
pub fn signature(&self, sigref: SigRef) -> Option<&Signature>
Retrieves the signature with reference sigref
previously added with import_signature
.
pub fn append_ebb_param(&mut self, ebb: Ebb, ty: Type) -> Value
[src]
pub fn append_ebb_param(&mut self, ebb: Ebb, ty: Type) -> Value
Creates a parameter for a specific Ebb
by appending it to the list of already existing
parameters.
Note: this function has to be called at the creation of the Ebb
before adding
instructions to it, otherwise this could interfere with SSA construction.
pub fn inst_results(&self, inst: Inst) -> &[Value]
[src]
pub fn inst_results(&self, inst: Inst) -> &[Value]
Returns the result values of an instruction.
pub fn change_jump_destination(&mut self, inst: Inst, new_dest: Ebb)
[src]
pub fn change_jump_destination(&mut self, inst: Inst, new_dest: Ebb)
Changes the destination of a jump instruction after creation.
Note: You are responsible for maintaining the coherence with the arguments of other jump instructions.
pub fn is_unreachable(&self) -> bool
[src]
pub fn is_unreachable(&self) -> bool
Returns true
if and only if the current Ebb
is sealed and has no predecessors declared.
The entry block of a function is never unreachable.
pub fn is_pristine(&self) -> bool
[src]
pub fn is_pristine(&self) -> bool
Returns true
if and only if no instructions have been added since the last call to
switch_to_block
.
pub fn is_filled(&self) -> bool
[src]
pub fn is_filled(&self) -> bool
Returns true
if and only if a terminator instruction has been inserted since the
last call to switch_to_block
.
pub fn display<'b, I: Into<Option<&'b TargetIsa>>>(
&'b self,
isa: I
) -> DisplayFunction
[src]
pub fn display<'b, I: Into<Option<&'b TargetIsa>>>(
&'b self,
isa: I
) -> DisplayFunction
Returns a displayable object for the function as it is.
Useful for debug purposes. Use it with None
for standard printing.
Auto Trait Implementations
impl<'a> Send for FunctionBuilder<'a>
impl<'a> Send for FunctionBuilder<'a>
impl<'a> Sync for FunctionBuilder<'a>
impl<'a> Sync for FunctionBuilder<'a>