1
+ (* *************************************************************************)
2
+ (* *)
3
+ (* OCaml *)
4
+ (* *)
5
+ (* Pierre Chambart, OCamlPro *)
6
+ (* *)
7
+ (* Copyright 2023 OCamlPro SAS *)
8
+ (* *)
9
+ (* All rights reserved. This file is distributed under the terms of *)
10
+ (* the GNU Lesser General Public License version 2.1, with the *)
11
+ (* special exception on linking described in the file LICENSE. *)
12
+ (* *)
13
+ (* *************************************************************************)
14
+
1
15
type atom =
2
16
| Value
3
17
| Value_int
4
18
| Unboxed_float
5
19
| Unboxed_int of Lambda .boxed_integer
6
20
7
- let rec fold_left_layout (f : 'acc -> 'e -> atom -> 'acc )
8
- (acc : 'acc ) ( expr : Clambda.ulambda ) (layout : Clambda_primitives.layout ) : 'acc =
21
+ let rec fold_left_layout (f : 'acc -> 'e -> atom -> 'acc ) ( acc : 'acc )
22
+ (expr : Clambda.ulambda ) (layout : Clambda_primitives.layout ) : 'acc =
9
23
match layout with
10
- | Ptop ->
11
- Misc. fatal_error " [Ptop] can't be stored in a closure."
24
+ | Ptop -> Misc. fatal_error " [Ptop] can't be stored in a closure."
12
25
| Pbottom ->
13
26
Misc. fatal_error
14
- " [Pbottom] should have been eliminated as dead code \
15
- and not stored in a closure."
27
+ " [Pbottom] should have been eliminated as dead code and not stored in a \
28
+ closure."
16
29
| Punboxed_float -> f acc expr Unboxed_float
17
30
| Punboxed_int bi -> f acc expr (Unboxed_int bi)
18
31
| Pvalue Pintval -> f acc expr Value_int
19
32
| Pvalue _ -> f acc expr Value
20
33
| Punboxed_product layouts ->
21
- List. fold_left (fun acc (field , layout ) ->
34
+ List. fold_left
35
+ (fun acc (field , layout ) ->
22
36
let expr : Clambda.ulambda =
23
- Uprim (Punboxed_product_field (field, layouts), [expr], Debuginfo. none) in
24
- fold_left_layout f acc expr layout) acc
37
+ Uprim (Punboxed_product_field (field, layouts), [expr], Debuginfo. none)
38
+ in
39
+ fold_left_layout f acc expr layout)
40
+ acc
25
41
(List. mapi (fun i v -> i, v) layouts)
26
42
27
43
type ('visible, 'invisible) decomposition' =
@@ -30,10 +46,12 @@ type ('visible, 'invisible) decomposition' =
30
46
| Product of ('visible , 'invisible ) decomposition ' array
31
47
32
48
type decomposition =
33
- | Atom of { offset : int ; layout : atom }
49
+ | Atom of
50
+ { offset : int ;
51
+ layout : atom
52
+ }
34
53
| Product of decomposition array
35
54
36
-
37
55
let print_atom ppf = function
38
56
| Value -> Format. fprintf ppf " val"
39
57
| Value_int -> Format. fprintf ppf " int"
@@ -42,7 +60,8 @@ let print_atom ppf = function
42
60
| Unboxed_int Pint64 -> Format. fprintf ppf " unboxed_int64"
43
61
| Unboxed_int Pnativeint -> Format. fprintf ppf " unboxed_nativeint"
44
62
45
- let equal_decomposition = (= )
63
+ let equal_decomposition = ( = )
64
+
46
65
let rec print_decomposition ppf dec =
47
66
match dec with
48
67
| Atom { offset; layout } ->
@@ -54,30 +73,26 @@ let rec print_decomposition ppf dec =
54
73
55
74
let rec decompose (layout : Lambda.layout ) : _ decomposition' =
56
75
match layout with
57
- | Ptop ->
58
- Misc. fatal_error " [Ptop] can't be stored in a closure."
76
+ | Ptop -> Misc. fatal_error " [Ptop] can't be stored in a closure."
59
77
| Pbottom ->
60
78
Misc. fatal_error
61
- " [Pbottom] should have been eliminated as dead code \
62
- and not stored in a closure."
79
+ " [Pbottom] should have been eliminated as dead code and not stored in a \
80
+ closure."
63
81
| Punboxed_float -> Gc_invisible (() , Unboxed_float )
64
82
| Punboxed_int bi -> Gc_invisible (() , Unboxed_int bi)
65
83
| Pvalue Pintval -> Gc_invisible (() , Value_int )
66
84
| Pvalue _ -> Gc_visible (() , Value )
67
- | Punboxed_product l ->
68
- Product (Array. of_list (List. map decompose l))
85
+ | Punboxed_product l -> Product (Array. of_list (List. map decompose l))
69
86
70
87
let rec solidify (dec : (int, int) decomposition' ) : decomposition =
71
88
match dec with
72
89
| Gc_visible (offset , layout ) -> Atom { offset; layout }
73
90
| Gc_invisible (offset , layout ) -> Atom { offset; layout }
74
- | Product a ->
75
- Product (Array. map solidify a)
91
+ | Product a -> Product (Array. map solidify a)
76
92
77
- let rec fold_decompose
78
- (f1 : 'acc -> 'a -> atom -> 'acc * 'b ) (f2 : 'acc -> 'c -> atom -> 'acc * 'd )
79
- (acc : 'acc ) (d : ('a, 'c) decomposition' ) :
80
- 'acc * ('b , 'd ) decomposition' =
93
+ let rec fold_decompose (f1 : 'acc -> 'a -> atom -> 'acc * 'b )
94
+ (f2 : 'acc -> 'c -> atom -> 'acc * 'd ) (acc : 'acc )
95
+ (d : ('a, 'c) decomposition' ) : 'acc * ('b, 'd) decomposition' =
81
96
match d with
82
97
| Gc_visible (v , layout ) ->
83
98
let acc, v = f1 acc v layout in
@@ -90,36 +105,22 @@ let rec fold_decompose
90
105
acc, Product elts
91
106
92
107
let atom_size (layout : atom ) =
93
- match layout with
94
- | Value
95
- | Value_int
96
- | Unboxed_float
97
- | Unboxed_int _ -> 1
108
+ match layout with Value | Value_int | Unboxed_float | Unboxed_int _ -> 1
98
109
99
110
let assign_invisible_offsets init_pos (var , dec ) =
100
- let f_visible acc () _layout =
101
- acc, ()
102
- in
103
- let f_invisible acc () layout =
104
- acc + atom_size layout, acc
105
- in
111
+ let f_visible acc () _layout = acc, () in
112
+ let f_invisible acc () layout = acc + atom_size layout, acc in
106
113
let acc, dec = fold_decompose f_visible f_invisible init_pos dec in
107
114
acc, (var, dec)
108
115
109
116
let assign_visible_offsets init_pos (var , dec ) =
110
- let f_visible acc () layout =
111
- acc + atom_size layout, acc
112
- in
113
- let f_invisible acc off _layout =
114
- acc, off
115
- in
117
+ let f_visible acc () layout = acc + atom_size layout, acc in
118
+ let f_invisible acc off _layout = acc, off in
116
119
let acc, dec = fold_decompose f_visible f_invisible init_pos dec in
117
120
acc, (var, solidify dec)
118
121
119
122
let decompose_free_vars ~base_offset ~free_vars =
120
- let free_vars =
121
- List. map (fun (var , kind ) -> var, decompose kind) free_vars
122
- in
123
+ let free_vars = List. map (fun (var , kind ) -> var, decompose kind) free_vars in
123
124
let base_offset, free_vars =
124
125
List. fold_left_map assign_invisible_offsets base_offset free_vars
125
126
in
0 commit comments