Advertisement
Guest User

optimize.py

a guest
Feb 19th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.04 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from __future__ import print_function
  4.  
  5. from collections import namedtuple
  6. from glob import iglob
  7.  
  8. # Regular expressions can be very helpful for text processing
  9. import re
  10.  
  11. # Each line has three properties:
  12. # - num (1, 2, 3, etc)
  13. # - code (no indent or comment)
  14. # - text (the whole line of text)
  15. Line = namedtuple('Line', ['num', 'code', 'text'])
  16.  
  17. # Make a list of N conditions to check in N lines
  18. # prev_lines[-1] is the previous line, prev_lines[-2] is before that one, etc
  19. conditions = [
  20. (lambda line1, prev_lines: line1.code.startswith('call ') and ',' not in line1.code),
  21. (lambda line2, prev_lines: line2.code == 'ret'),
  22. ]
  23.  
  24. # Check all the .asm files
  25. for filename in iglob('**/*.asm'):
  26.     with open(filename, 'r') as f:
  27.         printed = False
  28.         cur_label = None
  29.         prev_lines = []
  30.         state = 0
  31.         # Read each file line by line
  32.         for i, text in enumerate(f):
  33.             text = text.rstrip()
  34.             # Remove comments
  35.             code = text.split(';')[0].rstrip()
  36.             # Skip blank lines:
  37.             if not code:
  38.                 continue
  39.             # Save the most recent label for context
  40.             if code[0].isalpha() and ':' in code:
  41.                 cur_label = Line(i+1, code, text)
  42.                 continue
  43.             # Skip non-code lines
  44.             if code[0] not in {' ', '\t'}:
  45.                 continue
  46.             # This is a code line; proceed according to the state
  47.             code = code.lstrip()
  48.             cur_line = Line(i+1, code, text)
  49.             # Check the condition for the current state
  50.             condition = conditions[state]
  51.             if condition(cur_line, prev_lines):
  52.                 # The condition was met; advance to the next state
  53.                 prev_lines.append(cur_line)
  54.                 state += 1
  55.                 if state == len(conditions):
  56.                     # All the conditions were met; print the result and reset the state
  57.                     if cur_label:
  58.                         prev_lines.insert(0, cur_label)
  59.                     for line in prev_lines:
  60.                         print(filename, line.num, line.text, sep=':')
  61.                     printed = True
  62.                     prev_lines = []
  63.                     state = 0
  64.             else:
  65.                 # The condition was not met; reset the state
  66.                 prev_lines = []
  67.                 state = 0
  68.         # Print a blank line between different files
  69.         if printed:
  70.             print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement