From b5e794a5847f7d511daa7687f07d3d1f01b81616 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Mon, 14 May 2018 12:58:28 -1000 Subject: [PATCH] Update to Cretonne 0.8.0. --- Cargo.toml | 18 +++--- fuzz/Cargo.toml | 8 +-- lib/execute/Cargo.toml | 6 +- lib/execute/src/lib.rs | 6 +- lib/obj/Cargo.toml | 6 +- lib/obj/src/emit_module.rs | 4 +- lib/obj/src/lib.rs | 4 +- lib/runtime/Cargo.toml | 6 +- lib/runtime/src/instance.rs | 4 +- lib/runtime/src/lib.rs | 118 ++++++++++++++++++++---------------- lib/runtime/src/module.rs | 6 +- src/main.rs | 18 +++--- src/wasm2obj.rs | 16 ++--- 13 files changed, 116 insertions(+), 104 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 79ef2f16c9..9bde01f627 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,18 +17,18 @@ name = "wasm2obj" path = "src/wasm2obj.rs" [dependencies] -cretonne = "0.4.0" -cretonne-frontend = "0.4.0" -cretonne-reader = "0.4.0" -cretonne-wasm = "0.4.0" -cretonne-native = "0.4.0" +cretonne-codegen = "0.8.0" +cretonne-frontend = "0.8.0" +cretonne-reader = "0.8.0" +cretonne-wasm = "0.8.0" +cretonne-native = "0.8.0" wasmstandalone_runtime = { path = "lib/runtime" } wasmstandalone_execute = { path = "lib/execute" } wasmstandalone_obj = { path = "lib/obj" } -docopt = "0.8.3" -serde = "1.0.27" -serde_derive = "1.0.27" +docopt = "1.0.0" +serde = "1.0.55" +serde_derive = "1.0.55" tempdir = "*" -faerie = "0.1.0" +faerie = "0.3.0" [workspace] diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 1df1495714..2e5c042b42 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -13,14 +13,14 @@ path = "../lib/runtime" [dependencies.wasmstandalone_execute] path = "../lib/execute" -[dependencies.cretonne] -version = "0.3.0" +[dependencies.cretonne-codegen] +version = "0.8.0" [dependencies.cretonne-wasm] -version = "0.3.0" +version = "0.8.0" [dependencies.cretonne-native] -version = "0.3.0" +version = "0.8.0" [dependencies.libfuzzer-sys] git = "https://github.com/rust-fuzz/libfuzzer-sys.git" diff --git a/lib/execute/Cargo.toml b/lib/execute/Cargo.toml index dbe194091e..5e9ba30606 100644 --- a/lib/execute/Cargo.toml +++ b/lib/execute/Cargo.toml @@ -8,7 +8,7 @@ repository = "https://github.com/sunfishcode/wasmstandalone" license = "MIT/Apache-2.0" [dependencies] -cretonne = "0.4.0" -cretonne-wasm = "0.4.0" -region = "0.1.1" +cretonne-codegen = "0.8.0" +cretonne-wasm = "0.8.0" +region = "0.3.0" wasmstandalone_runtime = { path = "../runtime" } diff --git a/lib/execute/src/lib.rs b/lib/execute/src/lib.rs index 1449a7f1d0..5687996822 100644 --- a/lib/execute/src/lib.rs +++ b/lib/execute/src/lib.rs @@ -2,12 +2,12 @@ #![deny(missing_docs)] -extern crate cretonne; -extern crate cton_wasm; +extern crate cretonne_codegen; +extern crate cretonne_wasm; extern crate region; extern crate wasmstandalone_runtime; -use cretonne::isa::TargetIsa; +use cretonne_codegen::isa::TargetIsa; use std::mem::transmute; use region::Protection; use region::protect; diff --git a/lib/obj/Cargo.toml b/lib/obj/Cargo.toml index bd03ea7e91..524c2b9d4a 100644 --- a/lib/obj/Cargo.toml +++ b/lib/obj/Cargo.toml @@ -5,7 +5,7 @@ authors = ["The Cretonne Project Developers"] publish = false [dependencies] -cretonne = "0.4.0" -cretonne-wasm = "0.4.0" +cretonne-codegen = "0.8.0" +cretonne-wasm = "0.8.0" wasmstandalone_runtime = { path = "../runtime" } -faerie = "0.1.0" +faerie = "0.3.0" diff --git a/lib/obj/src/emit_module.rs b/lib/obj/src/emit_module.rs index 1ef3d9c273..429d60ae45 100644 --- a/lib/obj/src/emit_module.rs +++ b/lib/obj/src/emit_module.rs @@ -1,5 +1,5 @@ -use cretonne::settings; -use cretonne::settings::Configurable; +use cretonne_codegen::settings; +use cretonne_codegen::settings::Configurable; use faerie::Artifact; use wasmstandalone_runtime; diff --git a/lib/obj/src/lib.rs b/lib/obj/src/lib.rs index 69d9ec291e..742ab99c6e 100644 --- a/lib/obj/src/lib.rs +++ b/lib/obj/src/lib.rs @@ -1,5 +1,5 @@ -extern crate cretonne; -extern crate cton_wasm; +extern crate cretonne_codegen; +extern crate cretonne_wasm; extern crate faerie; extern crate wasmstandalone_runtime; diff --git a/lib/runtime/Cargo.toml b/lib/runtime/Cargo.toml index 9981797c8f..b25e574ad0 100644 --- a/lib/runtime/Cargo.toml +++ b/lib/runtime/Cargo.toml @@ -8,6 +8,6 @@ repository = "https://github.com/sunfishcode/wasmstandalone" license = "Apache-2.0" [dependencies] -cretonne = "0.4.0" -cretonne-wasm = "0.4.0" -wasmparser = "0.15.1" +cretonne-codegen = "0.8.0" +cretonne-wasm = "0.8.0" +wasmparser = "0.16.1" diff --git a/lib/runtime/src/instance.rs b/lib/runtime/src/instance.rs index 9b1ac563a1..edea184a52 100644 --- a/lib/runtime/src/instance.rs +++ b/lib/runtime/src/instance.rs @@ -1,8 +1,8 @@ //! An `Instance` contains all the runtime state used by execution of a wasm //! module. -use cretonne::ir; -use cton_wasm::GlobalIndex; +use cretonne_codegen::ir; +use cretonne_wasm::GlobalIndex; use module::Module; use DataInitializer; diff --git a/lib/runtime/src/lib.rs b/lib/runtime/src/lib.rs index 2d79227089..45a3ac8c3a 100644 --- a/lib/runtime/src/lib.rs +++ b/lib/runtime/src/lib.rs @@ -5,8 +5,8 @@ #![deny(missing_docs)] -extern crate cretonne; -extern crate cton_wasm; +extern crate cretonne_codegen; +extern crate cretonne_wasm; extern crate wasmparser; pub mod module; @@ -17,21 +17,20 @@ pub use module::Module; pub use compilation::Compilation; pub use instance::Instance; -use cton_wasm::{FunctionIndex, GlobalIndex, TableIndex, MemoryIndex, Global, Table, Memory, - GlobalValue, SignatureIndex, FuncTranslator}; -use cretonne::ir::{InstBuilder, FuncRef, ExtFuncData, ExternalName, Signature, AbiParam, CallConv, - ArgumentPurpose, ArgumentLoc, ArgumentExtension, Function}; -use cretonne::ir::types::*; -use cretonne::ir::immediates::Offset32; -use cretonne::cursor::FuncCursor; -use cretonne::ir; -use cretonne::isa; -use cretonne::settings; -use cretonne::binemit; -use std::error::Error; +use cretonne_wasm::{FunctionIndex, GlobalIndex, TableIndex, MemoryIndex, Global, Table, Memory, + 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::ir; +use cretonne_codegen::isa; +use cretonne_codegen::settings; +use cretonne_codegen::binemit; /// Compute a `ir::ExternalName` for a given wasm function index. -pub fn get_func_name(func_index: FunctionIndex) -> cretonne::ir::ExternalName { +pub fn get_func_name(func_index: FunctionIndex) -> cretonne_codegen::ir::ExternalName { debug_assert!(func_index as u32 as FunctionIndex == func_index); ir::ExternalName::user(0, func_index as u32) } @@ -49,13 +48,12 @@ pub enum Export { } /// Implementation of a relocation sink that just saves all the information for later -pub struct RelocSink<'func> { - func: &'func ir::Function, +pub struct RelocSink { /// Relocations recorded for the function. pub func_relocs: Vec, } -impl<'func> binemit::RelocSink for RelocSink<'func> { +impl binemit::RelocSink for RelocSink { fn reloc_ebb( &mut self, _offset: binemit::CodeOffset, @@ -90,19 +88,15 @@ impl<'func> binemit::RelocSink for RelocSink<'func> { &mut self, _offset: binemit::CodeOffset, _reloc: binemit::Reloc, - jt: ir::JumpTable, + _jt: ir::JumpTable, ) { - let _jump_table = &self.func.jump_tables[jt]; panic!("jump tables not yet implemented"); } } -impl<'func> RelocSink<'func> { - fn new(func: &'func Function) -> RelocSink { - RelocSink { - func, - func_relocs: Vec::new(), - } +impl RelocSink { + fn new() -> RelocSink { + RelocSink { func_relocs: Vec::new() } } } @@ -138,10 +132,10 @@ impl<'data> LazyContents<'data> { } /// Object containing the standalone runtime information. To be passed after creation as argument -/// to `cton_wasm::translatemodule`. +/// to `cretonne_wasm::translatemodule`. pub struct ModuleEnvironment<'data, 'module> { /// Compilation setting flags. - pub flags: &'module settings::Flags, + pub settings_flags: &'module settings::Flags, /// Module information. pub module: &'module mut Module, @@ -154,18 +148,18 @@ impl<'data, 'module> ModuleEnvironment<'data, 'module> { /// Allocates the runtime data structures with the given isa. pub fn new(flags: &'module settings::Flags, module: &'module mut Module) -> Self { Self { - flags, + settings_flags: flags, module, lazy: LazyContents::new(), } } fn func_env(&self) -> FuncEnvironment { - FuncEnvironment::new(&self.flags, &self.module) + FuncEnvironment::new(&self.settings_flags, &self.module) } fn native_pointer(&self) -> ir::Type { - use cton_wasm::FuncEnvironment; + use cretonne_wasm::FuncEnvironment; self.func_env().native_pointer() } @@ -175,7 +169,7 @@ impl<'data, 'module> ModuleEnvironment<'data, 'module> { /// `Module`. pub fn finish_translation(self) -> ModuleTranslation<'data, 'module> { ModuleTranslation { - flags: self.flags, + flags: self.settings_flags, module: self.module, lazy: self.lazy, } @@ -231,7 +225,7 @@ impl<'module_environment> FuncEnvironment<'module_environment> { } } -impl<'module_environment> cton_wasm::FuncEnvironment for FuncEnvironment<'module_environment> { +impl<'module_environment> cretonne_wasm::FuncEnvironment for FuncEnvironment<'module_environment> { fn flags(&self) -> &settings::Flags { &self.settings_flags } @@ -242,9 +236,9 @@ impl<'module_environment> cton_wasm::FuncEnvironment for FuncEnvironment<'module let offset = 0 * ptr_size; let offset32 = offset as i32; debug_assert_eq!(offset32 as usize, offset); - let new_base = func.create_global_var( - ir::GlobalVarData::VmCtx { offset: Offset32::new(offset32) }, - ); + let new_base = func.create_global_var(ir::GlobalVarData::VMContext { + offset: Offset32::new(offset32), + }); self.globals_base = Some(new_base); new_base }); @@ -264,7 +258,7 @@ impl<'module_environment> cton_wasm::FuncEnvironment for FuncEnvironment<'module fn make_heap(&mut self, func: &mut ir::Function, index: MemoryIndex) -> ir::Heap { let ptr_size = self.ptr_size(); let memories_base = self.memories_base.unwrap_or_else(|| { - let new_base = func.create_global_var(ir::GlobalVarData::VmCtx { + let new_base = func.create_global_var(ir::GlobalVarData::VMContext { offset: Offset32::new(ptr_size as i32), }); self.globals_base = Some(new_base); @@ -294,7 +288,14 @@ impl<'module_environment> cton_wasm::FuncEnvironment for FuncEnvironment<'module let sigidx = self.module.functions[index]; let signature = func.import_signature(self.module.signatures[sigidx].clone()); let name = get_func_name(index); - func.import_function(ir::ExtFuncData { name, signature }) + // We currently allocate all code segments independently, so nothing + // is colocated. + let colocated = false; + func.import_function(ir::ExtFuncData { + name, + signature, + colocated, + }) } fn translate_call_indirect( @@ -334,15 +335,19 @@ impl<'module_environment> cton_wasm::FuncEnvironment for FuncEnvironment<'module debug_assert_eq!(index, 0, "non-default memories not supported yet"); let grow_mem_func = self.grow_memory_extfunc.unwrap_or_else(|| { let sig_ref = pos.func.import_signature(Signature { - call_conv: CallConv::Native, + call_conv: self.settings_flags.call_conv(), argument_bytes: None, params: vec![AbiParam::new(I32)], returns: vec![AbiParam::new(I32)], }); + // We currently allocate all code segments independently, so nothing + // is colocated. + let colocated = false; // FIXME: Use a real ExternalName system. pos.func.import_function(ExtFuncData { name: ExternalName::testcase("grow_memory"), signature: sig_ref, + colocated, }) }); self.grow_memory_extfunc = Some(grow_mem_func); @@ -359,15 +364,19 @@ impl<'module_environment> cton_wasm::FuncEnvironment for FuncEnvironment<'module debug_assert_eq!(index, 0, "non-default memories not supported yet"); let cur_mem_func = self.current_memory_extfunc.unwrap_or_else(|| { let sig_ref = pos.func.import_signature(Signature { - call_conv: CallConv::Native, + call_conv: self.settings_flags.call_conv(), argument_bytes: None, params: Vec::new(), returns: vec![AbiParam::new(I32)], }); + // We currently allocate all code segments independently, so nothing + // is colocated. + let colocated = false; // FIXME: Use a real ExternalName system. pos.func.import_function(ExtFuncData { name: ExternalName::testcase("current_memory"), signature: sig_ref, + colocated, }) }); self.current_memory_extfunc = Some(cur_mem_func); @@ -377,14 +386,18 @@ impl<'module_environment> cton_wasm::FuncEnvironment for FuncEnvironment<'module } /// This trait is useful for -/// `cton_wasm::translatemodule` because it +/// `cretonne_wasm::translatemodule` because it /// tells how to translate runtime-dependent wasm instructions. These functions should not be /// called by the user. -impl<'data, 'module> cton_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data, 'module> { - fn get_func_name(&self, func_index: FunctionIndex) -> cretonne::ir::ExternalName { +impl<'data, 'module> cretonne_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data, 'module> { + fn get_func_name(&self, func_index: FunctionIndex) -> cretonne_codegen::ir::ExternalName { get_func_name(func_index) } + fn flags(&self) -> &settings::Flags { + self.settings_flags + } + fn declare_signature(&mut self, sig: &ir::Signature) { let mut sig = sig.clone(); sig.params.push(AbiParam { @@ -431,7 +444,7 @@ impl<'data, 'module> cton_wasm::ModuleEnvironment<'data> for ModuleEnvironment<' self.module.globals.push(global); } - fn get_global(&self, global_index: GlobalIndex) -> &cton_wasm::Global { + fn get_global(&self, global_index: GlobalIndex) -> &cretonne_wasm::Global { &self.module.globals[global_index] } @@ -557,7 +570,7 @@ impl<'data, 'module> ModuleTranslation<'data, 'module> { let mut functions = Vec::new(); let mut relocations = Vec::new(); for (func_index, input) in self.lazy.function_body_inputs.iter().enumerate() { - let mut context = cretonne::Context::new(); + let mut context = cretonne_codegen::Context::new(); context.func.name = get_func_name(func_index); context.func.signature = self.module.signatures[self.module.functions[func_index]] .clone(); @@ -566,15 +579,14 @@ impl<'data, 'module> ModuleTranslation<'data, 'module> { let reader = wasmparser::BinaryReader::new(input); trans .translate_from_reader(reader, &mut context.func, &mut self.func_env()) - .map_err(|e| String::from(e.description()))?; + .map_err(|e| e.to_string())?; - let code_size = context.compile(isa).map_err( - |e| String::from(e.description()), - )? as usize; - let mut code_buf: Vec = Vec::with_capacity(code_size as usize); - let mut reloc_sink = RelocSink::new(&context.func); - code_buf.resize(code_size, 0); - context.emit_to_memory(code_buf.as_mut_ptr(), &mut reloc_sink, isa); + let mut code_buf: Vec = Vec::new(); + let mut reloc_sink = RelocSink::new(); + let mut trap_sink = binemit::NullTrapSink {}; + context + .compile_and_emit(isa, &mut code_buf, &mut reloc_sink, &mut trap_sink) + .map_err(|e| e.to_string())?; functions.push(code_buf); relocations.push(reloc_sink.func_relocs); } diff --git a/lib/runtime/src/module.rs b/lib/runtime/src/module.rs index 291629b2fe..d4ffefa0e0 100644 --- a/lib/runtime/src/module.rs +++ b/lib/runtime/src/module.rs @@ -1,9 +1,9 @@ //! A `Module` contains all the relevant information translated from a //! WebAssembly module. -use cton_wasm::{FunctionIndex, GlobalIndex, TableIndex, MemoryIndex, Global, Table, Memory, - SignatureIndex}; -use cretonne::ir; +use cretonne_wasm::{FunctionIndex, GlobalIndex, TableIndex, MemoryIndex, Global, Table, Memory, + SignatureIndex}; +use cretonne_codegen::ir; use std::collections::HashMap; /// Possible values for a WebAssembly table element. diff --git a/src/main.rs b/src/main.rs index 8892b87bca..2a84ba0ba3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,22 +5,22 @@ //! IL. Can also executes the `start` function of the module by laying out the memories, globals //! and tables, then emitting the translated code with hardcoded addresses to memory. -extern crate cton_wasm; -extern crate cton_native; +extern crate cretonne_codegen; +extern crate cretonne_wasm; +extern crate cretonne_native; extern crate wasmstandalone_runtime; extern crate wasmstandalone_execute; -extern crate cretonne; extern crate docopt; #[macro_use] extern crate serde_derive; extern crate tempdir; -use cton_wasm::translate_module; +use cretonne_wasm::translate_module; use wasmstandalone_execute::{compile_module, execute}; use wasmstandalone_runtime::{Instance, Module, ModuleEnvironment}; use std::path::PathBuf; -use cretonne::isa::TargetIsa; -use cretonne::settings; +use cretonne_codegen::isa::TargetIsa; +use cretonne_codegen::settings; use std::fs::File; use std::error::Error; use std::io; @@ -30,7 +30,7 @@ use docopt::Docopt; use std::path::Path; use std::process::{exit, Command}; use tempdir::TempDir; -use cretonne::settings::Configurable; +use cretonne_codegen::settings::Configurable; const USAGE: &str = " Wasm to Cretonne IL translation utility. @@ -70,7 +70,7 @@ fn main() { .deserialize() }) .unwrap_or_else(|e| e.exit()); - let (mut flag_builder, isa_builder) = cton_native::builders().unwrap_or_else(|_| { + let (mut flag_builder, isa_builder) = cretonne_native::builders().unwrap_or_else(|_| { panic!("host machine is not a supported target"); }); @@ -84,7 +84,7 @@ fn main() { flag_builder.set("opt_level", "best").unwrap(); } - let isa = isa_builder.finish(settings::Flags::new(&flag_builder)); + let isa = isa_builder.finish(settings::Flags::new(flag_builder)); for filename in &args.arg_file { let path = Path::new(&filename); match handle_module(&args, path.to_path_buf(), &*isa) { diff --git a/src/wasm2obj.rs b/src/wasm2obj.rs index fd98a08233..2693666f6c 100644 --- a/src/wasm2obj.rs +++ b/src/wasm2obj.rs @@ -4,19 +4,19 @@ //! IL, then translates it to native code, and writes it out to a native //! object file with relocations. -extern crate cton_wasm; +extern crate cretonne_codegen; +extern crate cretonne_native; +extern crate cretonne_wasm; extern crate wasmstandalone_obj; extern crate wasmstandalone_runtime; -extern crate cretonne; -extern crate cton_native; extern crate docopt; #[macro_use] extern crate serde_derive; extern crate faerie; -use cton_wasm::translate_module; -use cretonne::settings; -use cretonne::isa; +use cretonne_wasm::translate_module; +use cretonne_codegen::settings; +use cretonne_codegen::isa; use wasmstandalone_obj::emit_module; use std::path::PathBuf; use std::fs::File; @@ -86,10 +86,10 @@ fn handle_module(path: PathBuf, output: &str) -> Result<(), String> { }; // FIXME: Make the target a parameter. - let (flag_builder, isa_builder) = cton_native::builders().unwrap_or_else(|_| { + let (flag_builder, isa_builder) = cretonne_native::builders().unwrap_or_else(|_| { panic!("host machine is not a supported target"); }); - let isa = isa_builder.finish(settings::Flags::new(&flag_builder)); + let isa = isa_builder.finish(settings::Flags::new(flag_builder)); let mut module = wasmstandalone_runtime::Module::new(); let mut environ = wasmstandalone_runtime::ModuleEnvironment::new(isa.flags(), &mut module);