def run(self, code): space = FakeSpace() def f(code): interp = numpy_compile(hlstr(code)) interp.run(space) res = interp.results[-1] w_res = res.eval(0).wrap(interp.space) if isinstance(w_res, BoolObject): return float(w_res.boolval) elif isinstance(w_res, FloatObject): return w_res.floatval elif isinstance(w_res, IntObject): return w_res.intval else: return -42. if self.graph is None: interp, graph = self.meta_interp(f, [llstr(code)], listops=True, backendopt=True, graph_and_interp_only=True) self.__class__.interp = interp self.__class__.graph = graph reset_stats() pyjitpl._warmrunnerdesc.memory_manager.alive_loops.clear() return self.interp.eval_graph(self.graph, [llstr(code)])
def test_streq_nonnull_char(): func = LLtypeHelpers._ll_2_str_eq_nonnull_char.im_func assert func(llstr("wor"), "x") == False assert func(llstr("w"), "x") == False assert func(llstr(""), "x") == False assert func(llstr("x"), "x") == True py.test.raises(AttributeError, func, llstr(None), "x")
def test_streq_checknull_char(): func = LLtypeHelpers._ll_2_str_eq_checknull_char.im_func assert func(llstr("wor"), "x") == False assert func(llstr("w"), "x") == False assert func(llstr(""), "x") == False assert func(llstr("x"), "x") == True assert func(llstr(None), "x") == False
def test_set_param_enable_opts(self): from pypy.rpython.annlowlevel import llstr, hlstr myjitdriver = JitDriver(greens=[], reds=['n']) class A(object): def m(self, n): return n - 1 def g(n): while n > 0: myjitdriver.can_enter_jit(n=n) myjitdriver.jit_merge_point(n=n) n = A().m(n) return n def f(n, enable_opts): set_param(None, 'enable_opts', hlstr(enable_opts)) return g(n) # check that the set_param will override the default res = self.meta_interp(f, [10, llstr('')]) assert res == 0 self.check_resops(new_with_vtable=1) res = self.meta_interp(f, [10, llstr(ALL_OPTS_NAMES)], enable_opts='') assert res == 0 self.check_resops(new_with_vtable=0)
def test_set_param_enable_opts(self): from pypy.rpython.annlowlevel import llstr, hlstr myjitdriver = JitDriver(greens = [], reds = ['n']) class A(object): def m(self, n): return n-1 def g(n): while n > 0: myjitdriver.can_enter_jit(n=n) myjitdriver.jit_merge_point(n=n) n = A().m(n) return n def f(n, enable_opts): myjitdriver.set_param('enable_opts', hlstr(enable_opts)) return g(n) # check that the set_param will override the default res = self.meta_interp(f, [10, llstr('')]) assert res == 0 self.check_loops(new_with_vtable=1) res = self.meta_interp(f, [10, llstr(ALL_OPTS_NAMES)], enable_opts='') assert res == 0 self.check_loops(new_with_vtable=0)
def test_simple(self): sb = StringBuilderRepr.ll_new(3) StringBuilderRepr.ll_append_char(sb, 'x') StringBuilderRepr.ll_append(sb, llstr("abc")) StringBuilderRepr.ll_append_slice(sb, llstr("foobar"), 2, 5) StringBuilderRepr.ll_append_multiple_char(sb, 'y', 3) s = StringBuilderRepr.ll_build(sb) assert hlstr(s) == "xabcobayyy"
def test_streq_slice_nonnull(): p1 = llstr("hello world") p2 = llstr("wor") func = LLtypeHelpers._ll_4_str_eq_slice_nonnull.im_func assert func(p1, 6, 3, p2) == True assert func(p1, 6, 2, p2) == False assert func(p1, 5, 3, p2) == False py.test.raises(AttributeError, func, p1, 2, 1, llstr(None))
def test_streq_slice_checknull(): p1 = llstr("hello world") p2 = llstr("wor") func = LLtypeHelpers._ll_4_str_eq_slice_checknull.im_func assert func(p1, 6, 3, p2) == True assert func(p1, 6, 2, p2) == False assert func(p1, 5, 3, p2) == False assert func(p1, 2, 1, llstr(None)) == False
def getvar(self, arg): if not arg: return ConstInt(0) try: return ConstInt(int(arg)) except ValueError: if self.is_float(arg): return ConstFloat(float(arg)) if arg.startswith('"') or arg.startswith("'"): # XXX ootype info = arg.strip("'\"") return ConstPtr(lltype.cast_opaque_ptr(llmemory.GCREF, llstr(info))) if arg.startswith('ConstClass('): name = arg[len('ConstClass('):-1] return self.get_const(name, 'class') elif arg == 'None': return None elif arg == 'NULL': if self.type_system == 'lltype': return ConstPtr(ConstPtr.value) else: return ConstObj(ConstObj.value) elif arg.startswith('ConstPtr('): name = arg[len('ConstPtr('):-1] return self.get_const(name, 'ptr') return self.vars[arg]
def test_streq_slice_char(): p1 = llstr("hello world") func = LLtypeHelpers._ll_4_str_eq_slice_char.im_func assert func(p1, 6, 3, "w") == False assert func(p1, 6, 0, "w") == False assert func(p1, 6, 1, "w") == True assert func(p1, 6, 1, "x") == False
def getvar(self, arg): if not arg: return ConstInt(0) try: return ConstInt(int(arg)) except ValueError: if self.is_float(arg): return ConstFloat(float(arg)) if arg.startswith('"') or arg.startswith("'"): # XXX ootype info = arg.strip("'\"") return ConstPtr( lltype.cast_opaque_ptr(llmemory.GCREF, llstr(info))) if arg.startswith('ConstClass('): name = arg[len('ConstClass('):-1] return self.get_const(name, 'class') elif arg == 'None': return None elif arg == 'NULL': if self.type_system == 'lltype': return ConstPtr(ConstPtr.value) else: return ConstObj(ConstObj.value) elif arg.startswith('ConstPtr('): name = arg[len('ConstPtr('):-1] return self.get_const(name, 'ptr') return self.vars[arg]
def f(): a = llstr("xyz") b = (llmemory.cast_ptr_to_adr(a) + llmemory.offsetof(STR, 'chars') + llmemory.itemoffsetof(STR.chars, 0)) buf = rffi.cast(rffi.VOIDP, b) return buf[2]
def read_code(): from pypy.module.marshal.interp_marshal import dumps filename = 'pypyjit_demo.py' source = readfile(filename) ec = space.getexecutioncontext() code = ec.compiler.compile(source, filename, 'exec', 0) return llstr(space.str_w(dumps(space, code, space.wrap(2))))
def get_const_ptr_for_string(s): from pypy.rpython.annlowlevel import llstr if not we_are_translated(): try: return _const_ptr_for_string[s] except KeyError: pass result = ConstPtr(lltype.cast_opaque_ptr(llmemory.GCREF, llstr(s))) if not we_are_translated(): _const_ptr_for_string[s] = result return result
def apply_jit(policy, interp, graph, CPUClass): print 'warmspot.jittify_and_run() started...' option.view = True LIST = graph.getargs()[0].concretetype lst = LIST.TO.ll_newlist(len(ARGS)) for i, arg in enumerate(ARGS): lst.ll_setitem_fast(i, llstr(arg)) warmspot.jittify_and_run(interp, graph, [lst], policy=policy, listops=True, CPUClass=CPUClass, backendopt=True, inline=True, optimizer=OPTIMIZER_FULL)
def test_streq_nonnull(): p1 = llstr("wor") p2 = llstr("wor") assert p1 != p2 func = LLtypeHelpers._ll_2_str_eq_nonnull.im_func assert func(p1, p1) == True assert func(p1, p2) == True assert func(p1, llstr("wrl")) == False assert func(p1, llstr("world")) == False assert func(p1, llstr("w")) == False py.test.raises(AttributeError, func, p1, llstr(None)) py.test.raises(AttributeError, func, llstr(None), p2)
def f(arg): s = llstr(hlstr(arg)) return len(s.chars)
def test_llstr(self): s = llstr("abc") assert len(s.chars) == 3 assert s.chars[0] == "a" assert s.chars[1] == "b" assert s.chars[2] == "c"
def ll_str(self, none): return llstr("None")
def test_nooveralloc(self): sb = StringBuilderRepr.ll_new(3) StringBuilderRepr.ll_append(sb, llstr("abc")) assert StringBuilderRepr.ll_build(sb) == sb.buf
def test_translation(self): # we import main to check if the target compiles from pypy.translator.goal.targetnumpystandalone import main interpret(main, [llstr('af+'), 100])
def conststr(self, str): ll = llstr(str) return history.ConstPtr(lltype.cast_opaque_ptr(llmemory.GCREF, ll))
def meta_interp_search(self, pattern, string, repeat=1): r = get_code(pattern) return self.meta_interp(entrypoint2, [list2array(r), llstr(string), repeat], listcomp=True, backendopt=True)
def ll_float_str(repr, f): return llstr(formatd("%f", f))
def resop_getopname(llop): return llstr(_cast_to_resop(llop).getopname())
def ll_float_str(repr, f): from pypy.rlib.rfloat import formatd return llstr(formatd(f, 'f', 6))