1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "jit/TypedObjectPrediction.h"
using namespace js;
using namespace jit;
static const size_t ALL_FIELDS = SIZE_MAX;
// Sets the prediction to be the common prefix of descrA and descrB,
// considering at most the first max fields.
//
// In the case where the current prediction is a specific struct,
// and we are now seeing a second struct, then descrA and descrB will be
// the current and new struct and max will be ALL_FIELDS.
//
// In the case where the current prediction is already a prefix, and
// we are now seeing an additional struct, then descrA will be the
// current struct and max will be the current prefix length, and
// descrB will be the new struct.
//
// (Note that in general it is not important which struct is passed as
// descrA and which struct is passed as descrB, as the operation is
// symmetric.)
void
TypedObjectPrediction::markAsCommonPrefix(const StructTypeDescr& descrA,
const StructTypeDescr& descrB,
size_t max)
{
// count is the number of fields in common. It begins as the min
// of the number of fields from descrA, descrB, and max, and then
// is decremented as we find uncommon fields.
if (max > descrA.fieldCount())
max = descrA.fieldCount();
if (max > descrB.fieldCount())
max = descrB.fieldCount();
size_t i = 0;
for (; i < max; i++) {
if (&descrA.fieldName(i) != &descrB.fieldName(i))
break;
if (&descrA.fieldDescr(i) != &descrB.fieldDescr(i))
break;
MOZ_ASSERT(descrA.fieldOffset(i) == descrB.fieldOffset(i));
}
if (i == 0) {
// empty prefix is not particularly useful.
markInconsistent();
} else {
setPrefix(descrA, i);
}
}
void
TypedObjectPrediction::addDescr(const TypeDescr& descr)
{
switch (predictionKind()) {
case Empty:
return setDescr(descr);
case Inconsistent:
return; // keep same state
case Descr: {
if (&descr == data_.descr)
return; // keep same state
if (descr.kind() != data_.descr->kind())
return markInconsistent();
if (descr.kind() != type::Struct)
return markInconsistent();
const StructTypeDescr& structDescr = descr.as<StructTypeDescr>();
const StructTypeDescr& currentDescr = data_.descr->as<StructTypeDescr>();
markAsCommonPrefix(structDescr, currentDescr, ALL_FIELDS);
return;
}
case Prefix:
if (descr.kind() != type::Struct)
return markInconsistent();
markAsCommonPrefix(*data_.prefix.descr,
descr.as<StructTypeDescr>(),
data_.prefix.fields);
return;
}
MOZ_CRASH("Bad predictionKind");
}
type::Kind
TypedObjectPrediction::kind() const
{
switch (predictionKind()) {
case TypedObjectPrediction::Empty:
case TypedObjectPrediction::Inconsistent:
break;
case TypedObjectPrediction::Descr:
return descr().kind();
case TypedObjectPrediction::Prefix:
return prefix().descr->kind();
}
MOZ_CRASH("Bad prediction kind");
}
bool
TypedObjectPrediction::ofArrayKind() const
{
switch (kind()) {
case type::Scalar:
case type::Reference:
case type::Simd:
case type::Struct:
return false;
case type::Array:
return true;
}
MOZ_CRASH("Bad kind");
}
bool
TypedObjectPrediction::hasKnownSize(uint32_t* out) const
{
switch (predictionKind()) {
case TypedObjectPrediction::Empty:
case TypedObjectPrediction::Inconsistent:
return false;
case TypedObjectPrediction::Descr:
*out = descr().size();
return true;
case TypedObjectPrediction::Prefix:
// We only know a prefix of the struct fields, hence we do not
// know its complete size.
return false;
default:
MOZ_CRASH("Bad prediction kind");
}
}
const TypedProto*
TypedObjectPrediction::getKnownPrototype() const
{
switch (predictionKind()) {
case TypedObjectPrediction::Empty:
case TypedObjectPrediction::Inconsistent:
return nullptr;
case TypedObjectPrediction::Descr:
if (descr().is<ComplexTypeDescr>())
return &descr().as<ComplexTypeDescr>().instancePrototype();
return nullptr;
case TypedObjectPrediction::Prefix:
// We only know a prefix of the struct fields, hence we cannot
// say for certain what its prototype will be.
return nullptr;
default:
MOZ_CRASH("Bad prediction kind");
}
}
template<typename T>
typename T::Type
TypedObjectPrediction::extractType() const
{
MOZ_ASSERT(kind() == T::Kind);
switch (predictionKind()) {
case TypedObjectPrediction::Empty:
case TypedObjectPrediction::Inconsistent:
break;
case TypedObjectPrediction::Descr:
return descr().as<T>().type();
case TypedObjectPrediction::Prefix:
break; // Prefixes are always structs, never scalars etc
}
MOZ_CRASH("Bad prediction kind");
}
ScalarTypeDescr::Type
TypedObjectPrediction::scalarType() const
{
return extractType<ScalarTypeDescr>();
}
ReferenceTypeDescr::Type
TypedObjectPrediction::referenceType() const
{
return extractType<ReferenceTypeDescr>();
}
SimdType
TypedObjectPrediction::simdType() const
{
return descr().as<SimdTypeDescr>().type();
}
bool
TypedObjectPrediction::hasKnownArrayLength(int32_t* length) const
{
switch (predictionKind()) {
case TypedObjectPrediction::Empty:
case TypedObjectPrediction::Inconsistent:
return false;
case TypedObjectPrediction::Descr:
// In later patches, this condition will always be true
// so long as this represents an array
if (descr().is<ArrayTypeDescr>()) {
*length = descr().as<ArrayTypeDescr>().length();
return true;
}
return false;
case TypedObjectPrediction::Prefix:
// Prefixes are always structs, never arrays
return false;
default:
MOZ_CRASH("Bad prediction kind");
}
}
TypedObjectPrediction
TypedObjectPrediction::arrayElementType() const
{
MOZ_ASSERT(ofArrayKind());
switch (predictionKind()) {
case TypedObjectPrediction::Empty:
case TypedObjectPrediction::Inconsistent:
break;
case TypedObjectPrediction::Descr:
return TypedObjectPrediction(descr().as<ArrayTypeDescr>().elementType());
case TypedObjectPrediction::Prefix:
break; // Prefixes are always structs, never arrays
}
MOZ_CRASH("Bad prediction kind");
}
bool
TypedObjectPrediction::hasFieldNamedPrefix(const StructTypeDescr& descr,
size_t fieldCount,
jsid id,
size_t* fieldOffset,
TypedObjectPrediction* out,
size_t* index) const
{
// Find the index of the field |id| if any.
if (!descr.fieldIndex(id, index))
return false;
// Check whether the index falls within our known safe prefix.
if (*index >= fieldCount)
return false;
// Load the offset and type.
*fieldOffset = descr.fieldOffset(*index);
*out = TypedObjectPrediction(descr.fieldDescr(*index));
return true;
}
bool
TypedObjectPrediction::hasFieldNamed(jsid id,
size_t* fieldOffset,
TypedObjectPrediction* fieldType,
size_t* fieldIndex) const
{
MOZ_ASSERT(kind() == type::Struct);
switch (predictionKind()) {
case TypedObjectPrediction::Empty:
case TypedObjectPrediction::Inconsistent:
return false;
case TypedObjectPrediction::Descr:
return hasFieldNamedPrefix(
descr().as<StructTypeDescr>(), ALL_FIELDS,
id, fieldOffset, fieldType, fieldIndex);
case TypedObjectPrediction::Prefix:
return hasFieldNamedPrefix(
*prefix().descr, prefix().fields,
id, fieldOffset, fieldType, fieldIndex);
default:
MOZ_CRASH("Bad prediction kind");
}
}
|