3 # Feed this script the output of objdump -M raw --no-show-raw-insn ppc-prog
5 # It will print the occurrence count of each opcode,
6 # and under it, indented by one character,
7 # the occurrence count of each operand.
9 # Registers used as operands or as base addresses are counted
10 # separately; immediates and offsets are grouped per bit length;
11 # branch target offsets are grouped by range bit length.
16 insn
= re
.compile('\s+(?P<addr>[0-9a-f]+):\s+(?P<opcode>[^ \n]+) *(?P<operands>.*)[\n]?')
18 opkind
= re
.compile('(?P<immediate>-?[0-9]+)|(?P<branch>[0-9a-f]+)(?: <.*>)?|(?P<offset>-?[0-9]+)\((?P<basereg>r[0-9]+)\)')
23 match
= opkind
.fullmatch(op
)
27 elif match
['immediate'] is not None:
28 op
= "%i-bit" % int (op
).bit_length ()
29 elif match
['branch'] is not None:
30 op
= "%i-bit range" % (int (match
['branch'], 16) - int (addr
, 16)).bit_length ()
31 elif match
['offset'] is not None:
32 count(ops
, match
['offset'])
35 raise "unrecognized operand kind"
42 for line
in sys
.stdin
:
43 match
= insn
.fullmatch(line
)
48 opcode
= match
['opcode']
49 operands
= match
['operands']
51 if opcode
not in histogram
:
53 histogram
[opcode
] = [1,ops
]
55 histogram
[opcode
][0] += 1
56 ops
= histogram
[opcode
][1]
59 for operand
in operands
.split(','):
62 hist
= list(histogram
.items())
63 hist
.sort(key
= (lambda x
: x
[1][0]))
65 print('%6i %s:' % (x
[1][0], x
[0]))
66 ops
= list(x
[1][1].items())
67 ops
.sort(key
= (lambda x
: x
[1]))
69 print(' %6i %s' % (x
[1], x
[0]))