-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataflow_analysis.py
34 lines (30 loc) · 1.05 KB
/
dataflow_analysis.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
from collections import deque
from typing import Dict, List
from graph import DirectedAdjList, transpose
from functools import reduce
from x86_ast import Jump, JumpIf, instr
def analyze_dataflow(G, transfer, bottom, join):
trans_G = transpose(G)
# liveness analysis: mapping 是 live_before_block
mapping = {}
for v in G.vertices():
mapping[v] = bottom
worklist = deque()
for v in G.vertices():
worklist.append(v)
while worklist:
node = worklist.pop()
input = reduce(join, [mapping[v] for v in trans_G.adjacent(node)], bottom)
output = transfer(node, input)
if output != mapping[node]:
mapping[node] = output
for v in G.adjacent(node):
worklist.append(v)
def build_cfg(basic_blocks: Dict[str, List[instr]]) -> DirectedAdjList:
cfg = DirectedAdjList()
for bb, stmts in basic_blocks.items():
for i in stmts:
match i:
case Jump(label) | JumpIf(_, label):
cfg.add_edge(bb, label)
return cfg