Ejemplo n.º 1
0
    def varsize_malloc_helper(self, hop, flags, meth, extraargs):
        def intconst(c): return rmodel.inputconst(lltype.Signed, c)
        op = hop.spaceop
        TYPE = op.result.concretetype.TO
        assert TYPE._is_varsize()
        if isinstance(TYPE, lltype.Struct):
            ARRAY = TYPE._flds[TYPE._arrayfld]
        else:
            ARRAY = TYPE
        assert isinstance(ARRAY, lltype.Array)
        c_const_size = intconst(llmemory.sizeof(TYPE, 0))
        c_item_size = intconst(llmemory.sizeof(ARRAY.OF))

        if ARRAY._hints.get("nolength", False):
            c_offset_to_length = None
        else:
            if isinstance(TYPE, lltype.Struct):
                offset_to_length = llmemory.FieldOffset(TYPE, TYPE._arrayfld) + \
                                   llmemory.ArrayLengthOffset(ARRAY)
            else:
                offset_to_length = llmemory.ArrayLengthOffset(ARRAY)
            c_offset_to_length = intconst(offset_to_length)

        args = [hop] + extraargs + [flags, TYPE,
                op.args[-1], c_const_size, c_item_size, c_offset_to_length]
        v_raw = meth(*args)

        hop.cast_result(v_raw)
Ejemplo n.º 2
0
def offsetofs(TYPE, *fldnames):
    import operator
    offsets = []
    for name in fldnames:
        assert name in TYPE._flds
        offset = llmemory.FieldOffset(TYPE, name)
        offsets.append(offset)
        TYPE = getattr(TYPE, name)
    return reduce(operator.add, offsets)
Ejemplo n.º 3
0
def test_fakeaddress2():
    S1 = lltype.GcStruct("S1", ("x", lltype.Signed), ("y", lltype.Signed))
    PtrS1 = lltype.Ptr(S1)
    S2 = lltype.GcStruct("S2", ("s", S1))

    s2 = lltype.malloc(S2)
    s2.s.x = 123
    s2.s.y = 456

    addr_s2 = llmemory.cast_ptr_to_adr(s2)
    addr_s1 = addr_s2 + llmemory.FieldOffset(S2, 's')

    def f():
        s1 = llmemory.cast_adr_to_ptr(addr_s1, PtrS1)
        return s1.x + s1.y

    fn = compile_function(f, [])
    assert fn() == 579
Ejemplo n.º 4
0
def test_null_padding():
    from pypy.rpython.lltypesystem import llmemory
    from pypy.rpython.lltypesystem import rstr
    chars_offset = llmemory.FieldOffset(rstr.STR, 'chars') + \
                   llmemory.ArrayItemsOffset(rstr.STR.chars)
    # sadly, there's no way of forcing this to fail if the strings
    # are allocated in a region of memory such that they just
    # happen to get a NUL byte anyway :/ (a debug build will
    # always fail though)
    def trailing_byte(s):
        adr_s = llmemory.cast_ptr_to_adr(s)
        return (adr_s + chars_offset).char[len(s)]
    def f(x):
        r = 0
        for i in range(x):
            r += ord(trailing_byte(' '*(100-x*x)))
        return r
    fn = compile_function(f, [int])
    res = fn(10)
    assert res == 0
Ejemplo n.º 5
0
    def _gct_resize_buffer_realloc(self, hop, v_newsize, grow=True):
        def intconst(c):
            return rmodel.inputconst(lltype.Signed, c)

        op = hop.spaceop
        flags = {'flavor': 'gc', 'varsize': True}
        TYPE = op.args[0].concretetype.TO
        ARRAY = TYPE._flds[TYPE._arrayfld]
        offset_to_length = llmemory.FieldOffset(TYPE, TYPE._arrayfld) + \
                           llmemory.ArrayLengthOffset(ARRAY)
        c_const_size = intconst(llmemory.sizeof(TYPE, 0))
        c_item_size = intconst(llmemory.sizeof(ARRAY.OF))

        c_lengthofs = intconst(offset_to_length)
        v_ptr = op.args[0]
        v_ptr = gen_cast(hop.llops, llmemory.GCREF, v_ptr)
        c_grow = rmodel.inputconst(lltype.Bool, grow)
        v_raw = self.perform_realloc(hop, v_ptr, v_newsize, c_const_size,
                                     c_item_size, c_lengthofs, c_grow)
        hop.cast_result(v_raw)