Skip to content
Snippets Groups Projects
Commit 9706e239 authored by Richard Torenvliet's avatar Richard Torenvliet
Browse files

Merge branch 'master' of github.com:taddeus/peephole

parents e97d3e56 bb1f3ba0
No related branches found
No related tags found
No related merge requests found
...@@ -21,6 +21,9 @@ class Statement: ...@@ -21,6 +21,9 @@ class Statement:
arguments.""" arguments."""
return self.stype == other.stype and self.name == other.name \ return self.stype == other.stype and self.name == other.name \
and self.args == other.args and self.args == other.args
def __len__(self):
return len(self.args)
def __str__(self): # pragma: nocover def __str__(self): # pragma: nocover
return '<Statement type=%s name=%s args=%s>' \ return '<Statement type=%s name=%s args=%s>' \
...@@ -115,8 +118,10 @@ class Block: ...@@ -115,8 +118,10 @@ class Block:
"""Replace the given range start-(start + count) with the given """Replace the given range start-(start + count) with the given
statement list, and move the pointer to the first statement after the statement list, and move the pointer to the first statement after the
replacement.""" replacement."""
if self.pointer == 0:
raise Exception('No statement have been read yet.')
if start == None: if start == None:
start = self.pointer start = self.pointer - 1
before = self.statements[:start] before = self.statements[:start]
after = self.statements[start + count:] after = self.statements[start + count:]
......
No preview for this file type
...@@ -9,5 +9,48 @@ class TestOptimize(unittest.TestCase): ...@@ -9,5 +9,48 @@ class TestOptimize(unittest.TestCase):
def setUp(self): def setUp(self):
pass pass
def test_(self): def test_optimize_global_movaa(self):
pass foo = S('command', 'foo')
bar = S('command', 'bar')
block = B([foo,
S('command', 'move', '$regA', '$regA'),
bar])
optimize_global(block)
self.assertEquals(block.statements, [foo, bar])
def test_optimize_global_movab(self):
foo = S('command', 'foo')
move = S('command', 'move', '$regA', '$regB')
bar = S('command', 'bar')
block = B([foo,
move,
bar])
optimize_global(block)
self.assertEquals(block.statements, [foo, move, bar])
def test_optimize_global_movinst_true(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
block = B([foo,
S('command', 'move', '$regA', '$regB'),
S('command', 'addu', '$regA', '$regA', 2),
bar])
optimize_global(block)
self.assertEquals(block.statements, [foo,
S('command', 'addu', '$regA', '$regB', 2),
bar])
def test_optimize_global_movinst_false(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
block = B([foo,
S('command', 'move', '$regA', '$regB'),
S('command', 'addu', '$regA', '$regC', 2),
bar])
optimize_global(block)
self.assertEquals(block.statements, [foo,
S('command', 'move', '$regA', '$regB'),
S('command', 'addu', '$regA', '$regC', 2),
bar])
...@@ -67,6 +67,7 @@ class TestStatement(unittest.TestCase): ...@@ -67,6 +67,7 @@ class TestStatement(unittest.TestCase):
self.assertEqual(self.block.peek(), S('comment', 'bar')) self.assertEqual(self.block.peek(), S('comment', 'bar'))
def test_replace(self): def test_replace(self):
self.block.read()
self.block.replace(1, [S('command', 'foobar')]) self.block.replace(1, [S('command', 'foobar')])
self.assertEqual(self.block.pointer, 1) self.assertEqual(self.block.pointer, 1)
self.assertEqual(self.block.statements, [S('command', 'foobar'), \ self.assertEqual(self.block.statements, [S('command', 'foobar'), \
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment