|
@@ -1,4 +1,5 @@
|
|
|
import modelverse_kernel.primitives as primitive_functions
|
|
|
+import modelverse_kernel.compiled as compiled_functions
|
|
|
import sys
|
|
|
import time
|
|
|
|
|
@@ -11,6 +12,7 @@ class ModelverseKernel(object):
|
|
|
def __init__(self, root):
|
|
|
self.root = root
|
|
|
self.primitives = {}
|
|
|
+ self.compiled = {}
|
|
|
self.returnvalue = None
|
|
|
self.success = True
|
|
|
self.generators = {}
|
|
@@ -54,12 +56,11 @@ class ModelverseKernel(object):
|
|
|
if phase_v == "finish":
|
|
|
gen = self.helper_init(user_root)
|
|
|
elif isinstance(phase_v, string_types):
|
|
|
- if phase_v == "init" and inst_v is None:
|
|
|
- #print("%-30s(%s)" % ("PRIMITIVE " + str(self.primitives[inst]), phase_v))
|
|
|
- try:
|
|
|
- gen = self.execute_primitive(user_root, inst)
|
|
|
- except:
|
|
|
- raise Exception("%s: error: IP points to undefined element and is not executable as primitive.")
|
|
|
+ if phase_v == "init" and inst in self.compiled:
|
|
|
+ #print("%-30s(%s)" % ("COMPILED " + str(self.primitives[inst]), phase_v))
|
|
|
+ gen = self.execute_primitive(user_root, inst)
|
|
|
+ elif inst_v is None:
|
|
|
+ raise Exception("%s: error understanding command (%s, %s)" % (self.debug_info, inst_v, phase_v))
|
|
|
else:
|
|
|
#print("%-30s(%s)" % (inst_v["value"], phase_v))
|
|
|
gen = getattr(self, "%s_%s" % (inst_v["value"], phase_v))(user_root)
|
|
@@ -91,6 +92,7 @@ class ModelverseKernel(object):
|
|
|
bodies = yield [("RD", [f, "body"]) for f in signatures]
|
|
|
for i in range(len(keys)):
|
|
|
self.primitives[bodies[i]] = getattr(primitive_functions, function_names[i])
|
|
|
+ self.compiled.update(self.primitives)
|
|
|
|
|
|
def execute_primitive(self, user_root, inst):
|
|
|
# execute_primitive
|
|
@@ -122,7 +124,10 @@ class ModelverseKernel(object):
|
|
|
try:
|
|
|
# Forward the message we get to this generator
|
|
|
# Sometimes it might not even be a generator, in which case this should already be in the except block (i.e., for the Read Root operation)
|
|
|
- prim = self.primitives[inst](**parameters)
|
|
|
+ if inst not in self.primitives:
|
|
|
+ print("Function %s" % self.compiled[inst])
|
|
|
+ print(" parameters: " + str(parameters))
|
|
|
+ prim = self.compiled[inst](**parameters)
|
|
|
inp = None
|
|
|
while 1:
|
|
|
inp = yield prim.send(inp)
|
|
@@ -134,7 +139,7 @@ class ModelverseKernel(object):
|
|
|
result = e.result
|
|
|
|
|
|
#if result is None:
|
|
|
- # raise Exception("Primitive raised exception: value of None for operation %s with parameters %s" % (self.primitives[inst], str(parameters)))
|
|
|
+ # raise Exception("Primitive raised exception: value of None for operation %s with parameters %s" % (self.compiled[inst], str(parameters)))
|
|
|
|
|
|
# Clean up the current stack, as if a return happened
|
|
|
old_frame = yield [("RD", [user_frame, "prev"])]
|
|
@@ -425,6 +430,21 @@ class ModelverseKernel(object):
|
|
|
]
|
|
|
if variable is None:
|
|
|
raise Exception("%s: not found as global: %s" % (self.debug_info, var_name))
|
|
|
+
|
|
|
+ # Resolved a global, so this is a string
|
|
|
+ # Potentially, this might even be a function that we have precompiled already!
|
|
|
+ # So check whether this is the case or not
|
|
|
+ compiled_function = getattr(compiled_functions, var_name, None)
|
|
|
+ if compiled_function is not None:
|
|
|
+ # We have a compiled function ready!
|
|
|
+ print("Using compiled version of '%s'" % var_name)
|
|
|
+
|
|
|
+ # Now we have to bind the ID to the compiled functions
|
|
|
+ # For this, we read out the body of the resolved data
|
|
|
+ compiler_val = yield [("RD", [variable, "value"])]
|
|
|
+ compiler_body = yield [("RD", [compiler_val, "body"])]
|
|
|
+ self.compiled[compiler_body] = compiled_function
|
|
|
+
|
|
|
else:
|
|
|
phase_link, returnvalue_link, new_phase = \
|
|
|
yield [("RDE", [user_frame, "phase"]),
|