Improved formatting

This commit is contained in:
Syrus Akbary
2018-10-15 03:03:00 +02:00
parent 78cf7800e5
commit 8d790d78f9
4 changed files with 59 additions and 49 deletions

View File

@ -6,22 +6,22 @@
//! synchronously instantiate a given webassembly::Module object. However, the //! synchronously instantiate a given webassembly::Module object. However, the
//! primary way to get an Instance is through the asynchronous //! primary way to get an Instance is through the asynchronous
//! webassembly::instantiateStreaming() function. //! webassembly::instantiateStreaming() function.
use cranelift_codegen::{isa, Context};
use cranelift_entity::EntityRef;
use cranelift_wasm::{FuncIndex, GlobalInit};
use memmap::MmapMut;
use region;
use spin::RwLock;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::sync::Arc; use std::sync::Arc;
use std::{mem, slice}; use std::{mem, slice};
use memmap::MmapMut;
use spin::RwLock;
use region;
use cranelift_entity::EntityRef;
use cranelift_wasm::{FuncIndex, GlobalInit};
use cranelift_codegen::{Context, isa};
use super::super::common::slice::{BoundedSlice, UncheckedSlice};
use super::errors::ErrorKind; use super::errors::ErrorKind;
use super::memory::LinearMemory; use super::memory::LinearMemory;
use super::relocation::{RelocSink, TrapSink};
use super::module::Module; use super::module::Module;
use super::module::{DataInitializer, Exportable}; use super::module::{DataInitializer, Exportable};
use super::super::common::slice::{BoundedSlice, UncheckedSlice}; use super::relocation::{RelocSink, TrapSink};
pub fn get_function_addr( pub fn get_function_addr(
base: *const (), base: *const (),
@ -96,10 +96,15 @@ impl Instance {
// Compile the functions // Compile the functions
for function_body in module.info.function_bodies.values() { for function_body in module.info.function_bodies.values() {
let mut context = Context::for_function(function_body.to_owned()); let mut func_context = Context::for_function(function_body.to_owned());
let code_size_offset = context.compile(&*isa).map_err(|e| ErrorKind::CompileError(e.to_string()))? as usize; // func_context.verify(&*isa).map_err(|e| ErrorKind::CompileError(e.to_string()))?;
// func_context.verify_locations(&*isa).map_err(|e| ErrorKind::CompileError(e.to_string()))?;
let code_size_offset = func_context
.compile(&*isa)
.map_err(|e| ErrorKind::CompileError(e.to_string()))?
as usize;
total_size += code_size_offset; total_size += code_size_offset;
context_and_offsets.push((context, code_size_offset)); context_and_offsets.push((func_context, code_size_offset));
} }
// Allocate the total memory for this functions // Allocate the total memory for this functions
@ -111,8 +116,13 @@ impl Instance {
let mut trap_sink = TrapSink::new(*func_offset); let mut trap_sink = TrapSink::new(*func_offset);
let mut reloc_sink = RelocSink::new(); let mut reloc_sink = RelocSink::new();
unsafe { unsafe {
func_context.emit_to_memory(&*isa, (region_start as usize + func_offset) as *mut u8, &mut reloc_sink, &mut trap_sink); func_context.emit_to_memory(
} &*isa,
(region_start as usize + func_offset) as *mut u8,
&mut reloc_sink,
&mut trap_sink,
);
};
} }
// Set protection of this memory region to Read + Execute // Set protection of this memory region to Read + Execute
@ -122,6 +132,7 @@ impl Instance {
.expect("unable to make memory readable+executable"); .expect("unable to make memory readable+executable");
} }
} }
// instantiate_tables // instantiate_tables
{ {
// Reserve table space // Reserve table space
@ -151,7 +162,7 @@ impl Instance {
table[base + table_element.offset + i] = func_addr as _; table[base + table_element.offset + i] = func_addr as _;
} }
} }
}; }
// instantiate_memories // instantiate_memories
{ {
@ -169,7 +180,7 @@ impl Instance {
let to_init = &mut mem_mut[init.offset..init.offset + init.data.len()]; let to_init = &mut mem_mut[init.offset..init.offset + init.data.len()];
to_init.copy_from_slice(&init.data); to_init.copy_from_slice(&init.data);
} }
}; }
// instantiate_globals // instantiate_globals
{ {
@ -193,7 +204,7 @@ impl Instance {
globals_data[i] = value; globals_data[i] = value;
} }
}; }
Ok(Instance { Ok(Instance {
tables: Arc::new(tables.into_iter().map(|table| RwLock::new(table)).collect()), tables: Arc::new(tables.into_iter().map(|table| RwLock::new(table)).collect()),

View File

@ -30,10 +30,19 @@ impl LinearMemory {
pub fn new(initial: u32, maximum: Option<u32>) -> Self { pub fn new(initial: u32, maximum: Option<u32>) -> Self {
assert!(initial <= MAX_PAGES); assert!(initial <= MAX_PAGES);
assert!(maximum.is_none() || maximum.unwrap() <= MAX_PAGES); assert!(maximum.is_none() || maximum.unwrap() <= MAX_PAGES);
debug!("Instantiate LinearMemory(initial={:?}, maximum={:?})", initial, maximum); debug!(
"Instantiate LinearMemory(initial={:?}, maximum={:?})",
initial, maximum
);
let len = PAGE_SIZE * match maximum { let len = PAGE_SIZE * match maximum {
Some(val) => val, Some(val) => val,
None => if initial > 0 { initial } else { 1 }, None => {
if initial > 0 {
initial
} else {
1
}
}
}; };
let mmap = MmapMut::map_anon(len as usize).unwrap(); let mmap = MmapMut::map_anon(len as usize).unwrap();
debug!("LinearMemory instantiated"); debug!("LinearMemory instantiated");

View File

@ -2,8 +2,8 @@ pub mod errors;
pub mod instance; pub mod instance;
pub mod memory; pub mod memory;
pub mod module; pub mod module;
pub mod utils;
pub mod relocation; pub mod relocation;
pub mod utils;
use cranelift_native; use cranelift_native;
use std::panic; use std::panic;

View File

@ -1,5 +1,5 @@
use cranelift_codegen::binemit; use cranelift_codegen::binemit;
use cranelift_codegen::ir::{self, TrapCode, SourceLoc, ExternalName}; use cranelift_codegen::ir::{self, ExternalName, SourceLoc, TrapCode};
#[derive(Debug)] #[derive(Debug)]
pub struct Relocation { pub struct Relocation {
@ -18,7 +18,6 @@ pub enum RelocationType {
Intrinsic(String), Intrinsic(String),
} }
/// Implementation of a relocation sink that just saves all the information for later /// Implementation of a relocation sink that just saves all the information for later
pub struct RelocSink { pub struct RelocSink {
// func: &'func ir::Function, // func: &'func ir::Function,
@ -48,35 +47,28 @@ impl binemit::RelocSink for RelocSink {
namespace: 0, namespace: 0,
index, index,
} => { } => {
self.func_relocs.push( self.func_relocs.push((
( Relocation {
Relocation { reloc,
reloc, offset,
offset, addend,
addend, },
}, RelocationType::Normal(index as _),
RelocationType::Normal(index as _), ));
) }
); ExternalName::TestCase { length, ascii } => {
},
ExternalName::TestCase {
length,
ascii,
} => {
let (slice, _) = ascii.split_at(length as usize); let (slice, _) = ascii.split_at(length as usize);
let name = String::from_utf8(slice.to_vec()).unwrap(); let name = String::from_utf8(slice.to_vec()).unwrap();
self.func_relocs.push( self.func_relocs.push((
( Relocation {
Relocation { reloc,
reloc, offset,
offset, addend,
addend, },
}, RelocationType::Intrinsic(name),
RelocationType::Intrinsic(name), ));
) }
);
},
_ => { _ => {
unimplemented!(); unimplemented!();
} }
@ -100,8 +92,6 @@ impl RelocSink {
} }
} }
pub struct TrapData { pub struct TrapData {
pub offset: usize, pub offset: usize,
pub code: TrapCode, pub code: TrapCode,