-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1.wat
100 lines (81 loc) · 2.13 KB
/
1.wat
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
(module
(export "f11_branching" (func $f11_branching))
(export "f12_branching_pure" (func $f12_branching_pure))
(export "f13_fibonacci_loop" (func $f13_fibonacci_loop))
(export "f14_fibonacci_recursive" (func $f14_fibonacci_recursive))
(func $f11_branching (param $cond i32) (result i32)
(local $x i32)
;; https://webassembly.github.io/spec/core/text/instructions.html#folded-instructions
(if (i32.gt_s (local.get $cond) (i32.const 50))
(then
(local.set $x (i32.const 1))
)
(else
(local.set $x (i32.const 0))
)
)
local.get $x
)
(func $f12_branching_pure (param $cond i32) (result i32)
(local $x i32)
(i32.gt_s (local.get $cond) (i32.const 50))
if
(local.set $x (i32.const 1))
else
(local.set $x (i32.const 0))
end
local.get $x
)
(func $f13_fibonacci_loop (param $n i32) (result i32)
(local $prev i32)
(local $curr i32)
(if (i32.eqz (local.get $n))
(return (i32.const 0))
)
(local.set $n
(i32.sub (local.get $n) (i32.const 1))
)
(local.set $prev (i32.const 0))
(local.set $curr (i32.const 1))
;; block
;; while (false) { CODE ... CODE; }
;;
;; loop
;; while (true) { CODE ... CODE; break; }
;; https://webassembly.github.io/spec/core/exec/instructions.html#exec-block
(block (loop
;; 1 0
(br_if 1 (i32.eqz (local.get $n)))
local.get $curr
(local.set $curr
(i32.add (local.get $prev) (local.get $curr))
)
local.set $prev
(local.set $n
(i32.sub (local.get $n) (i32.const 1))
)
(br 0)
))
local.get $curr
)
(func $f14_fibonacci_recursive (param $n i32) (result i32)
(if (i32.eq (local.get $n) (i32.const 0))
(then
(return (i32.const 0))
)
)
(if (i32.eq (local.get $n) (i32.const 1))
(then
(return (i32.const 1))
)
)
(i32.add
(call $f14_fibonacci_recursive
(i32.sub (local.get $n) (i32.const 2))
)
(call $f14_fibonacci_recursive
(i32.sub (local.get $n) (i32.const 1))
)
)
)
)