def dispatcher(self, shape, index, argtypes, resulttype): key = shape, index, tuple(argtypes), resulttype if key in self._dispatch_cache: return self._dispatch_cache[key] from pypy.translator.unsimplify import varoftype from pypy.objspace.flow.model import FunctionGraph, Link, Block, SpaceOperation inputargs = [varoftype(t) for t in [Char] + argtypes] startblock = Block(inputargs) startblock.exitswitch = inputargs[0] graph = FunctionGraph("dispatcher", startblock, varoftype(resulttype)) row_of_graphs = self.callfamily.calltables[shape][index] links = [] descs = list(self.s_pbc.descriptions) if self.s_pbc.can_be_None: descs.insert(0, None) for desc in descs: if desc is None: continue args_v = [varoftype(t) for t in argtypes] b = Block(args_v) llfn = self.rtyper.getcallable(row_of_graphs[desc]) v_fn = inputconst(typeOf(llfn), llfn) v_result = varoftype(resulttype) b.operations.append( SpaceOperation("direct_call", [v_fn] + args_v, v_result)) b.closeblock(Link([v_result], graph.returnblock)) i = self.descriptions.index(desc) links.append(Link(inputargs[1:], b, chr(i))) links[-1].llexitcase = chr(i) startblock.closeblock(*links) self.rtyper.annotator.translator.graphs.append(graph) ll_ret = self.rtyper.type_system.getcallable(graph) #FTYPE = FuncType c_ret = self._dispatch_cache[key] = inputconst(typeOf(ll_ret), ll_ret) return c_ret
def test_getfield(): # XXX a more compact encoding would be possible, something along # the lines of getfield_gc_r %r0, $offset, %r1 # which would not need a Descr at all. S1 = lltype.Struct("S1") S2 = lltype.GcStruct("S2") S = lltype.GcStruct( "S", ("int", lltype.Signed), ("ps1", lltype.Ptr(S1)), ("ps2", lltype.Ptr(S2)), ("flt", lltype.Float), ("boo", lltype.Bool), ("chr", lltype.Char), ("unc", lltype.UniChar), ) for name, suffix in [ ("int", "i"), ("ps1", "i"), ("ps2", "r"), ("flt", "f"), ("boo", "i"), ("chr", "i"), ("unc", "i"), ]: v_parent = varoftype(lltype.Ptr(S)) c_name = Constant(name, lltype.Void) v_result = varoftype(getattr(S, name)) op = SpaceOperation("getfield", [v_parent, c_name], v_result) op1 = Transformer(FakeCPU()).rewrite_operation(op) assert op1.opname == "getfield_gc_" + suffix fielddescr = ("fielddescr", S, name) assert op1.args == [v_parent, fielddescr] assert op1.result == v_result
def test_is_true(self): for opname, T in [('llong_is_true', lltype.SignedLongLong), ('ullong_is_true', lltype.UnsignedLongLong)]: v = varoftype(T) v_result = varoftype(lltype.Bool) op = SpaceOperation(opname, [v], v_result) tr = Transformer(FakeCPU(), FakeBuiltinCallControl()) oplist = tr.rewrite_operation(op) assert len(oplist) == 2 assert oplist[0].opname == 'residual_call_irf_f' assert oplist[0].args[0].value == 'llong_from_int' assert oplist[0].args[1] == 'calldescr-84' assert list(oplist[0].args[2]) == [const(0)] assert list(oplist[0].args[3]) == [] assert list(oplist[0].args[4]) == [] v_x = oplist[0].result assert isinstance(v_x, Variable) assert v_x.concretetype is T assert oplist[1].opname == 'residual_call_irf_i' assert oplist[1].args[0].value == 'llong_ne' assert oplist[1].args[1] == 'calldescr-76' assert list(oplist[1].args[2]) == [] assert list(oplist[1].args[3]) == [] assert list(oplist[1].args[4]) == [v, v_x] assert oplist[1].result == v_result
def test_setfield(): # XXX a more compact encoding would be possible; see test_getfield() S1 = lltype.Struct("S1") S2 = lltype.GcStruct("S2") S = lltype.GcStruct( "S", ("int", lltype.Signed), ("ps1", lltype.Ptr(S1)), ("ps2", lltype.Ptr(S2)), ("flt", lltype.Float), ("boo", lltype.Bool), ("chr", lltype.Char), ("unc", lltype.UniChar), ) for name, suffix in [ ("int", "i"), ("ps1", "i"), ("ps2", "r"), ("flt", "f"), ("boo", "i"), ("chr", "i"), ("unc", "i"), ]: v_parent = varoftype(lltype.Ptr(S)) c_name = Constant(name, lltype.Void) v_newvalue = varoftype(getattr(S, name)) op = SpaceOperation("setfield", [v_parent, c_name, v_newvalue], varoftype(lltype.Void)) op1 = Transformer(FakeCPU()).rewrite_operation(op) assert op1.opname == "setfield_gc_" + suffix fielddescr = ("fielddescr", S, name) assert op1.args == [v_parent, fielddescr, v_newvalue] assert op1.result is None
def test_graphs_from_no_target(): cc = CallControl() F = lltype.FuncType([], lltype.Signed) v = varoftype(lltype.Signed) op = SpaceOperation("indirect_call", [varoftype(lltype.Ptr(F)), Constant(None, lltype.Void)], v) lst = cc.graphs_from(op, {}.__contains__) assert lst is None
def test_unicode_concat(): # test that the oopspec is present and correctly transformed PSTR = lltype.Ptr(rstr.UNICODE) FUNC = lltype.FuncType([PSTR, PSTR], PSTR) func = lltype.functionptr(FUNC, 'll_strconcat', _callable=rstr.LLHelpers.ll_strconcat) v1 = varoftype(PSTR) v2 = varoftype(PSTR) v3 = varoftype(PSTR) op = SpaceOperation('direct_call', [const(func), v1, v2], v3) cc = FakeBuiltinCallControl() tr = Transformer(FakeCPU(), cc) op1 = tr.rewrite_operation(op) assert op1.opname == 'residual_call_r_r' assert op1.args[0].value == func assert op1.args[1] == 'calldescr-%d' % effectinfo.EffectInfo.OS_UNI_CONCAT assert op1.args[2] == ListOfKind('ref', [v1, v2]) assert op1.result == v3 # # check the callinfo_for_oopspec got = cc.callinfocollection.seen[0] assert got[0] == effectinfo.EffectInfo.OS_UNI_CONCAT assert got[1] == op1.args[1] # the calldescr assert heaptracker.int2adr(got[2]) == llmemory.cast_ptr_to_adr(func)
def test_getfield(): # XXX a more compact encoding would be possible, something along # the lines of getfield_gc_r %r0, $offset, %r1 # which would not need a Descr at all. S1 = lltype.Struct('S1') S2 = lltype.GcStruct('S2') S = lltype.GcStruct('S', ('int', lltype.Signed), ('ps1', lltype.Ptr(S1)), ('ps2', lltype.Ptr(S2)), ('flt', lltype.Float), ('boo', lltype.Bool), ('chr', lltype.Char), ('unc', lltype.UniChar)) for name, suffix in [('int', 'i'), ('ps1', 'i'), ('ps2', 'r'), ('flt', 'f'), ('boo', 'i'), ('chr', 'i'), ('unc', 'i')]: v_parent = varoftype(lltype.Ptr(S)) c_name = Constant(name, lltype.Void) v_result = varoftype(getattr(S, name)) op = SpaceOperation('getfield', [v_parent, c_name], v_result) op1 = Transformer(FakeCPU()).rewrite_operation(op) assert op1.opname == 'getfield_gc_' + suffix fielddescr = ('fielddescr', S, name) assert op1.args == [v_parent, fielddescr] assert op1.result == v_result
def test_nongc_ptr_eq(): v1 = varoftype(rclass.NONGCOBJECTPTR) v2 = varoftype(rclass.NONGCOBJECTPTR) v3 = varoftype(lltype.Bool) c0 = const(lltype.nullptr(rclass.NONGCOBJECT)) # for opname, reducedname in [("ptr_eq", "int_is_zero"), ("ptr_ne", "int_is_true")]: op = SpaceOperation(opname, [v1, v2], v3) op1 = Transformer().rewrite_operation(op) assert op1.opname == opname.replace("ptr_", "int_") assert op1.args == [v1, v2] # op = SpaceOperation(opname, [v1, c0], v3) op1 = Transformer().rewrite_operation(op) assert op1.opname == reducedname assert op1.args == [v1] # op = SpaceOperation(opname, [c0, v2], v3) op1 = Transformer().rewrite_operation(op) assert op1.opname == reducedname assert op1.args == [v2] # op = SpaceOperation("ptr_iszero", [v1], v3) op1 = Transformer().rewrite_operation(op) assert op1.opname == "int_is_zero" assert op1.args == [v1] # op = SpaceOperation("ptr_nonzero", [v1], v3) op1 = Transformer().rewrite_operation(op) assert op1.opname == "int_is_true" assert op1.args == [v1]
def test_symmetric(): ops = { 'int_add': 'int_add', 'int_or': 'int_or', 'int_gt': ('int_gt', 'int_lt'), 'uint_eq': 'int_eq', 'uint_le': ('uint_le', 'uint_ge'), 'char_ne': 'int_ne', 'char_lt': ('int_lt', 'int_gt'), 'uint_xor': 'int_xor', 'float_mul': 'float_mul', 'float_gt': ('float_gt', 'float_lt'), } v3 = varoftype(lltype.Signed) for v1 in [varoftype(lltype.Signed), const(42)]: for v2 in [varoftype(lltype.Signed), const(43)]: for name1, name2 in ops.items(): op = SpaceOperation(name1, [v1, v2], v3) op1 = Transformer(FakeCPU()).rewrite_operation(op) if isinstance(name2, str): name2 = name2, name2 if isinstance(v1, Constant) and isinstance(v2, Variable): assert op1.args == [v2, v1] assert op1.result == v3 assert op1.opname == name2[1] else: assert op1.args == [v1, v2] assert op1.result == v3 assert op1.opname == name2[0]
def test_setfield(): # XXX a more compact encoding would be possible; see test_getfield() S1 = lltype.Struct('S1') S2 = lltype.GcStruct('S2') S = lltype.GcStruct('S', ('int', lltype.Signed), ('ps1', lltype.Ptr(S1)), ('ps2', lltype.Ptr(S2)), ('flt', lltype.Float), ('boo', lltype.Bool), ('chr', lltype.Char), ('unc', lltype.UniChar)) for name, suffix in [('int', 'i'), ('ps1', 'i'), ('ps2', 'r'), ('flt', 'f'), ('boo', 'i'), ('chr', 'i'), ('unc', 'i')]: v_parent = varoftype(lltype.Ptr(S)) c_name = Constant(name, lltype.Void) v_newvalue = varoftype(getattr(S, name)) op = SpaceOperation('setfield', [v_parent, c_name, v_newvalue], varoftype(lltype.Void)) op1 = Transformer(FakeCPU()).rewrite_operation(op) assert op1.opname == 'setfield_gc_' + suffix fielddescr = ('fielddescr', S, name) assert op1.args == [v_parent, fielddescr, v_newvalue] assert op1.result is None
def test_symmetric(): ops = {'int_add': 'int_add', 'int_or': 'int_or', 'int_gt': ('int_gt', 'int_lt'), 'uint_eq': 'int_eq', 'uint_le': ('uint_le', 'uint_ge'), 'char_ne': 'int_ne', 'char_lt': ('int_lt', 'int_gt'), 'uint_xor': 'int_xor', 'float_mul': 'float_mul', 'float_gt': ('float_gt', 'float_lt'), } v3 = varoftype(lltype.Signed) for v1 in [varoftype(lltype.Signed), const(42)]: for v2 in [varoftype(lltype.Signed), const(43)]: for name1, name2 in ops.items(): op = SpaceOperation(name1, [v1, v2], v3) op1 = Transformer(FakeCPU()).rewrite_operation(op) if isinstance(name2, str): name2 = name2, name2 if isinstance(v1, Constant) and isinstance(v2, Variable): assert op1.args == [v2, v1] assert op1.result == v3 assert op1.opname == name2[1] else: assert op1.args == [v1, v2] assert op1.result == v3 assert op1.opname == name2[0]
def test_is_true(self): for opname, T in [('llong_is_true', lltype.SignedLongLong), ('ullong_is_true', lltype.UnsignedLongLong)]: v = varoftype(T) v_result = varoftype(lltype.Bool) op = SpaceOperation(opname, [v], v_result) tr = Transformer(FakeCPU(), FakeBuiltinCallControl()) oplist = tr.rewrite_operation(op) assert len(oplist) == 2 assert oplist[0].opname == 'residual_call_irf_f' assert oplist[0].args[0].value == opname.split( '_')[0] + '_from_int' assert oplist[0].args[1] == 'calldescr-84' assert list(oplist[0].args[2]) == [const(0)] assert list(oplist[0].args[3]) == [] assert list(oplist[0].args[4]) == [] v_x = oplist[0].result assert isinstance(v_x, Variable) assert v_x.concretetype is T assert oplist[1].opname == 'residual_call_irf_i' assert oplist[1].args[0].value == 'llong_ne' assert oplist[1].args[1] == 'calldescr-76' assert list(oplist[1].args[2]) == [] assert list(oplist[1].args[3]) == [] assert list(oplist[1].args[4]) == [v, v_x] assert oplist[1].result == v_result
def test_casts(self): self.do_check('cast_int_to_longlong', EffectInfo.OS_LLONG_FROM_INT, [lltype.Signed], lltype.SignedLongLong) self.do_check('cast_uint_to_longlong', EffectInfo.OS_LLONG_FROM_UINT, [lltype.Unsigned], lltype.SignedLongLong) self.do_check('truncate_longlong_to_int', EffectInfo.OS_LLONG_TO_INT, [lltype.SignedLongLong], lltype.Signed) self.do_check('cast_float_to_longlong', EffectInfo.OS_LLONG_FROM_FLOAT, [lltype.Float], lltype.SignedLongLong) self.do_check('cast_longlong_to_float', EffectInfo.OS_LLONG_TO_FLOAT, [lltype.SignedLongLong], lltype.Float) for T1 in [lltype.SignedLongLong, lltype.UnsignedLongLong]: for T2 in [lltype.Signed, lltype.Unsigned]: self.do_check('cast_primitive', EffectInfo.OS_LLONG_TO_INT, [T1], T2) self.do_check('force_cast', EffectInfo.OS_LLONG_TO_INT, [T1], T2) if T2 == lltype.Signed: expected = EffectInfo.OS_LLONG_FROM_INT else: expected = EffectInfo.OS_LLONG_FROM_UINT self.do_check('cast_primitive', expected, [T2], T1) self.do_check('force_cast', expected, [T2], T1) # for T1 in [lltype.SignedLongLong, lltype.UnsignedLongLong]: for T2 in [lltype.SignedLongLong, lltype.UnsignedLongLong]: vlist = [varoftype(T1)] v_result = varoftype(T2) op = SpaceOperation('force_cast', vlist, v_result) tr = Transformer(FakeCPU(), FakeBuiltinCallControl()) op1 = tr.rewrite_operation(op) assert op1 is None
def test_no_gcstruct_nesting_outside_of_OBJECT(): PARENT = lltype.GcStruct("parent") STRUCT = lltype.GcStruct("struct", ("parent", PARENT), ("x", lltype.Signed)) v_x = varoftype(lltype.Ptr(STRUCT)) op = SpaceOperation("getfield", [v_x, Constant("x", lltype.Void)], varoftype(lltype.Signed)) tr = Transformer(None, None) raises(NotImplementedError, tr.rewrite_operation, op)
def test_resizable_getitem(): builtin_test('list.getitem/NONNEG', [varoftype(VARLIST), varoftype(lltype.Signed)], lltype.Void, "") builtin_test('list.getitem/NEG', [varoftype(VARLIST), varoftype(lltype.Signed)], lltype.Void, "")
def test_symmetric(): ops = { "int_add": "int_add", "int_or": "int_or", "int_gt": ("int_gt", "int_lt"), "uint_eq": "int_eq", "uint_le": ("uint_le", "uint_ge"), "char_ne": "int_ne", "char_lt": ("int_lt", "int_gt"), "uint_xor": "int_xor", "float_mul": "float_mul", "float_gt": ("float_gt", "float_lt"), } v3 = varoftype(lltype.Signed) for v1 in [varoftype(lltype.Signed), const(42)]: for v2 in [varoftype(lltype.Signed), const(43)]: for name1, name2 in ops.items(): op = SpaceOperation(name1, [v1, v2], v3) op1 = Transformer(FakeCPU()).rewrite_operation(op) if isinstance(name2, str): name2 = name2, name2 if isinstance(v1, Constant) and isinstance(v2, Variable): assert op1.args == [v2, v1] assert op1.result == v3 assert op1.opname == name2[1] else: assert op1.args == [v1, v2] assert op1.result == v3 assert op1.opname == name2[0]
def test_nongc_ptr_eq(): v1 = varoftype(rclass.NONGCOBJECTPTR) v2 = varoftype(rclass.NONGCOBJECTPTR) v3 = varoftype(lltype.Bool) c0 = const(lltype.nullptr(rclass.NONGCOBJECT)) # for opname, reducedname in [('ptr_eq', 'int_is_zero'), ('ptr_ne', 'int_is_true')]: op = SpaceOperation(opname, [v1, v2], v3) op1 = Transformer().rewrite_operation(op) assert op1.opname == opname.replace('ptr_', 'int_') assert op1.args == [v1, v2] # op = SpaceOperation(opname, [v1, c0], v3) op1 = Transformer().rewrite_operation(op) assert op1.opname == reducedname assert op1.args == [v1] # op = SpaceOperation(opname, [c0, v2], v3) op1 = Transformer().rewrite_operation(op) assert op1.opname == reducedname assert op1.args == [v2] # op = SpaceOperation('ptr_iszero', [v1], v3) op1 = Transformer().rewrite_operation(op) assert op1.opname == 'int_is_zero' assert op1.args == [v1] # op = SpaceOperation('ptr_nonzero', [v1], v3) op1 = Transformer().rewrite_operation(op) assert op1.opname == 'int_is_true' assert op1.args == [v1]
def do_check(self, opname, oopspecindex, ARGS, RESULT): vlist = [varoftype(ARG) for ARG in ARGS] v_result = varoftype(RESULT) op = SpaceOperation(opname, vlist, v_result) tr = Transformer(FakeCPU(), FakeBuiltinCallControl()) op1 = tr.rewrite_operation(op) if isinstance(op1, list): [op1] = op1 # def is_llf(TYPE): return (TYPE == lltype.SignedLongLong or TYPE == lltype.UnsignedLongLong or TYPE == lltype.Float) if is_llf(RESULT): assert op1.opname == 'residual_call_irf_f' else: assert op1.opname == 'residual_call_irf_i' gotindex = getattr(EffectInfo, 'OS_' + op1.args[0].value.upper()) assert gotindex == oopspecindex assert op1.args[1] == 'calldescr-%d' % oopspecindex assert list(op1.args[2]) == [v for v in vlist if not is_llf(v.concretetype)] assert list(op1.args[3]) == [] assert list(op1.args[4]) == [v for v in vlist if is_llf(v.concretetype)] assert op1.result == v_result
def test_fixed_getitem_foldable(): builtin_test('list.getitem_foldable/NONNEG', [varoftype(FIXEDLIST), varoftype(lltype.Signed)], lltype.Void, "") builtin_test('list.getitem_foldable/NEG', [varoftype(FIXEDLIST), varoftype(lltype.Signed)], lltype.Void, "")
def do_check(self, opname, oopspecindex, ARGS, RESULT): vlist = [varoftype(ARG) for ARG in ARGS] v_result = varoftype(RESULT) op = SpaceOperation(opname, vlist, v_result) tr = Transformer(FakeCPU(), FakeBuiltinCallControl()) op1 = tr.rewrite_operation(op) if isinstance(op1, list): [op1] = op1 # def is_llf(TYPE): return (TYPE == lltype.SignedLongLong or TYPE == lltype.UnsignedLongLong or TYPE == lltype.Float) if is_llf(RESULT): assert op1.opname == 'residual_call_irf_f' else: assert op1.opname == 'residual_call_irf_i' gotindex = getattr(EffectInfo, 'OS_' + op1.args[0].value.upper().lstrip('U')) assert gotindex == oopspecindex assert op1.args[1] == 'calldescr-%d' % oopspecindex assert list( op1.args[2]) == [v for v in vlist if not is_llf(v.concretetype)] assert list(op1.args[3]) == [] assert list( op1.args[4]) == [v for v in vlist if is_llf(v.concretetype)] assert op1.result == v_result
def get_direct_call_op(argtypes, restype): FUNC = lltype.FuncType(argtypes, restype) fnptr = lltype.functionptr(FUNC, "g") # no graph c_fnptr = const(fnptr) vars = [varoftype(TYPE) for TYPE in argtypes] v_result = varoftype(restype) op = SpaceOperation('direct_call', [c_fnptr] + vars, v_result) return op
def test_write_barrier_support_setfield(): PTR_TYPE2 = lltype.Ptr(lltype.GcStruct('T', ('y', lltype.Signed))) PTR_TYPE = lltype.Ptr(lltype.GcStruct('S', ('x', PTR_TYPE2))) write_barrier_check(SpaceOperation( "setfield", [varoftype(PTR_TYPE), Constant('x', lltype.Void), varoftype(PTR_TYPE2)], varoftype(lltype.Void)))
def test_dont_add_write_barrier_for_constant_new_value(): PTR_TYPE2 = lltype.Ptr(lltype.GcStruct('T', ('y', lltype.Signed))) PTR_TYPE = lltype.Ptr(lltype.GcStruct('S', ('x', PTR_TYPE2))) write_barrier_check(SpaceOperation( "setfield", [varoftype(PTR_TYPE), Constant('x', lltype.Void), Constant('foo', varoftype(PTR_TYPE2))], varoftype(lltype.Void)), needs_write_barrier=False)
def test_write_barrier_support_setarrayitem(): PTR_TYPE2 = lltype.Ptr(lltype.GcStruct('T', ('y', lltype.Signed))) ARRAYPTR = lltype.Ptr(lltype.GcArray(PTR_TYPE2)) write_barrier_check(SpaceOperation( "setarrayitem", [varoftype(ARRAYPTR), varoftype(lltype.Signed), varoftype(PTR_TYPE2)], varoftype(lltype.Void)))
def test_unicode_getinteriorarraysize(): v = varoftype(lltype.Ptr(rstr.UNICODE)) v_result = varoftype(lltype.Signed) op = SpaceOperation("getinteriorarraysize", [v, Constant("chars", lltype.Void)], v_result) op1 = Transformer().rewrite_operation(op) assert op1.opname == "unicodelen" assert op1.args == [v] assert op1.result == v_result
def test_getfield_gc_pure(): S = lltype.GcStruct("S", ("x", lltype.Char), hints={"immutable": True}) v1 = varoftype(lltype.Ptr(S)) v2 = varoftype(lltype.Char) op = SpaceOperation("getfield", [v1, Constant("x", lltype.Void)], v2) op1 = Transformer(FakeCPU()).rewrite_operation(op) assert op1.opname == "getfield_gc_i_pure" assert op1.args == [v1, ("fielddescr", S, "x")] assert op1.result == v2
def test_unicode_getinteriorfield(): v = varoftype(lltype.Ptr(rstr.UNICODE)) v_index = varoftype(lltype.Signed) v_result = varoftype(lltype.UniChar) op = SpaceOperation("getinteriorfield", [v, Constant("chars", lltype.Void), v_index], v_result) op1 = Transformer().rewrite_operation(op) assert op1.opname == "unicodegetitem" assert op1.args == [v, v_index] assert op1.result == v_result
def test_raw_malloc_unsupported_flag(): S = rffi.CArray(lltype.Signed) v1 = varoftype(lltype.Signed) v = varoftype(lltype.Ptr(S)) flags = Constant({'flavor': 'raw', 'unsupported_flag': True}, lltype.Void) op = SpaceOperation('malloc_varsize', [Constant(S, lltype.Void), flags, v1], v) tr = Transformer(FakeCPU(), FakeResidualCallControl()) py.test.raises(UnsupportedMallocFlags, tr.rewrite_operation, op)
def test_int_abs(): v1 = varoftype(lltype.Signed) v2 = varoftype(lltype.Signed) op = SpaceOperation('int_abs', [v1], v2) tr = Transformer(FakeCPU(), FakeRegularCallControl()) tr.graph = "somemaingraph" oplist = tr.rewrite_operation(op) assert oplist[0].opname == 'inline_call_ir_i' assert oplist[0].args[0] == 'somejitcode'
def test_write_barrier_support_setfield(): PTR_TYPE2 = lltype.Ptr(lltype.GcStruct('T', ('y', lltype.Signed))) PTR_TYPE = lltype.Ptr(lltype.GcStruct('S', ('x', PTR_TYPE2))) write_barrier_check( SpaceOperation("setfield", [ varoftype(PTR_TYPE), Constant('x', lltype.Void), varoftype(PTR_TYPE2) ], varoftype(lltype.Void)))
def test_unicode_getinteriorarraysize(): v = varoftype(lltype.Ptr(rstr.UNICODE)) v_result = varoftype(lltype.Signed) op = SpaceOperation('getinteriorarraysize', [v, Constant('chars', lltype.Void)], v_result) op1 = Transformer().rewrite_operation(op) assert op1.opname == 'unicodelen' assert op1.args == [v] assert op1.result == v_result
def test_fixed_ll_arraycopy(): builtin_test('list.ll_arraycopy', [varoftype(FIXEDLIST), varoftype(FIXEDLIST), varoftype(lltype.Signed), varoftype(lltype.Signed), varoftype(lltype.Signed)], lltype.Void, NotSupported)
def test_write_barrier_support_setinteriorfield(): PTR_TYPE2 = lltype.Ptr(lltype.GcStruct('T', ('y', lltype.Signed))) ARRAYPTR2 = lltype.Ptr(lltype.GcArray(('a', lltype.Signed), ('b', PTR_TYPE2))) write_barrier_check(SpaceOperation( "setinteriorfield", [varoftype(ARRAYPTR2), varoftype(lltype.Signed), Constant('b', lltype.Void), varoftype(PTR_TYPE2)], varoftype(lltype.Void)))
def test_getfield_gc_pure(): S = lltype.GcStruct('S', ('x', lltype.Char), hints={'immutable': True}) v1 = varoftype(lltype.Ptr(S)) v2 = varoftype(lltype.Char) op = SpaceOperation('getfield', [v1, Constant('x', lltype.Void)], v2) op1 = Transformer(FakeCPU()).rewrite_operation(op) assert op1.opname == 'getfield_gc_i_pure' assert op1.args == [v1, ('fielddescr', S, 'x')] assert op1.result == v2
def test_write_barrier_support_setarrayitem(): PTR_TYPE2 = lltype.Ptr(lltype.GcStruct('T', ('y', lltype.Signed))) ARRAYPTR = lltype.Ptr(lltype.GcArray(PTR_TYPE2)) write_barrier_check( SpaceOperation("setarrayitem", [ varoftype(ARRAYPTR), varoftype(lltype.Signed), varoftype(PTR_TYPE2) ], varoftype(lltype.Void)))
def test_no_gcstruct_nesting_outside_of_OBJECT(): PARENT = lltype.GcStruct('parent') STRUCT = lltype.GcStruct('struct', ('parent', PARENT), ('x', lltype.Signed)) v_x = varoftype(lltype.Ptr(STRUCT)) op = SpaceOperation('getfield', [v_x, Constant('x', lltype.Void)], varoftype(lltype.Signed)) tr = Transformer(None, None) raises(NotImplementedError, tr.rewrite_operation, op)
def test_dont_add_write_barrier_for_constant_new_value(): PTR_TYPE2 = lltype.Ptr(lltype.GcStruct('T', ('y', lltype.Signed))) PTR_TYPE = lltype.Ptr(lltype.GcStruct('S', ('x', PTR_TYPE2))) write_barrier_check(SpaceOperation("setfield", [ varoftype(PTR_TYPE), Constant('x', lltype.Void), Constant('foo', varoftype(PTR_TYPE2)) ], varoftype(lltype.Void)), needs_write_barrier=False)
def test_getfield_gc(): S = lltype.GcStruct('S', ('x', lltype.Char)) v1 = varoftype(lltype.Ptr(S)) v2 = varoftype(lltype.Char) op = SpaceOperation('getfield', [v1, Constant('x', lltype.Void)], v2) op1 = Transformer(FakeCPU()).rewrite_operation(op) assert op1.opname == 'getfield_gc_i' assert op1.args == [v1, ('fielddescr', S, 'x')] assert op1.result == v2
def test_str_newstr(): c_STR = Constant(rstr.STR, lltype.Void) c_flavor = Constant({'flavor': 'gc'}, lltype.Void) v1 = varoftype(lltype.Signed) v2 = varoftype(lltype.Ptr(rstr.STR)) op = SpaceOperation('malloc_varsize', [c_STR, c_flavor, v1], v2) op1 = Transformer().rewrite_operation(op) assert op1.opname == 'newstr' assert op1.args == [v1] assert op1.result == v2
def test_str_getinteriorarraysize(): v = varoftype(lltype.Ptr(rstr.STR)) v_result = varoftype(lltype.Signed) op = SpaceOperation('getinteriorarraysize', [v, Constant('chars', lltype.Void)], v_result) op1 = Transformer().rewrite_operation(op) assert op1.opname == 'strlen' assert op1.args == [v] assert op1.result == v_result
def test_unicode_getinteriorfield(): v = varoftype(lltype.Ptr(rstr.UNICODE)) v_index = varoftype(lltype.Signed) v_result = varoftype(lltype.UniChar) op = SpaceOperation('getinteriorfield', [v, Constant('chars', lltype.Void), v_index], v_result) op1 = Transformer().rewrite_operation(op) assert op1.opname == 'unicodegetitem' assert op1.args == [v, v_index] assert op1.result == v_result
def test_write_barrier_support_setinteriorfield(): PTR_TYPE2 = lltype.Ptr(lltype.GcStruct('T', ('y', lltype.Signed))) ARRAYPTR2 = lltype.Ptr( lltype.GcArray(('a', lltype.Signed), ('b', PTR_TYPE2))) write_barrier_check( SpaceOperation("setinteriorfield", [ varoftype(ARRAYPTR2), varoftype(lltype.Signed), Constant('b', lltype.Void), varoftype(PTR_TYPE2) ], varoftype(lltype.Void)))
def test_cast_opaque_ptr(): S = lltype.GcStruct("S", ("x", lltype.Signed)) v1 = varoftype(lltype.Ptr(S)) v2 = varoftype(lltype.Ptr(rclass.OBJECT)) op = SpaceOperation('cast_opaque_ptr', [v1], v2) tr = Transformer() [op1, op2] = tr.rewrite_operation(op) assert op1.opname == 'mark_opaque_ptr' assert op1.args == [v1] assert op1.result is None assert op2 is None
def test_getfield_typeptr(): v_parent = varoftype(rclass.OBJECTPTR) c_name = Constant('typeptr', lltype.Void) v_result = varoftype(rclass.OBJECT.typeptr) op = SpaceOperation('getfield', [v_parent, c_name], v_result) oplist = Transformer(FakeCPU()).rewrite_operation(op) op0, op1 = oplist assert op0.opname == '-live-' assert op0.args == [] assert op1.opname == 'guard_class' assert op1.args == [v_parent] assert op1.result == v_result
def test_str_setinteriorfield(): v = varoftype(lltype.Ptr(rstr.STR)) v_index = varoftype(lltype.Signed) v_newchr = varoftype(lltype.Char) v_void = varoftype(lltype.Void) op = SpaceOperation( 'setinteriorfield', [v, Constant('chars', lltype.Void), v_index, v_newchr], v_void) op1 = Transformer().rewrite_operation(op) assert op1.opname == 'strsetitem' assert op1.args == [v, v_index, v_newchr] assert op1.result == v_void
def test_raw_malloc_zero(): S = rffi.CArray(lltype.Signed) v1 = varoftype(lltype.Signed) v = varoftype(lltype.Ptr(S)) flags = Constant({'flavor': 'raw', 'zero': True}, lltype.Void) op = SpaceOperation('malloc_varsize', [Constant(S, lltype.Void), flags, v1], v) tr = Transformer(FakeCPU(), FakeResidualCallControl()) op0, op1 = tr.rewrite_operation(op) assert op0.opname == 'residual_call_ir_i' assert op0.args[0].value == 'raw_malloc_varsize_zero' # pseudo-fn as a str assert op1.opname == '-live-' assert op1.args == []
def test_promote_1(): v1 = varoftype(lltype.Signed) v2 = varoftype(lltype.Signed) op = SpaceOperation('hint', [v1, Constant({'promote': True}, lltype.Void)], v2) oplist = Transformer().rewrite_operation(op) op0, op1, op2 = oplist assert op0.opname == '-live-' assert op0.args == [] assert op1.opname == 'int_guard_value' assert op1.args == [v1] assert op1.result is None assert op2 is None
def test_str_promote(): PSTR = lltype.Ptr(rstr.STR) v1 = varoftype(PSTR) v2 = varoftype(PSTR) op = SpaceOperation( 'hint', [v1, Constant({'promote_string': True}, lltype.Void)], v2) tr = Transformer(FakeCPU(), FakeBuiltinCallControl()) op0, op1, _ = tr.rewrite_operation(op) assert op1.opname == 'str_guard_value' assert op1.args[0] == v1 assert op1.args[2] == 'calldescr' assert op1.result == v2 assert op0.opname == '-live-'
def test_constants(self): for TYPE in [lltype.SignedLongLong, lltype.UnsignedLongLong]: v_x = varoftype(TYPE) vlist = [v_x, const(rffi.cast(TYPE, 7))] v_result = varoftype(TYPE) op = SpaceOperation('llong_add', vlist, v_result) tr = Transformer(FakeCPU(), FakeBuiltinCallControl()) op1 = tr.rewrite_operation(op) # assert op1.opname == 'residual_call_irf_f' assert list(op1.args[2]) == [] assert list(op1.args[3]) == [] assert list(op1.args[4]) == vlist assert op1.result == v_result
def test_dict_setinteriorfield(): DICT = lltype.GcArray( lltype.Struct('ENTRY', ('v', lltype.Signed), ('k', lltype.Signed))) v = varoftype(lltype.Ptr(DICT)) i = varoftype(lltype.Signed) v_void = varoftype(lltype.Void) op = SpaceOperation('setinteriorfield', [v, i, Constant('v', lltype.Void), i], v_void) op1 = Transformer(FakeCPU()).rewrite_operation(op) assert op1.opname == 'setinteriorfield_gc_i' assert op1.args == [v, i, i, ('interiorfielddescr', DICT, 'v')] op = SpaceOperation('setinteriorfield', [v, i, Constant('v', lltype.Void), v_void], v_void) op1 = Transformer(FakeCPU()).rewrite_operation(op) assert not op1
def newgraph(gv_FUNCTYPE, name): FUNCTYPE = _from_opaque(gv_FUNCTYPE).value # 'name' is just a way to track things name = from_opaque_string(name) inputargs = [] erasedinputargs = [] for ARG in FUNCTYPE.ARGS: v = flowmodel.Variable() v.concretetype = ARG inputargs.append(v) v = flowmodel.Variable() v.concretetype = lltype.erasedType(ARG) erasedinputargs.append(v) startblock = flowmodel.Block(inputargs) # insert an exploding operation here which is removed by # builder.end() to ensure that builder.end() is actually called. startblock.operations.append( flowmodel.SpaceOperation("debug_assert", [ flowmodel.Constant(False, lltype.Bool), flowmodel.Constant("you didn't call builder.end()?", lltype.Void) ], varoftype(lltype.Void))) return_var = flowmodel.Variable() return_var.concretetype = FUNCTYPE.RESULT graph = flowmodel.FunctionGraph(name, startblock, return_var) v1 = flowmodel.Variable() v1.concretetype = lltype.erasedType(FUNCTYPE.RESULT) graph.prereturnblock = flowmodel.Block([v1]) casting_link(graph.prereturnblock, [v1], graph.returnblock) substartblock = flowmodel.Block(erasedinputargs) casting_link(graph.startblock, inputargs, substartblock) fptr = lltype.functionptr(FUNCTYPE, name, graph=graph) return genconst(fptr)
def indirect_residual_call_test(argtypes, restype, expectedkind): # an indirect call that is residual in all cases is very similar to # a residual direct call op = get_direct_call_op(argtypes, restype) op.opname = 'indirect_call' op.args[0] = varoftype(op.args[0].concretetype) op.args.append(Constant(['somegraph1', 'somegraph2'], lltype.Void)) tr = Transformer(FakeCPU(), FakeResidualIndirectCallControl()) tr.graph = 'someinitialgraph' oplist = tr.rewrite_operation(op) op0, op1 = oplist reskind = getkind(restype)[0] assert op0.opname == 'residual_call_%s_%s' % (expectedkind, reskind) assert op0.result == op.result assert op0.args[0] == op.args[0] assert op0.args[1] == 'calldescr' assert len(op0.args) == 2 + len(expectedkind) for sublist, kind1 in zip(op0.args[2:], expectedkind): assert sublist.kind.startswith(kind1) assert list(sublist) == [ v for v in op.args[1:] if getkind(v.concretetype) == sublist.kind ] for v in op.args[1:]: kind = getkind(v.concretetype) assert kind == 'void' or kind[0] in expectedkind assert op1.opname == '-live-' assert op1.args == []
def test_unknown_operation(): op = SpaceOperation('foobar', [], varoftype(lltype.Void)) tr = Transformer() try: tr.rewrite_operation(op) except Exception, e: assert 'foobar' in str(e)
def test_symmetric_int_add_ovf(): v3 = varoftype(lltype.Signed) for v1 in [varoftype(lltype.Signed), const(42)]: for v2 in [varoftype(lltype.Signed), const(43)]: op = SpaceOperation('int_add_nonneg_ovf', [v1, v2], v3) oplist = Transformer(FakeCPU()).rewrite_operation(op) op0, op1 = oplist assert op0.opname == 'int_add_ovf' if isinstance(v1, Constant) and isinstance(v2, Variable): assert op0.args == [v2, v1] assert op0.result == v3 else: assert op0.args == [v1, v2] assert op0.result == v3 assert op1.opname == '-live-' assert op1.args == [] assert op1.result is None