[arch][arm] add python app to try to guess stack usage per function

Just a simple app that looks at push and add sp, #num lines to guess
the size of the function. Only tested against thumb2 code.

Generates build-*/lk.stack
This commit is contained in:
Travis Geiselbrecht
2012-11-11 13:59:30 -08:00
parent 86341de1df
commit ee9000e002
2 changed files with 86 additions and 0 deletions

View File

@@ -181,4 +181,12 @@ $(BUILDDIR)/system-twosegment.ld: $(LOCAL_DIR)/system-twosegment.ld
@$(MKDIR)
$(NOECHO)sed "s/%ROMBASE%/$(ROMBASE)/;s/%MEMBASE%/$(MEMBASE)/;s/%MEMSIZE%/$(MEMSIZE)/" < $< > $@
# arm specific script to try to guess stack usage
$(OUTELF).stack: LOCAL_DIR:=$(LOCAL_DIR)
$(OUTELF).stack: $(OUTELF).lst
$(NOECHO)echo generating stack usage $@
$(NOECHO)$(LOCAL_DIR)/stackusage < $< | sort -n -k 1 -r > $@
EXTRA_BUILDDEPS += $(OUTELF).stack
include make/module.mk

78
arch/arm/stackusage Executable file
View File

@@ -0,0 +1,78 @@
#!/usr/bin/env python
import sys
import re
hexrule = re.compile("([0-9a-fA-F]+)")
hex2byterule = re.compile("([0-9a-fA-F]{4})")
hexcolonrule = re.compile("([0-9a-fA-F]+)\:")
symbolrule = re.compile("<([_a-zA-Z]+[_0-9a-zA-Z]*)>:")
insrule = re.compile("([a-zA-Z][\.a-zA-Z]*)")
currsymbol = ""
curraddress = 0
for line in sys.stdin:
t = line.split()
if len(t) == 0:
continue
try:
count = 0
# match the address
match = hexcolonrule.match(t[0])
if match:
#print "%s %s" % (match, match.group(1))
curraddress = int(match.group(1), 16)
#print "curraddress 0x%x" % curraddress
# see if this is a symbol declaration
match = symbolrule.match(t[1])
if match:
#print "%s %s" % (match, match.group(1))
currsymbol = str(match.group(1))
#print "current symbol is now '%s'" % currsymbol
continue
# see if it's a one or two byte opcode
iindex = 2
match = hex2byterule.match(t[1])
if not match:
continue
match = hex2byterule.match(t[2])
if match:
#print "match %s, %s" % (match, match.group(0))
iindex = 3
#print "instruction starts at index %d: '%s'" % (iindex, t[iindex])
# match the instruction string
insmatch = insrule.match(t[iindex])
if not insmatch:
continue
ins = insmatch.group(1)
#print "instruction '%s'" % ins
# look for a few special instructions
if ins == "push":
count = (len(t) - 1 - iindex) * 4
#print "%d words pushed" % count
if ins == "sub":
reg = t[iindex+1]
if reg == "sp,":
conststr = t[iindex+2]
count = int(conststr[1:])
#print "subtracting from sp, val %d" % count
# if we found a stack offset, print it
if count > 0:
print "%d %s" % (count, currsymbol)
except IndexError:
continue
except Exception as e:
print "Exception %s" % e
continue