v / examples / wasm_codegen / control_flow.v
43 lines · 39 sloc · 760 bytes · 3a06b55388559598c05e46ddb776d7443180b56b
Raw
1import wasm
2
3fn main() {
4 mut m := wasm.Module{}
5 mut bif := m.new_function('block_if', [.i32_t], [.i32_t])
6 {
7 loc := bif.new_local(.i32_t)
8
9 // loc = i32.const 10
10 bif.i32_const(10)
11 bif.local_set(loc)
12
13 blk := bif.c_block([], [])
14 {
15 // if argument[0], break
16 bif.local_get(0)
17 bif.c_br_if(blk)
18 // loc = i32.const 11
19 bif.i32_const(11)
20 bif.local_set(loc)
21 }
22 bif.c_end(blk)
23
24 // return loc
25 bif.local_get(loc)
26 }
27 m.commit(bif, true)
28 mut ifexpr := m.new_function('if_expr', [.i32_t], [.i64_t])
29 {
30 ifexpr.local_get(0)
31 if_blk := ifexpr.c_if([], [.i64_t])
32 {
33 ifexpr.i64_const(5000)
34 }
35 ifexpr.c_else(if_blk)
36 {
37 ifexpr.i64_const(-5000)
38 }
39 ifexpr.c_end(if_blk)
40 }
41 m.commit(ifexpr, true)
42 print(m.compile().bytestr())
43}
44