Browse Source

Turn a bunch of cast operations into intrinsics

jonathanvdc 8 years ago
parent
commit
76dfa0e596
2 changed files with 24 additions and 0 deletions
  1. 17 0
      kernel/modelverse_jit/intrinsics.py
  2. 7 0
      kernel/modelverse_jit/jit.py

+ 17 - 0
kernel/modelverse_jit/intrinsics.py

@@ -33,6 +33,21 @@ UNARY_INTRINSICS = {
     'float_neg' : '-'
     'float_neg' : '-'
 }
 }
 
 
+CAST_INTRINSICS = {
+    'cast_i2f' : float,
+    'cast_i2s' : str,
+    'cast_i2b' : bool,
+    'cast_f2i' : int,
+    'cast_f2s' : str,
+    'cast_f2b' : bool,
+    'cast_s2i' : int,
+    'cast_s2f' : float,
+    'cast_s2b' : bool,
+    'cast_b2i' : int,
+    'cast_b2f' : float,
+    'cast_b2s' : str
+}
+
 def create_get_length(expression):
 def create_get_length(expression):
     """Creates an expression that evaluates the given expression, and then
     """Creates an expression that evaluates the given expression, and then
        computes the length of its result."""
        computes the length of its result."""
@@ -244,5 +259,7 @@ def register_intrinsics(target_jit):
         target_jit.register_binary_intrinsic(key, value)
         target_jit.register_binary_intrinsic(key, value)
     for (key, value) in UNARY_INTRINSICS.items():
     for (key, value) in UNARY_INTRINSICS.items():
         target_jit.register_unary_intrinsic(key, value)
         target_jit.register_unary_intrinsic(key, value)
+    for (key, value) in CAST_INTRINSICS.items():
+        target_jit.register_cast_intrinsic(key, value)
     for (key, value) in MISC_INTRINSICS.items():
     for (key, value) in MISC_INTRINSICS.items():
         target_jit.register_intrinsic(key, value)
         target_jit.register_intrinsic(key, value)

+ 7 - 0
kernel/modelverse_jit/jit.py

@@ -213,6 +213,13 @@ class ModelverseJit(object):
                 operator,
                 operator,
                 tree_ir.ReadValueInstruction(a))))
                 tree_ir.ReadValueInstruction(a))))
 
 
+    def register_cast_intrinsic(self, name, target_type):
+        """Registers an intrinsic with the JIT that represents a unary conversion operator."""
+        self.register_intrinsic(name, lambda a: tree_ir.CreateNodeWithValueInstruction(
+            tree_ir.CallInstruction(
+                tree_ir.LoadGlobalInstruction(target_type.__name__),
+                [tree_ir.ReadValueInstruction(a)])))
+
     def jit_parameters(self, body_id):
     def jit_parameters(self, body_id):
         """Acquires the parameter list for the given body id node."""
         """Acquires the parameter list for the given body id node."""
         if body_id not in self.jitted_parameters:
         if body_id not in self.jitted_parameters: