-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathTypeInference.cpp
4897 lines (4123 loc) · 146 KB
/
TypeInference.cpp
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* 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 "vm/TypeInference-inl.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/IntegerPrintfMacros.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/Move.h"
#include "mozilla/PodOperations.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/Sprintf.h"
#include <new>
#include "jsapi.h"
#include "builtin/String.h"
#include "gc/HashUtil.h"
#include "jit/BaselineIC.h"
#include "jit/BaselineJIT.h"
#include "jit/CompileInfo.h"
#include "jit/Ion.h"
#include "jit/IonAnalysis.h"
#include "jit/JitRealm.h"
#include "jit/OptimizationTracking.h"
#include "js/MemoryMetrics.h"
#include "js/UniquePtr.h"
#include "vm/HelperThreads.h"
#include "vm/JSContext.h"
#include "vm/JSObject.h"
#include "vm/JSScript.h"
#include "vm/Opcodes.h"
#include "vm/Printer.h"
#include "vm/Shape.h"
#include "vm/Time.h"
#include "vm/UnboxedObject.h"
#include "gc/Marking-inl.h"
#include "gc/PrivateIterators-inl.h"
#include "vm/JSAtom-inl.h"
#include "vm/JSScript-inl.h"
#include "vm/NativeObject-inl.h"
using namespace js;
using mozilla::DebugOnly;
using mozilla::Maybe;
using mozilla::PodArrayZero;
using mozilla::PodCopy;
using mozilla::PodZero;
#ifdef DEBUG
static inline jsid id___proto__(JSContext* cx) {
return NameToId(cx->names().proto);
}
static inline jsid id_constructor(JSContext* cx) {
return NameToId(cx->names().constructor);
}
static inline jsid id_caller(JSContext* cx) {
return NameToId(cx->names().caller);
}
const char* js::TypeIdStringImpl(jsid id) {
if (JSID_IS_VOID(id)) {
return "(index)";
}
if (JSID_IS_EMPTY(id)) {
return "(new)";
}
if (JSID_IS_SYMBOL(id)) {
return "(symbol)";
}
static char bufs[4][100];
static unsigned which = 0;
which = (which + 1) & 3;
PutEscapedString(bufs[which], 100, JSID_TO_FLAT_STRING(id), 0);
return bufs[which];
}
#endif
/////////////////////////////////////////////////////////////////////
// Logging
/////////////////////////////////////////////////////////////////////
/* static */ const char* TypeSet::NonObjectTypeString(TypeSet::Type type) {
if (type.isPrimitive()) {
switch (type.primitive()) {
case JSVAL_TYPE_UNDEFINED:
return "void";
case JSVAL_TYPE_NULL:
return "null";
case JSVAL_TYPE_BOOLEAN:
return "bool";
case JSVAL_TYPE_INT32:
return "int";
case JSVAL_TYPE_DOUBLE:
return "float";
case JSVAL_TYPE_STRING:
return "string";
case JSVAL_TYPE_SYMBOL:
return "symbol";
case JSVAL_TYPE_BIGINT:
return "BigInt";
case JSVAL_TYPE_MAGIC:
return "lazyargs";
default:
MOZ_CRASH("Bad type");
}
}
if (type.isUnknown()) {
return "unknown";
}
MOZ_ASSERT(type.isAnyObject());
return "object";
}
static UniqueChars MakeStringCopy(const char* s) {
AutoEnterOOMUnsafeRegion oomUnsafe;
char* copy = strdup(s);
if (!copy) {
oomUnsafe.crash("Could not copy string");
}
return UniqueChars(copy);
}
/* static */ UniqueChars TypeSet::TypeString(const TypeSet::Type type) {
if (type.isPrimitive() || type.isUnknown() || type.isAnyObject()) {
return MakeStringCopy(NonObjectTypeString(type));
}
char buf[100];
if (type.isSingleton()) {
JSObject* singleton = type.singletonNoBarrier();
SprintfLiteral(buf, "<%s %#" PRIxPTR ">", singleton->getClass()->name,
uintptr_t(singleton));
} else {
SprintfLiteral(buf, "[%s * %#" PRIxPTR "]",
type.groupNoBarrier()->clasp()->name,
uintptr_t(type.groupNoBarrier()));
}
return MakeStringCopy(buf);
}
/* static */ UniqueChars TypeSet::ObjectGroupString(const ObjectGroup* group) {
return TypeString(TypeSet::ObjectType(group));
}
#ifdef DEBUG
bool js::InferSpewActive(TypeSpewChannel channel) {
static bool active[SPEW_COUNT];
static bool checked = false;
if (!checked) {
checked = true;
PodArrayZero(active);
if (mozilla::recordreplay::IsRecordingOrReplaying()) {
return false;
}
const char* env = getenv("INFERFLAGS");
if (!env) {
return false;
}
if (strstr(env, "ops")) {
active[ISpewOps] = true;
}
if (strstr(env, "result")) {
active[ISpewResult] = true;
}
if (strstr(env, "full")) {
for (unsigned i = 0; i < SPEW_COUNT; i++) {
active[i] = true;
}
}
}
return active[channel];
}
static bool InferSpewColorable() {
/* Only spew colors on xterm-color to not screw up emacs. */
static bool colorable = false;
static bool checked = false;
if (!checked) {
checked = true;
if (mozilla::recordreplay::IsRecordingOrReplaying()) {
return false;
}
const char* env = getenv("TERM");
if (!env) {
return false;
}
if (strcmp(env, "xterm-color") == 0 || strcmp(env, "xterm-256color") == 0) {
colorable = true;
}
}
return colorable;
}
const char* js::InferSpewColorReset() {
if (!InferSpewColorable()) {
return "";
}
return "\x1b[0m";
}
const char* js::InferSpewColor(TypeConstraint* constraint) {
/* Type constraints are printed out using foreground colors. */
static const char* const colors[] = {"\x1b[31m", "\x1b[32m", "\x1b[33m",
"\x1b[34m", "\x1b[35m", "\x1b[36m",
"\x1b[37m"};
if (!InferSpewColorable()) {
return "";
}
return colors[DefaultHasher<TypeConstraint*>::hash(constraint) % 7];
}
const char* js::InferSpewColor(TypeSet* types) {
/* Type sets are printed out using bold colors. */
static const char* const colors[] = {"\x1b[1;31m", "\x1b[1;32m", "\x1b[1;33m",
"\x1b[1;34m", "\x1b[1;35m", "\x1b[1;36m",
"\x1b[1;37m"};
if (!InferSpewColorable()) {
return "";
}
return colors[DefaultHasher<TypeSet*>::hash(types) % 7];
}
# ifdef DEBUG
void js::InferSpewImpl(const char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "[infer] ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
}
# endif
MOZ_NORETURN MOZ_COLD static void MOZ_FORMAT_PRINTF(2, 3)
TypeFailure(JSContext* cx, const char* fmt, ...) {
char msgbuf[1024]; /* Larger error messages will be truncated */
char errbuf[1024];
va_list ap;
va_start(ap, fmt);
VsprintfLiteral(errbuf, fmt, ap);
va_end(ap);
SprintfLiteral(msgbuf, "[infer failure] %s", errbuf);
/* Dump type state, even if INFERFLAGS is unset. */
PrintTypes(cx, cx->compartment(), true);
MOZ_ReportAssertionFailure(msgbuf, __FILE__, __LINE__);
MOZ_CRASH();
}
bool js::ObjectGroupHasProperty(JSContext* cx, ObjectGroup* group, jsid id,
const Value& value) {
/*
* Check the correctness of the type information in the object's property
* against an actual value.
*/
AutoSweepObjectGroup sweep(group);
if (!group->unknownProperties(sweep) && !value.isUndefined()) {
id = IdToTypeId(id);
/* Watch for properties which inference does not monitor. */
if (id == id___proto__(cx) || id == id_constructor(cx) ||
id == id_caller(cx)) {
return true;
}
TypeSet::Type type = TypeSet::GetValueType(value);
AutoEnterAnalysis enter(cx);
/*
* We don't track types for properties inherited from prototypes which
* haven't yet been accessed during analysis of the inheriting object.
* Don't do the property instantiation now.
*/
TypeSet* types = group->maybeGetProperty(sweep, id);
if (!types) {
return true;
}
// Type set guards might miss when an object's group changes and its
// properties become unknown.
if (value.isObject()) {
if (types->unknownObject()) {
return true;
}
for (size_t i = 0; i < types->getObjectCount(); i++) {
if (TypeSet::ObjectKey* key = types->getObject(i)) {
if (key->unknownProperties()) {
return true;
}
}
}
JSObject* obj = &value.toObject();
if (!obj->hasLazyGroup() && obj->group()->maybeOriginalUnboxedGroup()) {
return true;
}
}
if (!types->hasType(type)) {
TypeFailure(cx, "Missing type in object %s %s: %s",
TypeSet::ObjectGroupString(group).get(), TypeIdString(id),
TypeSet::TypeString(type).get());
}
}
return true;
}
#endif
/////////////////////////////////////////////////////////////////////
// TypeSet
/////////////////////////////////////////////////////////////////////
TemporaryTypeSet::TemporaryTypeSet(LifoAlloc* alloc, Type type) {
if (type.isUnknown()) {
flags |= TYPE_FLAG_BASE_MASK;
return;
}
if (type.isPrimitive()) {
flags = PrimitiveTypeFlag(type.primitive());
if (flags == TYPE_FLAG_DOUBLE) {
flags |= TYPE_FLAG_INT32;
}
return;
}
if (type.isAnyObject()) {
flags |= TYPE_FLAG_ANYOBJECT;
return;
}
if (type.isGroup()) {
AutoSweepObjectGroup sweep(type.group());
if (type.group()->unknownProperties(sweep)) {
flags |= TYPE_FLAG_ANYOBJECT;
return;
}
}
setBaseObjectCount(1);
objectSet = reinterpret_cast<ObjectKey**>(type.objectKey());
if (type.isGroup()) {
ObjectGroup* ngroup = type.group();
AutoSweepObjectGroup sweep(ngroup);
if (ngroup->newScript(sweep) &&
ngroup->newScript(sweep)->initializedGroup()) {
addType(ObjectType(ngroup->newScript(sweep)->initializedGroup()), alloc);
}
}
}
bool TypeSet::mightBeMIRType(jit::MIRType type) const {
if (unknown()) {
return true;
}
if (type == jit::MIRType::Object) {
return unknownObject() || baseObjectCount() != 0;
}
switch (type) {
case jit::MIRType::Undefined:
return baseFlags() & TYPE_FLAG_UNDEFINED;
case jit::MIRType::Null:
return baseFlags() & TYPE_FLAG_NULL;
case jit::MIRType::Boolean:
return baseFlags() & TYPE_FLAG_BOOLEAN;
case jit::MIRType::Int32:
return baseFlags() & TYPE_FLAG_INT32;
case jit::MIRType::Float32: // Fall through, there's no JSVAL for Float32.
case jit::MIRType::Double:
return baseFlags() & TYPE_FLAG_DOUBLE;
case jit::MIRType::String:
return baseFlags() & TYPE_FLAG_STRING;
case jit::MIRType::Symbol:
return baseFlags() & TYPE_FLAG_SYMBOL;
case jit::MIRType::BigInt:
return baseFlags() & TYPE_FLAG_BIGINT;
case jit::MIRType::MagicOptimizedArguments:
return baseFlags() & TYPE_FLAG_LAZYARGS;
case jit::MIRType::MagicHole:
case jit::MIRType::MagicIsConstructing:
// These magic constants do not escape to script and are not observed
// in the type sets.
//
// The reason we can return false here is subtle: if Ion is asking the
// type set if it has seen such a magic constant, then the MIR in
// question is the most generic type, MIRType::Value. A magic constant
// could only be emitted by a MIR of MIRType::Value if that MIR is a
// phi, and we check that different magic constants do not flow to the
// same join point in GuessPhiType.
return false;
default:
MOZ_CRASH("Bad MIR type");
}
}
bool TypeSet::objectsAreSubset(TypeSet* other) {
if (other->unknownObject()) {
return true;
}
if (unknownObject()) {
return false;
}
for (unsigned i = 0; i < getObjectCount(); i++) {
ObjectKey* key = getObject(i);
if (!key) {
continue;
}
if (!other->hasType(ObjectType(key))) {
return false;
}
}
return true;
}
bool TypeSet::isSubset(const TypeSet* other) const {
if ((baseFlags() & other->baseFlags()) != baseFlags()) {
return false;
}
if (unknownObject()) {
MOZ_ASSERT(other->unknownObject());
} else {
for (unsigned i = 0; i < getObjectCount(); i++) {
ObjectKey* key = getObject(i);
if (!key) {
continue;
}
if (!other->hasType(ObjectType(key))) {
return false;
}
}
}
return true;
}
bool TypeSet::objectsIntersect(const TypeSet* other) const {
if (unknownObject() || other->unknownObject()) {
return true;
}
for (unsigned i = 0; i < getObjectCount(); i++) {
ObjectKey* key = getObject(i);
if (!key) {
continue;
}
if (other->hasType(ObjectType(key))) {
return true;
}
}
return false;
}
template <class TypeListT>
bool TypeSet::enumerateTypes(TypeListT* list) const {
/* If any type is possible, there's no need to worry about specifics. */
if (flags & TYPE_FLAG_UNKNOWN) {
return list->append(UnknownType());
}
/* Enqueue type set members stored as bits. */
for (TypeFlags flag = 1; flag < TYPE_FLAG_ANYOBJECT; flag <<= 1) {
if (flags & flag) {
Type type = PrimitiveType(TypeFlagPrimitive(flag));
if (!list->append(type)) {
return false;
}
}
}
/* If any object is possible, skip specifics. */
if (flags & TYPE_FLAG_ANYOBJECT) {
return list->append(AnyObjectType());
}
/* Enqueue specific object types. */
unsigned count = getObjectCount();
for (unsigned i = 0; i < count; i++) {
ObjectKey* key = getObject(i);
if (key) {
if (!list->append(ObjectType(key))) {
return false;
}
}
}
return true;
}
template bool TypeSet::enumerateTypes<TypeSet::TypeList>(TypeList* list) const;
template bool TypeSet::enumerateTypes<jit::TempTypeList>(
jit::TempTypeList* list) const;
inline bool TypeSet::addTypesToConstraint(JSContext* cx,
TypeConstraint* constraint) {
/*
* Build all types in the set into a vector before triggering the
* constraint, as doing so may modify this type set.
*/
TypeList types;
if (!enumerateTypes(&types)) {
return false;
}
for (unsigned i = 0; i < types.length(); i++) {
constraint->newType(cx, this, types[i]);
}
return true;
}
#ifdef DEBUG
static inline bool CompartmentsMatch(Compartment* a, Compartment* b) {
return !a || !b || a == b;
}
#endif
bool ConstraintTypeSet::addConstraint(JSContext* cx, TypeConstraint* constraint,
bool callExisting) {
checkMagic();
if (!constraint) {
/* OOM failure while constructing the constraint. */
return false;
}
MOZ_RELEASE_ASSERT(cx->zone()->types.activeAnalysis);
MOZ_ASSERT(
CompartmentsMatch(maybeCompartment(), constraint->maybeCompartment()));
InferSpew(ISpewOps, "addConstraint: %sT%p%s %sC%p%s %s", InferSpewColor(this),
this, InferSpewColorReset(), InferSpewColor(constraint), constraint,
InferSpewColorReset(), constraint->kind());
MOZ_ASSERT(constraint->next() == nullptr);
constraint->setNext(constraintList_);
constraintList_ = constraint;
if (callExisting) {
return addTypesToConstraint(cx, constraint);
}
return true;
}
void TypeSet::clearObjects() {
setBaseObjectCount(0);
objectSet = nullptr;
}
Compartment* TypeSet::maybeCompartment() {
if (unknownObject()) {
return nullptr;
}
unsigned objectCount = getObjectCount();
for (unsigned i = 0; i < objectCount; i++) {
ObjectKey* key = getObject(i);
if (!key) {
continue;
}
Compartment* comp = key->maybeCompartment();
if (comp) {
return comp;
}
}
return nullptr;
}
void TypeSet::addType(Type type, LifoAlloc* alloc) {
MOZ_ASSERT(CompartmentsMatch(maybeCompartment(), type.maybeCompartment()));
if (unknown()) {
return;
}
if (type.isUnknown()) {
flags |= TYPE_FLAG_BASE_MASK;
clearObjects();
MOZ_ASSERT(unknown());
return;
}
if (type.isPrimitive()) {
TypeFlags flag = PrimitiveTypeFlag(type.primitive());
if (flags & flag) {
return;
}
/* If we add float to a type set it is also considered to contain int. */
if (flag == TYPE_FLAG_DOUBLE) {
flag |= TYPE_FLAG_INT32;
}
flags |= flag;
return;
}
if (flags & TYPE_FLAG_ANYOBJECT) {
return;
}
if (type.isAnyObject()) {
goto unknownObject;
}
{
uint32_t objectCount = baseObjectCount();
ObjectKey* key = type.objectKey();
ObjectKey** pentry = TypeHashSet::Insert<ObjectKey*, ObjectKey, ObjectKey>(
*alloc, objectSet, objectCount, key);
if (!pentry) {
goto unknownObject;
}
if (*pentry) {
return;
}
*pentry = key;
setBaseObjectCount(objectCount);
// Limit the number of objects we track. There is a different limit
// depending on whether the set only contains DOM objects, which can
// have many different classes and prototypes but are still optimizable
// by IonMonkey.
if (objectCount >= TYPE_FLAG_OBJECT_COUNT_LIMIT) {
JS_STATIC_ASSERT(TYPE_FLAG_DOMOBJECT_COUNT_LIMIT >=
TYPE_FLAG_OBJECT_COUNT_LIMIT);
// Examining the entire type set is only required when we first hit
// the normal object limit.
if (objectCount == TYPE_FLAG_OBJECT_COUNT_LIMIT) {
for (unsigned i = 0; i < objectCount; i++) {
const Class* clasp = getObjectClass(i);
if (clasp && !clasp->isDOMClass()) {
goto unknownObject;
}
}
}
// Make sure the newly added object is also a DOM object.
if (!key->clasp()->isDOMClass()) {
goto unknownObject;
}
// Limit the number of DOM objects.
if (objectCount == TYPE_FLAG_DOMOBJECT_COUNT_LIMIT) {
goto unknownObject;
}
}
}
if (type.isGroup()) {
ObjectGroup* ngroup = type.group();
MOZ_ASSERT(!ngroup->singleton());
AutoSweepObjectGroup sweep(ngroup);
if (ngroup->unknownProperties(sweep)) {
goto unknownObject;
}
// If we add a partially initialized group to a type set, add the
// corresponding fully initialized group, as an object's group may change
// from the former to the latter via the acquired properties analysis.
if (ngroup->newScript(sweep) &&
ngroup->newScript(sweep)->initializedGroup()) {
addType(ObjectType(ngroup->newScript(sweep)->initializedGroup()), alloc);
}
}
if (false) {
unknownObject:
flags |= TYPE_FLAG_ANYOBJECT;
clearObjects();
}
}
// This class is used for post barriers on type set contents. The only times
// when type sets contain nursery references is when a nursery object has its
// group dynamically changed to a singleton. In such cases the type set will
// need to be traced at the next minor GC.
//
// There is no barrier used for TemporaryTypeSets. These type sets are only
// used during Ion compilation, and if some ConstraintTypeSet contains nursery
// pointers then any number of TemporaryTypeSets might as well. Thus, if there
// are any such ConstraintTypeSets in existence, all off thread Ion
// compilations are canceled by the next minor GC.
class TypeSetRef : public gc::BufferableRef {
Zone* zone;
ConstraintTypeSet* types;
#ifdef DEBUG
uint64_t minorGCNumberAtCreation;
#endif
public:
TypeSetRef(Zone* zone, ConstraintTypeSet* types)
: zone(zone),
types(types)
#ifdef DEBUG
,
minorGCNumberAtCreation(
zone->runtimeFromMainThread()->gc.minorGCCount())
#endif
{
}
void trace(JSTracer* trc) override {
MOZ_ASSERT(trc->runtime()->gc.minorGCCount() == minorGCNumberAtCreation);
types->trace(zone, trc);
}
};
void ConstraintTypeSet::postWriteBarrier(JSContext* cx, Type type) {
if (type.isSingletonUnchecked()) {
if (gc::StoreBuffer* sb = type.singletonNoBarrier()->storeBuffer()) {
sb->putGeneric(TypeSetRef(cx->zone(), this));
sb->setShouldCancelIonCompilations();
}
}
}
void ConstraintTypeSet::addType(const AutoSweepBase& sweep, JSContext* cx,
Type type) {
checkMagic();
MOZ_RELEASE_ASSERT(cx->zone()->types.activeAnalysis);
if (hasType(type)) {
return;
}
TypeSet::addType(type, &cx->typeLifoAlloc());
if (type.isObjectUnchecked() && unknownObject()) {
type = AnyObjectType();
}
postWriteBarrier(cx, type);
InferSpew(ISpewOps, "addType: %sT%p%s %s", InferSpewColor(this), this,
InferSpewColorReset(), TypeString(type).get());
/* Propagate the type to all constraints. */
if (!cx->helperThread()) {
TypeConstraint* constraint = constraintList(sweep);
while (constraint) {
constraint->newType(cx, this, type);
constraint = constraint->next();
}
} else {
MOZ_ASSERT(!constraintList_);
}
}
void TypeSet::print(FILE* fp) {
bool fromDebugger = !fp;
if (!fp) {
fp = stderr;
}
if (flags & TYPE_FLAG_NON_DATA_PROPERTY) {
fprintf(fp, " [non-data]");
}
if (flags & TYPE_FLAG_NON_WRITABLE_PROPERTY) {
fprintf(fp, " [non-writable]");
}
if (definiteProperty()) {
fprintf(fp, " [definite:%d]", definiteSlot());
}
if (baseFlags() == 0 && !baseObjectCount()) {
fprintf(fp, " missing");
return;
}
if (flags & TYPE_FLAG_UNKNOWN) {
fprintf(fp, " unknown");
}
if (flags & TYPE_FLAG_ANYOBJECT) {
fprintf(fp, " object");
}
if (flags & TYPE_FLAG_UNDEFINED) {
fprintf(fp, " void");
}
if (flags & TYPE_FLAG_NULL) {
fprintf(fp, " null");
}
if (flags & TYPE_FLAG_BOOLEAN) {
fprintf(fp, " bool");
}
if (flags & TYPE_FLAG_INT32) {
fprintf(fp, " int");
}
if (flags & TYPE_FLAG_DOUBLE) {
fprintf(fp, " float");
}
if (flags & TYPE_FLAG_STRING) {
fprintf(fp, " string");
}
if (flags & TYPE_FLAG_SYMBOL) {
fprintf(fp, " symbol");
}
if (flags & TYPE_FLAG_BIGINT) {
fprintf(fp, " BigInt");
}
if (flags & TYPE_FLAG_LAZYARGS) {
fprintf(fp, " lazyargs");
}
uint32_t objectCount = baseObjectCount();
if (objectCount) {
fprintf(fp, " object[%u]", objectCount);
unsigned count = getObjectCount();
for (unsigned i = 0; i < count; i++) {
ObjectKey* key = getObject(i);
if (key) {
fprintf(fp, " %s", TypeString(ObjectType(key)).get());
}
}
}
if (fromDebugger) {
fprintf(fp, "\n");
}
}
/* static */ void TypeSet::readBarrier(const TypeSet* types) {
if (types->unknownObject()) {
return;
}
for (unsigned i = 0; i < types->getObjectCount(); i++) {
if (ObjectKey* key = types->getObject(i)) {
if (key->isSingleton()) {
(void)key->singleton();
} else {
(void)key->group();
}
}
}
}
/* static */ bool TypeSet::IsTypeMarked(JSRuntime* rt, TypeSet::Type* v) {
bool rv;
if (v->isSingletonUnchecked()) {
JSObject* obj = v->singletonNoBarrier();
rv = IsMarkedUnbarriered(rt, &obj);
*v = TypeSet::ObjectType(obj);
} else if (v->isGroupUnchecked()) {
ObjectGroup* group = v->groupNoBarrier();
rv = IsMarkedUnbarriered(rt, &group);
*v = TypeSet::ObjectType(group);
} else {
rv = true;
}
return rv;
}
static inline bool IsObjectKeyAboutToBeFinalized(TypeSet::ObjectKey** keyp) {
TypeSet::ObjectKey* key = *keyp;
bool isAboutToBeFinalized;
if (key->isGroup()) {
ObjectGroup* group = key->groupNoBarrier();
isAboutToBeFinalized = IsAboutToBeFinalizedUnbarriered(&group);
if (!isAboutToBeFinalized) {
*keyp = TypeSet::ObjectKey::get(group);
}
} else {
MOZ_ASSERT(key->isSingleton());
JSObject* singleton = key->singletonNoBarrier();
isAboutToBeFinalized = IsAboutToBeFinalizedUnbarriered(&singleton);
if (!isAboutToBeFinalized) {
*keyp = TypeSet::ObjectKey::get(singleton);
}
}
return isAboutToBeFinalized;
}
bool TypeSet::IsTypeAboutToBeFinalized(TypeSet::Type* v) {
bool isAboutToBeFinalized;
if (v->isObjectUnchecked()) {
TypeSet::ObjectKey* key = v->objectKey();
isAboutToBeFinalized = IsObjectKeyAboutToBeFinalized(&key);
if (!isAboutToBeFinalized) {
*v = TypeSet::ObjectType(key);
}
} else {
isAboutToBeFinalized = false;
}
return isAboutToBeFinalized;
}
bool TypeSet::cloneIntoUninitialized(LifoAlloc* alloc,
TemporaryTypeSet* result) const {
unsigned objectCount = baseObjectCount();
unsigned capacity =
(objectCount >= 2) ? TypeHashSet::Capacity(objectCount) : 0;
ObjectKey** newSet;
if (capacity) {
// We allocate an extra word right before the array that stores the
// capacity, so make sure we clone that as well.
newSet = alloc->newArray<ObjectKey*>(capacity + 1);
if (!newSet) {
return false;
}
newSet++;
PodCopy(newSet - 1, objectSet - 1, capacity + 1);
}
new (result) TemporaryTypeSet(flags, capacity ? newSet : objectSet);
return true;
}
TemporaryTypeSet* TypeSet::clone(LifoAlloc* alloc) const {
TemporaryTypeSet* res = alloc->pod_malloc<TemporaryTypeSet>();
if (!res || !cloneIntoUninitialized(alloc, res)) {
return nullptr;
}
return res;
}
TemporaryTypeSet* TypeSet::cloneObjectsOnly(LifoAlloc* alloc) {
TemporaryTypeSet* res = clone(alloc);
if (!res) {
return nullptr;
}
res->flags &= ~TYPE_FLAG_BASE_MASK | TYPE_FLAG_ANYOBJECT;
return res;
}
TemporaryTypeSet* TypeSet::cloneWithoutObjects(LifoAlloc* alloc) {
TemporaryTypeSet* res = alloc->new_<TemporaryTypeSet>();
if (!res) {
return nullptr;
}
res->flags = flags & ~TYPE_FLAG_ANYOBJECT;
res->setBaseObjectCount(0);
return res;
}
/* static */ TemporaryTypeSet* TypeSet::unionSets(TypeSet* a, TypeSet* b,
LifoAlloc* alloc) {
TemporaryTypeSet* res = alloc->new_<TemporaryTypeSet>(
a->baseFlags() | b->baseFlags(), static_cast<ObjectKey**>(nullptr));
if (!res) {
return nullptr;
}
if (!res->unknownObject()) {
for (size_t i = 0; i < a->getObjectCount() && !res->unknownObject(); i++) {
if (ObjectKey* key = a->getObject(i)) {
res->addType(ObjectType(key), alloc);
}
}
for (size_t i = 0; i < b->getObjectCount() && !res->unknownObject(); i++) {
if (ObjectKey* key = b->getObject(i)) {
res->addType(ObjectType(key), alloc);
}
}
}
return res;
}
/* static */ TemporaryTypeSet* TypeSet::removeSet(TemporaryTypeSet* input,
TemporaryTypeSet* removal,
LifoAlloc* alloc) {
// Only allow removal of primitives and the "AnyObject" flag.
MOZ_ASSERT(!removal->unknown());
MOZ_ASSERT_IF(!removal->unknownObject(), removal->getObjectCount() == 0);
uint32_t flags = input->baseFlags() & ~removal->baseFlags();
TemporaryTypeSet* res =