pub struct FuncTranslator { /* private fields */ }
Expand description

WebAssembly to Cranelift IR function translator.

A FuncTranslator is used to translate a binary WebAssembly function into Cranelift IR guided by a FuncEnvironment object. A single translator instance can be reused to translate multiple functions which will reduce heap allocation traffic.

Implementations§

Create a new translator.

Examples found in repository?
src/environ/dummy.rs (line 164)
161
162
163
164
165
166
167
168
169
170
171
    pub fn new(config: TargetFrontendConfig, debug_info: bool) -> Self {
        Self {
            info: DummyModuleInfo::new(config),
            trans: FuncTranslator::new(),
            func_bytecode_sizes: Vec::new(),
            debug_info,
            module_name: None,
            function_names: SecondaryMap::new(),
            expected_reachability: None,
        }
    }

Returns the underlying FunctionBuilderContext that this translator uses.

Translate a binary WebAssembly function.

The code slice contains the binary WebAssembly function code as it appears in the code section of a WebAssembly module, not including the initial size of the function code. The slice is expected to contain two parts:

  • The declaration of locals, and
  • The function body as an expression.

See the WebAssembly specification.

The Cranelift IR function func should be completely empty except for the func.signature and func.name fields. The signature may contain special-purpose arguments which are not regarded as WebAssembly local variables. Any signature arguments marked as ArgumentPurpose::Normal are made accessible as WebAssembly local variables.

Translate a binary WebAssembly function from a FunctionBody.

Examples found in repository?
src/func_translator.rs (lines 70-75)
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    pub fn translate<FE: FuncEnvironment + ?Sized>(
        &mut self,
        validator: &mut FuncValidator<impl WasmModuleResources>,
        code: &[u8],
        code_offset: usize,
        func: &mut ir::Function,
        environ: &mut FE,
    ) -> WasmResult<()> {
        self.translate_body(
            validator,
            FunctionBody::new(code_offset, code),
            func,
            environ,
        )
    }
More examples
Hide additional examples
src/environ/dummy.rs (line 861)
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
    fn define_function_body(
        &mut self,
        mut validator: FuncValidator<ValidatorResources>,
        body: FunctionBody<'data>,
    ) -> WasmResult<()> {
        self.func_bytecode_sizes
            .push(body.get_binary_reader().bytes_remaining());
        let func = {
            let mut func_environ =
                DummyFuncEnvironment::new(&self.info, self.expected_reachability.clone());
            let func_index =
                FuncIndex::new(self.get_num_func_imports() + self.info.function_bodies.len());

            let sig = func_environ.vmctx_sig(self.get_func_type(func_index));
            let mut func =
                ir::Function::with_name_signature(UserFuncName::user(0, func_index.as_u32()), sig);

            if self.debug_info {
                func.collect_debug_info();
            }

            self.trans
                .translate_body(&mut validator, body, &mut func, &mut func_environ)?;
            func
        };
        self.info.function_bodies.push(func);
        Ok(())
    }

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.