import modelverse_kernel.primitives as primitive_functions
import modelverse_kernel.compiled as compiled_functions
from modelverse_kernel.request_handler import RequestHandler
import modelverse_kernel.jit as jit
from collections import defaultdict
import sys
import time

if sys.version > '3': # pragma: no cover
    string_types = (str,)
else:
    string_types = (str, unicode)

class ModelverseKernel(object):
    
    counter = 0

    def __init__(self, root):
        self.root = root
        self.returnvalue = None
        # request_handlers is a dictionary of tasknames to dictionaries of operations
        # to request handlers. In generics notation:
        #
        # Dictionary<
        #     Username,
        #     Dictionary<
        #         Operation,
        #         RequestHandler>>
        #
        self.request_handlers = {}
        self.allow_compiled = True

        # `self.jit` handles most JIT-related functionality.
        self.jit = jit.ModelverseJit()
        self.jit.compiled_function_lookup = lambda func_name : getattr(compiled_functions, func_name, None)
        self.jit.body_cache = {}

        self.debug_info = defaultdict(list)

    def try_to_protect(self, var):
        if isinstance(var, dict) and "id" in var and var['id'] is not None:
            return set([var['id']])
        elif type(var) == int:
            return set([var])
        elif isinstance(var, dict):
            protect = set()
            for v in var.values():
                protect |= self.try_to_protect(v)
            return protect
        elif isinstance(var, list):
            protect = set()
            for v in var:
                protect |= self.try_to_protect(v)
            return protect
        elif isinstance(var, set):
            protect = set()
            for v in var:
                protect |= self.try_to_protect(v)
            return protect
        return set()

    def protect_temporary_variables(self, taskname):
        generators = []
        for h in self.request_handlers.values():
            for handler in h.values():
                for generator in handler.generator_stack:
                    generators.append(generator)

        to_protect = set()
        for gen in generators:
            try:
                variables = gen.gi_frame.f_locals
                for var in variables.values():
                    to_protect |= self.try_to_protect(var)
            except:
                pass

        # Create the node to which everything is attached
        self.fixed_node, = yield [("CN", [])]
        yield [("CE", [self.root, self.fixed_node])]
        yield [("CE", [self.fixed_node, node]) for node in to_protect]
        yield [("RETURN", [None])]

    def unprotect_temporary_variables(self, taskname):
        yield [("DN", [self.fixed_node])]
        yield [("RETURN", [None])]

    def execute_yields(self, taskname, operation, params, reply):
        self.taskname = taskname
        if taskname not in self.request_handlers:
            self.request_handlers[taskname] = {}
            self.jit.cache.setdefault(taskname, {})
        if operation not in self.request_handlers[taskname]:
            # Create the generator for the function to execute
            self.request_handlers[taskname][operation] = RequestHandler()
        handler = self.request_handlers[taskname][operation]
        if len(handler.generator_stack) == 0:
            handler.push_generator(getattr(self, operation)(taskname, *params))

        return handler.handle_request(reply)

    def execute_rule(self, taskname):
        task_root, =    yield [("RD", [self.root, taskname])]
        if task_root is None:
            yield [("SLEEP", [0.01, False])]
            yield None
        else:
            task_frame, = yield [("RD", [task_root, "frame"])]
            self.inst, phase = yield [("RD", [task_frame, "IP"]),
                                      ("RD", [task_frame, "phase"]),
                                     ]
            self.new_debug, self.phase_v, inst_v = \
                            yield [("RD", [self.inst, "__debug"]),
                                   ("RV", [phase]),
                                   ("RV", [self.inst]),
                                  ]
            if self.new_debug is not None:
                if self.debug_info[taskname]:
                    self.debug_info[taskname][-1], = yield [("RV", [self.new_debug])]

            if self.phase_v == "finish":
                gen = self.helper_init(task_root)
            elif self.inst is None:
                raise Exception("Instruction pointer could not be found!")
            elif isinstance(self.phase_v, string_types):
                if self.phase_v == "init" and self.jit.is_jittable_entry_point(self.inst):
                    #print("%-30s(%s)" % ("COMPILED " + str(self.jit.jitted_entry_points[self.inst]), phase_v))
                    gen = self.execute_jit(task_root, self.inst, taskname, store=True, resolve=False)
                elif inst_v is None:
                    raise Exception("%s: error understanding command (%s, %s)" % (self.debug_info[taskname], inst_v, self.phase_v))
                else:
                    gen = self.get_inst_phase_generator(inst_v, self.phase_v, task_root)
            elif inst_v is None:
                raise Exception("%s: error understanding command (%s, %s)" % (self.debug_info[taskname], inst_v, self.phase_v))
            elif inst_v["value"] == "call":
                #print("%-30s(%s)" % ("call", "param"))
                gen = self.call_param(task_root)
            else:
                raise Exception("%s: error understanding command (%s, %s)" % (self.debug_info[taskname], inst_v, self.phase_v))

            yield [("CALL", [gen])]

        yield [("FINISH", [])]

    def get_inst_phase_generator(self, inst_v, phase_v, task_root):
        """Gets a generator for the given instruction in the given phase,
           for the specified task root."""
        #print("%-30s(%s) -- %s" % (inst_v["value"], phase_v, taskname))
        return getattr(self, "%s_%s" % (inst_v["value"], phase_v))(task_root)

    ##########################
    ### Process primitives ###
    ##########################
    def load_primitives(self, taskname):
        yield [("CALL_ARGS", [self.load_primitives_from, (taskname, 'primitives', primitive_functions)])]
        yield [("FINISH", [])]

    def load_primitives_from(self, taskname, source_name, source):
        hierarchy, = yield [("RD", [self.root, "__hierarchy"])]
        primitives, = yield [("RD", [hierarchy, source_name])]
        keys, = yield [("RDK", [primitives])]
        function_names = yield [("RV", [f]) for f in keys]
        signatures = yield [("RDN", [primitives, f]) for f in keys]
        bodies = yield [("RD", [f, "body"]) for f in signatures]
        for i in range(len(keys)):
            self.jit.register_compiled(bodies[i], getattr(source, function_names[i]), function_names[i])
        yield [("RETURN", [None])]

    def print_instruction(self, inst, indent, nested_indent=None):
        """
        intrinsics = {"integer_addition": (lambda x, y: "(%s + %s)" % (x, y)),
                      "string_join": (lambda x, y: "(str(%s) + str(%s))" % (x, y)),
                      }
        """
        intrinsics = {}

        if nested_indent is None:
            nested_indent = indent

        inst_type, = yield [("RV", [inst])]
        instruction = "(no_printer_for_%s)" % inst_type["value"]
        prev = ""

        if inst_type["value"] == "if":
            cond, true, false = yield [("RD", [inst, "cond"]),
                                       ("RD", [inst, "then"]),
                                       ("RD", [inst, "else"])]
            (prev_cond, instruction_cond), = yield [("CALL_ARGS", [self.print_instruction, (cond, 0, indent)])]
            (prev_true, instruction_true), = yield [("CALL_ARGS", [self.print_instruction, (true, indent+1)])]
            if false:
                (prev_false, instruction_false), = yield [("CALL_ARGS", [self.print_instruction, (false, indent+1)])]
                false = ("  " * indent + "else:\n%s%s") % (prev_false, instruction_false)
            else:
                false = ""

            instruction = prev_cond + \
                            "  " * indent + "if 'value' not in %s:\n" % instruction_cond + \
                            "  " * (indent + 1) + "%s['value'], = yield [('RV', [%s['id']])]\n" % (instruction_cond, instruction_cond) + \
                            "  " * indent + "if (%s['value']):\n" % instruction_cond + \
                                prev_true + \
                                instruction_true + \
                                false

        elif inst_type["value"] == "constant":
            node, = yield [("RD", [inst, "node"])]
            node_value, = yield [("RV", [node])]
            if node_value is not None:
                # There is a value to the node, so replicate the value
                if isinstance(node_value, string_types):
                    value = '"%s"' % node_value.replace('"', '\\"').replace("'", "\\'").replace('\t', '\\t').replace('\n', '\\n')
                else:
                    value = str(node_value)
                instruction = "constant_" + str(ModelverseKernel.counter)
                ModelverseKernel.counter += 1
                prev = "  " * nested_indent + instruction + " = {'value': " + value + "}\n"
            else:
                # Node is None, meaning that it was not about the value, but the node itself...
                instruction = "{'id': %s}" % str(node)

        elif inst_type["value"] == "return":
            value, = yield [("RD", [inst, "value"])]
            if value:
                (prev_value, instruction_value), = yield [("CALL_ARGS", [self.print_instruction, (value, 0, indent)])]
                instruction = prev_value + "  " * indent + "yield [('RETURN', [%s])]\n" % instruction_value
            else:
                instruction = "  " * indent + "yield [('RETURN', [None])]\n"

        elif inst_type["value"] == "declare":
            instruction = ""

        elif inst_type["value"] == "global":
            instruction = ""

        elif inst_type["value"] == "break":
            instruction = "  " * indent + "break\n"

        elif inst_type["value"] == "continue":
            instruction = "  " * indent + "continue\n"

        elif inst_type["value"] == "input":
            prev = "  " * nested_indent + "_inputs, = yield [('RD', [_root, 'input'])]\n" + \
                   "  " * nested_indent + "val, val_e, nxt = yield [('RD', [_inputs, 'value']), ('RDE', [_inputs, 'value']), ('RD', [_inputs, 'next'])]\n" + \
                   "  " * nested_indent + "_, val_e = yield [('DE', [val_e]), ('RDE', [_root, 'input'])]\n" + \
                   "  " * nested_indent + "yield [('CD', [_root, 'input', nxt]), ('DE', [val_e])]\n" + \
                   "  " * nested_indent + "_result = {'id': val}\n"

            instruction = "_result"

        elif inst_type["value"] == "output":
            value, = yield [("RD", [inst, "value"])]
            (prev, instruction), = yield [("CALL_ARGS", [self.print_instruction, (value, 0, indent)])]
            instruction = prev + \
                          "  " * indent + "if 'id' not in %s:\n" % instruction + \
                          "  " * (indent + 1) + "%s['id'], = yield [('CNV', [%s['value']])]\n" % (instruction, instruction) + \
                          "  " * indent + "_outputs, _outputs_e = yield [('RD', [_root, 'last_output']), ('RDE', [_root, 'last_output'])]\n" + \
                          "  " * indent + "_, _new = yield [('CD', [_outputs, 'value', %s['id']]), ('CN', [])]\n" % instruction + \
                          "  " * indent + "yield [('CD', [_outputs, 'next', _new]), ('DE', [_outputs_e]), ('CD', [_root, 'last_output', _new])]\n"

        elif inst_type["value"] == "resolve":
            value, = yield [("RD", [inst, "var"])]
            str_value, = yield [("RV", [value])]
            if str_value:
                # Is a global
                prev = \
                       "  " * nested_indent + "%s = _mvk.jit.cache[_taskname].get('%s', None)\n" % (str_value, str_value) + \
                       "  " * nested_indent + "if %s is None:\n" % str_value + \
                       "  " * (nested_indent + 1) + "%s, = yield [('RD', [_globs, '%s'])]\n" % (str_value, str_value) + \
                       "  " * (nested_indent + 1) + "%s, = yield [('RD', [%s, 'value'])]\n" % (str_value, str_value) + \
                       "  " * (nested_indent + 1) + "%s = {'id': %s}\n" % (str_value, str_value) + \
                       "  " * (nested_indent + 1) + "_mvk.jit.cache[_taskname]['%s'] = %s\n" % (str_value, str_value)
                instruction = str_value

                if self.jit.get_global_body_id(str_value) is None:
                    val, = yield [("RD", [self.root, self.taskname])]
                    val, = yield [("RD", [val, 'globals'])]
                    val, = yield [("RD", [val, str_value])]
                    val, = yield [("RD", [val, 'value'])]
                    val, = yield [("RD", [val, 'body'])]

                    self.jit.register_global(val, str_value)
            else:
                # Is a local
                instruction = "var_%s" % value

        elif inst_type["value"] == "assign":
            var, val = yield [("RD", [inst, "var"]),
                              ("RD", [inst, "value"])]
            (prev_var, instruction_var), = yield [("CALL_ARGS", [self.print_instruction, (var, 0, indent)])]
            (prev_val, instruction_val), = yield [("CALL_ARGS", [self.print_instruction, (val, 0, indent)])]

            instruction = prev_val + "  " * indent + instruction_var + " = " + instruction_val + "\n"

            if prev_var:
                # Got something to do before the variable is usable, so this is a global!
                # Therefore we actually do the operation in the Modelverse as well!
                instruction += \
                       "  " * nested_indent + "_var, = yield [('RD', [_globs, '%s'])]\n" % instruction_var + \
                       "  " * nested_indent + "if _var is None:\n" + \
                       "  " * (nested_indent + 1) + "_var, = yield [('CN', [])]\n" + \
                       "  " * (nested_indent + 1) + "yield [('CD', [_globs, '%s', _var])]\n" % instruction_var + \
                       "  " * nested_indent + "_old_edge, = yield [('RDE', [_var, 'value'])]\n" + \
                       "  " * nested_indent + "if 'id' not in %s:\n" % instruction_var + \
                       "  " * (nested_indent + 1) + "%s['id'], = yield [('CNV', [%s['value']])]\n" % (instruction_var, instruction_var) + \
                       "  " * nested_indent + "yield [('CD', [_var, 'value', %s['id']]), ('DE', [_old_edge])]\n" % instruction_var + \
                       "  " * nested_indent + "_mvk.jit.cache[_taskname]['%s'] = %s\n" % (instruction_var, instruction_var)

        elif inst_type["value"] == "call":
            func_name, = yield [("RD", [inst, "func"])]
            (prev_func_name, func_name), = yield [("CALL_ARGS", [self.print_instruction, (func_name, nested_indent, nested_indent)])]
            param_list = {}

            param, = yield [("RD", [inst, "params"])]
            computation = ""
            while param:
                value, name = yield [("RD", [param, "value"]), ("RD", [param, "name"])]
                name, = yield [("RV", [name])]
                (prev_res, instruction_res), = yield [("CALL_ARGS", [self.print_instruction, (value, 0, nested_indent)])]
                param, = yield [("RD", [param, "next_param"])]
                computation += prev_res
                param_list[name] = instruction_res

            value = "func_result_" + str(ModelverseKernel.counter)
            ModelverseKernel.counter += 1

            param_list = "{" + ", ".join(["'%s': %s" % (k, v) for k, v in param_list.items()]) + "}"
            actual_computation = "$$INDENT$$%s, = yield [('CALL_ARGS', [_mvk.execute_jit, (_root, %s['id'], _taskname, %s)])]\n" % (value, func_name, param_list)
                
            if indent == 0:
                # No indent, meaning that we use it inline
                # Therefore, we output the prev and value individually
                prev, instruction = prev_func_name + computation + actual_computation.replace("$$INDENT$$", "  " * nested_indent), value
            else:
                # Some indentation, meaning that we don't even use the return value
                # Therefore, we only do the yield
                prev, instruction = prev_func_name + computation, actual_computation.replace("$$INDENT$$", "  " * indent)


        elif inst_type["value"] == "access":
            value, = yield [("RD", [inst, "var"])]
            (prev, instruction), = yield [("CALL_ARGS", [self.print_instruction, (value, 0, nested_indent)])]

        elif inst_type["value"] == "while":
            cond, body = yield [("RD", [inst, "cond"]),
                                ("RD", [inst, "body"])]
            (prev_cond, instruction_cond), = yield [("CALL_ARGS", [self.print_instruction, (cond, 0, indent+1)])]
            (prev_body, instruction_body), = yield [("CALL_ARGS", [self.print_instruction, (body, indent+1)])]
            instruction = "  " * indent + "__counter_%s = 0\n" % inst + \
                          "  " * indent + "while 1:\n" + prev_cond + \
                          "  " * (indent + 1) + "if 'value' not in %s:\n" % instruction_cond + \
                          "  " * (indent + 2) + "%s['value'], = yield [('RV', [%s['id']])]\n" % (instruction_cond, instruction_cond) + \
                          "  " * (indent + 1) + "if not (%s['value']):\n" % instruction_cond + \
                          "  " * (indent + 2) + "break\n" + \
                          "  " * (indent + 1) + "else:\n" + \
                          "  " * (indent + 2) + "__counter_%s += 1\n" % inst + \
                          "  " * (indent + 2) + "if __counter_%s > 20:\n" % inst + \
                          "  " * (indent + 3) + "yield None\n" + \
                          "  " * (indent + 3) + "__counter_%s = 0\n" % inst + \
                          prev_body + instruction_body


        next_inst, = yield [("RD", [inst, "next"])]
        if next_inst:
            (prev_next, inst_next), = yield [("CALL_ARGS", [self.print_instruction, (next_inst, indent)])]
            next_inst = prev_next + inst_next
        else:
            next_inst = ""

        yield [('RETURN', [(prev, instruction + next_inst)])]

    def read_function(self, inst, suggested_name):
        initial_instruction = inst

        (params, _, is_mutable), = yield [("CALL_ARGS", [self.jit.jit_signature, (inst,)])]
        if is_mutable:
            print("Ignoring mutable or unreadable: %s" % suggested_name)
            raise jit.JitCompilationFailedException("FAIL")

        #print("Reading function: %s" % suggested_name)
        (prev, printed), = yield [("CALL_ARGS", [self.print_instruction, (inst, 1)])]
        preamble = "  _mvk = kwargs['mvk']\n" + \
                   "  _root = kwargs['task_root']\n" + \
                   "  _taskname = kwargs['taskname']\n" + \
                   "  _globs = _mvk.jit.cache[_taskname].get('_globs', None)\n" + \
                   "  if _globs is None:\n" + \
                   "    _globs, = yield [('RD', [kwargs['task_root'], 'globals'])]\n" + \
                   "    _mvk.jit.cache[_taskname]['_globs'] = _globs\n"
        printed = preamble + prev + printed
        #print("Total printed function: ")
        if params:
            func = "def " + suggested_name + "(" + ", ".join([chr(ord('a') + i) for i in range(len(params))]) + ", **kwargs):\n" + "".join(["  var_%s = %s\n" % (param, chr(ord('a') + i)) for i, param in enumerate(params)]) + printed
        else:
            func = "def " + suggested_name + "(**kwargs):\n" + printed
        
        #print(func)

        # To write out all generated functions
        #with open('/tmp/junk/%s' % suggested_name, 'w') as f:
        #    f.write(func)

        yield [("RETURN", [func])]

    def execute_jit(self, task_root, inst, taskname, params = {}, store=False, resolve=True):
        # execute_jit
        if resolve:
            try:
                inst = self.jit.body_cache[inst]
            except KeyError:
                body, = yield [("RD", [inst, "body"])]
                self.jit.body_cache[inst] = body
                inst = body

        if store:
            task_frame, =    yield [("RD", [task_root, "frame"])]
            symbols, =       yield [("RD", [task_frame, "symbols"])]
            dict_keys_ref, =  yield [("RDK", [symbols])]
            if dict_keys_ref:
                dict_keys_ref_n =   yield [("RD", [i, "name"]) for i in dict_keys_ref]
                dict_keys =       yield [("RV", [i]) for i in dict_keys_ref_n]
                dict_values_elem = yield [("RDN", [symbols, i]) for i in dict_keys_ref]
                dict_values = yield [("RD", [i, "value"]) for i in dict_values_elem]

                parameters = dict(zip(dict_keys, dict_values))
                parameters.update(params)

                for p in parameters.keys():
                    if not isinstance(parameters[p], dict):
                        parameters[p] = {'id': parameters[p]}
                params = parameters
                    
        params["root"] = self.root
        params["task_root"] = task_root
        params["taskname"] = taskname
        params["mvk"] = self

        # Have the JIT compile the function.
        if inst is None:
            suggested_name = self.jit.get_global_name(inst)
            if suggested_name is None:
                suggested_name = "func_%s" % str(inst)
            raise ValueError('body_id cannot be None: ' + str(suggested_name))

        elif inst in self.jit.jitted_entry_points:
            compiled_func = self.jit.jit_globals[self.jit.jitted_entry_points[inst]]
        else:
            compiled_func = self.jit.lookup_compiled_body(inst)

            if compiled_func is None:
                suggested_name = self.jit.get_global_name(inst)
                if suggested_name is None:
                    suggested_name = "func_%s" % str(inst)
                compiled_func, = yield [("CALL_ARGS", [self.read_function, (inst, suggested_name)])]
                compiled_func = self.merge_function(compiled_func, suggested_name)
                self.jit.register_compiled(inst, compiled_func, suggested_name)

        # Run the compiled function.
        if compiled_func == primitive_functions.dict_read:
            if "value" not in params['b']:
                params['b']['value'], = yield [("RV", [params['b']['id']])]
            result, = yield [("RD", [params['a']['id'], params['b']['value']])]
            result = {'id': result}
        else:
            results = yield [("CALL_KWARGS", [compiled_func, params])]
            if results is None:
                raise Exception("%s: primitive finished without returning a value!" % (self.debug_info[taskname]))
            else:
                result, = results
            if result is None:
                result = {'id': None, 'value': None}

        if store:
            # Clean up the current stack, as if a return happened
            old_frame, exception_return = yield [
                ("RD", [task_frame, "prev"]),
                ("RD", [task_frame, primitive_functions.EXCEPTION_RETURN_KEY])]

            #if self.debug_info[self.taskname]:
            #    self.debug_info[self.taskname].pop()

            if "id" not in result:
                result['id'], = yield [("CNV", [result['value']])]

            if exception_return is not None:
                # The caller has requested that we throw an exception instead of injecting
                # the return value into the caller's frame. Read the comment at
                # primitive_functions.EXCEPTION_RETURN_KEY for the rationale behind this design.
                yield [("CD", [task_root, "frame", old_frame]),
                       ("DN", [task_frame])]
                raise primitive_functions.InterpretedFunctionFinished(result)
            else:
                lnk, =          yield [("RDE", [old_frame, "returnvalue"])]
                _, _, _, _ =    yield [("CD", [old_frame, "returnvalue", result['id']]),
                                       ("CD", [task_root, "frame", old_frame]),
                                       ("DE", [lnk]),
                                       ("DN", [task_frame]),
                                      ]

        else:
            yield [("RETURN", [result])]
        yield [("RETURN", [None])]

    def merge_function(self, func, name):
        exec(func, self.jit.jit_globals)
        return self.jit.jit_globals[name]

    ########################################
    ### Execute input and output methods ###
    ########################################
    def get_output(self, taskname):
        task_root, =        yield [("RD", [self.root, taskname])]
        first_output, =     yield [("RD", [task_root, "output"])]
        next_output, rv =   yield [("RD", [first_output, "next"]),
                                   ("RD", [first_output, "value"]),
                                  ]
        if next_output is None:
            self.success = False
            self.returnvalue = None
        else:
            rv_value, _, _ = \
                            yield [("RV", [rv]),
                                   ("CD", [task_root, "output", next_output]),
                                   ("DN", [first_output]),
                                  ]

            self.returnvalue = rv_value
            self.success = True
            #print("OUTPUT: (%s, %s)" % (taskname, self.returnvalue))
        yield [("FINISH", [])]

    def set_input(self, taskname, value):
        task_root, =        yield [("RD", [self.root, taskname])]
        old_input, link, new_input, new_value = \
                            yield [("RD", [task_root, "last_input"]),
                                   ("RDE", [task_root, "last_input"]),
                                   ("CN", []),
                                   ("CNV", [value]),
                                  ]
        if new_value == None:
            new_value = self.root
        _, _, _, _ =        yield [("CD", [task_root, "last_input", new_input]),
                                   ("CD", [old_input, "next", new_input]),
                                   ("CD", [old_input, "value", new_value]),
                                   ("DE", [link])
                                  ]
        #print("INPUT: (%s, %s)" % (taskname, value))
        self.returnvalue = {"id": 100, "value": "success"}
        yield [("FINISH", [])]

    #############################################
    ### Transformation rules for instructions ###
    #############################################
    def continue_init(self, task_root):
        task_frame, =       yield [("RD", [task_root, "frame"])]
        inst, =             yield [("RD", [task_frame, "IP"])]
        while_inst, =       yield [("RD", [inst, "while"])]
        old_evalstack_link, old_phase_link, evalstack_roots = \
                            yield [("RDE", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "phase"]),
                                   ("RRD", [while_inst, self.taskname]),
                                  ]

        if len(evalstack_roots) == 1:
            evalstack_root = evalstack_roots[0]
        else:
            print("Got roots: " + str(evalstack_roots))
            raise Exception("Could not process continue statement!")

        prev_evalstack_roots, old_evalstack_phase_link = \
                            yield [("RRD", [evalstack_root, "prev"]),
                                   ("RDE", [evalstack_root, "phase"]),
                                  ]

        if len(prev_evalstack_roots) == 1:
            prev_evalstack_root = prev_evalstack_roots[0]
        else:
            raise Exception("Could not process continue statement!")

        new_evalstack_root, new_phase_while, new_phase_inst, prev_evalstack_root_link = \
                            yield [("CN", []),
                                   ("CNV", ["init"]),
                                   ("CNV", ["finish"]),
                                   ("RDE", [prev_evalstack_root, "prev"]),
                                  ]

        _, _, _, _, _, _, _, _  = \
                            yield [("CD", [task_frame, "evalstack", new_evalstack_root]),
                                   ("CD", [new_evalstack_root, "prev", evalstack_root]),
                                   ("CD", [task_frame, "phase", new_phase_inst]),
                                   ("CD", [evalstack_root, "phase", new_phase_while]),
                                   ("DE", [old_evalstack_link]),
                                   ("DE", [prev_evalstack_root_link]),
                                   ("DE", [old_phase_link]),
                                   ("DE", [old_evalstack_phase_link]),
                                  ]
        yield [("RETURN", [None])]

    def break_init(self, task_root):
        task_frame, =       yield [("RD", [task_root, "frame"])]
        inst, =             yield [("RD", [task_frame, "IP"])]
        while_inst, =       yield [("RD", [inst, "while"])]
        old_evalstack_link, old_phase_link, evalstack_roots = \
                            yield [("RDE", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "phase"]),
                                   ("RRD", [while_inst, self.taskname]),
                                  ]

        if len(evalstack_roots) == 1:
            evalstack_root = evalstack_roots[0]
        else:
            raise Exception("Could not process break statement!")

        prev_evalstack_roots, old_evalstack_phase_link = \
                            yield [("RRD", [evalstack_root, "prev"]),
                                   ("RDE", [evalstack_root, "phase"]),
                                  ]

        if len(prev_evalstack_roots) == 1:
            prev_evalstack_root = prev_evalstack_roots[0]
        else:
            raise Exception("Could not process break statement!")

        new_evalstack_root, new_phase_while, new_phase_inst, prev_evalstack_root_link = \
                            yield [("CN", []),
                                   ("CNV", ["finish"]),
                                   ("CNV", ["finish"]),
                                   ("RDE", [prev_evalstack_root, "prev"]),
                                  ]

        _, _, _, _, _, _, _, _  = \
                            yield [("CD", [task_frame, "evalstack", new_evalstack_root]),
                                   ("CD", [new_evalstack_root, "prev", evalstack_root]),
                                   ("CD", [task_frame, "phase", new_phase_inst]),
                                   ("CD", [evalstack_root, "phase", new_phase_while]),
                                   ("DE", [old_evalstack_link]),
                                   ("DE", [prev_evalstack_root_link]),
                                   ("DE", [old_phase_link]),
                                   ("DE", [old_evalstack_phase_link]),
                                  ]
        yield [("RETURN", [None])]

    def if_init(self, task_root):
        task_frame, =       yield [("RD", [task_root, "frame"])]
        evalstack, evalstack_link = \
                            yield [("RD", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "evalstack"]),
                                  ]
        inst, ip_link =     yield [("RD", [task_frame, "IP"]),
                                   ("RDE", [task_frame, "IP"]),
                                  ]
        cond, =             yield [("RD", [inst, "cond"])]
        new_evalstack, new_phase = \
                            yield [("CN", []),
                                   ("CNV", ["cond"]),
                                  ]
        _, _, _, _, _, _, _ = \
                            yield [("CD", [task_frame, "evalstack", new_evalstack]),
                                   ("CD", [new_evalstack, "prev", evalstack]),
                                   ("CD", [task_frame, "IP", cond]),
                                   ("CD", [evalstack, "inst", inst]),
                                   ("CD", [evalstack, "phase", new_phase]),
                                   ("DE", [evalstack_link]),
                                   ("DE", [ip_link]),
                                  ]
        yield [("RETURN", [None])]

    def if_cond(self, task_root):
        task_frame, =       yield [("RD", [task_root, "frame"])]
        returnvalue, inst = yield [("RD", [task_frame, "returnvalue"]),
                                   ("RD", [task_frame, "IP"]),
                                  ]
        returnvalue_v, =    yield [("RV", [returnvalue])]
        _else, =            yield [("RD", [inst, "else"])]

        if returnvalue_v:
            phase_link, evalstack, evalstack_link, ip_link, _then, new_evalstack, evalstack_phase, new_phase = \
                            yield [("RDE", [task_frame, "phase"]),
                                   ("RD", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "IP"]),
                                   ("RD", [inst, "then"]),
                                   ("CN", []),
                                   ("CNV", ["finish"]),
                                   ("CNV", ["init"]),
                                  ]
            _, _, _, _, _, _, _, _, _ = \
                            yield [("CD", [task_frame, "evalstack", new_evalstack]),
                                   ("CD", [task_frame, "IP", _then]),
                                   ("CD", [new_evalstack, "prev", evalstack]),
                                   ("CD", [evalstack, "inst", inst]),
                                   ("CD", [evalstack, "phase", evalstack_phase]),
                                   ("CD", [task_frame, "phase", new_phase]),
                                   ("DE", [evalstack_link]),
                                   ("DE", [ip_link]),
                                   ("DE", [phase_link]),
                                  ]
        elif _else is None:
            phase_link, new_phase = \
                            yield [("RDE", [task_frame, "phase"]),
                                   ("CNV", ["finish"]),
                                  ]
            _, _ =          yield [("CD", [task_frame, "phase", new_phase]),
                                   ("DE", [phase_link]),
                                  ]
        else:
            phase_link, evalstack, evalstack_link, ip_link = \
                            yield [("RDE", [task_frame, "phase"]),
                                   ("RD", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "IP"]),
                                  ]
            new_evalstack, new_phase, evalstack_phase = \
                            yield [("CN", []),
                                   ("CNV", ["init"]),
                                   ("CNV", ["finish"]),
                                  ]
            _, _, _, _, _, _, _, _, _ = \
                            yield [("CD", [task_frame, "evalstack", new_evalstack]),
                                   ("CD", [task_frame, "IP", _else]),
                                   ("CD", [new_evalstack, "prev", evalstack]),
                                   ("CD", [evalstack, "inst", inst]),
                                   ("CD", [evalstack, "phase", evalstack_phase]),
                                   ("CD", [task_frame, "phase", new_phase]),
                                   ("DE", [evalstack_link]),
                                   ("DE", [ip_link]),
                                   ("DE", [phase_link]),
                                  ]
        yield [("RETURN", [None])]

    def while_init(self, task_root):
        task_frame, =       yield [("RD", [task_root, "frame"])]
        evalstack, evalstack_link, ip_link, inst = \
                            yield [("RD", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "IP"]),
                                   ("RD", [task_frame, "IP"]),
                                  ]
        cond, new_evalstack, new_phase = \
                            yield [("RD", [inst, "cond"]),
                                   ("CN", []),
                                   ("CNV", ["cond"]),
                                  ]
        _, _, _, _, _, _, _ = \
                            yield [("CD", [task_frame, "evalstack", new_evalstack]),
                                   ("CD", [new_evalstack, "prev", evalstack]),
                                   ("CD", [task_frame, "IP", cond]),
                                   ("CD", [evalstack, "phase", new_phase]),
                                   ("CD", [evalstack, "inst", inst]),
                                   ("DE", [evalstack_link]),
                                   ("DE", [ip_link]),
                                  ]
        yield [("RETURN", [None])]

    def while_cond(self, task_root):
        task_frame, =       yield [("RD", [task_root, "frame"])]
        returnvalue, =      yield [("RD", [task_frame, "returnvalue"])]
        returnvalue_v, =    yield [("RV", [returnvalue])]

        if returnvalue_v:
            phase_link, evalstack, evalstack_link, ip_link, inst = \
                            yield [("RDE", [task_frame, "phase"]),
                                   ("RD", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "IP"]),
                                   ("RD", [task_frame, "IP"]),
                                  ]
            body, =         yield [("RD", [inst, "body"])]
            new_evalstack, new_phase, evalstack_phase = \
                            yield [("CN", []),
                                   ("CNV", ["init"]),
                                   ("CNV", ["init"]),
                                  ]
            _, _, _, _, _, _, _, _, _ = \
                            yield [("CD", [task_frame, "IP", body]),
                                   ("CD", [task_frame, "phase", new_phase]),
                                   ("CD", [task_frame, "evalstack", new_evalstack]),
                                   ("CD", [new_evalstack, "prev", evalstack]),
                                   ("CD", [evalstack, "inst", inst]),
                                   ("CD", [evalstack, "phase", evalstack_phase]),
                                   ("DE", [evalstack_link]),
                                   ("DE", [ip_link]),
                                   ("DE", [phase_link]),
                                  ]

            # Check if we already have a taskname link to the evalstack
            links, = yield [("RD", [evalstack, self.taskname])]
            if links is None:
                yield [("CD", [evalstack, self.taskname, inst])]
        else:
            phase_link, new_phase = \
                            yield [("RDE", [task_frame, "phase"]),
                                   ("CNV", ["finish"]),
                                  ]
            _, _ =          yield [("CD", [task_frame, "phase", new_phase]),
                                   ("DE", [phase_link])
                                  ]
        yield [("RETURN", [None])]

    def access_init(self, task_root):
        task_frame, =       yield [("RD", [task_root, "frame"])]
        evalstack, evalstack_link, inst, ip_link = \
                            yield [("RD", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "evalstack"]),
                                   ("RD", [task_frame, "IP"]),
                                   ("RDE", [task_frame, "IP"]),
                                  ]
        var, new_evalstack, new_phase = \
                            yield [("RD", [inst, "var"]),
                                   ("CN", []),
                                   ("CNV", ["eval"]),
                                  ]
        _, _, _, _, _, _, _ = \
                            yield [("CD", [task_frame, "IP", var]),
                                   ("CD", [task_frame, "evalstack", new_evalstack]),
                                   ("CD", [new_evalstack, "prev", evalstack]),
                                   ("CD", [evalstack, "inst", inst]),
                                   ("CD", [evalstack, "phase", new_phase]),
                                   ("DE", [evalstack_link]),
                                   ("DE", [ip_link]),
                                  ]
        yield [("RETURN", [None])]

    def access_eval(self, task_root):
        task_frame, =       yield [("RD", [task_root, "frame"])]
        phase_link, returnvalue_link, returnvalue = \
                            yield [("RDE", [task_frame, "phase"]),
                                   ("RDE", [task_frame, "returnvalue"]),
                                   ("RD", [task_frame, "returnvalue"]),
                                  ]
        value, new_phase =  yield [("RD", [returnvalue, "value"]),
                                   ("CNV", ["finish"]),
                                  ]
        _, _, _, _ =        yield [("CD", [task_frame, "phase", new_phase]),
                                   ("CD", [task_frame, "returnvalue", value]),
                                   ("DE", [phase_link]),
                                   ("DE", [returnvalue_link]),
                                  ]
        yield [("RETURN", [None])]

    def resolve_init(self, task_root):
        task_frame, =       yield [("RD", [task_root, "frame"])]
        symbols, evalstack, evalstack_link, ip_link, inst = \
                            yield [("RD", [task_frame, "symbols"]),
                                   ("RD", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "IP"]),
                                   ("RD", [task_frame, "IP"]),
                                  ]
        var, =              yield [("RD", [inst, "var"])]
        variable, =         yield [("RDN", [symbols, var])]

        if variable is None:
            phase_link, returnvalue_link, _globals, var_name = \
                            yield [("RDE", [task_frame, "phase"]),
                                   ("RDE", [task_frame, "returnvalue"]),
                                   ("RD", [task_root, "globals"]),
                                   ("RV", [var]),
                                  ]
            variable, new_phase = \
                            yield [("RD", [_globals, var_name]),
                                   ("CNV", ["finish"]),
                                  ]
            if variable is None:
                globs, = yield [("RDK", [_globals])]
                print("Globals: " + str(globs))
                globs = yield [("RV", [i]) for i in globs]
                print("Resolved globals: " + str(globs))
                raise Exception(jit.GLOBAL_NOT_FOUND_MESSAGE_FORMAT % 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
            if self.allow_compiled:
                compiled_function = getattr(compiled_functions, var_name, None)
                if compiled_function is not None:
                    # We have a compiled function ready!
                    # 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.jit.register_compiled(compiler_body, compiled_function, var_name)

            # If we're dealing with a function, then we might want to figure out what its body id
            # is now so we can suggest a name to the JIT later.
            if self.jit.get_global_body_id(var_name) is None:
                compiler_val, = yield [("RD", [variable, "value"])]
                if compiler_val is not None:
                    compiler_body, = yield [("RD", [compiler_val, "body"])]
                    if compiler_body is not None:
                        self.jit.register_global(compiler_body, var_name)

        else:
            phase_link, returnvalue_link, new_phase = \
                            yield [("RDE", [task_frame, "phase"]),
                                   ("RDE", [task_frame, "returnvalue"]),
                                   ("CNV", ["finish"]),
                                  ]
        _, _, _, _ =        yield [("CD", [task_frame, "phase", new_phase]),
                                   ("CD", [task_frame, "returnvalue", variable]),
                                   ("DE", [phase_link]),
                                   ("DE", [returnvalue_link]),
                                  ]
        yield [("RETURN", [None])]

    def assign_init(self, task_root):
        task_frame, =       yield [("RD", [task_root, "frame"])]
        evalstack, evalstack_link, ip_link, inst = \
                            yield [("RD", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "IP"]),
                                   ("RD", [task_frame, "IP"]),
                                  ]
        var, new_evalstack, new_phase = \
                            yield [("RD", [inst, "var"]),
                                   ("CN", []),
                                   ("CNV", ["value"]),
                                  ]
        _, _, _, _, _, _, _ = \
                            yield [("CD", [task_frame, "IP", var]),
                                   ("CD", [task_frame, "evalstack", new_evalstack]),
                                   ("CD", [new_evalstack, "prev", evalstack]),
                                   ("CD", [evalstack, "inst", inst]),
                                   ("CD", [evalstack, "phase", new_phase]),
                                   ("DE", [evalstack_link]),
                                   ("DE", [ip_link]),
                                  ]
        yield [("RETURN", [None])]

    def assign_value(self, task_root):
        task_frame, =       yield [("RD", [task_root, "frame"])]
        phase_link, evalstack, returnvalue, evalstack_link, ip_link, inst = \
                            yield [("RDE", [task_frame, "phase"]),
                                   ("RD", [task_frame, "evalstack"]),
                                   ("RD", [task_frame, "returnvalue"]),
                                   ("RDE", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "IP"]),
                                   ("RD", [task_frame, "IP"]),
                                  ]
        value, new_evalstack, new_phase, evalstack_phase = \
                            yield [("RD", [inst, "value"]),
                                   ("CN", []),
                                   ("CNV", ["init"]),
                                   ("CNV", ["assign"]),
                                  ]

        _, _, _, _, _, _, _, _, _, _ = \
                            yield [("CD", [task_frame, "variable", returnvalue]),
                                   ("CD", [task_frame, "phase", new_phase]),
                                   ("CD", [task_frame, "evalstack", new_evalstack]),
                                   ("CD", [new_evalstack, "prev", evalstack]),
                                   ("CD", [evalstack, "inst", inst]),
                                   ("CD", [evalstack, "phase", evalstack_phase]),
                                   ("CD", [task_frame, "IP", value]),
                                   ("DE", [evalstack_link]),
                                   ("DE", [phase_link]),
                                   ("DE", [ip_link]),
                                  ]
        yield [("RETURN", [None])]

    def assign_assign(self, task_root):
        task_frame, =       yield [("RD", [task_root, "frame"])]
        phase_link, returnvalue, variable_link, variable = \
                            yield [("RDE", [task_frame, "phase"]),
                                   ("RD", [task_frame, "returnvalue"]),
                                   ("RDE", [task_frame, "variable"]),
                                   ("RD", [task_frame, "variable"]),
                                  ]
        value_link, new_phase = \
                            yield [("RDE", [variable, "value"]),
                                   ("CNV", ["finish"]),
                                  ]
        _, _, _, _, _ =     yield [("CD", [task_frame, "phase", new_phase]),
                                   ("CD", [variable, "value", returnvalue]),
                                   ("DE", [variable_link]),
                                   ("DE", [value_link]),
                                   ("DE", [phase_link]),
                                  ]
        yield [("RETURN", [None])]
                    
    def return_init(self, task_root):
        task_frame, =       yield [("RD", [task_root, "frame"])]
        inst, =             yield [("RD", [task_frame, "IP"])]
        value, =            yield [("RD", [inst, "value"])]

        if value is None:
            prev_frame, =   yield [("RD", [task_frame, "prev"])]
            # If the callee's frame is marked with the '__exception_return' key, then
            # we need to throw an exception instead of just finishing here. This design
            # gives us O(1) state reads per jit-interpreter transition.
            exception_return, = yield [("RD", [task_frame, primitive_functions.EXCEPTION_RETURN_KEY])]
            if prev_frame is None:
                _, =            yield [("DN", [task_root])]
                del self.debug_info[self.taskname]
                #print("Cleanup task " + str(self.taskname))
            else:
                if self.debug_info[self.taskname]:
                    self.debug_info[self.taskname].pop()
                _, _ =          yield [("CD", [task_root, "frame", prev_frame]),
                                       ("DN", [task_frame]),
                                      ]

            if exception_return is not None:
                raise primitive_functions.InterpretedFunctionFinished(None)
        else:
            evalstack, evalstack_link, ip_link, new_evalstack, evalstack_phase = \
                            yield [("RD", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "IP"]),
                                   ("CN", []),
                                   ("CNV", ["eval"]),
                                  ]
            _, _, _, _, _, _, _ = \
                            yield [("CD", [task_frame, "evalstack", new_evalstack]),
                                   ("CD", [new_evalstack, "prev", evalstack]),
                                   ("CD", [evalstack, "inst", inst]),
                                   ("CD", [evalstack, "phase", evalstack_phase]),
                                   ("CD", [task_frame, "IP", value]),
                                   ("DE", [evalstack_link]),
                                   ("DE", [ip_link]),
                                  ]
        yield [("RETURN", [None])]

    def return_eval(self, task_root):
        if self.debug_info[self.taskname]:
            self.debug_info[self.taskname].pop()

        task_frame, = yield [("RD", [task_root, "frame"])]
        prev_frame, = yield [("RD", [task_frame, "prev"])]
        if prev_frame is None:
            _, =            yield [("DN", [task_root])]
            del self.debug_info[self.taskname]
        exception_return, returnvalue = yield [
            ("RD", [task_frame, primitive_functions.EXCEPTION_RETURN_KEY]),
            ("RD", [task_frame, "returnvalue"])]

        # If the callee's frame is marked with the '__exception_return' key, then
        # we need to throw an exception instead of just finishing here. This design
        # gives us O(1) state reads per jit-interpreter transition.
        if exception_return is not None:
            yield [
                ("CD", [task_root, "frame", prev_frame]),
                ("DN", [task_frame])]
            raise primitive_functions.InterpretedFunctionFinished(returnvalue)
        else:
            old_returnvalue_link, = yield [("RDE", [prev_frame, "returnvalue"])]
            yield [
                ("CD", [task_root, "frame", prev_frame]),
                ("CD", [prev_frame, "returnvalue", returnvalue]),
                ("DE", [old_returnvalue_link]),
                ("DN", [task_frame])]
        yield [("RETURN", [None])]

    def constant_init(self, task_root):
        task_frame, =       yield [("RD", [task_root, "frame"])]
        phase_link, returnvalue_link, inst = \
                            yield [("RDE", [task_frame, "phase"]),
                                   ("RDE", [task_frame, "returnvalue"]),
                                   ("RD", [task_frame, "IP"]),
                                  ]
        node, new_phase =   yield [("RD", [inst, "node"]),
                                   ("CNV", ["finish"]),
                                  ]
        _, _, _, _ =        yield [("CD", [task_frame, "phase", new_phase]),
                                   ("CD", [task_frame, "returnvalue", node]),
                                   ("DE", [returnvalue_link]),
                                   ("DE", [phase_link]),
                                  ]
        yield [("RETURN", [None])]

    def helper_init(self, task_root):
        task_frame, =       yield [("RD", [task_root, "frame"])]
        inst, =             yield [("RD", [task_frame, "IP"])]
        next, =             yield [("RD", [inst, "next"])]

        if next is None:
            ip_link, phase_link, evalstack_top = \
                            yield [("RDE", [task_frame, "IP"]),
                                   ("RDE", [task_frame, "phase"]),
                                   ("RD", [task_frame, "evalstack"]),
                                  ]
            evalstack, =    yield [("RD", [evalstack_top, "prev"])]
            evalstack_inst, evalstack_phase, evalstack_inst_link, evalstack_phase_link = \
                            yield [("RD", [evalstack, "inst"]),
                                   ("RD", [evalstack, "phase"]),
                                   ("RDE", [evalstack, "inst"]),
                                   ("RDE", [evalstack, "phase"]),
                                  ]
            _, _, _, _, _, _, _, _ = \
                            yield [("CD", [task_frame, "evalstack", evalstack]),
                                   ("CD", [task_frame, "IP", evalstack_inst]),
                                   ("CD", [task_frame, "phase", evalstack_phase]),
                                   ("DE", [ip_link]),
                                   ("DE", [phase_link]),
                                   ("DE", [evalstack_inst_link]),
                                   ("DE", [evalstack_phase_link]),
                                   ("DN", [evalstack_top]),
                                  ]
        else:
            ip_link, phase_link, new_phase = \
                            yield [("RDE", [task_frame, "IP"]),
                                   ("RDE", [task_frame, "phase"]),
                                   ("CNV", ["init"]),
                                  ]
            _, _, _, _ =    yield [("CD", [task_frame, "IP", next]),
                                   ("CD", [task_frame, "phase", new_phase]),
                                   ("DE", [ip_link]),
                                   ("DE", [phase_link]),
                                  ]
        yield [("RETURN", [None])]

    def call_init(self, task_root):
        task_frame, =       yield [("RD", [task_root, "frame"])]
        symbols, evalstack, evalstack_link, ip_link, inst = \
                            yield [("RD", [task_frame, "symbols"]),
                                   ("RD", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "IP"]),
                                   ("RD", [task_frame, "IP"]),
                                  ]
        func, params =      yield [("RD", [inst, "func"]),
                                   ("RD", [inst, "params"]),
                                  ]
        
        if params is None:
            new_evalstack, evalstack_phase = \
                            yield [("CN", []),
                                   ("CNV", ["call"]),
                                  ]
            _, _, _, _, _, _, _ = \
                            yield [("CD", [task_frame, "evalstack", new_evalstack]),
                                   ("CD", [new_evalstack, "prev", evalstack]),
                                   ("CD", [evalstack, "inst", inst]),
                                   ("CD", [evalstack, "phase", evalstack_phase]),
                                   ("CD", [task_frame, "IP", func]),
                                   ("DE", [evalstack_link]),
                                   ("DE", [ip_link]),
                                  ]
        else:
            new_evalstack,= yield [("CN", [])]
            _, _, _, _, _, _, _ = \
                            yield [("CD", [task_frame, "evalstack", new_evalstack]),
                                   ("CD", [new_evalstack, "prev", evalstack]),
                                   ("CD", [evalstack, "inst", inst]),
                                   ("CD", [evalstack, "phase", params]),
                                   ("CD", [task_frame, "IP", func]),
                                   ("DE", [evalstack_link]),
                                   ("DE", [ip_link]),
                                  ]
        yield [("RETURN", [None])]

    def call_call(self, task_root):
        self.debug_info[self.taskname].append("None")
        task_frame, =       yield [("RD", [task_root, "frame"])]
        inst, =             yield [("RD", [task_frame, "IP"])]
        param, =            yield [("RD", [inst, "last_param"])]

        if param is None:
            returnvalue, =  yield [("RD", [task_frame, "returnvalue"])]
            body, =         yield [("RD", [returnvalue, "body"])]
            self.jit.mark_entry_point(body)

            phase_link, frame_link, prev_phase, new_phase, new_frame, new_evalstack, new_symbols, new_returnvalue = \
                            yield [("RDE", [task_frame, "phase"]),
                                   ("RDE", [task_root, "frame"]),
                                   ("CNV", ["finish"]),
                                   ("CNV", ["init"]),
                                   ("CN", []),
                                   ("CN", []),
                                   ("CN", []),
                                   ("CN", []),
                                  ]
            _, _, _, _, _, _, _, _, _, _, _ = \
                            yield [("CD", [task_root, "frame", new_frame]),
                                   ("CD", [new_frame, "evalstack", new_evalstack]),
                                   ("CD", [new_frame, "symbols", new_symbols]),
                                   ("CD", [new_frame, "returnvalue", new_returnvalue]),
                                   ("CD", [new_frame, "caller", inst]),
                                   ("CD", [new_frame, "phase", new_phase]),
                                   ("CD", [new_frame, "IP", body]),
                                   ("CD", [new_frame, "prev", task_frame]),
                                   ("CD", [task_frame, "phase", prev_phase]),
                                   ("DE", [phase_link]),
                                   ("DE", [frame_link]),
                                  ]
        else:
            newer_frames, invoking_frames = \
                            yield [("RRD", [task_frame, "prev"]),
                                   ("RRD", [inst, "caller"]),
                                  ]
            new_frame = self.find_overlapping(newer_frames, invoking_frames)
            phase_link, frame_link, new_symbols, new_IP = \
                            yield [("RDE", [task_frame, "phase"]),
                                   ("RDE", [task_root, "frame"]),
                                   ("RD", [new_frame, "symbols"]),
                                   ("RD", [new_frame, "IP"]),
                                  ]
            signature, =    yield [("RRD", [new_IP, "body"])]
            signature = signature[0]
            sig_params, last_param = \
                            yield [("RD", [signature, "params"]),
                                   ("RD", [inst, "last_param"]),
                                  ]

            self.jit.mark_entry_point(new_IP)
            
            name, =         yield [("RD", [last_param, "name"])]
            name_value, =   yield [("RV", [name])]
            returnvalue, formal_parameter, new_phase, variable = \
                            yield [("RD", [task_frame, "returnvalue"]),
                                   ("RD", [sig_params, name_value]),
                                   ("CNV", ["finish"]),
                                   ("CN", []),
                                  ]
            _, _, _, t1 =   yield [("CD", [task_root, "frame", new_frame]),
                                   ("CD", [task_frame, "phase", new_phase]),
                                   ("CD", [variable, "value", returnvalue]),
                                   ("CE", [new_symbols, variable]),
                                  ]
            _, _, _ =       yield [("CE", [t1, formal_parameter]),
                                   ("DE", [frame_link]),
                                   ("DE", [phase_link]),
                                  ]
        yield [("RETURN", [None])]

    def find_overlapping(self, a, b):
        newer_frames = set(a)
        invoking_frames = set(b)
        matches = list(newer_frames.intersection(invoking_frames))
        if len(matches) == 1:
            return matches[0]
        elif len(matches) > 1:
            raise Exception("Error: multiple overlapping elements")
        else:
            raise Exception("Error: could not find any overlap")

    def call_param(self, task_root):
        task_frame, =       yield [("RD", [task_root, "frame"])]
        inst, phase =       yield [("RD", [task_frame, "IP"]),
                                   ("RD", [task_frame, "phase"]),
                                  ]
        params, last_param = \
                            yield [("RD", [inst, "params"]),
                                   ("RD", [inst, "last_param"]),
                                  ]
        next_param, =       yield [("RD", [params, "next_param"])]

        if params == phase:
            phase_link, ip_link, returnvalue, param_value, evalstack, evalstack_link = \
                            yield [("RDE", [task_frame, "phase"]),
                                   ("RDE", [task_frame, "IP"]),
                                   ("RD", [task_frame, "returnvalue"]),
                                   ("RD", [params, "value"]),
                                   ("RD", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "evalstack"]),
                                  ]
            body, =         yield [("RD", [returnvalue, "body"])]
            new_frame, prev_evalstack, new_phase, prev_phase, new_evalstack, new_symbols, new_returnvalue = \
                            yield [("CN", []),
                                   ("CN", []),
                                   ("CNV", ["init"]),
                                   ("CNV", ["init"]),
                                   ("CN", []),
                                   ("CN", []),
                                   ("CN", []),
                                  ]
            _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = \
                            yield [("CD", [new_frame, "evalstack", new_evalstack]),
                                   ("CD", [new_frame, "symbols", new_symbols]),
                                   ("CD", [new_frame, "returnvalue", new_returnvalue]),
                                   ("CD", [new_frame, "caller", inst]),
                                   ("CD", [new_frame, "phase", new_phase]),
                                   ("CD", [new_frame, "IP", body]),
                                   ("CD", [new_frame, "prev", task_frame]),
                                   ("CD", [task_frame, "phase", prev_phase]),
                                   ("CD", [task_frame, "IP", param_value]),
                                   ("CD", [prev_evalstack, "prev", evalstack]),
                                   ("CD", [evalstack, "inst", inst]),
                                   ("CD", [task_frame, "evalstack", prev_evalstack]),
                                   ("DE", [evalstack_link]),
                                   ("DE", [ip_link]),
                                   ("DE", [phase_link]),
                                  ]
            if next_param is not None:
                _ =         yield [("CD", [evalstack, "phase", next_param])]
            else:
                evalstack_phase, = \
                            yield [("CNV", ["call"])]
                _ =         yield [("CD", [evalstack, "phase", evalstack_phase])]
        else:
            frame_link, phase_link, newer_frames, invoking_frames = \
                            yield [("RDE", [task_root, "frame"]),
                                   ("RDE", [task_frame, "phase"]),
                                   ("RRD", [task_frame, "prev"]),
                                   ("RRD", [inst, "caller"]),
                                  ]
            new_frame = self.find_overlapping(newer_frames, invoking_frames)
            ip_link, evalstack, evalstack_link, new_symbols, new_IP = \
                            yield [("RDE", [task_frame, "IP"]),
                                   ("RD", [task_frame, "evalstack"]),
                                   ("RDE", [task_frame, "evalstack"]),
                                   ("RD", [new_frame, "symbols"]),
                                   ("RD", [new_frame, "IP"]),
                                  ]
            signature, =    yield [("RRD", [new_IP, "body"])]
            signature = signature[0]
            sig_params, =   yield [("RD", [signature, "params"])]

            if last_param == phase:
                prev_param, = \
                            yield [("RRD", [last_param, "next_param"])]
                prev_param = prev_param[0]
                name, =     yield [("RD", [prev_param, "name"])]
                name_value, = \
                            yield [("RV", [name])]
                evalstack_phase, = \
                            yield [("CNV", ["call"])]
                _ =         yield [("CD", [evalstack, "phase", evalstack_phase])]
                formal_parameter, param_value = \
                            yield [("RD", [sig_params, name_value]),
                                   ("RD", [last_param, "value"]),
                                  ]
            else:
                param_b, =  yield [("RD", [task_frame, "phase"])]
                param_c, param_a = \
                            yield [("RD", [param_b, "next_param"]),
                                   ("RRD", [param_b, "next_param"]),
                                  ]
                param_a = param_a[0]
                name, param_value = \
                            yield [("RD", [param_a, "name"]),
                                   ("RD", [param_b, "value"]),
                                  ]
                name_value, = \
                            yield [("RV", [name])]
                formal_parameter, _ = \
                            yield [("RD", [sig_params, name_value]),
                                   ("CD", [evalstack, "phase", param_c]),
                                  ]

            new_phase, new_evalstack, variable, returnvalue = \
                        yield [("CNV", ["init"]),
                               ("CN", []),
                               ("CN", []),
                               ("RD", [task_frame, "returnvalue"]),
                              ]
            _, _, _, _, _, _ = \
                        yield [("CD", [task_frame, "evalstack", new_evalstack]),
                               ("CD", [new_evalstack, "prev", evalstack]),
                               ("CD", [evalstack, "inst", inst]),
                               ("CD", [task_frame, "phase", new_phase]),
                               ("CD", [task_frame, "IP", param_value]),
                               ("CD", [variable, "value", returnvalue]),
                              ]

            t1, =       yield [("CE", [new_symbols, variable])]
            _, _, _, _ = \
                        yield [("CE", [t1, formal_parameter]),
                               ("DE", [phase_link]),
                               ("DE", [ip_link]),
                               ("DE", [evalstack_link]),
                              ]
        yield [("RETURN", [None])]

    def input_init(self, task_root):
        task_frame, =   yield [("RD", [task_root, "frame"])]
        returnvalue_link, _input = \
                        yield [("RDE", [task_frame, "returnvalue"]),
                               ("RD", [task_root, "input"]),
                              ]
        value, next, phase_link = \
                        yield [("RD", [_input, "value"]),
                               ("RD", [_input, "next"]),
                               ("RDE", [task_frame, "phase"]),
                              ]

        if value is not None:
            v =         yield [("RV", [value])]
            _, _, finish = \
                        yield [("CD", [task_frame, "returnvalue", value]),
                               ("CD", [task_root, "input", next]),
                               ("CNV", ["finish"]),
                              ]
            _, _, _, _ = \
                        yield [("CD", [task_frame, "phase", finish]),
                               ("DN", [_input]),
                               ("DE", [returnvalue_link]),
                               ("DE", [phase_link]),
                              ]
            self.input_value = value
        else:
            # No input yet, so just wait and don't advance IP or phase
            self.input_value = None
            ex = primitive_functions.SleepKernel(0.1, True)
            raise ex
        yield [("RETURN", [None])]

    def output_init(self, task_root):
        task_frame, =   yield [("RD", [task_root, "frame"])]
        evalstack, evalstack_link, ip_link, inst = \
                        yield [("RD", [task_frame, "evalstack"]),
                               ("RDE", [task_frame, "evalstack"]),
                               ("RDE", [task_frame, "IP"]),
                               ("RD", [task_frame, "IP"]),
                              ]
        value, new_evalstack, evalstack_phase = \
                        yield [("RD", [inst, "value"]),
                               ("CN", []),
                               ("CNV", ["output"]),
                              ]
        _, _, _, _, _, _, _ = \
                        yield [("CD", [task_frame, "evalstack", new_evalstack]),
                               ("CD", [new_evalstack, "prev", evalstack]),
                               ("CD", [evalstack, "inst", inst]),
                               ("CD", [evalstack, "phase", evalstack_phase]),
                               ("CD", [task_frame, "IP", value]),
                               ("DE", [evalstack_link]),
                               ("DE", [ip_link]),
                              ]
        yield [("RETURN", [None])]

    def output_output(self, task_root):
        task_frame, =   yield [("RD", [task_root, "frame"])]
        returnvalue_link, returnvalue, last_output, phase_link, last_output_link, new_last_output, finish = \
                        yield [("RDE", [task_frame, "returnvalue"]),
                               ("RD", [task_frame, "returnvalue"]),
                               ("RD", [task_root, "last_output"]),
                               ("RDE", [task_frame, "phase"]),
                               ("RDE", [task_root, "last_output"]),
                               ("CN", []),
                               ("CNV", ["finish"]),
                              ]
        _, _, _, _, _, _ = \
                        yield [("CD", [last_output, "value", returnvalue]),
                               ("CD", [last_output, "next", new_last_output]),
                               ("CD", [task_root, "last_output", new_last_output]),
                               ("CD", [task_frame, "phase", finish]),
                               ("DE", [last_output_link]),
                               ("DE", [phase_link]),
                              ]
        yield [("RETURN", [None])]

    def declare_init(self, task_root):
        task_frame, =   yield [("RD", [task_root, "frame"])]
        inst, =         yield [("RD", [task_frame, "IP"])]
        new_var, symbols, phase_link, empty_node, new_phase = \
                        yield [("RD", [inst, "var"]),
                               ("RD", [task_frame, "symbols"]),
                               ("RDE", [task_frame, "phase"]),
                               ("CN", []),
                               ("CNV", ["finish"]),
                              ]

        exists, =       yield [("RDN", [symbols, new_var])]
        if exists is None:
            new_edge, = yield [("CE", [symbols, empty_node])]
            _ =         yield [("CE", [new_edge, new_var])]

        _, _ =          yield [("CD", [task_frame, "phase", new_phase]),
                               ("DE", [phase_link]),
                              ]
        yield [("RETURN", [None])]

    def global_init(self, task_root):
        task_frame, =   yield [("RD", [task_root, "frame"])]
        inst, =         yield [("RD", [task_frame, "IP"])]
        new_var, global_symbols, phase_link, empty_node, new_phase = \
                        yield [("RD", [inst, "var"]),
                               ("RD", [task_root, "globals"]),
                               ("RDE", [task_frame, "phase"]),
                               ("CN", []),
                               ("CNV", ["finish"]),
                              ]

        value, =        yield [("RV", [new_var])]
        exists, =       yield [("RDE", [global_symbols, value])]

        if exists is not None:
            yield [("DE", [exists])]
        
        yield [("CD", [global_symbols, value, empty_node])]

        _, _ =          yield [("CD", [task_frame, "phase", new_phase]),
                               ("DE", [phase_link])
                              ]
        yield [("RETURN", [None])]
