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

Global optimalizations are now.

parent 01466521
No related branches found
No related tags found
No related merge requests found
......@@ -64,7 +64,7 @@ def optimize_global(statements):
lw = statements.peek()
if lw.is_command('lw') and lw[-1] == '0(%s)' % s[0]:
lw[-1] = s[2] + lw[1:]
lw[-1] = str(s[2]) + lw[-1][1:]
statements.replace(2, [lw])
continue
......@@ -78,7 +78,8 @@ def optimize_global(statements):
j, label = following
if j.is_command('j') and label.is_label(s[2]):
s[2] = label.name
s.name = 'bne'
s[2] = j[0]
statements.replace(3, [s, label])
......
......@@ -109,3 +109,110 @@ class TestOptimize(unittest.TestCase):
optimize_global(block)
self.assertEquals(block.statements, arguments)
def test_optimize_global_shift_true(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
block = B([foo,
S('command', 'sll', '$regA', '$regA', 0),
bar])
optimize_global(block)
self.assertEquals(block.statements, [foo,
bar])
def test_optimize_global_shift_false(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
arguments = [foo, \
S('command', 'sll', '$regA', '$regB', 0), \
bar]
block = B(arguments)
optimize_global(block)
self.assertEquals(block.statements, arguments)
arguments2 = [foo, \
S('command', 'sll', '$regA', '$regA', 1), \
bar]
block2 = B(arguments2)
optimize_global(block2)
self.assertEquals(block2.statements, arguments2)
def test_optimize_global_add_lw_true(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
block = B([foo,
S('command', 'add', '$regA', '$regA', 10),
S('command', 'lw', '$regB', '0($regA)'),
bar])
optimize_global(block)
self.assertEquals(block.statements, [foo,
S('command', 'lw', '$regB', '10($regA)'),
bar])
def test_optimize_global_add_lw_false(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
arguments = [foo, \
S('command', 'add', '$regA', '$regA', 10), \
S('command', 'lw', '$regB', '0($regC)'), \
bar]
block = B(arguments)
optimize_global(block)
arguments2 = [foo, \
S('command', 'add', '$regA', '$regB', 10), \
S('command', 'lw', '$regB', '0($regA)'), \
bar]
block2 = B(arguments2)
arguments3 = [foo, \
S('command', 'add', '$regA', '$regA', 10), \
S('command', 'lw', '$regB', '1($regA)'), \
bar]
block3 = B(arguments3)
optimize_global(block3)
self.assertEquals(block.statements, arguments)
self.assertEquals(block2.statements, arguments2)
self.assertEquals(block3.statements, arguments3)
# beq ..., $Lx -> bne ..., $Ly
# j $Ly $Lx:
# $Lx:
def test_optimize_global_beq_j_true(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
block = B([foo,
S('command', 'beq', '$regA', '$regB', '$Lx'),
S('command', 'j', '$Ly'),
S('label', '$Lx'),
bar])
optimize_global(block)
self.assertEquals(block.statements, [foo,
S('command', 'bne', '$regA', '$regB', '$Ly'),
S('label', '$Lx'),
bar])
def test_optimize_global_beq_j_false(self):
foo = S('command', 'foo')
bar = S('command', 'bar')
arguments = [foo, \
S('command', 'beq', '$regA', '$regB', '$Lz'), \
S('command', 'j', '$Ly'), \
S('label', '$Lx'), \
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