def getvar(self, arg): if not arg: return ConstInt(0) try: return ConstInt(int(arg)) except ValueError: if self.is_float(arg): return ConstFloat(float(arg)) if arg.startswith('"') or arg.startswith("'"): # XXX ootype info = arg.strip("'\"") return ConstPtr( lltype.cast_opaque_ptr(llmemory.GCREF, llstr(info))) if arg.startswith('ConstClass('): name = arg[len('ConstClass('):-1] return self.get_const(name, 'class') elif arg == 'None': return None elif arg == 'NULL': if self.type_system == 'lltype': return ConstPtr(ConstPtr.value) else: return ConstObj(ConstObj.value) elif arg.startswith('ConstPtr('): name = arg[len('ConstPtr('):-1] return self.get_const(name, 'ptr') return self.vars[arg]
def make_args_for_op(op, a, b): n=opname[op] if n[0:3] == 'INT' or n[0:4] == 'UINT': arg1 = ConstInt(a) arg2 = ConstInt(b) elif n[0:5] == 'FLOAT': arg1 = constfloat(float(a)) arg2 = constfloat(float(b)) elif n[0:3] == 'PTR': arg1 = ConstPtr(rffi.cast(llmemory.GCREF, a)) arg2 = ConstPtr(rffi.cast(llmemory.GCREF, b)) else: raise NotImplementedError( "Don't know how to make args for " + n) return arg1, arg2
def constant(value): if isinstance(lltype.typeOf(value), lltype.Ptr): return ConstPtr(value) elif isinstance(ootype.typeOf(value), ootype.OOType): return ConstObj(ootype.cast_to_object(value)) else: return ConstInt(value)
def get_structptr_var(self, r, must_have_vtable=False, type=lltype.Struct): while True: ptrvars = [(v, S) for (v, S) in self.ptrvars if isinstance(S, type)] if ptrvars and r.random() < 0.8: v, S = r.choice(ptrvars) else: prebuilt_ptr_consts = [(v, S) for (v, S, _) in self.prebuilt_ptr_consts if isinstance(S, type)] if prebuilt_ptr_consts and r.random() < 0.7: v, S = r.choice(prebuilt_ptr_consts) else: if type is lltype.Struct: # create a new constant structure must_have_vtable = must_have_vtable or r.random() < 0.5 p = self.get_random_structure(r, has_vtable=must_have_vtable) else: # create a new constant array p = self.get_random_array(r) S = lltype.typeOf(p).TO v = ConstPtr(lltype.cast_opaque_ptr(llmemory.GCREF, p)) self.prebuilt_ptr_consts.append((v, S, self.field_values(p))) if not (must_have_vtable and S._names[0] != 'parent'): break return v, S
def test_record_constptrs(self): class MyFakeCPU(object): def cast_adr_to_int(self, adr): assert adr == "some fake address" return 43 class MyFakeGCRefList(object): def get_address_of_gcref(self, s_gcref1): assert s_gcref1 == s_gcref return "some fake address" S = lltype.GcStruct('S') s = lltype.malloc(S) s_gcref = lltype.cast_opaque_ptr(llmemory.GCREF, s) v_random_box = BoxPtr() v_result = BoxInt() operations = [ ResOperation(rop.PTR_EQ, [v_random_box, ConstPtr(s_gcref)], v_result), ] gc_ll_descr = self.gc_ll_descr gc_ll_descr.gcrefs = MyFakeGCRefList() gcrefs = [] operations = get_deep_immutable_oplist(operations) operations2 = gc_ll_descr.rewrite_assembler(MyFakeCPU(), operations, gcrefs) assert operations2 == operations assert gcrefs == [s_gcref]
def test_known_class_generalization(self): knownclass1 = OptValue(BoxPtr()) knownclass1.make_constant_class(ConstPtr(self.someptr1), 0) info1 = NotVirtualStateInfo(knownclass1) info1.position = 0 knownclass2 = OptValue(BoxPtr()) knownclass2.make_constant_class(ConstPtr(self.someptr1), 0) info2 = NotVirtualStateInfo(knownclass2) info2.position = 0 assert info1.generalization_of(info2, {}, {}) assert info2.generalization_of(info1, {}, {}) knownclass3 = OptValue(BoxPtr()) knownclass3.make_constant_class(ConstPtr(self.someptr2), 0) info3 = NotVirtualStateInfo(knownclass3) info3.position = 0 assert not info1.generalization_of(info3, {}, {}) assert not info2.generalization_of(info3, {}, {}) assert not info3.generalization_of(info2, {}, {}) assert not info3.generalization_of(info1, {}, {})
def test_wrap(): def _is(box1, box2): return (box1.__class__ == box2.__class__ and box1.value == box2.value) p = lltype.malloc(lltype.GcStruct('S')) po = lltype.cast_opaque_ptr(llmemory.GCREF, p) assert _is(wrap(None, 42), BoxInt(42)) assert _is(wrap(None, 42.5), BoxFloat(42.5)) assert _is(wrap(None, p), BoxPtr(po)) assert _is(wrap(None, 42, in_const_box=True), ConstInt(42)) assert _is(wrap(None, 42.5, in_const_box=True), ConstFloat(42.5)) assert _is(wrap(None, p, in_const_box=True), ConstPtr(po))
def get_string(self, builder, r): current = getattr(builder, self.builder_cache) if current and r.random() < .8: v_string = r.choice(current) string = v_string.getref(self.ptr) else: string = self.alloc(builder.get_index(500, r).getint()) v_string = ConstPtr(lltype.cast_opaque_ptr(llmemory.GCREF, string)) current.append(v_string) for i in range(len(string.chars)): char = r.random_integer() % self.max string.chars[i] = lltype.cast_primitive(self.primitive, char) return v_string
def gen_guard(self, builder, r): if r.random() < 0.5: return GuardClassOperation.gen_guard(self, builder, r) else: v = BoxPtr(lltype.nullptr(llmemory.GCREF.TO)) op = ResOperation(rop.SAME_AS, [ConstPtr(v.value)], v) builder.loop.operations.append(op) v2, S2 = builder.get_structptr_var(r, must_have_vtable=True) vtable2 = S2._hints['vtable']._as_ptr() c_vtable2 = ConstAddr(llmemory.cast_ptr_to_adr(vtable2), builder.cpu) op = ResOperation(self.opnum, [v, c_vtable2], None) return op, False
def test_NotVirtualStateInfo_generalization(self): def isgeneral(value1, value2): info1 = NotVirtualStateInfo(value1) info1.position = 0 info2 = NotVirtualStateInfo(value2) info2.position = 0 return info1.generalization_of(info2, {}, {}) assert isgeneral(OptValue(BoxInt()), OptValue(ConstInt(7))) assert not isgeneral(OptValue(ConstInt(7)), OptValue(BoxInt())) ptr = OptValue(BoxPtr()) nonnull = OptValue(BoxPtr()) nonnull.make_nonnull(0) knownclass = OptValue(BoxPtr()) knownclass.make_constant_class(ConstPtr(self.someptr1), 0) const = OptValue(BoxPtr) const.make_constant_class(ConstPtr(self.someptr1), 0) const.make_constant(ConstPtr(self.someptr1)) inorder = [ptr, nonnull, knownclass, const] for i in range(len(inorder)): for j in range(i, len(inorder)): assert isgeneral(inorder[i], inorder[j]) if i != j: assert not isgeneral(inorder[j], inorder[i]) value1 = OptValue(BoxInt()) value2 = OptValue(BoxInt()) value2.intbound.make_lt(IntBound(10, 10)) assert isgeneral(value1, value2) assert not isgeneral(value2, value1) assert isgeneral(OptValue(ConstInt(7)), OptValue(ConstInt(7))) S = lltype.GcStruct('S') foo = lltype.malloc(S) fooref = lltype.cast_opaque_ptr(llmemory.GCREF, foo) assert isgeneral(OptValue(ConstPtr(fooref)), OptValue(ConstPtr(fooref)))
def test_bug_setfield_64bit(self): if WORD == 4: py.test.skip("only for 64 bits") TP = lltype.GcStruct('S', ('i', lltype.Signed)) ofsi = self.cpu.fielddescrof(TP, 'i') for i in range(500): p = lltype.malloc(TP) addr = rffi.cast(lltype.Signed, p) if fits_in_32bits(addr): break # fitting in 32 bits, good else: py.test.skip("cannot get a 32-bit pointer") res = ConstPtr(rffi.cast(llmemory.GCREF, addr)) self.execute_operation(rop.SETFIELD_RAW, [res, ConstInt(3**33)], 'void', ofsi) assert p.i == 3**33
def get_const(self, name, typ): if self._consts is None: return name obj = self._consts[name] if self.type_system == 'lltype': if typ == 'ptr': return ConstPtr(obj) else: assert typ == 'class' return ConstAddr(llmemory.cast_ptr_to_adr(obj), self.cpu) else: if typ == 'ptr': return ConstObj(obj) else: assert typ == 'class' return ConstObj(ootype.cast_to_object(obj))
def test_wrap(): def _is(box1, box2): return (box1.__class__ == box2.__class__ and box1.value == box2.value) p = lltype.malloc(lltype.GcStruct('S')) po = lltype.cast_opaque_ptr(llmemory.GCREF, p) assert _is(wrap(None, 42), BoxInt(42)) assert _is(wrap(None, 42.5), boxfloat(42.5)) assert _is(wrap(None, p), BoxPtr(po)) assert _is(wrap(None, 42, in_const_box=True), ConstInt(42)) assert _is(wrap(None, 42.5, in_const_box=True), constfloat(42.5)) assert _is(wrap(None, p, in_const_box=True), ConstPtr(po)) if longlong.supports_longlong: import sys from pypy.rlib.rarithmetic import r_longlong, r_ulonglong value = r_longlong(-sys.maxint * 17) assert _is(wrap(None, value), BoxFloat(value)) assert _is(wrap(None, value, in_const_box=True), ConstFloat(value)) value_unsigned = r_ulonglong(-sys.maxint * 17) assert _is(wrap(None, value_unsigned), BoxFloat(value)) sfval = r_singlefloat(42.5) ival = longlong.singlefloat2int(sfval) assert _is(wrap(None, sfval), BoxInt(ival)) assert _is(wrap(None, sfval, in_const_box=True), ConstInt(ival))
def setup_class(cls): if option.runappdirect: py.test.skip("Can't run this test with -A") space = gettestobjspace(usemodules=('pypyjit', )) cls.space = space w_f = space.appexec([], """(): def function(): pass return function """) cls.w_f = w_f ll_code = cast_instance_to_base_ptr(w_f.code) code_gcref = lltype.cast_opaque_ptr(llmemory.GCREF, ll_code) logger = Logger(MockSD()) oplist = parse(""" [i1, i2, p2] i3 = int_add(i1, i2) debug_merge_point(0, 0, 0, 0, ConstPtr(ptr0)) guard_nonnull(p2) [] guard_true(i3) [] """, namespace={ 'ptr0': code_gcref }).operations greenkey = [ConstInt(0), ConstInt(0), ConstPtr(code_gcref)] offset = {} for i, op in enumerate(oplist): if i != 1: offset[op] = i di_loop = JitDebugInfo(MockJitDriverSD, logger, JitCellToken(), oplist, 'loop', greenkey) di_loop_optimize = JitDebugInfo(MockJitDriverSD, logger, JitCellToken(), oplist, 'loop', greenkey) di_loop.asminfo = AsmInfo(offset, 0, 0) di_bridge = JitDebugInfo(MockJitDriverSD, logger, JitCellToken(), oplist, 'bridge', fail_descr_no=0) di_bridge.asminfo = AsmInfo(offset, 0, 0) def interp_on_compile(): di_loop.oplist = cls.oplist pypy_hooks.after_compile(di_loop) def interp_on_compile_bridge(): pypy_hooks.after_compile_bridge(di_bridge) def interp_on_optimize(): di_loop_optimize.oplist = cls.oplist pypy_hooks.before_compile(di_loop_optimize) def interp_on_abort(): pypy_hooks.on_abort(ABORT_TOO_LONG, pypyjitdriver, greenkey, 'blah') cls.w_on_compile = space.wrap(interp2app(interp_on_compile)) cls.w_on_compile_bridge = space.wrap( interp2app(interp_on_compile_bridge)) cls.w_on_abort = space.wrap(interp2app(interp_on_abort)) cls.w_int_add_num = space.wrap(rop.INT_ADD) cls.w_on_optimize = space.wrap(interp2app(interp_on_optimize)) cls.orig_oplist = oplist
def _get_cspecnode(s): from pypy.rpython.module.support import LLSupport llstr = lltype.cast_opaque_ptr(llmemory.GCREF, LLSupport.to_rstr(s)) box = ConstPtr(llstr) return ConstantSpecNode(box)