More code reorganization and cleanups.

This commit is contained in:
Dan Gohman
2018-08-03 15:06:59 -07:00
parent 33b7dfac00
commit ef5254c0da
12 changed files with 88 additions and 42 deletions

View File

@@ -18,9 +18,6 @@ path = "src/wasm2obj.rs"
[dependencies]
cranelift-codegen = "0.18.1"
cranelift-frontend = "0.18.1"
cranelift-reader = "0.18.1"
cranelift-wasm = "0.18.1"
cranelift-native = "0.18.1"
wasmtime-runtime = { path = "lib/runtime" }
wasmtime-execute = { path = "lib/execute" }

View File

@@ -1 +1 @@
doc-valid-idents = [ "WebAssembly", "NaN" ]
doc-valid-idents = [ "WebAssembly" ]

View File

@@ -24,11 +24,10 @@ fuzz_target!(|data: &[u8]| {
let isa = isa_builder.finish(settings::Flags::new(flag_builder));
let mut module = Module::new();
let mut runtime = ModuleEnvironment::new(&*isa, &mut module);
match translate_module(&data, &mut runtime) {
let translation = match translate_module(&data, &mut runtime) {
Ok(()) => (),
Err(_) => return,
};
let translation = runtime.finish_translation();
let _exec = match wasmtime_execute::compile_module(&*isa, &translation) {
Ok(x) => x,
Err(_) => return,

View File

@@ -37,7 +37,7 @@ fn relocate(compilation: &mut Compilation, relocations: &[Vec<Relocation>]) {
match r.reloc {
Reloc::Abs8 => unsafe {
let reloc_address = body.as_mut_ptr().offset(r.offset as isize) as i64;
let reloc_addend = r.addend as i64;
let reloc_addend = r.addend;
let reloc_abs = target_func_address as i64 + reloc_addend;
write_unaligned(reloc_address as *mut i64, reloc_abs);
},

View File

@@ -1,6 +1,16 @@
//! JIT-style runtime for WebAssembly using Cranelift.
#![deny(missing_docs)]
#![deny(missing_docs, trivial_numeric_casts, unused_extern_crates, unstable_features)]
#![warn(unused_import_braces)]
#![cfg_attr(feature = "clippy", plugin(clippy(conf_file = "../../clippy.toml")))]
#![cfg_attr(feature = "cargo-clippy", allow(new_without_default, new_without_default_derive))]
#![cfg_attr(
feature = "cargo-clippy",
warn(
float_arithmetic, mut_mut, nonminimal_bool, option_map_unwrap_or, option_map_unwrap_or_else,
print_stdout, unicode_not_nfc, use_self
)
)]
extern crate cranelift_codegen;
extern crate cranelift_wasm;

View File

@@ -7,6 +7,5 @@ license = "Apache-2.0 WITH LLVM-exception"
[dependencies]
cranelift-codegen = "0.18.1"
cranelift-wasm = "0.18.1"
wasmtime-runtime = { path = "../runtime" }
faerie = "0.4.4"

View File

@@ -1,5 +1,18 @@
//! Object-file writing library using the wasmtime runtime.
#![deny(missing_docs, trivial_numeric_casts, unused_extern_crates, unstable_features)]
#![warn(unused_import_braces)]
#![cfg_attr(feature = "clippy", plugin(clippy(conf_file = "../../clippy.toml")))]
#![cfg_attr(feature = "cargo-clippy", allow(new_without_default, new_without_default_derive))]
#![cfg_attr(
feature = "cargo-clippy",
warn(
float_arithmetic, mut_mut, nonminimal_bool, option_map_unwrap_or, option_map_unwrap_or_else,
print_stdout, unicode_not_nfc, use_self
)
)]
extern crate cranelift_codegen;
extern crate cranelift_wasm;
extern crate faerie;
extern crate wasmtime_runtime;

View File

@@ -71,8 +71,8 @@ impl binemit::RelocSink for RelocSink {
}
impl RelocSink {
fn new() -> RelocSink {
RelocSink {
fn new() -> Self {
Self {
func_relocs: Vec::new(),
}
}

View File

@@ -8,10 +8,9 @@ use cranelift_codegen::ir::{
};
use cranelift_codegen::isa;
use cranelift_codegen::settings;
use cranelift_wasm;
use cranelift_wasm::{
FunctionIndex, Global, GlobalIndex, GlobalVariable, Memory, MemoryIndex, SignatureIndex, Table,
TableIndex, WasmResult,
self, FunctionIndex, Global, GlobalIndex, GlobalVariable, Memory, MemoryIndex, SignatureIndex, Table,
TableIndex, WasmResult, translate_module,
};
use module::{DataInitializer, Export, LazyContents, Module, TableElements};
use target_lexicon::Triple;
@@ -23,7 +22,7 @@ pub fn get_func_name(func_index: FunctionIndex) -> ir::ExternalName {
}
/// Object containing the standalone runtime information. To be passed after creation as argument
/// to `cranelift_wasm::translatemodule`.
/// to `compile_module`.
pub struct ModuleEnvironment<'data, 'module> {
/// Compilation setting flags.
pub isa: &'module isa::TargetIsa,
@@ -54,16 +53,18 @@ impl<'data, 'module> ModuleEnvironment<'data, 'module> {
self.func_env().pointer_type()
}
/// Declare that translation of the module is complete. This consumes the
/// `ModuleEnvironment` with its mutable reference to the `Module` and
/// produces a `ModuleTranslation` with an immutable reference to the
/// `Module`.
pub fn finish_translation(self) -> ModuleTranslation<'data, 'module> {
ModuleTranslation {
/// Translate the given wasm module data using this environment. This consumes the
/// `ModuleEnvironment` with its mutable reference to the `Module` and produces a
/// `ModuleTranslation` with an immutable reference to the `Module` (which has
/// become fully populated).
pub fn translate(mut self, data: &'data [u8]) -> WasmResult<ModuleTranslation<'data, 'module>> {
translate_module(data, &mut self)?;
Ok(ModuleTranslation {
isa: self.isa,
module: self.module,
lazy: self.lazy,
}
})
}
}
@@ -117,7 +118,7 @@ impl<'module_environment> FuncEnvironment<'module_environment> {
}
/// This trait is useful for
/// `cranelift_wasm::translatemodule` because it
/// `cranelift_wasm::translate_module` because it
/// tells how to translate runtime-dependent wasm instructions. These functions should not be
/// called by the user.
impl<'data, 'module> cranelift_wasm::ModuleEnvironment<'data>
@@ -176,7 +177,7 @@ impl<'data, 'module> cranelift_wasm::ModuleEnvironment<'data>
self.module.globals.push(global);
}
fn get_global(&self, global_index: GlobalIndex) -> &cranelift_wasm::Global {
fn get_global(&self, global_index: GlobalIndex) -> &Global {
&self.module.globals[global_index]
}
@@ -273,7 +274,7 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
self.globals_base = Some(new_base);
new_base
});
let offset = index as usize * pointer_bytes;
let offset = index * pointer_bytes;
let offset32 = offset as i32;
debug_assert_eq!(offset32 as usize, offset);
let gv = func.create_global_value(ir::GlobalValueData::Deref {
@@ -295,7 +296,7 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
self.globals_base = Some(new_base);
new_base
});
let offset = index as usize * pointer_bytes;
let offset = index * pointer_bytes;
let offset32 = offset as i32;
debug_assert_eq!(offset32 as usize, offset);
let heap_base_addr = func.create_global_value(ir::GlobalValueData::Deref {

View File

@@ -3,7 +3,17 @@
//! the translation the base addresses of regions of memory that will hold the globals, tables and
//! linear memories.
#![deny(missing_docs)]
#![deny(missing_docs, trivial_numeric_casts, unused_extern_crates, unstable_features)]
#![warn(unused_import_braces)]
#![cfg_attr(feature = "clippy", plugin(clippy(conf_file = "../../clippy.toml")))]
#![cfg_attr(feature = "cargo-clippy", allow(new_without_default, new_without_default_derive))]
#![cfg_attr(
feature = "cargo-clippy",
warn(
float_arithmetic, mut_mut, nonminimal_bool, option_map_unwrap_or, option_map_unwrap_or_else,
print_stdout, unicode_not_nfc, use_self
)
)]
extern crate cranelift_codegen;
extern crate cranelift_wasm;

View File

@@ -5,9 +5,20 @@
//! 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.
#![deny(missing_docs, trivial_numeric_casts, unused_extern_crates, unstable_features)]
#![warn(unused_import_braces)]
#![cfg_attr(feature = "clippy", plugin(clippy(conf_file = "../../clippy.toml")))]
#![cfg_attr(feature = "cargo-clippy", allow(new_without_default, new_without_default_derive))]
#![cfg_attr(
feature = "cargo-clippy",
warn(
float_arithmetic, mut_mut, nonminimal_bool, option_map_unwrap_or, option_map_unwrap_or_else,
unicode_not_nfc, use_self
)
)]
extern crate cranelift_codegen;
extern crate cranelift_native;
extern crate cranelift_wasm;
extern crate docopt;
extern crate wasmtime_execute;
extern crate wasmtime_runtime;
@@ -18,7 +29,6 @@ extern crate tempdir;
use cranelift_codegen::isa::TargetIsa;
use cranelift_codegen::settings;
use cranelift_codegen::settings::Configurable;
use cranelift_wasm::translate_module;
use docopt::Docopt;
use std::error::Error;
use std::fs::File;
@@ -119,9 +129,8 @@ fn handle_module(args: &Args, path: PathBuf, isa: &TargetIsa) -> Result<(), Stri
data = read_to_end(file_path).map_err(|err| String::from(err.description()))?;
}
let mut module = Module::new();
let mut environ = ModuleEnvironment::new(isa, &mut module);
translate_module(&data, &mut environ).map_err(|e| e.to_string())?;
let translation = environ.finish_translation();
let environ = ModuleEnvironment::new(isa, &mut module);
let translation = environ.translate(&data).map_err(|e| e.to_string())?;
let instance = match compile_and_link_module(isa, &translation) {
Ok(compilation) => {
let mut instance =

View File

@@ -4,9 +4,20 @@
//! IL, then translates it to native code, and writes it out to a native
//! object file with relocations.
#![deny(missing_docs, trivial_numeric_casts, unused_extern_crates, unstable_features)]
#![warn(unused_import_braces)]
#![cfg_attr(feature = "clippy", plugin(clippy(conf_file = "../../clippy.toml")))]
#![cfg_attr(feature = "cargo-clippy", allow(new_without_default, new_without_default_derive))]
#![cfg_attr(
feature = "cargo-clippy",
warn(
float_arithmetic, mut_mut, nonminimal_bool, option_map_unwrap_or, option_map_unwrap_or_else,
unicode_not_nfc, use_self
)
)]
extern crate cranelift_codegen;
extern crate cranelift_native;
extern crate cranelift_wasm;
extern crate docopt;
extern crate wasmtime_obj;
extern crate wasmtime_runtime;
@@ -15,7 +26,6 @@ extern crate serde_derive;
extern crate faerie;
use cranelift_codegen::settings;
use cranelift_wasm::translate_module;
use docopt::Docopt;
use faerie::Artifact;
use std::error::Error;
@@ -91,21 +101,19 @@ fn handle_module(path: PathBuf, output: &str) -> Result<(), String> {
});
let isa = isa_builder.finish(settings::Flags::new(flag_builder));
let mut module = wasmtime_runtime::Module::new();
let mut environ = wasmtime_runtime::ModuleEnvironment::new(&*isa, &mut module);
translate_module(&data, &mut environ).map_err(|e| e.to_string())?;
let mut obj = Artifact::new(isa.triple().clone(), String::from(output));
let mut module = wasmtime_runtime::Module::new();
let environ = wasmtime_runtime::ModuleEnvironment::new(&*isa, &mut module);
let translation = environ.translate(&data).map_err(|e| e.to_string())?;
// FIXME: We need to initialize memory in a way that supports alternate
// memory spaces, imported base addresses, and offsets.
for init in &environ.lazy.data_initializers {
for init in &translation.lazy.data_initializers {
obj.define("memory", Vec::from(init.data))
.map_err(|err| format!("{}", err))?;
}
let translation = environ.finish_translation();
let (compilation, relocations) = compile_module(&translation, &*isa)?;
emit_module(&mut obj, &translation.module, &compilation, &relocations)?;