Commit 140259b0 authored by Jayke Meijer's avatar Jayke Meijer

Merge branch 'master' of github.com:taddeus/peephole

parents 98b2cba3 770cbc9f
......@@ -13,7 +13,7 @@ if __name__ == '__main__':
# Parse file
program = parse_file(argv[1])
program.debug = True
program.verbose = 1
if len(argv) > 3:
# Save input assembly in new file for easy comparison
......
......@@ -12,12 +12,24 @@ def optimize(program, verbose=0):
o = program.count_instructions()
changed = True
iterations = 0
while changed:
<<<<<<< HEAD
iterations += 1
if verbose > 1:
print 'main iteration %d', iterations
=======
>>>>>>> 98c43ff02c474a62e42ac89ba9fe20be98f9eccd
changed = False
# Optimize on a global level
if program.optimize_global():
if verbose > 1:
print 'changed on global level'
changed = True
# Perform dataflow analysis on new blocks
......@@ -25,6 +37,9 @@ def optimize(program, verbose=0):
# Optimize basic blocks
if program.optimize_blocks():
if verbose > 1:
print 'changed on block level'
changed = True
# Count number of instructions after optimization
......
......@@ -274,7 +274,7 @@ def fold_constants(block):
del register[reg]
known.append((reg, 'unknown'))
if block.debug and len(known):
if block.verbose and len(known):
s.set_inline_comment(','.join([' %s = %s' % k for k in known]))
return changed
......@@ -401,7 +401,7 @@ def eliminate_dead_code(block):
for reg in s.get_def():
if is_reg_dead_after(reg, block, n):
# Statement is redefined later, so this statement is useless
if block.debug:
if block.verbose:
s.stype = 'comment'
s.options['block'] = False
s.set_inline_comment(' dead register %s' % reg)
......@@ -411,7 +411,7 @@ def eliminate_dead_code(block):
changed = True
if not block.debug:
if not block.verbose:
block.apply_filter(lambda s: not hasattr(s, 'remove'))
return changed
......@@ -123,6 +123,7 @@ def remove_redundant_jumps(statements):
changed = False
statements.reset()
while not statements.end():
s = statements.read()
......@@ -142,6 +143,7 @@ def remove_redundant_branch_jumps(statements):
changed = False
statements.reset()
while not statements.end():
s = statements.read()
......
......@@ -21,8 +21,8 @@ class Program(Block):
if hasattr(self, 'statements'):
return self.statements
# Only add block start and end comments when in debug mode
if add_block_comments and self.debug:
# Only add block start and end comments when in verbose mode
if add_block_comments and self.verbose:
get_id = lambda b: b.bid
statements = []
......@@ -90,7 +90,7 @@ class Program(Block):
self.blocks = find_basic_blocks(self.statements)
for b in self.blocks:
b.debug = self.debug
b.verbose = self.verbose
# Remove the old statement list, since it will probably change
del self.statements
......
......@@ -231,7 +231,7 @@ class Statement:
class Block:
bid = 1
def __init__(self, statements=[], debug=False):
def __init__(self, statements=[], verbose=False):
self.statements = statements
self.pointer = 0
......@@ -239,7 +239,7 @@ class Block:
self.bid = Block.bid
Block.bid += 1
self.debug = debug
self.verbose = verbose
def __str__(self):
return '<Block bid=%d statements=%d>' % (self.bid, len(self))
......@@ -288,7 +288,7 @@ class Block:
start = self.pointer - 1
# Add a message in inline comments
if self.debug:
if self.verbose:
if len(message):
message = ' ' + message
......@@ -313,7 +313,7 @@ class Block:
if index == None:
index = self.pointer
if self.debug and len(message):
if self.verbose and len(message):
statement.set_inline_comment(' ' + message)
self.statements.insert(index, statement)
......
......@@ -64,37 +64,39 @@ class TestLiveness(unittest.TestCase):
self.assertEqual(b3.live_in, set(['b', 'd', '$4', '$5', '$6', '$7']))
self.assertEqual(b3.live_out, set(['$4', '$5', '$6', '$7']))
def test_create_in_out_two(self):
s11 = S('command', 'subu', 'i', 'm', '0x00000001')
s12 = S('command', 'move', 'j', 'n')
s13 = S('command', 'move', 'a', 'u1')
s14 = S('command', 'subu', 'i', 'm', '0x00000005')
s15 = S('command', 'j', 'L1')
s21 = S('label', 'L1')
s22 = S('command', 'addi', 'i', '0x00000001')
s23 = S('command', 'subi', 'j', '0x00000002')
s24 = S('command', 'bnq', 'i', 'j', 'L2')
s31 = S('command', 'move', 'a', 'u2')
s32 = S('command', 'j', 'L1')
s41 = S('label', 'L2')
s42 = S('command', 'move', 'i', 'u3')
s43 = S('command', 'beq', 'g', 'd', 'L3')
s51 = S('label', 'L3')
s52 = S('command', 'addu', 'b', 'i', 'a')
blocks = find_basic_blocks([s11, s12, s13, s14, s15,
s21, s22, s23, s24, s31, s32, s41, s42, s43, s51])
generate_flow_graph(blocks)
create_in_out(blocks)
for i, block in enumerate(blocks):
print 'block ', i,':\n\t in:', block.live_in
print '\t out:', block.live_out
# def test_create_in_out_two(self):
# s11 = S('command', 'subu', 'i', 'm', '0x00000001')
# s12 = S('command', 'move', 'j', 'n')
# s13 = S('command', 'move', 'a', 'u1')
# s14 = S('command', 'subu', 'i', 'm', '0x00000005')
# s15 = S('command', 'j', 'L1')
#
# s21 = S('label', 'L1')
# s22 = S('command', 'addi', 'i', '0x00000001')
# s23 = S('command', 'subi', 'j', '0x00000002')
# s24 = S('command', 'bne', 'i', 'j', 'L2')
#
# s31 = S('command', 'move', 'a', 'u2')
# s32 = S('command', 'j', 'L1')
# s41 = S('label', 'L2')
# s42 = S('command', 'move', 'i', 'u3')
# s43 = S('command', 'beq', 'i', 'j', 'L3')
#
# s51 = S('label', 'L3')
# s52 = S('command', 'addu', 'b', 'i', 'a')
#
# blocks = find_basic_blocks([s11, s12, s13, s14, s15,
# s21, s22, s23, s24, s31, s32, s41, s42, s43, s51, s52])
#
# generate_flow_graph(blocks)
# create_in_out(blocks)
#
# for i, block in enumerate(blocks):
# print 'block ', i,':\n\t in:', block.live_in
# print '\t out:', block.live_out
#
# #print blocks[-1].live_in
......
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