Example #1
0
def rewrite_overlays(func, env=None):
    """
    Resolve overlays of constants.
    """
    for op in func.ops:
        consts = collect_constants(op)
        new = []
        for c in consts:
            overlain = overlay_registry.lookup_overlay(c.const)
            if overlain:
                c = Const(overlain, type=c.type)
            new.append(c)
        substitute_args(op, consts, new)
Example #2
0
def rewrite_lowlevel_constants(func, env):
    """
    Rewrite constant pointers.
    """
    b = Builder(func)
    b.position_at_beginning(func.startblock)

    for op in func.ops:
        constants = collect_constants(op)
        new_constants = []
        for c in constants:
            new_constants.append(allocate_pointer_const(b, c))
        substitute_args(op, constants, new_constants)
Example #3
0
def rewrite_overlays(func, env=None):
    """
    Resolve overlays of constants.
    """
    for op in func.ops:
        consts = collect_constants(op)
        new = []
        for c in consts:
            overlain = overlay_registry.lookup_overlay(c.const)
            if overlain:
                c = Const(overlain, type=c.type)
            new.append(c)
        substitute_args(op, consts, new)
Example #4
0
def rewrite_lowlevel_constants(func, env):
    """
    Rewrite constant pointers.
    """
    b = Builder(func)
    b.position_at_beginning(func.startblock)

    for op in func.ops:
        constants = collect_constants(op)
        new_constants = []
        for c in constants:
            new_constants.append(allocate_pointer_const(b, c))
        substitute_args(op, constants, new_constants)
Example #5
0
def rewrite_externs(func, env):
    """Rewrite external symbol references

    Base on flypy.compiler.lower.constants.rewrite_constants
    """
    if env['flypy.state.opaque']:
        return
    target = env['flypy.target']
    # For each operation
    for op in func.ops:
        # Only for call operation
        if op.opcode == 'call':
            # For each constant
            constants = ir.collect_constants(op)
            new_constants = []
            for c in constants:
                extern = c.const

                if extern_support.is_extern_symbol(extern):
                    # Make a declare-only function
                    argtypes = extern.type.argtypes
                    restype = extern.type.restype

                    if target == "cpu":
                        # Install external symbol for CPU target
                        extern.install()

                    functype = ptypes.Function(lltype(restype),
                                               [lltype(t) for t in argtypes],
                                               extern.type.varargs)
                    # Note: Global value should really be part inserted into
                    # a module.  But there are no module support at this point.
                    replacment = ir.GlobalValue(extern.name, functype,
                                                external=True)
                else:
                    # No change
                    replacment = c
                # Add replacement
                new_constants.append(replacment)
            # Replace
            ir.substitute_args(op, constants, new_constants)
Example #6
0
def rewrite_externs(func, env):
    """Rewrite external symbol references

    Base on flypy.compiler.lower.constants.rewrite_constants
    """
    if env["flypy.state.opaque"]:
        return
    target = env["flypy.target"]
    # For each operation
    for op in func.ops:
        # Only for call operation
        if op.opcode == "call":
            # For each constant
            constants = ir.collect_constants(op)
            new_constants = []
            for c in constants:
                extern = c.const

                if extern_support.is_extern_symbol(extern):
                    # Make a declare-only function
                    argtypes = extern.type.argtypes
                    restype = extern.type.restype

                    if target == "cpu":
                        # Install external symbol for CPU target
                        extern.install()

                    functype = ptypes.Function(lltype(restype), [lltype(t) for t in argtypes], extern.type.varargs)
                    # Note: Global value should really be part inserted into
                    # a module.  But there are no module support at this point.
                    replacment = ir.GlobalValue(extern.name, functype, external=True)
                else:
                    # No change
                    replacment = c
                # Add replacement
                new_constants.append(replacment)
            # Replace
            ir.substitute_args(op, constants, new_constants)
Example #7
0
def rewrite_constants(func, env):
    """
    Rewrite constants with user-defined types to IR constants. Also rewrite
    constants of builtins to instances of numba classes.

        e.g. constant(None)  -> constant(NoneValue)
             constant("foo") -> constant(Bytes("foo"))
    """
    if env['numba.state.opaque']:
        return

    context = env['numba.typing.context']

    for op in func.ops:
        if op.opcode == 'exc_catch':
            continue
        constants = collect_constants(op)
        new_constants = []
        for c in constants:
            ty = context[c]

            # Python -> Numba (if not already)
            numba_obj = fromobject(c.const, ty)
            # Numba -> ctypes
            ctype_obj = toctypes(numba_obj, ty, _keep_alive)
            if byref(ty):
                ctype_obj = ctypes.pointer(ctype_obj)
            # ctypes -> pykit
            new_const = from_ctypes_value(ctype_obj)

            context[new_const] = ty
            new_constants.append(new_const)

            _keep_alive.extend([ctype_obj, c.const])

        substitute_args(op, constants, new_constants)
Example #8
0
def rewrite_constants(func, env):
    """
    Rewrite constants with user-defined types to IR constants. Also rewrite
    constants of builtins to instances of flypy classes.

        e.g. constant(None)  -> constant(NoneValue)
             constant("foo") -> constant(Bytes("foo"))
    """
    if env['flypy.state.opaque']:
        return

    context = env['flypy.typing.context']

    for op in func.ops:
        if op.opcode == 'exc_catch':
            continue
        constants = collect_constants(op)
        new_constants = []
        for c in constants:
            ty = context[c]

            # Python -> flypy (if not already)
            flypy_obj = fromobject(c.const, ty)
            # flypy -> ctypes
            ctype_obj = toctypes(flypy_obj, ty, _keep_alive)
            if byref(ty):
                ctype_obj = ctypes.pointer(ctype_obj)
            # ctypes -> pykit
            new_const = from_ctypes_value(ctype_obj)

            context[new_const] = ty
            new_constants.append(new_const)

            _keep_alive.extend([ctype_obj, c.const])

        substitute_args(op, constants, new_constants)