diff --git a/Cargo.lock b/Cargo.lock index eaff280..8bc1e93 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -674,6 +674,15 @@ version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" +[[package]] +name = "heck" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "hermit-abi" version = "0.1.19" @@ -698,6 +707,12 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" +[[package]] +name = "id-arena" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25a2bc672d1148e28034f176e01fffebb08b35768468cc954630da77a1449005" + [[package]] name = "indexmap" version = "1.7.0" @@ -1529,6 +1544,12 @@ version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" +[[package]] +name = "unicode-segmentation" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b" + [[package]] name = "unicode-width" version = "0.1.9" @@ -1585,6 +1606,7 @@ dependencies = [ "pbr", "pico-args", "upkr", + "walrus", "wasm-encoder", "wasmparser 0.81.0", ] @@ -1618,6 +1640,32 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "walrus" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eb08e48cde54c05f363d984bb54ce374f49e242def9468d2e1b6c2372d291f8" +dependencies = [ + "anyhow", + "id-arena", + "leb128", + "log", + "walrus-macro", + "wasmparser 0.77.0", +] + +[[package]] +name = "walrus-macro" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a6e5bd22c71e77d60140b0bd5be56155a37e5bd14e24f5f87298040d0cc40d7" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "wasi" version = "0.10.2+wasi-snapshot-preview1" @@ -1687,6 +1735,12 @@ dependencies = [ "leb128", ] +[[package]] +name = "wasmparser" +version = "0.77.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b35c86d22e720a07d954ebbed772d01180501afe7d03d464f413bb5f8914a8d6" + [[package]] name = "wasmparser" version = "0.80.2" diff --git a/examples/rust/build.sh b/examples/rust/build.sh index 98f511e..0ecec33 100755 --- a/examples/rust/build.sh +++ b/examples/rust/build.sh @@ -1,2 +1,3 @@ rustc --target=wasm32-unknown-unknown --crate-type cdylib -C opt-level="z" -C "link-args=--import-memory --initial-memory=262144 -zstack-size=65536" -o tunnel.wasm tunnel.rs && \ +uw8 filter-exports tunnel.wasm tunnel.wasm && \ wasm-opt -Oz -o tunnel.wasm tunnel.wasm diff --git a/examples/rust/readme.txt b/examples/rust/readme.txt index b21a897..562782f 100644 --- a/examples/rust/readme.txt +++ b/examples/rust/readme.txt @@ -18,8 +18,10 @@ these globals and exports: They are meant to be used for heap allocations and stack for any values that are not simple scalars (i32, f32, etc.). Since our -code doesn't actually use any of that, we can just delete them -in a text editor and assemble the code again with wat2wasm. +code doesn't actually use any of that, the globals are only +referenced by the exports and we can remove them using +'uw8 filter-exports' (preferably before running wasm-opt) which +removes all exports except those used by the MicroW8 platform. This gives us a 216 byte wasm file. Running this through uw8 pack brings us to the final size of 119 bytes. \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 27775e1..df8c0c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,16 +19,18 @@ fn main() -> Result<()> { match args.subcommand()?.as_ref().map(|s| s.as_str()) { Some("run") => run(args), Some("pack") => pack(args), - Some(other) => { - eprintln!("Unknown command '{}'", other); - process::exit(1); - } - None => { + Some("filter-exports") => filter_exports(args), + Some("help") | None => { println!("Usage:"); println!(" uw8 run [-w/--watch] [-p/--pack] [-u/--uncompressed] [-l/--level] [-o/--output ] "); println!(" uw8 pack [-u/--uncompressed] [-l/--level] "); + println!(" uw8 filter-exports "); Ok(()) } + Some(other) => { + eprintln!("Unknown command '{}'", other); + process::exit(1); + } } } @@ -153,3 +155,12 @@ fn pack(mut args: Arguments) -> Result<()> { Ok(()) } + +fn filter_exports(mut args: Arguments) -> Result<()> { + let in_file = args.free_from_os_str::(|s| Ok(s.into()))?; + let out_file = args.free_from_os_str::(|s| Ok(s.into()))?; + + uw8_tool::filter_exports(&in_file, &out_file)?; + + Ok(()) +} diff --git a/uw8-tool/Cargo.lock b/uw8-tool/Cargo.lock index 7623afc..780032b 100644 --- a/uw8-tool/Cargo.lock +++ b/uw8-tool/Cargo.lock @@ -56,6 +56,21 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "heck" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "id-arena" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25a2bc672d1148e28034f176e01fffebb08b35768468cc954630da77a1449005" + [[package]] name = "lazy_static" version = "1.4.0" @@ -74,6 +89,15 @@ version = "0.2.112" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125" +[[package]] +name = "log" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +dependencies = [ + "cfg-if", +] + [[package]] name = "num-traits" version = "0.2.14" @@ -101,6 +125,24 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "db8bcd96cb740d03149cbad5518db9fd87126a10ab519c011893b1754134c468" +[[package]] +name = "proc-macro2" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47aa80447ce4daf1717500037052af176af5d38cc3e571d9ec1c7353fc10c87d" +dependencies = [ + "proc-macro2", +] + [[package]] name = "sacabase" version = "2.0.0" @@ -110,6 +152,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "syn" +version = "1.0.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecb2e6da8ee5eb9a61068762a32fa9619cc591ceb055b3687f4cd4051ec2e06b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + [[package]] name = "time" version = "0.1.44" @@ -121,6 +174,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "unicode-segmentation" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b" + +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" + [[package]] name = "upkr" version = "0.1.0" @@ -140,8 +205,35 @@ dependencies = [ "pbr", "pico-args", "upkr", + "walrus", "wasm-encoder", - "wasmparser", + "wasmparser 0.81.0", +] + +[[package]] +name = "walrus" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eb08e48cde54c05f363d984bb54ce374f49e242def9468d2e1b6c2372d291f8" +dependencies = [ + "anyhow", + "id-arena", + "leb128", + "log", + "walrus-macro", + "wasmparser 0.77.0", +] + +[[package]] +name = "walrus-macro" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a6e5bd22c71e77d60140b0bd5be56155a37e5bd14e24f5f87298040d0cc40d7" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -159,6 +251,12 @@ dependencies = [ "leb128", ] +[[package]] +name = "wasmparser" +version = "0.77.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b35c86d22e720a07d954ebbed772d01180501afe7d03d464f413bb5f8914a8d6" + [[package]] name = "wasmparser" version = "0.81.0" diff --git a/uw8-tool/Cargo.toml b/uw8-tool/Cargo.toml index e4168d9..ee75be9 100644 --- a/uw8-tool/Cargo.toml +++ b/uw8-tool/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] wasmparser = "0.81" wasm-encoder = "0.8" +walrus = "0.19" anyhow = "1" pico-args = "0.4" upkr = { git = "https://github.com/exoticorn/upkr.git", rev = "2e7983fc" } diff --git a/uw8-tool/src/lib.rs b/uw8-tool/src/lib.rs index a31db69..e3fd271 100644 --- a/uw8-tool/src/lib.rs +++ b/uw8-tool/src/lib.rs @@ -1,5 +1,7 @@ mod base_module; mod pack; +mod filter_exports; pub use base_module::BaseModule; pub use pack::{pack, pack_file, unpack, unpack_file, PackConfig}; +pub use filter_exports::filter_exports; diff --git a/uw8-tool/src/main.rs b/uw8-tool/src/main.rs index e63b60c..bff0dc0 100644 --- a/uw8-tool/src/main.rs +++ b/uw8-tool/src/main.rs @@ -27,6 +27,11 @@ fn main() -> Result<()> { let dest: PathBuf = args.free_from_str()?; uw8_tool::unpack_file(&source, &dest)?; } + "filter-exports" => { + let source: PathBuf = args.free_from_str()?; + let dest: PathBuf = args.free_from_str()?; + uw8_tool::filter_exports(&source, &dest)?; + } _ => { eprintln!("Unknown subcommand '{}'", cmd); print_help(); @@ -44,6 +49,7 @@ fn print_help() { "Usage: uw8-tool make-base uw8-tool pack - uw8-tool unpack " + uw8-tool unpack + uw8-tool filter-exports " ); }