diff --git a/Cargo.lock b/Cargo.lock index 1caf6ca..abecac9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -302,29 +302,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "wasm2llvm" version = "0.1.0" dependencies = [ + "parity-wasm 0.40.2 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "wabt 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", - "wasmi 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "wasmi 0.5.1", "which 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "wasmi" version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", "memory_units 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "num-rational 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "parity-wasm 0.40.2 (registry+https://github.com/rust-lang/crates.io-index)", - "wasmi-validation 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "wasmi-validation 0.2.0", ] [[package]] name = "wasmi-validation" version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "parity-wasm 0.40.2 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -397,8 +396,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum wabt 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3c5c5c1286c6e578416982609f47594265f9d489f9b836157d403ad605a46693" "checksum wabt-sys 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "af5d153dc96aad7dc13ab90835b892c69867948112d95299e522d370c4e13a08" "checksum wasi 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b89c3ce4ce14bdc6fb6beaf9ec7928ca331de5df7e5ea278375642a2f478570d" -"checksum wasmi 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f31d26deb2d9a37e6cfed420edce3ed604eab49735ba89035e13c98f9a528313" -"checksum wasmi-validation 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6bc0356e3df56e639fc7f7d8a99741915531e27ed735d911ed83d7e1339c8188" "checksum which 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "240a31163872f7e8e49f35b42b58485e35355b07eb009d9f3686733541339a69" "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" diff --git a/Cargo.toml b/Cargo.toml index 65d9a34..5b4f3aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,8 @@ edition = "2018" [dependencies] wabt = "0.9.0" tempfile = "3.1.0" -wasmi = "0.5.1" +# TODO remove +wasmi = { path = "../wasmi" } +parity-wasm = "0.40" # test which = "3.0.0" diff --git a/src/lib.rs b/src/lib.rs index 8c9c047..78f055e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ extern crate wabt; extern crate tempfile; extern crate wasmi; +extern crate parity_wasm; pub fn wasm2ll(func: &str, _wasm: &[u8]) -> String { String::from(format!(r#" @@ -43,27 +44,50 @@ mod tests { // wasm.write_all(&module_binary); // wasm.flush(); - use wasmi::{Module, ModuleInstance, ImportsBuilder, NopExternals, GlobalRef, ExternVal::Func, FuncInstance}; - use std::cell::{Ref}; - let module = Module::from_buffer(buf).unwrap(); - let not_started = ModuleInstance::new(&module, &ImportsBuilder::default()) - .expect("Failed to instantiate module") - .run_start(&mut NopExternals) - .expect("Failed to run start function in module"); + // use wasmi::{Module, ModuleInstance, ImportsBuilder, NopExternals, GlobalRef, ExternVal::Func, FuncInstance, parity_wasm::elements::Instructions}; + // use std::cell::{Ref}; + // let module = Module::from_buffer(buf).unwrap(); + // let not_started = ModuleInstance::new(&module, &ImportsBuilder::default()) + // .expect("Failed to instantiate module") + // .run_start(&mut NopExternals) + // .expect("Failed to run start function in module"); + // let tmp: Ref> = not_started.globals(); // for global in &*tmp { // println!("{:?}", global); // } - match not_started.export_by_name("add") { - None => (), - Some(Func(func_ref)) => { - // here - let fun: &FuncInstance = &*func_ref; - let body = fun.body(); // XXX method `body` is private - // FuncBody::code - }, - _ => () - }; + + // match not_started.export_by_name("add") { + // None => (), + // Some(Func(func_ref)) => { + // // here + // let fun: &FuncInstance = &*func_ref; + // let body = fun.body().unwrap(); // XXX method `body` is private + // // found struct `wasmi::isa::Instructions` + // let code: parity_wasm::elements::Instructions = body.code; + // for i in code.elements() { + // println!("ins"); + // } + // }, + // _ => () + // }; + + use parity_wasm::elements::{Module, Internal::*}; + let module: Module = parity_wasm::deserialize_buffer(&buf).unwrap(); + for e in module.export_section().unwrap().entries() { + if e.field() == "add" { + if let Function(function_index) = e.internal() { + // println!("{:?}", index); + let code = &module.code_section().expect("Already checked, impossible").bodies()[*function_index as usize]; + println!("code: "); + for instruction in code.code().elements() { + println!("{}", instruction); + } + + break; + } + } + } funcs.insert(String::from("add"), &[0, 97, 115, 109]); // TODO }