Initial support for function, table, memory, and global imports.
This commit is contained in:
@@ -6,9 +6,12 @@ use cranelift_codegen::ir;
|
||||
use cranelift_codegen::ir::ExternalName;
|
||||
use cranelift_codegen::isa;
|
||||
use cranelift_codegen::{CodegenError, Context};
|
||||
use cranelift_entity::{EntityRef, PrimaryMap};
|
||||
use cranelift_entity::PrimaryMap;
|
||||
use cranelift_wasm::{DefinedFuncIndex, FuncIndex, FuncTranslator, WasmError};
|
||||
use environ::{get_func_name, get_memory_grow_name, get_memory_size_name, FuncEnvironment};
|
||||
use func_environ::{
|
||||
get_func_name, get_imported_memory32_grow_name, get_imported_memory32_size_name,
|
||||
get_memory32_grow_name, get_memory32_size_name, FuncEnvironment,
|
||||
};
|
||||
use module::Module;
|
||||
use std::vec::Vec;
|
||||
|
||||
@@ -49,13 +52,17 @@ impl binemit::RelocSink for RelocSink {
|
||||
name: &ExternalName,
|
||||
addend: binemit::Addend,
|
||||
) {
|
||||
let reloc_target = if *name == get_memory_grow_name() {
|
||||
RelocationTarget::MemoryGrow
|
||||
} else if *name == get_memory_size_name() {
|
||||
RelocationTarget::MemorySize
|
||||
let reloc_target = if *name == get_memory32_grow_name() {
|
||||
RelocationTarget::Memory32Grow
|
||||
} else if *name == get_imported_memory32_grow_name() {
|
||||
RelocationTarget::ImportedMemory32Grow
|
||||
} else if *name == get_memory32_size_name() {
|
||||
RelocationTarget::Memory32Size
|
||||
} else if *name == get_imported_memory32_size_name() {
|
||||
RelocationTarget::ImportedMemory32Size
|
||||
} else if let ExternalName::User { namespace, index } = *name {
|
||||
debug_assert!(namespace == 0);
|
||||
RelocationTarget::UserFunc(FuncIndex::new(index as usize))
|
||||
RelocationTarget::UserFunc(FuncIndex::from_u32(index))
|
||||
} else if let ExternalName::LibCall(libcall) = *name {
|
||||
RelocationTarget::LibCall(libcall)
|
||||
} else {
|
||||
@@ -107,10 +114,14 @@ pub enum RelocationTarget {
|
||||
UserFunc(FuncIndex),
|
||||
/// A compiler-generated libcall.
|
||||
LibCall(ir::LibCall),
|
||||
/// Function for growing the default memory by the specified amount of pages.
|
||||
MemoryGrow,
|
||||
/// Function for query current size of the default linear memory.
|
||||
MemorySize,
|
||||
/// Function for growing a locally-defined 32-bit memory by the specified amount of pages.
|
||||
Memory32Grow,
|
||||
/// Function for growing an imported 32-bit memory by the specified amount of pages.
|
||||
ImportedMemory32Grow,
|
||||
/// Function for query current size of a locally-defined 32-bit linear memory.
|
||||
Memory32Size,
|
||||
/// Function for query current size of an imported 32-bit linear memory.
|
||||
ImportedMemory32Size,
|
||||
}
|
||||
|
||||
/// Relocations to apply to function bodies.
|
||||
|
||||
@@ -1,721 +0,0 @@
|
||||
use cast;
|
||||
use cranelift_codegen::cursor::FuncCursor;
|
||||
use cranelift_codegen::ir;
|
||||
use cranelift_codegen::ir::condcodes::*;
|
||||
use cranelift_codegen::ir::immediates::{Offset32, Uimm64};
|
||||
use cranelift_codegen::ir::types::*;
|
||||
use cranelift_codegen::ir::{
|
||||
AbiParam, ArgumentPurpose, ExtFuncData, FuncRef, Function, InstBuilder, Signature,
|
||||
};
|
||||
use cranelift_codegen::isa;
|
||||
use cranelift_entity::EntityRef;
|
||||
use cranelift_wasm::{
|
||||
self, translate_module, FuncIndex, Global, GlobalIndex, GlobalVariable, Memory, MemoryIndex,
|
||||
SignatureIndex, Table, TableIndex, WasmResult,
|
||||
};
|
||||
use module::{
|
||||
DataInitializer, Export, LazyContents, MemoryPlan, MemoryStyle, Module, TableElements,
|
||||
TablePlan, TableStyle,
|
||||
};
|
||||
use std::clone::Clone;
|
||||
use std::string::String;
|
||||
use std::vec::Vec;
|
||||
use tunables::Tunables;
|
||||
use vmoffsets::VMOffsets;
|
||||
use WASM_PAGE_SIZE;
|
||||
|
||||
/// Compute a `ir::ExternalName` for a given wasm function index.
|
||||
pub fn get_func_name(func_index: FuncIndex) -> ir::ExternalName {
|
||||
ir::ExternalName::user(0, func_index.as_u32())
|
||||
}
|
||||
|
||||
/// Compute a `ir::ExternalName` for the `memory.grow` libcall.
|
||||
pub fn get_memory_grow_name() -> ir::ExternalName {
|
||||
ir::ExternalName::user(1, 0)
|
||||
}
|
||||
|
||||
/// Compute a `ir::ExternalName` for the `memory.size` libcall.
|
||||
pub fn get_memory_size_name() -> ir::ExternalName {
|
||||
ir::ExternalName::user(1, 1)
|
||||
}
|
||||
|
||||
/// Object containing the standalone environment information. To be passed after creation as
|
||||
/// argument to `compile_module`.
|
||||
pub struct ModuleEnvironment<'data, 'module> {
|
||||
/// Compilation setting flags.
|
||||
isa: &'module isa::TargetIsa,
|
||||
|
||||
/// Module information.
|
||||
module: &'module mut Module,
|
||||
|
||||
/// References to information to be decoded later.
|
||||
lazy: LazyContents<'data>,
|
||||
|
||||
/// Tunable parameters.
|
||||
tunables: Tunables,
|
||||
}
|
||||
|
||||
impl<'data, 'module> ModuleEnvironment<'data, 'module> {
|
||||
/// Allocates the enironment data structures with the given isa.
|
||||
pub fn new(
|
||||
isa: &'module isa::TargetIsa,
|
||||
module: &'module mut Module,
|
||||
tunables: Tunables,
|
||||
) -> Self {
|
||||
Self {
|
||||
isa,
|
||||
module,
|
||||
lazy: LazyContents::new(),
|
||||
tunables,
|
||||
}
|
||||
}
|
||||
|
||||
fn pointer_type(&self) -> ir::Type {
|
||||
self.isa.frontend_config().pointer_type()
|
||||
}
|
||||
|
||||
/// Translate the given wasm module data using this environment. This consumes the
|
||||
/// `ModuleEnvironment` with its mutable reference to the `Module` and produces a
|
||||
/// `ModuleTranslation` with an immutable reference to the `Module` (which has
|
||||
/// become fully populated).
|
||||
pub fn translate(mut self, data: &'data [u8]) -> WasmResult<ModuleTranslation<'data, 'module>> {
|
||||
translate_module(data, &mut self)?;
|
||||
|
||||
Ok(ModuleTranslation {
|
||||
isa: self.isa,
|
||||
module: self.module,
|
||||
lazy: self.lazy,
|
||||
tunables: self.tunables,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// The FuncEnvironment implementation for use by the `ModuleEnvironment`.
|
||||
pub struct FuncEnvironment<'module_environment> {
|
||||
/// Compilation setting flags.
|
||||
isa: &'module_environment isa::TargetIsa,
|
||||
|
||||
/// The module-level environment which this function-level environment belongs to.
|
||||
module: &'module_environment Module,
|
||||
|
||||
/// The Cranelift global holding the vmctx address.
|
||||
vmctx: Option<ir::GlobalValue>,
|
||||
|
||||
/// The Cranelift global holding the base address of the memories vector.
|
||||
memories_base: Option<ir::GlobalValue>,
|
||||
|
||||
/// The Cranelift global holding the base address of the tables vector.
|
||||
tables_base: Option<ir::GlobalValue>,
|
||||
|
||||
/// The Cranelift global holding the base address of the globals vector.
|
||||
globals_base: Option<ir::GlobalValue>,
|
||||
|
||||
/// The Cranelift global holding the base address of the signature IDs vector.
|
||||
signature_ids_base: Option<ir::GlobalValue>,
|
||||
|
||||
/// The external function declaration for implementing wasm's `memory.size`.
|
||||
memory_size_extfunc: Option<FuncRef>,
|
||||
|
||||
/// The external function declaration for implementing wasm's `memory.grow`.
|
||||
memory_grow_extfunc: Option<FuncRef>,
|
||||
|
||||
/// Offsets to struct fields accessed by JIT code.
|
||||
offsets: VMOffsets,
|
||||
}
|
||||
|
||||
impl<'module_environment> FuncEnvironment<'module_environment> {
|
||||
pub fn new(
|
||||
isa: &'module_environment isa::TargetIsa,
|
||||
module: &'module_environment Module,
|
||||
) -> Self {
|
||||
Self {
|
||||
isa,
|
||||
module,
|
||||
vmctx: None,
|
||||
memories_base: None,
|
||||
tables_base: None,
|
||||
globals_base: None,
|
||||
signature_ids_base: None,
|
||||
memory_size_extfunc: None,
|
||||
memory_grow_extfunc: None,
|
||||
offsets: VMOffsets::new(isa.pointer_bytes()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Transform the call argument list in preparation for making a call.
|
||||
fn get_real_call_args(func: &Function, call_args: &[ir::Value]) -> Vec<ir::Value> {
|
||||
let mut real_call_args = Vec::with_capacity(call_args.len() + 1);
|
||||
real_call_args.extend_from_slice(call_args);
|
||||
real_call_args.push(func.special_param(ArgumentPurpose::VMContext).unwrap());
|
||||
real_call_args
|
||||
}
|
||||
|
||||
fn vmctx(&mut self, func: &mut Function) -> ir::GlobalValue {
|
||||
self.vmctx.unwrap_or_else(|| {
|
||||
let vmctx = func.create_global_value(ir::GlobalValueData::VMContext);
|
||||
self.vmctx = Some(vmctx);
|
||||
vmctx
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// This trait is useful for `translate_module` because it tells how to translate
|
||||
/// enironment-dependent wasm instructions. These functions should not be called by the user.
|
||||
impl<'data, 'module> cranelift_wasm::ModuleEnvironment<'data>
|
||||
for ModuleEnvironment<'data, 'module>
|
||||
{
|
||||
fn target_config(&self) -> isa::TargetFrontendConfig {
|
||||
self.isa.frontend_config()
|
||||
}
|
||||
|
||||
fn declare_signature(&mut self, sig: &ir::Signature) {
|
||||
let sig = translate_signature(sig.clone(), self.pointer_type());
|
||||
// TODO: Deduplicate signatures.
|
||||
self.module.signatures.push(sig);
|
||||
}
|
||||
|
||||
fn get_signature(&self, sig_index: SignatureIndex) -> &ir::Signature {
|
||||
&self.module.signatures[sig_index]
|
||||
}
|
||||
|
||||
fn declare_func_import(&mut self, sig_index: SignatureIndex, module: &str, field: &str) {
|
||||
debug_assert_eq!(
|
||||
self.module.functions.len(),
|
||||
self.module.imported_funcs.len(),
|
||||
"Imported functions must be declared first"
|
||||
);
|
||||
self.module.functions.push(sig_index);
|
||||
|
||||
self.module
|
||||
.imported_funcs
|
||||
.push((String::from(module), String::from(field)));
|
||||
}
|
||||
|
||||
fn get_num_func_imports(&self) -> usize {
|
||||
self.module.imported_funcs.len()
|
||||
}
|
||||
|
||||
fn declare_func_type(&mut self, sig_index: SignatureIndex) {
|
||||
self.module.functions.push(sig_index);
|
||||
}
|
||||
|
||||
fn get_func_type(&self, func_index: FuncIndex) -> SignatureIndex {
|
||||
self.module.functions[func_index]
|
||||
}
|
||||
|
||||
fn declare_global_import(&mut self, global: Global, module: &str, field: &str) {
|
||||
debug_assert_eq!(
|
||||
self.module.globals.len(),
|
||||
self.module.imported_globals.len(),
|
||||
"Imported globals must be declared first"
|
||||
);
|
||||
self.module.globals.push(global);
|
||||
|
||||
self.module
|
||||
.imported_globals
|
||||
.push((String::from(module), String::from(field)));
|
||||
}
|
||||
|
||||
fn declare_global(&mut self, global: Global) {
|
||||
self.module.globals.push(global);
|
||||
}
|
||||
|
||||
fn get_global(&self, global_index: GlobalIndex) -> &Global {
|
||||
&self.module.globals[global_index]
|
||||
}
|
||||
|
||||
fn declare_table_import(&mut self, table: Table, module: &str, field: &str) {
|
||||
debug_assert_eq!(
|
||||
self.module.table_plans.len(),
|
||||
self.module.imported_tables.len(),
|
||||
"Imported tables must be declared first"
|
||||
);
|
||||
let plan = TablePlan::for_table(table, &self.tunables);
|
||||
self.module.table_plans.push(plan);
|
||||
|
||||
self.module
|
||||
.imported_tables
|
||||
.push((String::from(module), String::from(field)));
|
||||
}
|
||||
|
||||
fn declare_table(&mut self, table: Table) {
|
||||
let plan = TablePlan::for_table(table, &self.tunables);
|
||||
self.module.table_plans.push(plan);
|
||||
}
|
||||
|
||||
fn declare_table_elements(
|
||||
&mut self,
|
||||
table_index: TableIndex,
|
||||
base: Option<GlobalIndex>,
|
||||
offset: usize,
|
||||
elements: Vec<FuncIndex>,
|
||||
) {
|
||||
self.module.table_elements.push(TableElements {
|
||||
table_index,
|
||||
base,
|
||||
offset,
|
||||
elements,
|
||||
});
|
||||
}
|
||||
|
||||
fn declare_memory_import(&mut self, memory: Memory, module: &str, field: &str) {
|
||||
debug_assert_eq!(
|
||||
self.module.memory_plans.len(),
|
||||
self.module.imported_memories.len(),
|
||||
"Imported memories must be declared first"
|
||||
);
|
||||
let plan = MemoryPlan::for_memory(memory, &self.tunables);
|
||||
self.module.memory_plans.push(plan);
|
||||
|
||||
self.module
|
||||
.imported_memories
|
||||
.push((String::from(module), String::from(field)));
|
||||
}
|
||||
|
||||
fn declare_memory(&mut self, memory: Memory) {
|
||||
let plan = MemoryPlan::for_memory(memory, &self.tunables);
|
||||
self.module.memory_plans.push(plan);
|
||||
}
|
||||
|
||||
fn declare_data_initialization(
|
||||
&mut self,
|
||||
memory_index: MemoryIndex,
|
||||
base: Option<GlobalIndex>,
|
||||
offset: usize,
|
||||
data: &'data [u8],
|
||||
) {
|
||||
self.lazy.data_initializers.push(DataInitializer {
|
||||
memory_index,
|
||||
base,
|
||||
offset,
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
fn declare_func_export(&mut self, func_index: FuncIndex, name: &str) {
|
||||
self.module
|
||||
.exports
|
||||
.insert(String::from(name), Export::Function(func_index));
|
||||
}
|
||||
|
||||
fn declare_table_export(&mut self, table_index: TableIndex, name: &str) {
|
||||
self.module
|
||||
.exports
|
||||
.insert(String::from(name), Export::Table(table_index));
|
||||
}
|
||||
|
||||
fn declare_memory_export(&mut self, memory_index: MemoryIndex, name: &str) {
|
||||
self.module
|
||||
.exports
|
||||
.insert(String::from(name), Export::Memory(memory_index));
|
||||
}
|
||||
|
||||
fn declare_global_export(&mut self, global_index: GlobalIndex, name: &str) {
|
||||
self.module
|
||||
.exports
|
||||
.insert(String::from(name), Export::Global(global_index));
|
||||
}
|
||||
|
||||
fn declare_start_func(&mut self, func_index: FuncIndex) {
|
||||
debug_assert!(self.module.start_func.is_none());
|
||||
self.module.start_func = Some(func_index);
|
||||
}
|
||||
|
||||
fn define_function_body(&mut self, body_bytes: &'data [u8]) -> WasmResult<()> {
|
||||
self.lazy.function_body_inputs.push(body_bytes);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'module_environment> {
|
||||
fn target_config(&self) -> isa::TargetFrontendConfig {
|
||||
self.isa.frontend_config()
|
||||
}
|
||||
|
||||
fn make_global(&mut self, func: &mut ir::Function, index: GlobalIndex) -> GlobalVariable {
|
||||
let pointer_type = self.pointer_type();
|
||||
|
||||
let vmctx = self.vmctx(func);
|
||||
let mut globals_base = self.globals_base.unwrap_or_else(|| {
|
||||
let new_base = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
offset: Offset32::new(i32::from(self.offsets.vmctx_globals())),
|
||||
global_type: pointer_type,
|
||||
readonly: true,
|
||||
});
|
||||
self.globals_base = Some(new_base);
|
||||
new_base
|
||||
});
|
||||
let mut offset = self.offsets.index_vmglobal(index.as_u32());
|
||||
|
||||
// For imported memories, the `VMGlobal` array contains a pointer to
|
||||
// the `VMGlobalDefinition` rather than containing the `VMGlobalDefinition`
|
||||
// inline, so we do an extra indirection.
|
||||
if self.module.is_imported_global(index) {
|
||||
globals_base = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: globals_base,
|
||||
offset: Offset32::new(self.offsets.index_vmglobal_import_from(index.as_u32())),
|
||||
global_type: pointer_type,
|
||||
readonly: true,
|
||||
});
|
||||
offset = self.offsets.index_vmglobal(0);
|
||||
}
|
||||
|
||||
GlobalVariable::Memory {
|
||||
gv: globals_base,
|
||||
offset: offset.into(),
|
||||
ty: self.module.globals[index].ty,
|
||||
}
|
||||
}
|
||||
|
||||
fn make_heap(&mut self, func: &mut ir::Function, index: MemoryIndex) -> ir::Heap {
|
||||
let pointer_type = self.pointer_type();
|
||||
|
||||
let vmctx = self.vmctx(func);
|
||||
let mut memories_base = self.memories_base.unwrap_or_else(|| {
|
||||
let new_base = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
offset: Offset32::new(i32::from(self.offsets.vmctx_memories())),
|
||||
global_type: pointer_type,
|
||||
readonly: true,
|
||||
});
|
||||
self.memories_base = Some(new_base);
|
||||
new_base
|
||||
});
|
||||
let mut base_offset = self.offsets.index_vmmemory_definition_base(index.as_u32());
|
||||
let mut current_length_offset = self
|
||||
.offsets
|
||||
.index_vmmemory_definition_current_length(index.as_u32());
|
||||
|
||||
// For imported memories, the `VMMemory` array contains a pointer to
|
||||
// the `VMMemoryDefinition` rather than containing the `VMMemoryDefinition`
|
||||
// inline, so we do an extra indirection.
|
||||
if self.module.is_imported_memory(index) {
|
||||
memories_base = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: memories_base,
|
||||
offset: Offset32::new(self.offsets.index_vmmemory_import_from(index.as_u32())),
|
||||
global_type: pointer_type,
|
||||
readonly: true,
|
||||
});
|
||||
base_offset = self.offsets.index_vmmemory_definition_base(0);
|
||||
current_length_offset = self.offsets.index_vmmemory_definition_current_length(0);
|
||||
}
|
||||
|
||||
// If we have a declared maximum, we can make this a "static" heap, which is
|
||||
// allocated up front and never moved.
|
||||
let (offset_guard_size, heap_style, readonly_base) = match self.module.memory_plans[index] {
|
||||
MemoryPlan {
|
||||
memory: _,
|
||||
style: MemoryStyle::Dynamic,
|
||||
offset_guard_size,
|
||||
} => {
|
||||
let heap_bound = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: memories_base,
|
||||
offset: Offset32::new(current_length_offset),
|
||||
global_type: I32,
|
||||
readonly: false,
|
||||
});
|
||||
(
|
||||
Uimm64::new(offset_guard_size),
|
||||
ir::HeapStyle::Dynamic {
|
||||
bound_gv: heap_bound,
|
||||
},
|
||||
false,
|
||||
)
|
||||
}
|
||||
MemoryPlan {
|
||||
memory: _,
|
||||
style: MemoryStyle::Static { bound },
|
||||
offset_guard_size,
|
||||
} => (
|
||||
Uimm64::new(offset_guard_size),
|
||||
ir::HeapStyle::Static {
|
||||
bound: Uimm64::new(u64::from(bound) * u64::from(WASM_PAGE_SIZE)),
|
||||
},
|
||||
true,
|
||||
),
|
||||
};
|
||||
|
||||
let heap_base = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: memories_base,
|
||||
offset: Offset32::new(base_offset),
|
||||
global_type: pointer_type,
|
||||
readonly: readonly_base,
|
||||
});
|
||||
func.create_heap(ir::HeapData {
|
||||
base: heap_base,
|
||||
min_size: 0.into(),
|
||||
offset_guard_size,
|
||||
style: heap_style,
|
||||
index_type: I32,
|
||||
})
|
||||
}
|
||||
|
||||
fn make_table(&mut self, func: &mut ir::Function, index: TableIndex) -> ir::Table {
|
||||
let pointer_type = self.pointer_type();
|
||||
|
||||
let vmctx = self.vmctx(func);
|
||||
let mut tables_base = self.tables_base.unwrap_or_else(|| {
|
||||
let new_base = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
offset: Offset32::new(i32::from(self.offsets.vmctx_tables())),
|
||||
global_type: pointer_type,
|
||||
readonly: true,
|
||||
});
|
||||
self.tables_base = Some(new_base);
|
||||
new_base
|
||||
});
|
||||
let mut base_offset = self.offsets.index_vmtable_definition_base(index.as_u32());
|
||||
let mut current_elements_offset = self
|
||||
.offsets
|
||||
.index_vmtable_definition_current_elements(index.as_u32());
|
||||
|
||||
// For imported tables, the `VMTable` array contains a pointer to
|
||||
// the `VMTableDefinition` rather than containing the `VMTableDefinition`
|
||||
// inline, so we do an extra indirection.
|
||||
if self.module.is_imported_table(index) {
|
||||
tables_base = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: tables_base,
|
||||
offset: Offset32::new(self.offsets.index_vmtable_import_from(index.as_u32())),
|
||||
global_type: pointer_type,
|
||||
readonly: true,
|
||||
});
|
||||
base_offset = self.offsets.index_vmtable_definition_base(0);
|
||||
current_elements_offset = self.offsets.index_vmtable_definition_current_elements(0);
|
||||
}
|
||||
|
||||
let base_gv = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: tables_base,
|
||||
offset: Offset32::new(base_offset),
|
||||
global_type: pointer_type,
|
||||
readonly: false,
|
||||
});
|
||||
let bound_gv = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: tables_base,
|
||||
offset: Offset32::new(current_elements_offset),
|
||||
global_type: I32,
|
||||
readonly: false,
|
||||
});
|
||||
|
||||
let element_size = match self.module.table_plans[index].style {
|
||||
TableStyle::CallerChecksSignature => {
|
||||
u64::from(self.offsets.size_of_vmcaller_checked_anyfunc())
|
||||
}
|
||||
};
|
||||
|
||||
func.create_table(ir::TableData {
|
||||
base_gv,
|
||||
min_size: Uimm64::new(0),
|
||||
bound_gv,
|
||||
element_size: Uimm64::new(element_size),
|
||||
index_type: I32,
|
||||
})
|
||||
}
|
||||
|
||||
fn make_indirect_sig(&mut self, func: &mut ir::Function, index: SignatureIndex) -> ir::SigRef {
|
||||
func.import_signature(self.module.signatures[index].clone())
|
||||
}
|
||||
|
||||
fn make_direct_func(&mut self, func: &mut ir::Function, index: FuncIndex) -> ir::FuncRef {
|
||||
let sigidx = self.module.functions[index];
|
||||
let signature = func.import_signature(self.module.signatures[sigidx].clone());
|
||||
let name = get_func_name(index);
|
||||
// We currently allocate all code segments independently, so nothing
|
||||
// is colocated.
|
||||
let colocated = false;
|
||||
func.import_function(ir::ExtFuncData {
|
||||
name,
|
||||
signature,
|
||||
colocated,
|
||||
})
|
||||
}
|
||||
|
||||
fn translate_call_indirect(
|
||||
&mut self,
|
||||
mut pos: FuncCursor,
|
||||
table_index: TableIndex,
|
||||
table: ir::Table,
|
||||
sig_index: SignatureIndex,
|
||||
sig_ref: ir::SigRef,
|
||||
callee: ir::Value,
|
||||
call_args: &[ir::Value],
|
||||
) -> WasmResult<ir::Inst> {
|
||||
let pointer_type = self.pointer_type();
|
||||
|
||||
let table_entry_addr = pos.ins().table_addr(pointer_type, table, callee, 0);
|
||||
|
||||
// Dereference table_entry_addr to get the function address.
|
||||
let mut mem_flags = ir::MemFlags::new();
|
||||
mem_flags.set_notrap();
|
||||
mem_flags.set_aligned();
|
||||
let func_addr = pos.ins().load(
|
||||
pointer_type,
|
||||
mem_flags,
|
||||
table_entry_addr,
|
||||
i32::from(self.offsets.vmcaller_checked_anyfunc_func_ptr()),
|
||||
);
|
||||
|
||||
// If necessary, check the signature.
|
||||
match self.module.table_plans[table_index].style {
|
||||
TableStyle::CallerChecksSignature => {
|
||||
let sig_id_size = self.offsets.size_of_vmsignature_id();
|
||||
let sig_id_type = Type::int(u16::from(sig_id_size) * 8).unwrap();
|
||||
|
||||
let vmctx = self.vmctx(pos.func);
|
||||
let signature_ids_base = self.signature_ids_base.unwrap_or_else(|| {
|
||||
let new_base = pos.func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
offset: Offset32::new(i32::from(self.offsets.vmctx_signature_ids())),
|
||||
global_type: pointer_type,
|
||||
readonly: true,
|
||||
});
|
||||
self.signature_ids_base = Some(new_base);
|
||||
new_base
|
||||
});
|
||||
let sig_ids = pos.ins().global_value(pointer_type, signature_ids_base);
|
||||
|
||||
// Load the caller ID.
|
||||
// TODO: Factor this out into a MemFlags constructor, as it's used a lot.
|
||||
let mut mem_flags = ir::MemFlags::new();
|
||||
mem_flags.set_notrap();
|
||||
mem_flags.set_aligned();
|
||||
let caller_sig_id = pos.ins().load(
|
||||
sig_id_type,
|
||||
mem_flags,
|
||||
sig_ids,
|
||||
cast::i32(
|
||||
sig_index
|
||||
.as_u32()
|
||||
.checked_mul(u32::from(sig_id_size))
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
|
||||
// Load the callee ID.
|
||||
let mut mem_flags = ir::MemFlags::new();
|
||||
mem_flags.set_notrap();
|
||||
mem_flags.set_aligned();
|
||||
let callee_sig_id = pos.ins().load(
|
||||
sig_id_type,
|
||||
mem_flags,
|
||||
table_entry_addr,
|
||||
i32::from(self.offsets.vmcaller_checked_anyfunc_type_id()),
|
||||
);
|
||||
|
||||
// Check that they match.
|
||||
let cmp = pos.ins().icmp(IntCC::Equal, callee_sig_id, caller_sig_id);
|
||||
pos.ins().trapz(cmp, ir::TrapCode::BadSignature);
|
||||
}
|
||||
}
|
||||
|
||||
let real_call_args = FuncEnvironment::get_real_call_args(pos.func, call_args);
|
||||
Ok(pos.ins().call_indirect(sig_ref, func_addr, &real_call_args))
|
||||
}
|
||||
|
||||
fn translate_call(
|
||||
&mut self,
|
||||
mut pos: FuncCursor,
|
||||
_callee_index: FuncIndex,
|
||||
callee: ir::FuncRef,
|
||||
call_args: &[ir::Value],
|
||||
) -> WasmResult<ir::Inst> {
|
||||
let real_call_args = FuncEnvironment::get_real_call_args(pos.func, call_args);
|
||||
Ok(pos.ins().call(callee, &real_call_args))
|
||||
}
|
||||
|
||||
fn translate_memory_grow(
|
||||
&mut self,
|
||||
mut pos: FuncCursor,
|
||||
index: MemoryIndex,
|
||||
_heap: ir::Heap,
|
||||
val: ir::Value,
|
||||
) -> WasmResult<ir::Value> {
|
||||
let memory_grow_func = self.memory_grow_extfunc.unwrap_or_else(|| {
|
||||
let sig_ref = pos.func.import_signature(Signature {
|
||||
params: vec![
|
||||
AbiParam::new(I32),
|
||||
AbiParam::new(I32),
|
||||
AbiParam::special(self.pointer_type(), ArgumentPurpose::VMContext),
|
||||
],
|
||||
returns: vec![AbiParam::new(I32)],
|
||||
call_conv: self.isa.frontend_config().default_call_conv,
|
||||
});
|
||||
// We currently allocate all code segments independently, so nothing
|
||||
// is colocated.
|
||||
let colocated = false;
|
||||
pos.func.import_function(ExtFuncData {
|
||||
name: get_memory_grow_name(),
|
||||
signature: sig_ref,
|
||||
colocated,
|
||||
})
|
||||
});
|
||||
self.memory_grow_extfunc = Some(memory_grow_func);
|
||||
let memory_index = pos.ins().iconst(I32, index.index() as i64);
|
||||
let vmctx = pos.func.special_param(ArgumentPurpose::VMContext).unwrap();
|
||||
let call_inst = pos
|
||||
.ins()
|
||||
.call(memory_grow_func, &[val, memory_index, vmctx]);
|
||||
Ok(*pos.func.dfg.inst_results(call_inst).first().unwrap())
|
||||
}
|
||||
|
||||
fn translate_memory_size(
|
||||
&mut self,
|
||||
mut pos: FuncCursor,
|
||||
index: MemoryIndex,
|
||||
_heap: ir::Heap,
|
||||
) -> WasmResult<ir::Value> {
|
||||
let memory_size_func = self.memory_size_extfunc.unwrap_or_else(|| {
|
||||
let sig_ref = pos.func.import_signature(Signature {
|
||||
params: vec![
|
||||
AbiParam::new(I32),
|
||||
AbiParam::special(self.pointer_type(), ArgumentPurpose::VMContext),
|
||||
],
|
||||
returns: vec![AbiParam::new(I32)],
|
||||
call_conv: self.isa.frontend_config().default_call_conv,
|
||||
});
|
||||
// We currently allocate all code segments independently, so nothing
|
||||
// is colocated.
|
||||
let colocated = false;
|
||||
pos.func.import_function(ExtFuncData {
|
||||
name: get_memory_size_name(),
|
||||
signature: sig_ref,
|
||||
colocated,
|
||||
})
|
||||
});
|
||||
self.memory_size_extfunc = Some(memory_size_func);
|
||||
let memory_index = pos.ins().iconst(I32, index.index() as i64);
|
||||
let vmctx = pos.func.special_param(ArgumentPurpose::VMContext).unwrap();
|
||||
let call_inst = pos.ins().call(memory_size_func, &[memory_index, vmctx]);
|
||||
Ok(*pos.func.dfg.inst_results(call_inst).first().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
/// The result of translating via `ModuleEnvironment`.
|
||||
pub struct ModuleTranslation<'data, 'module> {
|
||||
/// Compilation setting flags.
|
||||
pub isa: &'module isa::TargetIsa,
|
||||
|
||||
/// Module information.
|
||||
pub module: &'module Module,
|
||||
|
||||
/// Pointers into the raw data buffer.
|
||||
pub lazy: LazyContents<'data>,
|
||||
|
||||
/// Tunable parameters.
|
||||
pub tunables: Tunables,
|
||||
}
|
||||
|
||||
impl<'data, 'module> ModuleTranslation<'data, 'module> {
|
||||
/// Return a new `FuncEnvironment` for translating a function.
|
||||
pub fn func_env(&self) -> FuncEnvironment {
|
||||
FuncEnvironment::new(self.isa, &self.module)
|
||||
}
|
||||
}
|
||||
|
||||
/// Add environment-specific function parameters.
|
||||
pub fn translate_signature(mut sig: ir::Signature, pointer_type: ir::Type) -> ir::Signature {
|
||||
sig.params
|
||||
.push(AbiParam::special(pointer_type, ArgumentPurpose::VMContext));
|
||||
sig
|
||||
}
|
||||
660
lib/environ/src/func_environ.rs
Normal file
660
lib/environ/src/func_environ.rs
Normal file
@@ -0,0 +1,660 @@
|
||||
use cast;
|
||||
use cranelift_codegen::cursor::FuncCursor;
|
||||
use cranelift_codegen::ir;
|
||||
use cranelift_codegen::ir::condcodes::*;
|
||||
use cranelift_codegen::ir::immediates::{Offset32, Uimm64};
|
||||
use cranelift_codegen::ir::types::*;
|
||||
use cranelift_codegen::ir::{
|
||||
AbiParam, ArgumentPurpose, ExtFuncData, FuncRef, Function, InstBuilder, Signature,
|
||||
};
|
||||
use cranelift_codegen::isa;
|
||||
use cranelift_entity::EntityRef;
|
||||
use cranelift_wasm::{
|
||||
self, DefinedGlobalIndex, DefinedMemoryIndex, DefinedTableIndex, FuncIndex, GlobalIndex,
|
||||
GlobalVariable, MemoryIndex, SignatureIndex, TableIndex, WasmResult,
|
||||
};
|
||||
use module::{MemoryPlan, MemoryStyle, Module, TableStyle};
|
||||
use std::clone::Clone;
|
||||
use std::vec::Vec;
|
||||
use vmoffsets::VMOffsets;
|
||||
use WASM_PAGE_SIZE;
|
||||
|
||||
/// Compute an `ir::ExternalName` for a given wasm function index.
|
||||
pub fn get_func_name(func_index: FuncIndex) -> ir::ExternalName {
|
||||
ir::ExternalName::user(0, func_index.as_u32())
|
||||
}
|
||||
|
||||
/// Compute an `ir::ExternalName` for the `memory.grow` libcall for
|
||||
/// 32-bit locally-defined memories.
|
||||
pub fn get_memory32_grow_name() -> ir::ExternalName {
|
||||
ir::ExternalName::user(1, 0)
|
||||
}
|
||||
|
||||
/// Compute an `ir::ExternalName` for the `memory.grow` libcall for
|
||||
/// 32-bit imported memories.
|
||||
pub fn get_imported_memory32_grow_name() -> ir::ExternalName {
|
||||
ir::ExternalName::user(1, 1)
|
||||
}
|
||||
|
||||
/// Compute an `ir::ExternalName` for the `memory.size` libcall for
|
||||
/// 32-bit locally-defined memories.
|
||||
pub fn get_memory32_size_name() -> ir::ExternalName {
|
||||
ir::ExternalName::user(1, 2)
|
||||
}
|
||||
|
||||
/// Compute an `ir::ExternalName` for the `memory.size` libcall for
|
||||
/// 32-bit imported memories.
|
||||
pub fn get_imported_memory32_size_name() -> ir::ExternalName {
|
||||
ir::ExternalName::user(1, 3)
|
||||
}
|
||||
|
||||
/// The FuncEnvironment implementation for use by the `ModuleEnvironment`.
|
||||
pub struct FuncEnvironment<'module_environment> {
|
||||
/// Compilation setting flags.
|
||||
isa: &'module_environment isa::TargetIsa,
|
||||
|
||||
/// The module-level environment which this function-level environment belongs to.
|
||||
module: &'module_environment Module,
|
||||
|
||||
/// The Cranelift global holding the vmctx address.
|
||||
vmctx: Option<ir::GlobalValue>,
|
||||
|
||||
/// The Cranelift global holding the base address of the imported functions table.
|
||||
imported_functions_base: Option<ir::GlobalValue>,
|
||||
|
||||
/// The Cranelift global holding the base address of the imported tables table.
|
||||
imported_tables_base: Option<ir::GlobalValue>,
|
||||
|
||||
/// The Cranelift global holding the base address of the imported memories table.
|
||||
imported_memories_base: Option<ir::GlobalValue>,
|
||||
|
||||
/// The Cranelift global holding the base address of the imported globals table.
|
||||
imported_globals_base: Option<ir::GlobalValue>,
|
||||
|
||||
/// The Cranelift global holding the base address of the tables vector.
|
||||
tables_base: Option<ir::GlobalValue>,
|
||||
|
||||
/// The Cranelift global holding the base address of the memories vector.
|
||||
memories_base: Option<ir::GlobalValue>,
|
||||
|
||||
/// The Cranelift global holding the base address of the globals vector.
|
||||
globals_base: Option<ir::GlobalValue>,
|
||||
|
||||
/// The Cranelift global holding the base address of the signature IDs vector.
|
||||
signature_ids_base: Option<ir::GlobalValue>,
|
||||
|
||||
/// The external function declaration for implementing wasm's `memory.size`
|
||||
/// for locally-defined 32-bit memories.
|
||||
memory32_size_extfunc: Option<FuncRef>,
|
||||
|
||||
/// The external function declaration for implementing wasm's `memory.size`
|
||||
/// for imported 32-bit memories.
|
||||
imported_memory32_size_extfunc: Option<FuncRef>,
|
||||
|
||||
/// The external function declaration for implementing wasm's `memory.grow`
|
||||
/// for locally-defined memories.
|
||||
memory_grow_extfunc: Option<FuncRef>,
|
||||
|
||||
/// The external function declaration for implementing wasm's `memory.grow`
|
||||
/// for imported memories.
|
||||
imported_memory_grow_extfunc: Option<FuncRef>,
|
||||
|
||||
/// Offsets to struct fields accessed by JIT code.
|
||||
offsets: VMOffsets,
|
||||
}
|
||||
|
||||
impl<'module_environment> FuncEnvironment<'module_environment> {
|
||||
pub fn new(
|
||||
isa: &'module_environment isa::TargetIsa,
|
||||
module: &'module_environment Module,
|
||||
) -> Self {
|
||||
Self {
|
||||
isa,
|
||||
module,
|
||||
vmctx: None,
|
||||
imported_functions_base: None,
|
||||
imported_tables_base: None,
|
||||
imported_memories_base: None,
|
||||
imported_globals_base: None,
|
||||
tables_base: None,
|
||||
memories_base: None,
|
||||
globals_base: None,
|
||||
signature_ids_base: None,
|
||||
memory32_size_extfunc: None,
|
||||
imported_memory32_size_extfunc: None,
|
||||
memory_grow_extfunc: None,
|
||||
imported_memory_grow_extfunc: None,
|
||||
offsets: VMOffsets::new(isa.pointer_bytes()),
|
||||
}
|
||||
}
|
||||
|
||||
fn pointer_type(&self) -> ir::Type {
|
||||
self.isa.frontend_config().pointer_type()
|
||||
}
|
||||
|
||||
/// Transform the call argument list in preparation for making a call.
|
||||
fn get_real_call_args(func: &Function, call_args: &[ir::Value]) -> Vec<ir::Value> {
|
||||
let mut real_call_args = Vec::with_capacity(call_args.len() + 1);
|
||||
real_call_args.extend_from_slice(call_args);
|
||||
real_call_args.push(func.special_param(ArgumentPurpose::VMContext).unwrap());
|
||||
real_call_args
|
||||
}
|
||||
|
||||
fn vmctx(&mut self, func: &mut Function) -> ir::GlobalValue {
|
||||
self.vmctx.unwrap_or_else(|| {
|
||||
let vmctx = func.create_global_value(ir::GlobalValueData::VMContext);
|
||||
self.vmctx = Some(vmctx);
|
||||
vmctx
|
||||
})
|
||||
}
|
||||
|
||||
fn get_imported_functions_base(&mut self, func: &mut Function) -> ir::GlobalValue {
|
||||
self.imported_functions_base.unwrap_or_else(|| {
|
||||
let pointer_type = self.pointer_type();
|
||||
let vmctx = self.vmctx(func);
|
||||
let new_base = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
offset: Offset32::new(i32::from(self.offsets.vmctx_imported_functions())),
|
||||
global_type: pointer_type,
|
||||
readonly: true,
|
||||
});
|
||||
self.imported_functions_base = Some(new_base);
|
||||
new_base
|
||||
})
|
||||
}
|
||||
|
||||
fn get_imported_tables_base(&mut self, func: &mut Function) -> ir::GlobalValue {
|
||||
self.imported_tables_base.unwrap_or_else(|| {
|
||||
let pointer_type = self.pointer_type();
|
||||
let vmctx = self.vmctx(func);
|
||||
let new_base = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
offset: Offset32::new(i32::from(self.offsets.vmctx_imported_tables())),
|
||||
global_type: pointer_type,
|
||||
readonly: true,
|
||||
});
|
||||
self.imported_tables_base = Some(new_base);
|
||||
new_base
|
||||
})
|
||||
}
|
||||
|
||||
fn get_imported_memories_base(&mut self, func: &mut Function) -> ir::GlobalValue {
|
||||
self.imported_memories_base.unwrap_or_else(|| {
|
||||
let pointer_type = self.pointer_type();
|
||||
let vmctx = self.vmctx(func);
|
||||
let new_base = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
offset: Offset32::new(i32::from(self.offsets.vmctx_imported_memories())),
|
||||
global_type: pointer_type,
|
||||
readonly: true,
|
||||
});
|
||||
self.imported_memories_base = Some(new_base);
|
||||
new_base
|
||||
})
|
||||
}
|
||||
|
||||
fn get_imported_globals_base(&mut self, func: &mut Function) -> ir::GlobalValue {
|
||||
self.imported_globals_base.unwrap_or_else(|| {
|
||||
let pointer_type = self.pointer_type();
|
||||
let vmctx = self.vmctx(func);
|
||||
let new_base = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
offset: Offset32::new(i32::from(self.offsets.vmctx_imported_globals())),
|
||||
global_type: pointer_type,
|
||||
readonly: true,
|
||||
});
|
||||
self.imported_globals_base = Some(new_base);
|
||||
new_base
|
||||
})
|
||||
}
|
||||
|
||||
fn get_tables_base(&mut self, func: &mut Function) -> ir::GlobalValue {
|
||||
self.tables_base.unwrap_or_else(|| {
|
||||
let pointer_type = self.pointer_type();
|
||||
let vmctx = self.vmctx(func);
|
||||
let new_base = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
offset: Offset32::new(i32::from(self.offsets.vmctx_tables())),
|
||||
global_type: pointer_type,
|
||||
readonly: true,
|
||||
});
|
||||
self.tables_base = Some(new_base);
|
||||
new_base
|
||||
})
|
||||
}
|
||||
|
||||
fn get_memories_base(&mut self, func: &mut Function) -> ir::GlobalValue {
|
||||
self.memories_base.unwrap_or_else(|| {
|
||||
let pointer_type = self.pointer_type();
|
||||
let vmctx = self.vmctx(func);
|
||||
let new_base = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
offset: Offset32::new(i32::from(self.offsets.vmctx_memories())),
|
||||
global_type: pointer_type,
|
||||
readonly: true,
|
||||
});
|
||||
self.memories_base = Some(new_base);
|
||||
new_base
|
||||
})
|
||||
}
|
||||
|
||||
fn get_globals_base(&mut self, func: &mut Function) -> ir::GlobalValue {
|
||||
self.globals_base.unwrap_or_else(|| {
|
||||
let pointer_type = self.pointer_type();
|
||||
let vmctx = self.vmctx(func);
|
||||
let new_base = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
offset: Offset32::new(i32::from(self.offsets.vmctx_globals())),
|
||||
global_type: pointer_type,
|
||||
readonly: true,
|
||||
});
|
||||
self.globals_base = Some(new_base);
|
||||
new_base
|
||||
})
|
||||
}
|
||||
|
||||
fn get_memory_grow_sig(&self, func: &mut Function) -> ir::SigRef {
|
||||
func.import_signature(Signature {
|
||||
params: vec![
|
||||
AbiParam::new(I32),
|
||||
AbiParam::new(I32),
|
||||
AbiParam::special(self.pointer_type(), ArgumentPurpose::VMContext),
|
||||
],
|
||||
returns: vec![AbiParam::new(I32)],
|
||||
call_conv: self.isa.frontend_config().default_call_conv,
|
||||
})
|
||||
}
|
||||
|
||||
/// Return the memory.grow function to call for the given index, along with the
|
||||
/// translated index value to pass to it.
|
||||
fn get_memory_grow_func(
|
||||
&mut self,
|
||||
func: &mut Function,
|
||||
index: MemoryIndex,
|
||||
) -> (FuncRef, usize) {
|
||||
if self.module.is_imported_memory(index) {
|
||||
let extfunc = self.imported_memory_grow_extfunc.unwrap_or_else(|| {
|
||||
let sig_ref = self.get_memory_grow_sig(func);
|
||||
func.import_function(ExtFuncData {
|
||||
name: get_imported_memory32_grow_name(),
|
||||
signature: sig_ref,
|
||||
// We currently allocate all code segments independently, so nothing
|
||||
// is colocated.
|
||||
colocated: false,
|
||||
})
|
||||
});
|
||||
self.imported_memory_grow_extfunc = Some(extfunc);
|
||||
(extfunc, index.index())
|
||||
} else {
|
||||
let extfunc = self.memory_grow_extfunc.unwrap_or_else(|| {
|
||||
let sig_ref = self.get_memory_grow_sig(func);
|
||||
func.import_function(ExtFuncData {
|
||||
name: get_memory32_grow_name(),
|
||||
signature: sig_ref,
|
||||
// We currently allocate all code segments independently, so nothing
|
||||
// is colocated.
|
||||
colocated: false,
|
||||
})
|
||||
});
|
||||
self.memory_grow_extfunc = Some(extfunc);
|
||||
(
|
||||
extfunc,
|
||||
self.module.defined_memory_index(index).unwrap().index(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_memory32_size_sig(&self, func: &mut Function) -> ir::SigRef {
|
||||
func.import_signature(Signature {
|
||||
params: vec![
|
||||
AbiParam::new(I32),
|
||||
AbiParam::special(self.pointer_type(), ArgumentPurpose::VMContext),
|
||||
],
|
||||
returns: vec![AbiParam::new(I32)],
|
||||
call_conv: self.isa.frontend_config().default_call_conv,
|
||||
})
|
||||
}
|
||||
|
||||
/// Return the memory.size function to call for the given index, along with the
|
||||
/// translated index value to pass to it.
|
||||
fn get_memory_size_func(
|
||||
&mut self,
|
||||
func: &mut Function,
|
||||
index: MemoryIndex,
|
||||
) -> (FuncRef, usize) {
|
||||
if self.module.is_imported_memory(index) {
|
||||
let extfunc = self.imported_memory32_size_extfunc.unwrap_or_else(|| {
|
||||
let sig_ref = self.get_memory32_size_sig(func);
|
||||
func.import_function(ExtFuncData {
|
||||
name: get_imported_memory32_size_name(),
|
||||
signature: sig_ref,
|
||||
// We currently allocate all code segments independently, so nothing
|
||||
// is colocated.
|
||||
colocated: false,
|
||||
})
|
||||
});
|
||||
self.imported_memory32_size_extfunc = Some(extfunc);
|
||||
(extfunc, index.index())
|
||||
} else {
|
||||
let extfunc = self.memory32_size_extfunc.unwrap_or_else(|| {
|
||||
let sig_ref = self.get_memory32_size_sig(func);
|
||||
func.import_function(ExtFuncData {
|
||||
name: get_memory32_size_name(),
|
||||
signature: sig_ref,
|
||||
// We currently allocate all code segments independently, so nothing
|
||||
// is colocated.
|
||||
colocated: false,
|
||||
})
|
||||
});
|
||||
self.memory32_size_extfunc = Some(extfunc);
|
||||
(
|
||||
extfunc,
|
||||
self.module.defined_memory_index(index).unwrap().index(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'module_environment> {
|
||||
fn target_config(&self) -> isa::TargetFrontendConfig {
|
||||
self.isa.frontend_config()
|
||||
}
|
||||
|
||||
fn make_table(&mut self, func: &mut ir::Function, index: TableIndex) -> ir::Table {
|
||||
let pointer_type = self.pointer_type();
|
||||
|
||||
let (table, def_index) = if let Some(def_index) = self.module.defined_table_index(index) {
|
||||
let table = self.get_tables_base(func);
|
||||
(table, def_index)
|
||||
} else {
|
||||
let imported_tables_base = self.get_imported_tables_base(func);
|
||||
let from_offset = self.offsets.index_vmtable_import_from(index);
|
||||
let table = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: imported_tables_base,
|
||||
offset: Offset32::new(from_offset),
|
||||
global_type: pointer_type,
|
||||
readonly: true,
|
||||
});
|
||||
(table, DefinedTableIndex::new(0))
|
||||
};
|
||||
let base_offset = self.offsets.index_vmtable_definition_base(def_index);
|
||||
let current_elements_offset = self
|
||||
.offsets
|
||||
.index_vmtable_definition_current_elements(def_index);
|
||||
|
||||
let base_gv = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: table,
|
||||
offset: Offset32::new(base_offset),
|
||||
global_type: pointer_type,
|
||||
readonly: false,
|
||||
});
|
||||
let bound_gv = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: table,
|
||||
offset: Offset32::new(current_elements_offset),
|
||||
global_type: self.offsets.type_of_vmtable_definition_current_elements(),
|
||||
readonly: false,
|
||||
});
|
||||
|
||||
let element_size = match self.module.table_plans[index].style {
|
||||
TableStyle::CallerChecksSignature => {
|
||||
u64::from(self.offsets.size_of_vmcaller_checked_anyfunc())
|
||||
}
|
||||
};
|
||||
|
||||
func.create_table(ir::TableData {
|
||||
base_gv,
|
||||
min_size: Uimm64::new(0),
|
||||
bound_gv,
|
||||
element_size: Uimm64::new(element_size),
|
||||
index_type: I32,
|
||||
})
|
||||
}
|
||||
|
||||
fn make_heap(&mut self, func: &mut ir::Function, index: MemoryIndex) -> ir::Heap {
|
||||
let pointer_type = self.pointer_type();
|
||||
|
||||
let (memory, def_index) = if let Some(def_index) = self.module.defined_memory_index(index) {
|
||||
let memory = self.get_memories_base(func);
|
||||
(memory, def_index)
|
||||
} else {
|
||||
let imported_memories_base = self.get_imported_memories_base(func);
|
||||
let from_offset = self.offsets.index_vmmemory_import_from(index);
|
||||
let memory = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: imported_memories_base,
|
||||
offset: Offset32::new(from_offset),
|
||||
global_type: pointer_type,
|
||||
readonly: true,
|
||||
});
|
||||
(memory, DefinedMemoryIndex::new(0))
|
||||
};
|
||||
let base_offset = self.offsets.index_vmmemory_definition_base(def_index);
|
||||
let current_length_offset = self
|
||||
.offsets
|
||||
.index_vmmemory_definition_current_length(def_index);
|
||||
|
||||
// If we have a declared maximum, we can make this a "static" heap, which is
|
||||
// allocated up front and never moved.
|
||||
let (offset_guard_size, heap_style, readonly_base) = match self.module.memory_plans[index] {
|
||||
MemoryPlan {
|
||||
memory: _,
|
||||
style: MemoryStyle::Dynamic,
|
||||
offset_guard_size,
|
||||
} => {
|
||||
let heap_bound = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: memory,
|
||||
offset: Offset32::new(current_length_offset),
|
||||
global_type: self.offsets.type_of_vmmemory_definition_current_length(),
|
||||
readonly: false,
|
||||
});
|
||||
(
|
||||
Uimm64::new(offset_guard_size),
|
||||
ir::HeapStyle::Dynamic {
|
||||
bound_gv: heap_bound,
|
||||
},
|
||||
false,
|
||||
)
|
||||
}
|
||||
MemoryPlan {
|
||||
memory: _,
|
||||
style: MemoryStyle::Static { bound },
|
||||
offset_guard_size,
|
||||
} => (
|
||||
Uimm64::new(offset_guard_size),
|
||||
ir::HeapStyle::Static {
|
||||
bound: Uimm64::new(u64::from(bound) * u64::from(WASM_PAGE_SIZE)),
|
||||
},
|
||||
true,
|
||||
),
|
||||
};
|
||||
|
||||
let heap_base = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: memory,
|
||||
offset: Offset32::new(base_offset),
|
||||
global_type: pointer_type,
|
||||
readonly: readonly_base,
|
||||
});
|
||||
func.create_heap(ir::HeapData {
|
||||
base: heap_base,
|
||||
min_size: 0.into(),
|
||||
offset_guard_size,
|
||||
style: heap_style,
|
||||
index_type: I32,
|
||||
})
|
||||
}
|
||||
|
||||
fn make_global(&mut self, func: &mut ir::Function, index: GlobalIndex) -> GlobalVariable {
|
||||
let pointer_type = self.pointer_type();
|
||||
|
||||
let (global, def_index) = if let Some(def_index) = self.module.defined_global_index(index) {
|
||||
let global = self.get_globals_base(func);
|
||||
(global, def_index)
|
||||
} else {
|
||||
let imported_globals_base = self.get_imported_globals_base(func);
|
||||
let from_offset = self.offsets.index_vmglobal_import_from(index);
|
||||
let global = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: imported_globals_base,
|
||||
offset: Offset32::new(from_offset),
|
||||
global_type: pointer_type,
|
||||
readonly: true,
|
||||
});
|
||||
(global, DefinedGlobalIndex::new(0))
|
||||
};
|
||||
let offset = self.offsets.index_vmglobal_definition(def_index);
|
||||
|
||||
GlobalVariable::Memory {
|
||||
gv: global,
|
||||
offset: offset.into(),
|
||||
ty: self.module.globals[index].ty,
|
||||
}
|
||||
}
|
||||
|
||||
fn make_indirect_sig(&mut self, func: &mut ir::Function, index: SignatureIndex) -> ir::SigRef {
|
||||
func.import_signature(self.module.signatures[index].clone())
|
||||
}
|
||||
|
||||
fn make_direct_func(&mut self, func: &mut ir::Function, index: FuncIndex) -> ir::FuncRef {
|
||||
let sigidx = self.module.functions[index];
|
||||
let signature = func.import_signature(self.module.signatures[sigidx].clone());
|
||||
let name = get_func_name(index);
|
||||
func.import_function(ir::ExtFuncData {
|
||||
name,
|
||||
signature,
|
||||
// We currently allocate all code segments independently, so nothing
|
||||
// is colocated.
|
||||
colocated: false,
|
||||
})
|
||||
}
|
||||
|
||||
fn translate_call_indirect(
|
||||
&mut self,
|
||||
mut pos: FuncCursor,
|
||||
table_index: TableIndex,
|
||||
table: ir::Table,
|
||||
sig_index: SignatureIndex,
|
||||
sig_ref: ir::SigRef,
|
||||
callee: ir::Value,
|
||||
call_args: &[ir::Value],
|
||||
) -> WasmResult<ir::Inst> {
|
||||
let pointer_type = self.pointer_type();
|
||||
|
||||
let table_entry_addr = pos.ins().table_addr(pointer_type, table, callee, 0);
|
||||
|
||||
// Dereference table_entry_addr to get the function address.
|
||||
let mem_flags = ir::MemFlags::trusted();
|
||||
let func_addr = pos.ins().load(
|
||||
pointer_type,
|
||||
mem_flags,
|
||||
table_entry_addr,
|
||||
i32::from(self.offsets.vmcaller_checked_anyfunc_func_ptr()),
|
||||
);
|
||||
|
||||
// If necessary, check the signature.
|
||||
match self.module.table_plans[table_index].style {
|
||||
TableStyle::CallerChecksSignature => {
|
||||
let sig_id_size = self.offsets.size_of_vmshared_signature_index();
|
||||
let sig_id_type = Type::int(u16::from(sig_id_size) * 8).unwrap();
|
||||
|
||||
let vmctx = self.vmctx(pos.func);
|
||||
let signature_ids_base = self.signature_ids_base.unwrap_or_else(|| {
|
||||
let new_base = pos.func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
offset: Offset32::new(i32::from(self.offsets.vmctx_signature_ids())),
|
||||
global_type: pointer_type,
|
||||
readonly: true,
|
||||
});
|
||||
self.signature_ids_base = Some(new_base);
|
||||
new_base
|
||||
});
|
||||
let sig_ids = pos.ins().global_value(pointer_type, signature_ids_base);
|
||||
|
||||
// Load the caller ID.
|
||||
let mem_flags = ir::MemFlags::trusted();
|
||||
let caller_sig_id = pos.ins().load(
|
||||
sig_id_type,
|
||||
mem_flags,
|
||||
sig_ids,
|
||||
cast::i32(
|
||||
sig_index
|
||||
.as_u32()
|
||||
.checked_mul(u32::from(sig_id_size))
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
|
||||
// Load the callee ID.
|
||||
let mem_flags = ir::MemFlags::trusted();
|
||||
let callee_sig_id = pos.ins().load(
|
||||
sig_id_type,
|
||||
mem_flags,
|
||||
table_entry_addr,
|
||||
i32::from(self.offsets.vmcaller_checked_anyfunc_type_index()),
|
||||
);
|
||||
|
||||
// Check that they match.
|
||||
let cmp = pos.ins().icmp(IntCC::Equal, callee_sig_id, caller_sig_id);
|
||||
pos.ins().trapz(cmp, ir::TrapCode::BadSignature);
|
||||
}
|
||||
}
|
||||
|
||||
let real_call_args = FuncEnvironment::get_real_call_args(pos.func, call_args);
|
||||
Ok(pos.ins().call_indirect(sig_ref, func_addr, &real_call_args))
|
||||
}
|
||||
|
||||
fn translate_call(
|
||||
&mut self,
|
||||
mut pos: FuncCursor,
|
||||
callee_index: FuncIndex,
|
||||
callee: ir::FuncRef,
|
||||
call_args: &[ir::Value],
|
||||
) -> WasmResult<ir::Inst> {
|
||||
let real_call_args = FuncEnvironment::get_real_call_args(pos.func, call_args);
|
||||
|
||||
// Handle direct calls to locally-defined functions.
|
||||
if !self.module.is_imported_function(callee_index) {
|
||||
return Ok(pos.ins().call(callee, &real_call_args));
|
||||
}
|
||||
|
||||
// Handle direct calls to imported functions. We use an indirect call
|
||||
// so that we don't have to patch the code at runtime.
|
||||
let pointer_type = self.pointer_type();
|
||||
let sig_ref = pos.func.dfg.ext_funcs[callee].signature;
|
||||
let imported_functions_base = self.get_imported_functions_base(&mut pos.func);
|
||||
let base = pos
|
||||
.ins()
|
||||
.global_value(pointer_type, imported_functions_base);
|
||||
let offset = self.offsets.index_vmfunction_body_import(callee_index);
|
||||
let mem_flags = ir::MemFlags::trusted();
|
||||
let func_addr = pos.ins().load(pointer_type, mem_flags, base, offset);
|
||||
Ok(pos.ins().call_indirect(sig_ref, func_addr, &real_call_args))
|
||||
}
|
||||
|
||||
fn translate_memory_grow(
|
||||
&mut self,
|
||||
mut pos: FuncCursor,
|
||||
index: MemoryIndex,
|
||||
_heap: ir::Heap,
|
||||
val: ir::Value,
|
||||
) -> WasmResult<ir::Value> {
|
||||
let (memory_grow_func, index_arg) = self.get_memory_grow_func(&mut pos.func, index);
|
||||
let memory_index = pos.ins().iconst(I32, index_arg as i64);
|
||||
let vmctx = pos.func.special_param(ArgumentPurpose::VMContext).unwrap();
|
||||
let call_inst = pos
|
||||
.ins()
|
||||
.call(memory_grow_func, &[val, memory_index, vmctx]);
|
||||
Ok(*pos.func.dfg.inst_results(call_inst).first().unwrap())
|
||||
}
|
||||
|
||||
fn translate_memory_size(
|
||||
&mut self,
|
||||
mut pos: FuncCursor,
|
||||
index: MemoryIndex,
|
||||
_heap: ir::Heap,
|
||||
) -> WasmResult<ir::Value> {
|
||||
let (memory_size_func, index_arg) = self.get_memory_size_func(&mut pos.func, index);
|
||||
let memory_index = pos.ins().iconst(I32, index_arg as i64);
|
||||
let vmctx = pos.func.special_param(ArgumentPurpose::VMContext).unwrap();
|
||||
let call_inst = pos.ins().call(memory_size_func, &[memory_index, vmctx]);
|
||||
Ok(*pos.func.dfg.inst_results(call_inst).first().unwrap())
|
||||
}
|
||||
}
|
||||
@@ -39,18 +39,19 @@ extern crate failure;
|
||||
extern crate failure_derive;
|
||||
|
||||
mod compilation;
|
||||
mod environ;
|
||||
mod func_environ;
|
||||
mod module;
|
||||
mod module_environ;
|
||||
mod tunables;
|
||||
mod vmoffsets;
|
||||
|
||||
pub use compilation::{
|
||||
compile_module, Compilation, CompileError, RelocSink, Relocation, RelocationTarget, Relocations,
|
||||
};
|
||||
pub use environ::{translate_signature, ModuleEnvironment, ModuleTranslation};
|
||||
pub use module::{
|
||||
DataInitializer, Export, MemoryPlan, MemoryStyle, Module, TableElements, TablePlan, TableStyle,
|
||||
};
|
||||
pub use module_environ::{translate_signature, ModuleEnvironment, ModuleTranslation};
|
||||
pub use tunables::Tunables;
|
||||
pub use vmoffsets::VMOffsets;
|
||||
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
use cranelift_codegen::ir;
|
||||
use cranelift_entity::{EntityRef, PrimaryMap};
|
||||
use cranelift_wasm::{
|
||||
DefinedFuncIndex, FuncIndex, Global, GlobalIndex, Memory, MemoryIndex, SignatureIndex, Table,
|
||||
TableIndex,
|
||||
DefinedFuncIndex, DefinedGlobalIndex, DefinedMemoryIndex, DefinedTableIndex, FuncIndex, Global,
|
||||
GlobalIndex, Memory, MemoryIndex, SignatureIndex, Table, TableIndex,
|
||||
};
|
||||
use std::cmp;
|
||||
use std::collections::HashMap;
|
||||
@@ -142,12 +142,12 @@ pub struct Module {
|
||||
/// Names of imported tables.
|
||||
pub imported_tables: PrimaryMap<TableIndex, (String, String)>,
|
||||
|
||||
/// Names of imported globals.
|
||||
pub imported_globals: PrimaryMap<GlobalIndex, (String, String)>,
|
||||
|
||||
/// Names of imported memories.
|
||||
pub imported_memories: PrimaryMap<MemoryIndex, (String, String)>,
|
||||
|
||||
/// Names of imported globals.
|
||||
pub imported_globals: PrimaryMap<GlobalIndex, (String, String)>,
|
||||
|
||||
/// Types of functions, imported and local.
|
||||
pub functions: PrimaryMap<FuncIndex, SignatureIndex>,
|
||||
|
||||
@@ -176,9 +176,9 @@ impl Module {
|
||||
Self {
|
||||
signatures: PrimaryMap::new(),
|
||||
imported_funcs: PrimaryMap::new(),
|
||||
imported_tables: PrimaryMap::new(),
|
||||
imported_memories: PrimaryMap::new(),
|
||||
imported_globals: PrimaryMap::new(),
|
||||
imported_tables: PrimaryMap::new(),
|
||||
functions: PrimaryMap::new(),
|
||||
table_plans: PrimaryMap::new(),
|
||||
memory_plans: PrimaryMap::new(),
|
||||
@@ -211,20 +211,71 @@ impl Module {
|
||||
index.index() < self.imported_funcs.len()
|
||||
}
|
||||
|
||||
/// Convert a `DefinedTableIndex` into a `TableIndex`.
|
||||
pub fn table_index(&self, defined_table: DefinedTableIndex) -> TableIndex {
|
||||
TableIndex::new(self.imported_tables.len() + defined_table.index())
|
||||
}
|
||||
|
||||
/// Convert a `TableIndex` into a `DefinedTableIndex`. Returns None if the
|
||||
/// index is an imported table.
|
||||
pub fn defined_table_index(&self, table: TableIndex) -> Option<DefinedTableIndex> {
|
||||
if table.index() < self.imported_tables.len() {
|
||||
None
|
||||
} else {
|
||||
Some(DefinedTableIndex::new(
|
||||
table.index() - self.imported_tables.len(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// Test whether the given table index is for an imported table.
|
||||
pub fn is_imported_table(&self, index: TableIndex) -> bool {
|
||||
index.index() < self.imported_tables.len()
|
||||
}
|
||||
|
||||
/// Test whether the given global index is for an imported global.
|
||||
pub fn is_imported_global(&self, index: GlobalIndex) -> bool {
|
||||
index.index() < self.imported_globals.len()
|
||||
/// Convert a `DefinedMemoryIndex` into a `MemoryIndex`.
|
||||
pub fn memory_index(&self, defined_memory: DefinedMemoryIndex) -> MemoryIndex {
|
||||
MemoryIndex::new(self.imported_memories.len() + defined_memory.index())
|
||||
}
|
||||
|
||||
/// Convert a `MemoryIndex` into a `DefinedMemoryIndex`. Returns None if the
|
||||
/// index is an imported memory.
|
||||
pub fn defined_memory_index(&self, memory: MemoryIndex) -> Option<DefinedMemoryIndex> {
|
||||
if memory.index() < self.imported_memories.len() {
|
||||
None
|
||||
} else {
|
||||
Some(DefinedMemoryIndex::new(
|
||||
memory.index() - self.imported_memories.len(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// Test whether the given memory index is for an imported memory.
|
||||
pub fn is_imported_memory(&self, index: MemoryIndex) -> bool {
|
||||
index.index() < self.imported_memories.len()
|
||||
}
|
||||
|
||||
/// Convert a `DefinedGlobalIndex` into a `GlobalIndex`.
|
||||
pub fn global_index(&self, defined_global: DefinedGlobalIndex) -> GlobalIndex {
|
||||
GlobalIndex::new(self.imported_globals.len() + defined_global.index())
|
||||
}
|
||||
|
||||
/// Convert a `GlobalIndex` into a `DefinedGlobalIndex`. Returns None if the
|
||||
/// index is an imported global.
|
||||
pub fn defined_global_index(&self, global: GlobalIndex) -> Option<DefinedGlobalIndex> {
|
||||
if global.index() < self.imported_globals.len() {
|
||||
None
|
||||
} else {
|
||||
Some(DefinedGlobalIndex::new(
|
||||
global.index() - self.imported_globals.len(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// Test whether the given global index is for an imported global.
|
||||
pub fn is_imported_global(&self, index: GlobalIndex) -> bool {
|
||||
index.index() < self.imported_globals.len()
|
||||
}
|
||||
}
|
||||
|
||||
/// A data initializer for linear memory.
|
||||
|
||||
261
lib/environ/src/module_environ.rs
Normal file
261
lib/environ/src/module_environ.rs
Normal file
@@ -0,0 +1,261 @@
|
||||
use cranelift_codegen::ir;
|
||||
use cranelift_codegen::ir::{AbiParam, ArgumentPurpose};
|
||||
use cranelift_codegen::isa;
|
||||
use cranelift_wasm::{
|
||||
self, translate_module, FuncIndex, Global, GlobalIndex, Memory, MemoryIndex, SignatureIndex,
|
||||
Table, TableIndex, WasmResult,
|
||||
};
|
||||
use func_environ::FuncEnvironment;
|
||||
use module::{DataInitializer, Export, LazyContents, MemoryPlan, Module, TableElements, TablePlan};
|
||||
use std::clone::Clone;
|
||||
use std::string::String;
|
||||
use std::vec::Vec;
|
||||
use tunables::Tunables;
|
||||
|
||||
/// Object containing the standalone environment information. To be passed after creation as
|
||||
/// argument to `compile_module`.
|
||||
pub struct ModuleEnvironment<'data, 'module> {
|
||||
/// Compilation setting flags.
|
||||
isa: &'module isa::TargetIsa,
|
||||
|
||||
/// Module information.
|
||||
module: &'module mut Module,
|
||||
|
||||
/// References to information to be decoded later.
|
||||
lazy: LazyContents<'data>,
|
||||
|
||||
/// Tunable parameters.
|
||||
tunables: Tunables,
|
||||
}
|
||||
|
||||
impl<'data, 'module> ModuleEnvironment<'data, 'module> {
|
||||
/// Allocates the enironment data structures with the given isa.
|
||||
pub fn new(
|
||||
isa: &'module isa::TargetIsa,
|
||||
module: &'module mut Module,
|
||||
tunables: Tunables,
|
||||
) -> Self {
|
||||
Self {
|
||||
isa,
|
||||
module,
|
||||
lazy: LazyContents::new(),
|
||||
tunables,
|
||||
}
|
||||
}
|
||||
|
||||
fn pointer_type(&self) -> ir::Type {
|
||||
self.isa.frontend_config().pointer_type()
|
||||
}
|
||||
|
||||
/// Translate the given wasm module data using this environment. This consumes the
|
||||
/// `ModuleEnvironment` with its mutable reference to the `Module` and produces a
|
||||
/// `ModuleTranslation` with an immutable reference to the `Module` (which has
|
||||
/// become fully populated).
|
||||
pub fn translate(mut self, data: &'data [u8]) -> WasmResult<ModuleTranslation<'data, 'module>> {
|
||||
translate_module(data, &mut self)?;
|
||||
|
||||
Ok(ModuleTranslation {
|
||||
isa: self.isa,
|
||||
module: self.module,
|
||||
lazy: self.lazy,
|
||||
tunables: self.tunables,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// This trait is useful for `translate_module` because it tells how to translate
|
||||
/// enironment-dependent wasm instructions. These functions should not be called by the user.
|
||||
impl<'data, 'module> cranelift_wasm::ModuleEnvironment<'data>
|
||||
for ModuleEnvironment<'data, 'module>
|
||||
{
|
||||
fn target_config(&self) -> isa::TargetFrontendConfig {
|
||||
self.isa.frontend_config()
|
||||
}
|
||||
|
||||
fn declare_signature(&mut self, sig: &ir::Signature) {
|
||||
let sig = translate_signature(sig.clone(), self.pointer_type());
|
||||
// TODO: Deduplicate signatures.
|
||||
self.module.signatures.push(sig);
|
||||
}
|
||||
|
||||
fn get_signature(&self, sig_index: SignatureIndex) -> &ir::Signature {
|
||||
&self.module.signatures[sig_index]
|
||||
}
|
||||
|
||||
fn declare_func_import(&mut self, sig_index: SignatureIndex, module: &str, field: &str) {
|
||||
debug_assert_eq!(
|
||||
self.module.functions.len(),
|
||||
self.module.imported_funcs.len(),
|
||||
"Imported functions must be declared first"
|
||||
);
|
||||
self.module.functions.push(sig_index);
|
||||
|
||||
self.module
|
||||
.imported_funcs
|
||||
.push((String::from(module), String::from(field)));
|
||||
}
|
||||
|
||||
fn get_num_func_imports(&self) -> usize {
|
||||
self.module.imported_funcs.len()
|
||||
}
|
||||
|
||||
fn declare_func_type(&mut self, sig_index: SignatureIndex) {
|
||||
self.module.functions.push(sig_index);
|
||||
}
|
||||
|
||||
fn get_func_type(&self, func_index: FuncIndex) -> SignatureIndex {
|
||||
self.module.functions[func_index]
|
||||
}
|
||||
|
||||
fn declare_global_import(&mut self, global: Global, module: &str, field: &str) {
|
||||
debug_assert_eq!(
|
||||
self.module.globals.len(),
|
||||
self.module.imported_globals.len(),
|
||||
"Imported globals must be declared first"
|
||||
);
|
||||
self.module.globals.push(global);
|
||||
|
||||
self.module
|
||||
.imported_globals
|
||||
.push((String::from(module), String::from(field)));
|
||||
}
|
||||
|
||||
fn declare_global(&mut self, global: Global) {
|
||||
self.module.globals.push(global);
|
||||
}
|
||||
|
||||
fn get_global(&self, global_index: GlobalIndex) -> &Global {
|
||||
&self.module.globals[global_index]
|
||||
}
|
||||
|
||||
fn declare_table_import(&mut self, table: Table, module: &str, field: &str) {
|
||||
debug_assert_eq!(
|
||||
self.module.table_plans.len(),
|
||||
self.module.imported_tables.len(),
|
||||
"Imported tables must be declared first"
|
||||
);
|
||||
let plan = TablePlan::for_table(table, &self.tunables);
|
||||
self.module.table_plans.push(plan);
|
||||
|
||||
self.module
|
||||
.imported_tables
|
||||
.push((String::from(module), String::from(field)));
|
||||
}
|
||||
|
||||
fn declare_table(&mut self, table: Table) {
|
||||
let plan = TablePlan::for_table(table, &self.tunables);
|
||||
self.module.table_plans.push(plan);
|
||||
}
|
||||
|
||||
fn declare_table_elements(
|
||||
&mut self,
|
||||
table_index: TableIndex,
|
||||
base: Option<GlobalIndex>,
|
||||
offset: usize,
|
||||
elements: Vec<FuncIndex>,
|
||||
) {
|
||||
self.module.table_elements.push(TableElements {
|
||||
table_index,
|
||||
base,
|
||||
offset,
|
||||
elements,
|
||||
});
|
||||
}
|
||||
|
||||
fn declare_memory_import(&mut self, memory: Memory, module: &str, field: &str) {
|
||||
debug_assert_eq!(
|
||||
self.module.memory_plans.len(),
|
||||
self.module.imported_memories.len(),
|
||||
"Imported memories must be declared first"
|
||||
);
|
||||
let plan = MemoryPlan::for_memory(memory, &self.tunables);
|
||||
self.module.memory_plans.push(plan);
|
||||
|
||||
self.module
|
||||
.imported_memories
|
||||
.push((String::from(module), String::from(field)));
|
||||
}
|
||||
|
||||
fn declare_memory(&mut self, memory: Memory) {
|
||||
let plan = MemoryPlan::for_memory(memory, &self.tunables);
|
||||
self.module.memory_plans.push(plan);
|
||||
}
|
||||
|
||||
fn declare_data_initialization(
|
||||
&mut self,
|
||||
memory_index: MemoryIndex,
|
||||
base: Option<GlobalIndex>,
|
||||
offset: usize,
|
||||
data: &'data [u8],
|
||||
) {
|
||||
self.lazy.data_initializers.push(DataInitializer {
|
||||
memory_index,
|
||||
base,
|
||||
offset,
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
fn declare_func_export(&mut self, func_index: FuncIndex, name: &str) {
|
||||
self.module
|
||||
.exports
|
||||
.insert(String::from(name), Export::Function(func_index));
|
||||
}
|
||||
|
||||
fn declare_table_export(&mut self, table_index: TableIndex, name: &str) {
|
||||
self.module
|
||||
.exports
|
||||
.insert(String::from(name), Export::Table(table_index));
|
||||
}
|
||||
|
||||
fn declare_memory_export(&mut self, memory_index: MemoryIndex, name: &str) {
|
||||
self.module
|
||||
.exports
|
||||
.insert(String::from(name), Export::Memory(memory_index));
|
||||
}
|
||||
|
||||
fn declare_global_export(&mut self, global_index: GlobalIndex, name: &str) {
|
||||
self.module
|
||||
.exports
|
||||
.insert(String::from(name), Export::Global(global_index));
|
||||
}
|
||||
|
||||
fn declare_start_func(&mut self, func_index: FuncIndex) {
|
||||
debug_assert!(self.module.start_func.is_none());
|
||||
self.module.start_func = Some(func_index);
|
||||
}
|
||||
|
||||
fn define_function_body(&mut self, body_bytes: &'data [u8]) -> WasmResult<()> {
|
||||
self.lazy.function_body_inputs.push(body_bytes);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// The result of translating via `ModuleEnvironment`.
|
||||
pub struct ModuleTranslation<'data, 'module> {
|
||||
/// Compilation setting flags.
|
||||
pub isa: &'module isa::TargetIsa,
|
||||
|
||||
/// Module information.
|
||||
pub module: &'module Module,
|
||||
|
||||
/// Pointers into the raw data buffer.
|
||||
pub lazy: LazyContents<'data>,
|
||||
|
||||
/// Tunable parameters.
|
||||
pub tunables: Tunables,
|
||||
}
|
||||
|
||||
impl<'data, 'module> ModuleTranslation<'data, 'module> {
|
||||
/// Return a new `FuncEnvironment` for translating a function.
|
||||
pub fn func_env(&self) -> FuncEnvironment {
|
||||
FuncEnvironment::new(self.isa, &self.module)
|
||||
}
|
||||
}
|
||||
|
||||
/// Add environment-specific function parameters.
|
||||
pub fn translate_signature(mut sig: ir::Signature, pointer_type: ir::Type) -> ir::Signature {
|
||||
sig.params
|
||||
.push(AbiParam::special(pointer_type, ArgumentPurpose::VMContext));
|
||||
sig
|
||||
}
|
||||
@@ -1,6 +1,12 @@
|
||||
//! Offsets and sizes of various structs in wasmtime-execute's vmcontext
|
||||
//! module.
|
||||
|
||||
use cranelift_codegen::ir;
|
||||
use cranelift_wasm::{
|
||||
DefinedGlobalIndex, DefinedMemoryIndex, DefinedTableIndex, FuncIndex, GlobalIndex, MemoryIndex,
|
||||
TableIndex,
|
||||
};
|
||||
|
||||
/// This class computes offsets to fields within `VMContext` and other
|
||||
/// related structs that JIT code accesses directly.
|
||||
pub struct VMOffsets {
|
||||
@@ -14,7 +20,86 @@ impl VMOffsets {
|
||||
}
|
||||
}
|
||||
|
||||
/// Offsets for `wasmtime_execute::VMMemoryDefinition`.
|
||||
/// Offsets for `*const VMFunctionBody`.
|
||||
impl VMOffsets {
|
||||
/// The size of the `current_elements` field.
|
||||
#[allow(clippy::identity_op)]
|
||||
pub fn size_of_vmfunction_body_ptr(&self) -> u8 {
|
||||
1 * self.pointer_size
|
||||
}
|
||||
}
|
||||
|
||||
/// Offsets for `VMTableImport`.
|
||||
impl VMOffsets {
|
||||
/// The offset of the `from` field.
|
||||
#[allow(clippy::erasing_op)]
|
||||
pub fn vmtable_import_from(&self) -> u8 {
|
||||
0 * self.pointer_size
|
||||
}
|
||||
|
||||
/// The offset of the `vmctx` field.
|
||||
#[allow(clippy::identity_op)]
|
||||
pub fn vmtable_import_vmctx(&self) -> u8 {
|
||||
1 * self.pointer_size
|
||||
}
|
||||
|
||||
/// Return the size of `VMTableImport`.
|
||||
pub fn size_of_vmtable_import(&self) -> u8 {
|
||||
2 * self.pointer_size
|
||||
}
|
||||
}
|
||||
|
||||
/// Offsets for `VMTableDefinition`.
|
||||
impl VMOffsets {
|
||||
/// The offset of the `base` field.
|
||||
#[allow(clippy::erasing_op)]
|
||||
pub fn vmtable_definition_base(&self) -> u8 {
|
||||
0 * self.pointer_size
|
||||
}
|
||||
|
||||
/// The offset of the `current_elements` field.
|
||||
#[allow(clippy::identity_op)]
|
||||
pub fn vmtable_definition_current_elements(&self) -> u8 {
|
||||
1 * self.pointer_size
|
||||
}
|
||||
|
||||
/// The size of the `current_elements` field.
|
||||
pub fn size_of_vmtable_definition_current_elements(&self) -> u8 {
|
||||
4
|
||||
}
|
||||
|
||||
/// Return the size of `VMTableDefinition`.
|
||||
pub fn size_of_vmtable_definition(&self) -> u8 {
|
||||
2 * self.pointer_size
|
||||
}
|
||||
|
||||
/// The type of the `current_elements` field.
|
||||
pub fn type_of_vmtable_definition_current_elements(&self) -> ir::Type {
|
||||
ir::Type::int(u16::from(self.size_of_vmtable_definition_current_elements()) * 8).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
/// Offsets for `VMMemoryImport`.
|
||||
impl VMOffsets {
|
||||
/// The offset of the `from` field.
|
||||
#[allow(clippy::erasing_op)]
|
||||
pub fn vmmemory_import_from(&self) -> u8 {
|
||||
0 * self.pointer_size
|
||||
}
|
||||
|
||||
/// The offset of the `vmctx` field.
|
||||
#[allow(clippy::identity_op)]
|
||||
pub fn vmmemory_import_vmctx(&self) -> u8 {
|
||||
1 * self.pointer_size
|
||||
}
|
||||
|
||||
/// Return the size of `VMMemoryImport`.
|
||||
pub fn size_of_vmmemory_import(&self) -> u8 {
|
||||
2 * self.pointer_size
|
||||
}
|
||||
}
|
||||
|
||||
/// Offsets for `VMMemoryDefinition`.
|
||||
impl VMOffsets {
|
||||
/// The offset of the `base` field.
|
||||
#[allow(clippy::erasing_op)]
|
||||
@@ -28,44 +113,23 @@ impl VMOffsets {
|
||||
1 * self.pointer_size
|
||||
}
|
||||
|
||||
/// The size of the `current_length` field.
|
||||
pub fn size_of_vmmemory_definition_current_length(&self) -> u8 {
|
||||
4
|
||||
}
|
||||
|
||||
/// Return the size of `VMMemoryDefinition`.
|
||||
pub fn size_of_vmmemory_definition(&self) -> u8 {
|
||||
2 * self.pointer_size
|
||||
}
|
||||
}
|
||||
|
||||
/// Offsets for `wasmtime_execute::VMMemoryImport`.
|
||||
impl VMOffsets {
|
||||
/// The offset of the `from` field.
|
||||
#[allow(clippy::erasing_op)]
|
||||
pub fn vmmemory_import_from(&self) -> u8 {
|
||||
0 * self.pointer_size
|
||||
}
|
||||
|
||||
/// Return the size of `VMMemoryImport`.
|
||||
#[allow(clippy::identity_op)]
|
||||
pub fn size_of_vmmemory_import(&self) -> u8 {
|
||||
1 * self.pointer_size
|
||||
/// The type of the `current_length` field.
|
||||
pub fn type_of_vmmemory_definition_current_length(&self) -> ir::Type {
|
||||
ir::Type::int(u16::from(self.size_of_vmmemory_definition_current_length()) * 8).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
/// Offsets for `wasmtime_execute::VMMemory`.
|
||||
impl VMOffsets {
|
||||
/// Return the size of `VMMemory`.
|
||||
pub fn size_of_vmmemory(&self) -> u8 {
|
||||
2 * self.pointer_size
|
||||
}
|
||||
}
|
||||
|
||||
/// Offsets for `wasmtime_execute::VMGlobalDefinition`.
|
||||
impl VMOffsets {
|
||||
/// Return the size of `VMGlobalDefinition`.
|
||||
pub fn size_of_vmglobal_definition(&self) -> u8 {
|
||||
8
|
||||
}
|
||||
}
|
||||
|
||||
/// Offsets for `wasmtime_execute::VMGlobalImport`.
|
||||
/// Offsets for `VMGlobalImport`.
|
||||
impl VMOffsets {
|
||||
/// The offset of the `from` field.
|
||||
#[allow(clippy::erasing_op)]
|
||||
@@ -80,67 +144,23 @@ impl VMOffsets {
|
||||
}
|
||||
}
|
||||
|
||||
/// Offsets for `wasmtime_execute::VMGlobal`.
|
||||
/// Offsets for `VMGlobalDefinition`.
|
||||
impl VMOffsets {
|
||||
/// Return the size of `VMGlobal`.
|
||||
pub fn size_of_vmglobal(&self) -> u8 {
|
||||
assert!(self.size_of_vmglobal_import() <= self.size_of_vmglobal_definition());
|
||||
self.size_of_vmglobal_definition()
|
||||
/// Return the size of `VMGlobalDefinition`.
|
||||
pub fn size_of_vmglobal_definition(&self) -> u8 {
|
||||
8
|
||||
}
|
||||
}
|
||||
|
||||
/// Offsets for `wasmtime_execute::VMTableDefinition`.
|
||||
/// Offsets for `VMSharedSignatureIndex`.
|
||||
impl VMOffsets {
|
||||
/// The offset of the `base` field.
|
||||
#[allow(clippy::erasing_op)]
|
||||
pub fn vmtable_definition_base(&self) -> u8 {
|
||||
0 * self.pointer_size
|
||||
}
|
||||
|
||||
/// The offset of the `current_elements` field.
|
||||
#[allow(clippy::identity_op)]
|
||||
pub fn vmtable_definition_current_elements(&self) -> u8 {
|
||||
1 * self.pointer_size
|
||||
}
|
||||
|
||||
/// Return the size of `VMTableDefinition`.
|
||||
pub fn size_of_vmtable_definition(&self) -> u8 {
|
||||
2 * self.pointer_size
|
||||
}
|
||||
}
|
||||
|
||||
/// Offsets for `wasmtime_execute::VMTableImport`.
|
||||
impl VMOffsets {
|
||||
/// The offset of the `from` field.
|
||||
#[allow(clippy::erasing_op)]
|
||||
pub fn vmtable_import_from(&self) -> u8 {
|
||||
0 * self.pointer_size
|
||||
}
|
||||
|
||||
/// Return the size of `VMTableImport`.
|
||||
#[allow(clippy::identity_op)]
|
||||
pub fn size_of_vmtable_import(&self) -> u8 {
|
||||
1 * self.pointer_size
|
||||
}
|
||||
}
|
||||
|
||||
/// Offsets for `wasmtime_execute::VMTable`.
|
||||
impl VMOffsets {
|
||||
/// Return the size of `VMTable`.
|
||||
pub fn size_of_vmtable(&self) -> u8 {
|
||||
2 * self.pointer_size
|
||||
}
|
||||
}
|
||||
|
||||
/// Offsets for `wasmtime_execute::VMSignatureId`.
|
||||
impl VMOffsets {
|
||||
/// Return the size of `VMSignatureId`.
|
||||
pub fn size_of_vmsignature_id(&self) -> u8 {
|
||||
/// Return the size of `VMSharedSignatureIndex`.
|
||||
pub fn size_of_vmshared_signature_index(&self) -> u8 {
|
||||
4
|
||||
}
|
||||
}
|
||||
|
||||
/// Offsets for `wasmtime_execute::VMCallerCheckedAnyfunc`.
|
||||
/// Offsets for `VMCallerCheckedAnyfunc`.
|
||||
impl VMOffsets {
|
||||
/// The offset of the `func_ptr` field.
|
||||
#[allow(clippy::erasing_op)]
|
||||
@@ -148,131 +168,208 @@ impl VMOffsets {
|
||||
0 * self.pointer_size
|
||||
}
|
||||
|
||||
/// The offset of the `type_id` field.
|
||||
/// The offset of the `type_index` field.
|
||||
#[allow(clippy::identity_op)]
|
||||
pub fn vmcaller_checked_anyfunc_type_id(&self) -> u8 {
|
||||
pub fn vmcaller_checked_anyfunc_type_index(&self) -> u8 {
|
||||
1 * self.pointer_size
|
||||
}
|
||||
|
||||
/// Return the size of `VMTable`.
|
||||
/// Return the size of `VMCallerCheckedAnyfunc`.
|
||||
pub fn size_of_vmcaller_checked_anyfunc(&self) -> u8 {
|
||||
2 * self.pointer_size
|
||||
}
|
||||
}
|
||||
|
||||
/// Offsets for `wasmtime_execute::VMContext`.
|
||||
/// Offsets for `VMContext`.
|
||||
impl VMOffsets {
|
||||
/// The offset of the `memories` field.
|
||||
/// The offset of the `tables` field.
|
||||
#[allow(clippy::erasing_op)]
|
||||
pub fn vmctx_memories(&self) -> u8 {
|
||||
pub fn vmctx_imported_functions(&self) -> u8 {
|
||||
0 * self.pointer_size
|
||||
}
|
||||
|
||||
/// The offset of the `globals` field.
|
||||
/// The offset of the `tables` field.
|
||||
#[allow(clippy::identity_op)]
|
||||
pub fn vmctx_globals(&self) -> u8 {
|
||||
pub fn vmctx_imported_tables(&self) -> u8 {
|
||||
1 * self.pointer_size
|
||||
}
|
||||
|
||||
/// The offset of the `memories` field.
|
||||
pub fn vmctx_imported_memories(&self) -> u8 {
|
||||
2 * self.pointer_size
|
||||
}
|
||||
|
||||
/// The offset of the `globals` field.
|
||||
pub fn vmctx_imported_globals(&self) -> u8 {
|
||||
3 * self.pointer_size
|
||||
}
|
||||
|
||||
/// The offset of the `tables` field.
|
||||
pub fn vmctx_tables(&self) -> u8 {
|
||||
2 * self.pointer_size
|
||||
4 * self.pointer_size
|
||||
}
|
||||
|
||||
/// The offset of the `memories` field.
|
||||
pub fn vmctx_memories(&self) -> u8 {
|
||||
5 * self.pointer_size
|
||||
}
|
||||
|
||||
/// The offset of the `globals` field.
|
||||
pub fn vmctx_globals(&self) -> u8 {
|
||||
6 * self.pointer_size
|
||||
}
|
||||
|
||||
/// The offset of the `signature_ids` field.
|
||||
pub fn vmctx_signature_ids(&self) -> u8 {
|
||||
3 * self.pointer_size
|
||||
7 * self.pointer_size
|
||||
}
|
||||
|
||||
/// Return the size of `VMContext`.
|
||||
#[allow(dead_code)]
|
||||
pub fn size_of_vmctx(&self) -> u8 {
|
||||
4 * self.pointer_size
|
||||
8 * self.pointer_size
|
||||
}
|
||||
|
||||
/// Return the offset from the `memories` pointer to `VMMemory` index `index`.
|
||||
pub fn index_vmmemory(&self, index: u32) -> i32 {
|
||||
/// Return the offset from the `imported_tables` pointer to `VMTableImport` index `index`.
|
||||
fn index_vmtable_import(&self, index: TableIndex) -> i32 {
|
||||
cast::i32(
|
||||
index
|
||||
.checked_mul(u32::from(self.size_of_vmmemory()))
|
||||
.as_u32()
|
||||
.checked_mul(u32::from(self.size_of_vmtable_import()))
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Return the offset from the `globals` pointer to `VMGlobal` index `index`.
|
||||
pub fn index_vmglobal(&self, index: u32) -> i32 {
|
||||
/// Return the offset from the `tables` pointer to `VMTableDefinition` index `index`.
|
||||
fn index_vmtable_definition(&self, index: DefinedTableIndex) -> i32 {
|
||||
cast::i32(
|
||||
index
|
||||
.checked_mul(u32::from(self.size_of_vmglobal()))
|
||||
.as_u32()
|
||||
.checked_mul(u32::from(self.size_of_vmtable_definition()))
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Return the offset from the `tables` pointer to `VMTable` index `index`.
|
||||
pub fn index_vmtable(&self, index: u32) -> i32 {
|
||||
/// Return the offset from the `imported_memories` pointer to `VMMemoryImport` index `index`.
|
||||
fn index_vmmemory_import(&self, index: MemoryIndex) -> i32 {
|
||||
cast::i32(
|
||||
index
|
||||
.checked_mul(u32::from(self.size_of_vmtable()))
|
||||
.as_u32()
|
||||
.checked_mul(u32::from(self.size_of_vmmemory_import()))
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Return the offset from the `memories` pointer to `VMMemoryDefinition` index `index`.
|
||||
fn index_vmmemory_definition(&self, index: DefinedMemoryIndex) -> i32 {
|
||||
cast::i32(
|
||||
index
|
||||
.as_u32()
|
||||
.checked_mul(u32::from(self.size_of_vmmemory_definition()))
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Return the offset from the `imported_globals` pointer to `VMGlobalImport` index `index`.
|
||||
fn index_vmglobal_import(&self, index: GlobalIndex) -> i32 {
|
||||
cast::i32(
|
||||
index
|
||||
.as_u32()
|
||||
.checked_mul(u32::from(self.size_of_vmglobal_import()))
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Return the offset from the `imported_functions` pointer to the
|
||||
/// `*const VMFunctionBody` index `index`.
|
||||
pub fn index_vmfunction_body_import(&self, index: FuncIndex) -> i32 {
|
||||
cast::i32(
|
||||
index
|
||||
.as_u32()
|
||||
.checked_mul(u32::from(self.size_of_vmfunction_body_ptr()))
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Return the offset from the `tables` pointer to the `from` field in
|
||||
/// `VMTableImport` index `index`.
|
||||
pub fn index_vmtable_import_from(&self, index: TableIndex) -> i32 {
|
||||
self.index_vmtable_import(index)
|
||||
.checked_add(i32::from(self.vmtable_import_from()))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Return the offset from the `tables` pointer to the `base` field in
|
||||
/// `VMTableDefinition` index `index`.
|
||||
pub fn index_vmtable_definition_base(&self, index: DefinedTableIndex) -> i32 {
|
||||
self.index_vmtable_definition(index)
|
||||
.checked_add(i32::from(self.vmtable_definition_base()))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Return the offset from the `tables` pointer to the `current_elements` field in
|
||||
/// `VMTableDefinition` index `index`.
|
||||
pub fn index_vmtable_definition_current_elements(&self, index: DefinedTableIndex) -> i32 {
|
||||
self.index_vmtable_definition(index)
|
||||
.checked_add(i32::from(self.vmtable_definition_current_elements()))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Return the offset from the `memories` pointer to the `from` field in
|
||||
/// `VMMemoryImport` index `index`.
|
||||
pub fn index_vmmemory_import_from(&self, index: MemoryIndex) -> i32 {
|
||||
self.index_vmmemory_import(index)
|
||||
.checked_add(i32::from(self.vmmemory_import_from()))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Return the offset from the `memories` pointer to the `vmctx` field in
|
||||
/// `VMMemoryImport` index `index`.
|
||||
pub fn index_vmmemory_import_vmctx(&self, index: MemoryIndex) -> i32 {
|
||||
self.index_vmmemory_import(index)
|
||||
.checked_add(i32::from(self.vmmemory_import_vmctx()))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Return the offset from the `memories` pointer to the `base` field in
|
||||
/// `VMMemory` index `index`.
|
||||
pub fn index_vmmemory_definition_base(&self, index: u32) -> i32 {
|
||||
self.index_vmmemory(index)
|
||||
/// `VMMemoryDefinition` index `index`.
|
||||
pub fn index_vmmemory_definition_base(&self, index: DefinedMemoryIndex) -> i32 {
|
||||
self.index_vmmemory_definition(index)
|
||||
.checked_add(i32::from(self.vmmemory_definition_base()))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Return the offset from the `memories` pointer to the `current_length` field in
|
||||
/// `VMMemoryDefinition` index `index`.
|
||||
pub fn index_vmmemory_definition_current_length(&self, index: u32) -> i32 {
|
||||
self.index_vmmemory(index)
|
||||
pub fn index_vmmemory_definition_current_length(&self, index: DefinedMemoryIndex) -> i32 {
|
||||
self.index_vmmemory_definition(index)
|
||||
.checked_add(i32::from(self.vmmemory_definition_current_length()))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Return the offset from the `memories` pointer to the `from` field in
|
||||
/// `VMMemoryImport` index `index`.
|
||||
pub fn index_vmmemory_import_from(&self, index: u32) -> i32 {
|
||||
self.index_vmmemory(index)
|
||||
.checked_add(i32::from(self.vmmemory_import_from()))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Return the offset from the `globals` pointer to the `from` field in
|
||||
/// `VMGlobal` index `index`.
|
||||
pub fn index_vmglobal_import_from(&self, index: u32) -> i32 {
|
||||
self.index_vmglobal(index)
|
||||
/// Return the offset from the `imported_globals` pointer to the `from` field in
|
||||
/// `VMGlobalImport` index `index`.
|
||||
pub fn index_vmglobal_import_from(&self, index: GlobalIndex) -> i32 {
|
||||
self.index_vmglobal_import(index)
|
||||
.checked_add(i32::from(self.vmglobal_import_from()))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Return the offset from the `tables` pointer to the `base` field in
|
||||
/// `VMTable` index `index`.
|
||||
pub fn index_vmtable_definition_base(&self, index: u32) -> i32 {
|
||||
self.index_vmtable(index)
|
||||
.checked_add(i32::from(self.vmtable_definition_base()))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Return the offset from the `tables` pointer to the `current_elements` field in
|
||||
/// `VMTable` index `index`.
|
||||
pub fn index_vmtable_definition_current_elements(&self, index: u32) -> i32 {
|
||||
self.index_vmtable(index)
|
||||
.checked_add(i32::from(self.vmtable_definition_current_elements()))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Return the offset from the `tables` pointer to the `from` field in
|
||||
/// `VMTableImport` index `index`.
|
||||
pub fn index_vmtable_import_from(&self, index: u32) -> i32 {
|
||||
self.index_vmtable(index)
|
||||
.checked_add(i32::from(self.vmtable_import_from()))
|
||||
.unwrap()
|
||||
/// Return the offset from the `globals` pointer to the `VMGlobalDefinition`
|
||||
/// index `index`.
|
||||
pub fn index_vmglobal_definition(&self, index: DefinedGlobalIndex) -> i32 {
|
||||
cast::i32(
|
||||
index
|
||||
.as_u32()
|
||||
.checked_mul(u32::from(self.size_of_vmglobal_definition()))
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user