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
(* SPDX-License-Identifier: AGPL-3.0-or-later *)
(* Copyright © 2021-2024 OCamlPro *)
(* Written by the Owi programmers *)
open Binary
open Text
open Types
let convert_block_type (bt : binary block_type) : text block_type =
match bt with
| Bt_raw (opt, ft) ->
let opt = match opt with None -> None | Some (Raw _ as opt) -> Some opt in
Bt_raw (opt, ft)
let convert_expr (e : binary expr Annotated.t) : text expr Annotated.t =
(* TODO: proper conversion ! *)
Obj.magic e
let convert_elem_mode (e : Binary.elem_mode) : Text.elem_mode =
match e with
| Elem_passive -> Elem_passive
| Elem_declarative -> Elem_declarative
| Elem_active (opt, e) ->
let opt = Option.map (fun i -> Raw i) opt in
let e = convert_expr e in
Elem_active (opt, e)
let convert_elem (e : Binary.elem) : Text.elem =
let { Binary.id; typ; init; mode } = e in
let init = List.map convert_expr init in
let mode = convert_elem_mode mode in
{ id; typ; init; mode }
let convert_data_mode (m : Binary.data_mode) : Text.data_mode =
match m with
| Data_passive -> Data_passive
| Data_active (i, e) ->
let e = convert_expr e in
Data_active (Some (Raw i), e)
let convert_data (e : Binary.data) : Text.data =
let { Binary.id; init; mode } : Binary.data = e in
let mode = convert_data_mode mode in
{ id; init; mode }
let from_types types : Text.module_field list =
Array.map (fun (t : Types.type_def) -> MType t) types |> Array.to_list
let from_global global : Text.module_field list =
Array.map
(function
| Runtime.Local (g : Binary.global) ->
let typ = g.typ in
let init = convert_expr g.init in
let id = g.id in
MGlobal { typ; init; id }
| Imported { modul; name; assigned_name; desc } ->
let desc = Import_global (assigned_name, desc) in
MImport { modul; name; desc } )
global
|> Array.to_list
let from_table table : Text.module_field list =
Array.map
(function
| Runtime.Local t -> MTable t
| Imported { modul; name; assigned_name; desc } ->
let desc = Import_table (assigned_name, desc) in
MImport { modul; name; desc } )
table
|> Array.to_list
let from_mem mem : Text.module_field list =
Array.map
(function
| Runtime.Local mem -> MMem mem
| Imported { modul; name; assigned_name; desc } ->
let desc = Import_mem (assigned_name, desc) in
MImport { modul; name; desc } )
mem
|> Array.to_list
let from_func func : Text.module_field list =
Array.map
(function
| Runtime.Local func ->
let type_f = convert_block_type func.type_f in
let locals = func.locals in
let body = convert_expr func.body in
let id = func.id in
MFunc { type_f; locals; body; id }
| Imported { modul; name; assigned_name; desc } ->
let desc = Import_func (assigned_name, convert_block_type desc) in
MImport { modul; name; desc } )
func
|> Array.to_list
let from_elem elem : Text.module_field list =
Array.map
(fun (elem : Binary.elem) ->
let elem = convert_elem elem in
MElem elem )
elem
|> Array.to_list
let from_data data : Text.module_field list =
Array.map
(fun (data : Binary.data) ->
let data = convert_data data in
MData data )
data
|> Array.to_list
let from_exports (exports : Binary.exports) : Text.module_field list =
let global =
List.map
(fun { name; id } ->
let id = Some (Raw id) in
MExport { name; desc = Export_global id } )
exports.global
in
let mem =
List.map
(fun { name; id } ->
let id = Some (Raw id) in
MExport { name; desc = Export_mem id } )
exports.mem
in
let table =
List.map
(fun { name; id } ->
let id = Some (Raw id) in
MExport { name; desc = Export_table id } )
exports.table
in
let func =
List.map
(fun { name; id } ->
let id = Some (Raw id) in
MExport { name; desc = Export_func id } )
exports.func
in
global @ mem @ table @ func
let from_start = function None -> [] | Some n -> [ MStart (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
| MImport _ as import -> Either.Left import
| local -> Either.Right local )
fields
in
let fields = imported @ locals in
let annots = [] in
{ Text.id; fields; annots }