2024-11-20 00:13:05 +01:00
|
|
|
open MiniImp
|
|
|
|
|
|
2024-12-03 17:18:42 +01:00
|
|
|
let colorred s =
|
|
|
|
|
"\027[31m" ^ s ^ "\027[0m"
|
|
|
|
|
|
2024-11-20 00:13:05 +01:00
|
|
|
let () =
|
2024-12-03 17:18:42 +01:00
|
|
|
let program = "
|
2024-12-27 21:11:38 +01:00
|
|
|
def main with input in output out as
|
|
|
|
|
out := in;
|
|
|
|
|
a := 1;
|
|
|
|
|
b := 2;
|
|
|
|
|
c := a + a;
|
2024-12-03 17:18:42 +01:00
|
|
|
"
|
|
|
|
|
in
|
|
|
|
|
|
2024-12-12 16:37:36 +01:00
|
|
|
(* Printf.printf "%s\n%s\n" (colorred "Program is") program; *)
|
2024-11-20 00:13:05 +01:00
|
|
|
|
|
|
|
|
let get_result x = Lexing.from_string x |> Parser.prg Lexer.lex in
|
|
|
|
|
|
|
|
|
|
let p = get_result program in
|
|
|
|
|
|
2024-12-12 16:37:36 +01:00
|
|
|
(* Format.printf "%s\n%a\n@?" (colorred "AST is") Types.pp_p_exp p; *)
|
2024-12-03 17:18:42 +01:00
|
|
|
|
|
|
|
|
let convertedcfg = CfgImp.convert_io 10 p in
|
2024-11-27 20:18:30 +01:00
|
|
|
|
2024-12-12 16:37:36 +01:00
|
|
|
(* Printf.printf "%s\n%a" (colorred "Converted CFG is") CfgImp.SSCfg.pp convertedcfg; *)
|
2024-11-27 20:18:30 +01:00
|
|
|
|
|
|
|
|
let convertedrisccfg = CfgRISC.convert convertedcfg in
|
2024-11-20 00:13:05 +01:00
|
|
|
|
2024-12-03 17:18:42 +01:00
|
|
|
Printf.printf "%s\n%a" (colorred "Converted RISC CFG is") CfgRISC.RISCCfg.pp convertedrisccfg;
|
|
|
|
|
|
2024-12-12 16:37:36 +01:00
|
|
|
(* ---------------------------------- *)
|
|
|
|
|
|
|
|
|
|
let analysiscfg = DefinedVariables.compute_defined_variables convertedrisccfg in
|
|
|
|
|
|
2024-12-21 02:16:04 +01:00
|
|
|
(* Printf.printf "%s\n%a" (colorred "Analysis CFG is") DefinedVariables.DVCfg.pp analysiscfg; *)
|
2024-12-12 16:37:36 +01:00
|
|
|
|
2024-12-27 21:11:38 +01:00
|
|
|
(* Printf.printf "%s" (colorred "Undefined Variables are:"); *)
|
|
|
|
|
(* ( *)
|
|
|
|
|
(* match DefinedVariables.check_undefined_variables analysiscfg with *)
|
|
|
|
|
(* | None -> Printf.printf " none"; *)
|
|
|
|
|
(* | Some l -> Printf.printf " %a" DefinedVariables.Variable.pplist l; *)
|
|
|
|
|
(* ); *)
|
2024-12-21 02:16:04 +01:00
|
|
|
(* Printf.printf "\n"; *)
|
2024-12-16 05:15:33 +01:00
|
|
|
|
2024-12-12 16:37:36 +01:00
|
|
|
let convertedrisccfg = DefinedVariables.compute_cfg analysiscfg in
|
|
|
|
|
|
2024-12-21 02:16:04 +01:00
|
|
|
(* Printf.printf "%s\n%a" (colorred "Converted RISC after analysis CFG is") CfgRISC.RISCCfg.pp convertedrisccfg; *)
|
|
|
|
|
|
|
|
|
|
|
2024-12-27 21:11:38 +01:00
|
|
|
(* let analysiscfg = LiveVariables.compute_live_variables convertedrisccfg in *)
|
2024-12-21 02:16:04 +01:00
|
|
|
|
2024-12-27 21:11:38 +01:00
|
|
|
(* Printf.printf "%s\n%a" (colorred "Live Analysis CFG is") LiveVariables.DVCfg.pp analysiscfg; *)
|
|
|
|
|
|
|
|
|
|
(* let convertedrisccfg = LiveVariables.compute_cfg analysiscfg in *)
|
|
|
|
|
|
|
|
|
|
(* Printf.printf "%s\n%a" (colorred "Converted RISC with no analysis CFG is") CfgRISC.RISCCfg.pp convertedrisccfg; *)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(* let convertedrisccfg = LiveVariables.compute_cfg (LiveVariables.optimize_cfg analysiscfg) in *)
|
|
|
|
|
|
|
|
|
|
(* Printf.printf "%s\n%a" (colorred "Converted RISC after analysis CFG is") CfgRISC.RISCCfg.pp convertedrisccfg; *)
|
|
|
|
|
|
|
|
|
|
let convertedrisccfg = ReduceRegisters.reduceregisters 4 convertedrisccfg in
|
|
|
|
|
|
|
|
|
|
Printf.printf "%s\n%a" (colorred "Converted RISC after reducing registers CFG is") CfgRISC.RISCCfg.pp convertedrisccfg;
|
2024-12-21 02:16:04 +01:00
|
|
|
|
2024-12-12 16:37:36 +01:00
|
|
|
|
|
|
|
|
(* ---------------------------------- *)
|
|
|
|
|
|
2024-12-27 21:11:38 +01:00
|
|
|
let risc = RISC.convert convertedrisccfg in
|
2024-12-01 12:55:20 +01:00
|
|
|
|
2024-12-27 21:11:38 +01:00
|
|
|
Printf.printf "%s\n%a" (colorred "RISC code is") RISC.RISCAssembly.pp risc;
|
2024-12-12 16:37:36 +01:00
|
|
|
|
2024-12-27 21:11:38 +01:00
|
|
|
let computerisc = RISCSemantics.reduce risc in
|
2024-12-03 17:18:42 +01:00
|
|
|
|
2024-12-27 21:11:38 +01:00
|
|
|
Printf.printf "%s\n%d\n" (colorred "Output of RISC code is") computerisc;
|
2024-12-03 17:18:42 +01:00
|
|
|
|
2024-12-12 16:37:36 +01:00
|
|
|
()
|