def test_call_stubs(): c0 = GcCache(False) ARGS = [lltype.Char, lltype.Signed] RES = lltype.Char descr1 = get_call_descr(c0, ARGS, RES) def f(a, b): return 'c' call_stub = descr1.call_stub fnptr = llhelper(lltype.Ptr(lltype.FuncType(ARGS, RES)), f) res = call_stub(rffi.cast(lltype.Signed, fnptr), [1, 2], None, None) assert res == ord('c') ARRAY = lltype.GcArray(lltype.Signed) ARGS = [lltype.Float, lltype.Ptr(ARRAY)] RES = lltype.Float def f(a, b): return float(b[0]) + a fnptr = llhelper(lltype.Ptr(lltype.FuncType(ARGS, RES)), f) descr2 = get_call_descr(c0, ARGS, RES) a = lltype.malloc(ARRAY, 3) opaquea = lltype.cast_opaque_ptr(llmemory.GCREF, a) a[0] = 1 res = descr2.call_stub(rffi.cast(lltype.Signed, fnptr), [], [opaquea], [longlong.getfloatstorage(3.5)]) assert longlong.getrealfloat(res) == 4.5
def test_call_aligned_with_spilled_values(self): from pypy.rlib.libffi import types cpu = self.cpu if not cpu.supports_floats: py.test.skip('requires floats') def func(*args): return float(sum(args)) F = lltype.Float I = lltype.Signed floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56] ints = [7, 11, 23, 13, -42, 1111, 95, 1] for case in range(256): local_floats = list(floats) local_ints = list(ints) args = [] spills = [] funcargs = [] float_count = 0 int_count = 0 for i in range(8): if case & (1 << i): args.append('f%d' % float_count) spills.append('force_spill(f%d)' % float_count) float_count += 1 funcargs.append(F) else: args.append('i%d' % int_count) spills.append('force_spill(i%d)' % int_count) int_count += 1 funcargs.append(I) arguments = ', '.join(args) spill_ops = '\n'.join(spills) FUNC = self.FuncType(funcargs, 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) ops = '[%s]\n' % arguments ops += '%s\n' % spill_ops ops += 'f99 = call(ConstClass(func_ptr), %s, descr=calldescr)\n' % arguments ops += 'finish(f99, %s)\n' % arguments loop = parse(ops, namespace=locals()) looptoken = JitCellToken() done_number = self.cpu.get_fail_descr_number( loop.operations[-1].getdescr()) self.cpu.compile_loop(loop.inputargs, loop.operations, looptoken) argvals, expected_result = self._prepare_args(args, floats, ints) res = self.cpu.execute_token(looptoken, *argvals) x = longlong.getrealfloat(cpu.get_latest_value_float(0)) assert abs(x - expected_result) < 0.0001
def test_call_aligned_with_spilled_values(self): from pypy.rlib.libffi import types cpu = self.cpu if not cpu.supports_floats: py.test.skip('requires floats') def func(*args): return float(sum(args)) F = lltype.Float I = lltype.Signed floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56] ints = [7, 11, 23, 13, -42, 1111, 95, 1] for case in range(256): local_floats = list(floats) local_ints = list(ints) args = [] spills = [] funcargs = [] float_count = 0 int_count = 0 for i in range(8): if case & (1<<i): args.append('f%d' % float_count) spills.append('force_spill(f%d)' % float_count) float_count += 1 funcargs.append(F) else: args.append('i%d' % int_count) spills.append('force_spill(i%d)' % int_count) int_count += 1 funcargs.append(I) arguments = ', '.join(args) spill_ops = '\n'.join(spills) FUNC = self.FuncType(funcargs, 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) ops = '[%s]\n' % arguments ops += '%s\n' % spill_ops ops += 'f99 = call(ConstClass(func_ptr), %s, descr=calldescr)\n' % arguments ops += 'finish(f99, %s)\n' % arguments loop = parse(ops, namespace=locals()) looptoken = JitCellToken() done_number = self.cpu.get_fail_descr_number(loop.operations[-1].getdescr()) self.cpu.compile_loop(loop.inputargs, loop.operations, looptoken) argvals, expected_result = self._prepare_args(args, floats, ints) res = self.cpu.execute_token(looptoken, *argvals) x = longlong.getrealfloat(cpu.get_latest_value_float(0)) assert abs(x - expected_result) < 0.0001
def interp_operations(self, f, args, **kwds): # get the JitCodes for the function f _get_jitcodes(self, self.CPUClass, f, args, self.type_system, **kwds) # try to run it with blackhole.py result1 = _run_with_blackhole(self, args) # try to run it with pyjitpl.py result2 = _run_with_pyjitpl(self, args) assert result1 == result2 or isnan(result1) and isnan(result2) # try to run it by running the code compiled just before result3 = _run_with_machine_code(self, args) assert result1 == result3 or result3 == NotImplemented or isnan(result1) and isnan(result3) # if (longlong.supports_longlong and isinstance(result1, longlong.r_float_storage)): result1 = longlong.getrealfloat(result1) return result1
def test_call_stubs_2(): c0 = GcCache(False) ARRAY = lltype.GcArray(lltype.Signed) ARGS = [lltype.Float, lltype.Ptr(ARRAY)] RES = lltype.Float def f2(a, b): return float(b[0]) + a fnptr = llhelper(lltype.Ptr(lltype.FuncType(ARGS, RES)), f2) descr2 = get_call_descr(c0, ARGS, RES) a = lltype.malloc(ARRAY, 3) opaquea = lltype.cast_opaque_ptr(llmemory.GCREF, a) a[0] = 1 res = descr2.call_stub_f(rffi.cast(lltype.Signed, fnptr), [], [opaquea], [longlong.getfloatstorage(3.5)]) assert longlong.getrealfloat(res) == 4.5
def specialize_value(TYPE, x): """'x' must be a Signed, a GCREF or a FLOATSTORAGE. This function casts it to a more specialized type, like Char or Ptr(..). """ INPUT = lltype.typeOf(x) if INPUT is lltype.Signed: if isinstance(TYPE, lltype.Ptr) and TYPE.TO._gckind == 'raw': # non-gc pointer return rffi.cast(TYPE, x) else: return lltype.cast_primitive(TYPE, x) elif INPUT is longlong.FLOATSTORAGE: assert TYPE is lltype.Float return longlong.getrealfloat(x) else: return lltype.cast_opaque_ptr(TYPE, x)
def interp_operations(self, f, args, **kwds): # get the JitCodes for the function f _get_jitcodes(self, self.CPUClass, f, args, self.type_system, **kwds) # try to run it with blackhole.py result1 = _run_with_blackhole(self, args) # try to run it with pyjitpl.py result2 = _run_with_pyjitpl(self, args) assert result1 == result2 or isnan(result1) and isnan(result2) # try to run it by running the code compiled just before result3 = _run_with_machine_code(self, args) assert result1 == result3 or result3 == NotImplemented or isnan( result1) and isnan(result3) # if (longlong.supports_longlong and isinstance(result1, longlong.r_float_storage)): result1 = longlong.getrealfloat(result1) return result1
def specialize_value(TYPE, x): """'x' must be a Signed, a GCREF or a FLOATSTORAGE. This function casts it to a more specialized type, like Char or Ptr(..). """ INPUT = lltype.typeOf(x) if INPUT is lltype.Signed: if isinstance(TYPE, lltype.Ptr) and TYPE.TO._gckind == 'raw': # non-gc pointer return rffi.cast(TYPE, x) elif TYPE is lltype.SingleFloat: return longlong.int2singlefloat(x) else: return lltype.cast_primitive(TYPE, x) elif INPUT is longlong.FLOATSTORAGE: if longlong.is_longlong(TYPE): return rffi.cast(TYPE, x) assert TYPE is lltype.Float return longlong.getrealfloat(x) else: return lltype.cast_opaque_ptr(TYPE, x)
def __repr__(self): floatvalue = longlong.getrealfloat(self.aslonglong) return '<FloatImmedLoc(%s)>' % (floatvalue,)
def test_functions(): xll = longlong.getfloatstorage(3.5) assert longlong.getrealfloat(xll) == 3.5 assert isinstance(longlong.gethash(xll), int)
def test_call_alignment_call_assembler(self): from pypy.rlib.libffi import types cpu = self.cpu if not cpu.supports_floats: py.test.skip('requires floats') fdescr3 = BasicFailDescr(3) fdescr4 = BasicFailDescr(4) def assembler_helper(failindex, virtualizable): assert 0, 'should not be called, but was with failindex (%d)' % failindex return 13 FUNCPTR = lltype.Ptr( lltype.FuncType([lltype.Signed, llmemory.GCREF], lltype.Signed)) class FakeJitDriverSD: index_of_virtualizable = -1 _assembler_helper_ptr = llhelper(FUNCPTR, assembler_helper) assembler_helper_adr = llmemory.cast_ptr_to_adr( _assembler_helper_ptr) floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56] ints = [7, 11, 23, 42, -42, 1111, 95, 1] for case in range(256): float_count = 0 int_count = 0 args = [] called_ops = '' total_index = -1 for i in range(8): if case & (1 << i): args.append('f%d' % float_count) else: args.append('i%d' % int_count) called_ops += 'f%d = cast_int_to_float(i%d)\n' % ( float_count, int_count) int_count += 1 if total_index == -1: total_index = float_count float_count += 1 else: called_ops += 'f%d = float_add(f%d, f%d)\n' % ( float_count + 1, total_index, float_count) total_index = float_count + 1 float_count += 2 arguments = ', '.join(args) called_ops = '[%s]\n' % arguments + called_ops called_ops += 'finish(f%d, descr=fdescr3)\n' % total_index # compile called loop called_loop = parse(called_ops, namespace=locals()) called_looptoken = JitCellToken() called_looptoken.outermost_jitdriver_sd = FakeJitDriverSD() done_number = self.cpu.get_fail_descr_number( called_loop.operations[-1].getdescr()) self.cpu.compile_loop(called_loop.inputargs, called_loop.operations, called_looptoken) argvals, expected_result = self._prepare_args(args, floats, ints) res = cpu.execute_token(called_looptoken, *argvals) assert res.identifier == 3 t = longlong.getrealfloat(cpu.get_latest_value_float(0)) assert abs(t - expected_result) < 0.0001 ARGS = [] RES = lltype.Float for x in args: if x[0] == 'f': ARGS.append(lltype.Float) else: ARGS.append(lltype.Signed) FakeJitDriverSD.portal_calldescr = self.cpu.calldescrof( lltype.Ptr(lltype.FuncType(ARGS, RES)), ARGS, RES, EffectInfo.MOST_GENERAL) ops = ''' [%s] f99 = call_assembler(%s, descr=called_looptoken) guard_not_forced()[] finish(f99, descr=fdescr4) ''' % (arguments, arguments) loop = parse(ops, namespace=locals()) # we want to take the fast path self.cpu.done_with_this_frame_float_v = done_number try: othertoken = JitCellToken() self.cpu.compile_loop(loop.inputargs, loop.operations, othertoken) # prepare call to called_loop argvals, _ = self._prepare_args(args, floats, ints) res = cpu.execute_token(othertoken, *argvals) x = longlong.getrealfloat(cpu.get_latest_value_float(0)) assert res.identifier == 4 assert abs(x - expected_result) < 0.0001 finally: del self.cpu.done_with_this_frame_float_v
def __repr__(self): floatvalue = longlong.getrealfloat(self.aslonglong) return '<FloatImmedLoc(%s)>' % (floatvalue, )
def test_functions(): xll = longlong.getfloatstorage(3.5) assert longlong.getrealfloat(xll) == 3.5 assert is_valid_int(longlong.gethash(xll))
def getfloat(self): return longlong.getrealfloat(self.getfloatstorage())
def getfloats(self, end): return [longlong.getrealfloat(self.cpu.get_latest_value_float(index)) for index in range(0, end)]
def test_call_alignment_call_assembler(self): from pypy.rlib.libffi import types cpu = self.cpu if not cpu.supports_floats: py.test.skip('requires floats') fdescr3 = BasicFailDescr(3) fdescr4 = BasicFailDescr(4) def assembler_helper(failindex, virtualizable): assert 0, 'should not be called, but was with failindex (%d)' % failindex return 13 FUNCPTR = lltype.Ptr(lltype.FuncType([lltype.Signed, llmemory.GCREF], lltype.Signed)) class FakeJitDriverSD: index_of_virtualizable = -1 _assembler_helper_ptr = llhelper(FUNCPTR, assembler_helper) assembler_helper_adr = llmemory.cast_ptr_to_adr( _assembler_helper_ptr) floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56] ints = [7, 11, 23, 42, -42, 1111, 95, 1] for case in range(256): float_count = 0 int_count = 0 args = [] called_ops = '' total_index = -1 for i in range(8): if case & (1<<i): args.append('f%d' % float_count) else: args.append('i%d' % int_count) called_ops += 'f%d = cast_int_to_float(i%d)\n' % ( float_count, int_count) int_count += 1 if total_index == -1: total_index = float_count float_count += 1 else: called_ops += 'f%d = float_add(f%d, f%d)\n' % ( float_count + 1, total_index, float_count) total_index = float_count + 1 float_count += 2 arguments = ', '.join(args) called_ops = '[%s]\n' % arguments + called_ops called_ops += 'finish(f%d, descr=fdescr3)\n' % total_index # compile called loop called_loop = parse(called_ops, namespace=locals()) called_looptoken = LoopToken() called_looptoken.outermost_jitdriver_sd = FakeJitDriverSD() done_number = self.cpu.get_fail_descr_number(called_loop.operations[-1].getdescr()) self.cpu.compile_loop(called_loop.inputargs, called_loop.operations, called_looptoken) expected_result = self._prepare_args(args, floats, ints) res = cpu.execute_token(called_looptoken) assert res.identifier == 3 t = longlong.getrealfloat(cpu.get_latest_value_float(0)) assert abs(t - expected_result) < 0.0001 ARGS = [] RES = lltype.Float for x in args: if x[0] == 'f': ARGS.append(lltype.Float) else: ARGS.append(lltype.Signed) FakeJitDriverSD.portal_calldescr = self.cpu.calldescrof( lltype.Ptr(lltype.FuncType(ARGS, RES)), ARGS, RES) ops = ''' [%s] f99 = call_assembler(%s, descr=called_looptoken) guard_not_forced()[] finish(f99, descr=fdescr4) ''' % (arguments, arguments) loop = parse(ops, namespace=locals()) # we want to take the fast path self.cpu.done_with_this_frame_float_v = done_number try: othertoken = LoopToken() self.cpu.compile_loop(loop.inputargs, loop.operations, othertoken) # prepare call to called_loop self._prepare_args(args, floats, ints) res = cpu.execute_token(othertoken) x = longlong.getrealfloat(cpu.get_latest_value_float(0)) assert res.identifier == 4 assert abs(x - expected_result) < 0.0001 finally: del self.cpu.done_with_this_frame_float_v