def wrap(cpu, value, in_const_box=False): if isinstance(lltype.typeOf(value), lltype.Ptr): if lltype.typeOf(value).TO._gckind == 'gc': value = lltype.cast_opaque_ptr(llmemory.GCREF, value) if in_const_box: return history.ConstPtr(value) else: return history.BoxPtr(value) else: adr = llmemory.cast_ptr_to_adr(value) value = cpu.cast_adr_to_int(adr) # fall through to the end of the function elif isinstance(lltype.typeOf(value), ootype.OOType): value = ootype.cast_to_object(value) if in_const_box: return history.ConstObj(value) else: return history.BoxObj(value) elif isinstance(value, float): if in_const_box: return history.ConstFloat(value) else: return history.BoxFloat(value) else: value = intmask(value) if in_const_box: return history.ConstInt(value) else: return history.BoxInt(value)
def do_new_with_vtable(self, vtablebox): vtable = vtablebox.getint() size = self.class_sizes[vtable] result = llimpl.do_new(size.ofs) llimpl.do_setfield_gc_int(result, self.fielddescrof_vtable.ofs, vtable, self.memo_cast) return history.BoxPtr(result)
def do_getarrayitem_gc(self, arraybox, indexbox, arraydescr): assert isinstance(arraydescr, Descr) array = arraybox.getref_base() index = indexbox.getint() if arraydescr.typeinfo == REF: return history.BoxPtr(llimpl.do_getarrayitem_gc_ptr(array, index)) elif arraydescr.typeinfo == INT: return history.BoxInt( llimpl.do_getarrayitem_gc_int(array, index, self.memo_cast)) elif arraydescr.typeinfo == FLOAT: return history.BoxFloat( llimpl.do_getarrayitem_gc_float(array, index)) else: raise NotImplementedError
def do_getfield_gc(self, structbox, fielddescr): assert isinstance(fielddescr, Descr) struct = structbox.getref_base() if fielddescr.typeinfo == REF: return history.BoxPtr( llimpl.do_getfield_gc_ptr(struct, fielddescr.ofs)) elif fielddescr.typeinfo == INT: return history.BoxInt( llimpl.do_getfield_gc_int(struct, fielddescr.ofs, self.memo_cast)) elif fielddescr.typeinfo == FLOAT: return history.BoxFloat( llimpl.do_getfield_gc_float(struct, fielddescr.ofs)) else: raise NotImplementedError
def do_getfield_raw(self, structbox, fielddescr): assert isinstance(fielddescr, Descr) struct = self.cast_int_to_adr(structbox.getint()) if fielddescr.typeinfo == REF: return history.BoxPtr( llimpl.do_getfield_raw_ptr(struct, fielddescr.ofs, self.memo_cast)) elif fielddescr.typeinfo == INT: return history.BoxInt( llimpl.do_getfield_raw_int(struct, fielddescr.ofs, self.memo_cast)) elif fielddescr.typeinfo == FLOAT: return history.BoxFloat( llimpl.do_getfield_raw_float(struct, fielddescr.ofs, self.memo_cast)) else: raise NotImplementedError
def test_casts(self): if not self.basic: py.test.skip("test written in a style that " "means it's frontend only") from pypy.rpython.lltypesystem import lltype, llmemory TP = lltype.GcStruct('x') def f(p): n = lltype.cast_ptr_to_int(p) return n x = lltype.malloc(TP) res = self.interp_operations(f, [x]) expected = self.metainterp.cpu.do_cast_ptr_to_int( history.BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, x))).value assert res == expected
def do_call(self, args, calldescr): assert isinstance(calldescr, Descr) func = args[0].getint() for arg in args[1:]: if arg.type == REF: llimpl.do_call_pushptr(arg.getref_base()) elif arg.type == FLOAT: llimpl.do_call_pushfloat(arg.getfloat()) else: llimpl.do_call_pushint(arg.getint()) if calldescr.typeinfo == REF: return history.BoxPtr(llimpl.do_call_ptr(func, self.memo_cast)) elif calldescr.typeinfo == INT: return history.BoxInt(llimpl.do_call_int(func, self.memo_cast)) elif calldescr.typeinfo == FLOAT: return history.BoxFloat(llimpl.do_call_float(func, self.memo_cast)) elif calldescr.typeinfo == 'v': # void llimpl.do_call_void(func, self.memo_cast) else: raise NotImplementedError
def wrap(cpu, value, in_const_box=False): if isinstance(lltype.typeOf(value), lltype.Ptr): if lltype.typeOf(value).TO._gckind == 'gc': value = lltype.cast_opaque_ptr(llmemory.GCREF, value) if in_const_box: return history.ConstPtr(value) else: return history.BoxPtr(value) else: adr = llmemory.cast_ptr_to_adr(value) value = heaptracker.adr2int(adr) # fall through to the end of the function elif isinstance(lltype.typeOf(value), ootype.OOType): value = ootype.cast_to_object(value) if in_const_box: return history.ConstObj(value) else: return history.BoxObj(value) elif (isinstance(value, float) or longlong.is_longlong(lltype.typeOf(value))): if isinstance(value, float): value = longlong.getfloatstorage(value) else: value = rffi.cast(lltype.SignedLongLong, value) if in_const_box: return history.ConstFloat(value) else: return history.BoxFloat(value) elif isinstance(value, str) or isinstance(value, unicode): assert len(value) == 1 # must be a character value = ord(value) elif lltype.typeOf(value) is lltype.SingleFloat: value = longlong.singlefloat2int(value) else: value = intmask(value) if in_const_box: return history.ConstInt(value) else: return history.BoxInt(value)
def get_exc_value_box(self, evalue): return history.BoxPtr(evalue)
def do_newunicode(self, lengthbox): length = lengthbox.getint() return history.BoxPtr(llimpl.do_newunicode(0, length))
def do_new_array(self, countbox, size): assert isinstance(size, Descr) count = countbox.getint() return history.BoxPtr(llimpl.do_new_array(size.ofs, count))
def do_new(self, size): assert isinstance(size, Descr) return history.BoxPtr(llimpl.do_new(size.ofs))