blob: 1822cc8355eaa88ed6bff9fe14c029fbdd489d8f [file] [log] [blame]
Kevin O'Connor5c4a8c62008-05-12 23:50:16 -04001#!/usr/bin/env python
2# Script that tries to find how much stack space each function in an
3# object is using.
4#
5# Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
6#
7# This file may be distributed under the terms of the GNU GPLv3 license.
8
9# Usage:
10# objdump -m i386 -M i8086 -M suffix -d out/rom16.o | tools/checkstack.py
11
12import sys
13import re
14
Kevin O'Connorf5cb0792008-06-07 10:11:36 -040015# List of functions we can assume are never called.
16#IGNORE = ['screenc', 'BX_PANIC', '__dprintf']
17IGNORE = ['screenc', 'BX_PANIC']
Kevin O'Connor5c4a8c62008-05-12 23:50:16 -040018
19# Find out maximum stack usage for a function
20def calcmaxstack(funcs, func):
21 info = funcs[func]
22 if func.split('.')[0] in IGNORE:
23 # Function is hardcoded to report 0 stack usage
24 info[1] = 0
25 return
26 # Find max of all nested calls.
27 max = info[0]
28 for addr, callfname, usage in info[2]:
29 callinfo = funcs[callfname]
30 if callinfo[1] is None:
31 calcmaxstack(funcs, callfname)
32 totusage = usage + callinfo[1]
33 if totusage > max:
34 max = totusage
35 info[1] = max
36
37hex_s = r'[0-9a-f]+'
38re_func = re.compile(r'^' + hex_s + r' <(?P<func>.*)>:$')
39re_asm = re.compile(
40 r'^[ ]*(?P<addr>' + hex_s + r'):\t.*\t'
Kevin O'Connorf5cb0792008-06-07 10:11:36 -040041 r'(addr32 )?(?P<insn>[a-z0-9]+ [^<]*)( <(?P<ref>.*)>)?$')
Kevin O'Connor5c4a8c62008-05-12 23:50:16 -040042re_usestack = re.compile(
43 r'^(push.*)|(sub.* [$](?P<num>0x' + hex_s + r'),%esp)$')
44
45def calc():
46 # funcs = {funcname: [basicstackusage, maxstackusage
47 # , [(addr, callfname, stackusage), ...]] }
Kevin O'Connorf5cb0792008-06-07 10:11:36 -040048 funcs = {'<indirect>': [0, 0, []]}
Kevin O'Connor5c4a8c62008-05-12 23:50:16 -040049 cur = None
50 atstart = 0
51 stackusage = 0
52
53 # Parse input lines
54 for line in sys.stdin.readlines():
55 m = re_func.match(line)
56 if m is not None:
57 # Found function
58 cur = [0, None, []]
59 funcs[m.group('func')] = cur
60 stackusage = 0
61 atstart = 1
Kevin O'Connorf5cb0792008-06-07 10:11:36 -040062 subfuncs = {}
Kevin O'Connor5c4a8c62008-05-12 23:50:16 -040063 continue
64 m = re_asm.match(line)
65 if m is not None:
66 insn = m.group('insn')
67
68 im = re_usestack.match(insn)
69 if im is not None:
Kevin O'Connorf5cb0792008-06-07 10:11:36 -040070 if insn[:5] == 'pushl' or insn[:6] == 'pushfl':
Kevin O'Connor5c4a8c62008-05-12 23:50:16 -040071 stackusage += 4
72 continue
Kevin O'Connorf5cb0792008-06-07 10:11:36 -040073 elif insn[:5] == 'pushw' or insn[:6] == 'pushfw':
74 stackusage += 2
75 continue
Kevin O'Connor5c4a8c62008-05-12 23:50:16 -040076 stackusage += int(im.group('num'), 16)
77
78 if atstart:
79 cur[0] = stackusage
80 atstart = 0
81
82 ref = m.group('ref')
Kevin O'Connorf5cb0792008-06-07 10:11:36 -040083 if ref is None:
84 if insn[:6] == 'lcallw':
85 stackusage += 4
86 ref = '<indirect>'
87 else:
88 # misc instruction - just ignore
89 continue
90 else:
91 # Jump or call insn
92 if '+' in ref:
93 # Inter-function jump - reset stack usage to
94 # preamble usage
95 stackusage = cur[0]
96 continue
97 if ref.split('.')[0] in IGNORE:
98 # Call ignored - list only for informational purposes
99 stackusage = 0
100 elif insn[:1] == 'j':
Kevin O'Connor5c4a8c62008-05-12 23:50:16 -0400101 # Tail call
102 stackusage = 0
Kevin O'Connorf5cb0792008-06-07 10:11:36 -0400103 elif insn[:5] == 'calll':
Kevin O'Connor5c4a8c62008-05-12 23:50:16 -0400104 stackusage += 4
105 else:
106 print "unknown call", ref
Kevin O'Connorf5cb0792008-06-07 10:11:36 -0400107 if (ref, stackusage) not in subfuncs:
Kevin O'Connor5c4a8c62008-05-12 23:50:16 -0400108 cur[2].append((m.group('addr'), ref, stackusage))
Kevin O'Connorf5cb0792008-06-07 10:11:36 -0400109 subfuncs[(ref, stackusage)] = 1
110 # Reset stack usage to preamble usage
111 stackusage = cur[0]
Kevin O'Connor5c4a8c62008-05-12 23:50:16 -0400112
113 continue
114
115 #print "other", repr(line)
116
117 # Calculate maxstackusage
118 for func, info in funcs.items():
119 if info[1] is not None:
120 continue
121 calcmaxstack(funcs, func)
122
123 # Show all functions
124 funcnames = funcs.keys()
125 funcnames.sort()
126 for func in funcnames:
127 basicusage, maxusage, calls = funcs[func]
128 if maxusage == 0:
129 continue
130 print "\n%s[%d,%d]:" % (func, basicusage, maxusage)
131 for addr, callfname, stackusage in calls:
132 callinfo = funcs[callfname]
Kevin O'Connorf5cb0792008-06-07 10:11:36 -0400133 print " %04s:%-40s [%d+%d,%d]" % (
Kevin O'Connor5c4a8c62008-05-12 23:50:16 -0400134 addr, callfname, stackusage, callinfo[0], stackusage+callinfo[1])
135
136def main():
137 calc()
138
139if __name__ == '__main__':
140 main()