Commit 2aac7d87 authored by Taddeus Kroes's avatar Taddeus Kroes

Fixed division in constant flding.

parent f971a60e
...@@ -173,49 +173,45 @@ def fold_constants(block): ...@@ -173,49 +173,45 @@ 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) message = 'Multiplication by 0: %d * 0' % (b if a else a)
message = 'Multiplication by 0: %d * 0' % (b if a else a) elif a == 1:
elif a == 1: # Multiplication by 1
# Multiplication by 1 hi = to_hex(0)
hi = to_hex(0) lo = to_hex(b)
lo = to_hex(b) message = 'Multiplication by 1: %d * 1' % b
message = 'Multiplication by 1: %d * 1' % b elif b == 1:
elif b == 1: # Multiplication by 1
# Multiplication by 1 hi = to_hex(0)
hi = to_hex(0) lo = to_hex(a)
lo = to_hex(a) message = 'Multiplication by 1: %d * 1' % a
message = 'Multiplication by 1: %d * 1' % a else:
else: # Calculate result and fill Hi/Lo registers
# Calculate result and fill Hi/Lo registers result = a * b
result = a * b binary = bin(result)[2:]
binary = bin(result)[2:] binary = '0' * (64 - len(binary)) + binary
binary = '0' * (64 - len(binary)) + binary hi = int(binary[:32], base=2)
hi = int(binary[:32], base=2) lo = int(binary[32:], base=2)
lo = int(binary[32:], base=2) message = 'Constant multiplication: %d * %d = %d' \
message = 'Constant multiplication: %d * %d = %d' \ % (a, b, result)
% (a, b, result)
# Replace the multiplication with two immidiate loads to the
# Replace the multiplication with two immidiate loads to the # Hi/Lo registers
# Hi/Lo registers 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
...@@ -232,12 +228,17 @@ def fold_constants(block): ...@@ -232,12 +228,17 @@ def fold_constants(block):
if s.name == 'addu': if s.name == 'addu':
result = rs_val + rt_val result = rs_val + rt_val
message = 'Constant addition: %d + %d = %d' \ message = 'Constant addition: %d + %d = %d' \
% (rs_val, rt_val, result) % (rs_val, rt_val, result)
if s.name == 'subu': if s.name == 'subu':
result = rs_val - rt_val result = rs_val - rt_val
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)
......
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