Skip to content

Commit 4e2a1bb

Browse files
zilincrossberg
andauthored
Translating thread and wait to js and test harness for multi-threaded programs (#229)
* [interpreter] Fix regsitry to be per-thread * Rebuild either patterns; fix tests * [test/js] use newer version of testharness * [interpreter] remove bogus unittest in Makefile * [interpreter] fix match_result definition in js generation * [test/js] thread/wait in js generation and tests --------- Co-authored-by: Andreas Rossberg <rossberg@mpi-sws.org>
1 parent 3635ca5 commit 4e2a1bb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+3877
-856
lines changed

interpreter/Makefile

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This Makefile uses dune but does not rely on ocamlfind or the Opam
22
# package manager to build. However, Opam package management is available
3-
# optionally through the check/install/uninstall targets.
3+
# optionally through the install target.
44
#
55
# The $(JSLIB).js target requires Js_of_ocaml (using ocamlfind).
66
#
@@ -9,80 +9,77 @@
99

1010
# Configuration
1111

12-
NAME = wasm
13-
OPT = $(NAME).exe
14-
ZIP = $(NAME).zip
12+
NAME = wasm
13+
LIB = $(NAME)
1514
JSLIB = wast.js
15+
ZIP = $(NAME).zip
1616

17-
BUILDDIR = _build/default
17+
BUILDDIR = _build/default
1818

1919
JS = # set to JS shell command to run JS tests, empty to skip
2020

2121

2222
# Main targets
2323

24-
.PHONY: default opt jslib all zip smallint
24+
.PHONY: default all ci jslib zip
25+
26+
default: $(NAME)
27+
all: default partest
28+
ci: all jslib zip
2529

26-
default: $(OPT)
2730
jslib: $(JSLIB)
28-
all: $(OPT) test
2931
zip: $(ZIP)
30-
smallint: smallint.exe
31-
ci: all jslib
3232

33-
# Building executable
34-
.PHONY: $(NAME).exe
35-
$(NAME).exe:
36-
rm -f $(NAME)
37-
dune build $@
38-
cp $(BUILDDIR)/$(OPT) $(NAME)
3933

40-
.PHONY: smallint.exe
41-
smallint.exe:
42-
dune build $@
34+
# Building
4335

44-
# Building JavaScript library
36+
.PHONY: $(NAME) $(JSLIB)
4537

46-
$(JSLIB): $(BUILDDIR)/$(JSLIB)
47-
cp $< $@
38+
$(NAME):
39+
rm -f $@
40+
dune build $@.exe
41+
ln $(BUILDDIR)/$@.exe $@
4842

49-
$(BUILDDIR)/$(JSLIB):
50-
dune build $(JSLIB)
43+
$(JSLIB):
44+
rm -f $@
45+
dune build $(@:%.js=%.bc.js)
46+
ln $(BUILDDIR)/$(@:%.js=%.bc.js) $@
5147

52-
# Executing test suite
48+
49+
# Test suite
5350

5451
TESTDIR = ../test/core
55-
# Skip _output directory, since that's a tmp directory, and list all other wast files.
56-
TESTFILES = $(shell cd $(TESTDIR); ls *.wast; ls [a-z]*/*.wast)
52+
TESTFILES = $(shell cd $(TESTDIR) > /dev/null; ls *.wast; ls [a-z]*/*.wast)
5753
TESTS = $(TESTFILES:%.wast=%)
5854

59-
.PHONY: test partest dune-test
55+
.PHONY: test partest quiettest
56+
57+
test: $(NAME)
58+
$(TESTDIR)/run.py --wasm `pwd`/$(NAME) $(if $(JS),--js '$(JS)',)
59+
60+
test/%: $(NAME)
61+
$(TESTDIR)/run.py --wasm `pwd`/$(NAME) $(if $(JS),--js '$(JS)',) $(TESTDIR)/$*.wast
6062

61-
test: $(OPT) smallint
62-
$(TESTDIR)/run.py --wasm `pwd`/$(BUILDDIR)/$(OPT) $(if $(JS),--js '$(JS)',)
63-
dune exec ./smallint.exe
63+
run/%: $(NAME)
64+
./$(NAME) $(TESTDIR)/$*.wast
6465

65-
test/%: $(OPT)
66-
$(TESTDIR)/run.py --wasm `pwd`/$(BUILDDIR)/$(OPT) $(if $(JS),--js '$(JS)',) $(TESTDIR)/$*.wast
66+
partest: $(NAME)
67+
make -j10 quiettest
6768

68-
run/%: $(OPT)
69-
./$(OPT) $(TESTDIR)/$*.wast
69+
quiettest: $(TESTS:%=quiettest/%)
70+
@echo All tests passed.
7071

71-
partest: $(TESTS:%=quiettest/%)
72-
@echo All tests passed.
72+
quiettest/%: $(NAME)
73+
@ ( \
74+
$(TESTDIR)/run.py 2>$(@F).out --wasm `pwd`/$(NAME) $(if $(JS),--js '$(JS)',) $(TESTDIR)/$*.wast && \
75+
rm $(@F).out \
76+
) || \
77+
(cat $(@F).out && rm $(@F).out && exit 1)
7378

74-
quiettest/%: $(OPT)
75-
@ ( \
76-
$(TESTDIR)/run.py 2>$(@F).out --wasm `pwd`/$(BUILDDIR)/$(OPT) $(if $(JS),--js '$(JS)',) $(TESTDIR)/$*.wast && \
77-
rm $(@F).out \
78-
) || \
79-
cat $(@F).out || rm $(@F).out || exit 1
8079

81-
smallinttest: smallint
82-
dune exec ./smallint.exe
80+
# Packaging
8381

84-
dunetest:
85-
dune test
82+
.PHONY: install
8683

8784
install:
8885
dune build -p $(NAME) @install
@@ -101,15 +98,16 @@ opam-release/%:
10198
rm opam-$*.zip
10299
@echo Created file ./opam, submit to github opam-repository/packages/wasm/wasm.$*/opam
103100

104-
# Miscellaneous targets
105-
106-
.PHONY: clean
107-
108101
$(ZIP):
109102
git archive --format=zip --prefix=$(NAME)/ -o $@ HEAD
110103

104+
105+
# Cleanup
106+
107+
.PHONY: clean distclean
108+
111109
clean:
112110
dune clean
113111

114112
distclean: clean
115-
rm -f $(NAME) $(JSLIB)
113+
rm -f $(NAME) $(JSLIB) $(ZIP)

interpreter/binary/encode.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ struct
5959
if -64L <= i && i < 64L then byte b
6060
else (byte (b lor 0x80); s64 (Int64.shift_right i 7))
6161

62-
let u1 i = u64 Int64.(logand (of_int i) 1L)
6362
let u32 i = u64 Int64.(logand (of_int32 i) 0xffffffffL)
6463
let s7 i = s64 (Int64.of_int i)
6564
let s32 i = s64 (Int64.of_int32 i)

interpreter/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
(env
5555
(_
5656
(flags
57-
(-w +a-4-27-42-44-45-70 -warn-error +a-3))))
57+
(-w +a-4-27-42-44-45-70 -warn-error +a-3-32))))
5858

5959
(rule
6060
(alias runtest)

interpreter/host/env.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ let exit vs =
4141

4242
let lookup name t =
4343
match Utf8.encode name, t with
44-
| "abort", ExternFuncType t -> ExternFunc (Func.alloc_host t abort)
45-
| "exit", ExternFuncType t -> ExternFunc (Func.alloc_host t exit)
46-
| _ -> raise Not_found
44+
| "abort", ExternFuncType t -> Some (ExternFunc (Func.alloc_host t abort))
45+
| "exit", ExternFuncType t -> Some (ExternFunc (Func.alloc_host t exit))
46+
| _ -> None

interpreter/host/spectest.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ let print (FuncType (_, out)) vs =
3737
List.map default_value out
3838

3939

40-
let lookup name t =
40+
let lookup' name t =
4141
match Utf8.encode name, t with
4242
| "print", _ -> ExternFunc (func print (FuncType ([], [])))
4343
| "print_i32", _ -> ExternFunc (func print (FuncType ([NumType I32Type], [])))
@@ -55,3 +55,6 @@ let lookup name t =
5555
| "table", _ -> ExternTable table
5656
| "memory", _ -> ExternMemory memory
5757
| _ -> raise Not_found
58+
59+
let lookup name t =
60+
try Some (lookup' name t) with Not_found -> None

interpreter/main/main.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ let name = "wasm"
22
let version = "2.0.1"
33

44
let configure () =
5-
Import.register (Utf8.decode "spectest") Spectest.lookup;
6-
Import.register (Utf8.decode "env") Env.lookup
5+
Import.register_global (Utf8.decode "spectest") Spectest.lookup;
6+
Import.register_global (Utf8.decode "env") Env.lookup
77

88
let banner () =
99
print_endline (name ^ " " ^ version ^ " reference interpreter")

interpreter/script/import.ml

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
open Source
2+
open Types
23
open Ast
34

45
module Unknown = Error.Make ()
56
exception Unknown = Unknown.Error (* indicates unknown import name *)
67

78
module Registry = Map.Make(struct type t = Ast.name let compare = compare end)
8-
let registry = ref Registry.empty
99

10-
let register name lookup = registry := Registry.add name lookup !registry
10+
type registry = (name -> extern_type -> Instance.extern option) Registry.t ref
1111

12-
let lookup (m : module_) (im : import) : Instance.extern =
12+
type lookup_export = name -> extern_type -> Instance.extern option
13+
14+
15+
let global : registry = ref Registry.empty
16+
let registry () : registry = ref !global
17+
18+
let register r name lookup = r := Registry.add name lookup !r
19+
let register_global name lookup = register global name lookup
20+
21+
let lookup r module_name item_name et : Instance.extern option =
22+
match Registry.find_opt module_name !r with
23+
| Some f -> f item_name et
24+
| None -> None
25+
26+
let lookup_import r (m : module_) (im : import) : Instance.extern =
1327
let {module_name; item_name; idesc} = im.it in
14-
let t = import_type m im in
15-
try Registry.find module_name !registry item_name t with Not_found ->
16-
Unknown.error im.at
17-
("unknown import \"" ^ string_of_name module_name ^
18-
"\".\"" ^ string_of_name item_name ^ "\"")
28+
let et = import_type m im in
29+
match lookup r module_name item_name et with
30+
| Some ext -> ext
31+
| None ->
32+
Unknown.error im.at
33+
("unknown import \"" ^ string_of_name module_name ^
34+
"\".\"" ^ string_of_name item_name ^ "\"")
1935

20-
let link m = List.map (lookup m) m.it.imports
36+
let link r m = List.map (lookup_import r m) m.it.imports

interpreter/script/import.mli

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
open Types
2+
open Ast
3+
4+
type registry
5+
16
exception Unknown of Source.region * string
27

3-
val link : Ast.module_ -> Instance.extern list (* raises Unknown *)
8+
val registry : unit -> registry
9+
10+
val lookup : registry -> name -> name -> extern_type -> Instance.extern option
11+
val link : registry -> module_ -> Instance.extern list (* raises Unknown *)
12+
13+
14+
type lookup_export = name -> extern_type -> Instance.extern option
415

5-
val register :
6-
Ast.name ->
7-
(Ast.name -> Types.extern_type -> Instance.extern (* raises Not_found *)) ->
8-
unit
16+
val register : registry -> name -> lookup_export -> unit
17+
val register_global : name -> lookup_export -> unit

0 commit comments

Comments
 (0)