Update to latest stable rustfmt-preview.

This commit is contained in:
Dan Gohman
2018-06-07 14:17:56 -07:00
parent 00fbd6d9bf
commit 99ee96ca16
7 changed files with 109 additions and 110 deletions

View File

@@ -7,11 +7,11 @@ extern crate cretonne_wasm;
extern crate region; extern crate region;
extern crate wasmstandalone_runtime; extern crate wasmstandalone_runtime;
use cretonne_codegen::isa::TargetIsa;
use cretonne_codegen::binemit::Reloc; use cretonne_codegen::binemit::Reloc;
use std::mem::transmute; use cretonne_codegen::isa::TargetIsa;
use region::Protection;
use region::protect; use region::protect;
use region::Protection;
use std::mem::transmute;
use std::ptr::write_unaligned; use std::ptr::write_unaligned;
use wasmstandalone_runtime::Compilation; use wasmstandalone_runtime::Compilation;
@@ -21,8 +21,8 @@ pub fn compile_module<'data, 'module>(
translation: &wasmstandalone_runtime::ModuleTranslation<'data, 'module>, translation: &wasmstandalone_runtime::ModuleTranslation<'data, 'module>,
) -> Result<wasmstandalone_runtime::Compilation<'module>, String> { ) -> Result<wasmstandalone_runtime::Compilation<'module>, String> {
debug_assert!( debug_assert!(
translation.module.start_func.is_none() || translation.module.start_func.is_none()
translation.module.start_func.unwrap() >= translation.module.imported_funcs.len(), || translation.module.start_func.unwrap() >= translation.module.imported_funcs.len(),
"imported start functions not supported yet" "imported start functions not supported yet"
); );
@@ -53,8 +53,8 @@ fn relocate(compilation: &mut Compilation, relocations: &wasmstandalone_runtime:
let reloc_address = body.as_mut_ptr().offset(r.offset as isize) as isize; let reloc_address = body.as_mut_ptr().offset(r.offset as isize) as isize;
let reloc_addend = r.addend as isize; let reloc_addend = r.addend as isize;
// TODO: Handle overflow. // TODO: Handle overflow.
let reloc_delta_i32 = (target_func_address - reloc_address + reloc_addend) as let reloc_delta_i32 =
i32; (target_func_address - reloc_address + reloc_addend) as i32;
write_unaligned(reloc_address as *mut i32, reloc_delta_i32); write_unaligned(reloc_address as *mut i32, reloc_delta_i32);
}, },
_ => panic!("unsupported reloc kind"), _ => panic!("unsupported reloc kind"),
@@ -81,9 +81,10 @@ pub fn execute(
compilation: &wasmstandalone_runtime::Compilation, compilation: &wasmstandalone_runtime::Compilation,
instance: &mut wasmstandalone_runtime::Instance, instance: &mut wasmstandalone_runtime::Instance,
) -> Result<(), String> { ) -> Result<(), String> {
let start_index = compilation.module.start_func.ok_or_else(|| { let start_index = compilation
String::from("No start function defined, aborting execution") .module
})?; .start_func
.ok_or_else(|| String::from("No start function defined, aborting execution"))?;
// TODO: Put all the function bodies into a page-aligned memory region, and // TODO: Put all the function bodies into a page-aligned memory region, and
// then make them ReadExecute rather than ReadWriteExecute. // then make them ReadExecute rather than ReadWriteExecute.
for code_buf in &compilation.functions { for code_buf in &compilation.functions {

View File

@@ -11,15 +11,15 @@ pub fn emit_module<'module>(
relocations: &wasmstandalone_runtime::Relocations, relocations: &wasmstandalone_runtime::Relocations,
) -> Result<(), String> { ) -> Result<(), String> {
debug_assert!( debug_assert!(
compilation.module.start_func.is_none() || compilation.module.start_func.is_none()
compilation.module.start_func.unwrap() >= compilation.module.imported_funcs.len(), || compilation.module.start_func.unwrap() >= compilation.module.imported_funcs.len(),
"imported start functions not supported yet" "imported start functions not supported yet"
); );
let mut shared_builder = settings::builder(); let mut shared_builder = settings::builder();
shared_builder.enable("enable_verifier").expect( shared_builder
"Missing enable_verifier setting", .enable("enable_verifier")
); .expect("Missing enable_verifier setting");
for (i, function_relocs) in relocations.iter().enumerate() { for (i, function_relocs) in relocations.iter().enumerate() {
assert!(function_relocs.is_empty(), "relocations not supported yet"); assert!(function_relocs.is_empty(), "relocations not supported yet");
@@ -27,9 +27,8 @@ pub fn emit_module<'module>(
let func_index = compilation.module.imported_funcs.len() + i; let func_index = compilation.module.imported_funcs.len() + i;
let string_name = format!("wasm_function[{}]", func_index); let string_name = format!("wasm_function[{}]", func_index);
obj.define(string_name, body.clone()).map_err(|err| { obj.define(string_name, body.clone())
format!("{}", err) .map_err(|err| format!("{}", err))?;
})?;
} }
Ok(()) Ok(())

View File

@@ -3,7 +3,7 @@
use cretonne_codegen::ir; use cretonne_codegen::ir;
use cretonne_wasm::GlobalIndex; use cretonne_wasm::GlobalIndex;
use module::Module; use module::{Module, TableElements};
use DataInitializer; use DataInitializer;
const PAGE_SIZE: usize = 65536; const PAGE_SIZE: usize = 65536;
@@ -29,15 +29,14 @@ impl Instance {
memories: Vec::new(), memories: Vec::new(),
globals: Vec::new(), globals: Vec::new(),
}; };
result.instantiate_tables(module); result.instantiate_tables(module, &module.table_elements);
result.instantiate_memories(module, data_initializers); result.instantiate_memories(module, data_initializers);
result.instantiate_globals(module); result.instantiate_globals(module);
result result
} }
/// Allocate memory in `self` for just the tables of the current module, /// Allocate memory in `self` for just the tables of the current module.
/// without any initializers applied yet. fn instantiate_tables(&mut self, module: &Module, table_initializers: &[TableElements]) {
fn instantiate_tables(&mut self, module: &Module) {
debug_assert!(self.tables.is_empty()); debug_assert!(self.tables.is_empty());
self.tables.reserve_exact(module.tables.len()); self.tables.reserve_exact(module.tables.len());
for table in &module.tables { for table in &module.tables {
@@ -46,10 +45,15 @@ impl Instance {
v.resize(len, 0); v.resize(len, 0);
self.tables.push(v); self.tables.push(v);
} }
for init in table_initializers {
debug_assert!(init.base.is_none(), "globalvar base not supported yet");
let to_init =
&mut self.tables[init.table_index][init.offset..init.offset + init.elements.len()];
to_init.copy_from_slice(&init.elements);
}
} }
/// Allocate memory in `instance` for just the memories of the current module, /// Allocate memory in `instance` for just the memories of the current module.
/// without any initializers applied yet.
fn instantiate_memories(&mut self, module: &Module, data_initializers: &[DataInitializer]) { fn instantiate_memories(&mut self, module: &Module, data_initializers: &[DataInitializer]) {
debug_assert!(self.memories.is_empty()); debug_assert!(self.memories.is_empty());
// Allocate the underlying memory and initialize it to all zeros. // Allocate the underlying memory and initialize it to all zeros.
@@ -62,8 +66,8 @@ impl Instance {
} }
for init in data_initializers { for init in data_initializers {
debug_assert!(init.base.is_none(), "globalvar base not supported yet"); debug_assert!(init.base.is_none(), "globalvar base not supported yet");
let to_init = &mut self.memories[init.memory_index][init.offset.. let to_init =
init.offset + init.data.len()]; &mut self.memories[init.memory_index][init.offset..init.offset + init.data.len()];
to_init.copy_from_slice(init.data); to_init.copy_from_slice(init.data);
} }
} }
@@ -79,12 +83,9 @@ impl Instance {
/// Returns a slice of the contents of allocated linear memory. /// Returns a slice of the contents of allocated linear memory.
pub fn inspect_memory(&self, memory_index: usize, address: usize, len: usize) -> &[u8] { pub fn inspect_memory(&self, memory_index: usize, address: usize, len: usize) -> &[u8] {
&self.memories.get(memory_index).expect( &self.memories
format!( .get(memory_index)
"no memory for index {}", .expect(format!("no memory for index {}", memory_index).as_str())
memory_index
).as_str(),
)
[address..address + len] [address..address + len]
} }

View File

@@ -9,25 +9,25 @@ extern crate cretonne_codegen;
extern crate cretonne_wasm; extern crate cretonne_wasm;
extern crate wasmparser; extern crate wasmparser;
pub mod module;
pub mod compilation; pub mod compilation;
pub mod instance; pub mod instance;
pub mod module;
pub use module::Module;
pub use compilation::Compilation; pub use compilation::Compilation;
pub use instance::Instance; pub use instance::Instance;
pub use module::Module;
use cretonne_wasm::{FunctionIndex, GlobalIndex, TableIndex, MemoryIndex, Global, Table, Memory, use cretonne_codegen::binemit;
GlobalValue, SignatureIndex, FuncTranslator};
use cretonne_codegen::ir::{InstBuilder, FuncRef, ExtFuncData, ExternalName, Signature, AbiParam,
ArgumentPurpose, ArgumentLoc, ArgumentExtension, Function};
use cretonne_codegen::ir::types::*;
use cretonne_codegen::ir::immediates::Offset32;
use cretonne_codegen::cursor::FuncCursor; use cretonne_codegen::cursor::FuncCursor;
use cretonne_codegen::ir; use cretonne_codegen::ir;
use cretonne_codegen::ir::immediates::Offset32;
use cretonne_codegen::ir::types::*;
use cretonne_codegen::ir::{AbiParam, ArgumentExtension, ArgumentLoc, ArgumentPurpose, ExtFuncData,
ExternalName, FuncRef, Function, InstBuilder, Signature};
use cretonne_codegen::isa; use cretonne_codegen::isa;
use cretonne_codegen::settings; use cretonne_codegen::settings;
use cretonne_codegen::binemit; use cretonne_wasm::{FuncTranslator, FunctionIndex, Global, GlobalIndex, GlobalValue, Memory,
MemoryIndex, SignatureIndex, Table, TableIndex};
/// Compute a `ir::ExternalName` for a given wasm function index. /// Compute a `ir::ExternalName` for a given wasm function index.
pub fn get_func_name(func_index: FunctionIndex) -> cretonne_codegen::ir::ExternalName { pub fn get_func_name(func_index: FunctionIndex) -> cretonne_codegen::ir::ExternalName {
@@ -96,7 +96,9 @@ impl binemit::RelocSink for RelocSink {
impl RelocSink { impl RelocSink {
fn new() -> RelocSink { fn new() -> RelocSink {
RelocSink { func_relocs: Vec::new() } RelocSink {
func_relocs: Vec::new(),
}
} }
} }
@@ -221,7 +223,11 @@ impl<'module_environment> FuncEnvironment<'module_environment> {
} }
fn ptr_size(&self) -> usize { fn ptr_size(&self) -> usize {
if self.settings_flags.is_64bit() { 8 } else { 4 } if self.settings_flags.is_64bit() {
8
} else {
4
}
} }
} }
@@ -275,7 +281,9 @@ impl<'module_environment> cretonne_wasm::FuncEnvironment for FuncEnvironment<'mo
base: ir::HeapBase::GlobalVar(heap_base), base: ir::HeapBase::GlobalVar(heap_base),
min_size: 0.into(), min_size: 0.into(),
guard_size: 0x8000_0000.into(), guard_size: 0x8000_0000.into(),
style: ir::HeapStyle::Static { bound: 0x1_0000_0000.into() }, style: ir::HeapStyle::Static {
bound: 0x1_0000_0000.into(),
},
}); });
h h
} }
@@ -422,10 +430,9 @@ impl<'data, 'module> cretonne_wasm::ModuleEnvironment<'data> for ModuleEnvironme
); );
self.module.functions.push(sig_index); self.module.functions.push(sig_index);
self.module.imported_funcs.push(( self.module
String::from(module), .imported_funcs
String::from(field), .push((String::from(module), String::from(field)));
));
} }
fn get_num_func_imports(&self) -> usize { fn get_num_func_imports(&self) -> usize {
@@ -489,31 +496,27 @@ impl<'data, 'module> cretonne_wasm::ModuleEnvironment<'data> for ModuleEnvironme
} }
fn declare_func_export(&mut self, func_index: FunctionIndex, name: &str) { fn declare_func_export(&mut self, func_index: FunctionIndex, name: &str) {
self.module.exports.insert( self.module
String::from(name), .exports
module::Export::Function(func_index), .insert(String::from(name), module::Export::Function(func_index));
);
} }
fn declare_table_export(&mut self, table_index: TableIndex, name: &str) { fn declare_table_export(&mut self, table_index: TableIndex, name: &str) {
self.module.exports.insert( self.module
String::from(name), .exports
module::Export::Table(table_index), .insert(String::from(name), module::Export::Table(table_index));
);
} }
fn declare_memory_export(&mut self, memory_index: MemoryIndex, name: &str) { fn declare_memory_export(&mut self, memory_index: MemoryIndex, name: &str) {
self.module.exports.insert( self.module
String::from(name), .exports
module::Export::Memory(memory_index), .insert(String::from(name), module::Export::Memory(memory_index));
);
} }
fn declare_global_export(&mut self, global_index: GlobalIndex, name: &str) { fn declare_global_export(&mut self, global_index: GlobalIndex, name: &str) {
self.module.exports.insert( self.module
String::from(name), .exports
module::Export::Global(global_index), .insert(String::from(name), module::Export::Global(global_index));
);
} }
fn declare_start_func(&mut self, func_index: FunctionIndex) { fn declare_start_func(&mut self, func_index: FunctionIndex) {
@@ -573,8 +576,8 @@ impl<'data, 'module> ModuleTranslation<'data, 'module> {
let func_index = i + self.module.imported_funcs.len(); let func_index = i + self.module.imported_funcs.len();
let mut context = cretonne_codegen::Context::new(); let mut context = cretonne_codegen::Context::new();
context.func.name = get_func_name(func_index); context.func.name = get_func_name(func_index);
context.func.signature = self.module.signatures[self.module.functions[func_index]] context.func.signature =
.clone(); self.module.signatures[self.module.functions[func_index]].clone();
let mut trans = FuncTranslator::new(); let mut trans = FuncTranslator::new();
let reader = wasmparser::BinaryReader::new(input); let reader = wasmparser::BinaryReader::new(input);

View File

@@ -1,9 +1,9 @@
//! A `Module` contains all the relevant information translated from a //! A `Module` contains all the relevant information translated from a
//! WebAssembly module. //! WebAssembly module.
use cretonne_wasm::{FunctionIndex, GlobalIndex, TableIndex, MemoryIndex, Global, Table, Memory,
SignatureIndex};
use cretonne_codegen::ir; use cretonne_codegen::ir;
use cretonne_wasm::{FunctionIndex, Global, GlobalIndex, Memory, MemoryIndex, SignatureIndex,
Table, TableIndex};
use std::collections::HashMap; use std::collections::HashMap;
/// Possible values for a WebAssembly table element. /// Possible values for a WebAssembly table element.

View File

@@ -6,31 +6,31 @@
//! and tables, then emitting the translated code with hardcoded addresses to memory. //! and tables, then emitting the translated code with hardcoded addresses to memory.
extern crate cretonne_codegen; extern crate cretonne_codegen;
extern crate cretonne_wasm;
extern crate cretonne_native; extern crate cretonne_native;
extern crate wasmstandalone_runtime; extern crate cretonne_wasm;
extern crate wasmstandalone_execute;
extern crate docopt; extern crate docopt;
extern crate wasmstandalone_execute;
extern crate wasmstandalone_runtime;
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;
extern crate tempdir; extern crate tempdir;
use cretonne_wasm::translate_module;
use wasmstandalone_execute::{compile_module, execute};
use wasmstandalone_runtime::{Instance, Module, ModuleEnvironment};
use std::path::PathBuf;
use cretonne_codegen::isa::TargetIsa; use cretonne_codegen::isa::TargetIsa;
use cretonne_codegen::settings; use cretonne_codegen::settings;
use std::fs::File; use cretonne_codegen::settings::Configurable;
use std::error::Error; use cretonne_wasm::translate_module;
use std::io;
use std::io::stdout;
use std::io::prelude::*;
use docopt::Docopt; use docopt::Docopt;
use std::error::Error;
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::io::stdout;
use std::path::Path; use std::path::Path;
use std::path::PathBuf;
use std::process::{exit, Command}; use std::process::{exit, Command};
use tempdir::TempDir; use tempdir::TempDir;
use cretonne_codegen::settings::Configurable; use wasmstandalone_execute::{compile_module, execute};
use wasmstandalone_runtime::{Instance, Module, ModuleEnvironment};
const USAGE: &str = " const USAGE: &str = "
Wasm to Cretonne IL translation utility. Wasm to Cretonne IL translation utility.
@@ -99,9 +99,7 @@ fn main() {
} }
fn handle_module(args: &Args, path: PathBuf, isa: &TargetIsa) -> Result<(), String> { fn handle_module(args: &Args, path: PathBuf, isa: &TargetIsa) -> Result<(), String> {
let mut data = read_to_end(path.clone()).map_err(|err| { let mut data = read_to_end(path.clone()).map_err(|err| String::from(err.description()))?;
String::from(err.description())
})?;
if !data.starts_with(&[b'\0', b'a', b's', b'm']) { if !data.starts_with(&[b'\0', b'a', b's', b'm']) {
let tmp_dir = TempDir::new("cretonne-wasm").unwrap(); let tmp_dir = TempDir::new("cretonne-wasm").unwrap();
let file_path = tmp_dir.path().join("module.wasm"); let file_path = tmp_dir.path().join("module.wasm");
@@ -111,14 +109,14 @@ fn handle_module(args: &Args, path: PathBuf, isa: &TargetIsa) -> Result<(), Stri
.arg("-o") .arg("-o")
.arg(file_path.to_str().unwrap()) .arg(file_path.to_str().unwrap())
.output() .output()
.or_else(|e| if let io::ErrorKind::NotFound = e.kind() { .or_else(|e| {
return Err(String::from("wat2wasm not found")); if let io::ErrorKind::NotFound = e.kind() {
} else { return Err(String::from("wat2wasm not found"));
return Err(String::from(e.description())); } else {
return Err(String::from(e.description()));
}
})?; })?;
data = read_to_end(file_path).map_err( data = read_to_end(file_path).map_err(|err| String::from(err.description()))?;
|err| String::from(err.description()),
)?;
} }
let mut module = Module::new(); let mut module = Module::new();
let mut environ = ModuleEnvironment::new(isa.flags(), &mut module); let mut environ = ModuleEnvironment::new(isa.flags(), &mut module);

View File

@@ -7,27 +7,27 @@
extern crate cretonne_codegen; extern crate cretonne_codegen;
extern crate cretonne_native; extern crate cretonne_native;
extern crate cretonne_wasm; extern crate cretonne_wasm;
extern crate docopt;
extern crate wasmstandalone_obj; extern crate wasmstandalone_obj;
extern crate wasmstandalone_runtime; extern crate wasmstandalone_runtime;
extern crate docopt;
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;
extern crate faerie; extern crate faerie;
use cretonne_wasm::translate_module;
use cretonne_codegen::settings;
use cretonne_codegen::isa; use cretonne_codegen::isa;
use wasmstandalone_obj::emit_module; use cretonne_codegen::settings;
use std::path::PathBuf; use cretonne_wasm::translate_module;
use std::fs::File; use docopt::Docopt;
use faerie::{Artifact, Elf, Target};
use std::error::Error; use std::error::Error;
use std::fmt::format;
use std::fs::File;
use std::io; use std::io;
use std::io::prelude::*; use std::io::prelude::*;
use docopt::Docopt;
use std::path::Path; use std::path::Path;
use std::path::PathBuf;
use std::process; use std::process;
use std::fmt::format; use wasmstandalone_obj::emit_module;
use faerie::{Artifact, Elf, Target};
const USAGE: &str = " const USAGE: &str = "
Wasm to native object translation utility. Wasm to native object translation utility.
@@ -100,9 +100,8 @@ fn handle_module(path: PathBuf, output: &str) -> Result<(), String> {
// FIXME: We need to initialize memory in a way that supports alternate // FIXME: We need to initialize memory in a way that supports alternate
// memory spaces, imported base addresses, and offsets. // memory spaces, imported base addresses, and offsets.
for init in &environ.lazy.data_initializers { for init in &environ.lazy.data_initializers {
obj.define("memory", Vec::from(init.data)).map_err(|err| { obj.define("memory", Vec::from(init.data))
format!("{}", err) .map_err(|err| format!("{}", err))?;
})?;
} }
let translation = environ.finish_translation(); let translation = environ.finish_translation();
@@ -119,12 +118,10 @@ fn handle_module(path: PathBuf, output: &str) -> Result<(), String> {
} }
// FIXME: Make the format a parameter. // FIXME: Make the format a parameter.
let file = ::std::fs::File::create(Path::new(output)).map_err(|x| { let file =
format(format_args!("{}", x)) ::std::fs::File::create(Path::new(output)).map_err(|x| format(format_args!("{}", x)))?;
})?; obj.write::<Elf>(file)
obj.write::<Elf>(file).map_err( .map_err(|x| format(format_args!("{}", x)))?;
|x| format(format_args!("{}", x)),
)?;
Ok(()) Ok(())
} }