statement.py 11 KB

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