bytecode_ir.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. """Provides data structures that represent parsed Modelverse bytecode graphs."""
  2. class Instruction(object):
  3. """Represents a Modelverse bytecode instruction."""
  4. def __init__(self):
  5. self.next_instruction = None
  6. self.debug_information = None
  7. def get_directly_reachable(self):
  8. """Gets all instructions that are directly reachable from this instruction, excluding the
  9. next instruction."""
  10. raise NotImplementedError()
  11. def get_reachable(self, filter_children=lambda _: True):
  12. """Gets the set of all instructions that are reachable from the given instruction, including
  13. this instruction. A function can be used to filter out certain instructions' children."""
  14. results = set()
  15. stack = [self]
  16. while len(stack) > 0:
  17. instr = stack.pop()
  18. results.add(instr)
  19. next_instr = instr.next_instruction
  20. if next_instr is not None and next_instr not in results:
  21. stack.append(next_instr)
  22. if filter_children(instr):
  23. for other in instr.get_directly_reachable():
  24. if other not in results:
  25. assert other is not None
  26. stack.append(other)
  27. return results
  28. class VariableNode(object):
  29. """Represents a variable node, which has an identifier and an optional name."""
  30. def __init__(self, node_id, name):
  31. self.node_id = node_id
  32. self.name = name
  33. def __str__(self):
  34. return 'var(%d, %s)' % (self.node_id, self.name)
  35. def __repr__(self):
  36. return 'VariableNode(%r, %r)' % (self.node_id, self.name)
  37. class SelectInstruction(Instruction):
  38. """Represents an 'if/else' instruction."""
  39. def __init__(self, condition, if_clause, else_clause):
  40. Instruction.__init__(self)
  41. self.condition = condition
  42. self.if_clause = if_clause
  43. self.else_clause = else_clause
  44. def get_directly_reachable(self):
  45. """Gets all instructions that are directly reachable from this instruction."""
  46. if self.else_clause is None:
  47. return (self.condition, self.if_clause)
  48. else:
  49. return (self.condition, self.if_clause, self.else_clause)
  50. constructor_parameters = (
  51. ('cond', Instruction),
  52. ('then', Instruction),
  53. ('else', Instruction))
  54. def __repr__(self):
  55. return '@%r: SelectInstruction(@%r, @%r, @%r)' % (
  56. id(self), id(self.condition), id(self.if_clause), id(self.else_clause))
  57. class WhileInstruction(Instruction):
  58. """Represents a 'while' instruction."""
  59. def __init__(self, condition, body):
  60. Instruction.__init__(self)
  61. self.condition = condition
  62. self.body = body
  63. def get_directly_reachable(self):
  64. """Gets all instructions that are directly reachable from this instruction."""
  65. return (self.condition, self.body)
  66. constructor_parameters = (
  67. ('cond', Instruction),
  68. ('body', Instruction))
  69. def __repr__(self):
  70. return '@%r: WhileInstruction(@%r, @%r)' % (
  71. id(self), id(self.condition), id(self.body))
  72. class BreakInstruction(Instruction):
  73. """Represents a 'break' instruction."""
  74. def __init__(self, loop):
  75. Instruction.__init__(self)
  76. self.loop = loop
  77. def get_directly_reachable(self):
  78. """Gets all instructions that are directly reachable from this instruction."""
  79. return (self.loop,)
  80. constructor_parameters = (('while', WhileInstruction),)
  81. def __repr__(self):
  82. return '@%r: BreakInstruction(@%r)' % (
  83. id(self), id(self.loop))
  84. class ContinueInstruction(Instruction):
  85. """Represents a 'continue' instruction."""
  86. def __init__(self, loop):
  87. Instruction.__init__(self)
  88. self.loop = loop
  89. def get_directly_reachable(self):
  90. """Gets all instructions that are directly reachable from this instruction."""
  91. return (self.loop,)
  92. constructor_parameters = (('while', WhileInstruction),)
  93. def __repr__(self):
  94. return '@%r: ContinueInstruction(@%r)' % (
  95. id(self), id(self.loop))
  96. class ReturnInstruction(Instruction):
  97. """Represents a 'return' instruction, which terminates the current function
  98. and optionally returns a value."""
  99. def __init__(self, value):
  100. Instruction.__init__(self)
  101. self.value = value
  102. def get_directly_reachable(self):
  103. """Gets all instructions that are directly reachable from this instruction."""
  104. if self.value is None:
  105. return ()
  106. else:
  107. return (self.value,)
  108. constructor_parameters = (('value', Instruction),)
  109. def __repr__(self):
  110. return '@%r: ReturnInstruction(@%r)' % (
  111. id(self), id(self.value))
  112. class CallInstruction(Instruction):
  113. """Represents a 'call' instruction, which calls a function with an argument
  114. list, encoded as a list of name-instruction tuples."""
  115. def __init__(self, target, argument_list):
  116. Instruction.__init__(self)
  117. self.target = target
  118. self.argument_list = argument_list
  119. def get_directly_reachable(self):
  120. """Gets all instructions that are directly reachable from this instruction."""
  121. return (self.target,) + tuple((value for _, value in self.argument_list))
  122. def __repr__(self):
  123. return '@%r: CallInstruction(@%r, [%s])' % (
  124. id(self), id(self.target),
  125. ', '.join(['%s=@%r' % (name, id(value)) for name, value in self.argument_list]))
  126. class ConstantInstruction(Instruction):
  127. """Represents a 'constant' instruction, which produces a reference
  128. to a constant node."""
  129. def __init__(self, constant_id):
  130. Instruction.__init__(self)
  131. self.constant_id = constant_id
  132. assert self.constant_id is not None
  133. def get_directly_reachable(self):
  134. """Gets all instructions that are directly reachable from this instruction."""
  135. return ()
  136. constructor_parameters = (('node', int),)
  137. def __repr__(self):
  138. return '@%r: ConstantInstruction(%r)' % (id(self), self.constant_id)
  139. class InputInstruction(Instruction):
  140. """Represents an 'input' instruction, which pops a node from the input
  141. queue."""
  142. def __init__(self):
  143. Instruction.__init__(self)
  144. def get_directly_reachable(self):
  145. """Gets all instructions that are directly reachable from this instruction."""
  146. return ()
  147. constructor_parameters = ()
  148. def __repr__(self):
  149. return '@%r: InputInstruction()' % id(self)
  150. class OutputInstruction(Instruction):
  151. """Represents an 'output' instruction, which pushes a node onto the output
  152. queue."""
  153. def __init__(self, value):
  154. Instruction.__init__(self)
  155. self.value = value
  156. def get_directly_reachable(self):
  157. """Gets all instructions that are directly reachable from this instruction."""
  158. return (self.value,)
  159. constructor_parameters = (('value', Instruction),)
  160. def __repr__(self):
  161. return '@%r: OutputInstruction(@%r)' % (
  162. id(self), id(self.value))
  163. class DeclareInstruction(Instruction):
  164. """Represents a 'declare' instruction, which declares a local variable."""
  165. def __init__(self, variable):
  166. Instruction.__init__(self)
  167. self.variable = variable
  168. def get_directly_reachable(self):
  169. """Gets all instructions that are directly reachable from this instruction."""
  170. return ()
  171. constructor_parameters = (('var', VariableNode),)
  172. def __repr__(self):
  173. return '@%r: DeclareInstruction(%r)' % (
  174. id(self), self.variable)
  175. class GlobalInstruction(Instruction):
  176. """Represents a 'global' instruction, which declares a global variable."""
  177. def __init__(self, variable):
  178. Instruction.__init__(self)
  179. self.variable = variable
  180. def get_directly_reachable(self):
  181. """Gets all instructions that are directly reachable from this instruction."""
  182. return ()
  183. constructor_parameters = (('var', VariableNode),)
  184. def __repr__(self):
  185. return '@%r: GlobalInstruction(%r)' % (
  186. id(self), self.variable)
  187. class ResolveInstruction(Instruction):
  188. """Represents a 'resolve' instruction, which resolves a variable node/name as
  189. either a local or global variable."""
  190. def __init__(self, variable):
  191. Instruction.__init__(self)
  192. self.variable = variable
  193. def get_directly_reachable(self):
  194. """Gets all instructions that are directly reachable from this instruction."""
  195. return ()
  196. constructor_parameters = (('var', VariableNode),)
  197. def __repr__(self):
  198. return '@%r: ResolveInstruction(%r)' % (
  199. id(self), self.variable)
  200. class AccessInstruction(Instruction):
  201. """Represents an 'access' instruction, which loads the node pointed to by a
  202. pointer node."""
  203. def __init__(self, pointer):
  204. Instruction.__init__(self)
  205. self.pointer = pointer
  206. def get_directly_reachable(self):
  207. """Gets all instructions that are directly reachable from this instruction."""
  208. return (self.pointer,)
  209. constructor_parameters = (('var', Instruction),)
  210. def __repr__(self):
  211. return '@%r: AccessInstruction(@%r)' % (
  212. id(self), id(self.pointer))
  213. class AssignInstruction(Instruction):
  214. """Represents an 'assign' instruction, which sets the node pointed to by a
  215. pointer node to the given node."""
  216. def __init__(self, pointer, value):
  217. Instruction.__init__(self)
  218. self.pointer = pointer
  219. self.value = value
  220. def get_directly_reachable(self):
  221. """Gets all instructions that are directly reachable from this instruction."""
  222. return (self.pointer, self.value)
  223. constructor_parameters = (
  224. ('var', Instruction),
  225. ('value', Instruction))
  226. def __repr__(self):
  227. return '@%r: AssignInstruction(@%r, @%r)' % (
  228. id(self), id(self.pointer), id(self.value))
  229. INSTRUCTION_TYPE_MAPPING = {
  230. 'if' : SelectInstruction,
  231. 'while' : WhileInstruction,
  232. 'return' : ReturnInstruction,
  233. 'constant' : ConstantInstruction,
  234. 'resolve' : ResolveInstruction,
  235. 'declare' : DeclareInstruction,
  236. 'global' : GlobalInstruction,
  237. 'assign' : AssignInstruction,
  238. 'access' : AccessInstruction,
  239. 'output' : OutputInstruction,
  240. 'input' : InputInstruction,
  241. 'call' : CallInstruction,
  242. 'break' : BreakInstruction,
  243. 'continue' : ContinueInstruction
  244. }
  245. """Maps instruction names to types."""