Skip to content

Commit

Permalink
Expose some functions from Freevars
Browse files Browse the repository at this point in the history
  • Loading branch information
vouillon authored and hhugo committed Aug 25, 2024
1 parent c64a149 commit a9c69e3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
16 changes: 12 additions & 4 deletions compiler/lib/freevars.ml
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ type st =
; mutable revisited : bool
}

let find_loops p =
let in_loop = ref Addr.Map.empty in
let find_loops p in_loop pc =
let in_loop = ref in_loop in
let index = ref 0 in
let state = ref Addr.Map.empty in
let stack = Stack.create () in
Expand Down Expand Up @@ -141,9 +141,17 @@ let find_loops p =
if st.revisited
then List.iter !l ~f:(fun pc' -> in_loop := Addr.Map.add pc' pc !in_loop))
in
Code.fold_closures p (fun _ _ (pc, _) () -> traverse pc) ();
traverse pc;
!in_loop

let find_loops_in_closure p pc = find_loops p Addr.Map.empty pc

let find_all_loops p =
Code.fold_closures
p
(fun _ _ (pc, _) (in_loop : _ Addr.Map.t) -> find_loops p in_loop pc)
Addr.Map.empty

let mark_variables in_loop p =
let vars = Var.Tbl.make () (-1) in
let visited = BitSet.create' p.free_pc in
Expand Down Expand Up @@ -245,7 +253,7 @@ let f p =
let f_mutable p =
Code.invariant p;
let t = Timer.make () in
let in_loop = find_loops p in
let in_loop = find_all_loops p in
let vars = mark_variables in_loop p in
let free_vars = free_variables vars in_loop p in
if times () then Format.eprintf " free vars 1: %a@." Timer.print t;
Expand Down
6 changes: 6 additions & 0 deletions compiler/lib/freevars.mli
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ val iter_block_free_vars : (Code.Var.t -> unit) -> Code.block -> unit

val iter_block_bound_vars : (Code.Var.t -> unit) -> Code.block -> unit

val iter_instr_free_vars : (Code.Var.t -> unit) -> Code.instr -> unit

val iter_last_free_var : (Code.Var.t -> unit) -> Code.last -> unit

val find_loops_in_closure : Code.program -> Code.Addr.t -> Code.Addr.t Code.Addr.Map.t

val f_mutable : Code.program -> Code.Var.Set.t Code.Addr.Map.t

val f : Code.program -> Code.Var.Set.t Code.Addr.Map.t

0 comments on commit a9c69e3

Please sign in to comment.