From 03e2e248f0583aba17bc7508ed163bf3515e4c10 Mon Sep 17 00:00:00 2001 From: Paweł Dybiec Date: Sun, 27 Jan 2019 23:47:42 +0100 Subject: Folding instructions --- source/mod_student/constant_folding.ml | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/source/mod_student/constant_folding.ml b/source/mod_student/constant_folding.ml index 3bebcfa..bad5f80 100644 --- a/source/mod_student/constant_folding.ml +++ b/source/mod_student/constant_folding.ml @@ -1,5 +1,7 @@ open Xi_lib open Ir +open Ir_utils + module Make(T:Iface.COMPILER_TOOLBOX) = struct @@ -9,15 +11,32 @@ module Make(T:Iface.COMPILER_TOOLBOX) = struct end) = struct open M + open Analysis let cfa = T.ConstantFoldingAnalysis.analyse proc + let rewrite_instr (knwl,instr)= + let knwl = Knowledge.pre knwl in + let knwl = Ir.RegMap.fold (fun key v m -> + match v with + |None -> m + |Some v -> Ir.RegMap.add key (E_Int v) m) knwl Ir.RegMap.empty in + subst_expr_instr knwl instr + let rewrite_label l = + if l = (ControlFlowGraph.entry_label cfg) || l = (ControlFlowGraph.exit_label cfg) then () + else + match Hashtbl.find cfa l with + |Simple _ -> () + |Complex {body;_} -> ControlFlowGraph.set_block cfg l (List.map rewrite_instr body) + + let rewrite () = Logger.extra_debug begin fun () -> Logger.dump_constant_folding "before-optimization" cfg cfa; - end; - failwith "not yet implemeneted" - + end; + let labels = ControlFlowGraph.labels cfg in + List.iter rewrite_label labels + end @@ -28,4 +47,4 @@ module Make(T:Iface.COMPILER_TOOLBOX) = struct end) in Instance.rewrite () -end \ No newline at end of file +end -- cgit 1.4.1