-
Notifications
You must be signed in to change notification settings - Fork 20
/
arm_frequency.py
71 lines (53 loc) · 2.14 KB
/
arm_frequency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
Small script to parse the output of arm-linux-androideabi-objdump
giving as a result the ammount of times a particular opname is used.
This was used to prioritize the implementation of the most used instructions
in an disassembler / emulator.
Agustin Gianni ([email protected])
"""
import re
import sys
import operator
from collections import defaultdict
setsflags_skip = ["cps", "mls", "mrs", "smmls", "srs", "subs", "vabs", "vcls", "vfms", "vmls", "vmrs", "vnmls", "qabs", "vrecps", "vrsqrts"]
skip_list = ["cbnz", "svc", "lsls", "sbcs", "bics", "rscs", "movs", "muls", "mls", "teq", "adcs", "smmls", "vcls", "vmls"]
cond_codes = ["eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc", "hi", "ls", "ge", "lt", "gt", "le"]
def drop_garbage(opname):
if opname.startswith("it"):
return "it"
# Drop the wide specifier.
t = opname[:-2] if (opname.endswith(".w") or opname.endswith(".n")) else opname
# Some opcodes end with the leters of condition codes but have nothing to do with them.
if t in skip_list:
# Remove the 'setsflags' indicator.
if t[-1] == "s" and t not in setsflags_skip:
return t[:-1]
return t
# Drop condition codes.
for code in cond_codes:
if t.endswith(code):
t2 = t[:-2]
if len(t2) <= 2 and not t2 in ["b", "bl", "bx"]:
print "You need to add the following opname to the 'skip_list' %s" % t
t = t2
break
# Remove the 'setsflags' indicator.
if t[-1] == "s" and t not in setsflags_skip:
return t[:-1]
return t
# Match the opcode, opname and arguments of objdump's output for ARM.
regex_str = "\s*[0-9a-f]+\:\s+([0-9a-f]+\s+[0-9a-f]*)\s+([a-zA-Z.]+)\s+(.+)"
regex = re.compile(regex_str)
opname_freq = defaultdict(lambda: 0, {})
with open(sys.argv[1]) as f:
for line in f:
r = regex.search(line)
if not r:
continue
opcode, opname, args = r.groups()
if opname.lower() in [".byte", ".word", ".dword", ".qword", ".short"]:
continue
opname = drop_garbage(opname)
opname_freq[opname] += 1
for el in sorted(opname_freq.iteritems(), key=operator.itemgetter(1), reverse=True):
print "Instruction %10s is used %10d times | decode_%s" % (el[0], el[1], el[0])