summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--source/mod_student/constant_folding.ml27
1 files 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