;; Test `return` operator (module ;; Import our myprint function (import "myenv" "print" (func $print (param i64 i32))) ;; Define a single page memory of 64KB. (memory $0 1) ;; Store the Hello World (null terminated) string at byte offset 0 (data (i32.const 40) "Test Passed\n") (data (i32.const 56) "#Test Failed\n") ;; Debug function (func $printd (param $len i32) i64.const 0 (local.get $len) (call $print) ) (func $printSuccess i64.const 40 i32.const 12 (call $print) ) (func $printFail i64.const 56 i32.const 13 (call $print) ) (func $assert_test_i32 (param $expected i32) (param $result i32) local.get $expected local.get $result i32.eq (if (then (call $printSuccess) ) (else (call $printFail) ) ) ) (func $assert_test_i64 (param $expected i64) (param $result i64) local.get $expected local.get $result i64.eq (if (then (call $printSuccess) ) (else (call $printFail) ) ) ) (func $dummy) (func $singular (param i32) (result i32) (if (local.get 0) (then (nop))) (if (local.get 0) (then (nop)) (else (nop))) (if (result i32) (local.get 0) (then (i32.const 7)) (else (i32.const 8))) ) (func $nested (param i32 i32) (result i32) (if (result i32) (local.get 0) (then (if (local.get 1) (then (call $dummy) (block) (nop))) (if (local.get 1) (then) (else (call $dummy) (block) (nop))) (if (result i32) (local.get 1) (then (call $dummy) (i32.const 9)) (else (call $dummy) (i32.const 10)) ) ) (else (if (local.get 1) (then (call $dummy) (block) (nop))) (if (local.get 1) (then) (else (call $dummy) (block) (nop))) (if (result i32) (local.get 1) (then (call $dummy) (i32.const 10)) (else (call $dummy) (i32.const 11)) ) ) ) ) (func $as-select-first (param i32) (result i32) (select (if (result i32) (local.get 0) (then (call $dummy) (i32.const 1)) (else (call $dummy) (i32.const 0)) ) (i32.const 2) (i32.const 3) ) ) (func $as-select-mid (param i32) (result i32) (select (i32.const 2) (if (result i32) (local.get 0) (then (call $dummy) (i32.const 1)) (else (call $dummy) (i32.const 0)) ) (i32.const 3) ) ) (func $as-select-last (param i32) (result i32) (select (i32.const 2) (i32.const 3) (if (result i32) (local.get 0) (then (call $dummy) (i32.const 1)) (else (call $dummy) (i32.const 0)) ) ) ) (func $as-loop-first (param i32) (result i32) (loop (result i32) (if (result i32) (local.get 0) (then (call $dummy) (i32.const 1)) (else (call $dummy) (i32.const 0)) ) (call $dummy) (call $dummy) ) ) (func $as-loop-mid (param i32) (result i32) (loop (result i32) (call $dummy) (if (result i32) (local.get 0) (then (call $dummy) (i32.const 1)) (else (call $dummy) (i32.const 0)) ) (call $dummy) ) ) (func $as-loop-last (param i32) (result i32) (loop (result i32) (call $dummy) (call $dummy) (if (result i32) (local.get 0) (then (call $dummy) (i32.const 1)) (else (call $dummy) (i32.const 0)) ) ) ) (func $as-if-condition (param i32) (result i32) (if (result i32) (if (result i32) (local.get 0) (then (i32.const 1)) (else (i32.const 0)) ) (then (call $dummy) (i32.const 2)) (else (call $dummy) (i32.const 3)) ) ) (func $as-br_if-first (param i32) (result i32) (block (result i32) (br_if 0 (if (result i32) (local.get 0) (then (call $dummy) (i32.const 1)) (else (call $dummy) (i32.const 0)) ) (i32.const 2) ) (return (i32.const 3)) ) ) (func $as-br_if-last (param i32) (result i32) (block (result i32) (br_if 0 (i32.const 2) (if (result i32) (local.get 0) (then (call $dummy) (i32.const 1)) (else (call $dummy) (i32.const 0)) ) ) (return (i32.const 3)) ) ) (func $as-br_table-first (param i32) (result i32) (block (result i32) (if (result i32) (local.get 0) (then (call $dummy) (i32.const 1)) (else (call $dummy) (i32.const 0)) ) (i32.const 2) (br_table 0 0) ) ) (func $as-br_table-last (param i32) (result i32) (block (result i32) (i32.const 2) (if (result i32) (local.get 0) (then (call $dummy) (i32.const 1)) (else (call $dummy) (i32.const 0)) ) (br_table 0 0) ) ) (func $func (param i32 i32) (result i32) (local.get 0)) (type $check (func (param i32 i32) (result i32))) (table funcref (elem $func)) (func $as-call_indirect-first (param i32) (result i32) (block (result i32) (call_indirect (type $check) (if (result i32) (local.get 0) (then (call $dummy) (i32.const 1)) (else (call $dummy) (i32.const 0)) ) (i32.const 2) (i32.const 0) ) ) ) (func $as-call_indirect-mid (param i32) (result i32) (block (result i32) (call_indirect (type $check) (i32.const 2) (if (result i32) (local.get 0) (then (call $dummy) (i32.const 1)) (else (call $dummy) (i32.const 0)) ) (i32.const 0) ) ) ) (func $as-call_indirect-last (param i32) (result i32) (block (result i32) (call_indirect (type $check) (i32.const 2) (i32.const 0) (if (result i32) (local.get 0) (then (call $dummy) (i32.const 1)) (else (call $dummy) (i32.const 0)) ) ) ) ) (func $as-store-first (param i32) (if (result i32) (local.get 0) (then (call $dummy) (i32.const 1)) (else (call $dummy) (i32.const 0)) ) (i32.const 2) (i32.store) ) (func $as-store-last (param i32) (i32.const 2) (if (result i32) (local.get 0) (then (call $dummy) (i32.const 1)) (else (call $dummy) (i32.const 0)) ) (i32.store) ) (func $as-memory.grow-value (param i32) (result i32) (memory.grow (if (result i32) (local.get 0) (then (i32.const 1)) (else (i32.const 0)) ) ) ) (func $f (param i32) (result i32) (local.get 0)) (func $as-call-value (param i32) (result i32) (call $f (if (result i32) (local.get 0) (then (i32.const 1)) (else (i32.const 0)) ) ) ) (func $as-return-value (param i32) (result i32) (if (result i32) (local.get 0) (then (i32.const 1)) (else (i32.const 0))) (return) ) (func $as-drop-operand (param i32) (drop (if (result i32) (local.get 0) (then (i32.const 1)) (else (i32.const 0)) ) ) ) (func $as-br-value (param i32) (result i32) (block (result i32) (br 0 (if (result i32) (local.get 0) (then (i32.const 1)) (else (i32.const 0)) ) ) ) ) (func $as-local.set-value (param i32) (result i32) (local i32) (local.set 0 (if (result i32) (local.get 0) (then (i32.const 1)) (else (i32.const 0)) ) ) (local.get 0) ) (func $as-local.tee-value (param i32) (result i32) (local.tee 0 (if (result i32) (local.get 0) (then (i32.const 1)) (else (i32.const 0)) ) ) ) (global $a (mut i32) (i32.const 10)) (func $as-global.set-value (param i32) (result i32) (global.set $a (if (result i32) (local.get 0) (then (i32.const 1)) (else (i32.const 0)) ) ) (global.get $a) ) (func $as-load-operand (param i32) (result i32) (i32.load (if (result i32) (local.get 0) (then (i32.const 11)) (else (i32.const 10)) ) ) ) (func $as-unary-operand (param i32) (result i32) (i32.ctz (if (result i32) (local.get 0) (then (call $dummy) (i32.const 13)) (else (call $dummy) (i32.const -13)) ) ) ) (func $as-binary-operand (param i32 i32) (result i32) (i32.mul (if (result i32) (local.get 0) (then (call $dummy) (i32.const 3)) (else (call $dummy) (i32.const -3)) ) (if (result i32) (local.get 1) (then (call $dummy) (i32.const 4)) (else (call $dummy) (i32.const -5)) ) ) ) (func $as-test-operand (param i32) (result i32) (i32.eqz (if (result i32) (local.get 0) (then (call $dummy) (i32.const 13)) (else (call $dummy) (i32.const 0)) ) ) ) (func $break-bare (result i32) (if (i32.const 1) (then (br 0) (unreachable))) (if (i32.const 1) (then (br 0) (unreachable)) (else (unreachable))) (if (i32.const 0) (then (unreachable)) (else (br 0) (unreachable))) (if (i32.const 1) (then (br_if 0 (i32.const 1)) (unreachable))) (if (i32.const 1) (then (br_if 0 (i32.const 1)) (unreachable)) (else (unreachable))) (if (i32.const 0) (then (unreachable)) (else (br_if 0 (i32.const 1)) (unreachable))) (if (i32.const 1) (then (br_table 0 (i32.const 0)) (unreachable))) (if (i32.const 1) (then (br_table 0 (i32.const 0)) (unreachable)) (else (unreachable))) (if (i32.const 0) (then (unreachable)) (else (br_table 0 (i32.const 0)) (unreachable))) (i32.const 19) ) (func $break-value (param i32) (result i32) (if (result i32) (local.get 0) (then (br 0 (i32.const 18)) (i32.const 19)) (else (br 0 (i32.const 21)) (i32.const 20)) ) ) (func $effects (param i32) (result i32) (local i32) (if (block (result i32) (local.set 1 (i32.const 1)) (local.get 0)) (then (local.set 1 (i32.mul (local.get 1) (i32.const 3))) (local.set 1 (i32.sub (local.get 1) (i32.const 5))) (local.set 1 (i32.mul (local.get 1) (i32.const 7))) (br 0) (local.set 1 (i32.mul (local.get 1) (i32.const 100))) ) (else (local.set 1 (i32.mul (local.get 1) (i32.const 5))) (local.set 1 (i32.sub (local.get 1) (i32.const 7))) (local.set 1 (i32.mul (local.get 1) (i32.const 3))) (br 0) (local.set 1 (i32.mul (local.get 1) (i32.const 1000))) ) ) (local.get 1) ) (func (export "_start") (call $assert_test_i32 (call $singular (i32.const 0)) (i32.const 8)) (call $assert_test_i32 (call $singular (i32.const 1)) (i32.const 7)) (call $assert_test_i32 (call $singular (i32.const 10)) (i32.const 7)) (call $assert_test_i32 (call $singular (i32.const -10)) (i32.const 7)) (call $assert_test_i32 (call $nested (i32.const 0) (i32.const 0)) (i32.const 11)) (call $assert_test_i32 (call $nested (i32.const 1) (i32.const 0)) (i32.const 10)) (call $assert_test_i32 (call $nested (i32.const 0) (i32.const 1)) (i32.const 10)) (call $assert_test_i32 (call $nested (i32.const 3) (i32.const 2)) (i32.const 9)) (call $assert_test_i32 (call $nested (i32.const 0) (i32.const -100)) (i32.const 10)) (call $assert_test_i32 (call $nested (i32.const 10) (i32.const 10)) (i32.const 9)) (call $assert_test_i32 (call $nested (i32.const 0) (i32.const -1)) (i32.const 10)) (call $assert_test_i32 (call $nested (i32.const -111) (i32.const -2)) (i32.const 9)) (call $assert_test_i32 (call $as-select-first (i32.const 0)) (i32.const 0)) (call $assert_test_i32 (call $as-select-first (i32.const 1)) (i32.const 1)) (call $assert_test_i32 (call $as-select-mid (i32.const 0)) (i32.const 2)) (call $assert_test_i32 (call $as-select-mid (i32.const 1)) (i32.const 2)) (call $assert_test_i32 (call $as-select-last (i32.const 0)) (i32.const 3)) (call $assert_test_i32 (call $as-select-last (i32.const 1)) (i32.const 2)) (call $assert_test_i32 (call $as-loop-first (i32.const 0)) (i32.const 0)) (call $assert_test_i32 (call $as-loop-first (i32.const 1)) (i32.const 1)) (call $assert_test_i32 (call $as-loop-mid (i32.const 0)) (i32.const 0)) (call $assert_test_i32 (call $as-loop-mid (i32.const 1)) (i32.const 1)) (call $assert_test_i32 (call $as-loop-last (i32.const 0)) (i32.const 0)) (call $assert_test_i32 (call $as-loop-last (i32.const 1)) (i32.const 1)) (call $assert_test_i32 (call $as-if-condition (i32.const 0)) (i32.const 3)) (call $assert_test_i32 (call $as-if-condition (i32.const 1)) (i32.const 2)) (call $assert_test_i32 (call $as-br_if-first (i32.const 0)) (i32.const 0)) (call $assert_test_i32 (call $as-br_if-first (i32.const 1)) (i32.const 1)) (call $assert_test_i32 (call $as-br_if-last (i32.const 0)) (i32.const 3)) (call $assert_test_i32 (call $as-br_if-last (i32.const 1)) (i32.const 2)) (call $assert_test_i32 (call $as-br_table-first (i32.const 0)) (i32.const 0)) (call $assert_test_i32 (call $as-br_table-first (i32.const 1)) (i32.const 1)) (call $assert_test_i32 (call $as-br_table-last (i32.const 0)) (i32.const 2)) (call $assert_test_i32 (call $as-br_table-last (i32.const 1)) (i32.const 2)) (call $assert_test_i32 (call $as-call_indirect-first (i32.const 0)) (i32.const 0)) (call $assert_test_i32 (call $as-call_indirect-first (i32.const 1)) (i32.const 1)) (call $assert_test_i32 (call $as-call_indirect-mid (i32.const 0)) (i32.const 2)) (call $assert_test_i32 (call $as-call_indirect-mid (i32.const 1)) (i32.const 2)) (call $assert_test_i32 (call $as-call_indirect-last (i32.const 0)) (i32.const 2)) (call $assert_test_i32 (call $as-memory.grow-value (i32.const 0)) (i32.const 1)) (call $assert_test_i32 (call $as-memory.grow-value (i32.const 1)) (i32.const 1)) (call $assert_test_i32 (call $as-call-value (i32.const 0)) (i32.const 0)) (call $assert_test_i32 (call $as-call-value (i32.const 1)) (i32.const 1)) (call $assert_test_i32 (call $as-return-value (i32.const 0)) (i32.const 0)) (call $assert_test_i32 (call $as-return-value (i32.const 1)) (i32.const 1)) (call $assert_test_i32 (call $as-br-value (i32.const 0)) (i32.const 0)) (call $assert_test_i32 (call $as-br-value (i32.const 1)) (i32.const 1)) (call $assert_test_i32 (call $as-local.set-value (i32.const 0)) (i32.const 0)) (call $assert_test_i32 (call $as-local.set-value (i32.const 1)) (i32.const 1)) (call $assert_test_i32 (call $as-local.tee-value (i32.const 0)) (i32.const 0)) (call $assert_test_i32 (call $as-local.tee-value (i32.const 1)) (i32.const 1)) (call $assert_test_i32 (call $as-global.set-value (i32.const 0)) (i32.const 0)) (call $assert_test_i32 (call $as-global.set-value (i32.const 1)) (i32.const 1)) (call $assert_test_i32 (call $as-load-operand (i32.const 0)) (i32.const 0)) (call $assert_test_i32 (call $as-load-operand (i32.const 1)) (i32.const 0)) (call $assert_test_i32 (call $as-unary-operand (i32.const 0)) (i32.const 0)) (call $assert_test_i32 (call $as-unary-operand (i32.const 1)) (i32.const 0)) (call $assert_test_i32 (call $as-unary-operand (i32.const -1)) (i32.const 0)) (call $assert_test_i32 (call $as-binary-operand (i32.const 0) (i32.const 0)) (i32.const 15)) (call $assert_test_i32 (call $as-binary-operand (i32.const 0) (i32.const 1)) (i32.const -12)) (call $assert_test_i32 (call $as-binary-operand (i32.const 1) (i32.const 0)) (i32.const -15)) (call $assert_test_i32 (call $as-binary-operand (i32.const 1) (i32.const 1)) (i32.const 12)) (call $assert_test_i32 (call $as-test-operand (i32.const 0)) (i32.const 1)) (call $assert_test_i32 (call $as-test-operand (i32.const 1)) (i32.const 0)) (call $assert_test_i32 (call $break-bare) (i32.const 19)) (call $assert_test_i32 (call $break-value (i32.const 1)) (i32.const 18)) (call $assert_test_i32 (call $break-value (i32.const 0)) (i32.const 21)) (call $assert_test_i32 (call $effects (i32.const 1)) (i32.const -14)) (call $assert_test_i32 (call $effects (i32.const 0)) (i32.const -6)) (call $as-store-first (i32.const 0)) (call $as-store-first (i32.const 1)) (call $as-store-last (i32.const 0)) (call $as-store-last (i32.const 1)) (call $as-drop-operand (i32.const 0)) (call $as-drop-operand (i32.const 1)) ) )