Debian Codegraph @

rpython/memory/gctransform/support.py

raw symbols SCIP
1 from rpython.rtyper.lltypesystem import lltype
2 from rpython.rtyper.lltypesystem.lloperation import llop
3 from rpython.rtyper.extregistry import ExtRegistryEntry
4 from rpython.annotator import model as annmodel
5 import os
6
7
8 def find_gc_ptrs_in_type(TYPE):
9 if isinstance(TYPE, lltype.Array):
10 return find_gc_ptrs_in_type(TYPE.OF)
11 elif isinstance(TYPE, lltype.Struct):
12 result = []
13 for name in TYPE._names:
14 result.extend(find_gc_ptrs_in_type(TYPE._flds[name]))
15 return result
16 elif isinstance(TYPE, lltype.Ptr) and TYPE._needsgc():
17 return [TYPE]
18 elif isinstance(TYPE, lltype.GcOpaqueType):
19 # heuristic: in theory the same problem exists with OpaqueType, but
20 # we use OpaqueType for other things too that we know are safely
21 # empty of further gc pointers
22 raise Exception("don't know what is in %r" % (TYPE,))
23 else:
24 return []
25
26 def get_rtti(TYPE):
27 if isinstance(TYPE, lltype.RttiStruct):
28 try:
29 return lltype.getRuntimeTypeInfo(TYPE)
30 except ValueError:
31 pass
32 return None
33
34 def _static_deallocator_body_for_type(v, TYPE, depth=1):
35 if isinstance(TYPE, lltype.Array):
36 inner = list(_static_deallocator_body_for_type('v_%i'%depth, TYPE.OF, depth+1))
37 if inner:
38 yield ' '*depth + 'i_%d = 0'%(depth,)
39 yield ' '*depth + 'l_%d = len(%s)'%(depth, v)
40 yield ' '*depth + 'while i_%d < l_%d:'%(depth, depth)
41 yield ' '*depth + ' v_%d = %s[i_%d]'%(depth, v, depth)
42 for line in inner:
43 yield line
44 yield ' '*depth + ' i_%d += 1'%(depth,)
45 elif isinstance(TYPE, lltype.Struct):
46 for name in TYPE._names:
47 inner = list(_static_deallocator_body_for_type(
48 v + '_' + name, TYPE._flds[name], depth))
49 if inner:
50 yield ' '*depth + v + '_' + name + ' = ' + v + '.' + name
51 for line in inner:
52 yield line
53 elif isinstance(TYPE, lltype.Ptr) and TYPE._needsgc():
54 yield ' '*depth + 'pop_alive(%s)'%v
55
56 class LLTransformerOp(object):
57 """Objects that can be called in ll functions.
58 Their calls are replaced by a simple operation of the GC transformer,
59 e.g. ll_pop_alive.
60 """
61 def __init__(self, transformer_method):
62 self.transformer_method = transformer_method
63
64 class LLTransformerOpEntry(ExtRegistryEntry):
65 "Annotation and specialization of LLTransformerOp() instances."
66 _type_ = LLTransformerOp
67
68 def compute_result_annotation(self, s_arg):
69 return annmodel.s_None
70
71 def specialize_call(self, hop):
72 op = self.instance # the LLTransformerOp instance
73 op.transformer_method(hop.args_v[0], hop.llops)
74 hop.exception_cannot_occur()
75 return hop.inputconst(hop.r_result.lowleveltype, hop.s_result.const)
76
77 def write(fd, string):
78 from rpython.rlib.rposix import c_write
79 return c_write(fd, string, len(string))
80
81 def ll_call_destructor(destrptr, destr_v, typename):
82 llop.revdb_do_next_call(lltype.Void)
83 try:
84 destrptr(destr_v)
85 except Exception as e:
86 try:
87 write(2, "a destructor of type ")
88 write(2, typename)
89 write(2, " raised an exception ")
90 write(2, str(e))
91 write(2, " ignoring it\n")
92 except:
93 pass
94
95 def ll_report_finalizer_error(e):
96 try:
97 write(2, "triggering finalizers raised an exception ")
98 write(2, str(e))
99 write(2, " ignoring it\n")
100 except:
101 pass