Commit 8c9efd40 authored by Jayke Meijer's avatar Jayke Meijer

Changed algebraic transformations to more generic form.

parent 5aba6a1e
...@@ -220,11 +220,11 @@ def copy_propagation(block): ...@@ -220,11 +220,11 @@ def copy_propagation(block):
def algebraic_transformations(block): def algebraic_transformations(block):
""" """
Change ineffective or useless algebraic transformations. Handled are: Change ineffective or useless algebraic transformations. Handled are:
- x = x + 0 -> remove - x = y + 0 -> x = y
- x = x - 0 -> remove - x = y - 0 -> x = y
- x = x * 1 -> remove - x = y * 1 -> x = y
- x = x * 0 -> x = 0 - x = y * 0 -> x = 0
- x = x * 2 -> x = x << 1 - x = y * 2 -> x = x << 1
""" """
changed = False changed = False
...@@ -232,10 +232,10 @@ def algebraic_transformations(block): ...@@ -232,10 +232,10 @@ def algebraic_transformations(block):
s = block.read() s = block.read()
if (s.is_command('addu') or s.is_command('subu')) and s[2] == 0: 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 changed = True
elif s.is_command('mult') and s[2] == 1: elif s.is_command('mult') and s[2] == 1:
block.replace(1, []) block.replace(1, [S('command', 'move', s[0], s[1])])
changed = True changed = True
elif s.is_command('mult') and s[2] == 0: elif s.is_command('mult') and s[2] == 0:
block.replace(1, [S('command', 'li', '$1', to_hex(0))]) block.replace(1, [S('command', 'li', '$1', to_hex(0))])
......
...@@ -78,7 +78,8 @@ class TestOptimizeAdvanced(unittest.TestCase): ...@@ -78,7 +78,8 @@ class TestOptimizeAdvanced(unittest.TestCase):
self.assertTrue(algebraic_transformations(block)) self.assertTrue(algebraic_transformations(block))
self.assertEqual(block.statements, [self.foo, self.assertEqual(block.statements, [self.foo,
self.bar]) S('command', 'move', '$1', '$2'),
self.bar])
def test_algebraic_transforms_add1(self): def test_algebraic_transforms_add1(self):
arguments = [self.foo, arguments = [self.foo,
...@@ -96,7 +97,8 @@ class TestOptimizeAdvanced(unittest.TestCase): ...@@ -96,7 +97,8 @@ class TestOptimizeAdvanced(unittest.TestCase):
self.assertTrue(algebraic_transformations(block)) self.assertTrue(algebraic_transformations(block))
self.assertEqual(block.statements, [self.foo, self.assertEqual(block.statements, [self.foo,
self.bar]) S('command', 'move', '$1', '$2'),
self.bar])
def test_algebraic_transforms_sub1(self): def test_algebraic_transforms_sub1(self):
arguments = [self.foo, arguments = [self.foo,
...@@ -124,7 +126,8 @@ class TestOptimizeAdvanced(unittest.TestCase): ...@@ -124,7 +126,8 @@ class TestOptimizeAdvanced(unittest.TestCase):
self.assertTrue(algebraic_transformations(block)) self.assertTrue(algebraic_transformations(block))
self.assertEqual(block.statements, [self.foo, self.assertEqual(block.statements, [self.foo,
self.bar]) S('command', 'move', '$1', '$2'),
self.bar])
def test_algebraic_transforms_mult2(self): def test_algebraic_transforms_mult2(self):
block = B([self.foo, block = B([self.foo,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment