Skip to content
Snippets Groups Projects
Commit e3796724 authored by Jayke Meijer's avatar Jayke Meijer
Browse files

Added unittests and fixed problems that arose from that.

parent bb1f3ba0
No related branches found
No related tags found
No related merge requests found
import re
from dataflow import find_basic_blocks from dataflow import find_basic_blocks
...@@ -36,9 +38,10 @@ def optimize_global(statements): ...@@ -36,9 +38,10 @@ def optimize_global(statements):
mov, jal = following mov, jal = following
if mov.is_command('move') and mov[1] == s[0] \ if mov.is_command('move') and mov[1] == s[0] \
and re.match('^\$[4-7]$', mov[0]) \
and jal.is_command('jal'): and jal.is_command('jal'):
s[0] = mov[0] s[0] = mov[0]
statements.replace(1, [], start=statements.pointer + 1) statements.replace(2, [s])
continue continue
# sw $regA, XX -> sw $regA, XX # sw $regA, XX -> sw $regA, XX
...@@ -46,8 +49,8 @@ def optimize_global(statements): ...@@ -46,8 +49,8 @@ def optimize_global(statements):
if s.is_command('sw'): if s.is_command('sw'):
ld = statements.peek() ld = statements.peek()
if ld.is_command('ld') and ld.args == s.args: if ld.is_command('ld') and ld[0] == s[0]:
statements.replace(2, [ld]) statements.replace(2, [s])
continue continue
# shift $regA, $regA, 0 -> --- remove it # shift $regA, $regA, 0 -> --- remove it
......
...@@ -120,6 +120,7 @@ class Block: ...@@ -120,6 +120,7 @@ class Block:
replacement.""" replacement."""
if self.pointer == 0: if self.pointer == 0:
raise Exception('No statement have been read yet.') raise Exception('No statement have been read yet.')
if start == None: if start == None:
start = self.pointer - 1 start = self.pointer - 1
......
...@@ -44,13 +44,68 @@ class TestOptimize(unittest.TestCase): ...@@ -44,13 +44,68 @@ class TestOptimize(unittest.TestCase):
def test_optimize_global_movinst_false(self): def test_optimize_global_movinst_false(self):
foo = S('command', 'foo') foo = S('command', 'foo')
bar = S('command', 'bar') 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, block = B([foo,
S('command', 'move', '$regA', '$regB'),
S('command', 'addu', '$regA', '$regC', 2), S('command', 'addu', '$regA', '$regC', 2),
S('command', 'move', '$4', '$regA'),
S('command', 'jal', 'L1'),
bar]) bar])
optimize_global(block) optimize_global(block)
self.assertEquals(block.statements, [foo, self.assertEquals(block.statements, [foo,
S('command', 'move', '$regA', '$regB'), S('command', 'addu', '$4', '$regC', 2),
S('command', 'addu', '$regA', '$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]) 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)
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