Skip to content

Commit 33dd2ff

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

File tree

3 files changed

+109
-56
lines changed

3 files changed

+109
-56
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: 15 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,35 @@ 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 ~eq:Hints.equal Hints.Hint_phys_equal hints
2327+
then Extern "%phys_equal"
2328+
else Eq
2329+
in
23232330
compile
23242331
infos
23252332
(pc + 1)
23262333
(State.pop 1 state)
2327-
(Let (x, Prim (Eq, [ Pv y; Pv z ])) :: instrs)
2334+
(Let (x, Prim (prim, [ Pv y; Pv z ])) :: instrs)
23282335
| NEQ ->
23292336
let y = State.accu state in
23302337
let z = State.peek 0 state in
23312338
let x, state = State.fresh_var state in
23322339

23332340
if debug_parser ()
23342341
then Format.printf "%a = mk_bool(%a != %a)@." Var.print x Var.print y Var.print z;
2342+
let hints = Hints.find infos.hints pc in
2343+
let prim =
2344+
if List.mem ~eq:Hints.equal Hints.Hint_phys_equal hints
2345+
then Extern "%not_phys_equal"
2346+
else Neq
2347+
in
23352348
compile
23362349
infos
23372350
(pc + 1)
23382351
(State.pop 1 state)
2339-
(Let (x, Prim (Neq, [ Pv y; Pv z ])) :: instrs)
2352+
(Let (x, Prim (prim, [ Pv y; Pv z ])) :: instrs)
23402353
| LTINT ->
23412354
let y = State.accu state in
23422355
let z = State.peek 0 state in

0 commit comments

Comments
 (0)