Debian Codegraph @

rpython/rtyper/rweakref.py

raw symbols SCIP
1 import weakref
2 from rpython.annotator import model as annmodel
3 from rpython.flowspace.model import Constant
4 from rpython.rtyper.error import TyperError
5 from rpython.rtyper.rmodel import Repr
6 from rpython.rtyper.lltypesystem import lltype, llmemory
7
8 # ____________________________________________________________
9 #
10 # RTyping of RPython-level weakrefs
11
12 class __extend__(annmodel.SomeWeakRef):
13 def rtyper_makerepr(self, rtyper):
14 if rtyper.getconfig().translation.rweakref:
15 return WeakRefRepr(rtyper)
16 else:
17 return EmulatedWeakRefRepr(rtyper)
18
19 def rtyper_makekey(self):
20 return self.__class__,
21
22
23 class BaseWeakRefRepr(Repr):
24 def __init__(self, rtyper):
25 self.rtyper = rtyper
26
27 def convert_const(self, value):
28 if value is None:
29 return lltype.nullptr(self.lowleveltype.TO)
30
31 assert isinstance(value, weakref.ReferenceType)
32 instance = value()
33 bk = self.rtyper.annotator.bookkeeper
34 if instance is None:
35 return self.dead_wref
36 else:
37 repr = self.rtyper.bindingrepr(Constant(instance))
38 llinstance = repr.convert_const(instance)
39 return self.do_weakref_create(llinstance)
40
41 def rtype_simple_call(self, hop):
42 v_wref, = hop.inputargs(self)
43 hop.exception_cannot_occur()
44 if hop.r_result.lowleveltype is lltype.Void: # known-to-be-dead weakref
45 return hop.inputconst(lltype.Void, None)
46 else:
47 assert v_wref.concretetype == self.lowleveltype
48 return self._weakref_deref(hop, v_wref)
49
50
51 class WeakRefRepr(BaseWeakRefRepr):
52 lowleveltype = llmemory.WeakRefPtr
53 dead_wref = llmemory.dead_wref
54
55 def do_weakref_create(self, llinstance):
56 return llmemory.weakref_create(llinstance)
57
58 def _weakref_create(self, hop, v_inst):
59 return hop.genop('weakref_create', [v_inst],
60 resulttype=llmemory.WeakRefPtr)
61
62 def _weakref_deref(self, hop, v_wref):
63 return hop.genop('weakref_deref', [v_wref],
64 resulttype=hop.r_result)
65
66
67 class EmulatedWeakRefRepr(BaseWeakRefRepr):
68 """For the case rweakref=False, we emulate RPython-level weakrefs
69 with regular strong references (but not low-level weakrefs).
70 """
71 lowleveltype = lltype.Ptr(lltype.GcStruct('EmulatedWeakRef',
72 ('ref', llmemory.GCREF)))
73 dead_wref = lltype.malloc(lowleveltype.TO, immortal=True, zero=True)
74
75 def do_weakref_create(self, llinstance):
76 p = lltype.malloc(self.lowleveltype.TO, immortal=True)
77 p.ref = lltype.cast_opaque_ptr(llmemory.GCREF, llinstance)
78 return p
79
80 def _weakref_create(self, hop, v_inst):
81 c_type = hop.inputconst(lltype.Void, self.lowleveltype.TO)
82 c_flags = hop.inputconst(lltype.Void, {'flavor': 'gc'})
83 v_ptr = hop.genop('malloc', [c_type, c_flags],
84 resulttype=self.lowleveltype)
85 v_gcref = hop.genop('cast_opaque_ptr', [v_inst],
86 resulttype=llmemory.GCREF)
87 c_ref = hop.inputconst(lltype.Void, 'ref')
88 hop.genop('setfield', [v_ptr, c_ref, v_gcref])
89 return v_ptr
90
91 def _weakref_deref(self, hop, v_wref):
92 c_ref = hop.inputconst(lltype.Void, 'ref')
93 v_gcref = hop.genop('getfield', [v_wref, c_ref],
94 resulttype=llmemory.GCREF)
95 return hop.genop('cast_opaque_ptr', [v_gcref],
96 resulttype=hop.r_result)