Skip to content
Snippets Groups Projects
Commit ead1b572 authored by Taddeus Kroes's avatar Taddeus Kroes
Browse files

Code cleanup.

parent 42be1bbf
No related branches found
No related tags found
No related merge requests found
def equal_mov(statement): def equal_mov(s):
'''Check for useless move operations.''' '''Check for useless move operations.'''
stype, name, args = statement return s.is_command() and s.name == 'move' and s[0] == s[1]
return stype == 'command' and name == 'move' \
and args['args'][0] == args['args'][1] def empty_shift(s):
def empty_shift(statement):
'''Check for useless shift operations.''' '''Check for useless shift operations.'''
shift_types = ['sll', 'sla', 'srl', 'sra'] return s.is_shift() and s[0] == s[1] and s[2] == 0
stype, name, args = statement
return stype == 'command' and name in shift_types and \
args['args'][0] == args['args'][1] and args['args'][2] == 0
def optimize_branch_jump_label(statements): def optimize_branch_jump_label(statements):
'''Optimize jumps after branches.''' '''Optimize jumps after branches.'''
out_statements = [] out_statements = []
for i in xrange(len(statements)): for i in xrange(len(statements)):
if i + 3 > len(statements): if i + 3 > len(statements):
out_statements.append(statements[i]) out_statements.append(statements[i])
continue continue
stype, name, args = statements[i] stype, name, args = statements[i]
stype2, name2, args2 = statements[i + 1] stype2, name2, args2 = statements[i + 1]
stype3, name3, args3 = statements[i + 2] stype3, name3, args3 = statements[i + 2]
if stype == 'command' and name == 'beq' and \ if stype == 'command' and name == 'beq' and \
stype2 == 'command' and name2 in ['j', 'jal'] and \ stype2 == 'command' and name2 in ['j', 'jal'] and \
stype3 == 'label' and name3 == args['args'][2]: stype3 == 'label' and name3 == args['args'][2]:
...@@ -33,45 +28,43 @@ def optimize_branch_jump_label(statements): ...@@ -33,45 +28,43 @@ def optimize_branch_jump_label(statements):
i += 3 i += 3
else: else:
out_statements.append(statements[i]) out_statements.append(statements[i])
return out_statements return out_statements
def optimize_global(statements): def optimize_global(statements):
'''Optimize one-line statements in entire code.''' '''Optimize one-line statements in entire code.'''
statements = optimize_branch_jump_label(statements) statements = optimize_branch_jump_label(statements)
return filter(lambda s: not equal_mov(s) and not empty_shift(s), statements) return filter(lambda s: not equal_mov(s) and not empty_shift(s), statements)
def optimize_blocks(blocks): def optimize_blocks(blocks):
'''Call the optimizer for each basic block. Do this several times until '''Call the optimizer for each basic block. Do this several times until
no more optimizations are achieved.''' no more optimizations are achieved.'''
changed = True changed = True
while changed: while changed:
changed = False changed = False
optimized = [] optimized = []
for block in blocks: for block in blocks:
block_changed, b = optimize_block(block) block_changed, b = optimize_block(block)
optimized.append(b) optimized.append(b)
if block_changed: if block_changed:
changed = True changed = True
blocks = optimized blocks = optimized
return reduce(lambda a, b: a + b, blocks, []) return reduce(lambda a, b: a + b, blocks, [])
def optimize_block(statements): def optimize_block(statements):
'''Optimize a basic block.''' '''Optimize a basic block.'''
changed = False changed = False
output_statements = [] output_statements = []
for statement in statements: for statement in statements:
new_statement = statement new_statement = statement
output_statements.append(new_statement) output_statements.append(new_statement)
return changed, output_statements return changed, output_statements
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