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

@@ -7,7 +7,8 @@
//! The special case of the initialize expressions for table elements offsets or global variables
//! is handled, according to the semantics of WebAssembly, to only specific expressions that are
//! interpreted on the fly.
use crate::environ::{ModuleEnvironment, WasmResult, WasmTypesMap};
use crate::environ::{ModuleEnvironment, WasmResult};
use crate::state::ModuleTranslationState;
use crate::translation_utils::{
tabletype_to_type, type_to_type, FuncIndex, Global, GlobalIndex, GlobalInit, Memory,
MemoryIndex, SignatureIndex, Table, TableElementType, TableIndex,
@@ -29,11 +30,11 @@ use wasmparser::{
/// Parses the Type section of the wasm module.
pub fn parse_type_section(
types: TypeSectionReader,
wasm_types: &mut WasmTypesMap,
module_translation_state: &mut ModuleTranslationState,
environ: &mut dyn ModuleEnvironment,
) -> WasmResult<()> {
let count = types.get_count();
wasm_types.inner.reserve(count as usize);
module_translation_state.wasm_types.reserve(count as usize);
environ.reserve_signatures(count)?;
for entry in types {
@@ -55,7 +56,7 @@ pub fn parse_type_section(
AbiParam::new(cret_arg)
}));
environ.declare_signature(sig)?;
wasm_types.inner.push((params, returns));
module_translation_state.wasm_types.push((params, returns));
}
ty => {
return Err(wasm_unsupported!(
@@ -327,14 +328,14 @@ pub fn parse_element_section<'data>(
/// Parses the Code section of the wasm module.
pub fn parse_code_section<'data>(
code: CodeSectionReader<'data>,
wasm_types: &WasmTypesMap,
module_translation_state: &ModuleTranslationState,
environ: &mut dyn ModuleEnvironment<'data>,
) -> WasmResult<()> {
for body in code {
let mut reader = body?.get_binary_reader();
let size = reader.bytes_remaining();
let offset = reader.original_position();
environ.define_function_body(wasm_types, reader.read_bytes(size)?, offset)?;
environ.define_function_body(module_translation_state, reader.read_bytes(size)?, offset)?;
}
Ok(())
}