winch: Introduce winch-environ (#6017)

This commit introduces the `winch-environ` crate. This crate's responsibility is
to provide a shared implementatation of the `winch_codegen::FuncEnv` trait,
which is Winch's function compilation environment, used to resolve module and
runtime specific information needed by the code generation, such as resolving
all the details about a callee in a WebAssembly module, or resolving specific
information from the `VMContext`.

As of this change, the implementation only includes the necessary pieces to
resolve a function callee in a WebAssembly module. The idea is to evolve the
`winch_codegen::FuncEnv` trait as we evolve Winch's code generation.
This commit is contained in:
Saúl Cabrera
2023-03-14 15:59:15 -04:00
committed by GitHub
parent e4d9bb7c5a
commit 80bfb35072
12 changed files with 119 additions and 29 deletions

41
winch/environ/src/lib.rs Normal file
View File

@@ -0,0 +1,41 @@
//! This crate implements Winch's function compilation environment,
//! which allows Winch's code generation to resolve module and runtime
//! specific information. This crate mainly implements the
//! `winch_codegen::FuncEnv` trait.
use wasmparser::types::Types;
use wasmtime_environ::{FuncIndex, Module};
use winch_codegen::{self, Callee, TargetIsa};
/// Function environment containing module and runtime specific
/// information.
pub struct FuncEnv<'a> {
/// The translated WebAssembly module.
pub module: &'a Module,
/// Type information about a module, once it has been validated.
pub types: &'a Types,
/// The current ISA.
pub isa: &'a dyn TargetIsa,
}
impl<'a> winch_codegen::FuncEnv for FuncEnv<'a> {
fn callee_from_index(&self, index: u32) -> Callee {
let func = self
.types
.function_at(index)
.unwrap_or_else(|| panic!("function type at index: {}", index));
Callee {
ty: func.clone(),
import: self.module.is_imported_function(FuncIndex::from_u32(index)),
index,
}
}
}
impl<'a> FuncEnv<'a> {
/// Create a new function environment.
pub fn new(module: &'a Module, types: &'a Types, isa: &'a dyn TargetIsa) -> Self {
Self { module, types, isa }
}
}