statement.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import re
  2. class Statement:
  3. sid = 1
  4. def __init__(self, stype, name, *args, **kwargs):
  5. self.stype = stype
  6. self.name = name
  7. self.args = list(args)
  8. self.options = kwargs
  9. # Assign a unique ID to each statement
  10. self.sid = Statement.sid
  11. Statement.sid += 1
  12. def __getitem__(self, n):
  13. """Get an argument."""
  14. return self.args[n]
  15. def __setitem__(self, n, value):
  16. """Set an argument."""
  17. self.args[n] = value
  18. def __eq__(self, other):
  19. """Check if two statements are equal by comparing their type, name and
  20. arguments."""
  21. return self.stype == other.stype and self.name == other.name \
  22. and self.args == other.args
  23. def __len__(self):
  24. return len(self.args)
  25. def __str__(self): # pragma: nocover
  26. return '<Statement sid=%d type=%s name=%s args=%s>' \
  27. % (self.sid, self.stype, self.name, self.args)
  28. def __repr__(self): # pragma: nocover
  29. return str(self)
  30. def is_comment(self):
  31. return self.stype == 'comment'
  32. def is_inline_comment(self):
  33. return self.is_comment() and self.options['inline']
  34. def is_directive(self):
  35. return self.stype == 'directive'
  36. def is_label(self, name=None):
  37. return self.stype == 'label' if name == None \
  38. else self.stype == 'label' and self.name == name
  39. def is_command(self, *args):
  40. return self.stype == 'command' and (not len(args) or self.name in args)
  41. def is_jump(self):
  42. """Check if the statement is a jump."""
  43. return self.is_command() \
  44. and re.match('^j|jal|beq|bne|blez|bgtz|bltz|bgez|bct|bcf$', \
  45. self.name)
  46. def is_branch(self):
  47. """Check if the statement is a branch."""
  48. return self.is_command() \
  49. and re.match('^beq|bne|blez|bgtz|bltz|bgez|bct|bcf$', \
  50. self.name)
  51. def is_branch_zero(self):
  52. """Check if statement is a branch that compares with zero."""
  53. return self.is_command() \
  54. and re.match('^blez|bgtz|bltz|bgez$', self.name)
  55. def is_shift(self):
  56. """Check if the statement is a shift operation."""
  57. return self.is_command() and re.match('^s(ll|rl|ra)$', self.name)
  58. def is_load(self):
  59. """Check if the statement is a load instruction."""
  60. return self.is_command() and self.name in ['lw', 'li', 'dlw', 'l.s', \
  61. 'l.d']
  62. def is_store(self):
  63. """Check if the statement is a store instruction."""
  64. return self.is_command() and self.name in ['sw', 's.d', 'dsw', 's.s', \
  65. 's.b']
  66. def is_arith(self):
  67. """Check if the statement is an arithmetic operation."""
  68. return self.is_command() \
  69. and re.match('^s(ll|rl|ra)'
  70. + '|(mfhi|mflo|abs|neg|and|[xn]?or)'
  71. + '|(add|sub|slt)u?'
  72. + '|(add|sub|mult|div|abs|neg|sqrt|c)\.[sd]$', \
  73. self.name)
  74. def is_monop(self):
  75. """Check if the statement is an unary operation."""
  76. return len(self) == 2 and self.is_arith()
  77. def is_binop(self):
  78. """Check if the statement is an binary operation."""
  79. return self.is_command() and len(self) == 3 and not self.is_jump()
  80. def is_load_non_immediate(self):
  81. """Check if the statement is a load statement."""
  82. return self.is_command() \
  83. and re.match('^l(w|a|b|bu|\.d|\.s)|dlw$', \
  84. self.name)
  85. def is_logical(self):
  86. """Check if the statement is a logical operator."""
  87. return self.is_command() and re.match('^(xor|or|and)i?$', self.name)
  88. def is_double_arithmetic(self):
  89. """Check if the statement is a arithmetic .d operator."""
  90. return self.is_command() and \
  91. re.match('^(add|sub|div|mul)\.d$', self.name)
  92. def is_double_unary(self):
  93. """Check if the statement is a unary .d operator."""
  94. return self.is_command() and \
  95. re.match('^(abs|neg|mov)\.d$', self.name)
  96. def is_move_from_spec(self):
  97. """Check if the statement is a move from the result register."""
  98. return self.is_command() and self.name in ['mflo', 'mthi']
  99. def is_set_if_less(self):
  100. """Check if the statement is a shift if less then."""
  101. return self.is_command() and self.name in ['slt', 'sltu']
  102. def is_convert(self):
  103. """Check if the statement is a convert operator."""
  104. return self.is_command() and re.match('^cvt\.[a-z\.]*$', self.name)
  105. def is_truncate(self):
  106. """Check if the statement is a convert operator."""
  107. return self.is_command() and re.match('^trunc\.[a-z\.]*$', self.name)
  108. def is_compare(self):
  109. """Check if the statement is a comparison."""
  110. return self.is_command() and re.match('^c\.[a-z\.]*$', self.name)
  111. def jump_target(self):
  112. """Get the jump target of this statement."""
  113. if not self.is_jump():
  114. raise Exception('Command "%s" has no jump target' % self.name)
  115. return self[-1]
  116. def get_def(self):
  117. """Get the variable that this statement defines, if any."""
  118. instr = ['move', 'addu', 'subu', 'li', 'mtc1', 'dmfc1', 'mov.d']
  119. if self.is_load_non_immediate() or self.is_arith() \
  120. or self.is_logical() or self.is_double_arithmetic() \
  121. or self.is_move_from_spec() or self.is_double_unary() \
  122. or self.is_set_if_less() or self.is_convert() \
  123. or self.is_truncate() or self.is_load() \
  124. or self.is_command(*instr):
  125. return self[:1]
  126. return []
  127. def get_use(self):
  128. """Get the variables that this statement uses, if any."""
  129. instr = ['addu', 'subu', 'mult', 'div', 'move', 'mtc1', 'mov.d', \
  130. 'dmfc1']
  131. use = []
  132. # Case arg0
  133. if self.is_branch() or self.is_store() or self.is_compare() \
  134. or self.is_command(*['mult', 'div', 'dsz']):
  135. if self.name == 'dsz':
  136. m = re.match('^\d+\(([^)]+)\)$', self[0])
  137. if m:
  138. use.append(m.group(1))
  139. else:
  140. use.append(self[0])
  141. # Case arg1 direct adressing
  142. if (self.is_branch() and not self.is_branch_zero()) or self.is_shift()\
  143. or self.is_double_arithmetic() or self.is_double_unary() \
  144. or self.is_logical() or self.is_convert() \
  145. or self.is_truncate() or self.is_set_if_less() \
  146. or self.is_command(*instr):
  147. use.append(self[1])
  148. # Case arg1 relative adressing
  149. if self.is_load_non_immediate() or self.is_store():
  150. m = re.match('^\d+\(([^)]+)\)$', self[1])
  151. if m:
  152. use.append(m.group(1))
  153. else:
  154. use.append(self[1])
  155. # Case arg2
  156. if self.is_double_arithmetic() or self.is_set_if_less() \
  157. or self.is_logical() \
  158. or self.is_command(*['addu', 'subu']):
  159. if not isinstance(self[2], int):
  160. use.append(self[2])
  161. return use
  162. def defines(self, reg):
  163. """Check if this statement defines the given register."""
  164. return reg in self.get_def()
  165. def uses(self, reg):
  166. """Check if this statement uses the given register."""
  167. return reg in self.get_use()
  168. class Block:
  169. bid = 1
  170. def __init__(self, statements=[]):
  171. self.statements = statements
  172. self.pointer = 0
  173. # Assign a unique ID to each block for printing purposes
  174. self.bid = Block.bid
  175. Block.bid += 1
  176. def __str__(self):
  177. return '<Block bid=%d statements=%d>' % (self.bid, len(self))
  178. def __repr__(self):
  179. return str(self)
  180. def __iter__(self):
  181. return iter(self.statements)
  182. def __getitem__(self, n):
  183. return self.statements[n]
  184. def __len__(self):
  185. return len(self.statements)
  186. def read(self, count=1):
  187. """Read the statement at the current pointer position and move the
  188. pointer one position to the right."""
  189. s = self.statements[self.pointer]
  190. self.pointer += 1
  191. return s
  192. def end(self):
  193. """Check if the pointer is at the end of the statement list."""
  194. return self.pointer >= len(self)
  195. def peek(self, count=1):
  196. """Read the statements until an offset from the current pointer
  197. position."""
  198. if self.end():
  199. return Statement('empty', '') if count == 1 else []
  200. return self.statements[self.pointer] if count == 1 \
  201. else self.statements[self.pointer:self.pointer + count]
  202. def replace(self, count, replacement, start=None):
  203. """Replace the given range start-(start + count) with the given
  204. statement list, and move the pointer to the first statement after the
  205. replacement."""
  206. if self.pointer == 0:
  207. raise Exception('No statement have been read yet.')
  208. if start == None:
  209. start = self.pointer - 1
  210. before = self.statements[:start]
  211. after = self.statements[start + count:]
  212. self.statements = before + replacement + after
  213. self.pointer = start + len(replacement)
  214. def insert(self, statement, index=None):
  215. if index == None:
  216. index = self.pointer
  217. self.statements.insert(index, statement)
  218. def apply_filter(self, callback):
  219. """Apply a filter to the statement list. If the callback returns True,
  220. the statement will remain in the list.."""
  221. self.statements = filter(callback, self.statements)
  222. def reverse_statements(self):
  223. """Reverse the statement list and reset the pointer."""
  224. self.statements = self.statements[::-1]
  225. self.pointer = 0