Skip to content

Commit

Permalink
CR
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierNicole committed Sep 23, 2024
1 parent 9d08e70 commit c594e7d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 18 deletions.
1 change: 0 additions & 1 deletion compiler/bin-js_of_ocaml/js_of_ocaml.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ open! Js_of_ocaml_compiler.Stdlib
open Js_of_ocaml_compiler

let () =
Config.set_target `JavaScript;
Sys.catch_break true;
let argv = Jsoo_cmdline.normalize_argv ~warn:(warn "%s") Sys.argv in
let argv =
Expand Down
15 changes: 14 additions & 1 deletion compiler/lib/code.ml
Original file line number Diff line number Diff line change
Expand Up @@ -909,15 +909,28 @@ let invariant { blocks; start; _ } =
assert (not (Var.ISet.mem defs x));
Var.ISet.add defs x)
in
let check_prim_arg = function
| Pc (NativeInt _ | Int32 _) ->
assert (
match Config.target () with
| `Wasm -> true
| _ -> false)
| Pc _ | Pv _ -> ()
in
let check_expr = function
| Apply _ -> ()
| Block (_, _, _, _) -> ()
| Field (_, _, _) -> ()
| Closure (l, cont) ->
List.iter l ~f:define;
check_cont cont
| Constant (NativeInt _ | Int32 _) ->
assert (
match Config.target () with
| `Wasm -> true
| _ -> false)
| Constant _ -> ()
| Prim (_, _) -> ()
| Prim (_, args) -> List.iter ~f:check_prim_arg args
| Special _ -> ()
in
let check_instr (i, _loc) =
Expand Down
8 changes: 4 additions & 4 deletions compiler/lib/eval.ml
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ type is_int =
| N
| Unknown

let is_int ~target info x =
let is_int info x =
match x with
| Pv x ->
get_approx
Expand All @@ -242,7 +242,7 @@ let is_int ~target info x =
match Flow.Info.def info x with
| Some (Constant (Int _)) -> Y
| Some (Constant (NativeInt _ | Int32 _)) ->
assert (Poly.equal target `Wasm);
(* These Wasm-specific constants are boxed *)
N
| Some (Block (_, _, _, _) | Constant _) -> N
| None | Some _ -> Unknown)
Expand All @@ -255,7 +255,7 @@ let is_int ~target info x =
x
| Pc (Int _) -> Y
| Pc (NativeInt _ | Int32 _) ->
assert (Poly.equal target `Wasm);
(* These Wasm-specific constants are boxed *)
N
| Pc _ -> N

Expand Down Expand Up @@ -384,7 +384,7 @@ let eval_instr ~target info ((x, loc) as i) =
below fail. *)
[ i ]
| Let (x, Prim (IsInt, [ y ])) -> (
match is_int ~target info y with
match is_int info y with
| Unknown -> [ i ]
| (Y | N) as b ->
let c = Constant (bool' Poly.(b = Y)) in
Expand Down
2 changes: 1 addition & 1 deletion compiler/lib/ocaml_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ let rec constant_of_const c : Code.constant =
| `JavaScript -> Int32.of_int_warning_on_overflow i
| `Wasm -> Int31.(of_int_warning_on_overflow i |> to_int32))
| Const_block (tag, l) ->
let l = Array.of_list (List.map l ~f:(fun c -> constant_of_const c)) in
let l = Array.of_list (List.map l ~f:constant_of_const) in
Tuple (tag, l, Unknown)

let rec find_loc_in_summary ident' = function
Expand Down
28 changes: 17 additions & 11 deletions compiler/lib/stdlib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ module type Arith_ops = sig
end

module Int31 : sig
type t = private int32
type t

include Arith_ops with type t := t

Expand Down Expand Up @@ -415,25 +415,31 @@ end = struct
~to_hex:(Printf.sprintf "%lx")
n

let neg = Int32.neg
let two_pow n =
assert (0 <= n && n <= 31);
Int32.shift_left 1l n

let min_int = Int32.neg (two_pow 30)

let neg x = if Int32.equal x min_int then x else Int32.neg x

let int_binop wrap f x y = wrap (f x y)
let int_binop f x y = wrap (f x y)

let add = int_binop wrap Int32.add
let add = int_binop Int32.add

let sub = int_binop wrap Int32.sub
let sub = int_binop Int32.sub

let mul = int_binop wrap Int32.mul
let mul = int_binop Int32.mul

let div = int_binop wrap Int32.div
let div = int_binop Int32.div

let rem = int_binop wrap Int32.rem
let rem = int_binop Int32.rem

let logand = int_binop wrap Int32.logand
let logand = int_binop Int32.logand

let logor = int_binop wrap Int32.logor
let logor = int_binop Int32.logor

let logxor = int_binop wrap Int32.logxor
let logxor = int_binop Int32.logxor

let shift_op wrap truncate f x y =
(* Limit the shift offset to [0, 31] *)
Expand Down

0 comments on commit c594e7d

Please sign in to comment.