winch_environ/
lib.rs

1//! This crate implements Winch's function compilation environment,
2//! which allows Winch's code generation to resolve module and runtime
3//! specific information.  This crate mainly implements the
4//! `winch_codegen::FuncEnv` trait.
5
6use wasmparser::types::Types;
7use wasmtime_environ::{FuncIndex, Module};
8use winch_codegen::{self, Callee, TargetIsa};
9
10/// Function environment containing module and runtime specific
11/// information.
12pub struct FuncEnv<'a> {
13    /// The translated WebAssembly module.
14    pub module: &'a Module,
15    /// Type information about a module, once it has been validated.
16    pub types: &'a Types,
17    /// The current ISA.
18    pub isa: &'a Box<dyn TargetIsa>,
19}
20
21impl<'a> winch_codegen::FuncEnv for FuncEnv<'a> {
22    fn callee_from_index(&self, index: u32) -> Callee {
23        let func = self
24            .types
25            .function_at(index)
26            .unwrap_or_else(|| panic!("function type at index: {}", index));
27
28        Callee {
29            ty: func.clone(),
30            import: self.module.is_imported_function(FuncIndex::from_u32(index)),
31            index,
32        }
33    }
34}
35
36impl<'a> FuncEnv<'a> {
37    /// Create a new function environment.
38    pub fn new(module: &'a Module, types: &'a Types, isa: &'a Box<dyn TargetIsa>) -> Self {
39        Self { module, types, isa }
40    }
41}