Debian Codegraph @

rpython/rtyper/lltypesystem/llgroup.py

raw symbols SCIP
1 import weakref
2 from rpython.rtyper.lltypesystem import lltype, llmemory, rffi
3 from rpython.rlib.rarithmetic import LONG_BIT
4
5
6 class GroupType(lltype.ContainerType):
7 """A 'group' that stores static structs together in memory.
8
9 On 32-bit platforms, the point is that they can be referenced by a
10 GroupMemberOffset which only takes 2 bytes (a USHORT), so the total
11 size of a group is limited to 18 (= the 16 bits in a USHORT, plus 2
12 bits at the end that are zero and so don't need to be stored).
13
14 On 64-bit platforms, we check that the address they end up at is
15 within the first 32 bits, so that we can store that address in half
16 a long (i.e. in a UINT).
17 """
18 _gckind = 'raw'
19
20 Group = GroupType()
21
22
23 class group(lltype._container):
24 _TYPE = Group
25 outdated = None
26
27 def __init__(self, name):
28 self.name = name
29 self.members = []
30
31 def add_member(self, structptr):
32 TYPE = lltype.typeOf(structptr)
33 assert isinstance(TYPE.TO, lltype.Struct)
34 assert TYPE.TO._gckind == 'raw'
35 struct = structptr._as_obj()
36 prevgroup = _membership.get(struct)
37 if prevgroup is not None:
38 prevgroup.outdated = (
39 "structure %s was inserted into another group" % (struct,))
40 assert struct._parentstructure() is None
41 index = len(self.members)
42 self.members.append(struct)
43 _membership[struct] = self
44 return GroupMemberOffset(self, index)
45
46 def member_of_group(structptr):
47 return _membership.get(structptr._as_obj(), None)
48
49 _membership = weakref.WeakValueDictionary()
50
51
52 if LONG_BIT == 32:
53 HALFSHIFT = 16
54 HALFWORD = rffi.USHORT
55 r_halfword = rffi.r_ushort
56 else:
57 HALFSHIFT = 32
58 HALFWORD = rffi.UINT
59 r_halfword = rffi.r_uint
60
61
62 class GroupMemberOffset(llmemory.Symbolic):
63 """The offset of a struct inside a group, stored compactly in a HALFWORD
64 (a USHORT or UINT). Can only be used by the lloperation 'get_group_member'.
65 """
66 def annotation(self):
67 from rpython.annotator import model
68 return model.SomeInteger(knowntype=r_halfword)
69
70 def lltype(self):
71 return HALFWORD
72
73 def __init__(self, grp, memberindex):
74 assert lltype.typeOf(grp) == Group
75 self.grpptr = grp._as_ptr()
76 self.index = memberindex
77 self.member = grp.members[memberindex]._as_ptr()
78
79 def __repr__(self):
80 return '%s(%s, %s)' % (self.__class__.__name__,
81 self.grpptr, self.index)
82
83 def __nonzero__(self):
84 return True
85
86 def _get_group_member(self, grpptr):
87 assert grpptr == self.grpptr, "get_group_member: wrong group!"
88 return self.member
89
90 def _get_next_group_member(self, grpptr, skipoffset):
91 # ad-hoc: returns a pointer to the group member that follows this one,
92 # given information in 'skipoffset' about how much to skip -- which
93 # is the size of the current member.
94 assert grpptr == self.grpptr, "get_next_group_member: wrong group!"
95 assert isinstance(skipoffset, llmemory.ItemOffset)
96 assert skipoffset.TYPE == lltype.typeOf(self.member).TO
97 assert skipoffset.repeat == 1
98 return self.grpptr._as_obj().members[self.index + 1]._as_ptr()
99
100
101 class CombinedSymbolic(llmemory.Symbolic):
102 """A general-purpose Signed symbolic that combines an unsigned half-word
103 (USHORT on 32-bit platforms, UINT on 64-bit platforms) and the rest
104 of the word (typically flags). Only supports extracting the half-word
105 with 'llop.extract_ushort', and extracting the rest of the word with
106 '&~0xFFFF' or with a direct masking like '&0x10000' (resp. on 64-bit
107 platform, with '&~0xFFFFFFFF' or '&0x100000000').
108 """
109 __slots__ = ['lowpart', 'rest']
110 MASK = (1<<HALFSHIFT)-1 # 0xFFFF or 0xFFFFFFFF
111
112 def annotation(self):
113 from rpython.annotator import model
114 return model.SomeInteger()
115
116 def lltype(self):
117 return lltype.Signed
118
119 def __init__(self, lowpart, rest):
120 assert (rest & CombinedSymbolic.MASK) == 0
121 self.lowpart = lowpart
122 self.rest = rest
123
124 def __repr__(self):
125 return '<CombinedSymbolic %r|%s>' % (self.lowpart, self.rest)
126
127 def __nonzero__(self):
128 return True
129
130 def __and__(self, other):
131 if (other & CombinedSymbolic.MASK) == 0:
132 return self.rest & other
133 if (other & CombinedSymbolic.MASK) == CombinedSymbolic.MASK:
134 return CombinedSymbolic(self.lowpart, self.rest & other)
135 raise Exception("other=0x%x" % other)
136
137 def __or__(self, other):
138 assert (other & CombinedSymbolic.MASK) == 0
139 return CombinedSymbolic(self.lowpart, self.rest | other)
140
141 def __add__(self, other):
142 assert (other & CombinedSymbolic.MASK) == 0
143 return CombinedSymbolic(self.lowpart, self.rest + other)
144
145 def __sub__(self, other):
146 assert (other & CombinedSymbolic.MASK) == 0
147 return CombinedSymbolic(self.lowpart, self.rest - other)
148
149 def __rshift__(self, other):
150 assert other >= HALFSHIFT
151 return self.rest >> other
152
153 def __eq__(self, other):
154 if (isinstance(other, CombinedSymbolic) and
155 self.lowpart is other.lowpart):
156 return self.rest == other.rest
157 else:
158 return NotImplemented
159
160 def __ne__(self, other):
161 if (isinstance(other, CombinedSymbolic) and
162 self.lowpart is other.lowpart):
163 return self.rest != other.rest
164 else:
165 return NotImplemented