cranelift-wasm: Create ModuleTranslationState and polish API a little (#1111)

* cranelift-wasm: replace `WasmTypesMap` with `ModuleTranslationState`

The `ModuleTranslationState` contains information decoded from the Wasm module
that must be referenced during each Wasm function's translation.

This is only for data that is maintained by `cranelift-wasm` itself, as opposed
to being maintained by the embedder. Data that is maintained by the embedder is
represented with `ModuleEnvironment`.

A `ModuleTranslationState` is returned by `translate_module`, and can then be
used when translating functions from that module.

* cranelift-wasm: rename `TranslationState` to `FuncTranslationState`

To disambiguate a bit with the new `ModuleTranslationState`.

* cranelift-wasm: Reorganize the internal `state` module into submodules

One module for the `ModuleTranslationState` and another for the
`FuncTranslationState`.

* cranelift-wasm: replace `FuncTranslator` with methods on `ModuleTranslationState`

`FuncTranslator` was two methods that always took ownership of `self`, so it
didn't really make sense as an object as opposed to two different functions, or
in this case methods on the object that actually persists for a longer time.

I think this improves ergonomics nicely.

Before:

```rust
let module_translation = translate_module(...)?;
for body in func_bodies {
    let mut translator = FuncTranslator::new();
    translator.translate(body, ...)?;
}
```

After:

```rust
let module_translation = translate_module(...)?;
for body in func_bodies {
    module_translation.translate_func(body, ...)?;
}
```

Note that this commit does not remove `FuncTranslator`. It still exists, but is
just a wrapper over the `ModuleTranslationState` methods, and it is marked
deprecated, so that downstream users get a heads up. This should make the
transition easier.

* Revert "cranelift-wasm: replace `FuncTranslator` with methods on `ModuleTranslationState`"

This reverts commit 075f9ae933bcaae39348b61287c8f78a4009340d.
This commit is contained in:
Nick Fitzgerald
2019-10-11 12:37:17 -07:00
committed by GitHub
parent b19f804ed5
commit ca53090f1b
12 changed files with 127 additions and 92 deletions

View File

@@ -6,7 +6,7 @@
//!
//! [Wasmtime]: https://github.com/CraneStation/wasmtime
use crate::state::TranslationState;
use crate::state::{FuncTranslationState, ModuleTranslationState};
use crate::translation_utils::{
FuncIndex, Global, GlobalIndex, Memory, MemoryIndex, SignatureIndex, Table, TableIndex,
};
@@ -15,7 +15,6 @@ use cranelift_codegen::cursor::FuncCursor;
use cranelift_codegen::ir::immediates::Offset32;
use cranelift_codegen::ir::{self, InstBuilder};
use cranelift_codegen::isa::TargetFrontendConfig;
use cranelift_entity::PrimaryMap;
use cranelift_frontend::FunctionBuilder;
use failure_derive::Fail;
use std::boxed::Box;
@@ -104,25 +103,6 @@ pub enum ReturnMode {
FallthroughReturn,
}
/// A map containing a Wasm module's original, raw signatures.
///
/// This is used for translating multi-value Wasm blocks inside functions, which
/// are encoded to refer to their type signature via index.
#[derive(Debug)]
pub struct WasmTypesMap {
pub(crate) inner:
PrimaryMap<SignatureIndex, (Box<[wasmparser::Type]>, Box<[wasmparser::Type]>)>,
}
impl WasmTypesMap {
/// Creates a new type map.
pub fn new() -> Self {
Self {
inner: PrimaryMap::new(),
}
}
}
/// Environment affecting the translation of a single WebAssembly function.
///
/// A `FuncEnvironment` trait object is required to translate a WebAssembly function to Cranelift
@@ -301,7 +281,7 @@ pub trait FuncEnvironment {
&mut self,
_op: &Operator,
_builder: &mut FunctionBuilder,
_state: &TranslationState,
_state: &FuncTranslationState,
) -> WasmResult<()> {
Ok(())
}
@@ -312,7 +292,7 @@ pub trait FuncEnvironment {
&mut self,
_op: &Operator,
_builder: &mut FunctionBuilder,
_state: &TranslationState,
_state: &FuncTranslationState,
) -> WasmResult<()> {
Ok(())
}
@@ -469,7 +449,7 @@ pub trait ModuleEnvironment<'data> {
/// functions is already provided by `reserve_func_types`.
fn define_function_body(
&mut self,
wasm_types: &WasmTypesMap,
module_translation_state: &ModuleTranslationState,
body_bytes: &'data [u8],
body_offset: usize,
) -> WasmResult<()>;