Skip to content

Commit 60facfb

Browse files
authored
[interpreter] Upgrade to OCaml 4.07 and refactor (#1191)
1 parent a7a1856 commit 60facfb

File tree

5 files changed

+31
-29
lines changed

5 files changed

+31
-29
lines changed

.travis.yml

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,21 @@ dist: bionic
66
addons:
77
apt:
88
sources:
9+
- sourceline: 'ppa:avsm/ppa'
910
- sourceline: 'deb https://dl.yarnpkg.com/debian/ stable main'
1011
key_url: 'https://dl.yarnpkg.com/debian/pubkey.gpg'
12+
update: true
1113
packages:
12-
- ocaml
13-
- ocamlbuild
14+
- opam
1415
- texlive-full
1516
- yarn
1617

1718
install:
19+
- opam init --auto-setup --compiler=4.07.1
20+
- eval $(opam env)
21+
- opam --version
22+
- ocaml --version
23+
- opam install --yes ocamlbuild.0.14.0
1824
- pip install Sphinx==2.4.4
1925
- git clone https://github.com/tabatkins/bikeshed.git
2026
- pip install --editable $PWD/bikeshed

interpreter/README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The text format defines modules in S-expression syntax. Moreover, it is generali
1515

1616
## Building
1717

18-
You'll need OCaml 4.02 or higher. An easy way to get this on Linux is to download the [source tarball from our mirror of the ocaml website](https://wasm.storage.googleapis.com/ocaml-4.02.2.tar.gz) and do the configure / make dance. On macOS, with [Homebrew](http://brew.sh/) installed, simply `brew install ocaml ocamlbuild`.
18+
You'll need OCaml 4.07 or higher. Instructions for installing a recent version of OCaml on multiple platforms are available [here](https://ocaml.org/docs/install.html). On most platforms, the recommended way is through [OPAM](https://ocaml.org/docs/install.html#OPAM).
1919

2020
Once you have OCaml, simply do
2121

@@ -43,17 +43,20 @@ That builds `all`, plus updates `winmake.bat`.
4343

4444
#### Building on Windows
4545

46-
We recommend a pre-built installer. With [this one](https://protz.github.io/ocaml-installer/) you have two options:
46+
The instructions depend on how you [installed OCaml on Windows](https://ocaml.org/docs/install.html#Windows).
4747

48-
1. Bare OCaml. If you just want to build the interpreter and don't care about modifying it, you don't need to install the Cygwin core that comes with the installer. Just install OCaml itself and run
48+
1. *Cygwin*: If you want to build a native code executable, or want to hack on the interpreter (i.e., use incremental compilation), then you need to install the Cygwin core that is included with the OCaml installer. Then you can build the interpreter using `make` in the Cygwin terminal, as described above.
49+
50+
2. *Windows Subsystem for Linux* (WSL): You can build the interpreter using `make`, as described above.
51+
52+
3. *From source*: If you just want to build the interpreter and don't care about modifying it, you don't need to install the Cygwin core that comes with the installer. Just install OCaml itself and run
4953
```
5054
winmake.bat
5155
```
5256
in a Windows shell, which creates a program named `wasm`. Note that this will be a byte code executable only, i.e., somewhat slower.
5357

54-
2. OCaml + Cygwin. If you want to build a native code executable, or want to hack on the interpreter (i.e., use incremental compilation), then you need to install the Cygwin core that is included with the OCaml installer. Then you can build the interpreter using `make` in the Cygwin terminal, as described above.
58+
In any way, in order to run the test suite you'll need to have Python installed. If you used Option 3, you can invoke the test runner `runtests.py` directly instead of doing it through `make`.
5559

56-
Either way, in order to run the test suite you'll need to have Python installed. If you used Option 1, you can invoke the test runner `runtests.py` directly instead of doing it through `make`.
5760

5861

5962
#### Cross-compiling the Interpreter to JavaScript ####

interpreter/exec/f32_convert.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ let convert_i32_u x =
2828
* Values that are too large would get rounded when represented in f64,
2929
* but double rounding via i64->f64->f32 can produce inaccurate results.
3030
* Hence, for large values we shift right but make sure to accumulate the lost
31-
* bits in the least signifant bit, such that rounding still is correct.
31+
* bits in the least significant bit, such that rounding still is correct.
3232
*)
3333
let convert_i64_s x =
3434
F32.of_float Int64.(
3535
if abs x < 0x10_0000_0000_0000L then to_float x else
3636
let r = if logand x 0xfffL = 0L then 0L else 1L in
37-
to_float (logor (shift_right x 12) r) *. (* TODO(ocaml-4.03): 0x1p12 *) 4096.0
37+
to_float (logor (shift_right x 12) r) *. 0x1p12
3838
)
3939

4040
let convert_i64_u x =
4141
F32.of_float Int64.(
4242
if I64.lt_u x 0x10_0000_0000_0000L then to_float x else
4343
let r = if logand x 0xfffL = 0L then 0L else 1L in
44-
to_float (logor (shift_right_logical x 12) r) *. (* TODO(ocaml-4.03): 0x1p12 *) 4096.0
44+
to_float (logor (shift_right_logical x 12) r) *. 0x1p12
4545
)
4646

4747
let reinterpret_i32 = F32.of_bits

interpreter/exec/float.ml

+10-17
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,17 @@ struct
138138
let mul x y = binary x ( *.) y
139139
let div x y = binary x (/.) y
140140

141-
let sqrt x = unary Pervasives.sqrt x
141+
let sqrt x = unary Stdlib.sqrt x
142142

143-
let ceil x = unary Pervasives.ceil x
144-
let floor x = unary Pervasives.floor x
143+
let ceil x = unary Stdlib.ceil x
144+
let floor x = unary Stdlib.floor x
145145

146146
let trunc x =
147147
let xf = to_float x in
148148
(* preserve the sign of zero *)
149149
if xf = 0.0 then x else
150150
(* trunc is either ceil or floor depending on which one is toward zero *)
151-
let f = if xf < 0.0 then Pervasives.ceil xf else Pervasives.floor xf in
151+
let f = if xf < 0.0 then Stdlib.ceil xf else Stdlib.floor xf in
152152
let result = of_float f in
153153
if is_nan result then determine_unary_nan result else result
154154

@@ -157,13 +157,13 @@ struct
157157
(* preserve the sign of zero *)
158158
if xf = 0.0 then x else
159159
(* nearest is either ceil or floor depending on which is nearest or even *)
160-
let u = Pervasives.ceil xf in
161-
let d = Pervasives.floor xf in
160+
let u = Stdlib.ceil xf in
161+
let d = Stdlib.floor xf in
162162
let um = abs_float (xf -. u) in
163163
let dm = abs_float (xf -. d) in
164164
let u_or_d =
165165
um < dm ||
166-
um = dm && let h = u /. 2. in Pervasives.floor h = h
166+
um = dm && let h = u /. 2. in Stdlib.floor h = h
167167
in
168168
let f = if u_or_d then u else d in
169169
let result = of_float f in
@@ -232,8 +232,8 @@ struct
232232
| n -> n
233233

234234
let compare_mantissa_str hex s1 s2 =
235-
let s1' = String.uppercase s1 in
236-
let s2' = String.uppercase s2 in
235+
let s1' = String.uppercase_ascii s1 in
236+
let s2' = String.uppercase_ascii s2 in
237237
compare_mantissa_str' hex s1' (skip_zeroes s1' 0) s2' (skip_zeroes s2' 0)
238238

239239
(*
@@ -267,7 +267,7 @@ struct
267267
if not hex then Printf.sprintf "%.*g" (String.length s) z else
268268
let m = logor (logand bits 0xf_ffff_ffff_ffffL) 0x10_0000_0000_0000L in
269269
(* Shift mantissa to match msb position in most significant hex digit *)
270-
let i = skip_zeroes (String.uppercase s) 0 in
270+
let i = skip_zeroes (String.uppercase_ascii s) 0 in
271271
if i = String.length s then Printf.sprintf "%.*g" (String.length s) z else
272272
let sh =
273273
match s.[i] with '1' -> 0 | '2'..'3' -> 1 | '4'..'7' -> 2 | _ -> 3 in
@@ -300,14 +300,7 @@ struct
300300
else
301301
Rep.logor x bare_nan
302302
else
303-
(* TODO(ocmal-4.03): replace buffer hack with this:
304303
let s' = String.concat "" (String.split_on_char '_' s) in
305-
*)
306-
let buf = Buffer.create (String.length s) in
307-
for i = 0 to String.length s - 1 do
308-
if s.[i] <> '_' then Buffer.add_char buf s.[i]
309-
done;
310-
let s' = Buffer.contents buf in
311304
let x = of_float (float_of_string_prevent_double_rounding s') in
312305
if is_inf x then failwith "of_string" else x
313306

interpreter/exec/i64_convert.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ let trunc_f32_u x =
2222
if xf >= -.Int64.(to_float min_int) *. 2.0 || xf <= -1.0 then
2323
raise Numeric_error.IntegerOverflow
2424
else if xf >= -.Int64.(to_float min_int) then
25-
Int64.(logxor (of_float (xf -. (* TODO(ocaml-4.03): 0x1p63 *) 9223372036854775808.0)) min_int)
25+
Int64.(logxor (of_float (xf -. 0x1p63)) min_int)
2626
else
2727
Int64.of_float xf
2828

@@ -44,7 +44,7 @@ let trunc_f64_u x =
4444
if xf >= -.Int64.(to_float min_int) *. 2.0 || xf <= -1.0 then
4545
raise Numeric_error.IntegerOverflow
4646
else if xf >= -.Int64.(to_float min_int) then
47-
Int64.(logxor (of_float (xf -. (* TODO(ocaml-4.03): 0x1p63 *) 9223372036854775808.0)) min_int)
47+
Int64.(logxor (of_float (xf -. 0x1p63)) min_int)
4848
else
4949
Int64.of_float xf
5050

0 commit comments

Comments
 (0)