Browse Source

Add a 'create' method to every CFG Instruction

jonathanvdc 8 years ago
parent
commit
d0f5a632bc
1 changed files with 119 additions and 0 deletions
  1. 119 0
      kernel/modelverse_jit/cfg_ir.py

+ 119 - 0
kernel/modelverse_jit/cfg_ir.py

@@ -153,6 +153,10 @@ class Instruction(object):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         raise NotImplementedError()
         raise NotImplementedError()
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        raise NotImplementedError()
+
     def get_all_dependencies(self):
     def get_all_dependencies(self):
         """Gets all definitions and instructions on which this instruction depends,
         """Gets all definitions and instructions on which this instruction depends,
            along with any dependencies of instruction dependencies."""
            along with any dependencies of instruction dependencies."""
@@ -177,6 +181,10 @@ class Branch(Instruction):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return self.arguments
         return self.arguments
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        return Branch(self.block, new_dependencies)
+
     def __str__(self):
     def __str__(self):
         return '!%d(%s)' % (self.block.index, ', '.join([arg.ref_str() for arg in self.arguments]))
         return '!%d(%s)' % (self.block.index, ', '.join([arg.ref_str() for arg in self.arguments]))
 
 
@@ -202,6 +210,10 @@ class JumpFlow(FlowInstruction):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return self.branches()
         return self.branches()
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        return JumpFlow(*new_dependencies)
+
     def branches(self):
     def branches(self):
         """Gets a list of basic blocks targeted by this flow instruction."""
         """Gets a list of basic blocks targeted by this flow instruction."""
         return [self.branch]
         return [self.branch]
@@ -225,6 +237,10 @@ class SelectFlow(FlowInstruction):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return [self.condition] + self.branches()
         return [self.condition] + self.branches()
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        return SelectFlow(*new_dependencies)
+
     def branches(self):
     def branches(self):
         """Gets a list of basic blocks targeted by this flow instruction."""
         """Gets a list of basic blocks targeted by this flow instruction."""
         return [self.if_branch, self.else_branch]
         return [self.if_branch, self.else_branch]
@@ -244,6 +260,10 @@ class ReturnFlow(FlowInstruction):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return [self.value]
         return [self.value]
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        return ReturnFlow(*new_dependencies)
+
     def branches(self):
     def branches(self):
         """Gets a list of basic blocks targeted by this flow instruction."""
         """Gets a list of basic blocks targeted by this flow instruction."""
         return []
         return []
@@ -262,6 +282,10 @@ class ThrowFlow(FlowInstruction):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return [self.exception]
         return [self.exception]
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        return ThrowFlow(*new_dependencies)
+
     def branches(self):
     def branches(self):
         """Gets a list of basic blocks targeted by this flow instruction."""
         """Gets a list of basic blocks targeted by this flow instruction."""
         return []
         return []
@@ -275,6 +299,11 @@ class UnreachableFlow(FlowInstruction):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return []
         return []
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        assert len(new_dependencies) == 0
+        return self
+
     def branches(self):
     def branches(self):
         """Gets a list of basic blocks targeted by this flow instruction."""
         """Gets a list of basic blocks targeted by this flow instruction."""
         return []
         return []
@@ -306,6 +335,11 @@ class BlockParameter(Value):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return []
         return []
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        assert len(new_dependencies) == 0
+        return BlockParameter()
+
     def __str__(self):
     def __str__(self):
         return 'block-parameter'
         return 'block-parameter'
 
 
@@ -315,6 +349,11 @@ class FunctionParameter(Value):
         Value.__init__(self)
         Value.__init__(self)
         self.name = name
         self.name = name
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        assert len(new_dependencies) == 0
+        return FunctionParameter(self.name)
+
     def get_dependencies(self):
     def get_dependencies(self):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return []
         return []
@@ -328,6 +367,11 @@ class Literal(Value):
         Value.__init__(self)
         Value.__init__(self)
         self.literal = literal
         self.literal = literal
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        assert len(new_dependencies) == 0
+        return Literal(self.literal)
+
     def get_dependencies(self):
     def get_dependencies(self):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return []
         return []
@@ -356,6 +400,13 @@ class IndirectFunctionCall(Value):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return [self.target] + [val for _, val in self.argument_list]
         return [self.target] + [val for _, val in self.argument_list]
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        return IndirectFunctionCall(
+            new_dependencies[0],
+            [(name, new_val)
+             for new_val, (name, _) in zip(new_dependencies[1:], self.argument_list)])
+
     def __str__(self):
     def __str__(self):
         return 'indirect-call %s(%s)' % (
         return 'indirect-call %s(%s)' % (
             self.target.ref_str(),
             self.target.ref_str(),
@@ -381,6 +432,14 @@ class DirectFunctionCall(Value):
         """Tells if this instruction has side-effects."""
         """Tells if this instruction has side-effects."""
         return True
         return True
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        return DirectFunctionCall(
+            self.target_name,
+            [(name, new_val)
+             for new_val, (name, _) in zip(new_dependencies, self.argument_list)],
+            self.calling_convention)
+
     def get_dependencies(self):
     def get_dependencies(self):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return [val for _, val in self.argument_list]
         return [val for _, val in self.argument_list]
@@ -400,6 +459,11 @@ class AllocateRootNode(Value):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return []
         return []
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        assert len(new_dependencies) == 0
+        return AllocateRootNode()
+
     def __str__(self):
     def __str__(self):
         return 'alloc-root-node'
         return 'alloc-root-node'
 
 
@@ -414,6 +478,10 @@ class DeallocateRootNode(Value):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return [self.root_node]
         return [self.root_node]
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        return DeallocateRootNode(*new_dependencies)
+
     def has_value(self):
     def has_value(self):
         """Tells if this value produces a result that is not None."""
         """Tells if this value produces a result that is not None."""
         return False
         return False
@@ -436,6 +504,11 @@ class DeclareLocal(Value):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return [self.root_node]
         return [self.root_node]
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        root_node, = new_dependencies
+        return DeclareLocal(self.variable, root_node)
+
     def has_value(self):
     def has_value(self):
         """Tells if this value produces a result that is not None."""
         """Tells if this value produces a result that is not None."""
         return False
         return False
@@ -457,6 +530,10 @@ class DeclareGlobal(Value):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return []
         return []
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        return DeclareGlobal(self.variable)
+
     def has_value(self):
     def has_value(self):
         """Tells if this value produces a result that is not None."""
         """Tells if this value produces a result that is not None."""
         return False
         return False
@@ -478,6 +555,10 @@ class CheckLocalExists(Value):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return []
         return []
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        return CheckLocalExists(self.variable)
+
     def __str__(self):
     def __str__(self):
         return 'check-local-exists %s' % self.variable
         return 'check-local-exists %s' % self.variable
 
 
@@ -491,6 +572,10 @@ class ResolveLocal(Value):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return []
         return []
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        return ResolveLocal(self.variable)
+
     def __str__(self):
     def __str__(self):
         return 'resolve-local %s' % self.variable
         return 'resolve-local %s' % self.variable
 
 
@@ -504,6 +589,10 @@ class ResolveGlobal(Value):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return []
         return []
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        return ResolveGlobal(self.variable)
+
     def __str__(self):
     def __str__(self):
         return 'resolve-global %s' % self.variable.name
         return 'resolve-global %s' % self.variable.name
 
 
@@ -518,6 +607,10 @@ class LoadPointer(Value):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return [self.pointer]
         return [self.pointer]
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        return LoadPointer(*new_dependencies)
+
     def __str__(self):
     def __str__(self):
         return 'load %s' % self.pointer.ref_str()
         return 'load %s' % self.pointer.ref_str()
 
 
@@ -534,6 +627,10 @@ class StoreAtPointer(Value):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return [self.pointer, self.value]
         return [self.pointer, self.value]
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        return StoreAtPointer(*new_dependencies)
+
     def has_value(self):
     def has_value(self):
         """Tells if this value produces a result that is not None."""
         """Tells if this value produces a result that is not None."""
         return False
         return False
@@ -556,6 +653,10 @@ class Read(Value):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return [self.node]
         return [self.node]
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        return Read(*new_dependencies)
+
     def __str__(self):
     def __str__(self):
         return 'read %s' % (self.node.ref_str())
         return 'read %s' % (self.node.ref_str())
 
 
@@ -570,6 +671,10 @@ class CreateNode(Value):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return [self.value]
         return [self.value]
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        return CreateNode(*new_dependencies)
+
     def __str__(self):
     def __str__(self):
         return 'create-node %s' % (self.value.ref_str())
         return 'create-node %s' % (self.value.ref_str())
 
 
@@ -579,6 +684,11 @@ class Input(Value):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return []
         return []
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        assert len(new_dependencies) == 0
+        return Input()
+
     def has_side_effects(self):
     def has_side_effects(self):
         """Tells if this instruction has side-effects."""
         """Tells if this instruction has side-effects."""
         return True
         return True
@@ -597,6 +707,10 @@ class Output(Value):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return [self.value]
         return [self.value]
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        return Output(*new_dependencies)
+
     def has_value(self):
     def has_value(self):
         """Tells if this value produces a result that is not None."""
         """Tells if this value produces a result that is not None."""
         return False
         return False
@@ -622,6 +736,11 @@ class Binary(Value):
         """Gets all definitions and instructions on which this instruction depends."""
         """Gets all definitions and instructions on which this instruction depends."""
         return [self.lhs, self.rhs]
         return [self.lhs, self.rhs]
 
 
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        lhs, rhs = new_dependencies
+        return Binary(lhs, self.operator, rhs)
+
     def __str__(self):
     def __str__(self):
         return 'binary %s, %r, %s' % (self.lhs.ref_str(), self.operator, self.rhs.ref_str())
         return 'binary %s, %r, %s' % (self.lhs.ref_str(), self.operator, self.rhs.ref_str())