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

View File

@@ -4,10 +4,11 @@ use cranelift_codegen::settings;
use std::{fs, path::PathBuf, str::FromStr};
use target_lexicon::Triple;
use wasmtime_environ::{
wasmparser::{types::Types, Parser as WasmParser, Validator},
DefinedFuncIndex, FunctionBodyData, Module, ModuleEnvironment, Tunables,
wasmparser::{Parser as WasmParser, Validator},
DefinedFuncIndex, FunctionBodyData, ModuleEnvironment, Tunables,
};
use winch_codegen::{lookup, TargetIsa};
use winch_codegen::lookup;
use winch_environ::FuncEnv;
use winch_filetests::disasm::disasm;
#[derive(Parser, Debug)]
@@ -40,32 +41,30 @@ pub fn run(opt: &Options) -> Result<()> {
let body_inputs = std::mem::take(&mut translation.function_body_inputs);
let module = &translation.module;
let types = translation.get_types();
let env = FuncEnv::new(module, &types, &*isa);
body_inputs
.into_iter()
.try_for_each(|func| compile(&*isa, module, types, func))?;
.try_for_each(|func| compile(&env, func))?;
Ok(())
}
fn compile(
isa: &dyn TargetIsa,
module: &Module,
types: &Types,
f: (DefinedFuncIndex, FunctionBodyData<'_>),
) -> Result<()> {
let index = module.func_index(f.0);
let sig = types
fn compile(env: &FuncEnv, f: (DefinedFuncIndex, FunctionBodyData<'_>)) -> Result<()> {
let index = env.module.func_index(f.0);
let sig = env
.types
.function_at(index.as_u32())
.expect(&format!("function type at index {:?}", index.as_u32()));
let FunctionBodyData { body, validator } = f.1;
let validator = validator.into_validator(Default::default());
let buffer = isa
let buffer = env
.isa
.compile_function(&sig, &body, validator)
.expect("Couldn't compile function");
println!("Disassembly for function: {}", index.as_u32());
disasm(buffer.data(), isa)?
disasm(buffer.data(), env.isa)?
.iter()
.for_each(|s| println!("{}", s));