Skip to content

Commit 654c63c

Browse files
authored
flambda-backend: Backport #295 kind changes to ocaml/ subfolder (#1018)
1 parent 50a9ce0 commit 654c63c

29 files changed

+463
-394
lines changed

asmcomp/afl_instrument.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,19 @@ let rec with_afl_logging b dbg =
5656

5757
and instrument = function
5858
(* these cases add logging, as they may be targets of conditional branches *)
59-
| Cifthenelse (cond, t_dbg, t, f_dbg, f, dbg) ->
59+
| Cifthenelse (cond, t_dbg, t, f_dbg, f, dbg, kind) ->
6060
Cifthenelse (instrument cond, t_dbg, with_afl_logging t t_dbg,
61-
f_dbg, with_afl_logging f f_dbg, dbg)
62-
| Ctrywith (e, ex, handler, dbg) ->
63-
Ctrywith (instrument e, ex, with_afl_logging handler dbg, dbg)
64-
| Cswitch (e, cases, handlers, dbg) ->
61+
f_dbg, with_afl_logging f f_dbg, dbg, kind)
62+
| Ctrywith (e, ex, handler, dbg, value_kind) ->
63+
Ctrywith (instrument e, ex, with_afl_logging handler dbg, dbg, value_kind)
64+
| Cswitch (e, cases, handlers, dbg, value_kind) ->
6565
let handlers =
6666
Array.map (fun (handler, handler_dbg) ->
6767
let handler = with_afl_logging handler handler_dbg in
6868
handler, handler_dbg)
6969
handlers
7070
in
71-
Cswitch (instrument e, cases, handlers, dbg)
71+
Cswitch (instrument e, cases, handlers, dbg, value_kind)
7272

7373
(* these cases add no logging, but instrument subexpressions *)
7474
| Clet (v, e, body) -> Clet (v, instrument e, instrument body)
@@ -80,12 +80,12 @@ and instrument = function
8080
| Ctuple es -> Ctuple (List.map instrument es)
8181
| Cop (op, es, dbg) -> Cop (op, List.map instrument es, dbg)
8282
| Csequence (e1, e2) -> Csequence (instrument e1, instrument e2)
83-
| Ccatch (isrec, cases, body) ->
83+
| Ccatch (isrec, cases, body, kind) ->
8484
let cases =
8585
List.map (fun (nfail, ids, e, dbg) -> nfail, ids, instrument e, dbg)
8686
cases
8787
in
88-
Ccatch (isrec, cases, instrument body)
88+
Ccatch (isrec, cases, instrument body, kind)
8989
| Cexit (ex, args) -> Cexit (ex, List.map instrument args)
9090
| Cregion e -> Cregion (instrument e)
9191
| Ctail e -> Ctail (instrument e)

asmcomp/cmm.ml

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ and operation =
173173
| Copaque
174174
| Cbeginregion | Cendregion
175175

176+
type value_kind =
177+
| Vval of Lambda.value_kind (* Valid OCaml values *)
178+
| Vint (* Untagged integers and off-heap pointers *)
179+
| Vaddr (* Derived pointers *)
180+
| Vfloat (* Unboxed floating-point numbers *)
181+
176182
type expression =
177183
Cconst_int of int * Debuginfo.t
178184
| Cconst_natint of nativeint * Debuginfo.t
@@ -189,17 +195,17 @@ type expression =
189195
| Cop of operation * expression list * Debuginfo.t
190196
| Csequence of expression * expression
191197
| Cifthenelse of expression * Debuginfo.t * expression
192-
* Debuginfo.t * expression * Debuginfo.t
198+
* Debuginfo.t * expression * Debuginfo.t * value_kind
193199
| Cswitch of expression * int array * (expression * Debuginfo.t) array
194-
* Debuginfo.t
200+
* Debuginfo.t * value_kind
195201
| Ccatch of
196202
rec_flag
197203
* (int * (Backend_var.With_provenance.t * machtype) list
198204
* expression * Debuginfo.t) list
199-
* expression
205+
* expression * value_kind
200206
| Cexit of int * expression list
201207
| Ctrywith of expression * Backend_var.With_provenance.t * expression
202-
* Debuginfo.t
208+
* Debuginfo.t * value_kind
203209
| Cregion of expression
204210
| Ctail of expression
205211

@@ -234,8 +240,8 @@ type phrase =
234240
Cfunction of fundecl
235241
| Cdata of data_item list
236242

237-
let ccatch (i, ids, e1, e2, dbg) =
238-
Ccatch(Nonrecursive, [i, ids, e2, dbg], e1)
243+
let ccatch (i, ids, e1, e2, dbg, kind) =
244+
Ccatch(Nonrecursive, [i, ids, e2, dbg], e1, kind)
239245

240246
let reset () =
241247
label_counter := init_label
@@ -244,21 +250,21 @@ let iter_shallow_tail f = function
244250
| Clet(_, _, body) | Cphantom_let (_, _, body) | Clet_mut(_, _, _, body) ->
245251
f body;
246252
true
247-
| Cifthenelse(_cond, _ifso_dbg, ifso, _ifnot_dbg, ifnot, _dbg) ->
253+
| Cifthenelse(_cond, _ifso_dbg, ifso, _ifnot_dbg, ifnot, _dbg, _value_kind) ->
248254
f ifso;
249255
f ifnot;
250256
true
251257
| Csequence(_e1, e2) ->
252258
f e2;
253259
true
254-
| Cswitch(_e, _tbl, el, _dbg') ->
260+
| Cswitch(_e, _tbl, el, _dbg', _value_kind) ->
255261
Array.iter (fun (e, _dbg) -> f e) el;
256262
true
257-
| Ccatch(_rec_flag, handlers, body) ->
263+
| Ccatch(_rec_flag, handlers, body, _value_kind) ->
258264
List.iter (fun (_, _, h, _dbg) -> f h) handlers;
259265
f body;
260266
true
261-
| Ctrywith(e1, _id, e2, _dbg) ->
267+
| Ctrywith(e1, _id, e2, _dbg, _value_kind) ->
262268
f e1;
263269
f e2;
264270
true
@@ -280,30 +286,34 @@ let iter_shallow_tail f = function
280286
| Cop _ ->
281287
false
282288

283-
let map_shallow_tail f = function
289+
let map_shallow_tail ?kind f = function
284290
| Clet(id, exp, body) ->
285291
Clet(id, exp, f body)
286292
| Clet_mut(id, kind, exp, body) ->
287293
Clet_mut(id, kind, exp, f body)
288294
| Cphantom_let(id, exp, body) ->
289295
Cphantom_let (id, exp, f body)
290-
| Cifthenelse(cond, ifso_dbg, ifso, ifnot_dbg, ifnot, dbg) ->
296+
| Cifthenelse(cond, ifso_dbg, ifso, ifnot_dbg, ifnot, dbg, kind_before) ->
291297
Cifthenelse
292298
(
293299
cond,
294300
ifso_dbg, f ifso,
295301
ifnot_dbg, f ifnot,
296-
dbg
302+
dbg,
303+
Option.value kind ~default:kind_before
297304
)
298305
| Csequence(e1, e2) ->
299306
Csequence(e1, f e2)
300-
| Cswitch(e, tbl, el, dbg') ->
301-
Cswitch(e, tbl, Array.map (fun (e, dbg) -> f e, dbg) el, dbg')
302-
| Ccatch(rec_flag, handlers, body) ->
307+
| Cswitch(e, tbl, el, dbg', kind_before) ->
308+
Cswitch(e, tbl, Array.map (fun (e, dbg) -> f e, dbg) el, dbg',
309+
Option.value kind ~default:kind_before)
310+
| Ccatch(rec_flag, handlers, body, kind_before) ->
303311
let map_h (n, ids, handler, dbg) = (n, ids, f handler, dbg) in
304-
Ccatch(rec_flag, List.map map_h handlers, f body)
305-
| Ctrywith(e1, id, e2, dbg) ->
306-
Ctrywith(f e1, id, f e2, dbg)
312+
Ccatch(rec_flag, List.map map_h handlers, f body,
313+
Option.value kind ~default:kind_before)
314+
| Ctrywith(e1, id, e2, dbg, kind_before) ->
315+
Ctrywith(f e1, id, f e2, dbg,
316+
Option.value kind ~default:kind_before)
307317
| Cregion e ->
308318
Cregion(f e)
309319
| Ctail e ->
@@ -319,7 +329,7 @@ let map_shallow_tail f = function
319329
| Ctuple _
320330
| Cop _ as cmm -> cmm
321331

322-
let map_tail f =
332+
let map_tail ?kind f =
323333
let rec loop = function
324334
| Cconst_int _
325335
| Cconst_natint _
@@ -330,7 +340,7 @@ let map_tail f =
330340
| Ctuple _
331341
| Cop _ as c ->
332342
f c
333-
| cmm -> map_shallow_tail loop cmm
343+
| cmm -> map_shallow_tail ?kind loop cmm
334344
in
335345
loop
336346

@@ -349,16 +359,16 @@ let iter_shallow f = function
349359
List.iter f el
350360
| Csequence (e1, e2) ->
351361
f e1; f e2
352-
| Cifthenelse(cond, _ifso_dbg, ifso, _ifnot_dbg, ifnot, _dbg) ->
362+
| Cifthenelse(cond, _ifso_dbg, ifso, _ifnot_dbg, ifnot, _dbg, _value_kind) ->
353363
f cond; f ifso; f ifnot
354-
| Cswitch (_e, _ia, ea, _dbg) ->
364+
| Cswitch (_e, _ia, ea, _dbg, _value_kind) ->
355365
Array.iter (fun (e, _) -> f e) ea
356-
| Ccatch (_rf, hl, body) ->
366+
| Ccatch (_rf, hl, body, _value_kind) ->
357367
let iter_h (_n, _ids, handler, _dbg) = f handler in
358368
List.iter iter_h hl; f body
359369
| Cexit (_n, el) ->
360370
List.iter f el
361-
| Ctrywith (e1, _id, e2, _dbg) ->
371+
| Ctrywith (e1, _id, e2, _dbg, _value_kind) ->
362372
f e1; f e2
363373
| Cregion e ->
364374
f e
@@ -386,17 +396,17 @@ let map_shallow f = function
386396
Cop (op, List.map f el, dbg)
387397
| Csequence (e1, e2) ->
388398
Csequence (f e1, f e2)
389-
| Cifthenelse(cond, ifso_dbg, ifso, ifnot_dbg, ifnot, dbg) ->
390-
Cifthenelse(f cond, ifso_dbg, f ifso, ifnot_dbg, f ifnot, dbg)
391-
| Cswitch (e, ia, ea, dbg) ->
392-
Cswitch (e, ia, Array.map (fun (e, dbg) -> f e, dbg) ea, dbg)
393-
| Ccatch (rf, hl, body) ->
399+
| Cifthenelse(cond, ifso_dbg, ifso, ifnot_dbg, ifnot, dbg, kind) ->
400+
Cifthenelse(f cond, ifso_dbg, f ifso, ifnot_dbg, f ifnot, dbg, kind)
401+
| Cswitch (e, ia, ea, dbg, kind) ->
402+
Cswitch (e, ia, Array.map (fun (e, dbg) -> f e, dbg) ea, dbg, kind)
403+
| Ccatch (rf, hl, body, kind) ->
394404
let map_h (n, ids, handler, dbg) = (n, ids, f handler, dbg) in
395-
Ccatch (rf, List.map map_h hl, f body)
405+
Ccatch (rf, List.map map_h hl, f body, kind)
396406
| Cexit (n, el) ->
397407
Cexit (n, List.map f el)
398-
| Ctrywith (e1, id, e2, dbg) ->
399-
Ctrywith (f e1, id, f e2, dbg)
408+
| Ctrywith (e1, id, e2, dbg, value_kind) ->
409+
Ctrywith (f e1, id, f e2, dbg, value_kind)
400410
| Cregion e ->
401411
Cregion (f e)
402412
| Ctail e ->

asmcomp/cmm.mli

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,15 @@ and operation =
171171
| Copaque (* Sys.opaque_identity *)
172172
| Cbeginregion | Cendregion
173173

174+
type value_kind =
175+
| Vval of Lambda.value_kind (* Valid OCaml values *)
176+
| Vint (* Untagged integers and off-heap pointers *)
177+
| Vaddr (* Derived pointers *)
178+
| Vfloat (* Unboxed floating-point numbers *)
179+
174180
(** Every basic block should have a corresponding [Debuginfo.t] for its
175181
beginning. *)
176-
and expression =
182+
type expression =
177183
Cconst_int of int * Debuginfo.t
178184
| Cconst_natint of nativeint * Debuginfo.t
179185
| Cconst_float of float * Debuginfo.t
@@ -190,17 +196,18 @@ and expression =
190196
| Cop of operation * expression list * Debuginfo.t
191197
| Csequence of expression * expression
192198
| Cifthenelse of expression * Debuginfo.t * expression
193-
* Debuginfo.t * expression * Debuginfo.t
199+
* Debuginfo.t * expression * Debuginfo.t * value_kind
194200
| Cswitch of expression * int array * (expression * Debuginfo.t) array
195-
* Debuginfo.t
201+
* Debuginfo.t * value_kind
196202
| Ccatch of
197203
rec_flag
198204
* (int * (Backend_var.With_provenance.t * machtype) list
199205
* expression * Debuginfo.t) list
200206
* expression
207+
* value_kind
201208
| Cexit of int * expression list
202209
| Ctrywith of expression * Backend_var.With_provenance.t * expression
203-
* Debuginfo.t
210+
* Debuginfo.t * value_kind
204211
| Cregion of expression
205212
| Ctail of expression
206213

@@ -237,7 +244,7 @@ type phrase =
237244

238245
val ccatch :
239246
int * (Backend_var.With_provenance.t * machtype) list
240-
* expression * expression * Debuginfo.t
247+
* expression * expression * Debuginfo.t * value_kind
241248
-> expression
242249

243250
val reset : unit -> unit
@@ -251,12 +258,12 @@ val iter_shallow_tail: (expression -> unit) -> expression -> bool
251258
considered to be in tail position (because their result become
252259
the final result for the expression). *)
253260

254-
val map_shallow_tail: (expression -> expression) -> expression -> expression
261+
val map_shallow_tail: ?kind:value_kind -> (expression -> expression) -> expression -> expression
255262
(** Apply the transformation to those immediate sub-expressions of an
256263
expression that are in tail position, using the same definition of "tail"
257264
as [iter_shallow_tail] *)
258265

259-
val map_tail: (expression -> expression) -> expression -> expression
266+
val map_tail: ?kind:value_kind -> (expression -> expression) -> expression -> expression
260267
(** Apply the transformation to an expression, trying to push it
261268
to all inner sub-expressions that can produce the final result,
262269
by recursively applying map_shallow_tail *)

0 commit comments

Comments
 (0)