bytecode_ir.py 5.1 KB

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