Browse Source

Make 'time' an intrinsic

jonathanvdc 8 years ago
parent
commit
44650c1015
2 changed files with 29 additions and 4 deletions
  1. 14 1
      kernel/modelverse_jit/intrinsics.py
  2. 15 3
      kernel/modelverse_jit/jit.py

+ 14 - 1
kernel/modelverse_jit/intrinsics.py

@@ -1,5 +1,6 @@
 import jit
 import tree_ir
+import time
 
 BINARY_INTRINSICS = {
     'value_eq' : '==',
@@ -220,7 +221,7 @@ MISC_INTRINSICS = {
             tree_ir.LoadLocalInstruction(jit.KWARGS_PARAMETER_NAME),
             tree_ir.LiteralInstruction('root')),
 
-    # # read_userroot
+    # read_userroot
     'read_userroot' :
         lambda:
         tree_ir.LoadIndexInstruction(
@@ -253,6 +254,16 @@ MISC_INTRINSICS = {
     'list_append' : __list_append
 }
 
+def register_time_intrinsic(target_jit):
+    """Registers the time() intrinsic with the given JIT."""
+    import_name = target_jit.import_value(time.time, 'time')
+    target_jit.register_intrinsic(
+        'time',
+        lambda: tree_ir.CreateNodeWithValueInstruction(
+            tree_ir.CallInstruction(
+                tree_ir.LoadGlobalInstruction(import_name),
+                [])))
+
 def register_intrinsics(target_jit):
     """Registers all intrinsics in the module with the given JIT."""
     for (key, value) in BINARY_INTRINSICS.items():
@@ -263,3 +274,5 @@ def register_intrinsics(target_jit):
         target_jit.register_cast_intrinsic(key, value)
     for (key, value) in MISC_INTRINSICS.items():
         target_jit.register_intrinsic(key, value)
+
+    register_time_intrinsic(target_jit)

+ 15 - 3
kernel/modelverse_jit/jit.py

@@ -150,8 +150,8 @@ class ModelverseJit(object):
         if body_id in self.todo_entry_points:
             self.todo_entry_points.remove(body_id)
 
-    def generate_function_name(self, suggested_name=None):
-        """Generates a new function name or picks the suggested name if it is still
+    def generate_name(self, infix, suggested_name=None):
+        """Generates a new name or picks the suggested name if it is still
            available."""
         if suggested_name is not None \
             and suggested_name not in self.jit_globals \
@@ -159,10 +159,15 @@ class ModelverseJit(object):
             self.jit_count += 1
             return suggested_name
         else:
-            function_name = 'jit_func%d' % self.jit_count
+            function_name = 'jit_%s%d' % (infix, self.jit_count)
             self.jit_count += 1
             return function_name
 
+    def generate_function_name(self, suggested_name=None):
+        """Generates a new function name or picks the suggested name if it is still
+           available."""
+        return self.generate_name('func', suggested_name)
+
     def register_compiled(self, body_id, compiled_function, function_name=None):
         """Registers a compiled entry point with the JIT."""
         # Get the function's name.
@@ -173,6 +178,13 @@ class ModelverseJit(object):
         if body_id in self.todo_entry_points:
             self.todo_entry_points.remove(body_id)
 
+    def import_value(self, value, suggested_name=None):
+        """Imports the given value into the JIT's global scope, with the given suggested name.
+           The actual name of the value (within the JIT's global scope) is returned."""
+        actual_name = self.generate_name('import', suggested_name)
+        self.jit_globals[actual_name] = value
+        return actual_name
+
     def lookup_compiled_function(self, name):
         """Looks up a compiled function by name. Returns a matching function,
            or None if no function was found."""