Просмотр исходного кода

Added unittests and fixed problems that arose from that.

Jayke Meijer 14 лет назад
Родитель
Сommit
e379672421
3 измененных файлов с 65 добавлено и 6 удалено
  1. 6 3
      src/optimize.py
  2. 1 0
      src/statement.py
  3. 58 3
      tests/test_optimize.py

+ 6 - 3
src/optimize.py

@@ -1,3 +1,5 @@
+import re
+
 from dataflow import find_basic_blocks
 
 
@@ -36,9 +38,10 @@ def optimize_global(statements):
                     mov, jal = following
 
                     if mov.is_command('move') and mov[1] == s[0] \
+                            and re.match('^\$[4-7]$', mov[0]) \
                             and jal.is_command('jal'):
                         s[0] = mov[0]
-                        statements.replace(1, [], start=statements.pointer + 1)
+                        statements.replace(2, [s])
                         continue
 
             # sw $regA, XX              ->  sw $regA, XX
@@ -46,8 +49,8 @@ def optimize_global(statements):
             if s.is_command('sw'):
                 ld = statements.peek()
 
-                if ld.is_command('ld') and ld.args == s.args:
-                    statements.replace(2, [ld])
+                if ld.is_command('ld') and ld[0] == s[0]:
+                    statements.replace(2, [s])
                     continue
 
             # shift $regA, $regA, 0     ->  --- remove it

+ 1 - 0
src/statement.py

@@ -120,6 +120,7 @@ class Block:
         replacement."""
         if self.pointer == 0:
             raise Exception('No statement have been read yet.')
+        
         if start == None:
             start = self.pointer - 1
 

+ 58 - 3
tests/test_optimize.py

@@ -44,13 +44,68 @@ class TestOptimize(unittest.TestCase):
     def test_optimize_global_movinst_false(self):
         foo = S('command', 'foo')
         bar = S('command', 'bar')
+        statements = [foo, \
+                      S('command', 'move', '$regA', '$regB'), \
+                      S('command', 'addu', '$regA', '$regC', 2), \
+                      bar]
+        
+        block = B(statements)
+        optimize_global(block)
+        self.assertEquals(block.statements, statements)
+                
+    def test_optimize_global_instr_mov_jal_true(self):
+        foo = S('command', 'foo')
+        bar = S('command', 'bar')
         
         block = B([foo,
-                   S('command', 'move', '$regA', '$regB'),
                    S('command', 'addu', '$regA', '$regC', 2),
+                   S('command', 'move', '$4', '$regA'),
+                   S('command', 'jal', 'L1'),
                    bar])
         optimize_global(block)
+        
         self.assertEquals(block.statements, [foo,                     
-                   S('command', 'move', '$regA', '$regB'),
-                   S('command', 'addu', '$regA', '$regC', 2),      
+                   S('command', 'addu', '$4', '$regC', 2),
+                   S('command', 'jal', 'L1'),      
+                   bar])
+        
+    def test_optimize_global_instr_mov_jal_false(self):
+        foo = S('command', 'foo')
+        bar = S('command', 'bar')
+        
+        arguments = [foo, \
+                      S('command', 'addu', '$regA', '$regC', 2), \
+                      S('command', 'move', '$3', '$regA'), \
+                      S('command', 'jal', 'L1'), \
+                      bar]
+        block = B(arguments)
+        optimize_global(block)
+        
+        self.assertEquals(block.statements, arguments)
+        
+    def test_optimize_global_sw_ld_true(self):
+        foo = S('command', 'foo')
+        bar = S('command', 'bar')
+        
+        block = B([foo,
+                   S('command', 'sw', '$regA', '$regB'),
+                   S('command', 'ld', '$regA', '$regC'),
+                   bar])
+        optimize_global(block)
+        
+        self.assertEquals(block.statements, [foo,
+                   S('command', 'sw', '$regA', '$regB'),
                    bar])
+                   
+    def test_optimize_global_sw_ld_false(self):
+        foo = S('command', 'foo')
+        bar = S('command', 'bar')
+        
+        arguments = [foo, \
+                     S('command', 'sw', '$regA', '$regB'), \
+                     S('command', 'ld', '$regD', '$regC'), \
+                     bar]
+        block = B(arguments)
+        optimize_global(block)
+        
+        self.assertEquals(block.statements, arguments)