cranelift-wasm: support multi-value Wasm (#1049)

This commit introduces initial support for multi-value Wasm. Wasm blocks and
calls can now take and return an arbitrary number of values.

The encoding for multi-value blocks means that we need to keep the contents of
the "Types" section around when translating function bodies. To do this, we
introduce a `WasmTypesMap` type that maps the type indices to their parameters
and returns, construct it when parsing the "Types" section, and shepherd it
through a bunch of functions and methods when translating function bodies.
This commit is contained in:
Nick Fitzgerald
2019-10-02 12:40:35 -07:00
committed by GitHub
parent f9d802fb1d
commit 10be3e4ba8
30 changed files with 610 additions and 138 deletions

View File

@@ -15,6 +15,7 @@ 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;
@@ -103,6 +104,24 @@ 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 {
pub(crate) fn new() -> Self {
WasmTypesMap {
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
@@ -449,6 +468,7 @@ pub trait ModuleEnvironment<'data> {
/// functions is already provided by `reserve_func_types`.
fn define_function_body(
&mut self,
wasm_types: &WasmTypesMap,
body_bytes: &'data [u8],
body_offset: usize,
) -> WasmResult<()>;