-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssa_visitor.py
56 lines (34 loc) · 1.32 KB
/
ssa_visitor.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
from collections import defaultdict
from visitors import Visitor
class Keys:
SSA_NAME = "ssa_name"
class SingleStaticAssignmentVisitor(Visitor):
def __init__(self, ast):
super(SingleStaticAssignmentVisitor, self).__init__(ast)
self.super = super(SingleStaticAssignmentVisitor, self)
self._variable_ssa_map = defaultdict(list)
self._ssa_variable_map = dict()
self._scope = None
self._scoped_variable_ssa_map = defaultdict(dict)
@property
def variable_ssa_map(self):
return self._variable_ssa_map
@property
def ssa_variable_map(self):
return self._ssa_variable_map
def make_ssa_name(self, name):
ssa_name = f"{name}_{len(self._variable_ssa_map[name])}"
self._variable_ssa_map[name].append(ssa_name)
self._ssa_variable_map[ssa_name] = name
return ssa_name
def visit_Assign_target(self, node):
target_id = self.super.visit_Name(node)
node[Keys.SSA_NAME] = self.make_ssa_name(target_id)
return target_id
def visit_Name(self, node):
name = self.super.visit_Name(node)
if name not in self._variable_ssa_map:
node[Keys.SSA_NAME] = self.make_ssa_name(name)
else:
node[Keys.SSA_NAME] = self._variable_ssa_map[name][-1]
return name