Source

hack2 / hacks / asmgen.py

from pprint import pprint
import time, pypyjit, ctypes
from pypy.jit.backend.x86.tool.viewcode import machine_code_dump

def f():
    i = 0
    while i < 1000000000:
        i += 1

buf = None

def hook(jd, type, (code, next_instr, is_profiled), ops, asmstart, asmlen):
    global buf
    buf = (ctypes.c_char * asmlen).from_address(asmstart)
#    asm = ctypes.string_at(asmstart, asmlen)
#    print hex(asmstart)
#    print hex(ord(asm[208]))
#    pprint(list(machine_code_dump(asm, asmstart, 'x86_64')))

pypyjit.set_compile_hook(hook)
t0 = time.time()
f()
t1 = time.time()
buf[208] = '\x02' # increase the iteration ratio 2x, we want to go faster!
f()
t2 = time.time()
print t2 - t1, t1 - t0
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.