-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
105 lines (89 loc) · 3.41 KB
/
parser.py
File metadata and controls
105 lines (89 loc) · 3.41 KB
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
101
102
103
104
105
'''TAKE A PIECE OF CODE AND GENERATE A TRACE OF THE BRANCH TAKEN FOR A CORESSSPONDING ADRESS'''
import ast
import itertools
address_counter = itertools.count(start=1000)
# --- Branch extraction using line numbers ---
def extract_branches(node, branches_map=None):
if branches_map is None:
branches_map = {}
if isinstance(node, ast.If):
line_no = node.lineno
if line_no not in branches_map:
branches_map[line_no] = next(address_counter)
# Recurse into 'if' body
for stmt in node.body:
extract_branches(stmt, branches_map)
# Recurse into 'else' or 'elif'
if node.orelse:
if len(node.orelse) == 1 and isinstance(node.orelse[0], ast.If):
extract_branches(node.orelse[0], branches_map)
else:
for stmt in node.orelse:
extract_branches(stmt, branches_map)
for child in ast.iter_child_nodes(node):
extract_branches(child, branches_map)
return branches_map
def generate_branch_trace():
"geenrate a list of branches and their addresses to be used in the predictor"
# --- Ar function with loops and branches ---
code = """
def simulate_scores():
scores = [45, 52, 63, 78, 82, 91, 100, 67, 88, 49]
for _ in range(5): # repeat loop to generate history
for score in scores:
if score >= 90:
grade = "A"
if score == 100:
comment = "Perfect!"
else:
comment = "Excellent"
elif score >= 75:
grade = "B"
if score >= 85:
comment = "Good job"
else:
comment = "Well done"
elif score >= 60:
grade = "C"
comment = "Needs improvement"
else:
grade = "F"
if score < 50:
comment = "Failing badly"
else:
comment = "Barely passing"
if score % 2 == 0:
even_comment = "Even score"
else:
even_comment = "Odd score"
"""
tree = ast.parse(code)
branches_map = extract_branches(tree)
# --- Generate repeated branch trace ---
trace = []
scores = [45, 52, 63, 78, 82, 91, 100, 67, 88, 49]
for _ in range(5): # repeat loop
for score in scores:
for line_no, addr in branches_map.items():
# Find the node corresponding to this line number
# Evaluate condition if possible
node = None
for n in ast.walk(tree):
if isinstance(n, ast.If) and n.lineno == line_no:
node = n
break
if node is None:
continue # safety
# Compile and evaluate the condition
cond_expr = compile(ast.Expression(node.test), filename="<ast>", mode="eval")
taken = int(eval(cond_expr, {'score': score}))
trace.append((addr, taken))
return trace, branches_map
# --- Main execution (only runs when script is executed directly) ---
if __name__ == "__main__":
trace, branches_map = generate_branch_trace()
# --- Output ---
print("Addresses:", list(branches_map.values()))
print("Trace (address, taken):")
for t in trace:
print(t)