def define_interior_ptrs(cls): from rpython.rtyper.lltypesystem.lltype import Struct, GcStruct, GcArray from rpython.rtyper.lltypesystem.lltype import Array, Signed, malloc S1 = Struct("S1", ('x', Signed)) T1 = GcStruct("T1", ('s', S1)) def f1(): t = malloc(T1) t.s.x = 1 return t.s.x S2 = Struct("S2", ('x', Signed)) T2 = GcArray(S2) def f2(): t = malloc(T2, 1) t[0].x = 1 return t[0].x S3 = Struct("S3", ('x', Signed)) T3 = GcStruct("T3", ('items', Array(S3))) def f3(): t = malloc(T3, 1) t.items[0].x = 1 return t.items[0].x S4 = Struct("S4", ('x', Signed)) T4 = Struct("T4", ('s', S4)) U4 = GcArray(T4) def f4(): u = malloc(U4, 1) u[0].s.x = 1 return u[0].s.x S5 = Struct("S5", ('x', Signed)) T5 = GcStruct("T5", ('items', Array(S5))) def f5(): t = malloc(T5, 1) return len(t.items) T6 = GcStruct("T6", ('s', Array(Signed))) def f6(): t = malloc(T6, 1) t.s[0] = 1 return t.s[0] def func(): return (f1() * 100000 + f2() * 10000 + f3() * 1000 + f4() * 100 + f5() * 10 + f6()) assert func() == 111111 return func
def conversion_table(r_from, r_to): if r_to in r_from._conversion_tables: return r_from._conversion_tables[r_to] else: t = malloc(Array(Char, hints={ 'nolength': True, 'immutable': True, 'static_immutable': True }), len(r_from.descriptions), immortal=True) l = [] for i, d in enumerate(r_from.descriptions): if d in r_to.descriptions: j = r_to.descriptions.index(d) l.append(j) t[i] = chr(j) else: l.append(None) if l == range(len(r_from.descriptions)): r = None else: r = inputconst(typeOf(t), t) r_from._conversion_tables[r_to] = r return r
def _setup_repr(self): if self.s_pbc.subset_of: assert self.s_pbc.can_be_None == self.s_pbc.subset_of.can_be_None r = self.rtyper.getrepr(self.s_pbc.subset_of) if r is not self: r.setup() self.descriptions = r.descriptions self.c_pointer_table = r.c_pointer_table return self.descriptions = list(self.s_pbc.descriptions) if self.s_pbc.can_be_None: self.descriptions.insert(0, None) POINTER_TABLE = Array(self.pointer_repr.lowleveltype, hints={ 'nolength': True, 'immutable': True, 'static_immutable': True }) pointer_table = malloc(POINTER_TABLE, len(self.descriptions), immortal=True) for i, desc in enumerate(self.descriptions): if desc is not None: pointer_table[i] = self.pointer_repr.convert_desc(desc) else: pointer_table[i] = self.pointer_repr.convert_const(None) self.c_pointer_table = inputconst(Ptr(POINTER_TABLE), pointer_table)
def test_scoped_allocator(): from rpython.rtyper.lltypesystem.lltype import scoped_alloc, Array, Signed T = Array(Signed) def f(): x = 0 with scoped_alloc(T, 1) as array: array[0] = -42 x = array[0] assert x == -42 res = interpret(f, [])
keepalive_until_here(src) keepalive_until_here(dst) return lst TEMP = GcArray(Ptr(STR)) TEMP_UNICODE = GcArray(Ptr(UNICODE)) # ____________________________________________________________ STR.become( GcStruct( 'rpy_string', ('hash', Signed), ('chars', Array(Char, hints={ 'immutable': True, 'extra_item_after_alloc': 1 })), adtmeths={ 'malloc': staticAdtMethod(mallocstr), 'empty': staticAdtMethod(emptystrfun), 'copy_contents': staticAdtMethod(copy_string_contents), 'copy_contents_from_str': staticAdtMethod(copy_string_contents), 'gethash': LLHelpers.ll_strhash, 'length': LLHelpers.ll_length, 'find': LLHelpers.ll_find, 'rfind': LLHelpers.ll_rfind }, hints={'remove_hash': True})) UNICODE.become( GcStruct('rpy_unicode', ('hash', Signed), ('chars', Array(UniChar, hints={'immutable': True})),
adst = llmemory.cast_ptr_to_adr(dst) + llmemory.itemoffsetof(DST, 0) llmemory.raw_memcopy(asrc, adst, llmemory.sizeof(DST.OF) * length) # end of "no GC" section keepalive_until_here(src) keepalive_until_here(dst) return lst TEMP = GcArray(Ptr(STR)) TEMP_UNICODE = GcArray(Ptr(UNICODE)) # ____________________________________________________________ STR.become( GcStruct('rpy_string', ('hash', Signed), ('chars', Array(Char, hints={'immutable': True})), adtmeths={ 'malloc': staticAdtMethod(mallocstr), 'empty': staticAdtMethod(emptystrfun), 'copy_contents': staticAdtMethod(copy_string_contents), 'copy_contents_from_str': staticAdtMethod(copy_string_contents), 'gethash': LLHelpers.ll_strhash, 'length': LLHelpers.ll_length, 'find': LLHelpers.ll_find, 'rfind': LLHelpers.ll_rfind })) UNICODE.become( GcStruct('rpy_unicode', ('hash', Signed), ('chars', Array(UniChar, hints={'immutable': True})), adtmeths={
total_len = sign + len + int(val == 0) result = mallocstr(total_len) if sign: result.chars[0] = '-' elif val == 0: result.chars[0] = '0' j = 0 while j < len: result.chars[total_len - j - 1] = chr(val % 10 + ord('0')) val //= 10 j += 1 return result hex_chars = malloc(Array(Char), 16, immortal=True) for i in range(16): hex_chars[i] = "%x" % i @jit.elidable def ll_int2hex(i, addPrefix): from rpython.rtyper.lltypesystem.rstr import mallocstr temp = malloc(CHAR_ARRAY, 20) len = 0 sign = 0 if i < 0: sign = 1 i = ll_unsigned(-i) else: