def test_ResumeDataLoopMemo_number_boxes(): memo = ResumeDataLoopMemo(FakeMetaInterpStaticData()) b1, b2 = [BoxInt(), BoxInt()] assert memo.num_cached_boxes() == 0 boxes = [] num = memo.assign_number_to_box(b1, boxes) assert num == -1 assert boxes == [b1] assert memo.num_cached_boxes() == 1 boxes = [None] num = memo.assign_number_to_box(b1, boxes) assert num == -1 assert boxes == [b1] num = memo.assign_number_to_box(b2, boxes) assert num == -2 assert boxes == [b1, b2] assert memo.num_cached_boxes() == 2 boxes = [None, None] num = memo.assign_number_to_box(b2, boxes) assert num == -2 assert boxes == [None, b2] num = memo.assign_number_to_box(b1, boxes) assert num == -1 assert boxes == [b1, b2] memo.clear_box_virtual_numbers() assert memo.num_cached_boxes() == 0
def test_rebuild_from_resumedata_two_guards_w_shared_virtuals(): b1, b2, b3, b4, b5, b6 = [BoxPtr(), BoxPtr(), BoxInt(), BoxPtr(), BoxInt(), BoxInt()] c1, c2, c3, c4 = [ConstInt(1), ConstInt(2), ConstInt(3), LLtypeMixin.nodebox.constbox()] storage = Storage() fs = [FakeFrame("code0", 0, -1, c1, b2, b3)] capture_resumedata(fs, None, [], storage) memo = ResumeDataLoopMemo(FakeMetaInterpStaticData()) values = {b2: virtual_value(b2, b5, c4)} modifier = ResumeDataVirtualAdder(storage, memo) liveboxes = modifier.finish(values) assert len(storage.rd_virtuals) == 1 assert storage.rd_virtuals[0].fieldnums == [tag(-1, TAGBOX), tag(0, TAGCONST)] storage2 = Storage() fs = [FakeFrame("code0", 0, -1, b1, b4, b2)] capture_resumedata(fs, None, [], storage2) values[b4] = virtual_value(b4, b6, c4) modifier = ResumeDataVirtualAdder(storage2, memo) liveboxes = modifier.finish(values) assert len(storage2.rd_virtuals) == 2 assert storage2.rd_virtuals[1].fieldnums == storage.rd_virtuals[0].fieldnums assert storage2.rd_virtuals[1] is storage.rd_virtuals[0]
def test_ops_offset(self): from pypy.rlib import debug i0 = BoxInt() i1 = BoxInt() i2 = BoxInt() looptoken = JitCellToken() targettoken = TargetToken() operations = [ ResOperation(rop.LABEL, [i0], None, descr=targettoken), ResOperation(rop.INT_ADD, [i0, ConstInt(1)], i1), ResOperation(rop.INT_LE, [i1, ConstInt(9)], i2), ResOperation(rop.JUMP, [i1], None, descr=targettoken), ] inputargs = [i0] debug._log = dlog = debug.DebugLog() info = self.cpu.compile_loop(inputargs, operations, looptoken) ops_offset = info.ops_offset debug._log = None # assert ops_offset is looptoken._x86_ops_offset # 2*(getfield_raw/int_add/setfield_raw) + ops + None assert len(ops_offset) == 2*3 + len(operations) + 1 assert (ops_offset[operations[0]] <= ops_offset[operations[1]] <= ops_offset[operations[2]] <= ops_offset[None])
def test_overflow_mc(self): from pypy.jit.backend.x86.assembler import MachineCodeBlockWrapper orig_size = MachineCodeBlockWrapper.MC_SIZE MachineCodeBlockWrapper.MC_SIZE = 1024 old_mc = self.cpu.assembler.mc old_mc2 = self.cpu.assembler.mc2 self.cpu.assembler.mc = None try: ops = [] base_v = BoxInt() v = base_v for i in range(1024): next_v = BoxInt() ops.append(ResOperation(rop.INT_ADD, [v, ConstInt(1)], next_v)) v = next_v ops.append( ResOperation(rop.FINISH, [v], None, descr=BasicFailDescr())) looptoken = LoopToken() self.cpu.compile_loop([base_v], ops, looptoken) assert self.cpu.assembler.mc != old_mc # overflowed self.cpu.set_future_value_int(0, base_v.value) self.cpu.execute_token(looptoken) assert self.cpu.get_latest_value_int(0) == 1024 finally: MachineCodeBlockWrapper.MC_SIZE = orig_size self.cpu.assembler.mc = old_mc self.cpu.assembler.mc2 = old_mc2
def test_rebuild_from_resumedata_with_virtualizable(): b1, b2, b3, b4 = [BoxInt(), BoxPtr(), BoxInt(), BoxPtr()] c1, c2, c3 = [ConstInt(1), ConstInt(2), ConstInt(3)] storage = Storage() fs = [ FakeFrame("code0", 0, -1, b1, c1, b2), FakeFrame("code1", 3, 7, b3, c2, b1), FakeFrame("code2", 9, -1, c3, b2) ] capture_resumedata(fs, [b4], storage) memo = ResumeDataLoopMemo(LLtypeMixin.cpu) modifier = ResumeDataVirtualAdder(storage, memo) liveboxes = modifier.finish({}) metainterp = MyMetaInterp() b1t, b2t, b3t, b4t = [BoxInt(), BoxPtr(), BoxInt(), BoxPtr()] newboxes = _resume_remap(liveboxes, [b1, b2, b3, b4], b1t, b2t, b3t, b4t) result = rebuild_from_resumedata(metainterp, newboxes, storage, True) assert result == [b4t] fs2 = [ FakeFrame("code0", 0, -1, b1t, c1, b2t), FakeFrame("code1", 3, 7, b3t, c2, b1t), FakeFrame("code2", 9, -1, c3, b2t) ] assert metainterp.framestack == fs2
def get_token_for_call(self, cpu): if self.loop_token is not None: return self.loop_token args = [BoxInt()] + self.instantiate_arg_classes() if self.get_result_size(cpu.translate_support_code) == 0: result = None result_list = [] else: if self.returns_a_pointer(): result = BoxPtr() elif self.returns_a_float(): result = BoxFloat() else: result = BoxInt() result_list = [result] operations = [ ResOperation(rop.CALL, args, result, self), ResOperation(rop.GUARD_NO_EXCEPTION, [], None, descr=BasicFailDescr()), ResOperation(rop.FINISH, result_list, None, descr=BasicFailDescr())] operations[1].fail_args = [] loop_token = LoopToken() cpu.compile_loop(args, operations, loop_token) self.loop_token = loop_token return loop_token
def test__make_virtual(): b1, b2 = BoxInt(), BoxInt() vbox = BoxPtr() modifier = ResumeDataVirtualAdder(None, None) modifier.liveboxes_from_env = {} modifier.liveboxes = {} modifier.virtuals = [] modifier.vfieldboxes = [] modifier.make_virtual(vbox, None, ['a', 'b'], [b1, b2]) assert modifier.liveboxes == { vbox: tag(0, TAGVIRTUAL), b1: UNASSIGNED, b2: UNASSIGNED } assert len(modifier.virtuals) == 1 assert modifier.vfieldboxes == [[b1, b2]] modifier = ResumeDataVirtualAdder(None, None) modifier.liveboxes_from_env = {vbox: tag(0, TAGVIRTUAL)} modifier.liveboxes = {} modifier.virtuals = [None] modifier.vfieldboxes = [None] modifier.make_virtual(vbox, None, ['a', 'b', 'c'], [b1, b2, vbox]) assert modifier.liveboxes == { b1: UNASSIGNED, b2: UNASSIGNED, vbox: tag(0, TAGVIRTUAL) } assert len(modifier.virtuals) == 1 assert modifier.vfieldboxes == [[b1, b2, vbox]]
def get_token_for_call(self, cpu): if self.loop_token is not None: return self.loop_token args = [BoxInt()] + self.instantiate_arg_classes() if self.get_result_size(cpu.translate_support_code) == 0: result = None result_list = [] else: if self.returns_a_pointer(): result = BoxPtr() elif self.returns_a_float(): result = BoxFloat() else: result = BoxInt() result_list = [result] operations = [ ResOperation(rop.CALL, args[:], result, self), ResOperation(rop.GUARD_NO_EXCEPTION, [], None, descr=BasicFailDescr()), ResOperation(rop.FINISH, result_list, None, descr=BasicFailDescr()) ] operations[1].fail_args = [] loop_token = LoopToken() # note: the 'args' that we pass below is not the same object as the # 'args[:]' that was passed above to ResOperation, because we want # the argument to ResOperation to be non-resizable, but the argument # to compile_loop to be resizable. cpu.compile_loop(args, operations, loop_token) self.loop_token = loop_token return loop_token
def test_simple_read(): b1, b2, b3 = [BoxInt(), BoxPtr(), BoxInt()] c1, c2, c3 = [ConstInt(1), ConstInt(2), ConstInt(3)] storage = Storage() storage.rd_consts = [c1, c2, c3] numb = Numbering(None, [tag(0, TAGBOX), tag(1, TAGBOX), tag(2, TAGBOX)]) numb = Numbering(numb, [tag(1, TAGCONST), tag(2, TAGCONST)]) numb = Numbering(numb, [ tag(0, TAGBOX), tag(0, TAGCONST), NULLREF, tag(0, TAGBOX), tag(1, TAGBOX) ]) storage.rd_numb = numb storage.rd_virtuals = None b1s, b2s, b3s = [BoxInt(), BoxPtr(), BoxInt()] assert b1s != b3s reader = ResumeDataReader(storage, [b1s, b2s, b3s], MyMetaInterp()) lst = reader.consume_boxes() assert lst == [b1s, ConstInt(1), LLtypeMixin.cpu.ts.CONST_NULL, b1s, b2s] lst = reader.consume_boxes() assert lst == [ConstInt(2), ConstInt(3)] lst = reader.consume_boxes() assert lst == [b1s, b2s, b3s]
def test_execute_nonspec(): cpu = FakeCPU() descr = FakeDescr() # cases with a descr # arity == -1 argboxes = [BoxInt(321), ConstInt(123)] box = execute_nonspec(cpu, rop.CALL, argboxes, descr) assert box.args == ('call', argboxes, descr) # arity == 0 box = execute_nonspec(cpu, rop.NEW, [], descr) assert box.args == ('new', descr) # arity == 1 box1 = BoxInt(515) box = execute_nonspec(cpu, rop.ARRAYLEN_GC, [box1], descr) assert box.args == ('arraylen_gc', box1, descr) # arity == 2 box2 = BoxInt(222) box = execute_nonspec(cpu, rop.SETFIELD_GC, [box1, box2], descr) assert box.args == ('setfield_gc', box1, box2, descr) # arity == 3 box3 = BoxInt(-33) box = execute_nonspec(cpu, rop.SETARRAYITEM_GC, [box1, box2, box3], descr) assert box.args == ('setarrayitem_gc', box1, box2, box3, descr) # cases without descr # arity == 1 box = execute_nonspec(cpu, rop.INT_INVERT, [box1]) assert box.value == ~515 # arity == 2 box = execute_nonspec(cpu, rop.INT_LSHIFT, [box1, BoxInt(3)]) assert box.value == 515 << 3 # arity == 3 box = execute_nonspec(cpu, rop.STRSETITEM, [box1, box2, box3]) assert box.args == ('strsetitem', box1, box2, box3)
def test_unwrap(): S = lltype.GcStruct('S') p = lltype.malloc(S) po = lltype.cast_opaque_ptr(llmemory.GCREF, p) assert unwrap(lltype.Void, BoxInt(42)) is None assert unwrap(lltype.Signed, BoxInt(42)) == 42 assert unwrap(lltype.Char, BoxInt(42)) == chr(42) assert unwrap(lltype.Float, BoxFloat(42.5)) == 42.5 assert unwrap(lltype.Ptr(S), BoxPtr(po)) == p
def test_call_with_singlefloats(self): cpu = self.cpu if not cpu.supports_floats or not cpu.supports_singlefloats: py.test.skip('requires floats and singlefloats') import random from pypy.rlib.libffi import types from pypy.rlib.rarithmetic import r_singlefloat def func(*args): res = 0.0 for i, x in enumerate(args): res += (i + 1.1) * float(x) return res F = lltype.Float S = lltype.SingleFloat I = lltype.Signed floats = [random.random() - 0.5 for i in range(8)] singlefloats = [r_singlefloat(random.random() - 0.5) for i in range(8)] ints = [random.randrange(-99, 99) for i in range(8)] for repeat in range(100): args = [] argvalues = [] argslist = [] local_floats = list(floats) local_singlefloats = list(singlefloats) local_ints = list(ints) for i in range(8): case = random.randrange(0, 3) if case == 0: args.append(F) arg = local_floats.pop() argslist.append(boxfloat(arg)) elif case == 1: args.append(S) arg = local_singlefloats.pop() argslist.append(BoxInt(longlong.singlefloat2int(arg))) else: args.append(I) arg = local_ints.pop() argslist.append(BoxInt(arg)) argvalues.append(arg) FUNC = self.FuncType(args, F) FPTR = self.Ptr(FUNC) func_ptr = llhelper(FPTR, func) calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo.MOST_GENERAL) funcbox = self.get_funcbox(cpu, func_ptr) res = self.execute_operation(rop.CALL, [funcbox] + argslist, 'float', descr=calldescr) expected = func(*argvalues) assert abs(res.getfloat() - expected) < 0.0001
def test_execute_varargs(): cpu = FakeCPU() descr = FakeCallDescr() argboxes = [BoxInt(99999), BoxInt(321), constfloat(2.25), ConstInt(123), BoxPtr(), boxfloat(5.5)] box = execute_varargs(cpu, FakeMetaInterp(), rop.CALL, argboxes, descr) assert box.getfloat() == 42.5 assert cpu.fakecalled == (99999, descr, [321, 123], [ConstPtr.value], [longlong.getfloatstorage(2.25), longlong.getfloatstorage(5.5)])
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 get_int_tests(): for opnum, args, retvalue in (list(_int_binary_operations()) + list(_int_comparison_operations()) + list(_int_unary_operations())): yield opnum, [BoxInt(x) for x in args], retvalue if len(args) > 1: assert len(args) == 2 yield opnum, [BoxInt(args[0]), ConstInt(args[1])], retvalue yield opnum, [ConstInt(args[0]), BoxInt(args[1])], retvalue if args[0] == args[1]: commonbox = BoxInt(args[0]) yield opnum, [commonbox, commonbox], retvalue
def compile_tmp_callback(cpu, jitdriver_sd, greenboxes, redargtypes, memory_manager=None): """Make a LoopToken that corresponds to assembler code that just calls back the interpreter. Used temporarily: a fully compiled version of the code may end up replacing it. """ jitcell_token = make_jitcell_token(jitdriver_sd) nb_red_args = jitdriver_sd.num_red_args assert len(redargtypes) == nb_red_args inputargs = [] for kind in redargtypes: if kind == history.INT: box = BoxInt() elif kind == history.REF: box = BoxPtr() elif kind == history.FLOAT: box = BoxFloat() else: raise AssertionError inputargs.append(box) k = jitdriver_sd.portal_runner_adr funcbox = history.ConstInt(heaptracker.adr2int(k)) callargs = [funcbox] + greenboxes + inputargs # result_type = jitdriver_sd.result_type if result_type == history.INT: result = BoxInt() elif result_type == history.REF: result = BoxPtr() elif result_type == history.FLOAT: result = BoxFloat() elif result_type == history.VOID: result = None else: assert 0, "bad result_type" if result is not None: finishargs = [result] else: finishargs = [] # jd = jitdriver_sd faildescr = PropagateExceptionDescr() operations = [ ResOperation(rop.CALL, callargs, result, descr=jd.portal_calldescr), ResOperation(rop.GUARD_NO_EXCEPTION, [], None, descr=faildescr), ResOperation(rop.FINISH, finishargs, None, descr=jd.portal_finishtoken) ] operations[1].setfailargs([]) operations = get_deep_immutable_oplist(operations) cpu.compile_loop(inputargs, operations, jitcell_token, log=False) if memory_manager is not None: # for tests memory_manager.keep_loop_alive(jitcell_token) return jitcell_token
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 handle_fail(self, metainterp_sd): if self.no == 0: raise metainterp_sd.warmrunnerdesc.DoneWithThisFrameInt(3) if self.no == 1: raise metainterp_sd.warmrunnerdesc.ContinueRunningNormally( [BoxInt(0), BoxInt(1)]) if self.no == 3: exc = lltype.malloc(OBJECT) exc.typeptr = exc_vtable raise metainterp_sd.warmrunnerdesc.ExitFrameWithExceptionRef( metainterp_sd.cpu, lltype.cast_opaque_ptr(llmemory.GCREF, exc)) return self.no
def test_virtual_adder_make_varray(): b2s, b4s = [BoxPtr(), BoxInt(4)] c1s = ConstInt(111) storage = Storage() memo = ResumeDataLoopMemo(FakeMetaInterpStaticData()) modifier = ResumeDataVirtualAdder(storage, memo) modifier.liveboxes_from_env = {} modifier.liveboxes = {} modifier.vfieldboxes = {} class FakeOptimizer(object): class cpu: pass def new_const_item(self, descr): return None v2 = VArrayValue(FakeOptimizer(), LLtypeMixin.arraydescr, 2, b2s) v2._items = [b4s, c1s] modifier.register_virtual_fields(b2s, [b4s, c1s]) liveboxes = [] values = {b2s: v2} modifier._number_virtuals(liveboxes, values, 0) dump_storage(storage, liveboxes) storage.rd_consts = memo.consts[:] storage.rd_numb = None # resume b1t, b3t, b4t = [BoxInt(11), BoxInt(33), BoxInt(44)] newboxes = _resume_remap(liveboxes, [#b2s -- virtual b4s], b4t) # resume metainterp = MyMetaInterp() reader = ResumeDataReader(storage, newboxes, metainterp) assert len(reader.virtuals) == 1 b2t = reader._decode_box(tag(0, TAGVIRTUAL)) trace = metainterp.trace expected = [ (rop.NEW_ARRAY, [ConstInt(2)], b2t, LLtypeMixin.arraydescr), (rop.SETARRAYITEM_GC, [b2t,ConstInt(0), b4t],None, LLtypeMixin.arraydescr), (rop.SETARRAYITEM_GC, [b2t,ConstInt(1), c1s], None, LLtypeMixin.arraydescr), ] for x, y in zip(expected, trace): assert x == y # ptr = b2t.value._obj.container._as_ptr() assert lltype.typeOf(ptr) == lltype.Ptr(lltype.GcArray(lltype.Signed)) assert len(ptr) == 2 assert ptr[0] == 44 assert ptr[1] == 111
def make_guards(self, box, guards): if self.has_lower and self.lower > MININT: bound = self.lower res = BoxInt() op = ResOperation(rop.INT_GE, [box, ConstInt(bound)], res) guards.append(op) op = ResOperation(rop.GUARD_TRUE, [res], None) guards.append(op) if self.has_upper and self.upper < MAXINT: bound = self.upper res = BoxInt() op = ResOperation(rop.INT_LE, [box, ConstInt(bound)], res) guards.append(op) op = ResOperation(rop.GUARD_TRUE, [res], None) guards.append(op)
def test_getfield_setfield(self): TP = lltype.GcStruct('x', ('s', lltype.Signed), ('i', rffi.INT), ('f', lltype.Float), ('u', rffi.USHORT), ('c1', lltype.Char), ('c2', lltype.Char), ('c3', lltype.Char)) res = self.execute_operation(rop.NEW, [], 'ref', self.cpu.sizeof(TP)) ofs_s = self.cpu.fielddescrof(TP, 's') ofs_i = self.cpu.fielddescrof(TP, 'i') #ofs_f = self.cpu.fielddescrof(TP, 'f') ofs_u = self.cpu.fielddescrof(TP, 'u') ofsc1 = self.cpu.fielddescrof(TP, 'c1') ofsc2 = self.cpu.fielddescrof(TP, 'c2') ofsc3 = self.cpu.fielddescrof(TP, 'c3') self.execute_operation(rop.SETFIELD_GC, [res, ConstInt(3)], 'void', ofs_s) # XXX ConstFloat #self.execute_operation(rop.SETFIELD_GC, [res, ofs_f, 1e100], 'void') # XXX we don't support shorts (at all) #self.execute_operation(rop.SETFIELD_GC, [res, ofs_u, ConstInt(5)], 'void') s = self.execute_operation(rop.GETFIELD_GC, [res], 'int', ofs_s) assert s.value == 3 self.execute_operation(rop.SETFIELD_GC, [res, BoxInt(3)], 'void', ofs_s) s = self.execute_operation(rop.GETFIELD_GC, [res], 'int', ofs_s) assert s.value == 3 self.execute_operation(rop.SETFIELD_GC, [res, BoxInt(1234)], 'void', ofs_i) i = self.execute_operation(rop.GETFIELD_GC, [res], 'int', ofs_i) assert i.value == 1234 #u = self.execute_operation(rop.GETFIELD_GC, [res, ofs_u], 'int') #assert u.value == 5 self.execute_operation(rop.SETFIELD_GC, [res, ConstInt(1)], 'void', ofsc1) self.execute_operation(rop.SETFIELD_GC, [res, ConstInt(3)], 'void', ofsc3) self.execute_operation(rop.SETFIELD_GC, [res, ConstInt(2)], 'void', ofsc2) c = self.execute_operation(rop.GETFIELD_GC, [res], 'int', ofsc1) assert c.value == 1 c = self.execute_operation(rop.GETFIELD_GC, [res], 'int', ofsc2) assert c.value == 2 c = self.execute_operation(rop.GETFIELD_GC, [res], 'int', ofsc3) assert c.value == 3
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 do_strgetitem(self, stringbox, indexbox): basesize, itemsize, ofs_length = symbolic.get_array_token( rstr.STR, self.translate_support_code) gcref = stringbox.getref_base() i = indexbox.getint() v = rffi.cast(rffi.CArrayPtr(lltype.Char), gcref)[basesize + i] return BoxInt(ord(v))
def do_strlen(self, stringbox): basesize, itemsize, ofs_length = symbolic.get_array_token( TP, self.translate_support_code) gcref = stringbox.getref_base() v = rffi.cast(rffi.CArrayPtr(lltype.Signed), gcref)[ofs_length / WORD] return BoxInt(v)
def do_getarrayitem_gc(self, arraybox, indexbox, arraydescr): itemindex = indexbox.getint() gcref = arraybox.getref_base() ofs, size, ptr, float = self.unpack_arraydescr(arraydescr) # --- start of GC unsafe code (no GC operation!) --- items = rffi.ptradd(rffi.cast(rffi.CCHARP, gcref), ofs) # if ptr: items = rffi.cast(rffi.CArrayPtr(lltype.Signed), items) pval = self._cast_int_to_gcref(items[itemindex]) # --- end of GC unsafe code --- return BoxPtr(pval) # if float: items = rffi.cast(rffi.CArrayPtr(lltype.Float), items) fval = items[itemindex] # --- end of GC unsafe code --- return BoxFloat(fval) # for TYPE, itemsize in unroll_basic_sizes: if size == itemsize: items = rffi.cast(rffi.CArrayPtr(TYPE), items) val = items[itemindex] # --- end of GC unsafe code --- return BoxInt(rffi.cast(lltype.Signed, val)) else: raise NotImplementedError("size = %d" % size)
def __init__(self, cpu, builder_factory, r, startvars=None): self.cpu = cpu if startvars is None: startvars = [] if cpu.supports_floats: # pick up a single threshold for the whole 'inputargs', so # that some loops have no or mostly no BoxFloat while others # have a lot of them k = r.random() # but make sure there is at least one BoxInt at_least_once = r.randrange(0, pytest.config.option.n_vars) else: k = -1 at_least_once = 0 for i in range(pytest.config.option.n_vars): if r.random() < k and i != at_least_once: startvars.append(BoxFloat(r.random_float_storage())) else: startvars.append(BoxInt(r.random_integer())) allow_delay = True else: allow_delay = False assert len(dict.fromkeys(startvars)) == len(startvars) self.startvars = startvars self.prebuilt_ptr_consts = [] self.r = r self.build_random_loop(cpu, builder_factory, r, startvars, allow_delay)
def box_for_var(self, elem): try: return self._cache[self.type_system, elem] except KeyError: pass if elem.startswith('i'): # integer box = BoxInt() _box_counter_more_than(elem[1:]) elif elem.startswith('f'): box = BoxFloat() _box_counter_more_than(elem[1:]) elif elem.startswith('p'): # pointer ts = getattr(self.cpu, 'ts', llhelper) box = ts.BoxRef() _box_counter_more_than(elem[1:]) else: for prefix, boxclass in self.boxkinds.iteritems(): if elem.startswith(prefix): box = boxclass() break else: raise ParseError("Unknown variable type: %s" % elem) self._cache[self.type_system, elem] = box box._str = elem return box
def new_box(self, fieldofs): if fieldofs.is_pointer_field(): return self.new_ptr_box() elif fieldofs.is_float_field(): return BoxFloat() else: return BoxInt()
def new_box_item(self, arraydescr): if arraydescr.is_array_of_pointers(): return self.new_ptr_box() elif arraydescr.is_array_of_floats(): return BoxFloat() else: return BoxInt()
def test_execute(): cpu = FakeCPU() descr = FakeDescr() box = execute(cpu, rop.INT_ADD, None, BoxInt(40), ConstInt(2)) assert box.value == 42 box = execute(cpu, rop.NEW, descr) assert box.args == ('new', descr)