Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[interpreter] Add dune support to build the #1408

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: ocaml/setup-ocaml@v2
with:
ocaml-compiler: 4.12.x
- run: opam install --yes ocamlbuild.0.14.0
- run: opam install --yes dune
- run: cd interpreter && opam exec make all

build-js-api-spec:
Expand Down Expand Up @@ -51,7 +51,7 @@ jobs:
submodules: "recursive"
- run: pip install bikeshed && bikeshed update
- run: pip install six
- run: sudo apt-get update -y && sudo apt-get install -y latexmk texlive-latex-recommended texlive-latex-extra texlive-fonts-recommended
- run: sudo apt update -y && sudo apt install -y latexmk texlive-latex-recommended texlive-latex-extra texlive-fonts-recommended
- run: pip install sphinx==4.0.0
- run: cd document/core && make all
- uses: actions/upload-artifact@v2
Expand Down
203 changes: 45 additions & 158 deletions interpreter/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,197 +2,84 @@
# package manager to build. However, Opam package management is available
# optionally through the check/install/uninstall targets.
#
# The $(JSLIB) target requires node.js and BuckleScript.
# The wast.js target requires node.js and BuckleScript.
#
# See README.me for instructions.


# Configuration

NAME = wasm
UNOPT = $(NAME).debug
OPT = $(NAME)
LIB = $(NAME)
ZIP = $(NAME).zip
JSLIB = wast.js
WINMAKE = winmake.bat

DIRS = util syntax binary text valid runtime exec script host main tests
LIBS = bigarray
FLAGS = -lexflags -ml -cflags '-w +a-4-27-42-44-45 -warn-error +a-3'
OCBA = ocamlbuild $(FLAGS) $(DIRS:%=-I %)
OCB = $(OCBA) $(LIBS:%=-libs %)
JS = # set to JS shell command to run JS tests

BUILDDIR = _build/default
UNOPT = $(BUILDDIR)/main/main.bc
OPT = $(BUILDDIR)/main/main.exe
JS = # set to JS shell command to run JS tests
TESTDIR = ../test/core
# Skip _output directory, since that's a tmp directory, and list all other wast files.
TESTFILES = $(shell cd $(TESTDIR); ls *.wast; ls [a-z]*/*.wast)

# Main targets

.PHONY: default opt unopt libopt libunopt jslib all land zip smallint

default: opt
debug: unopt
opt: $(OPT)
unopt: $(UNOPT)
libopt: _build/$(LIB).cmx _build/$(LIB).cmxa
libunopt: _build/$(LIB).cmo _build/$(LIB).cma
jslib: $(JSLIB)
all: unopt opt libunopt libopt test
land: $(WINMAKE) all
zip: $(ZIP)
smallint: smallint.native


# Building executable

empty =
space = $(empty) $(empty)
comma = ,

.INTERMEDIATE: _tags
_tags:
echo >$@ "true: bin_annot"
echo >>$@ "true: debug"
echo >>$@ "<{$(subst $(space),$(comma),$(DIRS))}/*.cmx>: for-pack($(PACK))"

$(UNOPT): main.byte
mv $< $@

$(OPT): main.native
mv $< $@

.PHONY: main.byte main.native
main.byte: _tags
$(OCB) -quiet $@

main.native: _tags
$(OCB) -quiet $@

.PHONY: smallint.byte smallint.native
smallint.byte: _tags
$(OCB) -quiet $@
smallint.native: _tags
$(OCB) -quiet $@


# Building library

FILES = $(shell ls $(DIRS:%=%/*) | grep '[.]ml[^.]*$$')
PACK = $(shell echo `echo $(LIB) | sed 's/^\(.\).*$$/\\1/g' | tr [:lower:] [:upper:]``echo $(LIB) | sed 's/^.\(.*\)$$/\\1/g'`)
.PHONY: default jslib all land zip

.INTERMEDIATE: $(LIB).mlpack
$(LIB).mlpack: $(DIRS)
ls $(FILES) \
| sed 's:\(.*/\)\{0,1\}\(.*\)\.[^\.]*:\2:' \
| grep -v main \
| sort | uniq \
>$@

.INTERMEDIATE: $(LIB).mllib
$(LIB).mllib:
echo Wasm >$@

_build/$(LIB).cmo: $(FILES) $(LIB).mlpack _tags Makefile
$(OCB) -quiet $(LIB).cmo

_build/$(LIB).cmx: $(FILES) $(LIB).mlpack _tags Makefile
$(OCB) -quiet $(LIB).cmx

_build/$(LIB).cma: $(FILES) $(LIB).mllib _tags Makefile
$(OCBA) -quiet $(LIB).cma

_build/$(LIB).cmxa: $(FILES) $(LIB).mllib _tags Makefile
$(OCBA) -quiet $(LIB).cmxa


# Building JavaScript library

.PHONY: $(JSLIB)
$(JSLIB): $(UNOPT)
mkdir -p _build/jslib/src
cp meta/jslib/* _build/jslib
cp $(DIRS:%=_build/%/*.ml*) meta/jslib/*.ml _build/jslib/src
rm _build/jslib/src/*.ml[^i]
(cd _build/jslib; ./build.sh ../../$@)


# Building Windows build file

$(WINMAKE): clean
echo rem Auto-generated from Makefile! >$@
echo set NAME=$(NAME) >>$@
echo if \'%1\' neq \'\' set NAME=%1 >>$@
$(OCB) main.byte \
| grep -v ocamldep \
| grep -v mkdir \
| sed s:`which ocaml`:ocaml:g \
| sed s:main/main.d.byte:%NAME%.exe: \
>>$@
default: $(OPT)
debug: $(UNOPT)
jslib: $(BUILDDIR)/jslib/wasm.bc.js
all: $(UNOPT) $(OPT) test
land: all
zip: wasm.zip

$(BUILDDIR)/%:
dune build $*

# Executing test suite

TESTDIR = ../test/core
# Skip _output directory, since that's a tmp directory, and list all other wast files.
TESTFILES = $(shell cd $(TESTDIR); ls *.wast; ls [a-z]*/*.wast)
TESTS = $(TESTFILES:%.wast=%)
.PHONY: test debugtest partest winmake.bat

.PHONY: test debugtest partest
winmake.bat:
dune clean
dune build main/main.bc
cat _build/log | grep -v "^#" | grep -v "ocamldep\.opt" | sed s:`which ocaml`:ocaml:g | sed s:_build/default:_build/:g | sed s:"cd _build/ &&"::g | sed s:".wasm.objs/"::g | sed s:"byte/"::g | sed s:"-I byte"::g | sed s:"^...."::g | sed s:")"::g | sed s:" ":" ":g

test: $(OPT) smallint
$(TESTDIR)/run.py --wasm `pwd`/$(OPT) $(if $(JS),--js '$(JS)',)
./smallint.native
debugtest: $(UNOPT) smallint
$(TESTDIR)/run.py --wasm `pwd`/$(UNOPT) $(if $(JS),--js '$(JS)',)
./smallint.native
test/%: $(OPT)
$(TESTDIR)/run.py --wasm `pwd`/$< $(if $(JS),--js '$(JS)',) $(TESTDIR)/$*.wast
debugtest/%: $(UNOPT)
$(TESTDIR)/run.py --wasm `pwd`/$< $(if $(JS),--js '$(JS)',) $(TESTDIR)/$*.wast

test/%: $(OPT)
$(TESTDIR)/run.py --wasm `pwd`/$(OPT) $(if $(JS),--js '$(JS)',) $(TESTDIR)/$*.wast
debugtest/%: $(UNOPT)
$(TESTDIR)/run.py --wasm `pwd`/$(UNOPT) $(if $(JS),--js '$(JS)',) $(TESTDIR)/$*.wast
test: $(OPT) smallinttest
$(TESTDIR)/run.py --wasm `pwd`/$< $(if $(JS),--js '$(JS)',)
debugtest: $(UNOPT) smallinttest
$(TESTDIR)/run.py --wasm `pwd`/$< $(if $(JS),--js '$(JS)',)

run/%: $(OPT)
./$(OPT) $(TESTDIR)/$*.wast
debug/%: $(UNOPT)
./$(UNOPT) $(TESTDIR)/$*.wast
run/%:
dune exec -- main/main.exe $(TESTDIR)/$*.wast
debug/%:
dune exec -- main/main.bc $(TESTDIR)/$*.wast

partest: $(TESTS:%=quiettest/%)
partest: $(TESTFILES:%.wast=quiettest/%)
@echo All tests passed.

quiettest/%: $(OPT)
quiettest/%: $(OPT)
@ ( \
$(TESTDIR)/run.py 2>$(@F).out --wasm `pwd`/$(OPT) $(if $(JS),--js '$(JS)',) $(TESTDIR)/$*.wast && \
$(TESTDIR)/run.py 2>$(@F).out --wasm `pwd`/$< $(if $(JS),--js '$(JS)',) $(TESTDIR)/$*.wast && \
rm $(@F).out \
) || \
cat $(@F).out || rm $(@F).out || exit 1

smallinttest: smallint
@./smallint.native
smallinttest:
dune runtest

# Miscellaneous targets

.PHONY: clean

$(ZIP): $(WINMAKE)
git archive --format=zip --prefix=$(NAME)/ -o $@ HEAD
wasm.zip:
git archive --format=zip --prefix=wasm/ -o $@ HEAD

clean:
rm -rf _build/jslib $(LIB).mlpack _tags
$(OCB) -clean

dune clean

# Opam support

.PHONY: check install uninstall

check:
# Check that we can find all relevant libraries
# when using ocamlfind
ocamlfind query $(LIBS)

install: _build/$(LIB).cmx _build/$(LIB).cmo
ocamlfind install $(LIB) meta/findlib/META _build/$(LIB).o \
$(wildcard _build/$(LIB).cm*) \
$(wildcard $(DIRS:%=%/*.mli))
install:
dune install

uninstall:
ocamlfind remove $(LIB)
dune uninstall
6 changes: 3 additions & 3 deletions interpreter/binary/decode.ml
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ let rec instr s =
| 0x0al -> let a, o = memop s in v128_load64_splat a o
| 0x0bl -> let a, o = memop s in v128_store a o
| 0x0cl -> v128_const (at v128 s)
| 0x0dl -> i8x16_shuffle (List.init 16 (fun x -> u8 s))
| 0x0dl -> i8x16_shuffle (List.init 16 (fun _x -> u8 s))
| 0x0el -> i8x16_swizzle
| 0x0fl -> i8x16_splat
| 0x10l -> i16x8_splat
Expand Down Expand Up @@ -925,7 +925,7 @@ let code_section s =

(* Element section *)

let passive s =
let passive _s =
Passive

let active s =
Expand All @@ -938,7 +938,7 @@ let active_zero s =
let offset = const s in
Active {index; offset}

let declarative s =
let declarative _s =
Declarative

let elem_index s =
Expand Down
4 changes: 2 additions & 2 deletions interpreter/binary/utf8.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let con n = 0x80 lor (n land 0x3f)
let rec encode ns = Lib.String.implode (List.map Char.chr (encode' ns))
and encode' = function
| [] -> []
| n::ns when n < 0 ->
| n::_ns when n < 0 ->
raise Utf8
| n::ns when n < 0x80 ->
n :: encode' ns
Expand All @@ -29,7 +29,7 @@ and decode' = function
| [] -> []
| b1::bs when b1 < 0x80 ->
code 0x0 b1 :: decode' bs
| b1::bs when b1 < 0xc0 ->
| b1::_bs when b1 < 0xc0 ->
raise Utf8
| b1::b2::bs when b1 < 0xe0 ->
code 0x80 ((b1 land 0x1f) lsl 6 + con b2) :: decode' bs
Expand Down
47 changes: 47 additions & 0 deletions interpreter/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
(copy_files#
(files binary/*.ml*))

(copy_files#
(files exec/*.ml*))

(copy_files#
(files host/*.ml*))

(copy_files#
(files main/flags.ml))

(copy_files#
(files runtime/*.ml*))

(copy_files#
(files script/*.ml*))

(copy_files#
(files syntax/*.ml*))

(copy_files#
(files text/*.ml*))

(copy_files#
(files util/*.ml*))

(copy_files#
(files valid/*.ml*))

(library
(public_name wasm)
(wrapped false))

; we don't use the `ocamllex` stanza directly as there's no way to pass the `-ml`
; if we don't pass `-ml` we have a transition table overflow...

(rule
(target lexer.ml)
(deps lexer.mll)
(action
(chdir
%{workspace_root}
(run %{bin:ocamllex} -q -ml -o %{target} %{deps}))))

(ocamlyacc
(modules parser))
26 changes: 26 additions & 0 deletions interpreter/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(lang dune 2.8)

(implicit_transitive_deps false)

(name wasm)

(license Apache-2.0)

(authors "TODO")

(maintainers "TODO")

(source
(github WebAssembly/spec))

(generate_opam_files true)

(package
(name wasm)
(synopsis "WebAssembly library")
(description "A library for writing/reading/running WebAssembly binaries.")
(tags
(wasm webassembly spec interpreter))
(depends
(ocaml
(>= 4.05))))
Loading