Skip to content

Commit

Permalink
CR
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierNicole committed Sep 20, 2024
1 parent 9319b5d commit b64108c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 34 deletions.
2 changes: 1 addition & 1 deletion compiler/bin-js_of_ocaml/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ let run
| `None ->
let prims = Linker.list_all () |> StringSet.elements in
assert (List.length prims > 0);
let code, uinfo = Parse_bytecode.predefined_exceptions in
let code, uinfo = Parse_bytecode.predefined_exceptions () in
let uinfo = { uinfo with primitives = uinfo.primitives @ prims } in
let code : Parse_bytecode.one =
{ code
Expand Down
60 changes: 29 additions & 31 deletions compiler/lib/eval.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ let set_static_env s value = Hashtbl.add static_env s value

let get_static_env s = try Some (Hashtbl.find static_env s) with Not_found -> None

module type Int = sig
include Arith_ops

val int_unop : constant list -> (t -> t) -> constant option

val int_binop : constant list -> (t -> t -> t) -> constant option

val shift_op : constant list -> (t -> int -> t) -> constant option

val of_int32_warning_on_overflow : int32 -> t

val to_int32 : t -> int32

val numbits : int
end

module Int32 = struct
include Int32

Expand All @@ -49,9 +65,13 @@ module Int32 = struct
| _ -> None

let numbits = 32

let of_int32_warning_on_overflow = Fun.id

let to_int32 = Fun.id
end

module Int31 = struct
module Int31 : Int = struct
include Int31

let int_unop l f =
Expand All @@ -77,6 +97,13 @@ module Int31 = struct
let numbits = 31
end

let m_int : (module Int) =
match Config.target () with
| `JavaScript -> (module Int32)
| `Wasm -> (module Int31)

module Int = (val m_int)

let float_binop_aux (l : constant list) (f : float -> float -> 'a) : 'a option =
let args =
match l with
Expand Down Expand Up @@ -110,23 +137,7 @@ let float_binop_bool l f =
| Some b -> bool b
| None -> None

module type Int = sig
include Arith_ops

val int_unop : constant list -> (t -> t) -> constant option

val int_binop : constant list -> (t -> t -> t) -> constant option

val shift_op : constant list -> (t -> int -> t) -> constant option

val of_int32_warning_on_overflow : int32 -> t

val to_int32 : t -> int32

val numbits : int
end

let eval_prim ~target x =
let eval_prim x =
match x with
| Not, [ Int i ] -> bool Int32.(i = 0l)
| Lt, [ Int i; Int j ] -> bool Int32.(i < j)
Expand All @@ -136,18 +147,6 @@ let eval_prim ~target x =
| Ult, [ Int i; Int j ] -> bool (Int32.(j < 0l) || Int32.(i < j))
| Extern name, l -> (
let name = Primitive.resolve name in
let (module Int : Int) =
match target with
| `JavaScript ->
(module struct
include Int32

let of_int32_warning_on_overflow = Fun.id

let to_int32 = Fun.id
end)
| `Wasm -> (module Int31)
in
match name, l with
(* int *)
| "%int_add", _ -> Int.int_binop l Int.add
Expand Down Expand Up @@ -420,7 +419,6 @@ let eval_instr ~target info ((x, loc) as i) =
| _ -> false)
then
eval_prim
~target
( prim
, List.map prim_args' ~f:(function
| Some c -> c
Expand Down
2 changes: 1 addition & 1 deletion compiler/lib/parse_bytecode.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3134,7 +3134,7 @@ let from_channel ic =
`Exe
| _ -> raise Magic_number.(Bad_magic_number (to_string magic)))

let predefined_exceptions =
let predefined_exceptions () =
let body =
let open Code in
List.map predefined_exceptions ~f:(fun (index, name) ->
Expand Down
2 changes: 1 addition & 1 deletion compiler/lib/parse_bytecode.mli
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ val from_string :
-> string
-> Code.program * Debug.t

val predefined_exceptions : Code.program * Unit_info.t
val predefined_exceptions : unit -> Code.program * Unit_info.t

val link_info :
symbols:Ocaml_compiler.Symtable.GlobalMap.t
Expand Down

0 comments on commit b64108c

Please sign in to comment.