Jelajahi Sumber

Working on copyprop.

Jayke Meijer 14 tahun lalu
induk
melakukan
b550716008
2 mengubah file dengan 34 tambahan dan 17 penghapusan
  1. 10 7
      src/optimize/advanced.py
  2. 24 10
      tests/test_optimize_advanced.py

+ 10 - 7
src/optimize/advanced.py

@@ -114,25 +114,27 @@ def copy_propagation(block):
 
     while not block.end():
         s = block.read()
-        print "len(s)",len(s)
+        
         if len(s) == 3:
             print "s[0] = ", s[0]
             print "s[1] = ", s[1]
             print "s[2] = ", s[2]
             
             if moves_from:
-                print moves_from
-                print moves_to
+                print "fr: ", moves_from
+                print "to: ", moves_to
 
-        if s.is_command('move') and s[0] not in moves_from:
-            moves_from.append(s[0])
-            moves_to.append(s[1])
+        if s.is_command('move') and s[0] not in moves_to:
+            moves_from.append(s[1])
+            moves_to.append(s[0])
             print "Added move to list."
         elif s.is_command('move'):
             for i in xrange(len(moves_to)):
                 if moves_to[i] == s[0]:
                     moves_from[i] = s[1]
         elif len(s) == 3 and s[0] in moves_to:
+            print len(s)
+            print len(moves_to)
             for i in xrange(len(moves_to)):
                 if moves_to[i] == s[0]:
                     del moves_to[i]
@@ -148,5 +150,6 @@ def copy_propagation(block):
                 if s[2] == moves_to[i]:
                     s[2] = moves_from[i]
                     print "Propagated"
-                
+                    
+        print ""       
     return False

+ 24 - 10
tests/test_optimize_advanced.py

@@ -14,17 +14,31 @@ class TestOptimizeAdvanced(unittest.TestCase):
     def test_eliminate_common_subexpressions(self):
         pass
         
-    def test_copy_propagation_true(self):
-#        block = B([self.foo,
+    def test_copy_propagation_true(self):        
+        print "testing true"
+        block = B([self.foo,
+                   S('command', 'move', '$1', '$2'),
+                   self.foo,
+                   S('command', 'addu', '$3', '$1', '$4'),
+                   self.bar])
+                   
+        copy_propagation(block)
+        self.assertEqual(block.statements, [self.foo,
+                   S('command', 'move', '$1', '$2'),
+                   self.foo,
+                   S('command', 'addu', '$3', '$2', '$4'),
+                   self.bar])
+        print "Test true succesfull"
+                   
+#    def test_copy_propagation_false(self):
+#        print "Testing false"
+#        arguments = [self.foo,
 #                   S('command', 'move', '$1', '$2'),
-#                   self.foo,
+#                   S('command', 'move', '$10', '$20'),
+#                   S('command', 'addu', '$1', '$5', 1),
 #                   S('command', 'addu', '$3', '$1', '$4'),
-#                   self.bar])
+#                   self.bar]
+#        block = B(arguments)
 #                   
 #        copy_propagation(block)
-#        self.assertEquals(block.statements, [self.foo,
-#                   S('command', 'move', '$1', '$2'),
-#                   self.foo,
-#                   S('command', 'addu', '$3', '$2', '$4'),
-#                   self.bar])
-        pass
+#        self.assertEqual(block.statements, arguments)