def test_nullity_with_guard(self): allops = [rop.INT_IS_TRUE] guards = [rop.GUARD_TRUE, rop.GUARD_FALSE] p = lltype.cast_opaque_ptr(llmemory.GCREF, lltype.malloc(lltype.GcStruct('x'))) nullptr = lltype.nullptr(llmemory.GCREF.TO) f = InputArgInt() for op in allops: for guard in guards: if op == rop.INT_IS_TRUE: bp = InputArgInt(1) n = InputArgInt(0) else: bp = InputArgRef(p) n = InputArgRef(nullptr) for b in (bp, n): i1 = ResOperation(rop.SAME_AS_I, [ConstInt(1)]) f = ResOperation(op, [b]) ops = [ i1, f, ResOperation(guard, [f], descr=BasicFailDescr()), ResOperation(rop.FINISH, [ConstInt(0)], descr=BasicFinalDescr()), ] ops[-2].setfailargs([i1]) looptoken = JitCellToken() self.cpu.compile_loop([b], ops, looptoken) deadframe = self.cpu.execute_token(looptoken, b.getint()) result = self.cpu.get_int_value(deadframe, 0) if guard == rop.GUARD_FALSE: assert result == execute(self.cpu, None, op, None, b) else: assert result != execute(self.cpu, None, op, None, b)
def test_known_classes(): cls = FakeClass() box1 = InputArgRef() box1.set_forwarded(InstancePtrInfo(known_class=cls)) box2 = InputArgRef() box3 = InputArgRef() optimizer = FakeOptimizer() numb_state = NumberingState(4) numb_state.append_int(1) # size of resume block liveboxes = [InputArgInt(), box2, box1, box3] serialize_optimizer_knowledge(optimizer, numb_state, liveboxes, {}, None) assert unpack_numbering( numb_state.create_numbering()) == [1, 0b010000, 0, 0, 0] rbox1 = InputArgRef() rbox2 = InputArgRef() rbox3 = InputArgRef() after_optimizer = FakeOptimizer(cpu=FakeCPU({rbox1: cls})) deserialize_optimizer_knowledge(after_optimizer, FakeStorage(numb_state.create_numbering()), [InputArgInt(), rbox2, rbox1, rbox3], liveboxes) assert box1 in after_optimizer.constant_classes assert box2 not in after_optimizer.constant_classes assert box3 not in after_optimizer.constant_classes
def test_frame_manager_basic(self): b0, b1 = newboxes(0, 1) fm = TFrameManager() loc0 = fm.loc(b0) assert fm.get_loc_index(loc0) == 0 # assert fm.get(b1) is None loc1 = fm.loc(b1) assert fm.get_loc_index(loc1) == 1 assert fm.get(b1) == loc1 # loc0b = fm.loc(b0) assert loc0b == loc0 # fm.loc(InputArgInt()) assert fm.get_frame_depth() == 3 # f0 = InputArgFloat() locf0 = fm.loc(f0) # can't be odd assert fm.get_loc_index(locf0) == 4 assert fm.get_frame_depth() == 6 # f1 = InputArgFloat() locf1 = fm.loc(f1) assert fm.get_loc_index(locf1) == 6 assert fm.get_frame_depth() == 8 fm.mark_as_free(b1) assert fm.freelist b2 = InputArgInt() fm.loc(b2) # should be in the same spot as b1 before assert fm.get(b1) is None assert fm.get(b2) == loc1 fm.mark_as_free(b0) p0 = InputArgRef() ploc = fm.loc(p0) assert fm.get_loc_index(ploc) == 0 assert fm.get_frame_depth() == 8 assert ploc != loc1 p1 = InputArgRef() p1loc = fm.loc(p1) assert fm.get_loc_index(p1loc) == 3 assert fm.get_frame_depth() == 8 fm.mark_as_free(p0) p2 = InputArgRef() p2loc = fm.loc(p2) assert p2loc == ploc assert len(fm.freelist) == 0 fm.mark_as_free(b2) f3 = InputArgFloat() fm.mark_as_free(p2) floc = fm.loc(f3) assert fm.get_loc_index(floc) == 0 for box in fm.bindings.keys(): fm.mark_as_free(box)
def test_stringitems(self): from rpython.rtyper.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, [InputArgRef(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, [InputArgRef(res), InputArgInt(2), ConstInt(ord('z'))], 'void') assert resbuf[ofs + ofs_items + 2] == 'z' r = self.execute_operation(rop.STRGETITEM, [InputArgRef(res), InputArgInt(2)], 'int') assert r == ord('z')
def get_runtime_item(self, box, descr, i): array = box.getref_base() if descr.is_array_of_pointers(): return InputArgRef(self.cpu.bh_getarrayitem_gc_r(array, i, descr)) elif descr.is_array_of_floats(): return InputArgFloat(self.cpu.bh_getarrayitem_gc_f(array, i, descr)) else: return InputArgInt(self.cpu.bh_getarrayitem_gc_i(array, i, descr))
def get_runtime_field(self, box, descr): struct = box.getref_base() if descr.is_pointer_field(): return InputArgRef(self.cpu.bh_getfield_gc_r(struct, descr)) elif descr.is_float_field(): return InputArgFloat(self.cpu.bh_getfield_gc_f(struct, descr)) else: return InputArgInt(self.cpu.bh_getfield_gc_i(struct, descr))
def test_virtual(self): loop = """ [p1, p2, i3] p0 = new_with_vtable(descr=simpledescr) setfield_gc(p0, i3, descr=simplevalue) jump(p0, p0, i3) """ es, loop, preamble = self.optimize(loop) vs = es.virtual_state assert vs.state[0] is vs.state[1] assert isinstance(vs.state[0], VirtualStateInfo) assert isinstance(vs.state[0].fieldstate[0], NotVirtualStateInfo) assert vs.state[0].fieldstate[0].level == LEVEL_UNKNOWN assert vs.numnotvirtuals == 1 p = InputArgRef() i = InputArgInt() ptrinfo = info.StructPtrInfo(self.nodesize, is_virtual=True) ptrinfo._fields = [i] p.set_forwarded(ptrinfo) vs.make_inputargs([p, p, i], FakeOptimizer())
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, [InputArgRef(res), ConstInt(2), InputArgInt(38)], 'void', descr) assert resbuf[itemsofs / WORD + 2] == 38 self.execute_operation( rop.SETARRAYITEM_GC, [InputArgRef(res), InputArgInt(3), InputArgInt(42)], 'void', descr) assert resbuf[itemsofs / WORD + 3] == 42 r = self.execute_operation( rop.GETARRAYITEM_GC_I, [InputArgRef(res), ConstInt(2)], 'int', descr) assert r == 38 r = self.execute_operation( rop.GETARRAYITEM_GC_I, [ConstPtr(res), InputArgInt(2)], 'int', descr) assert r == 38 r = self.execute_operation(rop.GETARRAYITEM_GC_I, [ConstPtr(res), ConstInt(2)], 'int', descr) assert r == 38 r = self.execute_operation( rop.GETARRAYITEM_GC_I, [InputArgRef(res), InputArgInt(2)], 'int', descr) assert r == 38 r = self.execute_operation( rop.GETARRAYITEM_GC_I, [InputArgRef(res), InputArgInt(3)], 'int', descr) assert r == 42
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) # # record the target of a temporary callback to the interpreter jl.tmp_callback(jitcell_token) # nb_red_args = jitdriver_sd.num_red_args assert len(redargtypes) == nb_red_args inputargs = [] for kind in redargtypes: if kind == history.INT: box = InputArgInt() elif kind == history.REF: box = InputArgRef() elif kind == history.FLOAT: box = InputArgFloat() else: raise AssertionError inputargs.append(box) k = jitdriver_sd.portal_runner_adr funcbox = history.ConstInt(adr2int(k)) callargs = [funcbox] + greenboxes + inputargs # jd = jitdriver_sd opnum = OpHelpers.call_for_descr(jd.portal_calldescr) call_op = ResOperation(opnum, callargs, descr=jd.portal_calldescr) if call_op.type != 'v' is not None: finishargs = [call_op] else: finishargs = [] # faildescr = jitdriver_sd.propagate_exc_descr operations = [ call_op, ResOperation(rop.GUARD_NO_EXCEPTION, [], descr=faildescr), ResOperation(rop.FINISH, finishargs, 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 pack_into_boxes(self, jump_op, jump_values): assert jump_op.getopnum() == rop.JUMP r = [] if jump_values is not None: assert len(jump_values) == len(jump_op.getarglist()) for i, v in enumerate(jump_values): if v is not None: r.append(InputArgRef(v)) else: r.append(None) else: for i, box in enumerate(jump_op.getarglist()): if box.type == 'r' and not box.is_constant(): # NOTE: we arbitrarily set the box contents to a NODE2 # object here. If you need something different, you # need to pass a 'jump_values' argument to e.g. # optimize_loop() r.append(InputArgRef(self.nodefulladdr)) else: r.append(None) return r
def test_execute_nonspec(): cpu = FakeCPU() descr = FakeDescr() # cases with a descr # arity == -1 argboxes = [InputArgInt(321), ConstInt(123)] box = _execute_arglist(cpu, FakeMetaInterp(), rop.CALL_F, argboxes, FakeCallDescr()) assert longlong.getrealfloat(box) == 42.5 # arity == 0 box = _execute_arglist(cpu, None, rop.NEW, [], descr) assert box.fakeargs == ('new', descr) # arity == 1 box1 = InputArgRef() box = _execute_arglist(cpu, None, rop.ARRAYLEN_GC, [box1], descr) assert box == 55 # arity == 2 box2 = boxfloat(222.2) fielddescr = FakeFieldDescr() _execute_arglist(cpu, None, rop.SETFIELD_GC, [box1, box2], fielddescr) assert cpu.fakesetfield == (box1.getref_base(), box2.getfloatstorage(), fielddescr) # arity == 3 box3 = InputArgInt(33) arraydescr = FakeArrayDescr() _execute_arglist(cpu, None, rop.SETARRAYITEM_GC, [box1, box3, box2], arraydescr) assert cpu.fakesetarrayitem == (box1.getref_base(), box3.getint(), box2.getfloatstorage(), arraydescr) # cases without descr # arity == 1 box = _execute_arglist(cpu, None, rop.INT_INVERT, [box3]) assert box == ~33 # arity == 2 box = _execute_arglist(cpu, None, rop.INT_LSHIFT, [box3, InputArgInt(3)]) assert box == 33 << 3 # arity == 3 _execute_arglist(cpu, None, rop.STRSETITEM, [box1, InputArgInt(3), box3]) assert cpu.fakestrsetitem == (box1.getref_base(), 3, box3.getint())
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 = InputArgRef(lltype.cast_opaque_ptr(llmemory.GCREF, u)) r = self.execute_operation(rop.UNICODEGETITEM, [b, ConstInt(2)], 'int') assert r == 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 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 = InputArgRef(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_I, [res], 'int', ofs_s) assert s == 3 self.execute_operation(rop.SETFIELD_GC, [res, InputArgInt(3)], 'void', ofs_s) s = self.execute_operation(rop.GETFIELD_GC_I, [res], 'int', ofs_s) assert s == 3 self.execute_operation(rop.SETFIELD_GC, [res, InputArgInt(1234)], 'void', ofs_i) i = self.execute_operation(rop.GETFIELD_GC_I, [res], 'int', ofs_i) assert i == 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_I, [res], 'int', ofsc1) assert c == 1 c = self.execute_operation(rop.GETFIELD_GC_I, [res], 'int', ofsc2) assert c == 2 c = self.execute_operation(rop.GETFIELD_GC_I, [res], 'int', ofsc3) assert c == 3
def newinputarg(self, elem): if elem.startswith('i'): v = InputArgInt(0) elif elem.startswith('f'): v = InputArgFloat.fromfloat(0.0) elif elem.startswith('v'): v = InputArgVector() elem = self.update_vector(v, elem) else: from rpython.rtyper.lltypesystem import lltype, llmemory assert elem.startswith('p') v = InputArgRef(lltype.nullptr(llmemory.GCREF.TO)) # ensure that the variable gets the proper naming self.update_memo(v, elem) self.vars[elem] = v return v
def test_gen_write_barrier(self): gc_ll_descr = self.gc_ll_descr llop1 = self.llop1 # rewriter = gc.GcRewriterAssembler(gc_ll_descr, None) newops = rewriter._newops v_base = InputArgRef() rewriter.gen_write_barrier(v_base) assert llop1.record == [] assert len(newops) == 1 assert newops[0].getopnum() == rop.COND_CALL_GC_WB assert newops[0].getarg(0) == v_base wbdescr = newops[0].getdescr() assert is_valid_int(wbdescr.jit_wb_if_flag) assert is_valid_int(wbdescr.jit_wb_if_flag_byteofs) assert is_valid_int(wbdescr.jit_wb_if_flag_singlebyte)
def test_execute_varargs(): cpu = FakeCPU() descr = FakeCallDescr() argboxes = [ InputArgInt(99999), InputArgInt(321), constfloat(2.25), ConstInt(123), InputArgRef(), boxfloat(5.5) ] box = execute_varargs(cpu, FakeMetaInterp(), rop.CALL_F, argboxes, descr) assert longlong.getrealfloat(box) == 42.5 assert cpu.fakecalled == (99999, [321, 123], [ConstPtr.value], [ longlong.getfloatstorage(2.25), longlong.getfloatstorage(5.5) ], descr)
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) res = InputArgRef(res) assert resbuf[ofs] == chr(10) for i in range(10): self.execute_operation( rop.SETARRAYITEM_GC, [res, ConstInt(i), InputArgInt(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_I, [res, ConstInt(i)], 'int', descr) assert r == i
def newrefboxes(count): return [InputArgRef() for _ in range(count)]