/
builtins-collections-gen.cc
1779 lines (1467 loc) Β· 67.9 KB
/
builtins-collections-gen.cc
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
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/builtins/builtins-constructor-gen.h"
#include "src/builtins/builtins-iterator-gen.h"
#include "src/builtins/builtins-utils-gen.h"
#include "src/code-stub-assembler.h"
#include "src/objects/hash-table.h"
namespace v8 {
namespace internal {
using compiler::Node;
class CollectionsBuiltinsAssembler : public CodeStubAssembler {
public:
explicit CollectionsBuiltinsAssembler(compiler::CodeAssemblerState* state)
: CodeStubAssembler(state) {}
protected:
Node* AllocateJSMap(Node* js_map_function);
template <typename CollectionType>
Node* AllocateOrderedHashTable();
Node* AllocateJSCollection(Node* js_map_function);
template <typename IteratorType>
Node* AllocateJSCollectionIterator(Node* context, int map_index,
Node* collection);
Node* GetHash(Node* const key);
Node* CallGetHashRaw(Node* const key);
Node* CallGetOrCreateHashRaw(Node* const key);
// Transitions the iterator to the non obsolete backing store.
// This is a NOP if the [table] is not obsolete.
typedef std::function<void(Node* const table, Node* const index)>
UpdateInTransition;
template <typename TableType>
std::tuple<Node*, Node*> Transition(
Node* const table, Node* const index,
UpdateInTransition const& update_in_transition);
template <typename IteratorType, typename TableType>
std::tuple<Node*, Node*> TransitionAndUpdate(Node* const iterator);
template <typename TableType>
std::tuple<Node*, Node*, Node*> NextSkipHoles(Node* table, Node* index,
Label* if_end);
// Builds code that finds OrderedHashTable entry for a key with hash code
// {hash} with using the comparison code generated by {key_compare}. The code
// jumps to {entry_found} if the key is found, or to {not_found} if the key
// was not found. In the {entry_found} branch, the variable
// entry_start_position will be bound to the index of the entry (relative to
// OrderedHashTable::kHashTableStartIndex).
//
// The {CollectionType} template parameter stands for the particular instance
// of OrderedHashTable, it should be OrderedHashMap or OrderedHashSet.
template <typename CollectionType>
void FindOrderedHashTableEntry(
Node* table, Node* hash,
std::function<void(Node* other, Label* if_same, Label* if_not_same)>
key_compare,
Variable* entry_start_position, Label* entry_found, Label* not_found);
// Specialization for Smi.
// The {result} variable will contain the entry index if the key was found,
// or the hash code otherwise.
template <typename CollectionType>
void FindOrderedHashTableEntryForSmiKey(Node* table, Node* key_tagged,
Variable* result, Label* entry_found,
Label* not_found);
void SameValueZeroSmi(Node* key_smi, Node* candidate_key, Label* if_same,
Label* if_not_same);
// Specialization for heap numbers.
// The {result} variable will contain the entry index if the key was found,
// or the hash code otherwise.
void SameValueZeroHeapNumber(Node* key_string, Node* candidate_key,
Label* if_same, Label* if_not_same);
template <typename CollectionType>
void FindOrderedHashTableEntryForHeapNumberKey(Node* context, Node* table,
Node* key_heap_number,
Variable* result,
Label* entry_found,
Label* not_found);
// Specialization for string.
// The {result} variable will contain the entry index if the key was found,
// or the hash code otherwise.
template <typename CollectionType>
void FindOrderedHashTableEntryForStringKey(Node* context, Node* table,
Node* key_tagged, Variable* result,
Label* entry_found,
Label* not_found);
Node* ComputeStringHash(Node* context, Node* string_key);
void SameValueZeroString(Node* context, Node* key_string, Node* candidate_key,
Label* if_same, Label* if_not_same);
// Specialization for non-strings, non-numbers. For those we only need
// reference equality to compare the keys.
// The {result} variable will contain the entry index if the key was found,
// or the hash code otherwise. If the hash-code has not been computed, it
// should be Smi -1.
template <typename CollectionType>
void FindOrderedHashTableEntryForOtherKey(Node* context, Node* table,
Node* key, Variable* result,
Label* entry_found,
Label* not_found);
template <typename CollectionType>
void TryLookupOrderedHashTableIndex(Node* const table, Node* const key,
Node* const context, Variable* result,
Label* if_entry_found,
Label* if_not_found);
Node* NormalizeNumberKey(Node* key);
void StoreOrderedHashMapNewEntry(Node* const table, Node* const key,
Node* const value, Node* const hash,
Node* const number_of_buckets,
Node* const occupancy);
void StoreOrderedHashSetNewEntry(Node* const table, Node* const key,
Node* const hash,
Node* const number_of_buckets,
Node* const occupancy);
};
template <typename CollectionType>
Node* CollectionsBuiltinsAssembler::AllocateOrderedHashTable() {
static const int kCapacity = CollectionType::kMinCapacity;
static const int kBucketCount = kCapacity / CollectionType::kLoadFactor;
static const int kDataTableLength = kCapacity * CollectionType::kEntrySize;
static const int kFixedArrayLength =
CollectionType::kHashTableStartIndex + kBucketCount + kDataTableLength;
static const int kDataTableStartIndex =
CollectionType::kHashTableStartIndex + kBucketCount;
STATIC_ASSERT(base::bits::IsPowerOfTwo(kCapacity));
STATIC_ASSERT(kCapacity <= CollectionType::kMaxCapacity);
// Allocate the table and add the proper map.
const ElementsKind elements_kind = HOLEY_ELEMENTS;
Node* const length_intptr = IntPtrConstant(kFixedArrayLength);
Node* const table = AllocateFixedArray(elements_kind, length_intptr);
CSA_ASSERT(this,
IntPtrLessThanOrEqual(
length_intptr, IntPtrConstant(FixedArray::kMaxRegularLength)));
Heap::RootListIndex map_index = Heap::kOrderedHashTableMapRootIndex;
// TODO(gsathya): Directly store correct in AllocateFixedArray,
// instead of overwriting here.
StoreMapNoWriteBarrier(table, map_index);
// Initialize the OrderedHashTable fields.
const WriteBarrierMode barrier_mode = SKIP_WRITE_BARRIER;
StoreFixedArrayElement(table, CollectionType::kNumberOfElementsIndex,
SmiConstant(0), barrier_mode);
StoreFixedArrayElement(table, CollectionType::kNumberOfDeletedElementsIndex,
SmiConstant(0), barrier_mode);
StoreFixedArrayElement(table, CollectionType::kNumberOfBucketsIndex,
SmiConstant(kBucketCount), barrier_mode);
// Fill the buckets with kNotFound.
Node* const not_found = SmiConstant(CollectionType::kNotFound);
STATIC_ASSERT(CollectionType::kHashTableStartIndex ==
CollectionType::kNumberOfBucketsIndex + 1);
STATIC_ASSERT((CollectionType::kHashTableStartIndex + kBucketCount) ==
kDataTableStartIndex);
for (int i = 0; i < kBucketCount; i++) {
StoreFixedArrayElement(table, CollectionType::kHashTableStartIndex + i,
not_found, barrier_mode);
}
// Fill the data table with undefined.
STATIC_ASSERT(kDataTableStartIndex + kDataTableLength == kFixedArrayLength);
for (int i = 0; i < kDataTableLength; i++) {
StoreFixedArrayElement(table, kDataTableStartIndex + i, UndefinedConstant(),
barrier_mode);
}
return table;
}
Node* CollectionsBuiltinsAssembler::AllocateJSCollection(
Node* js_map_function) {
CSA_ASSERT(this, IsConstructorMap(LoadMap(js_map_function)));
Node* const initial_map = LoadObjectField(
js_map_function, JSFunction::kPrototypeOrInitialMapOffset);
Node* const instance = AllocateJSObjectFromMap(initial_map);
StoreObjectFieldRoot(instance, JSMap::kTableOffset,
Heap::kUndefinedValueRootIndex);
return instance;
}
template <typename IteratorType>
Node* CollectionsBuiltinsAssembler::AllocateJSCollectionIterator(
Node* context, int map_index, Node* collection) {
Node* const table = LoadObjectField(collection, JSCollection::kTableOffset);
Node* const native_context = LoadNativeContext(context);
Node* const iterator_map = LoadContextElement(native_context, map_index);
Node* const iterator = AllocateInNewSpace(IteratorType::kSize);
StoreMapNoWriteBarrier(iterator, iterator_map);
StoreObjectFieldRoot(iterator, IteratorType::kPropertiesOrHashOffset,
Heap::kEmptyFixedArrayRootIndex);
StoreObjectFieldRoot(iterator, IteratorType::kElementsOffset,
Heap::kEmptyFixedArrayRootIndex);
StoreObjectFieldNoWriteBarrier(iterator, IteratorType::kTableOffset, table);
StoreObjectFieldNoWriteBarrier(iterator, IteratorType::kIndexOffset,
SmiConstant(0));
return iterator;
}
TF_BUILTIN(MapConstructor, CollectionsBuiltinsAssembler) {
const int kIterableArg = 0;
Node* argc =
ChangeInt32ToIntPtr(Parameter(BuiltinDescriptor::kArgumentsCount));
CodeStubArguments args(this, argc);
Node* const iterable = args.GetOptionalArgumentValue(kIterableArg);
Node* const new_target = Parameter(BuiltinDescriptor::kNewTarget);
Node* const context = Parameter(BuiltinDescriptor::kContext);
Label if_target_is_undefined(this, Label::kDeferred);
GotoIf(IsUndefined(new_target), &if_target_is_undefined);
Node* const native_context = LoadNativeContext(context);
Node* const js_map_fun =
LoadContextElement(native_context, Context::JS_MAP_FUN_INDEX);
VARIABLE(var_result, MachineRepresentation::kTagged);
Label init(this), exit(this), if_targetisnotmodified(this),
if_targetismodified(this);
Branch(WordEqual(js_map_fun, new_target), &if_targetisnotmodified,
&if_targetismodified);
BIND(&if_targetisnotmodified);
{
Node* const instance = AllocateJSCollection(js_map_fun);
var_result.Bind(instance);
Goto(&init);
}
BIND(&if_targetismodified);
{
ConstructorBuiltinsAssembler constructor_assembler(this->state());
Node* const instance = constructor_assembler.EmitFastNewObject(
context, js_map_fun, new_target);
var_result.Bind(instance);
Goto(&init);
}
BIND(&init);
Node* table = AllocateOrderedHashTable<OrderedHashMap>();
StoreObjectField(var_result.value(), JSMap::kTableOffset, table);
GotoIf(Word32Or(IsUndefined(iterable), IsNull(iterable)), &exit);
Label if_notcallable(this);
// TODO(gsathya): Add fast path for unmodified maps.
Node* const adder = GetProperty(context, var_result.value(),
isolate()->factory()->set_string());
GotoIf(TaggedIsSmi(adder), &if_notcallable);
GotoIfNot(IsCallable(adder), &if_notcallable);
IteratorBuiltinsAssembler iterator_assembler(this->state());
Node* const iterator = iterator_assembler.GetIterator(context, iterable);
GotoIf(IsUndefined(iterator), &exit);
Node* const fast_iterator_result_map =
LoadContextElement(native_context, Context::ITERATOR_RESULT_MAP_INDEX);
VARIABLE(var_exception, MachineRepresentation::kTagged, TheHoleConstant());
Label loop(this), if_notobject(this), if_exception(this);
Goto(&loop);
BIND(&loop);
{
Node* const next = iterator_assembler.IteratorStep(
context, iterator, &exit, fast_iterator_result_map);
Node* const next_value = iterator_assembler.IteratorValue(
context, next, fast_iterator_result_map);
GotoIf(TaggedIsSmi(next_value), &if_notobject);
GotoIfNot(IsJSReceiver(next_value), &if_notobject);
Node* const k =
GetProperty(context, next_value, isolate()->factory()->zero_string());
GotoIfException(k, &if_exception, &var_exception);
Node* const v =
GetProperty(context, next_value, isolate()->factory()->one_string());
GotoIfException(v, &if_exception, &var_exception);
Node* add_call = CallJS(CodeFactory::Call(isolate()), context, adder,
var_result.value(), k, v);
GotoIfException(add_call, &if_exception, &var_exception);
Goto(&loop);
BIND(&if_notobject);
{
Node* ret = CallRuntime(
Runtime::kThrowTypeError, context,
SmiConstant(MessageTemplate::kIteratorValueNotAnObject), next_value);
GotoIfException(ret, &if_exception, &var_exception);
Unreachable();
}
}
BIND(&if_exception);
{
iterator_assembler.IteratorCloseOnException(context, iterator,
&var_exception);
}
BIND(&if_notcallable);
{
Node* const receiver_str = HeapConstant(isolate()->factory()->add_string());
ThrowTypeError(context, MessageTemplate::kPropertyNotFunction, adder,
receiver_str, var_result.value());
}
BIND(&if_target_is_undefined);
ThrowTypeError(context, MessageTemplate::kConstructorNotFunction,
HeapConstant(isolate()->factory()->Map_string()));
BIND(&exit);
args.PopAndReturn(var_result.value());
}
TF_BUILTIN(SetConstructor, CollectionsBuiltinsAssembler) {
const int kIterableArg = 0;
Node* argc =
ChangeInt32ToIntPtr(Parameter(BuiltinDescriptor::kArgumentsCount));
CodeStubArguments args(this, argc);
Node* const iterable = args.GetOptionalArgumentValue(kIterableArg);
Node* const new_target = Parameter(BuiltinDescriptor::kNewTarget);
Node* const context = Parameter(BuiltinDescriptor::kContext);
Label if_target_is_undefined(this, Label::kDeferred);
GotoIf(IsUndefined(new_target), &if_target_is_undefined);
Node* const native_context = LoadNativeContext(context);
Node* const js_set_fun =
LoadContextElement(native_context, Context::JS_SET_FUN_INDEX);
VARIABLE(var_result, MachineRepresentation::kTagged);
Label init(this), exit(this), if_targetisnotmodified(this),
if_targetismodified(this);
Branch(WordEqual(js_set_fun, new_target), &if_targetisnotmodified,
&if_targetismodified);
BIND(&if_targetisnotmodified);
{
Node* const instance = AllocateJSCollection(js_set_fun);
var_result.Bind(instance);
Goto(&init);
}
BIND(&if_targetismodified);
{
ConstructorBuiltinsAssembler constructor_assembler(this->state());
Node* const instance = constructor_assembler.EmitFastNewObject(
context, js_set_fun, new_target);
var_result.Bind(instance);
Goto(&init);
}
BIND(&init);
Node* table = AllocateOrderedHashTable<OrderedHashSet>();
StoreObjectField(var_result.value(), JSSet::kTableOffset, table);
GotoIf(Word32Or(IsUndefined(iterable), IsNull(iterable)), &exit);
Label if_notcallable(this);
// TODO(gsathya): Add fast path for unmodified maps.
Node* const adder = GetProperty(context, var_result.value(),
isolate()->factory()->add_string());
GotoIf(TaggedIsSmi(adder), &if_notcallable);
GotoIfNot(IsCallable(adder), &if_notcallable);
IteratorBuiltinsAssembler iterator_assembler(this->state());
Node* const iterator = iterator_assembler.GetIterator(context, iterable);
GotoIf(IsUndefined(iterator), &exit);
Node* const fast_iterator_result_map =
LoadContextElement(native_context, Context::ITERATOR_RESULT_MAP_INDEX);
VARIABLE(var_exception, MachineRepresentation::kTagged, TheHoleConstant());
Label loop(this), if_notobject(this), if_exception(this);
Goto(&loop);
BIND(&loop);
{
Node* const next = iterator_assembler.IteratorStep(
context, iterator, &exit, fast_iterator_result_map);
Node* const next_value = iterator_assembler.IteratorValue(
context, next, fast_iterator_result_map);
Node* add_call = CallJS(CodeFactory::Call(isolate()), context, adder,
var_result.value(), next_value);
GotoIfException(add_call, &if_exception, &var_exception);
Goto(&loop);
}
BIND(&if_exception);
{
iterator_assembler.IteratorCloseOnException(context, iterator,
&var_exception);
}
BIND(&if_notcallable);
ThrowTypeError(context, MessageTemplate::kPropertyNotFunction, adder,
HeapConstant(isolate()->factory()->add_string()),
var_result.value());
BIND(&if_target_is_undefined);
ThrowTypeError(context, MessageTemplate::kConstructorNotFunction,
HeapConstant(isolate()->factory()->Set_string()));
BIND(&exit);
args.PopAndReturn(var_result.value());
}
Node* CollectionsBuiltinsAssembler::CallGetOrCreateHashRaw(Node* const key) {
Node* const function_addr =
ExternalConstant(ExternalReference::get_or_create_hash_raw(isolate()));
Node* const isolate_ptr =
ExternalConstant(ExternalReference::isolate_address(isolate()));
MachineType type_ptr = MachineType::Pointer();
MachineType type_tagged = MachineType::AnyTagged();
Node* const result = CallCFunction2(type_tagged, type_ptr, type_tagged,
function_addr, isolate_ptr, key);
return result;
}
Node* CollectionsBuiltinsAssembler::CallGetHashRaw(Node* const key) {
Node* const function_addr = ExternalConstant(
ExternalReference::orderedhashmap_gethash_raw(isolate()));
Node* const isolate_ptr =
ExternalConstant(ExternalReference::isolate_address(isolate()));
MachineType type_ptr = MachineType::Pointer();
MachineType type_tagged = MachineType::AnyTagged();
Node* const result = CallCFunction2(type_tagged, type_ptr, type_tagged,
function_addr, isolate_ptr, key);
return SmiUntag(result);
}
Node* CollectionsBuiltinsAssembler::GetHash(Node* const key) {
VARIABLE(var_result, MachineType::PointerRepresentation());
Label if_jsobject(this), other(this), done(this);
Node* instance_type = LoadMapInstanceType(LoadMap(key));
Branch(IsJSObjectInstanceType(instance_type), &if_jsobject, &other);
BIND(&if_jsobject);
{
Node* hash = LoadHashForJSObject(key, instance_type);
// TODO(gsathya): Change all uses of -1 to PropertyArray::kNoHashSentinel.
var_result.Bind(SelectConstant(
Word32Equal(hash, Int32Constant(PropertyArray::kNoHashSentinel)),
IntPtrConstant(-1), ChangeInt32ToIntPtr(hash),
MachineType::PointerRepresentation()));
Goto(&done);
}
BIND(&other);
{
var_result.Bind(CallGetHashRaw(key));
Goto(&done);
}
BIND(&done);
return var_result.value();
}
void CollectionsBuiltinsAssembler::SameValueZeroSmi(Node* key_smi,
Node* candidate_key,
Label* if_same,
Label* if_not_same) {
// If the key is the same, we are done.
GotoIf(WordEqual(candidate_key, key_smi), if_same);
// If the candidate key is smi, then it must be different (because
// we already checked for equality above).
GotoIf(TaggedIsSmi(candidate_key), if_not_same);
// If the candidate key is not smi, we still have to check if it is a
// heap number with the same value.
GotoIfNot(IsHeapNumber(candidate_key), if_not_same);
Node* const candidate_key_number = LoadHeapNumberValue(candidate_key);
Node* const key_number = SmiToFloat64(key_smi);
GotoIf(Float64Equal(candidate_key_number, key_number), if_same);
Goto(if_not_same);
}
template <typename CollectionType>
void CollectionsBuiltinsAssembler::FindOrderedHashTableEntryForSmiKey(
Node* table, Node* smi_key, Variable* result, Label* entry_found,
Label* not_found) {
Node* const key_untagged = SmiUntag(smi_key);
Node* const hash = ChangeInt32ToIntPtr(ComputeUnseededHash(key_untagged));
CSA_ASSERT(this, IntPtrGreaterThanOrEqual(hash, IntPtrConstant(0)));
result->Bind(hash);
FindOrderedHashTableEntry<CollectionType>(
table, hash,
[&](Node* other_key, Label* if_same, Label* if_not_same) {
SameValueZeroSmi(smi_key, other_key, if_same, if_not_same);
},
result, entry_found, not_found);
}
template <typename CollectionType>
void CollectionsBuiltinsAssembler::FindOrderedHashTableEntryForStringKey(
Node* context, Node* table, Node* key_tagged, Variable* result,
Label* entry_found, Label* not_found) {
Node* const hash = ComputeStringHash(context, key_tagged);
CSA_ASSERT(this, IntPtrGreaterThanOrEqual(hash, IntPtrConstant(0)));
result->Bind(hash);
FindOrderedHashTableEntry<CollectionType>(
table, hash,
[&](Node* other_key, Label* if_same, Label* if_not_same) {
SameValueZeroString(context, key_tagged, other_key, if_same,
if_not_same);
},
result, entry_found, not_found);
}
template <typename CollectionType>
void CollectionsBuiltinsAssembler::FindOrderedHashTableEntryForHeapNumberKey(
Node* context, Node* table, Node* key_heap_number, Variable* result,
Label* entry_found, Label* not_found) {
Node* hash = CallGetHashRaw(key_heap_number);
CSA_ASSERT(this, IntPtrGreaterThanOrEqual(hash, IntPtrConstant(0)));
result->Bind(hash);
Node* const key_float = LoadHeapNumberValue(key_heap_number);
FindOrderedHashTableEntry<CollectionType>(
table, hash,
[&](Node* other_key, Label* if_same, Label* if_not_same) {
SameValueZeroHeapNumber(key_float, other_key, if_same, if_not_same);
},
result, entry_found, not_found);
}
template <typename CollectionType>
void CollectionsBuiltinsAssembler::FindOrderedHashTableEntryForOtherKey(
Node* context, Node* table, Node* key, Variable* result, Label* entry_found,
Label* not_found) {
Node* hash = GetHash(key);
result->Bind(hash);
FindOrderedHashTableEntry<CollectionType>(
table, hash,
[&](Node* other_key, Label* if_same, Label* if_not_same) {
Branch(WordEqual(key, other_key), if_same, if_not_same);
},
result, entry_found, not_found);
}
Node* CollectionsBuiltinsAssembler::ComputeStringHash(Node* context,
Node* string_key) {
VARIABLE(var_result, MachineType::PointerRepresentation());
Label hash_not_computed(this), done(this, &var_result);
Node* hash =
ChangeInt32ToIntPtr(LoadNameHash(string_key, &hash_not_computed));
var_result.Bind(hash);
Goto(&done);
BIND(&hash_not_computed);
var_result.Bind(CallGetHashRaw(string_key));
Goto(&done);
BIND(&done);
return var_result.value();
}
void CollectionsBuiltinsAssembler::SameValueZeroString(Node* context,
Node* key_string,
Node* candidate_key,
Label* if_same,
Label* if_not_same) {
// If the candidate is not a string, the keys are not equal.
GotoIf(TaggedIsSmi(candidate_key), if_not_same);
GotoIfNot(IsString(candidate_key), if_not_same);
Branch(WordEqual(CallBuiltin(Builtins::kStringEqual, context, key_string,
candidate_key),
TrueConstant()),
if_same, if_not_same);
}
void CollectionsBuiltinsAssembler::SameValueZeroHeapNumber(Node* key_float,
Node* candidate_key,
Label* if_same,
Label* if_not_same) {
Label if_smi(this), if_keyisnan(this);
// If the candidate is not a string, the keys are not equal.
GotoIf(TaggedIsSmi(candidate_key), &if_smi);
GotoIfNot(IsHeapNumber(candidate_key), if_not_same);
{
// {candidate_key} is a heap number.
Node* const candidate_float = LoadHeapNumberValue(candidate_key);
GotoIf(Float64Equal(key_float, candidate_float), if_same);
// SameValueZero needs to treat NaNs as equal. First check if {key_float}
// is NaN.
BranchIfFloat64IsNaN(key_float, &if_keyisnan, if_not_same);
BIND(&if_keyisnan);
{
// Return true iff {candidate_key} is NaN.
Branch(Float64Equal(candidate_float, candidate_float), if_not_same,
if_same);
}
}
BIND(&if_smi);
{
Node* const candidate_float = SmiToFloat64(candidate_key);
Branch(Float64Equal(key_float, candidate_float), if_same, if_not_same);
}
}
template <typename CollectionType>
void CollectionsBuiltinsAssembler::FindOrderedHashTableEntry(
Node* table, Node* hash,
std::function<void(Node*, Label*, Label*)> key_compare,
Variable* entry_start_position, Label* entry_found, Label* not_found) {
// Get the index of the bucket.
Node* const number_of_buckets = SmiUntag(
LoadFixedArrayElement(table, CollectionType::kNumberOfBucketsIndex));
Node* const bucket =
WordAnd(hash, IntPtrSub(number_of_buckets, IntPtrConstant(1)));
Node* const first_entry = SmiUntag(LoadFixedArrayElement(
table, bucket, CollectionType::kHashTableStartIndex * kPointerSize));
// Walk the bucket chain.
Node* entry_start;
Label if_key_found(this);
{
VARIABLE(var_entry, MachineType::PointerRepresentation(), first_entry);
Label loop(this, {&var_entry, entry_start_position}),
continue_next_entry(this);
Goto(&loop);
BIND(&loop);
// If the entry index is the not-found sentinel, we are done.
GotoIf(
WordEqual(var_entry.value(), IntPtrConstant(CollectionType::kNotFound)),
not_found);
// Make sure the entry index is within range.
CSA_ASSERT(
this,
UintPtrLessThan(
var_entry.value(),
SmiUntag(SmiAdd(
LoadFixedArrayElement(table,
CollectionType::kNumberOfElementsIndex),
LoadFixedArrayElement(
table, CollectionType::kNumberOfDeletedElementsIndex)))));
// Compute the index of the entry relative to kHashTableStartIndex.
entry_start =
IntPtrAdd(IntPtrMul(var_entry.value(),
IntPtrConstant(CollectionType::kEntrySize)),
number_of_buckets);
// Load the key from the entry.
Node* const candidate_key = LoadFixedArrayElement(
table, entry_start,
CollectionType::kHashTableStartIndex * kPointerSize);
key_compare(candidate_key, &if_key_found, &continue_next_entry);
BIND(&continue_next_entry);
// Load the index of the next entry in the bucket chain.
var_entry.Bind(SmiUntag(LoadFixedArrayElement(
table, entry_start,
(CollectionType::kHashTableStartIndex + CollectionType::kChainOffset) *
kPointerSize)));
Goto(&loop);
}
BIND(&if_key_found);
entry_start_position->Bind(entry_start);
Goto(entry_found);
}
TF_BUILTIN(OrderedHashTableHealIndex, CollectionsBuiltinsAssembler) {
Node* table = Parameter(Descriptor::kTable);
Node* index = Parameter(Descriptor::kIndex);
CSA_ASSERT(this, TaggedIsNotSmi(table));
CSA_ASSERT(this, TaggedIsSmi(index));
Label return_index(this), return_zero(this);
// Check if we need to update the {index}.
GotoIfNot(SmiLessThan(SmiConstant(Smi::kZero), index), &return_zero);
// Check if the {table} was cleared.
Node* number_of_deleted_elements = LoadAndUntagObjectField(
table, OrderedHashTableBase::kNumberOfDeletedElementsOffset);
GotoIf(WordEqual(number_of_deleted_elements,
IntPtrConstant(OrderedHashTableBase::kClearedTableSentinel)),
&return_zero);
VARIABLE(var_i, MachineType::PointerRepresentation(), IntPtrConstant(0));
VARIABLE(var_index, MachineRepresentation::kTagged, index);
Label loop(this, {&var_i, &var_index});
Goto(&loop);
BIND(&loop);
{
Node* i = var_i.value();
GotoIfNot(IntPtrLessThan(i, number_of_deleted_elements), &return_index);
Node* removed_index = LoadFixedArrayElement(
table, i, OrderedHashTableBase::kRemovedHolesIndex * kPointerSize);
GotoIf(SmiGreaterThanOrEqual(removed_index, index), &return_index);
Decrement(&var_index, 1, SMI_PARAMETERS);
Increment(&var_i);
Goto(&loop);
}
BIND(&return_index);
Return(var_index.value());
BIND(&return_zero);
Return(SmiConstant(Smi::kZero));
}
template <typename TableType>
std::tuple<Node*, Node*> CollectionsBuiltinsAssembler::Transition(
Node* const table, Node* const index,
UpdateInTransition const& update_in_transition) {
VARIABLE(var_index, MachineType::PointerRepresentation(), index);
VARIABLE(var_table, MachineRepresentation::kTagged, table);
Label if_done(this), if_transition(this, Label::kDeferred);
Branch(TaggedIsSmi(
LoadObjectField(var_table.value(), TableType::kNextTableOffset)),
&if_done, &if_transition);
BIND(&if_transition);
{
Label loop(this, {&var_table, &var_index}), done_loop(this);
Goto(&loop);
BIND(&loop);
{
Node* table = var_table.value();
Node* index = var_index.value();
Node* next_table = LoadObjectField(table, TableType::kNextTableOffset);
GotoIf(TaggedIsSmi(next_table), &done_loop);
var_table.Bind(next_table);
var_index.Bind(
SmiUntag(CallBuiltin(Builtins::kOrderedHashTableHealIndex,
NoContextConstant(), table, SmiTag(index))));
Goto(&loop);
}
BIND(&done_loop);
// Update with the new {table} and {index}.
update_in_transition(var_table.value(), var_index.value());
Goto(&if_done);
}
BIND(&if_done);
return std::tuple<Node*, Node*>(var_table.value(), var_index.value());
}
template <typename IteratorType, typename TableType>
std::tuple<Node*, Node*> CollectionsBuiltinsAssembler::TransitionAndUpdate(
Node* const iterator) {
return Transition<TableType>(
LoadObjectField(iterator, IteratorType::kTableOffset),
LoadAndUntagObjectField(iterator, IteratorType::kIndexOffset),
[this, iterator](Node* const table, Node* const index) {
// Update the {iterator} with the new state.
StoreObjectField(iterator, IteratorType::kTableOffset, table);
StoreObjectFieldNoWriteBarrier(iterator, IteratorType::kIndexOffset,
SmiTag(index));
});
}
template <typename TableType>
std::tuple<Node*, Node*, Node*> CollectionsBuiltinsAssembler::NextSkipHoles(
Node* table, Node* index, Label* if_end) {
// Compute the used capacity for the {table}.
Node* number_of_buckets =
LoadAndUntagObjectField(table, TableType::kNumberOfBucketsOffset);
Node* number_of_elements =
LoadAndUntagObjectField(table, TableType::kNumberOfElementsOffset);
Node* number_of_deleted_elements =
LoadAndUntagObjectField(table, TableType::kNumberOfDeletedElementsOffset);
Node* used_capacity =
IntPtrAdd(number_of_elements, number_of_deleted_elements);
Node* entry_key;
Node* entry_start_position;
VARIABLE(var_index, MachineType::PointerRepresentation(), index);
Label loop(this, &var_index), done_loop(this);
Goto(&loop);
BIND(&loop);
{
GotoIfNot(IntPtrLessThan(var_index.value(), used_capacity), if_end);
entry_start_position = IntPtrAdd(
IntPtrMul(var_index.value(), IntPtrConstant(TableType::kEntrySize)),
number_of_buckets);
entry_key =
LoadFixedArrayElement(table, entry_start_position,
TableType::kHashTableStartIndex * kPointerSize);
Increment(&var_index);
Branch(IsTheHole(entry_key), &loop, &done_loop);
}
BIND(&done_loop);
return std::tuple<Node*, Node*, Node*>(entry_key, entry_start_position,
var_index.value());
}
TF_BUILTIN(MapGet, CollectionsBuiltinsAssembler) {
Node* const receiver = Parameter(Descriptor::kReceiver);
Node* const key = Parameter(Descriptor::kKey);
Node* const context = Parameter(Descriptor::kContext);
ThrowIfNotInstanceType(context, receiver, JS_MAP_TYPE, "Map.prototype.get");
Node* const table = LoadObjectField(receiver, JSMap::kTableOffset);
Node* index = CallBuiltin(Builtins::kMapLookupHashIndex, context, table, key);
Label if_found(this), if_not_found(this);
Branch(SmiGreaterThanOrEqual(index, SmiConstant(0)), &if_found,
&if_not_found);
BIND(&if_found);
Return(LoadFixedArrayElement(table, SmiUntag(index)));
BIND(&if_not_found);
Return(UndefinedConstant());
}
TF_BUILTIN(MapHas, CollectionsBuiltinsAssembler) {
Node* const receiver = Parameter(Descriptor::kReceiver);
Node* const key = Parameter(Descriptor::kKey);
Node* const context = Parameter(Descriptor::kContext);
ThrowIfNotInstanceType(context, receiver, JS_MAP_TYPE, "Map.prototype.has");
Node* const table = LoadObjectField(receiver, JSMap::kTableOffset);
Node* index = CallBuiltin(Builtins::kMapLookupHashIndex, context, table, key);
Label if_found(this), if_not_found(this);
Branch(SmiGreaterThanOrEqual(index, SmiConstant(0)), &if_found,
&if_not_found);
BIND(&if_found);
Return(TrueConstant());
BIND(&if_not_found);
Return(FalseConstant());
}
Node* CollectionsBuiltinsAssembler::NormalizeNumberKey(Node* const key) {
VARIABLE(result, MachineRepresentation::kTagged, key);
Label done(this);
GotoIf(TaggedIsSmi(key), &done);
GotoIfNot(IsHeapNumber(key), &done);
Node* const number = LoadHeapNumberValue(key);
GotoIfNot(Float64Equal(number, Float64Constant(0.0)), &done);
// We know the value is zero, so we take the key to be Smi 0.
// Another option would be to normalize to Smi here.
result.Bind(SmiConstant(0));
Goto(&done);
BIND(&done);
return result.value();
}
TF_BUILTIN(MapSet, CollectionsBuiltinsAssembler) {
Node* const receiver = Parameter(Descriptor::kReceiver);
Node* key = Parameter(Descriptor::kKey);
Node* const value = Parameter(Descriptor::kValue);
Node* const context = Parameter(Descriptor::kContext);
ThrowIfNotInstanceType(context, receiver, JS_MAP_TYPE, "Map.prototype.set");
key = NormalizeNumberKey(key);
Node* const table = LoadObjectField(receiver, JSMap::kTableOffset);
VARIABLE(entry_start_position_or_hash, MachineType::PointerRepresentation(),
IntPtrConstant(0));
Label entry_found(this), not_found(this);
TryLookupOrderedHashTableIndex<OrderedHashMap>(table, key, context,
&entry_start_position_or_hash,
&entry_found, ¬_found);
BIND(&entry_found);
// If we found the entry, we just store the value there.
StoreFixedArrayElement(table, entry_start_position_or_hash.value(), value,
UPDATE_WRITE_BARRIER,
kPointerSize * (OrderedHashMap::kHashTableStartIndex +
OrderedHashMap::kValueOffset));
Return(receiver);
Label no_hash(this), add_entry(this), store_new_entry(this);
BIND(¬_found);
{
// If we have a hash code, we can start adding the new entry.
GotoIf(IntPtrGreaterThanOrEqual(entry_start_position_or_hash.value(),
IntPtrConstant(0)),
&add_entry);
// Otherwise, go to runtime to compute the hash code.
entry_start_position_or_hash.Bind(SmiUntag(CallGetOrCreateHashRaw(key)));
Goto(&add_entry);
}
BIND(&add_entry);
VARIABLE(number_of_buckets, MachineType::PointerRepresentation());
VARIABLE(occupancy, MachineType::PointerRepresentation());
VARIABLE(table_var, MachineRepresentation::kTaggedPointer, table);
{
// Check we have enough space for the entry.
number_of_buckets.Bind(SmiUntag(
LoadFixedArrayElement(table, OrderedHashMap::kNumberOfBucketsIndex)));
STATIC_ASSERT(OrderedHashMap::kLoadFactor == 2);
Node* const capacity = WordShl(number_of_buckets.value(), 1);
Node* const number_of_elements = SmiUntag(
CAST(LoadObjectField(table, OrderedHashMap::kNumberOfElementsOffset)));
Node* const number_of_deleted = SmiUntag(CAST(LoadObjectField(
table, OrderedHashMap::kNumberOfDeletedElementsOffset)));
occupancy.Bind(IntPtrAdd(number_of_elements, number_of_deleted));
GotoIf(IntPtrLessThan(occupancy.value(), capacity), &store_new_entry);
// We do not have enough space, grow the table and reload the relevant
// fields.
CallRuntime(Runtime::kMapGrow, context, receiver);
table_var.Bind(LoadObjectField(receiver, JSMap::kTableOffset));
number_of_buckets.Bind(SmiUntag(LoadFixedArrayElement(
table_var.value(), OrderedHashMap::kNumberOfBucketsIndex)));
Node* const new_number_of_elements = SmiUntag(CAST(LoadObjectField(
table_var.value(), OrderedHashMap::kNumberOfElementsOffset)));
Node* const new_number_of_deleted = SmiUntag(CAST(LoadObjectField(
table_var.value(), OrderedHashMap::kNumberOfDeletedElementsOffset)));
occupancy.Bind(IntPtrAdd(new_number_of_elements, new_number_of_deleted));
Goto(&store_new_entry);
}
BIND(&store_new_entry);
// Store the key, value and connect the element to the bucket chain.
StoreOrderedHashMapNewEntry(table_var.value(), key, value,
entry_start_position_or_hash.value(),
number_of_buckets.value(), occupancy.value());
Return(receiver);
}
void CollectionsBuiltinsAssembler::StoreOrderedHashMapNewEntry(
Node* const table, Node* const key, Node* const value, Node* const hash,
Node* const number_of_buckets, Node* const occupancy) {
Node* const bucket =
WordAnd(hash, IntPtrSub(number_of_buckets, IntPtrConstant(1)));
Node* const bucket_entry = LoadFixedArrayElement(
table, bucket, OrderedHashMap::kHashTableStartIndex * kPointerSize);
// Store the entry elements.
Node* const entry_start = IntPtrAdd(
IntPtrMul(occupancy, IntPtrConstant(OrderedHashMap::kEntrySize)),
number_of_buckets);
StoreFixedArrayElement(table, entry_start, key, UPDATE_WRITE_BARRIER,
kPointerSize * OrderedHashMap::kHashTableStartIndex);
StoreFixedArrayElement(table, entry_start, value, UPDATE_WRITE_BARRIER,
kPointerSize * (OrderedHashMap::kHashTableStartIndex +
OrderedHashMap::kValueOffset));
StoreFixedArrayElement(table, entry_start, bucket_entry, SKIP_WRITE_BARRIER,
kPointerSize * (OrderedHashMap::kHashTableStartIndex +
OrderedHashMap::kChainOffset));
// Update the bucket head.
StoreFixedArrayElement(table, bucket, SmiTag(occupancy), SKIP_WRITE_BARRIER,