Skip to content

Commit 74c1cfc

Browse files
Replace resize_memory, memory_size, and page_size with functions imported from a "wasm_intrinsics" module.
1 parent accca7a commit 74c1cfc

12 files changed

Lines changed: 58 additions & 50 deletions

File tree

‎ml-proto/src/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ INCLUDES = -I host -I given -I spec
77
MODULES = \
88
host/params host/flags given/lib given/source given/float32 given/float64 spec/error \
99
spec/types spec/values spec/memory spec/ast \
10-
spec/check spec/arithmetic spec/eval \
10+
spec/check spec/arithmetic spec/eval spec/intrinsics \
1111
host/print host/builtins host/script \
1212
host/lexer host/parser \
1313
host/main

‎ml-proto/src/host/builtins.ml‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
open Source
22

3-
let print vs =
3+
let print host mem at vs =
44
List.iter Print.print_value (List.map (fun v -> Some v) vs);
55
None
66

77
let match_import i =
88
let {Ast.module_name; func_name; func_params; func_result} = i.it in
9-
if module_name <> "stdio" then
10-
Error.error i.at ("no builtin module \"" ^ module_name ^ "\"");
11-
match func_name with
12-
| "print" ->
9+
match module_name, func_name with
10+
| "stdio", "print" ->
1311
if func_result <> None then
1412
Error.error i.at "stdio.print has no result";
1513
print
14+
| "wasm_intrinsics", _ ->
15+
Intrinsics.match_import i
1616
| _ ->
17-
Error.error i.at ("no stdio." ^ func_name ^ "\"")
17+
Error.error i.at ("no import \"" ^ func_name ^ "\" from module \"" ^ module_name ^ "\"")
1818

1919
let match_imports (is : Ast.import list) =
2020
List.map match_import is

‎ml-proto/src/host/builtins.mli‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
val match_imports : Ast.import list -> Eval.import list
1+
val match_imports : Ast.import list -> Eval.import list

‎ml-proto/src/host/lexer.mll‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,6 @@ rule token = parse
232232
| "i32.reinterpret/f32" { CONVERT (Values.Int32 Int32Op.ReinterpretFloat) }
233233
| "i64.reinterpret/f64" { CONVERT (Values.Int64 Int64Op.ReinterpretFloat) }
234234

235-
| "page_size" { PAGESIZE }
236-
| "memory_size" { MEMORYSIZE }
237-
| "resize_memory" { RESIZEMEMORY }
238-
239235
| "func" { FUNC }
240236
| "param" { PARAM }
241237
| "result" { RESULT }

‎ml-proto/src/host/parser.mly‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ let anon_label c = {c with labels = VarMap.map ((+) 1) c.labels}
9898
%token GETLOCAL SETLOCAL LOAD STORE
9999
%token CONST UNARY BINARY COMPARE CONVERT
100100
%token FUNC PARAM RESULT LOCAL MODULE MEMORY SEGMENT IMPORT EXPORT TABLE
101-
%token PAGESIZE MEMORYSIZE RESIZEMEMORY
102101
%token ASSERTINVALID ASSERTEQ ASSERTFAULT INVOKE
103102
%token EOF
104103

@@ -184,9 +183,6 @@ oper :
184183
| BINARY expr expr { fun c -> Binary ($1, $2 c, $3 c) }
185184
| COMPARE expr expr { fun c -> Compare ($1, $2 c, $3 c) }
186185
| CONVERT expr { fun c -> Convert ($1, $2 c) }
187-
| PAGESIZE { fun c -> PageSize }
188-
| MEMORYSIZE { fun c -> MemorySize }
189-
| RESIZEMEMORY expr { fun c -> ResizeMemory ($2 c) }
190186
;
191187
expr_opt :
192188
| /* empty */ { fun c -> None }

‎ml-proto/src/spec/ast.ml‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,6 @@ and expr' =
9494
| Binary of binop * expr * expr
9595
| Compare of relop * expr * expr
9696
| Convert of cvt * expr
97-
| PageSize
98-
| MemorySize
99-
| ResizeMemory of expr
10097

10198
and arm = arm' Source.phrase
10299
and arm' =

‎ml-proto/src/spec/check.ml‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -218,16 +218,6 @@ let rec check_expr c et e =
218218
check_expr c (Some t1) e1;
219219
check_type (Some t) et e.at
220220

221-
| PageSize ->
222-
check_type (Some Int32Type) et e.at
223-
224-
| MemorySize ->
225-
check_type (Some Int32Type) et e.at
226-
227-
| ResizeMemory e ->
228-
check_expr c (Some Int32Type) e;
229-
check_type None et e.at
230-
231221
and check_exprs c ts es =
232222
let ets = List.map (fun x -> Some x) ts in
233223
try List.iter2 (check_expr c) ets es

‎ml-proto/src/spec/eval.ml‎

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ let error = Error.error
1313

1414
type value = Values.value
1515
type func = Ast.func
16-
type import = value list -> value option
1716
type host_params = {page_size : Memory.size}
17+
type import = Memory.t -> host_params -> Source.region -> value list -> value option
18+
type bound_import = Source.region -> value list -> value option
1819

1920
module ExportMap = Map.Make(String)
2021
type export_map = func ExportMap.t
2122

2223
type instance =
2324
{
2425
funcs : func list;
25-
imports : import list;
26+
imports : bound_import list;
2627
exports : export_map;
2728
tables : func list list;
2829
memory : Memory.t;
@@ -140,7 +141,7 @@ let rec eval_expr (c : config) (e : expr) =
140141

141142
| CallImport (x, es) ->
142143
let vs = List.map (fun ev -> some (eval_expr c ev) ev.at) es in
143-
(import c x) vs
144+
(import c x) e.at vs
144145

145146
| CallIndirect (x, e1, es) ->
146147
let i = int32 (eval_expr c e1) e1.at in
@@ -199,19 +200,6 @@ let rec eval_expr (c : config) (e : expr) =
199200
(try Some (Arithmetic.eval_cvt cvt v1)
200201
with Arithmetic.TypeError (_, v, t) -> type_error e1.at v t)
201202

202-
| PageSize ->
203-
Some (Int32 (page_size c))
204-
205-
| MemorySize ->
206-
Some (Int32 (Int32.of_int (Memory.size c.modul.memory)))
207-
208-
| ResizeMemory e ->
209-
let i = int32 (eval_expr c e) e.at in
210-
if (Int32.rem i (page_size c)) <> Int32.zero then
211-
error e.at "runtime: resize_memory operand not multiple of page_size";
212-
Memory.resize c.modul.memory (Int32.to_int i);
213-
None
214-
215203
and eval_expr_option c eo =
216204
match eo with
217205
| Some e -> eval_expr c e
@@ -248,8 +236,8 @@ let init_memory ast =
248236
Memory.init mem (List.map it segments);
249237
mem
250238

251-
let init m imports host =
252-
assert (List.length imports = List.length m.it.Ast.imports);
239+
let init m unbound_imports host =
240+
assert (List.length unbound_imports = List.length m.it.Ast.imports);
253241
assert (host.page_size > 0);
254242
assert (Lib.Int.is_power_of_two host.page_size);
255243
let {Ast.exports; tables; funcs; memory; _} = m.it in
@@ -258,6 +246,7 @@ let init m imports host =
258246
let export ex = ExportMap.add ex.it.name (func ex.it.func) in
259247
let exports = List.fold_right export exports ExportMap.empty in
260248
let tables = List.map (fun tab -> List.map func tab.it) tables in
249+
let imports = List.map (fun imp -> imp mem host) unbound_imports in
261250
{funcs; imports; exports; tables; memory = mem; host}
262251

263252
let invoke m name vs =

‎ml-proto/src/spec/eval.mli‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
type instance
66
type value = Values.value
7-
type import = value list -> value option
87
type host_params = {page_size : Memory.size}
8+
type import = Memory.t -> host_params -> Source.region -> value list -> value option
99

1010
val init : Ast.modul -> import list -> host_params -> instance
1111
val invoke : instance -> string -> value list -> value option

‎ml-proto/src/spec/intrinsics.ml‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
open Source
2+
open Values
3+
open Eval
4+
open Types
5+
6+
let memory_size mem host at _ =
7+
Some (Int32 (Int32.of_int (Memory.size mem)))
8+
9+
let page_size mem host at _ =
10+
Some (Int32 (Int32.of_int host.page_size))
11+
12+
let resize_memory mem host at vs =
13+
match vs with
14+
| Int32 i :: [] ->
15+
if (Int32.rem i (Int32.of_int host.page_size)) <> Int32.zero then
16+
Error.error at "runtime: resize_memory operand not multiple of page_size";
17+
Memory.resize mem (Int32.to_int i);
18+
None
19+
| _ ->
20+
Error.error at "runtime: resize_memory expects a single i32 parameter"
21+
22+
let match_import (i : Ast.import) =
23+
let {Ast.module_name; func_name; func_params; func_result} = i.it in
24+
let (result : Eval.import) = match module_name, func_name, func_result, func_params with
25+
| "wasm_intrinsics", "memory_size", Some { at = _; it = Int32Type}, [] -> memory_size
26+
| "wasm_intrinsics", "page_size", Some { at = _; it = Int32Type}, [] -> memory_size
27+
| "wasm_intrinsics", "resize_memory", None, { at = _; it = Int32Type} :: [] -> resize_memory
28+
| _, _, _, _ ->
29+
let func_params_it = List.map it func_params in
30+
let func_result_it = match func_result with
31+
| Some t -> Some t.it
32+
| None -> None
33+
in let func_type = { ins=func_params_it; out=func_result_it } in
34+
Error.error i.at ("no intrinsic \"" ^ func_name ^ "\" of type " ^ (string_of_func_type func_type))
35+
in result

0 commit comments

Comments
 (0)