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
(* SPDX-License-Identifier: AGPL-3.0-or-later *)
(* Copyright © 2021-2026 OCamlPro *)
(* Written by the Owi programmers *)

open Syntax

type t =
  { typ : Text.sub_type Array.t
  ; typ_names : (string, int) Hashtbl.t
  ; field_names : (int, (string, int) Hashtbl.t) Hashtbl.t
  ; global_names : (string, int) Hashtbl.t
  ; table_names : (string, int) Hashtbl.t
  ; mem_names : (string, int) Hashtbl.t
  ; func_names : (string, int) Hashtbl.t
  ; elem_names : (string, int) Hashtbl.t
  ; data_names : (string, int) Hashtbl.t
  ; tag_names : (string, int) Hashtbl.t
  }

let pp_table fmt tbl =
  Fmt.iter_bindings
    ~sep:(fun fmt () -> Fmt.pf fmt " ; ")
    Hashtbl.iter
    (fun fmt (name, n) -> Fmt.pf fmt "(%S, %d)" name n)
    fmt tbl

let pp fmt
  { typ
  ; typ_names
  ; field_names = _
  ; global_names
  ; table_names
  ; mem_names
  ; func_names
  ; elem_names
  ; data_names
  ; tag_names
  } =
  Fmt.pf fmt
    "Types: %a@\n\
     Types names: %a@\n\
     Global names: %a@\n\
     Table names: %a@\n\
     Mem names: %a@\n\
     Func names: %a@\n\
     Elem names: %a@\n\
     Data names: %a@\n\
     Tag names: %a@\n"
    (Fmt.array Text.pp_sub_type)
    typ pp_table typ_names pp_table global_names pp_table table_names pp_table
    mem_names pp_table func_names pp_table elem_names pp_table data_names
    pp_table tag_names

module Typetbl = Hashtbl.Make (struct
  type t = Text.func_type

  let equal = Text.func_type_eq

  let hash = Hashtbl.hash
end)

let add_sub_type ~name ~sub_type ~declared_types ~named_types ~field_names =
  let id = Dynarray.length declared_types in
  Option.iter (fun n -> Hashtbl.add named_types n id) name;
  match sub_type with
  | Text.{ ct = Def_struct_t fields; _ } ->
    let tbl = Hashtbl.create 16 in
    let* _idx =
      list_fold_left
        (fun idx (field_id, _) ->
          let* () =
            match field_id with
            | Some (Text.Text n) when Hashtbl.mem tbl n ->
              Error (`Msg "duplicate field")
            | Some (Text.Text n) ->
              Hashtbl.add tbl n idx;
              Ok ()
            | Some (Text.Raw _) | None -> Ok ()
          in
          Ok (idx + 1) )
        0 fields
    in
    if Hashtbl.length tbl > 0 then Hashtbl.add field_names id tbl;
    Dynarray.add_last declared_types sub_type;
    Ok ()
  | _ ->
    Dynarray.add_last declared_types sub_type;
    Ok ()

let assign_types (typ : Text.Typedef.t array) decl_types :
  ( Text.sub_type Array.t
  * (string, int) Hashtbl.t
  * (int, (string, int) Hashtbl.t) Hashtbl.t )
  Result.t =
  let all_func_types = Typetbl.create 64 in
  let named_types = Hashtbl.create 64 in
  let field_names : (int, (string, int) Hashtbl.t) Hashtbl.t =
    Hashtbl.create 16
  in
  let declared_types : Text.sub_type Dynarray.t = Dynarray.create () in
  let* () =
    array_iter
      (fun typedef ->
        match typedef with
        | Text.Typedef.SimpleType
            ( name
            , ( { Text.final = true; ids = []; ct = Text.Def_func_t ft } as
                sub_type ) ) ->
          let id = Dynarray.length declared_types in
          Typetbl.add all_func_types ft id;
          add_sub_type ~name ~sub_type ~declared_types ~named_types ~field_names
        | Text.Typedef.SimpleType (name, sub_type) ->
          add_sub_type ~name ~sub_type ~declared_types ~named_types ~field_names
        | Text.Typedef.RecType members ->
          list_iter
            (fun (name, sub_type) ->
              add_sub_type ~name ~sub_type ~declared_types ~named_types
                ~field_names )
            members )
      typ
  in
  Array.iter
    (fun func_type ->
      match Typetbl.find_opt all_func_types func_type with
      | Some _id -> ()
      | None ->
        let id = Dynarray.length declared_types in
        let sub_type =
          { Text.final = true; ids = []; ct = Text.Def_func_t func_type }
        in
        Dynarray.add_last declared_types sub_type;
        Typetbl.add all_func_types func_type id )
    decl_types;
  (* decl_types contains implicitly declared function types *)
  Ok (Dynarray.to_array declared_types, named_types, field_names)

let get_origin_name (get_name : 'a -> string option) (elt : ('a, 'b) Origin.t) :
  string option =
  match elt with
  | Local v -> get_name v
  | Imported { assigned_name; _ } -> assigned_name

let name kind ~get_name values =
  let named = Hashtbl.create 64 in
  let+ () =
    array_iteri
      (fun i elt_v ->
        match get_name elt_v with
        | None -> Ok ()
        | Some name ->
          if Hashtbl.mem named name then
            Fmt.error_msg "duplicate %s %s" kind name
          else begin
            Hashtbl.add named name i;
            Ok ()
          end )
      values
  in
  named

let check_type_id (types : Text.sub_type Array.t)
  (typ_names : (string, int) Hashtbl.t) (id, func_type) =
  let id =
    match id with
    | Text.Raw i -> i
    | Text name -> (
      match Hashtbl.find_opt typ_names name with
      | None -> assert false
      | Some v -> v )
  in
  if id >= Array.length types then Error (`Unknown_type (Text.Raw id))
  else
    match Array.unsafe_get types id with
    | Text.{ ct = Def_func_t func_type'; _ } ->
      if not (Text.func_type_eq func_type func_type') then
        Error `Inline_function_type
      else Ok ()
    | _ -> Error `Inline_function_type

let of_grouped
  ({ global
   ; table
   ; mem
   ; func
   ; elem
   ; data
   ; type_checks
   ; typ
   ; decl_types
   ; tag
   ; _
   } :
    Grouped.t ) : t Result.t =
  Log.debug (fun m -> m "assigning    ...");
  let* typ, typ_names, field_names = assign_types typ decl_types in
  let* global_names =
    name "global"
      ~get_name:(get_origin_name (fun ({ id; _ } : Text.Global.t) -> id))
      global
  in
  let* table_names =
    name "table"
      ~get_name:(get_origin_name (fun ({ id; _ } : Text.Table.t) -> id))
      table
  in
  let* mem_names =
    name "mem"
      ~get_name:(get_origin_name (fun ((id, _) : Text.Mem.t) -> id))
      mem
  in
  let* func_names =
    name "func"
      ~get_name:(get_origin_name (fun ({ id; _ } : Text.Func.t) -> id))
      func
  in
  let* elem_names =
    name "elem" ~get_name:(fun (elem : Text.Elem.t) -> elem.id) elem
  in
  let* data_names =
    name "data" ~get_name:(fun (data : Text.Data.t) -> data.id) data
  in
  let* tag_names =
    name "tag"
      ~get_name:(get_origin_name (fun ({ id; _ } : Text.Tag.t) -> id))
      tag
  in
  let+ () = array_iter (check_type_id typ typ_names) type_checks in

  let modul =
    { typ
    ; typ_names
    ; field_names
    ; global_names
    ; table_names
    ; mem_names
    ; func_names
    ; elem_names
    ; data_names
    ; tag_names
    }
  in
  Log.debug (fun m -> m "%a" pp modul);
  modul

let find (names : (string, int) Hashtbl.t) err : _ -> Binary.indice Result.t =
  function
  | Text.Raw i -> Ok i
  | Text name -> (
    match Hashtbl.find_opt names name with None -> Error err | Some i -> Ok i )

let find_func modul id = find modul.func_names (`Unknown_func id) id

let find_global modul id = find modul.global_names (`Unknown_global id) id

let find_memory modul id = find modul.mem_names (`Unknown_memory id) id

let find_table modul id = find modul.table_names (`Unknown_table id) id

let find_data modul id = find modul.data_names (`Unknown_data id) id

let find_elem modul id = find modul.elem_names (`Unknown_elem id) id

let find_type modul id = find modul.typ_names (`Unknown_type id) id

let find_tag modul id = find modul.tag_names (`Unknown_tag id) id

let get_func_type modul idx =
  if idx >= Array.length modul.typ then None
  else
    match Array.unsafe_get modul.typ idx with
    | Text.{ ct = Def_func_t ft; _ } -> Some ft
    | _ -> None

let get_types modul = modul.typ

let find_raw_func_type modul func_type =
  let ty_opt =
    Array.find_index
      (function
        | Text.{ ct = Def_func_t ft; _ } -> Text.func_type_eq func_type ft
        | _ -> false )
      modul.typ
  in
  match ty_opt with None -> assert false | Some idx -> idx

let find_field modul type_idx field_id =
  match field_id with
  | Text.Raw i -> Ok i
  | Text name -> (
    match Hashtbl.find_opt modul.field_names type_idx with
    | None ->
      Error
        (`Type_mismatch (Fmt.str "no named fields for struct type %d" type_idx))
    | Some tbl -> (
      match Hashtbl.find_opt tbl name with
      | None ->
        Error
          (`Type_mismatch
             (Fmt.str "unknown field %s in struct type %d" name type_idx) )
      | Some i -> Ok i ) )