Skip to content
This repository was archived by the owner on Nov 3, 2021. It is now read-only.

Commit 0cf128e

Browse files
rossbergbinji
authored andcommitted
[interpreter] Refactor element type in passive segments (#71)
* Refactor type in passive segments * Make tests more specific
1 parent 63cceba commit 0cf128e

File tree

11 files changed

+130
-127
lines changed

11 files changed

+130
-127
lines changed

interpreter/binary/decode.ml

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ type stream =
55
name : string;
66
bytes : string;
77
pos : int ref;
8-
has_data_count : bool ref;
8+
need_data_count : bool ref;
99
}
1010

1111
exception EOS
1212

13-
let stream name bs = {name; bytes = bs; pos = ref 0; has_data_count = ref false}
13+
let stream name bs = {name; bytes = bs; pos = ref 0; need_data_count = ref false}
1414

1515
let len s = String.length s.bytes
1616
let pos s = !(s.pos)
@@ -202,18 +202,15 @@ let memop s =
202202
let offset = vu32 s in
203203
Int32.to_int align, offset
204204

205-
let check_data_count s =
206-
require !(s.has_data_count) s (pos s - 1) "data count section required"
207-
208205
let misc_instr s =
209206
let pos = pos s in
210207
match op s with
211208
| 0x08 ->
212-
check_data_count s;
213209
let x = at var s in
214210
zero_flag s;
211+
s.need_data_count := true;
215212
memory_init x
216-
| 0x09 -> check_data_count s; data_drop (at var s)
213+
| 0x09 -> s.need_data_count := true; data_drop (at var s)
217214
| 0x0a -> zero_flag s; zero_flag s; memory_copy
218215
| 0x0b -> zero_flag s; memory_fill
219216
| 0x0c ->
@@ -625,16 +622,17 @@ let segment active passive s =
625622
let init = active s in
626623
Active {index; offset; init}
627624
| 1l ->
628-
let init = passive s in
629-
Passive init
625+
let etype, data = passive s in
626+
Passive {etype; data}
630627
| 2l ->
631628
let index = at var s in
632629
let offset = const s in
633630
let init = active s in
634631
Active {index; offset; init}
635632
| _ -> error s (pos s - 1) "invalid segment kind"
636633

637-
let active_elem s = Func (at var s)
634+
let active_elem s =
635+
Func (at var s)
638636

639637
let passive_elem s =
640638
match u8 s with
@@ -646,7 +644,7 @@ let passive_elem s =
646644
| _ -> error s (pos s - 1) "invalid elem"
647645

648646
let active_elem_segment s =
649-
FuncRefType, vec (at active_elem) s
647+
vec (at active_elem) s
650648

651649
let passive_elem_segment s =
652650
let etype = elem_type s in
@@ -663,19 +661,19 @@ let elem_section s =
663661
(* Data section *)
664662

665663
let memory_segment s =
666-
segment string string s
664+
segment string (fun s -> (), string s) s
667665

668666
let data_section s =
669667
section `DataSection (vec (at memory_segment)) [] s
670668

671669

672670
(* DataCount section *)
673671

672+
let data_count s =
673+
Some (vu32 s)
674+
674675
let data_count_section s =
675-
let contents s =
676-
s.has_data_count := true;
677-
opt vu32 true s
678-
in section `DataCountSection contents None s
676+
section `DataCountSection data_count None s
679677

680678

681679
(* Custom section *)
@@ -722,18 +720,19 @@ let module_ s =
722720
iterate custom_section s;
723721
let func_bodies = code_section s in
724722
iterate custom_section s;
725-
let data = data_section s in
723+
let datas = data_section s in
726724
iterate custom_section s;
727725
require (pos s = len s) s (len s) "junk after last section";
728726
require (List.length func_types = List.length func_bodies)
729727
s (len s) "function and code section have inconsistent lengths";
730-
require
731-
(data_count = None || data_count = Some (Int32.of_int (List.length data)))
728+
require (data_count = None || data_count = Some (Lib.List32.length datas))
732729
s (len s) "data count and data section have inconsistent lengths";
730+
require (not !(s.need_data_count) || data_count <> None)
731+
s (len s) "data count section required";
733732
let funcs =
734733
List.map2 Source.(fun t f -> {f.it with ftype = t} @@ f.at)
735734
func_types func_bodies
736-
in {types; tables; memories; globals; funcs; imports; exports; elems; data; start}
735+
in {types; tables; memories; globals; funcs; imports; exports; elems; datas; start}
737736

738737

739738
let decode name bs = at module_ (stream name bs)

interpreter/binary/encode.ml

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -485,10 +485,9 @@ let encode m =
485485
else begin
486486
u8 0x02; var index
487487
end;
488-
const offset;
489-
active init
490-
| Passive init ->
491-
u8 0x01; passive init
488+
const offset; active init
489+
| Passive {etype; data} ->
490+
u8 0x01; passive etype data
492491

493492
let active_elem el =
494493
match el.it with
@@ -501,24 +500,23 @@ let encode m =
501500
| Func x -> u8 0xd2; var x; end_ ()
502501

503502
let table_segment seg =
504-
let active (_,init) = vec active_elem init in
505-
let passive (etype,init) = elem_type etype; vec passive_elem init in
503+
let active init = vec active_elem init in
504+
let passive etype data = elem_type etype; vec passive_elem data in
506505
segment active passive seg
507506

508507
let elem_section elems =
509508
section 9 (vec table_segment) elems (elems <> [])
510509

511510
(* Data section *)
512511
let memory_segment seg =
513-
segment string string seg
512+
segment string (fun _ s -> string s) seg
514513

515-
let data_section data =
516-
section 11 (vec memory_segment) data (data <> [])
514+
let data_section datas =
515+
section 11 (vec memory_segment) datas (datas <> [])
517516

518-
(* DataCount section *)
519-
520-
let data_count_section data =
521-
section 12 len (List.length data) (data <> [])
517+
(* Data count section *)
518+
let data_count_section datas =
519+
section 12 len (List.length datas) true
522520

523521
(* Module *)
524522

@@ -534,8 +532,8 @@ let encode m =
534532
export_section m.it.exports;
535533
start_section m.it.start;
536534
elem_section m.it.elems;
537-
data_count_section m.it.data;
535+
data_count_section m.it.datas;
538536
code_section m.it.funcs;
539-
data_section m.it.data
537+
data_section m.it.datas
540538
end
541539
in E.module_ m; to_string s

interpreter/exec/eval.ml

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ let func (inst : module_inst) x = lookup "function" inst.funcs x
8282
let table (inst : module_inst) x = lookup "table" inst.tables x
8383
let memory (inst : module_inst) x = lookup "memory" inst.memories x
8484
let global (inst : module_inst) x = lookup "global" inst.globals x
85-
let elems (inst : module_inst) x = lookup "elems" inst.elems x
86-
let data (inst : module_inst) x = lookup "data" inst.data x
85+
let elem (inst : module_inst) x = lookup "element segment" inst.elems x
86+
let data (inst : module_inst) x = lookup "data segment" inst.datas x
8787
let local (frame : frame) x = lookup "local" frame.locals x
8888

89-
let elem inst x i at =
89+
let any_elem inst x i at =
9090
match Table.load (table inst x) i with
9191
| Table.Uninitialized ->
9292
Trap.error at ("uninitialized element " ^ Int32.to_string i)
@@ -95,7 +95,7 @@ let elem inst x i at =
9595
Trap.error at ("undefined element " ^ Int32.to_string i)
9696

9797
let func_elem inst x i at =
98-
match elem inst x i at with
98+
match any_elem inst x i at with
9999
| FuncElem f -> f
100100
| _ -> Crash.error at ("type mismatch for element " ^ Int32.to_string i)
101101

@@ -263,13 +263,8 @@ let rec step (c : config) : config =
263263
let src = I64_convert.extend_i32_u s in
264264
(try Memory.init mem bs dst src n; vs', []
265265
with exn -> vs', [Trapping (memory_error e.at exn) @@ e.at])
266-
| None -> vs', [Trapping "data segment dropped" @@ e.at])
267-
268-
| DataDrop x, vs ->
269-
let seg = data frame.inst x in
270-
(match !seg with
271-
| Some _ -> seg := None; vs, []
272-
| None -> vs, [Trapping "data segment dropped" @@ e.at])
266+
| None -> vs', [Trapping "data segment dropped" @@ e.at]
267+
)
273268

274269
| MemoryCopy, I32 n :: I32 s :: I32 d :: vs' ->
275270
let mem = memory frame.inst (0l @@ e.at) in
@@ -286,23 +281,32 @@ let rec step (c : config) : config =
286281

287282
| TableInit x, I32 n :: I32 s :: I32 d :: vs' ->
288283
let tab = table frame.inst (0l @@ e.at) in
289-
(match !(elems frame.inst x) with
284+
(match !(elem frame.inst x) with
290285
| Some es ->
291286
(try Table.init tab es d s n; vs', []
292287
with exn -> vs', [Trapping (table_error e.at exn) @@ e.at])
293-
| None -> vs', [Trapping "elements segment dropped" @@ e.at])
294-
295-
| ElemDrop x, vs ->
296-
let seg = elems frame.inst x in
297-
(match !seg with
298-
| Some _ -> seg := None; vs, []
299-
| None -> vs, [Trapping "elements segment dropped" @@ e.at])
288+
| None -> vs', [Trapping "element segment dropped" @@ e.at]
289+
)
300290

301291
| TableCopy, I32 n :: I32 s :: I32 d :: vs' ->
302292
let tab = table frame.inst (0l @@ e.at) in
303293
(try Table.copy tab d s n; vs', []
304294
with exn -> vs', [Trapping (table_error e.at exn) @@ e.at])
305295

296+
| DataDrop x, vs ->
297+
let seg = data frame.inst x in
298+
(match !seg with
299+
| Some _ -> seg := None; vs, []
300+
| None -> vs, [Trapping "data segment dropped" @@ e.at]
301+
)
302+
303+
| ElemDrop x, vs ->
304+
let seg = elem frame.inst x in
305+
(match !seg with
306+
| Some _ -> seg := None; vs, []
307+
| None -> vs, [Trapping "element segment dropped" @@ e.at]
308+
)
309+
306310
| _ ->
307311
let s1 = string_of_values (List.rev vs) in
308312
let s2 = string_of_value_types (List.map type_of (List.rev vs)) in
@@ -435,22 +439,22 @@ let create_export (inst : module_inst) (ex : export) : export_inst =
435439
| GlobalExport x -> ExternGlobal (global inst x)
436440
in name, ext
437441

438-
let elems_list inst init =
442+
let elem_list inst init =
439443
let to_elem el =
440444
match el.it with
441445
| Null -> Table.Uninitialized
442446
| Func x -> FuncElem (func inst x)
443447
in List.map to_elem init
444448

445-
let create_elems (inst : module_inst) (seg : table_segment) : elems_inst =
449+
let create_elem (inst : module_inst) (seg : table_segment) : elem_inst =
446450
match seg.it with
447451
| Active _ -> ref None
448-
| Passive (_,init) -> ref (Some (elems_list inst init))
452+
| Passive {data; _} -> ref (Some (elem_list inst data))
449453

450454
let create_data (inst : module_inst) (seg : memory_segment) : data_inst =
451455
match seg.it with
452456
| Active _ -> ref None
453-
| Passive init -> ref (Some init)
457+
| Passive {data; _} -> ref (Some data)
454458

455459

456460
let init_func (inst : module_inst) (func : func_inst) =
@@ -460,14 +464,14 @@ let init_func (inst : module_inst) (func : func_inst) =
460464

461465
let init_table (inst : module_inst) (seg : table_segment) =
462466
match seg.it with
463-
| Active {index; offset = const; init = (_,init)} ->
467+
| Active {index; offset = const; init} ->
464468
let tab = table inst index in
465469
let offset = i32 (eval_const inst const) const.at in
466-
let elems = elems_list inst init in
470+
let elems = elem_list inst init in
467471
let len = Int32.of_int (List.length elems) in
468472
(try Table.init tab elems offset 0l len
469473
with Table.Bounds -> Link.error seg.at "elements segment does not fit table")
470-
| Passive init -> ()
474+
| Passive _ -> ()
471475

472476
let init_memory (inst : module_inst) (seg : memory_segment) =
473477
match seg.it with
@@ -478,7 +482,7 @@ let init_memory (inst : module_inst) (seg : memory_segment) =
478482
let len = Int32.of_int (String.length init) in
479483
(try Memory.init mem init offset 0L len
480484
with Memory.Bounds -> Link.error seg.at "data segment does not fit memory")
481-
| Passive init -> ()
485+
| Passive _ -> ()
482486

483487

484488
let add_import (m : module_) (ext : extern) (im : import) (inst : module_inst)
@@ -494,7 +498,7 @@ let add_import (m : module_) (ext : extern) (im : import) (inst : module_inst)
494498
let init (m : module_) (exts : extern list) : module_inst =
495499
let
496500
{ imports; tables; memories; globals; funcs; types;
497-
exports; elems; data; start
501+
exports; elems; datas; start
498502
} = m.it
499503
in
500504
if List.length exts <> List.length imports then
@@ -509,18 +513,18 @@ let init (m : module_) (exts : extern list) : module_inst =
509513
funcs = inst0.funcs @ fs;
510514
tables = inst0.tables @ List.map (create_table inst0) tables;
511515
memories = inst0.memories @ List.map (create_memory inst0) memories;
512-
globals = inst0.globals @ List.map (create_global inst0) globals
516+
globals = inst0.globals @ List.map (create_global inst0) globals;
513517
}
514518
in
515519
let inst =
516520
{ inst1 with
517521
exports = List.map (create_export inst1) exports;
518-
elems = List.map (create_elems inst1) elems;
519-
data = List.map (create_data inst1) data
522+
elems = List.map (create_elem inst1) elems;
523+
datas = List.map (create_data inst1) datas;
520524
}
521525
in
522526
List.iter (init_func inst) fs;
523527
List.iter (init_table inst) elems;
524-
List.iter (init_memory inst) data;
528+
List.iter (init_memory inst) datas;
525529
Lib.Option.app (fun x -> ignore (invoke (func inst x) [])) start;
526530
inst

interpreter/runtime/instance.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ type module_inst =
88
memories : memory_inst list;
99
globals : global_inst list;
1010
exports : export_inst list;
11-
elems : elems_inst list;
12-
data : data_inst list;
11+
elems : elem_inst list;
12+
datas : data_inst list;
1313
}
1414

1515
and func_inst = module_inst ref Func.t
1616
and table_inst = Table.t
1717
and memory_inst = Memory.t
1818
and global_inst = Global.t
1919
and export_inst = Ast.name * extern
20-
and elems_inst = Table.elem list option ref
20+
and elem_inst = Table.elem list option ref
2121
and data_inst = string option ref
2222

2323
and extern =
@@ -33,7 +33,7 @@ type Table.elem += FuncElem of func_inst
3333

3434
let empty_module_inst =
3535
{ types = []; funcs = []; tables = []; memories = []; globals = [];
36-
exports = []; elems = []; data = [] }
36+
exports = []; elems = []; datas = [] }
3737

3838
let extern_type_of = function
3939
| ExternFunc func -> ExternFuncType (Func.type_of func)

0 commit comments

Comments
 (0)