Commits

Maciej Fijalkowski  committed acefa0d

Some progress - display exact lines from source code

  • Participants
  • Parent commits 97b1b55

Comments (0)

Files changed (3)

File disassembler.py

 
 import sys
 import types
+import inspect
 
 from opcode import *
 from opcode import __all__ as _opcodes_all
 class CodeRepresentation(object):
     """ Representation of opcodes
     """
-    def __init__(self, opcodes):
+    def __init__(self, opcodes, source):
         self.opcodes = opcodes
         self.map = {}
         for opcode in opcodes:
             self.map[opcode.pos] = opcode
+        self.source = source.split("\n")
 
 def _setup():
     for opcode in opname:
 
 def disassemble(co, lasti=-1):
     """Disassemble a code object."""
+    source = inspect.getsource(co)
     code = co.co_code
     labels = findlabels(code)
     linestarts = dict(findlinestarts(co))
             #     print '(' + free[oparg] + ')',
         else:
             oparg = None
-        res.append(globals()[opname[op]](pos, lastline, oparg))
-    return CodeRepresentation(res)
+        res.append(globals()[opname[op].replace('+', '_')](pos, lastline, oparg))
+    return CodeRepresentation(res, source)
 
 def disassemble_string(code, lasti=-1, varnames=None, names=None,
                        constants=None):
 
-import re
+import re, sys
 from pypy.jit.metainterp.resoperation import rop
 from module_finder import load_code
 from disassembler import dis
         return "%s, file '%s', line %d" % (self.name, self.filename,
                                            self.startlineno)
 
-    def getlineno(self):
+    def getcode(self):
         if self.code is None:
             self.code = dis(load_code(self.filename, self.name,
                                       self.startlineno))
-        return self.code.map[self.bytecode_no].lineno
+        return self.code
+
+    def getlineno(self):
+        code = self.getcode()
+        return code.map[self.bytecode_no].lineno
     lineno = property(getlineno)
 
     def __repr__(self):
         return "[%s]" % ", ".join([repr(op) for op in self.operations])
 
+    def pretty_print(self, out):
+        pass
+
 class Loop(object):
     filename = None
     name = None
     def __repr__(self):
         return "[%s]" % ", ".join([repr(chunk) for chunk in self.chunks])
 
+    def pretty_print(self, out):
+        print >>out, "Loop starting at %s in %s at %d" % (self.name,
+                                        self.filename, self.startlineno)
+        lineno = -1
+        for chunk in self.chunks:
+            if chunk.filename is not None and chunk.lineno != lineno:
+                lineno = chunk.lineno
+                source = chunk.getcode().source[chunk.lineno -
+                                                chunk.startlineno]
+                print >>out, "  ", source
+            chunk.pretty_print(out)
+
 def slice_debug_merge_points(loop):
     so_far = []
     res = []
 def parse_log_counts(lines):
     for line in lines:
         pass
+
+if __name__ == '__main__':
+    from pypy.tool.logparser import parse_log_file, extract_category
+    from pypy.jit.metainterp.test.oparser import parse
+
+    log = parse_log_file('log')
+    loops = [parse(l, no_namespace=True, nonstrict=True) for l in
+             extract_category(log, "jit-log-opt-")]
+    loops = [slice_debug_merge_points(loop) for loop in loops]
+    for i, loop in enumerate(loops):
+        loop.pretty_print(sys.stdout)

File module_finder.py

 
-import os, sys, marshal, types, ast
+import os, sys, marshal, types
 
 def _all_codes_from(code):
     res = {}
         code = marshal.loads(open(fname).read()[8:])
         assert isinstance(code, types.CodeType)
     elif fname.endswith('.py'):
-        code = compile(ast.parse(open(fname).read()), fname, 'exec')
+        code = compile(open(fname).read(), fname, 'exec')
     else:
+        import pdb
+        pdb.set_trace()
         raise Exception("Unknown file extension: %s" % fname)
     return _all_codes_from(code)