Browse Source

Support unary intrinsics in JIT

jonathanvdc 8 years ago
parent
commit
6e82fa81bf

+ 8 - 0
kernel/modelverse_jit/intrinsics.py

@@ -27,6 +27,12 @@ BINARY_INTRINSICS = {
     'float_lte' : '<='
 }
 
+UNARY_INTRINSICS = {
+    'bool_not' : 'not',
+    'integer_neg' : '-',
+    'float_neg' : '-'
+}
+
 MISC_INTRINSICS = {
     'element_eq' : (
         lambda lhs, rhs:
@@ -42,5 +48,7 @@ def register_intrinsics(target_jit):
     """Registers all intrinsics in the module with the given JIT."""
     for (key, value) in BINARY_INTRINSICS.items():
         target_jit.register_binary_intrinsic(key, value)
+    for (key, value) in UNARY_INTRINSICS.items():
+        target_jit.register_unary_intrinsic(key, value)
     for (key, value) in MISC_INTRINSICS.items():
         target_jit.register_intrinsic(key, value)

+ 7 - 0
kernel/modelverse_jit/jit.py

@@ -106,6 +106,13 @@ class ModelverseJit(object):
                 operator,
                 tree_ir.ReadValueInstruction(rhs))))
 
+    def register_unary_intrinsic(self, name, operator):
+        """Registers an intrinsic with the JIT that represents the given unary operation."""
+        self.register_intrinsic(name, lambda val: tree_ir.CreateNodeWithValueInstruction(
+            tree_ir.UnaryInstruction(
+                operator, 
+                tree_ir.ReadValueInstruction(val))))
+
     def jit_parameters(self, body_id):
         """Acquires the parameter list for the given body id node."""
         if body_id not in self.jitted_parameters:

+ 31 - 0
kernel/modelverse_jit/tree_ir.py

@@ -372,6 +372,37 @@ class BinaryInstruction(Instruction):
         else:
             code_generator.append_line('pass')
 
+class UnaryInstruction(Instruction):
+    """An instruction that performs a unary operation."""
+    def __init__(self, operator, operand):
+        Instruction.__init__(self)
+        self.operator = operator
+        self.operand = operand
+
+    def has_definition(self):
+        """Tells if this instruction requires a definition."""
+        return self.operator.has_definition()
+
+    def simplify(self):
+        """Applies basic simplification to this instruction and its children."""
+        simple_operator = self.operator.simplify()
+        return UnaryInstruction(self.operator, simple_operator)
+
+    def generate_python_use(self, code_generator):
+        """Generates a Python expression that retrieves this instruction's
+           result. The expression is returned as a string."""
+        return '(%s %s)' % (
+            self.operator,
+            self.operand.generate_python_use(code_generator))
+
+    def generate_python_def(self, code_generator):
+        """Generates a Python statement that executes this instruction.
+           The statement is appended immediately to the code generator."""
+        if self.operator.has_definition():
+            self.operator.generate_python_def(code_generator)
+        else:
+            code_generator.append_line('pass')
+
 class LoopInstruction(VoidInstruction):
     """Represents a loop-instruction, which loops until broken."""