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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
(* SPDX-License-Identifier: AGPL-3.0-or-later *)
(* Copyright © 2021-2024 OCamlPro *)
(* Written by the Owi programmers *)
let convert_indice : Binary.indice -> Text.indice = function i -> Text.Raw i
let convert_heap_type : Binary.heap_type -> Text.heap_type = function
| TypeUse id -> TypeUse (Raw id)
| Any_ht -> Any_ht
| None_ht -> None_ht
| Func_ht -> Func_ht
| NoFunc_ht -> NoFunc_ht
| Exn_ht -> Exn_ht
| NoExn_ht -> NoExn_ht
| Extern_ht -> Extern_ht
| NoExtern_ht -> NoExtern_ht
let convert_ref_type ((n, ht) : Binary.ref_type) : Text.ref_type =
(n, convert_heap_type ht)
let convert_val_type : Binary.val_type -> Text.val_type = function
| Num_type nt -> Num_type nt
| Ref_type rt -> Ref_type (convert_ref_type rt)
let convert_func_type ((pt, rt) : Binary.func_type) : Text.func_type =
( List.map (fun (id, vt) -> (id, convert_val_type vt)) pt
, List.map convert_val_type rt )
let convert_block_type : Binary.block_type -> Text.block_type = function
| Binary.Bt_raw (opt, ft) ->
let opt = Option.map convert_indice opt in
Text.Bt_raw (opt, convert_func_type ft)
let convert_memarg ({ offset; align } : Binary.memarg) : Text.memarg =
let offset =
if Int64.lt 0L offset then Some (Int64.to_string_u offset) else None
in
let align =
assert (Int32.le 0l align);
Some (Int32.to_string_u (Int32.shl 1l align))
in
{ offset; align }
let convert_i32_instr : Binary.i32_instr -> Text.i32_instr = function
| (Binary.Const i : Binary.i32_instr) -> Const i
| Clz -> Clz
| Ctz -> Ctz
| Popcnt -> Popcnt
| Add -> Add
| Sub -> Sub
| Mul -> Mul
| Div sx -> Div sx
| Rem sx -> Rem sx
| And -> And
| Or -> Or
| Xor -> Xor
| Shl -> Shl
| Shr sx -> Shr sx
| Rotl -> Rotl
| Rotr -> Rotr
| Eqz -> Eqz
| Eq -> Eq
| Ne -> Ne
| Lt sx -> Lt sx
| Gt sx -> Gt sx
| Le sx -> Le sx
| Ge sx -> Ge sx
| Extend8_s -> Extend8_s
| Extend16_s -> Extend16_s
| Wrap_i64 -> Wrap_i64
| Trunc_f (nn, sx) -> Trunc_f (nn, sx)
| Trunc_sat_f (nn, sx) -> Trunc_sat_f (nn, sx)
| Reinterpret_f nn -> Reinterpret_f nn
| Load (indice, memarg) ->
let indice = convert_indice indice in
let memarg = convert_memarg memarg in
Load (indice, memarg)
| Load8 (indice, sx, memarg) ->
let indice = convert_indice indice in
let memarg = convert_memarg memarg in
Load8 (indice, sx, memarg)
| Load16 (indice, sx, memarg) ->
let indice = convert_indice indice in
let memarg = convert_memarg memarg in
Load16 (indice, sx, memarg)
| Store (indice, memarg) ->
let indice = convert_indice indice in
let memarg = convert_memarg memarg in
Store (indice, memarg)
| Store8 (indice, memarg) ->
let indice = convert_indice indice in
let memarg = convert_memarg memarg in
Store8 (indice, memarg)
| Store16 (indice, memarg) ->
let indice = convert_indice indice in
let memarg = convert_memarg memarg in
Store16 (indice, memarg)
let convert_i64_instr : Binary.i64_instr -> Text.i64_instr = function
| Const i -> Const i
| Clz -> Clz
| Ctz -> Ctz
| Popcnt -> Popcnt
| Add -> Add
| Sub -> Sub
| Mul -> Mul
| Div sx -> Div sx
| Rem sx -> Rem sx
| And -> And
| Or -> Or
| Xor -> Xor
| Shl -> Shl
| Shr sx -> Shr sx
| Rotl -> Rotl
| Rotr -> Rotr
| Eqz -> Eqz
| Eq -> Eq
| Ne -> Ne
| Lt sx -> Lt sx
| Gt sx -> Gt sx
| Le sx -> Le sx
| Ge sx -> Ge sx
| Extend8_s -> Extend8_s
| Extend16_s -> Extend16_s
| Extend32_s -> Extend32_s
| Extend_i32 sx -> Extend_i32 sx
| Trunc_f (nn, sx) -> Trunc_f (nn, sx)
| Trunc_sat_f (nn, sx) -> Trunc_sat_f (nn, sx)
| Reinterpret_f nn -> Reinterpret_f nn
| Load (indice, memarg) ->
let indice = convert_indice indice in
let memarg = convert_memarg memarg in
Load (indice, memarg)
| Load8 (indice, sx, memarg) ->
let indice = convert_indice indice in
let memarg = convert_memarg memarg in
Load8 (indice, sx, memarg)
| Load16 (indice, sx, memarg) ->
let indice = convert_indice indice in
let memarg = convert_memarg memarg in
Load16 (indice, sx, memarg)
| Load32 (indice, sx, memarg) ->
let indice = convert_indice indice in
let memarg = convert_memarg memarg in
Load32 (indice, sx, memarg)
| Store (indice, memarg) ->
let indice = convert_indice indice in
let memarg = convert_memarg memarg in
Store (indice, memarg)
| Store8 (indice, memarg) ->
let indice = convert_indice indice in
let memarg = convert_memarg memarg in
Store8 (indice, memarg)
| Store16 (indice, memarg) ->
let indice = convert_indice indice in
let memarg = convert_memarg memarg in
Store16 (indice, memarg)
| Store32 (indice, memarg) ->
let indice = convert_indice indice in
let memarg = convert_memarg memarg in
Store32 (indice, memarg)
let convert_f32_instr : Binary.f32_instr -> Text.f32_instr = function
| (Const f : Binary.f32_instr) -> Const f
| Abs -> Abs
| Neg -> Neg
| Sqrt -> Sqrt
| Ceil -> Ceil
| Floor -> Floor
| Trunc -> Trunc
| Nearest -> Nearest
| Add -> Add
| Sub -> Sub
| Mul -> Mul
| Div -> Div
| Min -> Min
| Max -> Max
| Copysign -> Copysign
| Eq -> Eq
| Ne -> Ne
| Lt -> Lt
| Gt -> Gt
| Le -> Le
| Ge -> Ge
| Demote_f64 -> Demote_f64
| Convert_i (nn, sx) -> Convert_i (nn, sx)
| Reinterpret_i nn -> Reinterpret_i nn
| Load (indice, memarg) ->
let indice = convert_indice indice in
let memarg = convert_memarg memarg in
Load (indice, memarg)
| Store (indice, memarg) ->
let indice = convert_indice indice in
let memarg = convert_memarg memarg in
Store (indice, memarg)
let convert_f64_instr : Binary.f64_instr -> Text.f64_instr = function
| (Const f : Binary.f64_instr) -> Const f
| Abs -> Abs
| Neg -> Neg
| Sqrt -> Sqrt
| Ceil -> Ceil
| Floor -> Floor
| Trunc -> Trunc
| Nearest -> Nearest
| Add -> Add
| Sub -> Sub
| Mul -> Mul
| Div -> Div
| Min -> Min
| Max -> Max
| Copysign -> Copysign
| Eq -> Eq
| Ne -> Ne
| Lt -> Lt
| Gt -> Gt
| Le -> Le
| Ge -> Ge
| Promote_f32 -> Promote_f32
| Convert_i (nn, sx) -> Convert_i (nn, sx)
| Reinterpret_i nn -> Reinterpret_i nn
| Load (indice, memarg) ->
let indice = convert_indice indice in
let memarg = convert_memarg memarg in
Load (indice, memarg)
| Store (indice, memarg) ->
let indice = convert_indice indice in
let memarg = convert_memarg memarg in
Store (indice, memarg)
let convert_ref_instr : Binary.ref_instr -> Text.ref_instr = function
| Null heap_type -> Null (convert_heap_type heap_type)
| Is_null -> Is_null
| As_non_null -> As_non_null
| Func indice ->
let indice = convert_indice indice in
Func indice
let convert_local_instr : Binary.local_instr -> Text.local_instr = function
| Get indice ->
let indice = convert_indice indice in
Get indice
| Set indice ->
let indice = convert_indice indice in
Set indice
| Tee indice ->
let indice = convert_indice indice in
Tee indice
let convert_global_instr : Binary.global_instr -> Text.global_instr = function
| Get indice ->
let indice = convert_indice indice in
Get indice
| Set indice ->
let indice = convert_indice indice in
Set indice
let convert_table_instr : Binary.table_instr -> Text.table_instr = function
| Get indice ->
let indice = convert_indice indice in
Get indice
| Set indice ->
let indice = convert_indice indice in
Set indice
| Size indice ->
let indice = convert_indice indice in
Size indice
| Grow indice ->
let indice = convert_indice indice in
Grow indice
| Fill indice ->
let indice = convert_indice indice in
Fill indice
| Copy (indice1, indice2) ->
let indice1 = convert_indice indice1 in
let indice2 = convert_indice indice2 in
Copy (indice1, indice2)
| Init (indice1, indice2) ->
let indice1 = convert_indice indice1 in
let indice2 = convert_indice indice2 in
Init (indice1, indice2)
let convert_elem_instr : Binary.elem_instr -> Text.elem_instr = function
| Drop indice ->
let indice = convert_indice indice in
Drop indice
let convert_memory_instr : Binary.memory_instr -> Text.memory_instr = function
| Size indice ->
let indice = convert_indice indice in
Size indice
| Grow indice ->
let indice = convert_indice indice in
Grow indice
| Fill indice ->
let indice = convert_indice indice in
Fill indice
| Copy (indice1, indice2) ->
let indice1 = convert_indice indice1 in
let indice2 = convert_indice indice2 in
Copy (indice1, indice2)
| Init (indice1, indice2) ->
let indice1 = convert_indice indice1 in
let indice2 = convert_indice indice2 in
Init (indice1, indice2)
let convert_data_instr : Binary.data_instr -> Text.data_instr = function
| Drop indice ->
let indice = convert_indice indice in
Drop indice
let rec convert_instr : Binary.instr -> Text.instr = function
| Binary.I32 i -> Text.I32 (convert_i32_instr i)
| I64 i -> Text.I64 (convert_i64_instr i)
| F32 i -> Text.F32 (convert_f32_instr i)
| F64 i -> Text.F64 (convert_f64_instr i)
| V128 i -> Text.V128 i
| I8x16 i -> Text.I8x16 i
| I16x8 i -> Text.I16x8 i
| I32x4 i -> Text.I32x4 i
| I64x2 i -> Text.I64x2 i
| Ref i -> Ref (convert_ref_instr i)
| Local i -> Local (convert_local_instr i)
| Global i -> Global (convert_global_instr i)
| Table i -> Table (convert_table_instr i)
| Elem i -> Elem (convert_elem_instr i)
| Memory i -> Memory (convert_memory_instr i)
| Data i -> Data (convert_data_instr i)
| Br_table (ids, id) ->
let ids = Array.map convert_indice ids in
let id = convert_indice id in
Br_table (ids, id)
| Br_if id ->
let id = convert_indice id in
Br_if id
| Br id ->
let id = convert_indice id in
Br id
| Br_on_null id ->
let id = convert_indice id in
Br_on_null id
| Br_on_non_null id ->
let id = convert_indice id in
Br_on_non_null id
| Call id ->
let id = convert_indice id in
Call id
| Return_call id ->
let id = convert_indice id in
Return_call id
| If_else (id, bt, e1, e2) ->
let bt = Option.map convert_block_type bt in
let e1 = convert_expr e1 in
let e2 = convert_expr e2 in
If_else (id, bt, e1, e2)
| Loop (id, bt, e) ->
let bt = Option.map convert_block_type bt in
let e = convert_expr e in
Loop (id, bt, e)
| Block (id, bt, e) ->
let bt = Option.map convert_block_type bt in
let e = convert_expr e in
Block (id, bt, e)
| Call_indirect (tbl_i, bt) ->
let tbl_i = convert_indice tbl_i in
let bt = convert_block_type bt in
Call_indirect (tbl_i, bt)
| Return_call_indirect (tbl_i, bt) ->
let tbl_i = convert_indice tbl_i in
let bt = convert_block_type bt in
Return_call_indirect (tbl_i, bt)
| Call_ref t ->
let t = convert_indice t in
Call_ref t
| Return_call_ref bt ->
let bt = convert_block_type bt in
Return_call_ref bt
| Select typ ->
begin match typ with
| None -> Select None
| Some [ t ] -> Select (Some [ convert_val_type t ])
| Some [] | Some (_ :: _ :: _) ->
(* invalid result arity *)
(* TODO: maybe we could change the type of Binary.Select to prevent this from happening? *)
assert false
end
| Unreachable -> Unreachable
| Drop -> Drop
| Nop -> Nop
| Return -> Return
and convert_expr (e : Binary.expr Annotated.t) : Text.expr =
List.map (fun i -> convert_instr i.Annotated.raw) e.raw
let convert_elem_mode : Binary.Elem.Mode.t -> Text.Elem.Mode.t = function
| Binary.Elem.Mode.Passive -> Text.Elem.Mode.Passive
| Declarative -> Declarative
| Active (opt, e) ->
let opt = Option.map (fun i -> Text.Raw i) opt in
let e = convert_expr e in
Active (opt, e)
let convert_elem : Binary.Elem.t -> Text.Elem.t = function
| { Binary.Elem.id; typ; init; mode; explicit_typ } ->
let init = List.map convert_expr init in
let mode = convert_elem_mode mode in
{ Text.Elem.id; typ = convert_ref_type typ; init; mode; explicit_typ }
let convert_data_mode : Binary.Data.Mode.t -> Text.Data.Mode.t = function
| Binary.Data.Mode.Passive -> Text.Data.Mode.Passive
| Active (i, e) ->
let e = convert_expr e in
Active (Some (Raw i), e)
let convert_data : Binary.Data.t -> Text.Data.t = function
| { Binary.Data.id; init; mode } ->
let mode = convert_data_mode mode in
{ Text.Data.id; init; mode }
let from_types types : Text.Module.Field.t list =
Array.map
(fun ((id, ft) : Binary.Typedef.t) ->
Text.Module.Field.Typedef (id, convert_func_type ft) )
types
|> Array.to_list
let from_global (global : (Binary.Global.t, Binary.Global.Type.t) Origin.t array)
: Text.Module.Field.t list =
Array.map
(function
| Origin.Local ({ typ = mut, vt; init; id } : Binary.Global.t) ->
let init = convert_expr init in
let typ = (mut, convert_val_type vt) in
Text.Module.Field.Global { typ; init; id }
| Imported { modul_name; name; assigned_name; typ = mut, vt } ->
let typ =
Text.Import.Type.Global (assigned_name, (mut, convert_val_type vt))
in
Text.Module.Field.Import { modul_name; name; typ } )
global
|> Array.to_list
let convert_table_limits : Binary.Table.Type.limits -> Text.limits = function
| I64 { min; max } ->
{ is_i64 = true
; min = Int64.to_string_u min
; max = Option.map Int64.to_string_u max
}
| I32 { min; max } ->
{ is_i64 = false
; min = Int32.to_string_u min
; max = Option.map Int32.to_string_u max
}
let from_table table : Text.Module.Field.t list =
Array.map
(function
| Origin.Local Binary.Table.{ id; typ = limits, rt; init } ->
let init = Option.map convert_expr init in
Text.Module.Field.Table
{ id; typ = (convert_table_limits limits, convert_ref_type rt); init }
| Imported { modul_name; name; assigned_name; typ = limits, rt } ->
let typ =
Text.Import.Type.Table
(assigned_name, (convert_table_limits limits, convert_ref_type rt))
in
Import { modul_name; name; typ } )
table
|> Array.to_list
let convert_mem_limits : Binary.Mem.Type.limits -> Text.limits = function
| I64 { min; max } ->
{ is_i64 = true
; min = Int.to_string min
; max = Option.map Int.to_string max
}
| I32 { min; max } ->
{ is_i64 = false
; min = Int32.to_string_u min
; max = Option.map Int32.to_string_u max
}
let from_mem mem : Text.Module.Field.t list =
Array.map
(function
| Origin.Local (name, t) ->
Text.Module.Field.Mem (name, convert_mem_limits t)
| Imported { modul_name; name; assigned_name; typ } ->
let typ =
Text.Import.Type.Mem (assigned_name, convert_mem_limits typ)
in
Import { modul_name; name; typ } )
mem
|> Array.to_list
let from_func func : Text.Module.Field.t list =
Array.map
(function
| Origin.Local (func : Binary.Func.t) ->
let type_f = convert_block_type func.type_f in
let body = convert_expr func.body in
let id = func.id in
let locals =
List.map (fun (id, vt) -> (id, convert_val_type vt)) func.locals
in
Text.Module.Field.Func { type_f; locals; body; id }
| Imported { modul_name; name; assigned_name; typ } ->
let typ = convert_block_type typ in
let typ = Text.Import.Type.Func (assigned_name, typ) in
Text.Module.Field.Import { modul_name; name; typ } )
func
|> Array.to_list
let from_elem elem : Text.Module.Field.t list =
Array.map
(fun (elem : Binary.Elem.t) ->
let elem = convert_elem elem in
Text.Module.Field.Elem elem )
elem
|> Array.to_list
let from_data data : Text.Module.Field.t list =
Array.map
(fun (data : Binary.Data.t) ->
let data = convert_data data in
Text.Module.Field.Data data )
data
|> Array.to_list
let from_exports (exports : Binary.Module.Exports.t) : Text.Module.Field.t list
=
let global =
Array.map
(fun ({ name; id } : Binary.Export.t) ->
let id = Some (Text.Raw id) in
Text.Module.Field.Export { name; typ = Global id } )
exports.global
in
let mem =
Array.map
(fun ({ name; id } : Binary.Export.t) ->
let id = Some (Text.Raw id) in
Text.Module.Field.Export { name; typ = Mem id } )
exports.mem
in
let table =
Array.map
(fun ({ name; id } : Binary.Export.t) ->
let id = Some (Text.Raw id) in
Text.Module.Field.Export { name; typ = Table id } )
exports.table
in
let func =
Array.map
(fun ({ name; id } : Binary.Export.t) ->
let id = Some (Text.Raw id) in
Text.Module.Field.Export { name; typ = Func id } )
exports.func
in
Array.to_list global @ Array.to_list mem @ Array.to_list table
@ Array.to_list func
let from_start = function
| None -> []
| Some n -> [ Text.Module.Field.Start (Raw n) ]
let modul
{ Binary.Module.id
; types
; global
; table
; mem
; func
; elem
; data
; start
; exports
; _
} =
let fields =
from_types types @ from_global global @ from_table table @ from_mem mem
@ from_func func @ from_elem elem @ from_data data @ from_exports exports
@ from_start start
in
let imported, locals =
List.partition_map
(function
| Text.Module.Field.Import _ as import -> Either.Left import
| local -> Either.Right local )
fields
in
let fields = imported @ locals in
{ Text.Module.id; fields }