Add FuncEnvironment trait.

This trait is used to provide the environment necessary to translate a
single WebAssembly function without having other global data structures
for the WebAssembly module.

The WasmRuntime trait extends the FuncEnvironment trait for those uses
that want to parse a whole WebAssembly module.

- Change the handling of WebAssembly globals to use the FuncEnvironment
  trait as well as the new GlobalVar infrastructure in Cretonne. The
  runtime is not consulted on the translation of each
  get_global/get_global instruction. Instead it gets to create the
  GlobalVar declaration in the function preamble the first time the
  global is used.

- Change the handling of heap load/store instructions to use the new
  Heap infrastructure in Cretonne. The runtime is called to create the
  Heap declaration in the preamble. It is not involved in individual
  load/store instructions.
This commit is contained in:
Jakob Stoklund Olesen
2017-09-05 13:11:35 -07:00
parent 19c8ba5021
commit 0ac1d0dd94
6 changed files with 240 additions and 190 deletions

View File

@@ -1,14 +1,51 @@
//! All the runtime support necessary for the wasm to cretonne translation is formalized by the
//! trait `WasmRuntime`.
use cton_frontend::FunctionBuilder;
use cretonne::ir::{Value, SigRef};
use cretonne::ir::{self, Value, SigRef};
use translation_utils::{Local, FunctionIndex, TableIndex, GlobalIndex, MemoryIndex, Global, Table,
Memory};
/// The value of a WebAssembly global variable.
#[derive(Clone, Copy)]
pub enum GlobalValue {
/// This is a constant global with a value known at compile time.
Const(ir::Value),
/// This is a variable in memory that should be referenced as a `GlobalVar`.
Memory { gv: ir::GlobalVar, ty: ir::Type },
}
/// Environment affecting the translation of a single WebAssembly function.
///
/// A `FuncEnvironment` trait object is required to translate a WebAssembly function to Cretonne
/// IL. The function environment provides information about the WebAssembly module as well as the
/// runtime environment.
pub trait FuncEnvironment {
/// Get the Cretonne integer type to use for native pointers.
///
/// This should be `I64` for 64-bit architectures and `I32` for 32-bit architectures.
fn native_pointer(&self) -> ir::Type;
/// Set up the necessary preamble definitions in `func` to access the global variable
/// identified by `index`.
///
/// The index space covers both imported globals and globals defined by the module.
///
/// Return the global variable reference that should be used to access the global and the
/// WebAssembly type of the global.
fn make_global(&self, func: &mut ir::Function, index: GlobalIndex) -> GlobalValue;
/// Set up the necessary preamble definitions in `func` to access the linear memory identified
/// by `index`.
///
/// The index space covers both imported and locally declared memories.
fn make_heap(&self, func: &mut ir::Function, index: MemoryIndex) -> ir::Heap;
}
/// An object satisfyng the `WasmRuntime` trait can be passed as argument to the
/// [`translate_module`](fn.translate_module.html) function. These methods should not be called
/// by the user, they are only for the `wasm2cretonne` internal use.
pub trait WasmRuntime {
pub trait WasmRuntime: FuncEnvironment {
/// Declares a global to the runtime.
fn declare_global(&mut self, global: Global);
/// Declares a table to the runtime.
@@ -34,29 +71,10 @@ pub trait WasmRuntime {
fn begin_translation(&mut self);
/// Call this function between each function body translation.
fn next_function(&mut self);
/// Translates a `get_global` wasm instruction.
fn translate_get_global(
&self,
builder: &mut FunctionBuilder<Local>,
global_index: GlobalIndex,
) -> Value;
/// Translates a `set_global` wasm instruction.
fn translate_set_global(
&self,
builder: &mut FunctionBuilder<Local>,
global_index: GlobalIndex,
val: Value,
);
/// Translates a `grow_memory` wasm instruction. Returns the old size (in pages) of the memory.
fn translate_grow_memory(&mut self, builder: &mut FunctionBuilder<Local>, val: Value) -> Value;
/// Translates a `current_memory` wasm instruction. Returns the size in pages of the memory.
fn translate_current_memory(&mut self, builder: &mut FunctionBuilder<Local>) -> Value;
/// Returns the base address of a wasm memory as a Cretonne `Value`.
fn translate_memory_base_address(
&self,
builder: &mut FunctionBuilder<Local>,
index: MemoryIndex,
) -> Value;
/// Translates a `call_indirect` wasm instruction. It involves looking up the value contained
/// it the table at location `index_val` and calling the corresponding function.
fn translate_call_indirect<'a>(