mirrored from https://chromium.googlesource.com/v8/v8.git
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
builtins-string-gen.cc
1789 lines (1499 loc) · 68 KB
/
builtins-string-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-string-gen.h"
#include "src/base/strings.h"
#include "src/builtins/builtins-regexp-gen.h"
#include "src/builtins/builtins-utils-gen.h"
#include "src/builtins/builtins.h"
#include "src/codegen/code-factory.h"
#include "src/execution/protectors.h"
#include "src/heap/factory-inl.h"
#include "src/heap/heap-inl.h"
#include "src/logging/counters.h"
#include "src/objects/objects.h"
#include "src/objects/property-cell.h"
namespace v8 {
namespace internal {
TNode<RawPtrT> StringBuiltinsAssembler::DirectStringData(
TNode<String> string, TNode<Word32T> string_instance_type) {
// Compute the effective offset of the first character.
TVARIABLE(RawPtrT, var_data);
Label if_sequential(this), if_external(this), if_join(this);
Branch(Word32Equal(Word32And(string_instance_type,
Int32Constant(kStringRepresentationMask)),
Int32Constant(kSeqStringTag)),
&if_sequential, &if_external);
BIND(&if_sequential);
{
var_data = RawPtrAdd(
ReinterpretCast<RawPtrT>(BitcastTaggedToWord(string)),
IntPtrConstant(SeqOneByteString::kHeaderSize - kHeapObjectTag));
Goto(&if_join);
}
BIND(&if_external);
{
var_data = LoadExternalStringResourceDataPtr(CAST(string));
Goto(&if_join);
}
BIND(&if_join);
return var_data.value();
}
template <typename SubjectChar, typename PatternChar>
TNode<IntPtrT> StringBuiltinsAssembler::CallSearchStringRaw(
const TNode<RawPtrT> subject_ptr, const TNode<IntPtrT> subject_length,
const TNode<RawPtrT> search_ptr, const TNode<IntPtrT> search_length,
const TNode<IntPtrT> start_position) {
const TNode<ExternalReference> function_addr = ExternalConstant(
ExternalReference::search_string_raw<SubjectChar, PatternChar>());
const TNode<ExternalReference> isolate_ptr =
ExternalConstant(ExternalReference::isolate_address(isolate()));
MachineType type_ptr = MachineType::Pointer();
MachineType type_intptr = MachineType::IntPtr();
const TNode<IntPtrT> result = UncheckedCast<IntPtrT>(CallCFunction(
function_addr, type_intptr, std::make_pair(type_ptr, isolate_ptr),
std::make_pair(type_ptr, subject_ptr),
std::make_pair(type_intptr, subject_length),
std::make_pair(type_ptr, search_ptr),
std::make_pair(type_intptr, search_length),
std::make_pair(type_intptr, start_position)));
return result;
}
TNode<IntPtrT> StringBuiltinsAssembler::SearchOneByteStringInTwoByteString(
const TNode<RawPtrT> subject_ptr, const TNode<IntPtrT> subject_length,
const TNode<RawPtrT> search_ptr, const TNode<IntPtrT> search_length,
const TNode<IntPtrT> start_position) {
return CallSearchStringRaw<const base::uc16, const uint8_t>(
subject_ptr, subject_length, search_ptr, search_length, start_position);
}
TNode<IntPtrT> StringBuiltinsAssembler::SearchOneByteStringInOneByteString(
const TNode<RawPtrT> subject_ptr, const TNode<IntPtrT> subject_length,
const TNode<RawPtrT> search_ptr, const TNode<IntPtrT> search_length,
const TNode<IntPtrT> start_position) {
return CallSearchStringRaw<const uint8_t, const uint8_t>(
subject_ptr, subject_length, search_ptr, search_length, start_position);
}
TNode<IntPtrT> StringBuiltinsAssembler::SearchTwoByteStringInTwoByteString(
const TNode<RawPtrT> subject_ptr, const TNode<IntPtrT> subject_length,
const TNode<RawPtrT> search_ptr, const TNode<IntPtrT> search_length,
const TNode<IntPtrT> start_position) {
return CallSearchStringRaw<const base::uc16, const base::uc16>(
subject_ptr, subject_length, search_ptr, search_length, start_position);
}
TNode<IntPtrT> StringBuiltinsAssembler::SearchTwoByteStringInOneByteString(
const TNode<RawPtrT> subject_ptr, const TNode<IntPtrT> subject_length,
const TNode<RawPtrT> search_ptr, const TNode<IntPtrT> search_length,
const TNode<IntPtrT> start_position) {
return CallSearchStringRaw<const uint8_t, const base::uc16>(
subject_ptr, subject_length, search_ptr, search_length, start_position);
}
TNode<IntPtrT> StringBuiltinsAssembler::SearchOneByteInOneByteString(
const TNode<RawPtrT> subject_ptr, const TNode<IntPtrT> subject_length,
const TNode<RawPtrT> search_ptr, const TNode<IntPtrT> start_position) {
const TNode<RawPtrT> subject_start_ptr =
RawPtrAdd(subject_ptr, start_position);
const TNode<IntPtrT> search_byte =
ChangeInt32ToIntPtr(Load<Uint8T>(search_ptr));
const TNode<UintPtrT> search_length =
Unsigned(IntPtrSub(subject_length, start_position));
const TNode<ExternalReference> memchr =
ExternalConstant(ExternalReference::libc_memchr_function());
const TNode<RawPtrT> result_address = UncheckedCast<RawPtrT>(
CallCFunction(memchr, MachineType::Pointer(),
std::make_pair(MachineType::Pointer(), subject_start_ptr),
std::make_pair(MachineType::IntPtr(), search_byte),
std::make_pair(MachineType::UintPtr(), search_length)));
return Select<IntPtrT>(
WordEqual(result_address, IntPtrConstant(0)),
[=] { return IntPtrConstant(-1); },
[=] {
return IntPtrAdd(RawPtrSub(result_address, subject_start_ptr),
start_position);
});
}
void StringBuiltinsAssembler::GenerateStringEqual(TNode<String> left,
TNode<String> right) {
TVARIABLE(String, var_left, left);
TVARIABLE(String, var_right, right);
Label if_equal(this), if_notequal(this), if_indirect(this, Label::kDeferred),
restart(this, {&var_left, &var_right});
TNode<IntPtrT> lhs_length = LoadStringLengthAsWord(left);
TNode<IntPtrT> rhs_length = LoadStringLengthAsWord(right);
// Strings with different lengths cannot be equal.
GotoIf(WordNotEqual(lhs_length, rhs_length), &if_notequal);
Goto(&restart);
BIND(&restart);
TNode<String> lhs = var_left.value();
TNode<String> rhs = var_right.value();
TNode<Uint16T> lhs_instance_type = LoadInstanceType(lhs);
TNode<Uint16T> rhs_instance_type = LoadInstanceType(rhs);
StringEqual_Core(lhs, lhs_instance_type, rhs, rhs_instance_type, lhs_length,
&if_equal, &if_notequal, &if_indirect);
BIND(&if_indirect);
{
// Try to unwrap indirect strings, restart the above attempt on success.
MaybeDerefIndirectStrings(&var_left, lhs_instance_type, &var_right,
rhs_instance_type, &restart);
TailCallRuntime(Runtime::kStringEqual, NoContextConstant(), lhs, rhs);
}
BIND(&if_equal);
Return(TrueConstant());
BIND(&if_notequal);
Return(FalseConstant());
}
void StringBuiltinsAssembler::StringEqual_Core(
TNode<String> lhs, TNode<Word32T> lhs_instance_type, TNode<String> rhs,
TNode<Word32T> rhs_instance_type, TNode<IntPtrT> length, Label* if_equal,
Label* if_not_equal, Label* if_indirect) {
CSA_DCHECK(this, WordEqual(LoadStringLengthAsWord(lhs), length));
CSA_DCHECK(this, WordEqual(LoadStringLengthAsWord(rhs), length));
// Fast check to see if {lhs} and {rhs} refer to the same String object.
GotoIf(TaggedEqual(lhs, rhs), if_equal);
// Combine the instance types into a single 16-bit value, so we can check
// both of them at once.
TNode<Word32T> both_instance_types = Word32Or(
lhs_instance_type, Word32Shl(rhs_instance_type, Int32Constant(8)));
// Check if both {lhs} and {rhs} are internalized. Since we already know
// that they're not the same object, they're not equal in that case.
int const kBothInternalizedMask =
kIsNotInternalizedMask | (kIsNotInternalizedMask << 8);
int const kBothInternalizedTag = kInternalizedTag | (kInternalizedTag << 8);
GotoIf(Word32Equal(Word32And(both_instance_types,
Int32Constant(kBothInternalizedMask)),
Int32Constant(kBothInternalizedTag)),
if_not_equal);
// Check if both {lhs} and {rhs} are direct strings, and that in case of
// ExternalStrings the data pointer is cached.
STATIC_ASSERT(kUncachedExternalStringTag != 0);
STATIC_ASSERT(kIsIndirectStringTag != 0);
int const kBothDirectStringMask =
kIsIndirectStringMask | kUncachedExternalStringMask |
((kIsIndirectStringMask | kUncachedExternalStringMask) << 8);
GotoIfNot(Word32Equal(Word32And(both_instance_types,
Int32Constant(kBothDirectStringMask)),
Int32Constant(0)),
if_indirect);
// Dispatch based on the {lhs} and {rhs} string encoding.
int const kBothStringEncodingMask =
kStringEncodingMask | (kStringEncodingMask << 8);
int const kOneOneByteStringTag = kOneByteStringTag | (kOneByteStringTag << 8);
int const kTwoTwoByteStringTag = kTwoByteStringTag | (kTwoByteStringTag << 8);
int const kOneTwoByteStringTag = kOneByteStringTag | (kTwoByteStringTag << 8);
Label if_oneonebytestring(this), if_twotwobytestring(this),
if_onetwobytestring(this), if_twoonebytestring(this);
TNode<Word32T> masked_instance_types =
Word32And(both_instance_types, Int32Constant(kBothStringEncodingMask));
GotoIf(
Word32Equal(masked_instance_types, Int32Constant(kOneOneByteStringTag)),
&if_oneonebytestring);
GotoIf(
Word32Equal(masked_instance_types, Int32Constant(kTwoTwoByteStringTag)),
&if_twotwobytestring);
Branch(
Word32Equal(masked_instance_types, Int32Constant(kOneTwoByteStringTag)),
&if_onetwobytestring, &if_twoonebytestring);
BIND(&if_oneonebytestring);
StringEqual_Loop(lhs, lhs_instance_type, MachineType::Uint8(), rhs,
rhs_instance_type, MachineType::Uint8(), length, if_equal,
if_not_equal);
BIND(&if_twotwobytestring);
StringEqual_Loop(lhs, lhs_instance_type, MachineType::Uint16(), rhs,
rhs_instance_type, MachineType::Uint16(), length, if_equal,
if_not_equal);
BIND(&if_onetwobytestring);
StringEqual_Loop(lhs, lhs_instance_type, MachineType::Uint8(), rhs,
rhs_instance_type, MachineType::Uint16(), length, if_equal,
if_not_equal);
BIND(&if_twoonebytestring);
StringEqual_Loop(lhs, lhs_instance_type, MachineType::Uint16(), rhs,
rhs_instance_type, MachineType::Uint8(), length, if_equal,
if_not_equal);
}
void StringBuiltinsAssembler::StringEqual_Loop(
TNode<String> lhs, TNode<Word32T> lhs_instance_type, MachineType lhs_type,
TNode<String> rhs, TNode<Word32T> rhs_instance_type, MachineType rhs_type,
TNode<IntPtrT> length, Label* if_equal, Label* if_not_equal) {
CSA_DCHECK(this, WordEqual(LoadStringLengthAsWord(lhs), length));
CSA_DCHECK(this, WordEqual(LoadStringLengthAsWord(rhs), length));
// Compute the effective offset of the first character.
TNode<RawPtrT> lhs_data = DirectStringData(lhs, lhs_instance_type);
TNode<RawPtrT> rhs_data = DirectStringData(rhs, rhs_instance_type);
// Loop over the {lhs} and {rhs} strings to see if they are equal.
TVARIABLE(IntPtrT, var_offset, IntPtrConstant(0));
Label loop(this, &var_offset);
Goto(&loop);
BIND(&loop);
{
// If {offset} equals {end}, no difference was found, so the
// strings are equal.
GotoIf(WordEqual(var_offset.value(), length), if_equal);
// Load the next characters from {lhs} and {rhs}.
TNode<Word32T> lhs_value = UncheckedCast<Word32T>(
Load(lhs_type, lhs_data,
WordShl(var_offset.value(),
ElementSizeLog2Of(lhs_type.representation()))));
TNode<Word32T> rhs_value = UncheckedCast<Word32T>(
Load(rhs_type, rhs_data,
WordShl(var_offset.value(),
ElementSizeLog2Of(rhs_type.representation()))));
// Check if the characters match.
GotoIf(Word32NotEqual(lhs_value, rhs_value), if_not_equal);
// Advance to next character.
var_offset = IntPtrAdd(var_offset.value(), IntPtrConstant(1));
Goto(&loop);
}
}
TNode<String> StringBuiltinsAssembler::StringFromSingleUTF16EncodedCodePoint(
TNode<Int32T> codepoint) {
TVARIABLE(String, var_result, EmptyStringConstant());
Label if_isword16(this), if_isword32(this), return_result(this);
Branch(Uint32LessThan(codepoint, Int32Constant(0x10000)), &if_isword16,
&if_isword32);
BIND(&if_isword16);
{
var_result = StringFromSingleCharCode(codepoint);
Goto(&return_result);
}
BIND(&if_isword32);
{
TNode<String> value = AllocateSeqTwoByteString(2);
StoreNoWriteBarrier(
MachineRepresentation::kWord32, value,
IntPtrConstant(SeqTwoByteString::kHeaderSize - kHeapObjectTag),
codepoint);
var_result = value;
Goto(&return_result);
}
BIND(&return_result);
return var_result.value();
}
TNode<String> StringBuiltinsAssembler::AllocateConsString(TNode<Uint32T> length,
TNode<String> left,
TNode<String> right) {
// Added string can be a cons string.
Comment("Allocating ConsString");
TNode<Int32T> left_instance_type = LoadInstanceType(left);
TNode<Int32T> right_instance_type = LoadInstanceType(right);
// Determine the resulting ConsString map to use depending on whether
// any of {left} or {right} has two byte encoding.
STATIC_ASSERT(kOneByteStringTag != 0);
STATIC_ASSERT(kTwoByteStringTag == 0);
TNode<Int32T> combined_instance_type =
Word32And(left_instance_type, right_instance_type);
TNode<Map> result_map = CAST(Select<Object>(
IsSetWord32(combined_instance_type, kStringEncodingMask),
[=] { return ConsOneByteStringMapConstant(); },
[=] { return ConsStringMapConstant(); }));
TNode<HeapObject> result = AllocateInNewSpace(ConsString::kSize);
StoreMapNoWriteBarrier(result, result_map);
StoreObjectFieldNoWriteBarrier(result, ConsString::kLengthOffset, length);
StoreObjectFieldNoWriteBarrier(result, ConsString::kRawHashFieldOffset,
Int32Constant(String::kEmptyHashField));
StoreObjectFieldNoWriteBarrier(result, ConsString::kFirstOffset, left);
StoreObjectFieldNoWriteBarrier(result, ConsString::kSecondOffset, right);
return CAST(result);
}
TNode<String> StringBuiltinsAssembler::StringAdd(
TNode<ContextOrEmptyContext> context, TNode<String> left,
TNode<String> right) {
CSA_DCHECK(this, IsZeroOrContext(context));
TVARIABLE(String, result);
Label check_right(this), runtime(this, Label::kDeferred), cons(this),
done(this, &result), done_native(this, &result);
Counters* counters = isolate()->counters();
TNode<Uint32T> left_length = LoadStringLengthAsWord32(left);
GotoIfNot(Word32Equal(left_length, Uint32Constant(0)), &check_right);
result = right;
Goto(&done_native);
BIND(&check_right);
TNode<Uint32T> right_length = LoadStringLengthAsWord32(right);
GotoIfNot(Word32Equal(right_length, Uint32Constant(0)), &cons);
result = left;
Goto(&done_native);
BIND(&cons);
{
TNode<Uint32T> new_length = Uint32Add(left_length, right_length);
// If new length is greater than String::kMaxLength, goto runtime to
// throw. Note: we also need to invalidate the string length protector, so
// can't just throw here directly.
GotoIf(Uint32GreaterThan(new_length, Uint32Constant(String::kMaxLength)),
&runtime);
TVARIABLE(String, var_left, left);
TVARIABLE(String, var_right, right);
Label non_cons(this, {&var_left, &var_right});
Label slow(this, Label::kDeferred);
GotoIf(Uint32LessThan(new_length, Uint32Constant(ConsString::kMinLength)),
&non_cons);
result =
AllocateConsString(new_length, var_left.value(), var_right.value());
Goto(&done_native);
BIND(&non_cons);
Comment("Full string concatenate");
TNode<Int32T> left_instance_type = LoadInstanceType(var_left.value());
TNode<Int32T> right_instance_type = LoadInstanceType(var_right.value());
// Compute intersection and difference of instance types.
TNode<Int32T> ored_instance_types =
Word32Or(left_instance_type, right_instance_type);
TNode<Word32T> xored_instance_types =
Word32Xor(left_instance_type, right_instance_type);
// Check if both strings have the same encoding and both are sequential.
GotoIf(IsSetWord32(xored_instance_types, kStringEncodingMask), &runtime);
GotoIf(IsSetWord32(ored_instance_types, kStringRepresentationMask), &slow);
TNode<IntPtrT> word_left_length = Signed(ChangeUint32ToWord(left_length));
TNode<IntPtrT> word_right_length = Signed(ChangeUint32ToWord(right_length));
Label two_byte(this);
GotoIf(Word32Equal(Word32And(ored_instance_types,
Int32Constant(kStringEncodingMask)),
Int32Constant(kTwoByteStringTag)),
&two_byte);
// One-byte sequential string case
result = AllocateSeqOneByteString(new_length);
CopyStringCharacters(var_left.value(), result.value(), IntPtrConstant(0),
IntPtrConstant(0), word_left_length,
String::ONE_BYTE_ENCODING, String::ONE_BYTE_ENCODING);
CopyStringCharacters(var_right.value(), result.value(), IntPtrConstant(0),
word_left_length, word_right_length,
String::ONE_BYTE_ENCODING, String::ONE_BYTE_ENCODING);
Goto(&done_native);
BIND(&two_byte);
{
// Two-byte sequential string case
result = AllocateSeqTwoByteString(new_length);
CopyStringCharacters(var_left.value(), result.value(), IntPtrConstant(0),
IntPtrConstant(0), word_left_length,
String::TWO_BYTE_ENCODING,
String::TWO_BYTE_ENCODING);
CopyStringCharacters(var_right.value(), result.value(), IntPtrConstant(0),
word_left_length, word_right_length,
String::TWO_BYTE_ENCODING,
String::TWO_BYTE_ENCODING);
Goto(&done_native);
}
BIND(&slow);
{
// Try to unwrap indirect strings, restart the above attempt on success.
MaybeDerefIndirectStrings(&var_left, left_instance_type, &var_right,
right_instance_type, &non_cons);
Goto(&runtime);
}
}
BIND(&runtime);
{
result = CAST(CallRuntime(Runtime::kStringAdd, context, left, right));
Goto(&done);
}
BIND(&done_native);
{
IncrementCounter(counters->string_add_native(), 1);
Goto(&done);
}
BIND(&done);
return result.value();
}
void StringBuiltinsAssembler::BranchIfCanDerefIndirectString(
TNode<String> string, TNode<Int32T> instance_type, Label* can_deref,
Label* cannot_deref) {
TNode<Int32T> representation =
Word32And(instance_type, Int32Constant(kStringRepresentationMask));
GotoIf(Word32Equal(representation, Int32Constant(kThinStringTag)), can_deref);
GotoIf(Word32NotEqual(representation, Int32Constant(kConsStringTag)),
cannot_deref);
// Cons string.
TNode<String> rhs =
LoadObjectField<String>(string, ConsString::kSecondOffset);
GotoIf(IsEmptyString(rhs), can_deref);
Goto(cannot_deref);
}
void StringBuiltinsAssembler::DerefIndirectString(TVariable<String>* var_string,
TNode<Int32T> instance_type) {
#ifdef DEBUG
Label can_deref(this), cannot_deref(this);
BranchIfCanDerefIndirectString(var_string->value(), instance_type, &can_deref,
&cannot_deref);
BIND(&cannot_deref);
DebugBreak(); // Should be able to dereference string.
Goto(&can_deref);
BIND(&can_deref);
#endif // DEBUG
STATIC_ASSERT(static_cast<int>(ThinString::kActualOffset) ==
static_cast<int>(ConsString::kFirstOffset));
*var_string =
LoadObjectField<String>(var_string->value(), ThinString::kActualOffset);
}
void StringBuiltinsAssembler::MaybeDerefIndirectString(
TVariable<String>* var_string, TNode<Int32T> instance_type,
Label* did_deref, Label* cannot_deref) {
Label deref(this);
BranchIfCanDerefIndirectString(var_string->value(), instance_type, &deref,
cannot_deref);
BIND(&deref);
{
DerefIndirectString(var_string, instance_type);
Goto(did_deref);
}
}
void StringBuiltinsAssembler::MaybeDerefIndirectStrings(
TVariable<String>* var_left, TNode<Int32T> left_instance_type,
TVariable<String>* var_right, TNode<Int32T> right_instance_type,
Label* did_something) {
Label did_nothing_left(this), did_something_left(this),
didnt_do_anything(this);
MaybeDerefIndirectString(var_left, left_instance_type, &did_something_left,
&did_nothing_left);
BIND(&did_something_left);
{
MaybeDerefIndirectString(var_right, right_instance_type, did_something,
did_something);
}
BIND(&did_nothing_left);
{
MaybeDerefIndirectString(var_right, right_instance_type, did_something,
&didnt_do_anything);
}
BIND(&didnt_do_anything);
// Fall through if neither string was an indirect string.
}
TNode<String> StringBuiltinsAssembler::DerefIndirectString(
TNode<String> string, TNode<Int32T> instance_type, Label* cannot_deref) {
Label deref(this);
BranchIfCanDerefIndirectString(string, instance_type, &deref, cannot_deref);
BIND(&deref);
STATIC_ASSERT(static_cast<int>(ThinString::kActualOffset) ==
static_cast<int>(ConsString::kFirstOffset));
return LoadObjectField<String>(string, ThinString::kActualOffset);
}
TF_BUILTIN(StringAdd_CheckNone, StringBuiltinsAssembler) {
auto left = Parameter<String>(Descriptor::kLeft);
auto right = Parameter<String>(Descriptor::kRight);
TNode<ContextOrEmptyContext> context =
UncheckedParameter<ContextOrEmptyContext>(Descriptor::kContext);
CSA_DCHECK(this, IsZeroOrContext(context));
Return(StringAdd(context, left, right));
}
TF_BUILTIN(SubString, StringBuiltinsAssembler) {
auto string = Parameter<String>(Descriptor::kString);
auto from = Parameter<Smi>(Descriptor::kFrom);
auto to = Parameter<Smi>(Descriptor::kTo);
Return(SubString(string, SmiUntag(from), SmiUntag(to)));
}
void StringBuiltinsAssembler::GenerateStringRelationalComparison(
TNode<String> left, TNode<String> right, Operation op) {
TVARIABLE(String, var_left, left);
TVARIABLE(String, var_right, right);
Label if_less(this), if_equal(this), if_greater(this);
Label restart(this, {&var_left, &var_right});
Goto(&restart);
BIND(&restart);
TNode<String> lhs = var_left.value();
TNode<String> rhs = var_right.value();
// Fast check to see if {lhs} and {rhs} refer to the same String object.
GotoIf(TaggedEqual(lhs, rhs), &if_equal);
// Load instance types of {lhs} and {rhs}.
TNode<Uint16T> lhs_instance_type = LoadInstanceType(lhs);
TNode<Uint16T> rhs_instance_type = LoadInstanceType(rhs);
// Combine the instance types into a single 16-bit value, so we can check
// both of them at once.
TNode<Int32T> both_instance_types = Word32Or(
lhs_instance_type, Word32Shl(rhs_instance_type, Int32Constant(8)));
// Check that both {lhs} and {rhs} are flat one-byte strings.
int const kBothSeqOneByteStringMask =
kStringEncodingMask | kStringRepresentationMask |
((kStringEncodingMask | kStringRepresentationMask) << 8);
int const kBothSeqOneByteStringTag =
kOneByteStringTag | kSeqStringTag |
((kOneByteStringTag | kSeqStringTag) << 8);
Label if_bothonebyteseqstrings(this), if_notbothonebyteseqstrings(this);
Branch(Word32Equal(Word32And(both_instance_types,
Int32Constant(kBothSeqOneByteStringMask)),
Int32Constant(kBothSeqOneByteStringTag)),
&if_bothonebyteseqstrings, &if_notbothonebyteseqstrings);
BIND(&if_bothonebyteseqstrings);
{
// Load the length of {lhs} and {rhs}.
TNode<IntPtrT> lhs_length = LoadStringLengthAsWord(lhs);
TNode<IntPtrT> rhs_length = LoadStringLengthAsWord(rhs);
// Determine the minimum length.
TNode<IntPtrT> length = IntPtrMin(lhs_length, rhs_length);
// Compute the effective offset of the first character.
TNode<IntPtrT> begin =
IntPtrConstant(SeqOneByteString::kHeaderSize - kHeapObjectTag);
// Compute the first offset after the string from the length.
TNode<IntPtrT> end = IntPtrAdd(begin, length);
// Loop over the {lhs} and {rhs} strings to see if they are equal.
TVARIABLE(IntPtrT, var_offset, begin);
Label loop(this, &var_offset);
Goto(&loop);
BIND(&loop);
{
// Check if {offset} equals {end}.
Label if_done(this), if_notdone(this);
Branch(WordEqual(var_offset.value(), end), &if_done, &if_notdone);
BIND(&if_notdone);
{
// Load the next characters from {lhs} and {rhs}.
TNode<Uint8T> lhs_value = Load<Uint8T>(lhs, var_offset.value());
TNode<Uint8T> rhs_value = Load<Uint8T>(rhs, var_offset.value());
// Check if the characters match.
Label if_valueissame(this), if_valueisnotsame(this);
Branch(Word32Equal(lhs_value, rhs_value), &if_valueissame,
&if_valueisnotsame);
BIND(&if_valueissame);
{
// Advance to next character.
var_offset = IntPtrAdd(var_offset.value(), IntPtrConstant(1));
}
Goto(&loop);
BIND(&if_valueisnotsame);
Branch(Uint32LessThan(lhs_value, rhs_value), &if_less, &if_greater);
}
BIND(&if_done);
{
// All characters up to the min length are equal, decide based on
// string length.
GotoIf(IntPtrEqual(lhs_length, rhs_length), &if_equal);
Branch(IntPtrLessThan(lhs_length, rhs_length), &if_less, &if_greater);
}
}
}
BIND(&if_notbothonebyteseqstrings);
{
// Try to unwrap indirect strings, restart the above attempt on success.
MaybeDerefIndirectStrings(&var_left, lhs_instance_type, &var_right,
rhs_instance_type, &restart);
// TODO(bmeurer): Add support for two byte string relational comparisons.
switch (op) {
case Operation::kLessThan:
TailCallRuntime(Runtime::kStringLessThan, NoContextConstant(), lhs,
rhs);
break;
case Operation::kLessThanOrEqual:
TailCallRuntime(Runtime::kStringLessThanOrEqual, NoContextConstant(),
lhs, rhs);
break;
case Operation::kGreaterThan:
TailCallRuntime(Runtime::kStringGreaterThan, NoContextConstant(), lhs,
rhs);
break;
case Operation::kGreaterThanOrEqual:
TailCallRuntime(Runtime::kStringGreaterThanOrEqual, NoContextConstant(),
lhs, rhs);
break;
default:
UNREACHABLE();
}
}
BIND(&if_less);
switch (op) {
case Operation::kLessThan:
case Operation::kLessThanOrEqual:
Return(TrueConstant());
break;
case Operation::kGreaterThan:
case Operation::kGreaterThanOrEqual:
Return(FalseConstant());
break;
default:
UNREACHABLE();
}
BIND(&if_equal);
switch (op) {
case Operation::kLessThan:
case Operation::kGreaterThan:
Return(FalseConstant());
break;
case Operation::kLessThanOrEqual:
case Operation::kGreaterThanOrEqual:
Return(TrueConstant());
break;
default:
UNREACHABLE();
}
BIND(&if_greater);
switch (op) {
case Operation::kLessThan:
case Operation::kLessThanOrEqual:
Return(FalseConstant());
break;
case Operation::kGreaterThan:
case Operation::kGreaterThanOrEqual:
Return(TrueConstant());
break;
default:
UNREACHABLE();
}
}
TF_BUILTIN(StringEqual, StringBuiltinsAssembler) {
auto left = Parameter<String>(Descriptor::kLeft);
auto right = Parameter<String>(Descriptor::kRight);
GenerateStringEqual(left, right);
}
TF_BUILTIN(StringLessThan, StringBuiltinsAssembler) {
auto left = Parameter<String>(Descriptor::kLeft);
auto right = Parameter<String>(Descriptor::kRight);
GenerateStringRelationalComparison(left, right, Operation::kLessThan);
}
TF_BUILTIN(StringLessThanOrEqual, StringBuiltinsAssembler) {
auto left = Parameter<String>(Descriptor::kLeft);
auto right = Parameter<String>(Descriptor::kRight);
GenerateStringRelationalComparison(left, right, Operation::kLessThanOrEqual);
}
TF_BUILTIN(StringGreaterThan, StringBuiltinsAssembler) {
auto left = Parameter<String>(Descriptor::kLeft);
auto right = Parameter<String>(Descriptor::kRight);
GenerateStringRelationalComparison(left, right, Operation::kGreaterThan);
}
TF_BUILTIN(StringGreaterThanOrEqual, StringBuiltinsAssembler) {
auto left = Parameter<String>(Descriptor::kLeft);
auto right = Parameter<String>(Descriptor::kRight);
GenerateStringRelationalComparison(left, right,
Operation::kGreaterThanOrEqual);
}
TF_BUILTIN(StringCodePointAt, StringBuiltinsAssembler) {
auto receiver = Parameter<String>(Descriptor::kReceiver);
auto position = UncheckedParameter<IntPtrT>(Descriptor::kPosition);
// TODO(sigurds) Figure out if passing length as argument pays off.
TNode<IntPtrT> length = LoadStringLengthAsWord(receiver);
// Load the character code at the {position} from the {receiver}.
TNode<Int32T> code =
LoadSurrogatePairAt(receiver, length, position, UnicodeEncoding::UTF32);
// And return it as TaggedSigned value.
// TODO(turbofan): Allow builtins to return values untagged.
TNode<Smi> result = SmiFromInt32(code);
Return(result);
}
TF_BUILTIN(StringFromCodePointAt, StringBuiltinsAssembler) {
auto receiver = Parameter<String>(Descriptor::kReceiver);
auto position = UncheckedParameter<IntPtrT>(Descriptor::kPosition);
// TODO(sigurds) Figure out if passing length as argument pays off.
TNode<IntPtrT> length = LoadStringLengthAsWord(receiver);
// Load the character code at the {position} from the {receiver}.
TNode<Int32T> code =
LoadSurrogatePairAt(receiver, length, position, UnicodeEncoding::UTF16);
// Create a String from the UTF16 encoded code point
TNode<String> result = StringFromSingleUTF16EncodedCodePoint(code);
Return(result);
}
// -----------------------------------------------------------------------------
// ES6 section 21.1 String Objects
// ES6 #sec-string.fromcharcode
TF_BUILTIN(StringFromCharCode, StringBuiltinsAssembler) {
// TODO(ishell): use constants from Descriptor once the JSFunction linkage
// arguments are reordered.
auto argc = UncheckedParameter<Int32T>(Descriptor::kJSActualArgumentsCount);
auto context = Parameter<Context>(Descriptor::kContext);
CodeStubArguments arguments(this, argc);
TNode<Uint32T> unsigned_argc =
Unsigned(TruncateIntPtrToInt32(arguments.GetLengthWithoutReceiver()));
// Check if we have exactly one argument (plus the implicit receiver), i.e.
// if the parent frame is not an arguments adaptor frame.
Label if_oneargument(this), if_notoneargument(this);
Branch(IntPtrEqual(arguments.GetLengthWithoutReceiver(), IntPtrConstant(1)),
&if_oneargument, &if_notoneargument);
BIND(&if_oneargument);
{
// Single argument case, perform fast single character string cache lookup
// for one-byte code units, or fall back to creating a single character
// string on the fly otherwise.
TNode<Object> code = arguments.AtIndex(0);
TNode<Word32T> code32 = TruncateTaggedToWord32(context, code);
TNode<Int32T> code16 =
Signed(Word32And(code32, Int32Constant(String::kMaxUtf16CodeUnit)));
TNode<String> result = StringFromSingleCharCode(code16);
arguments.PopAndReturn(result);
}
TNode<Word32T> code16;
BIND(&if_notoneargument);
{
Label two_byte(this);
// Assume that the resulting string contains only one-byte characters.
TNode<String> one_byte_result = AllocateSeqOneByteString(unsigned_argc);
TVARIABLE(IntPtrT, var_max_index, IntPtrConstant(0));
// Iterate over the incoming arguments, converting them to 8-bit character
// codes. Stop if any of the conversions generates a code that doesn't fit
// in 8 bits.
CodeStubAssembler::VariableList vars({&var_max_index}, zone());
arguments.ForEach(vars, [&](TNode<Object> arg) {
TNode<Word32T> code32 = TruncateTaggedToWord32(context, arg);
code16 = Word32And(code32, Int32Constant(String::kMaxUtf16CodeUnit));
GotoIf(
Int32GreaterThan(code16, Int32Constant(String::kMaxOneByteCharCode)),
&two_byte);
// The {code16} fits into the SeqOneByteString {one_byte_result}.
TNode<IntPtrT> offset = ElementOffsetFromIndex(
var_max_index.value(), UINT8_ELEMENTS,
SeqOneByteString::kHeaderSize - kHeapObjectTag);
StoreNoWriteBarrier(MachineRepresentation::kWord8, one_byte_result,
offset, code16);
var_max_index = IntPtrAdd(var_max_index.value(), IntPtrConstant(1));
});
arguments.PopAndReturn(one_byte_result);
BIND(&two_byte);
// At least one of the characters in the string requires a 16-bit
// representation. Allocate a SeqTwoByteString to hold the resulting
// string.
TNode<String> two_byte_result = AllocateSeqTwoByteString(unsigned_argc);
// Copy the characters that have already been put in the 8-bit string into
// their corresponding positions in the new 16-bit string.
TNode<IntPtrT> zero = IntPtrConstant(0);
CopyStringCharacters(one_byte_result, two_byte_result, zero, zero,
var_max_index.value(), String::ONE_BYTE_ENCODING,
String::TWO_BYTE_ENCODING);
// Write the character that caused the 8-bit to 16-bit fault.
TNode<IntPtrT> max_index_offset =
ElementOffsetFromIndex(var_max_index.value(), UINT16_ELEMENTS,
SeqTwoByteString::kHeaderSize - kHeapObjectTag);
StoreNoWriteBarrier(MachineRepresentation::kWord16, two_byte_result,
max_index_offset, code16);
var_max_index = IntPtrAdd(var_max_index.value(), IntPtrConstant(1));
// Resume copying the passed-in arguments from the same place where the
// 8-bit copy stopped, but this time copying over all of the characters
// using a 16-bit representation.
arguments.ForEach(
vars,
[&](TNode<Object> arg) {
TNode<Word32T> code32 = TruncateTaggedToWord32(context, arg);
TNode<Word32T> code16 =
Word32And(code32, Int32Constant(String::kMaxUtf16CodeUnit));
TNode<IntPtrT> offset = ElementOffsetFromIndex(
var_max_index.value(), UINT16_ELEMENTS,
SeqTwoByteString::kHeaderSize - kHeapObjectTag);
StoreNoWriteBarrier(MachineRepresentation::kWord16, two_byte_result,
offset, code16);
var_max_index = IntPtrAdd(var_max_index.value(), IntPtrConstant(1));
},
var_max_index.value());
arguments.PopAndReturn(two_byte_result);
}
}
void StringBuiltinsAssembler::MaybeCallFunctionAtSymbol(
const TNode<Context> context, const TNode<Object> object,
const TNode<Object> maybe_string, Handle<Symbol> symbol,
DescriptorIndexNameValue additional_property_to_check,
const NodeFunction0& regexp_call, const NodeFunction1& generic_call) {
Label out(this);
Label get_property_lookup(this);
// Smis have to go through the GetProperty lookup in case Number.prototype or
// Object.prototype was modified.
GotoIf(TaggedIsSmi(object), &get_property_lookup);
// Take the fast path for RegExps.
// There's two conditions: {object} needs to be a fast regexp, and
// {maybe_string} must be a string (we can't call ToString on the fast path
// since it may mutate {object}).
{
Label stub_call(this), slow_lookup(this);
TNode<HeapObject> heap_object = CAST(object);
GotoIf(TaggedIsSmi(maybe_string), &slow_lookup);
GotoIfNot(IsString(CAST(maybe_string)), &slow_lookup);
// Note we don't run a full (= permissive) check here, because passing the
// check implies calling the fast variants of target builtins, which assume
// we've already made their appropriate fast path checks. This is not the
// case though; e.g.: some of the target builtins access flag getters.
// TODO(jgruber): Handle slow flag accesses on the fast path and make this
// permissive.
RegExpBuiltinsAssembler regexp_asm(state());
regexp_asm.BranchIfFastRegExp(
context, heap_object, LoadMap(heap_object),
PrototypeCheckAssembler::kCheckPrototypePropertyConstness,
additional_property_to_check, &stub_call, &slow_lookup);
BIND(&stub_call);
// TODO(jgruber): Add a no-JS scope once it exists.
regexp_call();
BIND(&slow_lookup);
// Special case null and undefined to skip the property lookup.
Branch(IsNullOrUndefined(heap_object), &out, &get_property_lookup);
}
// Fall back to a slow lookup of {heap_object[symbol]}.
//
// The spec uses GetMethod({heap_object}, {symbol}), which has a few quirks:
// * null values are turned into undefined, and
// * an exception is thrown if the value is not undefined, null, or callable.
// We handle the former by jumping to {out} for null values as well, while
// the latter is already handled by the Call({maybe_func}) operation.
BIND(&get_property_lookup);
const TNode<Object> maybe_func = GetProperty(context, object, symbol);
GotoIf(IsUndefined(maybe_func), &out);
GotoIf(IsNull(maybe_func), &out);
// Attempt to call the function.
generic_call(maybe_func);
BIND(&out);
}
const TNode<Smi> StringBuiltinsAssembler::IndexOfDollarChar(
const TNode<Context> context, const TNode<String> string) {
const TNode<String> dollar_string = HeapConstant(
isolate()->factory()->LookupSingleCharacterStringFromCode('$'));
const TNode<Smi> dollar_ix = CAST(CallBuiltin(
Builtin::kStringIndexOf, context, string, dollar_string, SmiConstant(0)));
return dollar_ix;
}
TNode<String> StringBuiltinsAssembler::GetSubstitution(
TNode<Context> context, TNode<String> subject_string,
TNode<Smi> match_start_index, TNode<Smi> match_end_index,
TNode<String> replace_string) {
CSA_DCHECK(this, TaggedIsPositiveSmi(match_start_index));
CSA_DCHECK(this, TaggedIsPositiveSmi(match_end_index));
TVARIABLE(String, var_result, replace_string);
Label runtime(this), out(this);
// In this primitive implementation we simply look for the next '$' char in
// {replace_string}. If it doesn't exist, we can simply return
// {replace_string} itself. If it does, then we delegate to
// String::GetSubstitution, passing in the index of the first '$' to avoid
// repeated scanning work.
// TODO(jgruber): Possibly extend this in the future to handle more complex
// cases without runtime calls.
const TNode<Smi> dollar_index = IndexOfDollarChar(context, replace_string);
Branch(SmiIsNegative(dollar_index), &out, &runtime);
BIND(&runtime);
{
CSA_DCHECK(this, TaggedIsPositiveSmi(dollar_index));
const TNode<Object> matched =
CallBuiltin(Builtin::kStringSubstring, context, subject_string,
SmiUntag(match_start_index), SmiUntag(match_end_index));
const TNode<String> replacement_string = CAST(
CallRuntime(Runtime::kGetSubstitution, context, matched, subject_string,
match_start_index, replace_string, dollar_index));
var_result = replacement_string;
Goto(&out);
}
BIND(&out);