Skip to content

Commit 9315996

Browse files
vouillonhhugo
authored andcommitted
Remove location from instructions
1 parent 41382a7 commit 9315996

27 files changed

+646
-787
lines changed

compiler/bin-js_of_ocaml/compile.ml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,9 @@ let run
242242
let var_k = Code.Var.fresh () in
243243
let var_v = Code.Var.fresh () in
244244
Code.
245-
[ Let (var_k, Prim (Extern "caml_jsstring_of_string", [ Pc (String k) ])), noloc
246-
; Let (var_v, Prim (Extern "caml_jsstring_of_string", [ Pc (String v) ])), noloc
247-
; ( Let
248-
(Var.fresh (), Prim (Extern "caml_set_static_env", [ Pv var_k; Pv var_v ]))
249-
, noloc )
245+
[ Let (var_k, Prim (Extern "caml_jsstring_of_string", [ Pc (String k) ]))
246+
; Let (var_v, Prim (Extern "caml_jsstring_of_string", [ Pc (String v) ]))
247+
; Let (Var.fresh (), Prim (Extern "caml_set_static_env", [ Pv var_k; Pv var_v ]))
250248
])
251249
in
252250
let output

compiler/lib/code.ml

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,8 @@ type last =
441441

442442
type block =
443443
{ params : Var.t list
444-
; body : (instr * loc) list
445-
; branch : last * loc
444+
; body : instr list
445+
; branch : last
446446
}
447447

448448
type program =
@@ -575,7 +575,7 @@ module Print = struct
575575
| Prim (p, l) -> prim f p l
576576
| Special s -> special f s
577577

578-
let instr f (i, _loc) =
578+
let instr f i =
579579
match i with
580580
| Let (x, e) -> Format.fprintf f "%a = %a" Var.print x expr e
581581
| Assign (x, y) -> Format.fprintf f "(assign) %a = %a" Var.print x Var.print y
@@ -588,7 +588,7 @@ module Print = struct
588588
Format.fprintf f "%a[%a] = %a" Var.print x Var.print y Var.print z
589589
| Event loc -> Format.fprintf f "event %s" (Parse_info.to_string loc)
590590

591-
let last f (l, _loc) =
591+
let last f l =
592592
match l with
593593
| Return x -> Format.fprintf f "return %a" Var.print x
594594
| Raise (x, `Normal) -> Format.fprintf f "raise %a" Var.print x
@@ -607,8 +607,8 @@ module Print = struct
607607
| Poptrap c -> Format.fprintf f "poptrap %a" cont c
608608

609609
type xinstr =
610-
| Instr of (instr * loc)
611-
| Last of (last * loc)
610+
| Instr of instr
611+
| Last of last
612612

613613
let block annot pc block =
614614
Format.eprintf "==== %d (%a) ====@." pc var_list block.params;
@@ -627,7 +627,7 @@ end
627627
let fold_closures p f accu =
628628
Addr.Map.fold
629629
(fun _ block accu ->
630-
List.fold_left block.body ~init:accu ~f:(fun accu (i, _loc) ->
630+
List.fold_left block.body ~init:accu ~f:(fun accu i ->
631631
match i with
632632
| Let (x, Closure (params, cont)) -> f (Some x) params cont accu
633633
| _ -> accu))
@@ -648,12 +648,12 @@ let prepend ({ start; blocks; free_pc } as p) body =
648648
| exception Not_found ->
649649
let new_start = free_pc in
650650
let blocks =
651-
Addr.Map.add new_start { params = []; body; branch = Stop, noloc } blocks
651+
Addr.Map.add new_start { params = []; body; branch = Stop } blocks
652652
in
653653
let free_pc = free_pc + 1 in
654654
{ start = new_start; blocks; free_pc })
655655

656-
let empty_block = { params = []; body = []; branch = Stop, noloc }
656+
let empty_block = { params = []; body = []; branch = Stop }
657657

658658
let empty =
659659
let start = 0 in
@@ -666,10 +666,9 @@ let is_empty p =
666666
| 1 -> (
667667
let _, v = Addr.Map.choose p.blocks in
668668
match v with
669-
| { body; branch = Stop, _; params = _ } -> (
669+
| { body; branch = Stop; params = _ } -> (
670670
match body with
671-
| ([] | [ (Let (_, Prim (Extern "caml_get_global_data", _)), _) ]) when true ->
672-
true
671+
| ([] | [ Let (_, Prim (Extern "caml_get_global_data", _)) ]) when true -> true
673672
| _ -> false)
674673
| _ -> false)
675674
| _ -> false
@@ -681,7 +680,7 @@ let poptraps blocks pc =
681680
else
682681
let visited = Addr.Set.add pc visited in
683682
let block = Addr.Map.find pc blocks in
684-
match fst block.branch with
683+
match block.branch with
685684
| Return _ | Raise _ | Stop -> acc, visited
686685
| Branch (pc', _) -> loop blocks pc' visited depth acc
687686
| Poptrap (pc', _) ->
@@ -709,7 +708,7 @@ let poptraps blocks pc =
709708

710709
let fold_children blocks pc f accu =
711710
let block = Addr.Map.find pc blocks in
712-
match fst block.branch with
711+
match block.branch with
713712
| Return _ | Raise _ | Stop -> accu
714713
| Branch (pc', _) | Poptrap (pc', _) -> f pc' accu
715714
| Pushtrap ((pc', _), _, (pc_h, _)) ->
@@ -726,7 +725,7 @@ let fold_children blocks pc f accu =
726725

727726
let fold_children_skip_try_body blocks pc f accu =
728727
let block = Addr.Map.find pc blocks in
729-
match fst block.branch with
728+
match block.branch with
730729
| Return _ | Raise _ | Stop -> accu
731730
| Branch (pc', _) | Poptrap (pc', _) -> f pc' accu
732731
| Pushtrap ((pc', _), _, (pc_h, _)) ->
@@ -789,7 +788,7 @@ let fold_closures_innermost_first { start; blocks; _ } f accu =
789788
let block = Addr.Map.find pc blocks in
790789
List.fold_left block.body ~init:accu ~f:(fun accu i ->
791790
match i with
792-
| Let (x, Closure (params, cont)), _ ->
791+
| Let (x, Closure (params, cont)) ->
793792
let accu = visit blocks (fst cont) f accu in
794793
f (Some x) params cont accu
795794
| _ -> accu))
@@ -808,7 +807,7 @@ let fold_closures_outermost_first { start; blocks; _ } f accu =
808807
let block = Addr.Map.find pc blocks in
809808
List.fold_left block.body ~init:accu ~f:(fun accu i ->
810809
match i with
811-
| Let (x, Closure (params, cont)), _ ->
810+
| Let (x, Closure (params, cont)) ->
812811
let accu = f (Some x) params cont accu in
813812
visit blocks (fst cont) f accu
814813
| _ -> accu))
@@ -879,7 +878,7 @@ let invariant { blocks; start; _ } =
879878
| Prim (_, args) -> List.iter ~f:check_prim_arg args
880879
| Special _ -> ()
881880
in
882-
let check_instr (i, _loc) =
881+
let check_instr i =
883882
match i with
884883
| Let (x, e) ->
885884
define x;
@@ -892,11 +891,11 @@ let invariant { blocks; start; _ } =
892891
in
893892
let rec check_events l =
894893
match l with
895-
| (Event _, _) :: (Event _, _) :: _ -> assert false
894+
| Event _ :: Event _ :: _ -> assert false
896895
| _ :: r -> check_events r
897896
| [] -> ()
898897
in
899-
let check_last (l, _loc) =
898+
let check_last l =
900899
match l with
901900
| Return _ -> ()
902901
| Raise _ -> ()

compiler/lib/code.mli

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ type last =
239239

240240
type block =
241241
{ params : Var.t list
242-
; body : (instr * loc) list
243-
; branch : last * loc
242+
; body : instr list
243+
; branch : last
244244
}
245245

246246
type program =
@@ -251,22 +251,22 @@ type program =
251251

252252
module Print : sig
253253
type xinstr =
254-
| Instr of (instr * loc)
255-
| Last of (last * loc)
254+
| Instr of instr
255+
| Last of last
256256

257257
val expr : Format.formatter -> expr -> unit
258258

259259
val constant : Format.formatter -> constant -> unit
260260

261261
val var_list : Format.formatter -> Var.t list -> unit
262262

263-
val instr : Format.formatter -> instr * loc -> unit
263+
val instr : Format.formatter -> instr -> unit
264264

265265
val block : (Addr.Map.key -> xinstr -> string) -> int -> block -> unit
266266

267267
val program : (Addr.Map.key -> xinstr -> string) -> program -> unit
268268

269-
val last : Format.formatter -> last * loc -> unit
269+
val last : Format.formatter -> last -> unit
270270

271271
val cont : Format.formatter -> cont -> unit
272272
end
@@ -310,7 +310,7 @@ val traverse :
310310
val preorder_traverse :
311311
fold_blocs_poly -> (Addr.t -> 'c -> 'c) -> Addr.t -> block Addr.Map.t -> 'c -> 'c
312312

313-
val prepend : program -> (instr * loc) list -> program
313+
val prepend : program -> instr list -> program
314314

315315
val empty : program
316316

compiler/lib/deadcode.ml

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ and mark_reachable st pc =
8787
then (
8888
st.reachable_blocks <- Addr.Set.add pc st.reachable_blocks;
8989
let block = Addr.Map.find pc st.blocks in
90-
List.iter block.body ~f:(fun (i, _loc) ->
90+
List.iter block.body ~f:(fun i ->
9191
match i with
9292
| Let (_, e) -> if not (pure_expr st.pure_funs e) then mark_expr st e
9393
| Event _ | Assign _ -> ()
@@ -104,7 +104,7 @@ and mark_reachable st pc =
104104
mark_var st y;
105105
mark_var st z
106106
| Offset_ref (x, _) -> mark_var st x);
107-
match fst block.branch with
107+
match block.branch with
108108
| Return x | Raise (x, _) -> mark_var st x
109109
| Stop -> ()
110110
| Branch cont | Poptrap cont -> mark_cont_reachable st cont
@@ -143,20 +143,17 @@ let filter_closure blocks st i =
143143
| Let (x, Closure (l, cont)) -> Let (x, Closure (l, filter_cont blocks st cont))
144144
| _ -> i
145145

146-
let filter_live_last blocks st (l, loc) =
147-
let l =
148-
match l with
149-
| Return _ | Raise _ | Stop -> l
150-
| Branch cont -> Branch (filter_cont blocks st cont)
151-
| Cond (x, cont1, cont2) ->
152-
Cond (x, filter_cont blocks st cont1, filter_cont blocks st cont2)
153-
| Switch (x, a1) ->
154-
Switch (x, Array.map a1 ~f:(fun cont -> filter_cont blocks st cont))
155-
| Pushtrap (cont1, x, cont2) ->
156-
Pushtrap (filter_cont blocks st cont1, x, filter_cont blocks st cont2)
157-
| Poptrap cont -> Poptrap (filter_cont blocks st cont)
158-
in
159-
l, loc
146+
let filter_live_last blocks st l =
147+
match l with
148+
| Return _ | Raise _ | Stop -> l
149+
| Branch cont -> Branch (filter_cont blocks st cont)
150+
| Cond (x, cont1, cont2) ->
151+
Cond (x, filter_cont blocks st cont1, filter_cont blocks st cont2)
152+
| Switch (x, a1) -> Switch (x, Array.map a1 ~f:(fun cont -> filter_cont blocks st cont))
153+
| Pushtrap (cont1, x, cont2) ->
154+
Pushtrap (filter_cont blocks st cont1, x, filter_cont blocks st cont2)
155+
| Poptrap cont -> Poptrap (filter_cont blocks st cont)
156+
160157
(****)
161158

162159
let ref_count st i =
@@ -170,7 +167,7 @@ let annot st pc xi =
170167
else
171168
match (xi : Code.Print.xinstr) with
172169
| Last _ -> " "
173-
| Instr (i, _) ->
170+
| Instr i ->
174171
let c = ref_count st i in
175172
if c > 0 then Format.sprintf "%d" c else if live_instr st i then " " else "x"
176173

@@ -197,13 +194,13 @@ let f ({ blocks; _ } as p : Code.program) =
197194
let pure_funs = Pure_fun.f p in
198195
Addr.Map.iter
199196
(fun _ block ->
200-
List.iter block.body ~f:(fun (i, _loc) ->
197+
List.iter block.body ~f:(fun i ->
201198
match i with
202199
| Let (x, e) -> add_def defs x (Expr e)
203200
| Assign (x, y) -> add_def defs x (Var y)
204201
| Event _ | Set_field (_, _, _, _) | Array_set (_, _, _) | Offset_ref (_, _) ->
205202
());
206-
match fst block.branch with
203+
match block.branch with
207204
| Return _ | Raise _ | Stop -> ()
208205
| Branch cont -> add_cont_dep blocks defs cont
209206
| Cond (_, cont1, cont2) ->
@@ -229,14 +226,14 @@ let f ({ blocks; _ } as p : Code.program) =
229226
pc
230227
{ params = List.filter block.params ~f:(fun x -> st.live.(Var.idx x) > 0)
231228
; body =
232-
List.fold_left block.body ~init:[] ~f:(fun acc (i, loc) ->
229+
List.fold_left block.body ~init:[] ~f:(fun acc i ->
233230
match i, acc with
234-
| Event _, (Event _, _) :: prev ->
231+
| Event _, Event _ :: prev ->
235232
(* Avoid consecutive events (keep just the last one) *)
236-
(i, loc) :: prev
233+
i :: prev
237234
| _ ->
238235
if live_instr st i
239-
then (filter_closure all_blocks st i, loc) :: acc
236+
then filter_closure all_blocks st i :: acc
240237
else acc)
241238
|> List.rev
242239
; branch = filter_live_last all_blocks st block.branch

compiler/lib/duplicate.ml

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,19 @@ let instr s i =
4646
| Array_set (x, y, z) -> Array_set (s x, s y, s z)
4747
| Event _ -> i
4848

49-
let instrs s l = List.map l ~f:(fun (i, loc) -> instr s i, loc)
49+
let instrs s l = List.map l ~f:(fun i -> instr s i)
5050

51-
let last m s (l, loc) =
52-
let l =
53-
match l with
54-
| Stop -> l
55-
| Branch cont -> Branch (subst_cont m s cont)
56-
| Pushtrap (cont1, x, cont2) ->
57-
Pushtrap (subst_cont m s cont1, s x, subst_cont m s cont2)
58-
| Return x -> Return (s x)
59-
| Raise (x, k) -> Raise (s x, k)
60-
| Cond (x, cont1, cont2) -> Cond (s x, subst_cont m s cont1, subst_cont m s cont2)
61-
| Switch (x, a1) -> Switch (s x, Array.map a1 ~f:(fun cont -> subst_cont m s cont))
62-
| Poptrap cont -> Poptrap (subst_cont m s cont)
63-
in
64-
l, loc
51+
let last m s l =
52+
match l with
53+
| Stop -> l
54+
| Branch cont -> Branch (subst_cont m s cont)
55+
| Pushtrap (cont1, x, cont2) ->
56+
Pushtrap (subst_cont m s cont1, s x, subst_cont m s cont2)
57+
| Return x -> Return (s x)
58+
| Raise (x, k) -> Raise (s x, k)
59+
| Cond (x, cont1, cont2) -> Cond (s x, subst_cont m s cont1, subst_cont m s cont2)
60+
| Switch (x, a1) -> Switch (s x, Array.map a1 ~f:(fun cont -> subst_cont m s cont))
61+
| Poptrap cont -> Poptrap (subst_cont m s cont)
6562

6663
let block m s block =
6764
{ params = List.map ~f:s block.params

0 commit comments

Comments
 (0)