Debian Codegraph @

rpython/flowspace/framestate.py

raw symbols SCIP
1 from rpython.flowspace.model import Variable, Constant, FSException
2 from rpython.rlib.unroll import SpecTag
3
4 def _copy(v):
5 from rpython.flowspace.flowcontext import FlowSignal
6 if isinstance(v, Variable):
7 return Variable(v)
8 elif isinstance(v, FlowSignal):
9 vars = [_copy(var) for var in v.args]
10 return v.rebuild(*vars)
11 else:
12 return v
13
14 def _union(seq1, seq2):
15 return [union(v1, v2) for v1, v2 in zip(seq1, seq2)]
16
17
18 class FrameState(object):
19 def __init__(self, locals_w, stack, last_exception, blocklist, next_offset):
20 self.locals_w = locals_w
21 self.stack = stack
22 self.last_exception = last_exception
23 self.blocklist = blocklist
24 self.next_offset = next_offset
25 self._mergeable = None
26
27 @property
28 def mergeable(self):
29 from rpython.flowspace.flowcontext import FlowSignal
30 if self._mergeable is not None:
31 return self._mergeable
32
33 self._mergeable = data = self.locals_w + recursively_flatten(self.stack)
34 if self.last_exception is None:
35 data.append(Constant(None))
36 data.append(Constant(None))
37 else:
38 data.append(self.last_exception.w_type)
39 data.append(self.last_exception.w_value)
40 return data
41
42 def copy(self):
43 "Make a copy of this state in which all Variables are fresh."
44 exc = self.last_exception
45 if exc is not None:
46 exc = FSException(_copy(exc.w_type), _copy(exc.w_value))
47 return FrameState(map(_copy, self.locals_w), map(_copy, self.stack),
48 exc, self.blocklist, self.next_offset)
49
50 def getvariables(self):
51 return [w for w in self.mergeable if isinstance(w, Variable)]
52
53 def matches(self, other):
54 """Two states match if they only differ by using different Variables
55 at the same place"""
56 # safety check, don't try to compare states with different
57 # nonmergeable states
58 assert self.blocklist == other.blocklist
59 assert self.next_offset == other.next_offset
60 for w1, w2 in zip(self.mergeable, other.mergeable):
61 if not (w1 == w2 or (isinstance(w1, Variable) and
62 isinstance(w2, Variable))):
63 return False
64 return True
65
66 def _exc_args(self):
67 if self.last_exception is None:
68 return [Constant(None), Constant(None)]
69 else:
70 return [self.last_exception.w_type,
71 self.last_exception.w_value]
72
73 def union(self, other):
74 """Compute a state that is at least as general as both self and other.
75 A state 'a' is more general than a state 'b' if all Variables in 'b'
76 are also Variables in 'a', but 'a' may have more Variables.
77 """
78 try:
79 locals = _union(self.locals_w, other.locals_w)
80 stack = _union(self.stack, other.stack)
81 if self.last_exception is None and other.last_exception is None:
82 exc = None
83 else:
84 args1 = self._exc_args()
85 args2 = other._exc_args()
86 exc = FSException(union(args1[0], args2[0]),
87 union(args1[1], args2[1]))
88 except UnionError:
89 return None
90 return FrameState(locals, stack, exc, self.blocklist, self.next_offset)
91
92 def getoutputargs(self, targetstate):
93 "Return the output arguments needed to link self to targetstate."
94 result = []
95 mergeable = self.mergeable
96 for i, w_target in enumerate(targetstate.mergeable):
97 if isinstance(w_target, Variable):
98 result.append(mergeable[i])
99 return result
100
101 class UnionError(Exception):
102 "The two states should be merged."
103
104
105 def union(w1, w2):
106 "Union of two variables or constants."
107 from rpython.flowspace.flowcontext import FlowSignal
108 if w1 == w2:
109 return w1
110 if w1 is None or w2 is None:
111 return None # if w1 or w2 is an undefined local, we "kill" the value
112 # coming from the other path and return an undefined local
113 if isinstance(w1, Variable) or isinstance(w2, Variable):
114 return Variable() # new fresh Variable
115 if isinstance(w1, Constant) and isinstance(w2, Constant):
116 if isinstance(w1.value, SpecTag) or isinstance(w2.value, SpecTag):
117 raise UnionError
118 else:
119 return Variable() # generalize different constants
120 if isinstance(w1, FlowSignal) and isinstance(w2, FlowSignal):
121 if type(w1) is not type(w2):
122 raise UnionError
123 vars = [union(v1, v2) for v1, v2 in zip(w1.args, w2.args)]
124 return w1.rebuild(*vars)
125 if isinstance(w1, FlowSignal) or isinstance(w2, FlowSignal):
126 raise UnionError
127 raise TypeError('union of %r and %r' % (w1.__class__.__name__,
128 w2.__class__.__name__))
129
130
131 def recursively_flatten(lst):
132 from rpython.flowspace.flowcontext import FlowSignal
133 if not lst:
134 return lst
135 i = -1
136 for i in range(len(lst)):
137 if isinstance(lst[i], FlowSignal):
138 break
139 else:
140 return lst
141 lst = lst[:]
142 while i < len(lst):
143 unroller = lst[i]
144 if not isinstance(unroller, FlowSignal):
145 i += 1
146 else:
147 lst[i:i + 1] = unroller.args
148 return lst