File: control-flow.wast

package info (click to toggle)
rust-wasmtime 26.0.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 48,492 kB
  • sloc: ansic: 4,003; sh: 561; javascript: 542; cpp: 254; asm: 175; ml: 96; makefile: 55
file content (116 lines) | stat: -rw-r--r-- 2,932 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
(module (func))

(module
  (func (export "if-without-result") (param i32) (param i32) (result i32)
    (if
      (i32.eq
        (local.get 0)
        (local.get 1)
      )
      (then (unreachable))
    )

    (local.get 0)
  )
)

(assert_return (invoke "if-without-result" (i32.const 2) (i32.const 3)) (i32.const 2))

(module
  (func (export "block") (param i32) (param i32) (result i32)
    (block (result i32)
      local.get 0
    )
  )
)

(assert_return (invoke "block" (i32.const 10) (i32.const 20)) (i32.const 10))

(module
  (func (export "br_block") (param i32) (param i32) (result i32)
    local.get 1
    (block (result i32)
      local.get 0
      local.get 0
      br 0
      unreachable
    )
    i32.add
  )
)

(assert_return (invoke "br_block" (i32.const 5) (i32.const 7)) (i32.const 12))

;; Tests discarding values on the value stack, while
;; carrying over the result using a conditional branch.
(module
  (func (export "brif_block") (param i32) (param i32) (result i32)
    local.get 1
    (block (result i32)
      local.get 0
      local.get 0
      br_if 0
      unreachable
    )
    i32.add
  )
)

(assert_return (invoke "brif_block" (i32.const 5) (i32.const 7)) (i32.const 12))

;; Tests that br_if keeps values in the case if the branch
;; hasn't been taken.
(module
  (func (export "brif_block_passthru") (param i32) (param i32) (result i32)
    (block (result i32)
      local.get 1
      local.get 0
      br_if 0
      local.get 1
      i32.add
    )
  )
)

(assert_return (invoke "brif_block_passthru" (i32.const 0) (i32.const 3)) (i32.const 6))

(module
  (func (export "i32.div_s") (param i32) (param i32) (result i32)
    (i32.div_s (local.get 0) (local.get 1))
  )
)

(module
  (func (export "br_table") (param $i i32) (result i32)
    (return
      (block $2 (result i32)
        (i32.add (i32.const 10)
          (block $1 (result i32)
            (i32.add (i32.const 100)
              (block $0 (result i32)
                (i32.add (i32.const 1000)
                  (block $default (result i32)
                    (br_table $0 $1 $2 $default
                      (i32.mul (i32.const 2) (local.get $i))
                      (i32.and (i32.const 3) (local.get $i))
                    )
                  )
                )
              )
            )
          )
        )
      )
    )
  )
)

(assert_return (invoke "br_table" (i32.const 0)) (i32.const 110))
(assert_return (invoke "br_table" (i32.const 1)) (i32.const 12))
(assert_return (invoke "br_table" (i32.const 2)) (i32.const 4))
(assert_return (invoke "br_table" (i32.const 3)) (i32.const 1116))
(assert_return (invoke "br_table" (i32.const 4)) (i32.const 118))
(assert_return (invoke "br_table" (i32.const 5)) (i32.const 20))
(assert_return (invoke "br_table" (i32.const 6)) (i32.const 12))
(assert_return (invoke "br_table" (i32.const 7)) (i32.const 1124))
(assert_return (invoke "br_table" (i32.const 8)) (i32.const 126))