Debian Codegraph @

rpython/translator/backendopt/finalizer.py

raw symbols SCIP
1 from rpython.translator.backendopt import graphanalyze
2 from rpython.rtyper.lltypesystem import lltype
3 from rpython.tool.ansi_print import AnsiLogger
4
5 log = AnsiLogger("finalizer")
6
7
8 class FinalizerError(Exception):
9 """__del__() is used for lightweight RPython destructors,
10 but the FinalizerAnalyzer found that it is not lightweight.
11
12 The set of allowed operations is restrictive for a good reason
13 - it's better to be safe. Specifically disallowed operations:
14
15 * anything that escapes self
16 * anything that can allocate
17 """
18
19 class FinalizerAnalyzer(graphanalyze.BoolGraphAnalyzer):
20 """ Analyzer that determines whether a finalizer is lightweight enough
21 so it can be called without all the complicated logic in the garbage
22 collector.
23 """
24 ok_operations = ['ptr_nonzero', 'ptr_eq', 'ptr_ne', 'free', 'same_as',
25 'direct_ptradd', 'force_cast', 'track_alloc_stop',
26 'raw_free', 'adr_eq', 'adr_ne',
27 'debug_print']
28
29 def analyze_light_finalizer(self, graph):
30 if getattr(graph.func, '_must_be_light_finalizer_', False):
31 self._must_be_light = graph
32 result = self.analyze_direct_call(graph)
33 del self._must_be_light
34 if result is self.top_result():
35 msg = '%s\nIn %r' % (FinalizerError.__doc__, graph)
36 raise FinalizerError(msg)
37 else:
38 result = self.analyze_direct_call(graph)
39 #if result is self.top_result():
40 # log.red('old-style non-light finalizer: %r' % (graph,))
41 return result
42
43 def analyze_simple_operation(self, op, graphinfo):
44 if op.opname in self.ok_operations:
45 return self.bottom_result()
46 if (op.opname.startswith('int_') or op.opname.startswith('float_')
47 or op.opname.startswith('uint_') or op.opname.startswith('cast_')):
48 return self.bottom_result()
49 if op.opname == 'setfield' or op.opname == 'bare_setfield':
50 TP = op.args[2].concretetype
51 if not isinstance(TP, lltype.Ptr) or TP.TO._gckind == 'raw':
52 # primitive type
53 return self.bottom_result()
54 if op.opname == 'getfield':
55 TP = op.result.concretetype
56 if not isinstance(TP, lltype.Ptr) or TP.TO._gckind == 'raw':
57 # primitive type
58 return self.bottom_result()
59
60 if not hasattr(self, '_must_be_light'):
61 return self.top_result()
62 msg = '%s\nFound this forbidden operation:\n%r\nin %r\nfrom %r' % (
63 FinalizerError.__doc__, op, graphinfo, self._must_be_light)
64 raise FinalizerError(msg)