Commit c308e7b7 authored by Taddeus Kroes's avatar Taddeus Kroes

Renamed 'glob' to 'standard' and cleaned up unit tests.

parent 12e64574
from src.dataflow import find_basic_blocks
from glob import redundant_move_1, redundant_move_2, \
from standard import redundant_move_1, redundant_move_2, \
redundant_move_3, redundant_move_4, redundant_load, \
redundant_shift, redundant_add
......@@ -41,8 +41,9 @@ def optimize_global(statements):
def optimize_block(statements):
"""Optimize a basic block."""
glob = [redundant_move_1, redundant_move_2, redundant_move_3, \
redundant_move_4, redundant_load, redundant_shift, redundant_add]
standard = [redundant_move_1, redundant_move_2, redundant_move_3, \
redundant_move_4, redundant_load, redundant_shift, \
redundant_add]
old_len = -1
while old_len != len(statements):
......@@ -51,9 +52,10 @@ def optimize_block(statements):
while not statements.end():
s = statements.read()
#
cont = False
for callback in glob:
for callback in standard:
if callback(s, statements):
cont = True
break
......
import unittest
from src.optimize import optimize_global, optimize_block#, optimize_blocks
from src.optimize import optimize_global, optimize_block
from src.statement import Statement as S, Block as B
class TestOptimize(unittest.TestCase):
def setUp(self):
pass
self.foo = S('command', 'foo')
self.bar = S('command', 'bar')
def tearDown(self):
del self.foo
del self.bar
def test_optimize_block_movaa(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
block = B([foo,
block = B([self.foo,
S('command', 'move', '$regA', '$regA'),
bar])
self.bar])
optimize_block(block)
self.assertEquals(block.statements, [foo, bar])
self.assertEquals(block.statements, [self.foo, self.bar])
def test_optimize_block_movab(self):
foo = S('command', 'foo')
move = S('command', 'move', '$regA', '$regB')
bar = S('command', 'bar')
block = B([foo,
block = B([self.foo,
move,
bar])
self.bar])
optimize_block(block)
self.assertEquals(block.statements, [foo, move, bar])
self.assertEquals(block.statements, [self.foo, move, self.bar])
def test_optimize_block_movinst_true(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
block = B([foo,
block = B([self.foo,
S('command', 'move', '$regA', '$regB'),
S('command', 'addu', '$regA', '$regA', 2),
bar])
self.bar])
optimize_block(block)
self.assertEquals(block.statements, [foo,
self.assertEquals(block.statements, [self.foo,
S('command', 'addu', '$regA', '$regB', 2),
bar])
self.bar])
def test_optimize_block_movinst_false(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
statements = [foo, \
statements = [self.foo, \
S('command', 'move', '$regA', '$regB'), \
S('command', 'addu', '$regA', '$regC', 2), \
bar]
self.bar]
block = B(statements)
optimize_block(block)
self.assertEquals(block.statements, statements)
def test_optimize_block_instr_mov_jal_true(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
block = B([foo,
block = B([self.foo,
S('command', 'addu', '$regA', '$regC', 2),
S('command', 'move', '$4', '$regA'),
S('command', 'jal', 'L1'),
bar])
self.bar])
optimize_block(block)
self.assertEquals(block.statements, [foo,
self.assertEquals(block.statements, [self.foo,
S('command', 'addu', '$4', '$regC', 2),
S('command', 'jal', 'L1'),
bar])
self.bar])
def test_optimize_block_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]
arguments = [self.foo, \
S('command', 'addu', '$regA', '$regC', 2), \
S('command', 'move', '$3', '$regA'), \
S('command', 'jal', 'L1'), \
self.bar]
block = B(arguments)
optimize_block(block)
self.assertEquals(block.statements, arguments)
def test_optimize_block_sw_ld_true(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
block = B([foo,
block = B([self.foo,
S('command', 'sw', '$regA', '$regB'),
S('command', 'lw', '$regA', '$regB'),
bar])
self.bar])
optimize_block(block)
self.assertEquals(block.statements, [foo,
self.assertEquals(block.statements, [self.foo,
S('command', 'sw', '$regA', '$regB'),
bar])
self.bar])
def test_optimize_block_sw_ld_false(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
arguments = [foo, \
arguments = [self.foo, \
S('command', 'sw', '$regA', '$regB'), \
S('command', 'lw', '$regD', '$regC'), \
bar]
self.bar]
block = B(arguments)
optimize_block(block)
self.assertEquals(block.statements, arguments)
def test_optimize_block_shift_true(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
block = B([foo,
block = B([self.foo,
S('command', 'sll', '$regA', '$regA', 0),
bar])
self.bar])
optimize_block(block)
self.assertEquals(block.statements, [foo, bar])
self.assertEquals(block.statements, [self.foo, self.bar])
def test_optimize_block_shift_false(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
arguments = [foo, \
arguments = [self.foo, \
S('command', 'sll', '$regA', '$regB', 0), \
bar]
self.bar]
block = B(arguments)
optimize_block(block)
self.assertEquals(block.statements, arguments)
arguments2 = [foo, \
arguments2 = [self.foo, \
S('command', 'sll', '$regA', '$regA', 1), \
bar]
self.bar]
block2 = B(arguments2)
optimize_block(block2)
self.assertEquals(block2.statements, arguments2)
def test_optimize_block_add_lw_true(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
block = B([foo,
block = B([self.foo,
S('command', 'addu', '$regA', '$regA', 10),
S('command', 'lw', '$regB', '0($regA)'),
bar])
self.bar])
optimize_block(block)
self.assertEquals(block.statements, [foo,
self.assertEquals(block.statements, [self.foo,
S('command', 'lw', '$regB', '10($regA)'),
bar])
self.bar])
def test_optimize_block_add_lw_false(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
arguments = [foo, \
arguments = [self.foo, \
S('command', 'addu', '$regA', '$regA', 10), \
S('command', 'lw', '$regB', '0($regC)'), \
bar]
self.bar]
block = B(arguments)
optimize_block(block)
arguments2 = [foo, \
arguments2 = [self.foo, \
S('command', 'addu', '$regA', '$regB', 10), \
S('command', 'lw', '$regB', '0($regA)'), \
bar]
self.bar]
block2 = B(arguments2)
arguments3 = [foo, \
arguments3 = [self.foo, \
S('command', 'addu', '$regA', '$regA', 10), \
S('command', 'lw', '$regB', '1($regA)'), \
bar]
self.bar]
block3 = B(arguments3)
optimize_block(block3)
......@@ -184,100 +156,70 @@ class TestOptimize(unittest.TestCase):
self.assertEquals(block3.statements, arguments3)
def test_optimize_global_beq_j_true(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
block = B([foo,
block = B([self.foo,
S('command', 'beq', '$regA', '$regB', '$Lx'),
S('command', 'j', '$Ly'),
S('label', '$Lx'),
bar])
self.bar])
optimize_global(block)
self.assertEquals(block.statements, [foo,
self.assertEquals(block.statements, [self.foo,
S('command', 'bne', '$regA', '$regB', '$Ly'),
S('label', '$Lx'),
bar])
self.bar])
def test_optimize_global_beq_j_false(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
arguments = [foo, \
arguments = [self.foo, \
S('command', 'beq', '$regA', '$regB', '$Lz'), \
S('command', 'j', '$Ly'), \
S('label', '$Lx'), \
bar]
self.bar]
block = B(arguments)
optimize_global(block)
self.assertEquals(block.statements, arguments)
def test_optimize_global_bne_j_true(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
block = B([foo,
block = B([self.foo,
S('command', 'bne', '$regA', '$regB', '$Lx'),
S('command', 'j', '$Ly'),
S('label', '$Lx'),
bar])
self.bar])
optimize_global(block)
self.assertEquals(block.statements, [foo,
self.assertEquals(block.statements, [self.foo,
S('command', 'beq', '$regA', '$regB', '$Ly'),
S('label', '$Lx'),
bar])
self.bar])
def test_optimize_global_bne_j_false(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
arguments = [foo, \
arguments = [self.foo, \
S('command', 'bne', '$regA', '$regB', '$Lz'), \
S('command', 'j', '$Ly'), \
S('label', '$Lx'), \
bar]
self.bar]
block = B(arguments)
optimize_global(block)
self.assertEquals(block.statements, arguments)
def test_optimize_block_move_move_true(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
block = B([foo,
block = B([self.foo,
S('command', 'move', '$regA', '$regB'),
S('command', 'move', '$regB', '$regA'),
bar])
self.bar])
optimize_block(block)
self.assertEquals(block.statements, [foo,
self.assertEquals(block.statements, [self.foo,
S('command', 'move', '$regA', '$regB'),
bar])
self.bar])
def test_optimize_block_mov_mov_false(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
arguments = [foo, \
arguments = [self.foo, \
S('command', 'move', '$regA', '$regB'), \
S('command', 'move', '$regB', '$regC'), \
bar]
self.bar]
block = B(arguments)
optimize_block(block)
self.assertEquals(block.statements, arguments)
#def test_optimize_blocks(self):
# foo = S('command', 'foo')
# bar = S('command', 'bar')
# block1 = B([foo, bar])
# block2 = B([bar, foo])
# blocks_in = [block1, block2];
# blocks_out = optimize_blocks(blocks_in)
# self.assertEquals(blocks_in, blocks_out)
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