diff --git a/docs/cli-options.md b/docs/cli-options.md index be78272a78..a5d5062055 100644 --- a/docs/cli-options.md +++ b/docs/cli-options.md @@ -71,16 +71,6 @@ $ wasmtime config new And that'll print out the path to the file you can edit. -## `wasm2obj` - -This is an experimental subcommand to compile a WebAssembly module to native -code. Work for this is still heavily under development, but you can execute this -with: - -```sh -$ wasmtime wasm2obj foo.wasm foo.o -``` - ## `compile` This subcommand is used to Ahead-Of-Time (AOT) compile a WebAssembly module to produce diff --git a/src/bin/wasmtime.rs b/src/bin/wasmtime.rs index 475f2c607b..de66f18cf2 100644 --- a/src/bin/wasmtime.rs +++ b/src/bin/wasmtime.rs @@ -6,7 +6,7 @@ use anyhow::Result; use structopt::{clap::AppSettings, clap::ErrorKind, StructOpt}; use wasmtime_cli::commands::{ - CompileCommand, ConfigCommand, RunCommand, SettingsCommand, WasmToObjCommand, WastCommand, + CompileCommand, ConfigCommand, RunCommand, SettingsCommand, WastCommand, }; /// Wasmtime WebAssembly Runtime @@ -44,9 +44,6 @@ enum WasmtimeApp { Run(RunCommand), /// Displays available Cranelift settings for a target. Settings(SettingsCommand), - /// Translates a WebAssembly module to native object file - #[structopt(name = "wasm2obj")] - WasmToObj(WasmToObjCommand), /// Runs a WebAssembly test script file Wast(WastCommand), } @@ -59,7 +56,6 @@ impl WasmtimeApp { Self::Compile(c) => c.execute(), Self::Run(c) => c.execute(), Self::Settings(c) => c.execute(), - Self::WasmToObj(c) => c.execute(), Self::Wast(c) => c.execute(), } } diff --git a/src/commands.rs b/src/commands.rs index ebed3d732b..be3a9624e1 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -4,7 +4,6 @@ mod compile; mod config; mod run; mod settings; -mod wasm2obj; mod wast; -pub use self::{compile::*, config::*, run::*, settings::*, wasm2obj::*, wast::*}; +pub use self::{compile::*, config::*, run::*, settings::*, wast::*}; diff --git a/src/commands/run.rs b/src/commands/run.rs index 577efd4f20..bbcf252a79 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -22,8 +22,9 @@ use wasmtime_wasi_crypto::WasiCryptoCtx; fn parse_module(s: &OsStr) -> Result { // Do not accept wasmtime subcommand names as the module name match s.to_str() { - Some("help") | Some("config") | Some("run") | Some("wasm2obj") | Some("wast") - | Some("compile") => Err("module name cannot be the same as a subcommand".into()), + Some("help") | Some("config") | Some("run") | Some("wast") | Some("compile") => { + Err("module name cannot be the same as a subcommand".into()) + } _ => Ok(s.into()), } } diff --git a/src/commands/wasm2obj.rs b/src/commands/wasm2obj.rs deleted file mode 100644 index 46fe5fd0c7..0000000000 --- a/src/commands/wasm2obj.rs +++ /dev/null @@ -1,76 +0,0 @@ -//! The module that implements the `wasmtime wasm2obj` command. - -use crate::obj::compile_to_obj; -use crate::{parse_target, pick_compilation_strategy, CommonOptions}; -use anyhow::{Context as _, Result}; -use std::{ - fs::File, - io::Write, - path::{Path, PathBuf}, -}; -use structopt::{clap::AppSettings, StructOpt}; -use target_lexicon::Triple; - -lazy_static::lazy_static! { - static ref AFTER_HELP: String = { - format!( - "The translation is dependent on the environment chosen.\n\ - The default is a dummy environment that produces placeholder values.\n\ - \n\ - {}", - crate::FLAG_EXPLANATIONS.as_str() - ) - }; -} - -/// Translates a WebAssembly module to native object file -#[derive(StructOpt)] -#[structopt( - name = "wasm2obj", - version = env!("CARGO_PKG_VERSION"), - setting = AppSettings::ColoredHelp, - after_help = AFTER_HELP.as_str(), -)] -pub struct WasmToObjCommand { - #[structopt(flatten)] - common: CommonOptions, - - /// The path of the WebAssembly module to translate - #[structopt(index = 1, value_name = "MODULE_PATH", parse(from_os_str))] - module: PathBuf, - - /// The path of the output object file - #[structopt(index = 2, value_name = "OUTPUT_PATH")] - output: String, - - /// The target triple; default is the host triple - #[structopt(long, value_name = "TARGET", parse(try_from_str = parse_target))] - target: Option, -} - -impl WasmToObjCommand { - /// Executes the command. - pub fn execute(self) -> Result<()> { - self.common.init_logging(); - - let strategy = pick_compilation_strategy(self.common.cranelift, self.common.lightbeam)?; - - let data = wat::parse_file(&self.module).context("failed to parse module")?; - - let obj = compile_to_obj( - &data, - self.target.as_ref(), - strategy, - self.common.enable_simd, - self.common.opt_level(), - self.common.debug_info, - )?; - - let mut file = - File::create(Path::new(&self.output)).context("failed to create object file")?; - file.write_all(&obj) - .context("failed to write object file")?; - - Ok(()) - } -} diff --git a/src/lib.rs b/src/lib.rs index a6ac4f2bf2..e2063d71d0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -94,17 +94,13 @@ lazy_static::lazy_static! { } pub mod commands; -mod obj; use anyhow::{bail, Result}; use std::collections::HashMap; use std::path::PathBuf; use structopt::StructOpt; -use target_lexicon::Triple; use wasmtime::{Config, ProfilingStrategy, Strategy}; -pub use obj::compile_to_obj; - fn pick_compilation_strategy(cranelift: bool, lightbeam: bool) -> Result { Ok(match (lightbeam, cranelift) { (true, false) => Strategy::Lightbeam, @@ -528,11 +524,6 @@ fn parse_cranelift_flag(name_and_value: &str) -> Result<(String, String)> { Ok((name, value)) } -fn parse_target(s: &str) -> Result { - use std::str::FromStr; - Triple::from_str(&s).map_err(|e| anyhow::anyhow!(e)) -} - #[cfg(test)] mod test { use super::*; diff --git a/src/obj.rs b/src/obj.rs deleted file mode 100644 index bff0957f7f..0000000000 --- a/src/obj.rs +++ /dev/null @@ -1,67 +0,0 @@ -use anyhow::{bail, Context as _, Result}; -use std::mem; -use target_lexicon::Triple; -use wasmparser::WasmFeatures; -use wasmtime::Strategy; -use wasmtime_environ::{ModuleEnvironment, PrimaryMap, Tunables}; - -/// Creates object file from binary wasm data. -pub fn compile_to_obj( - wasm: &[u8], - target: Option<&Triple>, - strategy: Strategy, - enable_simd: bool, - opt_level: wasmtime::OptLevel, - debug_info: bool, -) -> Result> { - match strategy { - Strategy::Cranelift | Strategy::Auto => {} - other => panic!("unsupported strategy {:?}", other), - } - let mut builder = wasmtime_cranelift::builder(); - if let Some(target) = target { - builder.target(target.clone())?; - } - let mut features = WasmFeatures::default(); - - if enable_simd { - builder.enable("enable_simd").unwrap(); - features.simd = true; - } - - match opt_level { - wasmtime::OptLevel::None => {} - wasmtime::OptLevel::Speed => { - builder.set("opt_level", "speed").unwrap(); - } - wasmtime::OptLevel::SpeedAndSize => { - builder.set("opt_level", "speed_and_size").unwrap(); - } - other => bail!("unknown optimization level {:?}", other), - } - - // TODO: Expose the tunables as command-line flags. - let mut tunables = Tunables::default(); - tunables.generate_native_debuginfo = debug_info; - tunables.parse_wasm_debuginfo = debug_info; - - let compiler = builder.build(); - let environ = ModuleEnvironment::new(&tunables, &features); - let (_main_module, mut translation, types) = environ - .translate(wasm) - .context("failed to translate module")?; - assert_eq!(translation.len(), 1); - let mut funcs = PrimaryMap::default(); - for (index, func) in mem::take(&mut translation[0].function_body_inputs) { - funcs.push(compiler.compile_function(&translation[0], index, func, &tunables, &types)?); - } - let mut obj = compiler.object()?; - compiler.emit_obj( - &translation[0], - &types, - funcs, - tunables.generate_native_debuginfo, - &mut obj, - )?; - Ok(obj.write()?) -} diff --git a/tests/all/debug/obj.rs b/tests/all/debug/obj.rs index 3818572c09..d83edad19b 100644 --- a/tests/all/debug/obj.rs +++ b/tests/all/debug/obj.rs @@ -3,25 +3,24 @@ use std::fs::File; use std::io::Write; use std::path::Path; use target_lexicon::Triple; -use wasmtime::Strategy; -use wasmtime_cli::compile_to_obj; +use wasmtime::{Config, Engine, Module}; pub fn compile_cranelift( wasm: &[u8], target: Option, output: impl AsRef, ) -> Result<()> { - let obj = compile_to_obj( - wasm, - target.as_ref(), - Strategy::Cranelift, - false, - wasmtime::OptLevel::None, - true, - )?; + let mut config = Config::new(); + config.debug_info(true); + if let Some(target) = target { + config.target(&target.to_string())?; + } + let engine = Engine::new(&config)?; + let module = Module::new(&engine, wasm)?; + let bytes = module.serialize()?; let mut file = File::create(output).context("failed to create object file")?; - file.write_all(&obj) + file.write_all(&bytes) .context("failed to write object file")?; Ok(())