示例#1
0
文件: unicode.py 项目: numba/numba
 def codegen(context, builder, sig, args):
     src, start, length = args
     in_str = cgutils.create_struct_proxy(
         types.unicode_type)(context, builder, value=src)
     view_str = cgutils.create_struct_proxy(
         types.unicode_type)(context, builder)
     view_str.meminfo = in_str.meminfo
     view_str.kind = in_str.kind
     view_str.is_ascii = in_str.is_ascii
     view_str.length = length
     # hash value -1 to indicate "need to compute hash"
     view_str.hash = context.get_constant(_Py_hash_t, -1)
     # get a pointer to start of slice data
     bw_typ = context.typing_context.resolve_value_type(_kind_to_byte_width)
     bw_sig = bw_typ.get_call_type(
         context.typing_context, (types.int32,), {})
     bw_impl = context.get_function(bw_typ, bw_sig)
     byte_width = bw_impl(builder, (in_str.kind,))
     offset = builder.mul(start, byte_width)
     view_str.data = builder.gep(in_str.data, [offset])
     # Set parent pyobject to NULL
     view_str.parent = cgutils.get_null_value(view_str.parent.type)
     # incref original string
     if context.enable_nrt:
         context.nrt.incref(builder, sig.args[0], src)
     return view_str._getvalue()
示例#2
0
文件: base.py 项目: dhavide/numba
            def imp(context, builder, sig, args):
                if attr in typ.struct:
                    instance_struct = cgutils.create_struct_proxy(typ)
                    [this, val] = args
                    inst = instance_struct(context, builder, value=this)
                    data_ptr = inst.data
                    data_struct = cgutils.create_struct_proxy(typ.get_data_type(),
                                                              kind='data')
                    data = data_struct(context, builder, ref=data_ptr)

                    # Get old value
                    attr_type = typ.struct[attr]
                    oldvalue = getattr(data, attr)

                    # Store n
                    setattr(data, attr, val)
                    context.nrt_incref(builder, attr_type, val)

                    # Delete old value
                    context.nrt_decref(builder, attr_type, oldvalue)
                elif attr in typ.jitprops:
                    setter = typ.jitprops[attr]['set']
                    setter.compile(sig)
                    cres = setter._compileinfos[sig.args]
                    out = context.call_internal(builder, cres.fndesc,
                                                cres.signature, args)
                    return imputils.impl_ret_new_ref(context, builder,
                                                     cres.signature, out)

                else:
                    msg = 'attribute {0!r} not implemented'.format(attr)
                    raise NotImplementedError(msg)
示例#3
0
def attr_impl(context, builder, typ, value, attr):
    """
    Generic getattr() for @jitclass instances.
    """
    if attr in typ.struct:
        # It's a struct field
        inst_struct = cgutils.create_struct_proxy(typ)
        inst = inst_struct(context, builder, value=value)
        data_pointer = inst.data
        data_struct = cgutils.create_struct_proxy(typ.get_data_type(),
                                                  kind='data')
        data = data_struct(context, builder, ref=data_pointer)
        return imputils.impl_ret_borrowed(context, builder,
                                          typ.struct[attr],
                                          getattr(data, attr))
    elif attr in typ.jitprops:
        # It's a jitted property
        getter = typ.jitprops[attr]['get']
        sig = templates.signature(None, typ)
        dispatcher = types.Dispatcher(getter)
        sig = dispatcher.get_call_type(context.typing_context, [typ], {})
        call = context.get_function(dispatcher, sig)
        out = call(builder, [value])
        return imputils.impl_ret_new_ref(context, builder, sig.return_type, out)

    raise NotImplementedError('attribute {0!r} not implemented'.format(attr))
示例#4
0
文件: base.py 项目: yuguen/numba
 def get_helper_class(self, typ, kind='value'):
     """
     Get a helper class for the given *typ*.
     """
     # XXX handle all types: complex, array, etc.
     # XXX should it be a method on the model instead? this would allow a default kind...
     return cgutils.create_struct_proxy(typ, kind)
示例#5
0
文件: base.py 项目: dhavide/numba
def imp_dtor(context, module, instance_type):
    llvoidptr = context.get_value_type(types.voidptr)
    llsize = context.get_value_type(types.uintp)
    dtor_ftype = llvmir.FunctionType(llvmir.VoidType(),
                                     [llvoidptr, llsize, llvoidptr])

    fname = "_Dtor.{0}".format(instance_type.name)
    dtor_fn = module.get_or_insert_function(dtor_ftype,
                                            name=fname)
    if dtor_fn.is_declaration:
        # Define
        builder = llvmir.IRBuilder(dtor_fn.append_basic_block())

        alloc_fe_type = instance_type.get_data_type()
        alloc_type = context.get_value_type(alloc_fe_type)

        data_struct = cgutils.create_struct_proxy(alloc_fe_type)

        ptr = builder.bitcast(dtor_fn.args[0], alloc_type.as_pointer())
        data = data_struct(context, builder, ref=ptr)

        context.nrt_decref(builder, alloc_fe_type, data._getvalue())

        builder.ret_void()

    return dtor_fn
示例#6
0
def make_slice(context, builder, typ, value=None):
    """
    Create a slice structure, optionally initialized from the given LLVM
    *value*.
    """
    cls = cgutils.create_struct_proxy(typ)
    return cls(context, builder, value=value)
示例#7
0
def _unbox_class_instance(typ, val, c):
    def access_member(member_offset):
        # Access member by byte offset
        offset = c.context.get_constant(types.uintp, member_offset)
        llvoidptr = ir.IntType(8).as_pointer()
        ptr = cgutils.pointer_add(c.builder, val, offset)
        casted = c.builder.bitcast(ptr, llvoidptr.as_pointer())
        return c.builder.load(casted)

    struct_cls = cgutils.create_struct_proxy(typ)
    inst = struct_cls(c.context, c.builder)

    # load from Python object
    ptr_meminfo = access_member(_box.box_meminfoptr_offset)
    ptr_dataptr = access_member(_box.box_dataptr_offset)

    # store to native structure
    inst.meminfo = c.builder.bitcast(ptr_meminfo, inst.meminfo.type)
    inst.data = c.builder.bitcast(ptr_dataptr, inst.data.type)

    ret = inst._getvalue()

    c.context.nrt.incref(c.builder, typ, ret)

    return NativeValue(ret, is_error=c.pyapi.c_api_error())
示例#8
0
def make_payload_cls(list_type):
    """
    Return the Structure representation of the given *list_type*'s payload
    (an instance of types.List).
    """
    # Note the payload is stored durably in memory, so we consider it
    # data and not value.
    return cgutils.create_struct_proxy(types.ListPayload(list_type), kind="data")
示例#9
0
文件: dictobject.py 项目: numba/numba
 def codegen(context, builder, sig, args):
     [td] = sig.args
     [d] = args
     # Incref
     context.nrt.incref(builder, td, d)
     ctor = cgutils.create_struct_proxy(td)
     dstruct = ctor(context, builder, value=d)
     # Returns the plain MemInfo
     return dstruct.meminfo
示例#10
0
文件: arrayobj.py 项目: meego/numba
def array_ctypes(context, builder, typ, value):
    arrayty = make_array(typ)
    array = arrayty(context, builder, value)
    # Cast void* data to uintp
    addr = builder.ptrtoint(array.data, context.get_value_type(types.uintp))
    # Create new ArrayCType structure
    ctinfo_type = cgutils.create_struct_proxy(types.ArrayCTypes(typ))
    ctinfo = ctinfo_type(context, builder)
    ctinfo.data = addr
    return ctinfo._getvalue()
示例#11
0
文件: unicode.py 项目: esc/numba
def box_unicode_str(typ, val, c):
    """
    Convert a native unicode structure to a unicode string
    """
    uni_str = cgutils.create_struct_proxy(typ)(c.context, c.builder, value=val)
    res = c.pyapi.string_from_kind_and_data(uni_str.kind, uni_str.data, uni_str.length)
    # hash isn't needed now, just compute it so it ends up in the unicodeobject
    # hash cache, cpython doesn't always do this, depends how a string was
    # created it's safe, just burns the cycles required to hash on @box
    c.pyapi.object_hash(res)
    c.context.nrt.decref(c.builder, typ, val)
    return res
示例#12
0
def attr_impl(context, builder, sig, args, attr):
    """
    Generic setattr() for @jitclass instances.
    """
    typ, valty = sig.args
    target, val = args

    if attr in typ.struct:
        # It's a struct member
        instance_struct = cgutils.create_struct_proxy(typ)
        inst = instance_struct(context, builder, value=target)
        data_ptr = inst.data
        data_struct = cgutils.create_struct_proxy(typ.get_data_type(),
                                                  kind='data')
        data = data_struct(context, builder, ref=data_ptr)

        # Get old value
        attr_type = typ.struct[attr]
        oldvalue = getattr(data, attr)

        # Store n
        setattr(data, attr, val)
        context.nrt_incref(builder, attr_type, val)

        # Delete old value
        context.nrt_decref(builder, attr_type, oldvalue)

    elif attr in typ.jitprops:
        # It's a jitted property
        setter = typ.jitprops[attr]['set']
        disp_type = types.Dispatcher(setter)
        sig = disp_type.get_call_type(context.typing_context,
                                      (typ, valty), {})
        call = context.get_function(disp_type, sig)
        call(builder, (target, val))

    else:
        raise NotImplementedError('attribute {0!r} not implemented'.format(attr))
示例#13
0
文件: unicode.py 项目: esc/numba
def make_string_from_constant(context, builder, typ, literal_string):
    """
    Get string data by `compile_time_get_string_data()` and return a
    unicode_type LLVM value
    """
    databytes, length, kind, hashv = \
        compile_time_get_string_data(literal_string)
    mod = builder.module
    gv = context.insert_const_bytes(mod, databytes)
    uni_str = cgutils.create_struct_proxy(typ)(context, builder)
    uni_str.data = gv
    uni_str.length = uni_str.length.type(length)
    uni_str.kind = uni_str.kind.type(kind)
    uni_str.hash = uni_str.hash.type(hashv)
    return uni_str._getvalue()
示例#14
0
        def _codegen(context, builder, sig, args):
            """Create DataFrameRollingTypeModel structure."""
            data, window, min_periods, center, win_type, on, axis, closed = args
            rolling = cgutils.create_struct_proxy(sig.return_type)(context, builder)
            rolling.data = data
            rolling.window = window
            rolling.min_periods = min_periods
            rolling.center = center
            rolling.win_type = win_type
            rolling.on = on
            rolling.axis = axis
            rolling.closed = closed

            if context.enable_nrt:
                context.nrt.incref(builder, self, rolling.data)

            return rolling._getvalue()
示例#15
0
def unbox_unicode_str(typ, obj, c):
    """
    Convert a unicode str object to a native unicode structure.
    """
    ok, data, length, kind = c.pyapi.string_as_string_size_and_kind(obj)
    uni_str = cgutils.create_struct_proxy(typ)(c.context, c.builder)
    uni_str.data = data
    uni_str.length = length
    uni_str.kind = kind
    uni_str.meminfo = c.pyapi.nrt_meminfo_new_from_pyobject(
        data,  # the borrowed data pointer
        obj,  # the owner pyobject; the call will incref it.
    )
    uni_str.parent = obj

    is_error = cgutils.is_not_null(c.builder, c.pyapi.err_occurred())
    return NativeValue(uni_str._getvalue(), is_error=is_error)
示例#16
0
def unbox_str_series(typ, val, c):
    """
    Unbox a Pandas String Series. We just redirect to StringArray implementation.
    """
    dtype = StringArrayPayloadType()
    payload = cgutils.create_struct_proxy(dtype)(c.context, c.builder)
    string_array = c.context.make_helper(c.builder, typ)

    # function signature of string_array_from_sequence
    # we use void* instead of PyObject*
    fnty = lir.FunctionType(lir.VoidType(), [
        lir.IntType(8).as_pointer(),
        lir.IntType(64).as_pointer(),
        lir.IntType(32).as_pointer().as_pointer(),
        lir.IntType(8).as_pointer().as_pointer(),
        lir.IntType(8).as_pointer().as_pointer(),
    ])
    fn = c.builder.module.get_or_insert_function(
        fnty, name="string_array_from_sequence")
    c.builder.call(fn, [
        val,
        string_array._get_ptr_by_name('num_items'),
        payload._get_ptr_by_name('offsets'),
        payload._get_ptr_by_name('data'),
        payload._get_ptr_by_name('null_bitmap'),
    ])

    # the raw data is now copied to payload
    # The native representation is a proxy to the payload, we need to
    # get a proxy and attach the payload and meminfo
    meminfo, meminfo_data_ptr = construct_string_array(c.context, c.builder)
    c.builder.store(payload._getvalue(), meminfo_data_ptr)

    string_array.meminfo = meminfo
    string_array.offsets = payload.offsets
    string_array.data = payload.data
    string_array.null_bitmap = payload.null_bitmap
    string_array.num_total_chars = c.builder.zext(
        c.builder.load(
            c.builder.gep(string_array.offsets, [string_array.num_items])),
        lir.IntType(64))

    # FIXME how to check that the returned size is > 0?
    is_error = cgutils.is_not_null(c.builder, c.pyapi.err_occurred())
    return NativeValue(string_array._getvalue(), is_error=is_error)
示例#17
0
def make_string_from_constant(context, builder, typ, literal_string):
    """
    Get string data by `compile_time_get_string_data()` and return a
    unicode_type LLVM value
    """
    databytes, length, kind, is_ascii, hashv = \
        compile_time_get_string_data(literal_string)
    mod = builder.module
    gv = context.insert_const_bytes(mod, databytes)
    uni_str = cgutils.create_struct_proxy(typ)(context, builder)
    uni_str.data = gv
    uni_str.length = uni_str.length.type(length)
    uni_str.kind = uni_str.kind.type(kind)
    uni_str.is_ascii = uni_str.is_ascii.type(is_ascii)
    # Set hash to -1 to indicate that it should be computed.
    # We cannot bake in the hash value because of hashseed randomization.
    uni_str.hash = uni_str.hash.type(-1)
    return uni_str._getvalue()
示例#18
0
    def _hpat_pandas_seriesgroupby_init_codegen(context, builder, signature,
                                                args):
        """
        It is looks like it creates SeriesGroupByModel structure

        - Fixed number of parameters. Must be 4
        - increase reference counr for the data
        """

        [data_val] = args
        series = cgutils.create_struct_proxy(signature.return_type)(context,
                                                                    builder)
        series.data = data_val

        if context.enable_nrt:
            context.nrt.incref(builder, data, series.data)

        return series._getvalue()
    def _hpat_pandas_dataframe_init_codegen(context, builder, signature, args):
        """
        It is looks like it creates DataFrameModel structure

        - Fixed number of parameters. Must be 4
        - increase reference counr for the data
        """

        [data_val] = args

        dataframe = cgutils.create_struct_proxy(signature.return_type)(context,
                                                                       builder)
        dataframe.data = data_val

        if context.enable_nrt:
            context.nrt.incref(builder, data, dataframe.data)

        return dataframe._getvalue()
    def _hpat_pandas_stringmethods_init_codegen(context, builder, signature,
                                                args):
        """
        It is looks like it creates StringMethodsModel structure

        - Fixed number of parameters. Must be 4
        - increase reference count for the data
        """

        [data_val] = args
        stringmethod = cgutils.create_struct_proxy(signature.return_type)(
            context, builder)
        stringmethod.data = data_val

        if context.enable_nrt:
            context.nrt.incref(builder, data, stringmethod.data)

        return stringmethod._getvalue()
示例#21
0
文件: unicode.py 项目: esc/numba
def unbox_unicode_str(typ, obj, c):
    """
    Convert a unicode str object to a native unicode structure.
    """
    ok, data, length, kind, hashv = c.pyapi.string_as_string_size_and_kind(obj)
    uni_str = cgutils.create_struct_proxy(typ)(c.context, c.builder)
    uni_str.data = data
    uni_str.length = length
    uni_str.kind = kind
    uni_str.hash = hashv
    uni_str.meminfo = c.pyapi.nrt_meminfo_new_from_pyobject(
        data,  # the borrowed data pointer
        obj,   # the owner pyobject; the call will incref it.
    )
    uni_str.parent = obj

    is_error = cgutils.is_not_null(c.builder, c.pyapi.err_occurred())
    return NativeValue(uni_str._getvalue(), is_error=is_error)
示例#22
0
def unbox_dataframe(typ, val, c):
    """unbox dataframe to an empty DataFrame struct
    columns will be extracted later if necessary.
    """
    n_cols = len(typ.columns)
    column_strs = [
        numba.unicode.make_string_from_constant(c.context, c.builder,
                                                string_type, a)
        for a in typ.columns
    ]
    # create dataframe struct and store values
    dataframe = cgutils.create_struct_proxy(typ)(c.context, c.builder)

    column_tup = c.context.make_tuple(c.builder,
                                      types.UniTuple(string_type, n_cols),
                                      column_strs)
    zero = c.context.get_constant(types.int8, 0)
    unboxed_tup = c.context.make_tuple(c.builder,
                                       types.UniTuple(types.int8, n_cols + 1),
                                       [zero] * (n_cols + 1))

    # TODO: support unboxing index
    if typ.index == types.none:
        dataframe.index = c.context.get_constant(types.none, None)
    if typ.index == string_array_type:
        index_obj = c.pyapi.object_getattr_string(val, "index")
        dataframe.index = unbox_str_series(string_array_type, index_obj,
                                           c).value
    if isinstance(typ.index, types.Array):
        index_obj = c.pyapi.object_getattr_string(val, "index")
        index_data = c.pyapi.object_getattr_string(index_obj, "_data")
        dataframe.index = unbox_array(typ.index, index_data, c).value

    dataframe.columns = column_tup
    dataframe.unboxed = unboxed_tup
    dataframe.parent = val

    # increase refcount of stored values
    if c.context.enable_nrt:
        # TODO: other objects?
        for var in column_strs:
            c.context.nrt.incref(c.builder, string_type, var)

    return NativeValue(dataframe._getvalue())
示例#23
0
    def codegen(context, builder, sig, args):
        str_arr, _ = args
        meminfo, meminfo_data_ptr = construct_str_arr_split_view(
            context, builder)

        in_str_arr = context.make_helper(builder, string_array_type, str_arr)

        # (str_arr_split_view_payload* out_view, int64_t n_strs,
        #  uint32_t* offsets, char* data, char sep)
        fnty = lir.FunctionType(lir.VoidType(), [
            meminfo_data_ptr.type,
            lir.IntType(64),
            lir.IntType(32).as_pointer(),
            lir.IntType(8).as_pointer(),
            lir.IntType(8)
        ])
        fn_impl = builder.module.get_or_insert_function(
            fnty, name="str_arr_split_view_impl")

        sep_val = context.get_constant(types.int8, ord(sep_typ.literal_value))
        builder.call(fn_impl, [
            meminfo_data_ptr, in_str_arr.num_items, in_str_arr.offsets,
            in_str_arr.data, sep_val
        ])

        view_payload = cgutils.create_struct_proxy(
            str_arr_split_view_payload_type)(
                context, builder, value=builder.load(meminfo_data_ptr))

        out_view = context.make_helper(builder, string_array_split_view_type)
        out_view.num_items = in_str_arr.num_items
        out_view.index_offsets = view_payload.index_offsets
        out_view.data_offsets = view_payload.data_offsets
        # TODO: incref?
        out_view.data = context.compile_internal(
            builder, lambda S: get_data_ptr(S),
            data_ctypes_type(string_array_type), [str_arr])
        # out_view.null_bitmap = view_payload.null_bitmap
        out_view.meminfo = meminfo
        ret = out_view._getvalue()
        #context.nrt.decref(builder, ty, ret)

        return impl_ret_new_ref(context, builder, string_array_split_view_type,
                                ret)
示例#24
0
文件: base.py 项目: dhavide/numba
def ctor_impl(context, builder, sig, args):
    # Allocate the instance
    inst_typ = sig.return_type
    alloc_type = context.get_data_type(inst_typ.get_data_type())
    alloc_size = context.get_abi_sizeof(alloc_type)

    meminfo = context.nrt_meminfo_alloc_dtor(
        builder,
        context.get_constant(types.uintp, alloc_size),
        imp_dtor(context, builder.module, inst_typ),
    )
    data_pointer = context.nrt_meminfo_data(builder, meminfo)
    data_pointer = builder.bitcast(data_pointer,
                                   alloc_type.as_pointer())

    # Nullify all data
    builder.store(cgutils.get_null_value(alloc_type),
                  data_pointer)

    inst_struct_typ = cgutils.create_struct_proxy(inst_typ)
    inst_struct = inst_struct_typ(context, builder)
    inst_struct.meminfo = meminfo
    inst_struct.data = data_pointer

    # Call the __init__
    # TODO: extract the following into a common util
    init_sig = (sig.return_type,) + sig.args

    init = inst_typ.jitmethods['__init__']
    init.compile(init_sig)
    cres = init._compileinfos[init_sig]
    realargs = [inst_struct._getvalue()] + list(args)
    context.call_internal(builder, cres.fndesc, types.void(*init_sig),
                          realargs)

    # Prepare reutrn value
    ret = inst_struct._getvalue()

    # Add function to link
    codegen = context.codegen()
    codegen.add_linking_library(cres.library)

    return imputils.impl_ret_new_ref(context, builder, inst_typ, ret)
示例#25
0
文件: unicode.py 项目: esc/numba
    def details(context, builder, signature, args):
        [kind_val, char_bytes_val, length_val] = args

        # fill the struct
        uni_str_ctor = cgutils.create_struct_proxy(types.unicode_type)
        uni_str = uni_str_ctor(context, builder)
        # add null padding character
        nbytes_val = builder.mul(char_bytes_val,
                                 builder.add(length_val,
                                             Constant(length_val.type, 1)))
        uni_str.meminfo = context.nrt.meminfo_alloc(builder, nbytes_val)
        uni_str.kind = kind_val
        uni_str.length = length_val
        # empty string has hash value -1 to indicate "need to compute hash"
        uni_str.hash = context.get_constant(_Py_hash_t, -1)
        uni_str.data = context.nrt.meminfo_data(builder, uni_str.meminfo)
        # Set parent to NULL
        uni_str.parent = cgutils.get_null_value(uni_str.parent.type)
        return uni_str._getvalue()
    def details(context, builder, signature, args):
        [kind_val, char_bytes_val, length_val] = args

        # fill the struct
        uni_str_ctor = cgutils.create_struct_proxy(types.unicode_type)
        uni_str = uni_str_ctor(context, builder)
        # add null padding character
        nbytes_val = builder.mul(
            char_bytes_val,
            builder.add(length_val, Constant(length_val.type, 1)))
        uni_str.meminfo = context.nrt.meminfo_alloc(builder, nbytes_val)
        uni_str.kind = kind_val
        uni_str.length = length_val
        # empty string has hash value -1 to indicate "need to compute hash"
        uni_str.hash = context.get_constant(_Py_hash_t, -1)
        uni_str.data = context.nrt.meminfo_data(builder, uni_str.meminfo)
        # Set parent to NULL
        uni_str.parent = cgutils.get_null_value(uni_str.parent.type)
        return uni_str._getvalue()
示例#27
0
def impl_ctor_datetime(context, builder, sig, args):
    typ = sig.return_type
    year, month, day = args
    #year, month, day, hour, minute, second, us, ns = args
    ts = cgutils.create_struct_proxy(typ)(context, builder)
    ts.year = year
    ts.month = month
    ts.day = day
    ts.hour = lir.Constant(lir.IntType(64), 0)
    ts.minute = lir.Constant(lir.IntType(64), 0)
    ts.second = lir.Constant(lir.IntType(64), 0)
    ts.microsecond = lir.Constant(lir.IntType(64), 0)
    ts.nanosecond = lir.Constant(lir.IntType(64), 0)
    #ts.hour = hour
    #ts.minute = minute
    #ts.second = second
    #ts.microsecond = us
    #ts.nanosecond = ns
    return ts._getvalue()
示例#28
0
def pq_read_string_lower(context, builder, sig, args):
    typ = sig.return_type
    string_array = cgutils.create_struct_proxy(typ)(context, builder)
    string_array.size = args[2]
    fnty = lir.FunctionType(lir.IntType(32), [
        lir.IntType(8).as_pointer(),
        lir.IntType(64),
        lir.IntType(8).as_pointer().as_pointer(),
        lir.IntType(8).as_pointer().as_pointer()
    ])

    fn = builder.module.get_or_insert_function(fnty, name="pq_read_string")
    res = builder.call(fn, [
        args[0], args[1],
        string_array._get_ptr_by_name('offsets'),
        string_array._get_ptr_by_name('data')
    ])

    return string_array._getvalue()
示例#29
0
    def codegen(context, builder, sig, args):
        [tmi, tdref] = sig.args
        td = tdref.instance_type
        [mi, _] = args

        ctor = cgutils.create_struct_proxy(td)
        dstruct = ctor(context, builder)

        data_pointer = context.nrt.meminfo_data(builder, mi)
        data_pointer = builder.bitcast(data_pointer, ll_list_type.as_pointer())

        dstruct.data = builder.load(data_pointer)
        dstruct.meminfo = mi

        return impl_ret_borrowed(
            context,
            builder,
            listtype,
            dstruct._getvalue(),
        )
示例#30
0
文件: dictobject.py 项目: numba/numba
    def codegen(context, builder, sig, args):
        [tmi, tdref] = sig.args
        td = tdref.instance_type
        [mi, _] = args

        ctor = cgutils.create_struct_proxy(td)
        dstruct = ctor(context, builder)

        data_pointer = context.nrt.meminfo_data(builder, mi)
        data_pointer = builder.bitcast(data_pointer, ll_dict_type.as_pointer())

        dstruct.data = builder.load(data_pointer)
        dstruct.meminfo = mi

        return impl_ret_borrowed(
            context,
            builder,
            dicttype,
            dstruct._getvalue(),
        )
示例#31
0
def ctor_impl(context, builder, sig, args):
    """
    Generic constructor (__new__) for jitclasses.
    """
    # Allocate the instance
    inst_typ = sig.return_type
    alloc_type = context.get_data_type(inst_typ.get_data_type())
    alloc_size = context.get_abi_sizeof(alloc_type)

    meminfo = context.nrt_meminfo_alloc_dtor(
        builder,
        context.get_constant(types.uintp, alloc_size),
        imp_dtor(context, builder.module, inst_typ),
    )
    data_pointer = context.nrt_meminfo_data(builder, meminfo)
    data_pointer = builder.bitcast(data_pointer,
                                   alloc_type.as_pointer())

    # Nullify all data
    builder.store(cgutils.get_null_value(alloc_type),
                  data_pointer)

    inst_struct_typ = cgutils.create_struct_proxy(inst_typ)
    inst_struct = inst_struct_typ(context, builder)
    inst_struct.meminfo = meminfo
    inst_struct.data = data_pointer

    # Call the jitted __init__
    # TODO: extract the following into a common util
    init_sig = (sig.return_type,) + sig.args

    init = inst_typ.jitmethods['__init__']
    disp_type = types.Dispatcher(init)
    call = context.get_function(disp_type, types.void(*init_sig))
    realargs = [inst_struct._getvalue()] + list(args)
    call(builder, realargs)

    # Prepare return value
    ret = inst_struct._getvalue()

    return imputils.impl_ret_new_ref(context, builder, inst_typ, ret)
示例#32
0
def ctor_impl(context, builder, sig, args):
    # Allocate the instance
    inst_typ = sig.return_type
    alloc_type = context.get_data_type(inst_typ.get_data_type())
    alloc_size = context.get_abi_sizeof(alloc_type)

    meminfo = context.nrt_meminfo_alloc_dtor(
        builder,
        context.get_constant(types.uintp, alloc_size),
        imp_dtor(context, builder.module, inst_typ),
    )
    data_pointer = context.nrt_meminfo_data(builder, meminfo)
    data_pointer = builder.bitcast(data_pointer, alloc_type.as_pointer())

    # Nullify all data
    builder.store(cgutils.get_null_value(alloc_type), data_pointer)

    inst_struct_typ = cgutils.create_struct_proxy(inst_typ)
    inst_struct = inst_struct_typ(context, builder)
    inst_struct.meminfo = meminfo
    inst_struct.data = data_pointer

    # Call the __init__
    # TODO: extract the following into a common util
    init_sig = (sig.return_type, ) + sig.args

    init = inst_typ.jitmethods['__init__']
    init.compile(init_sig)
    cres = init._compileinfos[init_sig]
    realargs = [inst_struct._getvalue()] + list(args)
    context.call_internal(builder, cres.fndesc, types.void(*init_sig),
                          realargs)

    # Prepare reutrn value
    ret = inst_struct._getvalue()

    # Add function to link
    codegen = context.codegen()
    codegen.add_linking_library(cres.library)

    return imputils.impl_ret_new_ref(context, builder, inst_typ, ret)
示例#33
0
文件: svc.py 项目: samaid/sdc
def svc_train_impl(context, builder, sig, args):
    X = context.make_array(sig.args[1])(context, builder, args[1])
    y = context.make_array(sig.args[2])(context, builder, args[2])

    zero = context.get_constant(types.intp, 0)
    one = context.get_constant(types.intp, 1)

    # num_features = builder.load(builder.gep(X.shape, [one]))
    # num_samples = builder.load(builder.gep(X.shape, [zero]))
    num_features = builder.extract_value(X.shape, 1)
    num_samples = builder.extract_value(X.shape, 0)

    # num_features, num_samples, X, y
    arg_typs = [
        lir.IntType(64),
        lir.IntType(64),
        lir.DoubleType().as_pointer(),
        lir.DoubleType().as_pointer(),
        lir.IntType(64).as_pointer()
    ]
    fnty = lir.FunctionType(lir.IntType(8).as_pointer(), arg_typs)

    fn = builder.module.get_or_insert_function(fnty, name="svc_train")

    dtype = SVCPayloadType()
    inst_struct = context.make_helper(builder, svc_type, args[0])
    data_pointer = context.nrt.meminfo_data(builder, inst_struct.meminfo)
    data_pointer = builder.bitcast(data_pointer,
                                   context.get_data_type(dtype).as_pointer())

    svc_struct = cgutils.create_struct_proxy(dtype)(context, builder,
                                                    builder.load(data_pointer))

    call_args = [
        num_features, num_samples, X.data, y.data,
        svc_struct._get_ptr_by_name('n_classes')
    ]
    model = builder.call(fn, call_args)
    svc_struct.model = model
    builder.store(svc_struct._getvalue(), data_pointer)
    return context.get_dummy_value()
示例#34
0
def box_str(typ, val, c):
    """
    """
    dtype = StringArrayPayloadType()

    inst_struct = c.context.make_helper(c.builder, typ, val)
    data_pointer = c.context.nrt.meminfo_data(c.builder, inst_struct.meminfo)
    # cgutils.printf(builder, "data [%p]\n", data_pointer)
    data_pointer = c.builder.bitcast(
        data_pointer,
        c.context.get_data_type(dtype).as_pointer())

    string_array = cgutils.create_struct_proxy(dtype)(
        c.context, c.builder, c.builder.load(data_pointer))

    # fnty = lir.FunctionType(lir.VoidType(), [lir.IntType(64)])
    # fn_print_int = c.builder.module.get_or_insert_function(fnty,
    #                                             name="print_int")
    # c.builder.call(fn_print_int, [string_array.size])

    string_list = c.pyapi.list_new(string_array.size)
    res = cgutils.alloca_once(c.builder, lir.IntType(8).as_pointer())
    c.builder.store(string_list, res)

    fnty = lir.FunctionType(
        lir.IntType(8).as_pointer(), [
            lir.IntType(8).as_pointer(),
            lir.IntType(8).as_pointer(),
            lir.IntType(64)
        ])
    fn_getitem = c.builder.module.get_or_insert_function(
        fnty, name="getitem_string_array")

    with cgutils.for_range(c.builder, string_array.size) as loop:
        c_str = c.builder.call(
            fn_getitem, [string_array.offsets, string_array.data, loop.index])
        pystr = c.pyapi.string_from_string(c_str)
        c.pyapi.list_setitem(string_list, loop.index, pystr)

    c.context.nrt.decref(c.builder, typ, val)
    return c.builder.load(res)
示例#35
0
def unbox_series(typ, val, c):
    arr_obj = c.pyapi.object_getattr_string(val, "values")
    series = cgutils.create_struct_proxy(typ)(c.context, c.builder)
    series.data = _unbox_series_data(typ.dtype, typ.data, arr_obj, c).value
    # TODO: other indices
    if typ.index == string_array_type:
        index_obj = c.pyapi.object_getattr_string(val, "index")
        series.index = unbox_str_series(string_array_type, index_obj, c).value

    if isinstance(typ.index, types.Array):
        index_obj = c.pyapi.object_getattr_string(val, "index")
        index_data = c.pyapi.object_getattr_string(index_obj, "_data")
        series.index = unbox_array(typ.index, index_data, c).value

    if typ.is_named:
        name_obj = c.pyapi.object_getattr_string(val, "name")
        series.name = numba.unicode.unbox_unicode_str(string_type, name_obj,
                                                      c).value
    # TODO: handle index and name
    c.pyapi.decref(arr_obj)
    return NativeValue(series._getvalue())
示例#36
0
def pq_read_string_lower(context, builder, sig, args):
    typ = sig.return_type
    dtype = StringArrayPayloadType()
    meminfo, data_pointer = construct_string_array(context, builder)
    string_array = cgutils.create_struct_proxy(dtype)(context, builder)
    string_array.size = args[2]
    fnty = lir.FunctionType(lir.IntType(32),
                            [lir.IntType(8).as_pointer(), lir.IntType(64),
                             lir.IntType(8).as_pointer().as_pointer(),
                             lir.IntType(8).as_pointer().as_pointer()])

    fn = builder.module.get_or_insert_function(fnty, name="pq_read_string")
    res = builder.call(fn, [args[0], args[1],
                            string_array._get_ptr_by_name('offsets'),
                            string_array._get_ptr_by_name('data')])
    builder.store(string_array._getvalue(),
                  data_pointer)
    inst_struct = context.make_helper(builder, typ)
    inst_struct.meminfo = meminfo
    ret = inst_struct._getvalue()
    return impl_ret_new_ref(context, builder, typ, ret)
示例#37
0
    def codegen(context, builder, sig, args):
        data_val, name_val = args
        # create series struct and store values
        range_index = cgutils.create_struct_proxy(
            sig.return_type)(context, builder)

        range_index.data = data_val

        if is_named:
            if isinstance(name, types.StringLiteral):
                range_index.name = numba.unicode.make_string_from_constant(
                    context, builder, types.unicode_type, name.literal_value)
            else:
                range_index.name = name_val

        if context.enable_nrt:
            context.nrt.incref(builder, sig.args[0], data_val)
            if is_named:
                context.nrt.incref(builder, sig.args[1], name_val)

        return range_index._getvalue()
示例#38
0
def _unbox_class_instance(typ, val, c):
    struct_cls = cgutils.create_struct_proxy(typ)
    inst = struct_cls(c.context, c.builder)

    int_meminfo = c.pyapi.object_getattr_string(val, "_meminfoptr")
    int_dataptr = c.pyapi.object_getattr_string(val, "_dataptr")

    ptr_meminfo = c.pyapi.long_as_voidptr(int_meminfo)
    ptr_dataptr = c.pyapi.long_as_voidptr(int_dataptr)

    c.pyapi.decref(int_meminfo)
    c.pyapi.decref(int_dataptr)

    inst.meminfo = c.builder.bitcast(ptr_meminfo, inst.meminfo.type)
    inst.data = c.builder.bitcast(ptr_dataptr, inst.data.type)

    ret = inst._getvalue()

    c.context.nrt_incref(c.builder, typ, ret)

    return NativeValue(ret, is_error=c.pyapi.c_api_error())
示例#39
0
def box_pandas_timestamp(typ, val, c):
    pdts = cgutils.create_struct_proxy(typ)(c.context, c.builder, value=val)
    year_obj = c.pyapi.long_from_longlong(pdts.year)
    month_obj = c.pyapi.long_from_longlong(pdts.month)
    day_obj = c.pyapi.long_from_longlong(pdts.day)
    hour_obj = c.pyapi.long_from_longlong(pdts.hour)
    minute_obj = c.pyapi.long_from_longlong(pdts.minute)
    second_obj = c.pyapi.long_from_longlong(pdts.second)
    us_obj = c.pyapi.long_from_longlong(pdts.microsecond)
    ns_obj = c.pyapi.long_from_longlong(pdts.nanosecond)

    pdts_obj = c.pyapi.unserialize(c.pyapi.serialize_object(pd.Timestamp))
    res = c.pyapi.call_function_objargs(pdts_obj, (year_obj, month_obj, day_obj, hour_obj, minute_obj, second_obj, us_obj, ns_obj))
    c.pyapi.decref(year_obj)
    c.pyapi.decref(month_obj)
    c.pyapi.decref(day_obj)
    c.pyapi.decref(hour_obj)
    c.pyapi.decref(minute_obj)
    c.pyapi.decref(second_obj)
    c.pyapi.decref(us_obj)
    c.pyapi.decref(ns_obj)
    return res
示例#40
0
def lower_string_arr_getitem(context, builder, sig, args):
    typ = sig.args[0]
    dtype = StringArrayPayloadType()

    inst_struct = context.make_helper(builder, typ, args[0])
    data_pointer = context.nrt.meminfo_data(builder, inst_struct.meminfo)
    # cgutils.printf(builder, "data [%p]\n", data_pointer)
    data_pointer = builder.bitcast(data_pointer,
                                   context.get_data_type(dtype).as_pointer())

    string_array = cgutils.create_struct_proxy(dtype)(
        context, builder, builder.load(data_pointer))
    fnty = lir.FunctionType(
        lir.IntType(8).as_pointer(), [
            lir.IntType(8).as_pointer(),
            lir.IntType(8).as_pointer(),
            lir.IntType(64)
        ])
    fn_getitem = builder.module.get_or_insert_function(
        fnty, name="getitem_string_array_std")
    return builder.call(fn_getitem,
                        [string_array.offsets, string_array.data, args[1]])
    def hpat_pandas_dataframe_box(typ, val, c):
        """
        This method is to copy data from JITted region data structure
        to new Python object data structure.
        Python object data structure has creating in this procedure.
        """

        dataframe = cgutils.create_struct_proxy(typ)(c.context,
                                                     c.builder,
                                                     value=val)

        ir_ptr_data = c.box(typ.data, dataframe.data)

        dataframe_ctor_args = c.pyapi.tuple_pack([
            ir_ptr_data,
        ])
        # dataframe_ctor_kwargs = c.pyapi.dict_pack([("data", ir_ptr_data), ])
        """
        It is better to use kwargs but it fails into SIGSEGV
        """

        dataframe_ctor_fn = c.pyapi.unserialize(
            c.pyapi.serialize_object(pandas.DataFrame))
        """
        Create a pandas.DataFrame ctor() function pointer
        """

        df_obj = c.pyapi.call(
            dataframe_ctor_fn,
            dataframe_ctor_args)  # kws=dataframe_ctor_kwargs)
        """
        Call pandas.DataFrame function pointer with parameters
        """

        c.pyapi.decref(ir_ptr_data)
        c.pyapi.decref(dataframe_ctor_args)
        c.pyapi.decref(dataframe_ctor_fn)

        return df_obj
示例#42
0
    def codegen(context, builder, signature, args):
        [_, ptr] = args
        ctor = cgutils.create_struct_proxy(list_ty)
        lstruct = ctor(context, builder)
        lstruct.data = ptr

        alloc_size = context.get_abi_sizeof(
            context.get_value_type(types.voidptr), )
        dtor = _imp_dtor(context, builder.module)
        meminfo = context.nrt.meminfo_alloc_dtor(
            builder,
            context.get_constant(types.uintp, alloc_size),
            dtor,
        )

        data_pointer = context.nrt.meminfo_data(builder, meminfo)
        data_pointer = builder.bitcast(data_pointer, ll_list_type.as_pointer())
        builder.store(ptr, data_pointer)

        lstruct.meminfo = meminfo

        return lstruct._getvalue()
示例#43
0
def unbox_COO(typ: COOType, obj: COO, c) -> NativeValue:
    ret_ptr = cgutils.alloca_once(c.builder, c.context.get_value_type(typ))
    is_error_ptr = cgutils.alloca_once_value(c.builder, cgutils.false_bit)
    fail_obj = c.context.get_constant_null(typ)

    with local_return(c.builder) as ret:
        fail_blk = c.builder.append_basic_block("fail")
        with c.builder.goto_block(fail_blk):
            c.builder.store(cgutils.true_bit, is_error_ptr)
            c.builder.store(fail_obj, ret_ptr)
            ret()

        data = _unbox_native_field(typ.data_type, obj, "data", c)
        with cgutils.if_unlikely(c.builder, data.is_error):
            c.builder.branch(fail_blk)

        coords = _unbox_native_field(typ.coords_type, obj, "coords", c)
        with cgutils.if_unlikely(c.builder, coords.is_error):
            c.builder.branch(fail_blk)

        shape = _unbox_native_field(typ.shape_type, obj, "shape", c)
        with cgutils.if_unlikely(c.builder, shape.is_error):
            c.builder.branch(fail_blk)

        fill_value = _unbox_native_field(typ.fill_value_type, obj,
                                         "fill_value", c)
        with cgutils.if_unlikely(c.builder, fill_value.is_error):
            c.builder.branch(fail_blk)

        coo = cgutils.create_struct_proxy(typ)(c.context, c.builder)
        coo.coords = coords.value
        coo.data = data.value
        coo.shape = shape.value
        coo.fill_value = fill_value.value
        c.builder.store(cgutils.false_bit, is_error_ptr)
        c.builder.store(coo._getvalue(), ret_ptr)

    return NativeValue(c.builder.load(ret_ptr),
                       is_error=c.builder.load(is_error_ptr))
示例#44
0
    def codegen(context, builder, signature, args):
        data_val, index_val, name_val = args
        # create series struct and store values
        series = cgutils.create_struct_proxy(signature.return_type)(context,
                                                                    builder)
        series.data = data_val
        series.index = index_val
        if is_named:
            if isinstance(name, types.StringLiteral):
                series.name = numba.unicode.make_string_from_constant(
                    context, builder, string_type, name.literal_value)
            else:
                series.name = name_val

        # increase refcount of stored values
        if context.enable_nrt:
            context.nrt.incref(builder, signature.args[0], data_val)
            context.nrt.incref(builder, signature.args[1], index_val)
            if is_named:
                context.nrt.incref(builder, signature.args[2], name_val)

        return series._getvalue()
示例#45
0
def bytes_to_charseq(context, builder, fromty, toty, val):
    barr = cgutils.create_struct_proxy(fromty)(context, builder, value=val)
    src = builder.bitcast(barr.data, ir.IntType(8).as_pointer())
    src_length = barr.nitems

    lty = context.get_value_type(toty)
    dstint_t = ir.IntType(8)
    dst_ptr = cgutils.alloca_once(builder, lty)
    dst = builder.bitcast(dst_ptr, dstint_t.as_pointer())

    dst_length = ir.Constant(src_length.type, toty.count)
    is_shorter_value = builder.icmp_unsigned('<', src_length, dst_length)
    count = builder.select(is_shorter_value, src_length, dst_length)
    with builder.if_then(is_shorter_value):
        cgutils.memset(builder, dst, ir.Constant(src_length.type, toty.count),
                       0)
    with cgutils.for_range(builder, count) as loop:
        in_ptr = builder.gep(src, [loop.index])
        in_val = builder.zext(builder.load(in_ptr), dstint_t)
        builder.store(in_val, builder.gep(dst, [loop.index]))

    return builder.load(dst_ptr)
示例#46
0
def ctor_impl(context, builder, sig, args):
    """
    Generic constructor (__new__) for jitclasses.
    """
    # Allocate the instance
    inst_typ = sig.return_type
    alloc_type = context.get_data_type(inst_typ.get_data_type())
    alloc_size = context.get_abi_sizeof(alloc_type)

    meminfo = context.nrt_meminfo_alloc_dtor(
        builder,
        context.get_constant(types.uintp, alloc_size),
        imp_dtor(context, builder.module, inst_typ),
    )
    data_pointer = context.nrt_meminfo_data(builder, meminfo)
    data_pointer = builder.bitcast(data_pointer, alloc_type.as_pointer())

    # Nullify all data
    builder.store(cgutils.get_null_value(alloc_type), data_pointer)

    inst_struct_typ = cgutils.create_struct_proxy(inst_typ)
    inst_struct = inst_struct_typ(context, builder)
    inst_struct.meminfo = meminfo
    inst_struct.data = data_pointer

    # Call the jitted __init__
    # TODO: extract the following into a common util
    init_sig = (sig.return_type, ) + sig.args

    init = inst_typ.jitmethods['__init__']
    disp_type = types.Dispatcher(init)
    call = context.get_function(disp_type, types.void(*init_sig))
    realargs = [inst_struct._getvalue()] + list(args)
    call(builder, realargs)

    # Prepare return value
    ret = inst_struct._getvalue()

    return imputils.impl_ret_new_ref(context, builder, inst_typ, ret)
示例#47
0
def pq_read_string_parallel_lower(context, builder, sig, args):
    typ = sig.return_type
    dtype = StringArrayPayloadType()
    meminfo, meminfo_data_ptr = construct_string_array(context, builder)
    str_arr_payload = cgutils.create_struct_proxy(dtype)(context, builder)
    string_array = context.make_helper(builder, typ)
    string_array.num_items = args[3]

    fnty = lir.FunctionType(lir.IntType(32), [
        lir.IntType(8).as_pointer(),
        lir.IntType(64),
        lir.IntType(32).as_pointer().as_pointer(),
        lir.IntType(8).as_pointer().as_pointer(),
        lir.IntType(8).as_pointer().as_pointer(),
        lir.IntType(64),
        lir.IntType(64)
    ])

    fn = builder.module.get_or_insert_function(fnty,
                                               name="pq_read_string_parallel")
    res = builder.call(fn, [
        args[0], args[1],
        str_arr_payload._get_ptr_by_name('offsets'),
        str_arr_payload._get_ptr_by_name('data'),
        str_arr_payload._get_ptr_by_name('null_bitmap'), args[2], args[3]
    ])

    builder.store(str_arr_payload._getvalue(), meminfo_data_ptr)

    string_array.meminfo = meminfo
    string_array.offsets = str_arr_payload.offsets
    string_array.data = str_arr_payload.data
    string_array.null_bitmap = str_arr_payload.null_bitmap
    string_array.num_total_chars = builder.zext(
        builder.load(
            builder.gep(string_array.offsets, [string_array.num_items])),
        lir.IntType(64))
    ret = string_array._getvalue()
    return impl_ret_new_ref(context, builder, typ, ret)
示例#48
0
文件: svc.py 项目: samaid/sdc
def svc_predict_impl(context, builder, sig, args):

    dtype = SVCPayloadType()
    inst_struct = context.make_helper(builder, svc_type, args[0])
    data_pointer = context.nrt.meminfo_data(builder, inst_struct.meminfo)
    data_pointer = builder.bitcast(data_pointer,
                                   context.get_data_type(dtype).as_pointer())
    svc_struct = cgutils.create_struct_proxy(dtype)(context, builder,
                                                    builder.load(data_pointer))

    p = context.make_array(sig.args[1])(context, builder, args[1])

    num_features = builder.extract_value(p.shape, 1)
    num_samples = builder.extract_value(p.shape, 0)

    ret_arr = _empty_nd_impl(context, builder, sig.return_type, [num_samples])

    call_args = [
        svc_struct.model, num_features, num_samples, p.data, ret_arr.data,
        svc_struct.n_classes
    ]

    # model, num_features, num_samples, p, ret
    arg_typs = [
        lir.IntType(8).as_pointer(),
        lir.IntType(64),
        lir.IntType(64),
        lir.DoubleType().as_pointer(),
        lir.DoubleType().as_pointer(),
        lir.IntType(64)
    ]
    fnty = lir.FunctionType(lir.VoidType(), arg_typs)

    fn = builder.module.get_or_insert_function(fnty, name="svc_predict")
    builder.call(fn, call_args)

    return impl_ret_new_ref(context, builder, sig.return_type,
                            ret_arr._getvalue())
示例#49
0
文件: dictobject.py 项目: numba/numba
    def codegen(context, builder, signature, args):
        [_, _, ptr] = args
        ctor = cgutils.create_struct_proxy(dict_ty)
        dstruct = ctor(context, builder)
        dstruct.data = ptr

        alloc_size = context.get_abi_sizeof(
            context.get_value_type(types.voidptr),
        )
        dtor = _imp_dtor(context, builder.module)
        meminfo = context.nrt.meminfo_alloc_dtor(
            builder,
            context.get_constant(types.uintp, alloc_size),
            dtor,
        )

        data_pointer = context.nrt.meminfo_data(builder, meminfo)
        data_pointer = builder.bitcast(data_pointer, ll_dict_type.as_pointer())
        builder.store(ptr, data_pointer)

        dstruct.meminfo = meminfo

        return dstruct._getvalue()
示例#50
0
def make_enumerate_cls(enum_type):
    """
    Return the Structure representation of the given *enum_type* (an
    instance of types.EnumerateType).
    """
    return cgutils.create_struct_proxy(enum_type)
示例#51
0
def make_array(array_type):
    """
    Return the Structure representation of the given *array_type*
    (an instance of types.Array).
    """
    return cgutils.create_struct_proxy(array_type)
示例#52
0
文件: rangeobj.py 项目: hargup/numba
def make_range_impl(range_state_type, range_iter_type, int_type):
    RangeState = cgutils.create_struct_proxy(range_state_type)

    @builtin
    @implement(types.range_type, int_type)
    def range1_impl(context, builder, sig, args):
        """
        range(stop: int) -> range object
        """
        [stop] = args
        state = RangeState(context, builder)
        state.start = context.get_constant(int_type, 0)
        state.stop = stop
        state.step = context.get_constant(int_type, 1)
        return state._getvalue()

    @builtin
    @implement(types.range_type, int_type, int_type)
    def range2_impl(context, builder, sig, args):
        """
        range(start: int, stop: int) -> range object
        """
        start, stop = args
        state = RangeState(context, builder)
        state.start = start
        state.stop = stop
        state.step = context.get_constant(int_type, 1)
        return state._getvalue()

    @builtin
    @implement(types.range_type, int_type, int_type, int_type)
    def range3_impl(context, builder, sig, args):
        """
        range(start: int, stop: int, step: int) -> range object
        """
        [start, stop, step] = args
        state = RangeState(context, builder)
        state.start = start
        state.stop = stop
        state.step = step
        return state._getvalue()

    @builtin
    @implement('getiter', range_state_type)
    def getiter_range32_impl(context, builder, sig, args):
        """
        range.__iter__
        """
        (value,) = args
        state = RangeState(context, builder, value)
        return RangeIter.from_range_state(context, builder, state)._getvalue()

    @iterator_impl(range_state_type, range_iter_type)
    class RangeIter(make_range_iterator(range_iter_type)):

        @classmethod
        def from_range_state(cls, context, builder, state):
            """
            Create a RangeIter initialized from the given RangeState *state*.
            """
            self = cls(context, builder)
            start = state.start
            stop = state.stop
            step = state.step

            startptr = cgutils.alloca_once(builder, start.type)
            builder.store(start, startptr)

            countptr = cgutils.alloca_once(builder, start.type)

            self.iter = startptr
            self.stop = stop
            self.step = step
            self.count = countptr

            diff = builder.sub(stop, start)
            zero = context.get_constant(int_type, 0)
            one = context.get_constant(int_type, 1)
            pos_diff = builder.icmp(lc.ICMP_SGT, diff, zero)
            pos_step = builder.icmp(lc.ICMP_SGT, step, zero)
            sign_differs = builder.xor(pos_diff, pos_step)
            zero_step = builder.icmp(lc.ICMP_EQ, step, zero)

            with cgutils.if_unlikely(builder, zero_step):
                # step shouldn't be zero
                context.call_conv.return_user_exc(builder, ValueError,
                                                  ("range() arg 3 must not be zero",))

            with builder.if_else(sign_differs) as (then, orelse):
                with then:
                    builder.store(zero, self.count)

                with orelse:
                    rem = builder.srem(diff, step)
                    rem = builder.select(pos_diff, rem, builder.neg(rem))
                    uneven = builder.icmp(lc.ICMP_SGT, rem, zero)
                    newcount = builder.add(builder.sdiv(diff, step),
                                           builder.select(uneven, one, zero))
                    builder.store(newcount, self.count)

            return self

        def iternext(self, context, builder, result):
            zero = context.get_constant(int_type, 0)
            countptr = self.count
            count = builder.load(countptr)
            is_valid = builder.icmp(lc.ICMP_SGT, count, zero)
            result.set_valid(is_valid)

            with builder.if_then(is_valid):
                value = builder.load(self.iter)
                result.yield_(value)
                one = context.get_constant(int_type, 1)

                builder.store(builder.sub(count, one), countptr)
                builder.store(builder.add(value, self.step), self.iter)
示例#53
0
文件: rangeobj.py 项目: hargup/numba
def make_range_iterator(typ):
    """
    Return the Structure representation of the given *typ* (an
    instance of types.RangeIteratorType).
    """
    return cgutils.create_struct_proxy(typ)
示例#54
0
文件: listobj.py 项目: dhavide/numba
def make_listiter_cls(iterator_type):
    """
    Return the Structure representation of the given *iterator_type* (an
    instance of types.ListIter).
    """
    return cgutils.create_struct_proxy(iterator_type)
示例#55
0
文件: listobj.py 项目: dhavide/numba
def make_list_cls(list_type):
    """
    Return the Structure representation of the given *list_type*
    (an instance of types.List).
    """
    return cgutils.create_struct_proxy(list_type)
示例#56
0
def array_as_array(context, builder, sig, args):
    [argtype], [arg] = sig.args, args
    val = cgutils.create_struct_proxy(argtype)(context, builder, ref=arg)
    return val._get_ptr_by_name('data')
示例#57
0
def make_zip_cls(zip_type):
    """
    Return the Structure representation of the given *zip_type* (an
    instance of types.ZipType).
    """
    return cgutils.create_struct_proxy(zip_type)
示例#58
0
def array_wrap_array(context, builder, sig, args):
    dest = cgutils.create_struct_proxy(sig.return_type)(context, builder)
    dest.data = args[1]
    return impl_ret_borrowed(context, builder, sig.return_type, dest._getvalue())
示例#59
0
文件: optional.py 项目: dhavide/numba
def make_optional(valtype):
    """
    Return the Structure representation of a optional value
    """
    return cgutils.create_struct_proxy(types.Optional(valtype))