Commit baff0f9f authored by Jayke Meijer's avatar Jayke Meijer

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

parents f0f259ea 620544cc
...@@ -173,13 +173,11 @@ def fold_constants(block): ...@@ -173,13 +173,11 @@ def fold_constants(block):
# Move of `Hi' register to another register # Move of `Hi' register to another register
register[s[0]] = register['$hi'] register[s[0]] = register['$hi']
known.append((s[0], register[s[0]])) known.append((s[0], register[s[0]]))
elif s.name in ['mult', 'div'] \ elif s.name == 'mult' 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
rs, rt = s rs, rt = s
a, b = register[rs], register[rt] a, b = register[rs], register[rt]
if s.name == 'mult':
if not a or not b: if not a or not b:
# Multiplication by 0 # Multiplication by 0
hi = lo = to_hex(0) hi = lo = to_hex(0)
...@@ -209,13 +207,11 @@ def fold_constants(block): ...@@ -209,13 +207,11 @@ def fold_constants(block):
block.replace(1, [S('command', 'li', '$hi', hi), block.replace(1, [S('command', 'li', '$hi', hi),
S('command', 'li', '$lo', li)], S('command', 'li', '$lo', li)],
message=message) message=message)
elif s.name == 'div':
lo, hi = divmod(rs, rt)
register['$lo'], register['$hi'] = lo, hi register['$lo'], register['$hi'] = lo, hi
known += [('$lo', lo), ('$hi', hi)] known += [('$lo', lo), ('$hi', hi)]
changed = True changed = True
elif s.name in ['addu', 'subu']: elif s.name in ['addu', 'subu', 'div']:
# Addition/subtraction with constants # Addition/subtraction with constants
rd, rs, rt = s rd, rs, rt = s
rs_known = rs in register rs_known = rs in register
...@@ -239,6 +235,11 @@ def fold_constants(block): ...@@ -239,6 +235,11 @@ def fold_constants(block):
message = 'Constant subtraction: %d - %d = %d' \ message = 'Constant subtraction: %d - %d = %d' \
% (rs_val, rt_val, result) % (rs_val, rt_val, result)
if s.name == 'div':
result = rs_val / rt_val
message = 'Constant division: %d - %d = %d' \
% (rs_val, rt_val, result)
block.replace(1, [S('command', 'li', rd, to_hex(result))], block.replace(1, [S('command', 'li', rd, to_hex(result))],
message=message) message=message)
register[rd] = result register[rd] = result
......
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