瀏覽代碼

Changed algebraic optimizations to proper mult structure.

Jayke Meijer 14 年之前
父節點
當前提交
e098d91cd5
共有 2 個文件被更改,包括 25 次插入16 次删除
  1. 17 12
      src/optimize/advanced.py
  2. 8 4
      tests/test_optimize_advanced.py

+ 17 - 12
src/optimize/advanced.py

@@ -137,7 +137,7 @@ def fold_constants(block):
             register[s[0]] = constants[s[1]]
         elif s.name in ['addu', 'subu', 'mult', 'div']:
             # Calculation with constants
-            rd, rs, rt = s
+            rd, rs, rt = s[0], s[1], s[2]
             rs_known = rs in register
             rt_known = rt in register
 
@@ -255,17 +255,22 @@ def algebraic_transformations(block):
         if (s.is_command('addu') or s.is_command('subu')) and s[2] == 0:
             block.replace(1, [S('command', 'move', s[0], s[1])])
             changed = True
-        elif s.is_command('mult') and s[2] == 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))])
-            changed = True
         elif s.is_command('mult'):
-            shift_amount = log(s[2], 2)
-            if shift_amount.is_integer():
-                new_command = S('command', 'sll', s[0], s[1], shift_amount)
-                block.replace(1, [new_command])
-                changed = True
+            next = block.peek()
+            if next.is_command('mflo'):
+                if s[1] == 1:
+                    block.replace(2, [S('command', 'move', next[0], s[0])])
+                    changed = True
+                    break
+                elif s[1] == 0:
+                    block.replace(2, [S('command', 'li', '$1', to_hex(0))])
+                    changed = True
+                    break
+                
+                shift_amount = log(s[1], 2)
+                if shift_amount.is_integer():
+                    new_command = S('command', 'sll', next[0], s[0], int(shift_amount))
+                    block.replace(2, [new_command])
+                    changed = True
 
     return changed

+ 8 - 4
tests/test_optimize_advanced.py

@@ -125,7 +125,8 @@ class TestOptimizeAdvanced(unittest.TestCase):
 
     def test_algebraic_transforms_mult0(self):
         block = B([self.foo,
-                   S('command', 'mult', '$1', '$2', 0),
+                   S('command', 'mult', '$2', 0),
+                   S('command', 'mflo', '$1'),
                    self.bar])
 
         self.assertTrue(algebraic_transformations(block))
@@ -135,7 +136,8 @@ class TestOptimizeAdvanced(unittest.TestCase):
 
     def test_algebraic_transforms_mult1(self):
         block = B([self.foo,
-                   S('command', 'mult', '$1', '$2', 1),
+                   S('command', 'mult', '$2', 1),
+                   S('command', 'mflo', '$1'),
                    self.bar])
 
         self.assertTrue(algebraic_transformations(block))
@@ -145,7 +147,8 @@ class TestOptimizeAdvanced(unittest.TestCase):
 
     def test_algebraic_transforms_mult2(self):
         block = B([self.foo,
-                   S('command', 'mult', '$1', '$2', 2),
+                   S('command', 'mult', '$2', 2),
+                   S('command', 'mflo', '$1'),
                    self.bar])
 
         self.assertTrue(algebraic_transformations(block))
@@ -155,7 +158,8 @@ class TestOptimizeAdvanced(unittest.TestCase):
 
     def test_algebraic_transforms_mult16(self):
         block = B([self.foo,
-                   S('command', 'mult', '$1', '$2', 16),
+                   S('command', 'mult', '$2', 16),
+                   S('command', 'mflo', '$1'),
                    self.bar])
 
         self.assertTrue(algebraic_transformations(block))