Author: Maciej Fijalkowski <[email protected]>
Branch: jit-applevel-hook
Changeset: r44515:f1e5f71e71b6
Date: 2011-05-26 15:15 +0200
http://bitbucket.org/pypy/pypy/changeset/f1e5f71e71b6/

Log:    improve hook and document it a bit

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
@@ -58,11 +58,30 @@
         space = self.space
         cache = space.fromcache(Cache)
         if space.is_true(cache.w_compile_hook):
+            memo = {}
+            list_w = [space.wrap(logger.repr_of_resop(memo, op))
+                      for op in operations]
             pycode = cast_base_ptr_to_instance(PyCode, ll_pycode)
-            space.call_function(cache.w_compile_hook, pycode)
+            space.call_function(cache.w_compile_hook,
+                                space.wrap('main'),
+                                space.wrap(type),
+                                space.newtuple([pycode,
+                                space.wrap(next_instr),
+                                space.wrap(is_being_profiled)]),
+                                space.newlist(list_w))
 
     def on_compile_bridge(self, logger, orig_looptoken, operations, n):
-        pass
+        space = self.space
+        cache = space.fromcache(Cache)
+        if space.is_true(cache.w_compile_hook):
+            memo = {}
+            list_w = [space.wrap(logger.repr_of_resop(memo, op))
+                      for op in operations]
+            space.call_function(cache.w_compile_hook,
+                                space.wrap('main'),
+                                space.wrap('bridge'),
+                                space.wrap(n),
+                                space.newlist(list_w))
 
 pypyjitdriver = PyPyJitDriver(get_printable_location = get_printable_location,
                               get_jitcell_at = get_jitcell_at,
@@ -171,6 +190,21 @@
 
 @unwrap_spec(ObjSpace, W_Root)
 def set_compile_hook(space, w_hook):
+    """ set_compile_hook(hook)
+
+    Set a compiling hook that will be called each time a loop is compiled.
+    The hook will be called with the following signature:
+    hook(merge_point_type, loop_type, greenkey or guard_number, operations)
+
+    for now merge point type is always `main`
+
+    loop_type can be either `loop` `entry_bridge` or `bridge`
+    in case loop is not `bridge`, greenkey will be a set of constants
+    for jit merge point. in case it's `main` it'll be a tuple
+    (code, offset, is_being_profiled)
+
+    XXX write down what else
+    """
     cache = space.fromcache(Cache)
     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
--- a/pypy/module/pypyjit/test/test_jit_hook.py
+++ b/pypy/module/pypyjit/test/test_jit_hook.py
@@ -31,7 +31,7 @@
         [i1, i2]
         i3 = int_add(i1, i2)
         guard_true(i3) []
-        """)
+        """).operations
 
         def interp_on_compile():
             pypyjitdriver.on_compile(logger, LoopToken(), oplist, 'loop',
@@ -48,12 +48,22 @@
         all = []
 
         def hook(*args):
-            all.append(args)
+            assert args[0] == 'main'
+            assert args[1] in ['loop', 'bridge']
+            all.append(args[2:])
         
         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
+        assert all[0][0][0].co_name == 'f'
+        assert all[0][0][1] == 0
+        assert all[0][0][2] == False
+        assert len(all[0][1]) == 2
+        assert 'int_add' in all[0][1][0]
+        self.on_compile_bridge()
+        assert len(all) == 2
+        pypyjit.set_compile_hook(None)
+        self.on_compile()
+        assert len(all) == 2
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to