Author: Maciej Fijalkowski <[email protected]>
Branch: jit-applevel-hook
Changeset: r44498:b80deceea541
Date: 2011-05-26 13:46 +0200
http://bitbucket.org/pypy/pypy/changeset/b80deceea541/
Log: first version of the hook - not very useful, passes only the code
object
diff --git a/pypy/module/pypyjit/interp_jit.py
b/pypy/module/pypyjit/interp_jit.py
--- a/pypy/module/pypyjit/interp_jit.py
+++ b/pypy/module/pypyjit/interp_jit.py
@@ -12,6 +12,8 @@
from pypy.interpreter.pycode import PyCode, CO_GENERATOR
from pypy.interpreter.pyframe import PyFrame
from pypy.interpreter.pyopcode import ExitFrame
+from pypy.interpreter.gateway import unwrap_spec
+from pypy.interpreter.baseobjspace import ObjSpace, W_Root
from opcode import opmap
from pypy.rlib.objectmodel import we_are_translated
@@ -49,8 +51,15 @@
greens = ['next_instr', 'is_being_profiled', 'pycode']
virtualizables = ['frame']
- def on_compile(self, looptoken, operations, type, *greenargs):
- pass
+ def on_compile(self, looptoken, operations, type, next_instr,
+ is_being_profiled, ll_pycode):
+ from pypy.rpython.annlowlevel import cast_base_ptr_to_instance
+
+ space = self.space
+ cache = space.fromcache(Cache)
+ if space.is_true(cache.w_compile_hook):
+ pycode = cast_base_ptr_to_instance(PyCode, ll_pycode)
+ space.call_function(cache.w_compile_hook, pycode)
def on_compile_bridge(self, orig_looptoken, operations, n):
pass
@@ -157,9 +166,11 @@
return space.call_args(w_callable, __args__)
class Cache(object):
- w_compile_hook = None
+ def __init__(self, space):
+ self.w_compile_hook = space.w_None
+@unwrap_spec(ObjSpace, W_Root)
def set_compile_hook(space, w_hook):
cache = space.fromcache(Cache)
- cache.w_hook = w_compile_hook
+ cache.w_compile_hook = w_hook
return space.w_None
diff --git a/pypy/module/pypyjit/test/test_jit_hook.py
b/pypy/module/pypyjit/test/test_jit_hook.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/pypyjit/test/test_jit_hook.py
@@ -0,0 +1,47 @@
+
+from pypy.conftest import gettestobjspace
+from pypy.interpreter.pycode import PyCode
+from pypy.interpreter.gateway import interp2app
+from pypy.jit.metainterp.history import LoopToken
+from pypy.jit.metainterp.resoperation import ResOperation, rop
+from pypy.rpython.annlowlevel import (cast_instance_to_base_ptr,
+ cast_base_ptr_to_instance)
+from pypy.module.pypyjit.interp_jit import pypyjitdriver
+
+class AppTestJitHook(object):
+ def setup_class(cls):
+ space = gettestobjspace(usemodules=('pypyjit',))
+ cls.space = space
+ w_f = space.appexec([], """():
+ def f():
+ pass
+ return f
+ """)
+ ll_code = cast_instance_to_base_ptr(w_f.code)
+
+ oplist = []
+
+ def interp_on_compile():
+ pypyjitdriver.on_compile(LoopToken(), oplist, 'loop',
+ 0, False, ll_code)
+
+ def interp_on_compile_bridge():
+ pypyjitdriver.on_compile_bridge(LoopToken(), oplist, 0)
+
+ cls.w_on_compile = space.wrap(interp2app(interp_on_compile))
+ cls.w_on_compile_bridge =
space.wrap(interp2app(interp_on_compile_bridge))
+
+ def test_on_compile(self):
+ import pypyjit
+ all = []
+
+ def hook(*args):
+ all.append(args)
+
+ self.on_compile()
+ pypyjit.set_compile_hook(hook)
+ assert not all
+ self.on_compile()
+ assert len(all) == 1
+ assert all[0][0].co_name == 'f'
+ print all
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit