def test_arrayitems(self): TP = lltype.GcArray(lltype.Signed) ofs = symbolic.get_field_token(TP, 'length', False)[0] itemsofs = symbolic.get_field_token(TP, 'items', False)[0] descr = self.cpu.arraydescrof(TP) res = self.execute_operation(rop.NEW_ARRAY, [ConstInt(10)], 'ref', descr) resbuf = self._resbuf(res) assert resbuf[ofs / WORD] == 10 self.execute_operation(rop.SETARRAYITEM_GC, [res, ConstInt(2), BoxInt(38)], 'void', descr) assert resbuf[itemsofs / WORD + 2] == 38 self.execute_operation(rop.SETARRAYITEM_GC, [res, BoxInt(3), BoxInt(42)], 'void', descr) assert resbuf[itemsofs / WORD + 3] == 42 r = self.execute_operation(rop.GETARRAYITEM_GC, [res, ConstInt(2)], 'int', descr) assert r.value == 38 r = self.execute_operation(rop.GETARRAYITEM_GC, [res.constbox(), BoxInt(2)], 'int', descr) assert r.value == 38 r = self.execute_operation( rop.GETARRAYITEM_GC, [res.constbox(), ConstInt(2)], 'int', descr) assert r.value == 38 r = self.execute_operation(rop.GETARRAYITEM_GC, [res, BoxInt(2)], 'int', descr) assert r.value == 38 r = self.execute_operation(rop.GETARRAYITEM_GC, [res, BoxInt(3)], 'int', descr) assert r.value == 42
def test_repr_of_descr(): c0 = GcCache(False) T = lltype.GcStruct('T') S = lltype.GcStruct('S', ('x', lltype.Char), ('y', lltype.Ptr(T)), ('z', lltype.Ptr(T))) descr1 = get_size_descr(c0, S) s = symbolic.get_size(S, False) assert descr1.repr_of_descr() == '<SizeDescr %d>' % s # descr2 = get_field_descr(c0, S, 'y') o, _ = symbolic.get_field_token(S, 'y', False) assert descr2.repr_of_descr() == '<GcPtrFieldDescr %d>' % o # descr2i = get_field_descr(c0, S, 'x') o, _ = symbolic.get_field_token(S, 'x', False) assert descr2i.repr_of_descr() == '<CharFieldDescr %d>' % o # descr3 = get_array_descr(c0, lltype.GcArray(lltype.Ptr(S))) assert descr3.repr_of_descr() == '<GcPtrArrayDescr>' # descr3i = get_array_descr(c0, lltype.GcArray(lltype.Char)) assert descr3i.repr_of_descr() == '<CharArrayDescr>' # cache = {} descr4 = get_call_descr(c0, [lltype.Char, lltype.Ptr(S)], lltype.Ptr(S)) assert 'GcPtrCallDescr' in descr4.repr_of_descr() # descr4i = get_call_descr(c0, [lltype.Char, lltype.Ptr(S)], lltype.Char) assert 'CharCallDescr' in descr4i.repr_of_descr() # descr4f = get_call_descr(c0, [lltype.Char, lltype.Ptr(S)], lltype.Float) assert 'FloatCallDescr' in descr4f.repr_of_descr()
def test_arrayitems(self): TP = lltype.GcArray(lltype.Signed) ofs = symbolic.get_field_token(TP, "length", False)[0] itemsofs = symbolic.get_field_token(TP, "items", False)[0] descr = self.cpu.arraydescrof(TP) res = self.execute_operation(rop.NEW_ARRAY, [ConstInt(10)], "ref", descr) resbuf = self._resbuf(res) assert resbuf[ofs / WORD] == 10 self.execute_operation(rop.SETARRAYITEM_GC, [res, ConstInt(2), BoxInt(38)], "void", descr) assert resbuf[itemsofs / WORD + 2] == 38 self.execute_operation(rop.SETARRAYITEM_GC, [res, BoxInt(3), BoxInt(42)], "void", descr) assert resbuf[itemsofs / WORD + 3] == 42 r = self.execute_operation(rop.GETARRAYITEM_GC, [res, ConstInt(2)], "int", descr) assert r.value == 38 r = self.execute_operation(rop.GETARRAYITEM_GC, [res.constbox(), BoxInt(2)], "int", descr) assert r.value == 38 r = self.execute_operation(rop.GETARRAYITEM_GC, [res.constbox(), ConstInt(2)], "int", descr) assert r.value == 38 r = self.execute_operation(rop.GETARRAYITEM_GC, [res, BoxInt(2)], "int", descr) assert r.value == 38 r = self.execute_operation(rop.GETARRAYITEM_GC, [res, BoxInt(3)], "int", descr) assert r.value == 42
def test_allocations(self): from pypy.rpython.lltypesystem import rstr allocs = [None] all = [] def f(size): allocs.insert(0, size) buf = ctypes.create_string_buffer(size) all.append(buf) return ctypes.cast(buf, ctypes.c_void_p).value func = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int)(f) addr = ctypes.cast(func, ctypes.c_void_p).value try: saved_addr = self.cpu.assembler.malloc_func_addr self.cpu.assembler.malloc_func_addr = addr ofs = symbolic.get_field_token(rstr.STR, 'chars', False)[0] res = self.execute_operation(rop.NEWSTR, [ConstInt(7)], 'ref') assert allocs[0] == 7 + ofs + WORD resbuf = self._resbuf(res) assert resbuf[ofs / WORD] == 7 # ------------------------------------------------------------ res = self.execute_operation(rop.NEWSTR, [BoxInt(7)], 'ref') assert allocs[0] == 7 + ofs + WORD resbuf = self._resbuf(res) assert resbuf[ofs / WORD] == 7 # ------------------------------------------------------------ TP = lltype.GcArray(lltype.Signed) ofs = symbolic.get_field_token(TP, 'length', False)[0] descr = self.cpu.arraydescrof(TP) res = self.execute_operation(rop.NEW_ARRAY, [ConstInt(10)], 'ref', descr) assert allocs[0] == 10 * WORD + ofs + WORD resbuf = self._resbuf(res) assert resbuf[ofs / WORD] == 10 # ------------------------------------------------------------ res = self.execute_operation(rop.NEW_ARRAY, [BoxInt(10)], 'ref', descr) assert allocs[0] == 10 * WORD + ofs + WORD resbuf = self._resbuf(res) assert resbuf[ofs / WORD] == 10 finally: self.cpu.assembler.malloc_func_addr = saved_addr
def test_allocations(self): from pypy.rpython.lltypesystem import rstr allocs = [None] all = [] def f(size): allocs.insert(0, size) buf = ctypes.create_string_buffer(size) all.append(buf) return ctypes.cast(buf, ctypes.c_void_p).value func = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int)(f) addr = ctypes.cast(func, ctypes.c_void_p).value try: saved_addr = self.cpu.assembler.malloc_func_addr self.cpu.assembler.malloc_func_addr = addr ofs = symbolic.get_field_token(rstr.STR, 'chars', False)[0] res = self.execute_operation(rop.NEWSTR, [ConstInt(7)], 'ref') assert allocs[0] == 7 + ofs + WORD resbuf = self._resbuf(res) assert resbuf[ofs/WORD] == 7 # ------------------------------------------------------------ res = self.execute_operation(rop.NEWSTR, [BoxInt(7)], 'ref') assert allocs[0] == 7 + ofs + WORD resbuf = self._resbuf(res) assert resbuf[ofs/WORD] == 7 # ------------------------------------------------------------ TP = lltype.GcArray(lltype.Signed) ofs = symbolic.get_field_token(TP, 'length', False)[0] descr = self.cpu.arraydescrof(TP) res = self.execute_operation(rop.NEW_ARRAY, [ConstInt(10)], 'ref', descr) assert allocs[0] == 10*WORD + ofs + WORD resbuf = self._resbuf(res) assert resbuf[ofs/WORD] == 10 # ------------------------------------------------------------ res = self.execute_operation(rop.NEW_ARRAY, [BoxInt(10)], 'ref', descr) assert allocs[0] == 10*WORD + ofs + WORD resbuf = self._resbuf(res) assert resbuf[ofs/WORD] == 10 finally: self.cpu.assembler.malloc_func_addr = saved_addr
def test_stringitems(self): from pypy.rpython.lltypesystem.rstr import STR ofs = symbolic.get_field_token(STR, 'chars', False)[0] ofs_items = symbolic.get_field_token(STR.chars, 'items', False)[0] res = self.execute_operation(rop.NEWSTR, [ConstInt(10)], 'ref') self.execute_operation(rop.STRSETITEM, [res, ConstInt(2), ConstInt(ord('d'))], 'void') resbuf = self._resbuf(res, ctypes.c_char) assert resbuf[ofs + ofs_items + 2] == 'd' self.execute_operation(rop.STRSETITEM, [res, BoxInt(2), ConstInt(ord('z'))], 'void') assert resbuf[ofs + ofs_items + 2] == 'z' r = self.execute_operation(rop.STRGETITEM, [res, BoxInt(2)], 'int') assert r.value == ord('z')
def test_stringitems(self): from pypy.rpython.lltypesystem.rstr import STR ofs = symbolic.get_field_token(STR, "chars", False)[0] ofs_items = symbolic.get_field_token(STR.chars, "items", False)[0] res = self.execute_operation(rop.NEWSTR, [ConstInt(10)], "ref") self.execute_operation(rop.STRSETITEM, [res, ConstInt(2), ConstInt(ord("d"))], "void") resbuf = self._resbuf(res, ctypes.c_char) assert resbuf[ofs + ofs_items + 2] == "d" self.execute_operation(rop.STRSETITEM, [res, BoxInt(2), ConstInt(ord("z"))], "void") assert resbuf[ofs + ofs_items + 2] == "z" r = self.execute_operation(rop.STRGETITEM, [res, BoxInt(2)], "int") assert r.value == ord("z")
def test_arrayitems_not_int(self): TP = lltype.GcArray(lltype.Char) ofs = symbolic.get_field_token(TP, "length", False)[0] itemsofs = symbolic.get_field_token(TP, "items", False)[0] descr = self.cpu.arraydescrof(TP) res = self.execute_operation(rop.NEW_ARRAY, [ConstInt(10)], "ref", descr) resbuf = self._resbuf(res, ctypes.c_char) assert resbuf[ofs] == chr(10) for i in range(10): self.execute_operation(rop.SETARRAYITEM_GC, [res, ConstInt(i), BoxInt(i)], "void", descr) for i in range(10): assert resbuf[itemsofs + i] == chr(i) for i in range(10): r = self.execute_operation(rop.GETARRAYITEM_GC, [res, ConstInt(i)], "int", descr) assert r.value == i
def __init__(self, rtyper, stats, translate_support_code=False, gcdescr=None): from pypy.jit.backend.llsupport.gc import get_ll_description self.rtyper = rtyper self.stats = stats self.translate_support_code = translate_support_code if translate_support_code: translator = rtyper.annotator.translator else: translator = None self.gc_ll_descr = get_ll_description(gcdescr, translator) self.vtable_offset, _ = symbolic.get_field_token( rclass.OBJECT, 'typeptr', translate_support_code) self._setup_prebuilt_error('ovf', OverflowError) self._setup_prebuilt_error('zer', ZeroDivisionError) if translate_support_code: self._setup_exception_handling_translated() else: self._setup_exception_handling_untranslated() self.clear_exception() self.setup() if translate_support_code: self._setup_on_leave_jitted_translated() else: self._setup_on_leave_jitted_untranslated()
def __init__(self, rtyper, stats, translate_support_code=False, gcdescr=None): from pypy.jit.backend.llsupport.gc import get_ll_description self.rtyper = rtyper self.stats = stats self.translate_support_code = translate_support_code if translate_support_code: translator = rtyper.annotator.translator else: translator = None self.gc_ll_descr = get_ll_description(gcdescr, translator) self.vtable_offset, _ = symbolic.get_field_token(rclass.OBJECT, 'typeptr', translate_support_code) self._setup_prebuilt_error('ovf', OverflowError) self._setup_prebuilt_error('zer', ZeroDivisionError) if translate_support_code: self._setup_exception_handling_translated() else: self._setup_exception_handling_untranslated() self.clear_exception() self.setup() if translate_support_code: self._setup_on_leave_jitted_translated() else: self._setup_on_leave_jitted_untranslated()
def __init__(self, rtyper, stats, opts, translate_support_code=False, gcdescr=None): assert type(opts) is not bool self.opts = opts from pypy.jit.backend.llsupport.gc import get_ll_description AbstractCPU.__init__(self) self.rtyper = rtyper self.stats = stats self.translate_support_code = translate_support_code if translate_support_code: translator = rtyper.annotator.translator else: translator = None self.gc_ll_descr = get_ll_description(gcdescr, translator, rtyper) if translator and translator.config.translation.gcremovetypeptr: self.vtable_offset = None else: self.vtable_offset, _ = symbolic.get_field_token(rclass.OBJECT, 'typeptr', translate_support_code) self._setup_prebuilt_error('ovf', OverflowError) self._setup_prebuilt_error('zer', ZeroDivisionError) if translate_support_code: self._setup_exception_handling_translated() else: self._setup_exception_handling_untranslated() self.saved_exc_value = lltype.nullptr(llmemory.GCREF.TO) self.asmmemmgr = AsmMemoryManager() self.setup() if translate_support_code: self._setup_on_leave_jitted_translated() else: self._setup_on_leave_jitted_untranslated()
def test_allocations(self): py.test.skip("rewrite or kill") from pypy.rpython.lltypesystem import rstr allocs = [None] all = [] orig_new = self.cpu.gc_ll_descr.funcptr_for_new def f(size): allocs.insert(0, size) return orig_new(size) self.cpu.assembler.setup_once() self.cpu.gc_ll_descr.funcptr_for_new = f ofs = symbolic.get_field_token(rstr.STR, 'chars', False)[0] res = self.execute_operation(rop.NEWSTR, [ConstInt(7)], 'ref') assert allocs[0] == 7 + ofs + WORD resbuf = self._resbuf(res) assert resbuf[ofs/WORD] == 7 # ------------------------------------------------------------ res = self.execute_operation(rop.NEWSTR, [BoxInt(7)], 'ref') assert allocs[0] == 7 + ofs + WORD resbuf = self._resbuf(res) assert resbuf[ofs/WORD] == 7 # ------------------------------------------------------------ TP = lltype.GcArray(lltype.Signed) ofs = symbolic.get_field_token(TP, 'length', False)[0] descr = self.cpu.arraydescrof(TP) res = self.execute_operation(rop.NEW_ARRAY, [ConstInt(10)], 'ref', descr) assert allocs[0] == 10*WORD + ofs + WORD resbuf = self._resbuf(res) assert resbuf[ofs/WORD] == 10 # ------------------------------------------------------------ res = self.execute_operation(rop.NEW_ARRAY, [BoxInt(10)], 'ref', descr) assert allocs[0] == 10*WORD + ofs + WORD resbuf = self._resbuf(res) assert resbuf[ofs/WORD] == 10
def test_arrayitems_not_int(self): TP = lltype.GcArray(lltype.Char) ofs = symbolic.get_field_token(TP, 'length', False)[0] itemsofs = symbolic.get_field_token(TP, 'items', False)[0] descr = self.cpu.arraydescrof(TP) res = self.execute_operation(rop.NEW_ARRAY, [ConstInt(10)], 'ref', descr) resbuf = self._resbuf(res, ctypes.c_char) assert resbuf[ofs] == chr(10) for i in range(10): self.execute_operation( rop.SETARRAYITEM_GC, [res, ConstInt(i), BoxInt(i)], 'void', descr) for i in range(10): assert resbuf[itemsofs + i] == chr(i) for i in range(10): r = self.execute_operation(rop.GETARRAYITEM_GC, [res, ConstInt(i)], 'int', descr) assert r.value == i
def get_field_descr(gccache, STRUCT, fieldname): cache = gccache._cache_field try: return cache[STRUCT][fieldname] except KeyError: offset, _ = symbolic.get_field_token(STRUCT, fieldname, gccache.translate_support_code) FIELDTYPE = getattr(STRUCT, fieldname) fielddescr = getFieldDescrClass(FIELDTYPE)(offset) cachedict = cache.setdefault(STRUCT, {}) cachedict[fieldname] = fielddescr return fielddescr
def test_unicode(self): ofs = symbolic.get_field_token(rstr.UNICODE, "chars", False)[0] u = rstr.mallocunicode(13) for i in range(13): u.chars[i] = unichr(ord(u"a") + i) b = BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, u)) r = self.execute_operation(rop.UNICODEGETITEM, [b, ConstInt(2)], "int") assert r.value == ord(u"a") + 2 self.execute_operation(rop.UNICODESETITEM, [b, ConstInt(2), ConstInt(ord(u"z"))], "void") assert u.chars[2] == u"z" assert u.chars[3] == u"d"
def fielddescrof(self, S, fieldname): try: return self._descr_caches['field', S, fieldname] except KeyError: pass ofs, _ = symbolic.get_field_token(S, fieldname, self.translate_support_code) size_index = self._get_size_index(getattr(S, fieldname)) descr = FieldDescr(ofs, size_index) self._descr_caches['field', S, fieldname] = descr return descr
def test_repr_of_descr(): def repr_of_descr(descr): s = descr.repr_of_descr() assert ',' not in s # makes the life easier for pypy.tool.jitlogparser return s c0 = GcCache(False) T = lltype.GcStruct('T') S = lltype.GcStruct('S', ('x', lltype.Char), ('y', lltype.Ptr(T)), ('z', lltype.Ptr(T))) descr1 = get_size_descr(c0, S) s = symbolic.get_size(S, False) assert repr_of_descr(descr1) == '<SizeDescr %d>' % s # descr2 = get_field_descr(c0, S, 'y') o, _ = symbolic.get_field_token(S, 'y', False) assert repr_of_descr(descr2) == '<FieldP S.y %d>' % o # descr2i = get_field_descr(c0, S, 'x') o, _ = symbolic.get_field_token(S, 'x', False) assert repr_of_descr(descr2i) == '<FieldU S.x %d>' % o # descr3 = get_array_descr(c0, lltype.GcArray(lltype.Ptr(S))) o = symbolic.get_size(lltype.Ptr(S), False) assert repr_of_descr(descr3) == '<ArrayP %d>' % o # descr3i = get_array_descr(c0, lltype.GcArray(lltype.Char)) assert repr_of_descr(descr3i) == '<ArrayU 1>' # descr4 = get_call_descr(c0, [lltype.Char, lltype.Ptr(S)], lltype.Ptr(S)) assert repr_of_descr(descr4) == '<Callr %d ir>' % o # descr4i = get_call_descr(c0, [lltype.Char, lltype.Ptr(S)], lltype.Char) assert repr_of_descr(descr4i) == '<Calli 1 ir>' # descr4f = get_call_descr(c0, [lltype.Char, lltype.Ptr(S)], lltype.Float) assert repr_of_descr(descr4f) == '<Callf 8 ir>' # descr5f = get_call_descr(c0, [lltype.Char], lltype.SingleFloat) assert repr_of_descr(descr5f) == '<CallS 4 i>'
def test_unicode(self): ofs = symbolic.get_field_token(rstr.UNICODE, 'chars', False)[0] u = rstr.mallocunicode(13) for i in range(13): u.chars[i] = unichr(ord(u'a') + i) b = BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, u)) r = self.execute_operation(rop.UNICODEGETITEM, [b, ConstInt(2)], 'int') assert r.value == ord(u'a') + 2 self.execute_operation(rop.UNICODESETITEM, [b, ConstInt(2), ConstInt(ord(u'z'))], 'void') assert u.chars[2] == u'z' assert u.chars[3] == u'd'
def get_field_descr(gccache, STRUCT, fieldname): cache = gccache._cache_field try: return cache[STRUCT][fieldname] except KeyError: offset, size = symbolic.get_field_token(STRUCT, fieldname, gccache.translate_support_code) FIELDTYPE = getattr(STRUCT, fieldname) flag = get_type_flag(FIELDTYPE) name = "%s.%s" % (STRUCT._name, fieldname) fielddescr = FieldDescr(name, offset, size, flag) cachedict = cache.setdefault(STRUCT, {}) cachedict[fieldname] = fielddescr return fielddescr
def test_repr_of_descr(): def repr_of_descr(descr): s = descr.repr_of_descr() assert "," not in s # makes the life easier for pypy.tool.jitlogparser return s c0 = GcCache(False) T = lltype.GcStruct("T") S = lltype.GcStruct("S", ("x", lltype.Char), ("y", lltype.Ptr(T)), ("z", lltype.Ptr(T))) descr1 = get_size_descr(c0, S) s = symbolic.get_size(S, False) assert repr_of_descr(descr1) == "<SizeDescr %d>" % s # descr2 = get_field_descr(c0, S, "y") o, _ = symbolic.get_field_token(S, "y", False) assert repr_of_descr(descr2) == "<FieldP S.y %d>" % o # descr2i = get_field_descr(c0, S, "x") o, _ = symbolic.get_field_token(S, "x", False) assert repr_of_descr(descr2i) == "<FieldU S.x %d>" % o # descr3 = get_array_descr(c0, lltype.GcArray(lltype.Ptr(S))) o = symbolic.get_size(lltype.Ptr(S), False) assert repr_of_descr(descr3) == "<ArrayP %d>" % o # descr3i = get_array_descr(c0, lltype.GcArray(lltype.Char)) assert repr_of_descr(descr3i) == "<ArrayU 1>" # descr4 = get_call_descr(c0, [lltype.Char, lltype.Ptr(S)], lltype.Ptr(S)) assert repr_of_descr(descr4) == "<Callr %d ir>" % o # descr4i = get_call_descr(c0, [lltype.Char, lltype.Ptr(S)], lltype.Char) assert repr_of_descr(descr4i) == "<Calli 1 ir>" # descr4f = get_call_descr(c0, [lltype.Char, lltype.Ptr(S)], lltype.Float) assert repr_of_descr(descr4f) == "<Callf 8 ir>" # descr5f = get_call_descr(c0, [lltype.Char], lltype.SingleFloat) assert repr_of_descr(descr5f) == "<CallS 4 i>"
def get_field_descr(gccache, STRUCT, fieldname): cache = gccache._cache_field try: return cache[STRUCT][fieldname] except KeyError: offset, size = symbolic.get_field_token(STRUCT, fieldname, gccache.translate_support_code) FIELDTYPE = getattr(STRUCT, fieldname) flag = get_type_flag(FIELDTYPE) name = '%s.%s' % (STRUCT._name, fieldname) fielddescr = FieldDescr(name, offset, size, flag) cachedict = cache.setdefault(STRUCT, {}) cachedict[fieldname] = fielddescr return fielddescr