Commit cbd31150 authored by Richard Torenvliet's avatar Richard Torenvliet

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

parents 42da38d6 98a981ab
...@@ -141,7 +141,10 @@ def fold_constants(block): ...@@ -141,7 +141,10 @@ def fold_constants(block):
if s.name == 'li': if s.name == 'li':
# Save value in register # Save value in register
if not isinstance(s[1], int): # Negative numbers are stored as int
register[s[0]] = int(s[1], 16) register[s[0]] = int(s[1], 16)
else:
register[s[0]] = s[1]
known.append((s[0], register[s[0]])) known.append((s[0], register[s[0]]))
elif s.name == 'move' and s[0] in register: elif s.name == 'move' and s[0] in register:
reg_to, reg_from = s reg_to, reg_from = s
...@@ -173,6 +176,7 @@ def fold_constants(block): ...@@ -173,6 +176,7 @@ def fold_constants(block):
elif s.name in ['mult', 'div'] \ elif s.name in ['mult', 'div'] \
and s[0]in register and s[1] in register: and s[0]in register and s[1] in register:
# Multiplication/division with constants # Multiplication/division with constants
print s
rs, rt = s rs, rt = s
a, b = register[rs], register[rt] a, b = register[rs], register[rt]
......
...@@ -154,7 +154,7 @@ class Statement: ...@@ -154,7 +154,7 @@ class Statement:
def get_def(self): def get_def(self):
"""Get the variable that this statement defines, if any.""" """Get the variable that this statement defines, if any."""
instr = ['move', 'addu', 'subu', 'li', 'dmfc1', 'mov.d'] instr = ['div', 'move', 'addu', 'subu', 'li', 'dmfc1', 'mov.d']
if self.is_command('mtc1'): if self.is_command('mtc1'):
return [self[1]] return [self[1]]
...@@ -178,7 +178,7 @@ class Statement: ...@@ -178,7 +178,7 @@ class Statement:
if (self.is_branch() \ if (self.is_branch() \
and not self.is_command(*['bc1f', 'bc1t', 'bct', 'bcf'])) \ and not self.is_command(*['bc1f', 'bc1t', 'bct', 'bcf'])) \
or self.is_store() or self.is_compare() \ or self.is_store() or self.is_compare() \
or self.is_command(*['mult', 'div', 'dsz', 'mtc1']): or self.is_command(*['mult', 'dsz', 'mtc1']):
if self.name == 'dsz': if self.name == 'dsz':
m = re.match('^[^(]+\(([^)]+)\)$', self[0]) m = re.match('^[^(]+\(([^)]+)\)$', self[0])
...@@ -206,7 +206,7 @@ class Statement: ...@@ -206,7 +206,7 @@ class Statement:
# Case arg2 # Case arg2
if self.is_double_arithmetic() or self.is_set_if_less() \ if self.is_double_arithmetic() or self.is_set_if_less() \
or self.is_logical() or self.is_truncate() \ or self.is_logical() or self.is_truncate() \
or self.is_command(*['addu', 'subu']): or self.is_command(*['addu', 'subu', 'div']):
if not isinstance(self[2], int): if not isinstance(self[2], int):
use.append(self[2]) use.append(self[2])
......
...@@ -99,8 +99,9 @@ class TestStatement(unittest.TestCase): ...@@ -99,8 +99,9 @@ class TestStatement(unittest.TestCase):
a = ['a'] a = ['a']
self.assertEqual(S('command', 'move', 'a', 'b').get_def(), a) self.assertEqual(S('command', 'move', 'a', 'b').get_def(), a)
self.assertEqual(S('command', 'subu', 'a', 'b').get_def(), a) self.assertEqual(S('command', 'subu', 'a', 'b', 'c').get_def(), a)
self.assertEqual(S('command', 'addu', 'a', 'b', 'c').get_def(), a) self.assertEqual(S('command', 'addu', 'a', 'b', 'c').get_def(), a)
self.assertEqual(S('command', 'div', 'a', 'b', 'c').get_def(), a)
self.assertEqual(S('command', 'sll', 'a', 'b', 'c').get_def(), a) self.assertEqual(S('command', 'sll', 'a', 'b', 'c').get_def(), a)
self.assertEqual(S('command', 'srl', 'a', 'b', 'c').get_def(), a) self.assertEqual(S('command', 'srl', 'a', 'b', 'c').get_def(), a)
self.assertEqual(S('command', 'la', 'a', '16($fp)').get_def(), a) self.assertEqual(S('command', 'la', 'a', '16($fp)').get_def(), a)
...@@ -130,7 +131,7 @@ class TestStatement(unittest.TestCase): ...@@ -130,7 +131,7 @@ class TestStatement(unittest.TestCase):
self.assertEqual(S('command', 'subu', '$3', '$1', '$2').get_use(), \ self.assertEqual(S('command', 'subu', '$3', '$1', '$2').get_use(), \
arg2) arg2)
self.assertEqual(S('command', 'mult', '$1', '$2').get_use(), arg2) self.assertEqual(S('command', 'mult', '$1', '$2').get_use(), arg2)
self.assertEqual(S('command', 'div', '$1', '$2').get_use(), arg2) self.assertEqual(S('command', 'div', '$3', '$1', '$2').get_use(), arg2)
self.assertEqual(S('command', 'move', '$2', '$1').get_use(), arg1) self.assertEqual(S('command', 'move', '$2', '$1').get_use(), arg1)
self.assertEqual(S('command', 'beq', '$1', '$2', '$L1').get_use(), \ self.assertEqual(S('command', 'beq', '$1', '$2', '$L1').get_use(), \
arg2) arg2)
......
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