From 7b4011aa609ee69915c6d7573954bc88032654d4 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Tue, 9 May 2017 18:10:24 +0300 Subject: [PATCH] global opt 2 --- src/optimizer.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/optimizer.rs b/src/optimizer.rs index ef49ca9..728c2db 100644 --- a/src/optimizer.rs +++ b/src/optimizer.rs @@ -452,4 +452,47 @@ mod tests { "There should 1 (one) global entry in the optimized module, since _call function uses it" ); } + + /// @spec 2 + /// Imagine there is one exported function in unoptimized module, `_call`, that we specify as the one + /// to stay during the optimization. The code of this function uses one global during the execution, + /// but we have a bunch of other unused globals in the code. Last globals should not survive the optimization, + /// while the former should. + #[test] + fn globals_2() { + let mut module = builder::module() + .global() + .value_type().i32() + .build() + .global() + .value_type().i64() + .build() + .global() + .value_type().f32() + .build() + .function() + .signature().param().i32().build() + .body() + .with_opcodes(elements::Opcodes::new( + vec![ + elements::Opcode::GetGlobal(1), + elements::Opcode::End + ] + )) + .build() + .build() + .export() + .field("_call") + .internal().func(0).build() + .build(); + + optimize(&mut module, vec!["_call"]).expect("optimizer to succeed"); + + assert_eq!( + 1, + module.global_section().expect("global section to be generated").entries().len(), + "There should 1 (one) global entry in the optimized module, since _call function uses only one" + ); + } + } \ No newline at end of file