statement.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import re
  2. class Statement:
  3. def __init__(self, stype, name, *args, **kwargs):
  4. self.stype = stype
  5. self.name = name
  6. self.args = list(args)
  7. self.options = kwargs
  8. def __getitem__(self, n):
  9. """Get an argument."""
  10. return self.args[n]
  11. def __setitem__(self, n, value):
  12. """Set an argument."""
  13. self.args[n] = value
  14. def __eq__(self, other):
  15. """Check if two statements are equal by comparing their type, name and
  16. arguments."""
  17. return self.stype == other.stype and self.name == other.name \
  18. and self.args == other.args
  19. def __len__(self):
  20. return len(self.args)
  21. def __str__(self): # pragma: nocover
  22. return '<Statement type=%s name=%s args=%s>' \
  23. % (self.stype, self.name, self.args)
  24. def __repr__(self): # pragma: nocover
  25. return str(self)
  26. def is_comment(self):
  27. return self.stype == 'comment'
  28. def is_inline_comment(self):
  29. return self.is_comment() and self.options['inline']
  30. def is_directive(self):
  31. return self.stype == 'directive'
  32. def is_label(self, name=None):
  33. return self.stype == 'label' if name == None \
  34. else self.stype == 'label' and self.name == name
  35. def is_command(self, *args):
  36. return self.stype == 'command' and (not len(args) or self.name in args)
  37. def is_jump(self):
  38. """Check if the statement is a jump."""
  39. return self.is_command() \
  40. and re.match('^j|jal|beq|bne|blez|bgtz|bltz|bgez|bct|bcf$', \
  41. self.name)
  42. def is_branch(self):
  43. """Check if the statement is a branch."""
  44. return self.is_command() \
  45. and re.match('^beq|bne|blez|bgtz|bltz|bgez|bct|bcf$', \
  46. self.name)
  47. def is_shift(self):
  48. """Check if the statement is a shift operation."""
  49. return self.is_command() and re.match('^s(ll|la|rl|ra)$', self.name)
  50. def is_load(self):
  51. """Check if the statement is a load instruction."""
  52. return self.is_command() and self.name in ['lw', 'dlw', 'l.s', 'l.d']
  53. def is_arith(self):
  54. """Check if the statement is an arithmetic operation."""
  55. return self.is_command() \
  56. and re.match('^(add|sub|mult|div|abs|neg)(u|\.d)?$', self.name)
  57. def is_monop(self):
  58. """Check if the statement is an unary operation."""
  59. return len(self) == 2 and self.is_arith()
  60. def is_binop(self):
  61. """Check if the statement is an binary operation."""
  62. return self.is_command() and len(self) == 3 and not self.is_jump()
  63. def jump_target(self):
  64. """Get the jump target of this statement."""
  65. if not self.is_jump():
  66. raise Exception('Command "%s" has no jump target' % self.name)
  67. return self[-1]
  68. def defines(self, reg):
  69. """Check if this statement defines the given register."""
  70. # TODO: Finish
  71. return (self.is_load() or self.is_arith()) and self[0] == reg
  72. def uses(self, reg):
  73. """Check if this statement uses the given register."""
  74. # TODO: Finish
  75. return (self.is_load() or self.is_arith()) and reg in self[1:]
  76. class Block:
  77. def __init__(self, statements=[]):
  78. self.statements = statements
  79. self.pointer = 0
  80. def __iter__(self):
  81. return iter(self.statements)
  82. def __getitem__(self, n):
  83. return self.statements[n]
  84. def __len__(self):
  85. return len(self.statements)
  86. def read(self, count=1):
  87. """Read the statement at the current pointer position and move the
  88. pointer one position to the right."""
  89. s = self.statements[self.pointer]
  90. self.pointer += 1
  91. return s
  92. def end(self):
  93. """Check if the pointer is at the end of the statement list."""
  94. return self.pointer == len(self)
  95. def peek(self, count=1):
  96. """Read the statements until an offset from the current pointer
  97. position."""
  98. if self.end():
  99. return Statement('empty', '') if count == 1 else []
  100. return self.statements[self.pointer] if count == 1 \
  101. else self.statements[self.pointer:self.pointer + count]
  102. def replace(self, count, replacement, start=None):
  103. """Replace the given range start-(start + count) with the given
  104. statement list, and move the pointer to the first statement after the
  105. replacement."""
  106. if self.pointer == 0:
  107. raise Exception('No statement have been read yet.')
  108. if start == None:
  109. start = self.pointer - 1
  110. before = self.statements[:start]
  111. after = self.statements[start + count:]
  112. self.statements = before + replacement + after
  113. self.pointer = start + len(replacement)
  114. def insert(self, statement, index=None):
  115. if index == None:
  116. index = self.pointer
  117. self.statements.insert(index, statement)
  118. def apply_filter(self, callback):
  119. """Apply a filter to the statement list. If the callback returns True,
  120. the statement will remain in the list.."""
  121. self.statements = filter(callback, self.statements)
  122. def reverse_statements(self):
  123. """Reverse the statement list and reset the pointer."""
  124. self.statements = self.statements[::-1]
  125. self.pointer = 0