pub struct Function {
pub abi: Option<Abi>,
pub backtrace: Backtrace,
pub control: ControlStack,
pub env: Env,
pub locals: Locals,
pub masm: MacroAssembler,
pub table: JumpTable,
pub ty: FuncType,
pub is_main: bool,
}
Expand description
The code generation abstraction.
Fields§
§abi: Option<Abi>
Abi of this function,
backtrace: Backtrace
The backtrace.
control: ControlStack
Control stack frames.
env: Env
WASM environment.
locals: Locals
The defined locals for a function.
masm: MacroAssembler
The macro assembler.
table: JumpTable
The jump table.
ty: FuncType
The function type.
is_main: bool
If this function is the main function.
Implementations§
Source§impl Function
impl Function
Sourcepub fn new(
env: Env,
ty: FuncType,
abi: Option<Abi>,
is_main: bool,
) -> Result<Self>
pub fn new( env: Env, ty: FuncType, abi: Option<Abi>, is_main: bool, ) -> Result<Self>
Create a new code generator.
Sourcepub fn emit_locals(
&mut self,
locals: &mut LocalsReader<'_>,
validator: &mut FuncValidator<ValidatorResources>,
) -> Result<()>
pub fn emit_locals( &mut self, locals: &mut LocalsReader<'_>, validator: &mut FuncValidator<ValidatorResources>, ) -> Result<()>
Emit function locals
- the function parameters.
- function body locals.
NOTE: we don’t care about the original offset of the locals. bcz we will serialize the locals to an index map anyway.
Sourcepub fn emit_operators(
&mut self,
ops: &mut OperatorsReader<'_>,
validator: &mut FuncValidator<ValidatorResources>,
) -> Result<()>
pub fn emit_operators( &mut self, ops: &mut OperatorsReader<'_>, validator: &mut FuncValidator<ValidatorResources>, ) -> Result<()>
Emit function operators.
Source§impl Function
impl Function
Sourcepub fn _call_indirect(
&mut self,
_type_index: u32,
_table_index: u32,
_table_byte: u8,
) -> Result<()>
pub fn _call_indirect( &mut self, _type_index: u32, _table_index: u32, _table_byte: u8, ) -> Result<()>
The call indirect instruction calls a function indirectly
through an operand indexing into a table.
Sourcepub fn _call(&mut self, index: u32) -> Result<()>
pub fn _call(&mut self, index: u32) -> Result<()>
Calls a function specified by its index.
This function determines whether the function is an external import or an internal
function. If it is an external function, it will call the call_imported
method.
Otherwise, it will call the call_internal
method to handle the internal function call.
§Panics
If an attempt is made to call an external function internally, this function will panic.
Sourcefn call_internal(&mut self, index: u32) -> Result<()>
fn call_internal(&mut self, index: u32) -> Result<()>
Calls an internal function specified by its index.
This function handles the mechanics of calling an internal function, including:
- Checking for recursion and returning an error if detected.
- Recording the current program counter (PC) to manage the return address.
- Adjusting the stack to accommodate parameters and the return address.
- Storing parameters in memory and registering the call index in the jump table.
§Errors
Returns an error if recursion is detected or if the function index is invalid.
Sourcefn call_imported(&mut self, index: u32) -> Result<()>
fn call_imported(&mut self, index: u32) -> Result<()>
Calls an imported function specified by its index.
This function retrieves the imported function from the environment and executes it. It handles various host functions and ensures that the correct operations are performed based on the function type.
§Errors
Returns an error if the imported function is not found or if an unsupported host function is encountered.
Source§impl Function
impl Function
Sourcepub fn _if(&mut self, blockty: BlockType) -> Result<()>
pub fn _if(&mut self, blockty: BlockType) -> Result<()>
The beginning of an if construct with an implicit block.
Sourcepub fn _block(&mut self, blockty: BlockType) -> Result<()>
pub fn _block(&mut self, blockty: BlockType) -> Result<()>
The begeinning of a block construct. A sequence of instructions with a label at the end.
Sourcepub fn _loop(&mut self, blockty: BlockType) -> Result<()>
pub fn _loop(&mut self, blockty: BlockType) -> Result<()>
A block with a label which may be used to form loops.
Sourcepub fn _select(&mut self) -> Result<()>
pub fn _select(&mut self) -> Result<()>
The select instruction selects one of its first two operands based on whether its third oprand is zero or not.
STACK: [cond, val2, val1] -> [val1] if cond is non-zero, [val2] otherwise.
Sourcepub fn _br(&mut self, _depth: u32) -> Result<()>
pub fn _br(&mut self, _depth: u32) -> Result<()>
Branch to a given label in an enclosing construct.
Performs an unconditional branch.
Sourcepub fn _br_if(&mut self, depth: u32) -> Result<()>
pub fn _br_if(&mut self, depth: u32) -> Result<()>
Performs a conditional branch if i32 is non-zero.
Conditional branch to a given label in an enclosing construct.
Sourcepub fn _br_table(&mut self, _table: BrTable<'_>) -> Result<()>
pub fn _br_table(&mut self, _table: BrTable<'_>) -> Result<()>
A jump table which jumps to a label in an enclosing construct.
Performs an indirect branch through an operand indexing into the label vector that is an immediate to the instruction, or to the default target if the operand is out of bounds.
Sourcepub fn _end(&mut self) -> Result<()>
pub fn _end(&mut self) -> Result<()>
Handle the end of instructions for different situations.
- End of control flow operators.
- End of function.
- End of program.
Sourcepub fn _unreachable(&mut self) -> Result<()>
pub fn _unreachable(&mut self) -> Result<()>
Mark as invalid for now.
TODO: recheck this implementation, if it is okay, provide more docs.
Sourcepub(crate) fn handle_frame_popping(
&mut self,
frame: ControlStackFrame,
) -> Result<()>
pub(crate) fn handle_frame_popping( &mut self, frame: ControlStackFrame, ) -> Result<()>
Handle the popping of a frame.
TODO: validate stack IO for all frames (#59)
Source§impl Function
impl Function
Sourcepub fn _local_get(&mut self, local_index: u32) -> Result<()>
pub fn _local_get(&mut self, local_index: u32) -> Result<()>
This instruction gets the value of a variable.
Sourcepub fn _local_set(&mut self, local_index: u32) -> Result<()>
pub fn _local_set(&mut self, local_index: u32) -> Result<()>
This instruction sets the value of a variable.
Sourcepub fn _local_tee(&mut self, index: u32) -> Result<()>
pub fn _local_tee(&mut self, index: u32) -> Result<()>
This _local_tee is like _local_set, but it also returns the value on the stack.
Sourcepub fn _global_get(&mut self, _: u32) -> Result<()>
pub fn _global_get(&mut self, _: u32) -> Result<()>
This instruction gets the value of a variable.
Sourcepub fn _global_set(&mut self, _: u32) -> Result<()>
pub fn _global_set(&mut self, _: u32) -> Result<()>
This instruction sets the value of a variable.
Sourcefn _local_get_calldata(&mut self, local_index: usize) -> Result<()>
fn _local_get_calldata(&mut self, local_index: usize) -> Result<()>
Local get from calldata.
Sourcefn _local_get_var(&mut self, local_index: usize) -> Result<()>
fn _local_get_var(&mut self, local_index: usize) -> Result<()>
Local get for variables.