Skip to content

Commit f16ba36

Browse files
committed
Compiler: phys_equal hints
1 parent 96fe9c4 commit f16ba36

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

compiler/lib/generate.ml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,8 @@ let plus_int x y =
374374

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

377+
let bool_not e = J.ECond (e, zero, one)
378+
377379
(****)
378380

379381
let source_location loc =
@@ -1547,6 +1549,24 @@ let rec translate_expr ctx loc x e level : (_ * J.statement_list) Expr_builder.t
15471549
| _ -> J.EBin (J.Plus, ca, cb)
15481550
in
15491551
return (add ca cb)
1552+
| Extern "%phys_equal", [ x; y ] ->
1553+
let* cx = access' ~ctx x in
1554+
let* cy = access' ~ctx y in
1555+
return
1556+
(bool
1557+
(J.call
1558+
(J.dot (s_var "Object") (Utf8_string.of_string_exn "is"))
1559+
[ cx; cy ]
1560+
loc))
1561+
| Extern "%not_phys_equal", [ x; y ] ->
1562+
let* cx = access' ~ctx x in
1563+
let* cy = access' ~ctx y in
1564+
return
1565+
(bool_not
1566+
(J.call
1567+
(J.dot (s_var "Object") (Utf8_string.of_string_exn "is"))
1568+
[ cx; cy ]
1569+
loc))
15501570
| Extern name_orig, l -> (
15511571
let name = Primitive.resolve name_orig in
15521572
match internal_prim name with

compiler/lib/parse_bytecode.ml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ module Hints = struct
368368
; layout : Lambda.bigarray_layout
369369
}
370370
| Hint_primitive of Primitive.description
371+
| Hint_phys_equal
371372

372373
type t = { hints : optimization_hint Int.Hashtbl.t }
373374

@@ -2320,23 +2321,33 @@ and compile infos pc state (instrs : instr list) =
23202321

23212322
if debug_parser ()
23222323
then Format.printf "%a = mk_bool(%a == %a)@." Var.print x Var.print y Var.print z;
2324+
let hints = Hints.find infos.hints pc in
2325+
let prim =
2326+
if List.mem Hints.Hint_phys_equal ~set:hints then Extern "%phys_equal" else Eq
2327+
in
23232328
compile
23242329
infos
23252330
(pc + 1)
23262331
(State.pop 1 state)
2327-
(Let (x, Prim (Eq, [ Pv y; Pv z ])) :: instrs)
2332+
(Let (x, Prim (prim, [ Pv y; Pv z ])) :: instrs)
23282333
| NEQ ->
23292334
let y = State.accu state in
23302335
let z = State.peek 0 state in
23312336
let x, state = State.fresh_var state in
23322337

23332338
if debug_parser ()
23342339
then Format.printf "%a = mk_bool(%a != %a)@." Var.print x Var.print y Var.print z;
2340+
let hints = Hints.find infos.hints pc in
2341+
let prim =
2342+
if List.mem Hints.Hint_phys_equal ~set:hints
2343+
then Extern "%not_phys_equal"
2344+
else Neq
2345+
in
23352346
compile
23362347
infos
23372348
(pc + 1)
23382349
(State.pop 1 state)
2339-
(Let (x, Prim (Neq, [ Pv y; Pv z ])) :: instrs)
2350+
(Let (x, Prim (prim, [ Pv y; Pv z ])) :: instrs)
23402351
| LTINT ->
23412352
let y = State.accu state in
23422353
let z = State.peek 0 state in

0 commit comments

Comments
 (0)