This commit is intended to be the first of many in implementing the module linking proposal. At this time this builds on #2059 so it shouldn't land yet. The goal of this commit is to compile bare-bones modules which use module linking, e.g. those with nested modules. My hope with module linking is that almost everything in wasmtime only needs mild refactorings to handle it. The goal is that all per-module structures are still per-module and at the top level there's just a `Vec` containing a bunch of modules. That's implemented currently where `wasmtime::Module` contains `Arc<[CompiledModule]>` and an index of which one it's pointing to. This should enable serialization/deserialization of any module in a nested modules scenario, no matter how you got it. Tons of features of the module linking proposal are missing from this commit. For example instantiation flat out doesn't work, nor does import/export of modules or instances. That'll be coming as future commits, but the purpose here is to start laying groundwork in Wasmtime for handling lots of modules in lots of places.
74 lines
2.5 KiB
Rust
74 lines
2.5 KiB
Rust
use anyhow::{bail, Context as _, Result};
|
|
use object::write::Object;
|
|
use target_lexicon::Triple;
|
|
use wasmparser::WasmFeatures;
|
|
use wasmtime::Strategy;
|
|
use wasmtime_environ::{settings, settings::Configurable, ModuleEnvironment, Tunables};
|
|
use wasmtime_jit::{native, Compiler};
|
|
|
|
/// 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<Object> {
|
|
let isa_builder = match target {
|
|
Some(target) => native::lookup(target.clone())?,
|
|
None => native::builder(),
|
|
};
|
|
let mut flag_builder = settings::builder();
|
|
let mut features = WasmFeatures::default();
|
|
|
|
// There are two possible traps for division, and this way
|
|
// we get the proper one if code traps.
|
|
flag_builder.enable("avoid_div_traps").unwrap();
|
|
|
|
if enable_simd {
|
|
flag_builder.enable("enable_simd").unwrap();
|
|
features.simd = true;
|
|
}
|
|
|
|
match opt_level {
|
|
wasmtime::OptLevel::None => {}
|
|
wasmtime::OptLevel::Speed => {
|
|
flag_builder.set("opt_level", "speed").unwrap();
|
|
}
|
|
wasmtime::OptLevel::SpeedAndSize => {
|
|
flag_builder.set("opt_level", "speed_and_size").unwrap();
|
|
}
|
|
other => bail!("unknown optimization level {:?}", other),
|
|
}
|
|
|
|
let isa = isa_builder.finish(settings::Flags::new(flag_builder));
|
|
|
|
// TODO: Expose the tunables as command-line flags.
|
|
let mut tunables = Tunables::default();
|
|
tunables.debug_info = debug_info;
|
|
|
|
let compiler = Compiler::new(
|
|
isa,
|
|
match strategy {
|
|
Strategy::Auto => wasmtime_jit::CompilationStrategy::Auto,
|
|
Strategy::Cranelift => wasmtime_jit::CompilationStrategy::Cranelift,
|
|
#[cfg(feature = "lightbeam")]
|
|
Strategy::Lightbeam => wasmtime_jit::CompilationStrategy::Lightbeam,
|
|
#[cfg(not(feature = "lightbeam"))]
|
|
Strategy::Lightbeam => bail!("lightbeam support not enabled"),
|
|
s => bail!("unknown compilation strategy {:?}", s),
|
|
},
|
|
tunables.clone(),
|
|
features.clone(),
|
|
);
|
|
|
|
let environ = ModuleEnvironment::new(compiler.isa().frontend_config(), &tunables, &features);
|
|
let mut translation = environ
|
|
.translate(wasm)
|
|
.context("failed to translate module")?;
|
|
assert_eq!(translation.len(), 1);
|
|
let compilation = compiler.compile(&mut translation[0])?;
|
|
Ok(compilation.obj)
|
|
}
|