Skip to content

Commit b4d0cd0

Browse files
committed
Add explanation of control flow instructions
1 parent 66e6d7b commit b4d0cd0

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,70 @@ There are two ways to express `if`...`else` statement
124124
)
125125
```
126126

127+
### Blocks of code
128+
129+
- `block`
130+
- `loop`
131+
132+
`br` is control flow instruction
133+
134+
```WebAssembly
135+
(block $B0
136+
;; statement(s)
137+
(br $B0) (; ━┓ ;)
138+
;; statement(s) (; ┃ ;)
139+
) (; ┃ ;)
140+
(; <━┛ ;)
141+
```
142+
143+
```WebAssembly
144+
(loop $L0 (; <━┓ ;)
145+
;; statement(s) (; ┃ ;)
146+
(br $L0) (; ━┛ ;)
147+
;; statement(s)
148+
)
149+
```
150+
151+
`br_if` is `br` with a condition
152+
153+
```cpp
154+
do {
155+
// statement(s)
156+
} while (condition);
157+
```
158+
159+
```WebAssembly
160+
(loop $L0
161+
;; statement(s)
162+
(br_if $L0
163+
(get_local $condition))
164+
)
165+
```
166+
167+
Example for loop
168+
169+
```cpp
170+
for (int i = 0; i < 10; i++) {
171+
// statement(s)
172+
}
173+
```
174+
175+
```WebAssembly
176+
(local $i i32)
177+
(set_local $i
178+
(i32.const 0))
179+
(loop $L0
180+
;; statement(s)
181+
(br_if $L0
182+
(i32.ne
183+
(i32.const 10)
184+
(tee_local $i
185+
(i32.add
186+
(get_local $i)
187+
(i32.const 1)))))
188+
)
189+
```
190+
127191
### Operators
128192
| | i32 | i64 | f32 | f64 |
129193
|:-:| --- | --- | --- | --- |

0 commit comments

Comments
 (0)