Esempio n. 1
0
def dump_refcount(typingctx, obj):
    """Dump the refcount of an object to stdout.

    Returns True if and only if object is reference-counted and NRT is enabled.
    """
    def codegen(context, builder, signature, args):
        [obj] = args
        [ty] = signature.args
        # A sequence of (type, meminfo)
        meminfos = []
        if context.enable_nrt:
            tmp_mis = context.nrt.get_meminfos(builder, ty, obj)
            meminfos.extend(tmp_mis)

        if meminfos:
            pyapi = context.get_python_api(builder)
            gil_state = pyapi.gil_ensure()
            pyapi.print_string("dump refct of {}".format(ty))
            for ty, mi in meminfos:
                miptr = builder.bitcast(mi, _meminfo_struct_type.as_pointer())
                refctptr = cgutils.gep_inbounds(builder, miptr, 0, 0)
                refct = builder.load(refctptr)

                pyapi.print_string(" | {} refct=".format(ty))
                # "%zu" is not portable.  just truncate refcount to 32-bit.
                # that's good enough for a debugging util.
                refct_32bit = builder.trunc(refct, ir.IntType(32))
                printed = cgutils.snprintf_stackbuffer(
                    builder,
                    30,
                    "%d".format(ty),
                    refct_32bit,
                )
                pyapi.sys_write_stdout(printed)

            pyapi.print_string(";\n")
            pyapi.gil_release(gil_state)
            return cgutils.true_bit
        else:
            return cgutils.false_bit

    sig = types.bool_(obj)
    return sig, codegen
Esempio n. 2
0
def str_arr_is_na(typingctx, str_arr_typ, ind_typ=None):
    # None default to make IntelliSense happy
    assert is_str_arr_typ(str_arr_typ)

    def codegen(context, builder, sig, args):
        in_str_arr, ind = args
        string_array = context.make_helper(builder, string_array_type,
                                           in_str_arr)

        # (null_bitmap[i / 8] & kBitmask[i % 8]) == 0;
        byte_ind = builder.lshr(ind, lir.Constant(lir.IntType(64), 3))
        bit_ind = builder.urem(ind, lir.Constant(lir.IntType(64), 8))
        byte = builder.load(
            builder.gep(string_array.null_bitmap, [byte_ind], inbounds=True))
        ll_typ_mask = lir.ArrayType(lir.IntType(8), 8)
        mask_tup = cgutils.alloca_once_value(
            builder, lir.Constant(ll_typ_mask, (1, 2, 4, 8, 16, 32, 64, 128)))
        mask = builder.load(
            builder.gep(mask_tup, [lir.Constant(lir.IntType(64), 0), bit_ind],
                        inbounds=True))
        return builder.icmp_unsigned('==', builder.and_(byte, mask),
                                     lir.Constant(lir.IntType(8), 0))

    return types.bool_(string_array_type, types.intp), codegen
Esempio n. 3
0
def hashmap_contains(typingctx, dict_type, key_type):

    ty_key, ty_val = dict_type.key_type, dict_type.value_type
    key_type_postfix, value_type_postfix = _get_types_postfixes(ty_key, ty_val)

    def codegen(context, builder, sig, args):
        dict_val, key_val = args

        key_val, lir_key_type = transform_input_arg(context, builder, key_type,
                                                    key_val)
        cdict = cgutils.create_struct_proxy(dict_type)(context,
                                                       builder,
                                                       value=dict_val)
        fnty = lir.FunctionType(lir.IntType(8),
                                [lir.IntType(8).as_pointer(), lir_key_type])
        func_name = f"hashmap_contains_{key_type_postfix}_to_{value_type_postfix}"
        fn_hashmap_contains = cgutils.get_or_insert_function(builder.module,
                                                             fnty,
                                                             name=func_name)

        res = builder.call(fn_hashmap_contains, [cdict.data_ptr, key_val])
        return context.cast(builder, res, types.uint8, types.bool_)

    return types.bool_(dict_type, key_type), codegen