Debian Codegraph @

rpython/rtyper/lltypesystem/rgcref.py

raw symbols SCIP
1 from rpython.rtyper.rmodel import Repr
2 from rpython.rtyper.lltypesystem import lltype, llmemory
3
4 from rpython.tool.pairtype import pairtype, extendabletype, pair
5
6 UNKNOWN = object()
7
8 class GCRefRepr(Repr):
9 lowleveltype = llmemory.GCREF
10
11 @staticmethod
12 def make(r_base, cache):
13 try:
14 return cache[r_base]
15 except KeyError:
16 res = cache[r_base] = GCRefRepr(r_base)
17 return res
18
19
20 def __init__(self, r_base):
21 self.r_base = r_base
22 self._ll_eq_func = UNKNOWN
23 self._ll_hash_func = UNKNOWN
24 if hasattr(r_base, 'll_str'):
25 ll_base_str = r_base.ll_str
26 def ll_str(ptr):
27 return ll_base_str(lltype.cast_opaque_ptr(r_base.lowleveltype, ptr))
28 self.ll_str = ll_str
29
30 def convert_const(self, x):
31 return lltype.cast_opaque_ptr(llmemory.GCREF, self.r_base.convert_const(x))
32
33 def get_ll_eq_function(self):
34 if self._ll_eq_func is UNKNOWN:
35 ll_base_eq_function = self.r_base.get_ll_eq_function()
36 if ll_base_eq_function is None:
37 ll_eq_func = None
38 else:
39 def ll_eq_func(ptr1, ptr2):
40 ptr1 = lltype.cast_opaque_ptr(self.r_base.lowleveltype, ptr1)
41 ptr2 = lltype.cast_opaque_ptr(self.r_base.lowleveltype, ptr2)
42 return ll_base_eq_function(ptr1, ptr2)
43 self._ll_eq_func = ll_eq_func
44 return self._ll_eq_func
45
46 def get_ll_hash_function(self):
47 if self._ll_hash_func is UNKNOWN:
48 ll_base_hash_function = self.r_base.get_ll_hash_function()
49 if ll_base_hash_function is None:
50 ll_hash_func = None
51 else:
52 def ll_hash_func(ptr):
53 ptr = lltype.cast_opaque_ptr(self.r_base.lowleveltype, ptr)
54 return ll_base_hash_function(ptr)
55 self._ll_hash_func = ll_hash_func
56 return self._ll_hash_func
57
58 def get_ll_dummyval_obj(self, rtyper, s_value):
59 return DummyValueBuilderGCRef(rtyper)
60
61 class __extend__(pairtype(GCRefRepr, Repr)):
62 def convert_from_to((r_from, r_to), v, llops):
63 if isinstance(r_to.lowleveltype, lltype.Ptr) and r_to.lowleveltype.TO._gckind == 'gc':
64 return llops.genop('cast_opaque_ptr', [v], r_to.lowleveltype)
65 return NotImplemented
66
67 class __extend__(pairtype(Repr, GCRefRepr)):
68 def convert_from_to((r_from, r_to), v, llops):
69 if r_from != r_to.r_base:
70 v = pair(r_from, r_to.r_base).convert_from_to(v, llops)
71 return llops.genop('cast_opaque_ptr', [v], r_to.lowleveltype)
72
73
74 class DummyValueBuilderGCRef(object):
75
76 def __init__(self, rtyper):
77 self.rtyper = rtyper
78
79 def _freeze_(self):
80 return True
81
82 def __hash__(self):
83 return hash(llmemory.GCREF)
84
85 def __eq__(self, other):
86 return (isinstance(other, DummyValueBuilderGCRef) and
87 self.rtyper is other.rtyper)
88
89 def __ne__(self, other):
90 return not (self == other)
91
92 @property
93 def ll_dummy_value(self):
94 try:
95 return self.rtyper.cache_dummy_values[llmemory.GCREF]
96 except KeyError:
97 from rpython.rtyper import rclass
98 from rpython.rtyper.rmodel import DummyValueBuilder
99 rinstbase = rclass.getinstancerepr(self.rtyper, None)
100 TYPE = rinstbase.lowleveltype
101 val = DummyValueBuilder(self.rtyper, TYPE.TO).ll_dummy_value
102 p = lltype.cast_opaque_ptr(llmemory.GCREF, val)
103 self.rtyper.cache_dummy_values[llmemory.GCREF] = p
104 return p
105
106