statement.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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, name=None):
  36. return self.stype == 'command' if name == None \
  37. else self.stype == 'command' and self.name == name
  38. def is_jump(self):
  39. """Check if the statement is a jump."""
  40. return self.is_command() \
  41. and re.match('^j|jal|beq|bne|blez|bgtz|bltz|bgez|bct|bcf$', \
  42. self.name)
  43. def is_branch(self):
  44. """Check if the statement is a branch."""
  45. return self.is_command() \
  46. and re.match('^beq|bne|blez|bgtz|bltz|bgez|bct|bcf$', \
  47. self.name)
  48. def is_shift(self):
  49. """Check if the statement is a shift operation."""
  50. return self.is_command() and re.match('^s(ll|la|rl|ra)$', self.name)
  51. def is_load(self):
  52. """Check if the statement is a load instruction."""
  53. return self.is_command() and self.name in ['lw', 'dlw', 'l.s', 'l.d']
  54. def is_arith(self):
  55. """Check if the statement is an arithmetic operation."""
  56. return self.is_command() \
  57. and re.match('^(add|sub|mult|div|abs|neg)(u|\.d)?$', self.name)
  58. def jump_target(self):
  59. """Get the jump target of this statement."""
  60. if not self.is_jump():
  61. raise Exception('Command "%s" has no jump target' % self.name)
  62. return self[-1]
  63. class Block:
  64. def __init__(self, statements=[]):
  65. self.statements = statements
  66. self.pointer = 0
  67. def __iter__(self):
  68. return iter(self.statements)
  69. def __getitem__(self, n):
  70. return self.statements[n]
  71. def __len__(self):
  72. return len(self.statements)
  73. def read(self, count=1):
  74. """Read the statement at the current pointer position and move the
  75. pointer one position to the right."""
  76. s = self.statements[self.pointer]
  77. self.pointer += 1
  78. return s
  79. def end(self):
  80. """Check if the pointer is at the end of the statement list."""
  81. return self.pointer == len(self)
  82. def peek(self, count=1):
  83. """Read the statements until an offset from the current pointer
  84. position."""
  85. return self.statements[self.pointer] if count == 1 \
  86. else self.statements[self.pointer:self.pointer + count]
  87. def replace(self, count, replacement, start=None):
  88. """Replace the given range start-(start + count) with the given
  89. statement list, and move the pointer to the first statement after the
  90. replacement."""
  91. if self.pointer == 0:
  92. raise Exception('No statement have been read yet.')
  93. if start == None:
  94. start = self.pointer - 1
  95. before = self.statements[:start]
  96. after = self.statements[start + count:]
  97. self.statements = before + replacement + after
  98. self.pointer = start + len(replacement)
  99. def apply_filter(self, callback):
  100. """Apply a filter to the statement list. If the callback returns True,
  101. the statement will remain in the list.."""
  102. self.statements = filter(callback, self.statements)