| 1 |
from __future__ import print_function |
| 2 |
|
| 3 |
from rpython.rtyper.lltypesystem.lltype import DelayedPointer |
| 4 |
from rpython.tool.algo.unionfind import UnionFind |
| 5 |
|
| 6 |
|
| 7 |
class GraphAnalyzer(object): |
| 8 |
verbose = False |
| 9 |
explanation = None |
| 10 |
|
| 11 |
def __init__(self, translator): |
| 12 |
self.translator = translator |
| 13 |
self._analyzed_calls = UnionFind(lambda graph: Dependency(self)) |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
@staticmethod |
| 18 |
def bottom_result(): |
| 19 |
raise NotImplementedError("abstract base class") |
| 20 |
|
| 21 |
@staticmethod |
| 22 |
def top_result(): |
| 23 |
raise NotImplementedError("abstract base class") |
| 24 |
|
| 25 |
@staticmethod |
| 26 |
def is_top_result(result): |
| 27 |
|
| 28 |
return False |
| 29 |
|
| 30 |
@staticmethod |
| 31 |
def result_builder(): |
| 32 |
raise NotImplementedError("abstract base class") |
| 33 |
|
| 34 |
@staticmethod |
| 35 |
def add_to_result(result, other): |
| 36 |
raise NotImplementedError("abstract base class") |
| 37 |
|
| 38 |
@staticmethod |
| 39 |
def finalize_builder(result): |
| 40 |
raise NotImplementedError("abstract base class") |
| 41 |
|
| 42 |
@staticmethod |
| 43 |
def join_two_results(result1, result2): |
| 44 |
raise NotImplementedError("abstract base class") |
| 45 |
|
| 46 |
def analyze_simple_operation(self, op, graphinfo=None): |
| 47 |
raise NotImplementedError("abstract base class") |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
def analyze_exceptblock(self, block, seen=None): |
| 52 |
return self.bottom_result() |
| 53 |
|
| 54 |
def analyze_exceptblock_in_graph(self, graph, block, seen=None): |
| 55 |
return self.analyze_exceptblock(block, seen) |
| 56 |
|
| 57 |
def analyze_startblock(self, block, seen=None): |
| 58 |
return self.bottom_result() |
| 59 |
|
| 60 |
def analyze_external_call(self, funcobj, seen=None): |
| 61 |
result = self.bottom_result() |
| 62 |
if hasattr(funcobj, '_callbacks'): |
| 63 |
bk = self.translator.annotator.bookkeeper |
| 64 |
for function in funcobj._callbacks.callbacks: |
| 65 |
desc = bk.getdesc(function) |
| 66 |
for graph in desc.getgraphs(): |
| 67 |
result = self.join_two_results( |
| 68 |
result, self.analyze_direct_call(graph, seen)) |
| 69 |
return result |
| 70 |
|
| 71 |
def analyze_link(self, graph, link): |
| 72 |
return self.bottom_result() |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
def compute_graph_info(self, graph): |
| 77 |
return None |
| 78 |
|
| 79 |
def explain_analyze_slowly(self, op): |
| 80 |
|
| 81 |
self.__init__(self.translator) |
| 82 |
self.explanation = explanation = [] |
| 83 |
oldverbose = self.verbose |
| 84 |
self.verbose = True |
| 85 |
try: |
| 86 |
self.analyze(op) |
| 87 |
finally: |
| 88 |
del self.explanation |
| 89 |
self.verbose = oldverbose |
| 90 |
explanation.reverse() |
| 91 |
return explanation |
| 92 |
|
| 93 |
def analyze(self, op, seen=None, graphinfo=None): |
| 94 |
if op.opname == "direct_call": |
| 95 |
try: |
| 96 |
funcobj = op.args[0].value._obj |
| 97 |
except DelayedPointer: |
| 98 |
return self.top_result() |
| 99 |
if funcobj is None: |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
return self.bottom_result() |
| 104 |
if getattr(funcobj, 'external', None) is not None: |
| 105 |
x = self.analyze_external_call(funcobj, seen) |
| 106 |
if self.verbose and x: |
| 107 |
self.dump_info('analyze_external_call %s: %r' % (op, x)) |
| 108 |
return x |
| 109 |
try: |
| 110 |
graph = funcobj.graph |
| 111 |
except AttributeError: |
| 112 |
return self.top_result() |
| 113 |
x = self.analyze_direct_call(graph, seen) |
| 114 |
if self.verbose and x: |
| 115 |
self.dump_info('analyze_direct_call(%s): %r' % (graph, x)) |
| 116 |
return x |
| 117 |
elif op.opname == "indirect_call": |
| 118 |
graphs = op.args[-1].value |
| 119 |
if graphs is None: |
| 120 |
if self.verbose: |
| 121 |
self.dump_info('%s to unknown' % (op,)) |
| 122 |
return self.top_result() |
| 123 |
x = self.analyze_indirect_call(graphs, seen) |
| 124 |
if self.verbose and x: |
| 125 |
self.dump_info('analyze_indirect_call(%s): %r' % (graphs, x)) |
| 126 |
return x |
| 127 |
x = self.analyze_simple_operation(op, graphinfo) |
| 128 |
if self.verbose and x: |
| 129 |
self.dump_info('%s: %r' % (op, x)) |
| 130 |
return x |
| 131 |
|
| 132 |
def dump_info(self, info): |
| 133 |
st = '[%s] %s' % (self.__class__.__name__, info) |
| 134 |
if self.explanation is not None: |
| 135 |
self.explanation.append(st) |
| 136 |
else: |
| 137 |
print(st) |
| 138 |
|
| 139 |
def analyze_direct_call(self, graph, seen=None): |
| 140 |
if seen is None: |
| 141 |
seen = DependencyTracker(self) |
| 142 |
if not seen.enter(graph): |
| 143 |
return seen.get_cached_result(graph) |
| 144 |
result = self.result_builder() |
| 145 |
graphinfo = self.compute_graph_info(graph) |
| 146 |
for block in graph.iterblocks(): |
| 147 |
if block is graph.startblock: |
| 148 |
result = self.add_to_result( |
| 149 |
result, |
| 150 |
self.analyze_startblock(block, seen) |
| 151 |
) |
| 152 |
elif block is graph.exceptblock: |
| 153 |
result = self.add_to_result( |
| 154 |
result, |
| 155 |
self.analyze_exceptblock_in_graph(graph, block, seen) |
| 156 |
) |
| 157 |
if not self.is_top_result(result): |
| 158 |
for op in block.operations: |
| 159 |
result = self.add_to_result( |
| 160 |
result, |
| 161 |
self.analyze(op, seen, graphinfo) |
| 162 |
) |
| 163 |
if self.is_top_result(result): |
| 164 |
break |
| 165 |
if not self.is_top_result(result): |
| 166 |
for exit in block.exits: |
| 167 |
result = self.add_to_result( |
| 168 |
result, |
| 169 |
self.analyze_link(exit, seen) |
| 170 |
) |
| 171 |
if self.is_top_result(result): |
| 172 |
break |
| 173 |
if self.is_top_result(result): |
| 174 |
break |
| 175 |
result = self.finalize_builder(result) |
| 176 |
seen.leave_with(result) |
| 177 |
return result |
| 178 |
|
| 179 |
def analyze_indirect_call(self, graphs, seen=None): |
| 180 |
result = self.result_builder() |
| 181 |
for graph in graphs: |
| 182 |
result = self.add_to_result( |
| 183 |
result, |
| 184 |
self.analyze_direct_call(graph, seen) |
| 185 |
) |
| 186 |
if self.is_top_result(result): |
| 187 |
break |
| 188 |
return self.finalize_builder(result) |
| 189 |
|
| 190 |
def analyze_all(self, graphs=None): |
| 191 |
if graphs is None: |
| 192 |
graphs = self.translator.graphs |
| 193 |
for graph in graphs: |
| 194 |
for block, op in graph.iterblockops(): |
| 195 |
self.analyze(op) |
| 196 |
|
| 197 |
|
| 198 |
class Dependency(object): |
| 199 |
def __init__(self, analyzer): |
| 200 |
self._analyzer = analyzer |
| 201 |
self._result = analyzer.bottom_result() |
| 202 |
|
| 203 |
def merge_with_result(self, result): |
| 204 |
self._result = self._analyzer.join_two_results(self._result, result) |
| 205 |
|
| 206 |
def absorb(self, other): |
| 207 |
self.merge_with_result(other._result) |
| 208 |
|
| 209 |
|
| 210 |
class DependencyTracker(object): |
| 211 |
"""This tracks the analysis of cyclic call graphs.""" |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
def __init__(self, analyzer): |
| 224 |
self.analyzer = analyzer |
| 225 |
|
| 226 |
|
| 227 |
self.graph_results = analyzer._analyzed_calls |
| 228 |
|
| 229 |
self.current_stack = [] |
| 230 |
|
| 231 |
|
| 232 |
def enter(self, graph): |
| 233 |
if graph not in self.graph_results: |
| 234 |
self.current_stack.append(graph) |
| 235 |
|
| 236 |
self.graph_results.find(graph) |
| 237 |
return True |
| 238 |
else: |
| 239 |
graph = self.graph_results.find_rep(graph) |
| 240 |
for j in range(len(self.current_stack)): |
| 241 |
othergraph = self.graph_results.find_rep(self.current_stack[j]) |
| 242 |
if graph is othergraph: |
| 243 |
|
| 244 |
for i in range(j, len(self.current_stack)): |
| 245 |
self.graph_results.union(self.current_stack[i], graph) |
| 246 |
|
| 247 |
break |
| 248 |
return False |
| 249 |
|
| 250 |
def leave_with(self, result): |
| 251 |
graph = self.current_stack.pop() |
| 252 |
|
| 253 |
dep = self.graph_results[graph] |
| 254 |
dep.merge_with_result(result) |
| 255 |
|
| 256 |
def get_cached_result(self, graph): |
| 257 |
dep = self.graph_results[graph] |
| 258 |
return dep._result |
| 259 |
|
| 260 |
|
| 261 |
class BoolGraphAnalyzer(GraphAnalyzer): |
| 262 |
"""generic way to analyze graphs: recursively follow it until the first |
| 263 |
operation is found on which self.analyze_simple_operation returns True""" |
| 264 |
|
| 265 |
def bottom_result(self): |
| 266 |
return False |
| 267 |
|
| 268 |
def top_result(self): |
| 269 |
return True |
| 270 |
|
| 271 |
def is_top_result(self, result): |
| 272 |
return result |
| 273 |
|
| 274 |
def result_builder(self): |
| 275 |
return False |
| 276 |
|
| 277 |
def add_to_result(self, result, other): |
| 278 |
return self.join_two_results(result, other) |
| 279 |
|
| 280 |
def finalize_builder(self, result): |
| 281 |
return result |
| 282 |
|
| 283 |
def join_two_results(self, result1, result2): |
| 284 |
return result1 or result2 |