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.
88 lines
3.9 KiB
Rust
88 lines
3.9 KiB
Rust
//! All the runtime support necessary for the wasm to cretonne translation is formalized by the
|
|
//! trait `WasmRuntime`.
|
|
use cton_frontend::FunctionBuilder;
|
|
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: FuncEnvironment {
|
|
/// Declares a global to the runtime.
|
|
fn declare_global(&mut self, global: Global);
|
|
/// Declares a table to the runtime.
|
|
fn declare_table(&mut self, table: Table);
|
|
/// Fills a declared table with references to functions in the module.
|
|
fn declare_table_elements(
|
|
&mut self,
|
|
table_index: TableIndex,
|
|
offset: usize,
|
|
elements: &[FunctionIndex],
|
|
);
|
|
/// Declares a memory to the runtime
|
|
fn declare_memory(&mut self, memory: Memory);
|
|
/// Fills a declared memory with bytes at module instantiation.
|
|
fn declare_data_initialization(
|
|
&mut self,
|
|
memory_index: MemoryIndex,
|
|
offset: usize,
|
|
data: &[u8],
|
|
) -> Result<(), String>;
|
|
/// Call this function after having declared all the runtime elements but prior to the
|
|
/// function body translation.
|
|
fn begin_translation(&mut self);
|
|
/// Call this function between each function body translation.
|
|
fn next_function(&mut self);
|
|
/// 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;
|
|
/// 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>(
|
|
&self,
|
|
builder: &'a mut FunctionBuilder<Local>,
|
|
sig_ref: SigRef,
|
|
index_val: Value,
|
|
call_args: &[Value],
|
|
) -> &'a [Value];
|
|
}
|