Skip to content

Commit fb65e5b

Browse files
committed
WIP
1 parent 7b43bbf commit fb65e5b

19 files changed

+452
-325
lines changed

compiler/lib/code.ml

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,13 @@ type instr =
352352
| Array_set of Var.t * Var.t * Var.t * loc
353353

354354
type last =
355-
| Return of Var.t
356-
| Raise of Var.t * [ `Normal | `Notrace | `Reraise ]
357-
| Stop
355+
| Return of Var.t * loc
356+
| Raise of Var.t * [ `Normal | `Notrace | `Reraise ] * loc
357+
| Stop of loc
358358
| Branch of cont
359-
| Cond of Var.t * cont * cont
360-
| Switch of Var.t * cont array * cont array
361-
| Pushtrap of cont * Var.t * cont * Addr.Set.t
359+
| Cond of Var.t * cont * cont * loc
360+
| Switch of Var.t * cont array * cont array * loc
361+
| Pushtrap of cont * Var.t * cont * Addr.Set.t * loc
362362
| Poptrap of cont
363363

364364
type block =
@@ -491,20 +491,20 @@ module Print = struct
491491

492492
let last f l =
493493
match l with
494-
| Return x -> Format.fprintf f "return %a" Var.print x
495-
| Raise (x, `Normal) -> Format.fprintf f "raise %a" Var.print x
496-
| Raise (x, `Reraise) -> Format.fprintf f "reraise %a" Var.print x
497-
| Raise (x, `Notrace) -> Format.fprintf f "raise_notrace %a" Var.print x
498-
| Stop -> Format.fprintf f "stop"
494+
| Return (x, _) -> Format.fprintf f "return %a" Var.print x
495+
| Raise (x, `Normal, _) -> Format.fprintf f "raise %a" Var.print x
496+
| Raise (x, `Reraise, _) -> Format.fprintf f "reraise %a" Var.print x
497+
| Raise (x, `Notrace, _) -> Format.fprintf f "raise_notrace %a" Var.print x
498+
| Stop _ -> Format.fprintf f "stop"
499499
| Branch c -> Format.fprintf f "branch %a" cont c
500-
| Cond (x, cont1, cont2) ->
500+
| Cond (x, cont1, cont2, _) ->
501501
Format.fprintf f "if %a then %a else %a" Var.print x cont cont1 cont cont2
502-
| Switch (x, a1, a2) ->
502+
| Switch (x, a1, a2, _) ->
503503
Format.fprintf f "switch %a {" Var.print x;
504504
Array.iteri a1 ~f:(fun i c -> Format.fprintf f "int %d -> %a; " i cont c);
505505
Array.iteri a2 ~f:(fun i c -> Format.fprintf f "tag %d -> %a; " i cont c);
506506
Format.fprintf f "}"
507-
| Pushtrap (cont1, x, cont2, pcs) ->
507+
| Pushtrap (cont1, x, cont2, pcs, _) ->
508508
Format.fprintf
509509
f
510510
"pushtrap %a handler %a => %a continuation %s"
@@ -559,12 +559,12 @@ let prepend ({ start; blocks; free_pc } as p) body =
559559
| exception Not_found ->
560560
let new_start = free_pc in
561561
let blocks =
562-
Addr.Map.add new_start { params = []; body; branch = Stop } blocks
562+
Addr.Map.add new_start { params = []; body; branch = Stop (-1) } blocks
563563
in
564564
let free_pc = free_pc + 1 in
565565
{ start = new_start; blocks; free_pc })
566566

567-
let empty_block = { params = []; body = []; branch = Stop }
567+
let empty_block = { params = []; body = []; branch = Stop (-1) }
568568

569569
let empty =
570570
let start = 0 in
@@ -577,7 +577,7 @@ let is_empty p =
577577
| 1 -> (
578578
let _, v = Addr.Map.choose p.blocks in
579579
match v with
580-
| { body; branch = Stop; params = _ } -> (
580+
| { body; branch = Stop _; params = _ } -> (
581581
match body with
582582
| ([] | [ Let (_, Prim (Extern "caml_get_global_data", _), _) ]) when true ->
583583
true
@@ -588,17 +588,17 @@ let is_empty p =
588588
let fold_children blocks pc f accu =
589589
let block = Addr.Map.find pc blocks in
590590
match block.branch with
591-
| Return _ | Raise _ | Stop -> accu
591+
| Return _ | Raise _ | Stop _ -> accu
592592
| Branch (pc', _) | Poptrap (pc', _) -> f pc' accu
593-
| Pushtrap ((pc', _), _, (pc_h, _), _) ->
593+
| Pushtrap ((pc', _), _, (pc_h, _), _, _) ->
594594
let accu = f pc' accu in
595595
let accu = f pc_h accu in
596596
accu
597-
| Cond (_, (pc1, _), (pc2, _)) ->
597+
| Cond (_, (pc1, _), (pc2, _), _) ->
598598
let accu = f pc1 accu in
599599
let accu = f pc2 accu in
600600
accu
601-
| Switch (_, a1, a2) ->
601+
| Switch (_, a1, a2, _) ->
602602
let accu = Array.fold_right ~init:accu ~f:(fun (pc, _) accu -> f pc accu) a1 in
603603
let accu = Array.fold_right ~init:accu ~f:(fun (pc, _) accu -> f pc accu) a2 in
604604
accu
@@ -719,15 +719,15 @@ let invariant { blocks; start; _ } =
719719
let check_last = function
720720
| Return _ -> ()
721721
| Raise _ -> ()
722-
| Stop -> ()
722+
| Stop _ -> ()
723723
| Branch cont -> check_cont cont
724-
| Cond (_x, cont1, cont2) ->
724+
| Cond (_x, cont1, cont2, _) ->
725725
check_cont cont1;
726726
check_cont cont2
727-
| Switch (_x, a1, a2) ->
727+
| Switch (_x, a1, a2, _) ->
728728
Array.iteri a1 ~f:(fun _ cont -> check_cont cont);
729729
Array.iteri a2 ~f:(fun _ cont -> check_cont cont)
730-
| Pushtrap (cont1, _x, cont2, _pcs) ->
730+
| Pushtrap (cont1, _x, cont2, _pcs, _) ->
731731
check_cont cont1;
732732
check_cont cont2
733733
| Poptrap cont -> check_cont cont

compiler/lib/code.mli

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,13 @@ type instr =
189189
| Array_set of Var.t * Var.t * Var.t * loc
190190

191191
type last =
192-
| Return of Var.t
193-
| Raise of Var.t * [ `Normal | `Notrace | `Reraise ]
194-
| Stop
192+
| Return of Var.t * loc
193+
| Raise of Var.t * [ `Normal | `Notrace | `Reraise ] * loc
194+
| Stop of loc
195195
| Branch of cont
196-
| Cond of Var.t * cont * cont
197-
| Switch of Var.t * cont array * cont array
198-
| Pushtrap of cont * Var.t * cont * Addr.Set.t
196+
| Cond of Var.t * cont * cont * loc
197+
| Switch of Var.t * cont array * cont array * loc
198+
| Pushtrap of cont * Var.t * cont * Addr.Set.t * loc
199199
| Poptrap of cont
200200

201201
type block =

compiler/lib/deadcode.ml

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,18 @@ and mark_reachable st pc =
9090
mark_var st z
9191
| Offset_ref (x, _, _) -> mark_var st x);
9292
match block.branch with
93-
| Return x | Raise (x, _) -> mark_var st x
94-
| Stop -> ()
93+
| Return (x, _) | Raise (x, _, _) -> mark_var st x
94+
| Stop _ -> ()
9595
| Branch cont | Poptrap cont -> mark_cont_reachable st cont
96-
| Cond (x, cont1, cont2) ->
96+
| Cond (x, cont1, cont2, _) ->
9797
mark_var st x;
9898
mark_cont_reachable st cont1;
9999
mark_cont_reachable st cont2
100-
| Switch (x, a1, a2) ->
100+
| Switch (x, a1, a2, _) ->
101101
mark_var st x;
102102
Array.iter a1 ~f:(fun cont -> mark_cont_reachable st cont);
103103
Array.iter a2 ~f:(fun cont -> mark_cont_reachable st cont)
104-
| Pushtrap (cont1, _, cont2, _) ->
104+
| Pushtrap (cont1, _, cont2, _, _) ->
105105
mark_cont_reachable st cont1;
106106
mark_cont_reachable st cont2)
107107

@@ -132,21 +132,23 @@ let filter_closure blocks st i =
132132

133133
let filter_live_last blocks st l =
134134
match l with
135-
| Return _ | Raise _ | Stop -> l
135+
| Return _ | Raise _ | Stop _ -> l
136136
| Branch cont -> Branch (filter_cont blocks st cont)
137-
| Cond (x, cont1, cont2) ->
138-
Cond (x, filter_cont blocks st cont1, filter_cont blocks st cont2)
139-
| Switch (x, a1, a2) ->
137+
| Cond (x, cont1, cont2, loc) ->
138+
Cond (x, filter_cont blocks st cont1, filter_cont blocks st cont2, loc)
139+
| Switch (x, a1, a2, loc) ->
140140
Switch
141141
( x
142142
, Array.map a1 ~f:(fun cont -> filter_cont blocks st cont)
143-
, Array.map a2 ~f:(fun cont -> filter_cont blocks st cont) )
144-
| Pushtrap (cont1, x, cont2, pcs) ->
143+
, Array.map a2 ~f:(fun cont -> filter_cont blocks st cont)
144+
, loc )
145+
| Pushtrap (cont1, x, cont2, pcs, loc) ->
145146
Pushtrap
146147
( filter_cont blocks st cont1
147148
, x
148149
, filter_cont blocks st cont2
149-
, Addr.Set.inter pcs st.reachable_blocks )
150+
, Addr.Set.inter pcs st.reachable_blocks
151+
, loc )
150152
| Poptrap cont -> Poptrap (filter_cont blocks st cont)
151153

152154
(****)
@@ -198,15 +200,15 @@ let f ({ blocks; _ } as p : Code.program) =
198200
| Assign (x, y, _) -> add_def defs x (Var y)
199201
| Set_field (_, _, _, _) | Array_set (_, _, _, _) | Offset_ref (_, _, _) -> ());
200202
match block.branch with
201-
| Return _ | Raise _ | Stop -> ()
203+
| Return _ | Raise _ | Stop _ -> ()
202204
| Branch cont -> add_cont_dep blocks defs cont
203-
| Cond (_, cont1, cont2) ->
205+
| Cond (_, cont1, cont2, _) ->
204206
add_cont_dep blocks defs cont1;
205207
add_cont_dep blocks defs cont2
206-
| Switch (_, a1, a2) ->
208+
| Switch (_, a1, a2, _) ->
207209
Array.iter a1 ~f:(fun cont -> add_cont_dep blocks defs cont);
208210
Array.iter a2 ~f:(fun cont -> add_cont_dep blocks defs cont)
209-
| Pushtrap (cont, _, cont_h, _) ->
211+
| Pushtrap (cont, _, cont_h, _, _) ->
210212
add_cont_dep blocks defs cont_h;
211213
add_cont_dep blocks defs cont
212214
| Poptrap cont -> add_cont_dep blocks defs cont)

0 commit comments

Comments
 (0)