forked from lowRISC/ibex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprof_script.py
100 lines (86 loc) · 3.83 KB
/
prof_script.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import re
# Function to parse the assembly code
def parse_assembly(assembly_code):
functions = {} # Dictionary to store function details
current_function = None
# Regular expressions to match function names and addresses
function_regex = r'<(.*?)>'
address_regex = r'^\s*(\S+):'
# Iterate through each line of the assembly code
for line in assembly_code.split('\n'):
# Check if the line contains a fu;'nction name
match = re.match(function_regex, line)
if match:
current_function = match.group(1)
functions[current_function] = {'start_address': None, 'ret_addresses': []}
continue
# Check if the line contains an address
match = re.match(address_regex, line)
if match and current_function:
address = match.group(1)
print(address)
if not functions[current_function]['start_address']:
functions[current_function]['start_address'] = address
if 'ret' in line or 'mret' in line:
print("found ret or mret")
# address_match = re.search(r'(\S+):\s*(ret|mret)', line)
address = match.group(1)
# if address_match:
# address = address_match.group(1)
functions[current_function]['ret_addresses'].append(address)
continue
# Check if the line contains ret or mret instructions
# match = re.match(address_regex, line)
# if match and current_function:
# address = match.group(1)
# print(address)
# if 'ret' in line or 'mret' in line:
# functions[current_function]['ret_addresses'].append(address)
# if current_function and ('ret' in line or 'mret' in line):
# if address:
# functions[current_function]['ret_addresses'].append(address)
if current_function:
print(line)
if 'ret' in line or 'mret' in line:
print("found ret or mret")
address_match = re.search(r'(\S+):\s*(ret|mret)', line)
if address_match:
address = address_match.group(1)
functions[current_function]['ret_addresses'].append(address)
return functions
# Function to write function details to a file
# def write_to_file(functions):
# with open('function_table.txt', 'w') as f:
# f.write("Function Name\tStart Address\tReturn Addresses\n")
# for function, details in functions.items():
# start_address = details['start_address']
# ret_addresses = ', '.join(details['ret_addresses'])
# f.write(f"{function}\t{start_address}\t{ret_addresses}\n")
def write_to_file(functions):
with open('function_table.txt', 'w') as f:
f.write("Function Name\tStart Address\tReturn Addresses\n")
for function, details in functions.items():
start_address = details['start_address']
ret_addresses = ', '.join(details['ret_addresses'])
f.write(f"{function}\t\t\t{start_address}\t\t\t{ret_addresses}\n")
# Sample assembly code
assembly_code = """
<putchar>:
100084: 0ff57793 andi a5,a0,255
100088: 00020737 lui a4,0x20
10008c: c31c sw a5,0(a4)
10008e: 8082 ret
<puts>:
100090: 00020737 lui a4,0x20
100094: 00054783 lbu a5,0(a0)
100098: e399 bnez a5,10009e <puts+0xe>
10009a: 4501 li a0,0
10009c: 8082 ret
10009e: 0505 addi a0,a0,1
1000a0: c31c sw a5,0(a4)
1000a2: bfcd j 100094 <puts+0x4>
"""
# Parse the assembly code
functions = parse_assembly(assembly_code)
# Write function details to a file
write_to_file(functions)