Update wasmstandalone for API changes.

This updates to the latest faerie and cretonne API changes.
This commit is contained in:
Dan Gohman
2018-02-22 09:44:02 -08:00
parent ebf2c3a17e
commit f276a021cb
9 changed files with 115 additions and 47 deletions

View File

@@ -38,22 +38,36 @@ fn relocate(compilation: &mut Compilation, relocations: &wasmstandalone_runtime:
// The relocations are relative to the relocation's address plus four bytes
// TODO: Support architectures other than x64, and other reloc kinds.
for (i, function_relocs) in relocations.iter().enumerate() {
for &(_reloc, func_index, offset) in function_relocs {
let target_func_address: isize = compilation.functions[func_index].as_ptr() as isize;
for ref r in function_relocs {
let target_func_address: isize = compilation.functions[r.func_index].as_ptr() as isize;
let body = &mut compilation.functions[i];
unsafe {
let reloc_address: isize = body.as_mut_ptr().offset(offset as isize + 4) as isize;
let reloc_delta_i32: i32 = (target_func_address - reloc_address) as i32;
let reloc_address = body.as_mut_ptr().offset(r.offset as isize + 4) as isize;
let reloc_addend = r.addend as isize;
let reloc_delta_i32 = (target_func_address - reloc_address + reloc_addend) as i32;
write_unaligned(reloc_address as *mut i32, reloc_delta_i32);
}
}
}
}
/// Create the VmCtx data structure for the JIT'd code to use. This must
/// match the VmCtx layout in the runtime.
fn make_vmctx(instance: &mut wasmstandalone_runtime::Instance) -> Vec<*mut u8> {
let mut memories = Vec::new();
let mut vmctx = Vec::new();
vmctx.push(instance.globals.as_mut_ptr());
for mem in &mut instance.memories {
memories.push(mem.as_mut_ptr());
}
vmctx.push(memories.as_mut_ptr() as *mut u8);
vmctx
}
/// Jumps to the code region of memory and execute the start function of the module.
pub fn execute(
compilation: &wasmstandalone_runtime::Compilation,
_instance: &wasmstandalone_runtime::Instance,
instance: &mut wasmstandalone_runtime::Instance,
) -> Result<(), String> {
let start_index = compilation.module.start_func.ok_or_else(|| {
String::from("No start function defined, aborting execution")
@@ -70,17 +84,20 @@ pub fn execute(
Err(err) => {
return Err(format!(
"failed to give executable permission to code: {}",
err.description()
err
))
}
}
let vmctx = make_vmctx(instance);
// Rather than writing inline assembly to jump to the code region, we use the fact that
// the Rust ABI for calling a function with no arguments and no return matches the one of
// the generated code.Thanks to this, we can transmute the code region into a first-class
// Rust function and call it.
unsafe {
let start_func = transmute::<_, fn()>(code_buf.as_ptr());
start_func();
let start_func = transmute::<_, fn(*const *mut u8)>(code_buf.as_ptr());
start_func(vmctx.as_ptr());
}
Ok(())
}