def test_regalloc_lists(self): v1 = Variable() v1.concretetype = lltype.Signed v2 = Variable() v2.concretetype = lltype.Signed v3 = Variable() v3.concretetype = lltype.Signed v4 = Variable() v4.concretetype = lltype.Signed v5 = Variable() v5.concretetype = lltype.Signed block = Block([v1]) block.operations = [ SpaceOperation('int_add', [v1, Constant(1, lltype.Signed)], v2), SpaceOperation('rescall', [ListOfKind('int', [v1, v2])], v5), SpaceOperation('rescall', [ListOfKind('int', [v1, v2])], v3), ] graph = FunctionGraph('f', block, v4) block.closeblock(Link([v3], graph.returnblock)) # self.check_assembler( graph, """ int_add %i0, $1 -> %i1 rescall I[%i0, %i1] -> %i2 rescall I[%i0, %i1] -> %i0 int_return %i0 """)
def test_assemble_list(): ssarepr = SSARepr("test") i0, i1 = Register('int', 0x16), Register('int', 0x17) ssarepr.insns = [ ('foobar', ListOfKind('int', [i0, i1, Constant(42, lltype.Signed)]), ListOfKind('ref', [])), ] assembler = Assembler() jitcode = assembler.assemble(ssarepr) assert jitcode.code == "\x00\x03\x16\x17\xFF\x00" assert assembler.insns == {'foobar/IR': 0} assert jitcode.constants_i == [42]
def test_assemble_list_semibug(): # the semibug is that after forcing 42 into the dict of constants, # it would be reused for all future 42's, even ones that can be # encoded directly. ssarepr = SSARepr("test") ssarepr.insns = [ ('foobar', ListOfKind('int', [Constant(42, lltype.Signed)])), ('foobar', ListOfKind('int', [Constant(42, lltype.Signed)])), ('baz', Constant(42, lltype.Signed)), ] assembler = Assembler() jitcode = assembler.assemble(ssarepr) assert jitcode.code == ("\x00\x01\xFF" "\x00\x01\xFF" "\x01\x2A") assert assembler.insns == {'foobar/I': 0, 'baz/c': 1} assert jitcode.constants_i == [42]
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_math_sqrt(): # test that the oopspec is present and correctly transformed FLOAT = lltype.Float FUNC = lltype.FuncType([FLOAT], FLOAT) func = lltype.functionptr(FUNC, 'll_math', _callable=ll_math.sqrt_nonneg) v1 = varoftype(FLOAT) v2 = varoftype(FLOAT) op = SpaceOperation('direct_call', [const(func), v1], v2) tr = Transformer(FakeCPU(), FakeBuiltinCallControl()) op1 = tr.rewrite_operation(op) assert op1.opname == 'residual_call_irf_f' assert op1.args[0].value == func assert op1.args[1] == 'calldescr-%d' % effectinfo.EffectInfo.OS_MATH_SQRT assert op1.args[2] == ListOfKind("int", []) assert op1.args[3] == ListOfKind("ref", []) assert op1.args[4] == ListOfKind('float', [v1]) assert op1.result == v2
def test_unformat_assembler_lists(): input = """ foo F[%f0, %f3] """ regs = {} ssarepr = unformat_assembler(input, regs) assert ssarepr.insns == [('foo', ListOfKind('float', [regs['%f0'], regs['%f3']]))]
def test_arg_sublist_1(self): v1 = varoftype(lltype.Signed) v2 = varoftype(lltype.Char) v3 = varoftype(rclass.OBJECTPTR) v4 = varoftype(lltype.Ptr(rstr.STR)) v5 = varoftype(lltype.Float) op = SpaceOperation( 'residual_call_ir_f', [ Constant(12345, lltype.Signed), # function ptr ListOfKind('int', [v1, v2]), # int args ListOfKind('ref', [v3, v4]) ], # ref args v5) # result flattener = GraphFlattener(None, fake_regallocs()) flattener.serialize_op(op) assert_format( flattener.ssarepr, """ residual_call_ir_f $12345, I[%i0, %i1], R[%r0, %r1] -> %f0 """)
def test_format_assembler_list(): ssarepr = SSARepr("test") i0, i1 = Register('int', 0), Register('int', 1) ssarepr.insns = [ ('foobar', ListOfKind('int', [i0, Constant(123, lltype.Signed), i1])), ] asm = format_assembler(ssarepr) expected = """ foobar I[%i0, $123, %i1] """ assert asm == str(py.code.Source(expected)).strip() + '\n'
def test_list_ll_arraycopy(): from pypy.rlib.rgc import ll_arraycopy LIST = lltype.GcArray(lltype.Signed) PLIST = lltype.Ptr(LIST) INT = lltype.Signed FUNC = lltype.FuncType([PLIST] * 2 + [INT] * 3, lltype.Void) func = lltype.functionptr(FUNC, 'll_arraycopy', _callable=ll_arraycopy) v1 = varoftype(PLIST) v2 = varoftype(PLIST) v3 = varoftype(INT) v4 = varoftype(INT) v5 = varoftype(INT) v6 = varoftype(lltype.Void) op = SpaceOperation('direct_call', [const(func), v1, v2, v3, v4, v5], v6) tr = Transformer(FakeCPU(), FakeBuiltinCallControl()) op1 = tr.rewrite_operation(op) assert op1.opname == 'residual_call_ir_v' assert op1.args[0].value == func assert op1.args[1] == 'calldescr-%d' % effectinfo.EffectInfo.OS_ARRAYCOPY assert op1.args[2] == ListOfKind('int', [v3, v4, v5]) assert op1.args[3] == ListOfKind('ref', [v1, v2])
def test_unicode_slice(): # test that the oopspec is present and correctly transformed PUNICODE = lltype.Ptr(rstr.UNICODE) INT = lltype.Signed FUNC = lltype.FuncType([PUNICODE, INT, INT], PUNICODE) func = lltype.functionptr(FUNC, '_ll_stringslice', _callable=rstr.LLHelpers._ll_stringslice) v1 = varoftype(PUNICODE) v2 = varoftype(INT) v3 = varoftype(INT) v4 = varoftype(PUNICODE) op = SpaceOperation('direct_call', [const(func), v1, v2, v3], v4) tr = Transformer(FakeCPU(), FakeBuiltinCallControl()) op1 = tr.rewrite_operation(op) assert op1.opname == 'residual_call_ir_r' assert op1.args[0].value == func assert op1.args[1] == 'calldescr-%d' % effectinfo.EffectInfo.OS_UNI_SLICE assert op1.args[2] == ListOfKind('int', [v2, v3]) assert op1.args[3] == ListOfKind('ref', [v1]) assert op1.result == v4
def test_str2unicode(): # test that the oopspec is present and correctly transformed PSTR = lltype.Ptr(rstr.STR) PUNICODE = lltype.Ptr(rstr.UNICODE) FUNC = lltype.FuncType([PSTR], PUNICODE) func = lltype.functionptr(FUNC, 'll_str2unicode', _callable=rstr.LLHelpers.ll_str2unicode) v1 = varoftype(PSTR) v2 = varoftype(PUNICODE) op = SpaceOperation('direct_call', [const(func), v1], v2) tr = Transformer(FakeCPU(), FakeBuiltinCallControl()) 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_STR2UNICODE assert op1.args[2] == ListOfKind('ref', [v1]) assert op1.result == v2
def unformat_arg(s): if s.endswith(','): s = s[:-1].rstrip() if s[0] == '%': try: return registers[s] except KeyError: num = int(s[2:]) if s[1] == 'i': reg = Register('int', num) elif s[1] == 'r': reg = Register('ref', num) elif s[1] == 'f': reg = Register('float', num) else: raise AssertionError("bad register type") registers[s] = reg return reg elif s[0] == '$': intvalue = int(s[1:]) return Constant(intvalue, lltype.Signed) elif s[0] == 'L': return TLabel(s) elif s[0] in 'IRF' and s[1] == '[' and s[-1] == ']': items = split_words(s[2:-1]) items = map(unformat_arg, items) return ListOfKind({ 'I': 'int', 'R': 'ref', 'F': 'float' }[s[0]], items) elif s.startswith('<SwitchDictDescr '): assert s.endswith('>') switchdict = SwitchDictDescr() switchdict._labels = [] items = split_words(s[len('<SwitchDictDescr '):-1]) for item in items: key, value = item.split(':') value = value.rstrip(',') switchdict._labels.append((int(key), TLabel(value))) return switchdict else: raise AssertionError("unsupported argument: %r" % (s, ))
def test_unicode_eq_checknull_char(): # test that the oopspec is present and correctly transformed PUNICODE = lltype.Ptr(rstr.UNICODE) FUNC = lltype.FuncType([PUNICODE, PUNICODE], lltype.Bool) func = lltype.functionptr(FUNC, 'll_streq', _callable=rstr.LLHelpers.ll_streq) v1 = varoftype(PUNICODE) v2 = varoftype(PUNICODE) v3 = varoftype(lltype.Bool) 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_i' assert op1.args[0].value == func assert op1.args[1] == 'calldescr-%d' % effectinfo.EffectInfo.OS_UNI_EQUAL assert op1.args[2] == ListOfKind('ref', [v1, v2]) assert op1.result == v3 # test that the OS_UNIEQ_* functions are registered cic = cc.callinfocollection assert cic.has_oopspec(effectinfo.EffectInfo.OS_UNIEQ_SLICE_NONNULL) assert cic.has_oopspec(effectinfo.EffectInfo.OS_UNIEQ_CHECKNULL_CHAR)