mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-29 16:41:33 +00:00
Improved offsets in module using pointer size rather than fixed numbers
This commit is contained in:
@ -83,6 +83,10 @@ pub struct Instance {
|
|||||||
// C-like pointers to data (heaps, globals, tables)
|
// C-like pointers to data (heaps, globals, tables)
|
||||||
pub data_pointers: DataPointers,
|
pub data_pointers: DataPointers,
|
||||||
|
|
||||||
|
// Default memory bound
|
||||||
|
// TODO: Support for only one LinearMemory for now.
|
||||||
|
pub default_memory_bound: i32,
|
||||||
|
|
||||||
/// WebAssembly table data
|
/// WebAssembly table data
|
||||||
// pub tables: Arc<Vec<RwLock<Vec<usize>>>>,
|
// pub tables: Arc<Vec<RwLock<Vec<usize>>>>,
|
||||||
pub tables: Arc<Vec<Vec<usize>>>,
|
pub tables: Arc<Vec<Vec<usize>>>,
|
||||||
@ -104,10 +108,6 @@ pub struct Instance {
|
|||||||
pub start_func: Option<FuncIndex>,
|
pub start_func: Option<FuncIndex>,
|
||||||
// Region start memory location
|
// Region start memory location
|
||||||
// code_base: *const (),
|
// code_base: *const (),
|
||||||
|
|
||||||
// Default memory bound
|
|
||||||
// TODO: Support for only one LinearMemory for now.
|
|
||||||
pub default_memory_bound: i32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Contains pointers to data (heaps, globals, tables) needed
|
/// Contains pointers to data (heaps, globals, tables) needed
|
||||||
|
@ -38,6 +38,35 @@ use cranelift_wasm::{
|
|||||||
use super::errors::ErrorKind;
|
use super::errors::ErrorKind;
|
||||||
use super::memory::LinearMemory;
|
use super::memory::LinearMemory;
|
||||||
|
|
||||||
|
/// Get the integer type used for representing pointers on this platform.
|
||||||
|
fn native_pointer_type() -> ir::Type {
|
||||||
|
if cfg!(target_pointer_width = "64") {
|
||||||
|
ir::types::I64
|
||||||
|
} else {
|
||||||
|
ir::types::I32
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Number of bytes in a native pointer.
|
||||||
|
pub fn native_pointer_size() -> i32 {
|
||||||
|
if cfg!(target_pointer_width = "64") {
|
||||||
|
8
|
||||||
|
} else {
|
||||||
|
4
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// /// Convert a TlsData offset into a `Offset32` for a global decl.
|
||||||
|
// fn offset32(offset: usize) -> ir::immediates::Offset32 {
|
||||||
|
// assert!(offset <= i32::max_value() as usize);
|
||||||
|
// (offset as i32).into()
|
||||||
|
// }
|
||||||
|
|
||||||
|
// /// Convert a usize offset into a `Imm64` for an iadd_imm.
|
||||||
|
// fn imm64(offset: usize) -> ir::immediates::Imm64 {
|
||||||
|
// (offset as i64).into()
|
||||||
|
// }
|
||||||
|
|
||||||
/// Compute a `ir::ExternalName` for a given wasm function index.
|
/// Compute a `ir::ExternalName` for a given wasm function index.
|
||||||
fn get_func_name(func_index: FuncIndex) -> ir::ExternalName {
|
fn get_func_name(func_index: FuncIndex) -> ir::ExternalName {
|
||||||
ir::ExternalName::user(0, func_index.index() as u32)
|
ir::ExternalName::user(0, func_index.index() as u32)
|
||||||
@ -337,8 +366,8 @@ impl<'environment> FuncEnvironmentTrait for FuncEnvironment<'environment> {
|
|||||||
// 0 is the offset of the vmctx.tables pointer respect to vmctx pointer
|
// 0 is the offset of the vmctx.tables pointer respect to vmctx pointer
|
||||||
let base = func.create_global_value(ir::GlobalValueData::Load {
|
let base = func.create_global_value(ir::GlobalValueData::Load {
|
||||||
base: vmctx,
|
base: vmctx,
|
||||||
offset: Offset32::new(0),
|
offset: Offset32::new(native_pointer_size() * 0),
|
||||||
global_type: self.pointer_type(),
|
global_type: native_pointer_type(),
|
||||||
});
|
});
|
||||||
|
|
||||||
// This will be 0 when the index is 0, not sure if the offset will work regardless
|
// This will be 0 when the index is 0, not sure if the offset will work regardless
|
||||||
@ -348,7 +377,7 @@ impl<'environment> FuncEnvironmentTrait for FuncEnvironment<'environment> {
|
|||||||
let base_gv = func.create_global_value(ir::GlobalValueData::Load {
|
let base_gv = func.create_global_value(ir::GlobalValueData::Load {
|
||||||
base: base,
|
base: base,
|
||||||
offset: Offset32::new(table_data_offset),
|
offset: Offset32::new(table_data_offset),
|
||||||
global_type: self.pointer_type(),
|
global_type: native_pointer_type(),
|
||||||
});
|
});
|
||||||
|
|
||||||
let bound_gv = func.create_global_value(ir::GlobalValueData::Load {
|
let bound_gv = func.create_global_value(ir::GlobalValueData::Load {
|
||||||
@ -369,28 +398,28 @@ impl<'environment> FuncEnvironmentTrait for FuncEnvironment<'environment> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn make_heap(&mut self, func: &mut ir::Function, index: MemoryIndex) -> ir::Heap {
|
fn make_heap(&mut self, func: &mut ir::Function, index: MemoryIndex) -> ir::Heap {
|
||||||
|
assert_eq!(index, 0, "Only one WebAssembly memory supported");
|
||||||
// Create a static heap whose base address is stored at `instance+8`.
|
// Create a static heap whose base address is stored at `instance+8`.
|
||||||
let vmctx = func.create_global_value(ir::GlobalValueData::VMContext);
|
let vmctx = func.create_global_value(ir::GlobalValueData::VMContext);
|
||||||
|
|
||||||
let heap_base_addr = func.create_global_value(ir::GlobalValueData::Load {
|
let heap_base_addr = func.create_global_value(ir::GlobalValueData::Load {
|
||||||
base: vmctx,
|
base: vmctx,
|
||||||
offset: Offset32::new(8),
|
offset: Offset32::new(native_pointer_size() * 1), // 8
|
||||||
global_type: self.pointer_type(),
|
global_type: native_pointer_type(),
|
||||||
});
|
});
|
||||||
|
|
||||||
let pointer_bytes = self.pointer_bytes();
|
let memories_offset = 0; // (index * pointer_bytes as usize) as i32;
|
||||||
let memories_offset = (index * pointer_bytes as usize) as i32;
|
|
||||||
|
|
||||||
// We de-reference the vm_context.memories addr
|
// We de-reference the vm_context.memories addr
|
||||||
let heap_base = func.create_global_value(ir::GlobalValueData::Load {
|
let heap_base = func.create_global_value(ir::GlobalValueData::Load {
|
||||||
base: heap_base_addr,
|
base: heap_base_addr,
|
||||||
offset: Offset32::new(memories_offset),
|
offset: Offset32::new(memories_offset),
|
||||||
global_type: self.pointer_type(),
|
global_type: native_pointer_type(),
|
||||||
});
|
});
|
||||||
|
|
||||||
let bound_gv = func.create_global_value(ir::GlobalValueData::Load {
|
let bound_gv = func.create_global_value(ir::GlobalValueData::Load {
|
||||||
base: vmctx,
|
base: vmctx,
|
||||||
offset: Offset32::new(120),
|
offset: Offset32::new(native_pointer_size() * 3), // 24
|
||||||
global_type: I32,
|
global_type: I32,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -454,15 +483,15 @@ impl<'environment> FuncEnvironmentTrait for FuncEnvironment<'environment> {
|
|||||||
|
|
||||||
let globals_base_addr = func.create_global_value(ir::GlobalValueData::Load {
|
let globals_base_addr = func.create_global_value(ir::GlobalValueData::Load {
|
||||||
base: vmctx,
|
base: vmctx,
|
||||||
offset: Offset32::new(16),
|
offset: Offset32::new(native_pointer_size() * 2),
|
||||||
global_type: self.pointer_type(),
|
global_type: native_pointer_type(),
|
||||||
});
|
});
|
||||||
|
|
||||||
let offset = (index * 8) as i64;
|
let offset = (index * native_pointer_size() as usize) as i64;
|
||||||
let iadd = func.create_global_value(ir::GlobalValueData::IAddImm {
|
let iadd = func.create_global_value(ir::GlobalValueData::IAddImm {
|
||||||
base: globals_base_addr,
|
base: globals_base_addr,
|
||||||
offset: Imm64::new(offset),
|
offset: Imm64::new(offset),
|
||||||
global_type: self.pointer_type(),
|
global_type: native_pointer_type(),
|
||||||
});
|
});
|
||||||
GlobalVariable::Memory {
|
GlobalVariable::Memory {
|
||||||
gv: iadd,
|
gv: iadd,
|
||||||
|
Reference in New Issue
Block a user