Skip to content

Commit

Permalink
phys_equal
Browse files Browse the repository at this point in the history
  • Loading branch information
hhugo committed Dec 4, 2024
1 parent 2ca7dbf commit 6806a1b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
16 changes: 16 additions & 0 deletions compiler/lib/generate.ml
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ let plus_int x y =

let bool e = J.ECond (e, one, zero)

let bool_not e = J.ECond (e, zero, one)

(****)

let source_location ctx position pc =
Expand Down Expand Up @@ -1398,6 +1400,20 @@ let rec translate_expr ctx loc x e level : (_ * J.statement_list) Expr_builder.t
| _ -> J.EBin (J.Plus, ca, cb)
in
return (add ca cb)
| Extern "%phys_equal", [x; y] ->
let* cx = access' ~ctx x in
let* cy = access' ~ctx y in
return (bool (J.call
(J.dot (s_var "Object") (Utf8_string.of_string_exn "is"))
[ cx; cy ]
loc))
| Extern "%not_phys_equal", [x; y] ->
let* cx = access' ~ctx x in
let* cy = access' ~ctx y in
return (bool_not (J.call
(J.dot (s_var "Object") (Utf8_string.of_string_exn "is"))
[ cx; cy ]
loc))
| Extern name, l -> (
let name = Primitive.resolve name in
match internal_prim name with
Expand Down
15 changes: 13 additions & 2 deletions compiler/lib/parse_bytecode.ml
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ module Hints = struct
; layout : Lambda.bigarray_layout
}
| Hint_primitive of Primitive.description
| Hint_phys_equal

module Int_table = Hashtbl.Make (Int)

Expand Down Expand Up @@ -2222,23 +2223,33 @@ and compile infos pc state (instrs : instr list) =

if debug_parser ()
then Format.printf "%a = mk_bool(%a == %a)@." Var.print x Var.print y Var.print z;
let hints = Hints.find infos.hints pc in
let prim =
if List.mem Hints.Hint_phys_equal ~set:hints then Extern "%phys_equal" else Eq
in
compile
infos
(pc + 1)
(State.pop 1 state)
(Let (x, Prim (Eq, [ Pv y; Pv z ])) :: instrs)
(Let (x, Prim (prim, [ Pv y; Pv z ])) :: instrs)
| NEQ ->
let y = State.accu state in
let z = State.peek 0 state in
let x, state = State.fresh_var state in

if debug_parser ()
then Format.printf "%a = mk_bool(%a != %a)@." Var.print x Var.print y Var.print z;
let hints = Hints.find infos.hints pc in
let prim =
if List.mem Hints.Hint_phys_equal ~set:hints
then Extern "%not_phys_equal"
else Neq
in
compile
infos
(pc + 1)
(State.pop 1 state)
(Let (x, Prim (Neq, [ Pv y; Pv z ])) :: instrs)
(Let (x, Prim (prim, [ Pv y; Pv z ])) :: instrs)
| LTINT ->
let y = State.accu state in
let z = State.peek 0 state in
Expand Down

0 comments on commit 6806a1b

Please sign in to comment.