Debian Codegraph @

pypy/module/_hpy_universal/interp_field.py

raw symbols SCIP
1 from rpython.rtyper.lltypesystem import lltype, rffi, llmemory
2 from rpython.rlib.objectmodel import we_are_translated
3 from rpython.rlib import rgc, jit
4 from rpython.rlib.debug import ll_assert
5 from pypy.interpreter.error import OperationError, oefmt
6 from pypy.interpreter.baseobjspace import W_Root
7 from pypy.module._hpy_universal.apiset import API
8 from pypy.module._hpy_universal import llapi
9 from pypy.module._hpy_universal.interp_type import W_HPyObject, W_HPyTypeObject
10
11 class UntranslatedHPyFieldStorage(object):
12 """
13 Proper support for HPyField is possible only after translation, because it
14 needs a tight integration with the GC.
15
16 For untranslated apptests, we use this small helper to keep track of which
17 object is stored in every HPyField. We don't need anything fancy, just
18 enough logic to make the tests work. In particular, we want to ensure that
19 if we HPyField_Store() something, we can HPyField_Load() it later.
20 """
21
22 def __init__(self):
23 self._fields = {}
24
25 def _freeze_(self):
26 raise Exception('You should not use UntranslatedHPyFieldStorage '
27 'from translated code')
28
29 def store_w(self, pf, w_obj):
30 # we need an ID to uniquely identify the HPyField pointed by pf: we
31 # can just use its address. If later another object has the same
32 # HPyField* we can safely override it, because it means that the
33 # original object has died.
34 #
35 # NOTE: with he current strategy, self._fields is never cleared so an
36 # HPyField_Store keeps the object alive forever. That's bad but we
37 # don't care for now, since it's used only by a few tests.
38 unique_id = rffi.cast(lltype.Signed, pf)
39 pf[0] = unique_id
40 self._fields[unique_id] = w_obj
41
42 def delete(self, pf):
43 unique_id = rffi.cast(lltype.Signed, pf)
44 self._fields.pop(unique_id, None)
45 pf[0] = 0
46
47 def load_w(self, f):
48 # returns the w_obj
49 if f not in self._fields:
50 # we are trying to read a field which was never written to: this
51 # is basically a segfault :)
52 assert False, 'boom'
53 return self._fields[f]
54
55 _STORAGE = UntranslatedHPyFieldStorage()
56
57 @jit.dont_look_inside
58 def field_store_w(space, pf, w_target, w_value):
59 ll_assert(isinstance(w_target, W_HPyObject), 'h_target is not a valid HPy object')
60 assert isinstance(w_target, W_HPyObject)
61 assert isinstance(w_value, W_Root)
62 if not we_are_translated():
63 _STORAGE.store_w(pf, w_value)
64 else:
65 storage = w_target._hpy_get_gc_storage(space)
66 # Should never happen
67 ll_assert(bool(storage.tp_traverse), "required tp_traverse function missing on storage")
68 assert bool(storage.tp_traverse)
69 rgc.ll_writebarrier(storage)
70 #
71 gcref = rgc.cast_instance_to_gcref(w_value)
72 pf[0] = rffi.cast(lltype.Signed, gcref)
73
74
75 @jit.dont_look_inside
76 @API.func("void HPyField_Store(HPyContext *ctx, HPy h_target, HPyField *pf, HPy h)")
77 def HPyField_Store(space, handles, ctx, h_target, pf, h):
78 # TODO: refactor this to use field_store_w, field_delete_w
79 if not we_are_translated():
80 # just for tests
81 if h == handles.NULL:
82 _STORAGE.delete(pf)
83 else:
84 w_obj = handles.deref(h)
85 _STORAGE.store_w(pf, w_obj)
86 return
87 #
88 # real implementation
89 if h == handles.NULL:
90 pf[0] = 0
91 else:
92 w_target = handles.deref(h_target)
93 ll_assert(isinstance(w_target, W_HPyObject) or isinstance(w_target, W_HPyTypeObject), 'h_target is not a valid HPy object')
94 assert (isinstance(w_target, W_HPyObject) or isinstance(w_target, W_HPyTypeObject))
95 storage = w_target._hpy_get_gc_storage(space)
96 # Should never happen
97 ll_assert(bool(storage.tp_traverse), "required tp_traverse function missing on storage")
98 assert bool(storage.tp_traverse)
99 rgc.ll_writebarrier(storage)
100 #
101 w_obj = handles.deref(h)
102 gcref = rgc.cast_instance_to_gcref(w_obj)
103 pf[0] = rffi.cast(lltype.Signed, gcref)
104
105 @jit.dont_look_inside
106 def field_load_w(space, w_source, f):
107 if we_are_translated():
108 ll_assert(isinstance(w_source, W_HPyObject) or isinstance(w_source, W_HPyTypeObject), 'h_target is not a valid HPy object')
109 assert (isinstance(w_source, W_HPyObject) or isinstance(w_source, W_HPyTypeObject))
110 gcref = rffi.cast(llmemory.GCREF, f)
111 w_obj = rgc.try_cast_gcref_to_instance(W_Root, gcref)
112 # if w_obj is None it means that the gcref didn't contain a W_Root, but
113 # this should not be possible
114 assert w_obj is not None
115 else:
116 # just for tests
117 w_obj = _STORAGE.load_w(f)
118 return w_obj
119
120 def field_delete_w(pf):
121 if we_are_translated():
122 pf[0] = 0
123 else:
124 _STORAGE.delete(pf)
125
126
127 @API.func("HPy HPyField_Load(HPyContext *ctx, HPy source_object, HPyField source_field)")
128 def HPyField_Load(space, handles, ctx, h_source, f):
129 w_source = handles.deref(h_source)
130 w_obj = field_load_w(space, w_source, f)
131 return handles.new(w_obj)
132
133 def is_hpy_object(w_obj):
134 return isinstance(w_obj, W_HPyObject) or isinstance(w_obj, W_HPyTypeObject)
135
136 def hpy_get_referents(space, w_obj):
137 """
138 NOT_RPYTHON. Called by gc.get_referents, only for hpy objects and only for tests
139 """
140 w_type = space.type(w_obj)
141 assert is_hpy_object(w_obj)
142 assert isinstance(w_type, W_HPyTypeObject)
143 if w_type.tp_traverse:
144 ll_collect_fields = _collect_fields.get_llhelper(space)
145 assert _collect_fields.allfields == []
146 NULL = llapi.cts.cast('void *', 0)
147 w_type.tp_traverse(w_obj._hpy_get_raw_storage(space), ll_collect_fields, NULL)
148 result = _collect_fields.allfields
149 _collect_fields.allfields = []
150 return result
151
152
153 @API.func("int _collect_fields(HPyField *f, void *arg)",
154 error_value=API.int(-1), is_helper=True)
155 def _collect_fields(space, handles, f, arg):
156 """
157 Only for tests, see hpy_get_referents
158 """
159 assert not we_are_translated()
160 unique_id = rffi.cast(lltype.Signed, f)
161 w_obj = _STORAGE._fields[unique_id]
162 _collect_fields.allfields.append(w_obj)
163 return API.int(0)
164 _collect_fields.allfields = []