def ll_int2oct(i, addPrefix): from rpython.rtyper.lltypesystem.rstr import mallocstr if i == 0: result = mallocstr(1) result.hash = 0 result.chars[0] = '0' return result temp = malloc(CHAR_ARRAY, 25) len = 0 sign = 0 if i < 0: sign = 1 i = ll_unsigned(-i) else: i = ll_unsigned(i) while i: temp[len] = hex_chars[i & 0x7] i >>= 3 len += 1 len += sign if addPrefix: len += 1 result = mallocstr(len) result.hash = 0 j = 0 if sign: result.chars[0] = '-' j = 1 if addPrefix: result.chars[j] = '0' j += 1 while j < len: result.chars[j] = temp[len - j - 1] j += 1 return result
def f(i, j): for param, _ in unroll_parameters: defl = PARAMETERS[param] set_param(jitdriver, param, defl) set_param(jitdriver, "threshold", 3) set_param(jitdriver, "trace_eagerness", 2) total = 0 frame = Frame(i) j = float(j) prev_s = rstr.mallocstr(16) while frame.i > 3: jitdriver.can_enter_jit(frame=frame, total=total, j=j, prev_s=prev_s) jitdriver.jit_merge_point(frame=frame, total=total, j=j, prev_s=prev_s) _get_virtualizable_token(frame) total += frame.i if frame.i >= 20: frame.i -= 2 frame.i -= 1 j *= -0.712 if j + (-j): raise ValueError j += frame.i k = myabs1(myabs2(j)) if k - abs(j): raise ValueError if k - abs(-j): raise ValueError s = rstr.mallocstr(16) rgc.ll_write_final_null_char(s) rgc.ll_write_final_null_char(prev_s) if (frame.i & 3) == 0: prev_s = s return chr(total % 253)
def ll_int2oct(i, addPrefix): from rpython.rtyper.lltypesystem.rstr import mallocstr if i == 0: result = mallocstr(1) result.hash = 0 result.chars[0] = '0' return result temp = malloc(CHAR_ARRAY, 25) len = 0 sign = 0 if i < 0: sign = 1 i = ll_unsigned(-i) else: i = ll_unsigned(i) while i: temp[len] = hex_chars[i & 0x7] i >>= 3 len += 1 len += sign if addPrefix: len += 1 result = mallocstr(len) result.hash = 0 j = 0 if sign: result.chars[0] = '-' j = 1 if addPrefix: result.chars[j] = '0' j += 1 while j < len: result.chars[j] = temp[len-j-1] j += 1 return result
def test_hash_equal_whatever_lltype(): s1 = rstr.mallocstr(2) s2 = rstr.mallocstr(2) s1.chars[0] = 'x'; s1.chars[1] = 'y' s2.chars[0] = 'x'; s2.chars[1] = 'y' def fn(x): assert hash_whatever(lltype.typeOf(x), x) == 42 assert (hash_whatever(lltype.typeOf(s1), s1) == hash_whatever(lltype.typeOf(s2), s2)) assert equal_whatever(lltype.typeOf(s1), s1, s2) fn(42) interpret(fn, [42], type_system='lltype')
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: i = ll_unsigned(i) if i == 0: len = 1 temp[0] = '0' else: while i: temp[len] = hex_chars[i & 0xf] i >>= 4 len += 1 len += sign if addPrefix: len += 2 result = mallocstr(len) result.hash = 0 j = 0 if sign: result.chars[0] = '-' j = 1 if addPrefix: result.chars[j] = '0' result.chars[j + 1] = 'x' j += 2 while j < len: result.chars[j] = temp[len - j - 1] j += 1 return result
def ll_int2dec(val): from rpython.rtyper.lltypesystem.rstr import mallocstr sign = int(val < 0) if sign: val = ll_unsigned(-val) else: val = ll_unsigned(val) len = 0 i = val while i: len += 1 i //= 10 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
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: i = ll_unsigned(i) if i == 0: len = 1 temp[0] = '0' else: while i: temp[len] = hex_chars[i & 0xf] i >>= 4 len += 1 len += sign if addPrefix: len += 2 result = mallocstr(len) result.hash = 0 j = 0 if sign: result.chars[0] = '-' j = 1 if addPrefix: result.chars[j] = '0' result.chars[j+1] = 'x' j += 2 while j < len: result.chars[j] = temp[len-j-1] j += 1 return result
def to_rstr(s): from rpython.rtyper.lltypesystem.rstr import STR, mallocstr if s is None: return lltype.nullptr(STR) p = mallocstr(len(s)) for i in range(len(s)): p.chars[i] = s[i] return p
def string_to_ll(s): from rpython.rtyper.lltypesystem.rstr import STR, mallocstr if s is None: return lltype.nullptr(STR) p = mallocstr(len(s)) for i in range(len(s)): p.chars[i] = s[i] return p
def test_strlen(self): s = rstr.mallocstr(300) ops = """ [p0] i1 = strlen(p0) finish(i1) """ self.interpret(ops, [s]) assert self.getint(0) == 300
def test_strsetitem(self): ops = ''' [p0, i] strsetitem(p0, 1, i) finish() ''' llstr = rstr.mallocstr(10) self.interpret(ops, [llstr, ord('a')]) assert llstr.chars[1] == 'a'
def test_strsetitem(self): ops = """ [p0, i] strsetitem(p0, 1, i) finish() """ llstr = rstr.mallocstr(10) self.interpret(ops, [llstr, ord("a")]) assert llstr.chars[1] == "a"
def ll_str(self, ll_b): from rpython.rtyper.lltypesystem.rstr import mallocstr, STR if ll_b: lgt = ll_b.length() ll_s = mallocstr(lgt) for i in range(lgt): ll_s.chars[i] = ll_b.chars[i] return ll_s else: return lltype.nullptr(STR)
def test_debug_fatalerror(): from rpython.rtyper.lltypesystem import lltype, llmemory, rstr from rpython.rtyper.llinterp import LLFatalError msg = rstr.mallocstr(1) msg.chars[0] = "!" msg = lltype.cast_opaque_ptr(llmemory.GCREF, msg) e = py.test.raises(LLFatalError, BlackholeInterpreter.bhimpl_debug_fatalerror.im_func, msg) assert str(e.value) == '!'
def f(): check(prebuilt) lst1 = [] for i, sz in enumerate(sizes): s = rstr.mallocstr(sz) a = lltype.malloc(A, flavor='raw') a.x = i lst1.append((s, a)) check(lst1) for _, a in lst1: lltype.free(a, flavor='raw') return 42
def llstr(s): from rpython.rtyper.lltypesystem.rstr import mallocstr, mallocunicode from rpython.rtyper.lltypesystem.rstr import STR, UNICODE if strtype is str: if s is None: return lltype.nullptr(STR) ll_s = mallocstr(len(s)) else: if s is None: return lltype.nullptr(UNICODE) ll_s = mallocunicode(len(s)) for i, c in enumerate(s): ll_s.chars[i] = c return ll_s
def test_extra_item_after_alloc(self): from rpython.rlib import rgc from rpython.rtyper.lltypesystem import lltype from rpython.rtyper.lltypesystem import rstr # all STR objects should be allocated with enough space for one # extra char. Check this for prebuilt strings, and for dynamically # allocated ones with the default GC for tests. Use strings of 8, # 16 and 24 chars because if the extra char is missing, writing to it # is likely to cause corruption in nearby structures. sizes = [random.choice([8, 16, 24]) for i in range(100)] A = lltype.Struct('A', ('x', lltype.Signed)) prebuilt = [(rstr.mallocstr(sz), lltype.malloc(A, flavor='raw', immortal=True)) for sz in sizes] k = 0 for i, (s, a) in enumerate(prebuilt): a.x = i for i in range(len(s.chars)): k += 1 if k == 256: k = 1 s.chars[i] = chr(k) def check(lst): hashes = [] for i, (s, a) in enumerate(lst): assert a.x == i rgc.ll_write_final_null_char(s) for i, (s, a) in enumerate(lst): assert a.x == i # check it was not overwritten def f(): check(prebuilt) lst1 = [] for i, sz in enumerate(sizes): s = rstr.mallocstr(sz) a = lltype.malloc(A, flavor='raw') a.x = i lst1.append((s, a)) check(lst1) for _, a in lst1: lltype.free(a, flavor='raw') return 42 fn = self.getcompiled(f, []) assert fn() == 42
def ll_str(self, i): # doesn't work for non-gc classes! from rpython.rtyper.lltypesystem import rstr from rpython.rtyper.lltypesystem.ll_str import ll_int2hex from rpython.rlib.rarithmetic import r_uint if not i: return rstr.null_str instance = cast_pointer(OBJECTPTR, i) uid = r_uint(cast_ptr_to_int(i)) nameLen = len(instance.typeptr.name) nameString = rstr.mallocstr(nameLen-1) i = 0 while i < nameLen - 1: nameString.chars[i] = instance.typeptr.name[i] i += 1 res = rstr.instance_str_prefix res = rstr.ll_strconcat(res, nameString) res = rstr.ll_strconcat(res, rstr.instance_str_infix) res = rstr.ll_strconcat(res, ll_int2hex(uid, False)) res = rstr.ll_strconcat(res, rstr.instance_str_suffix) return res
def ll_str(self, i): # doesn't work for non-gc classes! from rpython.rtyper.lltypesystem import rstr from rpython.rtyper.lltypesystem.ll_str import ll_int2hex from rpython.rlib.rarithmetic import r_uint if not i: return rstr.null_str instance = cast_pointer(OBJECTPTR, i) # Two choices: the first gives a fast answer but it can change # (typically only once) during the life of the object. #uid = r_uint(cast_ptr_to_int(i)) uid = r_uint(llop.gc_id(lltype.Signed, i)) # nameLen = len(instance.typeptr.name) nameString = rstr.mallocstr(nameLen-1) i = 0 while i < nameLen - 1: nameString.chars[i] = instance.typeptr.name[i] i += 1 res = rstr.instance_str_prefix res = rstr.ll_strconcat(res, nameString) res = rstr.ll_strconcat(res, rstr.instance_str_infix) res = rstr.ll_strconcat(res, ll_int2hex(uid, False)) res = rstr.ll_strconcat(res, rstr.instance_str_suffix) return res
def RPyString_New(length=lltype.Signed): return mallocstr(length)
def lowlevelhash(value): p = rstr.mallocstr(len(value)) for i in range(len(value)): p.chars[i] = value[i] return rstr.LLHelpers.ll_strhash(p)
def __init__(self, size): self.readonly = False self.size = size self.ll_val = mallocstr(size)
def g(): f(rstr.mallocstr(10))
def test_hlstr(self): s = mallocstr(3) s.chars[0] = "a" s.chars[1] = "b" s.chars[2] = "c" assert hlstr(s) == "abc"