diff --git a/bin/main.ml b/bin/main.ml index 86c7546..21c2600 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -5,13 +5,12 @@ let colorred s = let () = let program = " -def main with input n output result as - result := 0; - s := 0; - while (0 == ((n - 1) / (2 ^ s)) % 2) do ( - s := s + 1 +def main with input n output out as + for (x := 2, x < 0, x := 2) do ( + y := x + 3; + x := y; ); - + out := 1 - y; " in diff --git a/lib/miniImp/definedVariables.ml b/lib/miniImp/definedVariables.ml index 919771b..0c2a86b 100644 --- a/lib/miniImp/definedVariables.ml +++ b/lib/miniImp/definedVariables.ml @@ -18,6 +18,32 @@ module DVCfg = Dataflow.Make (CfgRISC.RISCSimpleStatements) (Variable) module DVCeltSet = Set.Make(Variable) +let variables (instr : DVCfg.elt) : DVCfg.internal list = + let helper (acc: DVCeltSet.t) (instr: DVCfg.elt) = + match instr with + | Nop -> + acc + | BRegOp (_, r1, r2, r3) -> + DVCeltSet.add r1.index acc |> + DVCeltSet.add r2.index |> + DVCeltSet.add r3.index + | BImmOp (_, r1, _, r3) + | URegOp (_, r1, r3) + | Load (r1, r3) + | Store (r1, r3) -> + DVCeltSet.add r1.index acc |> + DVCeltSet.add r3.index + | LoadI (_, r3) -> + DVCeltSet.add r3.index acc + in + + helper DVCeltSet.empty instr |> DVCeltSet.to_list + +let variables_all (instructions : DVCfg.elt list) : DVCfg.internal list = + List.fold_left (fun (acc: DVCeltSet.t) (instr: DVCfg.elt) -> + DVCeltSet.union acc (variables instr |> DVCeltSet.of_list) + ) DVCeltSet.empty instructions |> DVCeltSet.to_list + let variables_used (instr : DVCfg.elt) : DVCfg.internal list = let helper (acc: DVCeltSet.t) (instr: DVCfg.elt) = match instr with @@ -62,21 +88,21 @@ let variables_defined_all (instructions : DVCfg.elt list) : DVCfg.internal list DVCeltSet.union acc (variables_defined instr |> DVCeltSet.of_list) ) DVCeltSet.empty instructions |> DVCeltSet.to_list -let _variables_defined_nth (instructions : DVCfg.elt list) (i: int) : DVCfg.internal list = - variables_defined (List.nth instructions i) - -let _variables_defined_last (instructions : DVCfg.elt list) : DVCfg.internal list = - variables_defined (List.nth instructions ((List.length instructions) - 1)) - - -(* init function, assign the epmpty set to everything *) -let init : (DVCfg.elt list -> DVCfg.internalnode) = +(* init function, assign the bottom to everything *) +let _init_bottom : (DVCfg.elt list -> DVCfg.internalnode) = (fun l -> {internalin = []; internalout = []; internalbetween = (List.init (List.length l) (fun _ -> ([], [])))}) +(* init function, assign the top to everything *) +let init_top (all_variables) : (DVCfg.elt list -> DVCfg.internalnode) = + (fun l -> {internalin = all_variables; + internalout = all_variables; + internalbetween = (List.init (List.length l) + (fun _ -> (all_variables, all_variables)))}) + (* piece of code that computes vout for the whole block, not used, use lub below *) @@ -171,8 +197,13 @@ let update (t: DVCfg.t) (node: Cfg.Node.t) : DVCfg.internalnode = let compute_defined_variables (cfg: RISCCfg.t) : DVCfg.t = + let all_variables = List.fold_left + (fun acc (_, code) -> Utility.unique_union acc (variables_all code)) + [] + (Cfg.NodeMap.to_list cfg.content) + in DVCfg.from_cfg cfg - |> DVCfg.fixed_point ~init:init ~update:update + |> DVCfg.fixed_point ~init:(init_top all_variables) ~update:update