Skip to content

Commit 77214b7

Browse files
authored
flambda-backend: Obj changes for Flambda 2 (#71)
1 parent ecfdd72 commit 77214b7

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

stdlib/obj.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,18 @@ external magic : 'a -> 'b = "%identity"
3030
external is_int : t -> bool = "%obj_is_int"
3131
let [@inline always] is_block a = not (is_int a)
3232
external tag : t -> int = "caml_obj_tag" [@@noalloc]
33+
(* For Flambda 2 there is a strict distinction between arrays and other
34+
blocks. %obj_size and %obj_field may only be used on blocks. As such
35+
they are protected here using [Sys.opaque_identity], since this
36+
restriction is likely not respected by callees of this module. *)
3337
external size : t -> int = "%obj_size"
38+
let [@inline always] size t = size (Sys.opaque_identity t)
3439
external reachable_words : t -> int = "caml_obj_reachable_words"
3540
external field : t -> int -> t = "%obj_field"
41+
let [@inline always] field t = field (Sys.opaque_identity t)
3642
external set_field : t -> int -> t -> unit = "%obj_set_field"
43+
let [@inline always] set_field t index new_value =
44+
set_field (Sys.opaque_identity t) index new_value
3745
external floatarray_get : floatarray -> int -> float = "caml_floatarray_get"
3846
external floatarray_set :
3947
floatarray -> int -> float -> unit = "caml_floatarray_set"

stdlib/obj.mli

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ external magic : 'a -> 'b = "%identity"
3131
val [@inline always] is_block : t -> bool
3232
external is_int : t -> bool = "%obj_is_int"
3333
external tag : t -> int = "caml_obj_tag" [@@noalloc]
34-
external size : t -> int = "%obj_size"
34+
val size : t -> int
3535
external reachable_words : t -> int = "caml_obj_reachable_words"
3636
(**
3737
Computes the total size (in words, including the headers) of all
@@ -42,7 +42,7 @@ external reachable_words : t -> int = "caml_obj_reachable_words"
4242
@Since 4.04
4343
*)
4444

45-
external field : t -> int -> t = "%obj_field"
45+
val field : t -> int -> t
4646

4747
(** When using flambda:
4848
@@ -55,7 +55,7 @@ external field : t -> int -> t = "%obj_field"
5555
{!Sys.opaque_identity}, so any information about its contents will not
5656
be propagated.
5757
*)
58-
external set_field : t -> int -> t -> unit = "%obj_set_field"
58+
val set_field : t -> int -> t -> unit
5959

6060
val [@inline always] double_field : t -> int -> float (* @since 3.11.2 *)
6161
val [@inline always] set_double_field : t -> int -> float -> unit

testsuite/tests/warnings/w59.flambda.reference

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
File "w59.ml", line 46, characters 2-43:
2-
46 | Obj.set_field (Obj.repr o) 0 (Obj.repr 3);
3-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1+
File "w59.ml", line 51, characters 2-39:
2+
51 | set_field (Obj.repr o) 0 (Obj.repr 3);
3+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44
Warning 59 [flambda-assignment-to-non-mutable-value]: A potential assignment to a non-mutable value was detected
55
in this source file. Such assignments may generate incorrect code
66
when using Flambda.
7-
File "w59.ml", line 47, characters 2-43:
8-
47 | Obj.set_field (Obj.repr p) 0 (Obj.repr 3);
9-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7+
File "w59.ml", line 52, characters 2-39:
8+
52 | set_field (Obj.repr p) 0 (Obj.repr 3);
9+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1010
Warning 59 [flambda-assignment-to-non-mutable-value]: A potential assignment to a non-mutable value was detected
1111
in this source file. Such assignments may generate incorrect code
1212
when using Flambda.
13-
File "w59.ml", line 48, characters 2-43:
14-
48 | Obj.set_field (Obj.repr q) 0 (Obj.repr 3);
15-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
File "w59.ml", line 53, characters 2-39:
14+
53 | set_field (Obj.repr q) 0 (Obj.repr 3);
15+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1616
Warning 59 [flambda-assignment-to-non-mutable-value]: A potential assignment to a non-mutable value was detected
1717
in this source file. Such assignments may generate incorrect code
1818
when using Flambda.
19-
File "w59.ml", line 49, characters 2-43:
20-
49 | Obj.set_field (Obj.repr r) 0 (Obj.repr 3)
21-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19+
File "w59.ml", line 54, characters 2-39:
20+
54 | set_field (Obj.repr r) 0 (Obj.repr 3)
21+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2222
Warning 59 [flambda-assignment-to-non-mutable-value]: A potential assignment to a non-mutable value was detected
2323
in this source file. Such assignments may generate incorrect code
2424
when using Flambda.
25-
File "w59.ml", line 56, characters 2-7:
26-
56 | set o
25+
File "w59.ml", line 61, characters 2-7:
26+
61 | set o
2727
^^^^^
2828
Warning 59 [flambda-assignment-to-non-mutable-value]: A potential assignment to a non-mutable value was detected
2929
in this source file. Such assignments may generate incorrect code

testsuite/tests/warnings/w59.ml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,19 @@ let p = fun x -> x
4242
let q = 3.14
4343
let r = 1
4444

45+
(* %obj_set_field is OK here for Flambda 2 because we never use
46+
it on an array. We can't use Obj.field since that function
47+
contains a [Sys.opaque_identity]. *)
48+
external set_field : Obj.t -> int -> Obj.t -> unit = "%obj_set_field"
49+
4550
let () =
46-
Obj.set_field (Obj.repr o) 0 (Obj.repr 3);
47-
Obj.set_field (Obj.repr p) 0 (Obj.repr 3);
48-
Obj.set_field (Obj.repr q) 0 (Obj.repr 3);
49-
Obj.set_field (Obj.repr r) 0 (Obj.repr 3)
51+
set_field (Obj.repr o) 0 (Obj.repr 3);
52+
set_field (Obj.repr p) 0 (Obj.repr 3);
53+
set_field (Obj.repr q) 0 (Obj.repr 3);
54+
set_field (Obj.repr r) 0 (Obj.repr 3)
5055

5156
let set v =
52-
Obj.set_field (Obj.repr v) 0 (Obj.repr 3)
57+
set_field (Obj.repr v) 0 (Obj.repr 3)
5358
[@@inline]
5459

5560
let () =
@@ -59,7 +64,7 @@ let () =
5964

6065
let opaque = Sys.opaque_identity (1,2)
6166
let set_opaque =
62-
Obj.set_field
67+
set_field
6368
(Obj.repr opaque)
6469
0
6570
(Obj.repr 3)

0 commit comments

Comments
 (0)