advanced.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from src.statement import Statement as S
  2. def eliminate_common_subexpressions(block):
  3. """
  4. Common subexpression elimination:
  5. - Traverse through the statements in reverse order.
  6. - If the statement can be possibly be eliminated, walk further collecting
  7. all other occurrences of the expression until one of the arguments is
  8. assigned in a statement, or the start of the block has been reached.
  9. - If one or more occurrences were found, insert the expression with a new
  10. destination address before the last found occurrence and change all
  11. occurrences to a move instruction from that address.
  12. """
  13. found = False
  14. block.reverse_statements()
  15. while not block.end():
  16. s = block.read()
  17. if s.is_arith():
  18. pointer = block.pointer
  19. last = False
  20. args = s[1:]
  21. # Collect similar statements
  22. while not block.end():
  23. s2 = block.read()
  24. # Stop if one of the arguments is assigned
  25. if len(s2) and s2[0] in args:
  26. break
  27. # Replace a similar expression by a move instruction
  28. if s2.name == s.name and s2[1:] == args:
  29. block.replace(1, [S('command', 'move', s2[0], new_reg)])
  30. last = block.pointer
  31. # Insert an additional expression with a new destination address
  32. if last:
  33. block.insert(S('command', s.name, [new_reg] + args), last)
  34. found = True
  35. # Reset pointer to and continue from the original statement
  36. block.pointer = pointer
  37. block.reverse_statements()
  38. return found
  39. def fold_constants(block):
  40. """
  41. Constant folding:
  42. """
  43. return False
  44. def copy_propagtion(block):
  45. """
  46. Rename values that were copied to there original, so the copy statement
  47. might be useless, allowing it to be removed by dead code elimination.
  48. """
  49. return false