1#!/usr/bin/env python
2#
3# Mark functions in an arm assembly file. This is done by surrounding the
4# function with "# -- Begin Name" and "# -- End Name"
5# (This script is designed for arm ios assembly syntax)
6import sys
7import re
8
9inp = open(sys.argv[1], "r").readlines()
10
11# First pass
12linenum = 0
13INVALID=-100
14last_align = INVALID
15last_code = INVALID
16last_globl = INVALID
17begin = INVALID
18begins = dict()
19for line in inp:
20    linenum += 1
21    if ".align" in line:
22        last_align = linenum
23    if ".code" in line:
24        last_code = linenum
25    if ".globl" in line:
26        last_globl = linenum
27    m = re.search(r'.thumb_func\s+(\w+)', line)
28    if m:
29        funcname = m.group(1)
30        if last_code == last_align+1 and (linenum - last_code) < 4:
31            begin = last_align
32            if last_globl+1 == last_align:
33                begin = last_globl
34    if line == "\n" and begin != INVALID:
35        end = linenum
36        triple = (funcname, begin, end)
37        begins[begin] = triple
38        begin = INVALID
39
40# Second pass: Mark
41out = open(sys.argv[1], "w")
42in_func = None
43linenum = 0
44for line in inp:
45    linenum += 1
46    if in_func is not None and linenum == end:
47        out.write("# -- End  %s\n" % in_func)
48        in_func = None
49
50    triple = begins.get(linenum)
51    if triple is not None:
52        in_func, begin, end = triple
53        out.write("# -- Begin  %s\n" % in_func)
54    out.write(line)
55