Skip to content

Commit 9477928

Browse files
authored
Update exact function imports (#75)
Address feedback from #72, including by using (exact, deftype) pairs instead of full heaptypes where possible in the interpreter. Update the overview accordingly. Also fix #74 by introducing a new externtype binary encoding for exact function imports rather than using an s33 in the existing function import variant.
1 parent b67b780 commit 9477928

File tree

16 files changed

+131
-87
lines changed

16 files changed

+131
-87
lines changed

interpreter/binary/decode.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,12 @@ let tabletype s =
320320

321321
let externtype s =
322322
match byte s with
323-
| 0x00 -> ExternFuncT (heaptype s)
323+
| 0x00 -> ExternFuncT (Inexact, typeuse idx s)
324324
| 0x01 -> ExternTableT (tabletype s)
325325
| 0x02 -> ExternMemoryT (memorytype s)
326326
| 0x03 -> ExternGlobalT (globaltype s)
327327
| 0x04 -> ExternTagT (tagtype s)
328+
| 0x20 -> ExternFuncT (Exact, typeuse idx s)
328329
| _ -> error s (pos s - 1) "malformed import kind"
329330

330331

interpreter/binary/encode.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ struct
207207
| TableT (at, lim, t) -> reftype t; limits at lim
208208

209209
let externtype = function
210-
| ExternFuncT ht -> byte 0x00; heaptype ht
210+
| ExternFuncT (Inexact, ut) -> byte 0x00; typeuse u32 ut
211+
| ExternFuncT (Exact, ut) -> byte 0x20; typeuse u32 ut
211212
| ExternTableT tt -> byte 0x01; tabletype tt
212213
| ExternMemoryT mt -> byte 0x02; memorytype mt
213214
| ExternGlobalT gt -> byte 0x03; globaltype gt

interpreter/host/env.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ let exit vs =
4242

4343
let lookup name et =
4444
match Utf8.encode name, et with
45-
| "abort", ExternFuncT (UseHT (_exact, ut)) ->
45+
| "abort", ExternFuncT (_exact, ut) ->
4646
ExternFunc (Func.alloc_host (deftype_of_typeuse ut) abort)
47-
| "exit", ExternFuncT (UseHT (_exact, ut)) ->
47+
| "exit", ExternFuncT (_exact, ut) ->
4848
ExternFunc (Func.alloc_host (deftype_of_typeuse ut) exit)
4949
| _ -> raise Not_found

interpreter/runtime/instance.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ let externtype_of c = function
7575
| ExternGlobal glob -> ExternGlobalT (Global.type_of glob)
7676
| ExternMemory mem -> ExternMemoryT (Memory.type_of mem)
7777
| ExternTable tab -> ExternTableT (Table.type_of tab)
78-
| ExternFunc func -> ExternFuncT (UseHT (Exact, Def (Func.type_of func)))
78+
| ExternFunc func -> ExternFuncT (Exact, Def (Func.type_of func))
7979

8080
let export inst name =
8181
try Some (List.assoc name inst.exports) with Not_found -> None

interpreter/script/js.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ let invoke dt vs at =
424424
let rts0 = Lib.List32.init subject_type_idx (fun i -> dummy, (dummy, i)) in
425425
let rts, i = statify_deftype rts0 dt in
426426
List.map (fun (_, (rt, _)) -> rt @@ at) (Lib.List32.drop subject_type_idx rts),
427-
ExternFuncT (UseHT (Inexact, (Idx i))),
427+
ExternFuncT (Inexact, Idx i),
428428
List.concat (List.map value vs) @ [Call (subject_idx @@ at) @@ at]
429429

430430
let get t at =
@@ -606,9 +606,9 @@ let wrap item_name wrap_action wrap_assertion at =
606606
let imports =
607607
[ Import (Utf8.decode "module", item_name, idesc) @@ at;
608608
Import (Utf8.decode "spectest", Utf8.decode "hostref",
609-
ExternFuncT (UseHT (Inexact, (Idx 1l)))) @@ at;
609+
ExternFuncT (Inexact, Idx 1l)) @@ at;
610610
Import (Utf8.decode "spectest", Utf8.decode "eq_ref",
611-
ExternFuncT (UseHT (Inexact, (Idx 2l)))) @@ at;
611+
ExternFuncT (Inexact, Idx 2l)) @@ at;
612612
]
613613
in
614614
let item =
@@ -775,7 +775,7 @@ let of_action env act =
775775
"call(" ^ of_inst_opt env x_opt ^ ", " ^ of_name name ^ ", " ^
776776
"[" ^ String.concat ", " (List.map of_value vs) ^ "])",
777777
(match lookup_export env x_opt name act.at with
778-
| ExternFuncT (UseHT (_exact, (Def dt))) ->
778+
| ExternFuncT (_exact, Def dt) ->
779779
let (_, out) as ft = functype_of_comptype (expand_deftype dt) in
780780
if is_js_functype ft then
781781
None

interpreter/syntax/ast.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,10 @@ let exporttype_of (m : module_) (ex : export) : exporttype =
414414
| FuncX x ->
415415
let dts = funcs xts @ List.map (fun f ->
416416
let Func (y, _, _) = f.it in
417-
UseHT (Exact, Def (Lib.List32.nth dts y.it))) m.it.funcs in
418-
ExternFuncT (Lib.List32.nth dts x.it)
417+
(Exact, Def (Lib.List32.nth dts y.it))) m.it.funcs
418+
in
419+
let (exact, dt) = Lib.List32.nth dts x.it in
420+
ExternFuncT (exact, dt)
419421
in ExportT (name, subst_externtype (subst_of dts) xt)
420422

421423
let moduletype_of (m : module_) : moduletype =

interpreter/syntax/free.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ let externtype = function
129129
| ExternGlobalT gt -> globaltype gt
130130
| ExternMemoryT mt -> memorytype mt
131131
| ExternTableT tt -> tabletype tt
132-
| ExternFuncT ht -> heaptype ht
132+
| ExternFuncT (_, ut) -> typeuse ut
133133

134134
let blocktype = function
135135
| VarBlockType x -> types (idx x)

interpreter/syntax/types.ml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type externtype =
4949
| ExternGlobalT of globaltype
5050
| ExternMemoryT of memorytype
5151
| ExternTableT of tabletype
52-
| ExternFuncT of heaptype
52+
| ExternFuncT of exact * typeuse
5353

5454
type exporttype = ExportT of name * externtype
5555
type importtype = ImportT of name * name * externtype
@@ -125,8 +125,6 @@ let unpacked_fieldtype (FieldT (_mut, t)) = unpacked_storagetype t
125125
let idx_of_typeuse = function Idx x -> x | _ -> assert false
126126
let deftype_of_typeuse = function Def dt -> dt | _ -> assert false
127127

128-
let typeuse_of_heaptype = function UseHT (_, ut) -> ut | _ -> assert false
129-
130128
let structtype_of_comptype = function StructT fts -> fts | _ -> assert false
131129
let arraytype_of_comptype = function ArrayT ft -> ft | _ -> assert false
132130
let functype_of_comptype = function FuncT rt2 -> rt2 | _ -> assert false
@@ -141,7 +139,7 @@ let tags = List.filter_map (function ExternTagT tt -> Some tt | _ -> None)
141139
let globals = List.filter_map (function ExternGlobalT gt -> Some gt | _ -> None)
142140
let memories = List.filter_map (function ExternMemoryT mt -> Some mt | _ -> None)
143141
let tables = List.filter_map (function ExternTableT tt -> Some tt | _ -> None)
144-
let funcs = List.filter_map (function ExternFuncT ft -> Some ft | _ -> None)
142+
let funcs = List.filter_map (function ExternFuncT (exact, ut) -> Some (exact, ut) | _ -> None)
145143

146144

147145
(* Substitution *)
@@ -237,7 +235,7 @@ let subst_externtype s = function
237235
| ExternGlobalT gt -> ExternGlobalT (subst_globaltype s gt)
238236
| ExternMemoryT mt -> ExternMemoryT (subst_memorytype s mt)
239237
| ExternTableT tt -> ExternTableT (subst_tabletype s tt)
240-
| ExternFuncT ht -> ExternFuncT (subst_heaptype s ht)
238+
| ExternFuncT (exact, ut) -> ExternFuncT (exact, subst_typeuse s ut)
241239

242240

243241
let subst_exporttype s = function
@@ -448,8 +446,8 @@ let string_of_externtype = function
448446
| ExternGlobalT gt -> "global " ^ string_of_globaltype gt
449447
| ExternMemoryT mt -> "memory " ^ string_of_memorytype mt
450448
| ExternTableT tt -> "table " ^ string_of_tabletype tt
451-
| ExternFuncT ht -> "func " ^ string_of_heaptype ht
452-
449+
| ExternFuncT (Inexact, ut) -> "func " ^ string_of_typeuse ut
450+
| ExternFuncT (Exact, ut) -> "func exact " ^ string_of_typeuse ut
453451

454452
let string_of_exporttype = function
455453
| ExportT (name, xt) ->

interpreter/text/arrange.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,9 @@ let heaptype t = string_of_heaptype t
8484
let valtype t = string_of_valtype t
8585
let storagetype t = string_of_storagetype t
8686

87-
let heaptypeuse = function
88-
| UseHT (Inexact, ut) -> typeuse ut
89-
| UseHT (Exact, ut) -> Node ("exact", [typeuse ut])
90-
| _ -> assert false
87+
let exacttypeuse = function
88+
| (Inexact, ut) -> typeuse ut
89+
| (Exact, ut) -> Node ("exact", [typeuse ut])
9190

9291
let final = function
9392
| NoFinal -> ""
@@ -713,7 +712,8 @@ let importtype fx tx mx tgx gx = function
713712
| ExternGlobalT gt -> incr gx; Node ("global $" ^ nat (!gx - 1), globaltype gt)
714713
| ExternMemoryT mt -> incr mx; Node ("memory $" ^ nat (!mx - 1), memorytype mt)
715714
| ExternTableT tt -> incr tx; Node ("table $" ^ nat (!tx - 1), tabletype tt)
716-
| ExternFuncT ht -> incr fx; Node ("func $" ^ nat (!fx - 1), [heaptypeuse ht])
715+
| ExternFuncT (exact, ut) ->
716+
incr fx; Node ("func $" ^ nat (!fx - 1), [exacttypeuse (exact, ut)])
717717

718718
let import fx tx mx ex gx im =
719719
let Import (module_name, item_name, xt) = im.it in

interpreter/text/parser.mly

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,9 @@ limits :
492492
typeuse :
493493
| LPAR TYPE idx RPAR { fun c -> $3 c type_ }
494494

495-
heaptypeuse :
496-
| LPAR EXACT typeuse RPAR { fun c -> UseHT (Exact, Idx ($3 c).it) }
497-
| typeuse { fun c -> UseHT (Inexact, Idx ($1 c).it) }
495+
exacttypeuse :
496+
| LPAR EXACT typeuse RPAR { fun c -> (Exact, $3 c) }
497+
| typeuse { fun c -> (Inexact, $1 c) }
498498

499499
/* Immediates */
500500

@@ -998,20 +998,17 @@ func_fields :
998998
let y = inline_functype c' (fst $1 c') loc in
999999
let Func (_, ls, es) = snd $1 c' in
10001000
[Func (y, ls, es) @@ loc], [], [] }
1001-
| inline_import heaptypeuse func_fields_import /* Sugar */
1001+
| inline_import exacttypeuse func_fields_import /* Sugar */
10021002
{ fun c x loc ->
1003-
let exact, y = match ($2 c) with
1004-
| UseHT (exact, Idx y) -> exact, y
1005-
| _ -> assert false
1006-
in
1007-
let y = inline_functype_explicit c (y @@ loc) ($3 c) in
1003+
let exact, y = $2 c in
1004+
let y = inline_functype_explicit c y ($3 c) in
10081005
[],
1009-
[Import (fst $1, snd $1, ExternFuncT (UseHT (exact, Idx y.it))) @@ loc ], [] }
1006+
[Import (fst $1, snd $1, ExternFuncT (exact, Idx y.it)) @@ loc ], [] }
10101007
| inline_import func_fields_import /* Sugar */
10111008
{ fun c x loc ->
10121009
let y = inline_functype c ($2 c) loc in
10131010
[],
1014-
[Import (fst $1, snd $1, ExternFuncT (UseHT (Inexact, Idx y.it))) @@ loc ], [] }
1011+
[Import (fst $1, snd $1, ExternFuncT (Inexact, Idx y.it)) @@ loc ], [] }
10151012
| inline_export func_fields /* Sugar */
10161013
{ fun c x loc ->
10171014
let fns, ims, exs = $2 c x loc in fns, ims, $1 (FuncX x) c :: exs }
@@ -1253,9 +1250,9 @@ table_fields :
12531250
/* Imports & Exports */
12541251
12551252
externtype :
1256-
| LPAR FUNC bindidx_opt heaptypeuse RPAR
1253+
| LPAR FUNC bindidx_opt exacttypeuse RPAR
12571254
{ fun c -> ignore ($3 c anon_func bind_func);
1258-
fun () -> ExternFuncT ($4 c) }
1255+
fun () -> let exact, y = $4 c in ExternFuncT (exact, Idx y.it) }
12591256
| LPAR TAG bindidx_opt typeuse RPAR
12601257
{ fun c -> ignore ($3 c anon_tag bind_tag);
12611258
fun () -> ExternTagT (TagT (Idx ($4 c).it)) }
@@ -1276,7 +1273,7 @@ externtype :
12761273
fun () ->
12771274
let exact, ft = $4 c in
12781275
let y = inline_functype c ft $loc($4) in
1279-
ExternFuncT (UseHT (exact, Idx y.it)) }
1276+
ExternFuncT (exact, Idx y.it) }
12801277
12811278
import :
12821279
| LPAR IMPORT name name externtype RPAR

0 commit comments

Comments
 (0)