Browse Source

Define function calls in cfg_ir.py

jonathanvdc 8 years ago
parent
commit
d70b906618
1 changed files with 39 additions and 1 deletions
  1. 39 1
      kernel/modelverse_jit/cfg_ir.py

+ 39 - 1
kernel/modelverse_jit/cfg_ir.py

@@ -25,18 +25,21 @@ class BasicBlock(object):
 
 
     def append_parameter(self, parameter):
     def append_parameter(self, parameter):
         """Appends a parameter to this basic block."""
         """Appends a parameter to this basic block."""
+        assert isinstance(parameter, BlockParameter)
         result = Definition(self.counter.next_value(), parameter)
         result = Definition(self.counter.next_value(), parameter)
         self.parameters.append(result)
         self.parameters.append(result)
         return result
         return result
 
 
     def prepend_definition(self, value):
     def prepend_definition(self, value):
         """Defines the given value in this basic block."""
         """Defines the given value in this basic block."""
+        assert isinstance(value, Value)
         result = Definition(self.counter.next_value(), value)
         result = Definition(self.counter.next_value(), value)
         self.definitions.insert(0, result)
         self.definitions.insert(0, result)
         return result
         return result
 
 
     def append_definition(self, value):
     def append_definition(self, value):
         """Defines the given value in this basic block."""
         """Defines the given value in this basic block."""
+        assert isinstance(value, Value)
         result = Definition(self.counter.next_value(), value)
         result = Definition(self.counter.next_value(), value)
         self.definitions.append(result)
         self.definitions.append(result)
         return result
         return result
@@ -56,10 +59,12 @@ class Definition(object):
     def __init__(self, index, value):
     def __init__(self, index, value):
         self.index = index
         self.index = index
         self.value = value
         self.value = value
+        assert isinstance(value, Value)
 
 
     def redefine(self, new_value):
     def redefine(self, new_value):
         """Tweaks this definition to take on the given new value."""
         """Tweaks this definition to take on the given new value."""
         self.value = new_value
         self.value = new_value
+        assert isinstance(new_value, Value)
 
 
     def ref_str(self):
     def ref_str(self):
         """Gets a string that represents a reference to this definition."""
         """Gets a string that represents a reference to this definition."""
@@ -87,7 +92,9 @@ class Branch(Instruction):
     """Represents a branch from one basic block to another."""
     """Represents a branch from one basic block to another."""
     def __init__(self, block, arguments):
     def __init__(self, block, arguments):
         self.block = block
         self.block = block
+        assert isinstance(block, BasicBlock)
         self.arguments = arguments
         self.arguments = arguments
+        assert all([isinstance(arg, Definition) for arg in arguments])
 
 
     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."""
@@ -107,6 +114,7 @@ class JumpFlow(FlowInstruction):
     def __init__(self, branch):
     def __init__(self, branch):
         FlowInstruction.__init__(self)
         FlowInstruction.__init__(self)
         self.branch = branch
         self.branch = branch
+        assert isinstance(branch, Branch)
 
 
     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."""
@@ -125,8 +133,11 @@ class SelectFlow(FlowInstruction):
     def __init__(self, condition, if_branch, else_branch):
     def __init__(self, condition, if_branch, else_branch):
         FlowInstruction.__init__(self)
         FlowInstruction.__init__(self)
         self.condition = condition
         self.condition = condition
+        assert isinstance(condition, Definition)
         self.if_branch = if_branch
         self.if_branch = if_branch
+        assert isinstance(if_branch, Branch)
         self.else_branch = else_branch
         self.else_branch = else_branch
+        assert isinstance(else_branch, Branch)
 
 
     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."""
@@ -145,6 +156,7 @@ class ReturnFlow(FlowInstruction):
     def __init__(self, value):
     def __init__(self, value):
         FlowInstruction.__init__(self)
         FlowInstruction.__init__(self)
         self.value = value
         self.value = value
+        assert isinstance(value, Value)
 
 
     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."""
@@ -176,6 +188,10 @@ class Value(Instruction):
         """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 has_side_effects(self):
+        """Tells if this instruction has side-effects."""
+        return False
+
 class BlockParameter(Value):
 class BlockParameter(Value):
     """A basic block parameter."""
     """A basic block parameter."""
     def get_dependencies(self):
     def get_dependencies(self):
@@ -209,4 +225,26 @@ class Literal(Value):
         return []
         return []
 
 
     def __str__(self):
     def __str__(self):
-        return 'literal %s' % self.literal
+        return 'literal %r' % self.literal
+
+class JitFunctionCall(Value):
+    """A value that is the result of a function call."""
+    def __init__(self, target, argument_list):
+        Value.__init__(self)
+        assert isinstance(target, Definition)
+        self.target = target
+        assert all([isinstance(val, Definition) for val in argument_list])
+        self.argument_list = argument_list
+
+    def has_side_effects(self):
+        """Tells if this instruction has side-effects."""
+        return True
+
+    def get_dependencies(self):
+        """Gets all definitions and instructions on which this instruction depends."""
+        return [self.target] + [val for _, val in self.argument_list]
+
+    def __str__(self):
+        return 'call %s(%s)' % (
+            self.target.ref_str(),
+            ', '.join(['%s=%s' % (key, val.ref_str()) for key, val in self.argument_list]))