From 405564c063f8d9270988a3e6df95cb7fd2eab9e2 Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Fri, 5 Jan 2024 15:28:40 +0100 Subject: [PATCH] refactor: module instantiation Signed-off-by: Henry Gressmann --- Cargo.lock | 88 +++++++++---------- crates/tinywasm/src/instance.rs | 41 +++++++-- crates/tinywasm/src/module.rs | 42 ++------- crates/tinywasm/src/runtime/executor/mod.rs | 17 +++- .../tinywasm/tests/generated/progress-mvp.svg | 4 +- 5 files changed, 103 insertions(+), 89 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3b17097..da14548 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -71,7 +71,7 @@ dependencies = [ "argh_shared", "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] @@ -139,9 +139,9 @@ dependencies = [ [[package]] name = "bstr" -version = "1.8.0" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "542f33a8835a0884b006a0c3df3dadd99c0c3f296ed26c2fdc8028e01ad6230c" +checksum = "c48f0051a4b4c5e0b6d365cd04af53aeaa209e3cc15ec2cdb69e73cc87fbd0dc" dependencies = [ "memchr", "serde", @@ -310,9 +310,9 @@ dependencies = [ [[package]] name = "cpufeatures" -version = "0.2.11" +version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce420fe07aecd3e67c5f910618fe65e94158f6dcc0adf44e00d69ce2bdfe0fd0" +checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504" dependencies = [ "libc", ] @@ -586,9 +586,9 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] name = "iana-time-zone" -version = "0.1.58" +version = "0.1.59" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8326b86b6cff230b97d0d312a6c40a60726df3332e721f72a1b035f451663b20" +checksum = "b6a67363e2aa4443928ce15e57ebae94fd8949958fd1223c4cfc0cd473ad7539" dependencies = [ "android_system_properties", "core-foundation-sys", @@ -636,13 +636,13 @@ checksum = "8e04e2fd2b8188ea827b32ef11de88377086d690286ab35747ef7f9bf3ccb590" [[package]] name = "is-terminal" -version = "0.4.9" +version = "0.4.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" +checksum = "0bad00257d07be169d870ab665980b06cdb366d792ad690bf2e76876dc503455" dependencies = [ "hermit-abi", "rustix", - "windows-sys 0.48.0", + "windows-sys 0.52.0", ] [[package]] @@ -719,9 +719,9 @@ checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" [[package]] name = "memchr" -version = "2.6.4" +version = "2.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" +checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" [[package]] name = "miniz_oxide" @@ -886,9 +886,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.71" +version = "1.0.75" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75cb1540fadbd5b8fbccc4dddad2734eba435053f725621c070711a14bb5f4b8" +checksum = "907a61bd0f64c2f29cd1cf1dc34d05176426a3f504a78010f08416ddb7b13708" dependencies = [ "unicode-ident", ] @@ -915,9 +915,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.33" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] @@ -1017,9 +1017,9 @@ dependencies = [ [[package]] name = "rust-embed" -version = "8.1.0" +version = "8.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "810294a8a4a0853d4118e3b94bb079905f2107c7fe979d8f0faae98765eb6378" +checksum = "a82c0bbc10308ed323529fd3c1dce8badda635aa319a5ff0e6466f33b8101e3f" dependencies = [ "rust-embed-impl", "rust-embed-utils", @@ -1028,22 +1028,22 @@ dependencies = [ [[package]] name = "rust-embed-impl" -version = "8.1.0" +version = "8.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfc144a1273124a67b8c1d7cd19f5695d1878b31569c0512f6086f0f4676604e" +checksum = "6227c01b1783cdfee1bcf844eb44594cd16ec71c35305bf1c9fb5aade2735e16" dependencies = [ "proc-macro2", "quote", "rust-embed-utils", - "syn 2.0.43", + "syn 2.0.48", "walkdir", ] [[package]] name = "rust-embed-utils" -version = "8.1.0" +version = "8.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "816ccd4875431253d6bb54b804bcff4369cbde9bae33defde25fdf6c2ef91d40" +checksum = "8cb0a25bfbb2d4b4402179c2cf030387d9990857ce08a32592c6238db9fa8665" dependencies = [ "globset", "sha2", @@ -1101,35 +1101,35 @@ checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b" [[package]] name = "semver" -version = "1.0.20" +version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090" +checksum = "b97ed7a9823b74f99c7742f5336af7be5ecd3eeafcb1507d1fa93347b1d589b0" [[package]] name = "serde" -version = "1.0.193" +version = "1.0.194" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" +checksum = "0b114498256798c94a0689e1a15fec6005dee8ac1f41de56404b67afc2a4b773" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.193" +version = "1.0.194" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" +checksum = "a3385e45322e8f9931410f01b3031ec534c3947d0e94c18049af4d9f9907d4e0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] name = "serde_json" -version = "1.0.108" +version = "1.0.111" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" +checksum = "176e46fa42316f18edd598015a5166857fc835ec732f5215eac6b7bdbf0a84f4" dependencies = [ "itoa", "ryu", @@ -1172,9 +1172,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.43" +version = "2.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee659fb5f3d355364e1f3e5bc10fb82068efbf824a1e9d1c9504244a6469ad53" +checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" dependencies = [ "proc-macro2", "quote", @@ -1198,22 +1198,22 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.52" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83a48fd946b02c0a526b2e9481c8e2a17755e47039164a86c4070446e3a4614d" +checksum = "d54378c645627613241d077a3a79db965db602882668f9136ac42af9ecb730ad" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.52" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7fbe9b594d6568a6a1443250a7e67d80b74e1e96f6d1715e1e21cc1888291d3" +checksum = "fa0faa943b50f3db30a20aa7e265dbc66076993efed8463e8de414e5d06d3471" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] @@ -1350,7 +1350,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", "wasm-bindgen-shared", ] @@ -1372,7 +1372,7 @@ checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -1469,11 +1469,11 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows-core" -version = "0.51.1" +version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" dependencies = [ - "windows-targets 0.48.5", + "windows-targets 0.52.0", ] [[package]] diff --git a/crates/tinywasm/src/instance.rs b/crates/tinywasm/src/instance.rs index 86a9296..a584571 100644 --- a/crates/tinywasm/src/instance.rs +++ b/crates/tinywasm/src/instance.rs @@ -1,12 +1,11 @@ use alloc::{boxed::Box, string::ToString, sync::Arc, vec::Vec}; use tinywasm_types::{ - DataAddr, ElmAddr, Export, ExternalKind, FuncAddr, FuncType, GlobalAddr, Import, MemAddr, ModuleInstanceAddr, - TableAddr, + DataAddr, ElmAddr, ExternalKind, FuncAddr, FuncType, GlobalAddr, Import, MemAddr, ModuleInstanceAddr, TableAddr, }; use crate::{ func::{FromWasmValueTuple, IntoWasmValueTuple}, - Error, ExportInstance, FuncHandle, Result, Store, TypedFuncHandle, + Error, ExportInstance, FuncHandle, Module, Result, Store, TypedFuncHandle, }; /// A WebAssembly Module Instance @@ -36,6 +35,38 @@ pub(crate) struct ModuleInstanceInner { } impl ModuleInstance { + /// Instantiate the module in the given store + pub fn instantiate(store: &mut Store, module: Module) -> Result { + let idx = store.next_module_instance_idx(); + + let func_addrs = store.add_funcs(module.data.funcs.into(), idx); + let table_addrs = store.add_tables(module.data.table_types.into(), idx); + let mem_addrs = store.add_mems(module.data.memory_types.into(), idx); + let global_addrs = store.add_globals(module.data.globals.into(), idx); + let elem_addrs = store.add_elems(module.data.elements.into(), idx); + let data_addrs = store.add_datas(module.data.data.into(), idx); + + let instance = ModuleInstanceInner { + store_id: store.id(), + idx, + + types: module.data.func_types, + func_addrs, + table_addrs, + mem_addrs, + global_addrs, + elem_addrs, + data_addrs, + + func_start: module.data.start_func, + imports: module.data.imports, + exports: crate::ExportInstance(module.data.exports), + }; + let instance = ModuleInstance::new(instance); + store.add_instance(instance.clone())?; + Ok(instance) + } + /// Get the module's exports pub fn exports(&self) -> &ExportInstance { &self.0.exports @@ -101,7 +132,7 @@ impl ModuleInstance { /// (which is not part of the spec, but used by llvm) /// /// See - pub fn start_func(&mut self, store: &Store) -> Result> { + pub fn start_func(&self, store: &Store) -> Result> { if self.0.store_id != store.id() { return Err(Error::InvalidStore); } @@ -135,7 +166,7 @@ impl ModuleInstance { /// Returns None if the module has no start function /// /// See - pub fn start(&mut self, store: &mut Store) -> Result> { + pub fn start(&self, store: &mut Store) -> Result> { let Some(func) = self.start_func(store)? else { return Ok(None); }; diff --git a/crates/tinywasm/src/module.rs b/crates/tinywasm/src/module.rs index e26db90..51d9f33 100644 --- a/crates/tinywasm/src/module.rs +++ b/crates/tinywasm/src/module.rs @@ -1,14 +1,13 @@ -use alloc::vec::Vec; use tinywasm_types::TinyWasmModule; -use crate::{instance::ModuleInstanceInner, ModuleInstance, Result, Store}; +use crate::{ModuleInstance, Result, Store}; #[derive(Debug)] /// A WebAssembly Module /// /// See pub struct Module { - data: TinyWasmModule, + pub(crate) data: TinyWasmModule, } impl From<&TinyWasmModule> for Module { @@ -50,8 +49,8 @@ impl Module { /// Instantiate the module in the given store /// - // TODO: /// Runs the start function if it exists - // /// If you want to run the start function yourself, use `ModuleInstance::new` + /// Runs the start function if it exists + /// If you want to run the start function yourself, use `ModuleInstance::instantiate` /// /// See pub fn instantiate( @@ -59,37 +58,8 @@ impl Module { store: &mut Store, // imports: Option<()>, ) -> Result { - let idx = store.next_module_instance_idx(); - - let func_addrs = store.add_funcs(self.data.funcs.into(), idx); - let table_addrs = store.add_tables(self.data.table_types.into(), idx); - let mem_addrs = store.add_mems(self.data.memory_types.into(), idx); - let global_addrs = store.add_globals(self.data.globals.into(), idx); - let elem_addrs = store.add_elems(self.data.elements.into(), idx); - let data_addrs = store.add_datas(self.data.data.into(), idx); - - let instance = ModuleInstanceInner { - store_id: store.id(), - idx, - - types: self.data.func_types, - func_addrs, - table_addrs, - mem_addrs, - global_addrs, - elem_addrs, - data_addrs, - - func_start: self.data.start_func, - imports: self.data.imports, - exports: crate::ExportInstance(self.data.exports), - }; - - let instance = ModuleInstance::new(instance); - store.add_instance(instance.clone())?; - - // TODO: Auto-run start function? - // let _ = instance.start(store)?; + let instance = ModuleInstance::instantiate(store, self)?; + let _ = instance.start(store)?; Ok(instance) } } diff --git a/crates/tinywasm/src/runtime/executor/mod.rs b/crates/tinywasm/src/runtime/executor/mod.rs index ca404fa..8ae703e 100644 --- a/crates/tinywasm/src/runtime/executor/mod.rs +++ b/crates/tinywasm/src/runtime/executor/mod.rs @@ -5,11 +5,11 @@ use crate::{ get_label_args, log::debug, runtime::{BlockType, LabelFrame}, - CallFrame, Error, ModuleInstance, Result, Store, + CallFrame, Error, ModuleInstance, RawWasmValue, Result, Store, }; use alloc::vec::Vec; use log::info; -use tinywasm_types::Instruction; +use tinywasm_types::{ConstInstruction, Instruction}; mod macros; mod traits; @@ -75,6 +75,19 @@ enum ExecResult { Trap(crate::Trap), } +/// Execute a const instruction +pub(crate) fn exec_const(instr: ConstInstruction) -> RawWasmValue { + match instr { + ConstInstruction::F32Const(val) => val.into(), + ConstInstruction::F64Const(val) => val.into(), + ConstInstruction::I32Const(val) => val.into(), + ConstInstruction::I64Const(val) => val.into(), + ConstInstruction::GlobalGet(_) => unimplemented!("global get"), + ConstInstruction::RefFunc(_) => unimplemented!("ref func"), + ConstInstruction::RefNull(_) => unimplemented!("ref null"), + } +} + /// Run a single step of the interpreter /// A seperate function is used so later, we can more easily implement /// a step-by-step debugger (using generators once they're stable?) diff --git a/crates/tinywasm/tests/generated/progress-mvp.svg b/crates/tinywasm/tests/generated/progress-mvp.svg index ec72fb3..0feea61 100644 --- a/crates/tinywasm/tests/generated/progress-mvp.svg +++ b/crates/tinywasm/tests/generated/progress-mvp.svg @@ -56,9 +56,9 @@ v0.1.0 (17630) v0.2.0-alpha.0 (17747) - - + +