Commit e3796724 authored by Jayke Meijer's avatar Jayke Meijer

Added unittests and fixed problems that arose from that.

parent bb1f3ba0
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
......
......@@ -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
......
......@@ -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)
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