diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2014-03-01 06:05:35 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2014-03-01 06:05:35 -0500 |
commit | c9348c43feee1bead7fe28857b87074b163f15e0 (patch) | |
tree | 3a42fd5001fcda1f2fe2125b42e97f5dc4df8fc6 /lab | |
parent | 442a0ba30c5c803e649331d8c6e1e449fcc1a161 (diff) | |
download | python-coveragepy-git-c9348c43feee1bead7fe28857b87074b163f15e0.tar.gz |
Counting of opcodes in disassembled code (for akaptur's question)
Diffstat (limited to 'lab')
-rw-r--r-- | lab/parser.py | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/lab/parser.py b/lab/parser.py index a8e03eec..cc8266a9 100644 --- a/lab/parser.py +++ b/lab/parser.py @@ -1,6 +1,9 @@ """Parser.py: a main for invoking code in coverage/parser.py""" +from __future__ import division + import glob, os, sys +import collections from optparse import OptionParser import disgen @@ -8,6 +11,7 @@ import disgen from coverage.misc import CoverageException from coverage.parser import ByteParser, CodeParser +opcode_counts = collections.Counter() class ParserMain(object): """A main for code parsing experiments.""" @@ -25,6 +29,10 @@ class ParserMain(object): help="Disassemble" ) parser.add_option( + "-H", action="store_true", dest="histogram", + help="Count occurrences of opcodes" + ) + parser.add_option( "-R", action="store_true", dest="recursive", help="Recurse to find source files" ) @@ -51,18 +59,25 @@ class ParserMain(object): else: self.one_file(options, args[0]) + if options.histogram: + total = sum(opcode_counts.values()) + print("{} total opcodes".format(total)) + for opcode, number in opcode_counts.most_common(): + print("{:20s} {:6d} {:.1%}".format(opcode, number, number/total)) + + def one_file(self, options, filename): """Process just one file.""" try: bp = ByteParser(filename=filename) - except CoverageException as err: + except Exception as err: print("%s" % (err,)) return if options.dis: print("Main code:") - self.disassemble(bp) + self.disassemble(bp, histogram=options.histogram) arcs = bp._all_arcs() if options.chunks and not options.dis: @@ -104,7 +119,7 @@ class ParserMain(object): (lineno, m0, m1, m2, m3, a, ltext) ) - def disassemble(self, byte_parser): + def disassemble(self, byte_parser, histogram=False): """Disassemble code, for ad-hoc experimenting.""" for bp in byte_parser.child_parsers(): @@ -117,6 +132,9 @@ class ParserMain(object): print("\n%s: " % bp.code) upto = None for disline in disgen.disgen(bp.code): + if histogram: + opcode_counts[disline.opcode] += 1 + continue if disline.first: if srclines: upto = upto or disline.lineno-1 |