Forráskód Böngészése

Fixed iinc_1/idec_1 replacement

Taddeus Kroes 12 éve
szülő
commit
2398b7395b
4 módosított fájl, 26 hozzáadás és 6 törlés
  1. 16 4
      phases/peephole.ml
  2. 4 0
      phases/print.ml
  3. 4 2
      test/old/peephole.cvc
  4. 2 0
      types.ml

+ 16 - 4
phases/peephole.ml

@@ -27,18 +27,30 @@ let rec peephole = function
      *     v                  v
      * i{inc,dec} L C   |   i{inc,dec}_1 L
      *)
-    | (Load (Int, Current, index) :: LoadImm (IntVal i) :: Op (Add, Int) ::
+    | (Load (Int, Current, index) :: LoadConst (_, i) :: Op (Add, Int) ::
             Store (Int, Current, store) :: tl
-    |  LoadImm (IntVal i) :: Load (Int, Current, index) :: Op (Add, Int) ::
+    |  LoadConst (_, i) :: Load (Int, Current, index) :: Op (Add, Int) ::
             Store (Int, Current, store) :: tl) when store = index ->
         InlineComment (Inc (index, i), "add -> inc") :: (peephole tl)
 
-    | (Load (Int, Current, index) :: LoadImm (IntVal i) :: Op (Sub, Int) ::
+    | (Load (Int, Current, index) :: LoadConst (_, i) :: Op (Sub, Int) ::
             Store (Int, Current, store) :: tl
-    |  LoadImm (IntVal i) :: Load (Int, Current, index) :: Op (Sub, Int) ::
+    |  LoadConst (_, i) :: Load (Int, Current, index) :: Op (Sub, Int) ::
             Store (Int, Current, store) :: tl) when store = index ->
         InlineComment (Dec (index, i), "sub -> dec") :: (peephole tl)
 
+    | (Load (Int, Current, index) :: LoadImm (IntVal 1) :: Op (Add, Int) ::
+            Store (Int, Current, store) :: tl
+    |  LoadImm (IntVal 1) :: Load (Int, Current, index) :: Op (Add, Int) ::
+            Store (Int, Current, store) :: tl) when store = index ->
+        InlineComment (IncOne index, "add -> inc") :: (peephole tl)
+
+    | (Load (Int, Current, index) :: LoadImm (IntVal 1) :: Op (Sub, Int) ::
+            Store (Int, Current, store) :: tl
+    |  LoadImm (IntVal 1) :: Load (Int, Current, index) :: Op (Sub, Int) ::
+            Store (Int, Current, store) :: tl) when store = index ->
+        InlineComment (DecOne index, "sub -> dec") :: (peephole tl)
+
     | hd :: tl -> hd :: (peephole tl)
     | [] -> []
 

+ 4 - 0
phases/print.ml

@@ -97,6 +97,10 @@ let rec instr2str = function
         tab ^ "iinc " ^ si index ^ " " ^ si const
     | Dec (index, const) ->
         tab ^ "idec " ^ si index ^ " " ^ si const
+    | IncOne index ->
+        tab ^ "iinc_1 " ^ si index
+    | DecOne index ->
+        tab ^ "idec_1 " ^ si index
 
     (* Control flow *)
     | RtnInit scope ->

+ 4 - 2
test/old/peephole.cvc

@@ -3,8 +3,10 @@ extern void printInt(int val);
 export int main() {
     int a;
     int b;
-    a = a + 1;  // inc
-    a = a - 1;  // dec
+    a = a + 1;  // inc_1
+    a = a - 1;  // dec_1
+    a = a + 2;  // inc C
+    a = a - 2;  // dec C
     b = a + 1;  // NOT inc because not stored in a
 
     if (true)   // branch is removed

+ 2 - 0
types.ml

@@ -102,6 +102,8 @@ type instr =
     | Convert of ctype * ctype           (* i2f|f2i *)
     | Inc of int * int                   (* iinc L C *)
     | Dec of int * int                   (* idec L C *)
+    | IncOne of int                      (* iinc_1 L *)
+    | DecOne of int                      (* idec_1 L *)
 
     (* Control flow *)
     | RtnInit of stack_scope