Change hw6 to an unsolved version.

Signed-off-by: jmug <u.g.a.mariano@gmail.com>
This commit is contained in:
Mariano Uvalle 2025-01-24 23:10:01 -08:00
parent 0c04936ccf
commit ee01a8f5b2
186 changed files with 9605 additions and 4019 deletions

View file

@ -14,10 +14,10 @@ open Datastructures
"forward", but e.g. liveness instantiates the graph so that "forward"
here is "backward" in the control-flow graph.
This means that for a node n, the output information is explicitly
represented by the "find_fact" function:
out[n] = find_fact g n
The input information for [n] is implicitly represented by:
This means that for a node n, the output information is *explicitly*
represented by the "Graph.out" function:
out[n] = Graph.out g n
The input information for [n] is *implicitly* represented by:
in[n] = combine preds[n] (out[n])
*)
@ -46,18 +46,17 @@ module type DFA_GRAPH =
(* lookup / modify the dataflow annotations associated with a node *)
val out : t -> node -> fact
val add_fact : node -> fact -> t -> t
(* printing *)
(*
val to_string : t -> string
val printer : Format.formatter -> t -> unit
*)
end
(* abstract dataflow lattice signature -------------------------------------- *)
(* The general algorithm works over a generic lattice of abstract "facts".
- facts can be combined (this is the 'meet' operation)
- facts can be compared *)
- facts can be combined (this is the 'meet' operation)
- facts can be compared:
for `compare x y` the result indicates:
< 0 : x is less than y
= 0 : x equals y
> 0 : x is greater than y
*)
module type FACT =
sig
type t
@ -88,35 +87,9 @@ module type FACT =
TASK: complete the [solve] function, which implements the above algorithm.
*)
module Make (Fact : FACT) (Graph : DFA_GRAPH with type fact := Fact.t) =
(* I used ChatGPT here to help me understand functors and find some helper functions, like "choose, remove, union, and add" *)
struct
let solve (g:Graph.t) : Graph.t =
let worklist = Graph.nodes g in
let rec solve_helper g worklist =
if Graph.NodeS.is_empty worklist then g else
(* choose a node from the worklist *)
let current_node = Graph.NodeS.choose worklist in
(* find the node's predecessors and combine their flow facts *)
let preds : Graph.NodeS.t = Graph.preds g current_node in
let pred_fact = Graph.NodeS.fold
(fun pred acc -> Fact.combine [Graph.out g pred; acc])
preds (Fact.combine []) in
(* apply the flow function to the combined input to find the new output *)
let out_fact = Graph.flow g current_node pred_fact in
(* if the output has changed, update the graph and add the node's successors to the worklist *)
let is_zero = Fact.compare out_fact (Graph.out g current_node) in
let new_worklist = Graph.NodeS.remove current_node worklist in
if is_zero != 0 then let succs = Graph.succs g current_node in
solve_helper (Graph.add_fact current_node out_fact g) (Graph.NodeS.union succs new_worklist)
else (* it has not changed *)
solve_helper g new_worklist
in
let new_g = solve_helper g worklist in new_g
failwith "TODO HW6: Solver.solve unimplemented"
end