-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExamples.hs
223 lines (188 loc) · 3.67 KB
/
Examples.hs
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
module Language.Wasm.Examples where
import Language.Wasm.Prelude
-- Print the numbers from 1 to 10
countTo10 :: WasmModule
countTo10 = wasm do
fn #main do
const @Int 0
let' #i do
loop #next do
local.get #i
const 1
add
local.tee #i
dup
print
const 10
lt
br_if #next
-- Small example illustrating functions and globals.
functions :: WasmModule
functions = wasm do
global #g 2
fn #add_to_g @'[Int] do
local.get #g
add
local.set #g
fn #add_to_g_twice @'[Int] do
dup
call #add_to_g
call #add_to_g
fn #print_g @'[] do
local.get #g
print
fn #main do
const 2
call #add_to_g_twice
call #print_g
-- Small example to illustrate recursive functions.
-- Function #f just prints its argument and keeps calling
-- itself with a smaller and smaller argument, until it reaches 0.
recursion :: WasmModule
recursion = wasm do
fn #f @'[Int] do
dup
const 0
if gt then do
dup
print
const 1
sub
call #f
else
drop
fn #main do
const 10
call #f
-- This programs traps with an out-of-bounds memory access.
outOfBounds :: WasmModule
outOfBounds = wasm do
fn #main do
const 10
const ()
let_mem #m do
const 100
mem.load #m
print
-- This program traps with a division by zero.
divByZero :: WasmModule
divByZero = wasm do
fn #main do
const @Int 1
const 0
div
print
-- Allocate a memory to store the first n fibonacci numbers, and print it.
fibonacci :: WasmModule
fibonacci = wasm do
global #n 10
fn #main do
local.get #n
const @Int 0
let_mem #fibs do
const 0
const 1
mem.store #fibs
const 1
const 2
mem.store #fibs
const 2
let' #i do
loop #l do
local.get #i
local.get #n
if lt then do
local.get #i
const 2
sub
mem.load #fibs
local.get #i
const 1
sub
mem.load #fibs
add
local.get #i
swap
mem.store #fibs
local.get #i
const 1
add
local.set #i
br #l
else do
mem.print #fibs
-- Calculate and print the factorial of n, using a recursive implementation.
factorial :: Int -> WasmModule
factorial n = wasm do
global #n n
fn #factorial @'[Int] do
dup
const 1
if gt then do
dup
const 1
sub
call #factorial
mul
else do
drop
const 1
fn #main do
local.get #n
call #factorial
print
mutualRecursion :: WasmModule
mutualRecursion = wasm do
fn #f @'[Int] do
dup
const 0
if eq then
drop
else do
dup
print
const 1
sub
call #g
fn #g @'[Int] do
dup
const 0
if eq then
drop
else do
dup
print
const 1
sub
call #f
fn #main do
local.get #x
call #g
global #x 7
-- Squares all the elements in the host-provided memory buffer.
squareAll :: IORef (Vector Int) -> WasmModule
squareAll r = wasm do
host_mem #m r do
fn #main do
mem.size #m
let' #n do
const 0
let' #i do
loop #l do
local.get #i
local.get #n
if lt then do
local.get #i
dup
mem.load #m
dup
mul
mem.store #m
local.get #i
const 1
add
local.set #i
br #l
else do
nop
mem.print #m