bytecode_ir.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. class VariableNode(object):
  8. """Represents a variable node, which has an identifier and an optional name."""
  9. def __init__(self, node_id, name):
  10. self.node_id = node_id
  11. self.name = name
  12. def __str__(self):
  13. return 'var(%d, %s)' % (self.node_id, self.name)
  14. def __repr__(self):
  15. return 'VariableNode(%r, %r)' % (self.node_id, self.name)
  16. class SelectInstruction(Instruction):
  17. """Represents an 'if/else' instruction."""
  18. def __init__(self, condition, if_clause, else_clause):
  19. Instruction.__init__(self)
  20. self.condition = condition
  21. self.if_clause = if_clause
  22. self.else_clause = else_clause
  23. constructor_parameters = (
  24. ('cond', Instruction),
  25. ('then', Instruction),
  26. ('else', Instruction))
  27. class WhileInstruction(Instruction):
  28. """Represents a 'while' instruction."""
  29. def __init__(self, condition, body):
  30. Instruction.__init__(self)
  31. self.condition = condition
  32. self.body = body
  33. constructor_parameters = (
  34. ('cond', Instruction),
  35. ('body', Instruction))
  36. class BreakInstruction(Instruction):
  37. """Represents a 'break' instruction."""
  38. def __init__(self, loop):
  39. Instruction.__init__(self)
  40. self.loop = loop
  41. constructor_parameters = (('while', WhileInstruction),)
  42. class ContinueInstruction(Instruction):
  43. """Represents a 'continue' instruction."""
  44. def __init__(self, loop):
  45. Instruction.__init__(self)
  46. self.loop = loop
  47. constructor_parameters = (('while', WhileInstruction),)
  48. class ReturnInstruction(Instruction):
  49. """Represents a 'return' instruction, which terminates the current function
  50. and optionally returns a value."""
  51. def __init__(self, value):
  52. Instruction.__init__(self)
  53. self.value = value
  54. constructor_parameters = (('value', Instruction),)
  55. class CallInstruction(Instruction):
  56. """Represents a 'call' instruction, which calls a function with an argument
  57. list, encoded as a list of name-instruction tuples."""
  58. def __init__(self, target, argument_list):
  59. Instruction.__init__(self)
  60. self.target = target
  61. self.argument_list = argument_list
  62. class ConstantInstruction(Instruction):
  63. """Represents a 'constant' instruction, which produces a reference
  64. to a constant node."""
  65. def __init__(self, constant_id):
  66. Instruction.__init__(self)
  67. self.constant_id = constant_id
  68. constructor_parameters = (('node', int),)
  69. class InputInstruction(Instruction):
  70. """Represents an 'input' instruction, which pops a node from the input
  71. queue."""
  72. def __init__(self):
  73. Instruction.__init__(self)
  74. constructor_parameters = ()
  75. class OutputInstruction(Instruction):
  76. """Represents an 'output' instruction, which pushes a node onto the output
  77. queue."""
  78. def __init__(self, value):
  79. Instruction.__init__(self)
  80. self.value = value
  81. constructor_parameters = (('value', Instruction),)
  82. class DeclareInstruction(Instruction):
  83. """Represents a 'declare' instruction, which declares a local variable."""
  84. def __init__(self, variable):
  85. Instruction.__init__(self)
  86. self.variable = variable
  87. constructor_parameters = (('var', VariableNode),)
  88. class GlobalInstruction(Instruction):
  89. """Represents a 'global' instruction, which declares a global variable."""
  90. def __init__(self, variable):
  91. Instruction.__init__(self)
  92. self.variable = variable
  93. constructor_parameters = (('var', VariableNode),)
  94. class ResolveInstruction(Instruction):
  95. """Represents a 'resolve' instruction, which resolves a variable node/name as
  96. either a local or global variable."""
  97. def __init__(self, variable):
  98. Instruction.__init__(self)
  99. self.variable = variable
  100. constructor_parameters = (('var', VariableNode),)
  101. class AccessInstruction(Instruction):
  102. """Represents an 'access' instruction, which loads the node pointed to by a
  103. pointer node."""
  104. def __init__(self, pointer):
  105. Instruction.__init__(self)
  106. self.pointer = pointer
  107. constructor_parameters = (('var', Instruction),)
  108. class AssignInstruction(Instruction):
  109. """Represents an 'assign' instruction, which sets the node pointed to by a
  110. pointer node to the given node."""
  111. def __init__(self, pointer, value):
  112. Instruction.__init__(self)
  113. self.pointer = pointer
  114. self.value = value
  115. constructor_parameters = (
  116. ('var', Instruction),
  117. ('value', Instruction))
  118. INSTRUCTION_TYPE_MAPPING = {
  119. 'if' : SelectInstruction,
  120. 'while' : WhileInstruction,
  121. 'return' : ReturnInstruction,
  122. 'constant' : ConstantInstruction,
  123. 'resolve' : ResolveInstruction,
  124. 'declare' : DeclareInstruction,
  125. 'global' : GlobalInstruction,
  126. 'assign' : AssignInstruction,
  127. 'access' : AccessInstruction,
  128. 'output' : OutputInstruction,
  129. 'input' : InputInstruction,
  130. 'call' : CallInstruction,
  131. 'break' : BreakInstruction,
  132. 'continue' : ContinueInstruction
  133. }
  134. """Maps instruction names to types."""