rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sys |
| 4 | import re |
| 5 | |
| 6 | hexrule = re.compile("([0-9a-fA-F]+)") |
| 7 | hex2byterule = re.compile("([0-9a-fA-F]{4})") |
| 8 | hexcolonrule = re.compile("([0-9a-fA-F]+)\:") |
| 9 | symbolrule = re.compile("<([\._a-zA-Z]+[\._0-9a-zA-Z]*)>:") |
| 10 | insrule = re.compile("([a-zA-Z][\.a-zA-Z]*)") |
| 11 | |
| 12 | currsymbol = "" |
| 13 | curraddress = 0 |
| 14 | count = 0 |
| 15 | |
| 16 | for line in sys.stdin: |
| 17 | t = line.split() |
| 18 | |
| 19 | if len(t) == 0: |
| 20 | continue |
| 21 | |
| 22 | try: |
| 23 | |
| 24 | # match the address |
| 25 | match = hexcolonrule.match(t[0]) |
| 26 | if match: |
| 27 | #print "%s %s" % (match, match.group(1)) |
| 28 | curraddress = int(match.group(1), 16) |
| 29 | #print "curraddress 0x%x" % curraddress |
| 30 | |
| 31 | # see if this is a symbol declaration |
| 32 | match = symbolrule.match(t[1]) |
| 33 | if match: |
| 34 | # print the previous count |
| 35 | if count > 0: |
| 36 | print "%d %s" % (count, currsymbol) |
| 37 | count = 0 |
| 38 | |
| 39 | #print "%s %s" % (match, match.group(1)) |
| 40 | currsymbol = str(match.group(1)) |
| 41 | #print "current symbol is now '%s'" % currsymbol |
| 42 | continue |
| 43 | |
| 44 | # see if it's a one or two byte opcode |
| 45 | iindex = 2 |
| 46 | match = hex2byterule.match(t[1]) |
| 47 | if not match: |
| 48 | continue |
| 49 | match = hex2byterule.match(t[2]) |
| 50 | if match: |
| 51 | #print "match %s, %s" % (match, match.group(0)) |
| 52 | iindex = 3 |
| 53 | |
| 54 | #print "instruction starts at index %d: '%s'" % (iindex, t[iindex]) |
| 55 | |
| 56 | # match the instruction string |
| 57 | insmatch = insrule.match(t[iindex]) |
| 58 | if not insmatch: |
| 59 | continue |
| 60 | ins = insmatch.group(1) |
| 61 | #print "instruction '%s'" % ins |
| 62 | |
| 63 | # look for a few special instructions |
| 64 | if ins == "push": |
| 65 | c = (len(t) - 1 - iindex) * 4 |
| 66 | #print "%d bytes pushed" % c |
| 67 | count += c |
| 68 | |
| 69 | # look for a few special instructions |
| 70 | if ins == "stmdb": |
| 71 | c = (len(t) - 2 - iindex) * 4 |
| 72 | #print "%d bytes stmed" % c |
| 73 | count += c |
| 74 | |
| 75 | if ins == "sub": |
| 76 | reg = t[iindex+1] |
| 77 | if reg == "sp,": |
| 78 | conststr = t[iindex+2] |
| 79 | c = int(conststr[1:]) |
| 80 | #print "subtracting from sp, val %d" % c |
| 81 | count += c |
| 82 | |
| 83 | except IndexError: |
| 84 | continue |
| 85 | except Exception as e: |
| 86 | print "Exception %s" % e |
| 87 | continue |
| 88 | |
| 89 | # print the last count |
| 90 | if count > 0: |
| 91 | print "%d %s" % (count, currsymbol) |
| 92 | |