Kaynağa Gözat

Changed algebraic transformations to more generic form.

Jayke Meijer 14 yıl önce
ebeveyn
işleme
8c9efd40fb
2 değiştirilmiş dosya ile 13 ekleme ve 10 silme
  1. 7 7
      src/optimize/advanced.py
  2. 6 3
      tests/test_optimize_advanced.py

+ 7 - 7
src/optimize/advanced.py

@@ -220,11 +220,11 @@ def copy_propagation(block):
 def algebraic_transformations(block):
     """
     Change ineffective or useless algebraic transformations. Handled are:
-    - x = x + 0 -> remove
-    - x = x - 0 -> remove
-    - x = x * 1 -> remove
-    - x = x * 0 -> x = 0
-    - x = x * 2 -> x = x << 1
+    - x = y + 0 -> x = y
+    - x = y - 0 -> x = y
+    - x = y * 1 -> x = y
+    - x = y * 0 -> x = 0
+    - x = y * 2 -> x = x << 1
     """
     changed = False
     
@@ -232,10 +232,10 @@ def algebraic_transformations(block):
         s = block.read()
         
         if (s.is_command('addu') or s.is_command('subu')) and s[2] == 0:
-            block.replace(1, [])
+            block.replace(1, [S('command', 'move', s[0], s[1])])
             changed = True
         elif s.is_command('mult') and s[2] == 1:
-            block.replace(1, [])
+            block.replace(1, [S('command', 'move', s[0], s[1])])
             changed = True
         elif s.is_command('mult') and s[2] == 0:
             block.replace(1, [S('command', 'li', '$1', to_hex(0))])

+ 6 - 3
tests/test_optimize_advanced.py

@@ -78,7 +78,8 @@ class TestOptimizeAdvanced(unittest.TestCase):
                    
         self.assertTrue(algebraic_transformations(block))
         self.assertEqual(block.statements, [self.foo,
-                   self.bar])
+                         S('command', 'move', '$1', '$2'),
+                         self.bar])
                    
     def test_algebraic_transforms_add1(self):
         arguments = [self.foo,
@@ -96,7 +97,8 @@ class TestOptimizeAdvanced(unittest.TestCase):
                    
         self.assertTrue(algebraic_transformations(block))
         self.assertEqual(block.statements, [self.foo,
-                   self.bar])
+                         S('command', 'move', '$1', '$2'),
+                         self.bar])
                    
     def test_algebraic_transforms_sub1(self):
         arguments = [self.foo,
@@ -124,7 +126,8 @@ class TestOptimizeAdvanced(unittest.TestCase):
                    
         self.assertTrue(algebraic_transformations(block))
         self.assertEqual(block.statements, [self.foo,
-                   self.bar])
+                         S('command', 'move', '$1', '$2'),
+                         self.bar])
                    
     def test_algebraic_transforms_mult2(self):
         block = B([self.foo,