Move module translation from cranelift to wasmtime (#3196)

The main purpose for doing this is that this is a large piece of
functionality used by Wasmtime which is entirely independent of
Cranelift. Eventually Wasmtime wants to be able to compile without
Cranelift, but it can't also depend on `cranelift-wasm` in that
situation for module translation which means that something needs to
happen. One option is to refactor what's in `cranelift-wasm` into a
separate crate (since all these pieces don't actually depend on
`cranelift-codegen`), but I personally chose to not do this because:

* The `ModuleEnvironment` trait, AFAIK, only has a primary user of
  Wasmtime. The Spidermonkey integration, for example, does not use this.

* This is an extra layer of abstraction between Wasmtime and the
  compilation phase which was a bit of a pain to maintain. It couldn't
  be Wasmtime-specific as it was part of Cranelift but at the same time
  it had lots of Wasmtime-centric functionality (such as module
  linking).

* Updating the "dummy" implementation has become pretty onerous over
  time as frequent additions are made and the "dummy" implementation was
  never actually used anywhere. This ended up feeling like effectively
  busy-work to update this.

For these reasons I've opted to to move the meat of `cranelift-wasm`
used by `wasmtime-environ` directly into `wasmtime-environ`. This means
that the only real meat that Wasmtime uses from `cranelift-wasm` is the
function-translation bits in the `wasmtime-cranelift` crate.

The changes in `wasmtime-environ` are largely to inline module parsing
together so it's a bit easier to follow instead of trying to connect
the dots between lots of various function calls.
This commit is contained in:
Alex Crichton
2021-08-18 12:15:02 -05:00
committed by GitHub
parent fde767fedc
commit 03a3a5939a
2 changed files with 832 additions and 624 deletions

View File

@@ -1,7 +1,7 @@
//! Helper functions and structures for the translation.
use crate::environ::{TargetEnvironment, WasmResult, WasmType};
use crate::wasm_unsupported;
use core::convert::TryInto;
use crate::{wasm_unsupported, WasmError};
use core::convert::{TryFrom, TryInto};
use core::u32;
use cranelift_codegen::entity::entity_impl;
use cranelift_codegen::ir;
@@ -195,6 +195,17 @@ pub enum GlobalInit {
Import,
}
impl Global {
/// Creates a new `Global` type from wasmparser's representation.
pub fn new(ty: wasmparser::GlobalType, initializer: GlobalInit) -> WasmResult<Global> {
Ok(Global {
wasm_ty: ty.content_type.try_into()?,
mutability: ty.mutable,
initializer,
})
}
}
/// WebAssembly table.
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
@@ -207,6 +218,18 @@ pub struct Table {
pub maximum: Option<u32>,
}
impl TryFrom<wasmparser::TableType> for Table {
type Error = WasmError;
fn try_from(ty: wasmparser::TableType) -> WasmResult<Table> {
Ok(Table {
wasm_ty: ty.element_type.try_into()?,
minimum: ty.initial,
maximum: ty.maximum,
})
}
}
/// WebAssembly table element. Can be a function or a scalar type.
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
@@ -231,6 +254,17 @@ pub struct Memory {
pub memory64: bool,
}
impl From<wasmparser::MemoryType> for Memory {
fn from(ty: wasmparser::MemoryType) -> Memory {
Memory {
minimum: ty.initial,
maximum: ty.maximum,
shared: ty.shared,
memory64: ty.memory64,
}
}
}
/// WebAssembly event.
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
@@ -239,6 +273,14 @@ pub struct Tag {
pub ty: TypeIndex,
}
impl From<wasmparser::TagType> for Tag {
fn from(ty: wasmparser::TagType) -> Tag {
Tag {
ty: TypeIndex::from_u32(ty.type_index),
}
}
}
/// Helper function translating wasmparser types to Cranelift types when possible.
pub fn type_to_type<PE: TargetEnvironment + ?Sized>(
ty: wasmparser::Type,