فهرست منبع

Add unary operators to CFG IR

jonathanvdc 8 سال پیش
والد
کامیت
8f8642044d
2فایلهای تغییر یافته به همراه26 افزوده شده و 0 حذف شده
  1. 20 0
      kernel/modelverse_jit/cfg_ir.py
  2. 6 0
      kernel/modelverse_jit/cfg_to_tree.py

+ 20 - 0
kernel/modelverse_jit/cfg_ir.py

@@ -849,6 +849,26 @@ class Binary(Value):
     def __str__(self):
         return 'binary %s, %r, %s' % (self.lhs.ref_str(), self.operator, self.rhs.ref_str())
 
+class Unary(Value):
+    """A value that applies a unary operator to an operand value."""
+    def __init__(self, operator, operand):
+        Value.__init__(self)
+        self.operator = operator
+        self.operand = operand
+        assert isinstance(operand, Definition)
+
+    def get_dependencies(self):
+        """Gets all definitions and instructions on which this instruction depends."""
+        return [self.operand]
+
+    def create(self, new_dependencies):
+        """Creates an instruction of this type from the given set of dependencies."""
+        new_operand, = new_dependencies
+        return Unary(self.operator, new_operand)
+
+    def __str__(self):
+        return 'unary %r, %s' % (self.operator, self.operand.ref_str())
+
 def create_jump(block, arguments=None):
     """Creates a jump to the given block with the given argument list."""
     return JumpFlow(Branch(block, arguments))

+ 6 - 0
kernel/modelverse_jit/cfg_to_tree.py

@@ -811,6 +811,11 @@ class LoweringState(object):
         lhs, rhs = self.use_definition(value.lhs), self.use_definition(value.rhs)
         return tree_ir.BinaryInstruction(lhs, value.operator, rhs)
 
+    def lower_unary(self, value):
+        """Lowers a 'unary' value."""
+        operand, = self.use_definition(value.operand)
+        return tree_ir.UnaryInstruction(value.operator, operand)
+
     def lower_direct_call(self, value):
         """Lowers a direct function call."""
         calling_convention = value.calling_convention
@@ -956,6 +961,7 @@ class LoweringState(object):
         cfg_ir.CreateNode : lower_create_node,
         cfg_ir.CreateEdge : lower_create_edge,
         cfg_ir.Binary : lower_binary,
+        cfg_ir.Unary : lower_unary,
         cfg_ir.DirectFunctionCall : lower_direct_call,
         cfg_ir.IndirectFunctionCall : lower_indirect_call,
         cfg_ir.JumpFlow : lower_jump,