Improved offsets in module using pointer size rather than fixed numbers

This commit is contained in:
Syrus Akbary
2018-11-14 23:56:02 -08:00
parent 92d8de7df7
commit ef09889df9
2 changed files with 46 additions and 17 deletions

View File

@ -83,6 +83,10 @@ pub struct Instance {
// C-like pointers to data (heaps, globals, tables)
pub data_pointers: DataPointers,
// Default memory bound
// TODO: Support for only one LinearMemory for now.
pub default_memory_bound: i32,
/// WebAssembly table data
// pub tables: Arc<Vec<RwLock<Vec<usize>>>>,
pub tables: Arc<Vec<Vec<usize>>>,
@ -104,10 +108,6 @@ pub struct Instance {
pub start_func: Option<FuncIndex>,
// Region start memory location
// 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

View File

@ -38,6 +38,35 @@ use cranelift_wasm::{
use super::errors::ErrorKind;
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.
fn get_func_name(func_index: FuncIndex) -> ir::ExternalName {
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
let base = func.create_global_value(ir::GlobalValueData::Load {
base: vmctx,
offset: Offset32::new(0),
global_type: self.pointer_type(),
offset: Offset32::new(native_pointer_size() * 0),
global_type: native_pointer_type(),
});
// 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 {
base: base,
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 {
@ -369,28 +398,28 @@ impl<'environment> FuncEnvironmentTrait for FuncEnvironment<'environment> {
}
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`.
let vmctx = func.create_global_value(ir::GlobalValueData::VMContext);
let heap_base_addr = func.create_global_value(ir::GlobalValueData::Load {
base: vmctx,
offset: Offset32::new(8),
global_type: self.pointer_type(),
offset: Offset32::new(native_pointer_size() * 1), // 8
global_type: native_pointer_type(),
});
let pointer_bytes = self.pointer_bytes();
let memories_offset = (index * pointer_bytes as usize) as i32;
let memories_offset = 0; // (index * pointer_bytes as usize) as i32;
// We de-reference the vm_context.memories addr
let heap_base = func.create_global_value(ir::GlobalValueData::Load {
base: heap_base_addr,
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 {
base: vmctx,
offset: Offset32::new(120),
offset: Offset32::new(native_pointer_size() * 3), // 24
global_type: I32,
});
@ -454,15 +483,15 @@ impl<'environment> FuncEnvironmentTrait for FuncEnvironment<'environment> {
let globals_base_addr = func.create_global_value(ir::GlobalValueData::Load {
base: vmctx,
offset: Offset32::new(16),
global_type: self.pointer_type(),
offset: Offset32::new(native_pointer_size() * 2),
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 {
base: globals_base_addr,
offset: Imm64::new(offset),
global_type: self.pointer_type(),
global_type: native_pointer_type(),
});
GlobalVariable::Memory {
gv: iadd,