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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
open! Stdlib
open Code
module W = Wasm_ast
open Code_generation
module Make (Target : Target_sig.S) = struct
open Target
let bind_parameters l =
List.fold_left
~f:(fun l x ->
let* _ = l in
let* _ = add_var x in
return ())
~init:(return ())
l
let call ?typ ~cps ~arity closure args =
let funct = Var.fresh () in
let* closure = tee ?typ funct closure in
let args = args @ [ closure ] in
let* ty, funct =
Memory.load_function_pointer
~cps
~arity
~skip_cast:(Option.is_some typ)
(load funct)
in
return (W.Call_ref (ty, funct, args))
let curry_app_name n m = Printf.sprintf "curry_app %d_%d" n m
let curry_app ~context ~arity m ~name =
let args =
List.init ~f:(fun i -> Code.Var.fresh_n (Printf.sprintf "x_%d" i)) ~len:m
in
let f = Code.Var.fresh_n "f" in
let body =
let* () = no_event in
let* () = bind_parameters args in
let* _ = add_var f in
let* args' = expression_list load args in
let* _f = load f in
let rec loop m args closure closure_typ =
if m = arity
then
let* e =
call
?typ:closure_typ
~cps:false
~arity
(load closure)
(List.append args args')
in
instr (W.Push e)
else
let* load_arg, load_closure, closure_typ =
Closure.curry_load ~cps:false ~arity m closure
in
let* x = load_arg in
let closure' = Code.Var.fresh_n "f" in
let* () = store ?typ:closure_typ closure' load_closure in
loop (m + 1) (x :: args) closure' closure_typ
in
loop m [] f None
in
let param_names = args @ [ f ] in
let locals, body = function_body ~context ~param_names ~body in
W.Function
{ name
; exported_name = None
; typ = None
; signature = Type.func_type 1
; param_names
; locals
; body
}
let curry_name n m = Printf.sprintf "curry_%d_%d" n m
let rec curry ~context ~arity m ~name =
assert (m > 1);
let name', functions =
if m = 2
then
let nm = Var.fresh_n (curry_app_name arity 1) in
let func = curry_app ~context ~arity 1 ~name:nm in
nm, [ func ]
else
let nm = Var.fresh_n (curry_name arity (m - 1)) in
let functions = curry ~context ~arity (m - 1) ~name:nm in
nm, functions
in
let x = Code.Var.fresh_n "x" in
let f = Code.Var.fresh_n "f" in
let body =
let* () = no_event in
let* _ = add_var x in
let* _ = add_var f in
push (Closure.curry_allocate ~cps:false ~arity m ~f:name' ~closure:f ~arg:x)
in
let param_names = [ x; f ] in
let locals, body = function_body ~context ~param_names ~body in
W.Function
{ name
; exported_name = None
; typ = None
; signature = Type.func_type 1
; param_names
; locals
; body
}
:: functions
let curry ~arity ~name = curry ~arity arity ~name
let cps_curry_app_name n m = Printf.sprintf "cps_curry_app %d_%d" n m
let cps_curry_app ~context ~arity m ~name =
let args =
List.init ~f:(fun i -> Code.Var.fresh_n (Printf.sprintf "x_%d" i)) ~len:(m + 1)
in
let f = Code.Var.fresh_n "f" in
let body =
let* () = no_event in
let* () = bind_parameters args in
let* _ = add_var f in
let* args' = expression_list load args in
let* _f = load f in
let rec loop m args closure closure_typ =
if m = arity
then
let* e =
call
?typ:closure_typ
~cps:true
~arity:(arity + 1)
(load closure)
(List.append args args')
in
instr (W.Push e)
else
let* load_arg, load_closure, closure_typ =
Closure.curry_load ~cps:true ~arity m closure
in
let* x = load_arg in
let closure' = Code.Var.fresh_n "f" in
let* () = store ?typ:closure_typ closure' load_closure in
loop (m + 1) (x :: args) closure' closure_typ
in
loop m [] f None
in
let param_names = args @ [ f ] in
let locals, body = function_body ~context ~param_names ~body in
W.Function
{ name
; exported_name = None
; typ = None
; signature = Type.func_type 2
; param_names
; locals
; body
}
let cps_curry_name n m = Printf.sprintf "cps_curry_%d_%d" n m
let rec cps_curry ~context ~arity m ~name =
assert (m > 1);
let name', functions =
if m = 2
then
let nm = Var.fresh_n (cps_curry_app_name arity 1) in
let func = cps_curry_app ~context ~arity 1 ~name:nm in
nm, [ func ]
else
let nm = Var.fresh_n (cps_curry_name arity (m - 1)) in
let functions = cps_curry ~context ~arity (m - 1) ~name:nm in
nm, functions
in
let x = Code.Var.fresh_n "x" in
let cont = Code.Var.fresh_n "cont" in
let f = Code.Var.fresh_n "f" in
let body =
let* () = no_event in
let* _ = add_var x in
let* _ = add_var cont in
let* _ = add_var f in
let* e = Closure.curry_allocate ~cps:true ~arity m ~f:name' ~closure:f ~arg:x in
let* c = call ~cps:false ~arity:1 (load cont) [ e ] in
instr (W.Return (Some c))
in
let param_names = [ x; cont; f ] in
let locals, body = function_body ~context ~param_names ~body in
W.Function
{ name
; exported_name = None
; typ = None
; signature = Type.func_type 2
; param_names
; locals
; body
}
:: functions
let cps_curry ~arity ~name = cps_curry ~arity arity ~name
let apply ~context ~arity ~name =
assert (arity > 1);
let l =
List.rev
(List.init ~len:arity ~f:(fun i -> Code.Var.fresh_n (Printf.sprintf "x%d" i)))
in
let f = Code.Var.fresh_n "f" in
let body =
let* () = no_event in
let* () = bind_parameters l in
let* _ = add_var f in
Memory.check_function_arity
f
~cps:false
~arity
(fun ~typ closure ->
let* l = expression_list load l in
call ?typ ~cps:false ~arity closure l)
(let rec build_applies y l =
match l with
| [] ->
let* y = y in
instr (Push y)
| x :: rem ->
let* x = load x in
build_applies (call ~cps:false ~arity:1 y [ x ]) rem
in
build_applies (load f) l)
in
let param_names = l @ [ f ] in
let locals, body = function_body ~context ~param_names ~body in
W.Function
{ name
; exported_name = None
; typ = None
; signature = Type.primitive_type (arity + 1)
; param_names
; locals
; body
}
let cps_apply ~context ~arity ~name =
assert (arity > 2);
let l =
List.rev
(List.init ~len:arity ~f:(fun i -> Code.Var.fresh_n (Printf.sprintf "x%d" i)))
in
let f = Code.Var.fresh_n "f" in
let body =
let* () = no_event in
let* () = bind_parameters l in
let* _ = add_var f in
Memory.check_function_arity
f
~cps:true
~arity:(arity - 1)
(fun ~typ closure ->
let* l = expression_list load l in
call ?typ ~cps:true ~arity closure l)
(let* args =
Memory.allocate
~tag:0
~deadcode_sentinal:(Code.Var.fresh ())
~load
(List.map ~f:(fun x -> `Var x) (List.tl l))
in
let* make_iterator =
register_import ~name:"caml_apply_continuation" (Fun (Type.primitive_type 1))
in
let iterate = Var.fresh_n "iterate" in
let* () = store iterate (return (W.Call (make_iterator, [ args ]))) in
let x = List.hd l in
let* x = load x in
let* iterate = load iterate in
push (call ~cps:true ~arity:2 (load f) [ x; iterate ]))
in
let param_names = l @ [ f ] in
let locals, body = function_body ~context ~param_names ~body in
W.Function
{ name
; exported_name = None
; typ = None
; signature = Type.primitive_type (arity + 1)
; param_names
; locals
; body
}
let dummy ~context ~cps ~arity ~name =
let arity = if cps then arity + 1 else arity in
let l =
List.rev
(List.init ~len:arity ~f:(fun i -> Code.Var.fresh_n (Printf.sprintf "x%d" i)))
in
let f = Code.Var.fresh_n "f" in
let body =
let* () = no_event in
let* () = bind_parameters l in
let* _ = add_var f in
let* typ, closure = Memory.load_real_closure ~cps ~arity (load f) in
let* l = expression_list load l in
let* e =
call
~typ:(W.Ref { nullable = false; typ = Type typ })
~cps
~arity
(return closure)
l
in
instr (W.Return (Some e))
in
let param_names = l @ [ f ] in
let locals, body = function_body ~context ~param_names ~body in
W.Function
{ name
; exported_name = None
; typ = None
; signature = Type.func_type arity
; param_names
; locals
; body
}
let f ~context =
IntMap.iter
(fun arity name ->
let f = apply ~context ~arity ~name in
context.other_fields <- f :: context.other_fields)
context.apply_funs;
IntMap.iter
(fun arity name ->
let f = cps_apply ~context ~arity ~name in
context.other_fields <- f :: context.other_fields)
context.cps_apply_funs;
IntMap.iter
(fun arity name ->
let l = curry ~context ~arity ~name in
context.other_fields <- List.rev_append l context.other_fields)
context.curry_funs;
IntMap.iter
(fun arity name ->
let l = cps_curry ~context ~arity ~name in
context.other_fields <- List.rev_append l context.other_fields)
context.cps_curry_funs;
IntMap.iter
(fun arity name ->
let f = dummy ~context ~cps:false ~arity ~name in
context.other_fields <- f :: context.other_fields)
context.dummy_funs;
IntMap.iter
(fun arity name ->
let f = dummy ~context ~cps:true ~arity ~name in
context.other_fields <- f :: context.other_fields)
context.cps_dummy_funs
end