Browse Source

Define __repr__ methods for all tree_ir classes

jonathanvdc 8 years ago
parent
commit
518d67d071
1 changed files with 106 additions and 1 deletions
  1. 106 1
      kernel/modelverse_jit/tree_ir.py

+ 106 - 1
kernel/modelverse_jit/tree_ir.py

@@ -284,6 +284,9 @@ class EmptyInstruction(VoidInstruction):
         """Tells if this instruction requires a definition."""
         return False
 
+    def __repr__(self):
+        return "EmptyInstruction()"
+
 class SelectInstruction(Instruction):
     """Represents a select-instruction: an instruction that defines one of two
        child instructions, and sets its result to the defined child's result."""
@@ -352,6 +355,9 @@ class SelectInstruction(Instruction):
         """Generates Python code for this instruction."""
         return self.generate_python_if(code_generator)
 
+    def __repr__(self):
+        return "SelectInstruction(%r, %r, %r)" % (self.condition, self.if_clause, self.else_clause)
+
 class ReturnInstruction(VoidInstruction):
     """Represents a return-instruction."""
     def __init__(self, value):
@@ -400,6 +406,9 @@ class ReturnInstruction(VoidInstruction):
             self.value.generate_python_use(code_generator) +
             ')')
 
+    def __repr__(self):
+        return "ReturnInstruction(%r)" % self.value
+
 class RaiseInstruction(VoidInstruction):
     """An instruction that raises an error."""
     def __init__(self, value):
@@ -421,6 +430,9 @@ class RaiseInstruction(VoidInstruction):
         code_generator.append_line(
             'raise ' + self.value.generate_python_use(code_generator))
 
+    def __repr__(self):
+        return "RaiseInstruction(%r)" % self.value
+
 class CallInstruction(Instruction):
     """An instruction that performs a simple call."""
     def __init__(self, target, argument_list):
@@ -451,6 +463,9 @@ class CallInstruction(Instruction):
                 self.target.generate_python_use(code_generator),
                 ', '.join([arg.generate_python_use(code_generator) for arg in self.argument_list])))
 
+    def __repr__(self):
+        return "CallInstruction(%r, %r)" % (self.target, self.argument_list)
+
 class PrintInstruction(VoidInstruction):
     """An instruction that prints a value."""
     def __init__(self, argument):
@@ -475,6 +490,9 @@ class PrintInstruction(VoidInstruction):
             'print(%s)' % (
                 self.argument.generate_python_use(code_generator)))
 
+    def __repr__(self):
+        return "PrintInstruction(%r)" % self.argument
+
 class BinaryInstruction(Instruction):
     """An instruction that performs a binary operation."""
     def __init__(self, lhs, operator, rhs):
@@ -525,6 +543,9 @@ class BinaryInstruction(Instruction):
         else:
             code_generator.append_line('pass')
 
+    def __repr__(self):
+        return "BinaryInstruction(%r, %r, %r)" % (self.lhs, self.operator, self.rhs)
+
 class UnaryInstruction(Instruction):
     """An instruction that performs a unary operation."""
     def __init__(self, operator, operand):
@@ -569,6 +590,9 @@ class UnaryInstruction(Instruction):
         else:
             code_generator.append_line('pass')
 
+    def __repr__(self):
+        return "UnaryInstruction(%r, %r)" % (self.operator, self.operand)
+
 class LoopInstruction(VoidInstruction):
     """Represents a loop-instruction, which loops until broken."""
 
@@ -592,18 +616,61 @@ class LoopInstruction(VoidInstruction):
         self.body.generate_python_def(code_generator)
         code_generator.decrease_indentation()
 
+    def __repr__(self):
+        return "LoopInstruction(%r)" % self.body
+
 class BreakInstruction(VoidInstruction):
     """Represents a break-instruction."""
     def generate_python_def(self, code_generator):
         """Generates Python code for this instruction."""
         code_generator.append_line('break')
 
+    def __repr__(self):
+        return "BreakInstruction()"
+
 class ContinueInstruction(VoidInstruction):
     """Represents a continue-instruction."""
     def generate_python_def(self, code_generator):
         """Generates Python code for this instruction."""
         code_generator.append_line('continue')
 
+    def __repr__(self):
+        return "ContinueInstruction()"
+
+class IgnoreInstruction(VoidInstruction):
+    """Represents an instruction that evaluates its operand, and then discards
+       the result."""
+    def __init__(self, value):
+        VoidInstruction.__init__(self)
+        self.value = value
+
+    def has_definition_impl(self):
+        """Tells if this instruction requires a definition."""
+        return self.value.has_definition()
+
+    def get_children(self):
+        """Gets this instruction's sequence of child instructions."""
+        return [self.value]
+
+    def create(self, new_children):
+        """Creates a new instruction of this type from the given sequence of child instructions."""
+        value, = new_children
+        return IgnoreInstruction(value)
+
+    def simplify_node(self):
+        """Applies basic simplification to this instruction and its children."""
+        if not self.value.has_result():
+            return self.value
+        else:
+            return self
+
+    def generate_python_def(self, code_generator):
+        """Generates Python code for this instruction."""
+        self.value.generate_python_def(code_generator)
+
+    def __repr__(self):
+        return "IgnoreInstruction(%r)" % self.value
+
 class CompoundInstruction(Instruction):
     """Represents an instruction that evaluates two other instructions
        in order, and returns the second instruction's result."""
@@ -650,6 +717,9 @@ class CompoundInstruction(Instruction):
             self.first.generate_python_def(code_generator)
             self.second.generate_python_def(code_generator)
 
+    def __repr__(self):
+        return "CompoundInstruction(%r, %r)" % (self.first, self.second)
+
 class LiteralInstruction(Instruction):
     """Represents an integer, floating-point, string or Boolean literal."""
     def __init__(self, literal):
@@ -673,6 +743,9 @@ class LiteralInstruction(Instruction):
            result. The expression is returned as a string."""
         return repr(self.literal)
 
+    def __repr__(self):
+        return "LiteralInstruction(%r)" % self.literal
+
 class DictionaryLiteralInstruction(Instruction):
     """Constructs a dictionary literal."""
     def __init__(self, key_value_pairs):
@@ -731,6 +804,9 @@ class DictionaryLiteralInstruction(Instruction):
         else:
             return self.generate_dictionary_expr(code_generator)
 
+    def __repr__(self):
+        return "DictionaryLiteralInstruction(%r)" % self.key_value_pairs
+
 class StateInstruction(Instruction):
     """An instruction that accesses the modelverse state."""
     def get_result_type_impl(self):
@@ -756,7 +832,6 @@ class StateInstruction(Instruction):
     def generate_python_def(self, code_generator):
         """Generates a Python statement that executes this instruction.
            The statement is appended immediately to the code generator."""
-
         args = self.get_arguments()
         for arg_i in args:
             if arg_i.has_definition():
@@ -764,6 +839,9 @@ class StateInstruction(Instruction):
 
         code_generator.append_state_definition(self, self.get_opcode(), args)
 
+    def __repr__(self):
+        return "StateInstruction(%s)" % ', '.join(map(repr, self.get_arguments()))
+
 class RunGeneratorFunctionInstruction(StateInstruction):
     """An instruction that runs a generator function."""
     def __init__(self, function, argument_dict, result_type=PRIMITIVE_RESULT_TYPE):
@@ -882,6 +960,9 @@ class StoreLocalInstruction(LocalInstruction):
            The statement is appended immediately to the code generator."""
         code_generator.append_move_definition(self, self.value)
 
+    def __repr__(self):
+        return 'StoreLocalInstruction(%r, %r)' % (self.name, self.value)
+
 class LoadLocalInstruction(LocalInstruction):
     """An instruction that loads a value from a local variable."""
     def has_definition_impl(self):
@@ -896,6 +977,9 @@ class LoadLocalInstruction(LocalInstruction):
         """Creates a new instruction of this type from the given sequence of child instructions."""
         return self
 
+    def __repr__(self):
+        return 'LoadLocalInstruction(%r)' % self.name
+
 class DefineFunctionInstruction(VariableInstruction):
     """An instruction that defines a function."""
     def __init__(self, name, parameter_list, body):
@@ -921,6 +1005,9 @@ class DefineFunctionInstruction(VariableInstruction):
         self.body.generate_python_def(code_generator)
         code_generator.decrease_indentation()
 
+    def __repr__(self):
+        return 'DefineFunctionInstruction(%r, %r, %r)' % (self.name, self.parameter_list, self.body)
+
 class LocalExistsInstruction(LocalInstruction):
     """An instruction that checks if a local variable exists."""
     def has_definition_impl(self):
@@ -940,6 +1027,9 @@ class LocalExistsInstruction(LocalInstruction):
            result. The expression is returned as a string."""
         return "'%s' in locals()" % self.get_result_name_override(code_generator)
 
+    def __repr__(self):
+        return 'LocalExistsInstruction(%r)' % self.name
+
 class LoadGlobalInstruction(VariableInstruction):
     """An instruction that loads a value from a global variable."""
     def has_definition_impl(self):
@@ -954,6 +1044,9 @@ class LoadGlobalInstruction(VariableInstruction):
         """Creates a new instruction of this type from the given sequence of child instructions."""
         return self
 
+    def __repr__(self):
+        return 'LoadGlobalInstruction(%r)' % self.name
+
 class LoadIndexInstruction(Instruction):
     """An instruction that produces a value by indexing a specified expression with
        a given key."""
@@ -988,6 +1081,9 @@ class LoadIndexInstruction(Instruction):
             self.indexed.generate_python_use(code_generator),
             self.key.generate_python_use(code_generator))
 
+    def __repr__(self):
+        return 'LoadIndexInstruction(%r, %r)' % (self.indexed, self.key)
+
 class LoadMemberInstruction(Instruction):
     """An instruction that produces a value by loading a member from a container."""
     def __init__(self, container, member_name):
@@ -1020,6 +1116,9 @@ class LoadMemberInstruction(Instruction):
             self.container.generate_python_use(code_generator),
             self.member_name)
 
+    def __repr__(self):
+        return 'LoadMemberInstruction(%r, %r)' % (self.container, self.member_name)
+
 class StoreMemberInstruction(VoidInstruction):
     """An instruction that stores a value in a container member."""
     def __init__(self, container, member_name, value):
@@ -1051,6 +1150,9 @@ class StoreMemberInstruction(VoidInstruction):
             self.member_name,
             self.value.generate_python_use(code_generator)))
 
+    def __repr__(self):
+        return 'StoreMemberInstruction(%r, %r, %r)' % (self.container, self.member_name, self.value)
+
 class NopInstruction(VoidInstruction):
     """A nop instruction, which allows for the kernel's thread of execution to be interrupted."""
     def generate_python_def(self, code_generator):
@@ -1058,6 +1160,9 @@ class NopInstruction(VoidInstruction):
            The statement is appended immediately to the code generator."""
         code_generator.append_line('yield %s' % repr(NOP_LITERAL))
 
+    def __repr__(self):
+        return 'NopInstruction()'
+
 class ReadValueInstruction(StateInstruction):
     """An instruction that reads a value from a node."""
     def __init__(self, node_id):