diff --git a/src/backend.rs b/src/backend.rs index e1f4a6ca03..8c15536ff7 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -322,9 +322,7 @@ pub fn current_block_state(ctx: &Context) -> BlockState { ctx.block_state.clone() } -pub fn return_from_block(ctx: &mut Context, new_depth: StackDepth) { - let diff = ((ctx.block_state.depth.0 - new_depth.0) * WORD_SIZE) as i32; - +pub fn return_from_block(ctx: &mut Context) { if let Some(loc) = ctx.block_state.stack.last().unwrap().location(&ctx.locals) { match loc { ValueLocation::Reg(r) => { diff --git a/src/function_body.rs b/src/function_body.rs index 5e46e0dfba..d35bf3d05b 100644 --- a/src/function_body.rs +++ b/src/function_body.rs @@ -148,7 +148,7 @@ pub fn translate( .. }) => { if ty != Type::EmptyBlockType { - return_from_block(&mut ctx, block_state.depth); + return_from_block(&mut ctx); } // Finalize if..else block by jumping to the `end_label`. @@ -179,7 +179,7 @@ pub fn translate( let control_frame = control_frames.pop().expect("control stack is never empty"); if control_frame.ty != Type::EmptyBlockType && !control_frames.is_empty() { - return_from_block(&mut ctx, control_frame.block_state.depth); + return_from_block(&mut ctx); } if !control_frame.kind.is_loop() { diff --git a/src/tests.rs b/src/tests.rs index 09a18c38e9..822ebb507a 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -19,7 +19,7 @@ fn empty() { } macro_rules! binop_test { - ($op:ident, $func:path) => { + ($op:ident, $func:expr) => { quickcheck! { fn $op(a: u32, b: u32) -> bool { static CODE: &str = concat!( @@ -45,40 +45,27 @@ binop_test!(or, std::ops::BitOr::bitor); binop_test!(xor, std::ops::BitXor::bitxor); binop_test!(mul, u32::wrapping_mul); -#[test] -fn relop_eq() { - const CASES: &[(u32, u32, u32)] = &[ - (0, 0, 1), - (0, 1, 0), - (1, 0, 0), - (1, 1, 1), - (1312, 1, 0), - (1312, 1312, 1), - ]; +quickcheck! { + fn relop_eq(a: u32, b: u32) -> bool{ + static CODE: &str = r#" + (module + (func (param i32) (param i32) (result i32) (i32.eq (get_local 0) (get_local 1))) + ) + "#; - let code = r#" -(module - (func (param i32) (param i32) (result i32) (i32.eq (get_local 0) (get_local 1))) -) - "#; + lazy_static! { + static ref TRANSLATED: TranslatedModule = translate_wat(CODE); + } - for (a, b, expected) in CASES { - assert_eq!(execute_wat(code, *a, *b), *expected); + let out = unsafe { TRANSLATED.execute_func::<(u32, u32), u32>(0, (a, b)) }; + + (a == b) == (out == 1) } } -#[test] -fn if_then_else() { - const CASES: &[(u32, u32, u32)] = &[ - (0, 1, 1), - (0, 0, 0), - (1, 0, 0), - (1, 1, 1), - (1312, 1, 1), - (1312, 1312, 1312), - ]; - - let code = r#" +quickcheck! { + fn if_then_else(a: u32, b: u32) -> bool { + const CODE: &str = r#" (module (func (param i32) (param i32) (result i32) (if (result i32) @@ -91,13 +78,17 @@ fn if_then_else() { ) ) ) - "#; + "#; - for (a, b, expected) in CASES { - assert_eq!(execute_wat(code, *a, *b), *expected, "{}, {}", a, b); + lazy_static! { + static ref TRANSLATED: TranslatedModule = translate_wat(CODE); + } + + let out = unsafe { TRANSLATED.execute_func::<(u32, u32), u32>(0, (a, b)) }; + + out == (if a == b { a } else { b }) } } - #[test] fn if_without_result() { let code = r#"