Skip to content

Commit

Permalink
[interpreter] Factor data and element segments into abstract types (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rossberg authored Jun 9, 2022
1 parent f8c221c commit 2cdd5ac
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 17 deletions.
23 changes: 12 additions & 11 deletions interpreter/exec/eval.ml
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ let mem_oob frame x i n =

let data_oob frame x i n =
I64.gt_u (I64.add (I64_convert.extend_i32_u i) (I64_convert.extend_i32_u n))
(I64.of_int_u (String.length !(data frame.inst x)))
(Data.size (data frame.inst x))

let table_oob frame x i n =
I64.gt_u (I64.add (I64_convert.extend_i32_u i) (I64_convert.extend_i32_u n))
(I64_convert.extend_i32_u (Table.size (table frame.inst x)))

let elem_oob frame x i n =
I64.gt_u (I64.add (I64_convert.extend_i32_u i) (I64_convert.extend_i32_u n))
(I64.of_int_u (List.length !(elem frame.inst x)))
(I64_convert.extend_i32_u (Elem.size (elem frame.inst x)))

let rec step (c : config) : config =
let {frame; code = vs, es; _} = c in
Expand Down Expand Up @@ -302,10 +302,10 @@ let rec step (c : config) : config =
else if n = 0l then
vs', []
else
let seg = !(elem frame.inst y) in
let seg = elem frame.inst y in
vs', List.map (at e.at) [
Plain (Const (I32 d @@ e.at));
Refer (List.nth seg (Int32.to_int s));
Refer (Elem.load seg s);
Plain (TableSet x);
Plain (Const (I32 (I32.add d 1l) @@ e.at));
Plain (Const (I32 (I32.add s 1l) @@ e.at));
Expand All @@ -315,7 +315,7 @@ let rec step (c : config) : config =

| ElemDrop x, vs ->
let seg = elem frame.inst x in
seg := [];
Elem.drop seg;
vs, []

| Load {offset; ty; pack; _}, Num (I32 i) :: vs' ->
Expand Down Expand Up @@ -465,11 +465,12 @@ let rec step (c : config) : config =
else if n = 0l then
vs', []
else
let seg = !(data frame.inst x) in
let b = Int32.of_int (Char.code seg.[Int32.to_int s]) in
let seg = data frame.inst x in
let a = I64_convert.extend_i32_u s in
let b = Data.load seg a in
vs', List.map (at e.at) [
Plain (Const (I32 d @@ e.at));
Plain (Const (I32 b @@ e.at));
Plain (Const (I32 (I32.of_int_u (Char.code b)) @@ e.at));
Plain (Store
{ty = I32Type; align = 0; offset = 0l; pack = Some Pack8});
Plain (Const (I32 (I32.add d 1l) @@ e.at));
Expand All @@ -480,7 +481,7 @@ let rec step (c : config) : config =

| DataDrop x, vs ->
let seg = data frame.inst x in
seg := "";
Data.drop seg;
vs, []

| RefNull t, vs' ->
Expand Down Expand Up @@ -715,11 +716,11 @@ let create_export (inst : module_inst) (ex : export) : export_inst =

let create_elem (inst : module_inst) (seg : elem_segment) : elem_inst =
let {etype; einit; _} = seg.it in
ref (List.map (fun c -> as_ref (eval_const inst c)) einit)
Elem.alloc (List.map (fun c -> as_ref (eval_const inst c)) einit)

let create_data (inst : module_inst) (seg : data_segment) : data_inst =
let {dinit; _} = seg.it in
ref dinit
Data.alloc dinit


let add_import (m : module_) (ext : extern) (im : import) (inst : module_inst)
Expand Down
4 changes: 2 additions & 2 deletions interpreter/exec/ixx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ sig

val of_int : int -> t
val to_int : t -> int
val of_int64: int64 -> t
val to_int64: t -> int64
val of_int64 : int64 -> t
val to_int64 : t -> int64
val to_string : t -> string
val to_hex_string : t -> string

Expand Down
7 changes: 7 additions & 0 deletions interpreter/runtime/data.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type data = string ref
type t = data

let alloc bs = ref bs
let size seg = I64.of_int_u (String.length !seg)
let load seg i = (!seg).[Int64.to_int i]
let drop seg = seg := ""
7 changes: 7 additions & 0 deletions interpreter/runtime/data.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type data
type t = data

val alloc : string -> data
val size : data -> Memory.address
val load : data -> Memory.address -> char
val drop : data -> unit
7 changes: 7 additions & 0 deletions interpreter/runtime/elem.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type elem = Values.ref_ list ref
type t = elem

let alloc rs = ref rs
let size seg = Lib.List32.length !seg
let load seg i = Lib.List32.nth !seg i
let drop seg = seg := []
9 changes: 9 additions & 0 deletions interpreter/runtime/elem.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
open Values

type elem
type t = elem

val alloc : ref_ list -> elem
val size : elem -> Table.size
val load : elem -> Table.index -> ref_
val drop : elem -> unit
8 changes: 4 additions & 4 deletions interpreter/runtime/instance.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ type module_inst =
tables : table_inst list;
memories : memory_inst list;
globals : global_inst list;
exports : export_inst list;
elems : elem_inst list;
datas : data_inst list;
exports : export_inst list;
}

and func_inst = module_inst ref Func.t
and table_inst = Table.t
and memory_inst = Memory.t
and global_inst = Global.t
and elem_inst = Elem.t
and data_inst = Data.t
and export_inst = Ast.name * extern
and elem_inst = Values.ref_ list ref
and data_inst = string ref

and extern =
| ExternFunc of func_inst
Expand Down Expand Up @@ -55,7 +55,7 @@ let () =

let empty_module_inst =
{ types = []; funcs = []; tables = []; memories = []; globals = [];
exports = []; elems = []; datas = [] }
elems = []; datas = []; exports = [] }

let extern_type_of = function
| ExternFunc func -> ExternFuncType (Func.type_of func)
Expand Down

0 comments on commit 2cdd5ac

Please sign in to comment.