Skip to content

Commit 1efa4d6

Browse files
authored
Implement 1a (WebAssembly#63)
1 parent b196330 commit 1efa4d6

File tree

17 files changed

+290
-280
lines changed

17 files changed

+290
-280
lines changed

interpreter/binary/decode.ml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,7 @@ let rec instr s =
323323

324324
| 0x16 as b -> illegal s pos b
325325

326-
| 0x17 ->
327-
let bt = block_type s in
328-
let locs = locals s in
329-
let es = instr_block s in
330-
end_ s;
331-
let_ bt locs es
332-
333-
| 0x18 | 0x19 as b -> illegal s pos b
326+
| 0x17 | 0x18 | 0x19 as b -> illegal s pos b
334327

335328
| 0x1a -> drop
336329
| 0x1b -> select None

interpreter/binary/encode.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,6 @@ struct
189189
op 0x04; block_type bt; list instr es1;
190190
if es2 <> [] then op 0x05;
191191
list instr es2; end_ ()
192-
| Let (bt, locs, es) ->
193-
op 0x17; block_type bt; locals locs; list instr es; end_ ()
194192

195193
| Br x -> op 0x0c; var x
196194
| BrIf x -> op 0x0d; var x

interpreter/exec/eval.ml

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type 'a stack = 'a list
4949
type frame =
5050
{
5151
inst : module_inst;
52-
locals : value ref list;
52+
locals : value option ref list;
5353
}
5454

5555
type code = value stack * admin_instr list
@@ -64,7 +64,6 @@ and admin_instr' =
6464
| ReturningInvoke of value stack * func_inst
6565
| Breaking of int32 * value stack
6666
| Label of int * instr list * code
67-
| Local of int * value list * code
6867
| Frame of int * frame * code
6968

7069
type config =
@@ -74,8 +73,8 @@ type config =
7473
budget : int; (* to model stack overflow *)
7574
}
7675

77-
let frame inst = {inst; locals = []}
78-
let config inst vs es = {frame = frame inst; code = vs, es; budget = 300}
76+
let frame inst locals = {inst; locals}
77+
let config inst vs es = {frame = frame inst []; code = vs, es; budget = 300}
7978

8079
let plain e = Plain e.it @@ e.at
8180

@@ -184,16 +183,6 @@ let rec step (c : config) : config =
184183
else
185184
vs', [Plain (Block (bt, es1)) @@ e.at]
186185

187-
| Let (bt, locals, es'), vs ->
188-
let vs0, vs' = split (List.length locals) vs e.at in
189-
let FuncType (ts1, ts2) = block_type c.frame.inst bt e.at in
190-
let vs1, vs2 = split (List.length ts1) vs' e.at in
191-
vs2, [
192-
Local (List.length ts2, List.rev vs0,
193-
(vs1, [Plain (Block (bt, es')) @@ e.at])
194-
) @@ e.at
195-
]
196-
197186
| Br x, vs ->
198187
[], [Breaking (x.it, vs) @@ e.at]
199188

@@ -266,14 +255,19 @@ let rec step (c : config) : config =
266255
v1 :: vs', []
267256

268257
| LocalGet x, vs ->
269-
!(local c.frame x) :: vs, []
258+
(match !(local c.frame x) with
259+
| Some v ->
260+
v :: vs, []
261+
| None ->
262+
Crash.error e.at "read of uninitialized local"
263+
)
270264

271265
| LocalSet x, v :: vs' ->
272-
local c.frame x := v;
266+
local c.frame x := Some v;
273267
vs', []
274268

275269
| LocalTee x, v :: vs' ->
276-
local c.frame x := v;
270+
local c.frame x := Some v;
277271
v :: vs', []
278272

279273
| GlobalGet x, vs ->
@@ -676,18 +670,6 @@ let rec step (c : config) : config =
676670
let c' = step {c with code = code'} in
677671
vs, [Label (n, es0, c'.code) @@ e.at]
678672

679-
| Local (n, vs0, (vs', [])), vs ->
680-
vs' @ vs, []
681-
682-
| Local (n, vs0, (vs', e' :: es')), vs when is_jumping e' ->
683-
vs' @ vs, [e']
684-
685-
| Local (n, vs0, code'), vs ->
686-
let frame' = {c.frame with locals = List.map ref vs0 @ c.frame.locals} in
687-
let c' = step {c with frame = frame'; code = code'} in
688-
let vs0' = List.map (!) (take (List.length vs0) c'.frame.locals e.at) in
689-
vs, [Local (n, vs0', c'.code) @@ e.at]
690-
691673
| Frame (n, frame', (vs', [])), vs ->
692674
vs' @ vs, []
693675

@@ -710,17 +692,17 @@ let rec step (c : config) : config =
710692

711693
| Invoke f, vs ->
712694
let FuncType (ts1, ts2) = Func.type_of f in
713-
let args, vs' = split (List.length ts1) vs e.at in
695+
let n1, n2 = List.length ts1, List.length ts2 in
696+
let args, vs' = split n1 vs e.at in
714697
(match f with
715698
| Func.AstFunc (_, inst', func) ->
716699
let {locals; body; _} = func.it in
717700
let m = Lib.Promise.value inst' in
718701
let ts = List.map (fun t -> Types.sem_value_type m.types t.it) locals in
719-
let vs0 = List.rev args @ List.map default_value ts in
720-
let locals' = List.map (fun t -> t @@ func.at) ts1 @ locals in
721-
let bt = VarBlockType (SemVar (alloc (FuncDefType (FuncType ([], ts2))))) in
722-
let es0 = [Plain (Let (bt, locals', body)) @@ func.at] in
723-
vs', [Frame (List.length ts2, frame m, (List.rev vs0, es0)) @@ e.at]
702+
let locals' = List.(rev (map Option.some args) @ map default_value ts) in
703+
let frame' = {inst = m; locals = List.map ref locals'} in
704+
let instr' = [Label (n2, [], ([], List.map plain body)) @@ func.at] in
705+
vs', [Frame (n2, frame', ([], instr')) @@ e.at]
724706

725707
| Func.HostFunc (_, f) ->
726708
(try List.rev (f (List.rev args)) @ vs', []

interpreter/host/spectest.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ let print_value v =
2929
Printf.printf "%s : %s\n"
3030
(string_of_value v) (string_of_value_type (type_of_value v))
3131

32-
let print (FuncType (_, out)) vs =
32+
let print _ vs =
3333
List.iter print_value vs;
3434
flush_all ();
35-
List.map default_value out
35+
[]
3636

3737

3838
let lookup name t =

interpreter/runtime/value.ml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,22 +133,22 @@ let eq v1 v2 =
133133
(* Defaults *)
134134

135135
let default_num = function
136-
| I32Type -> I32 I32.zero
137-
| I64Type -> I64 I64.zero
138-
| F32Type -> F32 F32.zero
139-
| F64Type -> F64 F64.zero
136+
| I32Type -> Some (Num (I32 I32.zero))
137+
| I64Type -> Some (Num (I64 I64.zero))
138+
| F32Type -> Some (Num (F32 F32.zero))
139+
| F64Type -> Some (Num (F64 F64.zero))
140140

141141
let default_vec = function
142-
| V128Type -> V128 V128.zero
142+
| V128Type -> Some (Vec (V128 V128.zero))
143143

144144
let default_ref = function
145-
| (Nullable, t) -> NullRef t
146-
| (NonNullable, _) -> assert false
145+
| (Nullable, t) -> Some (Ref (NullRef t))
146+
| (NonNullable, _) -> None
147147

148148
let default_value = function
149-
| NumType t' -> Num (default_num t')
150-
| VecType t' -> Vec (default_vec t')
151-
| RefType t' -> Ref (default_ref t')
149+
| NumType t' -> default_num t'
150+
| VecType t' -> default_vec t'
151+
| RefType t' -> default_ref t'
152152
| BotType -> assert false
153153

154154

interpreter/syntax/ast.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ and instr' =
148148
| Block of block_type * instr list (* execute in sequence *)
149149
| Loop of block_type * instr list (* loop header *)
150150
| If of block_type * instr list * instr list (* conditional *)
151-
| Let of block_type * local list * instr list (* local bindings *)
152151
| Br of idx (* break to n-th surrounding label *)
153152
| BrIf of idx (* conditional break *)
154153
| BrTable of idx list * idx (* indexed break *)

interpreter/syntax/free.ml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,6 @@ let rec instr (e : instr) =
108108
| Const _ | Test _ | Compare _ | Unary _ | Binary _ | Convert _ -> empty
109109
| Block (bt, es) | Loop (bt, es) -> block_type bt ++ block es
110110
| If (bt, es1, es2) -> block_type bt ++ block es1 ++ block es2
111-
| Let (bt, ts, es) ->
112-
let free = block_type bt ++ block es in
113-
{free with locals = Lib.Fun.repeat (List.length ts) shift free.locals}
114111
| Br x | BrIf x | BrOnNull x | BrOnNonNull x -> labels (idx x)
115112
| BrTable (xs, x) -> list (fun x -> labels (idx x)) (x::xs)
116113
| Return | CallRef | ReturnCallRef -> empty

interpreter/syntax/operators.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ let select t = Select t
2121
let block bt es = Block (bt, es)
2222
let loop bt es = Loop (bt, es)
2323
let if_ bt es1 es2 = If (bt, es1, es2)
24-
let let_ bt ts es = Let (bt, ts, es)
2524

2625
let br x = Br x
2726
let br_if x = BrIf x

interpreter/syntax/types.ml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
(* Types *)
22

3-
type name = int list
3+
type type_idx = int32
4+
type local_idx = int32
5+
type name = Utf8.unicode
46

5-
and syn_var = int32
7+
and syn_var = type_idx
68
and sem_var = def_type Lib.Promise.t
79
and var = SynVar of syn_var | SemVar of sem_var
810

11+
and init = Initialized | Uninitialized
912
and nullability = NonNullable | Nullable
1013
and num_type = I32Type | I64Type | F32Type | F64Type
1114
and vec_type = V128Type
@@ -16,6 +19,7 @@ and value_type =
1619
NumType of num_type | VecType of vec_type | RefType of ref_type | BotType
1720

1821
and result_type = value_type list
22+
and instr_type = result_type * result_type * local_idx list
1923
and func_type = FuncType of result_type * result_type
2024
and def_type = FuncDefType of func_type
2125

@@ -24,6 +28,7 @@ type mutability = Immutable | Mutable
2428
type table_type = TableType of Int32.t limits * ref_type
2529
type memory_type = MemoryType of Int32.t limits
2630
type global_type = GlobalType of value_type * mutability
31+
type local_type = LocalType of value_type * init
2732
type extern_type =
2833
| ExternFuncType of func_type
2934
| ExternTableType of table_type
@@ -207,10 +212,12 @@ let string_of_name n =
207212
List.iter escape n;
208213
Buffer.contents b
209214

215+
let string_of_idx x = I32.to_string_u x
216+
210217
let rec string_of_var =
211218
let inner = ref false in
212219
function
213-
| SynVar x -> I32.to_string_u x
220+
| SynVar x -> string_of_idx x
214221
| SemVar x ->
215222
if !inner then "..." else
216223
( inner := true;

interpreter/text/arrange.ml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,6 @@ let rec instr e =
448448
| If (bt, es1, es2) ->
449449
"if", block_type bt @
450450
[Node ("then", list instr es1); Node ("else", list instr es2)]
451-
| Let (bt, locals, es) ->
452-
"let", block_type bt @ decls "local" (List.map Source.it locals) @
453-
list instr es
454451
| Br x -> "br " ^ var x, []
455452
| BrIf x -> "br_if " ^ var x, []
456453
| BrTable (xs, x) ->

0 commit comments

Comments
 (0)